From 2e82b6120d239058690a183e4e8401df9f34659b Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Wed, 13 Dec 2023 07:51:57 -0800 Subject: [PATCH] feat: add geometry set allocator for beam and rope Signed-off-by: Michael Pollind --- HPL2/include/graphics/GeometrySet.h | 2 +- HPL2/include/graphics/GraphicsBuffer.h | 9 +- HPL2/include/graphics/RendererDeferred2.h | 6 +- HPL2/include/physics/VerletParticle.h | 1 - HPL2/include/scene/Beam.h | 251 +++++++------- HPL2/include/scene/RopeEntity.h | 11 +- HPL2/sources/graphics/RendererDeferred2.cpp | 116 ++++--- HPL2/sources/scene/Beam.cpp | 348 +++++++++----------- HPL2/sources/scene/RopeEntity.cpp | 307 ++++++++++------- HPL2/sources/scene/SubMeshEntity.cpp | 1 - 10 files changed, 555 insertions(+), 497 deletions(-) diff --git a/HPL2/include/graphics/GeometrySet.h b/HPL2/include/graphics/GeometrySet.h index 982ef265b..c7f091579 100644 --- a/HPL2/include/graphics/GeometrySet.h +++ b/HPL2/include/graphics/GeometrySet.h @@ -77,7 +77,7 @@ namespace hpl { inline SharedBuffer& indexBuffer() { return m_indexBuffer; } void operator=(GeometrySet&& set); void operator=(const GeometrySet& set) = delete; - std::shared_ptr allocate(uint32_t numElements, uint32_t numIndecies); + std::shared_ptr allocate(uint32_t numElements, uint32_t numIndecies); private: OffsetAllocator::Allocator m_vertexStreamAllocator; OffsetAllocator::Allocator m_indexStreamAllocator; diff --git a/HPL2/include/graphics/GraphicsBuffer.h b/HPL2/include/graphics/GraphicsBuffer.h index 516b86fae..8bc624470 100644 --- a/HPL2/include/graphics/GraphicsBuffer.h +++ b/HPL2/include/graphics/GraphicsBuffer.h @@ -239,10 +239,8 @@ namespace hpl { case BufferType::MappedBuffer: switch(m_indexType) { case IndexBufferType::Uint32: - ASSERT(m_asset->m_buffer.begin() + (m_byteOffset + (index * sizeof(uint32_t))) < m_asset->m_buffer.end()); return *reinterpret_cast(reinterpret_cast(m_asset->m_mapped.m_mappedData) + (m_byteOffset + (index * sizeof(uint32_t)))); case IndexBufferType::Uint16: - ASSERT(m_asset->m_buffer.begin() + (m_byteOffset + (index * sizeof(uint16_t))) < m_asset->m_buffer.end()); return *reinterpret_cast(reinterpret_cast(m_asset->m_mapped.m_mappedData) + (m_byteOffset + (index * sizeof(uint16_t)))); } break; @@ -278,7 +276,6 @@ namespace hpl { uint32_t v = value; auto buf = std::span(reinterpret_cast(&v), sizeof(uint32_t)); const size_t targetOffset = (m_byteOffset + (index * sizeof(uint32_t))); - details::testAndResize(m_asset->m_buffer, targetOffset + buf.size()); std::copy(buf.begin(), buf.end(), reinterpret_cast(m_asset->m_mapped.m_mappedData) + targetOffset); break; } @@ -399,8 +396,10 @@ namespace hpl { ASSERT(m_asset->m_buffer.begin() + targetOffset < m_asset->m_buffer.end()); return *reinterpret_cast(m_asset->m_buffer.data() + targetOffset); } - case BufferType::MappedBuffer: { - ASSERT(false); + case BufferType::MappedBuffer: + { + ASSERT(m_asset->m_mapped.m_size == 0 || targetOffset < m_asset->m_mapped.m_size); + return *reinterpret_cast(reinterpret_cast(m_asset->m_mapped.m_mappedData) + targetOffset); break; } } diff --git a/HPL2/include/graphics/RendererDeferred2.h b/HPL2/include/graphics/RendererDeferred2.h index ae491ad83..ff7e8953a 100644 --- a/HPL2/include/graphics/RendererDeferred2.h +++ b/HPL2/include/graphics/RendererDeferred2.h @@ -65,7 +65,7 @@ namespace hpl { HPL_RTTI_IMPL_CLASS(iRenderer, cRendererDeferred, "{B3E5E5A1-1F9C-4F5C-9B9B-5B9B9B5B9B9B}") public: - static constexpr TinyImageFormat DepthBufferFormat = TinyImageFormat_D32_SFLOAT_S8_UINT; + static constexpr TinyImageFormat DepthBufferFormat = TinyImageFormat_D32_SFLOAT; static constexpr TinyImageFormat NormalBufferFormat = TinyImageFormat_R16G16B16A16_SFLOAT; static constexpr TinyImageFormat PositionBufferFormat = TinyImageFormat_R32G32B32A32_SFLOAT; static constexpr TinyImageFormat SpecularBufferFormat = TinyImageFormat_R8G8_UNORM; @@ -169,10 +169,6 @@ namespace hpl { SharedDescriptorSet m_sceneDescriptorConstSet; std::array m_sceneDescriptorPerFrameSet; - //SharedRootSignature m_rootSignatureCopyDepth; - //std::array m_descriptorCopyDepth; - //SharedShader m_copyDepthShader; - SharedShader m_visibilityBufferPassShader; SharedShader m_visibilityBufferAlphaPassShader; SharedShader m_visibilityShadePassShader; diff --git a/HPL2/include/physics/VerletParticle.h b/HPL2/include/physics/VerletParticle.h index f73d9dde8..9a0c2056c 100644 --- a/HPL2/include/physics/VerletParticle.h +++ b/HPL2/include/physics/VerletParticle.h @@ -134,7 +134,6 @@ namespace hpl { inline int GetUpdateCount() const { return mlUpdateCount; } cVerletParticleIterator GetParticleIterator(){ return cVerletParticleIterator(&mlstParticles); } - protected: virtual bool CheckParticleBodyCollision(iPhysicsBody *apBody)=0; virtual bool CheckSpecificDataSleeping()=0; //Tested when container is NOT sleeping diff --git a/HPL2/include/scene/Beam.h b/HPL2/include/scene/Beam.h index b1485282c..6aba504ef 100644 --- a/HPL2/include/scene/Beam.h +++ b/HPL2/include/scene/Beam.h @@ -16,9 +16,7 @@ * You should have received a copy of the GNU General Public License * along with Amnesia: The Dark Descent. If not, see . */ - -#ifndef HPL_BEAM_H -#define HPL_BEAM_H +#pragma once #include "math/MathTypes.h" #include "graphics/GraphicsTypes.h" @@ -28,131 +26,160 @@ namespace hpl { - class cMaterialManager; - class cResources; - class cGraphics; - class cFileSearcher; - class iLowLevelGraphics; - class cMaterial; - class iVertexBuffer; - - - class cBeam; - class cBeamEnd : public iEntity3D - { - friend class cBeam; - friend class cBeamEnd_UpdateCallback; - public: - cBeamEnd(const tString asName, cBeam *apBeam) : iEntity3D(asName), - mColor(1,1),mpBeam(apBeam) {} - - void SetColor(const cColor &aColor); - const cColor& GetColor(){ return mColor;} - - ///////////////////////////////// - //Entity implementation - tString GetEntityType(){ return "BeamEnd";} - private: - cColor mColor; - cBeam *mpBeam; - }; - - //------------------------------------------ - - class cBeamEnd_UpdateCallback : public iEntityCallback - { - public: - void OnTransformUpdate(iEntity3D * apEntity); - }; - - //------------------------------------------ - - class cBeam : public iRenderable - { - friend class cBeamEnd; - public: - cBeam(const tString asName, cResources *apResources,cGraphics *apGraphics); - ~cBeam(); - - void SetMaterial(cMaterial * apMaterial); - - const tString& GetFileName(){return msFileName;} - - /** - * Set the size. X= the thickness of the line, width of texture used. Y = the length that one texture height takes. - * \param avSize - */ - void SetSize(const cVector2f& avSize); - cVector2f GetSize(){ return mvSize;} - - void SetColor(const cColor &aColor); - const cColor& GetColor(){ return mColor;} - - void SetTileHeight(bool abX); - bool GetTileHeight(){ return mbTileHeight;} - - void SetMultiplyAlphaWithColor(bool abX); - bool GetMultiplyAlphaWithColor(){ return mbMultiplyAlphaWithColor;} - - cBeamEnd* GetEnd(){ return mpEnd;} - - bool LoadXMLProperties(const tString asFile); - - cVector3f GetAxis(){ return mvAxis;} - cVector3f GetMidPosition(){ return mvMidPosition;} - - ///////////////////////////////// - //Entity implementation - tString GetEntityType(){ return "Beam";} - - bool IsVisible(); - - //Renderable implementations - virtual cMaterial *GetMaterial() override{ return mpMaterial;} - virtual iVertexBuffer* GetVertexBuffer() override{return mpVtxBuffer;} + class cMaterialManager; + class cResources; + class cGraphics; + class cFileSearcher; + class iLowLevelGraphics; + class cMaterial; + class iVertexBuffer; + + class cBeam; + class cBeamEnd : public iEntity3D { + friend class cBeam; + friend class cBeamEnd_UpdateCallback; + + public: + cBeamEnd(const tString asName, cBeam* apBeam) + : iEntity3D(asName) + , mColor(1, 1) + , mpBeam(apBeam) { + } + + void SetColor(const cColor& aColor); + const cColor& GetColor() { + return mColor; + } + + ///////////////////////////////// + // Entity implementation + tString GetEntityType() { + return "BeamEnd"; + } + + private: + cColor mColor; + cBeam* mpBeam; + }; + + //------------------------------------------ + + class cBeamEnd_UpdateCallback : public iEntityCallback { + public: + void OnTransformUpdate(iEntity3D* apEntity); + }; + + //------------------------------------------ + + class cBeam : public iRenderable { + friend class cBeamEnd; + + public: + cBeam(const tString asName, cResources* apResources, cGraphics* apGraphics); + ~cBeam(); + + void SetMaterial(cMaterial* apMaterial); + + const tString& GetFileName() { + return msFileName; + } + + /** + * Set the size. X= the thickness of the line, width of texture used. Y = the length that one texture height takes. + * \param avSize + */ + void SetSize(const cVector2f& avSize); + cVector2f GetSize() { + return mvSize; + } + + void SetColor(const cColor& aColor); + const cColor& GetColor() { + return mColor; + } + + void SetTileHeight(bool abX); + bool GetTileHeight() { + return mbTileHeight; + } + + void SetMultiplyAlphaWithColor(bool abX); + bool GetMultiplyAlphaWithColor() { + return mbMultiplyAlphaWithColor; + } + + cBeamEnd* GetEnd() { + return mpEnd; + } + + bool LoadXMLProperties(const tString asFile); + + cVector3f GetAxis() { + return mvAxis; + } + cVector3f GetMidPosition() { + return mvMidPosition; + } + + // Entity implementation + tString GetEntityType() { + return "Beam"; + } + + bool IsVisible(); + + // Renderable implementations + virtual cMaterial* GetMaterial() override { + return mpMaterial; + } + virtual iVertexBuffer* GetVertexBuffer() override { + return nullptr; + } virtual DrawPacket ResolveDrawPacket(const ForgeRenderer::Frame& frame) override; - void UpdateGraphicsForFrame(float afFrameTime); + void UpdateGraphicsForFrame(float afFrameTime); - cBoundingVolume* GetBoundingVolume(); + cBoundingVolume* GetBoundingVolume(); - cMatrixf* GetModelMatrix(cFrustum *apFrustum); + cMatrixf* GetModelMatrix(cFrustum* apFrustum); - int GetMatrixUpdateCount(); + int GetMatrixUpdateCount(); + eRenderableType GetRenderType() { + return eRenderableType_Beam; + } - eRenderableType GetRenderType(){ return eRenderableType_Beam;} - private: - cMaterialManager* mpMaterialManager; - cFileSearcher *mpFileSearcher; - iLowLevelGraphics* mpLowLevelGraphics; + private: + std::shared_ptr m_geometry; - cMaterial *mpMaterial; - iVertexBuffer* mpVtxBuffer; + cMaterialManager* mpMaterialManager; + cFileSearcher* mpFileSearcher; + iLowLevelGraphics* mpLowLevelGraphics; - cBeamEnd *mpEnd; + cMaterial* mpMaterial; + cBeamEnd* mpEnd; - tString msFileName; + tString msFileName; - int mlStartTransformCount; - int mlEndTransformCount; + int mlStartTransformCount; + int mlEndTransformCount; - cMatrixf m_mtxTempTransform; + cMatrixf m_mtxTempTransform; - int mlLastRenderCount; + int mlLastRenderCount; - cBeamEnd_UpdateCallback mEndCallback; + cBeamEnd_UpdateCallback mEndCallback; - cVector2f mvSize; + cVector2f mvSize; - cVector3f mvAxis; - cVector3f mvMidPosition; + cVector3f mvAxis; + cVector3f mvMidPosition; - bool mbTileHeight; - bool mbMultiplyAlphaWithColor; + bool mbTileHeight; + bool mbMultiplyAlphaWithColor; - cColor mColor; - }; + cColor mColor = cColor(1,1,1,1); + uint8_t m_activeCopy = 0; + }; -}; -#endif // HPL_BEAM_H +}; // namespace hpl diff --git a/HPL2/include/scene/RopeEntity.h b/HPL2/include/scene/RopeEntity.h index b59f6fc67..b8a60063d 100644 --- a/HPL2/include/scene/RopeEntity.h +++ b/HPL2/include/scene/RopeEntity.h @@ -71,17 +71,14 @@ namespace hpl { void SetLengthTileSize(float afX){ mfLengthTileSize = afX;} float GetLengthTileSize(){ return mfLengthTileSize;} - - ///////////////////////////////// //Entity implementation tString GetEntityType(){ return "RopeEntity";} bool IsVisible(); - ///////////////////////////////// //Renderable implementations virtual cMaterial *GetMaterial() override{ return mpMaterial;} - virtual iVertexBuffer* GetVertexBuffer() override{return mpVtxBuffer;} + virtual iVertexBuffer* GetVertexBuffer() override{return nullptr;} virtual DrawPacket ResolveDrawPacket(const ForgeRenderer::Frame& frame) override; virtual void UpdateGraphicsForFrame(float afFrameTime) override; @@ -101,7 +98,7 @@ namespace hpl { iPhysicsRope *mpRope; cMaterial *mpMaterial; - iVertexBuffer* mpVtxBuffer; + std::shared_ptr m_geometry; int mlMaxSegments; @@ -113,6 +110,8 @@ namespace hpl { bool mbMultiplyAlphaWithColor; int mlLastUpdateCount; - }; + uint8_t m_activeCopy = 0; + int m_numberSegments = 0; + }; }; diff --git a/HPL2/sources/graphics/RendererDeferred2.cpp b/HPL2/sources/graphics/RendererDeferred2.cpp index 83a03c0fb..c32ee6fec 100644 --- a/HPL2/sources/graphics/RendererDeferred2.cpp +++ b/HPL2/sources/graphics/RendererDeferred2.cpp @@ -707,6 +707,9 @@ namespace hpl { frame.m_resourcePool->Push(viewportDatum->m_visiblityBuffer[frame.m_frameIndex]); frame.m_resourcePool->Push(viewportDatum->m_depthBuffer[frame.m_frameIndex]); frame.m_resourcePool->Push(viewportDatum->m_outputBuffer[frame.m_frameIndex]); + // ---------------- + // Setup Data + // ----------------- { std::array params = { DescriptorData{ .pName = "visibilityTexture", @@ -806,7 +809,6 @@ namespace hpl { endUpdateResource(&updateDesc); } RangeSubsetAlloc::RangeSubset opaqueIndirectArgs = indirectAllocSession.End(); - // diffuse build indirect list for opaque for (auto& renderable : translucenctRenderables) { cMaterial* material = renderable->GetMaterial(); @@ -896,66 +898,71 @@ namespace hpl { break; } } + } - - { - GpuCmdRingElement computeElem = getNextGpuCmdRingElement(&m_computeRing, true, 1); - - // check to see if we can use the cmd buffer - FenceStatus fenceStatus; - getFenceStatus(forgeRenderer->Rend(), computeElem.pFence, &fenceStatus); - if (fenceStatus == FENCE_STATUS_INCOMPLETE) { - waitForFences(forgeRenderer->Rend(), 1, &computeElem.pFence); - } + // ---------------- + // Compute pipeline + // ----------------- + { + GpuCmdRingElement computeElem = getNextGpuCmdRingElement(&m_computeRing, true, 1); - Cmd* computeCmd = computeElem.pCmds[0]; - resetCmdPool(forgeRenderer->Rend(), computeElem.pCmdPool); - beginCmd(computeCmd); + // check to see if we can use the cmd buffer + FenceStatus fenceStatus; + getFenceStatus(forgeRenderer->Rend(), computeElem.pFence, &fenceStatus); + if (fenceStatus == FENCE_STATUS_INCOMPLETE) { + waitForFences(forgeRenderer->Rend(), 1, &computeElem.pFence); + } - cmdBindPipeline(computeCmd, m_clearClusterPipeline.m_handle); - cmdBindDescriptorSet(computeCmd, 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle); - cmdDispatch(computeCmd, 1, 1, LightClusterSlices); - { - std::array barriers = { BufferBarrier{ m_lightClusterCountBuffer[frame.m_frameIndex].m_handle, - RESOURCE_STATE_UNORDERED_ACCESS, - RESOURCE_STATE_UNORDERED_ACCESS } }; - cmdResourceBarrier(computeCmd, barriers.size(), barriers.data(), 0, nullptr, 0, nullptr); - } - cmdBindPipeline(computeCmd, m_lightClusterPipeline.m_handle); - cmdBindDescriptorSet(computeCmd, 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle); - cmdDispatch(computeCmd, lightCount, 1, LightClusterSlices); - { - std::array barriers = { BufferBarrier{ m_lightClustersBuffer[frame.m_frameIndex].m_handle, - RESOURCE_STATE_UNORDERED_ACCESS, - RESOURCE_STATE_UNORDERED_ACCESS } }; - cmdResourceBarrier(computeCmd, barriers.size(), barriers.data(), 0, nullptr, 0, nullptr); - } + Cmd* computeCmd = computeElem.pCmds[0]; + resetCmdPool(forgeRenderer->Rend(), computeElem.pCmdPool); + beginCmd(computeCmd); - endCmd(computeCmd); + cmdBindPipeline(computeCmd, m_clearClusterPipeline.m_handle); + cmdBindDescriptorSet(computeCmd, 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle); + cmdDispatch(computeCmd, 1, 1, LightClusterSlices); + { + std::array barriers = { BufferBarrier{ m_lightClusterCountBuffer[frame.m_frameIndex].m_handle, + RESOURCE_STATE_UNORDERED_ACCESS, + RESOURCE_STATE_UNORDERED_ACCESS } }; + cmdResourceBarrier(computeCmd, barriers.size(), barriers.data(), 0, nullptr, 0, nullptr); + } + cmdBindPipeline(computeCmd, m_lightClusterPipeline.m_handle); + cmdBindDescriptorSet(computeCmd, 0, m_lightDescriptorPerFrameSet[frame.m_frameIndex].m_handle); + cmdDispatch(computeCmd, lightCount, 1, LightClusterSlices); + { + std::array barriers = { BufferBarrier{ m_lightClustersBuffer[frame.m_frameIndex].m_handle, + RESOURCE_STATE_UNORDERED_ACCESS, + RESOURCE_STATE_UNORDERED_ACCESS } }; + cmdResourceBarrier(computeCmd, barriers.size(), barriers.data(), 0, nullptr, 0, nullptr); + } - static Semaphore* prevGraphicsSemaphore = NULL; - FlushResourceUpdateDesc flushUpdateDesc = {}; - flushUpdateDesc.mNodeIndex = 0; - flushResourceUpdates(&flushUpdateDesc); - Semaphore* waitSemaphores[] = { flushUpdateDesc.pOutSubmittedSemaphore, frame.m_currentFrame > 1 ? prevGraphicsSemaphore : NULL }; - prevGraphicsSemaphore = frame.m_renderCompleteSemaphore; + endCmd(computeCmd); - QueueSubmitDesc submitDesc = {}; - submitDesc.mCmdCount = 1; - submitDesc.mSignalSemaphoreCount = 1; - submitDesc.mWaitSemaphoreCount = waitSemaphores[1] ? TF_ARRAY_COUNT(waitSemaphores) : 1; - submitDesc.ppCmds = &computeCmd; - submitDesc.ppSignalSemaphores = &computeElem.pSemaphore; - submitDesc.ppWaitSemaphores = waitSemaphores; - submitDesc.pSignalFence = computeElem.pFence; - submitDesc.mSubmitDone = (frame.m_currentFrame < 1); - queueSubmit(m_computeQueue, &submitDesc); + static Semaphore* prevGraphicsSemaphore = NULL; + FlushResourceUpdateDesc flushUpdateDesc = {}; + flushUpdateDesc.mNodeIndex = 0; + flushResourceUpdates(&flushUpdateDesc); + Semaphore* waitSemaphores[] = { flushUpdateDesc.pOutSubmittedSemaphore, + frame.m_currentFrame > 1 ? prevGraphicsSemaphore : NULL }; + prevGraphicsSemaphore = frame.m_renderCompleteSemaphore; - frame.m_waitSemaphores->push_back(computeElem.pSemaphore); + QueueSubmitDesc submitDesc = {}; + submitDesc.mCmdCount = 1; + submitDesc.mSignalSemaphoreCount = 1; + submitDesc.mWaitSemaphoreCount = waitSemaphores[1] ? TF_ARRAY_COUNT(waitSemaphores) : 1; + submitDesc.ppCmds = &computeCmd; + submitDesc.ppSignalSemaphores = &computeElem.pSemaphore; + submitDesc.ppWaitSemaphores = waitSemaphores; + submitDesc.pSignalFence = computeElem.pFence; + submitDesc.mSubmitDone = (frame.m_currentFrame < 1); + queueSubmit(m_computeQueue, &submitDesc); - } + frame.m_waitSemaphores->push_back(computeElem.pSemaphore); } + // ---------------- + // Visibility pass + // ----------------- { cmdBindRenderTargets(cmd, 0, NULL, NULL, NULL, NULL, NULL, -1, -1); std::array rtBarriers = { @@ -1095,6 +1102,13 @@ namespace hpl { cmdEndDebugMarker(cmd); } + + // -------------------- + // Translucency + // -------------------- + + + { cmdBindRenderTargets(cmd, 0, NULL, NULL, NULL, NULL, NULL, -1, -1); std::array barriers = { BufferBarrier{ m_lightClusterCountBuffer[frame.m_frameIndex].m_handle, diff --git a/HPL2/sources/scene/Beam.cpp b/HPL2/sources/scene/Beam.cpp index 4f5ce346d..eb9e2cce6 100644 --- a/HPL2/sources/scene/Beam.cpp +++ b/HPL2/sources/scene/Beam.cpp @@ -35,6 +35,9 @@ #include "graphics/Material.h" #include "graphics/MaterialType.h" #include "graphics/LowLevelGraphics.h" +#include "graphics/GraphicsBuffer.h" +#include "graphics/GeometrySet.h" +#include "graphics/MeshUtility.h" #include "scene/Camera.h" #include "scene/World.h" @@ -42,7 +45,10 @@ #include "engine/Engine.h" +#include "Common_3/Utilities/Math/MathTypes.h" + namespace hpl { + const uint32_t BeamNumberVerts = 4; cBeam::cBeam(const tString asName, cResources *apResources,cGraphics *apGraphics) : iRenderable(asName) @@ -63,37 +69,83 @@ namespace hpl { mlLastRenderCount = -1; - - mpVtxBuffer = mpLowLevelGraphics->CreateVertexBuffer( - eVertexBufferType_Hardware, - eVertexBufferDrawType_Tri, eVertexBufferUsageType_Dynamic,4,6); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Position,eVertexBufferElementFormat_Float,4); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Normal,eVertexBufferElementFormat_Float,3); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Color0,eVertexBufferElementFormat_Float,4); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Texture0,eVertexBufferElementFormat_Float,3); - - cVector3f vCoords[4] = {cVector3f((mvSize.x/2),-(mvSize.y/2),0), - cVector3f(-(mvSize.x/2),-(mvSize.y/2),0), - cVector3f(-(mvSize.x/2),(mvSize.y/2),0), - cVector3f((mvSize.x/2),(mvSize.y/2),0)}; - - cVector3f vTexCoords[4] = {cVector3f(1,1,0), //Bottom left - cVector3f(-1,1,0), //Bottom right - cVector3f(-1,-1,0), //Top left - cVector3f(1,-1,0)}; //Top right - - for(int i=0;i<4;i++) - { - mpVtxBuffer->AddVertexVec3f(eVertexBufferElement_Position, vCoords[i]); - mpVtxBuffer->AddVertexColor(eVertexBufferElement_Color0, cColor(1,1,1,1)); - mpVtxBuffer->AddVertexVec3f(eVertexBufferElement_Texture0, (vTexCoords[i] + cVector2f(1,1))/2); - mpVtxBuffer->AddVertexVec3f(eVertexBufferElement_Normal,cVector3f(0,0,1)); - } - - for(int i=0;i<3;i++) mpVtxBuffer->AddIndex(i); - for(int i=2;i<5;i++) mpVtxBuffer->AddIndex(i==4?0:i); - - mpVtxBuffer->Compile(eVertexCompileFlag_CreateTangents); + auto* graphicsAllocator = Interface::Get(); + auto& opaqueSet = graphicsAllocator->resolveSet(GraphicsAllocator::AllocationSet::OpaqueSet); + m_geometry = opaqueSet.allocate(4 * 2, 6); + + std::array positionCoords = { float3((mvSize.x / 2), -(mvSize.y / 2), 0.0f), + float3(-(mvSize.x / 2), -(mvSize.y / 2), 0.0f), + float3(-(mvSize.x / 2), (mvSize.y / 2), 0.0f), + float3((mvSize.x / 2), (mvSize.y / 2), 0.0f) }; + + std::array texcoords = { (float2(1.0f, 1.0f) + float2(1,1)) / 2.0f, // Bottom left + (float2(-1.0f, 1.0f) + float2(1, 1)) / 2.0f, // Bottom right + (float2(-1.0f, -1.0f) + float2(1, 1)) / 2.0f, // Top left + (float2(1.0f, -1.0f) + float2(1, 1)) / 2.0f }; // Top right + { + auto& indexStream = m_geometry->indexBuffer(); + auto positionStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_POSITION); + auto normalStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_NORMAL); + auto colorStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_COLOR); + auto textureStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TEXCOORD0); + auto tangentStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TANGENT); + + BufferUpdateDesc indexUpdateDesc = { indexStream.m_handle, 0, GeometrySet::IndexBufferStride * 6 }; + BufferUpdateDesc positionUpdateDesc = { positionStream->buffer().m_handle, 0, positionStream->stride() * BeamNumberVerts * 2 }; + BufferUpdateDesc normalUpdateDesc = { normalStream->buffer().m_handle, 0, normalStream->stride() * BeamNumberVerts * 2}; + BufferUpdateDesc colorUpdateDesc = { colorStream->buffer().m_handle, 0, colorStream->stride() * BeamNumberVerts * 2}; + BufferUpdateDesc textureUpdateDesc = { textureStream->buffer().m_handle, 0, textureStream->stride() * BeamNumberVerts * 2}; + BufferUpdateDesc tangentUpdateDesc = { tangentStream->buffer().m_handle, 0, tangentStream->stride() * BeamNumberVerts * 2}; + + beginUpdateResource(&indexUpdateDesc); + beginUpdateResource(&positionUpdateDesc); + beginUpdateResource(&normalUpdateDesc); + beginUpdateResource(&colorUpdateDesc); + beginUpdateResource(&textureUpdateDesc); + beginUpdateResource(&tangentUpdateDesc); + + GraphicsBuffer gpuIndexBuffer(indexUpdateDesc); + GraphicsBuffer gpuPositionBuffer(positionUpdateDesc); + GraphicsBuffer gpuNormalBuffer(normalUpdateDesc); + GraphicsBuffer gpuColorBuffer(colorUpdateDesc); + GraphicsBuffer gpuTextureBuffer(textureUpdateDesc); + GraphicsBuffer gpuTangentBuffer(tangentUpdateDesc); + + auto indexView = gpuIndexBuffer.CreateIndexView(); + { + uint32_t index = 0; + for (int i = 0; i < 3; i++) { + indexView.Write(index++, i); + } + for (int i = 2; i < 5; i++) { + indexView.Write(index++, i == 4 ? 0 : i); + } + } + for(size_t copyIdx = 0; copyIdx < 2; copyIdx++) { + auto positionView = gpuPositionBuffer.CreateStructuredView(positionStream->stride() * copyIdx * 4) ; + auto normalView = gpuNormalBuffer.CreateStructuredView(normalStream->stride() * copyIdx * 4); + auto colorView = gpuColorBuffer.CreateStructuredView(colorStream->stride() * copyIdx * 4); + auto textureView = gpuTextureBuffer.CreateStructuredView(textureStream->stride() * copyIdx * 4); + auto tangentView = gpuTangentBuffer.CreateStructuredView(tangentStream->stride() * copyIdx * 4); + for (size_t i = 0; i < 4; i++) { + positionView.Write(i, positionCoords[i]); + normalView.Write(i, float3(0.0f, 0.0f, 1.0f)); + colorView.Write(i, float4(1,1,1,1)); + textureView.Write(i, texcoords[i]); + } + Matrix4 trans = Matrix4::identity(); + hpl::MeshUtility::MikkTSpaceGenerate(4, 6, &indexView, &positionView, &textureView, &normalView, &tangentView); + } + + endUpdateResource(&indexUpdateDesc); + endUpdateResource(&positionUpdateDesc); + endUpdateResource(&normalUpdateDesc); + endUpdateResource(&colorUpdateDesc); + endUpdateResource(&textureUpdateDesc); + endUpdateResource(&tangentUpdateDesc); + } + + //mpVtxBuffer->Compile(eVertexCompileFlag_CreateTangents); mpEnd = hplNew( cBeamEnd, (asName + "_end",this)); mpEnd->AddCallback(&mEndCallback); @@ -110,12 +162,22 @@ namespace hpl { { hplDelete(mpEnd); if(mpMaterial) mpMaterialManager->Destroy(mpMaterial); - if(mpVtxBuffer) hplDelete(mpVtxBuffer); + //if(mpVtxBuffer) hplDelete(mpVtxBuffer); } DrawPacket cBeam::ResolveDrawPacket(const ForgeRenderer::Frame& frame) { - return static_cast(mpVtxBuffer)->resolveGeometryBinding(frame.m_currentFrame); + DrawPacket packet; + + DrawPacket::GeometrySetBinding binding{}; + packet.m_type = DrawPacket::DrawGeometryset; + binding.m_subAllocation = m_geometry.get(); + binding.m_indexOffset = 0; + binding.m_set = GraphicsAllocator::AllocationSet::OpaqueSet; + binding.m_numIndices = 6; + binding.m_vertexOffset = (m_activeCopy * 4); + packet.m_unified = binding; + return packet; } void cBeam::SetSize(const cVector2f& avSize) @@ -153,44 +215,13 @@ namespace hpl { if(mColor == aColor) return; mColor = aColor; - - float *pColors = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Color0); - - //Change "lower colors" - if(mbMultiplyAlphaWithColor) - { - for(int i=0; i<2;++i) - { - pColors[0] = mColor.r * mColor.a; - pColors[1] = mColor.g * mColor.a; - pColors[2] = mColor.b * mColor.a; - pColors[3] = mColor.a; - pColors+=4; - } - } - else - { - for(int i=0; i<2;++i) - { - pColors[0] = mColor.r; - pColors[1] = mColor.g; - pColors[2] = mColor.b; - pColors[3] = mColor.a; - pColors+=4; - } - } - mpVtxBuffer->UpdateData(eVertexElementFlag_Color0,false); } - //----------------------------------------------------------------------- - void cBeam::SetMaterial(cMaterial * apMaterial) { mpMaterial = apMaterial; } - //----------------------------------------------------------------------- - cBoundingVolume* cBeam::GetBoundingVolume() { if(mbUpdateBoundingVolume) @@ -218,8 +249,6 @@ namespace hpl { return &mBoundingVolume; } - //----------------------------------------------------------------------- - void cBeam::UpdateGraphicsForFrame(float afFrameTime) { if( mlStartTransformCount == GetTransformUpdateCount() && @@ -227,68 +256,83 @@ namespace hpl { { return; } + m_activeCopy = (m_activeCopy + 1) % ForgeRenderer::SwapChainLength; //////////////////////////////// //Get Axis mvAxis = mpEnd->GetWorldPosition() - GetWorldPosition(); - mvMidPosition =GetWorldPosition() + mvAxis*0.5f; + mvMidPosition = GetWorldPosition() + mvAxis*0.5f; float fDist = mvAxis.Length(); mvAxis.Normalize(); - //////////////////////////////// //Update vertex buffer cVector2f vBeamSize = cVector2f(mvSize.x, fDist); - - float *pPos = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Position); - float *pTex = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Texture0); - - cVector3f vCoords[4] = {cVector3f((vBeamSize.x/2),-(vBeamSize.y/2),0), - cVector3f(-(vBeamSize.x/2),-(vBeamSize.y/2),0), - cVector3f(-(vBeamSize.x/2),(vBeamSize.y/2),0), - cVector3f((vBeamSize.x/2),(vBeamSize.y/2),0)}; - - cVector3f vTexCoords[4]; - if(mbTileHeight) - { - vTexCoords[0] = cVector3f(1,1,0); //Bottom left - vTexCoords[1] = cVector3f(0,1,0); //Bottom right - vTexCoords[2] = cVector3f(0,-fDist/mvSize.y,0); //Top left - vTexCoords[3] = cVector3f(1,-fDist/mvSize.y,0); //Top right - } - else - { - vTexCoords[0] = cVector3f(1,1,0); //Bottom left - vTexCoords[1] = cVector3f(0,1,0); //Bottom right - vTexCoords[2] = cVector3f(0,0,0); //Top left - vTexCoords[3] = cVector3f(1,0,0); //Top right - } - - for(int i=0; i<4;++i) - { - pPos[0] = vCoords[i].x; - pPos[1] = vCoords[i].y; - pPos[2] = vCoords[i].z; - pPos+=4; - - pTex[0] = vTexCoords[i].x; - pTex[1] = vTexCoords[i].y; - pTex+=3; - } - - if(cMaterial::IsTranslucent(mpMaterial->Descriptor().m_id)) - { - mpVtxBuffer->UpdateData(eVertexElementFlag_Position | eVertexElementFlag_Texture0,false); - } - else - { - mpVtxBuffer->UpdateData(eVertexElementFlag_Position | eVertexElementFlag_Texture0,false); - } + const std::array vCoords = { float3((vBeamSize.x / 2), -(vBeamSize.y / 2), 0.0f), + float3(-(vBeamSize.x / 2), -(vBeamSize.y / 2), 0.0f), + float3(-(vBeamSize.x / 2), (vBeamSize.y / 2), 0.0f), + float3((vBeamSize.x / 2), (vBeamSize.y / 2), 0.0f) }; + + const std::array vTexCoords = mbTileHeight ? std::array { + float2(1.0f,1.0f), //Bottom left + float2(0.0f,1.0f), //Bottom right + float2(0.0f,-fDist/mvSize.y), //Top left + float2(1.0f,-fDist/mvSize.y) //Top right + } : std::array { + float2(1.0f,1.0f), //Bottom left + float2(0.0f,1.0f), //Bottom right + float2(0.0f,0.0f), //Top left + float2(1.0f,0.0f) //Top right + }; + + { + auto positionStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_POSITION); + auto colorStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_COLOR); + auto textureStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TEXCOORD0); + + BufferUpdateDesc positionUpdateDesc = { positionStream->buffer().m_handle, positionStream->stride() * 4 * m_activeCopy, positionStream->stride() * 4}; + BufferUpdateDesc textureUpdateDesc = { textureStream->buffer().m_handle, textureStream->stride() * 4 * m_activeCopy, textureStream->stride() * 4}; + BufferUpdateDesc colorUpdateDesc = { colorStream->buffer().m_handle, colorStream->stride() * 4 * m_activeCopy, colorStream->stride() * 4}; + beginUpdateResource(&positionUpdateDesc); + beginUpdateResource(&textureUpdateDesc); + beginUpdateResource(&colorUpdateDesc); + GraphicsBuffer gpuPositionBuffer(positionUpdateDesc); + GraphicsBuffer gpuTextureBuffer(textureUpdateDesc); + GraphicsBuffer gpuColorBuffer(colorUpdateDesc); + + auto positionView = gpuPositionBuffer.CreateStructuredView(); + auto textureView = gpuTextureBuffer.CreateStructuredView(); + auto colorView = gpuTextureBuffer.CreateStructuredView(); + for(int i=0; i < BeamNumberVerts ;++i) + { + positionView.Write(i, vCoords[i]); + textureView.Write(i, vTexCoords[i]); + } + cColor endColor = mpEnd->mColor; + cColor startColor = mColor; + if(mbMultiplyAlphaWithColor) { + colorView.Write( + 0, float4(startColor.r * startColor.a, startColor.g * startColor.a, startColor.b * startColor.a, startColor.a)); + colorView.Write( + 1, float4(startColor.r * startColor.a, startColor.g * startColor.a, startColor.b * startColor.a, startColor.a)); + colorView.Write(2, float4(endColor.r * endColor.a, endColor.g * endColor.a, endColor.b * endColor.a, endColor.a)); + colorView.Write(3, float4(endColor.r * endColor.a, endColor.g * endColor.a, endColor.b * endColor.a, endColor.a)); + } else { + colorView.Write( + 0, float4(startColor.r, startColor.g, startColor.b , startColor.a)); + colorView.Write( + 1, float4(startColor.r, startColor.g, startColor.b, startColor.a)); + colorView.Write(2, float4(endColor.r, endColor.g, endColor.b, endColor.a)); + colorView.Write(3, float4(endColor.r, endColor.g, endColor.b, endColor.a)); + } + + endUpdateResource(&positionUpdateDesc); + endUpdateResource(&textureUpdateDesc); + endUpdateResource(&colorUpdateDesc); + } } - //----------------------------------------------------------------------- - cMatrixf* cBeam::GetModelMatrix(cFrustum *apFrustum) { if(apFrustum==NULL)return &GetWorldMatrix(); @@ -333,17 +377,12 @@ namespace hpl { return &m_mtxTempTransform; } - //----------------------------------------------------------------------- - int cBeam::GetMatrixUpdateCount() { return GetTransformUpdateCount(); } - //----------------------------------------------------------------------- - - - bool cBeam::LoadXMLProperties(const tString asFile) + bool cBeam::LoadXMLProperties(const tString asFile) { msFileName = asFile; @@ -411,8 +450,6 @@ namespace hpl { return true; } - //----------------------------------------------------------------------- - bool cBeam::IsVisible() { if(mColor.r <= 0 && mColor.g <= 0 && mColor.b <= 0) return false; @@ -420,13 +457,6 @@ namespace hpl { return mbIsVisible; } - //----------------------------------------------------------------------- - - ////////////////////////////////////////////////////////////////////////// - // BEAM END TRANSFORM UPDATE CALLBACK - ////////////////////////////////////////////////////////////////////////// - - //----------------------------------------------------------------------- void cBeamEnd_UpdateCallback::OnTransformUpdate(iEntity3D * apEntity) { @@ -435,68 +465,10 @@ namespace hpl { pEnd->mpBeam->SetTransformUpdated(true); } - //----------------------------------------------------------------------- - - - ////////////////////////////////////////////////////////////////////////// - // BEAM END - ////////////////////////////////////////////////////////////////////////// - - //----------------------------------------------------------------------- - void cBeamEnd::SetColor(const cColor &aColor) { if(mColor == aColor) return; - mColor = aColor; - - float *pColors = mpBeam->mpVtxBuffer->GetFloatArray(eVertexBufferElement_Color0); - - //Change "upper colors" - pColors+= 4*2; - if(mpBeam->mbMultiplyAlphaWithColor) - { - for(int i=0; i<2;++i) - { - pColors[0] = mColor.r * mColor.a; - pColors[1] = mColor.g * mColor.a; - pColors[2] = mColor.b * mColor.a; - pColors[3] = mColor.a; - pColors+=4; - } - } - else - { - for(int i=0; i<2;++i) - { - pColors[0] = mColor.r; - pColors[1] = mColor.g; - pColors[2] = mColor.b; - pColors[3] = mColor.a; - pColors+=4; - } - } - - mpBeam->mpVtxBuffer->UpdateData(eVertexElementFlag_Color0,false); } - //----------------------------------------------------------------------- - - - ////////////////////////////////////////////////////////////////////////// - // PRIVATE METHODS - ////////////////////////////////////////////////////////////////////////// - - //----------------------------------------------------------------------- - - //----------------------------------------------------------------------- - - ////////////////////////////////////////////////////////////////////////// - // SAVE OBJECT STUFF - ////////////////////////////////////////////////////////////////////////// - - //----------------------------------------------------------------------- - - //----------------------------------------------------------------------- - } diff --git a/HPL2/sources/scene/RopeEntity.cpp b/HPL2/sources/scene/RopeEntity.cpp index 291104ca7..72568e8f3 100644 --- a/HPL2/sources/scene/RopeEntity.cpp +++ b/HPL2/sources/scene/RopeEntity.cpp @@ -26,6 +26,8 @@ #include "graphics/Material.h" #include "graphics/LowLevelGraphics.h" #include "graphics/VertexBuffer.h" +#include +#include #include "resources/Resources.h" #include "resources/MaterialManager.h" @@ -55,34 +57,76 @@ namespace hpl { mfLengthTileAmount = 1; mfLengthTileSize = 1; - mpVtxBuffer = mpLowLevelGraphics->CreateVertexBuffer( eVertexBufferType_Hardware, eVertexBufferDrawType_Tri, eVertexBufferUsageType_Dynamic, - 4 * mlMaxSegments, 6 * mlMaxSegments); - - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Position,eVertexBufferElementFormat_Float,4); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Normal,eVertexBufferElementFormat_Float,3); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Color0,eVertexBufferElementFormat_Float,4); - mpVtxBuffer->CreateElementArray(eVertexBufferElement_Texture0,eVertexBufferElementFormat_Float,3); - - for(int i=0; iAddVertexVec3f(eVertexBufferElement_Position, 0); - mpVtxBuffer->AddVertexColor(eVertexBufferElement_Color0, mColor); - mpVtxBuffer->AddVertexVec3f(eVertexBufferElement_Texture0, 0); - mpVtxBuffer->AddVertexVec3f(eVertexBufferElement_Normal, cVector3f(0,0,1)); - } - - for(int j=0;j<3;j++) mpVtxBuffer->AddIndex(j + i*4); - for(int j=2;j<5;j++) mpVtxBuffer->AddIndex( (j==4?0:j) + i*4); - } - - mpVtxBuffer->Compile(eVertexCompileFlag_CreateTangents); + { + auto* graphicsAllocator = Interface::Get(); + auto& opaqueSet = graphicsAllocator->resolveSet(GraphicsAllocator::AllocationSet::OpaqueSet); + m_geometry = opaqueSet.allocate(4 * mlMaxSegments, 6 * mlMaxSegments); + + auto& indexStream = m_geometry->indexBuffer(); + auto positionStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_POSITION); + auto normalStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_NORMAL); + auto colorStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_COLOR); + auto textureStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TEXCOORD0); + auto tangentStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TANGENT); + + BufferUpdateDesc indexUpdateDesc = { indexStream.m_handle, 0, GeometrySet::IndexBufferStride * 6 * mlMaxSegments}; + BufferUpdateDesc positionUpdateDesc = { positionStream->buffer().m_handle, 0, positionStream->stride() * 4 * mlMaxSegments }; + BufferUpdateDesc normalUpdateDesc = { normalStream->buffer().m_handle, 0, normalStream->stride() * 4 * mlMaxSegments}; + BufferUpdateDesc colorUpdateDesc = { colorStream->buffer().m_handle, 0, colorStream->stride() * 4 * mlMaxSegments}; + BufferUpdateDesc textureUpdateDesc = { textureStream->buffer().m_handle, 0, textureStream->stride() * 4 * mlMaxSegments}; + BufferUpdateDesc tangentUpdateDesc = { tangentStream->buffer().m_handle, 0, tangentStream->stride() * 4 * mlMaxSegments}; + + beginUpdateResource(&indexUpdateDesc); + beginUpdateResource(&positionUpdateDesc); + beginUpdateResource(&normalUpdateDesc); + beginUpdateResource(&colorUpdateDesc); + beginUpdateResource(&textureUpdateDesc); + beginUpdateResource(&tangentUpdateDesc); + + GraphicsBuffer gpuIndexBuffer(indexUpdateDesc); + GraphicsBuffer gpuPositionBuffer(positionUpdateDesc); + GraphicsBuffer gpuNormalBuffer(normalUpdateDesc); + GraphicsBuffer gpuColorBuffer(colorUpdateDesc); + GraphicsBuffer gpuTextureBuffer(textureUpdateDesc); + GraphicsBuffer gpuTangentBuffer(tangentUpdateDesc); + auto indexView = gpuPositionBuffer.CreateIndexView(); + uint32_t index = 0; + for (int i = 0; i < mlMaxSegments; ++i) { + for (int j = 0; j < 3; j++) { + indexView.Write(index, j + i * 4); + } + for (int j = 2; j < 5; j++) { + indexView.Write(index, (j == 4 ? 0 : j) + i * 4); + } + } + + for(size_t copyIdx = 0; copyIdx < 2; copyIdx++) { + auto positionView = gpuPositionBuffer.CreateStructuredView(positionStream->stride() * copyIdx * mlMaxSegments * 4); + auto normalView = gpuNormalBuffer.CreateStructuredView(normalStream->stride() * copyIdx * mlMaxSegments * 4); + auto colorView = gpuColorBuffer.CreateStructuredView(colorStream->stride() * copyIdx * mlMaxSegments * 4); + auto textureView = gpuTextureBuffer.CreateStructuredView(textureStream->stride() * copyIdx * mlMaxSegments * 4); + auto tangentView = gpuTangentBuffer.CreateStructuredView(tangentStream->stride() * copyIdx * mlMaxSegments * 4); + for(int i=0; i < mlMaxSegments; ++i) + { + for(size_t j = 0; j < 4; j++) { + tangentView.Write((i * 4) + j, float3(0, 0, 1)); + positionView.Write((i * 4) + j, float3(0,0,0)); + normalView.Write((i * 4) + j, float3(0.0f, 0.0f, 1.0f)); + colorView.Write((i * 4) + j, float4(mColor.r, mColor.g, mColor.g, mColor.a)); + textureView.Write((i * 4) + j, float2(0,0)); + } + } + Matrix4 trans = Matrix4::identity(); + hpl::MeshUtility::MikkTSpaceGenerate(4 * mlMaxSegments, 6 * mlMaxSegments, &indexView, &positionView, &textureView, &normalView, &tangentView); + } + + endUpdateResource(&indexUpdateDesc); + endUpdateResource(&positionUpdateDesc); + endUpdateResource(&normalUpdateDesc); + endUpdateResource(&colorUpdateDesc); + endUpdateResource(&textureUpdateDesc); + endUpdateResource(&tangentUpdateDesc); + } mbApplyTransformToBV = false; @@ -92,14 +136,27 @@ namespace hpl { //----------------------------------------------------------------------- DrawPacket cRopeEntity::ResolveDrawPacket(const ForgeRenderer::Frame& frame) { + DrawPacket packet; + if(m_numberSegments <= 0) { + return packet; + } - return static_cast(mpVtxBuffer)->resolveGeometryBinding(frame.m_currentFrame); + DrawPacket::GeometrySetBinding binding{}; + packet.m_type = DrawPacket::DrawGeometryset; + binding.m_subAllocation = m_geometry.get(); + binding.m_indexOffset = 0; + binding.m_set = GraphicsAllocator::AllocationSet::OpaqueSet; + binding.m_numIndices = 6 * m_numberSegments; + binding.m_vertexOffset = (m_activeCopy * mlMaxSegments * 4) + (m_numberSegments * 4); + packet.m_unified = binding; + return packet; + // return static_cast(mpVtxBuffer)->resolveGeometryBinding(frame.m_currentFrame); } cRopeEntity::~cRopeEntity() { if(mpMaterial) mpMaterialManager->Destroy(mpMaterial); - if(mpVtxBuffer) hplDelete(mpVtxBuffer); + //if(mpVtxBuffer) hplDelete(mpVtxBuffer); } @@ -118,26 +175,26 @@ namespace hpl { mColor = aColor; - float *pColors = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Color0); - - cColor finalColor = mColor; - if(mbMultiplyAlphaWithColor) - { - finalColor.r = finalColor.r * mColor.a; - finalColor.g = finalColor.g * mColor.a; - finalColor.b = finalColor.b * mColor.a; - } - - for(int i=0; iUpdateData(eVertexElementFlag_Color0,false); + //float *pColors = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Color0); + // + //cColor finalColor = mColor; + //if(mbMultiplyAlphaWithColor) + //{ + // finalColor.r = finalColor.r * mColor.a; + // finalColor.g = finalColor.g * mColor.a; + // finalColor.b = finalColor.b * mColor.a; + //} + // + //for(int i=0; iUpdateData(eVertexElementFlag_Color0,false); } //----------------------------------------------------------------------- @@ -179,25 +236,6 @@ namespace hpl { } - //----------------------------------------------------------------------- - - static inline void SetVec3(float *apPos, const cVector3f &aPos) - { - apPos[0] = aPos.x; - apPos[1] = aPos.y; - apPos[2] = aPos.z; - } - - static inline void SetVec4(float *apPos, const cVector3f &aPos) - { - apPos[0] = aPos.x; - apPos[1] = aPos.y; - apPos[2] = aPos.z; - apPos[3] = 1; - } - - //----------------------------------------------------------------------- - static cVector2f gvPosAdd[4] = { //cVector2f (1,1), cVector2f (1,0), cVector2f (-1,0), cVector2f (-1,1) cVector2f (1,0), cVector2f (-1,0), cVector2f (-1,1), cVector2f (1,1) @@ -205,18 +243,52 @@ namespace hpl { bool cRopeEntity::UpdateGraphicsForViewport(cFrustum *apFrustum,float afFrameTime) { - float *pPosArray = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Position); - float *pUvArray = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Texture0); - float *pNrmArray = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Normal); - float *pTanArray = mpVtxBuffer->GetFloatArray(eVertexBufferElement_Texture1Tangent); + m_activeCopy = (m_activeCopy + 1) % ForgeRenderer::SwapChainLength; + cColor finalColor = mColor; + if(mbMultiplyAlphaWithColor) + { + finalColor.r = finalColor.r * mColor.a; + finalColor.g = finalColor.g * mColor.a; + finalColor.b = finalColor.b * mColor.a; + } + + + auto positionStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_POSITION); + auto colorStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_COLOR); + auto textureStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TEXCOORD0); + auto normalStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_NORMAL); + auto tangentStream = m_geometry->getStreamBySemantic(ShaderSemantic::SEMANTIC_TANGENT); + + BufferUpdateDesc positionUpdateDesc = { positionStream->buffer().m_handle, positionStream->stride() * mlMaxSegments * 4 * m_activeCopy, positionStream->stride() * mlMaxSegments * 4 }; + BufferUpdateDesc textureUpdateDesc = { textureStream->buffer().m_handle, textureStream->stride() * mlMaxSegments * 4 * m_activeCopy, textureStream->stride() * mlMaxSegments * 4 }; + BufferUpdateDesc colorUpdateDesc = { colorStream->buffer().m_handle, colorStream->stride() * mlMaxSegments * 4 * m_activeCopy, colorStream->stride() * mlMaxSegments * 4 }; + BufferUpdateDesc normalUpdateDesc = { normalStream->buffer().m_handle, normalStream->stride() * mlMaxSegments * 4 * m_activeCopy, normalStream->stride() * mlMaxSegments * 4 }; + BufferUpdateDesc tangentUpdateDesc = { tangentStream->buffer().m_handle, tangentStream->stride() * mlMaxSegments * 4 * m_activeCopy, tangentStream->stride() * mlMaxSegments * 4 }; + + beginUpdateResource(&positionUpdateDesc); + beginUpdateResource(&textureUpdateDesc); + beginUpdateResource(&colorUpdateDesc); + beginUpdateResource(&normalUpdateDesc); + beginUpdateResource(&tangentUpdateDesc); + + GraphicsBuffer gpuPositionBuffer(positionUpdateDesc); + GraphicsBuffer gpuNormalBuffer(normalUpdateDesc); + GraphicsBuffer gpuColorBuffer(colorUpdateDesc); + GraphicsBuffer gpuTextureBuffer(textureUpdateDesc); + GraphicsBuffer gpuTangentBuffer(tangentUpdateDesc); + auto positionView = gpuPositionBuffer.CreateStructuredView(); + auto normalView = gpuNormalBuffer.CreateStructuredView(); + auto colorView = gpuColorBuffer.CreateStructuredView(); + auto textureView = gpuTextureBuffer.CreateStructuredView(); + auto tangentView = gpuTangentBuffer.CreateStructuredView(); float fSegmentLength = mpRope->GetSegmentLength(); - cVector3f vTexCoords[4] = { - cVector3f(1,1,0), //Bottom left - cVector3f(0,1,0), //Bottom right - cVector3f(0,0,0), //Top left - cVector3f(1,0,0) //Top right + cVector2f vTexCoords[4] = { + cVector2f(1,1), //Bottom left + cVector2f(0,1), //Bottom right + cVector2f(0,0), //Top left + cVector2f(1,0) //Top right }; vTexCoords[0].y *= mfLengthTileAmount; @@ -225,7 +297,9 @@ namespace hpl { cVerletParticleIterator it = mpRope->GetParticleIterator(); int lCount=0; cVector3f vPrevPos; - while(it.HasNext()) + + size_t segmentIndex = 0; + while(it.HasNext()) { if(lCount >= mlMaxSegments) break; ++lCount; @@ -237,7 +311,6 @@ namespace hpl { continue; } - ///////////////////////// //Calculate properties cVector3f vPos = pPart->GetSmoothPosition(); cVector3f vDelta = vPos - vPrevPos; @@ -246,75 +319,68 @@ namespace hpl { cVector3f vRight = cMath::Vector3Normalize(cMath::Vector3Cross(vUp, apFrustum->GetForward())); cVector3f vFwd = cMath::Vector3Cross(vRight, vUp); - ///////////////////////// //Update position - for(int i=0; i<4; ++i) - SetVec4(&pPosArray[i*4], vPrevPos + vRight * gvPosAdd[i].x*mfRadius + vUp * gvPosAdd[i].y*fLength); + for(int i=0; i<4; ++i) { + positionView.Write(segmentIndex + i, v3ToF3(cMath::ToForgeVec3(vPrevPos + vRight * gvPosAdd[i].x*mfRadius + vUp * gvPosAdd[i].y*fLength))); + //SetVec4(&pPosArray[i*4], vPrevPos + vRight * gvPosAdd[i].x*mfRadius + vUp * gvPosAdd[i].y*fLength); + } - ///////////////////////// //Update uv if(lCount==2 && (fLength < fSegmentLength || fSegmentLength==0)) { - ////////////////// //No segments if(fSegmentLength==0) { float fYAdd = 1 - fLength/ mfLengthTileSize; - - SetVec3(&pUvArray[0*3], vTexCoords[0] - cVector3f(0,fYAdd,0)); - SetVec3(&pUvArray[1*3], vTexCoords[1] - cVector3f(0,fYAdd,0)); - - SetVec3(&pUvArray[2*3], vTexCoords[2]); - SetVec3(&pUvArray[3*3], vTexCoords[3]); + textureView.Write(segmentIndex + 0, v2ToF2(cMath::ToForgeVec2(vTexCoords[0] - cVector2f(0,fYAdd)))); + textureView.Write(segmentIndex + 1, v2ToF2(cMath::ToForgeVec2(vTexCoords[1] - cVector2f(0,fYAdd)))); + textureView.Write(segmentIndex + 2, v2ToF2(cMath::ToForgeVec2(vTexCoords[2]))); + textureView.Write(segmentIndex + 3, v2ToF2(cMath::ToForgeVec2(vTexCoords[3]))); } - ////////////////// //First segment of many else { float fYAdd = (1 - (fLength / fSegmentLength))*mfLengthTileAmount; - SetVec3(&pUvArray[0*3], vTexCoords[0] - cVector3f(0,fYAdd,0) ); - SetVec3(&pUvArray[1*3], vTexCoords[1] - cVector3f(0,fYAdd,0) ); - - SetVec3(&pUvArray[2*3], vTexCoords[2]); - SetVec3(&pUvArray[3*3], vTexCoords[3]); + textureView.Write(segmentIndex + 0, v2ToF2(cMath::ToForgeVec2(vTexCoords[0] - cVector2f(0,fYAdd)))); + textureView.Write(segmentIndex + 1, v2ToF2(cMath::ToForgeVec2(vTexCoords[1] - cVector2f(0,fYAdd)))); + textureView.Write(segmentIndex + 2, v2ToF2(cMath::ToForgeVec2(vTexCoords[2]))); + textureView.Write(segmentIndex + 3, v2ToF2(cMath::ToForgeVec2(vTexCoords[3]))); } } else { - for(int i=0; i<4; ++i) - SetVec3(&pUvArray[i*3], vTexCoords[i]); + for(int i=0; i<4; ++i) { + textureView.Write(segmentIndex + i, v2ToF2(cMath::ToForgeVec2(vTexCoords[i]))); + } } - ///////////////////////// //Update Normal and Tangent for(int i=0; i<4; ++i) { - SetVec3(&pNrmArray[i*3], vFwd); - SetVec4(&pTanArray[i*4], vRight); + normalView.Write(i + segmentIndex, v3ToF3(cMath::ToForgeVec3(vFwd))); + tangentView.Write(i + segmentIndex, v3ToF3(cMath::ToForgeVec3(vRight))); + colorView.Write(i + segmentIndex,float4(finalColor.r,finalColor.g,finalColor.b,finalColor.a)); } - ///////////////////////// - //Update pointers - pPosArray += 4 * 4; - pUvArray += 3 * 4; - pNrmArray += 3 * 4; - pTanArray += 4 * 4; + segmentIndex++; - ///////////////////////// //Update misc vPrevPos = vPos; } - mpVtxBuffer->SetElementNum((lCount-1) * 6); - - mpVtxBuffer->UpdateData(eVertexElementFlag_Position | eVertexElementFlag_Texture0 | eVertexElementFlag_Texture1 | eVertexElementFlag_Normal, false); + endUpdateResource(&positionUpdateDesc); + endUpdateResource(&textureUpdateDesc); + endUpdateResource(&colorUpdateDesc); + endUpdateResource(&normalUpdateDesc); + endUpdateResource(&tangentUpdateDesc); + m_numberSegments = lCount - 1; + //mpVtxBuffer->SetElementNum((lCount-1) * 6); + //mpVtxBuffer->UpdateData(eVertexElementFlag_Position | eVertexElementFlag_Texture0 | eVertexElementFlag_Texture1 | eVertexElementFlag_Normal, false); return true; } - //----------------------------------------------------------------------- - cMatrixf* cRopeEntity::GetModelMatrix(cFrustum *apFrustum) { if(apFrustum==NULL)return &GetWorldMatrix(); @@ -322,15 +388,11 @@ namespace hpl { return NULL; } - //----------------------------------------------------------------------- - int cRopeEntity::GetMatrixUpdateCount() { return GetTransformUpdateCount(); } - //----------------------------------------------------------------------- - bool cRopeEntity::IsVisible() { if(mColor.r <= 0 && mColor.g <= 0 && mColor.b <= 0) return false; @@ -339,15 +401,6 @@ namespace hpl { } - //----------------------------------------------------------------------- - - - ////////////////////////////////////////////////////////////////////////// - // PRIVATE METHODS - ////////////////////////////////////////////////////////////////////////// - //----------------------------------------------------------------------- - - //----------------------------------------------------------------------- } diff --git a/HPL2/sources/scene/SubMeshEntity.cpp b/HPL2/sources/scene/SubMeshEntity.cpp index 1f6446b2e..fd129ec45 100644 --- a/HPL2/sources/scene/SubMeshEntity.cpp +++ b/HPL2/sources/scene/SubMeshEntity.cpp @@ -21,7 +21,6 @@ #include "Common_3/Resources/ResourceLoader/Interfaces/IResourceLoader.h" #include "FixPreprocessor.h" - #include "graphics/ForgeRenderer.h" #include "graphics/GraphicsAllocator.h" #include "graphics/GraphicsBuffer.h"