Debian Codegraph @

Source/Core/VideoBackends/OGL/ProgramShaderCache.h

raw symbols SCIP
1 // Copyright 2011 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <atomic>
7 #include <memory>
8 #include <mutex>
9 #include <string_view>
10 #include <unordered_map>
11
12 #include "Common/GL/GLExtensions/GLExtensions.h"
13 #include "VideoCommon/AsyncShaderCompiler.h"
14
15 namespace OGL
16 {
17 class OGLShader;
18 class GLVertexFormat;
19 class StreamBuffer;
20
21 struct SHADER
22 {
23 void Destroy()
24 {
25 DestroyShaders();
26 if (glprogid)
27 {
28 glDeleteProgram(glprogid);
29 glprogid = 0;
30 }
31 }
32
33 GLuint vsid = 0;
34 GLuint gsid = 0;
35 GLuint psid = 0;
36 GLuint glprogid = 0;
37
38 void SetProgramVariables();
39 void SetProgramBindings(bool is_compute);
40 void Bind() const;
41 void DestroyShaders();
42 };
43
44 struct PipelineProgramKey
45 {
46 u64 vertex_shader_id;
47 u64 geometry_shader_id;
48 u64 pixel_shader_id;
49
50 bool operator==(const PipelineProgramKey& rhs) const;
51 bool operator<(const PipelineProgramKey& rhs) const;
52 };
53
54 struct PipelineProgramKeyHash
55 {
56 std::size_t operator()(const PipelineProgramKey& key) const;
57 };
58
59 struct PipelineProgram
60 {
61 PipelineProgramKey key{};
62 SHADER shader;
63 std::atomic_size_t reference_count{1};
64 bool binary_retrieved = false;
65 };
66
67 class ProgramShaderCache
68 {
69 public:
70 static void BindVertexFormat(const GLVertexFormat* vertex_format);
71 static void ReBindVertexFormat();
72 static bool IsValidVertexFormatBound();
73 static void InvalidateVertexFormat();
74 static void InvalidateVertexFormatIfBound(GLuint vao);
75 static void InvalidateLastProgram();
76
77 static bool CompileComputeShader(SHADER& shader, std::string_view code);
78 static GLuint CompileSingleShader(GLenum type, std::string_view code);
79 static bool CheckShaderCompileResult(GLuint id, GLenum type, std::string_view code);
80 static bool CheckProgramLinkResult(GLuint id, std::string_view vcode, std::string_view pcode,
81 std::string_view gcode);
82 static StreamBuffer* GetUniformBuffer();
83 static u32 GetUniformBufferAlignment();
84 static void UploadConstants();
85 static void UploadConstants(const void* data, u32 data_size);
86
87 static void Init();
88 static void Shutdown();
89 static void CreateHeader();
90
91 // This counter increments with each shader object allocated, in order to give it a unique ID.
92 // Since the shaders can be destroyed after a pipeline is created, we can't use the shader pointer
93 // as a key for GL programs. For the same reason, we can't use the GL objects either. This ID is
94 // guaranteed to be unique for the emulation session, even if the memory allocator or GL driver
95 // re-uses pointers, therefore we won't have any collisions where the shaders attached to a
96 // pipeline do not match the pipeline configuration.
97 static u64 GenerateShaderID();
98
99 static PipelineProgram* GetPipelineProgram(const GLVertexFormat* vertex_format,
100 const OGLShader* vertex_shader,
101 const OGLShader* geometry_shader,
102 const OGLShader* pixel_shader, const void* cache_data,
103 size_t cache_data_size);
104 static void ReleasePipelineProgram(PipelineProgram* prog);
105
106 private:
107 using PipelineProgramMap =
108 std::unordered_map<PipelineProgramKey, std::unique_ptr<PipelineProgram>,
109 PipelineProgramKeyHash>;
110
111 static void CreateAttributelessVAO();
112
113 static PipelineProgramMap s_pipeline_programs;
114 static std::mutex s_pipeline_program_lock;
115
116 static u32 s_ubo_buffer_size;
117 static s32 s_ubo_align;
118
119 static GLuint s_attributeless_VBO;
120 static GLuint s_attributeless_VAO;
121 static GLuint s_last_VAO;
122 };
123
124 class SharedContextAsyncShaderCompiler : public VideoCommon::AsyncShaderCompiler
125 {
126 protected:
127 bool WorkerThreadInitMainThread(void** param) override;
128 bool WorkerThreadInitWorkerThread(void* param) override;
129 void WorkerThreadExit(void* param) override;
130 };
131
132 } // namespace OGL