Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cbuffer cbPerLightVolume
- {
- float3 gLightVolPos;
- float3 gLightVolScale;
- };
- RWTexture3D<uint> gLightTex0;
- RWTexture3D<uint> gLightTex1;
- RWTexture3D<uint> gLightTex2;
- RWTexture3D<uint> gLightTex3;
- RWTexture3D<uint> gLightTex4;
- float4 rgba8_as_uint_to_float4(uint input)
- {
- float4 output;
- output.x = (float) (input & 0x000000ff) / 255;
- output.y = (float)(((input>> 8) & 0x000000ff)) / 255;
- output.z = (float)(((input>>16) & 0x000000ff)) / 255;
- output.w = (float) (input>>24) / 255;
- return output;
- }
- uint float_to_uint(float value, float scale)
- {
- return (uint)floor(value * scale + 0.5f);
- }
- uint float4_to_rgba8_as_uint(float4 input)
- {
- uint output = ( (float_to_uint(saturate(input.x), 255)) |
- (float_to_uint(saturate(input.y), 255)<< 8) |
- (float_to_uint(saturate(input.z), 255)<<16) |
- (float_to_uint(saturate(input.w), 255)<<24) );
- return output;
- }
- void read6Axes(in int3 texCoord, out float3 xPos, out float3 xNeg, out float3 yPos, out float3 yNeg, out float3 zPos, out float3 zNeg)
- {
- float4 sample0 = rgba8_as_uint_to_float4(gLightTex0[texCoord]);
- float4 sample1 = rgba8_as_uint_to_float4(gLightTex1[texCoord]);
- float4 sample2 = rgba8_as_uint_to_float4(gLightTex2[texCoord]);
- float4 sample3 = rgba8_as_uint_to_float4(gLightTex3[texCoord]);
- float4 sample4 = rgba8_as_uint_to_float4(gLightTex4[texCoord]);
- xPos = sample0.xyz;
- xNeg = sample1.xyz;
- yPos = sample2.xyz;
- yNeg = sample3.xyz;
- zPos = sample4.xyz;
- zNeg = float3(sample0.w, sample1.w, sample2.w);
- }
- void write6Axes(int3 texCoord, float3 xPos, float3 xNeg, float3 yPos, float3 yNeg, float3 zPos, float3 zNeg)
- {
- gLightTex0[texCoord] = float4_to_rgba8_as_uint(float4(xPos, zNeg.x));
- gLightTex1[texCoord] = float4_to_rgba8_as_uint(float4(xNeg, zNeg.y));
- gLightTex2[texCoord] = float4_to_rgba8_as_uint(float4(yPos, zNeg.z));
- gLightTex3[texCoord] = float4_to_rgba8_as_uint(float4(yNeg, 0));
- gLightTex4[texCoord] = float4_to_rgba8_as_uint(float4(zPos, 0));
- }
- cbuffer cbPerLight
- {
- float4 gLightPosWAndRadius;
- float4 gLightColorAndIntens;
- float4 gLightDirAndCosAngle;
- };
- void storeLighting(int3 texCoord, float3 lightStrengthDir, float3 lightColor)
- {
- float3 xPos, xNeg, yPos, yNeg, zPos, zNeg;
- read6Axes(texCoord, xPos, xNeg, yPos, yNeg, zPos, zNeg);
- xPos += lightColor * max( lightStrengthDir.x, 0);
- xNeg += lightColor * max(-lightStrengthDir.x, 0);
- yPos += lightColor * max( lightStrengthDir.y, 0);
- yNeg += lightColor * max(-lightStrengthDir.y, 0);
- zPos += lightColor * max( lightStrengthDir.z, 0);
- zNeg += lightColor * max(-lightStrengthDir.z, 0);
- write6Axes(texCoord, xPos, xNeg, yPos, yNeg, zPos, zNeg);
- }
- [numthreads(8, 8, 8)]
- void CS( uint3 GroupID : SV_GroupID,
- uint3 DispatchThreadID : SV_DispatchThreadID,
- uint3 GroupThreadID : SV_GroupThreadID,
- uint GroupIndex : SV_GroupIndex )
- {
- const float3 lightPos = gLightPosWAndRadius.xyz;
- const float lightRadius = gLightPosWAndRadius.w;
- const float3 lightColor = gLightColorAndIntens.rgb;
- const float lightIntensity = gLightColorAndIntens.a;
- const float3 spotDir = gLightDirAndCosAngle.xyz;
- const float coneCosAngle = gLightDirAndCosAngle.w;
- float3 samplePos = gLightVolPos + (float3(DispatchThreadID) + float3(0.5f, 0.5f, 0.5f)) * gLightVolScale;
- float3 lightDir = lightPos - samplePos;
- float dist = length(lightDir);
- lightDir = normalize(lightDir);
- float spotAngleCos = saturate(dot( -spotDir, lightDir ));
- if(spotAngleCos < coneCosAngle)
- return;
- float atten = saturate((lightRadius - dist) / lightRadius);
- float angleAtten = (spotAngleCos - coneCosAngle) / (1.0 - coneCosAngle);
- float3 lightStrengthDir = lightDir * atten * angleAtten * lightIntensity;
- storeLighting(DispatchThreadID, lightStrengthDir, lightColor);
- }
- technique11 tech
- {
- pass P0
- {
- SetComputeShader(CompileShader(cs_5_0, CS()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement