summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Null/ShaderCache.h
blob: e9f23173a9c851d8494e380780c41183e2a47b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2015 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

#include <map>
#include <memory>

#include "VideoCommon/GeometryShaderGen.h"
#include "VideoCommon/PixelShaderGen.h"
#include "VideoCommon/VertexShaderGen.h"
#include "VideoCommon/VideoCommon.h"

namespace Null
{
template <typename Uid>
class ShaderCache
{
public:
  ShaderCache();
  virtual ~ShaderCache();

  void Clear();
  bool SetShader(DSTALPHA_MODE dst_alpha_mode, u32 primitive_type);

protected:
  virtual Uid GetUid(DSTALPHA_MODE dst_alpha_mode, u32 primitive_type, APIType api_type) = 0;
  virtual ShaderCode GenerateCode(DSTALPHA_MODE dst_alpha_mode, APIType api_type, Uid uid) = 0;

private:
  std::map<Uid, std::string> m_shaders;
  const std::string* m_last_entry = nullptr;
  Uid m_last_uid;
};

class VertexShaderCache : public ShaderCache<VertexShaderUid>
{
public:
  static std::unique_ptr<VertexShaderCache> s_instance;

protected:
  VertexShaderUid GetUid(DSTALPHA_MODE dst_alpha_mode, u32 primitive_type,
                         APIType api_type) override
  {
    return GetVertexShaderUid();
  }
  ShaderCode GenerateCode(DSTALPHA_MODE dst_alpha_mode, APIType api_type,
                          VertexShaderUid uid) override
  {
    return GenerateVertexShaderCode(api_type, uid.GetUidData());
  }
};

class GeometryShaderCache : public ShaderCache<GeometryShaderUid>
{
public:
  static std::unique_ptr<GeometryShaderCache> s_instance;

protected:
  GeometryShaderUid GetUid(DSTALPHA_MODE dst_alpha_mode, u32 primitive_type,
                           APIType api_type) override
  {
    return GetGeometryShaderUid(primitive_type);
  }
  ShaderCode GenerateCode(DSTALPHA_MODE dst_alpha_mode, APIType api_type,
                          GeometryShaderUid uid) override
  {
    return GenerateGeometryShaderCode(api_type, uid.GetUidData());
  }
};

class PixelShaderCache : public ShaderCache<PixelShaderUid>
{
public:
  static std::unique_ptr<PixelShaderCache> s_instance;

protected:
  PixelShaderUid GetUid(DSTALPHA_MODE dst_alpha_mode, u32 primitive_type, APIType api_type) override
  {
    return GetPixelShaderUid(dst_alpha_mode);
  }
  ShaderCode GenerateCode(DSTALPHA_MODE dst_alpha_mode, APIType api_type,
                          PixelShaderUid uid) override
  {
    return GeneratePixelShaderCode(dst_alpha_mode, api_type, uid.GetUidData());
  }
};

}  // namespace NULL