Skip to content

Commit

Permalink
Merge branch 'master' into TemporaryHackishBranch
Browse files Browse the repository at this point in the history
# Conflicts:
#	GPU/Directx9/VertexShaderGeneratorDX9.cpp
  • Loading branch information
LunaMoo committed Sep 9, 2018
2 parents 5952984 + ae73b8f commit 70b3d9c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
60 changes: 33 additions & 27 deletions GPU/Directx9/VertexShaderGeneratorDX9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ static void WriteGuardBand(char *&p) {

void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage lang) {
char *p = buffer;
const u32 vertType = gstate.vertType;

bool isModeThrough = id.Bit(VS_BIT_IS_THROUGH);
bool lmode = id.Bit(VS_BIT_LMODE);
Expand All @@ -97,7 +96,6 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
bool hasNormal = id.Bit(VS_BIT_HAS_NORMAL) && useHWTransform;
bool hasTexcoord = id.Bit(VS_BIT_HAS_TEXCOORD) || !useHWTransform;
bool enableFog = id.Bit(VS_BIT_ENABLE_FOG);
bool throughmode = id.Bit(VS_BIT_IS_THROUGH);
bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE);
int ls0 = id.Bits(VS_BIT_LS0, 2);
int ls1 = id.Bits(VS_BIT_LS1, 2);
Expand All @@ -118,7 +116,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
for (int i = 0; i < 4; i++) {
if (i == shadeLight0 || i == shadeLight1)
doLight[i] = LIGHT_SHADE;
if (id.Bit(VS_BIT_LIGHTING_ENABLE) && id.Bit(VS_BIT_LIGHT0_ENABLE + i))
if (enableLighting && id.Bit(VS_BIT_LIGHT0_ENABLE + i))
doLight[i] = LIGHT_FULL;
}
}
Expand Down Expand Up @@ -189,9 +187,9 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
}
if (enableLighting) {
WRITE(p, "float4 u_ambient : register(c%i);\n", CONST_VS_AMBIENT);
if ((gstate.materialupdate & 2) == 0 || !hasColor)
if ((matUpdate & 2) == 0 || !hasColor)
WRITE(p, "float3 u_matdiffuse : register(c%i);\n", CONST_VS_MATDIFFUSE);
// if ((gstate.materialupdate & 4) == 0)
// if ((matUpdate & 4) == 0)
WRITE(p, "float4 u_matspecular : register(c%i);\n", CONST_VS_MATSPECULAR); // Specular coef is contained in alpha
WRITE(p, "float3 u_matemissive : register(c%i);\n", CONST_VS_MATEMISSIVE);
}
Expand All @@ -206,6 +204,8 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, "cbuffer bones : register(b2) {\n%s};\n", cb_vs_bonesStr);
}

bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);

// And the "varyings".
bool texCoordInVec3 = false;
if (useHWTransform) {
Expand All @@ -232,7 +232,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, "struct VS_IN {\n");
WRITE(p, " float4 position : POSITION;\n");
if (doTexture && hasTexcoord) {
if (doTextureTransform && !throughmode) {
if (doTextureTransform && !isModeThrough) {
texCoordInVec3 = true;
WRITE(p, " float3 texcoord : TEXCOORD0;\n");
}
Expand Down Expand Up @@ -397,7 +397,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, " Out.v_fogdepth = In.position.w;\n");
}
if (lang == HLSL_D3D11 || lang == HLSL_D3D11_LEVEL9) {
if (gstate.isModeThrough()) {
if (isModeThrough) {
WRITE(p, " float4 outPos = mul(u_proj_through, float4(In.position.xyz, 1.0));\n");
} else {
if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) {
Expand All @@ -407,7 +407,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
}
}
} else {
if (gstate.isModeThrough()) {
if (isModeThrough) {
WRITE(p, " float4 outPos = mul(float4(In.position.xyz, 1.0), u_proj_through);\n");
} else {
if (gstate_c.Supports(GPU_ROUND_DEPTH_TO_16BIT)) {
Expand Down Expand Up @@ -628,9 +628,9 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage

// TODO: Declare variables for dots for shade mapping if needed.

const char *ambientStr = (gstate.materialupdate & 1) && hasColor ? "In.color0" : "u_matambientalpha";
const char *diffuseStr = (gstate.materialupdate & 2) && hasColor ? "In.color0.rgb" : "u_matdiffuse";
const char *specularStr = (gstate.materialupdate & 4) && hasColor ? "In.color0.rgb" : "u_matspecular.rgb";
const char *ambientStr = (matUpdate & 1) && hasColor ? "In.color0" : "u_matambientalpha";
const char *diffuseStr = (matUpdate & 2) && hasColor ? "In.color0.rgb" : "u_matdiffuse";
const char *specularStr = (matUpdate & 4) && hasColor ? "In.color0.rgb" : "u_matspecular.rgb";
if (doBezier || doSpline) {
ambientStr = (matUpdate & 1) && hasColor ? "col" : "u_matambientalpha";
diffuseStr = (matUpdate & 2) && hasColor ? "col.rgb" : "u_matdiffuse";
Expand All @@ -640,7 +640,7 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
bool diffuseIsZero = true;
bool specularIsZero = true;
bool distanceNeeded = false;

bool anySpots = false;
if (enableLighting) {
WRITE(p, " float4 lightSum0 = u_ambient * %s + float4(u_matemissive, 0.0);\n", ambientStr);

Expand All @@ -654,6 +654,8 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
specularIsZero = false;
if (type != GE_LIGHTTYPE_DIRECTIONAL)
distanceNeeded = true;
if (type == GE_LIGHTTYPE_SPOT || type == GE_LIGHTTYPE_UNKNOWN)
anySpots = true;
}

if (!specularIsZero) {
Expand All @@ -667,6 +669,10 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, " float distance;\n");
WRITE(p, " float lightScale;\n");
}
WRITE(p, " float ldot;\n");
if (anySpots) {
WRITE(p, " float angle;\n");
}
}

// Calculate lights if needed. If shade mapping is enabled, lights may need to be
Expand All @@ -690,12 +696,15 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
bool doSpecular = comp != GE_LIGHTCOMP_ONLYDIFFUSE;
bool poweredDiffuse = comp == GE_LIGHTCOMP_BOTHWITHPOWDIFFUSE;

WRITE(p, " ldot = max(dot(toLight, worldnormal), 0.0);\n");
if (poweredDiffuse) {
WRITE(p, " float dot%i = pow(dot(toLight, worldnormal), u_matspecular.a);\n", i);
// TODO: Somehow the NaN check from GLES seems unnecessary here?
// If it returned 0, it'd be wrong, so that's strange.
} else {
WRITE(p, " float dot%i = dot(toLight, worldnormal);\n", i);
// pow(0.0, 0.0) may be undefined, but the PSP seems to treat it as 1.0.
// Seen in Tales of the World: Radiant Mythology (#2424.)
WRITE(p, " if (ldot == 0.0 && u_matspecular.a == 0.0) {\n");
WRITE(p, " ldot = 1.0;\n");
WRITE(p, " } else {\n");
WRITE(p, " ldot = pow(ldot, u_matspecular.a);\n");
WRITE(p, " }\n");
}

const char *timesLightScale = " * lightScale";
Expand All @@ -710,9 +719,9 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
break;
case GE_LIGHTTYPE_SPOT:
case GE_LIGHTTYPE_UNKNOWN:
WRITE(p, " float angle%i = dot(normalize(u_lightdir%i), toLight);\n", i, i);
WRITE(p, " if (angle%i >= u_lightangle_spotCoef%i.x) {\n", i, i);
WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, float3(1.0, distance, distance*distance)), 0.0, 1.0) * pow(angle%i, u_lightangle_spotCoef%i.y);\n", i, i, i);
WRITE(p, " angle = dot(normalize(u_lightdir%i), toLight);\n", i);
WRITE(p, " if (angle >= u_lightangle_spotCoef%i.x) {\n", i);
WRITE(p, " lightScale = clamp(1.0 / dot(u_lightatt%i, float3(1.0, distance, distance*distance)), 0.0, 1.0) * pow(angle, u_lightangle_spotCoef%i.y);\n", i, i);
WRITE(p, " } else {\n");
WRITE(p, " lightScale = 0.0;\n");
WRITE(p, " }\n");
Expand All @@ -722,11 +731,11 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
break;
}

WRITE(p, " diffuse = (u_lightdiffuse%i * %s) * max(dot%i, 0.0);\n", i, diffuseStr, i);
WRITE(p, " diffuse = (u_lightdiffuse%i * %s) * ldot;\n", i, diffuseStr);
if (doSpecular) {
WRITE(p, " dot%i = dot(normalize(toLight + float3(0.0, 0.0, 1.0)), worldnormal);\n", i);
WRITE(p, " if (dot%i > 0.0)\n", i);
WRITE(p, " lightSum1 += u_lightspecular%i * %s * (pow(dot%i, u_matspecular.a) %s);\n", i, specularStr, i, timesLightScale);
WRITE(p, " ldot = dot(normalize(toLight + float3(0.0, 0.0, 1.0)), worldnormal);\n");
WRITE(p, " if (ldot > 0.0)\n");
WRITE(p, " lightSum1 += u_lightspecular%i * %s * (pow(ldot, u_matspecular.a) %s);\n", i, specularStr, timesLightScale);
}
WRITE(p, " lightSum0.rgb += (u_lightambient%i * %s.rgb + diffuse)%s;\n", i, ambientStr, timesLightScale);
}
Expand Down Expand Up @@ -762,9 +771,6 @@ void GenerateVertexShaderHLSL(const VShaderID &id, char *buffer, ShaderLanguage
WRITE(p, " Out.v_color1 = float3(0, 0, 0);\n");
}

bool scaleUV = !throughmode && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);


// Step 3: UV generation
if (doTexture) {
switch (uvGenMode) {
Expand Down
7 changes: 3 additions & 4 deletions GPU/GLES/VertexShaderGeneratorGLES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
bool hasNormal = id.Bit(VS_BIT_HAS_NORMAL) && useHWTransform;
bool hasTexcoord = id.Bit(VS_BIT_HAS_TEXCOORD) || !useHWTransform;
bool enableFog = id.Bit(VS_BIT_ENABLE_FOG);
bool throughmode = id.Bit(VS_BIT_IS_THROUGH);
bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE);
int ls0 = id.Bits(VS_BIT_LS0, 2);
int ls1 = id.Bits(VS_BIT_LS1, 2);
Expand All @@ -211,7 +210,7 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
for (int i = 0; i < 4; i++) {
if (i == shadeLight0 || i == shadeLight1)
doLight[i] = LIGHT_SHADE;
if (id.Bit(VS_BIT_LIGHTING_ENABLE) && id.Bit(VS_BIT_LIGHT0_ENABLE + i))
if (enableLighting && id.Bit(VS_BIT_LIGHT0_ENABLE + i))
doLight[i] = LIGHT_FULL;
}
}
Expand Down Expand Up @@ -239,7 +238,7 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,

bool texcoordVec3In = false;
if (doTexture && hasTexcoord) {
if (!useHWTransform && doTextureProjection && !throughmode) {
if (!useHWTransform && doTextureProjection && !isModeThrough) {
WRITE(p, "%s vec3 texcoord;\n", attribute);
texcoordVec3In = true;
} else {
Expand Down Expand Up @@ -267,7 +266,7 @@ void GenerateVertexShader(const VShaderID &id, char *buffer, uint32_t *attrMask,
WRITE(p, "uniform vec4 u_guardband;\n");
*uniformMask |= DIRTY_GUARDBAND;

bool scaleUV = !throughmode && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);
bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);

if (useHWTransform) {
// When transforming by hardware, we need a great deal more uniforms...
Expand Down
5 changes: 2 additions & 3 deletions GPU/Vulkan/VertexShaderGeneratorVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) {
bool hasNormal = id.Bit(VS_BIT_HAS_NORMAL) && useHWTransform;
bool hasTexcoord = id.Bit(VS_BIT_HAS_TEXCOORD) || !useHWTransform;
bool enableFog = id.Bit(VS_BIT_ENABLE_FOG);
bool throughmode = id.Bit(VS_BIT_IS_THROUGH);
bool flipNormal = id.Bit(VS_BIT_NORM_REVERSE);
int ls0 = id.Bits(VS_BIT_LS0, 2);
int ls1 = id.Bits(VS_BIT_LS1, 2);
Expand Down Expand Up @@ -179,7 +178,7 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) {

bool texcoordInVec3 = false;
if (doTexture && hasTexcoord) {
if (!useHWTransform && doTextureTransform && !throughmode) {
if (!useHWTransform && doTextureTransform && !isModeThrough) {
WRITE(p, "layout (location = %d) in vec3 texcoord;\n", (int)PspAttributeLocation::TEXCOORD);
texcoordInVec3 = true;
}
Expand Down Expand Up @@ -628,7 +627,7 @@ bool GenerateVulkanGLSLVertexShader(const VShaderID &id, char *buffer) {
}
}

bool scaleUV = !throughmode && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);
bool scaleUV = !isModeThrough && (uvGenMode == GE_TEXMAP_TEXTURE_COORDS || uvGenMode == GE_TEXMAP_UNKNOWN);

// Step 3: UV generation
if (doTexture) {
Expand Down

0 comments on commit 70b3d9c

Please sign in to comment.