Lighting shader kind of done (still has bugs)

This commit is contained in:
Brandon Mak 2022-10-27 02:28:38 +08:00
parent 6e9f54987f
commit ae88c70936
9 changed files with 54 additions and 15 deletions

View File

@ -219,7 +219,7 @@ namespace SHADE
// deferred composite
auto deferredCompositeShader = shaderModuleLibrary.GetShaderModule("DeferredCompositeCs.glsl");
gBufferNode->AddNodeCompute(deferredCompositeShader, { "Albedo", "Scene" });
gBufferNode->AddNodeCompute(deferredCompositeShader, { "Position", "Normals", "Albedo", "Scene" });
auto dummyNode = worldRenderGraph->AddNode("Dummy Pass", { "Scene" }, {"G-Buffer"}); // no predecessors
@ -403,6 +403,7 @@ namespace SHADE
// Force set the pipeline layout
currentCmdBuffer->ForceSetPipelineLayout(SHGraphicsGlobalData::GetDummyPipelineLayout(), SH_PIPELINE_TYPE::GRAPHICS);
currentCmdBuffer->ForceSetPipelineLayout(SHGraphicsGlobalData::GetDummyPipelineLayout(), SH_PIPELINE_TYPE::COMPUTE);
// Bind all the buffers required for meshes
for (auto& [buffer, bindingPoint] : MESH_DATA)

View File

@ -319,11 +319,12 @@ namespace SHADE
{
dynamicOffsets[i][0] = i * lightCountsAlignedSize;
// Even if the first binding is a count, we want to account for that too
for (uint32_t j = 1; j < static_cast<uint32_t>(dynamicOffsets.size()); ++j)
for (uint32_t j = 0; j < static_cast<uint32_t>(SH_LIGHT_TYPE::NUM_TYPES); ++j)
{
auto const& typeData = perTypeData[j];
{
dynamicOffsets[i][j] = j * typeData.GetAlignmentSize();
// +1 because 1st reserved for count
dynamicOffsets[i][j + 1] = i * typeData.GetAlignmentSize();
}
}
}
@ -376,6 +377,7 @@ namespace SHADE
{
dynamicOffsets[i].resize(NUM_LIGHT_TYPES + 1); // +1 for the count
}
}
/***************************************************************************/
@ -425,7 +427,7 @@ namespace SHADE
data.WriteToGPU(frameIndex);
}
lightCountsBuffer->WriteToMemory(lightCountsData.data(), lightCountsData.size() * sizeof (uint32_t), 0, lightCountsAlignedSize * frameIndex);
lightCountsBuffer->WriteToMemory(lightCountsData.data(), static_cast<uint32_t>(lightCountsData.size()) * sizeof (uint32_t), 0, lightCountsAlignedSize * frameIndex);
// If any of the buffers got expanded, the descriptor set is invalid because the expanded buffer
// is a new buffer. If some expansion was detected, update descriptor sets.
@ -442,8 +444,10 @@ namespace SHADE
// so we do it anyway. #NoteToSelf: if at any point it affects performance, do a check before computing.
ComputeDynamicOffsets();
//cmdBuffer->ForceSetPipelineLayout()
// Bind descriptor set (We bind at an offset because the buffer holds NUM_FRAME_BUFFERS sets of data).
cmdBuffer->BindDescriptorSet(lightingDataDescSet, SH_PIPELINE_TYPE::GRAPHICS, SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, {dynamicOffsets[frameIndex]});
cmdBuffer->BindDescriptorSet(lightingDataDescSet, SH_PIPELINE_TYPE::COMPUTE, SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, {dynamicOffsets[frameIndex]});
}

View File

@ -31,7 +31,7 @@ namespace SHADE
uint32_t cullingMask;
//! Diffuse color emitted by the light
SHVec4 diffuseColor;
alignas (16) SHVec4 diffuseColor;
};

View File

@ -53,7 +53,6 @@ namespace SHADE
descSetGroups[i] = graphStorage->descriptorPool->Allocate(layouts, variableCounts);
}
HandleResize();
}

View File

@ -9,22 +9,57 @@ struct DirectionalLightStruct
};
layout(local_size_x = 16, local_size_y = 16) in;
layout(set = 4, binding = 0, rgba8) uniform image2D inputImage;
layout(set = 4, binding = 1, rgba8) uniform image2D targetImage;
layout(set = 4, binding = 0, rgba8) uniform image2D positions;
layout(set = 4, binding = 1, rgba8) uniform image2D normals;
layout(set = 4, binding = 2, rgba8) uniform image2D albedo;
layout(set = 4, binding = 3, rgba8) uniform image2D targetImage;
layout(set = 1, binding = 0) buffer DirectionalLightData
layout(set = 1, binding = 0) uniform LightCounts
{
uint directionalLights;
uint pointLights;
uint spotLights;
} lightCounts;
layout(std430, set = 1, binding = 1) buffer DirectionalLightData
{
DirectionalLightStruct dLightData[];
} DirLightData;
void main()
{
{
// convenient variables
ivec2 globalThread = ivec2(gl_GlobalInvocationID);
vec3 color = imageLoad (inputImage, globalThread).rgb;
// Get the diffuse color of the pixel
vec3 pixelDiffuse = imageLoad (albedo, globalThread).rgb;
// Get position of fragment in world space
vec3 positionWorld = imageLoad (positions, globalThread).rgb;
// normal of fragment
vec3 normalWorld = imageLoad(normals, globalThread).rgb;
normalWorld = normalize (normalWorld);
vec3 fragColor = vec3 (0.0f);
for (int i = 0; i < lightCounts.directionalLights; ++i)
{
// get normalized direction of light
vec3 dLightNormalized = vec3 (0.0f, 0.0f, 1.0f);
//vec3 dLightNormalized = normalize (DirLightData.dLightData[i].direction);
// Get diffuse strength
float diffuseStrength = max (0, dot (dLightNormalized, normalWorld));
//fragColor += vec3 (1.0f) * diffuseStrength.rrr * pixelDiffuse;
fragColor += DirLightData.dLightData[i].diffuseColor.rgb * diffuseStrength.rrr * pixelDiffuse;
//fragColor += vec3 (dLightNormalized.rgb);
}
// store result into result image
imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(color, 1.0f));
//imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(fragColor, 1.0f));
imageStore(targetImage, ivec2(gl_GlobalInvocationID.xy), vec4(normalWorld, 1.0f));
}

View File

@ -30,7 +30,7 @@ layout(location = 3) flat in struct
//layout (set = 0, binding = )
layout (set = 0, binding = 1) uniform sampler2D textures[]; // for textures (global)
layout (set = 3, binding = 0) buffer MaterialProperties // For materials
layout (std430, set = 3, binding = 0) buffer MaterialProperties // For materials
{
MatPropData data[];
} MatProp;

View File

@ -44,6 +44,6 @@ void main()
Out.vertPos = worldTransform * vec4(aVertexPos, 1.0f);
Out.uv = aUV;
Out.normal = vec4 (aNormal, 1.0f);
Out.normal.rgb = mat3 (transpose (inverse (worldTransform))) * aNormal.rgb;
gl_Position = cameraData.vpMat * worldTransform * vec4 (aVertexPos, 1.0f);
}

Binary file not shown.