yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix geometry shader related modifier lowering. (#6197)4b9a34249

master
2.7 KiB93 linesraw
1// unit-test-geometry-shader.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 the compilation API for compiling geometry shaders to DXIL.
15
16#if SLANG_WINDOWS_FAMILY
17
18SLANG_UNIT_TEST(geometryShader)
19{
20    const char* userSourceBody = R"(
21        struct GS_INPUT
22        {
23            float4 PosSS : TEXTURE0;     // [Screen Space] Position
24        };
25
26        struct PS_INPUT
27        {
28            float4 PosSS : SV_POSITION;  // [Screen Space] Position
29        };
30
31        [maxvertexcount(3)] 
32        void main(triangle GS_INPUT input[3], inout TriangleStream<PS_INPUT> outStream)
33        {
34            PS_INPUT output;
35
36            output.PosSS = input[0].PosSS;
37            outStream.Append(output);
38     
39            output.PosSS = input[1].PosSS;
40            outStream.Append(output);
41
42            output.PosSS = input[2].PosSS;
43            outStream.Append(output);
44
45            outStream.RestartStrip();
46        }
47        )";
48    ComPtr<slang::IGlobalSession> globalSession;
49    SlangGlobalSessionDesc globalDesc = {};
50    globalDesc.enableGLSL = true;
51    SLANG_CHECK(slang_createGlobalSession2(&globalDesc, globalSession.writeRef()) == SLANG_OK);
52    slang::TargetDesc targetDesc = {};
53    targetDesc.format = SLANG_DXIL;
54    targetDesc.profile = globalSession->findProfile("sm_6_0");
55    slang::SessionDesc sessionDesc = {};
56    sessionDesc.targetCount = 1;
57    sessionDesc.targets = &targetDesc;
58    ComPtr<slang::ISession> session;
59    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
60
61    ComPtr<slang::IBlob> diagnosticBlob;
62    auto module = session->loadModuleFromSourceString(
63        "m",
64        "m.slang",
65        userSourceBody,
66        diagnosticBlob.writeRef());
67    SLANG_CHECK(module != nullptr);
68
69    ComPtr<slang::IEntryPoint> entryPoint;
70    module->findAndCheckEntryPoint(
71        "main",
72        SLANG_STAGE_GEOMETRY,
73        entryPoint.writeRef(),
74        diagnosticBlob.writeRef());
75
76    slang::IComponentType* componentTypes[2] = {module, entryPoint.get()};
77    ComPtr<slang::IComponentType> composedProgram;
78    session->createCompositeComponentType(
79        componentTypes,
80        2,
81        composedProgram.writeRef(),
82        diagnosticBlob.writeRef());
83
84    ComPtr<slang::IComponentType> linkedProgram;
85    composedProgram->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
86
87    ComPtr<slang::IBlob> code;
88    linkedProgram->getEntryPointCode(0, 0, code.writeRef(), diagnosticBlob.writeRef());
89
90    SLANG_CHECK(code != nullptr);
91}
92
93#endif