Skip to content

Commit 71e90a7

Browse files
Fix reflection for pointer element types. (#5797)
* Fix reflection for pointer element types. * Fix. --------- Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 525412c commit 71e90a7

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

source/slang/slang-type-layout.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -4763,7 +4763,16 @@ static TypeLayoutResult _createTypeLayout(TypeLayoutContext& context, Type* type
47634763

47644764
ptrLayout->addResourceUsage(info.kind, info.size);
47654765

4766-
const auto valueTypeLayout = _createTypeLayout(context, ptrType->getValueType());
4766+
TypeLayoutResult valueTypeLayout;
4767+
if (context.rules != &kScalarLayoutRulesImpl_)
4768+
{
4769+
auto subContext = context.with(&kScalarLayoutRulesImpl_);
4770+
valueTypeLayout = _createTypeLayout(subContext, ptrType->getValueType());
4771+
}
4772+
else
4773+
{
4774+
valueTypeLayout = _createTypeLayout(context, ptrType->getValueType());
4775+
}
47674776

47684777
ptrLayout->valueTypeLayout = valueTypeLayout.layout;
47694778

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// unit-test-ptr-layout.cpp
2+
3+
#include "slang-com-ptr.h"
4+
#include "slang.h"
5+
#include "unit-test/slang-unit-test.h"
6+
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
using namespace Slang;
11+
12+
SLANG_UNIT_TEST(pointerTypeLayout)
13+
{
14+
const char* testSource = "struct TestStruct {"
15+
" int3 member0;"
16+
" float member1;"
17+
"};";
18+
19+
ComPtr<slang::IGlobalSession> globalSession;
20+
SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
21+
slang::TargetDesc targetDesc = {};
22+
targetDesc.format = SLANG_SPIRV;
23+
targetDesc.profile = globalSession->findProfile("spirv_1_5");
24+
slang::SessionDesc sessionDesc = {};
25+
sessionDesc.targetCount = 1;
26+
sessionDesc.targets = &targetDesc;
27+
sessionDesc.compilerOptionEntryCount = 1;
28+
slang::CompilerOptionEntry compilerOptionEntry = {};
29+
compilerOptionEntry.name = slang::CompilerOptionName::EmitSpirvViaGLSL;
30+
compilerOptionEntry.value.kind = slang::CompilerOptionValueKind::Int;
31+
compilerOptionEntry.value.intValue0 = 1;
32+
sessionDesc.compilerOptionEntries = &compilerOptionEntry;
33+
34+
ComPtr<slang::ISession> session;
35+
SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
36+
37+
ComPtr<slang::IBlob> diagnosticBlob;
38+
auto module =
39+
session->loadModuleFromSourceString("m", "m.slang", testSource, diagnosticBlob.writeRef());
40+
SLANG_CHECK(module != nullptr);
41+
42+
43+
auto testBody = [&]()
44+
{
45+
auto reflection = module->getLayout();
46+
auto testStruct = reflection->findTypeByName("Ptr<TestStruct>");
47+
auto ptrLayout = reflection->getTypeLayout(testStruct);
48+
auto valueLayout = ptrLayout->getElementTypeLayout();
49+
SLANG_CHECK_ABORT(valueLayout->getFieldCount() == 2);
50+
SLANG_CHECK_ABORT(valueLayout->getFieldByIndex(1)->getOffset() == 12);
51+
};
52+
53+
testBody();
54+
}

0 commit comments

Comments
 (0)