diff --git a/GPU/Common/ShaderCommon.h b/GPU/Common/ShaderCommon.h index 16fcae3ae1..2b945ad426 100644 --- a/GPU/Common/ShaderCommon.h +++ b/GPU/Common/ShaderCommon.h @@ -31,6 +31,8 @@ enum ShaderLanguage { HLSL_DX9, HLSL_D3D11, HLSL_D3D11_LEVEL9, + + TEST_GLSL_VULKAN, }; enum DebugShaderType { @@ -142,6 +144,7 @@ enum DoLightComputation { struct GLSLShaderCompat { int glslVersionNumber; bool gles; + bool vulkan; const char *varying_fs; const char *varying_vs; const char *attribute; diff --git a/GPU/GLES/FragmentShaderGeneratorGLES.cpp b/GPU/GLES/FragmentShaderGeneratorGLES.cpp index 1dd89bb2b4..11ce7cf1c1 100644 --- a/GPU/GLES/FragmentShaderGeneratorGLES.cpp +++ b/GPU/GLES/FragmentShaderGeneratorGLES.cpp @@ -25,6 +25,7 @@ #include "Core/Config.h" #include "GPU/Common/GPUStateUtils.h" #include "GPU/Common/ShaderId.h" +#include "GPU/Common/ShaderUniforms.h" #include "GPU/GLES/FragmentShaderGeneratorGLES.h" #include "GPU/GLES/FramebufferManagerGLES.h" #include "GPU/GLES/ShaderManagerGLES.h" @@ -33,11 +34,21 @@ #define WRITE p+=sprintf +static const char *vulkan_glsl_preamble = +"#version 450\n" +"#extension GL_ARB_separate_shader_objects : enable\n" +"#extension GL_ARB_shading_language_420pack : enable\n" +"#extension GL_ARB_conservative_depth : enable\n" +"#extension GL_ARB_shader_image_load_store : enable\n" +"#define splat3(x) vec3(x)\n\n"; + bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLShaderCompat &compat, uint64_t *uniformMask, std::string *errorString) { *uniformMask = 0; bool highpFog = false; bool highpTexcoord = false; + bool fragmentTestCache = g_Config.bFragmentTestCache && !compat.vulkan; + if (compat.gles) { // PowerVR needs highp to do the fog in MHU correctly. // Others don't, and some can't handle highp in the fragment shader. @@ -49,26 +60,30 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha char *p = buffer; - WRITE(p, "#version %d%s\n", compat.glslVersionNumber, compat.gles ? " es" : ""); - - if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE && gl_extensions.EXT_blend_func_extended) { - WRITE(p, "#extension GL_EXT_blend_func_extended : require\n"); - } - if (gl_extensions.EXT_gpu_shader4) { - WRITE(p, "#extension GL_EXT_gpu_shader4 : enable\n"); - } - if (compat.framebufferFetchExtension) { - WRITE(p, "%s\n", compat.framebufferFetchExtension); - } - if (!compat.gles) { - WRITE(p, "#define lowp\n"); - WRITE(p, "#define mediump\n"); - WRITE(p, "#define highp\n"); + if (compat.vulkan) { + WRITE(p, vulkan_glsl_preamble); } else { - WRITE(p, "precision lowp float;\n"); - } + WRITE(p, "#version %d%s\n", compat.glslVersionNumber, compat.gles ? " es" : ""); - WRITE(p, "#define splat3(x) vec3(x)\n"); + if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE && gl_extensions.EXT_blend_func_extended) { + WRITE(p, "#extension GL_EXT_blend_func_extended : require\n"); + } + if (gl_extensions.EXT_gpu_shader4) { + WRITE(p, "#extension GL_EXT_gpu_shader4 : enable\n"); + } + if (compat.framebufferFetchExtension) { + WRITE(p, "%s\n", compat.framebufferFetchExtension); + } + if (!compat.gles) { + WRITE(p, "#define lowp\n"); + WRITE(p, "#define mediump\n"); + WRITE(p, "#define highp\n"); + } else { + WRITE(p, "precision lowp float;\n"); + } + + WRITE(p, "#define splat3(x) vec3(x)\n"); + } bool lmode = id.Bit(FS_BIT_LMODE); bool doTexture = id.Bit(FS_BIT_DO_TEXTURE); @@ -97,118 +112,167 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha GEBlendSrcFactor replaceBlendFuncA = (GEBlendSrcFactor)id.Bits(FS_BIT_BLENDFUNC_A, 4); GEBlendDstFactor replaceBlendFuncB = (GEBlendDstFactor)id.Bits(FS_BIT_BLENDFUNC_B, 4); GEBlendMode replaceBlendEq = (GEBlendMode)id.Bits(FS_BIT_BLENDEQ, 3); + StencilValueType replaceAlphaWithStencilType = (StencilValueType)id.Bits(FS_BIT_REPLACE_ALPHA_WITH_STENCIL_TYPE, 4); bool isModeClear = id.Bit(FS_BIT_CLEARMODE); - if (shaderDepal && gl_extensions.IsGLES) { - WRITE(p, "precision highp int;\n"); - } - const char *shading = ""; - if (compat.glslES30) + if (compat.glslES30 || compat.vulkan) shading = doFlatShading ? "flat" : ""; - if (doTexture) - WRITE(p, "uniform sampler2D tex;\n"); + bool earlyFragmentTests = ((!enableAlphaTest && !enableColorTest) || testForceToZero) && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT); + bool useAdrenoBugWorkaround = id.Bit(FS_BIT_NO_DEPTH_CANNOT_DISCARD_STENCIL); - if (!isModeClear && replaceBlend > REPLACE_BLEND_STANDARD) { - *uniformMask |= DIRTY_SHADERBLEND; - if (!gstate_c.Supports(GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH) && replaceBlend == REPLACE_BLEND_COPY_FBO) { - if (!compat.texelFetch) { - WRITE(p, "uniform vec2 u_fbotexSize;\n"); - } - WRITE(p, "uniform sampler2D fbotex;\n"); + if (compat.vulkan) { + if (earlyFragmentTests) { + WRITE(p, "layout (early_fragment_tests) in;\n"); + } else if (useAdrenoBugWorkaround && !gstate_c.Supports(GPU_ROUND_FRAGMENT_DEPTH_TO_16BIT)) { + WRITE(p, "layout (depth_unchanged) out float gl_FragDepth;\n"); } - if (replaceBlendFuncA >= GE_SRCBLEND_FIXA) { - WRITE(p, "uniform vec3 u_blendFixA;\n"); - } - if (replaceBlendFuncB >= GE_DSTBLEND_FIXB) { - WRITE(p, "uniform vec3 u_blendFixB;\n"); - } - } - if (needShaderTexClamp && doTexture) { - *uniformMask |= DIRTY_TEXCLAMP; - WRITE(p, "uniform vec4 u_texclamp;\n"); - if (id.Bit(FS_BIT_TEXTURE_AT_OFFSET)) { - WRITE(p, "uniform vec2 u_texclampoff;\n"); + WRITE(p, "layout (std140, set = 0, binding = 3) uniform baseUBO {\n%s};\n", ub_baseStr); + if (doTexture) { + WRITE(p, "layout (binding = 0) uniform sampler2D tex;\n"); } - } - if (enableAlphaTest || enableColorTest) { - if (g_Config.bFragmentTestCache) { - WRITE(p, "uniform sampler2D testtex;\n"); - } else { - *uniformMask |= DIRTY_ALPHACOLORREF; - WRITE(p, "uniform vec4 u_alphacolorref;\n"); - if (compat.bitwiseOps && ((enableColorTest && !colorTestAgainstZero) || (enableAlphaTest && !alphaTestAgainstZero))) { - *uniformMask |= DIRTY_ALPHACOLORMASK; - WRITE(p, "uniform ivec4 u_alphacolormask;\n"); + if (!isModeClear && replaceBlend > REPLACE_BLEND_STANDARD) { + if (replaceBlend == REPLACE_BLEND_COPY_FBO) { + WRITE(p, "layout (binding = 1) uniform sampler2D fbotex;\n"); } } - } - if (shaderDepal) { - WRITE(p, "uniform sampler2D pal;\n"); - WRITE(p, "uniform int u_depal;\n"); - *uniformMask |= DIRTY_DEPAL; - } + if (shaderDepal) { + WRITE(p, "layout (binding = 2) uniform sampler2D pal;\n"); + } - StencilValueType replaceAlphaWithStencilType = (StencilValueType)id.Bits(FS_BIT_REPLACE_ALPHA_WITH_STENCIL_TYPE, 4); - if (stencilToAlpha && replaceAlphaWithStencilType == STENCIL_VALUE_UNIFORM) { - *uniformMask |= DIRTY_STENCILREPLACEVALUE; - WRITE(p, "uniform float u_stencilReplaceValue;\n"); - } - if (doTexture && texFunc == GE_TEXFUNC_BLEND) { - *uniformMask |= DIRTY_TEXENV; - WRITE(p, "uniform vec3 u_texenv;\n"); - } + WRITE(p, "layout (location = 1) %s in vec4 v_color0;\n", shading); + if (lmode) + WRITE(p, "layout (location = 2) %s in vec3 v_color1;\n", shading); + if (enableFog) { + WRITE(p, "layout (location = 3) in float v_fogdepth;\n"); + } + if (doTexture) { + WRITE(p, "layout (location = 0) in vec3 v_texcoord;\n"); + } - WRITE(p, "%s %s vec4 v_color0;\n", shading, compat.varying_fs); - if (lmode) - WRITE(p, "%s %s vec3 v_color1;\n", shading, compat.varying_fs); - if (enableFog) { - *uniformMask |= DIRTY_FOGCOLOR; - WRITE(p, "uniform vec3 u_fogcolor;\n"); - WRITE(p, "%s %s float v_fogdepth;\n", compat.varying_fs, highpFog ? "highp" : "mediump"); - } - if (doTexture) { - WRITE(p, "%s %s vec3 v_texcoord;\n", compat.varying_fs, highpTexcoord ? "highp" : "mediump"); - } - - if (!g_Config.bFragmentTestCache) { if (enableAlphaTest && !alphaTestAgainstZero) { - if (compat.bitwiseOps) { - WRITE(p, "int roundAndScaleTo255i(in float x) { return int(floor(x * 255.0 + 0.5)); }\n"); - } else if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) { - WRITE(p, "float roundTo255thf(in mediump float x) { mediump float y = x + (0.5/255.0); return y - fract(y * 255.0) * (1.0 / 255.0); }\n"); - } else { - WRITE(p, "float roundAndScaleTo255f(in float x) { return floor(x * 255.0 + 0.5); }\n"); - } + WRITE(p, "int roundAndScaleTo255i(in float x) { return int(floor(x * 255.0 + 0.5)); }\n"); } if (enableColorTest && !colorTestAgainstZero) { - if (compat.bitwiseOps) { - WRITE(p, "ivec3 roundAndScaleTo255iv(in vec3 x) { return ivec3(floor(x * 255.0 + 0.5)); }\n"); - } else if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) { - WRITE(p, "vec3 roundTo255thv(in vec3 x) { vec3 y = x + (0.5/255.0); return y - fract(y * 255.0) * (1.0 / 255.0); }\n"); - } else { - WRITE(p, "vec3 roundAndScaleTo255v(in vec3 x) { return floor(x * 255.0 + 0.5); }\n"); + WRITE(p, "ivec3 roundAndScaleTo255iv(in vec3 x) { return ivec3(floor(x * 255.0 + 0.5)); }\n"); + } + + WRITE(p, "layout (location = 0, index = 0) out vec4 fragColor0;\n"); + if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE) { + WRITE(p, "layout (location = 0, index = 1) out vec4 fragColor1;\n"); + } + } else { + if (shaderDepal && gl_extensions.IsGLES) { + WRITE(p, "precision highp int;\n"); + } + + if (doTexture) + WRITE(p, "uniform sampler2D tex;\n"); + + if (!isModeClear && replaceBlend > REPLACE_BLEND_STANDARD) { + *uniformMask |= DIRTY_SHADERBLEND; + if (!gstate_c.Supports(GPU_SUPPORTS_ANY_FRAMEBUFFER_FETCH) && replaceBlend == REPLACE_BLEND_COPY_FBO) { + if (!compat.texelFetch) { + WRITE(p, "uniform vec2 u_fbotexSize;\n"); + } + WRITE(p, "uniform sampler2D fbotex;\n"); + } + if (replaceBlendFuncA >= GE_SRCBLEND_FIXA) { + WRITE(p, "uniform vec3 u_blendFixA;\n"); + } + if (replaceBlendFuncB >= GE_DSTBLEND_FIXB) { + WRITE(p, "uniform vec3 u_blendFixB;\n"); } } - } - if (!strcmp(compat.fragColor0, "fragColor0")) { - const char *qualifierColor0 = "out"; - if (compat.lastFragData && !strcmp(compat.lastFragData, compat.fragColor0)) { - qualifierColor0 = "inout"; + if (needShaderTexClamp && doTexture) { + *uniformMask |= DIRTY_TEXCLAMP; + WRITE(p, "uniform vec4 u_texclamp;\n"); + if (id.Bit(FS_BIT_TEXTURE_AT_OFFSET)) { + WRITE(p, "uniform vec2 u_texclampoff;\n"); + } } - // Output the output color definitions. - if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE) { - WRITE(p, "%s vec4 fragColor0;\n", qualifierColor0); - WRITE(p, "out vec4 fragColor1;\n"); - } else { - WRITE(p, "%s vec4 fragColor0;\n", qualifierColor0); + + if (enableAlphaTest || enableColorTest) { + if (g_Config.bFragmentTestCache) { + WRITE(p, "uniform sampler2D testtex;\n"); + } else { + *uniformMask |= DIRTY_ALPHACOLORREF; + WRITE(p, "uniform vec4 u_alphacolorref;\n"); + if (compat.bitwiseOps && ((enableColorTest && !colorTestAgainstZero) || (enableAlphaTest && !alphaTestAgainstZero))) { + *uniformMask |= DIRTY_ALPHACOLORMASK; + WRITE(p, "uniform ivec4 u_alphacolormask;\n"); + } + } } + + if (shaderDepal) { + WRITE(p, "uniform sampler2D pal;\n"); + WRITE(p, "uniform int u_depal_mask_shift_off_fmt;\n"); + *uniformMask |= DIRTY_DEPAL; + } + + if (stencilToAlpha && replaceAlphaWithStencilType == STENCIL_VALUE_UNIFORM) { + *uniformMask |= DIRTY_STENCILREPLACEVALUE; + WRITE(p, "uniform float u_stencilReplaceValue;\n"); + } + if (doTexture && texFunc == GE_TEXFUNC_BLEND) { + *uniformMask |= DIRTY_TEXENV; + WRITE(p, "uniform vec3 u_texenv;\n"); + } + + WRITE(p, "%s %s vec4 v_color0;\n", shading, compat.varying_fs); + if (lmode) + WRITE(p, "%s %s vec3 v_color1;\n", shading, compat.varying_fs); + if (enableFog) { + *uniformMask |= DIRTY_FOGCOLOR; + WRITE(p, "uniform vec3 u_fogcolor;\n"); + WRITE(p, "%s %s float v_fogdepth;\n", compat.varying_fs, highpFog ? "highp" : "mediump"); + } + if (doTexture) { + WRITE(p, "%s %s vec3 v_texcoord;\n", compat.varying_fs, highpTexcoord ? "highp" : "mediump"); + } + + if (!g_Config.bFragmentTestCache) { + if (enableAlphaTest && !alphaTestAgainstZero) { + if (compat.bitwiseOps) { + WRITE(p, "int roundAndScaleTo255i(in float x) { return int(floor(x * 255.0 + 0.5)); }\n"); + } else if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) { + WRITE(p, "float roundTo255thf(in mediump float x) { mediump float y = x + (0.5/255.0); return y - fract(y * 255.0) * (1.0 / 255.0); }\n"); + } else { + WRITE(p, "float roundAndScaleTo255f(in float x) { return floor(x * 255.0 + 0.5); }\n"); + } + } + if (enableColorTest && !colorTestAgainstZero) { + if (compat.bitwiseOps) { + WRITE(p, "ivec3 roundAndScaleTo255iv(in vec3 x) { return ivec3(floor(x * 255.0 + 0.5)); }\n"); + } else if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) { + WRITE(p, "vec3 roundTo255thv(in vec3 x) { vec3 y = x + (0.5/255.0); return y - fract(y * 255.0) * (1.0 / 255.0); }\n"); + } else { + WRITE(p, "vec3 roundAndScaleTo255v(in vec3 x) { return floor(x * 255.0 + 0.5); }\n"); + } + } + } + + if (!strcmp(compat.fragColor0, "fragColor0")) { + const char *qualifierColor0 = "out"; + if (compat.lastFragData && !strcmp(compat.lastFragData, compat.fragColor0)) { + qualifierColor0 = "inout"; + } + // Output the output color definitions. + if (stencilToAlpha == REPLACE_ALPHA_DUALSOURCE) { + WRITE(p, "%s vec4 fragColor0;\n", qualifierColor0); + WRITE(p, "out vec4 fragColor1;\n"); + } else { + WRITE(p, "%s vec4 fragColor0;\n", qualifierColor0); + } + } + } // PowerVR needs a custom modulo function. For some reason, this has far higher precision than the builtin one. @@ -217,7 +281,6 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha } WRITE(p, "void main() {\n"); - if (isModeClear) { // Clear mode does not allow any fancy shading. WRITE(p, " vec4 v = v_color0;\n"); @@ -292,7 +355,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha } WRITE(p, " vec2 tsize = vec2(textureSize(tex, 0));\n"); WRITE(p, " vec2 fraction;\n"); - WRITE(p, " bool bilinear = (u_depal >> 31) != 0;\n"); + WRITE(p, " bool bilinear = (u_depal_mask_shift_off_fmt >> 31) != 0;\n"); WRITE(p, " if (bilinear) {\n"); WRITE(p, " uv_round = uv * tsize - vec2(0.5, 0.5);\n"); WRITE(p, " fraction = fract(uv_round);\n"); @@ -304,10 +367,10 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha WRITE(p, " vec4 t1 = %sOffset(tex, uv_round, ivec2(1, 0));\n", compat.texture); WRITE(p, " vec4 t2 = %sOffset(tex, uv_round, ivec2(0, 1));\n", compat.texture); WRITE(p, " vec4 t3 = %sOffset(tex, uv_round, ivec2(1, 1));\n", compat.texture); - WRITE(p, " int depalMask = (u_depal & 0xFF);\n"); - WRITE(p, " int depalShift = ((u_depal >> 8) & 0xFF);\n"); - WRITE(p, " int depalOffset = (((u_depal >> 16) & 0xFF) << 4);\n"); - WRITE(p, " int depalFmt = ((u_depal >> 24) & 0x3);\n"); + WRITE(p, " int depalMask = (u_depal_mask_shift_off_fmt & 0xFF);\n"); + WRITE(p, " int depalShift = ((u_depal_mask_shift_off_fmt >> 8) & 0xFF);\n"); + WRITE(p, " int depalOffset = (((u_depal_mask_shift_off_fmt >> 16) & 0xFF) << 4);\n"); + WRITE(p, " int depalFmt = ((u_depal_mask_shift_off_fmt >> 24) & 0x3);\n"); WRITE(p, " ivec4 col; int index0; int index1; int index2; int index3;\n"); WRITE(p, " switch (depalFmt) {\n"); // We might want to include fmt in the shader ID if this is a performance issue. WRITE(p, " case 0:\n"); // 565 @@ -450,7 +513,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha // Texture access is at half texels [0.5/256, 255.5/256], but colors are normalized [0, 255]. // So we have to scale to account for the difference. std::string alphaTestXCoord = "0"; - if (g_Config.bFragmentTestCache) { + if (fragmentTestCache) { if (enableColorTest && !colorTestAgainstZero) { WRITE(p, " vec4 vScale256 = v * %f + %f;\n", 255.0 / 256.0, 0.5 / 256.0); alphaTestXCoord = "vScale256.a"; @@ -476,7 +539,7 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha // Maybe we could discard the drawcall, but it's pretty rare. Let's just statically discard here. WRITE(p, " %s\n", discardStatement); } - } else if (g_Config.bFragmentTestCache) { + } else if (fragmentTestCache) { WRITE(p, " float aResult = %s(testtex, vec2(%s, 0)).a;\n", compat.texture, alphaTestXCoord.c_str()); WRITE(p, " if (aResult < 0.5) %s\n", discardStatement); } else { @@ -530,9 +593,14 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha if (compat.bitwiseOps) { // Apparently GLES3 does not support vector bitwise ops. WRITE(p, " ivec3 v_scaled = roundAndScaleTo255iv(v.rgb);\n"); - const char *maskedFragColor = "ivec3(v_scaled.r & u_alphacolormask.r, v_scaled.g & u_alphacolormask.g, v_scaled.b & u_alphacolormask.b)"; - const char *maskedColorRef = "ivec3(int(u_alphacolorref.r) & u_alphacolormask.r, int(u_alphacolorref.g) & u_alphacolormask.g, int(u_alphacolorref.b) & u_alphacolormask.b)"; - WRITE(p, " if (%s %s %s) %s\n", maskedFragColor, colorTestFuncs[colorTestFunc], maskedColorRef, discardStatement); + if (compat.vulkan) { + // TODO: Use this for GL as well? + WRITE(p, " if ((v_scaled & u_alphacolormask.rgb) %s (u_alphacolorref.rgb & u_alphacolormask.rgb)) %s\n", colorTestFuncs[colorTestFunc], discardStatement); + } else { + const char *maskedFragColor = "ivec3(v_scaled.r & u_alphacolormask.r, v_scaled.g & u_alphacolormask.g, v_scaled.b & u_alphacolormask.b)"; + const char *maskedColorRef = "ivec3(int(u_alphacolorref.r) & u_alphacolormask.r, int(u_alphacolorref.g) & u_alphacolormask.g, int(u_alphacolorref.b) & u_alphacolormask.b)"; + WRITE(p, " if (%s %s %s) %s\n", maskedFragColor, colorTestFuncs[colorTestFunc], maskedColorRef, discardStatement); + } } else if (gl_extensions.gpuVendor == GPU_VENDOR_IMGTEC) { WRITE(p, " if (roundTo255thv(v.rgb) %s u_alphacolorref.rgb) %s\n", colorTestFuncs[colorTestFunc], discardStatement); } else { @@ -567,6 +635,11 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha default: srcFactor = "u_blendFixA"; break; } + if (!strcmp(srcFactor, "ERROR")) { + *errorString = "Bad replaceblend src factor"; + return false; + } + WRITE(p, " v.rgb = v.rgb * %s;\n", srcFactor); } @@ -632,6 +705,9 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha case GE_BLENDMODE_ABSDIFF: WRITE(p, " v.rgb = abs(v.rgb - destColor.rgb);\n"); break; + default: + *errorString = "Bad replace blend eq"; + return false; } } @@ -729,6 +805,10 @@ bool GenerateFragmentShaderGLSL(const FShaderID &id, char *buffer, const GLSLSha WRITE(p, " z = (1.0/65535.0) * floor(z * 65535.0);\n"); } WRITE(p, " gl_FragDepth = z;\n"); + } else if (!earlyFragmentTests && useAdrenoBugWorkaround) { + // Adreno (and possibly MESA/others) apply early frag tests even with discard in the shader. + // Writing depth prevents the bug, even with depth_unchanged specified. + WRITE(p, " gl_FragDepth = gl_FragCoord.z;\n"); } WRITE(p, "}\n"); diff --git a/GPU/GLES/ShaderManagerGLES.cpp b/GPU/GLES/ShaderManagerGLES.cpp index 4be9aace83..ccae98787a 100644 --- a/GPU/GLES/ShaderManagerGLES.cpp +++ b/GPU/GLES/ShaderManagerGLES.cpp @@ -166,7 +166,7 @@ LinkedShader::LinkedShader(GLRenderManager *render, VShaderID VSID, Shader *vs, queries.push_back({ &u_tess_weights_u, "u_tess_weights_u" }); queries.push_back({ &u_tess_weights_v, "u_tess_weights_v" }); queries.push_back({ &u_spline_counts, "u_spline_counts" }); - queries.push_back({ &u_depal, "u_depal" }); + queries.push_back({ &u_depal_mask_shift_off_fmt, "u_depal_mask_shift_off_fmt" }); attrMask = vs->GetAttrMask(); availableUniforms = vs->GetUniformMask() | fs->GetUniformMask(); @@ -298,7 +298,7 @@ void LinkedShader::UpdateUniforms(u32 vertType, const ShaderID &vsid, bool useBu uint32_t val = BytesToUint32(indexMask, indexShift, indexOffset, format); // Poke in a bilinear filter flag in the top bit. val |= gstate.isMagnifyFilteringEnabled() << 31; - render_->SetUniformI1(&u_depal, val); + render_->SetUniformI1(&u_depal_mask_shift_off_fmt, val); } // Update any dirty uniforms before we draw diff --git a/GPU/GLES/ShaderManagerGLES.h b/GPU/GLES/ShaderManagerGLES.h index 7d652311f1..c1c968dc76 100644 --- a/GPU/GLES/ShaderManagerGLES.h +++ b/GPU/GLES/ShaderManagerGLES.h @@ -88,7 +88,7 @@ public: // Shader depal int u_pal; // the texture - int u_depal; // the params + int u_depal_mask_shift_off_fmt; // the params // Fragment processing inputs int u_alphacolorref; diff --git a/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp b/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp index 06ddde1bd4..29579a11e4 100644 --- a/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp +++ b/GPU/Vulkan/FragmentShaderGeneratorVulkan.cpp @@ -562,10 +562,10 @@ bool GenerateFragmentShaderVulkanGLSL(const FShaderID &id, char *buffer, uint32_ LogicOpReplaceType replaceLogicOpType = (LogicOpReplaceType)id.Bits(FS_BIT_REPLACE_LOGIC_OP_TYPE, 2); switch (replaceLogicOpType) { case LOGICOPTYPE_ONE: - WRITE(p, " fragColor0.rgb = splat3(1.0);\n"); + WRITE(p, " fragColor0.rgb = vec3(1.0, 1.0, 1.0);\n"); break; case LOGICOPTYPE_INVERT: - WRITE(p, " fragColor0.rgb = splat3(1.0) - fragColor0.rgb;\n"); + WRITE(p, " fragColor0.rgb = vec3(1.0, 1.0, 1.0) - fragColor0.rgb;\n"); break; case LOGICOPTYPE_NORMAL: break; diff --git a/unittest/TestShaderGenerators.cpp b/unittest/TestShaderGenerators.cpp index e295ffd91d..cb28331bb0 100644 --- a/unittest/TestShaderGenerators.cpp +++ b/unittest/TestShaderGenerators.cpp @@ -1,3 +1,5 @@ +#include + #include "Common/StringUtils.h" #include "GPU/Common/ShaderId.h" @@ -20,6 +22,23 @@ #include "GPU/D3D9/D3DCompilerLoader.h" #include "GPU/D3D9/D3D9ShaderCompiler.h" +void SetupCompatForVulkan(GLSLShaderCompat &compat) { + compat.fragColor0 = "fragColor0"; + compat.fragColor1 = "fragColor1"; + compat.varying_fs = "in"; + compat.varying_vs = "out"; + compat.attribute = "in"; + compat.bitwiseOps = true; + compat.framebufferFetchExtension = false; + compat.gles = false; + compat.glslES30 = true; + compat.glslVersionNumber = 450; + compat.lastFragData = nullptr; + compat.texture = "texture"; + compat.texelFetch = "texelFetch"; + compat.vulkan = true; +} + bool GenerateFShader(FShaderID id, char *buffer, ShaderLanguage lang, std::string *errorString) { switch (lang) { case ShaderLanguage::HLSL_D3D11: @@ -30,6 +49,13 @@ bool GenerateFShader(FShaderID id, char *buffer, ShaderLanguage lang, std::strin return false; case ShaderLanguage::GLSL_VULKAN: return GenerateFragmentShaderVulkanGLSL(id, buffer, 0, errorString); + case ShaderLanguage::TEST_GLSL_VULKAN: + { + GLSLShaderCompat compat{}; + SetupCompatForVulkan(compat); + uint64_t uniformMask; + return GenerateFragmentShaderGLSL(id, buffer, compat, &uniformMask, errorString); + } case ShaderLanguage::GLSL_140: case ShaderLanguage::GLSL_300: // TODO: Need a device - except that maybe glslang could be used to verify these .... @@ -75,24 +101,54 @@ bool TestCompileShader(const char *buffer, ShaderLanguage lang, bool vertex) { return result; } case ShaderLanguage::GLSL_140: - return false; case ShaderLanguage::GLSL_300: - return false; + case ShaderLanguage::TEST_GLSL_VULKAN: + // Temporary while testing the vulkan->glsl merge + return true; + default: return false; } } +void PrintDiff(const char *a, const char *b) { + // Stupidest diff ever: Just print both lines, and a few around it, when we find a mismatch. + std::vector a_lines; + std::vector b_lines; + SplitString(a, '\n', a_lines); + SplitString(b, '\n', b_lines); + for (size_t i = 0; i < a_lines.size() && i < b_lines.size(); i++) { + if (a_lines[i] != b_lines[i]) { + // Print some context + for (size_t j = std::max((int)i - 4, 0); j < i; j++) { + printf("%s\n", a_lines[j].c_str()); + } + printf("DIFF found at line %d:\n", (int)i); + printf("a: %s\n", a_lines[i].c_str()); + printf("b: %s\n", b_lines[i].c_str()); + printf("...continues...\n"); + for (size_t j = i; j < i + 4 && j < a_lines.size(); j++) { + printf("a: %s\n", a_lines[j].c_str()); + printf("b: %s\n", b_lines[j].c_str()); + } + printf("==================\n"); + return; + } + } +} + + bool TestShaderGenerators() { LoadD3D11(); init_glslang(); LoadD3DCompilerDynamic(); ShaderLanguage languages[] = { - ShaderLanguage::HLSL_D3D11, + ShaderLanguage::TEST_GLSL_VULKAN, ShaderLanguage::GLSL_VULKAN, + ShaderLanguage::HLSL_D3D11, ShaderLanguage::GLSL_140, ShaderLanguage::GLSL_300, ShaderLanguage::HLSL_DX9, @@ -106,7 +162,7 @@ bool TestShaderGenerators() { } GMRng rng; int successes = 0; - int count = 200; + int count = 700; // Generate a bunch of random fragment shader IDs, try to generate shader source. // Then compile it and check that it's ok. @@ -128,6 +184,17 @@ bool TestShaderGenerators() { // We ignore the contents of the error string here, not even gonna try to compile if it errors. } + // Temporary test: Compare GLSL-in-Vulkan-mode vs Vulkan + if (generateSuccess[0] != generateSuccess[1]) { + printf("mismatching success!\n"); + return 1; + } + if (generateSuccess[0] && strcmp(buffer[0], buffer[1])) { + printf("mismatching shaders!\n"); + PrintDiff(buffer[0], buffer[1]); + return 1; + } + // Now that we have the strings ready for easy comparison (buffer,4 in the watch window), // let's try to compile them. for (int j = 0; j < numLanguages; j++) {