summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2018-04-07 05:22:08 -0400
committerGravatar bunnei2018-04-13 23:48:26 -0400
commit5617831d5fb42a692d09f2fd3c21cc1eac3ae903 (patch)
tree7b4988833857f6e80f83bf2193c325b19f0a2870 /src
parentrenderer_opengl: Add gl_shader_manager class. (diff)
downloadyuzu-5617831d5fb42a692d09f2fd3c21cc1eac3ae903.tar.gz
yuzu-5617831d5fb42a692d09f2fd3c21cc1eac3ae903.tar.xz
yuzu-5617831d5fb42a692d09f2fd3c21cc1eac3ae903.zip
gl_rasterizer: Use shader program manager, remove test shader.
Diffstat (limited to 'src')
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.cpp170
-rw-r--r--src/video_core/renderer_opengl/gl_rasterizer.h57
2 files changed, 31 insertions, 196 deletions
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index fc69fbe5a..2d58df45b 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -34,33 +34,7 @@ MICROPROFILE_DEFINE(OpenGL_Drawing, "OpenGL", "Drawing", MP_RGB(128, 128, 192));
34MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255)); 34MICROPROFILE_DEFINE(OpenGL_Blits, "OpenGL", "Blits", MP_RGB(100, 100, 255));
35MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100)); 35MICROPROFILE_DEFINE(OpenGL_CacheManagement, "OpenGL", "Cache Mgmt", MP_RGB(100, 255, 100));
36 36
37enum class UniformBindings : GLuint { Common, VS, FS };
38
39static void SetShaderUniformBlockBinding(GLuint shader, const char* name, UniformBindings binding,
40 size_t expected_size) {
41 GLuint ub_index = glGetUniformBlockIndex(shader, name);
42 if (ub_index != GL_INVALID_INDEX) {
43 GLint ub_size = 0;
44 glGetActiveUniformBlockiv(shader, ub_index, GL_UNIFORM_BLOCK_DATA_SIZE, &ub_size);
45 ASSERT_MSG(ub_size == expected_size,
46 "Uniform block size did not match! Got %d, expected %zu",
47 static_cast<int>(ub_size), expected_size);
48 glUniformBlockBinding(shader, ub_index, static_cast<GLuint>(binding));
49 }
50}
51
52static void SetShaderUniformBlockBindings(GLuint shader) {
53 SetShaderUniformBlockBinding(shader, "shader_data", UniformBindings::Common,
54 sizeof(RasterizerOpenGL::UniformData));
55 SetShaderUniformBlockBinding(shader, "vs_config", UniformBindings::VS,
56 sizeof(RasterizerOpenGL::VSUniformData));
57 SetShaderUniformBlockBinding(shader, "fs_config", UniformBindings::FS,
58 sizeof(RasterizerOpenGL::FSUniformData));
59}
60
61RasterizerOpenGL::RasterizerOpenGL() { 37RasterizerOpenGL::RasterizerOpenGL() {
62 shader_dirty = true;
63
64 has_ARB_buffer_storage = false; 38 has_ARB_buffer_storage = false;
65 has_ARB_direct_state_access = false; 39 has_ARB_direct_state_access = false;
66 has_ARB_separate_shader_objects = false; 40 has_ARB_separate_shader_objects = false;
@@ -88,6 +62,8 @@ RasterizerOpenGL::RasterizerOpenGL() {
88 } 62 }
89 } 63 }
90 64
65 ASSERT_MSG(has_ARB_separate_shader_objects, "has_ARB_separate_shader_objects is unsupported");
66
91 // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0 67 // Clipping plane 0 is always enabled for PICA fixed clip plane z <= 0
92 state.clip_distance[0] = true; 68 state.clip_distance[0] = true;
93 69
@@ -102,37 +78,28 @@ RasterizerOpenGL::RasterizerOpenGL() {
102 state.draw.uniform_buffer = uniform_buffer.handle; 78 state.draw.uniform_buffer = uniform_buffer.handle;
103 state.Apply(); 79 state.Apply();
104 80
105 glBufferData(GL_UNIFORM_BUFFER, sizeof(UniformData), nullptr, GL_STATIC_DRAW);
106 glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniform_buffer.handle);
107
108 uniform_block_data.dirty = true;
109
110 // Create render framebuffer 81 // Create render framebuffer
111 framebuffer.Create(); 82 framebuffer.Create();
112 83
113 if (has_ARB_separate_shader_objects) { 84 hw_vao.Create();
114 hw_vao.Create(); 85 hw_vao_enabled_attributes.fill(false);
115 hw_vao_enabled_attributes.fill(false);
116 86
117 stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER); 87 stream_buffer = OGLStreamBuffer::MakeBuffer(has_ARB_buffer_storage, GL_ARRAY_BUFFER);
118 stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2); 88 stream_buffer->Create(STREAM_BUFFER_SIZE, STREAM_BUFFER_SIZE / 2);
119 state.draw.vertex_buffer = stream_buffer->GetHandle(); 89 state.draw.vertex_buffer = stream_buffer->GetHandle();
120 90
121 pipeline.Create(); 91 shader_program_manager = std::make_unique<GLShader::ProgramManager>();
122 state.draw.program_pipeline = pipeline.handle; 92
123 state.draw.shader_program = 0; 93 state.draw.shader_program = 0;
124 state.draw.vertex_array = hw_vao.handle; 94 state.draw.vertex_array = hw_vao.handle;
125 state.Apply(); 95 state.Apply();
126 96
127 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle()); 97 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, stream_buffer->GetHandle());
128 98
129 vs_uniform_buffer.Create(); 99 vs_uniform_buffer.Create();
130 glBindBuffer(GL_UNIFORM_BUFFER, vs_uniform_buffer.handle); 100 glBindBuffer(GL_UNIFORM_BUFFER, vs_uniform_buffer.handle);
131 glBufferData(GL_UNIFORM_BUFFER, sizeof(VSUniformData), nullptr, GL_STREAM_COPY); 101 glBufferData(GL_UNIFORM_BUFFER, sizeof(GLShader::VSUniformData), nullptr, GL_STREAM_COPY);
132 glBindBufferBase(GL_UNIFORM_BUFFER, 1, vs_uniform_buffer.handle); 102 glBindBufferBase(GL_UNIFORM_BUFFER, 1, vs_uniform_buffer.handle);
133 } else {
134 UNREACHABLE();
135 }
136 103
137 accelerate_draw = AccelDraw::Disabled; 104 accelerate_draw = AccelDraw::Disabled;
138 105
@@ -200,26 +167,20 @@ void RasterizerOpenGL::SetupVertexArray(u8* array_ptr, GLintptr buffer_offset) {
200 buffer_offset += data_size; 167 buffer_offset += data_size;
201} 168}
202 169
203void RasterizerOpenGL::SetupVertexShader(VSUniformData* ub_ptr, GLintptr buffer_offset) { 170void RasterizerOpenGL::SetupVertexShader(GLShader::VSUniformData* ub_ptr, GLintptr buffer_offset) {
204 MICROPROFILE_SCOPE(OpenGL_VS); 171 MICROPROFILE_SCOPE(OpenGL_VS);
205 LOG_CRITICAL(Render_OpenGL, "Emulated shaders are not supported! Using a passthrough shader."); 172 UNREACHABLE();
206 glUseProgramStages(pipeline.handle, GL_VERTEX_SHADER_BIT, current_shader->shader.handle);
207} 173}
208 174
209void RasterizerOpenGL::SetupFragmentShader(FSUniformData* ub_ptr, GLintptr buffer_offset) { 175void RasterizerOpenGL::SetupFragmentShader(GLShader::FSUniformData* ub_ptr,
176 GLintptr buffer_offset) {
210 MICROPROFILE_SCOPE(OpenGL_FS); 177 MICROPROFILE_SCOPE(OpenGL_FS);
211 UNREACHABLE(); 178 UNREACHABLE();
212} 179}
213 180
214bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) { 181bool RasterizerOpenGL::AccelerateDrawBatch(bool is_indexed) {
215 if (!has_ARB_separate_shader_objects) {
216 UNREACHABLE();
217 return false;
218 }
219
220 accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays; 182 accelerate_draw = is_indexed ? AccelDraw::Indexed : AccelDraw::Arrays;
221 DrawArrays(); 183 DrawArrays();
222
223 return true; 184 return true;
224} 185}
225 186
@@ -280,18 +241,6 @@ void RasterizerOpenGL::DrawArrays() {
280 // Sync and bind the texture surfaces 241 // Sync and bind the texture surfaces
281 BindTextures(); 242 BindTextures();
282 243
283 // Sync and bind the shader
284 if (shader_dirty) {
285 SetShader();
286 shader_dirty = false;
287 }
288
289 // Sync the uniform data
290 if (uniform_block_data.dirty) {
291 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(UniformData), &uniform_block_data.data);
292 uniform_block_data.dirty = false;
293 }
294
295 // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable 244 // Viewport can have negative offsets or larger dimensions than our framebuffer sub-rect. Enable
296 // scissor test to prevent drawing outside of the framebuffer region 245 // scissor test to prevent drawing outside of the framebuffer region
297 state.scissor.enabled = true; 246 state.scissor.enabled = true;
@@ -311,7 +260,7 @@ void RasterizerOpenGL::DrawArrays() {
311 if (is_indexed) { 260 if (is_indexed) {
312 UNREACHABLE(); 261 UNREACHABLE();
313 } 262 }
314 buffer_size += sizeof(VSUniformData); 263 buffer_size += sizeof(GLShader::VSUniformData);
315 264
316 size_t ptr_pos = 0; 265 size_t ptr_pos = 0;
317 u8* buffer_ptr; 266 u8* buffer_ptr;
@@ -327,10 +276,10 @@ void RasterizerOpenGL::DrawArrays() {
327 UNREACHABLE(); 276 UNREACHABLE();
328 } 277 }
329 278
330 SetupVertexShader(reinterpret_cast<VSUniformData*>(&buffer_ptr[ptr_pos]), 279 SetupVertexShader(reinterpret_cast<GLShader::VSUniformData*>(&buffer_ptr[ptr_pos]),
331 buffer_offset + static_cast<GLintptr>(ptr_pos)); 280 buffer_offset + static_cast<GLintptr>(ptr_pos));
332 const GLintptr vs_ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos); 281 const GLintptr vs_ubo_offset = buffer_offset + static_cast<GLintptr>(ptr_pos);
333 ptr_pos += sizeof(VSUniformData); 282 ptr_pos += sizeof(GLShader::VSUniformData);
334 283
335 stream_buffer->Unmap(); 284 stream_buffer->Unmap();
336 285
@@ -343,9 +292,10 @@ void RasterizerOpenGL::DrawArrays() {
343 } 292 }
344 }; 293 };
345 294
346 copy_buffer(vs_uniform_buffer.handle, vs_ubo_offset, sizeof(VSUniformData)); 295 copy_buffer(vs_uniform_buffer.handle, vs_ubo_offset, sizeof(GLShader::VSUniformData));
347 296
348 glUseProgramStages(pipeline.handle, GL_FRAGMENT_SHADER_BIT, current_shader->shader.handle); 297 shader_program_manager->ApplyTo(state);
298 state.Apply();
349 299
350 if (is_indexed) { 300 if (is_indexed) {
351 UNREACHABLE(); 301 UNREACHABLE();
@@ -531,72 +481,6 @@ void RasterizerOpenGL::SamplerInfo::SyncWithConfig(const Tegra::Texture::TSCEntr
531 } 481 }
532} 482}
533 483
534void RasterizerOpenGL::SetShader() {
535 // TODO(bunnei): The below sets up a static test shader for passing untransformed vertices to
536 // OpenGL for rendering. This should be removed/replaced when we start emulating Maxwell
537 // shaders.
538
539 static constexpr char vertex_shader[] = R"(
540#version 150 core
541
542in vec2 vert_position;
543in vec2 vert_tex_coord;
544out vec2 frag_tex_coord;
545
546void main() {
547 // Multiply input position by the rotscale part of the matrix and then manually translate by
548 // the last column. This is equivalent to using a full 3x3 matrix and expanding the vector
549 // to `vec3(vert_position.xy, 1.0)`
550 gl_Position = vec4(mat2(mat3x2(0.0015625f, 0.0, 0.0, -0.0027778, -1.0, 1.0)) * vert_position + mat3x2(0.0015625f, 0.0, 0.0, -0.0027778, -1.0, 1.0)[2], 0.0, 1.0);
551 frag_tex_coord = vert_tex_coord;
552}
553)";
554
555 static constexpr char fragment_shader[] = R"(
556#version 150 core
557
558in vec2 frag_tex_coord;
559out vec4 color;
560
561uniform sampler2D tex[32];
562
563void main() {
564 color = texture(tex[0], frag_tex_coord);
565}
566)";
567
568 if (current_shader) {
569 return;
570 }
571
572 LOG_CRITICAL(Render_OpenGL, "Emulated shaders are not supported! Using a passthrough shader.");
573
574 current_shader = &test_shader;
575 if (has_ARB_separate_shader_objects) {
576 test_shader.shader.CreateFromSource(vertex_shader, nullptr, fragment_shader, true);
577 glActiveShaderProgram(pipeline.handle, test_shader.shader.handle);
578 } else {
579 UNREACHABLE();
580 }
581
582 state.draw.shader_program = test_shader.shader.handle;
583 state.Apply();
584
585 for (u32 texture = 0; texture < texture_samplers.size(); ++texture) {
586 // Set the texture samplers to correspond to different texture units
587 std::string uniform_name = "tex[" + std::to_string(texture) + "]";
588 GLint uniform_tex = glGetUniformLocation(test_shader.shader.handle, uniform_name.c_str());
589 if (uniform_tex != -1) {
590 glUniform1i(uniform_tex, TextureUnits::MaxwellTexture(texture).id);
591 }
592 }
593
594 if (has_ARB_separate_shader_objects) {
595 state.draw.shader_program = 0;
596 state.Apply();
597 }
598}
599
600void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface, 484void RasterizerOpenGL::BindFramebufferSurfaces(const Surface& color_surface,
601 const Surface& depth_surface, bool has_stencil) { 485 const Surface& depth_surface, bool has_stencil) {
602 state.draw.draw_framebuffer = framebuffer.handle; 486 state.draw.draw_framebuffer = framebuffer.handle;
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h
index 989c62d0d..b508f5acc 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.h
+++ b/src/video_core/renderer_opengl/gl_rasterizer.h
@@ -19,6 +19,7 @@
19#include "video_core/renderer_opengl/gl_rasterizer_cache.h" 19#include "video_core/renderer_opengl/gl_rasterizer_cache.h"
20#include "video_core/renderer_opengl/gl_resource_manager.h" 20#include "video_core/renderer_opengl/gl_resource_manager.h"
21#include "video_core/renderer_opengl/gl_shader_gen.h" 21#include "video_core/renderer_opengl/gl_shader_gen.h"
22#include "video_core/renderer_opengl/gl_shader_manager.h"
22#include "video_core/renderer_opengl/gl_state.h" 23#include "video_core/renderer_opengl/gl_state.h"
23#include "video_core/renderer_opengl/gl_stream_buffer.h" 24#include "video_core/renderer_opengl/gl_stream_buffer.h"
24 25
@@ -56,34 +57,6 @@ public:
56 OGLShader shader; 57 OGLShader shader;
57 }; 58 };
58 59
59 /// Uniform structure for the Uniform Buffer Object, all vectors must be 16-byte aligned
60 // NOTE: Always keep a vec4 at the end. The GL spec is not clear wether the alignment at
61 // the end of a uniform block is included in UNIFORM_BLOCK_DATA_SIZE or not.
62 // Not following that rule will cause problems on some AMD drivers.
63 struct UniformData {};
64
65 // static_assert(
66 // sizeof(UniformData) == 0x460,
67 // "The size of the UniformData structure has changed, update the structure in the shader");
68 static_assert(sizeof(UniformData) < 16384,
69 "UniformData structure must be less than 16kb as per the OpenGL spec");
70
71 struct VSUniformData {};
72 // static_assert(
73 // sizeof(VSUniformData) == 1856,
74 // "The size of the VSUniformData structure has changed, update the structure in the
75 // shader");
76 static_assert(sizeof(VSUniformData) < 16384,
77 "VSUniformData structure must be less than 16kb as per the OpenGL spec");
78
79 struct FSUniformData {};
80 // static_assert(
81 // sizeof(FSUniformData) == 1856,
82 // "The size of the FSUniformData structure has changed, update the structure in the
83 // shader");
84 static_assert(sizeof(FSUniformData) < 16384,
85 "FSUniformData structure must be less than 16kb as per the OpenGL spec");
86
87private: 60private:
88 class SamplerInfo { 61 class SamplerInfo {
89 public: 62 public:
@@ -122,9 +95,6 @@ private:
122 /// Syncs the clip coefficients to match the guest state 95 /// Syncs the clip coefficients to match the guest state
123 void SyncClipCoef(); 96 void SyncClipCoef();
124 97
125 /// Sets the OpenGL shader in accordance with the current guest state
126 void SetShader();
127
128 /// Syncs the cull mode to match the guest state 98 /// Syncs the cull mode to match the guest state
129 void SyncCullMode(); 99 void SyncCullMode();
130 100
@@ -152,18 +122,7 @@ private:
152 122
153 RasterizerCacheOpenGL res_cache; 123 RasterizerCacheOpenGL res_cache;
154 124
155 /// Shader used for test renderering - to be removed once we have emulated shaders 125 std::unique_ptr<GLShader::ProgramManager> shader_program_manager;
156 MaxwellShader test_shader{};
157
158 const MaxwellShader* current_shader{};
159 bool shader_dirty{};
160
161 struct {
162 UniformData data;
163 bool dirty;
164 } uniform_block_data = {};
165
166 OGLPipeline pipeline;
167 OGLVertexArray sw_vao; 126 OGLVertexArray sw_vao;
168 OGLVertexArray hw_vao; 127 OGLVertexArray hw_vao;
169 std::array<bool, 16> hw_vao_enabled_attributes; 128 std::array<bool, 16> hw_vao_enabled_attributes;
@@ -183,18 +142,10 @@ private:
183 void SetupVertexArray(u8* array_ptr, GLintptr buffer_offset); 142 void SetupVertexArray(u8* array_ptr, GLintptr buffer_offset);
184 143
185 OGLBuffer vs_uniform_buffer; 144 OGLBuffer vs_uniform_buffer;
186 std::unordered_map<GLShader::MaxwellVSConfig, VertexShader*> vs_shader_map;
187 std::unordered_map<std::string, VertexShader> vs_shader_cache;
188 OGLShader vs_default_shader;
189
190 void SetupVertexShader(VSUniformData* ub_ptr, GLintptr buffer_offset);
191 145
192 OGLBuffer fs_uniform_buffer; 146 void SetupVertexShader(GLShader::VSUniformData* ub_ptr, GLintptr buffer_offset);
193 std::unordered_map<GLShader::MaxwellFSConfig, FragmentShader*> fs_shader_map;
194 std::unordered_map<std::string, FragmentShader> fs_shader_cache;
195 OGLShader fs_default_shader;
196 147
197 void SetupFragmentShader(FSUniformData* ub_ptr, GLintptr buffer_offset); 148 void SetupFragmentShader(GLShader::FSUniformData* ub_ptr, GLintptr buffer_offset);
198 149
199 enum class AccelDraw { Disabled, Arrays, Indexed }; 150 enum class AccelDraw { Disabled, Arrays, Indexed };
200 AccelDraw accelerate_draw; 151 AccelDraw accelerate_draw;