Merge branch 'main' into SP3-13-Assets-Manager

This commit is contained in:
Xiao Qi 2022-09-24 12:40:31 +08:00
commit cf4f905d91
78 changed files with 2082 additions and 683 deletions

View File

@ -22,8 +22,10 @@
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Scene/SHSceneManager.h"
#include "Math/Transform/SHTransformSystem.h"
#include "Scenes/SBTestScene.h"
#include "Math/Transform/SHTransformComponent.h"
using namespace SHADE;
@ -43,28 +45,44 @@ namespace Sandbox
SDL_Init(SDL_INIT_EVERYTHING);
window.Create(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
// Create Systems
SHADE::SHSystemManager::CreateSystem<SHADE::SHScriptEngine>();
// TODO(Diren): Create Physics System here
SHADE::SHSystemManager::CreateSystem<SHADE::SHTransformSystem>();
SHADE::SHSystemManager::CreateSystem<SHADE::SHGraphicsSystem>();
SHADE::SHGraphicsSystem* graphicsSystem = static_cast<SHADE::SHGraphicsSystem*>(SHADE::SHSystemManager::GetSystem<SHADE::SHGraphicsSystem>());
// Create Routines
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHScriptEngine, SHADE::SHScriptEngine::FrameSetUpRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHScriptEngine, SHADE::SHScriptEngine::UpdateRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHScriptEngine, SHADE::SHScriptEngine::LateUpdateRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHScriptEngine, SHADE::SHScriptEngine::FrameCleanUpRoutine>();
// TODO(Diren): Register Physics System & Routines here
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHTransformSystem, SHADE::SHTransformSystem::TransformUpdateRoutine>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHTransformComponent>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::BatcherDispatcherRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::BeginRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::RenderRoutine>();
SHADE::SHSystemManager::RegisterRoutine<SHADE::SHGraphicsSystem, SHADE::SHGraphicsSystem::EndRoutine>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHRenderable>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHRenderable>();
SHADE::SHComponentManager::CreateComponentSparseSet<SHADE::SHTransformComponent>();
// Set up graphics system and windows
graphicsSystem->SetWindow(&window);
sdlWindow = SDL_CreateWindowFrom(window.GetHWND());
//auto [w, h] = window.GetWindowSize();
//SDL_SetWindowSize(sdlWindow, w, h);
SHADE::SHSystemManager::Init();
#ifdef SHEDITOR
#ifdef SHEDITOR
SHADE::SHEditor::Initialise(sdlWindow);
#else
#endif
// Set up scripting
SHADE::SHScriptEngine::Init();
#else
#endif
SHSceneManager::InitSceneManager<SBTestScene>("TestScene");
}
@ -99,11 +117,10 @@ namespace Sandbox
void SBApplication::Exit(void)
{
#ifdef SHEDITOR
#ifdef SHEDITOR
SHADE::SHEditor::Exit();
#endif
SHSceneManager::Exit();
SHADE::SHScriptEngine::Exit();
SHADE::SHSystemManager::Exit();
SDL_DestroyWindow(sdlWindow);
SDL_Quit();

View File

@ -7,6 +7,8 @@
#include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Scene/SHSceneManager.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsSystem.h"
#include "Scripting/SHScriptEngine.h"
#include "Math/Transform/SHTransformComponent.h"
using namespace SHADE;
@ -36,17 +38,56 @@ namespace Sandbox
// Create Materials
auto matInst = graphicsSystem->AddMaterialInstance();
// Create entity and add mesh
auto entity = SHADE::SHEntityManager::CreateEntity<SHADE::SHRenderable>();
auto& renderable = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHRenderable>(entity);
renderable.Mesh = CUBE_MESH;
renderable.SetMaterial(matInst);
renderable.TransformMatrix.Translate(0.0f, 0.0f, 2.0f);
// Create Stress Test Objects
static const SHVec3 TEST_OBJ_SCALE = { 0.2f, 0.2f, 0.2f };
constexpr int NUM_ROWS = 200;
constexpr int NUM_COLS = 100;
static const SHVec3 TEST_OBJ_SPACING = { 1.0f, 1.0f, 1.0f };
static const SHVec3 TEST_OBJ_START_POS = { - (NUM_COLS / 2 * TEST_OBJ_SPACING.x ), 0.0f, 0.0f };
for (int z = 0; z < NUM_ROWS; ++z)
for (int x = 0; x < NUM_COLS; ++x)
{
auto entity = SHEntityManager::CreateEntity<SHRenderable, SHTransformComponent>();
auto& renderable = *SHComponentManager::GetComponent_s<SHRenderable>(entity);
auto& transform = *SHComponentManager::GetComponent_s<SHTransformComponent>(entity);
renderable.Mesh = CUBE_MESH;
renderable.SetMaterial(matInst);
// Set initial positions
transform.SetWorldPosition(TEST_OBJ_START_POS + SHVec3{ x * TEST_OBJ_SPACING.x, 0.0f, z * TEST_OBJ_SPACING.z });
//transform.SetLocalScale(TEST_OBJ_SCALE);
stressTestObjects.emplace_back(entity);
}
// Create blank entity with a script
testObj = SHADE::SHEntityManager::CreateEntity();
SHADE::SHScriptEngine* scriptEngine = static_cast<SHADE::SHScriptEngine*>(SHADE::SHSystemManager::GetSystem<SHADE::SHScriptEngine>());
scriptEngine->AddScript(testObj, "TestScript");
}
void SBTestScene::Update(float dt)
{
(void)dt;
/*static float rotation = 0.0f;
auto& transform = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHTransformComponent>(testObj);
transform.SetLocalRotation(rotation, 0.0f, 0.0f);
rotation += dt * 10.0f;*/
/*static float rotation = 0.0f;
auto& transform = *SHADE::SHComponentManager::GetComponent_s<SHADE::SHTransformComponent>(stressTestObjects[0]);
transform.SetWorldPosition({rotation, 0.0f, 0.0f});
rotation += dt * 10.0f;*/
// Destroy entity if space is pressed
if (GetKeyState(VK_SPACE) & 0x8000)
{
SHADE::SHScriptEngine* scriptEngine = static_cast<SHADE::SHScriptEngine*>(SHADE::SHSystemManager::GetSystem<SHADE::SHScriptEngine>());
scriptEngine->RemoveAllScripts(testObj);
}
}
void SBTestScene::Render()

View File

@ -9,7 +9,8 @@ namespace Sandbox
{
private:
EntityID camera;
EntityID testObj;
std::vector<EntityID> stressTestObjects;
public:
virtual void Load();

View File

@ -19,7 +19,7 @@ namespace SHADE
SHEventHandle(T::*callback)(SHEventPtr);
public:
SHEventReceiverSpec(T* obj, void(T::* cb)(SHEventPtr))
SHEventReceiverSpec(T* obj, SHEventHandle(T::* cb)(SHEventPtr))
:SHEventReceiver(), object{ obj }, callback{ cb }
{

View File

@ -49,6 +49,11 @@ namespace SHADE
Init(newSize, data, srcSize, bufferUsageFlags, allocCreateInfo.usage, allocCreateInfo.flags);
}
void SHVkBuffer::FlushAllocation(uint32_t srcOffset, uint32_t dstOffset) noexcept
{
vmaFlushAllocation(vmaAllocator, alloc, srcOffset, dstOffset);
}
vk::Buffer SHVkBuffer::GetVkBuffer(void) const noexcept
{
return vkBuffer;
@ -72,8 +77,7 @@ namespace SHADE
/***************************************************************************/
void SHVkBuffer::Map(void) noexcept
{
if (!boundToCoherent)
vmaMapMemory(vmaAllocator, alloc, &mappedPtr);
vmaMapMemory(vmaAllocator, alloc, &mappedPtr);
}
/***************************************************************************/
@ -90,11 +94,8 @@ namespace SHADE
/***************************************************************************/
void SHVkBuffer::Unmap(void) noexcept
{
if (!boundToCoherent)
{
vmaUnmapMemory(vmaAllocator, alloc);
mappedPtr = nullptr;
}
vmaUnmapMemory(vmaAllocator, alloc);
mappedPtr = nullptr;
}
/***************************************************************************/
@ -132,9 +133,7 @@ namespace SHADE
Otherwise, only the copying is carried out.
In the instance where memory is non-coherent but HOST_VISIBLE, we want to
write to data and then unmap and flush it immediately. If you want to write
to memory in random-access fashion, consider, mapping, writing a few
things, unmapping then flushing.
write to data and then unmap and flush it immediately.
\param vmaAllocator
The VMA allocator object.
@ -155,18 +154,11 @@ namespace SHADE
/***************************************************************************/
void SHVkBuffer::MapWriteUnmap(void* data, uint32_t sizeToWrite, uint32_t srcOffset, uint32_t dstOffset) noexcept
{
if (!boundToCoherent)
{
// map from host visible memory to pointer, do a DMA, and then unmap
Map();
WriteToMemory(data, sizeToWrite, srcOffset, dstOffset);
Unmap();
}
else
{
if (mappedPtr)
std::memcpy(static_cast<uint8_t*>(mappedPtr) + dstOffset, static_cast<uint8_t*>(data) + srcOffset, sizeToWrite);
}
// map from host visible memory to pointer, do a DMA, and then unmap
Map();
WriteToMemory(data, sizeToWrite, srcOffset, dstOffset);
Unmap();
FlushAllocation(srcOffset, dstOffset);
}
/***************************************************************************/
@ -279,7 +271,6 @@ namespace SHADE
, mappedPtr{ nullptr }
, alloc {nullptr}
, randomAccessOptimized{false}
, boundToCoherent {false}
, vmaAllocator{allocator}
{}
@ -304,7 +295,6 @@ namespace SHADE
, mappedPtr{ std::move(rhs.mappedPtr) }
, alloc{ std::move (rhs.alloc) }
, randomAccessOptimized{ rhs.randomAccessOptimized }
, boundToCoherent{ rhs.boundToCoherent}
, vmaAllocator{ rhs.vmaAllocator }
, bufferUsageFlags {rhs.bufferUsageFlags}
, bufferCreateInfo { rhs.bufferCreateInfo }
@ -325,7 +315,6 @@ namespace SHADE
mappedPtr = std::move(rhs.mappedPtr);
alloc = std::move(rhs.alloc);
randomAccessOptimized = rhs.randomAccessOptimized;
boundToCoherent = rhs.boundToCoherent;
vmaAllocator = std::move (rhs.vmaAllocator);
rhs.vkBuffer = VK_NULL_HANDLE;
bufferCreateInfo = rhs.bufferCreateInfo;
@ -430,18 +419,20 @@ namespace SHADE
// mainly host visible. Can be cached (need to flush/invalidate), uncached (always coherent) and coherent (virtual).
if(memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
{
// If memory is marked to be coherent between CPU and GPU (no need flush/invalidate) (TODO: Verify if VMA_ALLOCATION_CREATE_MAPPED_BIT is used when VMA_MEMORY_USAGE_AUTO is set)
// TODO: also verify that coherent bit = pointer is already mapped
if (memPropFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
{
boundToCoherent = true;
const bool CREATE_MAPPED = allocFlags & VMA_ALLOCATION_CREATE_MAPPED_BIT;
if (CREATE_MAPPED)
mappedPtr = allocInfo.pMappedData;
}
else
mappedPtr = nullptr;
if (data)
MapWriteUnmap(data, srcSize, 0, 0);
{
if (CREATE_MAPPED)
WriteToMemory(data, srcSize, 0, 0);
else
MapWriteUnmap(data, srcSize, 0, 0);
}
}
else
{

View File

@ -41,9 +41,6 @@ namespace SHADE
//! If initialized with vma random access flag, this is true
bool randomAccessOptimized;
//! Whether or not this buffer is bound to coherent memory
bool boundToCoherent;
//! buffer usage info flags
vk::BufferUsageFlags bufferUsageFlags;
@ -100,6 +97,7 @@ namespace SHADE
void TransferToDeviceResource(Handle<SHVkCommandBuffer> const& cmdBufferHdl) noexcept;
void ResizeNoCopy (uint32_t newSize);
void ResizeReplace (uint32_t newSize, void* data, uint32_t srcSize);
void FlushAllocation (uint32_t srcOffset, uint32_t dstOffset) noexcept;
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */

View File

@ -8,6 +8,7 @@
#include "Graphics/Framebuffer/SHVkFramebuffer.h"
#include "Graphics/Pipeline/SHVkPipeline.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/Images/SHVkImage.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
@ -421,9 +422,42 @@ namespace SHADE
vkCommandBuffer.drawIndexed(indexCount, 1, firstIndex, vertexOffset, 0);
}
/***************************************************************************/
/*!
\brief
Issues a multi indirect draw call.
void SHVkCommandBuffer::PipelineBarrier (
\param indirectDrawData
SHVkBuffer containing the data for the multi indirect draw call.
\param drawCount
Number of multi indirect draw sub-calls stored in indirectDrawData.
*/
/***************************************************************************/
void SHVkCommandBuffer::DrawMultiIndirect(Handle<SHVkBuffer> indirectDrawData, uint32_t drawCount)
{
if (cmdBufferState != SH_CMD_BUFFER_STATE::RECORDING)
{
SHLOG_ERROR("Command buffer must have started recording before a pipeline can be bound.");
return;
}
vkCommandBuffer.drawIndexedIndirect(indirectDrawData->GetVkBuffer(), 0, drawCount, sizeof(vk::DrawIndexedIndirectCommand));
}
void SHVkCommandBuffer::CopyBufferToImage(const vk::Buffer& src, const vk::Image& dst, const std::vector<vk::BufferImageCopy>& copyInfo)
{
vkCommandBuffer.copyBufferToImage
(
src, dst, vk::ImageLayout::eTransferDstOptimal,
static_cast<uint32_t>(copyInfo.size()), copyInfo.data()
);
}
void SHVkCommandBuffer::PipelineBarrier(
vk::PipelineStageFlags srcStage,
vk::PipelineStageFlags dstStage,
vk::DependencyFlags deps,
@ -457,33 +491,11 @@ namespace SHADE
// //vkCommandBuffer.pipelineBarrier()
//}
/***************************************************************************/
/*!
\brief
Issues a multi indirect draw call.
\param indirectDrawData
SHVkBuffer containing the data for the multi indirect draw call.
\param drawCount
Number of multi indirect draw sub-calls stored in indirectDrawData.
*/
/***************************************************************************/
void SHVkCommandBuffer::DrawMultiIndirect(Handle<SHVkBuffer> indirectDrawData, uint32_t drawCount)
void SHVkCommandBuffer::ForceSetPipelineLayout(Handle<SHVkPipelineLayout> pipelineLayout) noexcept
{
if (cmdBufferState != SH_CMD_BUFFER_STATE::RECORDING)
{
SHLOG_ERROR("Command buffer must have started recording before a pipeline can be bound.");
return;
}
vkCommandBuffer.drawIndexedIndirect(indirectDrawData->GetVkBuffer(), 0, drawCount, sizeof(vk::DrawIndexedIndirectCommand));
boundPipelineLayoutHdl = pipelineLayout;
}
/***************************************************************************/
/*!

View File

@ -15,6 +15,7 @@ namespace SHADE
class SHVkFramebuffer;
class SHVkPipeline;
class SHVkBuffer;
class SHVkImage;
class SHVkDescriptorSetGroup;
enum class SH_CMD_BUFFER_TYPE
@ -116,6 +117,10 @@ namespace SHADE
// Draw Commands
void DrawArrays (uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex, uint32_t firstInstance) const noexcept;
void DrawIndexed (uint32_t indexCount, uint32_t firstIndex, uint32_t vertexOffset) const noexcept;
void DrawMultiIndirect (Handle<SHVkBuffer> indirectDrawData, uint32_t drawCount);
// Buffer Copy
void CopyBufferToImage (const vk::Buffer& src, const vk::Image& dst, const std::vector<vk::BufferImageCopy>& copyInfo);
// memory barriers
void PipelineBarrier (
@ -129,7 +134,6 @@ namespace SHADE
bool IsReadyToSubmit (void) const noexcept;
void HandlePostSubmit (void) noexcept;
void DrawMultiIndirect (Handle<SHVkBuffer> indirectDrawData, uint32_t drawCount);
// Push Constant variable setting
template <typename T>
@ -137,6 +141,7 @@ namespace SHADE
{
memcpy (static_cast<uint8_t*>(pushConstantData) + boundPipelineLayoutHdl->GetPushConstantInterface().GetOffset(variableName), &data, sizeof (T));
};
void ForceSetPipelineLayout (Handle<SHVkPipelineLayout> pipelineLayout) noexcept;
void SubmitPushConstants (void) const noexcept;

View File

@ -20,23 +20,6 @@ namespace SHADE
}
/***************************************************************************/
/*!
\brief
Links the write infos to the vulkan write descriptor sets.
*/
/***************************************************************************/
void SHDescriptorSetUpdater::LinkInfoToWriteDescSet(void) noexcept
{
for (uint32_t i = 0; i < writeInfos.size(); ++i)
{
writeDescSets[i].pImageInfo = writeInfos[i].descImageInfos.data();
writeDescSets[i].pBufferInfo = writeInfos[i].descBufferInfos.data();
writeDescSets[i].pTexelBufferView = writeInfos[i].descTexelBufferInfos.data();
}
}
SHDescriptorWriteInfo& SHDescriptorWriteInfo::operator=(SHDescriptorWriteInfo&& rhs) noexcept
{
@ -65,11 +48,6 @@ namespace SHADE
}
std::vector<vk::WriteDescriptorSet> const& SHDescriptorSetUpdater::GetWriteDescriptorSets(void) const noexcept
{
return writeDescSets;
}
SHDescriptorSetUpdater& SHDescriptorSetUpdater::operator=(SHDescriptorSetUpdater&& rhs) noexcept
{
if (&rhs == this)

View File

@ -41,10 +41,6 @@ namespace SHADE
//! When we want to update a write, we need to use this to identify the index of the write.
std::unordered_map<BindingAndSetHash, uint32_t> writeHashMap;
//! We keep this here because we want this to be immediately passable to vkUpdateDescriptorSets
std::vector<vk::WriteDescriptorSet> writeDescSets;
void LinkInfoToWriteDescSet(void) noexcept;
public:
SHDescriptorSetUpdater (void) noexcept;
@ -52,8 +48,6 @@ namespace SHADE
SHDescriptorSetUpdater& operator= (SHDescriptorSetUpdater&& rhs) noexcept;
public:
std::vector<vk::WriteDescriptorSet> const& GetWriteDescriptorSets (void) const noexcept;
friend class SHVkDescriptorSetGroup;
};
}

View File

@ -50,9 +50,8 @@ namespace SHADE
return *this;
}
std::vector<Handle<SHVkDescriptorSetGroup>> SHVkDescriptorPool::Allocate(const std::vector<Handle<SHVkDescriptorSetLayout>>& layouts, std::vector<uint32_t> const& variableDescCounts)
Handle<SHVkDescriptorSetGroup> SHVkDescriptorPool::Allocate(const std::vector<Handle<SHVkDescriptorSetLayout>>& layouts, std::vector<uint32_t> const& variableDescCounts)
{
SHVkInstance::GetResourceManager().Create<SHVkDescriptorSetGroup>(device, GetHandle(), layouts, variableDescCounts);
return {};
return SHVkInstance::GetResourceManager().Create<SHVkDescriptorSetGroup>(device, GetHandle(), layouts, variableDescCounts);
}
}

View File

@ -101,7 +101,7 @@ namespace SHADE
/// Handles to the created Descriptor Sets. If this DescriptorPool has run out of
/// space, lesser number of Handles will be returned.
/// </returns>
std::vector<Handle<SHVkDescriptorSetGroup>> Allocate(const std::vector<Handle<SHVkDescriptorSetLayout>>& layouts, std::vector<uint32_t> const& variableDescCounts);
Handle<SHVkDescriptorSetGroup> Allocate(const std::vector<Handle<SHVkDescriptorSetLayout>>& layouts, std::vector<uint32_t> const& variableDescCounts);
private:
/*-----------------------------------------------------------------------------*/

View File

@ -7,6 +7,11 @@
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Graphics/Descriptors/SHVkDescriptorSetLayout.h"
#include "Tools/SHLogger.h"
#include "Graphics/Images/SHVkImage.h"
#include "Graphics/Images/SHVkImageView.h"
#include "Graphics/Images/SHVkSampler.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/SHVkUtil.h"
namespace SHADE
{
@ -39,6 +44,7 @@ namespace SHADE
: device{deviceHdl}
, descPool {pool}
, descSets{}
, layoutsUsed {layouts}
{
// Create the layout for each concurrent frame
std::vector<vk::DescriptorSetLayout> vkLayouts{ layouts.size() };
@ -47,6 +53,7 @@ namespace SHADE
for (uint32_t i = 0; i < layouts.size(); ++i)
{
vkLayouts[i] = layouts[i]->GetVkHandle();
setIndexing.emplace(layouts[i]->GetSetIndex(), i);
}
// Check for variable descriptor count
@ -73,34 +80,22 @@ namespace SHADE
// allocate descriptor sets
descSets = device->GetVkLogicalDevice().allocateDescriptorSets(DESC_SET_LAYOUT_CREATE_INFO);
// Now we want to prepare the write descriptor sets for writing later.
// Now we want to prepare the write descriptor sets info for writing later.
for (uint32_t i = 0; i < layouts.size(); ++i)
{
auto const& bindings = layouts[i]->GetBindings();
for (auto& binding : bindings)
{
BindingAndSetHash writeHash = binding.BindPoint;
writeHash |= static_cast<uint64_t>(i) << 32;
writeHash |= static_cast<uint64_t>(layouts[i]->GetSetIndex()) << 32;
// new write for the binding
updater.writeInfos.emplace_back();
updater.writeHashMap.try_emplace(writeHash, updater.writeInfos.size() - 1);
auto& writeInfo = updater.writeInfos.back();
updater.writeDescSets.emplace_back();
auto& writeDescSet = updater.writeDescSets.back();
// Initialize info for write
writeDescSet.descriptorType = binding.Type;
writeDescSet.dstArrayElement = 0;
writeDescSet.dstSet = descSets[i];
writeDescSet.dstBinding = binding.BindPoint;
// Descriptor count for the write descriptor set. Usually this is set to 1, but if binding is variable sized, set to info passed in
uint32_t descriptorCount = (binding.flags & vk::DescriptorBindingFlagBits::eVariableDescriptorCount) ? variableDescCounts[i] : 1;
writeDescSet.descriptorCount = descriptorCount;
switch (binding.Type)
{
@ -114,8 +109,10 @@ namespace SHADE
case vk::DescriptorType::eUniformTexelBuffer:
case vk::DescriptorType::eStorageTexelBuffer:
case vk::DescriptorType::eUniformBuffer:
case vk::DescriptorType::eUniformBufferDynamic:
case vk::DescriptorType::eStorageBuffer:
writeInfo.descImageInfos.resize (descriptorCount);
case vk::DescriptorType::eStorageBufferDynamic:
writeInfo.descBufferInfos.resize (descriptorCount);
break;
//case vk::DescriptorType::eUniformBufferDynamic:
// break;
@ -130,8 +127,6 @@ namespace SHADE
}
}
}
// Link all the writeDescSet data for vkUpdateDescriptorSets to write to the linked descriptors
updater.LinkInfoToWriteDescSet();
}
/***************************************************************************/
@ -160,7 +155,7 @@ namespace SHADE
*/
/***************************************************************************/
void SHVkDescriptorSetGroup::ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector<std::pair<vk::ImageView, vk::Sampler>> const& imageViewsAndSamplers) noexcept
void SHVkDescriptorSetGroup::ModifyWriteDescImage(uint32_t set, uint32_t binding, std::span<std::pair<Handle<SHVkImageView>, Handle<SHVkSampler>>> const& imageViewsAndSamplers) noexcept
{
// Find the target writeDescSet
BindingAndSetHash writeHash = binding;
@ -176,32 +171,83 @@ namespace SHADE
{
// write sampler and image view
auto& ivs = imageViewsAndSamplers[i];
writeInfo.descImageInfos[i].imageView = ivs.first;
writeInfo.descImageInfos[i].sampler = ivs.second;
writeInfo.descImageInfos[i].imageView = ivs.first->GetImageView();
writeInfo.descImageInfos[i].sampler = ivs.second->GetVkSampler();
}
}
void SHVkDescriptorSetGroup::UpdateDescriptorSet(void) noexcept
void SHVkDescriptorSetGroup::ModifyWriteDescBuffer(uint32_t set, uint32_t binding, std::span<Handle<SHVkBuffer>> const& buffers, uint32_t offset, uint32_t range) noexcept
{
device->UpdateDescriptorSets(updater.GetWriteDescriptorSets());
// Find the target writeDescSet
BindingAndSetHash writeHash = binding;
writeHash |= static_cast<uint64_t>(set) << 32;
auto& writeInfo = updater.writeInfos[updater.writeHashMap.at(writeHash)];
if (buffers.size() > writeInfo.descBufferInfos.size())
{
SHLOG_ERROR("Attempting write too many descriptors into descriptor set. Failed to write to vk::WriteDescriptorSet. ");
}
for (uint32_t i = 0; i < buffers.size(); ++i)
{
// write sampler and image view
auto& buffer = buffers[i];
writeInfo.descBufferInfos[i].buffer = buffer->GetVkBuffer();
writeInfo.descBufferInfos[i].offset = offset;
writeInfo.descBufferInfos[i].range = range;
}
}
void SHVkDescriptorSetGroup::UpdateSingleDescriptorSetImages(uint32_t set, uint32_t binding) noexcept
void SHVkDescriptorSetGroup::UpdateDescriptorSetImages(uint32_t set, uint32_t binding) noexcept
{
vk::WriteDescriptorSet writeDescSet{};
// Get binding + set hash
BindingAndSetHash bsHash = SHVkUtil::GenBindingSetHash(set, binding);
// to index a set
uint32_t setIndex = setIndexing[bsHash];
// to index a write for a binding
uint32_t writeInfoIndex = updater.writeHashMap[bsHash];
// Initialize info for write
writeDescSet.descriptorType = vk::DescriptorType::eCombinedImageSampler;
writeDescSet.descriptorType = layoutsUsed[setIndex]->GetBindings()[binding].Type;
writeDescSet.dstArrayElement = 0;
writeDescSet.dstSet = descSets[set];
writeDescSet.dstSet = descSets[setIndex];
writeDescSet.dstBinding = binding;
writeDescSet.pImageInfo = updater.writeInfos[set].descImageInfos.data();
writeDescSet.descriptorCount = static_cast<uint32_t>(updater.writeInfos[set].descImageInfos.size());
writeDescSet.pImageInfo = updater.writeInfos[writeInfoIndex].descImageInfos.data();
writeDescSet.descriptorCount = static_cast<uint32_t>(updater.writeInfos[writeInfoIndex].descImageInfos.size());
device->UpdateDescriptorSet(writeDescSet);
}
void SHVkDescriptorSetGroup::UpdateDescriptorSetBuffer(uint32_t set, uint32_t binding) noexcept
{
vk::WriteDescriptorSet writeDescSet{};
// Get binding + set hash
BindingAndSetHash bsHash = SHVkUtil::GenBindingSetHash(set, binding);
// to index a set
uint32_t setIndex = setIndexing[bsHash];
// to index a write for a binding
uint32_t writeInfoIndex = updater.writeHashMap[bsHash];
// Initialize info for write
writeDescSet.descriptorType = layoutsUsed[setIndex]->GetBindings()[binding].Type;
writeDescSet.dstArrayElement = 0;
writeDescSet.dstSet = descSets[setIndex];
writeDescSet.dstBinding = binding;
writeDescSet.pBufferInfo = updater.writeInfos[writeInfoIndex].descBufferInfos.data();
writeDescSet.descriptorCount = static_cast<uint32_t>(updater.writeInfos[writeInfoIndex].descBufferInfos.size());
device->UpdateDescriptorSet(writeDescSet);
}
}

View File

@ -14,6 +14,10 @@ namespace SHADE
class SHVkLogicalDevice;
class SHVkDescriptorPool;
class SHVkDescriptorSetLayout;
class SHVkSampler;
class SHVkImage;
class SHVkImageView;
class SHVkBuffer;
/*---------------------------------------------------------------------------------*/
@ -54,12 +58,14 @@ namespace SHADE
SHVkDescriptorSetGroup& operator=(SHVkDescriptorSetGroup&& rhs) noexcept = default;
/*-----------------------------------------------------------------------------*/
/* Descriptor set writing */
/* Public member functions */
/*-----------------------------------------------------------------------------*/
void ModifyWriteDescImage(uint32_t set, uint32_t binding, std::vector<std::pair<vk::ImageView, vk::Sampler>> const& imageViewsAndSamplers) noexcept;
void UpdateDescriptorSet(void) noexcept;
void UpdateDescriptorSetImages(uint32_t set, uint32_t binding) noexcept;
void UpdateDescriptorSetBuffer(uint32_t set, uint32_t binding) noexcept;
void ModifyWriteDescImage(uint32_t set, uint32_t binding, std::span<std::pair<Handle<SHVkImageView>, Handle<SHVkSampler>>> const& imageViewsAndSamplers) noexcept;
void ModifyWriteDescBuffer (uint32_t set, uint32_t binding, std::span<Handle<SHVkBuffer>> const& buffers, uint32_t offset, uint32_t range) noexcept;
void UpdateSingleDescriptorSetImages(uint32_t set, uint32_t binding) noexcept;
/*-----------------------------------------------------------------------------*/
/* Getter Functions */
@ -81,9 +87,17 @@ namespace SHADE
//! Descriptor pool to allocate descriptor sets
Handle<SHVkDescriptorPool> descPool;
//! Sometimes when we pass in a layout, the set of the layout used in the
//! shader cannot be used to index into descSets. This is to mitigate that issue
//! when we update descriptor sets.
std::unordered_map<SetIndex, uint32_t> setIndexing;
//! Descriptor sets
std::vector<vk::DescriptorSet> descSets;
//! Layouts used to create this descriptor set group
std::vector<Handle<SHVkDescriptorSetLayout>> layoutsUsed;
//! for updating descriptor sets. We want to cache this so that we don't create the
//! write structs at runtime.
SHDescriptorSetUpdater updater;

View File

@ -7,9 +7,10 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Constructor/Destructor */
/*---------------------------------------------------------------------------------*/
SHVkDescriptorSetLayout::SHVkDescriptorSetLayout(Handle<SHVkLogicalDevice> device, const std::vector<Binding>& bindings)
SHVkDescriptorSetLayout::SHVkDescriptorSetLayout(Handle<SHVkLogicalDevice> device, SetIndex set, const std::vector<Binding>& bindings)
: device { device }
, layoutDesc { bindings }
, setIndex {set}
{
// Check if auto-binding point calculation configuration is valid
bool autoCalc = false;
@ -74,6 +75,7 @@ namespace SHADE
: device {rhs.device}
, setLayout {rhs.setLayout}
, layoutDesc{std::move (rhs.layoutDesc)}
, setIndex {rhs.setIndex}
{
rhs.setLayout = VK_NULL_HANDLE;
}
@ -90,6 +92,11 @@ namespace SHADE
return layoutDesc;
}
SetIndex SHVkDescriptorSetLayout::GetSetIndex(void) const noexcept
{
return setIndex;
}
SHVkDescriptorSetLayout& SHVkDescriptorSetLayout::operator=(SHVkDescriptorSetLayout&& rhs) noexcept
{
if (&rhs == this)
@ -98,6 +105,7 @@ namespace SHADE
device = rhs.device;
setLayout = rhs.setLayout;
layoutDesc = std::move(rhs.layoutDesc);
setIndex = rhs.setIndex;
rhs.setLayout = VK_NULL_HANDLE;

View File

@ -74,7 +74,7 @@ namespace SHADE
/// </summary>
/// <param name="device"></param>
/// <param name="bindings"></param>
SHVkDescriptorSetLayout(Handle<SHVkLogicalDevice> device, const std::vector<Binding>& bindings);
SHVkDescriptorSetLayout(Handle<SHVkLogicalDevice> device, SetIndex setIndex, const std::vector<Binding>& bindings);
SHVkDescriptorSetLayout(const SHVkDescriptorSetLayout&) = delete;
SHVkDescriptorSetLayout(SHVkDescriptorSetLayout&& rhs) noexcept;
/// <summary>
@ -97,6 +97,7 @@ namespace SHADE
/// <returns>Handle to the Vulkan Descriptor Set Layout handle.</returns>
inline const vk::DescriptorSetLayout& GetVkHandle() const { return setLayout; }
std::vector<Binding> const& GetBindings (void) const noexcept;
SetIndex GetSetIndex (void) const noexcept;
private:
/*-----------------------------------------------------------------------------*/
@ -105,5 +106,6 @@ namespace SHADE
Handle<SHVkLogicalDevice> device;
vk::DescriptorSetLayout setLayout;
std::vector<Binding> layoutDesc; // Stores description of the layout
SetIndex setIndex; // Index of the set
};
}

View File

@ -467,7 +467,12 @@ namespace SHADE
*/
/***************************************************************************/
Handle<SHVkPipelineLayout> SHVkLogicalDevice::CreatePipelineLayout(SHPipelineLayoutParams& pipelineLayoutParams) noexcept
Handle<SHVkPipelineLayout> SHVkLogicalDevice::CreatePipelineLayout(SHPipelineLayoutParams const& pipelineLayoutParams) noexcept
{
return SHVkInstance::GetResourceManager().Create <SHVkPipelineLayout>(GetHandle(), pipelineLayoutParams);
}
Handle<SHVkPipelineLayout> SHVkLogicalDevice::CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept
{
return SHVkInstance::GetResourceManager().Create <SHVkPipelineLayout>(GetHandle(), pipelineLayoutParams);
}
@ -488,9 +493,9 @@ namespace SHADE
*/
/***************************************************************************/
Handle<SHVkPipeline> SHVkLogicalDevice::CreatePipeline(Handle<SHVkPipelineLayout> const& pipelineLayoutHdl, SHVkPipelineState const* const state, Handle<SHVkRenderpass> const& renderpassHdl, Handle<SHSubpass> subpass, SH_PIPELINE_TYPE type) noexcept
Handle<SHVkPipeline> SHVkLogicalDevice::CreateGraphicsPipeline(Handle<SHVkPipelineLayout> const& pipelineLayoutHdl, SHVkPipelineState const* const state, Handle<SHVkRenderpass> const& renderpassHdl, Handle<SHSubpass> subpass) noexcept
{
return SHVkInstance::GetResourceManager().Create <SHVkPipeline>(GetHandle(), pipelineLayoutHdl, state, renderpassHdl, subpass, type);
return SHVkInstance::GetResourceManager().Create <SHVkPipeline>(GetHandle(), pipelineLayoutHdl, state, renderpassHdl, subpass);
}
@ -510,9 +515,9 @@ namespace SHADE
}
Handle<SHVkDescriptorSetLayout> SHVkLogicalDevice::CreateDescriptorSetLayout(std::vector<SHVkDescriptorSetLayout::Binding> const& bindings) noexcept
Handle<SHVkDescriptorSetLayout> SHVkLogicalDevice::CreateDescriptorSetLayout(SetIndex setIndex, std::vector<SHVkDescriptorSetLayout::Binding> const& bindings) noexcept
{
return SHVkInstance::GetResourceManager().Create <SHVkDescriptorSetLayout>(GetHandle(), bindings);
return SHVkInstance::GetResourceManager().Create <SHVkDescriptorSetLayout>(GetHandle(), setIndex, bindings);
}

View File

@ -21,6 +21,7 @@
#include "Graphics/Descriptors/SHVkDescriptorSetLayout.h"
#include "Graphics/Images/SHVkImage.h"
namespace SHADE
{
/*-----------------------------------------------------------------------*/
@ -171,23 +172,23 @@ namespace SHADE
std::string const& shaderName
) noexcept;
Handle<SHVkPipeline> CreatePipeline (
Handle<SHVkPipeline> CreateGraphicsPipeline (
Handle<SHVkPipelineLayout> const& pipelineLayoutHdl,
SHVkPipelineState const* const state,
Handle<SHVkRenderpass> const& renderpassHdl,
Handle<SHSubpass> subpass,
SH_PIPELINE_TYPE type
Handle<SHSubpass> subpass
) noexcept;
Handle<SHVkRenderpass> CreateRenderpass (std::span<vk::AttachmentDescription> const vkDescriptions, std::vector<SHVkSubpassParams> const& subpasses) noexcept;
Handle<SHVkRenderpass> CreateRenderpass (std::span<vk::AttachmentDescription> const vkDescriptions, std::span<vk::SubpassDescription> const spDescs, std::span<vk::SubpassDependency> const spDeps) noexcept;
Handle<SHVkFramebuffer> CreateFramebuffer (Handle<SHVkRenderpass> const& renderpassHdl, std::vector<Handle<SHVkImageView>> const& attachments, uint32_t inWidth, uint32_t inHeight) noexcept;
Handle<SHVkDescriptorSetLayout> CreateDescriptorSetLayout (std::vector<SHVkDescriptorSetLayout::Binding> const& bindings) noexcept;
Handle<SHVkDescriptorSetLayout> CreateDescriptorSetLayout (SetIndex setIndex, std::vector<SHVkDescriptorSetLayout::Binding> const& bindings) noexcept;
Handle<SHVkDescriptorPool> CreateDescriptorPools (const SHVkDescriptorPool::Config& config = {}) noexcept;
Handle<SHVkDescriptorSetGroup> CreateDescriptorSetGroup(Handle<SHVkDescriptorPool> pool,
std::vector<Handle<SHVkDescriptorSetLayout>> const& layouts,
std::vector<uint32_t> const& variableDescCounts) noexcept;
Handle<SHVkPipelineLayout> CreatePipelineLayout (SHPipelineLayoutParams& pipelineLayoutParams) noexcept;
Handle<SHVkPipelineLayout> CreatePipelineLayout(SHPipelineLayoutParams const& pipelineLayoutParams) noexcept;
Handle<SHVkPipelineLayout> CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept;
Handle<SHVkFence> CreateFence (void) const noexcept;
Handle<SHVkSemaphore> CreateSemaphore (void) const noexcept;

View File

@ -234,7 +234,7 @@ namespace SHADE
return SHVkInstance::GetResourceManager().Create<SHVkImageView>(inLogicalDeviceHdl, parent, createParams);
}
void SHVkImage::TransferToDeviceResource(void) noexcept
void SHVkImage::TransferToDeviceResource(Handle<SHVkCommandBuffer> cmdBufferHdl) noexcept
{
// prepare copy regions
std::vector<vk::BufferImageCopy> copyRegions{mipOffsets.size()};
@ -252,7 +252,7 @@ namespace SHADE
copyRegions[i].imageExtent = vk::Extent3D{ width >> i, height >> i, 1 };
}
//PrepareImageTransition();
cmdBufferHdl->CopyBufferToImage(stagingBuffer, vkImage, copyRegions);
}
/***************************************************************************/
@ -274,7 +274,7 @@ namespace SHADE
*/
/***************************************************************************/
void SHVkImage::PrepareImageTransition(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept
void SHVkImage::PrepareImageTransitionInfo(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept
{
barrier.oldLayout = oldLayout;
barrier.newLayout = newLayout;
@ -286,33 +286,6 @@ namespace SHADE
barrier.subresourceRange.levelCount = mipLevelCount;
barrier.subresourceRange.baseArrayLayer = 0;
barrier.subresourceRange.layerCount = layerCount;
vk::PipelineStageFlagBits srcStage = vk::PipelineStageFlagBits::eTopOfPipe;
vk::PipelineStageFlagBits dstStage = vk::PipelineStageFlagBits::eTopOfPipe;
if (oldLayout == vk::ImageLayout::eUndefined && newLayout == vk::ImageLayout::eTransferDstOptimal)
{
srcStage = vk::PipelineStageFlagBits::eTopOfPipe;
dstStage = vk::PipelineStageFlagBits::eTransfer;
barrier.srcAccessMask = vk::AccessFlagBits::eNone;
barrier.dstAccessMask = vk::AccessFlagBits::eTransferWrite;
}
else if (oldLayout == vk::ImageLayout::eTransferDstOptimal && newLayout == vk::ImageLayout::eShaderReadOnlyOptimal)
{
srcStage = vk::PipelineStageFlagBits::eTransfer;
// TODO, what if we want to access in compute shader
dstStage = vk::PipelineStageFlagBits::eFragmentShader;
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
}
else
{
SHLOG_ERROR("Image layouts are invalid. ");
}
}
void SHVkImage::LinkWithExteriorImage(vk::Image inVkImage, vk::ImageType type, uint32_t inWidth, uint32_t inHeight, uint32_t inDepth, uint32_t layers, uint8_t levels, vk::Format format, vk::ImageUsageFlags flags) noexcept

View File

@ -135,8 +135,8 @@ namespace SHADE
/* PUBLIC MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/
Handle<SHVkImageView> CreateImageView (Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, Handle<SHVkImage> const& parent, SHImageViewDetails const& createParams) const noexcept;
void TransferToDeviceResource (void) noexcept;
void PrepareImageTransition (vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept;
void TransferToDeviceResource (Handle<SHVkCommandBuffer> cmdBufferHdl) noexcept;
void PrepareImageTransitionInfo (vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::ImageMemoryBarrier& barrier) noexcept;
/*-----------------------------------------------------------------------*/
/* GETTERS AND SETTERS */

View File

@ -4,4 +4,9 @@
namespace SHADE
{
vk::Sampler SHVkSampler::GetVkSampler(void) const noexcept
{
return vkSampler;
}
}

View File

@ -22,6 +22,7 @@ namespace SHADE
SHVkSampler (SHVkSampler&& rhs) noexcept;
SHVkSampler&& operator=(SHVkSampler&& rhs) noexcept;
vk::Sampler GetVkSampler (void) const noexcept;
};
}

View File

@ -22,6 +22,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/Pipeline/SHVkPipeline.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"
#include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h"
#include "ECS_Base/Managers/SHComponentManager.h"
#include "Math/Transform/SHTransformComponent.h"
namespace SHADE
{
@ -33,6 +35,9 @@ namespace SHADE
{
if (!pipeline)
throw std::invalid_argument("Attempted to create a SHBatch with an invalid SHPipeline!");
// Mark all as dirty
setAllDirtyFlags();
}
void SHBatch::Add(const SHRenderable* renderable)
@ -52,6 +57,9 @@ namespace SHADE
// Add renderable in
subBatch->Renderables.insert(renderable);
// Mark all as dirty
setAllDirtyFlags();
}
void SHBatch::Remove(const SHRenderable* renderable)
@ -67,6 +75,10 @@ namespace SHADE
return;
subBatch->Renderables.erase(renderable);
// Mark all as dirty
for (bool& dirt : isDirty)
dirt = true;
}
void SHBatch::Clear()
@ -81,15 +93,56 @@ namespace SHADE
// Clear GPU buffers
drawDataBuffer.Free();
transformDataBuffer.Free();
matPropsBuffer.Free();
for (int i = 0; i < SHGraphicsConstants::NUM_FRAME_BUFFERS; ++i)
{
drawDataBuffer[i].Free();
transformDataBuffer[i].Free();
matPropsBuffer[i].Free();
}
}
void SHBatch::Build(Handle<SHVkLogicalDevice> device)
void SHBatch::UpdateTransformBuffer(uint32_t frameIndex)
{
if (frameIndex >= SHGraphicsConstants::NUM_FRAME_BUFFERS)
{
SHLOG_WARNING("[SHBatch] Attempted to update transform buffers with an invalid frame index.");
return;
}
// Reset Transform Data
transformData.clear();
// Populate on the CPU
for (auto& subBatch : subBatches)
for (const SHRenderable* renderable : subBatch.Renderables)
{
// Transform
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(renderable->GetEID());
if (!transform)
{
SHLOG_WARNING("[SHBatch] Entity contianing a SHRenderable with no SHTransformComponent found!");
transformData.emplace_back();
}
else
{
transformData.emplace_back(transform->GetTRS());
}
}
// Transfer to GPU
transformDataBuffer[frameIndex]->WriteToMemory(transformData.data(), transformData.size() * sizeof(SHMatrix), 0, 0);
}
void SHBatch::Build(Handle<SHVkLogicalDevice> device, uint32_t frameIndex)
{
if (frameIndex >= SHGraphicsConstants::NUM_FRAME_BUFFERS)
{
SHLOG_WARNING("[SHBatch] Attempted to update build batch buffers with an invalid frame index.");
return;
}
// No need to build as there are no changes
if (!isDirty)
if (!isDirty[frameIndex])
return;
// Count number of elements
@ -99,108 +152,131 @@ namespace SHADE
numTotalElements += subBatch.Renderables.size();
}
// Generate CPU buffers
// - Draw data
drawData.reserve(subBatches.size());
drawData.clear();
// - Transform data
transformData.reserve(numTotalElements);
transformData.clear();
// - Material Properties Data
const Handle<SHShaderBlockInterface> SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface
(
SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE,
SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA,
vk::ShaderStageFlagBits::eFragment
);
const bool EMPTY_MAT_PROPS = !SHADER_INFO;
Byte singleMatPropSize = 0;
Byte matPropTotalBytes = 0;
if (!EMPTY_MAT_PROPS)
// Generate CPU buffers if there are changes
if (isCPUBuffersDirty)
{
singleMatPropSize = SHADER_INFO->GetBytesRequired();
matPropTotalBytes = drawData.size() * singleMatPropSize;
if (matPropsDataSize < matPropTotalBytes)
// - Draw data
drawData.reserve(subBatches.size());
drawData.clear();
// - Transform data
transformData.reserve(numTotalElements);
transformData.clear();
// - Material Properties Data
const Handle<SHShaderBlockInterface> SHADER_INFO = pipeline->GetPipelineLayout()->GetShaderBlockInterface
(
SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE,
SHGraphicsConstants::DescriptorSetBindings::BATCHED_PER_INST_DATA,
vk::ShaderStageFlagBits::eFragment
);
const bool EMPTY_MAT_PROPS = !SHADER_INFO;
Byte singleMatPropSize = 0;
Byte matPropTotalBytes = 0;
if (!EMPTY_MAT_PROPS)
{
matPropsData.reset(new char[matPropTotalBytes]);
matPropsDataSize = matPropTotalBytes;
}
}
// Build Sub Batches
uint32_t nextInstanceIndex = 0;
char* propsCurrPtr = matPropsData.get();
for (auto& subBatch : subBatches)
{
// Create command
drawData.emplace_back(vk::DrawIndexedIndirectCommand
{
.indexCount = subBatch.Mesh->IndexCount,
.instanceCount = static_cast<uint32_t>(subBatch.Renderables.size()),
.firstIndex = subBatch.Mesh->FirstIndex,
.vertexOffset = subBatch.Mesh->FirstVertex,
.firstInstance = nextInstanceIndex
});
// Fill in buffers (CPU)
for (const SHRenderable* renderable : subBatch.Renderables)
{
// Transform
transformData.emplace_back(renderable->TransformMatrix);
// Material Properties
if (!EMPTY_MAT_PROPS)
singleMatPropSize = SHADER_INFO->GetBytesRequired();
matPropTotalBytes = drawData.size() * singleMatPropSize;
if (matPropsDataSize < matPropTotalBytes)
{
renderable->GetMaterial()->ExportProperties(propsCurrPtr);
propsCurrPtr += singleMatPropSize;
matPropsData.reset(new char[matPropTotalBytes]);
matPropsDataSize = matPropTotalBytes;
}
}
// Build Sub Batches
uint32_t nextInstanceIndex = 0;
char* propsCurrPtr = matPropsData.get();
for (auto& subBatch : subBatches)
{
// Create command
drawData.emplace_back(vk::DrawIndexedIndirectCommand
{
.indexCount = subBatch.Mesh->IndexCount,
.instanceCount = static_cast<uint32_t>(subBatch.Renderables.size()),
.firstIndex = subBatch.Mesh->FirstIndex,
.vertexOffset = subBatch.Mesh->FirstVertex,
.firstInstance = nextInstanceIndex
});
// Fill in buffers (CPU)
for (const SHRenderable* renderable : subBatch.Renderables)
{
// Transform
auto transform = SHComponentManager::GetComponent_s<SHTransformComponent>(renderable->GetEID());
if (!transform)
{
SHLOG_WARNING("[SHBatch] Entity contianing a SHRenderable with no SHTransformComponent found!");
transformData.emplace_back();
}
else
{
transformData.emplace_back(transform->GetTRS());
}
// Material Properties
if (!EMPTY_MAT_PROPS)
{
renderable->GetMaterial()->ExportProperties(propsCurrPtr);
propsCurrPtr += singleMatPropSize;
}
}
}
// Successfully update CPU buffers
isCPUBuffersDirty = false;
}
// Send all buffered data to the GPU buffers
using BuffUsage = vk::BufferUsageFlagBits;
// - Draw Data
if (drawDataBuffer)
drawDataBuffer->Unmap();
const uint32_t DRAW_DATA_BYTES = static_cast<uint32_t>(drawData.size() * sizeof(vk::DrawIndexedIndirectCommand));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, drawDataBuffer, drawData.data(), DRAW_DATA_BYTES,
device, drawDataBuffer[frameIndex], drawData.data(), DRAW_DATA_BYTES,
BuffUsage::eIndirectBuffer
);
drawDataBuffer->Map();
// - Transform Buffer
if (transformDataBuffer)
transformDataBuffer->Unmap();
const uint32_t TF_DATA_BYTES = static_cast<uint32_t>(transformData.size() * sizeof(SHMatrix));
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, transformDataBuffer, transformData.data(), TF_DATA_BYTES,
device, transformDataBuffer[frameIndex], transformData.data(), TF_DATA_BYTES,
BuffUsage::eVertexBuffer
);
transformDataBuffer->Map();
// - Material Properties Buffer
if (!EMPTY_MAT_PROPS)
if (matPropsData)
{
if (matPropsBuffer)
matPropsBuffer->Unmap();
SHVkUtil::EnsureBufferAndCopyHostVisibleData
(
device, matPropsBuffer, matPropsData.get(), static_cast<uint32_t>(matPropTotalBytes),
device, matPropsBuffer[frameIndex], matPropsData.get(), static_cast<uint32_t>(matPropsDataSize),
BuffUsage::eStorageBuffer
);
matPropsBuffer->Map();
}
isDirty = false;
isDirty[frameIndex] = false;
}
/*---------------------------------------------------------------------------------*/
/* SHBatch - Usage Functions */
/*---------------------------------------------------------------------------------*/
void SHBatch::Draw(Handle<SHVkCommandBuffer> cmdBuffer)
void SHBatch::Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex)
{
if (frameIndex >= SHGraphicsConstants::NUM_FRAME_BUFFERS)
{
SHLOG_WARNING("[SHBatch] Attempted to draw a batch with an invalid frame index.");
return;
}
cmdBuffer->BindPipeline(pipeline);
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer, 0);
cmdBuffer->DrawMultiIndirect(drawDataBuffer, static_cast<uint32_t>(drawData.size()));
cmdBuffer->BindVertexBuffer(SHGraphicsConstants::VertexBufferBindings::TRANSFORM, transformDataBuffer[frameIndex], 0);
cmdBuffer->DrawMultiIndirect(drawDataBuffer[frameIndex], static_cast<uint32_t>(drawData.size()));
}
/*---------------------------------------------------------------------------------*/
/* SHBatch - Helper Functions */
/*---------------------------------------------------------------------------------*/
void SHBatch::setAllDirtyFlags()
{
for (bool& dirt : isDirty)
dirt = true;
isCPUBuffersDirty = true;
}
}

View File

@ -14,12 +14,14 @@ of DigiPen Institute of Technology is prohibited.
// STL Includes
#include <unordered_set>
#include <array>
// External Dependencies
#include "Graphics/SHVulkanIncludes.h"
// Project Includes
#include "Resource/Handle.h"
#include "Graphics/MiddleEnd/Interface/SHMaterial.h"
#include "Math/SHMatrix.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"
namespace SHADE
{
@ -71,8 +73,9 @@ namespace SHADE
void Add(const SHRenderable* renderable);
void Remove(const SHRenderable* renderable);
void Clear();
void Build(Handle<SHVkLogicalDevice> device);
void Draw(Handle<SHVkCommandBuffer> cmdBuffer);
void UpdateTransformBuffer(uint32_t frameIndex);
void Build(Handle<SHVkLogicalDevice> device, uint32_t frameIndex);
void Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex);
/*-----------------------------------------------------------------------------*/
/* Getter Functions */
@ -87,15 +90,21 @@ namespace SHADE
Handle<SHVkPipeline> pipeline;
// Batch Tree
std::vector<SHSubBatch> subBatches;
bool isDirty = true;
std::array<bool, SHGraphicsConstants::NUM_FRAME_BUFFERS> isDirty;
// CPU Buffers
std::vector<vk::DrawIndexedIndirectCommand> drawData;
std::vector<SHMatrix> transformData;
std::unique_ptr<char> matPropsData;
Byte matPropsDataSize = 0;
bool isCPUBuffersDirty = true;
// GPU Buffers
Handle<SHVkBuffer> drawDataBuffer;
Handle<SHVkBuffer> transformDataBuffer;
Handle<SHVkBuffer> matPropsBuffer;
std::array<Handle<SHVkBuffer>, SHGraphicsConstants::NUM_FRAME_BUFFERS> drawDataBuffer;
std::array<Handle<SHVkBuffer>, SHGraphicsConstants::NUM_FRAME_BUFFERS> transformDataBuffer;
std::array<Handle<SHVkBuffer>, SHGraphicsConstants::NUM_FRAME_BUFFERS> matPropsBuffer;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
void setAllDirtyFlags();
};
}

View File

@ -91,12 +91,12 @@ namespace SHADE
(*superBatch)->Remove(renderable);
}
void SHBatcher::FinaliseBatches(Handle<SHVkLogicalDevice> device)
void SHBatcher::FinaliseBatches(Handle<SHVkLogicalDevice> device, uint32_t frameIndex)
{
// Build SuperBatches
for (auto& batch : superBatches)
{
batch->Build(device);
batch->Build(device, frameIndex);
}
}
@ -109,6 +109,14 @@ namespace SHADE
superBatches.clear();
}
void SHBatcher::UpdateTransformBuffer(uint32_t frameIndex)
{
for (auto& batch : superBatches)
{
batch->UpdateTransformBuffer(frameIndex);
}
}
void SHBatcher::RegisterSuperBatch(Handle<SHSuperBatch> superBatch)
{
superBatches.emplace_back(superBatch);

View File

@ -51,8 +51,9 @@ namespace SHADE
void PrepareBatches();
void AddToBatch(SHRenderable const* renderable);
void RemoveFromBatch(SHRenderable const* renderable);
void FinaliseBatches(Handle<SHVkLogicalDevice> device);
void FinaliseBatches(Handle<SHVkLogicalDevice> device, uint32_t frameIndex);
void ClearBatches();
void UpdateTransformBuffer(uint32_t frameIndex);
void RegisterSuperBatch(Handle<SHSuperBatch> superBatch);
void DeregisterSuperBatch(Handle<SHSuperBatch> superBatch);

View File

@ -78,21 +78,29 @@ namespace SHADE
batches.clear();
}
void SHSuperBatch::Build(Handle<SHVkLogicalDevice> device) noexcept
void SHSuperBatch::UpdateTransformBuffer(uint32_t frameIndex)
{
// Build all batches
for (auto& batch : batches)
{
batch.Build(device);
batch.UpdateTransformBuffer(frameIndex);
}
}
void SHSuperBatch::Draw(Handle<SHVkCommandBuffer> cmdBuffer) noexcept
void SHSuperBatch::Build(Handle<SHVkLogicalDevice> device, uint32_t frameIndex) noexcept
{
// Build all batches
for (auto& batch : batches)
{
batch.Draw(cmdBuffer);
batch.Build(device, frameIndex);
}
}
void SHSuperBatch::Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept
{
// Build all batches
for (auto& batch : batches)
{
batch.Draw(cmdBuffer, frameIndex);
}
}

View File

@ -55,8 +55,9 @@ namespace SHADE
void Add(const SHRenderable* renderable) noexcept;
void Remove(const SHRenderable* renderable) noexcept;
void Clear() noexcept;
void Build(Handle<SHVkLogicalDevice> device) noexcept;
void Draw(Handle<SHVkCommandBuffer> cmdBuffer) noexcept;
void UpdateTransformBuffer(uint32_t frameIndex);
void Build(Handle<SHVkLogicalDevice> device, uint32_t frameIndex) noexcept;
void Draw(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept;
/*-----------------------------------------------------------------------------*/
/* Getter Functions */

View File

@ -2,6 +2,7 @@
#include "SHGraphicsGlobalData.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Graphics/Pipeline/SHPipelineState.h"
#include "Graphics/Pipeline/SHVkPipelineLayout.h"
#include "Graphics/Descriptors/SHVkDescriptorSetLayout.h"
namespace SHADE
@ -27,7 +28,7 @@ namespace SHADE
};
// For global data (generic data and textures)
Handle<SHVkDescriptorSetLayout> staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ genericDataBinding, texturesBinding });
Handle<SHVkDescriptorSetLayout> staticGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::STATIC_GLOBALS,{ genericDataBinding, texturesBinding });
SHVkDescriptorSetLayout::Binding lightBinding
{
@ -38,7 +39,7 @@ namespace SHADE
};
// For Dynamic global data (lights)
Handle<SHVkDescriptorSetLayout> dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ lightBinding });
Handle<SHVkDescriptorSetLayout> dynamicGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::DYNAMIC_GLOBALS, { lightBinding });
SHVkDescriptorSetLayout::Binding cameraDataBinding
{
@ -49,7 +50,7 @@ namespace SHADE
};
// For High frequency global data (camera)
Handle<SHVkDescriptorSetLayout> cameraDataGlobalLayout = logicalDevice->CreateDescriptorSetLayout({ cameraDataBinding });
Handle<SHVkDescriptorSetLayout> cameraDataGlobalLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, { cameraDataBinding });
SHVkDescriptorSetLayout::Binding materialDataBinding
{
@ -60,12 +61,15 @@ namespace SHADE
};
// For High frequency global data (camera)
Handle<SHVkDescriptorSetLayout> materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout({ materialDataBinding });
Handle<SHVkDescriptorSetLayout> materialDataPerInstanceLayout = logicalDevice->CreateDescriptorSetLayout(SHGraphicsConstants::DescriptorSetIndex::PER_INSTANCE, { materialDataBinding });
globalDescSetLayouts.push_back(staticGlobalLayout);
globalDescSetLayouts.push_back(dynamicGlobalLayout);
globalDescSetLayouts.push_back(cameraDataGlobalLayout);
globalDescSetLayouts.push_back(materialDataPerInstanceLayout);
dummyPipelineLayout = logicalDevice->CreatePipelineLayoutDummy(SHPipelineLayoutParamsDummy{globalDescSetLayouts});
}
void SHGraphicsGlobalData::InitDefaultVertexInputState(void) noexcept
@ -94,4 +98,9 @@ namespace SHADE
return defaultVertexInputState;
}
Handle<SHVkPipelineLayout> SHGraphicsGlobalData::GetDummyPipelineLayout(void) const noexcept
{
return dummyPipelineLayout;
}
}

View File

@ -9,6 +9,7 @@ namespace SHADE
class SHVkLogicalDevice;
class SHVkDescriptorSetLayout;
class SHVkDescriptorSetGroup;
class SHVkPipelineLayout;
class SH_API SHGraphicsGlobalData
{
@ -22,6 +23,10 @@ namespace SHADE
//! Default vertex input state (used by everything).
SHVertexInputState defaultVertexInputState;
//! Since we want to bind global data but can't do so without a pipeline layout,
//! we create a dummy pipeline layout to use it for binding.
Handle<SHVkPipelineLayout> dummyPipelineLayout;
void InitDescSetLayouts (Handle<SHVkLogicalDevice> logicalDevice) noexcept;
void InitDefaultVertexInputState(void) noexcept;
public:
@ -35,5 +40,6 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
std::vector<Handle<SHVkDescriptorSetLayout>> const& GetDescSetLayouts (void) const noexcept;
SHVertexInputState const& GetDefaultViState (void) const noexcept;
Handle<SHVkPipelineLayout> GetDummyPipelineLayout (void) const noexcept;
};
}

View File

@ -22,24 +22,24 @@ namespace SHADE
void SHCamera::SetLookAt(const SHVec3& pos, const SHVec3& target, const SHVec3& up)
{
SHVec3 view = target - pos; view = SHVec3::Normalise(view);
SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right);
const SHVec3 UP = SHVec3::Cross(right, view);
viewMatrix = SHMatrix::Identity;
viewMatrix(0, 0) = right[0];
viewMatrix(1, 0) = right[1];
viewMatrix(2, 0) = right[2];
viewMatrix(0, 1) = UP[0];
viewMatrix(1, 1) = UP[1];
viewMatrix(2, 1) = UP[2];
viewMatrix(0, 2) = -view[0];
viewMatrix(1, 2) = -view[1];
viewMatrix(2, 2) = -view[2];
viewMatrix(3, 0) = -right.Dot(pos);
viewMatrix(3, 1) = -UP.Dot(pos);
viewMatrix(3, 2) = view.Dot(pos);
isDirty = true;
SHVec3 right = SHVec3::Cross(view, up); right = SHVec3::Normalise(right);
const SHVec3 UP = SHVec3::Cross(right, view);
viewMatrix = SHMatrix::Identity;
viewMatrix(0, 0) = UP[0];
viewMatrix(1, 0) = UP[1];
viewMatrix(2, 0) = UP[2];
viewMatrix(0, 1) = right[0];
viewMatrix(1, 1) = right[1];
viewMatrix(2, 1) = right[2];
viewMatrix(0, 2) = view[0];
viewMatrix(1, 2) = view[1];
viewMatrix(2, 2) = view[2];
viewMatrix(3, 0) = -UP.Dot(pos);
viewMatrix(3, 1) = -right.Dot(pos);
viewMatrix(3, 2) = -view.Dot(pos);
isDirty = true;
}
/*---------------------------------------------------------------------------------*/

View File

@ -29,6 +29,7 @@ of DigiPen Institute of Technology is prohibited.
#include "Graphics/MiddleEnd/Batching/SHSuperBatch.h"
#include "SHGraphicsConstants.h"
#include "Graphics/MiddleEnd/GlobalData/SHGraphicsGlobalData.h"
#include "Graphics/Buffers/SHVkBuffer.h"
namespace SHADE
{
@ -113,7 +114,8 @@ namespace SHADE
screenCamera->SetLookAt(SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 1.0f), SHVec3(0.0f, 1.0f, 0.0f));
screenCamera->SetOrthographic(static_cast<float>(windowDims.first), static_cast<float>(windowDims.second), 0.01f, 100.0f);
worldCamera = resourceManager.Create<SHCamera>();
worldCamera->SetLookAt(SHVec3(0.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 1.0f), SHVec3(0.0f, 1.0f, 0.0f));
//worldCamera->SetLookAt(SHVec3(1.0f, 0.0f, -1.0f), SHVec3(0.0f, 0.0f, 2.0f), SHVec3(0.0f, 1.0f, 0.0f));
worldCamera->SetLookAt(SHVec3(0.0f, 5.0f, -1.0f), SHVec3(0.0f, 0.0f, 2.0f), SHVec3(0.0f, 1.0f, 0.0f));
worldCamera->SetPerspective(90.0f, static_cast<float>(windowDims.first), static_cast<float>(windowDims.second), 0.0f, 100.0f);
// Create Default Viewport
@ -129,7 +131,7 @@ namespace SHADE
}
// Initialize world render graph
worldRenderGraph->Init(device, swapchain, renderContextCmdPools, globalData);
worldRenderGraph->Init(device, swapchain, globalData);
//worldRenderGraph->AddResource("Position", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat);
//worldRenderGraph->AddResource("Normals", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat);
//worldRenderGraph->AddResource("Composite", SH_ATT_DESC_TYPE::COLOR, windowDims.first, windowDims.second, vk::Format::eR16G16B16A16Sfloat);
@ -167,7 +169,7 @@ namespace SHADE
debugWorldRenderer->SetCamera(worldCamera);*/
// Add world renderer to default viewport
worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), descPool, globalData->GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph);
worldRenderer = defaultViewport->AddRenderer(resourceManager, swapchain->GetNumImages(), renderContextCmdPools, descPool, globalData->GetDescSetLayouts()[SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS], worldRenderGraph);
worldRenderer->SetCamera(worldCamera);
@ -203,6 +205,7 @@ namespace SHADE
{
// Frame data for the current frame
auto const& frameData = renderContext.GetCurrentFrameData();
uint32_t frameIndex = renderContext.GetCurrentFrame();
// semaphore index. This is for render graphs to have semaphores to signal that the next render graph will use to wait on.
bool semIndex = 0;
@ -219,7 +222,6 @@ namespace SHADE
renderContext.ResetFence();
// For every viewport
for (int vpIndex = 0; vpIndex < static_cast<int>(viewports.size()); ++vpIndex)
{
@ -228,16 +230,42 @@ namespace SHADE
// For every renderer
for (int renIndex = 0; renIndex < static_cast<int>(renderers.size()); ++renIndex)
{
// Draw first
renderers[renIndex]->Draw(renderContext.GetCurrentFrame(), MESH_DATA);
/*-----------------------------------------------------------------------*/
/* Renderer start */
/*-----------------------------------------------------------------------*/
// get command buffer of the renderer in the current frame
auto currentCmdBuffer = renderers[renIndex]->GetCommandBuffer(frameIndex);
// get render graph
auto rg = renderers[renIndex]->GetRenderGraph();
// Begin recording the command buffer
currentCmdBuffer->BeginRecording();
currentCmdBuffer->ForceSetPipelineLayout(globalData->GetDummyPipelineLayout());
// Bind all the buffers required for meshes
for (auto& [buffer, bindingPoint] : MESH_DATA)
{
if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eVertexBuffer)
currentCmdBuffer->BindVertexBuffer(bindingPoint, buffer, 0);
else if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eIndexBuffer)
currentCmdBuffer->BindIndexBuffer(buffer, 0);
}
// bind camera data
renderers[renIndex]->UpdateDataAndBind(currentCmdBuffer, frameIndex);
// Draw first
renderers[renIndex]->Draw(frameIndex);
// End the command buffer recording
currentCmdBuffer->EndRecording();
/*-----------------------------------------------------------------------*/
/* Renderer end */
/*-----------------------------------------------------------------------*/
// submit a command buffer from the current render graph and make it wait for the previous render graph before submitting it to GPU.
graphicsQueue->SubmitCommandBuffer
(
{ rg->GetCommandBuffer(renderContext.GetCurrentFrame()) },
{ currentCmdBuffer },
{ (vpIndex == viewports.size() - 1 && renIndex == renderers.size() - 1) ? frameData.semRenderFinishHdl : graphSemaphores[!semIndex] },
{ (vpIndex == 0 && renIndex == 0) ? frameData.semImgAvailableHdl : graphSemaphores[semIndex] },
{ vk::PipelineStageFlagBits::eColorAttachmentOutput },
@ -279,7 +307,7 @@ namespace SHADE
for (auto vp : viewports)
for (auto renderer : vp->GetRenderers())
{
renderer->GetRenderGraph()->FinaliseBatch();
renderer->GetRenderGraph()->FinaliseBatch(renderContext.GetCurrentFrame());
}
// Resize

View File

@ -27,7 +27,6 @@ namespace SHADE
sharedMaterial = {};
material = {};
oldMaterial = {};
}
void SHRenderable::OnDestroy()
@ -93,5 +92,4 @@ namespace SHADE
materialChanged = false;
oldMaterial = {};
}
}

View File

@ -66,7 +66,6 @@ namespace SHADE
/* Data Members */
/*-------------------------------------------------------------------------------*/
Handle<SHMesh> Mesh;
SHMatrix TransformMatrix; // TODO: Replace with Transform component
private:
/*-------------------------------------------------------------------------------*/

View File

@ -20,20 +20,33 @@ of DigiPen Institute of Technology is prohibited.
#include "SHMaterial.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"
#include "Graphics/Descriptors/SHVkDescriptorSetGroup.h"
#include "Graphics/Buffers/SHVkBuffer.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Constructor/Destructors */
/*-----------------------------------------------------------------------------------*/
SHRenderer::SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph)
SHRenderer::SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph)
: viewport { viewport }
, renderGraph { renderGraph }
{
cameraDescriptorSet = logicalDevice->CreateDescriptorSetGroup(descriptorPool, { cameraDescLayout }, { 1 });
cpuCameraData.resize(numFrames);
commandBuffers.resize(static_cast<std::size_t>(numFrames));
for (uint32_t i = 0; i < commandBuffers.size(); ++i)
commandBuffers[i] = cmdPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY);
cameraDescriptorSet = descriptorPool->Allocate({ cameraDescLayout }, { 1 });
cameraDataAlignedSize = logicalDevice->PadUBOSize(sizeof(SHShaderCameraData));
cameraBuffer = logicalDevice->CreateBuffer(cameraDataAlignedSize * numFrames, nullptr, cameraDataAlignedSize * numFrames, vk::BufferUsageFlagBits::eUniformBuffer, VMA_MEMORY_USAGE_AUTO, VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
std::array cameraBufferArray{cameraBuffer};
cameraDescriptorSet->ModifyWriteDescBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA, std::span<Handle<SHVkBuffer>>{ cameraBufferArray.data(), cameraBufferArray.size()}, 0, sizeof (SHShaderCameraData));
cameraDescriptorSet->UpdateDescriptorSetBuffer(SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, SHGraphicsConstants::DescriptorSetBindings::CAMERA_DATA);
}
/*-----------------------------------------------------------------------------------*/
@ -47,21 +60,33 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
/* Drawing Functions */
/*-----------------------------------------------------------------------------------*/
void SHRenderer::Draw(uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept
void SHRenderer::Draw(uint32_t frameIndex) noexcept
{
renderGraph->Execute(frameIndex, graphScopeBuffers);
renderGraph->Execute(frameIndex, commandBuffers[frameIndex]);
}
void SHRenderer::BindDescriptorSet(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept
void SHRenderer::UpdateDataAndBind(Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept
{
std::array<uint32_t, 1> dynamicOffsets{ frameIndex * cameraDataAlignedSize };
cpuCameraData.viewProjectionMatrix = camera->GetViewProjectionMatrix();
cameraBuffer->WriteToMemory(&cpuCameraData, sizeof(SHShaderCameraData), 0, cameraDataAlignedSize * frameIndex);
std::array<uint32_t, 1> dynamicOffsets{ frameIndex * cameraDataAlignedSize };
cmdBuffer->BindDescriptorSet(cameraDescriptorSet, vk::PipelineBindPoint::eGraphics, SHGraphicsConstants::DescriptorSetIndex::HIGH_FREQUENCY_GLOBALS, std::span{ dynamicOffsets.data(), 1 });
}
void SHRenderer::UpdateCameraDataToBuffer(void) noexcept
{
}
Handle<SHRenderGraph> SHRenderer::GetRenderGraph(void) const noexcept
{
return renderGraph;
}
Handle<SHVkCommandBuffer> SHRenderer::GetCommandBuffer(uint32_t frameIndex) const noexcept
{
return commandBuffers[frameIndex];
}
}

View File

@ -39,6 +39,7 @@ namespace SHADE
class SHVkDescriptorSetGroup;
class SHGraphicsGlobalData;
class SHVkDescriptorPool;
class SHVkBuffer;
struct SHShaderCameraData
{
@ -63,7 +64,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Constructor/Destructors */
/*-----------------------------------------------------------------------------*/
SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph);
SHRenderer(Handle<SHVkLogicalDevice> logicalDevice, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHViewport> viewport, Handle<SHRenderGraph> renderGraph);
/*-----------------------------------------------------------------------------*/
/* Camera Registration */
@ -73,13 +74,15 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Drawing Functions */
/*-----------------------------------------------------------------------------*/
void Draw(uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept;
void BindDescriptorSet (Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept;
void Draw(uint32_t frameIndex) noexcept;
void UpdateDataAndBind (Handle<SHVkCommandBuffer> cmdBuffer, uint32_t frameIndex) noexcept;
void UpdateCameraDataToBuffer (void) noexcept;
/*-----------------------------------------------------------------------------*/
/* Setters and Getters */
/*-----------------------------------------------------------------------------*/
Handle<SHRenderGraph> GetRenderGraph (void) const noexcept;
Handle<SHVkCommandBuffer> GetCommandBuffer(uint32_t frameIndex) const noexcept;
private:
/*-----------------------------------------------------------------------------*/
@ -92,7 +95,16 @@ namespace SHADE
Handle<SHCamera> camera;
Handle<SHRenderGraph> renderGraph;
Handle<SHVkDescriptorSetGroup> cameraDescriptorSet;
std::vector<SHShaderCameraData> cpuCameraData;
Handle<SHVkBuffer> cameraBuffer;
// we really only need 1 copy even though we need %swapchainImages copies for
// GPU.
SHShaderCameraData cpuCameraData;
//! Command buffers for the render graph
std::vector<Handle<SHVkCommandBuffer>> commandBuffers;
};
}

View File

@ -49,10 +49,10 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
/* Renderer Registration Functions */
/*---------------------------------------------------------------------------------*/
Handle<SHRenderer> SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph)
Handle<SHRenderer> SHViewport::AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph)
{
// Create the renderer
auto renderer = resourceManager.Create<SHRenderer>(device, numFrames, descriptorPool, cameraDescLayout, GetHandle(), renderGraph);
auto renderer = resourceManager.Create<SHRenderer>(device, numFrames, cmdPools, descriptorPool, cameraDescLayout, GetHandle(), renderGraph);
// Store
renderers.emplace_back(renderer);

View File

@ -32,6 +32,7 @@ namespace SHADE
class SHRenderGraph;
class SHVkDescriptorPool;
class SHVkDescriptorSetLayout;
class SHVkCommandPool;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
@ -58,7 +59,7 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
/* Renderers Registration Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHRenderer> AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph);
Handle<SHRenderer> AddRenderer(ResourceManager& resourceManager, uint32_t numFrames, std::vector<Handle<SHVkCommandPool>>& cmdPools, Handle<SHVkDescriptorPool> descriptorPool, Handle<SHVkDescriptorSetLayout> cameraDescLayout, Handle<SHRenderGraph> renderGraph);
void RemoveRenderer(Handle<SHRenderer> renderer);
/*-----------------------------------------------------------------------------*/

View File

@ -18,7 +18,7 @@ namespace SHADE
auto pipelineLayout = logicalDevice->CreatePipelineLayout(params);
// Create the pipeline and configure the default vertex input state
auto newPipeline = logicalDevice->CreatePipeline(pipelineLayout, nullptr, renderpass, subpass, SH_PIPELINE_TYPE::GRAPHICS);
auto newPipeline = logicalDevice->CreateGraphicsPipeline(pipelineLayout, nullptr, renderpass, subpass);
newPipeline->GetPipelineState().SetVertexInputState(globalData->GetDefaultViState());
// Actually construct the pipeline

View File

@ -0,0 +1,142 @@
/************************************************************************************//*!
\file SHTextureLibrary.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2022
\brief Contains definitions for all of the functions of the classes that deal
with storage and management of buffers for textures.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#include "SHpch.h"
#include "SHTextureLibrary.h"
#include "Graphics/SHVulkanIncludes.h"
#include "Graphics/Devices/SHVkLogicalDevice.h"
#include "Graphics/Buffers/SHVkBuffer.h"
#include "Graphics/Commands/SHVkCommandBuffer.h"
#include "Graphics/SHVkUtil.h"
#include "Tools/SHLogger.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
Handle<SHTexture> SHTextureLibrary::Add(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, SHTexture::TextureFormat format, uint32_t mipLevels)
{
isDirty = true;
auto handle = resourceManager.Create<SHTexture>();
addJobs.emplace_back(AddJob { pixelCount, pixelData, format, mipLevels });
return handle;
}
void SHTextureLibrary::Remove(Handle<SHTexture> texture)
{
if (!texture)
throw std::invalid_argument("Attempted to remove a Texture that did not belong to the Texture Library!");
removeJobs.emplace_back(texture);
isDirty = true;
}
void SHTextureLibrary::BuildImages(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer, Handle<SHVkQueue> graphicsQueue, Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> descLayout)
{
/* Remove Textures */
std::vector<vk::ImageMemoryBarrier> pipelineBarriers(addJobs.size());
/* Add Textures */
// Transition
for (int i = 0; auto& job : addJobs)
{
job.Image = resourceManager.Create<SHVkImage>();
job.Image->PrepareImageTransitionInfo(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, pipelineBarriers[i]);
++i;
}
vk::PipelineStageFlagBits srcStage = vk::PipelineStageFlagBits::eTopOfPipe;
vk::PipelineStageFlagBits dstStage = vk::PipelineStageFlagBits::eTopOfPipe;
preparePipelineBarriers(vk::ImageLayout::eUndefined, vk::ImageLayout::eTransferDstOptimal, srcStage, dstStage, pipelineBarriers);
cmdBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, {}, {}, pipelineBarriers);
// Copy
for (auto& job : addJobs)
{
job.Image->TransferToDeviceResource(cmdBuffer);
}
// Transition
for (int i = 0; auto & job : addJobs)
{
// Transition
job.Image->PrepareImageTransitionInfo(vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, pipelineBarriers[i]);
}
preparePipelineBarriers(vk::ImageLayout::eTransferDstOptimal, vk::ImageLayout::eShaderReadOnlyOptimal, srcStage, dstStage, pipelineBarriers);
cmdBuffer->PipelineBarrier(vk::PipelineStageFlagBits::eTopOfPipe, vk::PipelineStageFlagBits::eTransfer, {}, {}, {}, pipelineBarriers);
// Execute Commands
graphicsQueue->SubmitCommandBuffer({ cmdBuffer });
device->WaitIdle();
// Create Image View
for (auto& job : addJobs)
{
const SHImageViewDetails DETAILS
{
.viewType = vk::ImageViewType::e2D,
.format = job.TextureFormat,
.imageAspectFlags = vk::ImageAspectFlagBits::eColor,
.baseMipLevel = 0,
.mipLevelCount = job.MipLevels,
.baseArrayLayer = 0,
.layerCount = 0
};
job.Handle->ImageView = job.Image->CreateImageView(device, job.Image, DETAILS);
}
// Build Descriptor
Handle<SHVkDescriptorSetGroup> descSetGroup = descPool->Allocate({ descLayout }, { 1 });
isDirty = false;
}
/*---------------------------------------------------------------------------------*/
/* Usage Functions */
/*---------------------------------------------------------------------------------*/
void SHTextureLibrary::preparePipelineBarriers(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::PipelineStageFlagBits& srcStage, vk::PipelineStageFlagBits& dstStage, std::vector<vk::ImageMemoryBarrier>& barriers)
{
if (oldLayout == vk::ImageLayout::eUndefined && newLayout == vk::ImageLayout::eTransferDstOptimal)
{
srcStage = vk::PipelineStageFlagBits::eTopOfPipe;
dstStage = vk::PipelineStageFlagBits::eTransfer;
for (auto& barrier : barriers)
{
barrier.srcAccessMask = vk::AccessFlagBits::eNone;
barrier.dstAccessMask = vk::AccessFlagBits::eTransferWrite;
}
}
else if (oldLayout == vk::ImageLayout::eTransferDstOptimal && newLayout == vk::ImageLayout::eShaderReadOnlyOptimal)
{
srcStage = vk::PipelineStageFlagBits::eTransfer;
// TODO, what if we want to access in compute shader
dstStage = vk::PipelineStageFlagBits::eFragmentShader;
for (auto& barrier : barriers)
{
barrier.srcAccessMask = vk::AccessFlagBits::eTransferWrite;
barrier.dstAccessMask = vk::AccessFlagBits::eShaderRead;
}
}
else
{
SHLOG_ERROR("Image layouts are invalid. ");
}
}
}

View File

@ -0,0 +1,163 @@
/************************************************************************************//*!
\file SHTextureLibrary.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 20, 2022
\brief Contains definitions for all of the classes that deal with storage and
management of textures.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// STL Includes
#include <vector>
// Project Includes
#include "Resource/Handle.h"
#include "Resource/ResourceLibrary.h"
#include "Math/SHMath.h"
#include "Graphics/SHVulkanIncludes.h"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Forward Declarations */
/*---------------------------------------------------------------------------------*/
class SHVkBuffer;
class SHVkLogicalDevice;
class SHVkCommandBuffer;
class SHVkImage;
class SHVkImageView;
class SHVkQueue;
class SHVkDescriptorPool;
class SHVkDescriptorSetLayout;
/*---------------------------------------------------------------------------------*/
/* Type Definitions */
/*---------------------------------------------------------------------------------*/
class SHTexture
{
public:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
using PixelChannel = float;
using TextureFormat = vk::Format; // TODO: Change
using Index = uint32_t;
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
Handle<SHVkImageView> ImageView;
Index TextureArrayIndex = std::numeric_limits<Index>::max();
};
/***********************************************************************************/
/*!
\brief
Manages storage for all textures in the Graphics System as a single set of
textures.
*/
/***********************************************************************************/
class SHTextureLibrary
{
public:
/*-----------------------------------------------------------------------------*/
/* Usage Functions */
/*-----------------------------------------------------------------------------*/
/*******************************************************************************/
/*!
\brief
Adds a texture to the Texture Library. But this does not mean that the
textures have been added yet. A call to "BuildImages()" is required to
transfer all textures into the GPU.
\param pixelCount
Number of pixels in this Mesh.
\param positions
Pointer to the first in a contiguous array of SHMathVec3s that define vertex
positions.
\param format
Format of the texture loaded in.
\return
Handle to the created Texture. This is not valid to be used until a call to
BuildImages().
*/
/*******************************************************************************/
Handle<SHTexture> Add(uint32_t pixelCount, const SHTexture::PixelChannel* const pixelData, SHTexture::TextureFormat format, uint32_t mipLevels);
/*******************************************************************************/
/*!
\brief
Removes a mesh from the Texture Library. But this does not mean that the
textures have been removed yet. A call to "BuildImages()" is required to
finalise all changes.
\param mesh
Handle to the mesh to remove.
*/
/*******************************************************************************/
void Remove(Handle<SHTexture> mesh);
/***************************************************************************/
/*!
\brief
Finalises all changes to the Texture Library into the GPU buffers.
\param device
Device used to create and update the buffers.
\param cmdBuffer
Command buffer used to set up transfers of data in the GPU memory. This
call must be preceded by calls to cmdBuffer's BeginRecording() and ended
with EndRecording(). Do recall to also submit the cmdBuffer to a transfer
queue.
*/
/***************************************************************************/
void BuildImages(Handle<SHVkLogicalDevice> device, Handle<SHVkCommandBuffer> cmdBuffer, Handle<SHVkQueue> graphicsQueue, Handle<SHVkDescriptorPool> descPool, Handle<SHVkDescriptorSetLayout> descLayout);
/*-----------------------------------------------------------------------------*/
/* Getter Functions */
/*-----------------------------------------------------------------------------*/
Handle<SHVkBuffer> GetTextureBuffer() const noexcept { return texStorageBuffer; }
private:
/*-----------------------------------------------------------------------------*/
/* Type Definition */
/*-----------------------------------------------------------------------------*/
struct AddJob
{
uint32_t PixelCount = 0;
const SHTexture::PixelChannel* PixelData = nullptr;
SHTexture::TextureFormat TextureFormat = {};
uint32_t MipLevels = 0;
Handle<SHVkImage> Image;
Handle<SHTexture> Handle;
};
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
// Manipulation Queues
std::vector<AddJob> addJobs;
std::vector<Handle<SHTexture>> removeJobs;
// Tracking
ResourceManager resourceManager;
std::vector<Handle<SHTexture>> texOrder;
// CPU Storage
std::vector<SHTexture::PixelChannel> texStorage;
// GPU Storage
Handle<SHVkBuffer> texStorageBuffer{};
// Flags
bool isDirty = true;
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
void preparePipelineBarriers(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::PipelineStageFlagBits& srcStage, vk::PipelineStageFlagBits& dstStage, std::vector<vk::ImageMemoryBarrier>& barriers);
};
}

View File

@ -25,6 +25,15 @@ namespace SHADE
//! want to use it for allocating descriptor sets.
std::vector<Handle<SHVkDescriptorSetLayout>> const& globalDescSetLayouts = {};
};
struct SHPipelineLayoutParamsDummy
{
/*-----------------------------------------------------------------------*/
/* MEMBER VARIABLES */
/*-----------------------------------------------------------------------*/
std::vector<Handle<SHVkDescriptorSetLayout>> const& globalDescSetLayouts = {};
};
}
#endif

View File

@ -64,7 +64,7 @@ namespace SHADE
vk::CullModeFlags cull_mode{ vk::CullModeFlagBits::eBack };
//! CW or CCW
vk::FrontFace frontFacingOrientation{ vk::FrontFace::eClockwise };
vk::FrontFace frontFacingOrientation{ vk::FrontFace::eCounterClockwise };
bool depthBias{ VK_FALSE };
};

View File

@ -179,7 +179,7 @@ namespace SHADE
/*!
\brief
Non-default ctor.
Non-default ctor for creating graphics pipeline.
\param inLogicalDeviceHdl
Needed for creation and destruction.
@ -200,14 +200,12 @@ namespace SHADE
The subpass that this pipeline will be used in. If state is not
nullptr, this parameter is ignored.
\param type
The type of the pipeline.
*/
/***************************************************************************/
SHVkPipeline::SHVkPipeline(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, Handle<SHVkPipelineLayout> const& inPipelineLayout, SHVkPipelineState const* const state, Handle<SHVkRenderpass> const& renderpassHdl, Handle<SHSubpass> subpass, SH_PIPELINE_TYPE type) noexcept
SHVkPipeline::SHVkPipeline(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, Handle<SHVkPipelineLayout> const& inPipelineLayout, SHVkPipelineState const* const state, Handle<SHVkRenderpass> const& renderpassHdl, Handle<SHSubpass> subpass) noexcept
: pipelineState{ }
, pipelineType {type}
, pipelineType {SH_PIPELINE_TYPE::GRAPHICS}
, vkPipeline {VK_NULL_HANDLE}
, logicalDeviceHdl{ inLogicalDeviceHdl }
, pipelineLayout { inPipelineLayout }
@ -250,6 +248,33 @@ namespace SHADE
vkPipeline = VK_NULL_HANDLE;
}
/***************************************************************************/
/*!
\brief
Just to differentiate between compute and graphics pipeline, we will
have a constructor that takes in less parameters; sufficient for the
compute pipeline to be created.
\param inLogicalDeviceHdl
\param inPipelineLayout
\return
*/
/***************************************************************************/
SHVkPipeline::SHVkPipeline(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, Handle<SHVkPipelineLayout> const& inPipelineLayout) noexcept
: pipelineState{ }
, pipelineType{ SH_PIPELINE_TYPE::COMPUTE }
, vkPipeline{ VK_NULL_HANDLE }
, logicalDeviceHdl{ inLogicalDeviceHdl }
, pipelineLayout{ inPipelineLayout }
, created{ false }
{
}
/***************************************************************************/
/*!

View File

@ -51,8 +51,10 @@ namespace SHADE
Handle<SHVkPipelineLayout> const& inPipelineLayout,
SHVkPipelineState const* const state,
Handle<SHVkRenderpass> const& renderpassHdl,
Handle<SHSubpass> subpass,
SH_PIPELINE_TYPE type) noexcept;
Handle<SHSubpass> subpass) noexcept;
SHVkPipeline(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl,
Handle<SHVkPipelineLayout> const& inPipelineLayout) noexcept;
SHVkPipeline (SHVkPipeline&& rhs) noexcept;
~SHVkPipeline (void) noexcept;

View File

@ -198,7 +198,7 @@ namespace SHADE
// 1 descriptor set layout for every descriptor set detected.
for (auto const& set : setsWithBindings)
{
auto newDescriptorSetLayout = logicalDeviceHdl->CreateDescriptorSetLayout(set.second);
auto newDescriptorSetLayout = logicalDeviceHdl->CreateDescriptorSetLayout(set.first, set.second);
descriptorSetLayoutsAllocate.push_back(newDescriptorSetLayout);
}
@ -254,12 +254,6 @@ namespace SHADE
// Clear pc ranges
vkPcRanges.clear();
// Kill all descriptor set layouts
for (auto& layout : descriptorSetLayoutsGlobal)
SHVkInstance::GetResourceManager().Free(layout);
descriptorSetLayoutsGlobal.clear();
for (auto& layout : descriptorSetLayoutsAllocate)
SHVkInstance::GetResourceManager().Free(layout);
@ -285,9 +279,9 @@ namespace SHADE
*/
/***************************************************************************/
SHVkPipelineLayout::SHVkPipelineLayout(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParams& pipelineLayoutParams) noexcept
SHVkPipelineLayout::SHVkPipelineLayout(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParams const& pipelineLayoutParams) noexcept
: vkPipelineLayout {VK_NULL_HANDLE}
, shaderModules{std::move (pipelineLayoutParams.shaderModules)}
, shaderModules{pipelineLayoutParams.shaderModules}
, logicalDeviceHdl {inLogicalDeviceHdl}
, pushConstantInterface{}
, vkPcRanges{}
@ -308,6 +302,46 @@ namespace SHADE
RecreateIfNeeded ();
}
SHVkPipelineLayout::SHVkPipelineLayout(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept
: vkPipelineLayout{ VK_NULL_HANDLE }
, shaderModules{ }
, logicalDeviceHdl{ inLogicalDeviceHdl }
, pushConstantInterface{}
, vkPcRanges{}
, descriptorSetLayoutsGlobal{}
, descriptorSetLayoutsAllocate{}
, vkDescriptorSetLayoutsAllocate{}
, vkDescriptorSetLayoutsPipeline{}
{
vkDescriptorSetLayoutsPipeline.resize(pipelineLayoutParams.globalDescSetLayouts.size());
for (uint32_t i = 0; auto& layout : vkDescriptorSetLayoutsPipeline)
{
layout = pipelineLayoutParams.globalDescSetLayouts[i]->GetVkHandle();
++i;
}
vk::PipelineLayoutCreateInfo plCreateInfo{};
// Set push constant data to pipeline layout
plCreateInfo.pushConstantRangeCount = 0;
plCreateInfo.pPushConstantRanges = nullptr;
// To initialize the descriptor set layouts for the pipeline layout.
plCreateInfo.setLayoutCount = static_cast<uint32_t>(vkDescriptorSetLayoutsPipeline.size());
plCreateInfo.pSetLayouts = vkDescriptorSetLayoutsPipeline.data();
if (auto const RESULT = logicalDeviceHdl->GetVkLogicalDevice().createPipelineLayout(&plCreateInfo, nullptr, &vkPipelineLayout); RESULT != vk::Result::eSuccess)
{
SHVulkanDebugUtil::ReportVkError(RESULT, "Failed to create Pipeline Layout. ");
return;
}
else
SHVulkanDebugUtil::ReportVkSuccess("Successfully created Pipeline Layout. ");
}
/***************************************************************************/
/*!

View File

@ -57,7 +57,8 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* CTORS AND DTORS */
/*-----------------------------------------------------------------------*/
SHVkPipelineLayout (Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParams& pipelineLayoutParams) noexcept;
SHVkPipelineLayout(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParams const& pipelineLayoutParams) noexcept;
SHVkPipelineLayout(Handle<SHVkLogicalDevice> const& inLogicalDeviceHdl, SHPipelineLayoutParamsDummy const& pipelineLayoutParams) noexcept;
SHVkPipelineLayout (SHVkPipelineLayout&& rhs) noexcept;
~SHVkPipelineLayout (void) noexcept;
SHVkPipelineLayout& operator= (SHVkPipelineLayout&& rhs) noexcept;

View File

@ -346,10 +346,13 @@ namespace SHADE
inputReferences.push_back({ resourceAttachmentMapping->at(ptrToResources->at(resourceToReference).GetId().Raw), vk::ImageLayout::eShaderReadOnlyOptimal });
}
void SHSubpass::Execute(Handle<SHVkCommandBuffer>& commandBuffer) noexcept
void SHSubpass::Execute(Handle<SHVkCommandBuffer>& commandBuffer, uint32_t frameIndex) noexcept
{
// Ensure correct transforms are provided
superBatch->UpdateTransformBuffer(frameIndex);
// Draw all the batches
superBatch->Draw(commandBuffer);
superBatch->Draw(commandBuffer, frameIndex);
// Draw all the exterior draw calls
for (auto& drawCall : exteriorDrawCalls)
@ -588,7 +591,7 @@ namespace SHADE
for (uint32_t i = 0; i < subpasses.size(); ++i)
{
subpasses[i]->Execute(commandBuffer);
subpasses[i]->Execute(commandBuffer, frameIndex);
// Go to next subpass if not last subpass
if (i != subpasses.size() - 1)
@ -621,9 +624,9 @@ namespace SHADE
return pipeline;
}
void SHRenderGraphNode::FinaliseBatch()
{
batcher.FinaliseBatches(logicalDeviceHdl);
void SHRenderGraphNode::FinaliseBatch(uint32_t frameIndex)
{
batcher.FinaliseBatches(logicalDeviceHdl, frameIndex);
}
/***************************************************************************/
@ -946,25 +949,6 @@ namespace SHADE
}
}
/***************************************************************************/
/*!
\brief
Configures command pools and command buffers.
*/
/***************************************************************************/
void SHRenderGraph::ConfigureCommands(void) noexcept
{
//commandPools.resize (static_cast<std::size_t>(swapchainHdl->GetNumImages()));
commandBuffers.resize(static_cast<std::size_t>(swapchainHdl->GetNumImages()));
for (uint32_t i = 0; i < commandBuffers.size(); ++i)
{
commandBuffers[i] = commandPools[i]->RequestCommandBuffer(SH_CMD_BUFFER_TYPE::PRIMARY);
}
}
/***************************************************************************/
/*!
@ -980,12 +964,11 @@ namespace SHADE
*/
/***************************************************************************/
void SHRenderGraph::Init(Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<Handle<SHVkCommandPool>> const& cmdPools, Handle<SHGraphicsGlobalData> inGlobalData) noexcept
void SHRenderGraph::Init(Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, Handle<SHGraphicsGlobalData> inGlobalData) noexcept
{
logicalDeviceHdl = logicalDevice;
swapchainHdl = swapchain;
globalData = inGlobalData;
commandPools = cmdPools;
}
/***************************************************************************/
@ -1087,40 +1070,24 @@ namespace SHADE
ConfigureSubpasses();
ConfigureRenderpasses();
ConfigureFramebuffers();
ConfigureCommands();
}
// TODO: The graph scope buffers were meant to bind vertex buffers and index buffers for meshes. Find a
// better way to manage these
void SHRenderGraph::Execute(uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept
void SHRenderGraph::Execute(uint32_t frameIndex, Handle<SHVkCommandBuffer> cmdBuffer) noexcept
{
//commandPools[frameIndex]->Reset();
auto& cmdBuffer = commandBuffers[frameIndex];
cmdBuffer->BeginRecording();
// TODO: DON'T HARDCODE THIS
cmdBuffer->SetViewportScissor(1920.0f, 1080.0f, 1920, 1080);
for (auto& [buffer, bindingPoint]: graphScopeBuffers)
{
if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eVertexBuffer)
cmdBuffer->BindVertexBuffer(bindingPoint, buffer, 0);
else if (buffer->GetUsageBits() & vk::BufferUsageFlagBits::eIndexBuffer)
cmdBuffer->BindIndexBuffer(buffer, 0);
}
for (auto& node : nodes)
node->Execute(cmdBuffer, frameIndex);
cmdBuffer->EndRecording();
}
void SHRenderGraph::FinaliseBatch()
{
void SHRenderGraph::FinaliseBatch(uint32_t frameIndex)
{
for (auto& node : nodes)
{
node->FinaliseBatch();
node->FinaliseBatch(frameIndex);
}
}
@ -1132,9 +1099,5 @@ namespace SHADE
return {};
}
Handle<SHVkCommandBuffer> const& SHRenderGraph::GetCommandBuffer(uint32_t frameIndex) const noexcept
{
return commandBuffers[frameIndex];
}
}

View File

@ -137,7 +137,7 @@ namespace SHADE
void AddInput(std::string resourceToReference) noexcept;
// Runtime functions
void Execute(Handle<SHVkCommandBuffer>& commandBuffer) noexcept;
void Execute(Handle<SHVkCommandBuffer>& commandBuffer, uint32_t frameIndex) noexcept;
void AddExteriorDrawCalls(std::function<void(Handle<SHVkCommandBuffer>&)> const& newDrawCall) noexcept;
/*-----------------------------------------------------------------------*/
@ -230,7 +230,7 @@ namespace SHADE
// TODO: RemoveSubpass()
void Execute (Handle<SHVkCommandBuffer>& commandBuffer, uint32_t frameIndex) noexcept;
Handle<SHVkPipeline> GetOrCreatePipeline (std::pair<Handle<SHVkShaderModule>, Handle<SHVkShaderModule>> const& vsFsPair, Handle<SHSubpass> subpass) noexcept;
void FinaliseBatch();
void FinaliseBatch(uint32_t frameIndex);
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */
@ -250,7 +250,6 @@ namespace SHADE
void ConfigureSubpasses (void) noexcept;
void ConfigureRenderpasses (void) noexcept;
void ConfigureFramebuffers (void) noexcept;
void ConfigureCommands (void) noexcept;
/*-----------------------------------------------------------------------*/
/* PRIVATE MEMBER VARIABLES */
@ -271,12 +270,6 @@ namespace SHADE
//! Resource library for graph handles
ResourceManager resourceManager;
//! Command pool for the render graph. DO NOT RESET OR FREE THESE, THEY ARE NON-OWNING. TODO: If application is multithreaded, we need more pools.
std::vector<Handle<SHVkCommandPool>> commandPools;
//! Command buffers for the render graph
std::vector<Handle<SHVkCommandBuffer>> commandBuffers;
//! Handle to global data
Handle<SHGraphicsGlobalData> globalData;
@ -290,18 +283,17 @@ namespace SHADE
/*-----------------------------------------------------------------------*/
/* PUBLIC MEMBER FUNCTIONS */
/*-----------------------------------------------------------------------*/
void Init (Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, std::vector<Handle<SHVkCommandPool>> const& cmdPools, Handle<SHGraphicsGlobalData> inGlobalData) noexcept;
void Init (Handle<SHVkLogicalDevice> const& logicalDevice, Handle<SHVkSwapchain> const& swapchain, Handle<SHGraphicsGlobalData> inGlobalData) noexcept;
void AddResource (std::string resourceName, SH_ATT_DESC_TYPE type, uint32_t w = static_cast<uint32_t>(-1), uint32_t h = static_cast<uint32_t>(-1), vk::Format format = vk::Format::eB8G8R8A8Unorm, uint8_t levels = 1, vk::ImageCreateFlagBits createFlags = {});
Handle<SHRenderGraphNode> AddNode (std::string nodeName, std::initializer_list<std::string> resourceNames, std::initializer_list<std::string> predecessorNodes) noexcept;
void Generate (void) noexcept;
void Execute (uint32_t frameIndex, std::initializer_list<std::pair<Handle<SHVkBuffer>, uint32_t>> graphScopeBuffers) noexcept;
void FinaliseBatch();
void Execute (uint32_t frameIndex, Handle<SHVkCommandBuffer> cmdBuffer) noexcept;
void FinaliseBatch(uint32_t frameIndex);
/*-----------------------------------------------------------------------*/
/* SETTERS AND GETTERS */
/*-----------------------------------------------------------------------*/
Handle<SHRenderGraphNode> GetNode (std::string const& nodeName) const noexcept;
Handle<SHVkCommandBuffer> const& GetCommandBuffer (uint32_t frameIndex) const noexcept;
};
}

View File

@ -71,4 +71,14 @@ namespace SHADE
);
}
}
BindingAndSetHash SHVkUtil::GenBindingSetHash(uint32_t set, uint32_t binding) noexcept
{
// Find the target writeDescSet
BindingAndSetHash writeHash = binding;
writeHash |= static_cast<uint64_t>(set) << 32;
return writeHash;
}
}

View File

@ -76,6 +76,8 @@ namespace SHADE
*/
/***********************************************************************************/
static void EnsureBufferAndCopyHostVisibleData(Handle<SHVkLogicalDevice> device, Handle<SHVkBuffer>& bufferHandle, void* src, uint32_t size, vk::BufferUsageFlagBits usage);
static BindingAndSetHash GenBindingSetHash (uint32_t set, uint32_t binding) noexcept;
};
}

View File

@ -6,6 +6,8 @@
namespace SHADE
{
using SHQueueFamilyIndex = uint32_t;
using BindingAndSetHash = uint64_t;
using SetIndex = uint32_t;
}

View File

@ -6,5 +6,6 @@
#define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
#define VULKAN_HPP_NO_NODISCARD_WARNINGS
#include <vulkan/vulkan.hpp>
#include "Graphics/SHVulkanDefines.h"
#endif

View File

@ -9,8 +9,6 @@
namespace SHADE
{
using BindingAndSetHash = uint64_t;
struct SHShaderDescriptorBindingInfo
{
private:

View File

@ -21,12 +21,16 @@
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* Static Data Member Definitions */
/*-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/
SHTransformSystem::SHTransformSystem()
: SHSystemRoutine { "Transform Routine", false }
SHTransformSystem::TransformUpdateRoutine::TransformUpdateRoutine()
: SHSystemRoutine { "Transform Update", true }
{}
@ -34,13 +38,23 @@ namespace SHADE
/* Public Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
void SHTransformSystem::Execute(double dt) noexcept
void SHTransformSystem::TransformUpdateRoutine::Execute(double) noexcept
{
// Get the current scene graph to traverse and update
const auto& SCENE_GRAPH = SHSceneManager::GetCurrentSceneGraph();
UpdateEntity(SCENE_GRAPH.GetRoot());
}
void SHTransformSystem::Init()
{
}
void SHTransformSystem::Exit()
{
}
/*-----------------------------------------------------------------------------------*/
/* Private Function Member Definitions */
/*-----------------------------------------------------------------------------------*/
@ -52,19 +66,8 @@ namespace SHADE
for (const auto* child : node->GetChildren())
{
// Active states of entities should sync with scene nodes
const bool IS_NODE_ACTIVE = child->isActive;
#ifdef _DEBUG
const bool IS_ENTITY_ACTIVE = SHEntityManager::GetEntityByID(child->GetEntityID())->GetActive();
SHASSERT(IS_NODE_ACTIVE == IS_ENTITY_ACTIVE, "Entity and Node active states are not synced!")
#endif
if (!IS_NODE_ACTIVE)
{
UpdateEntity(child);
continue;
}
const bool HAS_TRANSFORM = SHComponentManager::HasComponent<SHTransformComponent>(child->GetEntityID());
if (!HAS_TRANSFORM)
@ -72,8 +75,13 @@ namespace SHADE
auto* childTransform = SHComponentManager::GetComponent<SHTransformComponent>(child->GetEntityID());
if (childTransform->dirty || HAS_PARENT_CHANGED)
UpdateTransform(*childTransform, NODE_TRANSFORM);
// Only update if node in hierarchy and component are both active
const bool IS_NODE_ACTIVE = child->IsActive();
if (IS_NODE_ACTIVE && childTransform->isActive)
{
if (childTransform->dirty || HAS_PARENT_CHANGED)
UpdateTransform(*childTransform, NODE_TRANSFORM);
}
UpdateEntity(child);
@ -90,7 +98,7 @@ namespace SHADE
if (parent)
{
localToWorld = parent->GetTRS();
worldToLocal = SHMatrix::Inverse(tf.local.trs);
worldToLocal = SHMatrix::Inverse(localToWorld);
}
while (!tf.updateQueue.empty())
@ -134,6 +142,10 @@ namespace SHADE
tf.world.scale = tf.local.scale * (parent ? parent->GetLocalScale() : SHVec3::One);
tf.world.ComputeTRS();
// Transpose TRS to column major
tf.local.trs.Transpose();
tf.world.trs.Transpose();
}
} // namespace SHADE

View File

@ -21,19 +21,18 @@ namespace SHADE
/* Type Definitions */
/*-----------------------------------------------------------------------------------*/
class SH_API SHTransformSystem : public SHSystemRoutine
class SH_API SHTransformSystem final : public SHSystem
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/
SHTransformSystem ();
~SHTransformSystem () = default;
SHTransformSystem () = default;
~SHTransformSystem () override = default;
SHTransformSystem (const SHTransformSystem&) = delete;
SHTransformSystem (SHTransformSystem&&) = delete;
SHTransformSystem (const SHTransformSystem&) = delete;
SHTransformSystem (SHTransformSystem&&) = delete;
/*---------------------------------------------------------------------------------*/
/* Operator Overloads */
@ -42,11 +41,43 @@ namespace SHADE
SHTransformSystem& operator= (const SHTransformSystem&) = delete;
SHTransformSystem& operator= (SHTransformSystem&&) = delete;
/*---------------------------------------------------------------------------------*/
/* System Routines */
/*---------------------------------------------------------------------------------*/
class SH_API TransformUpdateRoutine final: public SHSystemRoutine
{
public:
/*-------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*-------------------------------------------------------------------------------*/
TransformUpdateRoutine ();
~TransformUpdateRoutine () = default;
TransformUpdateRoutine (const TransformUpdateRoutine&) = delete;
TransformUpdateRoutine (TransformUpdateRoutine&&) = delete;
/*-------------------------------------------------------------------------------*/
/* Operator Overloads */
/*-------------------------------------------------------------------------------*/
TransformUpdateRoutine& operator= (const TransformUpdateRoutine&) = delete;
TransformUpdateRoutine& operator= (TransformUpdateRoutine&&) = delete;
/*-------------------------------------------------------------------------------*/
/* Function Members */
/*-------------------------------------------------------------------------------*/
void Execute(double dt) noexcept override;
};
/*---------------------------------------------------------------------------------*/
/* Function Members */
/*---------------------------------------------------------------------------------*/
void Execute(double dt) noexcept override;
void Init () override;
void Exit () override;
private:
/*---------------------------------------------------------------------------------*/

View File

@ -25,14 +25,14 @@ namespace SHADE
/*-----------------------------------------------------------------------------------*/
SHSceneNode::SHSceneNode(EntityID eid, SHSceneNode* parent) noexcept
: isActive { true }
: active { true }
, entityID { eid }
, parent { parent }
{}
SHSceneNode::SHSceneNode(const SHSceneNode& rhs) noexcept
: isActive { rhs.isActive }
: active { rhs.active }
, entityID { rhs.entityID }
, parent { rhs.parent }
{
@ -40,7 +40,7 @@ namespace SHADE
}
SHSceneNode::SHSceneNode(SHSceneNode&& rhs) noexcept
: isActive { rhs.isActive }
: active { rhs.active }
, entityID { rhs.entityID }
, parent { rhs.parent }
{
@ -52,9 +52,9 @@ namespace SHADE
if (this == &rhs)
return *this;
isActive = rhs.isActive;
entityID = rhs.entityID;
parent = rhs.parent;
active = rhs.active;
entityID = rhs.entityID;
parent = rhs.parent;
children.clear();
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
@ -64,9 +64,9 @@ namespace SHADE
SHSceneNode& SHSceneNode::operator=(SHSceneNode&& rhs) noexcept
{
isActive = rhs.isActive;
entityID = rhs.entityID;
parent = rhs.parent;
active = rhs.active;
entityID = rhs.entityID;
parent = rhs.parent;
children.clear();
std::ranges::copy(rhs.children.begin(), rhs.children.end(), std::back_inserter(children));
@ -104,6 +104,11 @@ namespace SHADE
/* Getter Function Definitions */
/*-----------------------------------------------------------------------------------*/
bool SHSceneNode::IsActive() const noexcept
{
return active;
}
EntityID SHSceneNode::GetEntityID() const noexcept
{
return entityID;
@ -154,7 +159,7 @@ namespace SHADE
if (root != nullptr)
return root;
SHLOG_WARNING("Scene has no root object!")
SHLOG_ERROR("Scene has no root object!")
return nullptr;
}
@ -171,7 +176,7 @@ namespace SHADE
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to Get Scene node!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to Get Scene node!", entityID)
return nullptr;
}
////////////////////////////////////////
@ -192,7 +197,7 @@ namespace SHADE
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to get Parent node!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to get Parent node!", entityID)
return nullptr;
}
////////////////////////////////////////
@ -213,7 +218,7 @@ namespace SHADE
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene!", entityID)
return nullptr;
}
@ -248,7 +253,7 @@ namespace SHADE
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene!", entityID)
return nullptr;
}
////////////////////////////////////////
@ -269,7 +274,7 @@ namespace SHADE
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene!", entityID)
return root->GetChildren();
}
////////////////////////////////////////
@ -277,6 +282,27 @@ namespace SHADE
return NODE_ITER->second->GetChildren();
}
bool SHSceneGraph::IsActiveInHierarchy(EntityID entityID) const noexcept
{
////////////////////////////////////////
// Error handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid!", entityID)
return false;
}
const auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene!", entityID)
return false;
}
////////////////////////////////////////
return NODE_ITER->second->IsActive();
}
/*-----------------------------------------------------------------------------------*/
/* Setter Function Definitions */
/*-----------------------------------------------------------------------------------*/
@ -289,7 +315,7 @@ namespace SHADE
}
// Handle self assignment
if (parentNode == parent)
if (parent && parentNode->entityID == parent->entityID)
return;
if (parent)
@ -300,6 +326,16 @@ namespace SHADE
parent->AddChild(this);
}
void SHSceneNode::SetActive(bool newActiveState) noexcept
{
active = newActiveState;
for (auto* child : children)
{
SetActive(newActiveState);
}
}
void SHSceneGraph::SetParent(EntityID entityID, SHSceneNode* parent) const noexcept
{
////////////////////////////////////////
@ -318,6 +354,9 @@ namespace SHADE
}
////////////////////////////////////////
if (parent == nullptr)
parent = root;
NODE_ITER->second->SetParent(parent);
}
@ -340,14 +379,14 @@ namespace SHADE
auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to set parent!", entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to set parent!", entityID)
return;
}
auto PARENT_ITER = entityNodeMap.find(parent);
if (PARENT_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID)
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID)
return;
}
////////////////////////////////////////
@ -458,9 +497,16 @@ namespace SHADE
SHSceneNode* newNode = AllocateNode(entityID);
if (parent == nullptr)
{
// Specific handling for root to avoid a warning when removing a non-existent child
parent = root;
newNode->SetParent(parent);
newNode->parent = root;
root->children.emplace_back(newNode);
}
else
{
newNode->SetParent(parent);
}
return newNode;
}
@ -476,7 +522,7 @@ namespace SHADE
auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_WARNING("Entity {} does not exist in the scene!", entityID)
SHLOG_ERROR("Entity {} does not exist in the scene!", entityID)
return false;
}

View File

@ -38,12 +38,6 @@ namespace SHADE
friend class SHSceneGraph;
public:
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
bool isActive;
/*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/
@ -60,6 +54,7 @@ namespace SHADE
/* Getter Functions */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] bool IsActive () const noexcept;
[[nodiscard]] EntityID GetEntityID () const noexcept;
[[nodiscard]] SHSceneNode* GetParent () const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren () const noexcept;
@ -70,7 +65,8 @@ namespace SHADE
/* Setter Functions */
/*---------------------------------------------------------------------------------*/
void SetParent (SHSceneNode* parentNode) noexcept;
void SetParent (SHSceneNode* parentNode) noexcept;
void SetActive (bool newActiveState) noexcept;
/*---------------------------------------------------------------------------------*/
/* Function Members */
@ -88,6 +84,7 @@ namespace SHADE
/* Data Members */
/*---------------------------------------------------------------------------------*/
bool active;
EntityID entityID;
SHSceneNode* parent;
std::vector<SHSceneNode*> children;
@ -121,12 +118,14 @@ namespace SHADE
/* Getter Functions */
/*---------------------------------------------------------------------------------*/
[[nodiscard]] const SHSceneNode* GetRoot () const noexcept;
[[nodiscard]] SHSceneNode* GetNode (EntityID entityID) const noexcept;
[[nodiscard]] SHSceneNode* GetParent (EntityID entityID) const noexcept;
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, SHSceneNode* childNode) const noexcept;
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, EntityID childEntityID) const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren (EntityID entityID) const noexcept;
[[nodiscard]] const SHSceneNode* GetRoot () const noexcept;
[[nodiscard]] SHSceneNode* GetNode (EntityID entityID) const noexcept;
[[nodiscard]] SHSceneNode* GetParent (EntityID entityID) const noexcept;
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, SHSceneNode* childNode) const noexcept;
[[nodiscard]] SHSceneNode* GetChild (EntityID entityID, EntityID childEntityID) const noexcept;
[[nodiscard]] const std::vector<SHSceneNode*>& GetChildren (EntityID entityID) const noexcept;
[[nodiscard]] bool IsActiveInHierarchy (EntityID entityID) const noexcept;
/*---------------------------------------------------------------------------------*/
/* Setter Functions */

View File

@ -16,7 +16,6 @@
#include "SHScene.h"
#include <functional>
#include "SH_API.h"
//Project Headers
#include "SH_API.h"

View File

@ -16,39 +16,27 @@ of DigiPen Institute of Technology is prohibited.
// Standard Library
#include <fstream> // std::fstream
#include <filesystem> // std::filesystem::canonical, std::filesystem::remove
#include <memory> // std::shared_ptr
// Project Headers
#include "Tools/SHLogger.h"
#include "Tools/SHStringUtils.h"
#include "ECS_Base/Events/SHEntityDestroyedEvent.h"
#include "Events/SHEvent.h"
#include "Events/SHEventReceiver.h"
#include "Events/SHEventManager.hpp"
namespace SHADE
{
/*--------------------------------------------------------------------------------*/
/* Static Definitions */
/*--------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Static Definitions */
/*----------------------------------------------------------------------------------*/
const std::string SHScriptEngine::DEFAULT_CSHARP_NAMESPACE = std::string("SHADE");
SHDotNetRuntime SHScriptEngine::dotNet { false };
SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineInit = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineLoadScripts = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineUnloadScripts = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineReloadScripts = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csEngineExit = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsFrameSetUp = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteFixedUpdate = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteUpdate = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsExecuteLateUpdate = nullptr;
SHScriptEngine::CsFuncPtr SHScriptEngine::csScriptsFrameCleanUp = nullptr;
SHScriptEngine::CsScriptManipFuncPtr SHScriptEngine::csScriptsAdd = nullptr;
SHScriptEngine::CsScriptBasicFuncPtr SHScriptEngine::csScriptsRemoveAll = nullptr;
SHScriptEngine::CsScriptOptionalFuncPtr SHScriptEngine::csScriptsRemoveAllImmediately = nullptr;
SHScriptEngine::CsScriptSerialiseFuncPtr SHScriptEngine::csScriptsSerialise = nullptr;
SHScriptEngine::CsScriptDeserialiseFuncPtr SHScriptEngine::csScriptDeserialise = nullptr;
SHScriptEngine::CsScriptSerialiseYamlFuncPtr SHScriptEngine::csScriptsSerialiseYaml = nullptr;
SHScriptEngine::CsScriptSerialiseYamlFuncPtr SHScriptEngine::csScriptDeserialiseYaml = nullptr;
SHScriptEngine::CsScriptEditorFuncPtr SHScriptEngine::csEditorRenderScripts = nullptr;
const std::string SHScriptEngine::CSPROJ_DIR = "..\\..\\TempScriptsFolder";
const std::string SHScriptEngine::CSPROJ_PATH = std::string(CSPROJ_DIR) + "\\SHADE_Scripting.csproj";
/*---------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Lifecycle Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::Init()
{
// Do not allow initialization if already initialised
@ -71,15 +59,9 @@ namespace SHADE
// Initialise the CSharp Engine
csEngineInit();
// Link events
// - Entity Destruction
/*onEntityDestroy = [this](const SHEntity& e)
{
csScriptsRemoveAll(e.GetEID());
csGOLibNotifyDestroyEntity(e.GetEID());
};
ECS::OnEntityDestroy += onEntityDestroy;*/
// Register entity creation events
registerEvents();
}
void SHScriptEngine::UnloadScriptAssembly()
{
@ -118,26 +100,26 @@ namespace SHADE
dotNet.Exit();
}
/*---------------------------------------------------------------------------------*/
/* Script Manipulation Functions */
/*---------------------------------------------------------------------------------*/
bool SHScriptEngine::AddScript(const SHEntity& entity, const std::string_view& scriptName)
/*-----------------------------------------------------------------------------------*/
/* Script Manipulation Functions */
/*-----------------------------------------------------------------------------------*/
bool SHScriptEngine::AddScript(EntityID entity, const std::string_view& scriptName)
{
return csScriptsAdd(entity.GetEID(), scriptName.data());
return csScriptsAdd(entity, scriptName.data());
}
void SHScriptEngine::RemoveAllScripts(const SHEntity& entity)
void SHScriptEngine::RemoveAllScripts(EntityID entity)
{
csScriptsRemoveAll(entity.GetEID());
csScriptsRemoveAll(entity);
}
void SHScriptEngine::RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy)
void SHScriptEngine::RemoveAllScriptsImmediately(EntityID entity, bool callOnDestroy)
{
csScriptsRemoveAllImmediately(entity.GetEID(), callOnDestroy);
csScriptsRemoveAllImmediately(entity, callOnDestroy);
}
/*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*---------------------------------------------------------------------------------*/
std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity)
std::string SHScriptEngine::SerialiseScripts(const SHEntity& entity) const
{
// Create buffer needed to store serialised script data
constexpr int BUFFER_SIZE = 10240;
@ -159,41 +141,40 @@ namespace SHADE
return result;
}
/*---------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*---------------------------------------------------------------------------------*/
void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml)
/*-----------------------------------------------------------------------------------*/
/* Script Serialisation Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::DeserialiseScript(const SHEntity& entity, const std::string& yaml) const
{
csScriptDeserialise(entity.GetEID(), yaml.c_str());
}
/*---------------------------------------------------------------------------------*/
/* Script Editor Functions */
/*---------------------------------------------------------------------------------*/
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity)
/*-----------------------------------------------------------------------------------*/
/* Script Editor Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::RenderScriptsInInspector(const SHEntity& entity) const
{
csEditorRenderScripts(entity.GetEID());
}
/*---------------------------------------------------------------------------------*/
/* Static Utility Functions */
/*---------------------------------------------------------------------------------*/
bool SHScriptEngine::BuildScriptAssembly(bool debug)
/*-----------------------------------------------------------------------------------*/
/* Static Utility Functions */
/*-----------------------------------------------------------------------------------*/
bool SHScriptEngine::BuildScriptAssembly(bool debug) const
{
constexpr std::string_view BUILD_LOG_PATH = "../Build.log";
static const std::string BUILD_LOG_PATH = "../Build.log";
// Generate csproj file if it doesn't exist
static const std::filesystem::path CSPROJ_PATH = "../SHADE_Scripting.csproj";
if (!std::filesystem::exists(CSPROJ_PATH))
{
GenerateScriptsCsProjFile(CSPROJ_PATH);
}
// Prepare directory (delete useless files)
deleteFolder("net5.0");
deleteFolder("ref");
deleteFolder("../SHADE_Scripting");
deleteFolder("../obj");
deleteFolder(CSPROJ_DIR + "\\net5.0");
deleteFolder(CSPROJ_DIR + "\\ref");
deleteFolder(CSPROJ_DIR + "\\obj");
deleteFolder(CSPROJ_DIR + "\\bin");
// Attempt to build the assembly
std::ostringstream oss;
@ -203,7 +184,7 @@ namespace SHADE
const bool BUILD_SUCCESS = execProcess
(
L"C:\\Windows\\system32\\cmd.exe",
L"/K \"dotnet build \"../SHADE_Scripting.csproj\" -c Debug -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet & exit\""
L"/K \"" + generateBuildCommand(debug) + L" & exit\""
) == 0;
if (BUILD_SUCCESS)
{
@ -221,6 +202,7 @@ namespace SHADE
// Clean up built files
deleteFolder("./tmp");
deleteFolder(CSPROJ_DIR + "\\obj");
// Read the build log and output to the console
dumpBuildLog(BUILD_LOG_PATH);
@ -228,9 +210,9 @@ namespace SHADE
deleteFile(BUILD_LOG_PATH);
return BUILD_SUCCESS;
}
}
void SHScriptEngine::GenerateScriptsCsProjFile(const std::filesystem::path& path)
void SHScriptEngine::GenerateScriptsCsProjFile(const std::filesystem::path& path) const
{
// Sample
static std::string_view FILE_CONTENTS =
@ -241,11 +223,11 @@ namespace SHADE
<Configurations>Release;Debug</Configurations>\n\
</PropertyGroup>\n\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Release|x64'\">\n\
<OutputPath>.\\bin_Release-x64</OutputPath>\n\
<OutputPath>.\\bin\\Release</OutputPath>\n\
<PlatformTarget>x64</PlatformTarget>\n\
</PropertyGroup>\n\
<PropertyGroup Condition=\"'$(Configuration)|$(Platform)'=='Debug|x64'\"> \n\
<OutputPath>.\\bin_Debug-x64</OutputPath>\n\
<OutputPath>.\\bin\\Debug</OutputPath>\n\
<PlatformTarget>x64</PlatformTarget>\n\
<DefineConstants>DEBUG;TRACE</DefineConstants>\n\
<Optimize>false</Optimize>\n\
@ -264,7 +246,8 @@ namespace SHADE
</ItemGroup>\n\
<ItemGroup>\n\
<Reference Include=\"SHADE_Managed\">\n\
<HintPath>.\\bin\\SHADE_Managed.dll</HintPath>\n\
<HintPath Condition=\"Exists('..\\bin\\Debug\\SHADE_Managed.dll')\">..\\bin\\Debug\\SHADE_Managed.dll</HintPath>\
<HintPath Condition=\"Exists('..\\bin\\Release\\SHADE_Managed.dll')\">..\\bin\\Release\\SHADE_Managed.dll</HintPath>\
</Reference>\n\
</ItemGroup>\n\
</Project>";
@ -273,17 +256,27 @@ namespace SHADE
std::ofstream file(path);
if (!file.is_open())
throw std::runtime_error("Unable to create CsProj file!");
// Fill the file
file << FILE_CONTENTS;
// Close
file.close();
}
}
/*---------------------------------------------------------------------------------*/
/* Helper Functions */
/*---------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/* Event Handler Functions */
/*-----------------------------------------------------------------------------------*/
SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHEntityDestroyedEvent>*>(eventPtr.get());
csScriptsRemoveAll(eventData->data->eid);
return eventData->handle;
}
/*-----------------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------------*/
void SHScriptEngine::loadFunctions()
{
std::ostringstream oss;
@ -401,6 +394,17 @@ namespace SHADE
);*/
}
void SHScriptEngine::registerEvents()
{
// Register for entity destroyed event
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> destroyedEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onEntityDestroyed)
};
ReceiverPtr receiver = std::dynamic_pointer_cast<SHEventReceiver>(destroyedEventReceiver);
SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, receiver);
}
void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath)
{
std::ifstream buildLog(buildLogPath);
@ -423,25 +427,25 @@ namespace SHADE
}
}
}
void SHScriptEngine::deleteFile(const std::string_view& filePath)
void SHScriptEngine::deleteFile(const std::string& filePath)
{
try
{
std::filesystem::remove(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
void SHScriptEngine::deleteFolder(const std::string_view& filePath)
try
{
try
{
std::filesystem::remove_all(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
std::filesystem::remove(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
void SHScriptEngine::deleteFolder(const std::string& filePath)
{
try
{
std::filesystem::remove_all(std::filesystem::canonical(filePath));
}
catch (...) {} // Ignore deletion failures
}
bool SHScriptEngine::fileExists(const std::string_view& filePath)
bool SHScriptEngine::fileExists(const std::filesystem::path& filePath)
{
std::error_code error;
if (std::filesystem::exists(filePath, error))
@ -483,8 +487,8 @@ namespace SHADE
DWORD status;
while (true)
{
const auto SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status);
if (!SUCCESS)
const auto EXEC_SUCCESS = GetExitCodeProcess(procInfo.hProcess, &status);
if (!EXEC_SUCCESS)
{
auto err = GetLastError();
std::ostringstream oss;
@ -503,4 +507,12 @@ namespace SHADE
}
}
std::wstring SHScriptEngine::generateBuildCommand(bool debug)
{
std::wostringstream oss;
oss << "dotnet build \"" << SHStringUtils::StrToWstr(CSPROJ_PATH) << "\" -c ";
oss << debug ? "Debug" : "Release";
oss << " -o \"./tmp/\" -fl -flp:LogFile=build.log;Verbosity=quiet";
return oss.str();
}
}

View File

@ -15,10 +15,14 @@ of DigiPen Institute of Technology is prohibited.
#include <filesystem>
// Project Headers
#include "SH_API.h"
#include "SHDotNetRuntime.h"
#include "ECS_Base/SHECSMacros.h"
#include "ECS_Base/Entity/SHEntity.h"
#include "SH_API.h"
#include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h"
#include "Events/SHEventDefines.h"
#include "Events/SHEvent.h"
namespace SHADE
{
@ -26,13 +30,41 @@ namespace SHADE
/// Manages initialisation of the DotNetRuntime and interfacing with CLR code written
/// and executed on .NET.
/// </summary>
class SH_API SHScriptEngine
class SH_API SHScriptEngine final : public SHSystem
{
public:
/*-----------------------------------------------------------------------------*/
/* Type Definitions */
/*-----------------------------------------------------------------------------*/
class SH_API FrameSetUpRoutine final : public SHSystemRoutine
{
public:
FrameSetUpRoutine();
void Execute(double dt) noexcept override final;
};
class SH_API UpdateRoutine final : public SHSystemRoutine
{
public:
UpdateRoutine();
void Execute(double dt) noexcept override final;
};
class SH_API LateUpdateRoutine final : public SHSystemRoutine
{
public:
LateUpdateRoutine();
void Execute(double dt) noexcept override final;
};
class SH_API FrameCleanUpRoutine final : public SHSystemRoutine
{
public:
FrameCleanUpRoutine();
void Execute(double dt) noexcept override final;
};
/*-----------------------------------------------------------------------------*/
/* Constructor */
/*-----------------------------------------------------------------------------*/
SHScriptEngine() = delete;
SHScriptEngine() = default;
/*-----------------------------------------------------------------------------*/
/* Lifecycle Functions */
@ -41,33 +73,33 @@ namespace SHADE
/// Initialises the DotNetRuntime and retrieves function pointers to all
/// functions on the CLR used to interface with the engine.
/// </summary>
static void Init();
void Init() override;
/// <summary>
/// Loads the managed script assembly. Ensure this is only called after
/// UnloadScriptAssembly() has been called.
/// </summary>
static void UnloadScriptAssembly();
void UnloadScriptAssembly();
/// <summary>
/// Unloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so.
/// </summary>
static void LoadScriptAssembly();
void LoadScriptAssembly();
/// <summary>
/// Reloads the managed script assembly.
/// Take note that this will clear all existing scripts, ensure that the scene
/// is saved before doing so.
/// </summary>
static void ReloadScriptAssembly();
void ReloadScriptAssembly();
/// <summary>
/// Executes the FixedUpdate()s of the Scripts that are attached to
/// Entities.
/// </summary>
static void ExecuteFixedUpdates();
void ExecuteFixedUpdates();
/// <summary>
/// Shuts down the DotNetRuntime.
/// </summary>
static void Exit();
void Exit() override;
/*-----------------------------------------------------------------------------*/
/* Script Manipulation Functions */
@ -84,14 +116,14 @@ namespace SHADE
/// True if successfully added. False otherwise with the error logged to the
/// console.
/// </returns>
static bool AddScript(const SHEntity& entity, const std::string_view& scriptName);
bool AddScript(EntityID entity, const std::string_view& scriptName);
/// <summary>
/// Removes all Scripts attached to the specified Entity. Does not do anything
/// if the specified Entity is invalid or does not have any Scripts
/// attached.
/// </summary>
/// <param name="entity">The entity to remove the scripts from.</param>
static void RemoveAllScripts(const SHEntity& entity);
void RemoveAllScripts(EntityID entity);
/// <summary>
/// Removes all Scripts attached to the specified Entity. Unlike
/// RemoveAllScripts(), this removes all the scripts immediately.
@ -103,7 +135,7 @@ namespace SHADE
/// Whether or not to call OnDestroy on the scripts. This is ignored if not in
/// play mode.
/// </param>
static void RemoveAllScriptsImmediately(const SHEntity& entity, bool callOnDestroy);
void RemoveAllScriptsImmediately(EntityID entity, bool callOnDestroy);
/*-----------------------------------------------------------------------------*/
/* Script Serialisation Functions */
@ -116,7 +148,7 @@ namespace SHADE
/// <returns>
/// String that represents the set of scripts attached to the specified Entity.
/// </returns>
static std::string SerialiseScripts(const SHEntity& entity);
std::string SerialiseScripts(const SHEntity& entity) const;
/// <summary>
/// Loads the specified JSON string and creates a Script for the specified Entity
/// based on the specified JSON string.
@ -125,7 +157,7 @@ namespace SHADE
/// <param name="yaml">
/// The YAML string that represents the Script to load into the Entity.
/// </param>
static void DeserialiseScript(const SHEntity& entity, const std::string& yaml);
void DeserialiseScript(const SHEntity& entity, const std::string& yaml) const;
/*-----------------------------------------------------------------------------*/
/* Script Editor Functions */
@ -138,7 +170,7 @@ namespace SHADE
/// rendering code.
/// </summary>
/// <param name="entity">The Entity to render the Scripts of.</param>
static void RenderScriptsInInspector(const SHEntity& entity);
void RenderScriptsInInspector(const SHEntity& entity) const;
/*-----------------------------------------------------------------------------*/
/* Static Utility Functions */
@ -153,12 +185,12 @@ namespace SHADE
/// can be debugged.
/// </param>
/// <returns>Whether or not the build succeeded.</returns>
static bool BuildScriptAssembly(bool debug = false);
bool BuildScriptAssembly(bool debug = false) const;
/// <summary>
/// Generates a .csproj file for editing and compiling the C# scripts.
/// </summary>
/// <param name="path">File path to the generated file.</param>
static void GenerateScriptsCsProjFile(const std::filesystem::path& path);
void GenerateScriptsCsProjFile(const std::filesystem::path& path) const;
private:
/*-----------------------------------------------------------------------------*/
@ -178,45 +210,56 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/
static constexpr std::string_view DEFAULT_CSHARP_LIB_NAME = "SHADE_Managed";
static constexpr std::string_view MANAGED_SCRIPT_LIB_NAME = "SHADE_Scripting";
static const std::string CSPROJ_DIR;
static const std::string CSPROJ_PATH;
static const std::string DEFAULT_CSHARP_NAMESPACE;
/*-----------------------------------------------------------------------------*/
/* Data Members */
/*-----------------------------------------------------------------------------*/
static SHDotNetRuntime dotNet;
SHDotNetRuntime dotNet { false };
// Function Pointers to CLR Code
// - Engine Lifecycle
static CsFuncPtr csEngineInit;
static CsFuncPtr csEngineLoadScripts;
static CsFuncPtr csEngineUnloadScripts;
static CsFuncPtr csEngineReloadScripts;
static CsFuncPtr csEngineExit;
CsFuncPtr csEngineInit = nullptr;
CsFuncPtr csEngineLoadScripts = nullptr;
CsFuncPtr csEngineUnloadScripts = nullptr;
CsFuncPtr csEngineReloadScripts = nullptr;
CsFuncPtr csEngineExit = nullptr;
// - Scripts Store
static CsFuncPtr csScriptsFrameSetUp;
static CsFuncPtr csScriptsExecuteFixedUpdate;
static CsFuncPtr csScriptsExecuteUpdate;
static CsFuncPtr csScriptsExecuteLateUpdate;
static CsFuncPtr csScriptsFrameCleanUp;
static CsScriptManipFuncPtr csScriptsAdd;
static CsScriptBasicFuncPtr csScriptsRemoveAll;
static CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately;
static CsScriptSerialiseFuncPtr csScriptsSerialise;
static CsScriptDeserialiseFuncPtr csScriptDeserialise;
static CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml;
static CsScriptSerialiseYamlFuncPtr csScriptDeserialiseYaml;
CsFuncPtr csScriptsFrameSetUp = nullptr;
CsFuncPtr csScriptsExecuteFixedUpdate = nullptr;
CsFuncPtr csScriptsExecuteUpdate = nullptr;
CsFuncPtr csScriptsExecuteLateUpdate = nullptr;
CsFuncPtr csScriptsFrameCleanUp = nullptr;
CsScriptManipFuncPtr csScriptsAdd = nullptr;
CsScriptBasicFuncPtr csScriptsRemoveAll = nullptr;
CsScriptOptionalFuncPtr csScriptsRemoveAllImmediately = nullptr;
CsScriptSerialiseFuncPtr csScriptsSerialise = nullptr;
CsScriptDeserialiseFuncPtr csScriptDeserialise = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptsSerialiseYaml = nullptr;
CsScriptSerialiseYamlFuncPtr csScriptDeserialiseYaml = nullptr;
// - Editor
static CsScriptEditorFuncPtr csEditorRenderScripts;
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
// Delegates
/*ECS::EntityEvent::Delegate onEntityCreate;
ECS::EntityEvent::Delegate onEntityDestroy;*/
/*-----------------------------------------------------------------------------*/
/* Event Handler Functions */
/*-----------------------------------------------------------------------------*/
SHEventHandle onEntityDestroyed(SHEventPtr eventPtr);
/*-----------------------------------------------------------------------------*/
/* Helper Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Loads all the function pointers to CLR code that we need to execute.
/// </summary>
static void loadFunctions();
void loadFunctions();
/// <summary>
/// Registers events for the scripting system
/// </summary>
void registerEvents();
/// <summary>
/// Reads the file via the specified path that represents a build log of error
/// and warning messages.
@ -230,18 +273,19 @@ namespace SHADE
/// Deletes the file as specified by the file path.
/// </summary>
/// <param name="filePath">File path to the file to delete.</param>
static void deleteFile(const std::string_view& filePath);
static void deleteFile(const std::string& filePath);
/// <summary>
/// Deletes the folder and all files in it as specified by the file path.
/// </summary>
/// <param name="filePath">File path to the file to delete.</param>
static void deleteFolder(const std::string_view& filePath);
static void deleteFolder(const std::string& filePath);
/// <summary>
/// Checks if a specified file exists.
/// </summary>
/// <param name="filePath">File path to the file to check.</param>
/// <returns> True if the file exists </returns>
static bool fileExists(const std::string_view& filePath);
static bool fileExists(const std::filesystem::path& filePath);
static DWORD execProcess(const std::wstring& path, const std::wstring& args);
static std::wstring generateBuildCommand(bool debug);
};
}

View File

@ -0,0 +1,63 @@
/************************************************************************************//*!
\file SHScriptEngineRoutines.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 17, 2021
\brief Contains the implementation or functions of SystemRoutines in the
SHScriptEngine class.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include <SHpch.h>
// Primary Header
#include "SHScriptEngine.h"
namespace SHADE
{
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - FrameSetUpRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::FrameSetUpRoutine::FrameSetUpRoutine()
: SHSystemRoutine("Script Engine Frame Set Up", false)
{}
void SHScriptEngine::FrameSetUpRoutine::Execute(double) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsFrameSetUp();
}
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - UpdateRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::UpdateRoutine::UpdateRoutine()
: SHSystemRoutine("Script Engine Update", false)
{}
void SHScriptEngine::UpdateRoutine::Execute(double) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteUpdate();
}
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - LateUpdateRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::LateUpdateRoutine::LateUpdateRoutine()
: SHSystemRoutine("Script Engine Late Update", false)
{}
void SHScriptEngine::LateUpdateRoutine::Execute(double) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsExecuteLateUpdate();
}
/*-----------------------------------------------------------------------------------*/
/* System Routine Functions - FrameCleanUpRoutine */
/*-----------------------------------------------------------------------------------*/
SHScriptEngine::FrameCleanUpRoutine::FrameCleanUpRoutine()
: SHSystemRoutine("Script Engine Frame Clean Up", false)
{}
void SHScriptEngine::FrameCleanUpRoutine::Execute(double) noexcept
{
reinterpret_cast<SHScriptEngine*>(system)->csScriptsFrameCleanUp();
}
}

View File

@ -0,0 +1,54 @@
/************************************************************************************//*!
\file SHLog.cpp
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 17, 2022
\brief Contains the definition of functions of the SHLog static class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Header
#include "SHpch.h"
// Primary Header
#include "SHLog.h"
// Project Includes
#include "SHLogger.h"
namespace SHADE
{
void SHLog::Info(const std::string& msg) noexcept
{
SHLOG_INFO(msg)
}
void SHLog::Warning(const std::string& msg) noexcept
{
SHLOG_WARNING(msg)
}
void SHLog::Error(const std::string& msg) noexcept
{
SHLOG_ERROR(msg)
}
void SHLog::Critical(const std::string& msg) noexcept
{
SHLOG_CRITICAL(msg)
}
void SHLog::Floor() noexcept
{
SHLOG_FLOOR()
}
#ifdef _DEBUG
void SHLog::Trace(const std::string& msg) noexcept
{
SHLOG_TRACE(msg)
}
#endif
}

View File

@ -0,0 +1,48 @@
/************************************************************************************//*!
\file SHLog.h
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 17, 2022
\brief Contains the SHLog static class.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Standard Library
#include <string>
// Project Headers
#include "SH_API.h"
namespace SHADE
{
/*!************************************************************************************
\class SHLog
\brief
Static class that contains wrapper functions for SHLogger's macros.
**************************************************************************************/
class SH_API SHLog
{
public:
/*---------------------------------------------------------------------------------*/
/* Constructor */
/*---------------------------------------------------------------------------------*/
SHLog() = delete;
/*---------------------------------------------------------------------------------*/
/* Logging Functions */
/*---------------------------------------------------------------------------------*/
static void Info(const std::string& msg) noexcept;
static void Warning(const std::string& msg) noexcept;
static void Error(const std::string& msg) noexcept;
static void Critical(const std::string& msg) noexcept;
static void Floor() noexcept;
#ifdef _DEBUG
static void Trace(const std::string& msg) noexcept;
#endif
};
}

View File

@ -21,6 +21,9 @@ of DigiPen Institute of Technology is prohibited.
#include <msclr\marshal_cppstd.h>
// External Dependencies
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Scene/SHSceneManager.h"
#include "Scene/SHSceneGraph.h"
#include "Tools/SHLog.h"
// Project Headers
#include "Utility/Convert.hxx"
#include "Utility/Debug.hxx"
@ -124,27 +127,31 @@ namespace SHADE
return T();
}
// Get Transform component and get the children list
throw gcnew System::NotImplementedException;
//Pls::Transform* tf = Pls::ECS::GetComponent<Pls::Transform>(entity);
//if (tf == nullptr)
// return T();
// Get Entity's SceneGraphNode
SHSceneNode* sceneGraphNode = SHSceneManager::GetCurrentSceneGraph().GetNode(entity);
if (sceneGraphNode == nullptr)
{
std::ostringstream oss;
oss << "[ECS_CLI] Failed to retrieve SceneGraphNode of entity #" << entity << ". This should not happen!";
SHLog::Warning(oss.str());
return T();
}
//// Search direct children first
//for (const auto& child : tf->GetChildren())
//{
// T component = GetComponent<T>(child);
// if (component != nullptr)
// return component;
//}
// Search direct children first
for (const auto& child : sceneGraphNode->GetChildren())
{
T component = GetComponent<T>(child->GetEntityID());
if (component != nullptr)
return component;
}
//// Search their children
//for (const auto& child : tf->GetChildren())
//{
// T script = GetComponentInChildren<T>(child);
// if (script != nullptr)
// return script;
//}
// Search their children
for (const auto& child : sceneGraphNode->GetChildren())
{
T component = GetComponentInChildren<T>(child->GetEntityID());
if (component != nullptr)
return component;
}
// None here
return T();

View File

@ -56,7 +56,7 @@ namespace SHADE
}
bool GameObject::IsActiveInHierarchy::get()
{
throw gcnew System::NotImplementedException();
return true; // TODO: Update once we have an equivalent on the Entity object
}
/*---------------------------------------------------------------------------------*/

View File

@ -19,7 +19,7 @@ of DigiPen Institute of Technology is prohibited.
namespace SHADE
{
///<summary>
/// CLR version of the the PlushieEngine's Vector2 class that represents a
/// CLR version of the the SHADE Engine's Vector2 class that represents a
/// 2-Dimensional Vector. Designed to closely match Unity's Vector2 struct.
/// </summary>
[System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)]

View File

@ -20,6 +20,7 @@ of DigiPen Institute of Technology is prohibited.
#include <sstream>
// External Dependencies
#include "ECS_Base/Managers/SHEntityManager.h"
#include "Tools/SHLog.h"
// Project Headers
#include "Utility/Debug.hxx"
#include "Utility/Convert.hxx"
@ -147,7 +148,6 @@ namespace SHADE
// Check if entity exists and is a valid GameObject
if (!EntityUtils::IsValid(entity))
throw gcnew System::ArgumentException("Invalid Entity provided to get a Script from.");
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
@ -155,27 +155,31 @@ namespace SHADE
return T();
}
// Get Transform component and get the children list
throw gcnew System::NotImplementedException;
//Pls::Transform* tf = Pls::ECS::GetComponent<Pls::Transform>(Convert::ToNative(entity));
//if (tf == nullptr)
// return T();
// Get Entity's SceneGraphNode
SHSceneNode* sceneGraphNode = SHSceneManager::GetCurrentSceneGraph().GetNode(entity);
if (sceneGraphNode == nullptr)
{
std::ostringstream oss;
oss << "[ECS_CLI] Failed to retrieve SceneGraphNode of entity #" << entity << ". This should not happen!";
SHLog::Warning(oss.str());
return T();
}
//// Search direct children first
//for (const auto& child : tf->GetChildren())
//{
// T script = GetScript<T>(Convert::ToCLI(child));
// if (script != nullptr)
// return script;
//}
// Search direct children first
for (const auto& child : sceneGraphNode->GetChildren())
{
T script = GetScript<T>(child->GetEntityID());
if (script != nullptr)
return script;
}
//// Search their children
//for (const auto& child : tf->GetChildren())
//{
// T script = GetScriptInChildren<T>(Convert::ToCLI(child));
// if (script != nullptr)
// return script;
//}
// Search their children
for (const auto& child : sceneGraphNode->GetChildren())
{
T script = GetScript<T>(child->GetEntityID());
if (script != nullptr)
return script;
}
// None here
return T();
@ -232,7 +236,6 @@ namespace SHADE
// Check if entity exists
if (!EntityUtils::IsValid(entity))
throw gcnew System::ArgumentException("Invalid Entity provided to remove a Script from.");
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
@ -263,8 +266,7 @@ namespace SHADE
Debug::LogError("[ScriptStore] Attempted to remove a Script from an invalid Entity!");
return false;
}
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
{
@ -285,14 +287,7 @@ namespace SHADE
}
void ScriptStore::RemoveAllScripts(Entity entity)
{
SAFE_NATIVE_CALL_BEGIN
// Check if entity exists
if (!EntityUtils::IsValid(entity))
{
Debug::LogError("[ScriptStore] Attempted to remove Scripts from an invalid Entity!");
return;
}
SAFE_NATIVE_CALL_BEGIN
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
return;
@ -309,13 +304,6 @@ namespace SHADE
void ScriptStore::RemoveAllScriptsImmediately(Entity entity, bool callOnDestroy)
{
SAFE_NATIVE_CALL_BEGIN
// Check if entity exists
if (!EntityUtils::IsValid(entity))
{
Debug::LogError("[ScriptStore] Attempted to remove Scripts from an invalid Entity!");
return;
}
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
return;
@ -384,10 +372,7 @@ namespace SHADE
while (disposalQueue.Count > 0)
{
Script^ script = disposalQueue.Dequeue();
/*if (Application::IsPlaying)
{
script->OnDestroy();
}*/
script->OnDestroy();
auto entity = script->Owner.GetEntity();
auto scriptList = scripts[script->Owner.GetEntity()];
scriptList->Remove(script);
@ -494,7 +479,6 @@ namespace SHADE
// Check if entity exists, otherwise nothing
if (!EntityUtils::IsValid(entity))
return true;
// Check if entity exists in the script storage
if (!scripts.ContainsKey(entity))
@ -665,7 +649,7 @@ namespace SHADE
// Entity Validity Check
if (nativeEntity == nullptr)
throw gcnew System::InvalidOperationException("Attempted to get native Component to an invalid Entity.");
return false;
// Check active state
return nativeEntity->GetActive();

View File

@ -0,0 +1,242 @@
/************************************************************************************//*!
\file ReflectionUtilities.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 6, 2021
\brief Contains the definition of the functions for the ReflectionUtilities
managed static class.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "Serialisation/ReflectionUtilities.hxx"
// Project Includes
#include "SerializeFieldAttribute.hxx"
#include "Utility/Convert.hxx"
#include "Math/Vector2.hxx"
#include "Math/Vector3.hxx"
#include "Utility/Debug.hxx"
/*-------------------------------------------------------------------------------------*/
/* Macro Functions */
/*-------------------------------------------------------------------------------------*/
/// <summary>
/// Macro expansion that is used in RapidJsonValueToField() to retrieve the specified
/// member of a Vector type that is stored into a Vector named "vec".
/// </summary>
/// <param name="MEMBER">The name of the member to retrieve.</param>
#define PRIMITIVE_VECTOR_FIELD_ASSIGN(MEMBER) \
iter = jsonValue.FindMember(#MEMBER); \
if (iter != jsonValue.MemberEnd()) \
{ \
vec.MEMBER = iter->value.GetDouble(); \
} \
/*-------------------------------------------------------------------------------------*/
/* Function Definitions */
/*-------------------------------------------------------------------------------------*/
namespace SHADE
{
System::Collections::Generic::IEnumerable<System::Reflection::FieldInfo^>^ ReflectionUtilities::GetInstanceFields(System::Object^ object)
{
using namespace System::Reflection;
return object->GetType()->GetFields
(
BindingFlags::Public | BindingFlags::NonPublic | BindingFlags::Instance
);
}
bool ReflectionUtilities::FieldIsSerialisable(System::Reflection::FieldInfo^ fieldInfo)
{
return fieldInfo->IsPublic || fieldInfo->GetCustomAttributes(SerializeField::typeid, true)->Length > 0;
}
/*---------------------------------------------------------------------------------*/
/* Serialisation Functions */
/*---------------------------------------------------------------------------------*/
void ReflectionUtilities::Serialise(System::Object^ object, YAML::Emitter& yaml)
{
using namespace System::Reflection;
// Create YAML object
yaml << YAML::Key << Convert::ToNative(object->GetType()->FullName);
yaml << YAML::BeginMap;
// Get all fields
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = GetInstanceFields(object);
for each (FieldInfo^ field in fields)
{
// Ignore private and non-SerialiseField
if (!FieldIsSerialisable(field))
continue;
// Serialise
writeFieldIntoYaml(field, object, yaml);
}
yaml << YAML::EndMap;
}
void ReflectionUtilities::Deserialise(YAML::Node& yamlNode, Object^ object)
{
using namespace System::Reflection;
// Load the YAML
if (!yamlNode.IsMap())
{
// Invalid
Debug::LogError
(
System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of \"{0}\" script.",
object->GetType()->FullName)
);
return;
}
// Get all fields
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = GetInstanceFields(object);
for each (FieldInfo^ field in fields)
{
// Ignore private and non-SerialiseField
if (!FieldIsSerialisable(field))
continue;
// Deserialise
const std::string FIELD_NAME = Convert::ToNative(field->Name);
if (yamlNode[FIELD_NAME])
{
writeYamlIntoField(field, object, yamlNode[FIELD_NAME]);
}
}
}
/*---------------------------------------------------------------------------------*/
/* Serialization Helper Functions */
/*---------------------------------------------------------------------------------*/
void ReflectionUtilities::writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Emitter& yaml)
{
// Field Name
yaml << YAML::Key << Convert::ToNative(fieldInfo->Name);
// Field Value
yaml << YAML::Value;
if (fieldInsertYaml<System::Int16> (fieldInfo, object, yaml) ||
fieldInsertYaml<System::Int32> (fieldInfo, object, yaml) ||
fieldInsertYaml<System::Int64> (fieldInfo, object, yaml) ||
fieldInsertYaml<System::UInt16>(fieldInfo, object, yaml) ||
fieldInsertYaml<System::UInt32>(fieldInfo, object, yaml) ||
fieldInsertYaml<System::UInt64>(fieldInfo, object, yaml) ||
fieldInsertYaml<System::Byte> (fieldInfo, object, yaml) ||
fieldInsertYaml<bool> (fieldInfo, object, yaml) ||
fieldInsertYaml<float> (fieldInfo, object, yaml) ||
fieldInsertYaml<double> (fieldInfo, object, yaml))
{
return;
}
else if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid))
{
yaml << safe_cast<int>(fieldInfo->GetValue(object));
}
else if (fieldInfo->FieldType == System::String::typeid)
{
System::String^ str = safe_cast<System::String^>(fieldInfo->GetValue(object));
yaml << Convert::ToNative(str);
}
else if (fieldInfo->FieldType == Vector2::typeid)
{
Vector2 vec = safe_cast<Vector2>(fieldInfo->GetValue(object));
yaml << YAML::BeginSeq << YAML::Flow << vec.x << vec.y << YAML::EndSeq;
}
else if (fieldInfo->FieldType == Vector3::typeid)
{
Vector3 vec = safe_cast<Vector3>(fieldInfo->GetValue(object));
yaml << YAML::BeginSeq << YAML::Flow << vec.x << vec.y << vec.z << YAML::EndSeq;
}
else // Not any of the supported types
{
Debug::LogWarning(Convert::ToNative(System::String::Format
(
"[ReflectionUtilities] Failed to parse \"{0}\" of \"{1}\" type for serialization.",
fieldInfo->Name, fieldInfo->FieldType)
));
}
}
void ReflectionUtilities::writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)
{
if (fieldInfo->FieldType == System::Int16::typeid)
{
fieldInfo->SetValue(object, node.as<int>());
}
if (fieldAssignYaml<System::Int16> (fieldInfo, object, node) ||
fieldAssignYaml<System::Int32> (fieldInfo, object, node) ||
fieldAssignYaml<System::Int64> (fieldInfo, object, node) ||
fieldAssignYaml<System::UInt16>(fieldInfo, object, node) ||
fieldAssignYaml<System::UInt32>(fieldInfo, object, node) ||
fieldAssignYaml<System::UInt64>(fieldInfo, object, node) ||
fieldAssignYaml<System::Byte> (fieldInfo, object, node) ||
fieldAssignYaml<bool> (fieldInfo, object, node) ||
fieldAssignYaml<float> (fieldInfo, object, node) ||
fieldAssignYaml<double> (fieldInfo, object, node))
{
return;
}
else if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid))
{
fieldInfo->SetValue(object, node.as<int>());
}
else if (fieldInfo->FieldType == System::String::typeid)
{
fieldInfo->SetValue(object, Convert::ToCLI(node.as<std::string>()));
}
else if (fieldInfo->FieldType == Vector2::typeid)
{
if (node.IsSequence() && node.size() == 2)
{
Vector2 vec;
vec.x = node[0].as<float>();
vec.y = node[1].as<float>();
fieldInfo->SetValue(object, vec);
}
else
{
Debug::LogWarning
(
System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of a Vector2 \"{0}\" field in \"{1}\" script.",
fieldInfo->Name, object->GetType()->FullName)
);
}
}
else if (fieldInfo->FieldType == Vector3::typeid)
{
if (node.IsSequence() && node.size() == 3)
{
Vector3 vec;
vec.x = node[0].as<float>();
vec.y = node[1].as<float>();
vec.z = node[2].as<float>();
fieldInfo->SetValue(object, vec);
}
else
{
Debug::LogWarning
(
System::String::Format("[ReflectionUtilities] Invalid YAML Node provided for deserialization of a Vector3 \"{0}\" field in \"{1}\" script.",
fieldInfo->Name, object->GetType()->FullName)
);
}
}
else // Not any of the supported types
{
Debug::LogWarning(Convert::ToNative(System::String::Format
(
"[ReflectionUtilities] Failed to parse \"{0}\" of \"{1}\" type for deserialisation.",
fieldInfo->Name, fieldInfo->FieldType)
));
}
}
}

View File

@ -0,0 +1,54 @@
/************************************************************************************//*!
\file ReflectionUtilities.h++
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Sep 16, 2022
\brief Contains the definition of the template functions of the managed
ReflectionUtilities static class.
Note: This file is written in C++17/CLI.
Copyright (C) 2022 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// Primary Header
#include "ReflectionUtilities.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Serialization Helper Functions */
/*---------------------------------------------------------------------------------*/
template<typename FieldType>
bool ReflectionUtilities::fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Emitter& emitter)
{
if (fieldInfo->FieldType == FieldType::typeid)
{
emitter << safe_cast<FieldType>(fieldInfo->GetValue(object));
return true;
}
return false;
}
template<typename FieldType>
bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)
{
return fieldAssignYaml<FieldType, FieldType>(fieldInfo, object, node);
}
template<typename FieldType, typename CastType>
bool ReflectionUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)
{
if (fieldInfo->FieldType == FieldType::typeid)
{
fieldInfo->SetValue(object, node.as<CastType>());
return true;
}
return false;
}
}

View File

@ -0,0 +1,81 @@
/************************************************************************************//*!
\file ReflectionUtilities.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 6, 2021
\brief Contains the definition of the managed ReflectionUtilities static class.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
// External Dependencies
#include <yaml-cpp/yaml.h>
namespace SHADE
{
/// <summary>
/// Contains useful static functions for working with Reflection.
/// </summary>
private ref class ReflectionUtilities abstract sealed
{
public:
/*-----------------------------------------------------------------------------*/
/* Utility Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Retrieves a set of all non-static (instance) fields from a specified object.
/// </summary>
/// <param name="object">The object to get non-static fields from.</param>
/// <returns>Immutable list of non-static fields.</returns>
static System::Collections::Generic::IEnumerable<System::Reflection::FieldInfo^>^ GetInstanceFields(System::Object^ object);
/// <summary>
/// Checks if a specified field is a candidate for serialisation. This means that
/// the field is public or private with the [SerialiseField] attribute.
/// </summary>
/// <param name="fieldInfo">The field to check.</param>
/// <returns>
/// True if the specified field is a candidate for serialisation.
/// </returns>
static bool FieldIsSerialisable(System::Reflection::FieldInfo^ fieldInfo);
/*-----------------------------------------------------------------------------*/
/* Serialisation Functions */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Creates a JSON node that represents the specified object and its associated
/// serialisable fields. Public fields and fields marked with the SerialiseField
/// attribute will be serialised.
/// </summary>
/// <param name="object">The object to serialise.</param>
static void Serialise(System::Object^ object, YAML::Emitter& yaml);
/// <summary>
/// Deserialises a YAML node that contains a map of Scripts and copies the
/// deserialised data into the specified object if there are matching fields.
/// </summary>
/// <param name="yamlNode">
/// The JSON string that contains the data to copy into this PlushieScript
/// object.
/// </param>
/// <param name="object">The object to copy deserialised data into.</param>
static void Deserialise(YAML::Node& yamlNode, Object^ object);
/*-----------------------------------------------------------------------------*/
/* Serialization Helper Functions */
/*-----------------------------------------------------------------------------*/
static void writeFieldIntoYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Emitter& yaml);
template<typename FieldType>
static bool fieldInsertYaml(System::Reflection::FieldInfo^ fieldInfo, System::Object^ object, YAML::Emitter& emitter);
static void writeYamlIntoField(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node);
template<typename FieldType>
static bool fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node);
template<typename FieldType, typename CastType>
static bool fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node);
};
}
#include "ReflectionUtilities.h++"

View File

@ -0,0 +1,27 @@
/************************************************************************************//*!
\file SerializeFieldAttribute.cxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 5, 2021
\brief Contains the definition of the functions of the managed SerializeField
Attribute class.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
// Precompiled Headers
#include "SHpch.h"
// Primary Header
#include "SerializeFieldAttribute.hxx"
namespace SHADE
{
/*---------------------------------------------------------------------------------*/
/* Constructors */
/*---------------------------------------------------------------------------------*/
SerializeField::SerializeField()
{}
}

View File

@ -0,0 +1,35 @@
/************************************************************************************//*!
\file SerializeFieldAttribute.hxx
\author Tng Kah Wei, kahwei.tng, 390009620
\par email: kahwei.tng\@digipen.edu
\date Nov 5, 2021
\brief Contains the definition of the managed SerializeField Attribute class with
the declaration of functions for working with it.
Note: This file is written in C++17/CLI.
Copyright (C) 2021 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents without the prior written consent
of DigiPen Institute of Technology is prohibited.
*//*************************************************************************************/
#pragma once
namespace SHADE
{
/// <summary>
/// Simple attribute to mark that a field in a Script should be serialised.
/// </summary>
[System::AttributeUsage(System::AttributeTargets::Field)]
public ref class SerializeField : public System::Attribute
{
public:
/*-----------------------------------------------------------------------------*/
/* Constructors */
/*-----------------------------------------------------------------------------*/
/// <summary>
/// Default Constructor
/// </summary>
SerializeField();
};
}

View File

@ -18,6 +18,8 @@ of DigiPen Institute of Technology is prohibited.
#include "Debug.hxx"
// Standard Libraries
#include <sstream>
// External Libraries
#include "Tools/SHLog.h"
// Project Headers
#include "Convert.hxx"
@ -28,11 +30,11 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/
void Debug::Log(const std::string& str)
{
std::cout << str << std::endl;
SHLog::Info(str);
}
void Debug::Log(System::String^ str)
{
System::Console::WriteLine(str);
{
SHLog::Info(Convert::ToNative(str));
}
void Debug::Log(System::String^ str, Object^ owner)
@ -47,15 +49,15 @@ namespace SHADE
{
std::ostringstream oss;
oss << "[" << throwerName << "] " << Convert::ToNative(str);
std::cout << oss.str() << std::endl;
Log(oss.str());
}
void Debug::LogWarning(const std::string& str)
{
std::cout << str << std::endl;
SHLog::Warning(str);
}
void Debug::LogWarning(System::String^ str)
{
System::Console::WriteLine(str);
{
SHLog::Warning(Convert::ToNative(str));
}
void Debug::LogWarning(System::String^ str, Object^ thrower)
{
@ -69,16 +71,16 @@ namespace SHADE
void Debug::LogWarning(System::String^ str, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] " << Convert::ToNative(str);
std::cout << oss.str() << std::endl;
oss << "[" << throwerName << "] " << Convert::ToNative(str);
LogWarning(oss.str());
}
void Debug::LogError(const std::string& str)
{
std::cout << str << std::endl;
SHLog::Error(str);
}
void Debug::LogError(System::String^ str)
{
System::Console::WriteLine(str);
SHLog::Error(Convert::ToNative(str));
}
void Debug::LogError(System::String^ str, Object^ thrower)
{
@ -88,7 +90,7 @@ namespace SHADE
{
std::ostringstream oss;
oss << "[" << throwerName << "] -> " << Convert::ToNative(str);
std::cout << oss.str() << std::endl;
LogError(oss.str());
}
void Debug::LogError(System::String^ str, System::String^ throwerName)
{
@ -111,12 +113,12 @@ namespace SHADE
{
std::ostringstream oss;
oss << "[" << throwerName << "] Unhandled exception: " << Convert::ToNative(exception->ToString());
std::cout << oss.str() << std::endl;
LogError(oss.str());
}
void Debug::LogExceptionNative(const std::exception& exception, const std::string& throwerName)
{
std::ostringstream oss;
oss << "[" << throwerName << "] Unhandled exception: " << exception.what();
std::cout << oss.str() << std::endl;
LogError(oss.str());
}
}

View File

@ -0,0 +1,23 @@
using SHADE;
public class TestScript : Script
{
public TestScript(GameObject gameObj) : base(gameObj) {}
protected override void awake()
{
Debug.Log("TestScript.Awake()");
}
protected override void start()
{
Debug.Log("TestScript.Start()");
}
protected override void update()
{
Debug.Log("TestScript.Update()");
}
protected override void onDestroy()
{
Debug.Log("TestScript.OnDestroy()");
}
}

View File

@ -0,0 +1,11 @@
#define SET_STATIC_GLOBALS 0
#define SET_DYNAMIC_GLOBALS 1
#define SET_HIGH_FREQUENCY_GLOBALS 2
#define BINDING_GENERIC_DATA 0
#define BINDING_IMAGE_AND_SAMPLERS_DATA 1
#define BINDING_LIGHTS_DATA 0
#define BINDING_CAMERA_DATA 0
#define BINDING_BATCHED_PER_INST_DATA 0

View File

@ -1,6 +1,8 @@
#version 450
#extension GL_KHR_vulkan_glsl : enable
//#include "ShaderDescriptorDefinitions.glsl"
layout(location = 0) in vec3 aVertexPos;
layout(location = 1) in vec2 aUV;
layout(location = 2) in vec3 aNormal;
@ -28,6 +30,12 @@ layout(location = 0) out struct
} Out;
layout(set = 2, binding = 0) uniform CameraData
{
vec4 position;
mat4 vpMat;
} cameraData;
void main()
{
//const float gamma = testPushConstant.eyePosition.w;
@ -48,6 +56,7 @@ void main()
//Out.uv = aUV;
// render NDC first
gl_Position = vec4(aVertexPos, 1.0);
//gl_Position = vec4(aVertexPos, 1.0f);
gl_Position = cameraData.vpMat * worldTransform * vec4 (aVertexPos, 1.0f);
Out.vertColor = vec4 (aVertexPos, 1.0f);
}

Binary file not shown.