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>
|
|
|
|
#include <locale.h>
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <xlocale.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
|
|
|
#include "VideoCommon/LightingShaderGen.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];
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
static inline void GenerateGeometryShader(T& out, u32 components, API_TYPE ApiType)
|
|
|
|
{
|
|
|
|
// 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);
|
|
|
|
#ifndef ANDROID
|
|
|
|
locale_t locale;
|
|
|
|
locale_t old_locale;
|
|
|
|
if (is_writing_shadercode)
|
|
|
|
{
|
|
|
|
locale = newlocale(LC_NUMERIC_MASK, "C", nullptr); // New locale for compilation
|
|
|
|
old_locale = uselocale(locale); // Apply the locale for this thread
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (is_writing_shadercode)
|
|
|
|
text[sizeof(text) - 1] = 0x7C; // canary
|
|
|
|
|
|
|
|
out.Write("//Geometry Shader for 3D stereoscopy\n");
|
|
|
|
|
2014-10-30 16:29:56 -06: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-10-30 16:29:56 -06:00
|
|
|
out.Write("layout(triangles, invocations = %d) in;\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
2014-10-29 07:51:12 -06: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 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-10-16 10:52:32 -06:00
|
|
|
out.Write("layout(std140%s) uniform VSBlock {\n", g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 2" : "");
|
|
|
|
else
|
|
|
|
out.Write("cbuffer VSBlock {\n");
|
2014-10-29 07:15:12 -06:00
|
|
|
out.Write(s_shader_uniforms);
|
|
|
|
out.Write("};\n");
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-10-29 06:16:24 -06:00
|
|
|
GenerateVSOutputStruct(out, ApiType);
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-10-24 18:05:49 -06:00
|
|
|
out.Write("centroid in VS_OUTPUT v[];\n");
|
|
|
|
out.Write("centroid out VS_OUTPUT o;\n");
|
2014-10-16 10:52:32 -06:00
|
|
|
|
2014-10-29 07:57:49 -06:00
|
|
|
out.Write("flat out int layer;\n");
|
2014-10-26 17:29:37 -06:00
|
|
|
|
2014-10-16 10:52:32 -06:00
|
|
|
out.Write("void main()\n{\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-29 19:08:15 -06:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
|
|
|
|
out.Write("\tlayer = gl_InvocationID;\n");
|
|
|
|
else
|
2014-10-30 16:29:56 -06:00
|
|
|
out.Write("\tfor (layer = 0; layer < %d; ++layer) {\n", g_ActiveConfig.iStereoMode > 0 ? 2 : 1);
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-29 19:08:15 -06:00
|
|
|
out.Write("\tgl_Layer = layer;\n");
|
2014-10-30 16:40:03 -06:00
|
|
|
out.Write("\tvec4 stereoproj = " I_PROJECTION"[0];\n");
|
|
|
|
out.Write("\tstereoproj[2] += " I_STEREOOFFSET"[layer] * stereoproj[0];\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-16 10:52:32 -06:00
|
|
|
out.Write("\tfor (int i = 0; i < gl_in.length(); ++i) {\n");
|
2014-10-24 18:05:49 -06:00
|
|
|
out.Write("\t\to = v[i];\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-30 16:29:56 -06:00
|
|
|
if (g_ActiveConfig.iStereoMode > 0)
|
2014-10-29 19:08:15 -06:00
|
|
|
out.Write("\t\to.pos = float4(dot(stereoproj, v[i].rawpos), dot(" I_PROJECTION"[1], v[i].rawpos), dot(" I_PROJECTION"[2], v[i].rawpos), dot(" I_PROJECTION"[3], v[i].rawpos)); \n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-26 12:17:00 -06:00
|
|
|
out.Write("\t\tgl_Position = o.pos;\n");
|
2014-10-16 10:52:32 -06:00
|
|
|
out.Write("\t\tEmitVertex();\n");
|
|
|
|
out.Write("\t}\n");
|
|
|
|
out.Write("\tEndPrimitive();\n");
|
2014-10-30 08:13:31 -06:00
|
|
|
|
2014-10-29 07:51:12 -06:00
|
|
|
if (!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!");
|
|
|
|
|
|
|
|
#ifndef ANDROID
|
|
|
|
uselocale(old_locale); // restore locale
|
|
|
|
freelocale(locale);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetGeometryShaderUid(GeometryShaderUid& object, u32 components, API_TYPE ApiType)
|
|
|
|
{
|
|
|
|
GenerateGeometryShader<GeometryShaderUid>(object, components, ApiType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GenerateGeometryShaderCode(ShaderCode& object, u32 components, API_TYPE ApiType)
|
|
|
|
{
|
|
|
|
GenerateGeometryShader<ShaderCode>(object, components, ApiType);
|
|
|
|
}
|