-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[DirectX] Adding support for Root Descriptors in obj2yaml/yaml2obj #137259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -158,6 +158,11 @@ enum class RootElementFlag : uint32_t { | |||
#include "DXContainerConstants.def" | ||||
}; | ||||
|
||||
#define ROOT_DESCRIPTOR_FLAG(Num, Val) Val = 1ull << Num, | ||||
enum class RootDescriptorFlag : uint32_t { | ||||
#include "DXContainerConstants.def" | ||||
}; | ||||
|
||||
#define ROOT_PARAMETER(Val, Enum) Enum = Val, | ||||
enum class RootParameterType : uint32_t { | ||||
#include "DXContainerConstants.def" | ||||
|
@@ -580,7 +585,28 @@ struct ProgramSignatureElement { | |||
|
||||
static_assert(sizeof(ProgramSignatureElement) == 32, | ||||
"ProgramSignatureElement is misaligned"); | ||||
namespace RST0 { | ||||
namespace v0 { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the v0 namespace corresponds to Version 1, and the v1 namespace is Version 2? Any reason not to use the same numbering scheme for the versions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems worthwhile to make the naming consistent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be good to get this addressed, but I'm fine with it in a different PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @damyanp @spall @inbelic @bogner I remember one detail, DXC uses the versions as 1 and 2: https://github.com/microsoft/DirectXShaderCompiler/blob/1198c30f05ed944873ca55e89970fae407e2aacc/include/dxc/DxilRootSignature/DxilRootSignature.h#L91C1-L92C1. The version is part of the binary container, and it will lead to the parsing of several fields, I am not sure if we should change that here, since I would still need to read and write 1 and 2 as the values identifying the version, I could change those, by doing something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this message, but what I was suggesting, and what I believe @damyanp was suggesting, is that in this PR you make the namespaces match the version naming convention. So v0 -> v1 and v1 -> v2. It seems you think we were suggesting you change the Version numbers to match your namespaces? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why use namespaces to distinguish these structs and not give them different names like 'RootDescriptorV1' and 'RootDescriptorV2'? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is following the same pattern established in other parts of DXContainer, such as:
My understating for this original decision is, newer versions of DXContainer are superset of previous versions, meaning, they add new fields, structs and flags, don't remove nor modify existing definition. |
||||
struct RootDescriptor { | ||||
uint32_t ShaderRegister; | ||||
uint32_t RegisterSpace; | ||||
void swapBytes() { | ||||
sys::swapByteOrder(ShaderRegister); | ||||
sys::swapByteOrder(RegisterSpace); | ||||
} | ||||
}; | ||||
} // namespace v0 | ||||
|
||||
namespace v1 { | ||||
struct RootDescriptor : public v0::RootDescriptor { | ||||
uint32_t Flags; | ||||
void swapBytes() { | ||||
v0::RootDescriptor::swapBytes(); | ||||
sys::swapByteOrder(Flags); | ||||
} | ||||
}; | ||||
} // namespace v1 | ||||
} // namespace RST0 | ||||
// following dx12 naming | ||||
// https://learn.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_root_constants | ||||
struct RootConstants { | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,16 +121,19 @@ namespace DirectX { | |
struct RootParameterView { | ||
const dxbc::RootParameterHeader &Header; | ||
StringRef ParamData; | ||
RootParameterView(const dxbc::RootParameterHeader &H, StringRef P) | ||
RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is used in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like other constructors have But regardless, it is literally unused: RootParameterView(uint32_t V, const dxbc::RootParameterHeader &H, StringRef P)
: Header(H), ParamData(P) {} RootDescriptorView does have a Version passed to its read method. But this V passed to the RootParameterView constructor is not used. |
||
: Header(H), ParamData(P) {} | ||
|
||
template <typename T> Expected<T> readParameter() { | ||
T Struct; | ||
if (sizeof(T) != ParamData.size()) | ||
template <typename T, typename VersionT = T> Expected<T> readParameter() { | ||
assert(sizeof(VersionT) <= sizeof(T) && | ||
"Parameter of higher version must inherit all previous version data " | ||
"members"); | ||
if (sizeof(VersionT) != ParamData.size()) | ||
return make_error<GenericBinaryError>( | ||
"Reading structure out of file bounds", object_error::parse_failed); | ||
|
||
memcpy(&Struct, ParamData.data(), sizeof(T)); | ||
T Struct; | ||
memcpy(&Struct, ParamData.data(), sizeof(VersionT)); | ||
Comment on lines
+135
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting pretty lost trying to read this and understand what T vs VersionT is meant to mean. But it looks like we expect VersionT to be smaller than T. Which means that this memcpy won't overwrite everything in T, which means that the rest of it is uninitialized? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking here: are you planning on addressing this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if this need addressing, the intended behaviour is to leave things uninitialized, since the fields are not being used by the current version. I am waiting on @bogner comments to see how and if this need addressing. |
||
// DXContainer is always little endian | ||
if (sys::IsBigEndianHost) | ||
Struct.swapBytes(); | ||
|
@@ -149,6 +152,24 @@ struct RootConstantView : RootParameterView { | |
} | ||
}; | ||
|
||
struct RootDescriptorView : RootParameterView { | ||
static bool classof(const RootParameterView *V) { | ||
return (V->Header.ParameterType == | ||
llvm::to_underlying(dxbc::RootParameterType::CBV) || | ||
V->Header.ParameterType == | ||
llvm::to_underlying(dxbc::RootParameterType::SRV) || | ||
V->Header.ParameterType == | ||
llvm::to_underlying(dxbc::RootParameterType::UAV)); | ||
} | ||
|
||
llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) { | ||
if (Version == 1) | ||
joaosaffran marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return readParameter<dxbc::RST0::v1::RootDescriptor, | ||
dxbc::RST0::v0::RootDescriptor>(); | ||
return readParameter<dxbc::RST0::v1::RootDescriptor>(); | ||
} | ||
}; | ||
|
||
static Error parseFailed(const Twine &Msg) { | ||
return make_error<GenericBinaryError>(Msg.str(), object_error::parse_failed); | ||
} | ||
|
@@ -192,6 +213,14 @@ class RootSignature { | |
case dxbc::RootParameterType::Constants32Bit: | ||
DataSize = sizeof(dxbc::RootConstants); | ||
break; | ||
case dxbc::RootParameterType::CBV: | ||
case dxbc::RootParameterType::SRV: | ||
case dxbc::RootParameterType::UAV: | ||
if (Version == 1) | ||
DataSize = sizeof(dxbc::RST0::v0::RootDescriptor); | ||
else | ||
DataSize = sizeof(dxbc::RST0::v1::RootDescriptor); | ||
break; | ||
} | ||
size_t EndOfSectionByte = getNumStaticSamplers() == 0 | ||
? PartData.size() | ||
|
@@ -201,7 +230,7 @@ class RootSignature { | |
return parseFailed("Reading structure out of file bounds"); | ||
|
||
StringRef Buff = PartData.substr(Header.ParameterOffset, DataSize); | ||
RootParameterView View = RootParameterView(Header, Buff); | ||
RootParameterView View = RootParameterView(Version, Header, Buff); | ||
return View; | ||
} | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think we can either remove this name space, or, we should have all the defined structs within it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but I will do it in a follow-up PR. I want to avoid noise for reviewers