Files
Seele/res/shaders/ForwardPlus.slang
T

77 lines
2.3 KiB
Plaintext
Raw Normal View History

2020-10-03 11:00:10 +02:00
import Common;
2020-06-02 11:46:18 +02:00
import LightEnv;
import BRDF;
import Material;
2020-09-19 14:36:50 +02:00
import VERTEX_INPUT_IMPORT;
import MATERIAL_IMPORT;
import PrimitiveSceneData;
import MaterialParameter;
2020-06-02 11:46:18 +02:00
2021-06-12 18:51:29 +02:00
layout(set = INDEX_LIGHT_ENV, binding = 4)
2021-05-10 23:57:55 +02:00
StructuredBuffer<uint> lightIndexList;
2021-06-12 18:51:29 +02:00
layout(set = INDEX_LIGHT_ENV, binding = 5)
2021-05-10 23:57:55 +02:00
RWTexture2D<uint2> lightGrid;
2020-06-02 11:46:18 +02:00
struct VertexStageOutput
{
ShaderAttributeInterpolation shaderAttributeInterpolation : Interpolation;
VertexValueCache cache : ShaderCache;
2021-10-16 12:59:11 +02:00
float4 position : SV_Position;
2020-06-02 11:46:18 +02:00
};
[shader("vertex")]
VertexStageOutput vertexMain(
2021-10-16 12:59:11 +02:00
VertexShaderInput input)
2020-06-02 11:46:18 +02:00
{
VertexStageOutput output;
VertexValueCache cache = input.getVertexCache();
2021-10-16 12:59:11 +02:00
float3 worldPosition = input.getWorldPosition();
2020-10-03 11:00:10 +02:00
worldPosition += gMaterial.getWorldOffset();
float4 clipSpacePosition;
2020-06-02 11:46:18 +02:00
2020-10-03 11:00:10 +02:00
float3x3 tangentToLocal = cache.getTangentToLocal();
MaterialVertexParameter vertexParams = input.getMaterialVertexParameters(cache, worldPosition, tangentToLocal);
2020-06-02 11:46:18 +02:00
float4 viewSpacePosition = mul(gViewParams.viewMatrix, float4(worldPosition, 1));
clipSpacePosition = mul(gViewParams.projectionMatrix, viewSpacePosition);
output.position = clipSpacePosition;
output.shaderAttributeInterpolation = input.getInterpolants(cache, vertexParams);
2021-05-06 17:02:10 +02:00
output.cache = cache;
return output;
2020-06-02 11:46:18 +02:00
}
[shader("fragment")]
float4 fragmentMain(
ShaderAttributeInterpolation input : Interpolation,
VertexValueCache cache : ShaderCache,
float4 position : SV_Position
) : SV_Target
2020-06-02 11:46:18 +02:00
{
MaterialFragmentParameter materialParams = input.getMaterialParameter(position);
2021-10-16 12:59:11 +02:00
TMaterial.BRDF brdf = gMaterial.prepare(materialParams);
float3 viewDir = normalize(materialParams.viewDir);
2020-06-02 11:46:18 +02:00
float3 result = float3(0, 0, 0);
2021-06-12 18:51:29 +02:00
for (int i = 0; i < numDirectionalLights; i++)
2020-06-02 11:46:18 +02:00
{
2021-06-12 18:51:29 +02:00
result += directionalLights[i].illuminate(materialParams, brdf, viewDir);
2020-06-02 11:46:18 +02:00
}
2021-10-16 12:59:11 +02:00
uint2 tileIndex = uint2(floor(position.xy / BLOCK_SIZE));
uint2 gridValue = lightGrid[tileIndex];
2021-10-16 12:59:11 +02:00
uint startOffset = gridValue.x;
uint lightCount = gridValue.y;
2020-06-02 11:46:18 +02:00
for (int j = 0; j < lightCount; ++j)
2020-06-02 11:46:18 +02:00
{
2021-10-16 12:59:11 +02:00
uint lightIndex = lightIndexList[startOffset + j];
PointLight pointLight = pointLights[lightIndex];
result += pointLight.illuminate(materialParams, brdf, viewDir);
2020-06-02 11:46:18 +02:00
}
return float4(result, 1);
2020-06-02 11:46:18 +02:00
}