Skip to content

Commit

Permalink
D3D11: use global instead of per shader cbuffers
Browse files Browse the repository at this point in the history
as we overwrite all data on binding anyway
  • Loading branch information
paroj committed Sep 8, 2024
1 parent cf0f1e9 commit 2b00a11
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 230 deletions.
4 changes: 2 additions & 2 deletions RenderSystems/Direct3D11/include/OgreD3D11HLSLProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace Ogre {
};

typedef std::vector<D3D11_SIGNATURE_PARAMETER_DESC> D3d11ShaderParameters;
typedef std::map<String, int> BufferInfoMap;
protected:

static CmdTarget msCmdTarget;
Expand Down Expand Up @@ -106,7 +107,6 @@ namespace Ogre {
HardwareBufferPtr mDefaultBuffer; // for $Globals OR $Params

// Make sure that objects have index and name, or some search will fail
typedef std::map<String, int> BufferInfoMap;
BufferInfoMap mBufferInfoMap;

// Map to store interface slot position.
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace Ogre {
ID3D11ComputeShader* getComputeShader(void) const;
const MicroCode & getMicroCode(void) const;

std::vector<ID3D11Buffer*> getConstantBuffers(const GpuProgramParametersPtr& params);
const BufferInfoMap& getBufferInfoMap() const { return mBufferInfoMap; }

// Get slot for a specific interface
unsigned int getSubroutineSlot(const String& subroutineSlotName) const;
Expand Down
15 changes: 3 additions & 12 deletions RenderSystems/Direct3D11/include/OgreD3D11RenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ namespace Ogre

bool mReadBackAsTexture;

D3D11HLSLProgram* mBoundVertexProgram;
D3D11HLSLProgram* mBoundFragmentProgram;
D3D11HLSLProgram* mBoundGeometryProgram;
D3D11HLSLProgram* mBoundTessellationHullProgram;
D3D11HLSLProgram* mBoundTessellationDomainProgram;
D3D11HLSLProgram* mBoundComputeProgram;
std::array<D3D11HLSLProgram*, GPT_COUNT> mBoundProgram;

ComPtr<ID3D11ShaderResourceView> mDSTResView;
ComPtr<ID3D11BlendState> mBoundBlendState;
Expand All @@ -141,6 +136,8 @@ 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];

Expand Down Expand Up @@ -259,12 +256,6 @@ namespace Ogre
void setStencilState(const StencilState& state) override;

// Low-level overridden members, mainly for internal use
D3D11HLSLProgram* _getBoundVertexProgram() const;
D3D11HLSLProgram* _getBoundFragmentProgram() const;
D3D11HLSLProgram* _getBoundGeometryProgram() const;
D3D11HLSLProgram* _getBoundTessellationHullProgram() const;
D3D11HLSLProgram* _getBoundTessellationDomainProgram() const;
D3D11HLSLProgram* _getBoundComputeProgram() const;
void _setTexture(size_t unit, bool enabled, const TexturePtr &texPtr);
void _setSampler(size_t unit, Sampler& sampler);
void _setAlphaRejectSettings( CompareFunction func, unsigned char value, bool alphaToCoverage );
Expand Down
38 changes: 4 additions & 34 deletions RenderSystems/Direct3D11/src/OgreD3D11HLSLProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,8 @@ namespace Ogre {
{
getConstantDefinitions();

bool hasDefaultBuffer = false;

for(UINT b = 0; b < mConstantBufferNr; b++)
{
switch (mD3d11ShaderBufferDescs[b].Type)
Expand All @@ -819,12 +821,13 @@ namespace Ogre {
String cb_name = mD3d11ShaderBufferDescs[b].Name;
if(cb_name == "$Globals" || cb_name == "$Params" || cb_name == "OgreUniforms")
{
if(mDefaultBuffer)
if(hasDefaultBuffer)
LogManager::getSingleton().logError(mName+" - default cbuffer already exists. Ignoring "+cb_name);
else
{
mDefaultBuffer = HardwareBufferManager::getSingleton().createUniformBuffer(mD3d11ShaderBufferDescs[b].Size);
}
hasDefaultBuffer = true;
}
else
{
Expand Down Expand Up @@ -1514,39 +1517,6 @@ namespace Ogre {
return it->second;
}
//-----------------------------------------------------------------------------
std::vector<ID3D11Buffer*> D3D11HLSLProgram::getConstantBuffers(const GpuProgramParametersPtr& params)
{
std::vector<ID3D11Buffer*> buffers;
if(mDefaultBuffer)
{
OgreAssert(mDefaultBuffer->getSizeInBytes() == params->getConstantList().size(), "unexpected buffer size");
mDefaultBuffer->writeData(0, mDefaultBuffer->getSizeInBytes(), params->getConstantList().data(), true);

buffers.push_back(static_cast<D3D11HardwareBuffer*>(mDefaultBuffer.get())->getD3DBuffer());
}
else
{
buffers.push_back(NULL);
}

for (const auto& usage : params->getSharedParameters())
{
if(const auto& buf = usage.getSharedParams()->_getHardwareBuffer())
{
// hardware baked cbuffer
auto it = mBufferInfoMap.find(usage.getName());
if(it == mBufferInfoMap.end())
continue; // TODO: error?

size_t slot = it->second;
buffers.resize(std::max(slot + 1, buffers.size()));
buffers[slot] = static_cast<D3D11HardwareBuffer*>(buf.get())->getD3DBuffer();
}
}

return buffers;
}
//-----------------------------------------------------------------------------
ID3D11VertexShader* D3D11HLSLProgram::getVertexShader(void) const
{
assert(mType == GPT_VERTEX_PROGRAM);
Expand Down
Loading

0 comments on commit 2b00a11

Please sign in to comment.