From f787f2b7824c9d7fecd64243c741abe9a6f50800 Mon Sep 17 00:00:00 2001 From: Brandon Mak Date: Sun, 19 Mar 2023 20:11:29 +0800 Subject: [PATCH] particles WIP --- Assets/Scenes/Scene2.shade | 4 +- .../Particles/SHParticleEmitterComponent.cpp | 10 ++++ .../Particles/SHParticleEmitterComponent.h | 38 ++++++++++----- .../Particles/SHParticleSubSystem.cpp | 47 +++++++++++++++++-- .../MiddleEnd/Particles/SHParticleSubSystem.h | 10 ++++ .../src/Serialization/SHYAMLConverters.h | 5 +- 6 files changed, 92 insertions(+), 22 deletions(-) diff --git a/Assets/Scenes/Scene2.shade b/Assets/Scenes/Scene2.shade index 564c78c4..e14f32cb 100644 --- a/Assets/Scenes/Scene2.shade +++ b/Assets/Scenes/Scene2.shade @@ -152,8 +152,8 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 0, z: 0} - Rotate: {x: 0, y: 0, z: 0} + Translate: {x: 0, y: 0.823412895, z: -4.31447983} + Rotate: {x: -0, y: 0, z: -0} Scale: {x: 1, y: 1, z: 1} IsActive: true classSHADE::SHParticleEmitterComponent: diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp index 2427ade9..9541a47b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.cpp @@ -87,6 +87,11 @@ namespace SHADE cpuEmitterData.lifeAndSizeRange.w = size; } + void SHParticleEmitterComponent::SetCustomShader(Handle shaderModule) noexcept + { + customUpdateShader = shaderModule; + } + uint32_t SHParticleEmitterComponent::GetEmissionCount(void) const noexcept { return emissionCount; @@ -154,4 +159,9 @@ namespace SHADE } + Handle SHParticleEmitterComponent::GetCustomShader(void) const noexcept + { + return customUpdateShader; + } + } \ No newline at end of file diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h index dea510f2..d99f8403 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleEmitterComponent.h @@ -11,10 +11,13 @@ namespace SHADE class SHVkBuffer; class SHVkDescriptorSetGroup; class SHVkDescriptorSetLayout; + class SHVkShaderModule; + class SHVkPipeline; class SHParticleEmitterComponent : public SHComponent { private: + struct GPUEmitterStruct { //! Angular ranges of emission @@ -94,6 +97,12 @@ namespace SHADE //! will contain 2 bindings that point to 2 buffers (input and output). Handle particleDescriptorSet; + //! Custom update shader for the particles in this component + Handle customUpdateShader; + + //! Internally the system will bind this pipeline when it detects that this is not a null handle + Handle customUpdatePipeline; + //! Emitter's data on the CPU side. To be copied to GPU. GPUEmitterStruct cpuEmitterData; @@ -131,21 +140,24 @@ namespace SHADE void SetTextureAssetID (AssetID id); void SetMinSize (float size) noexcept; void SetMaxSize (float size) noexcept; + void SetCustomShader (Handle shaderModule) noexcept; - uint32_t GetEmissionCount (void) const noexcept; - bool GetPassive (void) const noexcept; - float GetEmissionInterval (void) const noexcept; - float GetMinLife (void) const noexcept; - float GetMaxLife (void) const noexcept; - SHVec4 const& GetAngularRangesAndOffsets (void) const noexcept; - float GetMinSpeed (void) const noexcept; - float GetMaxSpeed (void) const noexcept; - float GetRotationSpeed (void) const noexcept; - uint32_t GetTextureIndex (void) const noexcept; - AssetID GetTextureAssetID (void) const noexcept; - float GetMinSize (void) const noexcept; - float GetMaxSize (void) const noexcept; + uint32_t GetEmissionCount (void) const noexcept; + bool GetPassive (void) const noexcept; + float GetEmissionInterval (void) const noexcept; + float GetMinLife (void) const noexcept; + float GetMaxLife (void) const noexcept; + SHVec4 const& GetAngularRangesAndOffsets (void) const noexcept; + float GetMinSpeed (void) const noexcept; + float GetMaxSpeed (void) const noexcept; + float GetRotationSpeed (void) const noexcept; + uint32_t GetTextureIndex (void) const noexcept; + AssetID GetTextureAssetID (void) const noexcept; + float GetMinSize (void) const noexcept; + float GetMaxSize (void) const noexcept; + Handle GetCustomShader (void) const noexcept; + friend class SHParticleSubSystem; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp index bd08b48f..05295fd0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.cpp @@ -240,6 +240,34 @@ namespace SHADE postUpdateBarriers.push_back(drawDataBarrierPostUpdate); } + Handle SHParticleSubSystem::GetCustomUpdatePipeline(Handle customUpdateShader) noexcept + { + if (!customUpdateShader) + return {}; + + if (!customUpdatePipelineCache.contains(customUpdateShader)) + { + SHPipelineLayoutParams plParams + { + .shaderModules = {customUpdateShader}, + .predefinedDescSetLayouts = SHGraphicsPredefinedData::GetSystemData(SHGraphicsPredefinedData::SystemType::PARTICLE_RENEDERING).descSetLayouts + }; + + auto pipelineLayout = logicalDevice->CreatePipelineLayout(plParams); + auto newPipeline = logicalDevice->CreateComputePipeline(pipelineLayout); + newPipeline->ConstructPipeline(); + + if (!newPipeline) + return {}; + + auto customUpdateShaderData = CustomPipeline{ newPipeline, pipelineLayout }; + + customUpdatePipelineCache.emplace (customUpdateShader, customUpdateShaderData); + } + + return customUpdatePipelineCache.at (customUpdateShader).customPipeline; + } + void SHParticleSubSystem::Init(Handle device, Handle inDescPool, Handle compatibleRenderpass, Handle subpass, Handle VS, Handle FS, Handle emitCS, Handle defaultUpdateCS) noexcept { descPool = inDescPool; @@ -416,13 +444,22 @@ namespace SHADE /*-----------------------------------------------------------------------*/ /* EMITTING PARTICLES DONE, BEGIN UPDATES.... */ /*-----------------------------------------------------------------------*/ - - - // bind the pipeline for updating - cmdBuffer->BindPipeline(defaultUpdatePipelineData.pipeline); - for (auto& emitter : emitters) { + if (emitter.customUpdateShader) + { + if (!emitter.customUpdatePipeline) + emitter.customUpdatePipeline = GetCustomUpdatePipeline(emitter.customUpdateShader); + + // bind the custom pipeline for updating + cmdBuffer->BindPipeline(emitter.customUpdatePipeline); + } + else + { + // bind the pipeline for updating + cmdBuffer->BindPipeline(defaultUpdatePipelineData.pipeline); + } + if (emitter.isActive) UpdateCompoennt(cmdBuffer, emitter, frameIndex); } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.h index ec7418cd..a0675da6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Particles/SHParticleSubSystem.h @@ -34,6 +34,12 @@ namespace SHADE static constexpr uint32_t DYOFF_INDEX_INDICES_DATA = 3; static constexpr uint32_t DYOFF_INDEX_DRAW_DATA = 4; + struct CustomPipeline + { + Handle customPipeline; + Handle customPipelineLayout; + }; + // To hold data for a pipeline and pipeline layout. // We want this here because particles require 3 pipeline sets: @@ -76,6 +82,8 @@ namespace SHADE //! Desc pool for particle component desc set allocation Handle descPool; + std::unordered_map, CustomPipeline> customUpdatePipelineCache; + void InitializeComponent (SHParticleEmitterComponent& comp) noexcept; void EmitComponent (Handle cmdBuffer, SHParticleEmitterComponent& comp, uint32_t frameIndex) noexcept; @@ -84,6 +92,8 @@ namespace SHADE void PreparePrePostUpdateBarriers (std::vector& preUpdateBarriers, std::vector& postUpdateBarriers, SHParticleEmitterComponent const& emitter, uint32_t const EMITTER_INDEX, uint32_t const FRAME_INDEX) noexcept; + Handle GetCustomUpdatePipeline (Handle customUpdateShader) noexcept; + public: void Init(Handle device, Handle inDescPool, Handle compatibleRenderpass, Handle subpass, Handle VS, Handle FS, Handle emitCS, Handle defaultUpdateCS) noexcept; diff --git a/SHADE_Engine/src/Serialization/SHYAMLConverters.h b/SHADE_Engine/src/Serialization/SHYAMLConverters.h index daf3d4ca..0a879146 100644 --- a/SHADE_Engine/src/Serialization/SHYAMLConverters.h +++ b/SHADE_Engine/src/Serialization/SHYAMLConverters.h @@ -574,12 +574,13 @@ namespace YAML AssetID id = node[TEXTURE_ASSET_ID_TAG.data()].as(); Handle texture = SHResourceManager::LoadOrGet(id); - gfxSystem->BuildTextures(); + SHResourceManager::FinaliseChanges(); + //gfxSystem->BuildTextures(); rhs.SetTextureIndex(texture->TextureArrayIndex); rhs.SetTextureAssetID(id); } - + return true; } };