From ae88c709369b3d0e010a6820a2df1ea40d6e81c1 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Thu, 27 Oct 2022 02:28:38 +0800 Subject: [PATCH] Lighting shader kind of done (still has bugs) --- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 3 +- .../MiddleEnd/Lights/SHLightingSubSystem.cpp | 12 +++-- .../MiddleEnd/Lights/SHLightingSubSystem.h | 2 +- .../RenderGraph/SHRenderGraphNodeCompute.cpp | 1 - TempShaderFolder/DeferredCompositeCs.glsl | 47 +++++++++++++++--- TempShaderFolder/DeferredCompositeCs.spv | Bin 1716 -> 3384 bytes TempShaderFolder/TestCubeFs.glsl | 2 +- TempShaderFolder/TestCubeVs.glsl | 2 +- TempShaderFolder/TestCubeVs.spv | Bin 2684 -> 3036 bytes 9 files changed, 54 insertions(+), 15 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index dfa4e41e..58a86bcd 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -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) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp index a22fb662..6e8dd916 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.cpp @@ -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(dynamicOffsets.size()); ++j) + for (uint32_t j = 0; j < static_cast(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(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]}); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h index 6c974730..efc6ddf6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Lights/SHLightingSubSystem.h @@ -31,7 +31,7 @@ namespace SHADE uint32_t cullingMask; //! Diffuse color emitted by the light - SHVec4 diffuseColor; + alignas (16) SHVec4 diffuseColor; }; diff --git a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp index a5208fcf..678fc41b 100644 --- a/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp +++ b/SHADE_Engine/src/Graphics/RenderGraph/SHRenderGraphNodeCompute.cpp @@ -53,7 +53,6 @@ namespace SHADE descSetGroups[i] = graphStorage->descriptorPool->Allocate(layouts, variableCounts); } - HandleResize(); } diff --git a/TempShaderFolder/DeferredCompositeCs.glsl b/TempShaderFolder/DeferredCompositeCs.glsl index 354a8954..c5e543db 100644 --- a/TempShaderFolder/DeferredCompositeCs.glsl +++ b/TempShaderFolder/DeferredCompositeCs.glsl @@ -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)); } \ No newline at end of file diff --git a/TempShaderFolder/DeferredCompositeCs.spv b/TempShaderFolder/DeferredCompositeCs.spv index 679dc135f5f556eb95565c51c1c9f1c2aca8821a..ffd0343f71e1704bba6f534f17d842cba15ac6cf 100644 GIT binary patch literal 3384 zcmZ9O`*Ku85XL8$O#(>>2qAbOZio^UjHuicxx_3k(S-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