Add in GLSL setting again.

PS and VS making. Untested and won't work for now.

Add in program shader cache files.

Readd NativeVertexFormat stuffs.

Add in PS and VS cache things.

SetShaders in places.

Fixed EFB cache index computations in OpenGL renderer.

The previous computation was very likely to go out of array bounds,
which could result in crashes on EFB access.

Also, the cache size was rounded down instead of up. This is a problem
since EFB_HEIGHT (528) is not a multiple of EFB_CACHE_RECT_SIZE (64).
This commit is contained in:
Ryan Houdek
2011-11-30 21:00:21 -06:00
committed by Sonicadvance1
parent 3990002250
commit a357c77257
20 changed files with 1168 additions and 505 deletions

View File

@ -509,122 +509,117 @@ static void BuildSwapModeTable()
}
}
const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num)
{
if(ApiType == API_GLSL)
return ""; // Nothing to do here
static char result[64];
sprintf(result, " : register(%s%d)", prefix, num);
return result;
}
const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u32 components)
{
setlocale(LC_NUMERIC, "C"); // Reset locale for compilation
text[sizeof(text) - 1] = 0x7C; // canary
BuildSwapModeTable(); // Needed for WriteStage
int numStages = bpmem.genMode.numtevstages + 1;
int numTexgen = bpmem.genMode.numtexgens;
char *p = text;
WRITE(p, "//Pixel Shader for TEV stages\n");
WRITE(p, "//%i TEV stages, %i texgens, XXX IND stages\n",
numStages, numTexgen/*, bpmem.genMode.numindstages*/);
int nIndirectStagesUsed = 0;
if (bpmem.genMode.numindstages > 0)
{
for (int i = 0; i < numStages; ++i)
{
if (bpmem.tevind[i].IsActive() && bpmem.tevind[i].bt < bpmem.genMode.numindstages)
nIndirectStagesUsed |= 1 << bpmem.tevind[i].bt;
}
}
DepthTextureEnable = (bpmem.ztex2.op != ZTEXTURE_DISABLE && !bpmem.zcontrol.zcomploc && bpmem.zmode.testenable && bpmem.zmode.updateenable) || g_ActiveConfig.bEnablePerPixelDepth ;
// Declare samplers
if(ApiType != API_D3D11)
{
WRITE(p, "uniform sampler2D ");
}
else
{
WRITE(p, "sampler ");
}
bool bfirst = true;
for (int i = 0; i < 8; ++i)
{
WRITE(p, "%s samp%d : register(s%d)", bfirst?"":",", i, i);
bfirst = false;
}
WRITE(p, ";\n");
if(ApiType == API_D3D11)
{
WRITE(p, "Texture2D ");
bfirst = true;
for (int i = 0; i < 8; ++i)
{
WRITE(p, "%s Tex%d : register(t%d)", bfirst?"":",", i, i);
bfirst = false;
}
WRITE(p, ";\n");
}
WRITE(p, "\n");
WRITE(p, "uniform float4 " I_COLORS"[4] : register(c%d);\n", C_COLORS);
WRITE(p, "uniform float4 " I_KCOLORS"[4] : register(c%d);\n", C_KCOLORS);
WRITE(p, "uniform float4 " I_ALPHA"[1] : register(c%d);\n", C_ALPHA);
WRITE(p, "uniform float4 " I_TEXDIMS"[8] : register(c%d);\n", C_TEXDIMS);
WRITE(p, "uniform float4 " I_ZBIAS"[2] : register(c%d);\n", C_ZBIAS);
WRITE(p, "uniform float4 " I_INDTEXSCALE"[2] : register(c%d);\n", C_INDTEXSCALE);
WRITE(p, "uniform float4 " I_INDTEXMTX"[6] : register(c%d);\n", C_INDTEXMTX);
WRITE(p, "uniform float4 " I_FOG"[3] : register(c%d);\n", C_FOG);
WRITE(p, "uniform float4 "I_COLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_COLORS));
WRITE(p, "uniform float4 "I_KCOLORS"[4] %s;\n", WriteRegister(ApiType, "c", C_KCOLORS));
WRITE(p, "uniform float4 "I_ALPHA"[1] %s;\n", WriteRegister(ApiType, "c", C_ALPHA));
WRITE(p, "uniform float4 "I_TEXDIMS"[8] %s;\n", WriteRegister(ApiType, "c", C_TEXDIMS));
WRITE(p, "uniform float4 "I_ZBIAS"[2] %s;\n", WriteRegister(ApiType, "c", C_ZBIAS));
WRITE(p, "uniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteRegister(ApiType, "c", C_INDTEXSCALE));
WRITE(p, "uniform float4 "I_INDTEXMTX"[6] %s;\n", WriteRegister(ApiType, "c", C_INDTEXMTX));
WRITE(p, "uniform float4 "I_FOG"[3] %s;\n", WriteRegister(ApiType, "c", C_FOG));
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
{
WRITE(p,"typedef struct { float4 col; float4 cosatt; float4 distatt; float4 pos; float4 dir; } Light;\n");
WRITE(p,"typedef struct { Light lights[8]; } s_" I_PLIGHTS";\n");
WRITE(p, "uniform s_" I_PLIGHTS" " I_PLIGHTS" : register(c%d);\n", C_PLIGHTS);
WRITE(p, "typedef struct { float4 C0, C1, C2, C3; } s_" I_PMATERIALS";\n");
WRITE(p, "uniform s_" I_PMATERIALS" " I_PMATERIALS" : register(c%d);\n", C_PMATERIALS);
WRITE(p, "uniform float4 "I_PLIGHTS"[40] %s;\n", WriteRegister(ApiType, "c", C_PLIGHTS));
WRITE(p, "uniform float4 "I_PMATERIALS"[4] %s;\n", WriteRegister(ApiType, "c", C_PMATERIALS));
}
WRITE(p, "void main(\n");
if(ApiType != API_D3D11)
{
WRITE(p, " out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n",
dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "",
DepthTextureEnable ? "\n out float depth : DEPTH," : "",
ApiType & API_OPENGL ? "WPOS" : ApiType & API_D3D9_SM20 ? "POSITION" : "VPOS");
}
else
{
WRITE(p, " 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," : "",
DepthTextureEnable ? "\n out float depth : SV_Depth," : "");
}
WRITE(p, " in float4 colors_0 : COLOR0,\n");
WRITE(p, " in float4 colors_1 : COLOR1");
// 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, ",\n in float3 uv%d : TEXCOORD%d", i, i);
WRITE(p, ",\n in float4 clipPos : TEXCOORD%d", numTexgen);
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
WRITE(p, ",\n in float4 Normal : TEXCOORD%d", numTexgen + 1);
}
else
{
// wpos is in w of first 4 texcoords
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
if(ApiType != API_GLSL)
{
WRITE(p, "void main(\n");
if(ApiType != API_D3D11)
{
for (int i = 0; i < 8; ++i)
WRITE(p, ",\n in float4 uv%d : TEXCOORD%d", i, i);
WRITE(p, " out float4 ocol0 : COLOR0,%s%s\n in float4 rawpos : %s,\n",
dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND ? "\n out float4 ocol1 : COLOR1," : "",
DepthTextureEnable ? "\n out float depth : DEPTH," : "",
ApiType & API_OPENGL ? "WPOS" : ApiType & API_D3D9_SM20 ? "POSITION" : "VPOS");
}
else
{
for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i)
WRITE(p, ",\n in float%d uv%d : TEXCOORD%d", i < 4 ? 4 : 3 , i, i);
WRITE(p, " 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," : "",
DepthTextureEnable ? "\n out float depth : SV_Depth," : "");
}
WRITE(p, " in float4 colors_0 : COLOR0,\n");
WRITE(p, " in float4 colors_1 : COLOR1");
// 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, ",\n in float3 uv%d : TEXCOORD%d", i, i);
WRITE(p, ",\n in float4 clipPos : TEXCOORD%d", numTexgen);
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
WRITE(p, ",\n in float4 Normal : TEXCOORD%d", numTexgen + 1);
}
else
{
// wpos is in w of first 4 texcoords
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
{
for (int i = 0; i < 8; ++i)
WRITE(p, ",\n in float4 uv%d : TEXCOORD%d", i, i);
}
else
{
for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i)
WRITE(p, ",\n in float%d uv%d : TEXCOORD%d", i < 4 ? 4 : 3 , i, i);
}
}
WRITE(p, " ) {\n");
}
else
{
// GLSL doesn't do main arguments
// Once we switch to GLSL 1.3 we will bind a lot of these.
WRITE(p, " float4 ocol0;\n");
if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND)
WRITE(p, " float4 ocol1;\n"); // Will be supported later
if(DepthTextureEnable)
WRITE(p, " float depth;\n"); // TODO: Passed to Vertex Shader right?
WRITE(p, " float4 rawpos = gl_FragCoord;\n");
WRITE(p, " float4 colors_0 = gl_Color;\n");
WRITE(p, " float4 colors_1 = gl_SecondaryColor;\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, " float3 uv%d = gl_TexCoord[%d].xyz;\n", i, i);
WRITE(p, " float4 clipPos = gl_TexCoord[%d];\n", numTexgen);
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
WRITE(p, " float4 Normal = gl_TexCoord[%d];\n", numTexgen + 1);
}
else
{
// wpos is in w of first 4 texcoords
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
{
for (int i = 0; i < 8; ++i)
WRITE(p, " float4 uv%d = gl_TexCoord[%d];\n", i, i);
}
else
{
for (unsigned int i = 0; i < xfregs.numTexGen.numTexGens; ++i)
WRITE(p, " float%d uv%d = gl_TexCoord[%d]%s;\n", i < 4 ? 4 : 3 , i, i, i < 4 ? ".xyz" : "");
}
}
WRITE(p, "void main()\n{\n");
}
WRITE(p, " ) {\n");
char* pmainstart = p;
int Pretest = AlphaPreTest();
@ -633,12 +628,14 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
if (!Pretest)
{
// alpha test will always fail, so restart the shader and just make it an empty function
WRITE(p, "ocol0 = 0;\n");
if(DepthTextureEnable)
WRITE(p, "depth = 1.f;\n");
if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND)
WRITE(p, "ocol1 = 0;\n");
WRITE(p, "discard;\n");
if(ApiType == API_GLSL)
WRITE(p, "gl_FragData[0] = ocol0;\n");
if(ApiType != API_D3D11)
WRITE(p, "return;\n");
}
@ -763,6 +760,25 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
if(Pretest == -1)
{
WriteAlphaTest(p, ApiType, dstAlphaMode);
// alpha test will always fail, so restart the shader and just make it an empty function
/* p = pmainstart;
WRITE(p, "ocol0 = 0;\n");
if(DepthTextureEnable)
WRITE(p, "depth = 1.f;\n");
if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND)
WRITE(p, "ocol1 = 0;\n");
if(ApiType == API_GLSL)
{
// Once we switch to GLSL 1.3 and bind variables, we won't need to do this
WRITE(p, "gl_FragData[0] = ocol0;\n");
if(DepthTextureEnable)
WRITE(p, "gl_FragDepth = depth;\n");
if(dstAlphaMode == DSTALPHA_DUAL_SOURCE_BLEND)
; // TODO: Will do this later
}
WRITE(p, "discard;\n");
if(ApiType != API_D3D11)
WRITE(p, "return;\n");*/
}
if((bpmem.fog.c_proj_fsel.fsel != 0) || DepthTextureEnable)
@ -805,6 +821,13 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
WRITE(p, " ocol1 = ocol0;\n");
// ...and the alpha from ocol0 will be written to the framebuffer.
WRITE(p, " ocol0.a = " I_ALPHA"[0].a;\n");
if(ApiType == API_GLSL)
{
if(DepthTextureEnable)
WRITE(p, "gl_FragDepth = depth;\n");
WRITE(p, "gl_FragData[0] = ocol0;\n");
}
}
WRITE(p, "}\n");