1 /*
2  * Copyright (C) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  */
7 
8 #pragma once
9 
10 #include "shared/source/memory_manager/multi_graphics_allocation.h"
11 
12 #include "opencl/source/mem_obj/buffer.h"
13 #include "opencl/source/mem_obj/image.h"
14 #include "opencl/source/mem_obj/mem_obj.h"
15 
16 namespace NEO {
17 enum class TransferDirection {
18     HostToHost,
19     HostToLocal,
20     LocalToHost,
21     LocalToLocal,
22 };
23 
24 struct CsrSelectionArgs {
25     struct Resource {
26         bool isLocal = false;
27         const GraphicsAllocation *allocation = nullptr;
28         const Image *image = nullptr;
29         const size_t *imageOrigin = nullptr;
30     };
31 
32     cl_command_type cmdType;
33     const size_t *size = nullptr;
34     Resource srcResource;
35     Resource dstResource;
36     TransferDirection direction;
37 
CsrSelectionArgsCsrSelectionArgs38     CsrSelectionArgs(cl_command_type cmdType, const size_t *size)
39         : cmdType(cmdType),
40           size(size),
41           direction(TransferDirection::HostToHost) {}
42 
43     template <typename ResourceType>
CsrSelectionArgsCsrSelectionArgs44     CsrSelectionArgs(cl_command_type cmdType, ResourceType *src, ResourceType *dst, uint32_t rootDeviceIndex, const size_t *size)
45         : cmdType(cmdType),
46           size(size) {
47         if (src) {
48             processResource(*src, rootDeviceIndex, this->srcResource);
49         }
50         if (dst) {
51             processResource(*dst, rootDeviceIndex, this->dstResource);
52         }
53         this->direction = createTransferDirection(srcResource.isLocal, dstResource.isLocal);
54     }
55 
CsrSelectionArgsCsrSelectionArgs56     CsrSelectionArgs(cl_command_type cmdType, Image *src, Image *dst, uint32_t rootDeviceIndex, const size_t *size, const size_t *srcOrigin, const size_t *dstOrigin)
57         : CsrSelectionArgs(cmdType, src, dst, rootDeviceIndex, size) {
58         if (src) {
59             srcResource.imageOrigin = srcOrigin;
60         }
61         if (dst) {
62             dstResource.imageOrigin = dstOrigin;
63         }
64     }
65 
processResourceCsrSelectionArgs66     static void processResource(const Image &image, uint32_t rootDeviceIndex, Resource &outResource) {
67         processResource(image.getMultiGraphicsAllocation(), rootDeviceIndex, outResource);
68         outResource.image = &image;
69     }
70 
processResourceCsrSelectionArgs71     static void processResource(const Buffer &buffer, uint32_t rootDeviceIndex, Resource &outResource) {
72         processResource(buffer.getMultiGraphicsAllocation(), rootDeviceIndex, outResource);
73     }
74 
processResourceCsrSelectionArgs75     static void processResource(const MultiGraphicsAllocation &multiGfxAlloc, uint32_t rootDeviceIndex, Resource &outResource) {
76         processResource(*multiGfxAlloc.getGraphicsAllocation(rootDeviceIndex), rootDeviceIndex, outResource);
77     }
78 
processResourceCsrSelectionArgs79     static void processResource(const GraphicsAllocation &gfxAlloc, uint32_t rootDeviceIndex, Resource &outResource) {
80         outResource.allocation = &gfxAlloc;
81         outResource.isLocal = gfxAlloc.isAllocatedInLocalMemoryPool();
82     }
83 
createTransferDirectionCsrSelectionArgs84     static inline TransferDirection createTransferDirection(bool srcLocal, bool dstLocal) {
85         if (srcLocal) {
86             if (dstLocal) {
87                 return TransferDirection::LocalToLocal;
88             } else {
89                 return TransferDirection::LocalToHost;
90             }
91         } else {
92             if (dstLocal) {
93                 return TransferDirection::HostToLocal;
94             } else {
95                 return TransferDirection::HostToHost;
96             }
97         }
98     }
99 };
100 
101 } // namespace NEO
102