Skip to content

Commit 184dc5c

Browse files
author
Tim Foley
authored
Merge from v0.9.15 (shader-slang#460)
* Fix bug when subscripting a type that must be split (shader-slang#396) The logic was creating a `PairPseudoExpr` as part of a subscript (`operator[]`) operation, but neglecting to fill in its `pairInfo` field, which led to a null-pointer crash further along. * Allow writes to UAV textures (shader-slang#416) Work on shader-slang#415 This issue is already fixed in the `v0.10.*` line, but I'm back-porting the fix to `v0.9.*`. The issue here was that the stdlib declarations for texture types were only including the `get` accessor for subscript operations, even if the texture was write-able. I've also included the fixes for other subscript accessors in the stdlib (notably that `OutputPatch<T>` is readable, but not writable, despite what the name seems to imply). * Fix infinite loop in semantic parsing (shader-slang#424) The code for parsing semantics was looking for a fixed set of tokens to terminate a semantic list, rather than assuming that whenever you don't see a `:` ahead, you probably are done with semantics. This meant that you could get into an infinite loop just with simple mistakes like leaving out a `;`. This change fixes the parser to note infinite loop in this case, and adds a test case to verify the fix. * Expose HLSL `shared` modifier through reflection. (shader-slang#436) This is a request from Falcor, because the `shared` modifier can be used as a hint to optimize the grouping of parameters for binding. The intention is that `shared` marks shader parameters (including parameter blocks) that will us the same values across many draw calls (e.g., per-frame data, as opposed to per-model or per-instance). The mechanism I'm using here is to provide a general reflection API for exposing the `Modifier`s already attached to declarations. While the only modifier exposed is `shared`, and the only modifier information being exposed is presence/absence, this interface could be extended down the line.
1 parent 17b66ef commit 184dc5c

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

slang.h

+27
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,7 @@ extern "C"
497497

498498
typedef struct SlangReflection SlangReflection;
499499
typedef struct SlangReflectionEntryPoint SlangReflectionEntryPoint;
500+
typedef struct SlangReflectionModifier SlangReflectionModifier;
500501
typedef struct SlangReflectionType SlangReflectionType;
501502
typedef struct SlangReflectionTypeLayout SlangReflectionTypeLayout;
502503
typedef struct SlangReflectionVariable SlangReflectionVariable;
@@ -643,6 +644,12 @@ extern "C"
643644
SLANG_LAYOUT_RULES_DEFAULT,
644645
};
645646

647+
typedef SlangUInt32 SlangModifierID;
648+
enum
649+
{
650+
SLANG_MODIFIER_SHARED,
651+
};
652+
646653
// Type Reflection
647654

648655
SLANG_API SlangTypeKind spReflectionType_GetKind(SlangReflectionType* type);
@@ -683,6 +690,8 @@ extern "C"
683690
SLANG_API char const* spReflectionVariable_GetName(SlangReflectionVariable* var);
684691
SLANG_API SlangReflectionType* spReflectionVariable_GetType(SlangReflectionVariable* var);
685692

693+
SLANG_API SlangReflectionModifier* spReflectionVariable_FindModifier(SlangReflectionVariable* var, SlangModifierID modifierID);
694+
686695
// Variable Layout Reflection
687696

688697
SLANG_API SlangReflectionVariable* spReflectionVariableLayout_GetVariable(SlangReflectionVariableLayout* var);
@@ -1034,6 +1043,14 @@ namespace slang
10341043
}
10351044
};
10361045

1046+
struct Modifier
1047+
{
1048+
enum ID : SlangModifierID
1049+
{
1050+
Shared = SLANG_MODIFIER_SHARED,
1051+
};
1052+
};
1053+
10371054
struct VariableReflection
10381055
{
10391056
char const* getName()
@@ -1045,6 +1062,11 @@ namespace slang
10451062
{
10461063
return (TypeReflection*) spReflectionVariable_GetType((SlangReflectionVariable*) this);
10471064
}
1065+
1066+
Modifier* findModifier(Modifier::ID id)
1067+
{
1068+
return (Modifier*) spReflectionVariable_FindModifier((SlangReflectionVariable*) this, (SlangModifierID) id);
1069+
}
10481070
};
10491071

10501072
struct VariableLayoutReflection
@@ -1059,6 +1081,11 @@ namespace slang
10591081
return getVariable()->getName();
10601082
}
10611083

1084+
Modifier* findModifier(Modifier::ID id)
1085+
{
1086+
return getVariable()->findModifier(id);
1087+
}
1088+
10621089
TypeLayoutReflection* getTypeLayout()
10631090
{
10641091
return (TypeLayoutReflection*) spReflectionVariableLayout_GetTypeLayout((SlangReflectionVariableLayout*) this);

source/slang/reflection.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,26 @@ SLANG_API SlangReflectionType* spReflectionVariable_GetType(SlangReflectionVaria
681681
return convert(var->getType());
682682
}
683683

684+
SLANG_API SlangReflectionModifier* spReflectionVariable_FindModifier(SlangReflectionVariable* inVar, SlangModifierID modifierID)
685+
{
686+
auto var = convert(inVar);
687+
if(!var) return nullptr;
688+
689+
Modifier* modifier = nullptr;
690+
switch( modifierID )
691+
{
692+
case SLANG_MODIFIER_SHARED:
693+
modifier = var->FindModifier<HLSLEffectSharedModifier>();
694+
break;
695+
696+
default:
697+
return nullptr;
698+
}
699+
700+
return (SlangReflectionModifier*) modifier;
701+
}
702+
703+
684704
// Variable Layout Reflection
685705

686706
SLANG_API SlangReflectionVariable* spReflectionVariableLayout_GetVariable(SlangReflectionVariableLayout* inVarLayout)

tests/reflection/shared-modifier.hlsl

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// shared-modifier.hlsl
2+
//TEST:REFLECTION:-profile ps_5_0 -target hlsl
3+
4+
// Confirm that we expose the `shared` modifier in reflection data.
5+
6+
Texture2D t;
7+
shared SamplerState s;
8+
9+
float4 main(float2 uv : UV) : SV_Target
10+
{
11+
return t.Sample(s, uv);
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
result code = 0
2+
standard error = {
3+
}
4+
standard output = {
5+
{
6+
"parameters": [
7+
{
8+
"name": "t",
9+
"binding": {"kind": "shaderResource", "index": 0},
10+
"type": {
11+
"kind": "resource",
12+
"baseShape": "texture2D"
13+
}
14+
},
15+
{
16+
"name": "s",
17+
"shared": true,
18+
"binding": {"kind": "samplerState", "index": 0},
19+
"type": {
20+
"kind": "samplerState"
21+
}
22+
}
23+
],
24+
"entryPoints": [
25+
{
26+
"name": "main",
27+
"stage:": "fragment",
28+
"parameters": [
29+
{
30+
"name": "uv",
31+
"stage": "fragment",
32+
"binding": {"kind": "varyingInput", "index": 0},
33+
"semanticName": "UV",
34+
"type": {
35+
"kind": "vector",
36+
"elementCount": 2,
37+
"elementType": {
38+
"kind": "scalar",
39+
"scalarType": "float32"
40+
}
41+
}
42+
}
43+
]
44+
}
45+
]
46+
}
47+
}

tools/slang-reflection-test/main.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ static void emitReflectionNameInfoJSON(
239239
write(writer, "\"");
240240
}
241241

242+
static void emitReflectionModifierInfoJSON(
243+
PrettyWriter& writer,
244+
slang::VariableReflection* var)
245+
{
246+
if( var->findModifier(slang::Modifier::Shared) )
247+
{
248+
write(writer, ",\n\"shared\": true");
249+
}
250+
}
251+
242252
static void emitReflectionVarLayoutJSON(
243253
PrettyWriter& writer,
244254
slang::VariableLayoutReflection* var)
@@ -252,6 +262,8 @@ static void emitReflectionVarLayoutJSON(
252262
write(writer, "\"type\": ");
253263
emitReflectionTypeLayoutJSON(writer, var->getTypeLayout());
254264

265+
emitReflectionModifierInfoJSON(writer, var->getVariable());
266+
255267
emitReflectionVarBindingInfoJSON(writer, var);
256268

257269
dedent(writer);
@@ -607,8 +619,10 @@ static void emitReflectionVarInfoJSON(
607619
slang::VariableReflection* var)
608620
{
609621
emitReflectionNameInfoJSON(writer, var->getName());
610-
write(writer, ",\n");
611622

623+
emitReflectionModifierInfoJSON(writer, var);
624+
625+
write(writer, ",\n");
612626
write(writer, "\"type\": ");
613627
emitReflectionTypeJSON(writer, var->getType());
614628
}
@@ -622,6 +636,8 @@ static void emitReflectionParamJSON(
622636

623637
emitReflectionNameInfoJSON(writer, param->getName());
624638

639+
emitReflectionModifierInfoJSON(writer, param->getVariable());
640+
625641
emitReflectionVarBindingInfoJSON(writer, param);
626642
write(writer, ",\n");
627643

0 commit comments

Comments
 (0)