Skip to content

Commit 939688e

Browse files
author
Tim Foley
authored
Add reflection API to get type name (shader-slang#263)
This is currently only useful for `struct` types. I implemented a special-case exception so that the auto-generated `struct` types used for `cbuffer` members don't show their internal name. I did *not* implement any logic to avoid returning the name `vector` for a vector type, etc., since they are all `DeclRefType`s and it seemed easiest to just let the user access information they can't really use.
1 parent 417c9f3 commit 939688e

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

slang.h

+11
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ extern "C"
601601
SLANG_API SlangResourceAccess spReflectionType_GetResourceAccess(SlangReflectionType* type);
602602
SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangReflectionType* type);
603603

604+
SLANG_API char const* spReflectionType_GetName(SlangReflectionType* type);
605+
604606
// Type Layout Reflection
605607

606608
SLANG_API SlangReflectionType* spReflectionTypeLayout_GetType(SlangReflectionTypeLayout* type);
@@ -801,6 +803,11 @@ namespace slang
801803
{
802804
return spReflectionType_GetResourceAccess((SlangReflectionType*) this);
803805
}
806+
807+
char const* getName()
808+
{
809+
return spReflectionType_GetName((SlangReflectionType*) this);
810+
}
804811
};
805812

806813
enum ParameterCategory : SlangParameterCategory
@@ -924,6 +931,10 @@ namespace slang
924931
return getType()->getResourceAccess();
925932
}
926933

934+
char const* getName()
935+
{
936+
return getType()->getName();
937+
}
927938
};
928939

929940
struct VariableReflection

source/slang/reflection.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,27 @@ SLANG_API SlangResourceAccess spReflectionType_GetResourceAccess(SlangReflection
392392
return SLANG_RESOURCE_ACCESS_NONE;
393393
}
394394

395+
SLANG_API char const* spReflectionType_GetName(SlangReflectionType* inType)
396+
{
397+
auto type = convert(inType);
398+
399+
if( auto declRefType = type->As<DeclRefType>() )
400+
{
401+
auto declRef = declRefType->declRef;
402+
403+
// Don't return a name for auto-generated anonymous types
404+
// that represent `cbuffer` members, etc.
405+
auto decl = declRef.getDecl();
406+
if(decl->HasModifier<ImplicitParameterGroupElementTypeModifier>())
407+
return nullptr;
408+
409+
return getText(declRef.GetName()).begin();
410+
}
411+
412+
return nullptr;
413+
}
414+
415+
395416
SLANG_API SlangReflectionType* spReflectionType_GetResourceResultType(SlangReflectionType* inType)
396417
{
397418
auto type = convert(inType);

tests/reflection/std430-layout.glsl.expected

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ standard output = {
6161
"name": "e",
6262
"type": {
6363
"kind": "struct",
64+
"name": "Foo",
6465
"fields": [
6566
{
6667
"name": "f",

tests/reflection/vertex-input-semantics.hlsl.expected

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ standard output = {
3030
"semanticName": "B",
3131
"type": {
3232
"kind": "struct",
33+
"name": "B",
3334
"fields": [
3435
{
3536
"name": "b0",
@@ -48,6 +49,7 @@ standard output = {
4849
"name": "b1",
4950
"type": {
5051
"kind": "struct",
52+
"name": "X",
5153
"fields": [
5254
{
5355
"name": "x0",
@@ -91,11 +93,13 @@ standard output = {
9193
"binding": {"kind": "vertexInput", "index": 4, "count": 3},
9294
"type": {
9395
"kind": "struct",
96+
"name": "C",
9497
"fields": [
9598
{
9699
"name": "c0",
97100
"type": {
98101
"kind": "struct",
102+
"name": "X",
99103
"fields": [
100104
{
101105
"name": "x0",

tools/slang-reflection-test/main.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,17 @@ static void emitReflectionTypeLayoutInfoJSON(
460460

461461
case slang::TypeReflection::Kind::Struct:
462462
{
463+
auto structTypeLayout = typeLayout;
464+
463465
write(writer, "\"kind\": \"struct\",\n");
466+
if( auto name = structTypeLayout->getName() )
467+
{
468+
emitReflectionNameInfoJSON(writer, structTypeLayout->getName());
469+
write(writer, ",\n");
470+
}
464471
write(writer, "\"fields\": [\n");
465472
indent(writer);
466473

467-
auto structTypeLayout = typeLayout;
468474
auto fieldCount = structTypeLayout->getFieldCount();
469475
for( uint32_t ff = 0; ff < fieldCount; ++ff )
470476
{

0 commit comments

Comments
 (0)