Skip to content

Commit

Permalink
[D3D11] HLSL shaders were creating one v1 constant buffer per defined…
Browse files Browse the repository at this point in the history
… constant buffer. Removed. Only $Global & $Params buffers are defined.

[D3D11] HLSL shaders were incorrectly handling $Global & $Params buffers (it only worked if only one of these buffers was used and no other const buffer was defined at slot b0)
IMPORTANT: [D3D11] Microcode changed. User's caches must be deleted and recreated otherwise loading old caches will crash.
  • Loading branch information
darksylinc committed Apr 11, 2016
1 parent f486957 commit 632c635
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 134 deletions.
25 changes: 16 additions & 9 deletions RenderSystems/Direct3D11/include/OgreD3D11HLSLProgram.h
Expand Up @@ -250,9 +250,9 @@ namespace Ogre {
};

// Make sure that objects have index and name, or some search will fail
typedef std::set<BufferInfo> BufferInfoMap;
typedef std::set<BufferInfo>::iterator BufferInfoIterator;
BufferInfoMap mBufferInfoMap;
// typedef std::set<BufferInfo> BufferInfoMap;
// typedef std::set<BufferInfo>::iterator BufferInfoIterator;
// BufferInfoMap mBufferInfoMap;

// Map to store interface slot position.
// Number of interface slots is size of this map.
Expand All @@ -274,7 +274,7 @@ namespace Ogre {
typedef vector<GpuConstantDefinitionWithName>::type D3d11ShaderVariableSubparts;
typedef D3d11ShaderVariableSubparts::iterator D3d11ShaderVariableSubpartsIter;

typedef struct MemberTypeName
struct MemberTypeName
{
LPCSTR Name;
};
Expand All @@ -299,6 +299,15 @@ namespace Ogre {
D3d11ShaderVariables mVarDescPointer;
D3d11ShaderTypeDescs mD3d11ShaderTypeDescs;
D3d11ShaderTypeDescs mMemberTypeDesc;
enum DefaultBufferTypes
{
BufferGlobal,
BufferParam,
NumDefaultBufferTypes
};
//D3D11_SHADER_INPUT_BIND_DESC mDefaultBufferBindPoints[NumDefaultBufferTypes];
UINT mDefaultBufferBindPoint;
BufferInfo mDefaultBuffers[NumDefaultBufferTypes];
MemberTypeNames mMemberTypeName;
InterfaceSlots mInterfaceSlots;

Expand Down Expand Up @@ -352,11 +361,9 @@ namespace Ogre {
ID3D11ComputeShader* getComputeShader(void) const;
const MicroCode & getMicroCode(void) const;

ID3D11Buffer* getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask);

void getConstantBuffers(ID3D11Buffer** buffers, unsigned int& numBuffers,
ID3D11ClassInstance** classes, unsigned int& numInstances,
GpuProgramParametersSharedPtr params, uint16 variabilityMask);
/// buffers must have a capacity of 2, i.e. ID3D11Buffer *buffers[2];
void getConstantBuffers( ID3D11Buffer** buffers, UINT &outSlotStart, UINT &outNumBuffers,
GpuProgramParametersSharedPtr params, uint16 variabilityMask );

// Get slot for a specific interface
unsigned int getSubroutineSlot(const String& subroutineSlotName) const;
Expand Down
127 changes: 55 additions & 72 deletions RenderSystems/Direct3D11/src/OgreD3D11HLSLProgram.cpp
Expand Up @@ -332,6 +332,7 @@ namespace Ogre {
cacheMicrocode->read(&mConstantBufferSize, sizeof(uint32));
cacheMicrocode->read(&mConstantBufferNr, sizeof(uint32));
cacheMicrocode->read(&mNumSlots, sizeof(uint32));
cacheMicrocode->read(&mDefaultBufferBindPoint, sizeof(uint32));

READ_START(mD3d11ShaderInputParameters, D3D11_SIGNATURE_PARAMETER_DESC)
READ_NAME(SemanticName)
Expand Down Expand Up @@ -605,6 +606,23 @@ namespace Ogre {
mSerStrings.push_back(name);
curParam.SemanticName = &(*name)[0];
}

{
mDefaultBufferBindPoint = -1;

D3D11_SHADER_INPUT_BIND_DESC curParam;
HRESULT hr;
hr = shaderReflection->GetResourceBindingDescByName( "$Global", &curParam );

if( SUCCEEDED(hr) )
mDefaultBufferBindPoint = std::min( curParam.BindPoint, mDefaultBufferBindPoint );

hr = shaderReflection->GetResourceBindingDescByName( "$Params", &curParam );

if( SUCCEEDED(hr) )
mDefaultBufferBindPoint = std::min( curParam.BindPoint, mDefaultBufferBindPoint );
}

/*
if (shaderDesc.ConstantBuffers > 1)
{
Expand Down Expand Up @@ -794,6 +812,7 @@ namespace Ogre {
+ sizeof(uint32) // mConstantBufferSize
+ sizeof(uint32) // mConstantBufferNr
+ sizeof(uint32) // mNumSlots
+ sizeof(uint32) // mDefaultBufferBindPoint
SIZE_OF_DATA_START(mD3d11ShaderInputParameters, D3D11_SIGNATURE_PARAMETER_DESC)
SIZE_OF_DATA_NAME(SemanticName)
SIZE_OF_DATA_UINT(SemanticIndex)
Expand Down Expand Up @@ -954,6 +973,7 @@ namespace Ogre {
newMicrocode->write(&mConstantBufferSize, sizeof(uint32));
newMicrocode->write(&mConstantBufferNr, sizeof(uint32));
newMicrocode->write(&mNumSlots, sizeof(uint32));
newMicrocode->write(&mDefaultBufferBindPoint, sizeof(uint32));

WRITE_START(mD3d11ShaderInputParameters, D3D11_SIGNATURE_PARAMETER_DESC)
WRITE_NAME(SemanticName)
Expand Down Expand Up @@ -1126,9 +1146,13 @@ namespace Ogre {
// so parse all.
case D3D_CT_CBUFFER:
case D3D_CT_TBUFFER:
if( !strcmp( mD3d11ShaderBufferDescs[b].Name, "$Global" ) ||
!strcmp( mD3d11ShaderBufferDescs[b].Name, "$Params" ) )
{
// Insert buffer info
BufferInfoIterator it = mBufferInfoMap.insert(BufferInfo(0, mD3d11ShaderBufferDescs[b].Name)).first;
const DefaultBufferTypes bufferType = !strcmp( mD3d11ShaderBufferDescs[b].Name,
"$Global" ) ? BufferGlobal :
BufferParam;
BufferInfo *it = &mDefaultBuffers[bufferType];

// Guard to create uniform buffer only once
if (it->mUniformBuffer.isNull())
Expand Down Expand Up @@ -1232,7 +1256,8 @@ namespace Ogre {
void D3D11HLSLProgram::unloadHighLevelImpl(void)
{
mSlotMap.clear();
mBufferInfoMap.clear();
for( size_t i=0; i<NumDefaultBufferTypes; ++i )
mDefaultBuffers[i] = BufferInfo();

InputLayoutVaoBindVec::const_iterator itor = mInputLayoutVaoBind.begin();
InputLayoutVaoBindVec::const_iterator end = mInputLayoutVaoBind.end();
Expand Down Expand Up @@ -1520,6 +1545,7 @@ namespace Ogre {
, mVertexShader(NULL), mConstantBufferSize(0)
, mPixelShader(NULL),mGeometryShader(NULL), mHullShader(NULL), mDomainShader(NULL), mComputeShader(NULL)
, mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false), shaderMacroSet(false)
, mDefaultBufferBindPoint(-1)
{
#if SUPPORT_SM2_0_HLSL_SHADERS == 1
mEnableBackwardsCompatibility = true;
Expand Down Expand Up @@ -1552,7 +1578,8 @@ namespace Ogre {
D3D11HLSLProgram::~D3D11HLSLProgram()
{
//SAFE_RELEASE(mConstantBuffer);
mBufferInfoMap.clear();
for( size_t i=0; i<NumDefaultBufferTypes; ++i )
mDefaultBuffers[i] = BufferInfo();

// have to call this here reather than in Resource destructor
// since calling virtual methods in base destructors causes crash
Expand Down Expand Up @@ -2070,64 +2097,16 @@ namespace Ogre {
return d3dInputLayout;
}
//-----------------------------------------------------------------------------
ID3D11Buffer* D3D11HLSLProgram::getConstantBuffer(GpuProgramParametersSharedPtr params, uint16 variabilityMask)
void D3D11HLSLProgram::getConstantBuffers( ID3D11Buffer** buffers, UINT &outSlotStart,
UINT &outNumBuffers, GpuProgramParametersSharedPtr params,
uint16 variabilityMask )
{
// Update the Constant Buffer

if(!mBufferInfoMap.empty())
{
BufferInfoIterator it = mBufferInfoMap.begin();

if (!it->mUniformBuffer.isNull())
{
void* pMappedData = it->mUniformBuffer->lock(v1::HardwareBuffer::HBL_DISCARD);

// Only iterate through parsed variables (getting size of list)
void* src = 0;
ShaderVarWithPosInBuf* iter = &(it->mShaderVars[0]);
unsigned int lSize = it->mShaderVars.size();
for (size_t i = 0 ; i < lSize; i++, iter++)
{
const GpuConstantDefinition& def = params->getConstantDefinition(iter->name);
// Since we are mapping with write discard, contents of the buffer are undefined.
// We must set every variable, even if it has not changed.
//if (def.variability & variabilityMask)
{
if(def.isFloat())
{
src = (void *)&(*(params->getFloatConstantList().begin() + def.physicalIndex));
}
else if( def.isUnsignedInt() )
{
src = (void *)&(*(params->getUnsignedIntConstantList().begin() + def.physicalIndex));
}
else
{
src = (void *)&(*(params->getIntConstantList().begin() + def.physicalIndex));
}
UINT numBuffers = 0;

memcpy( &(((char *)(pMappedData))[iter->startOffset]), src , iter->size);
}
}

it->mUniformBuffer->unlock();

return static_cast<v1::D3D11HardwareUniformBuffer*>(it->mUniformBuffer.get())->getD3DConstantBuffer();
}
}

return NULL;
}
//-----------------------------------------------------------------------------
void D3D11HLSLProgram::getConstantBuffers(ID3D11Buffer** buffers, unsigned int& numBuffers,
ID3D11ClassInstance** classes, unsigned int& numClasses,
GpuProgramParametersSharedPtr params, uint16 variabilityMask)
{
// Update the Constant Buffers
BufferInfoIterator it = mBufferInfoMap.begin();
BufferInfoIterator end = mBufferInfoMap.end();
while (it != end)
for( size_t i=0; i<NumDefaultBufferTypes; ++i )
{
BufferInfo *it = &mDefaultBuffers[i];
if (!it->mUniformBuffer.isNull())
{
void* pMappedData = it->mUniformBuffer->lock(v1::HardwareBuffer::HBL_DISCARD);
Expand Down Expand Up @@ -2163,26 +2142,30 @@ namespace Ogre {
it->mUniformBuffer->unlock();

// Add buffer to list
buffers[numBuffers] = static_cast<v1::D3D11HardwareUniformBuffer*>(it->mUniformBuffer.get())->getD3DConstantBuffer();
buffers[numBuffers] = static_cast<v1::D3D11HardwareUniformBuffer*>(
it->mUniformBuffer.get())->getD3DConstantBuffer();
// Increment number of buffers
numBuffers++;
++numBuffers;
}
}

// Update class instances
SlotIterator sit = mSlotMap.begin();
SlotIterator send = mSlotMap.end();
while (sit != send)
{
// Get constant name
const GpuConstantDefinition& def = params->getConstantDefinition(sit->first);
outSlotStart = mDefaultBufferBindPoint;
outNumBuffers = numBuffers;

// Set to correct slot
//classes[sit->second] =
// Update class instances
// SlotIterator sit = mSlotMap.begin();
// SlotIterator send = mSlotMap.end();
// while (sit != send)
// {
// // Get constant name
// const GpuConstantDefinition& def = params->getConstantDefinition(sit->first);

// // Set to correct slot
// //classes[sit->second] =

// Increment class count
numClasses++;
}
// // Increment class count
// numClasses++;
// }
}
//-----------------------------------------------------------------------------
ID3D11VertexShader* D3D11HLSLProgram::getVertexShader(void) const
Expand Down

0 comments on commit 632c635

Please sign in to comment.