yum-mirror/slang

Making it easier to work with shaders

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

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
3.8 KiB115 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 IModule::findAndCheckEntryPoint API supports discovering
15// entrypoints without a [shader] attribute.
16
17SLANG_UNIT_TEST(genericInterfaceConformance)
18{
19    // Source for a module that contains an undecorated entrypoint.
20    const char* userSourceBody = R"(
21        public interface ITestInterface<Real : IFloat> {
22            Real sample();
23        }
24
25        struct TestInterfaceImpl<Real : IFloat> : ITestInterface<Real> {
26            Real sample() {
27                return x;
28            }
29            Real x;
30        }
31
32        //TEST_INPUT: set data = new StructuredBuffer<ITestInterface<float> >[new TestInterfaceImpl<float>{1.0}];
33        StructuredBuffer<ITestInterface<float>> data;
34
35        //TEST_INPUT: set outputBuffer = out ubuffer(data=[0 0 0 0], stride=4);
36        RWStructuredBuffer<int> outputBuffer;
37
38        //TEST_INPUT: type_conformance TestInterfaceImpl<float>:ITestInterface<float> = 3
39
40        [numthreads(1, 1, 1)]
41        void computeMain()
42        {
43            let obj = data[0];
44            // CHECK: 1
45            outputBuffer[0] = int(obj.sample());
46        }
47        )";
48
49    ComPtr<slang::IGlobalSession> globalSession;
50    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
51    slang::TargetDesc targetDesc = {};
52    targetDesc.format = SLANG_HLSL;
53
54    slang::SessionDesc sessionDesc = {};
55    sessionDesc.targetCount = 1;
56    sessionDesc.targets = &targetDesc;
57    sessionDesc.allowGLSLSyntax = true;
58
59    ComPtr<slang::ISession> session;
60    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
61
62    ComPtr<slang::IBlob> diagnosticBlob;
63    auto module = session->loadModuleFromSourceString(
64        "m",
65        "m.slang",
66        userSourceBody,
67        diagnosticBlob.writeRef());
68    SLANG_CHECK(module != nullptr);
69
70    ComPtr<slang::IEntryPoint> entryPoint;
71    module->findAndCheckEntryPoint(
72        "computeMain",
73        SLANG_STAGE_COMPUTE,
74        entryPoint.writeRef(),
75        diagnosticBlob.writeRef());
76    SLANG_CHECK(entryPoint != nullptr);
77
78    ComPtr<slang::IComponentType> compositeProgram;
79    slang::IComponentType* components[] = {module, entryPoint.get()};
80    session->createCompositeComponentType(
81        components,
82        2,
83        compositeProgram.writeRef(),
84        diagnosticBlob.writeRef());
85    SLANG_CHECK(compositeProgram != nullptr);
86
87    ComPtr<slang::ITypeConformance> typeConformance;
88    auto result = session->createTypeConformanceComponentType(
89        compositeProgram->getLayout()->findTypeByName("TestInterfaceImpl<float>"),
90        compositeProgram->getLayout()->findTypeByName("ITestInterface<float>"),
91        typeConformance.writeRef(),
92        3,
93        diagnosticBlob.writeRef());
94    SLANG_CHECK(result == SLANG_OK);
95    SLANG_CHECK(typeConformance != nullptr);
96
97    ComPtr<slang::IComponentType> compositeProgram2;
98    slang::IComponentType* components2[] = {compositeProgram.get(), typeConformance.get()};
99    session->createCompositeComponentType(
100        components2,
101        2,
102        compositeProgram2.writeRef(),
103        diagnosticBlob.writeRef());
104
105    ComPtr<slang::IComponentType> linkedProgram;
106    compositeProgram2->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
107    SLANG_CHECK(linkedProgram != nullptr);
108
109    ComPtr<slang::IBlob> code;
110    linkedProgram->getEntryPointCode(0, 0, code.writeRef(), diagnosticBlob.writeRef());
111    SLANG_CHECK(code != nullptr);
112
113    auto codeSrc = UnownedStringSlice((const char*)code->getBufferPointer());
114    SLANG_CHECK(codeSrc.indexOf(toSlice("computeMain")) != -1);
115}