mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-28 18:33:14 +00:00
LightingShaderGen: Use macro magic instead of snprintf. Should fix performance problems.
This commit is contained in:
parent
22d9736787
commit
fe2ca814c5
@ -9,40 +9,22 @@
|
||||
#include "NativeVertexFormat.h"
|
||||
#include "XFMemory.h"
|
||||
|
||||
static const char* LightCol(const char* lightsName, unsigned int index, const char* swizzle)
|
||||
{
|
||||
static char result[32];
|
||||
snprintf(result, sizeof(result), "%s[5*%d].%s", lightsName, index, swizzle);
|
||||
return result;
|
||||
}
|
||||
|
||||
static const char* LightCosAtt(const char* lightsName, unsigned int index)
|
||||
{
|
||||
static char result[32];
|
||||
snprintf(result, sizeof(result), "%s[5*%d+1]", lightsName, index);
|
||||
return result;
|
||||
}
|
||||
#define LIGHT_COL "%s[5*%d].%s"
|
||||
#define LIGHT_COL_PARAMS(lightsName, index, swizzle) (lightsName), (index), (swizzle)
|
||||
|
||||
static const char* LightDistAtt(const char* lightsName, unsigned int index)
|
||||
{
|
||||
static char result[32];
|
||||
snprintf(result, sizeof(result), "%s[5*%d+2]", lightsName, index);
|
||||
return result;
|
||||
}
|
||||
#define LIGHT_COSATT "%s[5*%d+1]"
|
||||
#define LIGHT_COSATT_PARAMS(lightsName, index) (lightsName), (index)
|
||||
|
||||
static const char* LightPos(const char* lightsName, unsigned int index)
|
||||
{
|
||||
static char result[32];
|
||||
snprintf(result, sizeof(result), "%s[5*%d+3]", lightsName, index);
|
||||
return result;
|
||||
}
|
||||
#define LIGHT_DISTATT "%s[5*%d+2]"
|
||||
#define LIGHT_DISTATT_PARAMS(lightsName, index) (lightsName), (index)
|
||||
|
||||
#define LIGHT_POS "%s[5*%d+3]"
|
||||
#define LIGHT_POS_PARAMS(lightsName, index) (lightsName), (index)
|
||||
|
||||
#define LIGHT_DIR "%s[5*%d+4]"
|
||||
#define LIGHT_DIR_PARAMS(lightsName, index) (lightsName), (index)
|
||||
|
||||
static const char* LightDir(const char* lightsName, unsigned int index)
|
||||
{
|
||||
static char result[32];
|
||||
snprintf(result, sizeof(result), "%s[5*%d+4]", lightsName, index);
|
||||
return result;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static void GenerateLightShader(T& object, LightingUidData& uid_data, int index, int litchan_index, const char* lightsName, int coloralpha)
|
||||
@ -62,13 +44,13 @@ static void GenerateLightShader(T& object, LightingUidData& uid_data, int index,
|
||||
switch (chan.diffusefunc)
|
||||
{
|
||||
case LIGHTDIF_NONE:
|
||||
object.Write("lacc.%s += %s;\n", swizzle, LightCol(lightsName, index, swizzle));
|
||||
object.Write("lacc.%s += " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
|
||||
break;
|
||||
case LIGHTDIF_SIGN:
|
||||
case LIGHTDIF_CLAMP:
|
||||
object.Write("ldir = normalize(%s.xyz - pos.xyz);\n", LightPos(lightsName, index));
|
||||
object.Write("lacc.%s += %sdot(ldir, _norm0)) * %s;\n",
|
||||
swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", LightCol(lightsName, index, swizzle));
|
||||
object.Write("ldir = normalize(" LIGHT_POS".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
|
||||
object.Write("lacc.%s += %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
|
||||
swizzle, chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(", LIGHT_COL_PARAMS(lightsName, index, swizzle));
|
||||
break;
|
||||
default: _assert_(0);
|
||||
}
|
||||
@ -77,31 +59,34 @@ static void GenerateLightShader(T& object, LightingUidData& uid_data, int index,
|
||||
{
|
||||
if (chan.attnfunc == 3)
|
||||
{ // spot
|
||||
object.Write("ldir = %s.xyz - pos.xyz;\n", LightPos(lightsName, index));
|
||||
object.Write("ldir = " LIGHT_POS".xyz - pos.xyz;\n", LIGHT_POS_PARAMS(lightsName, index));
|
||||
object.Write("dist2 = dot(ldir, ldir);\n"
|
||||
"dist = sqrt(dist2);\n"
|
||||
"ldir = ldir / dist;\n"
|
||||
"attn = max(0.0f, dot(ldir, %s.xyz));\n", LightDir(lightsName, index));
|
||||
object.Write("attn = max(0.0f, dot(%s.xyz, float3(1.0f, attn, attn*attn))) / dot(%s.xyz, float3(1.0f,dist,dist2));\n", LightCosAtt(lightsName, index), LightDistAtt(lightsName, index));
|
||||
"attn = max(0.0f, dot(ldir, " LIGHT_DIR".xyz));\n",
|
||||
LIGHT_DIR_PARAMS(lightsName, index));
|
||||
object.Write("attn = max(0.0f, dot(" LIGHT_COSATT".xyz, float3(1.0f, attn, attn*attn))) / dot(" LIGHT_DISTATT".xyz, float3(1.0f,dist,dist2));\n",
|
||||
LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
|
||||
}
|
||||
else if (chan.attnfunc == 1)
|
||||
{ // specular
|
||||
object.Write("ldir = normalize(%s.xyz);\n", LightPos(lightsName, index));
|
||||
object.Write("attn = (dot(_norm0,ldir) >= 0.0f) ? max(0.0f, dot(_norm0, %s.xyz)) : 0.0f;\n", LightDir(lightsName, index));
|
||||
object.Write("attn = max(0.0f, dot(%s.xyz, float3(1,attn,attn*attn))) / dot(%s.xyz, float3(1,attn,attn*attn));\n", LightCosAtt(lightsName, index), LightDistAtt(lightsName, index));
|
||||
object.Write("ldir = normalize(" LIGHT_POS".xyz);\n", LIGHT_POS_PARAMS(lightsName, index));
|
||||
object.Write("attn = (dot(_norm0,ldir) >= 0.0f) ? max(0.0f, dot(_norm0, " LIGHT_DIR".xyz)) : 0.0f;\n", LIGHT_DIR_PARAMS(lightsName, index));
|
||||
object.Write("attn = max(0.0f, dot(" LIGHT_COSATT".xyz, float3(1,attn,attn*attn))) / dot(" LIGHT_DISTATT".xyz, float3(1,attn,attn*attn));\n",
|
||||
LIGHT_COSATT_PARAMS(lightsName, index), LIGHT_DISTATT_PARAMS(lightsName, index));
|
||||
}
|
||||
|
||||
switch (chan.diffusefunc)
|
||||
{
|
||||
case LIGHTDIF_NONE:
|
||||
object.Write("lacc.%s += attn * %s;\n", swizzle, LightCol(lightsName, index, swizzle));
|
||||
object.Write("lacc.%s += attn * " LIGHT_COL";\n", swizzle, LIGHT_COL_PARAMS(lightsName, index, swizzle));
|
||||
break;
|
||||
case LIGHTDIF_SIGN:
|
||||
case LIGHTDIF_CLAMP:
|
||||
object.Write("lacc.%s += attn * %sdot(ldir, _norm0)) * %s;\n",
|
||||
object.Write("lacc.%s += attn * %sdot(ldir, _norm0)) * " LIGHT_COL";\n",
|
||||
swizzle,
|
||||
chan.diffusefunc != LIGHTDIF_SIGN ? "max(0.0f," :"(",
|
||||
LightCol(lightsName, index, swizzle));
|
||||
LIGHT_COL_PARAMS(lightsName, index, swizzle));
|
||||
break;
|
||||
default: _assert_(0);
|
||||
}
|
||||
|
@ -20,7 +20,7 @@
|
||||
static char text[16768];
|
||||
|
||||
template<class T>
|
||||
static void DefineVSOutputStructMember(T& object, API_TYPE api_type, const char* type, const char* name, int var_index, const char* semantic, int semantic_index = -1)
|
||||
static inline void DefineVSOutputStructMember(T& object, API_TYPE api_type, const char* type, const char* name, int var_index, const char* semantic, int semantic_index = -1)
|
||||
{
|
||||
object.Write(" %s %s", type, name);
|
||||
if (var_index != -1)
|
||||
@ -38,7 +38,7 @@ static void DefineVSOutputStructMember(T& object, API_TYPE api_type, const char*
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static void GenerateVSOutputStruct(T& object, u32 components, API_TYPE api_type)
|
||||
static inline void GenerateVSOutputStruct(T& object, u32 components, API_TYPE api_type)
|
||||
{
|
||||
object.Write("struct VS_OUTPUT {\n");
|
||||
DefineVSOutputStructMember(object, api_type, "float4", "pos", -1, "POSITION");
|
||||
@ -67,7 +67,7 @@ static void GenerateVSOutputStruct(T& object, u32 components, API_TYPE api_type)
|
||||
}
|
||||
|
||||
template<class T>
|
||||
static void GenerateVertexShader(T& out, u32 components, API_TYPE api_type)
|
||||
static inline void GenerateVertexShader(T& out, u32 components, API_TYPE api_type)
|
||||
{
|
||||
// Non-uid template parameters will write to the dummy data (=> gets optimized out)
|
||||
vertex_shader_uid_data dummy_data;
|
||||
@ -353,7 +353,7 @@ static void GenerateVertexShader(T& out, u32 components, API_TYPE api_type)
|
||||
// transform the light dir into tangent space
|
||||
uid_data.texMtxInfo[i].embosslightshift = xfregs.texMtxInfo[i].embosslightshift;
|
||||
uid_data.texMtxInfo[i].embosssourceshift = xfregs.texMtxInfo[i].embosssourceshift;
|
||||
out.Write("ldir = normalize(%s.xyz - pos.xyz);\n", LightPos(I_LIGHTS, texinfo.embosslightshift));
|
||||
out.Write("ldir = normalize(" LIGHT_POS".xyz - pos.xyz);\n", LIGHT_POS_PARAMS(I_LIGHTS, texinfo.embosslightshift));
|
||||
out.Write("o.tex%d.xyz = o.tex%d.xyz + float3(dot(ldir, _norm1), dot(ldir, _norm2), 0.0f);\n", i, texinfo.embosssourceshift);
|
||||
}
|
||||
else
|
||||
|
Loading…
x
Reference in New Issue
Block a user