yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix argument buffer tier2 layout computation. (#6101)edf5e9f97

master
2.3 KiB70 linesraw
1// unit-test-argument-buffer-tier-2-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 metal argument buffer tier2 layout rules.
15
16SLANG_UNIT_TEST(metalArgumentBufferTier2Reflection)
17{
18    const char* userSourceBody = R"(
19        struct A
20        {
21          float3 one;
22          float3 two;
23          float three;
24        }
25
26        struct Args{
27          ParameterBlock<A> a;
28        }
29        ParameterBlock<Args> argument_buffer;
30        RWStructuredBuffer<float> outputBuffer;
31
32        [numthreads(1,1,1)]
33        void computeMain()
34        {
35            outputBuffer[0] = argument_buffer.a.two.x;
36        }
37        )";
38
39    auto moduleName = "moduleG" + String(Process::getId());
40    String userSource = "import " + moduleName + ";\n" + userSourceBody;
41    ComPtr<slang::IGlobalSession> globalSession;
42    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
43    slang::TargetDesc targetDesc = {};
44    targetDesc.format = SLANG_SPIRV;
45    targetDesc.profile = globalSession->findProfile("spirv_1_5");
46    slang::SessionDesc sessionDesc = {};
47    sessionDesc.targetCount = 1;
48    sessionDesc.targets = &targetDesc;
49    ComPtr<slang::ISession> session;
50    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
51
52    ComPtr<slang::IBlob> diagnosticBlob;
53    auto module = session->loadModuleFromSourceString(
54        "m",
55        "m.slang",
56        userSourceBody,
57        diagnosticBlob.writeRef());
58    SLANG_CHECK(module != nullptr);
59
60    auto layout = module->getLayout();
61
62    auto type = layout->findTypeByName("A");
63    auto typeLayout = layout->getTypeLayout(type, slang::LayoutRules::MetalArgumentBufferTier2);
64    SLANG_CHECK(typeLayout->getFieldByIndex(0)->getOffset() == 0);
65    SLANG_CHECK(typeLayout->getFieldByIndex(0)->getTypeLayout()->getSize() == 16);
66    SLANG_CHECK(typeLayout->getFieldByIndex(1)->getOffset() == 16);
67    SLANG_CHECK(typeLayout->getFieldByIndex(1)->getTypeLayout()->getSize() == 16);
68    SLANG_CHECK(typeLayout->getFieldByIndex(2)->getOffset() == 32);
69    SLANG_CHECK(typeLayout->getFieldByIndex(2)->getTypeLayout()->getSize() == 4);
70}