From d4adc3abb8b8bf81f01266546bfe249b7f70ffa7 Mon Sep 17 00:00:00 2001 From: Ced2911 Date: Sun, 22 Sep 2013 11:27:40 +0200 Subject: [PATCH] [dx9] don't do alpha test in pixel shader, use state mapping hrydgard: Cleaned up a bit, disabled by default Conflicts: GPU/Directx9/TextureCacheDX9.cpp --- GPU/Directx9/PixelShaderGeneratorDX9.cpp | 17 +++-------------- GPU/Directx9/PixelShaderGeneratorDX9.h | 3 +++ GPU/Directx9/StateMappingDX9.cpp | 14 ++++++++++++++ GPU/Directx9/TextureCacheDX9.cpp | 2 -- GPU/Directx9/helper/dx_state.cpp | 4 ++++ GPU/Directx9/helper/dx_state.h | 4 ++++ GPU/GLES/FragmentShaderGenerator.cpp | 2 ++ 7 files changed, 30 insertions(+), 16 deletions(-) diff --git a/GPU/Directx9/PixelShaderGeneratorDX9.cpp b/GPU/Directx9/PixelShaderGeneratorDX9.cpp index 8c366a6346..134d92de2e 100644 --- a/GPU/Directx9/PixelShaderGeneratorDX9.cpp +++ b/GPU/Directx9/PixelShaderGeneratorDX9.cpp @@ -25,8 +25,6 @@ // #define DEBUG_SHADER -// GL_NV_shader_framebuffer_fetch looks interesting.... - namespace DX9 { static bool IsAlphaTestTriviallyTrue() { @@ -253,19 +251,16 @@ void GenerateFragmentShaderDX9(char *buffer) { WRITE(p, " float4 v = In.v_color0 %s;\n", secondary); } +#if !defined(DX9_USE_HW_ALPHA_TEST) if (enableAlphaTest) { GEComparison alphaTestFunc = gstate.getAlphaTestFunction(); const char *alphaTestFuncs[] = { "#", "#", " != ", " == ", " >= ", " > ", " <= ", " < " }; // never/always don't make sense if (alphaTestFuncs[alphaTestFunc][0] != '#') { - // WRITE(p, " if (roundAndScaleTo255f(v.a) %s u_alphacolorref.a) discard;\n", alphaTestFuncs[alphaTestFunc]); - //WRITE(p, "clip((roundAndScaleTo255f(v.rgb) %s u_alphacolorref.a)? -1:1);\n", alphaTestFuncs[alphaTestFunc]); - - //WRITE(p, " if (roundAndScaleTo255f(v.a) %s u_alphacolorref.a) clip(-1);\n", alphaTestFuncs[alphaTestFunc]); + // TODO: Rewrite this to use clip() appropriately (like, clip(v.a - u_alphacolorref.a)) WRITE(p, " if (roundTo255th(v.a) %s u_alphacolorref.a) clip(-1);\n", alphaTestFuncs[alphaTestFunc]); - //WRITE(p, " if (roundTo255th(v.a) %s u_alphacolorref.a) v.r=1;\n", alphaTestFuncs[alphaTestFunc]); } } - +#endif // TODO: Before or after the color test? if (enableColorDoubling && enableAlphaDoubling) { WRITE(p, " v = v * 2.0;\n"); @@ -280,15 +275,9 @@ void GenerateFragmentShaderDX9(char *buffer) { const char *colorTestFuncs[] = { "#", "#", " != ", " == " }; // never/always don't make sense u32 colorTestMask = gstate.getColorTestMask(); if (colorTestFuncs[colorTestFunc][0] != '#') { - //WRITE(p, "clip((roundAndScaleTo255v(v.rgb) %s u_alphacolorref.rgb)? -1:1);\n", colorTestFuncs[colorTestFunc]); - //WRITE(p, "if (roundAndScaleTo255v(v.rgb) %s u_alphacolorref.rgb) clip(-1);\n", colorTestFuncs[colorTestFunc]); - - // cleanup ? const char * test = colorTestFuncs[colorTestFunc]; - //WRITE(p, "float3 colortest = roundAndScaleTo255v(v.rgb);\n"); WRITE(p, "float3 colortest = roundTo255thv(v.rgb);\n"); WRITE(p, "if ((colortest.r %s u_alphacolorref.r) && (colortest.g %s u_alphacolorref.g) && (colortest.b %s u_alphacolorref.b )) clip(-1);\n", test, test, test); - } } diff --git a/GPU/Directx9/PixelShaderGeneratorDX9.h b/GPU/Directx9/PixelShaderGeneratorDX9.h index 83bd15f6dd..98c59fe8fb 100644 --- a/GPU/Directx9/PixelShaderGeneratorDX9.h +++ b/GPU/Directx9/PixelShaderGeneratorDX9.h @@ -19,6 +19,9 @@ #include "Globals.h" +// TODO: Bench both ways. Result may be different on old vs new hardware though.. +// #define DX9_USE_HW_ALPHA_TEST 1 + namespace DX9 { struct FragmentShaderIDDX9 diff --git a/GPU/Directx9/StateMappingDX9.cpp b/GPU/Directx9/StateMappingDX9.cpp index 97633dde68..0bc713b739 100644 --- a/GPU/Directx9/StateMappingDX9.cpp +++ b/GPU/Directx9/StateMappingDX9.cpp @@ -27,6 +27,7 @@ #include "GPU/Directx9/ShaderManagerDX9.h" #include "GPU/Directx9/TextureCacheDX9.h" #include "GPU/Directx9/FramebufferDX9.h" +#include "GPU/Directx9/PixelShaderGeneratorDX9.h" namespace DX9 { @@ -268,6 +269,19 @@ void TransformDrawEngineDX9::ApplyDrawState(int prim) { } } +#if defined(DX9_USE_HW_ALPHA_TEST) + // Older hardware (our target for DX9) often has separate alpha testing hardware that + // is generally faster than using discard/clip. Let's use it. + if (gstate.alphaTestEnable) { + dxstate.alphaTest.enable(); + GEComparison alphaTestFunc = gstate.getAlphaTestFunction(); + dxstate.alphaTestFunc.set(ztests[alphaTestFunc]); + dxstate.alphaTestRef.set(gstate.getAlphaTestRef()); + } else { + dxstate.alphaTest.disable(); + } +#endif + float renderWidthFactor, renderHeightFactor; float renderWidth, renderHeight; float renderX, renderY; diff --git a/GPU/Directx9/TextureCacheDX9.cpp b/GPU/Directx9/TextureCacheDX9.cpp index 8234ae32ce..65c4bfe427 100644 --- a/GPU/Directx9/TextureCacheDX9.cpp +++ b/GPU/Directx9/TextureCacheDX9.cpp @@ -1343,13 +1343,11 @@ void TextureCacheDX9::CheckAlpha(TexCacheEntry &entry, u32 *pixelData, u32 dstFm } static inline void copyTexture(int xoffset, int yoffset, int w, int h, int pitch, int srcfmt, int fmt, void * pSrc, void * pDst) { - // Swap color int y; switch(fmt) { case D3DFMT_R5G6B5: case D3DFMT_A4R4G4B4: case D3DFMT_A1R5G5B5: - // Really needed ? for(y = 0; y < h; y++) { const u16 *src = (const u16 *)((u8*)pSrc + (w*2) * y); u16 *dst = (u16*)((u8*)pDst + pitch * y); diff --git a/GPU/Directx9/helper/dx_state.cpp b/GPU/Directx9/helper/dx_state.cpp index 1f3dc0eaf9..67117fa4df 100644 --- a/GPU/Directx9/helper/dx_state.cpp +++ b/GPU/Directx9/helper/dx_state.cpp @@ -41,6 +41,10 @@ void DirectxState::Restore() { // viewport.restore(); count++; + alphaTest.restore(); count++; + alphaTestFunc.restore(); count++; + alphaTestRef.restore(); count++; + stencilTest.restore(); count++; stencilOp.restore(); count++; stencilFunc.restore(); count++; diff --git a/GPU/Directx9/helper/dx_state.h b/GPU/Directx9/helper/dx_state.h index bb9b520578..c39c272204 100644 --- a/GPU/Directx9/helper/dx_state.h +++ b/GPU/Directx9/helper/dx_state.h @@ -303,6 +303,10 @@ public: BoolState depthTest; + DxState1 alphaTestFunc; + DxState1 alphaTestRef; + BoolState alphaTest; + DxState1 depthFunc; DxState1 depthWrite; diff --git a/GPU/GLES/FragmentShaderGenerator.cpp b/GPU/GLES/FragmentShaderGenerator.cpp index 2298e87bc0..5c0d83dffa 100644 --- a/GPU/GLES/FragmentShaderGenerator.cpp +++ b/GPU/GLES/FragmentShaderGenerator.cpp @@ -392,11 +392,13 @@ void ComputeFragmentShaderID(FragmentShaderID *id) { } id0 |= (lmode & 1) << 11; +#if !defined(DX9_USE_HW_ALPHA_TEST) if (enableAlphaTest) { // 4 bits total. id0 |= 1 << 12; id0 |= gstate.getAlphaTestFunction() << 13; } +#endif if (enableColorTest) { // 3 bits total. id0 |= 1 << 16;