From 32820d2036749005229ccfb62a91608e3d4296af Mon Sep 17 00:00:00 2001 From: hrydgard Date: Wed, 22 Oct 2008 18:39:46 +0000 Subject: [PATCH] Backport Donko's hackfix to DX9 plugin. Move some code around. Add a couple of sanity checks. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@937 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Plugin_VideoDX9/Src/PixelShader.cpp | 17 +++++---- .../Plugin_VideoDX9/Src/ShaderManager.cpp | 8 ++-- .../Plugins/Plugin_VideoOGL/Src/BPStructs.cpp | 7 ++-- .../Plugin_VideoOGL/Src/PixelShader.cpp | 35 ++++++++++-------- .../Plugin_VideoOGL/Src/TextureMngr.cpp | 8 ++-- .../Plugin_VideoOGL/Src/VertexLoader.h | 37 ------------------- .../Plugin_VideoOGL/Src/VertexShader.cpp | 8 +++- .../Plugin_VideoOGL/Src/VertexShader.h | 37 +++++++++++++++++++ 8 files changed, 83 insertions(+), 74 deletions(-) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp index edd7ad9326..e2dc4ccf6a 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp @@ -283,14 +283,13 @@ char swapModeTable[4][5]; void BuildSwapModeTable() { - //bpmem.tevregs[0]. for (int i = 0; i < 4; i++) { - swapModeTable[i][0]=swapColors[bpmem.tevksel[i*2].swap1]; - swapModeTable[i][1]=swapColors[bpmem.tevksel[i*2].swap2]; - swapModeTable[i][2]=swapColors[bpmem.tevksel[i*2+1].swap1]; - swapModeTable[i][3]=swapColors[bpmem.tevksel[i*2+1].swap2]; - swapModeTable[i][4]=0; + swapModeTable[i][0] = swapColors[bpmem.tevksel[i*2].swap1]; + swapModeTable[i][1] = swapColors[bpmem.tevksel[i*2].swap2]; + swapModeTable[i][2] = swapColors[bpmem.tevksel[i*2+1].swap1]; + swapModeTable[i][3] = swapColors[bpmem.tevksel[i*2+1].swap2]; + swapModeTable[i][4] = 0; } } @@ -336,9 +335,11 @@ float3 comp16 = float3(1,255,0), comp24 = float3(1,255,255*255);\n\ WriteAlphaTest(p); + /* see GL shader generator - this is Donko's hack if (bpmem.dstalpha.enable) WRITE(p," return float4(prev.rgb,constalpha.x);\n"); else + */ WRITE(p," return prev;\n"); WRITE(p,"}\n\0"); @@ -348,8 +349,8 @@ float3 comp16 = float3(1,255,0), comp24 = float3(1,255,255*255);\n\ void WriteStage(char *&p, int n) { - char *rasswap = swapModeTable[bpmem.combiners[n].alphaC.rswap]; - char *texswap = swapModeTable[bpmem.combiners[n].alphaC.tswap]; + const char *rasswap = swapModeTable[bpmem.combiners[n].alphaC.rswap]; + const char *texswap = swapModeTable[bpmem.combiners[n].alphaC.tswap]; int texfun = xfregs.texcoords[n].texmtxinfo.projection; diff --git a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp index 3277c1ae44..6db6e0801d 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp @@ -118,7 +118,7 @@ void PShaderCache::Cleanup() PSCache::iterator iter; iter = pshaders.begin(); - while(iter != pshaders.end()) + while (iter != pshaders.end()) { PSCacheEntry &entry = iter->second; if (entry.frameCount < frameCount-30) @@ -128,7 +128,7 @@ void PShaderCache::Cleanup() } else { - iter++; + iter++; } } SETSTAT(stats.numPixelShadersAlive, (int)pshaders.size()); @@ -145,7 +145,7 @@ void VShaderCache::Init() void VShaderCache::Shutdown() { VSCache::iterator iter = vshaders.begin(); - for (;iter!=vshaders.end();iter++) + for (; iter != vshaders.end(); iter++) iter->second.Destroy(); vshaders.clear(); } @@ -157,7 +157,7 @@ void VShaderCache::SetShader() if (D3D::GetShaderVersion() < 2) return; // we are screwed - if(shader) { + if (shader) { //D3D::dev->SetVertexShader(shader); return; } diff --git a/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp b/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp index 162322ccd1..3387a941f2 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/BPStructs.cpp @@ -65,8 +65,9 @@ void BPInit() bpmem.bpMask = 0xFFFFFF; } - -// Called att the end of every: OpcodeDecoding.cpp ExecuteDisplayList > Decode() > LoadBPReg +// Called at the end of every: OpcodeDecoding.cpp ExecuteDisplayList > Decode() > LoadBPReg +// TODO - turn into function table. The (future) DL jit can then call the functions directly, +// getting rid of dynamic dispatch. void BPWritten(int addr, int changes, int newval) { //static int count = 0; @@ -448,7 +449,7 @@ void BPWritten(int addr, int changes, int newval) // EFB to XFB if(g_Config.bUseXFB) { - XFB_Write(Memory_GetPtr(bpmem.copyTexDest<<5), multirc, (bpmem.copyMipMapStrideChannels << 4), bpmem.copyTexSrcWH.y + 1, bpmem.dispcopyyscale/256.0f); + XFB_Write(Memory_GetPtr(bpmem.copyTexDest<<5), multirc, (bpmem.copyMipMapStrideChannels << 4), bpmem.copyTexSrcWH.y + 1, bpmem.dispcopyyscale / 256.0f); } else { diff --git a/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp b/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp index 6e3b3c283c..31cc8d7935 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/PixelShader.cpp @@ -35,10 +35,10 @@ // output is given by .outreg // tevtemp is set according to swapmodetables and -void WriteStage(char *&p, int n, u32 texture_mask); -void WrapNonPow2Tex(char* &p, const char* var, int texmap, u32 texture_mask); -void WriteAlphaCompare(char *&p, int num, int comp); -bool WriteAlphaTest(char *&p); +static void WriteStage(char *&p, int n, u32 texture_mask); +static void WrapNonPow2Tex(char* &p, const char* var, int texmap, u32 texture_mask); +static void WriteAlphaCompare(char *&p, int num, int comp); +static bool WriteAlphaTest(char *&p); const float epsilon8bit = 1.0f / 255.0f; @@ -257,23 +257,25 @@ static const char* tevIndFmtScale[] = {"255.0f", "31.0f", "15.0f", "8.0f" }; static const char *swapColors = "rgba"; static char swapModeTable[4][5]; + static char text[16384]; -void BuildSwapModeTable() +static void BuildSwapModeTable() { //bpmem.tevregs[0]. for (int i = 0; i < 4; i++) { - swapModeTable[i][0]=swapColors[bpmem.tevksel[i*2].swap1]; - swapModeTable[i][1]=swapColors[bpmem.tevksel[i*2].swap2]; - swapModeTable[i][2]=swapColors[bpmem.tevksel[i*2+1].swap1]; - swapModeTable[i][3]=swapColors[bpmem.tevksel[i*2+1].swap2]; - swapModeTable[i][4]=0; + swapModeTable[i][0] = swapColors[bpmem.tevksel[i*2].swap1]; + swapModeTable[i][1] = swapColors[bpmem.tevksel[i*2].swap2]; + swapModeTable[i][2] = swapColors[bpmem.tevksel[i*2+1].swap1]; + swapModeTable[i][3] = swapColors[bpmem.tevksel[i*2+1].swap2]; + swapModeTable[i][4] = 0; } } char *GeneratePixelShader(u32 texture_mask, bool has_zbuffer_target, bool bRenderZToCol0) { + text[sizeof(text) - 1] = 0x7C; // canary DVSTARTPROFILE(); BuildSwapModeTable(); @@ -283,7 +285,7 @@ char *GeneratePixelShader(u32 texture_mask, bool has_zbuffer_target, bool bRende char *p = text; WRITE(p, "//Pixel Shader for TEV stages\n"); WRITE(p, "//%i TEV stages, %i texgens, %i IND stages\n", - numStages,numTexgen,bpmem.genMode.numindstages); + numStages, numTexgen, bpmem.genMode.numindstages); bool bRenderZ = has_zbuffer_target && bpmem.zmode.updateenable; bool bOutputZ = bpmem.ztex2.op != ZTEXTURE_DISABLE; @@ -305,7 +307,7 @@ char *GeneratePixelShader(u32 texture_mask, bool has_zbuffer_target, bool bRende } } - // samplers + // Declare samplers if (texture_mask) { WRITE(p, "uniform samplerRECT "); bool bfirst = true; @@ -455,11 +457,12 @@ char *GeneratePixelShader(u32 texture_mask, bool has_zbuffer_target, bool bRende } } WRITE(p, "}\n"); - + if (text[sizeof(text) - 1] != 0x7C) + PanicAlert("PixelShader generator - buffer too small, canary has been eaten!"); return text; } -void WriteStage(char *&p, int n, u32 texture_mask) +static void WriteStage(char *&p, int n, u32 texture_mask) { char *rasswap = swapModeTable[bpmem.combiners[n].alphaC.rswap]; char *texswap = swapModeTable[bpmem.combiners[n].alphaC.tswap]; @@ -775,7 +778,7 @@ void WrapNonPow2Tex(char* &p, const char* var, int texmap, u32 texture_mask) } } -void WriteAlphaCompare(char *&p, int num, int comp) +static void WriteAlphaCompare(char *&p, int num, int comp) { switch(comp) { case ALPHACMP_ALWAYS: WRITE(p, "(false)"); break; @@ -789,7 +792,7 @@ void WriteAlphaCompare(char *&p, int num, int comp) } } -bool WriteAlphaTest(char *&p) +static bool WriteAlphaTest(char *&p) { u32 op = bpmem.alphaFunc.logic; u32 comp[2] = {bpmem.alphaFunc.comp0,bpmem.alphaFunc.comp1}; diff --git a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp index b6f1da4d33..e9e9c11a68 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/TextureMngr.cpp @@ -98,7 +98,7 @@ void TextureMngr::TCacheEntry::Destroy() *ptr = oldpixel; } texture = 0; -} +} void TextureMngr::Init() { @@ -509,10 +509,10 @@ void TextureMngr::CopyRenderTargetToTexture(u32 address, bool bFromZBuffer, bool GL_REPORT_ERRORD(); // create and attach the render target - std::map::iterator itdepth = mapDepthTargets.find((h<<16)|w); + std::map::iterator itdepth = mapDepthTargets.find((h << 16) | w); - if( itdepth == mapDepthTargets.end() ) { - DEPTHTARGET& depth = mapDepthTargets[(h<<16)|w]; + if (itdepth == mapDepthTargets.end()) { + DEPTHTARGET& depth = mapDepthTargets[(h << 16) | w]; depth.framecount = frameCount; glGenRenderbuffersEXT( 1, &depth.targ); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth.targ); diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h index 3c9e906d36..d148d7cf51 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexLoader.h @@ -18,10 +18,6 @@ #ifndef _VERTEXLOADER_H #define _VERTEXLOADER_H -#define SHADER_POSMTX_ATTRIB 1 -#define SHADER_NORM1_ATTRIB 6 -#define SHADER_NORM2_ATTRIB 7 - using namespace std; #include "CPMemory.h" @@ -30,39 +26,6 @@ using namespace std; #define LOADERDECL __cdecl typedef void (LOADERDECL *TPipelineFunction)(void*); -// m_components -enum { - VB_HAS_POSMTXIDX =(1<<1), - VB_HAS_TEXMTXIDX0=(1<<2), - VB_HAS_TEXMTXIDX1=(1<<3), - VB_HAS_TEXMTXIDX2=(1<<4), - VB_HAS_TEXMTXIDX3=(1<<5), - VB_HAS_TEXMTXIDX4=(1<<6), - VB_HAS_TEXMTXIDX5=(1<<7), - VB_HAS_TEXMTXIDX6=(1<<8), - VB_HAS_TEXMTXIDX7=(1<<9), - VB_HAS_TEXMTXIDXALL=(0xff<<2), - //VB_HAS_POS=0, // Implied, it always has pos! don't bother testing - VB_HAS_NRM0=(1<<10), - VB_HAS_NRM1=(1<<11), - VB_HAS_NRM2=(1<<12), - VB_HAS_NRMALL=(7<<10), - - VB_HAS_COL0=(1<<13), - VB_HAS_COL1=(1<<14), - - VB_HAS_UV0=(1<<15), - VB_HAS_UV1=(1<<16), - VB_HAS_UV2=(1<<17), - VB_HAS_UV3=(1<<18), - VB_HAS_UV4=(1<<19), - VB_HAS_UV5=(1<<20), - VB_HAS_UV6=(1<<21), - VB_HAS_UV7=(1<<22), - VB_HAS_UVALL=(0xff<<15), - VB_HAS_UVTEXMTXSHIFT=13, -}; - // There are 8 of these. Most games only use the first, and just reconfigure it all the time // as needed, unfortunately. // TODO - clarify the role of this class. It seems to have taken on some irrelevant stuff. diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp index eb043f4ac8..f7caa2ae9e 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.cpp @@ -23,9 +23,10 @@ #include "VertexShader.h" // This is the tricky one to get rid off. -#include "VertexLoader.h" +// #include "VertexLoader.h" static char text[16384]; + #define WRITE p+=sprintf #define LIGHTS_POS "" @@ -34,6 +35,7 @@ char *GenerateLightShader(char* p, int index, const LitChannel& chan, const char char *GenerateVertexShader(u32 components, bool has_zbuffer_target) { + text[sizeof(text) - 1] = 0x7C; // canary DVSTARTPROFILE(); _assert_( bpmem.genMode.numtexgens == xfregs.numTexGens); @@ -405,6 +407,8 @@ char *GenerateVertexShader(u32 components, bool has_zbuffer_target) WRITE(p, "return o;\n}\n"); + if (text[sizeof(text) - 1] != 0x7C) + PanicAlert("VertexShader generator - buffer too small, canary has been eaten!"); return text; } @@ -415,7 +419,7 @@ char* GenerateLightShader(char* p, int index, const LitChannel& chan, const char if (coloralpha == 1 ) swizzle = "xyz"; else if (coloralpha == 2 ) swizzle = "w"; - if (!(chan.attnfunc&1)) { + if (!(chan.attnfunc & 1)) { // atten disabled switch (chan.diffusefunc) { case LIGHTDIF_NONE: diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h b/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h index 8865601cd1..551a7fa631 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexShader.h @@ -20,6 +20,43 @@ #include "XFMemory.h" +#define SHADER_POSMTX_ATTRIB 1 +#define SHADER_NORM1_ATTRIB 6 +#define SHADER_NORM2_ATTRIB 7 + +// m_components +enum { + VB_HAS_POSMTXIDX =(1<<1), + VB_HAS_TEXMTXIDX0=(1<<2), + VB_HAS_TEXMTXIDX1=(1<<3), + VB_HAS_TEXMTXIDX2=(1<<4), + VB_HAS_TEXMTXIDX3=(1<<5), + VB_HAS_TEXMTXIDX4=(1<<6), + VB_HAS_TEXMTXIDX5=(1<<7), + VB_HAS_TEXMTXIDX6=(1<<8), + VB_HAS_TEXMTXIDX7=(1<<9), + VB_HAS_TEXMTXIDXALL=(0xff<<2), + //VB_HAS_POS=0, // Implied, it always has pos! don't bother testing + VB_HAS_NRM0=(1<<10), + VB_HAS_NRM1=(1<<11), + VB_HAS_NRM2=(1<<12), + VB_HAS_NRMALL=(7<<10), + + VB_HAS_COL0=(1<<13), + VB_HAS_COL1=(1<<14), + + VB_HAS_UV0=(1<<15), + VB_HAS_UV1=(1<<16), + VB_HAS_UV2=(1<<17), + VB_HAS_UV3=(1<<18), + VB_HAS_UV4=(1<<19), + VB_HAS_UV5=(1<<20), + VB_HAS_UV6=(1<<21), + VB_HAS_UV7=(1<<22), + VB_HAS_UVALL=(0xff<<15), + VB_HAS_UVTEXMTXSHIFT=13, +}; + // shader variables #define I_POSNORMALMATRIX "cpnmtx" #define I_PROJECTION "cproj"