2014-10-16 16:52:32 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
|
|
|
#include "VideoCommon/LightingShaderGen.h"
|
2014-10-30 22:40:03 +00:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2014-10-16 16:52:32 +00:00
|
|
|
|
|
|
|
static char text[16384];
|
|
|
|
|
|
|
|
template<class T>
|
2014-12-14 18:41:16 +00:00
|
|
|
static inline void GenerateGeometryShader(T& out, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 16:52:32 +00: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 18:41:16 +00:00
|
|
|
uid_data->primitive_type = primitive_type;
|
2014-10-30 22:29:56 +00:00
|
|
|
uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
|
2014-10-16 16:52:32 +00:00
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
{
|
|
|
|
// Insert layout parameters
|
2014-10-29 13:51:12 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
2014-10-30 22:29:56 +00:00
|
|
|
out.Write("layout(triangles, invocations = %d) in;\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
2014-10-29 13:51:12 +00:00
|
|
|
else
|
|
|
|
out.Write("layout(triangles) in;\n");
|
|
|
|
out.Write("layout(triangle_strip, max_vertices = %d) out;\n", g_ActiveConfig.backend_info.bSupportsGSInstancing ? 3 : 6);
|
2014-10-16 16:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out.Write("%s", s_lighting_struct);
|
|
|
|
|
|
|
|
// uniforms
|
2014-10-26 18:17:00 +00:00
|
|
|
if (ApiType == API_OPENGL)
|
2014-12-14 20:23:13 +00:00
|
|
|
out.Write("layout(std140%s) uniform GSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 3" : "");
|
2014-10-16 16:52:32 +00:00
|
|
|
else
|
2014-12-14 20:23:13 +00:00
|
|
|
out.Write("cbuffer GSBlock {\n");
|
|
|
|
out.Write(
|
|
|
|
"\tfloat4 " I_STEREOPARAMS";\n"
|
|
|
|
"};\n");
|
2014-10-16 16:52:32 +00:00
|
|
|
|
2014-11-22 18:18:45 +00:00
|
|
|
uid_data->numTexGens = xfmem.numTexGen.numTexGens;
|
|
|
|
uid_data->pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
|
|
|
|
|
2014-11-02 22:40:52 +00:00
|
|
|
GenerateVSOutputStruct<T>(out, ApiType);
|
2014-10-16 16:52:32 +00:00
|
|
|
|
2014-11-02 22:40:52 +00:00
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
{
|
|
|
|
out.Write("centroid in VS_OUTPUT o[3];\n");
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("centroid out VS_OUTPUT vs;\n");
|
|
|
|
out.Write("flat out int layer;\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
|
|
|
out.Write("void main()\n{\n");
|
|
|
|
}
|
|
|
|
else // D3D
|
|
|
|
{
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("struct GS_OUTPUT {\n");
|
|
|
|
out.Write("\tVS_OUTPUT vs;\n");
|
|
|
|
out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n");
|
|
|
|
out.Write("};\n");
|
|
|
|
|
2014-11-14 11:39:25 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
{
|
|
|
|
out.Write("[maxvertexcount(3)]\n[instance(%d)]\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
|
|
|
out.Write("void main(triangle VS_OUTPUT o[3], inout TriangleStream<GS_OUTPUT> Output, in uint InstanceID : SV_GSInstanceID)\n{\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out.Write("[maxvertexcount(6)]\n");
|
|
|
|
out.Write("void main(triangle VS_OUTPUT o[3], inout TriangleStream<GS_OUTPUT> Output)\n{\n");
|
|
|
|
}
|
2014-10-16 16:52:32 +00:00
|
|
|
|
2014-11-02 22:40:52 +00:00
|
|
|
out.Write("\tGS_OUTPUT gs;\n");
|
|
|
|
}
|
2014-10-26 23:29:37 +00:00
|
|
|
|
2014-11-02 22:40:52 +00:00
|
|
|
out.Write("\tVS_OUTPUT f;\n");
|
2014-10-30 14:13:31 +00:00
|
|
|
|
2014-11-11 23:40:58 +00: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.
|
2014-10-30 01:08:15 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
2014-11-14 11:39:25 +00:00
|
|
|
{
|
|
|
|
if (ApiType == API_OPENGL)
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("\tint eye = gl_InvocationID;\n");
|
2014-11-14 11:39:25 +00:00
|
|
|
else
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("\tint eye = InstanceID;\n");
|
2014-11-14 11:39:25 +00:00
|
|
|
}
|
2014-10-30 01:08:15 +00:00
|
|
|
else
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("\tfor (int eye = 0; eye < %d; ++eye) {\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
2014-10-30 14:13:31 +00:00
|
|
|
|
2014-11-16 08:21:19 +00:00
|
|
|
out.Write("\tfor (int i = 0; i < 3; ++i) {\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2014-11-14 11:39:25 +00:00
|
|
|
// Select the output layer
|
2014-11-02 22:40:52 +00:00
|
|
|
if (ApiType == API_OPENGL)
|
2014-12-03 21:35:34 +00:00
|
|
|
{
|
|
|
|
out.Write("\t\tgl_Layer = eye;\n");
|
|
|
|
out.Write("\t\tlayer = eye;\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
out.Write("\t\tgs.layer = eye;\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
2014-11-04 10:53:19 +00:00
|
|
|
out.Write("\t\tf = o[i];\n");
|
2014-11-16 08:21:19 +00:00
|
|
|
out.Write("\t\tfloat4 pos = o[i].pos;\n");
|
2014-10-30 14:13:31 +00:00
|
|
|
|
2014-10-30 22:29:56 +00:00
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
2014-11-01 22:55:04 +00:00
|
|
|
{
|
2014-11-11 23:40:58 +00: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-03 21:35:34 +00:00
|
|
|
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");
|
2014-11-01 22:55:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-16 08:21:19 +00:00
|
|
|
out.Write("\t\tf.pos.x = pos.x;\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
out.Write("\t\tgl_Position = pos;\n");
|
|
|
|
|
2014-12-03 21:35:34 +00:00
|
|
|
out.Write("\t\t%s = f;\n", (ApiType == API_OPENGL) ? "vs" : "gs.vs");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
out.Write("\t\tEmitVertex();\n");
|
|
|
|
else
|
|
|
|
out.Write("\t\tOutput.Append(gs);\n");
|
|
|
|
|
2014-10-16 16:52:32 +00:00
|
|
|
out.Write("\t}\n");
|
2014-11-02 22:40:52 +00:00
|
|
|
|
|
|
|
if (ApiType == API_OPENGL)
|
|
|
|
out.Write("\tEndPrimitive();\n");
|
|
|
|
else
|
2014-11-14 11:39:25 +00:00
|
|
|
out.Write("\tOutput.RestartStrip();\n");
|
2014-10-30 14:13:31 +00:00
|
|
|
|
2014-10-29 13:51:12 +00:00
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
out.Write("\t}\n");
|
2014-10-30 14:13:31 +00:00
|
|
|
|
2014-10-16 16:52:32 +00: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-14 18:41:16 +00:00
|
|
|
void GetGeometryShaderUid(GeometryShaderUid& object, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 16:52:32 +00:00
|
|
|
{
|
2014-12-14 18:41:16 +00:00
|
|
|
GenerateGeometryShader<GeometryShaderUid>(object, primitive_type, ApiType);
|
2014-10-16 16:52:32 +00:00
|
|
|
}
|
|
|
|
|
2014-12-14 18:41:16 +00:00
|
|
|
void GenerateGeometryShaderCode(ShaderCode& object, u32 primitive_type, API_TYPE ApiType)
|
2014-10-16 16:52:32 +00:00
|
|
|
{
|
2014-12-14 18:41:16 +00:00
|
|
|
GenerateGeometryShader<ShaderCode>(object, primitive_type, ApiType);
|
2014-10-16 16:52:32 +00:00
|
|
|
}
|