Implemented Shadows (f0kin finally)

This commit is contained in:
Brandon Mak 2023-03-07 17:39:51 +08:00
parent 826df94c96
commit b6b2c75fc3
7 changed files with 38 additions and 25 deletions

View File

@ -32,10 +32,11 @@ layout(location = 0) in struct
vec2 uv; // location = 1 vec2 uv; // location = 1
vec4 normal; // location = 2 vec4 normal; // location = 2
vec4 worldPos; // location = 3 vec4 worldPos; // location = 3
vec3 worldNormal; // location = 4
} In; } In;
// material stuff // material stuff
layout(location = 4) flat in struct layout(location = 5) flat in struct
{ {
int materialIndex; int materialIndex;
uint eid; uint eid;
@ -72,10 +73,11 @@ void main()
{ {
position = In.vertPos; position = In.vertPos;
normals = In.normal; normals = In.normal;
albedo = texture(textures[nonuniformEXT(MatProp.data[In2.materialIndex].textureIndex)], In.uv); albedo = vec4 (texture(textures[nonuniformEXT(MatProp.data[In2.materialIndex].textureIndex)], In.uv).xyz, 1.0f);
worldSpacePosition = In.worldPos; worldSpacePosition = In.worldPos;
outEntityID = In2.eid; outEntityID = In2.eid;
lightLayerIndices = uvec4 (In2.lightLayerIndex, 0, 0, 1);
// float vpHeight = float (In2.screenSpacePos.y) - MatProp.data[In2.materialIndex].highlightPosition; // float vpHeight = float (In2.screenSpacePos.y) - MatProp.data[In2.materialIndex].highlightPosition;
// bring the frame of reference to the object's screen space pos // bring the frame of reference to the object's screen space pos
@ -100,4 +102,6 @@ void main()
} }
else else
objectVFX = vec4(0.0f, 0.0f, 0.0f, 1.0f); objectVFX = vec4(0.0f, 0.0f, 0.0f, 1.0f);
objectVFX.a = 1.0f;
} }

View File

@ -17,6 +17,7 @@ layout(location = 0) out struct
vec2 uv; // location = 1 vec2 uv; // location = 1
vec4 normal; // location = 2 vec4 normal; // location = 2
vec4 worldPos; // location = 3 vec4 worldPos; // location = 3
vec3 worldNormal; // location = 4
} Out; } Out;
@ -36,7 +37,7 @@ struct GenericData
}; };
// material stuff // material stuff
layout(location = 4) out struct layout(location = 5) out struct
{ {
int materialIndex; int materialIndex;
uint eid; uint eid;
@ -76,11 +77,13 @@ void main()
// uvs for texturing in fragment shader // uvs for texturing in fragment shader
Out.uv = aUV; Out.uv = aUV;
mat3 transposeInv = mat3 (transpose(inverse(modelViewMat))); mat3 mvTransInv = mat3 (transpose(inverse(modelViewMat)));
mat3 modelTransInv = mat3 (transpose(inverse(worldTransform)));
// normals are also in view space // normals are also in view space
Out.normal.rgb = transposeInv * aNormal.rgb; Out.normal.rgb = mvTransInv * aNormal.rgb;
Out.normal.rgb = normalize (Out.normal.rgb); Out.normal.rgb = normalize (Out.normal.rgb);
Out.worldNormal = normalize (modelTransInv * aNormal);
// Get center of object in world position // Get center of object in world position
vec4 worldPos = vec4(worldTransform[3][0], worldTransform[3][1], worldTransform[3][2], 1.0f); vec4 worldPos = vec4(worldTransform[3][0], worldTransform[3][1], worldTransform[3][2], 1.0f);

View File

@ -128,7 +128,7 @@ namespace SHADE
SHFreetypeInstance::Init(); SHFreetypeInstance::Init();
//SHAssetManager::CompileAsset("../../Assets/Shaders/DeferredComposite_CS.glsl", false); SHAssetManager::CompileAsset("../../Assets/Shaders/DeferredComposite_CS.glsl", false);
//SHAssetManager::CompileAsset("../../Assets/Shaders/ShadowMap_FS.glsl", false); //SHAssetManager::CompileAsset("../../Assets/Shaders/ShadowMap_FS.glsl", false);
//SHAssetManager::CompileAsset("../../Assets/Shaders/ShadowMap_FS.glsl", false); //SHAssetManager::CompileAsset("../../Assets/Shaders/ShadowMap_FS.glsl", false);
//SHAssetManager::CompileAsset("../../Assets/Shaders/SSAO_CS.glsl", false); //SHAssetManager::CompileAsset("../../Assets/Shaders/SSAO_CS.glsl", false);
@ -865,12 +865,13 @@ namespace SHADE
// we need to wait for the device to finish using the graph first // we need to wait for the device to finish using the graph first
device->WaitIdle(); device->WaitIdle();
auto const& EVENT_DATA = reinterpret_cast<const SHEventSpec<SHLightEnableShadowEvent>*>(eventPtr.get())->data; auto const& EVENT_DATA = reinterpret_cast<const SHEventSpec<SHLightEnableShadowEvent>*>(eventPtr.get())->data;
auto* lightComp = SHComponentManager::GetComponent<SHLightComponent>(EVENT_DATA->lightEntity); auto* lightComp = SHComponentManager::GetComponent<SHLightComponent>(EVENT_DATA->lightEntity);
std::string depthResourceName = "ShadowMap_Depth " + std::to_string(EVENT_DATA->lightEntity); std::string depthResourceName = "ShadowMap_Depth " + std::to_string(EVENT_DATA->lightEntity);
std::string shadowMapResourceName = "ShadowMap " + std::to_string(EVENT_DATA->lightEntity); std::string shadowMapResourceName = "ShadowMap " + std::to_string(EVENT_DATA->lightEntity);
std::string shadowMapBlurredResourceName = "ShadowMap Blurred" + std::to_string(EVENT_DATA->lightEntity); std::string shadowMapBlurredResourceName = "ShadowMap Blurred" + std::to_string(EVENT_DATA->lightEntity);
Handle<SHSubpass> companionSubpass = renderGraph->GetNode(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_PASS.data())->GetSubpass(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_WRITE_SUBPASS); Handle<SHSubpass> gBufferWriteSubpass = renderGraph->GetNode(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_PASS.data())->GetSubpass(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_WRITE_SUBPASS);
Handle<SHSubpass> gBufferWriteVfxSubpass = renderGraph->GetNode(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_PASS.data())->GetSubpass(SHGraphicsConstants::RenderGraphEntityNames::GBUFFER_WRITE_VFX_SUBPASS);
if (EVENT_DATA->generateRenderer) if (EVENT_DATA->generateRenderer)
{ {
@ -929,7 +930,8 @@ namespace SHADE
); );
shadowMapPipeline = tempLibrary.GetGraphicsPipeline({ shadowMapVS, shadowMapFS, shadowMapDrawSubpass }); shadowMapPipeline = tempLibrary.GetGraphicsPipeline({ shadowMapVS, shadowMapFS, shadowMapDrawSubpass });
} }
shadowMapDrawSubpass->SetCompanionSubpass(companionSubpass, shadowMapPipeline); // set companion subpass and pipeline shadowMapDrawSubpass->AddCompanionSubpass(gBufferWriteSubpass, shadowMapPipeline); // set companion subpass and pipeline
shadowMapDrawSubpass->AddCompanionSubpass(gBufferWriteVfxSubpass, shadowMapPipeline); // set companion subpass and pipeline
// add the shadow map and the blurred version to the lighting system // add the shadow map and the blurred version to the lighting system
uint32_t const NEW_SHADOW_MAP_INDEX = lightingSubSystem->AddShadowMap(renderGraph->GetRenderGraphResource(shadowMapBlurredResourceName), EVENT_DATA->lightEntity); uint32_t const NEW_SHADOW_MAP_INDEX = lightingSubSystem->AddShadowMap(renderGraph->GetRenderGraphResource(shadowMapBlurredResourceName), EVENT_DATA->lightEntity);

View File

@ -77,7 +77,7 @@ namespace SHADE
, name { rhs.name } , name { rhs.name }
, viewport {rhs.viewport} , viewport {rhs.viewport}
, renderer {rhs.renderer} , renderer {rhs.renderer}
, companionSubpass {rhs.companionSubpass} , companionSubpasses {std::move (rhs.companionSubpasses)}
, dummyPipelineLayout{rhs.dummyPipelineLayout} , dummyPipelineLayout{rhs.dummyPipelineLayout}
{ {
@ -115,7 +115,7 @@ namespace SHADE
name = std::move(rhs.name); name = std::move(rhs.name);
renderer = rhs.renderer; renderer = rhs.renderer;
viewport = rhs.viewport; viewport = rhs.viewport;
companionSubpass = rhs.companionSubpass; companionSubpasses = rhs.companionSubpasses;
dummyPipelineLayout = rhs.dummyPipelineLayout; dummyPipelineLayout = rhs.dummyPipelineLayout;
@ -237,17 +237,19 @@ namespace SHADE
BindInputDescriptorSets (commandBuffer, descMappings.at(SHPredefinedDescriptorTypes::RENDER_GRAPH_RESOURCE), frameIndex); BindInputDescriptorSets (commandBuffer, descMappings.at(SHPredefinedDescriptorTypes::RENDER_GRAPH_RESOURCE), frameIndex);
// If companion subpass is not a valid handle, render super batch normally if (companionSubpasses.empty())
if (!companionSubpass.companion)
{ {
// Draw all the batches // Draw all the batches
superBatch->Draw(commandBuffer, frameIndex); superBatch->Draw(commandBuffer, frameIndex);
} }
else else
{ {
// if not bind pipeline for companion and and execute draw command for (auto& companion : companionSubpasses)
commandBuffer->BindPipeline(companionSubpass.pipeline); {
companionSubpass.companion->superBatch->Draw(commandBuffer, frameIndex, false); // if not bind pipeline for companion and and execute draw command
commandBuffer->BindPipeline(companion.pipeline);
companion.subpass->superBatch->Draw(commandBuffer, frameIndex, false);
}
} }
} }
@ -493,10 +495,11 @@ namespace SHADE
subpassIndex = index; subpassIndex = index;
} }
void SHSubpass::SetCompanionSubpass(Handle<SHSubpass> companion, Handle<SHVkPipeline> pipeline) noexcept void SHSubpass::AddCompanionSubpass(Handle<SHSubpass> companion, Handle<SHVkPipeline> pipeline) noexcept
{ {
companionSubpass.companion = companion; companionSubpasses.push_back(CompanionSubpass{companion, pipeline});
companionSubpass.pipeline = pipeline; //companionSubpass.companion = companion;
//companionSubpass.pipeline = pipeline;
} }
/***************************************************************************/ /***************************************************************************/

View File

@ -33,7 +33,7 @@ namespace SHADE
struct CompanionSubpass struct CompanionSubpass
{ {
// subpass whose data will be borrowed to draw // subpass whose data will be borrowed to draw
Handle<SHSubpass> companion; Handle<SHSubpass> subpass;
// Pipeline that will be used for all the draw calls from all batches of the companion subpass // Pipeline that will be used for all the draw calls from all batches of the companion subpass
Handle<SHVkPipeline> pipeline; Handle<SHVkPipeline> pipeline;
@ -114,7 +114,8 @@ namespace SHADE
//! Optional component to a companion subpass. If the subpass handle of this object //! Optional component to a companion subpass. If the subpass handle of this object
//! is valid, the subpass will be drawn using this companion's data. //! is valid, the subpass will be drawn using this companion's data.
CompanionSubpass companionSubpass; //CompanionSubpass companionSubpass;
std::vector<CompanionSubpass> companionSubpasses;
private: private:
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
@ -165,7 +166,7 @@ namespace SHADE
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* PUBLIC SETTERS AND GETTERS */ /* PUBLIC SETTERS AND GETTERS */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
void SetCompanionSubpass (Handle<SHSubpass> companion, Handle<SHVkPipeline> pipeline) noexcept; void AddCompanionSubpass (Handle<SHSubpass> companion, Handle<SHVkPipeline> pipeline) noexcept;
Handle<SHRenderGraphNode> GetParentNode(void) const noexcept; Handle<SHRenderGraphNode> GetParentNode(void) const noexcept;
SHSubPassIndex GetIndex() const noexcept; SHSubPassIndex GetIndex() const noexcept;