summaryrefslogtreecommitdiffstats
path: root/tools/gfx-unit-test/resolve-resource-tests.cpp
blob: eac03c22898baa595ae791fe3718b362d2e9e35e (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#if 0
// Disabled: slang-rhi doesn't have resolveResource API.

#include "core/slang-basic.h"
#include "gfx-test-util.h"
#include "unit-test/slang-unit-test.h"

#include <slang-rhi.h>
#include <slang-rhi/shader-cursor.h>

#if SLANG_WINDOWS_FAMILY
#include <d3d12.h>
#endif

using namespace Slang;
using namespace rhi;

namespace
{
using namespace gfx_test;

struct Vertex
{
    float position[3];
    float color[3];
};

static const int kVertexCount = 12;
static const Vertex kVertexData[kVertexCount] = {
    // Triangle 1
    {{0, 0, 0.5}, {1, 0, 0}},
    {{1, 1, 0.5}, {1, 0, 0}},
    {{-1, 1, 0.5}, {1, 0, 0}},

    // Triangle 2
    {{-1, 1, 0.5}, {0, 1, 0}},
    {{0, 0, 0.5}, {0, 1, 0}},
    {{-1, -1, 0.5}, {0, 1, 0}},

    // Triangle 3
    {{-1, -1, 0.5}, {0, 0, 1}},
    {{0, 0, 0.5}, {0, 0, 1}},
    {{1, -1, 0.5}, {0, 0, 1}},

    // Triangle 4
    {{1, -1, 0.5}, {0, 0, 0}},
    {{0, 0, 0.5}, {0, 0, 0}},
    {{1, 1, 0.5}, {0, 0, 0}},
};

const int kWidth = 256;
const int kHeight = 256;
Format format = Format::RGBA32Float;

ComPtr<IBuffer> createVertexBuffer(IDevice* device)
{
            BufferDesc vertexBufferDesc;
        vertexBufferDesc.size = kVertexCount * sizeof(Vertex);
        vertexBufferDesc.defaultState = ResourceState::VertexBuffer;
        vertexBufferDesc.usage = BufferUsage::VertexBuffer;
    ComPtr<IBuffer> vertexBuffer = device->createBuffer(vertexBufferDesc, &kVertexData[0]);
    SLANG_CHECK_ABORT(vertexBuffer != nullptr);
    return vertexBuffer;
}

struct BaseResolveResourceTest
{
    IDevice* device;
    UnitTestContext* context;

    ComPtr<ITexture> msaaTexture;
    ComPtr<ITexture> dstTexture;

    ComPtr<IRenderPipeline> pipelineState;

    ComPtr<IBuffer> vertexBuffer;

    struct TextureInfo
    {
        Extent3D extent;
        int numMipLevels;
        int arraySize;
        const SubresourceData* initData;
    };

    void init(IDevice* device, UnitTestContext* context)
    {
        this->device = device;
        this->context = context;
    }

    void createRequiredResources(
        TextureInfo msaaTextureInfo,
        TextureInfo dstTextureInfo,
        Format format)
    {
        VertexStreamDesc vertexStreams[] = {
            {sizeof(Vertex), InputSlotClass::PerVertex, 0},
        };

        InputElementDesc inputElements[] = {
            // Vertex buffer data
            {"POSITION", 0, Format::RGB32Float, offsetof(Vertex, position), 0},
            {"COLOR", 0, Format::RGB32Float, offsetof(Vertex, color), 0},
        };

        TextureDesc msaaTexDesc = {};
        msaaTexDesc.type = TextureType::Texture2D;
        msaaTexDesc.mipCount = dstTextureInfo.numMipLevels;
        msaaTexDesc.arrayLength = dstTextureInfo.arraySize;
        msaaTexDesc.size = dstTextureInfo.extent;
        msaaTexDesc.defaultState = ResourceState::RenderTarget;
        msaaTexDesc.usage = TextureUsage::RenderTarget;
        msaaTexDesc.format = format;
        msaaTexDesc.sampleCount = 4;

        msaaTexture = device->createTexture(msaaTexDesc, msaaTextureInfo.initData);
        SLANG_CHECK_ABORT(msaaTexture);

        TextureDesc dstTexDesc = {};
        dstTexDesc.type = TextureType::Texture2D;
        dstTexDesc.mipCount = dstTextureInfo.numMipLevels;
        dstTexDesc.arrayLength = dstTextureInfo.arraySize;
        dstTexDesc.size = dstTextureInfo.extent;
        dstTexDesc.defaultState = ResourceState::CopyDestination;
        dstTexDesc.usage = TextureUsage::CopyDestination | TextureUsage::CopySource;
        dstTexDesc.format = format;

        dstTexture = device->createTexture(dstTexDesc, dstTextureInfo.initData);
        SLANG_CHECK_ABORT(dstTexture);

        InputLayoutDesc inputLayoutDesc = {};
        inputLayoutDesc.inputElementCount = SLANG_COUNT_OF(inputElements);
        inputLayoutDesc.inputElements = inputElements;
        inputLayoutDesc.vertexStreamCount = SLANG_COUNT_OF(vertexStreams);
        inputLayoutDesc.vertexStreams = vertexStreams;
        auto inputLayout = device->createInputLayout(inputLayoutDesc);
        SLANG_CHECK_ABORT(inputLayout != nullptr);

        vertexBuffer = createVertexBuffer(device);

        ComPtr<IShaderProgram> shaderProgram;
        slang::ProgramLayout* slangReflection;
        GFX_CHECK_CALL_ABORT(loadGraphicsProgram(
            device,
            shaderProgram,
            "resolve-resource-shader",
            "vertexMain",
            "fragmentMain",
            slangReflection));

        ColorTargetDesc colorTarget = {};
        colorTarget.format = format;
        
        RenderPipelineDesc pipelineDesc = {};
        pipelineDesc.program = shaderProgram.get();
        pipelineDesc.inputLayout = inputLayout;
        pipelineDesc.targets = &colorTarget;
        pipelineDesc.targetCount = 1;
        pipelineDesc.primitiveTopology = PrimitiveTopology::TriangleList;
        pipelineDesc.depthStencil.depthTestEnable = false;
        pipelineDesc.depthStencil.depthWriteEnable = false;
        pipelineState = device->createRenderPipeline(pipelineDesc);
        SLANG_CHECK_ABORT(pipelineState);
    }

    void submitGPUWork(
        SubresourceRange msaaSubresource,
        SubresourceRange dstSubresource,
        Extent3D extent)
    {
        auto queue = device->getQueue(QueueType::Graphics);

        ComPtr<ICommandEncoder> encoder = queue->createCommandEncoder();
        
        // Create render target view
        TextureViewDesc rtvDesc = {};
        rtvDesc.format = format;
        auto rtv = device->createTextureView(msaaTexture, rtvDesc);

        RenderPassColorAttachment colorAttachment = {};
        colorAttachment.view = rtv;
        colorAttachment.loadOp = LoadOp::Clear;
        colorAttachment.storeOp = StoreOp::Store;
        float clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f};
        memcpy(colorAttachment.clearValue, clearColor, sizeof(clearColor));

        RenderPassDesc passDesc = {};
        passDesc.colorAttachments = &colorAttachment;
        passDesc.colorAttachmentCount = 1;

        auto renderEncoder = encoder->beginRenderPass(passDesc);
        auto rootObject = renderEncoder->bindPipeline(pipelineState);

        Viewport viewport = {};
        viewport.maxZ = 1.0f;
        viewport.extentX = kWidth;
        viewport.extentY = kHeight;
        
        RenderState state = {};
        state.viewports[0] = viewport;
        state.viewportCount = 1;
        state.vertexBuffers[0] = BufferOffsetPair(vertexBuffer, 0);
        state.vertexBufferCount = 1;
        renderEncoder->setRenderState(state);

        DrawArguments drawArgs = {};
        drawArgs.vertexCount = kVertexCount;
        drawArgs.startVertexLocation = 0;
        renderEncoder->draw(drawArgs);
        renderEncoder->end();

        // Note: slang-rhi doesn't have a direct resolveResource function
        // For MSAA resolve, we would typically use a resolve render pass or blit operation
        // For this test, we'll use a simple copy operation instead
        encoder->copyTexture(
            dstTexture,
            dstSubresource,
            Offset3D{0, 0, 0},
            msaaTexture,
            msaaSubresource,
            Offset3D{0, 0, 0},
            extent);
        encoder->setTextureState(
            dstTexture,
            dstSubresource,
            ResourceState::CopySource);
            
        queue->submit(encoder->finish());
        queue->waitOnHost();
    }

    void checkTestResults(
        int pixelCount,
        int channelCount,
        const int* testXCoords,
        const int* testYCoords,
        float* testResults)
    {
        // Read texture values back from four specific pixels located within the triangles
        // and compare against expected values (because testing every single pixel will be too long
        // and tedious and requires maintaining reference images).
        ComPtr<ISlangBlob> resultBlob;
        size_t rowPitch = 0;
        size_t pixelSize = 0;
        GFX_CHECK_CALL_ABORT(device->readTexture(
            dstTexture,
            0, // layer
            0, // mip
            resultBlob.writeRef(),
            nullptr)); // SubresourceLayout output is optional
        auto result = (float*)resultBlob->getBufferPointer();

        int cursor = 0;
        for (int i = 0; i < pixelCount; ++i)
        {
            auto x = testXCoords[i];
            auto y = testYCoords[i];
            auto pixelPtr = result + x * channelCount + y * rowPitch / sizeof(float);
            for (int j = 0; j < channelCount; ++j)
            {
                testResults[cursor] = pixelPtr[j];
                cursor++;
            }
        }

        float expectedResult[] = {0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.5f, 0.0f, 0.0f,
                                  1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f,
                                  0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f, 1.0f};
        SLANG_CHECK(memcmp(testResults, expectedResult, 128) == 0);
    }
};

struct ResolveResourceSimple : BaseResolveResourceTest
{
    void run()
    {
        Extent3D extent = {};
        extent.width = kWidth;
        extent.height = kHeight;
        extent.depth = 1;

        TextureInfo msaaTextureInfo = {extent, 1, 1, nullptr};
        TextureInfo dstTextureInfo = {extent, 1, 1, nullptr};

        createRequiredResources(msaaTextureInfo, dstTextureInfo, format);

        SubresourceRange msaaSubresource = {};
        msaaSubresource.layer = 0;
        msaaSubresource.layerCount = 1;
        msaaSubresource.mip = 0;
        msaaSubresource.mipCount = 1;

        SubresourceRange dstSubresource = {};
        dstSubresource.layer = 0;
        dstSubresource.layerCount = 1;
        dstSubresource.mip = 0;
        dstSubresource.mipCount = 1;

        submitGPUWork(msaaSubresource, dstSubresource, extent);

        const int kPixelCount = 8;
        const int kChannelCount = 4;
        int testXCoords[kPixelCount] = {64, 127, 191, 64, 191, 64, 127, 191};
        int testYCoords[kPixelCount] = {64, 64, 64, 127, 127, 191, 191, 191};
        float testResults[kPixelCount * kChannelCount];

        checkTestResults(kPixelCount, kChannelCount, testXCoords, testYCoords, testResults);
    }
};

template<typename T>
void resolveResourceTestImpl(IDevice* device, UnitTestContext* context)
{
    T test;
    test.init(device, context);
    test.run();
}
} // namespace

namespace gfx_test
{
SLANG_UNIT_TEST(resolveResourceSimpleD3D12)
{
    runTestImpl(
        resolveResourceTestImpl<ResolveResourceSimple>,
        unitTestContext,
        DeviceType::D3D12);
}

SLANG_UNIT_TEST(resolveResourceSimpleVulkan)
{
    runTestImpl(
        resolveResourceTestImpl<ResolveResourceSimple>,
        unitTestContext,
        DeviceType::Vulkan);
}
} // namespace gfx_test

#endif