2014-10-16 10:52:32 -06:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
#include "VideoCommon/BPMemory.h"
|
2014-10-16 10:52:32 -06:00
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
|
|
|
#include "VideoCommon/LightingShaderGen.h"
|
2014-12-14 14:27:09 -07:00
|
|
|
#include "VideoCommon/VertexManagerBase.h"
|
2014-10-30 16:40:03 -06:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2014-10-16 10:52:32 -06:00
|
|
|
|
|
|
|
static char text[16384];
|
|
|
|
|
2014-12-14 14:27:09 -07:00
|
|
|
static const char* primitives_ogl[] =
|
|
|
|
{
|
|
|
|
"points",
|
|
|
|
"lines",
|
|
|
|
"triangles"
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* primitives_d3d[] =
|
|
|
|
{
|
|
|
|
"point",
|
|
|
|
"line",
|
|
|
|
"triangle"
|
|
|
|
};
|
|
|
|
|
2014-10-16 10:52:32 -06:00
|
|
|
template<class T>
|
2014-12-14 11:41:16 -07:00
|
|
|
static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 10:52:32 -06:00
|
|
|
{
|
|
|
|
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
|
|
|
|
geometry_shader_uid_data dummy_data;
|
|
|
|
geometry_shader_uid_data* uid_data = out.template GetUidData<geometry_shader_uid_data>();
|
|
|
|
if (uid_data == nullptr)
|
|
|
|
uid_data = &dummy_data;
|
|
|
|
|
|
|
|
out.SetBuffer(text);
|
|
|
|
const bool is_writing_shadercode = (out.GetBuffer() != nullptr);
|
|
|
|
|
|
|
|
if (is_writing_shadercode)
|
|
|
|
text[sizeof(text) - 1] = 0x7C; // canary
|
|
|
|
|
2014-12-14 11:41:16 -07:00
|
|
|
uid_data->primitive_type = primitive_type;
|
2014-12-14 14:27:09 -07:00
|
|
|
const unsigned int vertex_in = primitive_type + 1;
|
|
|
|
const unsigned int vertex_out = primitive_type == PRIMITIVE_TRIANGLES ? 3 : 4;
|
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
|
2014-10-16 10:52:32 -06:00
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
{
|
|
|
|
// Insert layout parameters
|
2014-10-29 07:51:12 -06:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
2014-12-15 14:12:17 -07:00
|
|
|
{
|
2014-12-14 14:27:09 -07:00
|
|
|
out.Write("layout(%s, invocations = %d) in;\n", primitives_ogl[primitive_type], g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", vertex_out);
|
|
|
|
}
|
2014-10-29 07:51:12 -06:00
|
|
|
else
|
2014-12-15 14:12:17 -07:00
|
|
|
{
|
2014-12-14 14:27:09 -07:00
|
|
|
out.Write("layout(%s) in;\n", primitives_ogl[primitive_type]);
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.iStereoMode > 0 ? vertex_out * 2 : vertex_out);
|
|
|
|
}
|
2014-10-16 10:52:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
out.Write("%s", s_lighting_struct);
|
|
|
|
|
|
|
|
// uniforms
|
2014-10-26 12:17:00 -06:00
|
|
|
if (ApiType == API_OPENGL)
|
2014-12-14 13:23:13 -07:00
|
|
|
out.Write("layout(std140%s) uniform GSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 3" : "");
|
2014-10-16 10:52:32 -06:00
|
|
|
else
|
2014-12-14 13:23:13 -07:00
|
|
|
out.Write("cbuffer GSBlock {\n");
|
|
|
|
out.Write(
|
|
|
|
"\tfloat4 " I_STEREOPARAMS";\n"
|
2014-12-14 13:44:33 -07:00
|
|
|
"\tfloat4 " I_LINEPTWIDTH";\n"
|
|
|
|
"\tfloat4 " I_VIEWPORT";\n"
|
|
|
|
"\tuint " I_TEXOFFSETFLAGS"[8];\n"
|
2014-12-14 13:23:13 -07:00
|
|
|
"};\n");
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-12-14 13:44:33 -07:00
|
|
|
uid_data->numTexGens = bpmem.genMode.numtexgens;
|
2014-11-22 11:18:45 -07:00
|
|
|
uid_data->pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
|
|
|
|
|
2014-11-02 15:40:52 -07:00
|
|
|
GenerateVSOutputStruct<T>(out, ApiType);
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-11-02 15:40:52 -07:00
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
{
|
2014-12-14 13:50:46 -07:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
out.Write("#define InstanceID gl_InvocationID\n");
|
|
|
|
|
2014-12-14 14:27:09 -07:00
|
|
|
out.Write("centroid in VS_OUTPUT o[%d];\n", vertex_in);
|
2014-12-03 14:35:34 -07:00
|
|
|
out.Write("centroid out VS_OUTPUT vs;\n");
|
2014-12-14 14:27:09 -07:00
|
|
|
|
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
|
|
|
out.Write("flat out int layer;\n");
|
2014-11-02 15:40:52 -07:00
|
|
|
|
|
|
|
out.Write("void main()\n{\n");
|
|
|
|
}
|
|
|
|
else // D3D
|
|
|
|
{
|
2014-12-03 14:35:34 -07:00
|
|
|
out.Write("struct GS_OUTPUT {\n");
|
|
|
|
out.Write("\tVS_OUTPUT vs;\n");
|
2014-12-14 14:27:09 -07:00
|
|
|
|
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
|
|
|
out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n");
|
|
|
|
|
2014-12-03 14:35:34 -07:00
|
|
|
out.Write("};\n");
|
|
|
|
|
2014-11-14 04:39:25 -07:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
{
|
2014-12-15 14:12:17 -07:00
|
|
|
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);
|
2014-11-14 04:39:25 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-15 14:12:17 -07:00
|
|
|
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);
|
2014-11-14 04:39:25 -07:00
|
|
|
}
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-11-02 15:40:52 -07:00
|
|
|
out.Write("\tGS_OUTPUT gs;\n");
|
|
|
|
}
|
2014-10-26 17:29:37 -06:00
|
|
|
|
2014-11-02 15:40:52 -07:00
|
|
|
out.Write("\tVS_OUTPUT f;\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
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");
|
|
|
|
}
|
2014-12-15 14:29:56 -07:00
|
|
|
else if (primitive_type == PRIMITIVE_POINTS)
|
|
|
|
{
|
|
|
|
// 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_LINEPTWIDTH"[1] / " I_VIEWPORT".x, -" I_LINEPTWIDTH"[1] / " I_VIEWPORT".y) * o[0].pos.w;\n");
|
|
|
|
}
|
2014-12-15 14:12:17 -07:00
|
|
|
|
2014-12-14 14:27:09 -07:00
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
2014-12-03 14:35:34 -07:00
|
|
|
{
|
2014-12-14 14:27:09 -07:00
|
|
|
// If the GPU supports invocation we don't need a for loop and can simply use the
|
|
|
|
// invocation identifier to determine which layer we're rendering.
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
out.Write("\tint eye = InstanceID;\n");
|
|
|
|
else
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\tfor (int eye = 0; eye < 2; ++eye) {\n");
|
2014-12-03 14:35:34 -07:00
|
|
|
}
|
2014-12-14 14:27:09 -07:00
|
|
|
|
|
|
|
out.Write("\tfor (int i = 0; i < %d; ++i) {\n", vertex_in);
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\tVS_OUTPUT f = o[i];\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-30 16:29:56 -06:00
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
2014-11-01 16:55:04 -06:00
|
|
|
{
|
2014-12-14 14:27:09 -07:00
|
|
|
// Select the output layer
|
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
{
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\tgl_Layer = eye;\n");
|
|
|
|
out.Write("\tlayer = eye;\n");
|
2014-12-14 14:27:09 -07:00
|
|
|
}
|
|
|
|
else
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\tgs.layer = eye;\n");
|
2014-12-14 14:27:09 -07:00
|
|
|
|
2014-11-11 16:40:58 -07:00
|
|
|
// 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
|
|
|
|
// vertex which contains the negated z-component of the original vertex.
|
|
|
|
// For negative parallax (out-of-screen effects) we subtract a convergence value from
|
|
|
|
// 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"
|
2014-12-15 14:12:17 -07:00
|
|
|
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");
|
2014-11-01 16:55:04 -06:00
|
|
|
}
|
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
if (primitive_type == PRIMITIVE_LINES)
|
|
|
|
{
|
|
|
|
out.Write("\tVS_OUTPUT l = f;\n");
|
|
|
|
out.Write("\tVS_OUTPUT r = f;\n");
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\tl.pos.xy -= offset * l.pos.w;\n");
|
|
|
|
out.Write("\tr.pos.xy += offset * r.pos.w;\n");
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
for (unsigned int i = 0; i < bpmem.genMode.numtexgens; ++i)
|
|
|
|
{
|
2014-12-15 14:29:56 -07:00
|
|
|
out.Write("\tr.tex%d.x += " I_LINEPTWIDTH"[2] * (" I_TEXOFFSETFLAGS"[%d] & 0x01);\n", i, i);
|
2014-12-15 14:12:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EmitVertex<T>(out, "l", ApiType);
|
|
|
|
EmitVertex<T>(out, "r", ApiType);
|
|
|
|
}
|
2014-12-15 14:29:56 -07:00
|
|
|
else if (primitive_type == PRIMITIVE_POINTS)
|
|
|
|
{
|
|
|
|
out.Write("VS_OUTPUT ll = f;\n");
|
|
|
|
out.Write("VS_OUTPUT lr = f;\n");
|
|
|
|
out.Write("VS_OUTPUT ul = f;\n");
|
|
|
|
out.Write("VS_OUTPUT ur = f;\n");
|
|
|
|
|
|
|
|
out.Write("ll.pos.xy += float2(-1,-1) * offset;\n");
|
|
|
|
out.Write("lr.pos.xy += float2(1,-1) * offset;\n");
|
|
|
|
out.Write("ul.pos.xy += float2(-1,1) * offset;\n");
|
|
|
|
out.Write("ur.pos.xy += offset;\n");
|
|
|
|
|
|
|
|
out.Write("float2 texOffset = float2(" I_LINEPTWIDTH"[3], " I_LINEPTWIDTH"[3]);\n");
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < bpmem.genMode.numtexgens; ++i)
|
|
|
|
{
|
|
|
|
out.Write("ll.tex%d.xy += float2(0,1) * texOffset * (" I_TEXOFFSETFLAGS"[%d] & 0x10);\n", i, i);
|
|
|
|
out.Write("lr.tex%d.xy += texOffset * (" I_TEXOFFSETFLAGS"[%d] & 0x10);\n", i, i);
|
|
|
|
out.Write("ur.tex%d.xy += float2(1,0) * texOffset * (" I_TEXOFFSETFLAGS"[%d] & 0x10);\n", i, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
EmitVertex<T>(out, "ll", ApiType);
|
|
|
|
EmitVertex<T>(out, "lr", ApiType);
|
|
|
|
EmitVertex<T>(out, "ul", ApiType);
|
|
|
|
EmitVertex<T>(out, "ur", ApiType);
|
|
|
|
}
|
2014-11-02 15:40:52 -07:00
|
|
|
else
|
2014-12-15 14:12:17 -07:00
|
|
|
{
|
|
|
|
EmitVertex<T>(out, "f", ApiType);
|
|
|
|
}
|
2014-11-02 15:40:52 -07:00
|
|
|
|
2014-10-16 10:52:32 -06:00
|
|
|
out.Write("\t}\n");
|
2014-11-02 15:40:52 -07:00
|
|
|
|
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
out.Write("\tEndPrimitive();\n");
|
|
|
|
else
|
2014-12-15 14:12:17 -07:00
|
|
|
out.Write("\toutput.RestartStrip();\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
if (g_ActiveConfig.iStereoMode > 0 && !g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
out.Write("\t}\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-16 10:52:32 -06:00
|
|
|
out.Write("}\n");
|
|
|
|
|
|
|
|
if (is_writing_shadercode)
|
|
|
|
{
|
|
|
|
if (text[sizeof(text) - 1] != 0x7C)
|
|
|
|
PanicAlert("GeometryShader generator - buffer too small, canary has been eaten!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 14:12:17 -07:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-12-14 11:41:16 -07:00
|
|
|
void GetGeometryShaderUid(GeometryShaderUid& object, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 10:52:32 -06:00
|
|
|
{
|
2014-12-14 11:41:16 -07:00
|
|
|
GenerateGeometryShader<GeometryShaderUid>(object, primitive_type, ApiType);
|
2014-10-16 10:52:32 -06:00
|
|
|
}
|
|
|
|
|
2014-12-14 11:41:16 -07:00
|
|
|
void GenerateGeometryShaderCode(ShaderCode& object, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 10:52:32 -06:00
|
|
|
{
|
2014-12-14 11:41:16 -07:00
|
|
|
GenerateGeometryShader<ShaderCode>(object, primitive_type, ApiType);
|
2014-10-16 10:52:32 -06:00
|
|
|
}
|