[llvm] [NFC] Refactoring MCDXBC to support out of order storage of root parameters (PR #137284)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Wed May 14 21:04:57 PDT 2025


================
@@ -274,27 +274,39 @@ void DXContainerWriter::writeParts(raw_ostream &OS) {
       RS.StaticSamplersOffset = P.RootSignature->StaticSamplersOffset;
 
       for (const auto &Param : P.RootSignature->Parameters) {
-        mcdxbc::RootParameter NewParam;
-        NewParam.Header = dxbc::RootParameterHeader{
-            Param.Type, Param.Visibility, Param.Offset};
+        auto Header = dxbc::RootParameterHeader{Param.Type, Param.Visibility,
+                                                Param.Offset};
 
         switch (Param.Type) {
         case llvm::to_underlying(dxbc::RootParameterType::Constants32Bit):
-          NewParam.Constants.Num32BitValues = Param.Constants.Num32BitValues;
-          NewParam.Constants.RegisterSpace = Param.Constants.RegisterSpace;
-          NewParam.Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          dxbc::RootConstants Constants;
+          Constants.Num32BitValues = Param.Constants.Num32BitValues;
+          Constants.RegisterSpace = Param.Constants.RegisterSpace;
+          Constants.ShaderRegister = Param.Constants.ShaderRegister;
+          RS.ParametersContainer.addParameter(Header, Constants);
           break;
         case llvm::to_underlying(dxbc::RootParameterType::SRV):
         case llvm::to_underlying(dxbc::RootParameterType::UAV):
         case llvm::to_underlying(dxbc::RootParameterType::CBV):
-          NewParam.Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
-          NewParam.Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
-          if (P.RootSignature->Version > 1)
-            NewParam.Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+          if (RS.Version == 1) {
+            dxbc::RTS0::v1::RootDescriptor Descriptor;
+            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+            RS.ParametersContainer.addParameter(Header, Descriptor);
+          } else {
+            dxbc::RTS0::v2::RootDescriptor Descriptor;
+            Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
+            Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
+            Descriptor.Flags = Param.Descriptor.getEncodedFlags();
+            RS.ParametersContainer.addParameter(Header, Descriptor);
+          }
----------------
bogner wrote:

This seems clearer than the implicit cast to from the `v1` to the `v2` descriptor:
```c++
          dxbc::RTS0::v2::RootDescriptor Descriptor;
          Descriptor.RegisterSpace = Param.Descriptor.RegisterSpace;
          Descriptor.ShaderRegister = Param.Descriptor.ShaderRegister;
          if (RS.Version > 1)
            Descriptor.Flags = Param.Descriptor.getEncodedFlags();
          RS.ParametersContainer.addParameter(Header, Descriptor);
```

aside: We should probably make the `v2::RootDescriptor` constructor that takes a `v1::RootDescriptor` explicit - this implicit cast being legal seems dangerous

https://github.com/llvm/llvm-project/pull/137284


More information about the llvm-commits mailing list