From 61ed40749f01eb21c9053bb736a1cc8cc8f05e76 Mon Sep 17 00:00:00 2001 From: Tony Wasserka Date: Sun, 22 Sep 2013 18:07:21 +0200 Subject: [PATCH] Shader generators: Remove any references to D3D9 and cleanup the affected code. --- .../Core/VideoCommon/Src/PixelShaderGen.cpp | 66 ++++++---------- .../Src/TextureConversionShader.cpp | 78 ++++++------------- .../Core/VideoCommon/Src/VertexShaderGen.cpp | 30 ++----- Source/Core/VideoCommon/Src/VideoCommon.h | 7 +- .../Src/LineGeometryShader.cpp | 2 +- .../Plugin_VideoDX11/Src/PixelShaderCache.cpp | 6 +- .../Src/PointGeometryShader.cpp | 2 +- .../Src/VertexShaderCache.cpp | 6 +- Source/Plugins/Plugin_VideoDX11/Src/main.cpp | 2 +- 9 files changed, 62 insertions(+), 137 deletions(-) diff --git a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp index 84410bc643..b09f736f6e 100644 --- a/Source/Core/VideoCommon/Src/PixelShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/PixelShaderGen.cpp @@ -271,20 +271,15 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T for (int i = 0; i < 8; ++i) out.Write("uniform sampler2D samp%d;\n", i); } - else + else // D3D { // Declare samplers for (int i = 0; i < 8; ++i) - out.Write("%s samp%d : register(s%d);\n", (ApiType == API_D3D11) ? "sampler" : "uniform sampler2D", i, i); + out.Write("sampler samp%d : register(s%d);\n", i, i); - if (ApiType == API_D3D11) - { - out.Write("\n"); - for (int i = 0; i < 8; ++i) - { - out.Write("Texture2D Tex%d : register(t%d);\n", i, i); - } - } + out.Write("\n"); + for (int i = 0; i < 8; ++i) + out.Write("Texture2D Tex%d : register(t%d);\n", i, i); } out.Write("\n"); @@ -367,7 +362,7 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T out.Write("void main()\n{\n"); } - else + else // D3D { if (forced_early_z) { @@ -382,22 +377,12 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T } out.Write("void main(\n"); - if(ApiType != API_D3D11) - { - out.Write(" out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "", - per_pixel_depth ? "\n out float depth : DEPTH," : "", - ApiType & API_D3D9_SM20 ? "POSITION" : "VPOS"); - } - else - { - out.Write(" out float4 ocol0 : SV_Target0,%s%s\n in float4 rawpos : SV_Position,\n", - dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : SV_Target1," : "", - per_pixel_depth ? "\n out float depth : SV_Depth," : ""); - } + out.Write(" out float4 ocol0 : SV_Target0,%s%s\n in float4 rawpos : SV_Position,\n", + dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : SV_Target1," : "", + per_pixel_depth ? "\n out float depth : SV_Depth," : ""); - // "centroid" attribute is only supported by D3D11 - const char* optCentroid = (ApiType == API_D3D11 ? "centroid" : ""); + // Use centroid sampling to make MSAA work properly + const char* optCentroid = "centroid"; out.Write(" in %s float4 colors_0 : COLOR0,\n", optCentroid); out.Write(" in %s float4 colors_1 : COLOR1", optCentroid); @@ -623,10 +608,11 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T WriteAlphaTest(out, uid_data, ApiType, dstAlphaMode, per_pixel_depth); + // TODO: Make more sense out of this comment // D3D9 doesn't support readback of depth in pixel shader, so we always have to calculate it again. // This shouldn't be a performance issue as the written depth is usually still from perspective division // but this isn't true for z-textures, so there will be depth issues between enabled and disabled z-textures fragments - if ((ApiType == API_OPENGL || ApiType == API_D3D11) && g_ActiveConfig.bFastDepthCalc) + if (g_ActiveConfig.bFastDepthCalc) out.Write("float zCoord = rawpos.z;\n"); else { @@ -682,18 +668,10 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T if (dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND) { out.SetConstantsUsed(C_ALPHA, C_ALPHA); - if(ApiType & API_D3D9) - { - // alpha component must be 0 or the shader will not compile (Direct3D 9Ex restriction) - // Colors will be blended against the color from ocol1 in D3D 9... - out.Write("\tocol1 = float4(prev.a, prev.a, prev.a, 0.0);\n"); - } - else - { - // Colors will be blended against the alpha from ocol1... - out.Write("\tocol1 = prev;\n"); - } - // ...and the alpha from ocol0 will be written to the framebuffer. + + // Colors will be blended against the alpha from ocol1 and + // the alpha from ocol0 will be written to the framebuffer. + out.Write("\tocol1 = prev;\n"); out.Write("\tocol0.a = " I_ALPHA"[0].a;\n"); } @@ -1127,10 +1105,10 @@ void SampleTexture(T& out, const char *texcoords, const char *texswap, int texma { out.SetConstantsUsed(C_TEXDIMS+texmap,C_TEXDIMS+texmap); - if (ApiType == API_D3D11) + if (ApiType == API_D3D) out.Write("Tex%d.Sample(samp%d,%s.xy * " I_TEXDIMS"[%d].xy).%s;\n", texmap,texmap, texcoords, texmap, texswap); - else - out.Write("%s(samp%d,%s.xy * " I_TEXDIMS"[%d].xy).%s;\n", ApiType == API_OPENGL ? "texture" : "tex2D", texmap, texcoords, texmap, texswap); + else // OGL + out.Write("texture(samp%d,%s.xy * " I_TEXDIMS"[%d].xy).%s;\n", texmap, texcoords, texmap, texswap); } static const char *tevAlphaFuncsTable[] = @@ -1198,14 +1176,14 @@ static inline void WriteAlphaTest(T& out, pixel_shader_uid_data& uid_data, API_T // It seems to be less buggy than not to update the depth buffer if alpha test fails, // but both ways wouldn't be accurate. - // OpenGL 4.2 has a flag which allows the driver to still update the depth buffer + // OpenGL 4.2 has a flag which allows the driver to still update the depth buffer // if alpha test fails. The driver doesn't have to, but I assume they all do because // it's the much faster code path for the GPU. uid_data.alpha_test_use_zcomploc_hack = bpmem.UseEarlyDepthTest() && bpmem.zmode.updateenable && !g_ActiveConfig.backend_info.bSupportsEarlyZ; if (!uid_data.alpha_test_use_zcomploc_hack) { out.Write("\t\tdiscard;\n"); - if (ApiType != API_D3D11) + if (ApiType != API_D3D) out.Write("\t\treturn;\n"); } diff --git a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp index fec8b8afde..7916e47c12 100644 --- a/Source/Core/VideoCommon/Src/TextureConversionShader.cpp +++ b/Source/Core/VideoCommon/Src/TextureConversionShader.cpp @@ -83,38 +83,22 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) { WRITE(p, "#define samp0 samp9\n"); WRITE(p, "uniform sampler2DRect samp0;\n"); - } - else if (ApiType & API_D3D9) - { - WRITE(p,"uniform sampler samp0 : register(s0);\n"); - } - else - { - WRITE(p,"sampler samp0 : register(s0);\n"); - WRITE(p, "Texture2D Tex0 : register(t0);\n"); - } - if (ApiType == API_OPENGL) - { WRITE(p, " out vec4 ocol0;\n"); WRITE(p, " VARYIN float2 uv0;\n"); WRITE(p, "void main()\n"); } - else + else // D3D { + WRITE(p,"sampler samp0 : register(s0);\n"); + WRITE(p, "Texture2D Tex0 : register(t0);\n"); + WRITE(p,"void main(\n"); - if (ApiType != API_D3D11) - { - WRITE(p," out float4 ocol0 : COLOR0,\n"); - } - else - { - WRITE(p," out float4 ocol0 : SV_Target,\n"); - } + WRITE(p," out float4 ocol0 : SV_Target,\n"); WRITE(p," in float2 uv0 : TEXCOORD0)\n"); } - WRITE(p, "{\n" + WRITE(p, "{\n" " float2 sampleUv;\n" " float2 uv1 = floor(uv0);\n"); @@ -142,15 +126,15 @@ void WriteSwizzler(char*& p, u32 format, API_TYPE ApiType) if (ApiType != API_OPENGL) { - WRITE(p, " sampleUv = sampleUv + float2(0.0,1.0);\n");// still to determine the reason for this + WRITE(p, " sampleUv = sampleUv + float2(0.0,1.0);\n"); // still need to determine the reason for this WRITE(p, " sampleUv = sampleUv / " I_COLORS"[0].zw;\n"); } } -// block dimensions : widthStride, heightStride +// block dimensions : widthStride, heightStride // texture dims : width, height, x offset, y offset void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) -{ +{ // [0] left, top, right, bottom of source rectangle within source texture // [1] width and height of destination texture in pixels // Two were merged for GLSL @@ -164,39 +148,23 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) { WRITE(p, "#define samp0 samp9\n"); WRITE(p, "uniform sampler2DRect samp0;\n"); - } - else if (ApiType & API_D3D9) - { - WRITE(p,"uniform sampler samp0 : register(s0);\n"); - } - else - { - WRITE(p,"sampler samp0 : register(s0);\n"); - WRITE(p, "Texture2D Tex0 : register(t0);\n"); - } - if (ApiType == API_OPENGL) - { WRITE(p, " out float4 ocol0;\n"); WRITE(p, " VARYIN float2 uv0;\n"); WRITE(p, "void main()\n"); } else { + WRITE(p,"sampler samp0 : register(s0);\n"); + WRITE(p, "Texture2D Tex0 : register(t0);\n"); + WRITE(p,"void main(\n"); - if(ApiType != API_D3D11) - { - WRITE(p," out float4 ocol0 : COLOR0,\n"); - } - else - { - WRITE(p," out float4 ocol0 : SV_Target,\n"); - } + WRITE(p," out float4 ocol0 : SV_Target,\n"); WRITE(p," in float2 uv0 : TEXCOORD0)\n"); } - WRITE(p, "{\n" + WRITE(p, "{\n" " float2 sampleUv;\n" " float2 uv1 = floor(uv0);\n"); @@ -232,18 +200,16 @@ void Write32BitSwizzler(char*& p, u32 format, API_TYPE ApiType) void WriteSampleColor(char*& p, const char* colorComp, const char* dest, API_TYPE ApiType) { const char* texSampleOpName; - if (ApiType & API_D3D9) - texSampleOpName = "tex2D"; - else if (ApiType == API_D3D11) + if (ApiType == API_D3D) texSampleOpName = "tex0.Sample"; - else + else // OGL texSampleOpName = "texture2DRect"; // the increment of sampleUv.x is delayed, so we perform it here. see WriteIncrementSampleX. const char* texSampleIncrementUnit; - if (ApiType != API_OPENGL) + if (ApiType == API_D3D) texSampleIncrementUnit = I_COLORS"[0].x / " I_COLORS"[0].z"; - else + else // OGL texSampleIncrementUnit = I_COLORS"[0].x"; WRITE(p, " %s = %s(samp0, sampleUv + float2(%d.0 * (%s), 0.0)).%s;\n", @@ -296,7 +262,7 @@ void WriteEncoderEnd(char* p, API_TYPE ApiType) void WriteI8Encoder(char* p, API_TYPE ApiType) { WriteSwizzler(p, GX_TF_I8, ApiType); - WRITE(p, " float3 texSample;\n"); + WRITE(p, " float3 texSample;\n"); WriteSampleColor(p, "rgb", "texSample", ApiType); WriteColorToIntensity(p, "texSample", "ocol0.b"); @@ -430,7 +396,7 @@ void WriteRGB565Encoder(char* p,API_TYPE ApiType) WRITE(p, " float2 texRs = float2(texSample0.r, texSample1.r);\n"); WRITE(p, " float2 texGs = float2(texSample0.g, texSample1.g);\n"); WRITE(p, " float2 texBs = float2(texSample0.b, texSample1.b);\n"); - + WriteToBitDepth(p, 6, "texGs", "float2 gInt"); WRITE(p, " float2 gUpper = floor(gInt / 8.0);\n"); WRITE(p, " float2 gLower = gInt - gUpper * 8.0;\n"); @@ -888,12 +854,12 @@ const char *GenerateEncodingShader(u32 format,API_TYPE ApiType) break; default: PanicAlert("Unknown texture copy format: 0x%x\n", format); - break; + break; } if (text[sizeof(text) - 1] != 0x7C) PanicAlert("TextureConversionShader generator - buffer too small, canary has been eaten!"); - + #ifndef ANDROID uselocale(old_locale); // restore locale freelocale(locale); diff --git a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp index 39b478feb5..336979a09b 100644 --- a/Source/Core/VideoCommon/Src/VertexShaderGen.cpp +++ b/Source/Core/VideoCommon/Src/VertexShaderGen.cpp @@ -28,7 +28,7 @@ static void DefineVSOutputStructMember(T& object, API_TYPE api_type, const char* if (api_type == API_OPENGL) object.Write(";\n"); - else + else // D3D { if (semantic_index != -1) object.Write(" : %s%d;\n", semantic, semantic_index); @@ -167,7 +167,7 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ out.Write("void main()\n{\n"); } - else + else // D3D { out.Write("VS_OUTPUT main(\n"); @@ -197,19 +197,10 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ // transforms if (components & VB_HAS_POSMTXIDX) { - if (api_type & API_D3D9) - { - out.Write("int4 indices = D3DCOLORtoUBYTE4(blend_indices);\n"); - out.Write("int posmtx = indices.x;\n"); - } - else if (api_type == API_D3D11) - { - out.Write("int posmtx = blend_indices.x * 255.0;\n"); - } + if (api_type == API_D3D) + out.Write("int posmtx = blend_indices.x * 255.0;\n"); // TODO: Ugly, should use an integer instead else - { out.Write("int posmtx = int(fposmtx);\n"); - } if (is_writing_shadercode && (DriverDetails::HasBug(DriverDetails::BUG_NODYNUBOACCESS) && !DriverDetails::HasBug(DriverDetails::BUG_ANNIHILATEDUBOS)) ) { @@ -454,11 +445,11 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ //write the true depth value, if the game uses depth textures pixel shaders will override with the correct values //if not early z culling will improve speed - if (api_type & API_D3D9 || api_type == API_D3D11) + if (api_type == API_D3D) { out.Write("o.pos.z = " I_DEPTHPARAMS".x * o.pos.w + o.pos.z * " I_DEPTHPARAMS".y;\n"); } - else + else // OGL { // this results in a scale from -1..0 to -1..1 after perspective // divide @@ -482,13 +473,6 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ //seems to get rather complicated } - if (api_type & API_D3D9) - { - // D3D9 is addressing pixel centers instead of pixel boundaries in clip space. - // Thus we need to offset the final position by half a pixel - out.Write("o.pos = o.pos + float4(" I_DEPTHPARAMS".z, " I_DEPTHPARAMS".w, 0.f, 0.f);\n"); - } - if(api_type == API_OPENGL) { // Bit ugly here @@ -528,7 +512,7 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ out.Write("gl_Position = o.pos;\n"); out.Write("}\n"); } - else + else // D3D { out.Write("return o;\n}\n"); } diff --git a/Source/Core/VideoCommon/Src/VideoCommon.h b/Source/Core/VideoCommon/Src/VideoCommon.h index c85a6e382a..175bd8a5b0 100644 --- a/Source/Core/VideoCommon/Src/VideoCommon.h +++ b/Source/Core/VideoCommon/Src/VideoCommon.h @@ -85,11 +85,8 @@ struct TargetRectangle : public MathUtil::Rectangle typedef enum { API_OPENGL = 1, - API_D3D9_SM30 = 2, - API_D3D9_SM20 = 4, - API_D3D9 = 6, - API_D3D11 = 8, - API_NONE = 16 + API_D3D = 2, + API_NONE = 3 } API_TYPE; inline u32 RGBA8ToRGBA6ToRGBA8(u32 src) diff --git a/Source/Plugins/Plugin_VideoDX11/Src/LineGeometryShader.cpp b/Source/Plugins/Plugin_VideoDX11/Src/LineGeometryShader.cpp index 9009d39029..4b96953a3a 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/LineGeometryShader.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/LineGeometryShader.cpp @@ -172,7 +172,7 @@ bool LineGeometryShader::SetShader(u32 components, float lineWidth, static char buffer[16384]; ShaderCode code; code.SetBuffer(buffer); - GenerateVSOutputStructForGS(code, components, API_D3D11); + GenerateVSOutputStructForGS(code, components, API_D3D); code.Write("\n%s", LINE_GS_COMMON); std::stringstream numTexCoordsStream; diff --git a/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp b/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp index 1dd98edd2e..56b61e05cd 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/PixelShaderCache.cpp @@ -453,11 +453,11 @@ void PixelShaderCache::Shutdown() bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) { PixelShaderUid uid; - GetPixelShaderUid(uid, dstAlphaMode, API_D3D11, components); + GetPixelShaderUid(uid, dstAlphaMode, API_D3D, components); if (g_ActiveConfig.bEnableShaderDebugging) { PixelShaderCode code; - GeneratePixelShaderCode(code, dstAlphaMode, API_D3D11, components); + GeneratePixelShaderCode(code, dstAlphaMode, API_D3D, components); pixel_uid_checker.AddToIndexAndCheck(code, uid, "Pixel", "p"); } @@ -487,7 +487,7 @@ bool PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components) // Need to compile a new shader PixelShaderCode code; - GeneratePixelShaderCode(code, dstAlphaMode, API_D3D11, components); + GeneratePixelShaderCode(code, dstAlphaMode, API_D3D, components); D3DBlob* pbytecode; if (!D3D::CompilePixelShader(code.GetBuffer(), (unsigned int)strlen(code.GetBuffer()), &pbytecode)) diff --git a/Source/Plugins/Plugin_VideoDX11/Src/PointGeometryShader.cpp b/Source/Plugins/Plugin_VideoDX11/Src/PointGeometryShader.cpp index 9d6e02104e..f33d7444a2 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/PointGeometryShader.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/PointGeometryShader.cpp @@ -166,7 +166,7 @@ bool PointGeometryShader::SetShader(u32 components, float pointSize, static char buffer[16384]; ShaderCode code; code.SetBuffer(buffer); - GenerateVSOutputStructForGS(code, components, API_D3D11); + GenerateVSOutputStructForGS(code, components, API_D3D); code.Write("\n%s", POINT_GS_COMMON); std::stringstream numTexCoordsStream; diff --git a/Source/Plugins/Plugin_VideoDX11/Src/VertexShaderCache.cpp b/Source/Plugins/Plugin_VideoDX11/Src/VertexShaderCache.cpp index 6121824b8e..d9ea7e2a8c 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/VertexShaderCache.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/VertexShaderCache.cpp @@ -200,11 +200,11 @@ void VertexShaderCache::Shutdown() bool VertexShaderCache::SetShader(u32 components) { VertexShaderUid uid; - GetVertexShaderUid(uid, components, API_D3D11); + GetVertexShaderUid(uid, components, API_D3D); if (g_ActiveConfig.bEnableShaderDebugging) { VertexShaderCode code; - GenerateVertexShaderCode(code, components, API_D3D11); + GenerateVertexShaderCode(code, components, API_D3D); vertex_uid_checker.AddToIndexAndCheck(code, uid, "Vertex", "v"); } @@ -230,7 +230,7 @@ bool VertexShaderCache::SetShader(u32 components) } VertexShaderCode code; - GenerateVertexShaderCode(code, components, API_D3D11); + GenerateVertexShaderCode(code, components, API_D3D); D3DBlob* pbytecode = NULL; D3D::CompileVertexShader(code.GetBuffer(), (int)strlen(code.GetBuffer()), &pbytecode); diff --git a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp index 5b59ec2bd6..989fb2ef91 100644 --- a/Source/Plugins/Plugin_VideoDX11/Src/main.cpp +++ b/Source/Plugins/Plugin_VideoDX11/Src/main.cpp @@ -82,7 +82,7 @@ void InitBackendInfo() return; } - g_Config.backend_info.APIType = API_D3D11; + g_Config.backend_info.APIType = API_D3D; g_Config.backend_info.bUseRGBATextures = true; // the GX formats barely match any D3D11 formats g_Config.backend_info.bUseMinimalMipCount = true; g_Config.backend_info.bSupports3DVision = false;