yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix entrypoint auto discovery logic. (#5885)49e912a9d

master
1.8 KiB56 linesraw
1// unit-test-cuda-compile.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 compilation API can be used to produce CUDA source.
15
16SLANG_UNIT_TEST(CudaCompile)
17{
18    // Source for a module that contains an undecorated entrypoint.
19    const char* userSourceBody = R"(
20        [CudaDeviceExport]
21        float testExportedFunc(float3 particleRayOrigin)
22        {
23            return dot(particleRayOrigin,particleRayOrigin); 
24        };
25        )";
26
27    auto moduleName = "moduleG" + String(Process::getId());
28    ComPtr<slang::IGlobalSession> globalSession;
29    SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK);
30    slang::TargetDesc targetDesc = {};
31    targetDesc.format = SLANG_CUDA_SOURCE;
32    slang::SessionDesc sessionDesc = {};
33    sessionDesc.targetCount = 1;
34    sessionDesc.targets = &targetDesc;
35    ComPtr<slang::ISession> session;
36    SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK);
37
38    ComPtr<slang::IBlob> diagnosticBlob;
39    auto module = session->loadModuleFromSourceString(
40        "m",
41        "m.slang",
42        userSourceBody,
43        diagnosticBlob.writeRef());
44    SLANG_CHECK(module != nullptr);
45
46    ComPtr<slang::IComponentType> linkedProgram;
47    module->link(linkedProgram.writeRef(), diagnosticBlob.writeRef());
48    SLANG_CHECK(linkedProgram != nullptr);
49
50    ComPtr<slang::IBlob> code;
51    linkedProgram->getTargetCode(0, code.writeRef(), diagnosticBlob.writeRef());
52    SLANG_CHECK(code != nullptr);
53    SLANG_CHECK(code->getBufferSize() != 0);
54    String text = String((char*)code->getBufferPointer());
55    SLANG_CHECK(text.indexOf("testExportedFunc") > 0);
56}