-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Changes from 7 commits
0abacfc
8b8c02a
7ac9641
c105458
efe76aa
a928e9d
15eb6f5
1b3e10a
1f31957
e8fbfce
a31e5a5
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. Ohh got it, yeah, misunderstood that. Changing the namespaces (v0 -> v1 and v1 -> v2), would make that inconsistent with the other namespaces naming conventions in this file. So I think the best approach would be changing the naming conventions for all namespaces in this file. 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 disagree that changing this name would make this inconsistent. The other uint32_t getVersion() const {
return Size >= sizeof(dxbc::PSV::v3::RuntimeInfo)
? 3
: (Size >= sizeof(dxbc::PSV::v2::RuntimeInfo) ? 2
: (Size >= sizeof(dxbc::PSV::v1::RuntimeInfo)) ? 1
: 0);
} Using namespaces called 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. 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 don't love the pattern, but it is already established in the PSV structs so I think it makes sense to follow that here. |
||||
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 |
---|---|---|
|
@@ -72,9 +72,24 @@ ROOT_ELEMENT_FLAG(11, SamplerHeapDirectlyIndexed) | |
#undef ROOT_ELEMENT_FLAG | ||
#endif // ROOT_ELEMENT_FLAG | ||
|
||
|
||
// ROOT_DESCRIPTOR_FLAG(bit offset for the flag, name). | ||
#ifdef ROOT_DESCRIPTOR_FLAG | ||
|
||
ROOT_DESCRIPTOR_FLAG(0, NONE) | ||
ROOT_DESCRIPTOR_FLAG(1, DATA_VOLATILE) | ||
ROOT_DESCRIPTOR_FLAG(2, DATA_STATIC_WHILE_SET_AT_EXECUTE) | ||
ROOT_DESCRIPTOR_FLAG(3, DATA_STATIC) | ||
Comment on lines
+79
to
+82
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. Hm, these values are wrong. We shouldn't have edit: apparently these actually should be 2, 4, and 8. Nonetheless, NONE with a value of 1 seems incorrect. |
||
#undef ROOT_DESCRIPTOR_FLAG | ||
#endif // ROOT_DESCRIPTOR_FLAG | ||
|
||
|
||
#ifdef ROOT_PARAMETER | ||
|
||
ROOT_PARAMETER(1, Constants32Bit) | ||
ROOT_PARAMETER(2, CBV) | ||
ROOT_PARAMETER(3, SRV) | ||
ROOT_PARAMETER(4, UAV) | ||
#undef ROOT_PARAMETER | ||
#endif // ROOT_PARAMETER | ||
|
||
|
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. 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.
|
||
: 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)); | ||
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. 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 do think with the NFC changes in the refactor pr and now the way the structs will be used (using variants of the versioned structs), it makes less sense to do this version of using memcpy 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. memcpy-ing part of a struct is really questionable, I think we should avoid doing this this way. We can do this fairly equivalently but in a more type safe way if we define llvm::Expected<dxbc::RST0::v1::RootDescriptor> read(uint32_t Version) {
if (Version == 1) {
auto Descriptor = readParameter<dxbc::RST0::v0::RootDescriptor>();
if (Error E = Descriptor.takeError())
return E;
return dxbc::RST0::v1::RootDescriptor(*Descriptor);
}
assert(Version == 2); // or maybe error handling? I'm not sure who calls this.
return readParameter<dxbc::RST0::v1::RootDescriptor>();
} then we just need to add a constructor for the v1 (ie, version 2) root descriptor that takes the v0 (ie, version 1) struct as an argument: namespace v1 {
struct RootDescriptor : public v0::RootDescriptor {
RootDescriptor() = default;
RootDescriptor(v0::RootDescriptor &Base)
: v0::RootDescriptor(Base), Flags(0u) {}
// ... |
||
// 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
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.
We should at least spell it correctly (
RTS0
)