ShaderGen: Fix per pixel lighting.

This commit is contained in:
NeoBrainX 2013-03-31 23:29:33 +02:00
parent 248d56d930
commit f6d65a636e
7 changed files with 32 additions and 27 deletions

View File

@ -18,15 +18,12 @@
#ifndef _LIGHTINGSHADERGEN_H_
#define _LIGHTINGSHADERGEN_H_
#include <typeinfo>
#include "ShaderGenCommon.h"
#include "NativeVertexFormat.h"
#include "XFMemory.h"
// uid_data needs to have a struct named lighting_uid
template<class T,class UidDataT>
static void GenerateLightShader(T& object, UidDataT& uid_data, int index, int litchan_index, const char* lightsName, int coloralpha)
template<class T>
static void GenerateLightShader(T& object, LightingUidData& uid_data, int index, int litchan_index, const char* lightsName, int coloralpha)
{
const LitChannel& chan = (litchan_index > 1) ? xfregs.alpha[litchan_index-2] : xfregs.color[litchan_index];
const char* swizzle = "xyzw";
@ -93,8 +90,8 @@ static void GenerateLightShader(T& object, UidDataT& uid_data, int index, int li
// materials name is I_MATERIALS in vs and I_PMATERIALS in ps
// inColorName is color in vs and colors_ in ps
// dest is o.colors_ in vs and colors_ in ps
template<class T, class UidDataT>
static void GenerateLightingShader(T& object, UidDataT& uid_data, int components, const char* materialsName, const char* lightsName, const char* inColorName, const char* dest)
template<class T>
static void GenerateLightingShader(T& object, LightingUidData& uid_data, int components, const char* materialsName, const char* lightsName, const char* inColorName, const char* dest)
{
for (unsigned int j = 0; j < xfregs.numChan.numColorChans; j++)
{
@ -187,7 +184,7 @@ static void GenerateLightingShader(T& object, UidDataT& uid_data, int components
{
if (mask & (1<<i))
{
GenerateLightShader<T,UidDataT>(object, uid_data, i, j, lightsName, 3);
GenerateLightShader<T>(object, uid_data, i, j, lightsName, 3);
}
}
}
@ -197,9 +194,9 @@ static void GenerateLightingShader(T& object, UidDataT& uid_data, int components
for (int i = 0; i < 8; ++i)
{
if (!(mask&(1<<i)) && (color.GetFullLightMask() & (1<<i)))
GenerateLightShader<T,UidDataT>(object, uid_data, i, j, lightsName, 1);
GenerateLightShader<T>(object, uid_data, i, j, lightsName, 1);
if (!(mask&(1<<i)) && (alpha.GetFullLightMask() & (1<<i)))
GenerateLightShader<T,UidDataT>(object, uid_data, i, j+2, lightsName, 2);
GenerateLightShader<T>(object, uid_data, i, j+2, lightsName, 2);
}
}
else if (color.enablelighting || alpha.enablelighting)
@ -213,7 +210,7 @@ static void GenerateLightingShader(T& object, UidDataT& uid_data, int components
for (int i = 0; i < 8; ++i)
{
if (workingchannel.GetFullLightMask() & (1<<i))
GenerateLightShader<T,UidDataT>(object, uid_data, i, lit_index, lightsName, coloralpha);
GenerateLightShader<T>(object, uid_data, i, lit_index, lightsName, coloralpha);
}
}
object.Write("%s%d = mat * saturate(lacc);\n", dest, j);

View File

@ -341,12 +341,12 @@ void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u
out.Write("\t%sfloat4 " I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_INDTEXSCALE));
out.Write("\t%sfloat4 " I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_INDTEXMTX));
out.Write("\t%sfloat4 " I_FOG"[3] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_FOG));
// For pixel lighting - TODO: Should only be defined when per pixel lighting is enabled!
out.Write("struct Light { float4 col; float4 cosatt; float4 distatt; float4 pos; float4 dir; };\n");
out.Write("\t%sLight " I_PLIGHTS"[8] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PLIGHTS));
out.Write("\t%sfloat4 " I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PMATERIALS));
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
out.Write("};\n");
@ -366,7 +366,7 @@ void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u
out.Write("float4 colors_1 = colors_12;\n");
// compute window position if needed because binding semantic WPOS is not widely supported
// Let's set up attributes
// Let's set up attributes
if (xfregs.numTexGen.numTexGens < 7)
{
for (int i = 0; i < 8; ++i)
@ -479,10 +479,10 @@ void GeneratePixelShader(T& out, DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType, u
out.Write("\tfloat4 mat, lacc;\n"
"\tfloat3 ldir, h;\n"
"\tfloat dist, dist2, attn;\n");
/// TODO
out.SetConstantsUsed(C_PLIGHTS, C_PLIGHTS+39); // TODO: Can be optimized further
out.SetConstantsUsed(C_PMATERIALS, C_PMATERIALS+3);
/// p = GenerateLightingShader(p, components, I_PMATERIALS, I_PLIGHTS, "colors_", "colors_");
GenerateLightingShader<T>(out, uid_data.lighting, components, I_PMATERIALS, I_PLIGHTS, "colors_", "colors_");
}
if (numTexgen < 7)

View File

@ -187,6 +187,8 @@ struct pixel_shader_uid_data
u32 bHasIndStage : 16;
u32 xfregs_numTexGen_numTexGens : 4;
LightingUidData lighting;
};
typedef ShaderUid<pixel_shader_uid_data> PixelShaderUid;

View File

@ -153,4 +153,17 @@ static void DeclareUniform(T& object, API_TYPE api_type, bool using_ubos, const
object.Write(";\n");
}
struct LightingUidData
{
struct
{
u32 matsource : 1;
u32 enablelighting : 1;
u32 ambsource : 1;
u32 diffusefunc : 2;
u32 attnfunc : 2;
u32 light_mask : 8;
} lit_chans[4];
};
#endif // _SHADERGENCOMMON_H

View File

@ -270,7 +270,7 @@ static void GenerateVertexShader(T& out, u32 components, API_TYPE api_type)
}
// TODO: This probably isn't necessary if pixel lighting is enabled.
GenerateLightingShader<T,vertex_shader_uid_data>(out, uid_data, components, I_MATERIALS, I_LIGHTS, "color", "o.colors_");
GenerateLightingShader<T>(out, uid_data.lighting, components, I_MATERIALS, I_LIGHTS, "color", "o.colors_");
if (xfregs.numChan.numColorChans < 2)
{

View File

@ -79,7 +79,6 @@ const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 },
#pragma pack(4)
struct vertex_shader_uid_data
{
u32 components;
u32 numColorChans : 2;
@ -100,14 +99,8 @@ struct vertex_shader_uid_data
struct {
u32 enabled : 1;
} dualTexTrans;
struct {
u32 matsource : 1;
u32 enablelighting : 1;
u32 ambsource : 1;
u32 diffusefunc : 2;
u32 attnfunc : 2;
u32 light_mask : 8;
} lit_chans[4];
LightingUidData lighting;
};
#pragma pack()

View File

@ -135,7 +135,7 @@ struct VideoConfig
int iAdapter;
// Debugging
bool bEnableShaderDebugging; // TODO: Obsolete?
bool bEnableShaderDebugging;
// Static config per API
// TODO: Move this out of VideoConfig