Skip to content

Commit 7d4432b

Browse files
authored
Merge pull request shader-slang#1375 from csyonghe/findtypebynamefix
Fix FindTypeByName reflection API not finding stdlib types.
2 parents 43c1467 + 52026c7 commit 7d4432b

5 files changed

+68
-2
lines changed

source/slang/slang-check-shader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ namespace Slang
12901290
// it defines keywords like `true` and `false`).
12911291
//
12921292
RefPtr<Scope> scope = new Scope();
1293-
scope->parent = getLinkage()->getSessionImpl()->baseLanguageScope;
1293+
scope->parent = getLinkage()->getSessionImpl()->slangLanguageScope;
12941294
//
12951295
// Next, the scope needs to include all of the
12961296
// modules in the program as peers, as if they

tools/slang-test/slang-test.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
<ClCompile Include="test-reporter.cpp" />
178178
<ClCompile Include="unit-offset-container.cpp" />
179179
<ClCompile Include="unit-test-byte-encode.cpp" />
180+
<ClCompile Include="unit-test-find-type-by-name.cpp" />
180181
<ClCompile Include="unit-test-free-list.cpp" />
181182
<ClCompile Include="unit-test-memory-arena.cpp" />
182183
<ClCompile Include="unit-test-path.cpp" />

tools/slang-test/slang-test.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,8 @@
6565
<ClCompile Include="unit-test-string.cpp">
6666
<Filter>Source Files</Filter>
6767
</ClCompile>
68+
<ClCompile Include="unit-test-find-type-by-name.cpp">
69+
<Filter>Source Files</Filter>
70+
</ClCompile>
6871
</ItemGroup>
6972
</Project>

tools/slang-test/test-reporter.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
#include "../../source/core/slang-dictionary.h"
1010

1111

12-
#define SLANG_CHECK(x) TestReporter::get()->addResultWithLocation((x), #x, __FILE__, __LINE__);
12+
#define SLANG_CHECK(x) TestReporter::get()->addResultWithLocation((x), #x, __FILE__, __LINE__);
13+
#define SLANG_CHECK_ABORT(x) \
14+
{ \
15+
bool _slang_check_result = (x); \
16+
TestReporter::get()->addResultWithLocation(_slang_check_result, #x, __FILE__, __LINE__); \
17+
if (!_slang_check_result) return; \
18+
}
1319

1420
struct TestRegister
1521
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// unit-test-byte-encode.cpp
2+
3+
#include "../../slang.h"
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
#include "test-context.h"
9+
10+
using namespace Slang;
11+
12+
static void findTypeByNameTest()
13+
{
14+
const char* testSource =
15+
"struct TestStruct {"
16+
" int member0;"
17+
" Texture2D texture1;"
18+
"};";
19+
auto session = spCreateSession();
20+
auto request = spCreateCompileRequest(session);
21+
spAddCodeGenTarget(request, SLANG_DXBC);
22+
int tuIndex = spAddTranslationUnit(request, SLANG_SOURCE_LANGUAGE_SLANG, "tu1");
23+
spAddTranslationUnitSourceString(request, tuIndex, "internalFile", testSource);
24+
spCompile(request);
25+
26+
auto testBody = [&]()
27+
{
28+
auto reflection = slang::ShaderReflection::get(request);
29+
auto testStruct = reflection->findTypeByName("TestStruct");
30+
SLANG_CHECK_ABORT(testStruct->getFieldCount() == 2);
31+
auto field0Name = testStruct->getFieldByIndex(0)->getName();
32+
SLANG_CHECK_ABORT(field0Name != nullptr && strcmp(field0Name, "member0") == 0);
33+
auto field1Name = testStruct->getFieldByIndex(1)->getName();
34+
SLANG_CHECK_ABORT(field1Name != nullptr && strcmp(field1Name, "texture1") == 0);
35+
36+
auto intType = reflection->findTypeByName("int");
37+
auto intTypeName = intType->getName();
38+
SLANG_CHECK_ABORT(intTypeName && strcmp(intTypeName, "int") == 0);
39+
40+
auto paramBlockType = reflection->findTypeByName("ParameterBlock<TestStruct>");
41+
SLANG_CHECK_ABORT(paramBlockType != nullptr);
42+
auto paramBlockTypeName = paramBlockType->getName();
43+
SLANG_CHECK_ABORT(paramBlockTypeName && strcmp(paramBlockTypeName, "ParameterBlock") == 0);
44+
auto paramBlockElementType = paramBlockType->getElementType();
45+
SLANG_CHECK_ABORT(paramBlockElementType != nullptr);
46+
auto paramBlockElementTypeName = paramBlockElementType->getName();
47+
SLANG_CHECK_ABORT(paramBlockElementTypeName && strcmp(paramBlockElementTypeName, "TestStruct") == 0);
48+
};
49+
50+
testBody();
51+
52+
spDestroyCompileRequest(request);
53+
spDestroySession(session);
54+
}
55+
56+
SLANG_UNIT_TEST("findTypeByName", findTypeByNameTest);

0 commit comments

Comments
 (0)