VideoCommon: Pass interface blocks between shader stages to resolve naming conflicts.

This commit is contained in:
Jules Blok 2014-12-16 23:52:42 +01:00
parent bd6d229733
commit 782a5adb94
3 changed files with 56 additions and 31 deletions

View File

@ -87,18 +87,24 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
out.Write("#define InstanceID gl_InvocationID\n");
out.Write("centroid in VS_OUTPUT o[%d];\n", vertex_in);
out.Write("centroid out VS_OUTPUT vs;\n");
out.Write("in VertexData {\n");
out.Write("\tcentroid VS_OUTPUT o;\n");
out.Write("} vs[%d];\n", vertex_in);
out.Write("out VertexData {\n");
out.Write("\tcentroid VS_OUTPUT o;\n");
if (g_ActiveConfig.iStereoMode > 0)
out.Write("flat out int layer;\n");
out.Write("\tflat int layer;\n");
out.Write("} ps;\n");
out.Write("void main()\n{\n");
}
else // D3D
{
out.Write("struct GS_OUTPUT {\n");
out.Write("\tVS_OUTPUT vs;\n");
out.Write("struct VertexData {\n");
out.Write("\tVS_OUTPUT o;\n");
if (g_ActiveConfig.iStereoMode > 0)
out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n");
@ -108,25 +114,36 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
{
out.Write("[maxvertexcount(%d)]\n[instance(%d)]\n", vertex_out, g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream<GS_OUTPUT> output, in uint InstanceID : SV_GSInstanceID)\n{\n", primitives_d3d[primitive_type], vertex_in);
out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream<VertexData> output, in uint InstanceID : SV_GSInstanceID)\n{\n", primitives_d3d[primitive_type], vertex_in);
}
else
{
out.Write("[maxvertexcount(%d)]\n", g_ActiveConfig.iStereoMode > 0 ? vertex_out * 2 : vertex_out);
out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream<GS_OUTPUT> output)\n{\n", primitives_d3d[primitive_type], vertex_in);
out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream<VertexData> output)\n{\n", primitives_d3d[primitive_type], vertex_in);
}
out.Write("\tGS_OUTPUT gs;\n");
out.Write("\tVertexData ps;\n");
}
if (primitive_type == PRIMITIVE_LINES)
{
if (ApiType == API_OPENGL)
{
out.Write("\tVS_OUTPUT start = vs[0].o;\n");
out.Write("\tVS_OUTPUT end = vs[1].o;\n");
}
else
{
out.Write("\tVS_OUTPUT start = o[0];\n");
out.Write("\tVS_OUTPUT end = o[1];\n");
}
// GameCube/Wii's line drawing algorithm is a little quirky. It does not
// use the correct line caps. Instead, the line caps are vertical or
// horizontal depending the slope of the line.
out.Write(
"\tfloat2 offset;\n"
"\tfloat2 to = abs(o[1].pos.xy - o[0].pos.xy);\n"
"\tfloat2 to = abs(end.pos.xy - start.pos.xy);\n"
// FIXME: What does real hardware do when line is at a 45-degree angle?
// FIXME: Lines aren't drawn at the correct width. See Twilight Princess map.
"\tif (" I_LINEPTPARAMS".y * to.y > " I_LINEPTPARAMS".x * to.x) {\n"
@ -141,9 +158,14 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
}
else if (primitive_type == PRIMITIVE_POINTS)
{
if (ApiType == API_OPENGL)
out.Write("\tVS_OUTPUT point = vs[0].o;\n");
else
out.Write("\tVS_OUTPUT point = o[0];\n");
// Offset from center to upper right vertex
// Lerp PointSize/2 from [0,0..VpWidth,VpHeight] to [-1,1..1,-1]
out.Write("float2 offset = float2(" I_LINEPTPARAMS".w / " I_LINEPTPARAMS".x, -" I_LINEPTPARAMS".w / " I_LINEPTPARAMS".y) * o[0].pos.w;\n");
out.Write("float2 offset = float2(" I_LINEPTPARAMS".w / " I_LINEPTPARAMS".x, -" I_LINEPTPARAMS".w / " I_LINEPTPARAMS".y) * point.pos.w;\n");
}
if (g_ActiveConfig.iStereoMode > 0)
@ -157,20 +179,18 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
}
out.Write("\tfor (int i = 0; i < %d; ++i) {\n", vertex_in);
out.Write("\tVS_OUTPUT f = o[i];\n");
if (ApiType == API_OPENGL)
out.Write("\tVS_OUTPUT f = vs[i].o;\n");
else
out.Write("\tVS_OUTPUT f = o[i];\n");
if (g_ActiveConfig.iStereoMode > 0)
{
// Select the output layer
out.Write("\tps.layer = eye;\n");
if (ApiType == API_OPENGL)
{
out.Write("\tgl_Layer = eye;\n");
out.Write("\tlayer = eye;\n");
}
else
{
out.Write("\tgs.layer = eye;\n");
}
// For stereoscopy add a small horizontal offset in Normalized Device Coordinates proportional
// to the depth of the vertex. We retrieve the depth value from the w-component of the projected
@ -179,8 +199,8 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
// the depth value. This results in objects at a distance smaller than the convergence
// distance to seemingly appear in front of the screen.
// This formula is based on page 13 of the "Nvidia 3D Vision Automatic, Best Practices Guide"
out.Write("\tf.clipPos.x += " I_STEREOPARAMS"[eye] * (o[i].clipPos.w - " I_STEREOPARAMS"[2]);\n");
out.Write("\tf.pos.x += " I_STEREOPARAMS"[eye] * (o[i].pos.w - " I_STEREOPARAMS"[2]);\n");
out.Write("\tf.clipPos.x += " I_STEREOPARAMS"[eye] * (f.clipPos.w - " I_STEREOPARAMS"[2]);\n");
out.Write("\tf.pos.x += " I_STEREOPARAMS"[eye] * (f.pos.w - " I_STEREOPARAMS"[2]);\n");
}
if (primitive_type == PRIMITIVE_LINES)
@ -264,12 +284,12 @@ static inline void EmitVertex(T& out, const char* vertex, API_TYPE ApiType)
if (ApiType == API_OPENGL)
out.Write("\tgl_Position = %s.pos;\n", vertex);
out.Write("\t%s = %s;\n", (ApiType == API_OPENGL) ? "vs" : "gs.vs", vertex);
out.Write("\tps.o = %s;\n", vertex);
if (ApiType == API_OPENGL)
out.Write("\tEmitVertex();\n");
else
out.Write("\toutput.Append(gs);\n");
out.Write("\toutput.Append(ps);\n");
}
void GetGeometryShaderUid(GeometryShaderUid& object, u32 primitive_type, API_TYPE ApiType)

View File

@ -316,11 +316,14 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T
// As a workaround, we interpolate at the centroid of the coveraged pixel, which
// is always inside the primitive.
// Without MSAA, this flag is defined to have no effect.
out.Write("centroid in VS_OUTPUT vs;\n");
uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
out.Write("in VertexData {\n");
out.Write("\tcentroid VS_OUTPUT o;\n");
if (g_ActiveConfig.iStereoMode > 0)
out.Write("flat in int layer;\n");
out.Write("\tflat int layer;\n");
out.Write("};\n");
out.Write("void main()\n{\n");
@ -328,18 +331,18 @@ static inline void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_T
// Let's set up attributes
for (unsigned int i = 0; i < numTexgen; ++i)
{
out.Write("\tfloat3 uv%d = vs.tex%d;\n", i, i);
out.Write("\tfloat3 uv%d = o.tex%d;\n", i, i);
}
out.Write("\tfloat4 clipPos = vs.clipPos;\n");
out.Write("\tfloat4 clipPos = o.clipPos;\n");
if (g_ActiveConfig.bEnablePixelLighting)
{
out.Write("\tfloat4 Normal = vs.Normal;\n");
out.Write("\tfloat4 Normal = o.Normal;\n");
}
// On Mali, global variables must be initialized as constants.
// This is why we initialize these variables locally instead.
out.Write("\tfloat4 colors_0 = vs.colors_0;\n");
out.Write("\tfloat4 colors_1 = vs.colors_1;\n");
out.Write("\tfloat4 colors_0 = o.colors_0;\n");
out.Write("\tfloat4 colors_1 = o.colors_1;\n");
out.Write("\tfloat4 rawpos = gl_FragCoord;\n");
}

View File

@ -72,7 +72,9 @@ static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_typ
out.Write("in float%d tex%d; // ATTR%d,\n", hastexmtx ? 3 : 2, i, SHADER_TEXTURE0_ATTRIB + i);
}
out.Write("centroid out VS_OUTPUT o;\n");
out.Write("out VertexData {\n"
"\tcentroid VS_OUTPUT o;\n"
"};\n");
out.Write("void main()\n{\n");
}