diff options
| author | 2015-10-12 22:23:26 -0400 | |
|---|---|---|
| committer | 2015-10-21 21:59:44 -0400 | |
| commit | 0ebcff710e6b5d266158ec364cf08bd8f22ae74a (patch) | |
| tree | 6ce8538ed885a8497ca842dab88f94789a483a1c /src | |
| parent | gl_rasterizer: Use MMH3 hash for shader cache hey. (diff) | |
| download | yuzu-0ebcff710e6b5d266158ec364cf08bd8f22ae74a.tar.gz yuzu-0ebcff710e6b5d266158ec364cf08bd8f22ae74a.tar.xz yuzu-0ebcff710e6b5d266158ec364cf08bd8f22ae74a.zip | |
gl_shader_gen: Various cleanups to shader generation.
Diffstat (limited to 'src')
| -rw-r--r-- | src/video_core/renderer_opengl/gl_rasterizer.h | 2 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_gen.cpp | 92 | ||||
| -rw-r--r-- | src/video_core/renderer_opengl/gl_shader_util.cpp | 6 |
3 files changed, 52 insertions, 48 deletions
diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index cf8df8d9c..872cae7da 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h | |||
| @@ -95,7 +95,7 @@ namespace std { | |||
| 95 | 95 | ||
| 96 | template <> | 96 | template <> |
| 97 | struct hash<PicaShaderConfig> { | 97 | struct hash<PicaShaderConfig> { |
| 98 | std::size_t operator()(const PicaShaderConfig& k) const { | 98 | size_t operator()(const PicaShaderConfig& k) const { |
| 99 | return Common::ComputeHash64(&k, sizeof(PicaShaderConfig)); | 99 | return Common::ComputeHash64(&k, sizeof(PicaShaderConfig)); |
| 100 | } | 100 | } |
| 101 | }; | 101 | }; |
diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 84883b483..7506cdc08 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp | |||
| @@ -29,33 +29,33 @@ static void AppendSource(std::string& out, TevStageConfig::Source source, | |||
| 29 | using Source = TevStageConfig::Source; | 29 | using Source = TevStageConfig::Source; |
| 30 | switch (source) { | 30 | switch (source) { |
| 31 | case Source::PrimaryColor: | 31 | case Source::PrimaryColor: |
| 32 | out += "attr[2]"; | 32 | out += "primary_color"; |
| 33 | break; | 33 | break; |
| 34 | case Source::PrimaryFragmentColor: | 34 | case Source::PrimaryFragmentColor: |
| 35 | // HACK: Until we implement fragment lighting, use primary_color | 35 | // HACK: Until we implement fragment lighting, use primary_color |
| 36 | out += "attr[2]"; | 36 | out += "primary_color"; |
| 37 | break; | 37 | break; |
| 38 | case Source::SecondaryFragmentColor: | 38 | case Source::SecondaryFragmentColor: |
| 39 | // HACK: Until we implement fragment lighting, use zero | 39 | // HACK: Until we implement fragment lighting, use zero |
| 40 | out += "vec4(0.0, 0.0, 0.0, 0.0)"; | 40 | out += "vec4(0.0)"; |
| 41 | break; | 41 | break; |
| 42 | case Source::Texture0: | 42 | case Source::Texture0: |
| 43 | out += "texture(tex[0], attr[3].xy)"; | 43 | out += "texture(tex[0], texcoord[0])"; |
| 44 | break; | 44 | break; |
| 45 | case Source::Texture1: | 45 | case Source::Texture1: |
| 46 | out += "texture(tex[1], attr[3].zw)"; | 46 | out += "texture(tex[1], texcoord[1])"; |
| 47 | break; | 47 | break; |
| 48 | case Source::Texture2: // TODO: Unverified | 48 | case Source::Texture2: |
| 49 | out += "texture(tex[2], attr[5].zw)"; | 49 | out += "texture(tex[2], texcoord[2])"; |
| 50 | break; | 50 | break; |
| 51 | case Source::PreviousBuffer: | 51 | case Source::PreviousBuffer: |
| 52 | out += "g_combiner_buffer"; | 52 | out += "combiner_buffer"; |
| 53 | break; | 53 | break; |
| 54 | case Source::Constant: | 54 | case Source::Constant: |
| 55 | out += "const_color[" + index_name + "]"; | 55 | ((out += "const_color[") += index_name) += ']'; |
| 56 | break; | 56 | break; |
| 57 | case Source::Previous: | 57 | case Source::Previous: |
| 58 | out += "g_last_tex_env_out"; | 58 | out += "last_tex_env_out"; |
| 59 | break; | 59 | break; |
| 60 | default: | 60 | default: |
| 61 | out += "vec4(0.0)"; | 61 | out += "vec4(0.0)"; |
| @@ -172,8 +172,8 @@ static void AppendAlphaModifier(std::string& out, TevStageConfig::AlphaModifier | |||
| 172 | /// Writes the combiner function for the color components for the specified TEV stage operation | 172 | /// Writes the combiner function for the color components for the specified TEV stage operation |
| 173 | static void AppendColorCombiner(std::string& out, TevStageConfig::Operation operation, | 173 | static void AppendColorCombiner(std::string& out, TevStageConfig::Operation operation, |
| 174 | const std::string& variable_name) { | 174 | const std::string& variable_name) { |
| 175 | out += "clamp("; | ||
| 175 | using Operation = TevStageConfig::Operation; | 176 | using Operation = TevStageConfig::Operation; |
| 176 | |||
| 177 | switch (operation) { | 177 | switch (operation) { |
| 178 | case Operation::Replace: | 178 | case Operation::Replace: |
| 179 | out += variable_name + "[0]"; | 179 | out += variable_name + "[0]"; |
| @@ -182,19 +182,20 @@ static void AppendColorCombiner(std::string& out, TevStageConfig::Operation oper | |||
| 182 | out += variable_name + "[0] * " + variable_name + "[1]"; | 182 | out += variable_name + "[0] * " + variable_name + "[1]"; |
| 183 | break; | 183 | break; |
| 184 | case Operation::Add: | 184 | case Operation::Add: |
| 185 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0))"; | 185 | out += variable_name + "[0] + " + variable_name + "[1]"; |
| 186 | break; | 186 | break; |
| 187 | case Operation::AddSigned: | 187 | case Operation::AddSigned: |
| 188 | out += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - vec3(0.5), vec3(0.0), vec3(1.0))"; | 188 | out += variable_name + "[0] + " + variable_name + "[1] - vec3(0.5)"; |
| 189 | break; | 189 | break; |
| 190 | case Operation::Lerp: | 190 | case Operation::Lerp: |
| 191 | // TODO(bunnei): Verify if HW actually does this per-component, otherwise we can just use builtin lerp | ||
| 191 | out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])"; | 192 | out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (vec3(1.0) - " + variable_name + "[2])"; |
| 192 | break; | 193 | break; |
| 193 | case Operation::Subtract: | 194 | case Operation::Subtract: |
| 194 | out += "max(" + variable_name + "[0] - " + variable_name + "[1], vec3(0.0))"; | 195 | out += variable_name + "[0] - " + variable_name + "[1]"; |
| 195 | break; | 196 | break; |
| 196 | case Operation::MultiplyThenAdd: | 197 | case Operation::MultiplyThenAdd: |
| 197 | out += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], vec3(1.0))"; | 198 | out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]"; |
| 198 | break; | 199 | break; |
| 199 | case Operation::AddThenMultiply: | 200 | case Operation::AddThenMultiply: |
| 200 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]"; | 201 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], vec3(1.0)) * " + variable_name + "[2]"; |
| @@ -204,11 +205,13 @@ static void AppendColorCombiner(std::string& out, TevStageConfig::Operation oper | |||
| 204 | LOG_CRITICAL(Render_OpenGL, "Unknown color combiner operation: %u", operation); | 205 | LOG_CRITICAL(Render_OpenGL, "Unknown color combiner operation: %u", operation); |
| 205 | break; | 206 | break; |
| 206 | } | 207 | } |
| 208 | out += ", vec3(0.0), vec3(1.0))"; | ||
| 207 | } | 209 | } |
| 208 | 210 | ||
| 209 | /// Writes the combiner function for the alpha component for the specified TEV stage operation | 211 | /// Writes the combiner function for the alpha component for the specified TEV stage operation |
| 210 | static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation operation, | 212 | static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation operation, |
| 211 | const std::string& variable_name) { | 213 | const std::string& variable_name) { |
| 214 | out += "clamp("; | ||
| 212 | using Operation = TevStageConfig::Operation; | 215 | using Operation = TevStageConfig::Operation; |
| 213 | switch (operation) { | 216 | switch (operation) { |
| 214 | case Operation::Replace: | 217 | case Operation::Replace: |
| @@ -218,19 +221,19 @@ static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation oper | |||
| 218 | out += variable_name + "[0] * " + variable_name + "[1]"; | 221 | out += variable_name + "[0] * " + variable_name + "[1]"; |
| 219 | break; | 222 | break; |
| 220 | case Operation::Add: | 223 | case Operation::Add: |
| 221 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0)"; | 224 | out += variable_name + "[0] + " + variable_name + "[1]"; |
| 222 | break; | 225 | break; |
| 223 | case Operation::AddSigned: | 226 | case Operation::AddSigned: |
| 224 | out += "clamp(" + variable_name + "[0] + " + variable_name + "[1] - 0.5, 0.0, 1.0)"; | 227 | out += variable_name + "[0] + " + variable_name + "[1] - 0.5"; |
| 225 | break; | 228 | break; |
| 226 | case Operation::Lerp: | 229 | case Operation::Lerp: |
| 227 | out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])"; | 230 | out += variable_name + "[0] * " + variable_name + "[2] + " + variable_name + "[1] * (1.0 - " + variable_name + "[2])"; |
| 228 | break; | 231 | break; |
| 229 | case Operation::Subtract: | 232 | case Operation::Subtract: |
| 230 | out += "max(" + variable_name + "[0] - " + variable_name + "[1], 0.0)"; | 233 | out += variable_name + "[0] - " + variable_name + "[1]"; |
| 231 | break; | 234 | break; |
| 232 | case Operation::MultiplyThenAdd: | 235 | case Operation::MultiplyThenAdd: |
| 233 | out += "min(" + variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2], 1.0)"; | 236 | out += variable_name + "[0] * " + variable_name + "[1] + " + variable_name + "[2]"; |
| 234 | break; | 237 | break; |
| 235 | case Operation::AddThenMultiply: | 238 | case Operation::AddThenMultiply: |
| 236 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]"; | 239 | out += "min(" + variable_name + "[0] + " + variable_name + "[1], 1.0) * " + variable_name + "[2]"; |
| @@ -240,6 +243,7 @@ static void AppendAlphaCombiner(std::string& out, TevStageConfig::Operation oper | |||
| 240 | LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner operation: %u", operation); | 243 | LOG_CRITICAL(Render_OpenGL, "Unknown alpha combiner operation: %u", operation); |
| 241 | break; | 244 | break; |
| 242 | } | 245 | } |
| 246 | out += ", 0.0, 1.0)"; | ||
| 243 | } | 247 | } |
| 244 | 248 | ||
| 245 | /// Writes the if-statement condition used to evaluate alpha testing | 249 | /// Writes the if-statement condition used to evaluate alpha testing |
| @@ -253,22 +257,22 @@ static void AppendAlphaTestCondition(std::string& out, Regs::CompareFunc func) { | |||
| 253 | out += "false"; | 257 | out += "false"; |
| 254 | break; | 258 | break; |
| 255 | case CompareFunc::Equal: | 259 | case CompareFunc::Equal: |
| 256 | out += "int(g_last_tex_env_out.a * 255.0f) != alphatest_ref"; | 260 | out += "int(last_tex_env_out.a * 255.0f) != alphatest_ref"; |
| 257 | break; | 261 | break; |
| 258 | case CompareFunc::NotEqual: | 262 | case CompareFunc::NotEqual: |
| 259 | out += "int(g_last_tex_env_out.a * 255.0f) == alphatest_ref"; | 263 | out += "int(last_tex_env_out.a * 255.0f) == alphatest_ref"; |
| 260 | break; | 264 | break; |
| 261 | case CompareFunc::LessThan: | 265 | case CompareFunc::LessThan: |
| 262 | out += "int(g_last_tex_env_out.a * 255.0f) >= alphatest_ref"; | 266 | out += "int(last_tex_env_out.a * 255.0f) >= alphatest_ref"; |
| 263 | break; | 267 | break; |
| 264 | case CompareFunc::LessThanOrEqual: | 268 | case CompareFunc::LessThanOrEqual: |
| 265 | out += "int(g_last_tex_env_out.a * 255.0f) > alphatest_ref"; | 269 | out += "int(last_tex_env_out.a * 255.0f) > alphatest_ref"; |
| 266 | break; | 270 | break; |
| 267 | case CompareFunc::GreaterThan: | 271 | case CompareFunc::GreaterThan: |
| 268 | out += "int(g_last_tex_env_out.a * 255.0f) <= alphatest_ref"; | 272 | out += "int(last_tex_env_out.a * 255.0f) <= alphatest_ref"; |
| 269 | break; | 273 | break; |
| 270 | case CompareFunc::GreaterThanOrEqual: | 274 | case CompareFunc::GreaterThanOrEqual: |
| 271 | out += "int(g_last_tex_env_out.a * 255.0f) < alphatest_ref"; | 275 | out += "int(last_tex_env_out.a * 255.0f) < alphatest_ref"; |
| 272 | break; | 276 | break; |
| 273 | default: | 277 | default: |
| 274 | out += "false"; | 278 | out += "false"; |
| @@ -307,16 +311,16 @@ static void WriteTevStage(std::string& out, const PicaShaderConfig& config, unsi | |||
| 307 | AppendAlphaCombiner(out, stage.alpha_op, "alpha_results_" + index_name); | 311 | AppendAlphaCombiner(out, stage.alpha_op, "alpha_results_" + index_name); |
| 308 | out += ";\n"; | 312 | out += ";\n"; |
| 309 | 313 | ||
| 310 | out += "g_last_tex_env_out = vec4(" | 314 | out += "last_tex_env_out = vec4(" |
| 311 | "clamp(color_output_" + index_name + " * " + std::to_string(stage.GetColorMultiplier()) + ".0, 0.0, 1.0)," | 315 | "clamp(color_output_" + index_name + " * " + std::to_string(stage.GetColorMultiplier()) + ".0, vec3(0.0), vec3(1.0))," |
| 312 | "clamp(alpha_output_" + index_name + " * " + std::to_string(stage.GetAlphaMultiplier()) + ".0, 0.0, 1.0));\n"; | 316 | "clamp(alpha_output_" + index_name + " * " + std::to_string(stage.GetAlphaMultiplier()) + ".0, 0.0, 1.0));\n"; |
| 313 | } | 317 | } |
| 314 | 318 | ||
| 315 | if (config.TevStageUpdatesCombinerBufferColor(index)) | 319 | if (config.TevStageUpdatesCombinerBufferColor(index)) |
| 316 | out += "g_combiner_buffer.rgb = g_last_tex_env_out.rgb;\n"; | 320 | out += "combiner_buffer.rgb = last_tex_env_out.rgb;\n"; |
| 317 | 321 | ||
| 318 | if (config.TevStageUpdatesCombinerBufferAlpha(index)) | 322 | if (config.TevStageUpdatesCombinerBufferAlpha(index)) |
| 319 | out += "g_combiner_buffer.a = g_last_tex_env_out.a;\n"; | 323 | out += "combiner_buffer.a = last_tex_env_out.a;\n"; |
| 320 | } | 324 | } |
| 321 | 325 | ||
| 322 | std::string GenerateFragmentShader(const PicaShaderConfig& config) { | 326 | std::string GenerateFragmentShader(const PicaShaderConfig& config) { |
| @@ -324,10 +328,11 @@ std::string GenerateFragmentShader(const PicaShaderConfig& config) { | |||
| 324 | #version 330 | 328 | #version 330 |
| 325 | #extension GL_ARB_explicit_uniform_location : require | 329 | #extension GL_ARB_explicit_uniform_location : require |
| 326 | 330 | ||
| 327 | #define NUM_VTX_ATTR 7 | ||
| 328 | #define NUM_TEV_STAGES 6 | 331 | #define NUM_TEV_STAGES 6 |
| 329 | 332 | ||
| 330 | in vec4 attr[NUM_VTX_ATTR]; | 333 | in vec4 primary_color; |
| 334 | in vec2 texcoord[3]; | ||
| 335 | |||
| 331 | out vec4 color; | 336 | out vec4 color; |
| 332 | )"; | 337 | )"; |
| 333 | 338 | ||
| @@ -347,16 +352,16 @@ out vec4 color; | |||
| 347 | return out; | 352 | return out; |
| 348 | } | 353 | } |
| 349 | 354 | ||
| 350 | for (std::size_t index = 0; index < config.tev_stages.size(); ++index) | 355 | for (size_t index = 0; index < config.tev_stages.size(); ++index) |
| 351 | WriteTevStage(out, config, (unsigned)index); | 356 | WriteTevStage(out, config, (unsigned)index); |
| 352 | 357 | ||
| 353 | if (config.alpha_test_func != Regs::CompareFunc::Always) { | 358 | if (config.alpha_test_func != Regs::CompareFunc::Always) { |
| 354 | out += "if ("; | 359 | out += "if ("; |
| 355 | AppendAlphaTestCondition(out, config.alpha_test_func); | 360 | AppendAlphaTestCondition(out, config.alpha_test_func); |
| 356 | out += ") {\n discard;\n }\n"; | 361 | out += ") discard;\n"; |
| 357 | } | 362 | } |
| 358 | 363 | ||
| 359 | out += "color = g_last_tex_env_out;\n}"; | 364 | out += "color = last_tex_env_out;\n}"; |
| 360 | 365 | ||
| 361 | return out; | 366 | return out; |
| 362 | } | 367 | } |
| @@ -365,21 +370,20 @@ std::string GenerateVertexShader() { | |||
| 365 | static const std::string out = R"( | 370 | static const std::string out = R"( |
| 366 | #version 330 | 371 | #version 330 |
| 367 | 372 | ||
| 368 | #define NUM_VTX_ATTR 7 | ||
| 369 | |||
| 370 | in vec4 vert_position; | 373 | in vec4 vert_position; |
| 371 | in vec4 vert_color; | 374 | in vec4 vert_color; |
| 372 | in vec2 vert_texcoords0; | 375 | in vec2 vert_texcoord0; |
| 373 | in vec2 vert_texcoords1; | 376 | in vec2 vert_texcoord1; |
| 374 | in vec2 vert_texcoords2; | 377 | in vec2 vert_texcoord2; |
| 375 | 378 | ||
| 376 | out vec4 attr[NUM_VTX_ATTR]; | 379 | out vec4 primary_color; |
| 380 | out vec2 texcoord[3]; | ||
| 377 | 381 | ||
| 378 | void main() { | 382 | void main() { |
| 379 | attr[2] = vert_color; | 383 | primary_color = vert_color; |
| 380 | attr[3] = vec4(vert_texcoords0.xy, vert_texcoords1.xy); | 384 | texcoord[0] = vert_texcoord0; |
| 381 | attr[5] = vec4(0.0, 0.0, vert_texcoords2.xy); | 385 | texcoord[1] = vert_texcoord1; |
| 382 | 386 | texcoord[2] = vert_texcoord2; | |
| 383 | gl_Position = vec4(vert_position.x, -vert_position.y, -vert_position.z, vert_position.w); | 387 | gl_Position = vec4(vert_position.x, -vert_position.y, -vert_position.z, vert_position.w); |
| 384 | } | 388 | } |
| 385 | )"; | 389 | )"; |
diff --git a/src/video_core/renderer_opengl/gl_shader_util.cpp b/src/video_core/renderer_opengl/gl_shader_util.cpp index ce218b857..2fa0ceb3e 100644 --- a/src/video_core/renderer_opengl/gl_shader_util.cpp +++ b/src/video_core/renderer_opengl/gl_shader_util.cpp | |||
| @@ -68,9 +68,9 @@ GLuint LoadProgram(const char* vertex_shader, const char* fragment_shader) { | |||
| 68 | 68 | ||
| 69 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_POSITION, "vert_position"); | 69 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_POSITION, "vert_position"); |
| 70 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_COLOR, "vert_color"); | 70 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_COLOR, "vert_color"); |
| 71 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 0, "vert_texcoords0"); | 71 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 0, "vert_texcoord0"); |
| 72 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 1, "vert_texcoords1"); | 72 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 1, "vert_texcoord1"); |
| 73 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 2, "vert_texcoords2"); | 73 | glBindAttribLocation(program_id, Attributes::ATTRIBUTE_TEXCOORDS + 2, "vert_texcoord2"); |
| 74 | 74 | ||
| 75 | glLinkProgram(program_id); | 75 | glLinkProgram(program_id); |
| 76 | 76 | ||