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 679dc135..ffd0343f 100644 Binary files a/TempShaderFolder/DeferredCompositeCs.spv and b/TempShaderFolder/DeferredCompositeCs.spv differ diff --git a/TempShaderFolder/TestCubeFs.glsl b/TempShaderFolder/TestCubeFs.glsl index 44b9210b..1c5c9c15 100644 --- a/TempShaderFolder/TestCubeFs.glsl +++ b/TempShaderFolder/TestCubeFs.glsl @@ -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; diff --git a/TempShaderFolder/TestCubeVs.glsl b/TempShaderFolder/TestCubeVs.glsl index bac9444d..dc8f0b98 100644 --- a/TempShaderFolder/TestCubeVs.glsl +++ b/TempShaderFolder/TestCubeVs.glsl @@ -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); } \ No newline at end of file diff --git a/TempShaderFolder/TestCubeVs.spv b/TempShaderFolder/TestCubeVs.spv index 9cf9d86d..0dfd6cf2 100644 Binary files a/TempShaderFolder/TestCubeVs.spv and b/TempShaderFolder/TestCubeVs.spv differ