From ba4143e82b30c9f48befd089275977685ed70a18 Mon Sep 17 00:00:00 2001 From: "XTra.KrazzY" Date: Fri, 12 Sep 2008 19:04:13 +0000 Subject: [PATCH] Optimized vertex and pixel shader generation. Medium FPS boost. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@500 8ced0084-cf51-0410-be5f-012b33b47a6e --- .../Plugin_VideoDX9/Src/PixelShader.cpp | 41 ++++++++--------- .../Plugin_VideoDX9/Src/ShaderManager.cpp | 9 +++- .../Plugin_VideoDX9/Src/VertexShader.cpp | 44 ++++++++++++++++++- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp index 748b19284f..5a5c075938 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/PixelShader.cpp @@ -302,35 +302,33 @@ const char *GeneratePixelShader() int numSamplers = 8; char *p = text; - WRITE(p,"//Pixel Shader for TEV stages\n"); - WRITE(p,"//%i TEV stages, %i texgens, %i IND stages, %i COL channels\n", + WRITE(p,"//Pixel Shader for TEV stages\n\ +//%i TEV stages, %i texgens, %i IND stages, %i COL channels\n", bpmem.genMode.numtevstages,bpmem.genMode.numtexgens,bpmem.genMode.numindstages,bpmem.genMode.numcolchans); //write kcolor declarations - for (int i = 0; i < 4; i++) - WRITE(p,"float4 k%i : register(c%i);\n",i,PS_CONST_KCOLORS+i); + for (int i = 0; i < 4; i++) { + if(i < 3) { + WRITE(p,"float4 k%i : register(c%i);\n\ + float4 color%i : register(c%i);\n",i,PS_CONST_KCOLORS+i, i,PS_CONST_COLORS+i+1); + } else { + WRITE(p,"float4 k%i : register(c%i);\n",i,PS_CONST_KCOLORS+i); + } + } - for (int i = 0; i < 3; i++) - WRITE(p,"float4 color%i : register(c%i);\n",i,PS_CONST_COLORS+i+1); + WRITE(p,"float constalpha : register(c%i);\n\ +float2 alphaRef : register(c%i);\n\n\ +sampler samp[%i] : register(s0);\n\n\ +float4 main(in float4 colors[2] : COLOR0",PS_CONST_CONSTALPHA,PS_CONST_ALPHAREF,numSamplers); - WRITE(p,"float constalpha : register(c%i);\n",PS_CONST_CONSTALPHA); - WRITE(p,"float2 alphaRef : register(c%i);\n",PS_CONST_ALPHAREF); - - WRITE(p,"\n"); - - WRITE(p,"sampler samp[%i] : register(s0);\n",numSamplers); - - WRITE(p,"\n"); - - WRITE(p,"float4 main(in float4 colors[2] : COLOR0"); if (numTexgen) WRITE(p,", float4 uv[%i] : TEXCOORD0",numTexgen); else WRITE(p,", float4 uv[1] : TEXCOORD0"); //HACK - WRITE(p,") : COLOR\n"); - WRITE(p,"{\n"); - WRITE(p,"float4 c0=color0,c1=color1,c2=color2,prev=float4(0.0f,0.0f,0.0f,0.0f),textemp,rastemp,konsttemp;\n"); - WRITE(p,"\n"); + WRITE(p,") : COLOR\n\ +{\n\ +float4 c0=color0,c1=color1,c2=color2,prev=float4(0.0f,0.0f,0.0f,0.0f),textemp,rastemp,konsttemp;\n\ +\n"); for (int i = 0; i < numStages; i++) WriteStage(p,i); //build the equation for this stage @@ -342,8 +340,7 @@ const char *GeneratePixelShader() else WRITE(p," return prev;\n"); - WRITE(p,"}\n"); - WRITE(p,"\0"); + WRITE(p,"}\n\0"); return text; } diff --git a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp index 4792b03b66..c28128f11c 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/ShaderManager.cpp @@ -152,9 +152,15 @@ void VShaderCache::Shutdown() void VShaderCache::SetShader() { + static LPDIRECT3DVERTEXSHADER9 shader = NULL; if (D3D::GetShaderVersion() < 2) return; // we are screwed + if(shader) { + //D3D::dev->SetVertexShader(shader); + return; + } + static LPDIRECT3DVERTEXSHADER9 lastShader = 0; DVSTARTPROFILE(); @@ -176,7 +182,7 @@ void VShaderCache::SetShader() } const char *code = GenerateVertexShader(); - LPDIRECT3DVERTEXSHADER9 shader = D3D::CompileVShader(code, int(strlen(code))); + shader = D3D::CompileVShader(code, int(strlen(code))); if (shader) { //Make an entry in the table @@ -185,6 +191,7 @@ void VShaderCache::SetShader() entry.frameCount=frameCount; vshaders[currentHash] = entry; } + D3D::dev->SetVertexShader(shader); INCSTAT(stats.numVertexShadersCreated); diff --git a/Source/Plugins/Plugin_VideoDX9/Src/VertexShader.cpp b/Source/Plugins/Plugin_VideoDX9/Src/VertexShader.cpp index 3617bc146f..c8c4a3386f 100644 --- a/Source/Plugins/Plugin_VideoDX9/Src/VertexShader.cpp +++ b/Source/Plugins/Plugin_VideoDX9/Src/VertexShader.cpp @@ -20,6 +20,46 @@ #include "VertexShader.h" #include "BPStructs.h" +static const char *genericVS = "// Generic Vertex Shader\ +\n\ +struct VS_INPUT {\n\ +float4 pos : POSITION;\n\ +float3 normal : NORMAL;\n\ +float4 colors[2] : COLOR0;\n\ +float3 uv[8] : TEXCOORD0;\n\ +};\n\ +\n\ +struct VS_OUTPUT {\n\ +float4 pos : POSITION;\n\ +float4 colors[2] : COLOR0;\n\ +//numtexgen\n\ +float4 uv[5] : TEXCOORD0;\n\ +};\n\ +\n\ +uniform matrix matWorldViewProj : register(c0);\n\ +\n\ +VS_OUTPUT main(const VS_INPUT input)\n\ +{\n\ +VS_OUTPUT output;\n\ +\n\ +output.pos = mul(matWorldViewProj, input.pos);\n\ +// texgen\n\ +output.uv[0] = float4(input.uv[0].xy,0,input.uv[0].z);\n\ +output.uv[1] = float4(input.uv[1].xy,0,input.uv[1].z);\n\ +output.uv[2] = float4(input.uv[2].xy,0,input.uv[2].z);\n\ +output.uv[3] = float4(input.uv[3].xy,0,input.uv[3].z);\n\ +output.uv[4] = float4(input.uv[4].xy,0,input.uv[4].z);\n\ +\n\ +for (int i=0; i<2; i++)\n output.colors[i] = input.colors[i];\n\ +return output;\n\ +}\0"; + +const char *GenerateVertexShader() { + return genericVS; +} + +/* + char text2[65536]; #define WRITE p+=sprintf @@ -91,7 +131,7 @@ const char *GenerateVertexShader() /* * xform->vertexshader ideas -*/ +*//* void WriteTexgen(char *&p, int n) { @@ -102,4 +142,4 @@ void WriteTexgen(char *&p, int n) void WriteLight(int color, int component) { -} +} */