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
2.6 KiB86 linesraw
1// unit-test-default-matrix-layout.cpp
2
3#include "../../source/core/slang-list.h"
4#include "slang-com-helper.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
12namespace
13{
14
15using namespace Slang;
16
17struct DefaultMatrixLayoutTestContext
18{
19    DefaultMatrixLayoutTestContext(UnitTestContext* context)
20        : m_unitTestContext(context)
21    {
22        slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
23    }
24
25    SlangResult runTests()
26    {
27        slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession;
28        ComPtr<slang::ISession> session;
29        slang::SessionDesc sessionDesc{};
30        sessionDesc.targetCount = 1;
31        slang::TargetDesc targetDesc{};
32        targetDesc.format = SLANG_GLSL;
33        targetDesc.profile = slangSession->findProfile("glsl_460");
34        sessionDesc.targets = &targetDesc;
35        sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
36        SLANG_RETURN_ON_FAIL(slangSession->createSession(sessionDesc, session.writeRef()));
37
38        auto module = session->loadModuleFromSourceString(
39            "mymodule",
40            "mymodule.slang",
41            R"(
42            RWStructuredBuffer<float> output;
43            [numthreads(1,1,1)] [shader("compute")]
44            void main(uniform float3x4 m)
45            {
46                output[0] = m[0][0];
47            })");
48        if (!module)
49            return SLANG_FAIL;
50
51        ComPtr<slang::IEntryPoint> entryPoint;
52        SLANG_RETURN_ON_FAIL(module->findEntryPointByName("main", entryPoint.writeRef()));
53
54        if (!entryPoint)
55            return SLANG_FAIL;
56
57        slang::IComponentType* components[] = {module, entryPoint.get()};
58        ComPtr<slang::IComponentType> composedProgram;
59        SLANG_RETURN_ON_FAIL(
60            session->createCompositeComponentType(components, 2, composedProgram.writeRef()));
61
62        ComPtr<slang::IComponentType> linkedProgram;
63        SLANG_RETURN_ON_FAIL(composedProgram->link(linkedProgram.writeRef()));
64
65        ComPtr<slang::IBlob> outCode;
66        SLANG_RETURN_ON_FAIL(linkedProgram->getEntryPointCode(0, 0, outCode.writeRef()));
67
68        const char* code = (const char*)outCode->getBufferPointer();
69        if (strstr(code, "row_major") != nullptr)
70            return SLANG_OK;
71        return SLANG_FAIL;
72    }
73
74    UnitTestContext* m_unitTestContext;
75};
76
77} // namespace
78
79SLANG_UNIT_TEST(defaultMatrixLayout)
80{
81    DefaultMatrixLayoutTestContext context(unitTestContext);
82
83    const auto result = context.runTests();
84
85    SLANG_CHECK(SLANG_SUCCEEDED(result));
86}