Skip to content

Commit 4c4e682

Browse files
Set correct offset for images created from buffer
- images from subbuffer with non-zero offset relative to parent buffer didn't have correct Surface Address offset Change-Id: I6ae2b87f8c9d19e40ec14a29b5eadc7401db18ad
1 parent 63edbcf commit 4c4e682

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

runtime/mem_obj/image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,12 @@ Image *Image::create(Context *context,
206206
hostPtrToSet = const_cast<void *>(hostPtr);
207207
parentBuffer->incRefInternal();
208208
Gmm::queryImgFromBufferParams(imgInfo, memory);
209+
210+
auto bufferOffset = static_cast<uint32_t>(parentBuffer->getOffset());
211+
if (bufferOffset != 0) {
212+
imgInfo.offset = bufferOffset;
213+
}
214+
209215
if (memoryManager->peekVirtualPaddingSupport() && (imageDesc->image_type == CL_MEM_OBJECT_IMAGE2D)) {
210216
// Retrieve sizes from GMM and apply virtual padding if buffer storage is not big enough
211217
auto queryGmmImgInfo(imgInfo);

unit_tests/mem_obj/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ set(IGDRCL_SRCS_tests_mem_obj
2727
${CMAKE_CURRENT_SOURCE_DIR}/destructor_callback_tests.cpp
2828
${CMAKE_CURRENT_SOURCE_DIR}/get_mem_object_info_subbufer_tests.cpp
2929
${CMAKE_CURRENT_SOURCE_DIR}/get_mem_object_info_tests.cpp
30+
${CMAKE_CURRENT_SOURCE_DIR}/image_from_subbuffer_tests.cpp
3031
${CMAKE_CURRENT_SOURCE_DIR}/image1d_tests.cpp
3132
${CMAKE_CURRENT_SOURCE_DIR}/image2d_from_buffer_tests.cpp
3233
${CMAKE_CURRENT_SOURCE_DIR}/image2d_tests.cpp
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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/mem_obj/image.h"
24+
#include "runtime/mem_obj/buffer.h"
25+
#include "runtime/helpers/aligned_memory.h"
26+
#include "unit_tests/fixtures/device_fixture.h"
27+
#include "unit_tests/mocks/mock_context.h"
28+
#include "test.h"
29+
30+
#include <memory>
31+
using namespace OCLRT;
32+
33+
// Tests for cl_khr_image2d_from_buffer
34+
class ImageFromSubBufferTest : public DeviceFixture, public ::testing::Test {
35+
public:
36+
ImageFromSubBufferTest() {}
37+
38+
protected:
39+
void SetUp() override {
40+
imageFormat.image_channel_data_type = CL_UNORM_INT8;
41+
imageFormat.image_channel_order = CL_RGBA;
42+
43+
imageDesc.image_array_size = 0;
44+
imageDesc.image_depth = 0;
45+
imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
46+
imageDesc.image_height = 128 / 2;
47+
imageDesc.image_width = 256 / 2;
48+
imageDesc.num_mip_levels = 0;
49+
imageDesc.image_row_pitch = 0;
50+
imageDesc.image_slice_pitch = 0;
51+
imageDesc.num_samples = 0;
52+
53+
size = 128 * 256 * 4;
54+
hostPtr = alignedMalloc(size, 16);
55+
ASSERT_NE(nullptr, hostPtr);
56+
57+
parentBuffer = clCreateBuffer(&context, CL_MEM_USE_HOST_PTR | CL_MEM_READ_WRITE, size, hostPtr, &retVal);
58+
ASSERT_EQ(CL_SUCCESS, retVal);
59+
60+
const cl_buffer_region region = {size / 2, size / 2};
61+
62+
subBuffer = clCreateSubBuffer(parentBuffer, CL_MEM_READ_WRITE, CL_BUFFER_CREATE_TYPE_REGION,
63+
reinterpret_cast<const void *>(&region), &retVal);
64+
ASSERT_EQ(CL_SUCCESS, retVal);
65+
66+
imageDesc.mem_object = subBuffer;
67+
ASSERT_NE(nullptr, imageDesc.mem_object);
68+
}
69+
void TearDown() override {
70+
clReleaseMemObject(subBuffer);
71+
clReleaseMemObject(parentBuffer);
72+
alignedFree(hostPtr);
73+
}
74+
75+
Image *createImage() {
76+
cl_mem_flags flags = CL_MEM_READ_ONLY;
77+
auto surfaceFormat = (SurfaceFormatInfo *)Image::getSurfaceFormatFromTable(flags, &imageFormat);
78+
return Image::create(&context, flags, surfaceFormat, &imageDesc, NULL, retVal);
79+
}
80+
cl_image_format imageFormat;
81+
cl_image_desc imageDesc;
82+
cl_int retVal = CL_SUCCESS;
83+
MockContext context;
84+
void *hostPtr;
85+
size_t size;
86+
cl_mem parentBuffer;
87+
cl_mem subBuffer;
88+
};
89+
90+
TEST_F(ImageFromSubBufferTest, CreateImage2dFromSubBufferWithOffset) {
91+
std::unique_ptr<Image> imageFromSubBuffer(createImage());
92+
EXPECT_NE(nullptr, imageFromSubBuffer);
93+
94+
SurfaceOffsets surfaceOffsets = {0};
95+
imageFromSubBuffer->getSurfaceOffsets(surfaceOffsets);
96+
97+
uint32_t offsetExpected = static_cast<uint32_t>(size) / 2;
98+
99+
EXPECT_EQ(offsetExpected, surfaceOffsets.offset);
100+
EXPECT_EQ(0u, surfaceOffsets.xOffset);
101+
EXPECT_EQ(0u, surfaceOffsets.yOffset);
102+
EXPECT_EQ(0u, surfaceOffsets.yOffsetForUVplane);
103+
}

0 commit comments

Comments
 (0)