mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-21 05:09:34 -06:00
VideoCommon: Merge LineGeometryShader into GeometryShaderGen.
This adds line-width emulation support to OpenGL.
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include "VideoCommon/BPMemory.h"
|
||||
#include "VideoCommon/GeometryShaderGen.h"
|
||||
#include "VideoCommon/LightingShaderGen.h"
|
||||
#include "VideoCommon/VertexManagerBase.h"
|
||||
@ -43,17 +44,22 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
|
||||
|
||||
uid_data->primitive_type = primitive_type;
|
||||
const unsigned int vertex_in = primitive_type + 1;
|
||||
uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
|
||||
const unsigned int vertex_out = primitive_type == PRIMITIVE_TRIANGLES ? 3 : 4;
|
||||
|
||||
uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
|
||||
if (ApiType == API_OPENGL)
|
||||
{
|
||||
// Insert layout parameters
|
||||
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
||||
{
|
||||
out.Write("layout(%s, invocations = %d) in;\n", primitives_ogl[primitive_type], g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
||||
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", vertex_out);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.Write("layout(%s) in;\n", primitives_ogl[primitive_type]);
|
||||
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.backend_info.bSupportsGSInstancing ? vertex_out : vertex_out * 2);
|
||||
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.iStereoMode > 0 ? vertex_out * 2 : vertex_out);
|
||||
}
|
||||
}
|
||||
|
||||
out.Write("%s", s_lighting_struct);
|
||||
@ -98,15 +104,15 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
|
||||
|
||||
out.Write("};\n");
|
||||
|
||||
out.Write("[maxvertexcount(%d)]\n", vertex_out);
|
||||
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
||||
{
|
||||
out.Write("[instance(%d)]\n", 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("[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);
|
||||
}
|
||||
else
|
||||
{
|
||||
out.Write("void main(%s VS_OUTPUT o[%d], inout TriangleStream<GS_OUTPUT> Output)\n{\n", primitives_d3d[primitive_type], vertex_in);
|
||||
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("\tGS_OUTPUT gs;\n");
|
||||
@ -114,6 +120,27 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
|
||||
|
||||
out.Write("\tVS_OUTPUT f;\n");
|
||||
|
||||
if (primitive_type == PRIMITIVE_LINES)
|
||||
{
|
||||
// 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");
|
||||
out.Write("\tfloat2 to = abs(o[1].pos.xy - o[0].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.
|
||||
out.Write("\tif (" I_VIEWPORT".y * to.y > " I_VIEWPORT".x * to.x) {\n");
|
||||
// Line is more tall. Extend geometry left and right.
|
||||
// Lerp LineWidth/2 from [0..VpWidth] to [-1..1]
|
||||
out.Write("\t\toffset = float2(" I_LINEPTWIDTH"[0] / " I_VIEWPORT".x, 0);\n");
|
||||
out.Write("\t} else {\n");
|
||||
// Line is more wide. Extend geometry up and down.
|
||||
// Lerp LineWidth/2 from [0..VpHeight] to [1..-1]
|
||||
out.Write("\t\toffset = float2(0, -" I_LINEPTWIDTH"[0] / " I_VIEWPORT".y);\n");
|
||||
out.Write("\t}\n");
|
||||
}
|
||||
|
||||
if (g_ActiveConfig.iStereoMode > 0)
|
||||
{
|
||||
// If the GPU supports invocation we don't need a for loop and can simply use the
|
||||
@ -121,24 +148,22 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
|
||||
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
||||
out.Write("\tint eye = InstanceID;\n");
|
||||
else
|
||||
out.Write("\tfor (int eye = 0; eye < %d; ++eye) {\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
||||
out.Write("\tfor (int eye = 0; eye < 2; ++eye) {\n");
|
||||
}
|
||||
|
||||
out.Write("\tfor (int i = 0; i < %d; ++i) {\n", vertex_in);
|
||||
|
||||
out.Write("\t\tf = o[i];\n");
|
||||
out.Write("\t\tfloat4 pos = o[i].pos;\n");
|
||||
out.Write("\tVS_OUTPUT f = o[i];\n");
|
||||
|
||||
if (g_ActiveConfig.iStereoMode > 0)
|
||||
{
|
||||
// Select the output layer
|
||||
if (ApiType == API_OPENGL)
|
||||
{
|
||||
out.Write("\t\tgl_Layer = eye;\n");
|
||||
out.Write("\t\tlayer = eye;\n");
|
||||
out.Write("\tgl_Layer = eye;\n");
|
||||
out.Write("\tlayer = eye;\n");
|
||||
}
|
||||
else
|
||||
out.Write("\t\tgs.layer = eye;\n");
|
||||
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
|
||||
@ -147,30 +172,40 @@ 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("\t\tf.clipPos.x = o[i].clipPos.x + " I_STEREOPARAMS"[eye] * (o[i].clipPos.w - " I_STEREOPARAMS"[2]);\n");
|
||||
out.Write("\t\tpos.x = o[i].pos.x + " I_STEREOPARAMS"[eye] * (o[i].pos.w - " I_STEREOPARAMS"[2]);\n");
|
||||
out.Write("\t\tf.pos.x = pos.x;\n");
|
||||
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");
|
||||
}
|
||||
|
||||
if (ApiType == API_OPENGL)
|
||||
out.Write("\t\tgl_Position = pos;\n");
|
||||
if (primitive_type == PRIMITIVE_LINES)
|
||||
{
|
||||
out.Write("\tVS_OUTPUT l = f;\n");
|
||||
out.Write("\tVS_OUTPUT r = f;\n");
|
||||
|
||||
out.Write("\t\t%s = f;\n", (ApiType == API_OPENGL) ? "vs" : "gs.vs");
|
||||
out.Write("\tl.pos.xy -= offset * l.pos.w;\n");
|
||||
out.Write("\tr.pos.xy += offset * r.pos.w;\n");
|
||||
|
||||
if (ApiType == API_OPENGL)
|
||||
out.Write("\t\tEmitVertex();\n");
|
||||
for (unsigned int i = 0; i < bpmem.genMode.numtexgens; ++i)
|
||||
{
|
||||
out.Write("\tr.tex%d.x += " I_LINEPTWIDTH"[2] * (" I_TEXOFFSETFLAGS"[%d] % 2);\n", i, i);
|
||||
}
|
||||
|
||||
EmitVertex<T>(out, "l", ApiType);
|
||||
EmitVertex<T>(out, "r", ApiType);
|
||||
}
|
||||
else
|
||||
out.Write("\t\tOutput.Append(gs);\n");
|
||||
{
|
||||
EmitVertex<T>(out, "f", ApiType);
|
||||
}
|
||||
|
||||
out.Write("\t}\n");
|
||||
|
||||
if (ApiType == API_OPENGL)
|
||||
out.Write("\tEndPrimitive();\n");
|
||||
else
|
||||
out.Write("\tOutput.RestartStrip();\n");
|
||||
out.Write("\toutput.RestartStrip();\n");
|
||||
|
||||
if (!g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
||||
out.Write("\t}\n");
|
||||
if (g_ActiveConfig.iStereoMode > 0 && !g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
||||
out.Write("\t}\n");
|
||||
|
||||
out.Write("}\n");
|
||||
|
||||
@ -181,6 +216,20 @@ static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE A
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
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);
|
||||
|
||||
if (ApiType == API_OPENGL)
|
||||
out.Write("\tEmitVertex();\n");
|
||||
else
|
||||
out.Write("\toutput.Append(gs);\n");
|
||||
}
|
||||
|
||||
void GetGeometryShaderUid(GeometryShaderUid& object, u32 primitive_type, API_TYPE ApiType)
|
||||
{
|
||||
GenerateGeometryShader<GeometryShaderUid>(object, primitive_type, ApiType);
|
||||
|
Reference in New Issue
Block a user