summaryrefslogtreecommitdiff
path: root/tools/gfx-unit-test/get-shared-handle.cpp
blob: 7c091abc84f5c687b58ec918c7d41544db10c0b2 (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
#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 gfx;

namespace gfx_test
{
    void sharedHandleTestImpl(IDevice* srcDevice, IDevice* dstDevice, UnitTestContext* context)
    {
        const int numberCount = 4;
        float initialData[] = { 0.0f, 1.0f, 2.0f, 3.0f };
        IBufferResource::Desc bufferDesc = {};
        bufferDesc.sizeInBytes = numberCount * sizeof(float);
        bufferDesc.format = gfx::Format::Unknown;
        bufferDesc.elementSize = sizeof(float);
        bufferDesc.allowedStates = ResourceStateSet(
            ResourceState::ShaderResource,
            ResourceState::UnorderedAccess,
            ResourceState::CopyDestination,
            ResourceState::CopySource);
        bufferDesc.defaultState = ResourceState::UnorderedAccess;
        bufferDesc.cpuAccessFlags = AccessFlag::Write | AccessFlag::Read;
        bufferDesc.isShared = true;

        ComPtr<IBufferResource> srcBuffer;
        GFX_CHECK_CALL_ABORT(srcDevice->createBufferResource(
            bufferDesc,
            (void*)initialData,
            srcBuffer.writeRef()));

        InteropHandle sharedHandle;
        GFX_CHECK_CALL_ABORT(srcBuffer->getSharedHandle(&sharedHandle));
        ComPtr<IBufferResource> dstBuffer;
        GFX_CHECK_CALL_ABORT(dstDevice->createBufferFromSharedHandle(sharedHandle, bufferDesc, dstBuffer.writeRef()));

        InteropHandle testHandle;
        GFX_CHECK_CALL_ABORT(dstBuffer->getNativeResourceHandle(&testHandle));
        IBufferResource::Desc* testDesc = dstBuffer->getDesc();
        SLANG_CHECK(testDesc->elementSize == sizeof(float));
        SLANG_CHECK(testDesc->sizeInBytes == numberCount * sizeof(float));
        compareComputeResult(dstDevice, dstBuffer, Slang::makeArray<float>(0.0f, 1.0f, 2.0f, 3.0f));
    }

    void sharedHandleTestAPI(UnitTestContext* context, Slang::RenderApiFlag::Enum srcApi, Slang::RenderApiFlag::Enum dstApi)
    {
        auto srcDevice = createTestingDevice(context, srcApi);
        auto dstDevice = createTestingDevice(context, dstApi);
        if (!srcDevice || !dstDevice)
        {
            SLANG_IGNORE_TEST;
        }

        sharedHandleTestImpl(srcDevice, dstDevice, context);
    }
#if 0
    // Temporarily disabled due to inconsistent test results on TC
    SLANG_UNIT_TEST(sharedHandleD3D12ToCUDA)
    {
        sharedHandleTestAPI(unitTestContext, Slang::RenderApiFlag::D3D12, Slang::RenderApiFlag::CUDA);
    }
#endif
}