Skip to content

Commit

Permalink
Minor shaderwriter refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Oct 23, 2022
1 parent ec62b9d commit 9de6f6d
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 33 deletions.
16 changes: 8 additions & 8 deletions Common/GPU/ShaderWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void ShaderWriter::BeginVSMain(Slice<InputDef> inputs, Slice<UniformDef> uniform
}
}

void ShaderWriter::BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> varyings, FSFlags flags) {
void ShaderWriter::BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> varyings) {
_assert_(this->stage_ == ShaderStage::Fragment);
switch (lang_.shaderLanguage) {
case HLSL_D3D11:
Expand All @@ -266,13 +266,13 @@ void ShaderWriter::BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> 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");
Expand All @@ -287,14 +287,14 @@ void ShaderWriter::BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> 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");
Expand All @@ -312,7 +312,7 @@ void ShaderWriter::BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> var

F(") {\n");
C(" PS_OUT ps_out;\n");
if (flags & FSFLAG_WRITEDEPTH) {
if (flags_ & ShaderWriterFlags::FS_WRITE_DEPTH) {
C(" float gl_FragDepth;\n");
}

Expand Down Expand Up @@ -406,13 +406,13 @@ void ShaderWriter::EndVSMain(Slice<VaryingDef> 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");
Expand Down
16 changes: 10 additions & 6 deletions Common/GPU/ShaderWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,17 @@ struct VaryingDef {
const char *precision;
};

enum FSFlags {
FSFLAG_NONE = 0,
FSFLAG_WRITEDEPTH = 1,
enum class ShaderWriterFlags {
NONE = 0,
FS_WRITE_DEPTH = 1,
FS_AUTO_STEREO = 2,
};
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<const char *> extensions = Slice<const char *>()) : p_(buffer), lang_(lang), stage_(stage) {
ShaderWriter(char *buffer, const ShaderLanguageDesc &lang, ShaderStage stage, Slice<const char *> extensions = Slice<const char *>(), ShaderWriterFlags flags = ShaderWriterFlags::NONE) : p_(buffer), lang_(lang), stage_(stage) {
Preamble(extensions);
}
ShaderWriter(const ShaderWriter &) = delete;
Expand Down Expand Up @@ -77,6 +79,7 @@ class ShaderWriter {
void DeclareSamplers(Slice<SamplerDef> 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);
Expand All @@ -85,12 +88,12 @@ class ShaderWriter {

// Simple shaders with no special tricks.
void BeginVSMain(Slice<InputDef> inputs, Slice<UniformDef> uniforms, Slice<VaryingDef> varyings);
void BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> varyings, FSFlags flags);
void BeginFSMain(Slice<UniformDef> uniforms, Slice<VaryingDef> varyings);
void BeginGSMain(Slice<VaryingDef> varyings, Slice<VaryingDef> outVaryings);

// For simple shaders that output a single color, we can deal with this generically.
void EndVSMain(Slice<VaryingDef> varyings);
void EndFSMain(const char *vec4_color_variable, FSFlags flags);
void EndFSMain(const char *vec4_color_variable);
void EndGSMain();

const ShaderLanguageDesc &Lang() const {
Expand All @@ -116,4 +119,5 @@ class ShaderWriter {
char *p_;
const ShaderLanguageDesc &lang_;
const ShaderStage stage_;
ShaderWriterFlags flags_;
};
5 changes: 3 additions & 2 deletions GPU/Common/DepalettizeShaderCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ void GenerateDepalSmoothed(ShaderWriter &writer, const DepalConfig &config) {

void GenerateDepalFs(ShaderWriter &writer, const DepalConfig &config) {
writer.DeclareSamplers(samplers);
writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH);
writer.HighPrecisionFloat();
writer.BeginFSMain(config.bufferFormat == GE_FORMAT_DEPTH16 ? g_draw2Duniforms : Slice<UniformDef>::empty(), varyings, FSFLAG_NONE);
writer.BeginFSMain(config.bufferFormat == GE_FORMAT_DEPTH16 ? g_draw2Duniforms : Slice<UniformDef>::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.
Expand All @@ -369,5 +370,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");
}
23 changes: 13 additions & 10 deletions GPU/Common/Draw2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ const UniformBufferDesc draw2DUBDesc{ sizeof(Draw2DUB), {

Draw2DPipelineInfo GenerateDraw2DCopyColorFs(ShaderWriter &writer) {
writer.DeclareSamplers(samplers);
writer.BeginFSMain(Slice<UniformDef>::empty(), varyings, FSFLAG_NONE);
writer.BeginFSMain(Slice<UniformDef>::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",
Expand All @@ -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",
Expand All @@ -89,10 +89,11 @@ Draw2DPipelineInfo GenerateDraw2DCopyColorRect2LinFs(ShaderWriter &writer) {

Draw2DPipelineInfo GenerateDraw2DCopyDepthFs(ShaderWriter &writer) {
writer.DeclareSamplers(samplers);
writer.BeginFSMain(Slice<UniformDef>::empty(), varyings, FSFLAG_WRITEDEPTH);
writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH);
writer.BeginFSMain(Slice<UniformDef>::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",
Expand All @@ -103,15 +104,16 @@ Draw2DPipelineInfo GenerateDraw2DCopyDepthFs(ShaderWriter &writer) {

Draw2DPipelineInfo GenerateDraw2D565ToDepthFs(ShaderWriter &writer) {
writer.DeclareSamplers(samplers);
writer.BeginFSMain(Slice<UniformDef>::empty(), varyings, FSFLAG_WRITEDEPTH);
writer.SetFlags(ShaderWriterFlags::FS_WRITE_DEPTH);
writer.BeginFSMain(Slice<UniformDef>::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.
DepthScaleFactors factors = GetDepthScaleFactors();
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",
Expand All @@ -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.
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions GPU/Common/GeometryShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@


bool GenerateGeometryShader(const GShaderID &id, char *buffer, const ShaderLanguageDesc &compat, const Draw::Bugs bugs, std::string *errorString) {
std::vector<const char*> gl_exts;
std::vector<const char*> 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");
Expand Down
4 changes: 2 additions & 2 deletions GPU/Common/ReinterpretFramebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions GPU/Common/StencilCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 9de6f6d

Please sign in to comment.