Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 6edfcb1

Browse files
committed
[Reflection] Fetch more information for Dxil shader reflection
1 parent 1832c18 commit 6edfcb1

File tree

4 files changed

+2598
-258
lines changed

4 files changed

+2598
-258
lines changed

Include/ShaderConductor/ShaderConductor.hpp

Lines changed: 266 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,240 @@ namespace ShaderConductor
121121
BlobImpl* m_impl = nullptr;
122122
};
123123

124+
class SC_API Reflection
125+
{
126+
public:
127+
class ReflectionImpl;
128+
struct VariableDesc;
129+
130+
class SC_API ConstantBuffer
131+
{
132+
friend class ReflectionImpl;
133+
134+
public:
135+
ConstantBuffer() noexcept;
136+
ConstantBuffer(const ConstantBuffer& other);
137+
ConstantBuffer(ConstantBuffer&& other) noexcept;
138+
~ConstantBuffer() noexcept;
139+
140+
ConstantBuffer& operator=(const ConstantBuffer& other);
141+
ConstantBuffer& operator=(ConstantBuffer&& other) noexcept;
142+
143+
bool Valid() const noexcept;
144+
145+
const char* Name() const noexcept;
146+
uint32_t Size() const noexcept;
147+
148+
uint32_t NumVariables() const noexcept;
149+
const VariableDesc* VariableByIndex(uint32_t index) const noexcept;
150+
const VariableDesc* VariableByName(const char* name) const noexcept;
151+
152+
private:
153+
class ConstantBufferImpl;
154+
ConstantBufferImpl* m_impl = nullptr;
155+
};
156+
157+
class SC_API VariableType
158+
{
159+
friend class ConstantBuffer;
160+
161+
public:
162+
enum class DataType
163+
{
164+
Void,
165+
166+
Bool,
167+
Int,
168+
Uint,
169+
Float,
170+
171+
Half,
172+
Int16,
173+
Uint16,
174+
175+
Struct,
176+
};
177+
178+
public:
179+
VariableType() noexcept;
180+
VariableType(const VariableType& other);
181+
VariableType(VariableType&& other) noexcept;
182+
~VariableType() noexcept;
183+
184+
VariableType& operator=(const VariableType& other);
185+
VariableType& operator=(VariableType&& other) noexcept;
186+
187+
bool Valid() const noexcept;
188+
189+
const char* Name() const noexcept;
190+
DataType Type() const noexcept;
191+
uint32_t Rows() const noexcept; // Number of rows (for matrices, 1 for other numeric, 0 if not applicable)
192+
uint32_t Columns() const noexcept; // Number of columns (for vectors & matrices, 1 for other numeric, 0 if not applicable)
193+
uint32_t Elements() const noexcept; // Number of elements (0 if not an array)
194+
uint32_t ElementStride() const noexcept;
195+
196+
uint32_t NumMembers() const noexcept;
197+
const VariableDesc* MemberByIndex(uint32_t index) const noexcept;
198+
const VariableDesc* MemberByName(const char* name) const noexcept;
199+
200+
private:
201+
class VariableTypeImpl;
202+
VariableTypeImpl* m_impl = nullptr;
203+
};
204+
205+
struct ResourceDesc
206+
{
207+
char name[256]; // Name of the resource
208+
ShaderResourceType type; // Type of resource (e.g. texture, cbuffer, etc.)
209+
uint32_t space;
210+
uint32_t bindPoint; // Starting bind point
211+
uint32_t bindCount; // Number of contiguous bind points (for arrays)
212+
};
213+
214+
struct VariableDesc
215+
{
216+
char name[256];
217+
VariableType type;
218+
uint32_t offset; // Offset in cbuffer or stuct
219+
uint32_t size; // Size of the variable
220+
};
221+
222+
enum class ComponentMask : uint8_t
223+
{
224+
X = 0x1U,
225+
Y = 0x2U,
226+
Z = 0x4U,
227+
W = 0x8U,
228+
};
229+
230+
struct SignatureParameterDesc
231+
{
232+
char semantic[256];
233+
uint32_t semanticIndex;
234+
uint32_t location;
235+
VariableType::DataType componentType;
236+
ComponentMask mask;
237+
};
238+
239+
enum class PrimitiveTopology
240+
{
241+
Undefined,
242+
Points,
243+
Lines,
244+
LineStrip,
245+
Triangles,
246+
TriangleStrip,
247+
248+
LinesAdj,
249+
LineStripAdj,
250+
TrianglesAdj,
251+
TriangleStripAdj,
252+
253+
Patches_1_CtrlPoint,
254+
Patches_2_CtrlPoint,
255+
Patches_3_CtrlPoint,
256+
Patches_4_CtrlPoint,
257+
Patches_5_CtrlPoint,
258+
Patches_6_CtrlPoint,
259+
Patches_7_CtrlPoint,
260+
Patches_8_CtrlPoint,
261+
Patches_9_CtrlPoint,
262+
Patches_10_CtrlPoint,
263+
Patches_11_CtrlPoint,
264+
Patches_12_CtrlPoint,
265+
Patches_13_CtrlPoint,
266+
Patches_14_CtrlPoint,
267+
Patches_15_CtrlPoint,
268+
Patches_16_CtrlPoint,
269+
Patches_17_CtrlPoint,
270+
Patches_18_CtrlPoint,
271+
Patches_19_CtrlPoint,
272+
Patches_20_CtrlPoint,
273+
Patches_21_CtrlPoint,
274+
Patches_22_CtrlPoint,
275+
Patches_23_CtrlPoint,
276+
Patches_24_CtrlPoint,
277+
Patches_25_CtrlPoint,
278+
Patches_26_CtrlPoint,
279+
Patches_27_CtrlPoint,
280+
Patches_28_CtrlPoint,
281+
Patches_29_CtrlPoint,
282+
Patches_30_CtrlPoint,
283+
Patches_31_CtrlPoint,
284+
Patches_32_CtrlPoint,
285+
};
286+
287+
enum class TessellatorOutputPrimitive
288+
{
289+
Undefined,
290+
Point,
291+
Line,
292+
TriangleCW,
293+
TriangleCCW,
294+
};
295+
296+
enum class TessellatorPartitioning
297+
{
298+
Undefined,
299+
Integer,
300+
Pow2,
301+
FractionalOdd,
302+
FractionalEven,
303+
};
304+
305+
enum class TessellatorDomain
306+
{
307+
Undefined,
308+
Line,
309+
Triangle,
310+
Quad,
311+
};
312+
313+
public:
314+
Reflection() noexcept;
315+
Reflection(const Reflection& other);
316+
Reflection(Reflection&& other) noexcept;
317+
~Reflection() noexcept;
318+
319+
Reflection& operator=(const Reflection& other);
320+
Reflection& operator=(Reflection&& other) noexcept;
321+
322+
bool Valid() const noexcept;
323+
324+
uint32_t NumResources() const noexcept;
325+
const ResourceDesc* ResourceByIndex(uint32_t index) const noexcept;
326+
const ResourceDesc* ResourceByName(const char* name) const noexcept;
327+
328+
uint32_t NumConstantBuffers() const noexcept;
329+
const ConstantBuffer* ConstantBufferByIndex(uint32_t index) const noexcept;
330+
const ConstantBuffer* ConstantBufferByName(const char* name) const noexcept;
331+
332+
uint32_t NumInputParameters() const noexcept;
333+
const SignatureParameterDesc* InputParameter(uint32_t index) const noexcept;
334+
uint32_t NumOutputParameters() const noexcept;
335+
const SignatureParameterDesc* OutputParameter(uint32_t index) const noexcept;
336+
337+
PrimitiveTopology GSHSInputPrimitive() const noexcept;
338+
PrimitiveTopology GSOutputTopology() const noexcept;
339+
uint32_t GSMaxNumOutputVertices() const noexcept;
340+
uint32_t GSNumInstances() const noexcept;
341+
342+
TessellatorOutputPrimitive HSOutputPrimitive() const noexcept;
343+
TessellatorPartitioning HSPartitioning() const noexcept;
344+
345+
TessellatorDomain HSDSTessellatorDomain() const noexcept;
346+
uint32_t HSDSNumPatchConstantParameters() const noexcept;
347+
const SignatureParameterDesc* HSDSPatchConstantParameter(uint32_t index) const noexcept;
348+
uint32_t HSDSNumConrolPoints() const noexcept;
349+
350+
uint32_t CSBlockSizeX() const noexcept;
351+
uint32_t CSBlockSizeY() const noexcept;
352+
uint32_t CSBlockSizeZ() const noexcept;
353+
354+
private:
355+
ReflectionImpl* m_impl = nullptr;
356+
};
357+
124358
class SC_API Compiler
125359
{
126360
public:
@@ -182,6 +416,8 @@ namespace ShaderConductor
182416
int shiftAllSamplersBindings = 0;
183417
int shiftAllCBuffersBindings = 0;
184418
int shiftAllUABuffersBindings = 0;
419+
420+
bool needReflection = false;
185421
};
186422

187423
struct TargetDesc
@@ -191,22 +427,6 @@ namespace ShaderConductor
191427
bool asModule;
192428
};
193429

194-
struct ReflectionDesc
195-
{
196-
char name[256]; // Name of the resource
197-
ShaderResourceType type; // Type of resource (e.g. texture, cbuffer, etc.)
198-
uint32_t bufferBindPoint; // Buffer's starting bind point
199-
uint32_t bindPoint; // Starting bind point
200-
uint32_t bindCount; // Number of contiguous bind points (for arrays)
201-
};
202-
203-
struct ReflectionResultDesc
204-
{
205-
Blob descs; // The underneath type is ReflectionDesc
206-
uint32_t descCount = 0;
207-
uint32_t instructionCount = 0;
208-
};
209-
210430
struct ResultDesc
211431
{
212432
Blob target;
@@ -215,7 +435,7 @@ namespace ShaderConductor
215435
Blob errorWarningMsg;
216436
bool hasError;
217437

218-
ReflectionResultDesc reflection;
438+
Reflection reflection;
219439
};
220440

221441
struct DisassembleDesc
@@ -250,6 +470,35 @@ namespace ShaderConductor
250470
static bool LinkSupport();
251471
static ResultDesc Link(const LinkDesc& modules, const Options& options, const TargetDesc& target);
252472
};
473+
474+
inline Reflection::ComponentMask& operator|=(Reflection::ComponentMask& lhs, Reflection::ComponentMask rhs)
475+
{
476+
lhs = static_cast<Reflection::ComponentMask>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
477+
return lhs;
478+
}
479+
inline constexpr Reflection::ComponentMask operator|(Reflection::ComponentMask lhs, Reflection::ComponentMask rhs)
480+
{
481+
return static_cast<Reflection::ComponentMask>(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
482+
}
483+
484+
inline Reflection::ComponentMask& operator&=(Reflection::ComponentMask& lhs, Reflection::ComponentMask rhs)
485+
{
486+
lhs = static_cast<Reflection::ComponentMask>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
487+
return lhs;
488+
}
489+
inline constexpr Reflection::ComponentMask operator&(Reflection::ComponentMask lhs, Reflection::ComponentMask rhs)
490+
{
491+
return static_cast<Reflection::ComponentMask>(static_cast<uint8_t>(lhs) & static_cast<uint8_t>(rhs));
492+
}
493+
494+
inline bool HasAllFlags(Reflection::ComponentMask flags, Reflection::ComponentMask contains)
495+
{
496+
return (static_cast<uint8_t>(flags) & static_cast<uint8_t>(contains)) == static_cast<uint8_t>(contains);
497+
}
498+
inline bool HasAnyFlags(Reflection::ComponentMask flags, Reflection::ComponentMask contains)
499+
{
500+
return (static_cast<uint8_t>(flags) & static_cast<uint8_t>(contains)) != 0;
501+
}
253502
} // namespace ShaderConductor
254503

255504
#endif // SHADER_CONDUCTOR_HPP

0 commit comments

Comments
 (0)