Skip to content

Commit b35e4fd

Browse files
DXBCUtils::RemapResourceBindings: return byte code blob instead of patching it in place
1 parent e415791 commit b35e4fd

File tree

5 files changed

+37
-31
lines changed

5 files changed

+37
-31
lines changed

Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ void PipelineStateD3D11Impl::RemapOrVerifyShaderResources(const TShaderStages&
141141

142142
if (HandleRemappedBytecodeFn)
143143
{
144-
RefCntAutoPtr<IDataBlob> pPatchedBytecode = DataBlobImpl::MakeCopy(pBytecode);
145-
if (!DXBCUtils::RemapResourceBindings(ResourceMap, pPatchedBytecode->GetDataPtr(), pPatchedBytecode->GetSize()))
144+
RefCntAutoPtr<IDataBlob> pPatchedBytecode = DXBCUtils::RemapResourceBindings(ResourceMap, pBytecode->GetConstDataPtr(), pBytecode->GetSize());
145+
if (!pPatchedBytecode)
146146
LOG_ERROR_AND_THROW("Failed to remap resource bindings in shader '", pShader->GetDesc().Name, "'.");
147147

148148
HandleRemappedBytecodeFn(s, pShader, pPatchedBytecode);

Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ void PipelineStateD3D12Impl::RemapOrVerifyShaderResources(TShaderStages&
491491
}
492492
else
493493
{
494-
pBytecode = DataBlobImpl::MakeCopy(pBytecode);
495-
if (!DXBCUtils::RemapResourceBindings(ResourceMap, pBytecode->GetDataPtr(), pBytecode->GetSize()))
494+
pBytecode = DXBCUtils::RemapResourceBindings(ResourceMap, pBytecode->GetDataPtr(), pBytecode->GetSize());
495+
if (!pBytecode)
496496
LOG_ERROR_AND_THROW("Failed to remap resource bindings in shader '", pShader->GetDesc().Name, "'.");
497497
}
498498
}

Graphics/ShaderTools/include/DXBCUtils.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -30,6 +30,8 @@
3030
#include "Constants.h"
3131
#include "Shader.h"
3232
#include "ResourceBindingMap.hpp"
33+
#include "DataBlob.h"
34+
#include "RefCntAutoPtr.hpp"
3335

3436
namespace Diligent
3537
{
@@ -46,9 +48,9 @@ using TResourceBindingMap = ResourceBinding::TMap;
4648
/// \param [in] ResourceMap - Resource binding map. For every resource in the
4749
/// byte code it must define the binding (shader register).
4850
/// \param [inout] pBytecode - Byte code that will be patched.
49-
bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
50-
void* pBytecode,
51-
size_t Size);
51+
RefCntAutoPtr<IDataBlob> RemapResourceBindings(const TResourceBindingMap& ResourceMap,
52+
const void* pBytecode,
53+
size_t Size);
5254
}; // namespace DXBCUtils
5355

5456
} // namespace Diligent

Graphics/ShaderTools/src/DXBCUtils.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "DXBCUtils.hpp"
3838
#include "../../../ThirdParty/GPUOpenShaderUtils/DXBCChecksum.h"
39+
#include "DataBlobImpl.hpp"
3940

4041
namespace Diligent
4142
{
@@ -1700,37 +1701,34 @@ void ShaderBytecodeRemapper::PatchBytecode(Uint32* Token, const void* EndPtr) no
17001701
namespace DXBCUtils
17011702
{
17021703

1703-
bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
1704-
void* pBytecode,
1705-
size_t Size)
1704+
RefCntAutoPtr<IDataBlob> RemapResourceBindings(const TResourceBindingMap& ResourceMap,
1705+
const void* pBytecode,
1706+
size_t Size)
17061707
{
17071708
if (pBytecode == nullptr)
17081709
{
17091710
LOG_ERROR_MESSAGE("pBytecode must not be null.");
1710-
return false;
1711+
return {};
17111712
}
17121713

1713-
char* const Ptr = static_cast<char*>(pBytecode);
1714-
const void* const EndPtr = Ptr + Size;
1715-
17161714
if (Size < sizeof(DXBCHeader))
17171715
{
17181716
LOG_ERROR_MESSAGE("The size of the byte code (", Size, ") is too small to contain the DXBC header. The byte code may be corrupted.");
1719-
return false;
1717+
return {};
17201718
}
17211719

1722-
DXBCHeader& Header = *reinterpret_cast<DXBCHeader*>(Ptr);
1720+
const DXBCHeader& Header = *static_cast<const DXBCHeader*>(pBytecode);
17231721
if (Header.TotalSize != Size)
17241722
{
17251723
LOG_ERROR_MESSAGE("The byte code size (", Header.TotalSize, ") specified in the header does not match the actual size (", Size,
17261724
"). The byte code may be corrupted.");
1727-
return false;
1725+
return {};
17281726
}
17291727

17301728
#ifdef DILIGENT_DEVELOPMENT
17311729
{
17321730
DWORD Checksum[4] = {};
1733-
CalculateDXBCChecksum(reinterpret_cast<BYTE*>(Ptr), static_cast<DWORD>(Size), Checksum);
1731+
CalculateDXBCChecksum(static_cast<const BYTE*>(pBytecode), static_cast<DWORD>(Size), Checksum);
17341732

17351733
DEV_CHECK_ERR((Checksum[0] == Header.Checksum[0] &&
17361734
Checksum[1] == Header.Checksum[1] &&
@@ -1743,9 +1741,14 @@ bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
17431741
if (Header.Magic != DXBCFourCC)
17441742
{
17451743
LOG_ERROR_MESSAGE("Bytecode header does not contain the 'DXBC' magic number. The byte code may be corrupted.");
1746-
return false;
1744+
return {};
17471745
}
17481746

1747+
RefCntAutoPtr<IDataBlob> pRemappedBytecode = DataBlobImpl::Create(Size, pBytecode);
1748+
1749+
char* const Ptr = pRemappedBytecode->GetDataPtr<char>();
1750+
const void* const EndPtr = Ptr + Size;
1751+
17491752
const Uint32* Chunks = reinterpret_cast<Uint32*>(Ptr + sizeof(Header));
17501753
ResourceBindingPerType BindingsPerType;
17511754
TExtendedResourceMap ExtResourceMap;
@@ -1760,12 +1763,12 @@ bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
17601763
if (pChunk + 1 > EndPtr)
17611764
{
17621765
LOG_ERROR_MESSAGE("Not enough space for the chunk header. The byte code may be corrupted.");
1763-
return false;
1766+
return {};
17641767
}
17651768
if ((Ptr + Chunks[i] + pChunk->Length) > EndPtr)
17661769
{
17671770
LOG_ERROR_MESSAGE("Not enough space for the chunk data. The byte code may be corrupted.");
1768-
return false;
1771+
return {};
17691772
}
17701773

17711774
if (pChunk->Magic == RDEFFourCC)
@@ -1801,29 +1804,29 @@ bool RemapResourceBindings(const TResourceBindingMap& ResourceMap,
18011804
}
18021805
catch (...)
18031806
{
1804-
return false;
1807+
return {};
18051808
}
18061809

18071810
if (!RemapResDef)
18081811
{
18091812
LOG_ERROR_MESSAGE("Failed to find 'RDEF' chunk with the resource definition.");
1810-
return false;
1813+
return {};
18111814
}
18121815

18131816
if (!RemapBytecode)
18141817
{
18151818
LOG_ERROR_MESSAGE("Failed to find 'SHDR' or 'SHEX' chunk with the shader bytecode.");
1816-
return false;
1819+
return {};
18171820
}
18181821

18191822
// update checksum
18201823
DWORD Checksum[4] = {};
18211824
CalculateDXBCChecksum(reinterpret_cast<BYTE*>(Ptr), static_cast<DWORD>(Size), Checksum);
18221825

18231826
static_assert(sizeof(Header.Checksum) == sizeof(Checksum), "Unexpected checksum size");
1824-
memcpy(Header.Checksum, Checksum, sizeof(Header.Checksum));
1827+
memcpy(pRemappedBytecode->GetDataPtr<DXBCHeader>()->Checksum, Checksum, sizeof(Header.Checksum));
18251828

1826-
return true;
1829+
return pRemappedBytecode;
18271830
}
18281831

18291832
} // namespace DXBCUtils

Tests/DiligentCoreAPITest/src/DXBCUtilsTest.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 Diligent Graphics LLC
2+
* Copyright 2019-2025 Diligent Graphics LLC
33
* Copyright 2015-2019 Egor Yusov
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -49,19 +49,20 @@ void TestDXBCRemapping(const char* Source, const char* Entry, const char* Profil
4949
CComPtr<ID3DBlob> Blob;
5050
CComPtr<ID3DBlob> CompilerOutput;
5151

52-
auto hr = D3DCompile(Source, strlen(Source), nullptr, nullptr, nullptr, Entry, Profile, D3DCOMPILE_ENABLE_STRICTNESS, 0, &Blob, &CompilerOutput);
52+
HRESULT hr = D3DCompile(Source, strlen(Source), nullptr, nullptr, nullptr, Entry, Profile, D3DCOMPILE_ENABLE_STRICTNESS, 0, &Blob, &CompilerOutput);
5353
if (FAILED(hr))
5454
{
5555
const char* Msg = CompilerOutput ? static_cast<char*>(CompilerOutput->GetBufferPointer()) : "";
5656
LOG_ERROR_MESSAGE("D3DCompile failed: ", Msg);
5757
}
5858
ASSERT_HRESULT_SUCCEEDED(hr);
5959

60-
ASSERT_TRUE(DXBCUtils::RemapResourceBindings(ResMap, Blob->GetBufferPointer(), Blob->GetBufferSize()));
60+
RefCntAutoPtr<IDataBlob> RemappedByteCode = DXBCUtils::RemapResourceBindings(ResMap, Blob->GetBufferPointer(), Blob->GetBufferSize());
61+
ASSERT_TRUE(RemappedByteCode);
6162

6263
CComPtr<ID3D12ShaderReflection> ShaderReflection;
6364

64-
hr = D3DReflect(Blob->GetBufferPointer(), Blob->GetBufferSize(), __uuidof(ShaderReflection), reinterpret_cast<void**>(&ShaderReflection));
65+
hr = D3DReflect(RemappedByteCode->GetConstDataPtr(), RemappedByteCode->GetSize(), __uuidof(ShaderReflection), reinterpret_cast<void**>(&ShaderReflection));
6566
ASSERT_HRESULT_SUCCEEDED(hr);
6667

6768
D3D12_SHADER_DESC ShaderDesc = {};

0 commit comments

Comments
 (0)