summaryrefslogtreecommitdiff
path: root/tools/gfx-unit-test/clear-texture-test.cpp
blob: 4ad82f31352601a3d45d79fde9e113d52afc1cf4 (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
#include "tools/unit-test/slang-unit-test.h"

#include "slang-gfx.h"
#include "gfx-test-util.h"
#include "tools/gfx-util/shader-cursor.h"
#include "source/core/slang-basic.h"

using namespace Slang;
using namespace gfx;

namespace gfx_test
{
    void clearTextureTestImpl(IDevice* device, UnitTestContext* context)
    {
        Slang::ComPtr<ITransientResourceHeap> transientHeap;
        ITransientResourceHeap::Desc transientHeapDesc = {};
        transientHeapDesc.constantBufferSize = 4096;
        GFX_CHECK_CALL_ABORT(
            device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef()));

        ITextureResource::Desc srcTexDesc = {};
        srcTexDesc.type = IResource::Type::Texture2D;
        srcTexDesc.numMipLevels = 1;
        srcTexDesc.arraySize = 1;
        srcTexDesc.size.width = 4;
        srcTexDesc.size.height = 4;
        srcTexDesc.size.depth = 1;
        srcTexDesc.defaultState = ResourceState::RenderTarget;
        srcTexDesc.allowedStates = ResourceStateSet(
            ResourceState::RenderTarget,
            ResourceState::CopySource,
            ResourceState::CopyDestination);
        srcTexDesc.format = Format::R32G32B32A32_FLOAT;

        Slang::ComPtr<ITextureResource> srcTexture;
        GFX_CHECK_CALL_ABORT(device->createTextureResource(
            srcTexDesc, nullptr, srcTexture.writeRef()));

        Slang::ComPtr<IResourceView> rtv;
        IResourceView::Desc rtvDesc = {};
        rtvDesc.type = IResourceView::Type::RenderTarget;
        rtvDesc.format = Format::R32G32B32A32_FLOAT;
        rtvDesc.renderTarget.shape = IResource::Type::Texture2D;
        rtvDesc.subresourceRange.layerCount = 1;
        rtvDesc.subresourceRange.mipLevelCount = 1;
        rtv = device->createTextureView(srcTexture, rtvDesc);

        {
            ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics};
            auto queue = device->createCommandQueue(queueDesc);

            auto commandBuffer = transientHeap->createCommandBuffer();
            auto resourceEncoder = commandBuffer->encodeResourceCommands();
            ClearValue clearValue = {};
            clearValue.color.floatValues[0] = 0.5f;
            clearValue.color.floatValues[1] = 1.0f;
            clearValue.color.floatValues[2] = 0.2f;
            clearValue.color.floatValues[3] = 0.1f;
            resourceEncoder->clearResourceView(rtv, &clearValue, ClearResourceViewFlags::FloatClearValues);
            resourceEncoder->textureBarrier(
                srcTexture, ResourceState::RenderTarget, ResourceState::CopySource);
            resourceEncoder->endEncoding();

            commandBuffer->close();
            queue->executeCommandBuffer(commandBuffer);

            queue->waitOnHost();

            Slang::ComPtr<ISlangBlob> blob;
            size_t rowPitch, pixelSize;
            device->readTextureResource(
                srcTexture,
                ResourceState::CopySource,
                blob.writeRef(),
                &rowPitch,
                &pixelSize);
            float* data = (float*)blob->getBufferPointer();
            for (int i = 0; i < 4; i++)
            {
                SLANG_CHECK(data[i] == clearValue.color.floatValues[i]);
            }
        }
    }

    SLANG_UNIT_TEST(clearTextureTestVulkan)
    {
        runTestImpl(clearTextureTestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan);
    }
}