From ecf2797173f5818b4ff0b91f5803304737f50bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 23 Oct 2022 21:50:29 +0200 Subject: [PATCH] Minor refactor of ShaderWriter flags. Extracted from the stereo PR --- Common/GPU/ShaderWriter.cpp | 16 ++++++++-------- Common/GPU/ShaderWriter.h | 15 +++++++++------ GPU/Common/DepalettizeShaderCommon.cpp | 4 ++-- GPU/Common/Draw2D.cpp | 23 +++++++++++++---------- GPU/Common/GeometryShaderGenerator.cpp | 6 +++--- GPU/Common/ReinterpretFramebuffer.cpp | 4 ++-- GPU/Common/StencilCommon.cpp | 4 ++-- 7 files changed, 39 insertions(+), 33 deletions(-) diff --git a/Common/GPU/ShaderWriter.cpp b/Common/GPU/ShaderWriter.cpp index 0f0d416bf7..5399c6241a 100644 --- a/Common/GPU/ShaderWriter.cpp +++ b/Common/GPU/ShaderWriter.cpp @@ -252,7 +252,7 @@ void ShaderWriter::BeginVSMain(Slice inputs, Slice uniform } } -void ShaderWriter::BeginFSMain(Slice uniforms, Slice varyings, FSFlags flags) { +void ShaderWriter::BeginFSMain(Slice uniforms, Slice varyings) { _assert_(this->stage_ == ShaderStage::Fragment); switch (lang_.shaderLanguage) { case HLSL_D3D11: @@ -266,13 +266,13 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var C("};\n"); } - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C("float gl_FragDepth;\n"); } C("struct PS_OUT {\n"); C(" vec4 target : SV_Target0;\n"); - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C(" float depth : SV_Depth;\n"); } C("};\n"); @@ -287,14 +287,14 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var F(") {\n"); C(" PS_OUT ps_out;\n"); - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C(" float gl_FragDepth;\n"); } break; case HLSL_D3D9: C("struct PS_OUT {\n"); C(" vec4 target : SV_Target0;\n"); - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C(" float depth : DEPTH;\n"); } C("};\n"); @@ -312,7 +312,7 @@ void ShaderWriter::BeginFSMain(Slice uniforms, Slice var F(") {\n"); C(" PS_OUT ps_out;\n"); - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C(" float gl_FragDepth;\n"); } @@ -406,13 +406,13 @@ void ShaderWriter::EndVSMain(Slice varyings) { C("}\n"); } -void ShaderWriter::EndFSMain(const char *vec4_color_variable, FSFlags flags) { +void ShaderWriter::EndFSMain(const char *vec4_color_variable) { _assert_(this->stage_ == ShaderStage::Fragment); switch (lang_.shaderLanguage) { case HLSL_D3D11: case HLSL_D3D9: F(" ps_out.target = %s;\n", vec4_color_variable); - if (flags & FSFLAG_WRITEDEPTH) { + if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) { C(" ps_out.depth = gl_FragDepth;\n"); } C(" return ps_out;\n"); diff --git a/Common/GPU/ShaderWriter.h b/Common/GPU/ShaderWriter.h index a957362b70..f9160c0491 100644 --- a/Common/GPU/ShaderWriter.h +++ b/Common/GPU/ShaderWriter.h @@ -30,15 +30,16 @@ struct VaryingDef { const char *precision; }; -enum FSFlags { - FSFLAG_NONE = 0, - FSFLAG_WRITEDEPTH = 1, +enum class ShaderWriterFlags { + NONE = 0, + FS_WRITE_DEPTH = 1, }; +ENUM_CLASS_BITOPS(ShaderWriterFlags); class ShaderWriter { public: // Extensions are supported for both OpenGL ES and Vulkan (though of course, they're different). - ShaderWriter(char *buffer, const ShaderLanguageDesc &lang, ShaderStage stage, Slice extensions = Slice()) : p_(buffer), lang_(lang), stage_(stage) { + ShaderWriter(char *buffer, const ShaderLanguageDesc &lang, ShaderStage stage, Slice extensions = Slice(), ShaderWriterFlags flags = ShaderWriterFlags::NONE) : p_(buffer), lang_(lang), stage_(stage) { Preamble(extensions); } ShaderWriter(const ShaderWriter &) = delete; @@ -77,6 +78,7 @@ public: void DeclareSamplers(Slice samplers); void ConstFloat(const char *name, float value); + void SetFlags(ShaderWriterFlags flags) { flags_ |= flags; } ShaderWriter &SampleTexture2D(const char *texName, const char *uv); ShaderWriter &SampleTexture2DOffset(const char *texName, const char *uv, int offX, int offY); @@ -85,12 +87,12 @@ public: // Simple shaders with no special tricks. void BeginVSMain(Slice inputs, Slice uniforms, Slice varyings); - void BeginFSMain(Slice uniforms, Slice varyings, FSFlags flags); + void BeginFSMain(Slice uniforms, Slice varyings); void BeginGSMain(Slice varyings, Slice outVaryings); // For simple shaders that output a single color, we can deal with this generically. void EndVSMain(Slice varyings); - void EndFSMain(const char *vec4_color_variable, FSFlags flags); + void EndFSMain(const char *vec4_color_variable); void EndGSMain(); const ShaderLanguageDesc &Lang() const { @@ -116,4 +118,5 @@ private: char *p_; const ShaderLanguageDesc &lang_; const ShaderStage stage_; + ShaderWriterFlags flags_; }; diff --git a/GPU/Common/DepalettizeShaderCommon.cpp b/GPU/Common/DepalettizeShaderCommon.cpp index 628efc787f..7ff6c8b587 100644 --- a/GPU/Common/DepalettizeShaderCommon.cpp +++ b/GPU/Common/DepalettizeShaderCommon.cpp @@ -349,7 +349,7 @@ void GenerateDepalSmoothed(ShaderWriter &writer, const DepalConfig &config) { void GenerateDepalFs(ShaderWriter &writer, const DepalConfig &config) { writer.DeclareSamplers(samplers); writer.HighPrecisionFloat(); - writer.BeginFSMain(config.bufferFormat == GE_FORMAT_DEPTH16 ? g_draw2Duniforms : Slice::empty(), varyings, FSFLAG_NONE); + writer.BeginFSMain(config.bufferFormat == GE_FORMAT_DEPTH16 ? g_draw2Duniforms : Slice::empty(), varyings); if (config.smoothedDepal) { // Handles a limited set of cases, but doesn't need any integer math so we don't // need two variants. @@ -369,5 +369,5 @@ void GenerateDepalFs(ShaderWriter &writer, const DepalConfig &config) { _assert_msg_(false, "Shader language not supported for depal: %d", (int)writer.Lang().shaderLanguage); } } - writer.EndFSMain("outColor", FSFLAG_NONE); + writer.EndFSMain("outColor"); } diff --git a/GPU/Common/Draw2D.cpp b/GPU/Common/Draw2D.cpp index 05bead52e4..93d542bafb 100644 --- a/GPU/Common/Draw2D.cpp +++ b/GPU/Common/Draw2D.cpp @@ -59,9 +59,9 @@ const UniformBufferDesc draw2DUBDesc{ sizeof(Draw2DUB), { Draw2DPipelineInfo GenerateDraw2DCopyColorFs(ShaderWriter &writer) { writer.DeclareSamplers(samplers); - writer.BeginFSMain(Slice::empty(), varyings, FSFLAG_NONE); + writer.BeginFSMain(Slice::empty(), varyings); writer.C(" vec4 outColor = ").SampleTexture2D("tex", "v_texcoord.xy").C(";\n"); - writer.EndFSMain("outColor", FSFLAG_NONE); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "draw2d_copy_color", @@ -72,13 +72,13 @@ Draw2DPipelineInfo GenerateDraw2DCopyColorFs(ShaderWriter &writer) { Draw2DPipelineInfo GenerateDraw2DCopyColorRect2LinFs(ShaderWriter &writer) { writer.DeclareSamplers(samplers); - writer.BeginFSMain(g_draw2Duniforms, varyings, FSFLAG_NONE); + writer.BeginFSMain(g_draw2Duniforms, varyings); writer.C(" vec2 tSize = texSize / scaleFactor;\n"); writer.C(" vec2 pixels = v_texcoord * tSize;\n"); writer.C(" float u = mod(pixels.x, tSize.x);\n"); writer.C(" float v = floor(pixels.x / tSize.x);\n"); writer.C(" vec4 outColor = ").SampleTexture2D("tex", "vec2(u, v) / tSize").C(";\n"); - writer.EndFSMain("outColor", FSFLAG_NONE); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "draw2d_copy_color_rect2lin", @@ -89,10 +89,11 @@ Draw2DPipelineInfo GenerateDraw2DCopyColorRect2LinFs(ShaderWriter &writer) { Draw2DPipelineInfo GenerateDraw2DCopyDepthFs(ShaderWriter &writer) { writer.DeclareSamplers(samplers); - writer.BeginFSMain(Slice::empty(), varyings, FSFLAG_WRITEDEPTH); + writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH); + writer.BeginFSMain(Slice::empty(), varyings); writer.C(" vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0);\n"); writer.C(" gl_FragDepth = ").SampleTexture2D("tex", "v_texcoord.xy").C(".x;\n"); - writer.EndFSMain("outColor", FSFLAG_WRITEDEPTH); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "draw2d_copy_depth", @@ -103,7 +104,8 @@ Draw2DPipelineInfo GenerateDraw2DCopyDepthFs(ShaderWriter &writer) { Draw2DPipelineInfo GenerateDraw2D565ToDepthFs(ShaderWriter &writer) { writer.DeclareSamplers(samplers); - writer.BeginFSMain(Slice::empty(), varyings, FSFLAG_WRITEDEPTH); + writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH); + writer.BeginFSMain(Slice::empty(), varyings); writer.C(" vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0);\n"); // Unlike when just copying a depth buffer, here we're generating new depth values so we'll // have to apply the scaling. @@ -111,7 +113,7 @@ Draw2DPipelineInfo GenerateDraw2D565ToDepthFs(ShaderWriter &writer) { writer.C(" vec3 rgb = ").SampleTexture2D("tex", "v_texcoord.xy").C(".xyz;\n"); writer.F(" highp float depthValue = (floor(rgb.x * 31.99) + floor(rgb.y * 63.99) * 32.0 + floor(rgb.z * 31.99) * 2048.0); \n"); writer.F(" gl_FragDepth = (depthValue / %f) + %f;\n", factors.scale, factors.offset); - writer.EndFSMain("outColor", FSFLAG_WRITEDEPTH); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "draw2d_565_to_depth", @@ -122,7 +124,8 @@ Draw2DPipelineInfo GenerateDraw2D565ToDepthFs(ShaderWriter &writer) { Draw2DPipelineInfo GenerateDraw2D565ToDepthDeswizzleFs(ShaderWriter &writer) { writer.DeclareSamplers(samplers); - writer.BeginFSMain(g_draw2Duniforms, varyings, FSFLAG_WRITEDEPTH); + writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH); + writer.BeginFSMain(g_draw2Duniforms, varyings); writer.C(" vec4 outColor = vec4(0.0, 0.0, 0.0, 0.0);\n"); // Unlike when just copying a depth buffer, here we're generating new depth values so we'll // have to apply the scaling. @@ -136,7 +139,7 @@ Draw2DPipelineInfo GenerateDraw2D565ToDepthDeswizzleFs(ShaderWriter &writer) { writer.C(" vec3 rgb = ").SampleTexture2D("tex", "coord").C(".xyz;\n"); writer.F(" highp float depthValue = (floor(rgb.x * 31.99) + floor(rgb.y * 63.99) * 32.0 + floor(rgb.z * 31.99) * 2048.0); \n"); writer.F(" gl_FragDepth = (depthValue / %f) + %f;\n", factors.scale, factors.offset); - writer.EndFSMain("outColor", FSFLAG_WRITEDEPTH); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "draw2d_565_to_depth_deswizzle", diff --git a/GPU/Common/GeometryShaderGenerator.cpp b/GPU/Common/GeometryShaderGenerator.cpp index fc8a9f9a27..586afdca0f 100644 --- a/GPU/Common/GeometryShaderGenerator.cpp +++ b/GPU/Common/GeometryShaderGenerator.cpp @@ -39,16 +39,16 @@ bool GenerateGeometryShader(const GShaderID &id, char *buffer, const ShaderLanguageDesc &compat, const Draw::Bugs bugs, std::string *errorString) { - std::vector gl_exts; + std::vector extensions; if (ShaderLanguageIsOpenGL(compat.shaderLanguage)) { if (gl_extensions.EXT_gpu_shader4) { - gl_exts.push_back("#extension GL_EXT_gpu_shader4 : enable"); + extensions.push_back("#extension GL_EXT_gpu_shader4 : enable"); } } bool vertexRangeCulling = !id.Bit(GS_BIT_CURVE); bool clipClampedDepth = gstate_c.Use(GPU_USE_DEPTH_CLAMP); - ShaderWriter p(buffer, compat, ShaderStage::Geometry, gl_exts); + ShaderWriter p(buffer, compat, ShaderStage::Geometry, extensions); p.C("layout(triangles) in;\n"); if (clipClampedDepth && vertexRangeCulling && !gstate_c.Use(GPU_USE_CLIP_DISTANCE)) { p.C("layout(triangle_strip, max_vertices = 12) out;\n"); diff --git a/GPU/Common/ReinterpretFramebuffer.cpp b/GPU/Common/ReinterpretFramebuffer.cpp index 09f2552a9b..ea80fa8b3d 100644 --- a/GPU/Common/ReinterpretFramebuffer.cpp +++ b/GPU/Common/ReinterpretFramebuffer.cpp @@ -161,7 +161,7 @@ Draw2DPipelineInfo GenerateReinterpretFragmentShader(ShaderWriter &writer, GEBuf } } - writer.BeginFSMain(g_draw2Duniforms, varyings, FSFLAG_NONE); + writer.BeginFSMain(g_draw2Duniforms, varyings); if (IsBufferFormat16Bit(from) && IsBufferFormat16Bit(to)) { writer.C(" vec4 val = ").SampleTexture2D("tex", "v_texcoord.xy").C(";\n"); @@ -182,7 +182,7 @@ Draw2DPipelineInfo GenerateReinterpretFragmentShader(ShaderWriter &writer, GEBuf writer.C(" vec4 outColor = unpackColor(u == 0.0 ? packColor(val.rg) : packColor(val.ba));\n"); } - writer.EndFSMain("outColor", FSFLAG_NONE); + writer.EndFSMain("outColor"); return Draw2DPipelineInfo{ "reinterpret", diff --git a/GPU/Common/StencilCommon.cpp b/GPU/Common/StencilCommon.cpp index fd552208f7..c829e40f03 100644 --- a/GPU/Common/StencilCommon.cpp +++ b/GPU/Common/StencilCommon.cpp @@ -94,7 +94,7 @@ void GenerateStencilFs(char *buffer, const ShaderLanguageDesc &lang, const Draw: writer.C("float roundAndScaleTo255f(in float x) { return floor(x * 255.99); }\n"); - writer.BeginFSMain(uniforms, varyings, FSFLAG_NONE); + writer.BeginFSMain(uniforms, varyings); writer.C(" vec4 index = ").SampleTexture2D("tex", "v_texcoord.xy").C(";\n"); writer.C(" vec4 outColor = index.aaaa;\n"); // Only care about a. @@ -106,7 +106,7 @@ void GenerateStencilFs(char *buffer, const ShaderLanguageDesc &lang, const Draw: writer.C(" gl_FragDepth = gl_FragCoord.z;\n"); } - writer.EndFSMain("outColor", FSFLAG_NONE); + writer.EndFSMain("outColor"); } // This can probably be shared with some other shaders, like reinterpret or the future depth upload.