From 194a77503c97863cb8e65acfc78e61f5e728146f Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Sun, 8 Sep 2024 16:51:04 +0200 Subject: [PATCH] Main: factor out common D3D11 and GL3Plus UBO code --- OgreMain/include/OgreRenderSystem.h | 4 ++++ OgreMain/src/OgreRenderSystem.cpp | 12 ++++++++++++ .../Direct3D11/include/OgreD3D11RenderSystem.h | 2 -- .../Direct3D11/src/OgreD3D11RenderSystem.cpp | 11 ++--------- .../GL3Plus/include/OgreGL3PlusRenderSystem.h | 2 -- .../GL3Plus/src/OgreGL3PlusRenderSystem.cpp | 8 +------- 6 files changed, 19 insertions(+), 20 deletions(-) diff --git a/OgreMain/include/OgreRenderSystem.h b/OgreMain/include/OgreRenderSystem.h index a4923146601..87bedd9697c 100644 --- a/OgreMain/include/OgreRenderSystem.h +++ b/OgreMain/include/OgreRenderSystem.h @@ -1201,6 +1201,8 @@ namespace Ogre void setFFPLightParams(uint32 index, bool enabled); bool flipFrontFace() const; static CompareFunction reverseCompareFunction(CompareFunction func); + + const HardwareBufferPtr& updateDefaultUniformBuffer(GpuProgramType type, const ConstantList& params); private: StencilState mStencilState; @@ -1208,6 +1210,8 @@ namespace Ogre HardwareVertexBufferSharedPtr mGlobalInstanceVertexBuffer; /// a vertex declaration for the global vertex buffer for the global instancing VertexDeclaration* mGlobalInstanceVertexDeclaration; + /// buffers for default uniform blocks + HardwareBufferPtr mUniformBuffer[GPT_COUNT]; /// the number of global instances (this number will be multiply by the render op instance number) uint32 mGlobalNumberOfInstances; }; diff --git a/OgreMain/src/OgreRenderSystem.cpp b/OgreMain/src/OgreRenderSystem.cpp index 2160ffb6558..9349be8653b 100644 --- a/OgreMain/src/OgreRenderSystem.cpp +++ b/OgreMain/src/OgreRenderSystem.cpp @@ -136,6 +136,18 @@ namespace Ogre { mFixedFunctionParams->setAutoConstant(light_offset + 5, GpuProgramParameters::ACT_SPOTLIGHT_PARAMS, index); } + const HardwareBufferPtr& RenderSystem::updateDefaultUniformBuffer(GpuProgramType gptype, const ConstantList& params) + { + auto& ubo = mUniformBuffer[gptype]; + if (!ubo || ubo->getSizeInBytes() < params.size()) + { + ubo = HardwareBufferManager::getSingleton().createUniformBuffer(params.size()); + } + + ubo->writeData(0, params.size(), params.data(), true); + + return ubo; + } //----------------------------------------------------------------------- RenderSystem::~RenderSystem() { diff --git a/RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h b/RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h index d06a9a02689..6961c803a80 100644 --- a/RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h +++ b/RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h @@ -136,8 +136,6 @@ namespace Ogre ID3D11ShaderResourceView * mBoundTextures[OGRE_MAX_TEXTURE_LAYERS]; size_t mBoundTexturesCount; - HardwareBufferPtr mConstantBuffer[GPT_COUNT]; // for $Globals OR $Params - // List of class instances per shader stage ID3D11ClassInstance* mClassInstances[6][8]; diff --git a/RenderSystems/Direct3D11/src/OgreD3D11RenderSystem.cpp b/RenderSystems/Direct3D11/src/OgreD3D11RenderSystem.cpp index da18992f4c0..30ea7235d40 100644 --- a/RenderSystems/Direct3D11/src/OgreD3D11RenderSystem.cpp +++ b/RenderSystems/Direct3D11/src/OgreD3D11RenderSystem.cpp @@ -2272,16 +2272,9 @@ namespace Ogre std::vector buffers = {NULL}; - auto paramsSize = params->getConstantList().size(); - if(paramsSize) + if(params->getConstantList().size()) { - auto& cbuffer = mConstantBuffer[gptype]; - - if(!cbuffer || cbuffer->getSizeInBytes() < paramsSize) - cbuffer = mHardwareBufferManager->createUniformBuffer(paramsSize); - - cbuffer->writeData(0, paramsSize, params->getConstantList().data(), true); - + auto& cbuffer = updateDefaultUniformBuffer(gptype, params->getConstantList()); buffers[0] = static_cast(cbuffer.get())->getD3DBuffer(); } diff --git a/RenderSystems/GL3Plus/include/OgreGL3PlusRenderSystem.h b/RenderSystems/GL3Plus/include/OgreGL3PlusRenderSystem.h index 7e146bcc67c..d2957747acf 100644 --- a/RenderSystems/GL3Plus/include/OgreGL3PlusRenderSystem.h +++ b/RenderSystems/GL3Plus/include/OgreGL3PlusRenderSystem.h @@ -102,8 +102,6 @@ namespace Ogre { std::array mCurrentShader; - HardwareBufferPtr mUniformBuffer[GPT_COUNT]; - GLenum getBlendMode(SceneBlendFactor ogreBlend) const; void bindVertexElementToGpu(const VertexElement& elem, diff --git a/RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp b/RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp index 393280d6821..0bb4878018f 100644 --- a/RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp +++ b/RenderSystems/GL3Plus/src/OgreGL3PlusRenderSystem.cpp @@ -1652,13 +1652,7 @@ namespace Ogre { if (paramsSize && getCapabilities()->hasCapability(RSC_SEPARATE_SHADER_OBJECTS) && !params->hasLogicalIndexedParameters()) { - auto& ubo = mUniformBuffer[gptype]; - if(!ubo || ubo->getSizeInBytes() < paramsSize) - { - ubo = mHardwareBufferManager->createUniformBuffer(paramsSize); - } - - ubo->writeData(0, ubo->getSizeInBytes(), params->getConstantList().data(), true); + auto& ubo = updateDefaultUniformBuffer(gptype, params->getConstantList()); int binding = gptype == GPT_COMPUTE_PROGRAM ? 0 : (int(gptype) % GPT_PIPELINE_COUNT); static_cast(ubo.get())->setGLBufferBinding(binding);