yum-mirror/slang

Making it easier to work with shaders

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

jarcherNVUpdate cuda context creation to support cuda 13 (#8181)3639e71df

master
3.8 KiB122 linesraw
1// cuda-helper-functions.h
2#pragma once
3
4#include "../../../source/core/slang-list.h"
5#include "cuda-base.h"
6#include "slang-gfx.h"
7
8namespace gfx
9{
10using namespace Slang;
11
12#ifdef GFX_ENABLE_CUDA
13namespace cuda
14{
15SLANG_FORCE_INLINE bool _isError(CUresult result)
16{
17    return result != 0;
18}
19
20// A enum used to control if errors are reported on failure of CUDA call.
21enum class CUDAReportStyle
22{
23    Normal,
24    Silent,
25};
26
27struct CUDAErrorInfo
28{
29    CUDAErrorInfo(
30        const char* filePath,
31        int lineNo,
32        const char* errorName = nullptr,
33        const char* errorString = nullptr)
34        : m_filePath(filePath), m_lineNo(lineNo), m_errorName(errorName), m_errorString(errorString)
35    {
36    }
37    SlangResult handle() const;
38
39    const char* m_filePath;
40    int m_lineNo;
41    const char* m_errorName;
42    const char* m_errorString;
43};
44
45// If this code path is enabled, CUDA errors will be reported directly to StdWriter::out stream.
46
47SlangResult _handleCUDAError(CUresult cuResult, const char* file, int line);
48
49#define SLANG_CUDA_HANDLE_ERROR(x) _handleCUDAError(x, __FILE__, __LINE__)
50
51#define SLANG_CUDA_RETURN_ON_FAIL(x)              \
52    {                                             \
53        auto _res = x;                            \
54        if (_isError(_res))                       \
55            return SLANG_CUDA_HANDLE_ERROR(_res); \
56    }
57
58#define SLANG_CUDA_RETURN_WITH_REPORT_ON_FAIL(x, r)                                             \
59    {                                                                                           \
60        auto _res = x;                                                                          \
61        if (_isError(_res))                                                                     \
62        {                                                                                       \
63            return (r == CUDAReportStyle::Normal) ? SLANG_CUDA_HANDLE_ERROR(_res) : SLANG_FAIL; \
64        }                                                                                       \
65    }
66
67#define SLANG_CUDA_ASSERT_ON_FAIL(x)           \
68    {                                          \
69        auto _res = x;                         \
70        if (_isError(_res))                    \
71        {                                      \
72            SLANG_ASSERT(!"Failed CUDA call"); \
73        };                                     \
74    }
75
76#ifdef RENDER_TEST_OPTIX
77
78bool _isError(OptixResult result);
79
80#if 1
81SlangResult _handleOptixError(OptixResult result, char const* file, int line);
82
83#define SLANG_OPTIX_HANDLE_ERROR(RESULT) _handleOptixError(RESULT, __FILE__, __LINE__)
84#else
85#define SLANG_OPTIX_HANDLE_ERROR(RESULT) SLANG_FAIL
86#endif
87
88#define SLANG_OPTIX_RETURN_ON_FAIL(EXPR)           \
89    do                                             \
90    {                                              \
91        auto _res = EXPR;                          \
92        if (_isError(_res))                        \
93            return SLANG_OPTIX_HANDLE_ERROR(_res); \
94    } while (0)
95
96void _optixLogCallback(unsigned int level, const char* tag, const char* message, void* userData);
97
98#endif
99
100AdapterLUID getAdapterLUID(int deviceIndex);
101
102// Version-aware cuCtxCreate wrapper that works with both CUDA 12 and CUDA 13
103inline CUresult createCudaContext(CUcontext* pctx, unsigned int flags, CUdevice dev)
104{
105#if CUDA_VERSION >= 13000
106    // CUDA 13+ requires CUctxCreateParams
107    CUctxCreateParams ctxCreateParams = {};
108    return cuCtxCreate(pctx, &ctxCreateParams, flags, dev);
109#else
110    // CUDA 12 and earlier use the old signature
111    return cuCtxCreate(pctx, flags, dev);
112#endif
113}
114
115} // namespace cuda
116#endif
117
118Result SLANG_MCALL getCUDAAdapters(List<AdapterInfo>& outAdapters);
119
120Result SLANG_MCALL createCUDADevice(const IDevice::Desc* desc, IDevice** outDevice);
121
122} // namespace gfx