yum-mirror/slang

Making it easier to work with shaders

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

aidanfnvReturn non-escaped strings from user-defined attributes (#6735)9972a5269

master
2.5 KiB84 linesraw
1// unit-test-translation-unit-import.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 reflection API provides correct info about attributes.
15
16SLANG_UNIT_TEST(attributeReflection)
17{
18    const char* userSourceBody = R"(
19        public enum E
20        {
21            V0,
22            V1,
23        };
24
25        [__AttributeUsage(_AttributeTargets.Struct)]
26        public struct NormalTextureAttribute
27        {
28            public E Type;
29            public float x;
30        };
31
32        [COM("042BE50B-CB01-4DBB-8367-3A9CDCBE2F49")]
33        interface IInterface { void f(); }
34
35        [NormalTexture(E.V1, 6)]
36        struct TS {};
37        )";
38    String userSource = userSourceBody;
39    ComPtr<slang::IGlobalSession> globalSession;
40    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
41    slang::TargetDesc targetDesc = {};
42    targetDesc.format = SLANG_HLSL;
43    targetDesc.profile = globalSession->findProfile("sm_5_0");
44    slang::SessionDesc sessionDesc = {};
45    sessionDesc.targetCount = 1;
46    sessionDesc.targets = &targetDesc;
47    ComPtr<slang::ISession> session;
48    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
49
50    ComPtr<slang::IBlob> diagnosticBlob;
51    auto module = session->loadModuleFromSourceString(
52        "m",
53        "m.slang",
54        userSourceBody,
55        diagnosticBlob.writeRef());
56    SLANG_CHECK(module != nullptr);
57
58    auto reflection = module->getLayout();
59
60    auto interfaceType = reflection->findTypeByName("IInterface");
61    SLANG_CHECK(interfaceType != nullptr);
62
63    auto comAttribute = interfaceType->findAttributeByName("COM");
64    SLANG_CHECK(comAttribute != nullptr);
65
66    size_t size = 0;
67    auto guid = comAttribute->getArgumentValueString(0, &size);
68    UnownedStringSlice stringSlice = UnownedStringSlice(guid, size);
69    SLANG_CHECK(stringSlice == "042BE50B-CB01-4DBB-8367-3A9CDCBE2F49");
70
71    auto testType = reflection->findTypeByName("TS");
72    SLANG_CHECK(testType != nullptr);
73
74    auto normalTextureAttribute = testType->findAttributeByName("NormalTexture");
75    SLANG_CHECK(normalTextureAttribute != nullptr);
76
77    int value = 0;
78    normalTextureAttribute->getArgumentValueInt(0, &value);
79    SLANG_CHECK(value == 1);
80
81    float fvalue = 0;
82    normalTextureAttribute->getArgumentValueFloat(1, &fvalue);
83    SLANG_CHECK(fvalue == 6.0);
84}