Skip to content

Commit 79a0e32

Browse files
Enable transforming image 3d to image 2d array
Change-Id: I8fdc6899780481bdebeaf858a330e9dea822bda3
1 parent 0a97dfb commit 79a0e32

24 files changed

+867
-6
lines changed

runtime/kernel/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
set(RUNTIME_SRCS_KERNEL
2222
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
2323
${CMAKE_CURRENT_SOURCE_DIR}/dynamic_kernel_info.h
24+
${CMAKE_CURRENT_SOURCE_DIR}/image_transformer.cpp
25+
${CMAKE_CURRENT_SOURCE_DIR}/image_transformer.h
2426
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp
2527
${CMAKE_CURRENT_SOURCE_DIR}/kernel.h
2628
${CMAKE_CURRENT_SOURCE_DIR}/kernel.inl
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#include "runtime/helpers/ptr_math.h"
24+
#include "runtime/kernel/image_transformer.h"
25+
#include "runtime/mem_obj/image.h"
26+
#include "runtime/program/kernel_info.h"
27+
28+
namespace OCLRT {
29+
void ImageTransformer::registerImage3d(uint32_t argIndex) {
30+
if (std::find(argIndexes.begin(), argIndexes.end(), argIndex) == argIndexes.end()) {
31+
argIndexes.push_back(argIndex);
32+
}
33+
}
34+
void ImageTransformer::transformImagesTo2dArray(const KernelInfo &kernelInfo, const std::vector<Kernel::SimpleKernelArgInfo> &kernelArguments, void *ssh) {
35+
for (auto const &argIndex : argIndexes) {
36+
if (kernelInfo.kernelArgInfo.at(argIndex).isTransformable) {
37+
auto clMemObj = *(static_cast<const cl_mem *>(kernelArguments.at(argIndex).value));
38+
auto image = castToObjectOrAbort<Image>(clMemObj);
39+
auto surfaceState = ptrOffset(ssh, kernelInfo.kernelArgInfo.at(argIndex).offsetHeap);
40+
image->transformImage3dTo2dArray(surfaceState);
41+
}
42+
}
43+
transformed = true;
44+
}
45+
void ImageTransformer::transformImagesTo3d(const KernelInfo &kernelInfo, const std::vector<Kernel::SimpleKernelArgInfo> &kernelArguments, void *ssh) {
46+
for (auto const &argIndex : argIndexes) {
47+
auto clMemObj = *(static_cast<const cl_mem *>(kernelArguments.at(argIndex).value));
48+
auto image = castToObjectOrAbort<Image>(clMemObj);
49+
auto surfaceState = ptrOffset(ssh, kernelInfo.kernelArgInfo.at(argIndex).offsetHeap);
50+
image->transformImage2dArrayTo3d(surfaceState);
51+
}
52+
transformed = false;
53+
}
54+
bool ImageTransformer::didTransform() const {
55+
return transformed;
56+
}
57+
bool ImageTransformer::hasRegisteredImages3d() const {
58+
return !argIndexes.empty();
59+
}
60+
}

runtime/kernel/image_transformer.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a
5+
* copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation
7+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8+
* and/or sell copies of the Software, and to permit persons to whom the
9+
* Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included
12+
* in all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18+
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19+
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20+
* OTHER DEALINGS IN THE SOFTWARE.
21+
*/
22+
23+
#pragma once
24+
#include "runtime/kernel/kernel.h"
25+
26+
namespace OCLRT {
27+
28+
class ImageTransformer {
29+
public:
30+
void registerImage3d(uint32_t argIndex);
31+
void transformImagesTo2dArray(const KernelInfo &kernelInfo, const std::vector<Kernel::SimpleKernelArgInfo> &kernelArguments, void *ssh);
32+
void transformImagesTo3d(const KernelInfo &kernelInfo, const std::vector<Kernel::SimpleKernelArgInfo> &kernelArguments, void *ssh);
33+
bool didTransform() const;
34+
bool hasRegisteredImages3d() const;
35+
36+
protected:
37+
bool transformed = false;
38+
std::vector<uint32_t> argIndexes;
39+
};
40+
}

runtime/kernel/kernel.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "runtime/mem_obj/buffer.h"
4242
#include "runtime/mem_obj/image.h"
4343
#include "runtime/mem_obj/pipe.h"
44+
#include "runtime/kernel/image_transformer.h"
4445
#include "runtime/memory_manager/memory_manager.h"
4546
#include "runtime/memory_manager/surface.h"
4647
#include "runtime/os_interface/debug_settings_manager.h"
@@ -105,6 +106,7 @@ Kernel::Kernel(Program *programArg, const KernelInfo &kernelInfoArg, const Devic
105106
kernelReflectionSurface(nullptr),
106107
usingSharedObjArgs(false) {
107108
program->retain();
109+
imageTransformer.reset(new ImageTransformer);
108110
}
109111

110112
Kernel::~Kernel() {
@@ -773,6 +775,7 @@ cl_int Kernel::setArg(uint32_t argIndex, size_t argSize, const void *argVal) {
773775
patchedArgumentsNum++;
774776
kernelArguments[argIndex].isPatched = true;
775777
}
778+
resolveArgs();
776779
}
777780
return retVal;
778781
}
@@ -1210,6 +1213,10 @@ cl_int Kernel::setArgImageWithMipLevel(uint32_t argIndex,
12101213
auto &imageDesc = pImage->getImageDesc();
12111214
auto &imageFormat = pImage->getImageFormat();
12121215

1216+
if (imageDesc.image_type == CL_MEM_OBJECT_IMAGE3D) {
1217+
imageTransformer->registerImage3d(argIndex);
1218+
}
1219+
12131220
patch<uint32_t, size_t>(imageDesc.image_width, crossThreadData, kernelArgInfo.offsetImgWidth);
12141221
patch<uint32_t, size_t>(imageDesc.image_height, crossThreadData, kernelArgInfo.offsetImgHeight);
12151222
patch<uint32_t, size_t>(imageDesc.image_depth, crossThreadData, kernelArgInfo.offsetImgDepth);
@@ -2045,4 +2052,31 @@ cl_int Kernel::checkCorrectImageAccessQualifier(cl_uint argIndex,
20452052
}
20462053
return CL_SUCCESS;
20472054
}
2055+
2056+
void Kernel::resolveArgs() {
2057+
if (!Kernel::isPatched() || !imageTransformer->hasRegisteredImages3d() || !canTransformImages())
2058+
return;
2059+
bool canTransformImageTo2dArray = true;
2060+
for (uint32_t i = 0; i < patchedArgumentsNum; i++) {
2061+
if (kernelInfo.kernelArgInfo.at(i).isSampler) {
2062+
auto clSamplerObj = *(static_cast<const cl_sampler *>(kernelArguments.at(i).value));
2063+
auto sampler = castToObjectOrAbort<Sampler>(clSamplerObj);
2064+
if (sampler->isTransformable()) {
2065+
canTransformImageTo2dArray = true;
2066+
} else {
2067+
canTransformImageTo2dArray = false;
2068+
break;
2069+
}
2070+
}
2071+
}
2072+
if (canTransformImageTo2dArray) {
2073+
imageTransformer->transformImagesTo2dArray(kernelInfo, kernelArguments, getSurfaceStateHeap());
2074+
} else if (imageTransformer->didTransform()) {
2075+
imageTransformer->transformImagesTo3d(kernelInfo, kernelArguments, getSurfaceStateHeap());
2076+
}
2077+
}
2078+
2079+
bool Kernel::canTransformImages() const {
2080+
return device.getHardwareInfo().pPlatform->eRenderCoreFamily >= IGFX_GEN9_CORE;
2081+
}
20482082
} // namespace OCLRT

runtime/kernel/kernel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
namespace OCLRT {
3636
struct CompletionStamp;
3737
class GraphicsAllocation;
38+
class ImageTransformer;
3839
class Surface;
3940
class PrintfHandler;
4041

@@ -126,6 +127,7 @@ class Kernel : public BaseObject<_cl_kernel> {
126127

127128
cl_int cloneKernel(Kernel *pSourceKernel);
128129

130+
MOCKABLE_VIRTUAL bool canTransformImages() const;
129131
MOCKABLE_VIRTUAL bool isPatched() const;
130132

131133
// API entry points
@@ -455,6 +457,8 @@ class Kernel : public BaseObject<_cl_kernel> {
455457

456458
void patchBlocksCurbeWithConstantValues();
457459

460+
void resolveArgs();
461+
458462
Program *program;
459463
Context *context;
460464
const Device &device;
@@ -481,5 +485,6 @@ class Kernel : public BaseObject<_cl_kernel> {
481485
uint32_t patchedArgumentsNum = 0;
482486

483487
std::vector<PatchInfoData> patchInfoDataList;
488+
std::unique_ptr<ImageTransformer> imageTransformer;
484489
};
485490
} // namespace OCLRT

runtime/mem_obj/image.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ class Image : public MemObj {
182182
const McsSurfaceInfo &getMcsSurfaceInfo() { return mcsSurfaceInfo; }
183183
size_t calculateOffsetForMapping(const MemObjOffsetArray &origin) const override;
184184

185+
virtual void transformImage2dArrayTo3d(void *memory) = 0;
186+
virtual void transformImage3dTo2dArray(void *memory) = 0;
187+
185188
const bool isTiledImage;
186189

187190
protected:
@@ -272,7 +275,8 @@ class ImageHw : public Image {
272275
void setMediaSurfaceRotation(void *memory) override;
273276
void setSurfaceMemoryObjectControlStateIndexToMocsTable(void *memory, uint32_t value) override;
274277
void appendSurfaceStateParams(RENDER_SURFACE_STATE *surfaceState);
275-
278+
void transformImage2dArrayTo3d(void *memory) override;
279+
void transformImage3dTo2dArray(void *memory) override;
276280
static Image *create(Context *context,
277281
cl_mem_flags flags,
278282
size_t size,

runtime/mem_obj/image.inl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,4 +250,21 @@ void ImageHw<GfxFamily>::setMediaImageArg(void *memory) {
250250

251251
surfaceState->setSurfaceBaseAddress(getGraphicsAllocation()->getGpuAddress() + this->surfaceOffsets.offset);
252252
}
253+
254+
template <typename GfxFamily>
255+
void ImageHw<GfxFamily>::transformImage2dArrayTo3d(void *memory) {
256+
DEBUG_BREAK_IF(imageDesc.image_type != CL_MEM_OBJECT_IMAGE3D);
257+
using SURFACE_TYPE = typename RENDER_SURFACE_STATE::SURFACE_TYPE;
258+
auto surfaceState = reinterpret_cast<RENDER_SURFACE_STATE *>(memory);
259+
surfaceState->setSurfaceType(SURFACE_TYPE::SURFACE_TYPE_SURFTYPE_3D);
260+
surfaceState->setSurfaceArray(false);
261+
}
262+
template <typename GfxFamily>
263+
void ImageHw<GfxFamily>::transformImage3dTo2dArray(void *memory) {
264+
DEBUG_BREAK_IF(imageDesc.image_type != CL_MEM_OBJECT_IMAGE3D);
265+
using SURFACE_TYPE = typename RENDER_SURFACE_STATE::SURFACE_TYPE;
266+
auto surfaceState = reinterpret_cast<RENDER_SURFACE_STATE *>(memory);
267+
surfaceState->setSurfaceType(SURFACE_TYPE::SURFACE_TYPE_SURFTYPE_2D);
268+
surfaceState->setSurfaceArray(true);
269+
}
253270
} // namespace OCLRT

runtime/program/kernel_arg_info.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -69,6 +69,7 @@ struct KernelArgInfo {
6969
uint32_t offsetBufferOffset = undefinedOffset;
7070

7171
bool needPatch = false;
72+
bool isTransformable = false;
7273

7374
cl_kernel_arg_access_qualifier accessQualifier = CL_KERNEL_ARG_ACCESS_NONE;
7475
cl_kernel_arg_address_qualifier addressQualifier = CL_KERNEL_ARG_ADDRESS_GLOBAL;

runtime/program/kernel_info.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -303,6 +303,8 @@ void KernelInfo::storeKernelArgument(
303303
kernelArgInfo[argNum].accessQualifier = pImageMemObjKernelArg->Writeable
304304
? CL_KERNEL_ARG_ACCESS_READ_WRITE
305305
: CL_KERNEL_ARG_ACCESS_READ_ONLY;
306+
307+
kernelArgInfo[argNum].isTransformable = pImageMemObjKernelArg->Transformable != 0;
306308
patchInfo.imageMemObjKernelArgs.push_back(pImageMemObjKernelArg);
307309
}
308310

runtime/sampler/sampler.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -187,4 +187,11 @@ cl_int Sampler::getInfo(cl_sampler_info paramName, size_t paramValueSize,
187187

188188
return retVal;
189189
}
190+
191+
bool Sampler::isTransformable() const {
192+
return addressingMode == CL_ADDRESS_CLAMP_TO_EDGE &&
193+
filterMode == CL_FILTER_NEAREST &&
194+
normalizedCoordinates == CL_FALSE;
195+
}
196+
190197
} // namespace OCLRT

0 commit comments

Comments
 (0)