| 1 |
/* |
| 2 |
* Copyright (C) 2018-2024 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#pragma once |
| 9 |
#include "shared/source/memory_manager/allocation_properties.h" |
| 10 |
#include "shared/source/memory_manager/memory_manager.h" |
| 11 |
|
| 12 |
#include "opencl/source/mem_obj/buffer.h" |
| 13 |
#include "opencl/source/sharings/d3d/d3d_sharing.h" |
| 14 |
|
| 15 |
namespace NEO { |
| 16 |
class Buffer; |
| 17 |
class Context; |
| 18 |
|
| 19 |
template <typename D3D> |
| 20 |
class D3DBuffer : public D3DSharing<D3D> { |
| 21 |
typedef typename D3D::D3DBufferObj D3DBufferObj; |
| 22 |
typedef typename D3D::D3DBufferDesc D3DBufferDesc; |
| 23 |
|
| 24 |
public: |
| 25 |
static Buffer *create(Context *context, D3DBufferObj *d3dBuffer, cl_mem_flags flags, cl_int *retCode) { |
| 26 |
ErrorCodeHelper err(retCode, CL_SUCCESS); |
| 27 |
auto sharingFcns = context->getSharing<D3DSharingFunctions<D3D>>(); |
| 28 |
void *sharedHandle = nullptr; |
| 29 |
|
| 30 |
D3DBufferDesc bufferDesc = {}; |
| 31 |
sharingFcns->getBufferDesc(&bufferDesc, d3dBuffer); |
| 32 |
|
| 33 |
bool sharedResource = false; |
| 34 |
D3DBufferObj *bufferStaging = nullptr; |
| 35 |
if (bufferDesc.MiscFlags & D3DResourceFlags::MISC_SHARED) { |
| 36 |
bufferStaging = d3dBuffer; |
| 37 |
sharedResource = true; |
| 38 |
} else { |
| 39 |
sharingFcns->createBuffer(&bufferStaging, bufferDesc.ByteWidth); |
| 40 |
} |
| 41 |
|
| 42 |
sharingFcns->getSharedHandle(bufferStaging, &sharedHandle); |
| 43 |
AllocationProperties properties = {context->getDevice(0)->getRootDeviceIndex(), |
| 44 |
false, // allocateMemory |
| 45 |
0, // size |
| 46 |
AllocationType::sharedBuffer, |
| 47 |
false, // isMultiStorageAllocation |
| 48 |
context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex())}; |
| 49 |
MemoryManager::OsHandleData osHandleData{sharedHandle}; |
| 50 |
auto alloc = context->getMemoryManager()->createGraphicsAllocationFromSharedHandle(osHandleData, properties, true, false, true, nullptr); |
| 51 |
|
| 52 |
auto d3dBufferObj = new D3DBuffer<D3D>(context, d3dBuffer, bufferStaging, sharedResource); |
| 53 |
|
| 54 |
auto rootDeviceIndex = alloc->getRootDeviceIndex(); |
| 55 |
auto multiGraphicsAllocation = MultiGraphicsAllocation(rootDeviceIndex); |
| 56 |
multiGraphicsAllocation.addAllocation(alloc); |
| 57 |
|
| 58 |
return Buffer::createSharedBuffer(context, flags, d3dBufferObj, std::move(multiGraphicsAllocation)); |
| 59 |
} |
| 60 |
~D3DBuffer() override = default; |
| 61 |
|
| 62 |
protected: |
| 63 |
D3DBuffer(Context *context, D3DBufferObj *d3dBuffer, D3DBufferObj *bufferStaging, bool sharedResource) |
| 64 |
: D3DSharing<D3D>(context, d3dBuffer, bufferStaging, 0, sharedResource){}; |
| 65 |
}; |
| 66 |
} // namespace NEO |