[unity] Fixed shader compilation error on PS4 platform related to static const in function. Closes #1600.

This commit is contained in:
Harald Csaszar 2020-01-14 19:26:45 +01:00
parent 3cf66670cc
commit 9acf99e763

View File

@ -1,6 +1,6 @@
#ifndef SPRITE_VERTEX_LIGHTING_INCLUDED #ifndef SPRITE_VERTEX_LIGHTING_INCLUDED
#define SPRITE_VERTEX_LIGHTING_INCLUDED #define SPRITE_VERTEX_LIGHTING_INCLUDED
#include "ShaderShared.cginc" #include "ShaderShared.cginc"
#include "SpriteLighting.cginc" #include "SpriteLighting.cginc"
#include "SpriteSpecular.cginc" #include "SpriteSpecular.cginc"
@ -78,25 +78,25 @@
struct VertexOutput struct VertexOutput
{ {
float4 pos : SV_POSITION; float4 pos : SV_POSITION;
fixed4 color : COLOR; fixed4 color : COLOR;
float3 texcoord : TEXCOORD0; float3 texcoord : TEXCOORD0;
#if defined(PER_PIXEL_LIGHTING) #if defined(PER_PIXEL_LIGHTING)
half4 VertexLightInfo0 : TEXCOORD1; half4 VertexLightInfo0 : TEXCOORD1;
half4 VertexLightInfo1 : TEXCOORD2; half4 VertexLightInfo1 : TEXCOORD2;
half4 VertexLightInfo2 : TEXCOORD3; half4 VertexLightInfo2 : TEXCOORD3;
half4 VertexLightInfo3 : TEXCOORD4; half4 VertexLightInfo3 : TEXCOORD4;
half4 VertexLightInfo4 : TEXCOORD5; half4 VertexLightInfo4 : TEXCOORD5;
#if defined(_NORMALMAP) #if defined(_NORMALMAP)
half4 normalWorld : TEXCOORD6; half4 normalWorld : TEXCOORD6;
half4 tangentWorld : TEXCOORD7; half4 tangentWorld : TEXCOORD7;
half4 binormalWorld : TEXCOORD8; half4 binormalWorld : TEXCOORD8;
#else #else
half3 normalWorld : TEXCOORD6; half3 normalWorld : TEXCOORD6;
half3 VertexLightInfo5 : TEXCOORD7; half3 VertexLightInfo5 : TEXCOORD7;
#endif #endif
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
half4 LightAttenuations : ATTENUATIONS; half4 LightAttenuations : ATTENUATIONS;
@ -107,8 +107,8 @@ struct VertexOutput
#else //!PER_PIXEL_LIGHTING #else //!PER_PIXEL_LIGHTING
half3 FullLighting : TEXCOORD1; half3 FullLighting : TEXCOORD1;
#endif // !PER_PIXEL_LIGHTING #endif // !PER_PIXEL_LIGHTING
#if defined(_FOG) #if defined(_FOG)
@ -126,8 +126,8 @@ struct VertexLightInfo
{ {
half3 lightDirection; half3 lightDirection;
fixed3 lightColor; fixed3 lightColor;
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
float attenuation; float attenuation;
#endif // _DIFFUSE_RAMP #endif // _DIFFUSE_RAMP
}; };
@ -135,18 +135,18 @@ struct VertexLightInfo
inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos) inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos)
{ {
VertexLightInfo lightInfo; VertexLightInfo lightInfo;
//For directional lights unity_LightPosition.w is set to zero //For directional lights unity_LightPosition.w is set to zero
lightInfo.lightDirection = unity_LightPosition[index].xyz - viewPos.xyz * unity_LightPosition[index].w; lightInfo.lightDirection = unity_LightPosition[index].xyz - viewPos.xyz * unity_LightPosition[index].w;
float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection); float lengthSq = dot(lightInfo.lightDirection, lightInfo.lightDirection);
// don't produce NaNs if some vertex position overlaps with the light // don't produce NaNs if some vertex position overlaps with the light
lengthSq = max(lengthSq, 0.000001); lengthSq = max(lengthSq, 0.000001);
lightInfo.lightDirection *= rsqrt(lengthSq); lightInfo.lightDirection *= rsqrt(lengthSq);
float attenuation = 1.0 / (1.0 + lengthSq * unity_LightAtten[index].z); float attenuation = 1.0 / (1.0 + lengthSq * unity_LightAtten[index].z);
#if defined(SPOT_LIGHTS) #if defined(SPOT_LIGHTS)
//Spot light attenuation - for non-spot lights unity_LightAtten.x is set to -1 and y is set to 1 //Spot light attenuation - for non-spot lights unity_LightAtten.x is set to -1 and y is set to 1
{ {
@ -155,46 +155,45 @@ inline VertexLightInfo getVertexLightAttenuatedInfo(int index, float3 viewPos)
attenuation *= saturate(spotAtt); attenuation *= saturate(spotAtt);
} }
#endif // SPOT_LIGHTS #endif // SPOT_LIGHTS
//If using a diffuse ramp texture then need to pass through the lights attenuation, otherwise premultiply the light color with it //If using a diffuse ramp texture then need to pass through the lights attenuation, otherwise premultiply the light color with it
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
lightInfo.lightColor = unity_LightColor[index].rgb; lightInfo.lightColor = unity_LightColor[index].rgb;
lightInfo.attenuation = attenuation; lightInfo.attenuation = attenuation;
#else #else
lightInfo.lightColor = unity_LightColor[index].rgb * attenuation; lightInfo.lightColor = unity_LightColor[index].rgb * attenuation;
#endif // _DIFFUSE_RAMP #endif // _DIFFUSE_RAMP
return lightInfo; return lightInfo;
} }
//Magic constants used to tweak ambient to approximate pixel shader spherical harmonics
static const fixed3 worldUp = fixed3(0, 1, 0);
static const float skyGroundDotMul = 2.5;
static const float minEquatorMix = 0.5;
static const float equatorColorBlur = 0.33;
fixed3 calculateAmbientLight(half3 normalWorld) fixed3 calculateAmbientLight(half3 normalWorld)
{ {
#if defined(_SPHERICAL_HARMONICS) #if defined(_SPHERICAL_HARMONICS)
//Magic constants used to tweak ambient to approximate pixel shader spherical harmonics
static const fixed3 worldUp = fixed3(0,1,0);
static const float skyGroundDotMul = 2.5;
static const float minEquatorMix = 0.5;
static const float equatorColorBlur = 0.33;
float upDot = dot(normalWorld, worldUp); float upDot = dot(normalWorld, worldUp);
//Fade between a flat lerp from sky to ground and a 3 way lerp based on how bright the equator light is. //Fade between a flat lerp from sky to ground and a 3 way lerp based on how bright the equator light is.
//This simulates how directional lights get blurred using spherical harmonics //This simulates how directional lights get blurred using spherical harmonics
//Work out color from ground and sky, ignoring equator //Work out color from ground and sky, ignoring equator
float adjustedDot = upDot * skyGroundDotMul; float adjustedDot = upDot * skyGroundDotMul;
fixed3 skyGroundColor = lerp(unity_AmbientGround, unity_AmbientSky, saturate((adjustedDot + 1.0) * 0.5)); fixed3 skyGroundColor = lerp(unity_AmbientGround, unity_AmbientSky, saturate((adjustedDot + 1.0) * 0.5));
//Work out equator lights brightness //Work out equator lights brightness
float equatorBright = saturate(dot(unity_AmbientEquator.rgb, unity_AmbientEquator.rgb)); float equatorBright = saturate(dot(unity_AmbientEquator.rgb, unity_AmbientEquator.rgb));
//Blur equator color with sky and ground colors based on how bright it is. //Blur equator color with sky and ground colors based on how bright it is.
fixed3 equatorBlurredColor = lerp(unity_AmbientEquator, saturate(unity_AmbientEquator + unity_AmbientGround + unity_AmbientSky), equatorBright * equatorColorBlur); fixed3 equatorBlurredColor = lerp(unity_AmbientEquator, saturate(unity_AmbientEquator + unity_AmbientGround + unity_AmbientSky), equatorBright * equatorColorBlur);
//Work out 3 way lerp inc equator light //Work out 3 way lerp inc equator light
fixed3 equatorColor = lerp(equatorBlurredColor, unity_AmbientGround, -upDot) * step(upDot, 0) + lerp(equatorBlurredColor, unity_AmbientSky, upDot) * step(0, upDot); fixed3 equatorColor = lerp(equatorBlurredColor, unity_AmbientGround, -upDot) * step(upDot, 0) + lerp(equatorBlurredColor, unity_AmbientSky, upDot) * step(0, upDot);
//Mix the two colors together based on how bright the equator light is //Mix the two colors together based on how bright the equator light is
return lerp(skyGroundColor, equatorColor, saturate(equatorBright + minEquatorMix)); return lerp(skyGroundColor, equatorColor, saturate(equatorBright + minEquatorMix));
@ -202,8 +201,8 @@ fixed3 calculateAmbientLight(half3 normalWorld)
//Flat ambient is just the sky color //Flat ambient is just the sky color
return unity_AmbientSky.rgb; return unity_AmbientSky.rgb;
#endif // !_SPHERICAL_HARMONICS #endif // !_SPHERICAL_HARMONICS
} }
//////////////////////////////////////// ////////////////////////////////////////
@ -225,7 +224,7 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNorma
{ {
float angleDot = max(0, dot(viewNormal, lightViewDir)); float angleDot = max(0, dot(viewNormal, lightViewDir));
fixed3 lightDiffuse = attenuatedLightColor * angleDot; fixed3 lightDiffuse = attenuatedLightColor * angleDot;
return lightDiffuse; return lightDiffuse;
} }
@ -260,7 +259,7 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNorma
#define VERTEX_LIGHT_3_G VertexLightInfo5.y #define VERTEX_LIGHT_3_G VertexLightInfo5.y
#define VERTEX_LIGHT_3_B VertexLightInfo5.z #define VERTEX_LIGHT_3_B VertexLightInfo5.z
#endif #endif
#if defined(_DIFFUSE_RAMP) #if defined(_DIFFUSE_RAMP)
#define LIGHT_DIFFUSE_ATTEN_0 LightAttenuations.x #define LIGHT_DIFFUSE_ATTEN_0 LightAttenuations.x
@ -272,7 +271,7 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNorma
{ \ { \
output.LIGHT_DIFFUSE_ATTEN_##index = lightInfo.attenuation; \ output.LIGHT_DIFFUSE_ATTEN_##index = lightInfo.attenuation; \
} }
#define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \ #define ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
{ \ { \
diffuse += calculateLightDiffuse(lightColor, viewNormal, lightViewDir, input.LIGHT_DIFFUSE_ATTEN_##index); \ diffuse += calculateLightDiffuse(lightColor, viewNormal, lightViewDir, input.LIGHT_DIFFUSE_ATTEN_##index); \
@ -301,7 +300,7 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNorma
fixed3 lightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \ fixed3 lightColor = fixed3(input.VERTEX_LIGHT_##index##_R, input.VERTEX_LIGHT_##index##_G, input.VERTEX_LIGHT_##index##_B); \
ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \ ADD_VERTEX_LIGHT_DIFFUSE(index, diffuse, input, lightColor, viewNormal, lightViewDir) \
} }
#if defined(SPECULAR) #if defined(SPECULAR)
#define ADD_VERTEX_LIGHT_SPEC(index, input, viewNormal, specData, combinedLightData, indirectDiffuse, indirectSpecular) \ #define ADD_VERTEX_LIGHT_SPEC(index, input, viewNormal, specData, combinedLightData, indirectDiffuse, indirectSpecular) \
@ -314,7 +313,7 @@ inline fixed3 calculateLightDiffuse(fixed3 attenuatedLightColor, half3 viewNorma
} }
#endif #endif
#else //!PER_PIXEL_LIGHTING #else //!PER_PIXEL_LIGHTING
//////////////////////////////////////// ////////////////////////////////////////
@ -329,7 +328,7 @@ inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal)
} }
#endif // !PER_PIXEL_LIGHTING #endif // !PER_PIXEL_LIGHTING
//////////////////////////////////////// ////////////////////////////////////////
// Vertex program // Vertex program
// //
@ -337,10 +336,10 @@ inline fixed3 calculateLightDiffuse(int index, float3 viewPos, half3 viewNormal)
VertexOutput vert(VertexInput input) VertexOutput vert(VertexInput input)
{ {
VertexOutput output; VertexOutput output;
UNITY_SETUP_INSTANCE_ID(input); UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
output.pos = calculateLocalPos(input.vertex); output.pos = calculateLocalPos(input.vertex);
output.color = calculateVertexColor(input.color); output.color = calculateVertexColor(input.color);
output.texcoord = float3(calculateTextureCoord(input.texcoord), 0); output.texcoord = float3(calculateTextureCoord(input.texcoord), 0);
@ -348,15 +347,15 @@ VertexOutput vert(VertexInput input)
float3 viewPos = UnityObjectToViewPos(input.vertex); //float3 viewPos = mul(UNITY_MATRIX_MV, input.vertex); // float3 viewPos = UnityObjectToViewPos(input.vertex); //float3 viewPos = mul(UNITY_MATRIX_MV, input.vertex); //
#if defined(FIXED_NORMALS_BACKFACE_RENDERING) || defined(_RIM_LIGHTING) #if defined(FIXED_NORMALS_BACKFACE_RENDERING) || defined(_RIM_LIGHTING)
float4 powWorld = calculateWorldPos(input.vertex); float4 powWorld = calculateWorldPos(input.vertex);
#endif #endif
float backFaceSign = 1; float backFaceSign = 1;
#if defined(FIXED_NORMALS_BACKFACE_RENDERING) #if defined(FIXED_NORMALS_BACKFACE_RENDERING)
backFaceSign = calculateBackfacingSign(powWorld.xyz); backFaceSign = calculateBackfacingSign(powWorld.xyz);
#endif #endif
#if defined(PER_PIXEL_LIGHTING) #if defined(PER_PIXEL_LIGHTING)
#if defined(_RIM_LIGHTING) #if defined(_RIM_LIGHTING)
output.posWorld = powWorld; output.posWorld = powWorld;
#endif #endif
@ -365,35 +364,35 @@ VertexOutput vert(VertexInput input)
PACK_VERTEX_LIGHT(1, output, viewPos) PACK_VERTEX_LIGHT(1, output, viewPos)
PACK_VERTEX_LIGHT(2, output, viewPos) PACK_VERTEX_LIGHT(2, output, viewPos)
PACK_VERTEX_LIGHT(3, output, viewPos) PACK_VERTEX_LIGHT(3, output, viewPos)
output.normalWorld.xyz = calculateSpriteWorldNormal(input, backFaceSign); output.normalWorld.xyz = calculateSpriteWorldNormal(input, backFaceSign);
#if defined(_NORMALMAP) #if defined(_NORMALMAP)
output.tangentWorld.xyz = calculateWorldTangent(input.tangent); output.tangentWorld.xyz = calculateWorldTangent(input.tangent);
output.binormalWorld.xyz = calculateSpriteWorldBinormal(input, output.normalWorld, output.tangentWorld, backFaceSign); output.binormalWorld.xyz = calculateSpriteWorldBinormal(input, output.normalWorld, output.tangentWorld, backFaceSign);
#endif #endif
#else // !PER_PIXEL_LIGHTING #else // !PER_PIXEL_LIGHTING
//Just pack full lighting //Just pack full lighting
float3 viewNormal = calculateSpriteViewNormal(input, backFaceSign); float3 viewNormal = calculateSpriteViewNormal(input, backFaceSign);
//Get Ambient diffuse //Get Ambient diffuse
float3 normalWorld = calculateSpriteWorldNormal(input, backFaceSign); float3 normalWorld = calculateSpriteWorldNormal(input, backFaceSign);
fixed3 ambient = calculateAmbientLight(normalWorld); fixed3 ambient = calculateAmbientLight(normalWorld);
fixed3 diffuse = calculateLightDiffuse(0, viewPos, viewNormal); fixed3 diffuse = calculateLightDiffuse(0, viewPos, viewNormal);
diffuse += calculateLightDiffuse(1, viewPos, viewNormal); diffuse += calculateLightDiffuse(1, viewPos, viewNormal);
diffuse += calculateLightDiffuse(2, viewPos, viewNormal); diffuse += calculateLightDiffuse(2, viewPos, viewNormal);
diffuse += calculateLightDiffuse(3, viewPos, viewNormal); diffuse += calculateLightDiffuse(3, viewPos, viewNormal);
output.FullLighting = ambient + diffuse; output.FullLighting = ambient + diffuse;
#endif // !PER_PIXEL_LIGHTING #endif // !PER_PIXEL_LIGHTING
#if defined(_FOG) #if defined(_FOG)
UNITY_TRANSFER_FOG(output, output.pos); UNITY_TRANSFER_FOG(output, output.pos);
#endif // _FOG #endif // _FOG
return output; return output;
} }
@ -405,70 +404,70 @@ fixed4 frag(VertexOutput input) : SV_Target
{ {
fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); fixed4 texureColor = calculateTexturePixel(input.texcoord.xy);
ALPHA_CLIP(texureColor, input.color) ALPHA_CLIP(texureColor, input.color)
#if defined(PER_PIXEL_LIGHTING) #if defined(PER_PIXEL_LIGHTING)
#if defined(_NORMALMAP) #if defined(_NORMALMAP)
half3 normalWorld = calculateNormalFromBumpMap(input.texcoord.xy, input.tangentWorld.xyz, input.binormalWorld.xyz, input.normalWorld.xyz); half3 normalWorld = calculateNormalFromBumpMap(input.texcoord.xy, input.tangentWorld.xyz, input.binormalWorld.xyz, input.normalWorld.xyz);
#else #else
half3 normalWorld = input.normalWorld.xyz; half3 normalWorld = input.normalWorld.xyz;
#endif #endif
//Get Ambient diffuse //Get Ambient diffuse
fixed3 ambient = calculateAmbientLight(normalWorld); fixed3 ambient = calculateAmbientLight(normalWorld);
half3 normalView = normalize(mul((float3x3)UNITY_MATRIX_V, normalWorld)); half3 normalView = normalize(mul((float3x3)UNITY_MATRIX_V, normalWorld));
#if defined(SPECULAR) #if defined(SPECULAR)
SpecularCommonData specData = getSpecularData(input.texcoord.xy, texureColor, input.color); SpecularCommonData specData = getSpecularData(input.texcoord.xy, texureColor, input.color);
SpecularLightData combinedLightData = (SpecularLightData)0; SpecularLightData combinedLightData = (SpecularLightData)0;
ADD_VERTEX_LIGHT_SPEC(0, input, normalView, specData, combinedLightData, ambient, unity_IndirectSpecColor.rgb) ADD_VERTEX_LIGHT_SPEC(0, input, normalView, specData, combinedLightData, ambient, unity_IndirectSpecColor.rgb)
ADD_VERTEX_LIGHT_SPEC(1, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0)) ADD_VERTEX_LIGHT_SPEC(1, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0))
ADD_VERTEX_LIGHT_SPEC(2, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0)) ADD_VERTEX_LIGHT_SPEC(2, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0))
ADD_VERTEX_LIGHT_SPEC(3, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0)) ADD_VERTEX_LIGHT_SPEC(3, input, normalView, specData, combinedLightData, fixed3(0,0,0), fixed3(0,0,0))
fixed4 pixel = calculateLitPixel(fixed4(specData.diffColor, specData.alpha), combinedLightData.lighting); fixed4 pixel = calculateLitPixel(fixed4(specData.diffColor, specData.alpha), combinedLightData.lighting);
pixel.rgb += combinedLightData.specular * specData.alpha; pixel.rgb += combinedLightData.specular * specData.alpha;
APPLY_EMISSION_SPECULAR(pixel, input.texcoord) APPLY_EMISSION_SPECULAR(pixel, input.texcoord)
#else #else
//Find vertex light diffuse //Find vertex light diffuse
fixed3 diffuse = fixed3(0,0,0); fixed3 diffuse = fixed3(0,0,0);
//Add each vertex light to diffuse //Add each vertex light to diffuse
ADD_VERTEX_LIGHT(0, input, normalView, diffuse) ADD_VERTEX_LIGHT(0, input, normalView, diffuse)
ADD_VERTEX_LIGHT(1, input, normalView, diffuse) ADD_VERTEX_LIGHT(1, input, normalView, diffuse)
ADD_VERTEX_LIGHT(2, input, normalView, diffuse) ADD_VERTEX_LIGHT(2, input, normalView, diffuse)
ADD_VERTEX_LIGHT(3, input, normalView, diffuse) ADD_VERTEX_LIGHT(3, input, normalView, diffuse)
fixed3 lighting = ambient + diffuse; fixed3 lighting = ambient + diffuse;
APPLY_EMISSION(lighting, input.texcoord.xy) APPLY_EMISSION(lighting, input.texcoord.xy)
fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting); fixed4 pixel = calculateLitPixel(texureColor, input.color, lighting);
#endif #endif
#if defined(_RIM_LIGHTING) #if defined(_RIM_LIGHTING)
pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel); pixel.rgb = applyRimLighting(input.posWorld, normalWorld, pixel);
#endif #endif
#else // !PER_PIXEL_LIGHTING #else // !PER_PIXEL_LIGHTING
APPLY_EMISSION(input.FullLighting, input.texcoord.xy) APPLY_EMISSION(input.FullLighting, input.texcoord.xy)
fixed4 pixel = calculateLitPixel(texureColor, input.color, input.FullLighting); fixed4 pixel = calculateLitPixel(texureColor, input.color, input.FullLighting);
#endif // !PER_PIXEL_LIGHTING #endif // !PER_PIXEL_LIGHTING
COLORISE(pixel) COLORISE(pixel)
APPLY_FOG(pixel, input) APPLY_FOG(pixel, input)
return pixel; return pixel;
} }
#endif // SPRITE_VERTEX_LIGHTING_INCLUDED #endif // SPRITE_VERTEX_LIGHTING_INCLUDED