Files
Seele/res/shaders/BasePass.slang
T

69 lines
2.5 KiB
Plaintext
Raw Normal View History

2023-10-09 17:20:30 +02:00
import Common;
2023-11-05 10:36:01 +01:00
import LightEnv;
import MaterialParameter;
2024-07-10 21:07:10 +02:00
import MATERIAL_FILE_NAME;
2023-11-05 10:36:01 +01:00
struct LightCullingData
{
2024-02-21 10:30:04 +01:00
RWStructuredBuffer<uint> lightIndexList;
2023-11-05 10:36:01 +01:00
2024-02-21 10:30:04 +01:00
RWTexture2D<uint2> lightGrid;
2023-11-05 10:36:01 +01:00
};
2023-11-08 23:27:21 +01:00
ParameterBlock<LightCullingData> pLightCullingData;
2023-10-09 17:20:30 +02:00
2025-05-10 22:52:46 +02:00
static const float4x4 biasMat = float4x4(
0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
2023-10-09 17:20:30 +02:00
[shader("pixel")]
float4 fragmentMain(in FragmentParameter params) : SV_Target
2023-10-09 17:20:30 +02:00
{
2023-11-13 09:07:23 +01:00
LightingParameter lightingParams = params.getLightingParameter();
2023-12-15 11:57:13 +01:00
MaterialParameter materialParams = params.getMaterialParameter();
2025-05-04 21:30:01 +02:00
var brdf = Material.prepare(materialParams);
2025-05-09 14:59:58 +02:00
brdf.setNormal(normalize(params.normal_WS));
2024-02-20 22:38:35 +01:00
uint2 tileIndex = uint2(floor(params.position_CS.xy / BLOCK_SIZE));
2024-02-21 10:30:04 +01:00
uint startOffset = pLightCullingData.lightGrid[tileIndex].x;
uint lightCount = pLightCullingData.lightGrid[tileIndex].y;
2023-11-05 10:36:01 +01:00
float3 result = float3(0, 0, 0);
2023-11-08 23:27:21 +01:00
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
2023-11-05 10:36:01 +01:00
{
2025-05-10 22:52:46 +02:00
float4 lightSpacePos = mul(biasMat, mul(pLightEnv.directionalLights[i].lightSpaceMatrix, float4(params.position_WS, 1)));
float4 shadowCoord = lightSpacePos / lightSpacePos.w;
2025-05-10 22:58:20 +02:00
int2 texDim;
pLightEnv.shadowMap.GetDimensions(texDim.x, texDim.y);
float scale = 1.5f;
float dx = scale * 1.0 / float(texDim.x);
float dy = scale * 1.0 / float(texDim.y);
float shadowFactor = 0.0f;
int count = 0;
int range = 1;
for (int x = -range; x <= range; ++x) {
for (int y = -range; y <= range; ++y) {
float shadow = 1.0f;
if (shadowCoord.z > 0.0 && shadowCoord.z < 1.0)
{
float dist = pLightEnv.shadowMap.Sample(pLightEnv.shadowSampler, shadowCoord.xy + float2(dx * x, dy * y)).r;
if (shadowCoord.w > 0 && dist > shadowCoord.z)
{
shadow = 0;
}
}
shadowFactor += shadow;
count++;
}
2025-05-10 22:52:46 +02:00
}
2025-05-09 14:59:58 +02:00
2025-05-10 22:58:20 +02:00
result += (shadowFactor / count) * pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
2023-11-05 10:36:01 +01:00
}
2025-05-10 22:52:46 +02:00
for (uint i = 0; i < pLightEnv.numPointLights; ++i)
2023-11-05 10:36:01 +01:00
{
2025-05-10 22:52:46 +02:00
//uint lightIndex = pLightCullingData.lightIndexList[startOffset + i];
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
2023-11-05 10:36:01 +01:00
}
2025-05-10 22:58:20 +02:00
result += brdf.evaluateAmbient(lightingParams.viewDir_WS);
2024-08-07 21:52:45 +02:00
return float4(result, brdf.getAlpha());
2024-12-27 17:06:43 +01:00
}