Implemented improved shadows (still abit whack), trajectory system (untested), and window resize event #352

Merged
Xenosas1337 merged 7 commits from SP3-1-Rendering into main 2023-02-20 14:35:25 +08:00
11 changed files with 491 additions and 13 deletions
Showing only changes of commit cee45863fa - Show all commits

View File

@ -0,0 +1,22 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
#extension GL_EXT_nonuniform_qualifier : require
layout(location = 0) in struct
{
vec4 vertPos; // location 0
vec2 uv; // location = 1
vec4 color; // location = 2
} In;
layout(location = 0) out vec4 fragColor;
void main()
{
// default red first
fragColor = In.color;
}

View File

@ -0,0 +1,36 @@
#version 450
#extension GL_KHR_vulkan_glsl : enable
// vertex inputs
layout(location = 0) in vec4 aPos;
layout(location = 1) in vec2 aUV;
layout(location = 2) in vec2 aColor;
layout(location = 3) in mat4 aTransform;
// between shader stages
layout(location = 0) out struct
{
vec4 vertPos; // location 0
vec2 uv; // location = 1
vec4 color; // location = 2
} Out;
// Camera data
layout(set = 1, binding = 0) uniform CameraData
{
vec4 position;
mat4 vpMat;
mat4 viewMat;
mat4 projMat;
} cameraData;
void main()
{
Out.uv = aUV;
Out.color = aColor;
gl_Position = cameraData.projMat * aTransform * vec4(aPos, 1.0f);
}

View File

@ -52,6 +52,12 @@ namespace SHADE
{SHPredefinedDescriptorTypes::RENDER_GRAPH_RESOURCE, 3},
{SHPredefinedDescriptorTypes::RENDER_GRAPH_NODE_COMPUTE_RESOURCE, 4},
});
perSystemData[SHUtilities::ConvertEnum(SystemType::TRAJECTORY_RENDERING)].descMappings.AddMappings
({
{SHPredefinedDescriptorTypes::STATIC_DATA, 0},
{SHPredefinedDescriptorTypes::CAMERA, 1},
});
}
void SHGraphicsPredefinedData::InitDummyPipelineLayouts(Handle<SHVkLogicalDevice> logicalDevice) noexcept
@ -222,6 +228,12 @@ namespace SHADE
SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::CAMERA |
SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::LIGHTS
);
perSystemData[SHUtilities::ConvertEnum(SystemType::TRAJECTORY_RENDERING)].descSetLayouts = GetPredefinedDescSetLayouts
(
SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::STATIC_DATA |
SHGraphicsPredefinedData::PredefinedDescSetLayoutTypes::CAMERA
);
}
void SHGraphicsPredefinedData::InitPredefinedVertexInputState(void) noexcept

View File

@ -38,6 +38,7 @@ namespace SHADE
BATCHING_ANIM,
TEXT_RENDERING,
RENDER_GRAPH_NODE_COMPUTE,
TRAJECTORY_RENDERING,
NUM_TYPES
};
static constexpr int SYSTEM_TYPE_COUNT = static_cast<int>(SystemType::NUM_TYPES);

View File

@ -282,6 +282,13 @@ namespace SHADE
*/
/***************************************************************************/
static constexpr uint32_t BONE_MATRIX_FIRST_INDEX = 8;
/***************************************************************************/
/*!
\brief
Vertex buffer bindings for color
*/
/***************************************************************************/
static constexpr uint32_t TRAJECTORY_COLOR = 2;
static constexpr uint32_t CALCULATED_GLYPH_POSITION = 0;
static constexpr uint32_t GLYPH_INDEX = 1;

View File

@ -433,6 +433,11 @@ namespace SHADE
auto uiNode = renderGraph->GetNode(SHGraphicsConstants::RenderGraphEntityNames::SCREEN_SPACE_PASS.data());
textRenderingSubSystem->Init(device, uiNode->GetRenderpass(), uiNode->GetSubpass(SHGraphicsConstants::RenderGraphEntityNames::UI_SUBPASS), descPool, textVS, textFS);
trajectoryRenderingSubSystem = resourceManager.Create<SHTrajectoryRenderingSubSystem>();
//auto gBufferNode = renderGraph->GetNode(SHGraphicsConstants::RenderGraphEntityNames::SCREEN_SPACE_PASS.data());
//trajectoryRenderingSubSystem->Init(device, )
SHGlobalDescriptorSets::SetLightingSubSystem(lightingSubSystem);
}
@ -589,19 +594,19 @@ namespace SHADE
static bool shadowAdded = false;
if (shadowAdded == false && SHInputManager::GetKey(SHInputManager::SH_KEYCODE::B))
{
shadowAdded = true;
auto& lightComps = SHComponentManager::GetDense<SHLightComponent>();
//if (lightComps.size() > 2)
//{
// lightComps[2].SetEnableShadow(true);
//}
for (auto& comp : lightComps)
{
comp.SetEnableShadow(true);
}
}
//if (shadowAdded == false && SHInputManager::GetKey(SHInputManager::SH_KEYCODE::B))
//{
// shadowAdded = true;
// auto& lightComps = SHComponentManager::GetDense<SHLightComponent>();
// //if (lightComps.size() > 2)
// //{
// // lightComps[2].SetEnableShadow(true);
// //}
// for (auto& comp : lightComps)
// {
// comp.SetEnableShadow(true);
// }
//}
renderGraph->Begin(frameIndex);
auto cmdBuffer = renderGraph->GetCommandBuffer(frameIndex);

View File

@ -36,6 +36,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/MiddleEnd/TextRendering/SHFontLibrary.h"
#include "Graphics/MiddleEnd/Interface/SHRenderer.h"
#include "Graphics/Events/SHGraphicsEvents.h"
#include "Graphics/MiddleEnd/TrajectoryRendering/SHTrajectoryRenderingSubSystem.h"
namespace SHADE
{
@ -505,6 +506,7 @@ namespace SHADE
Handle<SHPostOffscreenRenderSystem> postOffscreenRenderSubSystem;
Handle<SHLightingSubSystem> lightingSubSystem;
Handle<SHTextRenderingSubSystem> textRenderingSubSystem;
Handle<SHTrajectoryRenderingSubSystem> trajectoryRenderingSubSystem;
Handle<SHSSAO> ssaoStorage;
uint32_t resizeWidth = 1;

View File

@ -0,0 +1,91 @@
#include "SHpch.h"
#include "SHTrajectoryRenderableComponent.h"
namespace SHADE
{
/***************************************************************************/
/*!
\brief
On create the text has nothing.
*/
/***************************************************************************/
void SHTrajectoryRenderableComponent::OnCreate(void)
{
}
void SHTrajectoryRenderableComponent::OnDestroy(void)
{
}
void SHTrajectoryRenderableComponent::ClearPositions(void) noexcept
{
positions.clear();
}
bool SHTrajectoryRenderableComponent::HasPositions(void) const noexcept
{
return !positions.empty();
}
std::vector<SHVec3> SHTrajectoryRenderableComponent::GetPositions(void) const noexcept
{
return positions;
}
Handle<SHMesh> SHTrajectoryRenderableComponent::GetMesh(void) const noexcept
{
return mesh;
}
SHVec4 const& SHTrajectoryRenderableComponent::GetStartColor(void) const noexcept
{
return startColor;
}
SHVec4 const& SHTrajectoryRenderableComponent::GetEndColor(void) const noexcept
{
return endColor;
}
float SHTrajectoryRenderableComponent::GetColorEvolveRate(void) const noexcept
{
return colorEvolveRate;
}
void SHTrajectoryRenderableComponent::SetMesh(Handle<SHMesh> newMesh) noexcept
{
mesh = newMesh;
}
void SHTrajectoryRenderableComponent::SetStartColor(SHVec4 color) noexcept
{
startColor = color;
}
void SHTrajectoryRenderableComponent::SetEndColor(SHVec4 color) noexcept
{
endColor = color;
}
void SHTrajectoryRenderableComponent::SetColorEvolveRate(float rate) noexcept
{
colorEvolveRate = rate;
}
}
namespace rttr
{
RTTR_REGISTRATION
{
using namespace SHADE;
registration::class_<SHTrajectoryRenderableComponent>("Trajectory Renderer Component");
};
}

View File

@ -0,0 +1,57 @@
#pragma once
#include "Math/Vector/SHVec3.h"
#include "Math/Vector/SHVec4.h"
#include "Resource/SHHandle.h"
#include "ECS_Base/Components/SHComponent.h"
#include <rttr/registration>
namespace SHADE
{
class SHMesh;
class SHTrajectoryRenderableComponent : public SHComponent
{
private:
//! Mesh used to render the trajectory
Handle<SHMesh> mesh;
//! positions to plot for rendering. Will be cleared every frame.
std::vector<SHVec3> positions;
//! Starting color of the trajectory
SHVec4 startColor;
//! Color the trajectory should evolve to the longer it is
SHVec4 endColor;
//! evolving rate of the color (with respect to dt)
float colorEvolveRate;
public:
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/
void SetMesh(Handle<SHMesh> newMesh) noexcept;
void SetStartColor(SHVec4 startColor) noexcept;
void SetEndColor (SHVec4 endColor) noexcept;
void SetColorEvolveRate (float rate) noexcept;
std::vector<SHVec3> GetPositions (void) const noexcept;
Handle<SHMesh> GetMesh (void) const noexcept;
SHVec4 const& GetStartColor (void) const noexcept;
SHVec4 const& GetEndColor (void) const noexcept;
float GetColorEvolveRate (void) const noexcept;
void OnCreate(void) override final;
void OnDestroy(void) override final;
void ClearPositions(void) noexcept;
bool HasPositions(void) const noexcept;
RTTR_ENABLE()
};
}

View File

@ -0,0 +1,179 @@
#include "SHpch.h"
#include "SHTrajectoryRenderingSubSystem.h"
#include "ECS_Base/Managers/SHComponentManager.h"
#include "Graphics/MiddleEnd/TrajectoryRendering/SHTrajectoryRenderableComponent.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Math/Transform/SHTransformComponent.h"
#include "Graphics\MiddleEnd\Interface\SHMeshLibrary.h"
#include "Graphics/SHVkUtil.h"
#include "Graphics/MiddleEnd/GlobalData/SHGraphicsPredefinedData.h"
#include "Graphics/Pipeline/SHVkPipeline.h"
#include "Graphics/RenderGraph/SHSubpass.h"
#include "Graphics/MiddleEnd/GlobalData/SHGlobalDescriptorSets.h"
#include "Graphics/MiddleEnd/Interface/SHRenderer.h"
namespace SHADE
{
void SHTrajectoryRenderingSubSystem::Init(Handle<SHVkLogicalDevice> device, Handle<SHVkRenderpass> compatibleRenderpass, Handle<SHSubpass> subpass, Handle<SHVkShaderModule> trajectoryVS, Handle<SHVkShaderModule> trajectoryFS) noexcept
{
logicalDevice = device;
SHComponentManager::CreateComponentSparseSet<SHTrajectoryRenderableComponent>();
// prepare pipeline layout params
SHPipelineLayoutParams plParams
{
.shaderModules = {trajectoryVS, trajectoryFS},
.predefinedDescSetLayouts = SHGraphicsPredefinedData::GetSystemData(SHGraphicsPredefinedData::SystemType::TRAJECTORY_RENDERING).descSetLayouts
};
pipelineLayout = logicalDevice->CreatePipelineLayout(plParams);
// Create pipeline
pipeline = logicalDevice->CreateGraphicsPipeline(pipelineLayout, nullptr, compatibleRenderpass, subpass);
// vertex input state of the pipeline
SHVertexInputState vertexInputState;
vertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_3D) }); // Attribute positions at binding 0
vertexInputState.AddBinding(false, false, { SHVertexAttribute(SHAttribFormat::FLOAT_2D) }); // Attribute uv at binding 1
vertexInputState.AddBinding(true, true, { SHVertexAttribute(SHAttribFormat::FLOAT_4D) }); // Instanced attribute color at binding 2
vertexInputState.AddBinding(true, true, { SHVertexAttribute(SHAttribFormat::MAT_4D) }); // Instanced Transform at binding 3 - 6 (4 slots)
pipeline->GetPipelineState().SetVertexInputState(vertexInputState);
SHColorBlendState colorBlendState{};
colorBlendState.logic_op_enable = VK_FALSE;
colorBlendState.logic_op = vk::LogicOp::eCopy;
auto const& subpassColorReferences = subpass->GetColorAttachmentReferences();
colorBlendState.attachments.reserve(static_cast<uint32_t>(subpassColorReferences.size()));
for (auto& att : subpassColorReferences)
{
colorBlendState.attachments.push_back(vk::PipelineColorBlendAttachmentState
{
.blendEnable = SHVkUtil::IsBlendCompatible(subpass->GetFormatFromAttachmentReference(att.attachment)) ? true : false,
.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha,
.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha,
.colorBlendOp = vk::BlendOp::eAdd,
.srcAlphaBlendFactor = vk::BlendFactor::eOne,
.dstAlphaBlendFactor = vk::BlendFactor::eZero,
.alphaBlendOp = vk::BlendOp::eAdd,
.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA,
}
);
}
pipeline->GetPipelineState().SetColorBlenState(colorBlendState);
}
void SHTrajectoryRenderingSubSystem::Run(uint32_t frameIndex, float dt) noexcept
{
auto& comps = SHComponentManager::GetDense<SHTrajectoryRenderableComponent>();
for (auto& comp : comps)
{
// If has positions, feed data to buffer.
if (comp.HasPositions())
{
SHTransformComponent* transform = SHComponentManager::GetComponent_s<SHTransformComponent>(comp.GetEID());
if (transform)
{
// convenient variable
SHVec4 const& startColor = comp.GetStartColor();
SHVec4 const& endColor = comp.GetEndColor();
float colorEvolveRate = comp.GetColorEvolveRate();
// trs to be reused
SHMatrix trs = transform->GetTRS();
// starting color of trajectory
SHVec4 currentColor = comp.GetStartColor();
// Start from 0 and slowly evolve to 1
float lerpValue = 0.0f;
// Will be used for baseInstance later
uint32_t oldTransformDataSize = transformData.size();
auto meshHandle = comp.GetMesh();
auto const& positions = comp.GetPositions();
for (auto& pos : positions)
{
// modify position and reuse matrix
trs.m[3][0] = pos.x;
trs.m[3][1] = pos.y;
trs.m[3][2] = pos.z;
transformData.push_back(trs);
colorData.push_back(currentColor);
// evolve color
currentColor = SHVec4::Lerp(startColor, endColor, lerpValue);
// evolve lerp value and clamp to 1
lerpValue = std::max (1.0f, lerpValue + (colorEvolveRate * dt));
}
// add draw data for this trajectory
drawData.push_back(vk::DrawIndexedIndirectCommand
{
.indexCount = meshHandle->IndexCount,
.instanceCount = static_cast<uint32_t>(transformData.size()) - oldTransformDataSize,
.firstIndex = meshHandle->FirstIndex,
.vertexOffset = meshHandle->FirstVertex,
.firstInstance = oldTransformDataSize
});
}
}
// clear at the end of every frame since data is already in buffers
comp.ClearPositions();
}
// read transform data to buffer
// read draw data to buffer
SHVkUtil::EnsureBufferAndCopyHostVisibleData(logicalDevice, transformBuffer, transformData.data(), sizeof (SHMatrix) * transformData.size(), vk::BufferUsageFlagBits::eVertexBuffer, "Trajectory System Transform Buffer");
SHVkUtil::EnsureBufferAndCopyHostVisibleData(logicalDevice, drawDataBuffer, drawData.data(), sizeof(vk::DrawIndexedIndirectCommand) * drawData.size(), vk::BufferUsageFlagBits::eIndirectBuffer, "Trajectory System Draw Data Buffer");
SHVkUtil::EnsureBufferAndCopyHostVisibleData(logicalDevice, colorBuffer, colorData.data(), sizeof(SHVec4) * colorData.size(), vk::BufferUsageFlagBits::eVertexBuffer, "Trajectory System Color Data Buffer");
}
void SHTrajectoryRenderingSubSystem::Render(Handle<SHVkCommandBuffer> cmdBuffer, Handle<SHRenderer> renderer, uint32_t frameIndex) noexcept
{
auto const& mappings = SHGraphicsPredefinedData::GetMappings(SHGraphicsPredefinedData::SystemType::TRAJECTORY_RENDERING);
uint32_t staticGlobalSetIndex = mappings.at(SHPredefinedDescriptorTypes::STATIC_DATA);
uint32_t cameraSetIndex = mappings.at(SHPredefinedDescriptorTypes::CAMERA);
cmdBuffer->BindPipeline(pipeline);
// Bind global data
SHGlobalDescriptorSets::BindStaticGlobalData(cmdBuffer, SH_PIPELINE_TYPE::GRAPHICS, staticGlobalSetIndex);
// Bind camera data
renderer->BindDescriptorSet(cmdBuffer, SH_PIPELINE_TYPE::GRAPHICS, cameraSetIndex, frameIndex);
// Bind color vertex buffer
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRAJECTORY_COLOR, colorBuffer, 0);
// call draw call
cmdBuffer->DrawMultiIndirect(drawDataBuffer, drawData.size());
// clear CPU transform and draw data
transformData.clear();
drawData.clear();
colorData.clear();
}
void SHTrajectoryRenderingSubSystem::Exit(void) noexcept
{
}
}

View File

@ -0,0 +1,66 @@
#pragma once
#include "Resource/SHHandle.h"
#include "Graphics/Pipeline/SHPipelineState.h"
#include "Math/SHMatrix.h"
namespace SHADE
{
class SHVkLogicalDevice;
class SHVkDescriptorPool;
class SHVkDescriptorSetGroup;
class SHVkDescriptorSetLayout;
class SHVkBuffer;
class SHLightComponent;
class SHVkCommandBuffer;
class SHVkPipeline;
class SHVkPipelineLayout;
class SHVkRenderpass;
class SHSubpass;
class SHVkShaderModule;
class SHRenderer;
class SHTrajectoryRenderingSubSystem
{
private:
Handle<SHVkLogicalDevice> logicalDevice;
//! Every trajectory renderable will have one of these
std::vector<vk::DrawIndexedIndirectCommand> drawData;
//! For the MDI call
Handle<SHVkBuffer> drawDataBuffer;
//! matrix data to copy into buffer
std::vector<SHMatrix> transformData;
//! All trajectory renderables will use this transform buffer
Handle<SHVkBuffer> transformBuffer;
//! Each object will have their own color data
std::vector<SHVec4> colorData;
//! buffer to hold color data for objects
Handle<SHVkBuffer> colorBuffer;
//! Pipeline for rendering the trajectories
Handle<SHVkPipeline> pipeline;
//! Pipeline layout for the pipeline
Handle<SHVkPipelineLayout> pipelineLayout;
public:
void Init (Handle<SHVkLogicalDevice> device, Handle<SHVkRenderpass> compatibleRenderpass, Handle<SHSubpass> subpass, Handle<SHVkShaderModule> textVS, Handle<SHVkShaderModule> textFS) noexcept;
void Run(uint32_t frameIndex, float dt) noexcept;
void Render(Handle<SHVkCommandBuffer> cmdBuffer, Handle<SHRenderer> renderer, uint32_t frameIndex) noexcept;
void Exit(void) noexcept;
};
}