summaryrefslogtreecommitdiffstats
path: root/tools/gfx-unit-test/gfx-test-util.h
blob: 38577cb083efaa6d4277046f66a95f934dbdb656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#pragma once

#include "core/slang-basic.h"
#include "core/slang-blob.h"
#include "core/slang-render-api-util.h"
#include "core/slang-test-tool-util.h"
#include "slang-rhi.h"
#include "span.h"
#include "unit-test/slang-unit-test.h"

// GFX_CHECK_CALL and GFX_CHECK_CALL_ABORT are used to check SlangResult
#define GFX_CHECK_CALL(x) SLANG_CHECK(!SLANG_FAILED(x))
#define GFX_CHECK_CALL_ABORT(x) SLANG_CHECK_ABORT(!SLANG_FAILED(x))

using namespace rhi;

namespace gfx_test
{
enum class PrecompilationMode
{
    None,
    SlangIR,
    InternalLink,
    ExternalLink,
};
/// Helper function for print out diagnostic messages output by Slang compiler.
void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob);

/// Loads a compute shader module and produces a `rhi::IShaderProgram`.
Slang::Result loadComputeProgram(
    rhi::IDevice* device,
    Slang::ComPtr<rhi::IShaderProgram>& outShaderProgram,
    const char* shaderModuleName,
    const char* entryPointName,
    slang::ProgramLayout*& slangReflection);

Slang::Result loadComputeProgram(
    rhi::IDevice* device,
    slang::ISession* slangSession,
    Slang::ComPtr<rhi::IShaderProgram>& outShaderProgram,
    const char* shaderModuleName,
    const char* entryPointName,
    slang::ProgramLayout*& slangReflection);

Slang::Result loadComputeProgramFromSource(
    rhi::IDevice* device,
    Slang::ComPtr<rhi::IShaderProgram>& outShaderProgram,
    std::string_view source);

Slang::Result loadGraphicsProgram(
    rhi::IDevice* device,
    Slang::ComPtr<rhi::IShaderProgram>& outShaderProgram,
    const char* shaderModuleName,
    const char* vertexEntryPointName,
    const char* fragmentEntryPointName,
    slang::ProgramLayout*& slangReflection);

template<typename T>
void compareResultFuzzy(const T* result, const T* expectedResult, size_t count)
{
    for (size_t i = 0; i < count; ++i)
    {
        SLANG_CHECK(abs(result[i] - expectedResult[i]) < 0.01f);
    }
}

template<typename T>
void compareResult(const T* result, const T* expectedResult, size_t count)
{
    for (size_t i = 0; i < count; i++)
    {
        SLANG_CHECK(result[i] == expectedResult[i]);
    }
}

template<typename T>
void compareComputeResult(rhi::IDevice* device, rhi::IBuffer* buffer, span<T> expectedResult)
{
    size_t bufferSize = expectedResult.size() * sizeof(T);
    // Read back the results.`
    ComPtr<ISlangBlob> bufferData;
    SLANG_CHECK(SLANG_SUCCEEDED(device->readBuffer(buffer, 0, bufferSize, bufferData.writeRef())));
    SLANG_CHECK(bufferData->getBufferSize() == bufferSize);
    const T* result = reinterpret_cast<const T*>(bufferData->getBufferPointer());

    if constexpr (std::is_same<T, float>::value || std::is_same<T, double>::value)
        compareResultFuzzy(result, expectedResult.data(), expectedResult.size());
    else
        compareResult<T>(result, expectedResult.data(), expectedResult.size());
}

template<typename T, size_t Count>
void compareComputeResult(
    rhi::IDevice* device,
    rhi::IBuffer* buffer,
    std::array<T, Count> expectedResult)
{
    compareComputeResult(device, buffer, span<T>(expectedResult.data(), Count));
}

template<typename T>
void compareComputeResult(
    rhi::IDevice* device,
    rhi::ITexture* texture,
    uint32_t layer,
    uint32_t mip,
    span<T> expectedResult)
{
    size_t bufferSize = expectedResult.size() * sizeof(T);
    // Read back the results.
    ComPtr<ISlangBlob> textureData;
    rhi::SubresourceLayout layout;
    SLANG_CHECK(
        SLANG_SUCCEEDED(device->readTexture(texture, layer, mip, textureData.writeRef(), &layout)));
    SLANG_CHECK(textureData->getBufferSize() >= bufferSize);

    uint8_t* buffer = (uint8_t*)textureData->getBufferPointer();
    for (uint32_t z = 0; z < layout.size.depth; z++)
    {
        for (uint32_t y = 0; y < layout.size.height; y++)
        {
            for (uint32_t x = 0; x < layout.size.width; x++)
            {
                const uint8_t* src = reinterpret_cast<const uint8_t*>(
                    buffer + z * layout.slicePitch + y * layout.rowPitch + x * layout.colPitch);
                uint8_t* dst = reinterpret_cast<uint8_t*>(
                    buffer +
                    (((z * layout.size.depth + y) * layout.size.width) + x) * layout.colPitch);
                ::memcpy(dst, src, layout.colPitch);
            }
        }
    }

    const T* result = reinterpret_cast<const T*>(textureData->getBufferPointer());

    if constexpr (std::is_same<T, float>::value)
        compareResultFuzzy(result, expectedResult.data(), expectedResult.size());
    else
        compareResult<T>(result, expectedResult.data(), expectedResult.size());
}

template<typename T, size_t Count>
void compareComputeResult(
    rhi::IDevice* device,
    rhi::ITexture* texture,
    uint32_t layer,
    uint32_t mip,
    std::array<T, Count> expectedResult)
{
    compareComputeResult(device, texture, layer, mip, span<T>(expectedResult.data(), Count));
}

Slang::ComPtr<rhi::IDevice> createTestingDevice(
    UnitTestContext* context,
    rhi::DeviceType deviceType,
    Slang::List<const char*> additionalSearchPaths = {});

Slang::List<const char*> getSlangSearchPaths();

void initializeRenderDoc();
void renderDocBeginFrame();
void renderDocEndFrame();

template<typename T, typename... Args>
auto makeArray(Args... args)
{
    return std::array<T, sizeof...(Args)>{static_cast<T>(args)...};
}

inline bool deviceTypeInEnabledApis(rhi::DeviceType deviceType, Slang::RenderApiFlags enabledApis)
{
    switch (deviceType)
    {
    case rhi::DeviceType::Default:
        return true;
    case rhi::DeviceType::CPU:
        return enabledApis & Slang::RenderApiFlag::CPU;
    case rhi::DeviceType::CUDA:
        return enabledApis & Slang::RenderApiFlag::CUDA;
    case rhi::DeviceType::Metal:
        return enabledApis & Slang::RenderApiFlag::Metal;
    case rhi::DeviceType::WGPU:
        return enabledApis & Slang::RenderApiFlag::WebGPU;
    case rhi::DeviceType::Vulkan:
        return enabledApis & Slang::RenderApiFlag::Vulkan;
    case rhi::DeviceType::D3D11:
        return enabledApis & Slang::RenderApiFlag::D3D11;
    case rhi::DeviceType::D3D12:
        return enabledApis & Slang::RenderApiFlag::D3D12;
    }
    return true;
}


template<typename ImplFunc>
void runTestImpl(
    const ImplFunc& f,
    UnitTestContext* context,
    rhi::DeviceType deviceType,
    Slang::List<const char*> searchPaths = {})
{
    if (!deviceTypeInEnabledApis(deviceType, context->enabledApis))
    {
        SLANG_IGNORE_TEST
    }

    auto device = createTestingDevice(context, deviceType, searchPaths);
    if (!device)
    {
        SLANG_IGNORE_TEST
    }
#if SLANG_WIN32
    // Skip d3d12 tests on x86 now since dxc doesn't function correctly there on Windows 11.
    if (rhi::DeviceType == rhi::DeviceType::D3D12)
    {
        SLANG_IGNORE_TEST
    }
#endif
    // Skip d3d11 tests when we don't have DXBC support as they're bound to
    // fail without a backend compiler
    if (deviceType == rhi::DeviceType::D3D11 && !SLANG_ENABLE_DXBC_SUPPORT)
    {
        SLANG_IGNORE_TEST
    }
    try
    {
        renderDocBeginFrame();
        f(device, context);
    }
    catch (AbortTestException& e)
    {
        renderDocEndFrame();
        throw e;
    }
    renderDocEndFrame();
}

} // namespace gfx_test