yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix parameter location reflection for pure data paramblocks. (#5956)5df3a74af

master
3.7 KiB108 linesraw
1// unit-test-parameter-usage-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
14// Test that the isParameterLocationUsed API works.
15
16SLANG_UNIT_TEST(isParameterLocationUsedReflection)
17{
18    // Source for a module that contains an undecorated entrypoint.
19    const char* userSourceBody = R"(
20        Texture2D g_tex : register(t0);
21        struct Params {
22            Texture2D tex2;
23            Texture2D tex3;
24        };
25        struct Material
26        {
27            float2 uvScale;
28            float2 uvBias;
29        }
30        ParameterBlock<Params> gParams;
31        ConstantBuffer<Material> gcMaterial;
32        ParameterBlock<Material> gMaterial;
33        [shader("fragment")]
34        float4 fragMain(float4 pos:SV_Position, float unused:COLOR0, float4 used:COLOR1) : SV_Target
35        {
36            return g_tex.Load(int3(0, 0, 0)) + gParams.tex3.Load(int3(0)) + used + gMaterial.uvScale.x + gcMaterial.uvBias.x;
37        }
38        )";
39
40    auto moduleName = "moduleG" + String(Process::getId());
41    String userSource = "import " + moduleName + ";\n" + userSourceBody;
42    ComPtr<slang::IGlobalSession> globalSession;
43    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
44    slang::TargetDesc targetDesc = {};
45    targetDesc.format = SLANG_SPIRV;
46    targetDesc.profile = globalSession->findProfile("spirv_1_5");
47    slang::SessionDesc sessionDesc = {};
48    sessionDesc.targetCount = 1;
49    sessionDesc.targets = &targetDesc;
50    ComPtr<slang::ISession> session;
51    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
52
53    ComPtr<slang::IBlob> diagnosticBlob;
54    auto module = session->loadModuleFromSourceString(
55        "m",
56        "m.slang",
57        userSourceBody,
58        diagnosticBlob.writeRef());
59    SLANG_CHECK(module != nullptr);
60
61    ComPtr<slang::IEntryPoint> entryPoint;
62    module->findAndCheckEntryPoint(
63        "fragMain",
64        SLANG_STAGE_FRAGMENT,
65        entryPoint.writeRef(),
66        diagnosticBlob.writeRef());
67    SLANG_CHECK(entryPoint != nullptr);
68
69    ComPtr<slang::IComponentType> compositeProgram;
70    slang::IComponentType* components[] = {module, entryPoint.get()};
71    session->createCompositeComponentType(
72        components,
73        2,
74        compositeProgram.writeRef(),
75        diagnosticBlob.writeRef());
76    SLANG_CHECK(compositeProgram != nullptr);
77
78    ComPtr<slang::IComponentType> linkedProgram;
79    compositeProgram->link(linkedProgram.writeRef(), nullptr);
80
81    ComPtr<slang::IMetadata> metadata;
82    linkedProgram->getTargetMetadata(0, metadata.writeRef(), nullptr);
83
84    bool isUsed = false;
85    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 0, isUsed);
86    SLANG_CHECK(isUsed);
87
88    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 1, isUsed);
89    SLANG_CHECK(isUsed);
90
91    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 0, 2, isUsed);
92    SLANG_CHECK(!isUsed);
93
94    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 0, isUsed);
95    SLANG_CHECK(!isUsed);
96
97    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 1, 1, isUsed);
98    SLANG_CHECK(isUsed);
99
100    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT, 2, 0, isUsed);
101    SLANG_CHECK(isUsed);
102
103    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_VARYING_INPUT, 0, 0, isUsed);
104    SLANG_CHECK(!isUsed);
105
106    metadata->isParameterLocationUsed(SLANG_PARAMETER_CATEGORY_VARYING_INPUT, 0, 1, isUsed);
107    SLANG_CHECK(isUsed);
108}