Fixed BlinnPhong shading
This commit is contained in:
Vendored
+1
@@ -4,6 +4,7 @@
|
||||
"files.associations": {
|
||||
"*.h": "cpp",
|
||||
"*.ush": "hlsl",
|
||||
"*.slang": "hlsl",
|
||||
"*.pl": "prolog",
|
||||
"xstring": "cpp",
|
||||
"list": "cpp",
|
||||
|
||||
@@ -14,7 +14,6 @@ RWTexture2D<uint2> lightGrid;
|
||||
|
||||
struct VertexStageOutput
|
||||
{
|
||||
ShaderAttributeInterpolation shaderAttributeInterpolation : Interpolation;
|
||||
VertexValueCache cache : ShaderCache;
|
||||
float4 position : SV_Position;
|
||||
};
|
||||
@@ -27,36 +26,30 @@ VertexStageOutput vertexMain(
|
||||
VertexValueCache cache = input.getVertexCache();
|
||||
float3 worldPosition = input.getWorldPosition();
|
||||
//worldPosition += gMaterial.getWorldOffset();
|
||||
float4 clipSpacePosition;
|
||||
|
||||
float3x3 tangentToLocal = cache.getTangentToLocal();
|
||||
float4 viewSpacePosition = mul(gViewParams.viewMatrix, float4(worldPosition, 1));
|
||||
MaterialVertexParameter vertexParams = input.getMaterialVertexParameters(cache, worldPosition, viewSpacePosition.xyz, tangentToLocal);
|
||||
MaterialVertexParameter vertexParams = input.getMaterialVertexParameters(cache, worldPosition, viewSpacePosition.xyz);
|
||||
|
||||
clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition);
|
||||
float4 clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition);
|
||||
output.position = clipSpacePosition;
|
||||
output.shaderAttributeInterpolation = input.getInterpolants(cache, vertexParams);
|
||||
output.cache = cache;
|
||||
return output;
|
||||
}
|
||||
|
||||
[shader("fragment")]
|
||||
float4 fragmentMain(
|
||||
ShaderAttributeInterpolation input : Interpolation,
|
||||
VertexValueCache cache : ShaderCache,
|
||||
float4 position : SV_Position
|
||||
) : SV_Target
|
||||
{
|
||||
MaterialFragmentParameter materialParams = input.getMaterialParameter(position);
|
||||
MaterialFragmentParameter materialParams = cache.getFragmentParameters();
|
||||
let brdf = gMaterial.prepare(materialParams);
|
||||
|
||||
float3 viewDir = normalize(materialParams.viewDir);
|
||||
|
||||
|
||||
float3 result = float3(0, 0, 0);
|
||||
|
||||
for (int i = 0; i < numDirectionalLights; i++)
|
||||
{
|
||||
result += directionalLights[i].illuminate(materialParams, brdf, viewDir);
|
||||
result += directionalLights[i].illuminate(materialParams, brdf);
|
||||
}
|
||||
|
||||
uint2 tileIndex = uint2(floor(position.xy / BLOCK_SIZE));
|
||||
@@ -68,8 +61,7 @@ float4 fragmentMain(
|
||||
{
|
||||
//uint lightIndex = lightIndexList[startOffset + j];
|
||||
PointLight pointLight = pointLights[j];
|
||||
if(pointLight.colorRange.w < length(pointLight.getViewPos() - input.viewPosition)) continue;
|
||||
result += pointLight.illuminate(materialParams, brdf, viewDir);
|
||||
result += pointLight.illuminate(materialParams, brdf);
|
||||
}
|
||||
return float4(result, 1.0f);
|
||||
}
|
||||
|
||||
+8
-108
@@ -2,25 +2,25 @@ import Common;
|
||||
|
||||
interface IBRDF
|
||||
{
|
||||
float3 evaluate(float3 view, float3 light, float3 normal, float3 tangent, float3 biTangent, float3 lightColor);
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor);
|
||||
};
|
||||
|
||||
struct BlinnPhong : IBRDF
|
||||
{
|
||||
float3 baseColor;
|
||||
float metallic = 0;
|
||||
float3 normal = float3(0, 1, 0);
|
||||
float3 normal = float3(0, 0, 1);
|
||||
float specular = 0.5;
|
||||
float roughness = 0.5;
|
||||
float sheen = 1.f;
|
||||
|
||||
float3 evaluate(float3 view, float3 light, float3 surfaceNormal, float3 tangent, float3 biTangent, float3 lightColor)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
float nDotL = saturate(dot(normal, light));
|
||||
float3 h = normalize(light + view);
|
||||
float nDotL = saturate(dot(normal, lightDir_TS));
|
||||
float3 h = normalize(lightDir_TS + viewDir_TS);
|
||||
float nDotH = saturate(dot(normal, h));
|
||||
|
||||
return baseColor;
|
||||
return baseColor * (nDotL + nDotH) * lightColor;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -39,108 +39,8 @@ struct DisneyBRDF : IBRDF
|
||||
float clearCoat = 0;
|
||||
float clearCoatGloss = 1;
|
||||
|
||||
float sqr(float x)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
float SchlickFresnel(float u)
|
||||
{
|
||||
float m = clamp(1 - u, 0, 1);
|
||||
float m2 = m * m;
|
||||
return m2 * m2 * m; // pow(m,5)
|
||||
}
|
||||
|
||||
float GTR1(float NdotH, float a)
|
||||
{
|
||||
if (a >= 1)
|
||||
return 1 / PI;
|
||||
float a2 = a * a;
|
||||
float t = 1 + (a2 - 1) * NdotH * NdotH;
|
||||
return (a2 - 1) / (PI * log(a2) * t);
|
||||
}
|
||||
|
||||
float GTR2(float NdotH, float a)
|
||||
{
|
||||
float a2 = a * a;
|
||||
float t = 1 + (a2 - 1) * NdotH * NdotH;
|
||||
return a2 / (PI * t * t);
|
||||
}
|
||||
|
||||
float GTR2_aniso(float NdotH, float HdotX, float HdotY, float ax, float ay)
|
||||
{
|
||||
return 1 / (PI * ax * ay * sqr(sqr(HdotX / ax) + sqr(HdotY / ay) + NdotH * NdotH));
|
||||
}
|
||||
|
||||
float smithG_GGX(float NdotV, float alphaG)
|
||||
{
|
||||
float a = alphaG * alphaG;
|
||||
float b = NdotV * NdotV;
|
||||
return 1 / (NdotV + sqrt(a + b - a * b));
|
||||
}
|
||||
|
||||
float smithG_GGX_aniso(float NdotV, float VdotX, float VdotY, float ax, float ay)
|
||||
{
|
||||
return 1 / (NdotV + sqrt(sqr(VdotX * ax) + sqr(VdotY * ay) + sqr(NdotV)));
|
||||
}
|
||||
|
||||
float3 mon2lin(float3 x)
|
||||
{
|
||||
return float3(pow(x[0], 2.2), pow(x[1], 2.2), pow(x[2], 2.2));
|
||||
}
|
||||
|
||||
|
||||
float3 evaluate(float3 V, float3 L, float3 surfaceNormal, float3 X, float3 Y, float3 lightColor)
|
||||
{
|
||||
float3 N = normal;
|
||||
float NdotL = dot(N, L);
|
||||
float NdotV = dot(N, V);
|
||||
if (NdotL < 0 || NdotV < 0)
|
||||
return float3(0);
|
||||
float3 H = normalize(L + V);
|
||||
|
||||
float NdotH = dot(N, H);
|
||||
float LdotH = dot(L, H);
|
||||
float3 Cdlin = mon2lin(baseColor);
|
||||
float Cdlum = .3 * Cdlin[0] + .6 * Cdlin[1] + .1 * Cdlin[2]; // luminance approx.
|
||||
float3 Ctint = Cdlum > 0 ? Cdlin / Cdlum : float3(1); // normalize lum. to isolate hue+sat
|
||||
float3 Cspec0 = lerp(specular * .08 * lerp(float3(1), Ctint, specularTint), Cdlin, metallic);
|
||||
float3 Csheen = lerp(float3(1), Ctint, sheenTint);
|
||||
|
||||
// Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
|
||||
// and mix in diffuse retro-reflection based on roughness
|
||||
float FL = SchlickFresnel(NdotL), FV = SchlickFresnel(NdotV);
|
||||
float Fd90 = 0.5 + 2 * LdotH * LdotH * roughness;
|
||||
float Fd = lerp(1.0, Fd90, FL) * lerp(1.0, Fd90, FV);
|
||||
|
||||
// Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
|
||||
// 1.25 scale is used to (roughly) preserve albedo
|
||||
// Fss90 used to "flatten" retroreflection based on roughness
|
||||
float Fss90 = LdotH * LdotH * roughness;
|
||||
float Fss = lerp(1.0, Fss90, FL) * lerp(1.0, Fss90, FV);
|
||||
float ss = 1.25 * (Fss * (1 / (NdotL + NdotV) - .5) + .5);
|
||||
|
||||
// specular
|
||||
float aspect = sqrt(1 - anisotropic * .9);
|
||||
float ax = max(.001, sqr(roughness) / aspect);
|
||||
float ay = max(.001, sqr(roughness) * aspect);
|
||||
float Ds = GTR2_aniso(NdotH, dot(H, X), dot(H, Y), ax, ay);
|
||||
float FH = SchlickFresnel(LdotH);
|
||||
float3 Fs = lerp(Cspec0, float3(1), FH);
|
||||
float Gs;
|
||||
Gs = smithG_GGX_aniso(NdotL, dot(L, X), dot(L, Y), ax, ay);
|
||||
Gs *= smithG_GGX_aniso(NdotV, dot(V, X), dot(V, Y), ax, ay);
|
||||
|
||||
// sheen
|
||||
float3 Fsheen = FH * sheen * Csheen;
|
||||
|
||||
// clearcoat (ior = 1.5 -> F0 = 0.04)
|
||||
float Dr = GTR1(NdotH, lerp(.1, .001, clearCoatGloss));
|
||||
float Fr = lerp(.04, 1.0, FH);
|
||||
float Gr = smithG_GGX(NdotL, .25) * smithG_GGX(NdotV, .25);
|
||||
|
||||
return ((1 / PI) * lerp(Fd, ss, subsurface) * Cdlin + Fsheen)
|
||||
* (1 - metallic)
|
||||
+ Gs * Fs * Ds + .25 * clearCoat * Gr * Fr * Dr;
|
||||
return baseColor;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,33 +4,33 @@ import Common;
|
||||
|
||||
interface ILightEnv
|
||||
{
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo);
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf);
|
||||
};
|
||||
|
||||
struct DirectionalLight : ILightEnv
|
||||
{
|
||||
float4 color;
|
||||
float4 direction;
|
||||
float4 intensity;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 wo)
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf)
|
||||
{
|
||||
return intensity.xyz * brdf.evaluate(wo, normalize(direction.xyz), input.worldNormal, input.worldTangent, input.worldBiTangent, color.xyz);
|
||||
float3 lightDir_TS = input.transformWorldToTangent(normalize(direction.xyz));
|
||||
return brdf.evaluate(input.viewDir_TS, -lightDir_TS, color.xyz);
|
||||
}
|
||||
};
|
||||
|
||||
struct PointLight : ILightEnv
|
||||
{
|
||||
float4 positionWS;
|
||||
float4 position_WS;
|
||||
float4 colorRange;
|
||||
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf, float3 viewDir)
|
||||
float3 illuminate<B:IBRDF>(MaterialFragmentParameter input, B brdf)
|
||||
{
|
||||
float3 lightVec = positionWS.xyz - input.worldPosition;
|
||||
float d = length(lightVec);
|
||||
float3 direction = normalize(lightVec);
|
||||
float3 position_TS = input.transformWorldToTangent(position_WS.xyz);
|
||||
float3 lightDir_TS = position_TS - input.position_TS;
|
||||
float d = length(lightDir_TS);
|
||||
float illuminance = max(1 - d / colorRange.w, 0);
|
||||
return illuminance * brdf.evaluate(viewDir, direction, input.worldNormal, input.worldTangent, input.worldBiTangent, colorRange.xyz);
|
||||
return illuminance * brdf.evaluate(input.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
||||
}
|
||||
|
||||
bool insidePlane(Plane plane)
|
||||
@@ -57,7 +57,7 @@ struct PointLight : ILightEnv
|
||||
}
|
||||
float3 getViewPos()
|
||||
{
|
||||
return mul(gViewParams.viewMatrix, positionWS).xyz;
|
||||
return mul(gViewParams.viewMatrix, position_WS).xyz;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ struct MaterialVertexParameter
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3 viewPosition;
|
||||
float3x3 tangentToWorld;
|
||||
float3x3 worldToTangent;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
@@ -19,29 +19,19 @@ struct MaterialVertexParameter
|
||||
|
||||
struct MaterialFragmentParameter
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3 viewDir;
|
||||
float3 worldNormal;
|
||||
float3 worldTangent;
|
||||
float3 worldBiTangent;
|
||||
float4 clipPosition;
|
||||
float3 position_TS;
|
||||
float3 viewDir_TS;
|
||||
float4 vertexColor;
|
||||
float3x3 tangentToWorld;
|
||||
float3x3 worldToTangent;
|
||||
#ifdef USE_INSTANCING
|
||||
float3 perInstanceParams;
|
||||
#endif
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
float3 transformNormalToWorld(float3 tangentSpaceNormal)
|
||||
float3 transformWorldToTangent(float3 vec)
|
||||
{
|
||||
float3 result = mul(tangentToWorld, tangentSpaceNormal);
|
||||
return result;
|
||||
}
|
||||
float3 transformNormalTexture(float3 rawNormal)
|
||||
{
|
||||
rawNormal = 2.0 * rawNormal - float3(1.0, 1.0, 1.0);
|
||||
return transformNormalToWorld(rawNormal);
|
||||
return mul(worldToTangent, vec);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
struct PrimitiveSceneData
|
||||
{
|
||||
float4x4 localToWorld;
|
||||
float4x4 worldToLocal;
|
||||
float4x4 modelToWorld;
|
||||
float4x4 worldToModel;
|
||||
float4 actorLocation;
|
||||
};
|
||||
|
||||
|
||||
@@ -10,20 +10,52 @@ struct VertexValueCache
|
||||
//every attribute, including this matrix, but matrix layout
|
||||
//qualifiers are illegal for non-uniform and buffer fields,
|
||||
//leading to a compiler error
|
||||
float3 tangentToLocal[3];
|
||||
float3 tangentToWorld[3];
|
||||
float3 position_MS;
|
||||
float3 tangent;
|
||||
float3 biTangent;
|
||||
float3 normal;
|
||||
float4 color;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float2 texCoords[NUM_MATERIAL_TEXCOORDS];
|
||||
#endif
|
||||
|
||||
float3x3 getTangentToLocal()
|
||||
float4x4 getModelToTangent()
|
||||
{
|
||||
return float3x3(tangentToLocal[0], tangentToLocal[1], tangentToLocal[2]);
|
||||
return transpose(float4x4(
|
||||
float4(normalize(tangent), 0.0f),
|
||||
float4(normalize(biTangent), 0.0f),
|
||||
float4(normalize(normal), 0.0f),
|
||||
float4(0, 0, 0, 1)
|
||||
));
|
||||
}
|
||||
|
||||
float3x3 getTangentToWorld()
|
||||
float4x4 getWorldToTangent()
|
||||
{
|
||||
return float3x3(tangentToWorld[0], tangentToWorld[1], tangentToWorld[2]);
|
||||
return transpose(float4x4(
|
||||
normalize(mul(getSceneData().worldToModel, float4(tangent, 0))),
|
||||
normalize(mul(getSceneData().worldToModel, float4(biTangent, 0))),
|
||||
normalize(mul(getSceneData().worldToModel, float4(normal, 0))),
|
||||
float4(0, 0, 0, 1)
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
MaterialFragmentParameter getFragmentParameters()
|
||||
{
|
||||
MaterialFragmentParameter result = (MaterialFragmentParameter)0;
|
||||
float4x4 worldToTangent = getWorldToTangent();
|
||||
float4 cameraPos_TS = mul(worldToTangent, gViewParams.cameraPos_WS);
|
||||
float4 position_TS = mul(getModelToTangent(), float4(position_MS, 1.0f));
|
||||
result.worldToTangent = float3x3(worldToTangent);
|
||||
result.position_TS = position_TS.xyz;
|
||||
result.viewDir_TS = normalize((cameraPos_TS - position_TS).xyz);
|
||||
result.vertexColor = color;
|
||||
for(uint i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
|
||||
{
|
||||
result.texCoords[i] = texCoords[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
#if USE_INSTANCING
|
||||
float4 instanceOrigin;
|
||||
float3 instanceTransform1;
|
||||
@@ -36,60 +68,15 @@ struct VertexValueCache
|
||||
float4(instanceTransform1.xyz, 0.0f),
|
||||
float4(instanceTransform2.xyz, 0.0f),
|
||||
float4(instanceTransform3.xyz, 0.0f),
|
||||
float4(instanceOrigin.xyz, 1.0f));
|
||||
)
|
||||
float4(instanceOrigin.xyz, 1.0f)
|
||||
);
|
||||
}
|
||||
#endif // USE_INSTANCING
|
||||
};
|
||||
|
||||
struct ShaderAttributeInterpolation
|
||||
{
|
||||
float3 worldPosition;
|
||||
float3 viewPosition;
|
||||
float3 normal;
|
||||
float3 tangent;
|
||||
float3 biTangent;
|
||||
float4 color;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
float4 packedTexCoords[(NUM_MATERIAL_TEXCOORDS+1) / 2];
|
||||
#endif // NUM_MATERIAL_TEXCOORDS
|
||||
#if USE_INSTANCING
|
||||
float4 perInstanceParams;
|
||||
#endif
|
||||
|
||||
MaterialFragmentParameter getMaterialParameter(float4 clipSpacePosition)
|
||||
{
|
||||
MaterialFragmentParameter result;
|
||||
#if NUM_MATERIAL_TEXCOORDS
|
||||
for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
|
||||
{
|
||||
if(i % 2 == 1)
|
||||
{
|
||||
result.texCoords[i] = packedTexCoords[i / 2].zw;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.texCoords[i] = packedTexCoords[i / 2].xy;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
result.tangentToWorld[0] = tangent;
|
||||
result.tangentToWorld[1] = biTangent;
|
||||
result.tangentToWorld[2] = normal;
|
||||
result.clipPosition = clipSpacePosition;
|
||||
result.worldPosition = worldPosition;
|
||||
result.viewDir = gViewParams.cameraPos_WS.xyz - worldPosition;
|
||||
result.worldNormal = normal;
|
||||
result.worldTangent = tangent;
|
||||
result.worldBiTangent = biTangent;
|
||||
result.vertexColor = color;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct VertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
float3 position;
|
||||
float3 normal;
|
||||
float3 tangent;
|
||||
float3 biTangent;
|
||||
@@ -118,28 +105,24 @@ struct VertexShaderInput
|
||||
float3 instanceTransform2;
|
||||
float3 instanceTransform3;
|
||||
#endif // USE_INSTANCING
|
||||
|
||||
float3x3 calcTangentToLocal()
|
||||
{
|
||||
float3x3 result;
|
||||
result[0] = tangent;
|
||||
result[1] = biTangent;
|
||||
result[2] = normal;
|
||||
|
||||
return result;
|
||||
float3 getModelPosition()
|
||||
{
|
||||
return position;
|
||||
}
|
||||
|
||||
|
||||
float3 getWorldPosition()
|
||||
{
|
||||
float4x4 localToWorld = getSceneData().localToWorld;
|
||||
float3 rotatedPosition = localToWorld[0].xyz * position.xxx + localToWorld[1].xyz * position.yyy + localToWorld[2].xyz * position.zzz;
|
||||
return mul(getSceneData().localToWorld, position).xyz;
|
||||
//rotatedPosition + float3(localToWorld[0].w, localToWorld[1].w, localToWorld[2].w);
|
||||
return mul(getSceneData().modelToWorld, float4(position, 1.0f)).xyz;
|
||||
}
|
||||
|
||||
VertexValueCache getVertexCache()
|
||||
{
|
||||
VertexValueCache cache;
|
||||
cache.position_MS = position;
|
||||
cache.tangent = tangent;
|
||||
cache.biTangent = biTangent;
|
||||
cache.normal = normal;
|
||||
cache.color = color;
|
||||
#if USE_INSTANCING
|
||||
cache.instanceTransform1 = instanceTransform1;
|
||||
@@ -148,25 +131,18 @@ struct VertexShaderInput
|
||||
cache.instanceOrigin = instanceOrigin;
|
||||
#endif
|
||||
|
||||
float3x3 tangentToLocal = calcTangentToLocal();
|
||||
cache.tangentToLocal[0] = tangentToLocal[0];
|
||||
cache.tangentToLocal[1] = tangentToLocal[1];
|
||||
cache.tangentToLocal[2] = tangentToLocal[2];
|
||||
|
||||
cache.tangentToWorld[0] = mul(getSceneData().localToWorld, float4(tangentToLocal[0], 0.0f)).xyz;
|
||||
cache.tangentToWorld[1] = mul(getSceneData().localToWorld, float4(tangentToLocal[1], 0.0f)).xyz;
|
||||
cache.tangentToWorld[2] = mul(getSceneData().localToWorld, float4(tangentToLocal[2], 0.0f)).xyz;
|
||||
cache.texCoords[0] = packedTexCoords2;
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
MaterialVertexParameter getMaterialVertexParameters(VertexValueCache cache, float3 worldPosition, float3 viewPosition, float3x3 tangentToLocal)
|
||||
MaterialVertexParameter getMaterialVertexParameters(VertexValueCache cache, float3 worldPosition, float3 viewPosition)
|
||||
{
|
||||
MaterialVertexParameter result;
|
||||
result.worldPosition = worldPosition;
|
||||
result.viewPosition = viewPosition;
|
||||
result.vertexColor = cache.color;
|
||||
result.tangentToWorld = cache.getTangentToWorld();
|
||||
result.worldToTangent = float3x3(cache.getWorldToTangent());
|
||||
// TODO instancing
|
||||
/*for(int i = 0; i < NUM_MATERIAL_TEXCOORDS-1; i+=2)
|
||||
{
|
||||
@@ -179,35 +155,11 @@ struct VertexShaderInput
|
||||
result.texCoords[0] = packedTexCoords2;
|
||||
return result;
|
||||
}
|
||||
|
||||
ShaderAttributeInterpolation getInterpolants(VertexValueCache cache, MaterialVertexParameter vertexParams)
|
||||
{
|
||||
ShaderAttributeInterpolation result = (ShaderAttributeInterpolation)0;
|
||||
float3x3 tangentToWorld = cache.getTangentToWorld();
|
||||
result.normal = mul(getSceneData().localToWorld, float4(normal, 0.0f)).xyz;
|
||||
result.tangent = mul(getSceneData().localToWorld, float4(tangent, 0.0f)).xyz;
|
||||
result.biTangent = mul(getSceneData().localToWorld, float4(biTangent, 0.0f)).xyz;
|
||||
result.color = color;
|
||||
result.worldPosition = vertexParams.worldPosition;
|
||||
result.viewPosition = vertexParams.viewPosition;
|
||||
for(int i = 0; i < NUM_MATERIAL_TEXCOORDS; ++i)
|
||||
{
|
||||
if(i % 2 == 1)
|
||||
{
|
||||
result.packedTexCoords[i / 2].zw = vertexParams.texCoords[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
result.packedTexCoords[i / 2].xy = vertexParams.texCoords[i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
struct PositionOnlyVertexShaderInput
|
||||
{
|
||||
float4 position;
|
||||
float3 position;
|
||||
#if USE_INSTANCING
|
||||
float4 instanceOrigin;
|
||||
float3 instanceTransform1;
|
||||
@@ -216,9 +168,6 @@ struct PositionOnlyVertexShaderInput
|
||||
#endif // USE_INSTANCING
|
||||
float3 getWorldPosition()
|
||||
{
|
||||
float4x4 localToWorld = getSceneData().localToWorld;
|
||||
float3 rotatedPosition = localToWorld[0].xyz * position.xxx + localToWorld[1].xyz * position.yyy + localToWorld[2].xyz * position.zzz;
|
||||
return mul(getSceneData().localToWorld, position).xyz;
|
||||
//rotatedPosition + float3(localToWorld[0].w, localToWorld[1].w, localToWorld[2].w);
|
||||
return mul(getSceneData().modelToWorld, float4(position, 1.0f)).xyz;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -126,7 +126,7 @@ void BasePass::beginFrame(const Component::Camera& cam)
|
||||
|
||||
viewParams.viewMatrix = cam.getViewMatrix();
|
||||
viewParams.projectionMatrix = viewport->getProjectionMatrix();
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 0);
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 1);
|
||||
viewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
|
||||
uniformUpdate.size = sizeof(ViewParameter);
|
||||
uniformUpdate.data = (uint8*)&viewParams;
|
||||
|
||||
@@ -21,7 +21,7 @@ void DebugPass::beginFrame(const Component::Camera& cam)
|
||||
|
||||
viewParams.viewMatrix = cam.getViewMatrix();
|
||||
viewParams.projectionMatrix = viewport->getProjectionMatrix();
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 0);
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 1);
|
||||
viewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
|
||||
uniformUpdate.size = sizeof(ViewParameter);
|
||||
uniformUpdate.data = (uint8*)&viewParams;
|
||||
|
||||
@@ -110,7 +110,7 @@ void DepthPrepass::beginFrame(const Component::Camera& cam)
|
||||
|
||||
viewParams.viewMatrix = cam.getViewMatrix();
|
||||
viewParams.projectionMatrix = viewport->getProjectionMatrix();
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 0);
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 1);
|
||||
viewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
|
||||
uniformUpdate.size = sizeof(ViewParameter);
|
||||
uniformUpdate.data = (uint8*)&viewParams;
|
||||
|
||||
@@ -25,25 +25,11 @@ void LightCullingPass::beginFrame(const Component::Camera& cam)
|
||||
BulkResourceData uniformUpdate;
|
||||
viewParams.viewMatrix = cam.getViewMatrix();
|
||||
viewParams.projectionMatrix = viewport->getProjectionMatrix();
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 0);
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 1);
|
||||
viewParams.screenDimensions = Vector2(static_cast<float>(viewportWidth), static_cast<float>(viewportHeight));
|
||||
uniformUpdate.size = sizeof(ViewParameter);
|
||||
uniformUpdate.data = (uint8*)&viewParams;
|
||||
viewParamsBuffer->updateContents(uniformUpdate);
|
||||
|
||||
const LightEnv& lightEnv = passData.lightEnv;
|
||||
uniformUpdate.size = sizeof(DirectionalLight) * MAX_DIRECTIONAL_LIGHTS;
|
||||
uniformUpdate.data = (uint8*)&lightEnv.directionalLights;
|
||||
directLightBuffer->updateContents(uniformUpdate);
|
||||
uniformUpdate.size = sizeof(PointLight) * MAX_POINT_LIGHTS;
|
||||
uniformUpdate.data = (uint8*)&lightEnv.pointLights;
|
||||
pointLightBuffer->updateContents(uniformUpdate);
|
||||
|
||||
uniformUpdate.size = sizeof(uint32);
|
||||
uniformUpdate.data = (uint8*)&lightEnv.numDirectionalLights;
|
||||
numDirLightBuffer->updateContents(uniformUpdate);
|
||||
uniformUpdate.data = (uint8*)&lightEnv.numPointLights;
|
||||
numPointLightBuffer->updateContents(uniformUpdate);
|
||||
|
||||
BulkResourceData counterReset;
|
||||
uint32 reset = 0;
|
||||
@@ -68,10 +54,10 @@ void LightCullingPass::beginFrame(const Component::Camera& cam)
|
||||
cullingDescriptorSet->updateBuffer(9, frustumBuffer);
|
||||
|
||||
|
||||
lightEnvDescriptorSet->updateBuffer(0, directLightBuffer);
|
||||
lightEnvDescriptorSet->updateBuffer(1, numDirLightBuffer);
|
||||
lightEnvDescriptorSet->updateBuffer(2, pointLightBuffer);
|
||||
lightEnvDescriptorSet->updateBuffer(3, numPointLightBuffer);
|
||||
lightEnvDescriptorSet->updateBuffer(0, passData.lightEnv.directionalLights);
|
||||
lightEnvDescriptorSet->updateBuffer(1, passData.lightEnv.numDirectional);
|
||||
lightEnvDescriptorSet->updateBuffer(2, passData.lightEnv.pointLights);
|
||||
lightEnvDescriptorSet->updateBuffer(3, passData.lightEnv.numPoints);
|
||||
lightEnvDescriptorSet->writeChanges();
|
||||
//std::cout << "LightCulling beginFrame()" << std::endl;
|
||||
//co_return;
|
||||
@@ -202,28 +188,6 @@ void LightCullingPass::publishOutputs()
|
||||
resources->registerBufferOutput("LIGHTCULLING_OLIGHTLIST", oLightIndexList);
|
||||
resources->registerBufferOutput("LIGHTCULLING_TLIGHTLIST", tLightIndexList);
|
||||
|
||||
structInfo = {
|
||||
.resourceData = {
|
||||
.size = sizeof(DirectionalLight) * MAX_DIRECTIONAL_LIGHTS,
|
||||
.data = nullptr,
|
||||
},
|
||||
.stride = sizeof(DirectionalLight),
|
||||
.bDynamic = true,
|
||||
};
|
||||
directLightBuffer = graphics->createStructuredBuffer(structInfo);
|
||||
structInfo.resourceData.size = sizeof(PointLight) * MAX_POINT_LIGHTS;
|
||||
pointLightBuffer = graphics->createStructuredBuffer(structInfo);
|
||||
|
||||
UniformBufferCreateInfo uniformInfo = {
|
||||
.resourceData = {
|
||||
.size = sizeof(uint32),
|
||||
.data = nullptr
|
||||
},
|
||||
.bDynamic = true
|
||||
};
|
||||
numDirLightBuffer = graphics->createUniformBuffer(uniformInfo);
|
||||
numPointLightBuffer = graphics->createUniformBuffer(uniformInfo);
|
||||
|
||||
resources->registerBufferOutput("DIRECTIONAL_LIGHTS", directLightBuffer);
|
||||
resources->registerUniformOutput("NUM_DIRECTIONAL_LIGHTS", numDirLightBuffer);
|
||||
resources->registerBufferOutput("POINT_LIGHTS", pointLightBuffer);
|
||||
|
||||
@@ -60,10 +60,6 @@ private:
|
||||
Gfx::PStructuredBuffer tLightIndexList;
|
||||
Gfx::PTexture2D oLightGrid;
|
||||
Gfx::PTexture2D tLightGrid;
|
||||
Gfx::PStructuredBuffer directLightBuffer;
|
||||
Gfx::PUniformBuffer numDirLightBuffer;
|
||||
Gfx::PStructuredBuffer pointLightBuffer;
|
||||
Gfx::PUniformBuffer numPointLightBuffer;
|
||||
Gfx::PDescriptorSet lightEnvDescriptorSet;
|
||||
Gfx::PDescriptorSet cullingDescriptorSet;
|
||||
Gfx::PDescriptorLayout lightEnvDescriptorLayout;
|
||||
|
||||
@@ -83,7 +83,7 @@ void SkyboxRenderPass::beginFrame(const Component::Camera& cam)
|
||||
|
||||
viewParams.viewMatrix = cam.getViewMatrix();
|
||||
viewParams.projectionMatrix = viewport->getProjectionMatrix();
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 0);
|
||||
viewParams.cameraPosition = Vector4(cam.getCameraPosition(), 1);
|
||||
viewParams.screenDimensions = Vector2(static_cast<float>(viewport->getSizeX()), static_cast<float>(viewport->getSizeY()));
|
||||
uniformUpdate.size = sizeof(ViewParameter);
|
||||
uniformUpdate.data = (uint8*)&viewParams;
|
||||
|
||||
@@ -18,6 +18,29 @@ Scene::Scene(Gfx::PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
, physics(registry)
|
||||
{
|
||||
|
||||
StructuredBufferCreateInfo structInfo = {
|
||||
.resourceData = {
|
||||
.size = sizeof(DirectionalLight) * MAX_DIRECTIONAL_LIGHTS,
|
||||
.data = nullptr,
|
||||
},
|
||||
.stride = sizeof(DirectionalLight),
|
||||
.bDynamic = true,
|
||||
};
|
||||
lightEnv.directionalLights = graphics->createStructuredBuffer(structInfo);
|
||||
structInfo.resourceData.size = sizeof(PointLight) * MAX_POINT_LIGHTS;
|
||||
lightEnv.pointLights = graphics->createStructuredBuffer(structInfo);
|
||||
|
||||
UniformBufferCreateInfo uniformInfo = {
|
||||
.resourceData = {
|
||||
.size = sizeof(uint32),
|
||||
.data = nullptr
|
||||
},
|
||||
.bDynamic = true
|
||||
};
|
||||
lightEnv.numDirectional = graphics->createUniformBuffer(uniformInfo);
|
||||
lightEnv.numPoints = graphics->createUniformBuffer(uniformInfo);
|
||||
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
@@ -84,12 +107,12 @@ Array<MeshBatch> Scene::getStaticMeshes()
|
||||
|
||||
LightEnv Scene::getLightBuffer() const
|
||||
{
|
||||
LightEnv result;
|
||||
result.directionalLights[0].color = Vector4(0.4, 0.3, 0.5, 1.0);
|
||||
result.directionalLights[0].direction = Vector4(0.5, -0.5, 0, 0);
|
||||
result.directionalLights[0].intensity = Vector4(1.0, 0.9, 0.7, 0.5);
|
||||
result.numDirectionalLights = 1;
|
||||
result.numPointLights = 0;
|
||||
result.numDirectionalLights = 0;
|
||||
result.pointLights[0].positionWS = Vector4(0, 50, 0, 0);
|
||||
result.pointLights[0].colorRange = Vector4(0.2, 0.4, 0.7, 100);
|
||||
result.numPointLights = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ struct DirectionalLight
|
||||
{
|
||||
Vector4 color;
|
||||
Vector4 direction;
|
||||
Vector4 intensity;
|
||||
};
|
||||
|
||||
struct PointLight
|
||||
@@ -29,10 +28,10 @@ struct PointLight
|
||||
#define MAX_POINT_LIGHTS 256
|
||||
struct LightEnv
|
||||
{
|
||||
DirectionalLight directionalLights[MAX_DIRECTIONAL_LIGHTS];
|
||||
PointLight pointLights[MAX_POINT_LIGHTS];
|
||||
uint32 numDirectionalLights;
|
||||
uint32 numPointLights;
|
||||
Gfx::PStructuredBuffer directionalLights;
|
||||
Gfx::PUniformBuffer numDirectional;
|
||||
Gfx::PStructuredBuffer pointLights;
|
||||
Gfx::PUniformBuffer numPoints;
|
||||
};
|
||||
|
||||
class Scene
|
||||
@@ -83,7 +82,8 @@ private:
|
||||
Vector4 actorLocation;
|
||||
};
|
||||
Array<PrimitiveSceneData> sceneData;
|
||||
Gfx::PStructuredBuffer sceneDataBuffer;
|
||||
Gfx::PStructuredBuffer sceneDataBuffer;
|
||||
LightEnv lightEnv;
|
||||
PhysicsSystem physics;
|
||||
Gfx::PGraphics graphics;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user