Skip to content

Commit

Permalink
Fix thread malloc issue with GetConstantBufferByName (#3033)
Browse files Browse the repository at this point in the history
- Switches to StringRef for keys since these won't attempt to allocate string on find.
- Ensure string is owned by the CShaderReflectionConstantBuffer object.
- catch potential issues (don't leak exceptions) in PDB stream load.
  • Loading branch information
tex3d authored Jul 9, 2020
1 parent 1db4cd6 commit f78b10c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/HLSL/DxilContainerReflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,13 @@ class DxilModuleReflection {
std::vector<std::unique_ptr<CShaderReflectionConstantBuffer>> m_CBs;
std::vector<D3D12_SHADER_INPUT_BIND_DESC> m_Resources;
std::vector<std::unique_ptr<CShaderReflectionType>> m_Types;
std::map<std::string, UINT> m_CBsByName;

// Key strings owned by CShaderReflectionConstantBuffer objects
std::map<StringRef, UINT> m_CBsByName;
// Due to the possibility of overlapping names between CB and other resources,
// m_StructuredBufferCBsByName is the index into m_CBs corresponding to
// StructuredBuffer resources, separately from CB resources.
std::map<std::string, UINT> m_StructuredBufferCBsByName;
std::map<StringRef, UINT> m_StructuredBufferCBsByName;

void CreateReflectionObjects();
void CreateReflectionObjectForResource(DxilResourceBase *R);
Expand Down Expand Up @@ -287,14 +289,15 @@ HRESULT DxilContainerReflection::Load(IDxcBlob *pContainer) {
}

CComPtr<IDxcBlob> pPDBContainer;
{
try {
DxcThreadMalloc DxcMalloc(m_pMalloc);
CComPtr<IStream> pStream;
IFR(hlsl::CreateReadOnlyBlobStream(pContainer, &pStream));
if (SUCCEEDED(hlsl::pdb::LoadDataFromStream(m_pMalloc, pStream, &pPDBContainer))) {
pContainer = pPDBContainer;
}
}
CATCH_CPP_RETURN_HRESULT();

uint32_t bufLen = pContainer->GetBufferSize();
const DxilContainerHeader *pHeader =
Expand Down Expand Up @@ -1216,7 +1219,8 @@ void CShaderReflectionConstantBuffer::Initialize(
std::vector<std::unique_ptr<CShaderReflectionType>>& allTypes,
bool bUsageInMetadata) {
ZeroMemory(&m_Desc, sizeof(m_Desc));
m_Desc.Name = CB.GetGlobalName().c_str();
m_ReflectionName = CB.GetGlobalName();
m_Desc.Name = m_ReflectionName.c_str();
m_Desc.Size = CB.GetSize();
m_Desc.Size = (m_Desc.Size + 0x0f) & ~(0x0f); // Round up to 16 bytes for reflection.
m_Desc.Type = D3D_CT_CBUFFER;
Expand Down

0 comments on commit f78b10c

Please sign in to comment.