Preparing stuff for DX11:

- be more strict with the input signature of generated pixel shaders
- replace the D3D parameter with an API_TYPE one, so that we can further distinguish between DX9 and DX11
- for what it's worth dx11 (using vs_4_0) doesn't support D3DCOLORtoUBYTE4; added a workaround for this
- add a "dx9-" prefix to the shader cache files to avoid conflicts with dx11 shaders

Apart from that we're calling CommandProcessor::Shutdown() in main.cpp,Shutdown() now.
Delete all files in User/ShaderCache if you want to save a few KB disk space :P


git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5659 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
NeoBrainX 2010-06-12 15:49:21 +00:00
parent f0d178122e
commit a6ebc8234f
11 changed files with 75 additions and 61 deletions

View File

@ -22,7 +22,7 @@ static const char ID[4] = {'D', 'C', 'A', 'C'};
// Update this to the current SVN revision every time you change shader generation code.
// We don't automatically get this from SVN_REV because that would mean regenerating the
// shader cache for every revision, graphics-related or not, which is simply annoying.
const int version = 5658;
const int version = 5659;
LinearDiskCache::LinearDiskCache()
: file_(NULL), num_entries_(0) {

View File

@ -425,25 +425,27 @@ const char *GeneratePixelShaderCode(u32 texture_mask, bool dstAlphaEnable, u32 H
WRITE(p, "uniform float4 "I_FOG"[2] : register(c%d);\n", C_FOG);
WRITE(p, "void main(\n");
WRITE(p, " out float4 ocol0 : COLOR0,\n");
WRITE(p, " out float4 ocol0 : COLOR0,\n");
WRITE(p, " out float depth : DEPTH,\n");
WRITE(p, " in float4 rawpos : POSITION,\n");
WRITE(p, " in float4 colors_0 : COLOR0,\n");
WRITE(p, " in float4 colors_1 : COLOR1\n");
// compute window position if needed because binding semantic WPOS is not widely supported
if (numTexgen < 7)
{
for (int i = 0; i < numTexgen; ++i)
WRITE(p, " in float3 uv%d : TEXCOORD%d, \n", i, i);
WRITE(p, ", \n in float3 uv%d : TEXCOORD%d", i, i);
WRITE(p, " in float4 clipPos : TEXCOORD%d, \n", numTexgen);
WRITE(p, ", \n in float4 clipPos : TEXCOORD%d", numTexgen);
}
else
{
// wpos is in w of first 4 texcoords
for (int i = 0; i < numTexgen; ++i)
WRITE(p, " in float%d uv%d : TEXCOORD%d, \n", i<4?4:3, i, i);
WRITE(p, ", \n in float%d uv%d : TEXCOORD%d", i<4?4:3, i, i);
}
WRITE(p, " in float4 colors_0 : COLOR0,\n in float4 colors_1 : COLOR1){\n");
WRITE(p, " ) {\n");
char* pmainstart = p;
@ -490,7 +492,6 @@ const char *GeneratePixelShaderCode(u32 texture_mask, bool dstAlphaEnable, u32 H
}
}
for (int i = 0; i < numStages; i++)
WriteStage(p, i, texture_mask,HLSL); //build the equation for this stage
@ -533,9 +534,9 @@ const char *GeneratePixelShaderCode(u32 texture_mask, bool dstAlphaEnable, u32 H
WRITE(p, "zCoord = frac(zCoord);\n");
WRITE(p, "zCoord = zCoord * (16777216.0f/16777215.0f);\n");
}
WRITE(p, "depth = zCoord;\n");
if (dstAlphaEnable)
WRITE(p, " ocol0 = float4(prev.rgb,"I_ALPHA"[0].a);\n");
else
@ -544,7 +545,7 @@ const char *GeneratePixelShaderCode(u32 texture_mask, bool dstAlphaEnable, u32 H
WRITE(p, " ocol0 = prev;\n");
}
}
WRITE(p, "}\n");
if (text[sizeof(text) - 1] != 0x7C)
@ -620,9 +621,9 @@ static void WriteStage(char *&p, int n, u32 texture_mask, u32 HLSL)
if (bpmem.tevind[n].bs != ITBA_OFF)
{
WRITE(p, "alphabump = indtex%d.%s %s;\n",
bpmem.tevind[n].bt,
tevIndAlphaSel[bpmem.tevind[n].bs],
tevIndAlphaScale[bpmem.tevind[n].fmt]);
bpmem.tevind[n].bt,
tevIndAlphaSel[bpmem.tevind[n].bs],
tevIndAlphaScale[bpmem.tevind[n].fmt]);
}
// format
WRITE(p, "float3 indtevcrd%d = indtex%d * %s;\n", n, bpmem.tevind[n].bt, tevIndFmtScale[bpmem.tevind[n].fmt]);

View File

@ -21,9 +21,9 @@
#include "BPMemory.h"
#include "PixelShaderGen.h"
void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4);
void SetPSConstant4fv(int const_number, const float *f);
void SetMultiPSConstant4fv(int const_number, int count, const float *f);
void SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4);
void SetPSConstant4fv(unsigned int const_number, const float *f);
void SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f);
// The non-API dependent parts.
class PixelShaderManager

View File

@ -80,7 +80,7 @@ static char text[16384];
char *GenerateLightShader(char *p, int index, const LitChannel& chan, const char *dest, int coloralpha);
const char *GenerateVertexShaderCode(u32 components, bool D3D)
const char *GenerateVertexShaderCode(u32 components, API_TYPE api_type)
{
setlocale(LC_NUMERIC, "C"); // Reset locale for compilation
text[sizeof(text) - 1] = 0x7C; // canary
@ -89,6 +89,7 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
_assert_(bpmem.genMode.numtexgens == xfregs.numTexGens);
_assert_(bpmem.genMode.numcolchans == xfregs.nNumChans);
bool is_d3d = (api_type == API_D3D9 || api_type == API_D3D11);
u32 lightMask = 0;
if (xfregs.nNumChans > 0)
lightMask |= xfregs.colChans[0].color.GetFullLightMask() | xfregs.colChans[0].alpha.GetFullLightMask();
@ -107,7 +108,7 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
"typedef struct { Light lights[8]; } s_"I_LIGHTS";\n"
"typedef struct { float4 C0,C1,C2,C3; } s_"I_MATERIALS";\n"
"typedef struct { float4 T0,T1,T2,T3; } s_"I_PROJECTION";\n"
"typedef struct { float4 params; } s_"I_FOGPARAMS";\n"); // a, b, c, b_shift
);
WRITE(p, "struct VS_OUTPUT {\n");
WRITE(p, " float4 pos : POSITION;\n");
@ -134,7 +135,6 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, "uniform s_"I_LIGHTS" "I_LIGHTS" : register(c%d);\n", C_LIGHTS);
WRITE(p, "uniform s_"I_MATERIALS" "I_MATERIALS" : register(c%d);\n", C_MATERIALS);
WRITE(p, "uniform s_"I_PROJECTION" "I_PROJECTION" : register(c%d);\n", C_PROJECTION);
WRITE(p, "uniform s_"I_FOGPARAMS" "I_FOGPARAMS" : register(c%d);\n", C_FOGPARAMS);
WRITE(p, "VS_OUTPUT main(\n");
@ -142,13 +142,13 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
if (components & VB_HAS_NRM0)
WRITE(p, " float3 rawnorm0 : NORMAL0,\n");
if (components & VB_HAS_NRM1) {
if (D3D)
if (is_d3d)
WRITE(p, " float3 rawnorm1 : NORMAL1,\n");
else
WRITE(p, " float3 rawnorm1 : ATTR%d,\n", SHADER_NORM1_ATTRIB);
}
if (components & VB_HAS_NRM2) {
if (D3D)
if (is_d3d)
WRITE(p, " float3 rawnorm2 : NORMAL2,\n");
else
WRITE(p, " float3 rawnorm2 : ATTR%d,\n", SHADER_NORM2_ATTRIB);
@ -163,7 +163,7 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, " float%d tex%d : TEXCOORD%d,\n", hastexmtx ? 3 : 2, i,i);
}
if (components & VB_HAS_POSMTXIDX) {
if (D3D)
if (is_d3d)
{
WRITE(p, " float4 blend_indices : BLENDINDICES,\n");
}
@ -175,10 +175,14 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
// transforms
if (components & VB_HAS_POSMTXIDX) {
if (D3D)
if (api_type == API_D3D9)
{
WRITE(p, "int4 indices = D3DCOLORtoUBYTE4(blend_indices);\n"
"int posmtx = indices.x;\n");
WRITE(p, "int4 indices = D3DCOLORtoUBYTE4(blend_indices);\n");
WRITE(p, "int posmtx = indices.x;\n");
}
else if (api_type == API_D3D11)
{
WRITE(p, "int posmtx = blend_indices.x * 255.0f;\n");
}
else
{
@ -208,7 +212,6 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, "float3 _norm1 = float3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm1), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm1));\n");
if (components & VB_HAS_NRM2)
WRITE(p, "float3 _norm2 = float3(dot("I_POSNORMALMATRIX".N0.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N1.xyz, rawnorm2), dot("I_POSNORMALMATRIX".N2.xyz, rawnorm2));\n");
}
if (!(components & VB_HAS_NRM0))
@ -326,6 +329,7 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, "}\n");
}
// zero left over channels
for (int i = xfregs.nNumChans; i < 2; ++i)
WRITE(p, "o.colors[%d] = float4(0.0f,0.0f,0.0f,0.0f);\n", i);
@ -452,11 +456,11 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, "o.tex3.w = o.pos.w;\n");
}
if (D3D)
if (is_d3d)
{
WRITE(p, "o.pos.z = o.pos.z + o.pos.w;\n");
}
else
WRITE(p, "o.pos.z = o.pos.z + o.pos.w;\n");
}
else
{
// scale to gl clip space - which is -o.pos.w to o.pos.w, hence the addition.
WRITE(p, "o.pos.z = (o.pos.z * 2.0f) + o.pos.w;\n");
@ -464,6 +468,7 @@ const char *GenerateVertexShaderCode(u32 components, bool D3D)
WRITE(p, "return o;\n}\n");
if (text[sizeof(text) - 1] != 0x7C)
PanicAlert("VertexShader generator - buffer too small, canary has been eaten!");
return text;

View File

@ -99,8 +99,15 @@ public:
}
};
typedef enum
{
API_OPENGL,
API_D3D9,
API_D3D11
} API_TYPE;
// components is included in the uid.
const char *GenerateVertexShaderCode(u32 components, bool D3D);
const char *GenerateVertexShaderCode(u32 components, API_TYPE api_type);
void GetVertexShaderId(VERTEXSHADERUID *uid, u32 components);
extern VERTEXSHADERUID last_vertex_shader_uid;

View File

@ -64,9 +64,10 @@ public:
static void ResetView();
};
void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4);
void SetVSConstant4fv(int const_number, const float *f);
void SetMultiVSConstant3fv(int const_number, int count, const float *f);
void SetMultiVSConstant4fv(int const_number, int count, const float *f);
void SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4);
void SetVSConstant4fv(unsigned int const_number, const float *f);
void SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f);
void SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f);
#endif // _VERTEXSHADERMANAGER_H

View File

@ -48,7 +48,6 @@ static LPDIRECT3DPIXELSHADER9 s_DepthMatrixProgram[3];
static LPDIRECT3DPIXELSHADER9 s_ClearProgram = 0;
LPDIRECT3DPIXELSHADER9 PixelShaderCache::GetColorMatrixProgram(int SSAAMode)
{
return s_ColorMatrixProgram[SSAAMode % 3];
@ -69,7 +68,7 @@ LPDIRECT3DPIXELSHADER9 PixelShaderCache::GetClearProgram()
return s_ClearProgram;
}
void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4)
void SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
if (lastPSconstants[const_number][0] != f1 || lastPSconstants[const_number][1] != f2 ||
lastPSconstants[const_number][2] != f3 || lastPSconstants[const_number][3] != f4)
@ -83,7 +82,7 @@ void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4)
}
}
void SetPSConstant4fv(int const_number, const float *f)
void SetPSConstant4fv(unsigned int const_number, const float *f)
{
if (memcmp(&lastPSconstants[const_number], f, sizeof(float) * 4)) {
memcpy(&lastPSconstants[const_number], f, sizeof(float) * 4);
@ -91,7 +90,7 @@ void SetPSConstant4fv(int const_number, const float *f)
}
}
void SetMultiPSConstant4fv(int const_number, int count, const float *f)
void SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
{
if (memcmp(&lastPSconstants[const_number], f, count * sizeof(float) * 4)) {
memcpy(&lastPSconstants[const_number], f, count * sizeof(float) * 4);
@ -260,7 +259,7 @@ void PixelShaderCache::Init()
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
char cache_filename[MAX_PATH];
sprintf(cache_filename, "%s%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX), globals->unique_id);
sprintf(cache_filename, "%sdx9-%s-ps.cache", File::GetUserPath(D_SHADERCACHE_IDX), globals->unique_id);
PixelShaderCacheInserter inserter;
int read_items = g_ps_disk_cache.OpenAndRead(cache_filename, &inserter);
}

View File

@ -56,7 +56,7 @@ LPDIRECT3DVERTEXSHADER9 VertexShaderCache::GetClearVertexShader()
}
void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
void SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
if (lastVSconstants[const_number][0] != f1 ||
lastVSconstants[const_number][1] != f2 ||
@ -71,7 +71,7 @@ void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
}
}
void SetVSConstant4fv(int const_number, const float *f)
void SetVSConstant4fv(unsigned int const_number, const float *f)
{
if (memcmp(&lastVSconstants[const_number], f, sizeof(float) * 4)) {
memcpy(&lastVSconstants[const_number], f, sizeof(float) * 4);
@ -79,10 +79,10 @@ void SetVSConstant4fv(int const_number, const float *f)
}
}
void SetMultiVSConstant3fv(int const_number, int count, const float *f)
void SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f)
{
bool change = false;
for (int i = 0; i < count; i++)
for (unsigned int i = 0; i < count; i++)
{
if (lastVSconstants[const_number + i][0] != f[0 + i*3] ||
lastVSconstants[const_number + i][1] != f[1 + i*3] ||
@ -94,7 +94,7 @@ void SetMultiVSConstant3fv(int const_number, int count, const float *f)
}
if (change)
{
for (int i = 0; i < count; i++)
for (unsigned int i = 0; i < count; i++)
{
lastVSconstants[const_number + i][0] = f[0 + i*3];
lastVSconstants[const_number + i][1] = f[1 + i*3];
@ -105,7 +105,7 @@ void SetMultiVSConstant3fv(int const_number, int count, const float *f)
}
}
void SetMultiVSConstant4fv(int const_number, int count, const float *f)
void SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
{
if (memcmp(&lastVSconstants[const_number], f, count * sizeof(float) * 4)) {
memcpy(&lastVSconstants[const_number], f, count * sizeof(float) * 4);
@ -215,7 +215,7 @@ void VertexShaderCache::Init()
File::CreateDir(File::GetUserPath(D_SHADERCACHE_IDX));
char cache_filename[MAX_PATH];
sprintf(cache_filename, "%s%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX), globals->unique_id);
sprintf(cache_filename, "%sdx9-%s-vs.cache", File::GetUserPath(D_SHADERCACHE_IDX), globals->unique_id);
VertexShaderCacheInserter inserter;
int read_items = g_vs_disk_cache.OpenAndRead(cache_filename, &inserter);
}
@ -284,7 +284,7 @@ bool VertexShaderCache::SetShader(u32 components)
return false;
}
const char *code = GenerateVertexShaderCode(components, true);
const char *code = GenerateVertexShaderCode(components, API_D3D9);
u8 *bytecode;
int bytecodelen;
if (!D3D::CompileVertexShader(code, (int)strlen(code), &bytecode, &bytecodelen))

View File

@ -286,6 +286,7 @@ void Shutdown()
s_FifoShuttingDown = FALSE;
s_swapRequested = FALSE;
Fifo_Shutdown();
CommandProcessor::Shutdown();
VertexManager::Shutdown();
VertexLoaderManager::Shutdown();
VertexShaderCache::Shutdown();

View File

@ -48,7 +48,7 @@ static FRAGMENTSHADER* pShaderLast = NULL;
static float lastPSconstants[C_COLORMATRIX+16][4];
void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4)
void SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
if (lastPSconstants[const_number][0] != f1 || lastPSconstants[const_number][1] != f2 ||
lastPSconstants[const_number][2] != f3 || lastPSconstants[const_number][3] != f4)
@ -62,7 +62,7 @@ void SetPSConstant4f(int const_number, float f1, float f2, float f3, float f4)
}
}
void SetPSConstant4fv(int const_number, const float *f)
void SetPSConstant4fv(unsigned int const_number, const float *f)
{
if (memcmp(&lastPSconstants[const_number], f, sizeof(float) * 4)) {
memcpy(&lastPSconstants[const_number], f, sizeof(float) * 4);
@ -70,10 +70,10 @@ void SetPSConstant4fv(int const_number, const float *f)
}
}
void SetMultiPSConstant4fv(int const_number, int count, const float *f)
void SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
{
const float *f0 = f;
for (int i = 0; i < count ;i++,f0+=4)
for (unsigned int i = 0; i < count ;i++,f0+=4)
{
if (memcmp(&lastPSconstants[const_number + i], f0, sizeof(float) * 4)) {
memcpy(&lastPSconstants[const_number + i], f0, sizeof(float) * 4);
@ -89,7 +89,7 @@ void PixelShaderCache::Init()
CurrentShader = 0;
GL_REPORT_ERRORD();
for (int i = 0; i < (C_COLORMATRIX+16) * 4; i++)
for (unsigned int i = 0; i < (C_COLORMATRIX+16) * 4; i++)
lastPSconstants[i/4][i%4] = -100000000.0f;
memset(&last_pixel_shader_uid, 0xFF, sizeof(last_pixel_shader_uid));

View File

@ -46,7 +46,7 @@ static VERTEXSHADER *pShaderLast = NULL;
static int s_nMaxVertexInstructions;
static float GC_ALIGNED16(lastVSconstants[C_FOGPARAMS+8][4]);
void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
void SetVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{
if ( lastVSconstants[const_number][0] != f1 ||
lastVSconstants[const_number][1] != f2 ||
@ -61,7 +61,7 @@ void SetVSConstant4f(int const_number, float f1, float f2, float f3, float f4)
}
}
void SetVSConstant4fv(int const_number, const float *f)
void SetVSConstant4fv(unsigned int const_number, const float *f)
{
if (memcmp(&lastVSconstants[const_number], f, sizeof(float) * 4)) {
memcpy(&lastVSconstants[const_number], f, sizeof(float) * 4);
@ -69,10 +69,10 @@ void SetVSConstant4fv(int const_number, const float *f)
}
}
void SetMultiVSConstant4fv(int const_number, int count, const float *f)
void SetMultiVSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
{
const float *f0 = f;
for (int i = 0; i < count; i++,f0+=4)
for (unsigned int i = 0; i < count; i++,f0+=4)
{
if (memcmp(&lastVSconstants[const_number + i], f0, sizeof(float) * 4)) {
memcpy(&lastVSconstants[const_number + i], f0, sizeof(float) * 4);
@ -81,9 +81,9 @@ void SetMultiVSConstant4fv(int const_number, int count, const float *f)
}
}
void SetMultiVSConstant3fv(int const_number, int count, const float *f)
void SetMultiVSConstant3fv(unsigned int const_number, unsigned int count, const float *f)
{
for (int i = 0; i < count; i++)
for (unsigned int i = 0; i < count; i++)
{
if (lastVSconstants[const_number + i][0] != f[0 + i*3] ||
lastVSconstants[const_number + i][1] != f[1 + i*3] ||
@ -151,7 +151,7 @@ VERTEXSHADER* VertexShaderCache::GetShader(u32 components)
VSCacheEntry& entry = vshaders[uid];
entry.frameCount = frameCount;
pShaderLast = &entry.shader;
const char *code = GenerateVertexShaderCode(components, false);
const char *code = GenerateVertexShaderCode(components, API_OPENGL);
#if defined(_DEBUG) || defined(DEBUGFAST)
if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) {