yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

James Helferty (NVIDIA)Hide atomics struct from reflection api (#7520)78dc7c3fd

master
5.5 KiB135 linesraw
1// unit-test-atomic-reflection.cpp
2
3#include "../../source/core/slang-io.h"
4#include "../../source/core/slang-process.h"
5#include "slang-com-ptr.h"
6#include "slang.h"
7#include "unit-test/slang-unit-test.h"
8
9#include <stdio.h>
10#include <stdlib.h>
11
12using namespace Slang;
13
14SLANG_UNIT_TEST(atomicReflection)
15{
16    const char* userSourceBody = R"(
17        RWStructuredBuffer<uint> bufA;  // for reference
18        RWStructuredBuffer<Atomic<uint>> bufB;
19        struct TestStruct {
20            Atomic<uint> fieldA;
21            Atomic<uint> fieldB[2];
22            vector<Atomic<uint>, 2> fieldC;
23        };
24        RWStructuredBuffer<TestStruct> bufC;
25
26        [shader("compute")]
27        [numthreads(64, 1, 1)]
28        void main(uint2 dispatchThreadId : SV_DispatchThreadID)
29        {
30        }
31        )";
32    String userSource = userSourceBody;
33    ComPtr<slang::IGlobalSession> globalSession;
34    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
35    slang::TargetDesc targetDesc = {};
36    targetDesc.format = SLANG_HLSL;
37    targetDesc.profile = globalSession->findProfile("sm_5_0");
38    slang::SessionDesc sessionDesc = {};
39    sessionDesc.targetCount = 1;
40    sessionDesc.targets = &targetDesc;
41    ComPtr<slang::ISession> session;
42    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
43
44    ComPtr<slang::IBlob> diagnosticBlob;
45    auto module = session->loadModuleFromSourceString(
46        "m",
47        "m.slang",
48        userSourceBody,
49        diagnosticBlob.writeRef());
50    SLANG_CHECK(module != nullptr);
51
52    auto reflection = module->getLayout();
53
54    auto bufAVariableLayout = reflection->getParameterByIndex(0);
55    auto bufBVariableLayout = reflection->getParameterByIndex(1);
56    auto bufCVariableLayout = reflection->getParameterByIndex(2);
57
58    SLANG_CHECK(bufAVariableLayout != nullptr);
59    SLANG_CHECK(bufBVariableLayout != nullptr);
60    SLANG_CHECK(bufCVariableLayout != nullptr);
61
62    // This test walks down the parameters in a different way from the JSON
63    // option. The slangc option "-reflection-json" walks:
64    //   param->getTypeLayout()->getElementTypeLayout()->getType()->getKind()
65    // This test uses an alternate walk:
66    //   param->getVariable()->getType()->getElementType()->getKind()
67
68    auto bufAVariable = bufAVariableLayout->getVariable();
69    auto bufBVariable = bufBVariableLayout->getVariable();
70    auto bufCVariable = bufCVariableLayout->getVariable();
71
72    SLANG_CHECK(bufAVariable != nullptr);
73    SLANG_CHECK(bufBVariable != nullptr);
74    SLANG_CHECK(bufCVariable != nullptr);
75
76    auto bufAType = bufAVariable->getType();
77    auto bufBType = bufBVariable->getType();
78    auto bufCType = bufCVariable->getType();
79
80    SLANG_CHECK(bufAType->getKind() == slang::TypeReflection::Kind::Resource);
81    SLANG_CHECK(bufBType->getKind() == slang::TypeReflection::Kind::Resource);
82    SLANG_CHECK(bufCType->getKind() == slang::TypeReflection::Kind::Resource);
83
84    auto bufAElementType = bufAType->getElementType();
85    auto bufBElementType = bufBType->getElementType();
86    auto bufCElementType = bufCType->getElementType();
87
88    SLANG_CHECK(bufAElementType->getKind() == slang::TypeReflection::Kind::Scalar);
89    SLANG_CHECK(bufBElementType->getKind() == slang::TypeReflection::Kind::Scalar);
90    SLANG_CHECK(bufAElementType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
91    SLANG_CHECK(bufBElementType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
92
93    auto bufAResResultType = bufAType->getResourceResultType();
94    auto bufBResResultType = bufBType->getResourceResultType();
95
96    SLANG_CHECK(bufAResResultType->getKind() == slang::TypeReflection::Kind::Scalar);
97    SLANG_CHECK(bufBResResultType->getKind() == slang::TypeReflection::Kind::Scalar);
98    SLANG_CHECK(bufAResResultType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
99    SLANG_CHECK(bufBResResultType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
100
101    // Atomics embedded in structs require traversing the fields
102    SLANG_CHECK(bufCElementType->getKind() == slang::TypeReflection::Kind::Struct);
103
104    auto bufCFieldCount = bufCElementType->getFieldCount();
105    SLANG_CHECK(bufCFieldCount == 3);
106
107    auto fieldA = bufCElementType->getFieldByIndex(0);
108    auto fieldB = bufCElementType->getFieldByIndex(1);
109    auto fieldC = bufCElementType->getFieldByIndex(2);
110
111    SLANG_CHECK(fieldA != nullptr);
112    SLANG_CHECK(fieldB != nullptr);
113    SLANG_CHECK(fieldC != nullptr);
114
115    auto fieldAType = fieldA->getType();
116    auto fieldBType = fieldB->getType();
117    auto fieldCType = fieldC->getType();
118
119    SLANG_CHECK(fieldAType->getKind() == slang::TypeReflection::Kind::Scalar);
120    SLANG_CHECK(fieldAType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
121
122    SLANG_CHECK(fieldBType->getKind() == slang::TypeReflection::Kind::Array);
123    SLANG_CHECK(fieldBType->getElementCount() == 2);
124    auto fieldBElementType = fieldBType->getElementType();
125    SLANG_CHECK(fieldBElementType != nullptr);
126    SLANG_CHECK(fieldBElementType->getKind() == slang::TypeReflection::Kind::Scalar);
127    SLANG_CHECK(fieldBElementType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
128
129    SLANG_CHECK(fieldCType->getKind() == slang::TypeReflection::Kind::Vector);
130    SLANG_CHECK(fieldCType->getElementCount() == 2);
131    auto fieldCElementType = fieldCType->getElementType();
132    SLANG_CHECK(fieldCElementType != nullptr);
133    SLANG_CHECK(fieldCElementType->getKind() == slang::TypeReflection::Kind::Scalar);
134    SLANG_CHECK(fieldCElementType->getScalarType() == slang::TypeReflection::ScalarType::UInt32);
135}