Skip to content

Commit d91dea8

Browse files
committed
refactor buffer and image creation with properties
1 parent e08431a commit d91dea8

2 files changed

Lines changed: 167 additions & 147 deletions

File tree

include/CL/opencl.hpp

Lines changed: 122 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,14 +4466,10 @@ class Buffer : public Memory
44664466
{
44674467
cl_int error;
44684468

4469-
if (properties.empty()) {
4470-
object_ = CL_(clCreateBufferWithProperties)(context(), nullptr, flags,
4471-
size, host_ptr, &error);
4472-
}
4473-
else {
4474-
object_ = CL_(clCreateBufferWithProperties)(
4475-
context(), properties.data(), flags, size, host_ptr, &error);
4476-
}
4469+
object_ = CL_(clCreateBufferWithProperties)(
4470+
context(),
4471+
properties.empty() ? nullptr : properties.data(),
4472+
flags, size, host_ptr, &error);
44774473

44784474
detail::errHandler(error, __CREATE_BUFFER_ERR);
44794475
if (err != nullptr) {
@@ -4978,26 +4974,23 @@ class Image1D : public Image
49784974
*/
49794975
Image1D(const Context &context, const vector<cl_mem_properties> &properties,
49804976
cl_mem_flags flags, ImageFormat format, size_type width,
4981-
void *host_ptr = nullptr, cl_int *err = nullptr) {
4982-
cl_int error;
4977+
void *host_ptr = nullptr, cl_int *err = nullptr)
4978+
{
4979+
cl_int error;
49834980

4984-
cl_image_desc desc = {};
4985-
desc.image_type = CL_MEM_OBJECT_IMAGE1D;
4986-
desc.image_width = width;
4981+
cl_image_desc desc = {};
4982+
desc.image_type = CL_MEM_OBJECT_IMAGE1D;
4983+
desc.image_width = width;
49874984

4988-
if (properties.empty()) {
49894985
object_ = CL_(clCreateImageWithProperties)(
4990-
context(), nullptr, flags, &format, &desc, host_ptr, &error);
4991-
} else {
4992-
object_ =
4993-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
4994-
&format, &desc, host_ptr, &error);
4995-
}
4996-
4997-
detail::errHandler(error, __CREATE_IMAGE_ERR);
4998-
if (err != nullptr) {
4999-
*err = error;
5000-
}
4986+
context(),
4987+
properties.empty() ? nullptr : properties.data(),
4988+
flags, &format, &desc, host_ptr, &error);
4989+
4990+
detail::errHandler(error, __CREATE_IMAGE_ERR);
4991+
if (err != nullptr) {
4992+
*err = error;
4993+
}
50014994
}
50024995
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
50034996

@@ -5074,27 +5067,24 @@ class Image1DBuffer : public Image
50745067
Image1DBuffer(const Context &context,
50755068
const vector<cl_mem_properties> &properties,
50765069
cl_mem_flags flags, ImageFormat format, size_type width,
5077-
const Buffer &buffer, cl_int *err = nullptr) {
5078-
cl_int error;
5070+
const Buffer &buffer, cl_int *err = nullptr)
5071+
{
5072+
cl_int error;
50795073

5080-
cl_image_desc desc = {};
5081-
desc.image_type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
5082-
desc.image_width = width;
5083-
desc.buffer = buffer();
5074+
cl_image_desc desc = {};
5075+
desc.image_type = CL_MEM_OBJECT_IMAGE1D_BUFFER;
5076+
desc.image_width = width;
5077+
desc.buffer = buffer();
50845078

5085-
if (properties.empty()) {
50865079
object_ = CL_(clCreateImageWithProperties)(
5087-
context(), nullptr, flags, &format, &desc, nullptr, &error);
5088-
} else {
5089-
object_ =
5090-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5091-
&format, &desc, nullptr, &error);
5092-
}
5093-
5094-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5095-
if (err != nullptr) {
5096-
*err = error;
5097-
}
5080+
context(),
5081+
properties.empty() ? nullptr : properties.data(),
5082+
flags, &format, &desc, nullptr, &error);
5083+
5084+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5085+
if (err != nullptr) {
5086+
*err = error;
5087+
}
50985088
}
50995089
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
51005090

@@ -5170,28 +5160,25 @@ class Image1DArray : public Image
51705160
const vector<cl_mem_properties> &properties,
51715161
cl_mem_flags flags, ImageFormat format, size_type arraySize,
51725162
size_type width, size_type rowPitch = 0,
5173-
void *host_ptr = nullptr, cl_int *err = nullptr) {
5174-
cl_int error;
5163+
void *host_ptr = nullptr, cl_int *err = nullptr)
5164+
{
5165+
cl_int error;
51755166

5176-
cl_image_desc desc = {};
5177-
desc.image_type = CL_MEM_OBJECT_IMAGE1D_ARRAY;
5178-
desc.image_width = width;
5179-
desc.image_array_size = arraySize;
5180-
desc.image_row_pitch = rowPitch;
5167+
cl_image_desc desc = {};
5168+
desc.image_type = CL_MEM_OBJECT_IMAGE1D_ARRAY;
5169+
desc.image_width = width;
5170+
desc.image_array_size = arraySize;
5171+
desc.image_row_pitch = rowPitch;
51815172

5182-
if (properties.empty()) {
51835173
object_ = CL_(clCreateImageWithProperties)(
5184-
context(), nullptr, flags, &format, &desc, host_ptr, &error);
5185-
} else {
5186-
object_ =
5187-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5188-
&format, &desc, host_ptr, &error);
5189-
}
5190-
5191-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5192-
if (err != nullptr) {
5193-
*err = error;
5194-
}
5174+
context(),
5175+
properties.empty() ? nullptr : properties.data(),
5176+
flags, &format, &desc, host_ptr, &error);
5177+
5178+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5179+
if (err != nullptr) {
5180+
*err = error;
5181+
}
51955182
}
51965183
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
51975184

@@ -5411,28 +5398,25 @@ class Image2D : public Image
54115398
Image2D(const Context &context, const vector<cl_mem_properties> &properties,
54125399
cl_mem_flags flags, ImageFormat format, size_type width,
54135400
size_type height, size_type row_pitch = 0, void *host_ptr = nullptr,
5414-
cl_int *err = nullptr) {
5415-
cl_int error;
5401+
cl_int *err = nullptr)
5402+
{
5403+
cl_int error;
54165404

5417-
cl_image_desc desc = {};
5418-
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
5419-
desc.image_width = width;
5420-
desc.image_height = height;
5421-
desc.image_row_pitch = row_pitch;
5405+
cl_image_desc desc = {};
5406+
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
5407+
desc.image_width = width;
5408+
desc.image_height = height;
5409+
desc.image_row_pitch = row_pitch;
54225410

5423-
if (properties.empty()) {
54245411
object_ = CL_(clCreateImageWithProperties)(
5425-
context(), nullptr, flags, &format, &desc, host_ptr, &error);
5426-
} else {
5427-
object_ =
5428-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5429-
&format, &desc, host_ptr, &error);
5430-
}
5431-
5432-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5433-
if (err != nullptr) {
5434-
*err = error;
5435-
}
5412+
context(),
5413+
properties.empty() ? nullptr : properties.data(),
5414+
flags, &format, &desc, host_ptr, &error);
5415+
5416+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5417+
if (err != nullptr) {
5418+
*err = error;
5419+
}
54365420
}
54375421

54385422
/*! \brief Constructs a Image2D with specified properties.
@@ -5447,29 +5431,26 @@ class Image2D : public Image
54475431
Image2D(const Context &context, const vector<cl_mem_properties> &properties,
54485432
cl_mem_flags flags, ImageFormat format, const Buffer &buffer,
54495433
size_type width, size_type height, size_type row_pitch = 0,
5450-
cl_int *err = nullptr) {
5451-
cl_int error;
5434+
cl_int *err = nullptr)
5435+
{
5436+
cl_int error;
54525437

5453-
cl_image_desc desc = {};
5454-
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
5455-
desc.image_width = width;
5456-
desc.image_height = height;
5457-
desc.image_row_pitch = row_pitch;
5458-
desc.buffer = buffer();
5438+
cl_image_desc desc = {};
5439+
desc.image_type = CL_MEM_OBJECT_IMAGE2D;
5440+
desc.image_width = width;
5441+
desc.image_height = height;
5442+
desc.image_row_pitch = row_pitch;
5443+
desc.buffer = buffer();
54595444

5460-
if (properties.empty()) {
54615445
object_ = CL_(clCreateImageWithProperties)(
5462-
context(), nullptr, flags, &format, &desc, nullptr, &error);
5463-
} else {
5464-
object_ =
5465-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5466-
&format, &desc, nullptr, &error);
5467-
}
5468-
5469-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5470-
if (err != nullptr) {
5471-
*err = error;
5472-
}
5446+
context(),
5447+
properties.empty() ? nullptr : properties.data(),
5448+
flags, &format, &desc, nullptr, &error);
5449+
5450+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5451+
if (err != nullptr) {
5452+
*err = error;
5453+
}
54735454
}
54745455

54755456
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
@@ -5628,30 +5609,27 @@ class Image2DArray : public Image
56285609
cl_mem_flags flags, ImageFormat format, size_type arraySize,
56295610
size_type width, size_type height, size_type rowPitch = 0,
56305611
size_type slicePitch = 0, void *host_ptr = nullptr,
5631-
cl_int *err = nullptr) {
5632-
cl_int error;
5633-
5634-
cl_image_desc desc = {};
5635-
desc.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY;
5636-
desc.image_width = width;
5637-
desc.image_height = height;
5638-
desc.image_array_size = arraySize;
5639-
desc.image_row_pitch = rowPitch;
5640-
desc.image_slice_pitch = slicePitch;
5641-
5642-
if (properties.empty()) {
5612+
cl_int *err = nullptr)
5613+
{
5614+
cl_int error;
5615+
5616+
cl_image_desc desc = {};
5617+
desc.image_type = CL_MEM_OBJECT_IMAGE2D_ARRAY;
5618+
desc.image_width = width;
5619+
desc.image_height = height;
5620+
desc.image_array_size = arraySize;
5621+
desc.image_row_pitch = rowPitch;
5622+
desc.image_slice_pitch = slicePitch;
5623+
56435624
object_ = CL_(clCreateImageWithProperties)(
5644-
context(), nullptr, flags, &format, &desc, host_ptr, &error);
5645-
} else {
5646-
object_ =
5647-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5648-
&format, &desc, host_ptr, &error);
5649-
}
5650-
5651-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5652-
if (err != nullptr) {
5653-
*err = error;
5654-
}
5625+
context(),
5626+
properties.empty() ? nullptr : properties.data(),
5627+
flags, &format, &desc, host_ptr, &error);
5628+
5629+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5630+
if (err != nullptr) {
5631+
*err = error;
5632+
}
56555633
}
56565634
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
56575635

@@ -5770,30 +5748,27 @@ class Image3D : public Image
57705748
cl_mem_flags flags, ImageFormat format, size_type width,
57715749
size_type height, size_type depth, size_type row_pitch = 0,
57725750
size_type slice_pitch = 0, void *host_ptr = nullptr,
5773-
cl_int *err = nullptr) {
5774-
cl_int error;
5775-
5776-
cl_image_desc desc = {};
5777-
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
5778-
desc.image_width = width;
5779-
desc.image_height = height;
5780-
desc.image_depth = depth;
5781-
desc.image_row_pitch = row_pitch;
5782-
desc.image_slice_pitch = slice_pitch;
5783-
5784-
if (properties.empty()) {
5751+
cl_int *err = nullptr)
5752+
{
5753+
cl_int error;
5754+
5755+
cl_image_desc desc = {};
5756+
desc.image_type = CL_MEM_OBJECT_IMAGE3D;
5757+
desc.image_width = width;
5758+
desc.image_height = height;
5759+
desc.image_depth = depth;
5760+
desc.image_row_pitch = row_pitch;
5761+
desc.image_slice_pitch = slice_pitch;
5762+
57855763
object_ = CL_(clCreateImageWithProperties)(
5786-
context(), nullptr, flags, &format, &desc, host_ptr, &error);
5787-
} else {
5788-
object_ =
5789-
CL_(clCreateImageWithProperties)(context(), properties.data(), flags,
5790-
&format, &desc, host_ptr, &error);
5791-
}
5792-
5793-
detail::errHandler(error, __CREATE_IMAGE_ERR);
5794-
if (err != nullptr) {
5795-
*err = error;
5796-
}
5764+
context(),
5765+
properties.empty() ? nullptr : properties.data(),
5766+
flags, &format, &desc, host_ptr, &error);
5767+
5768+
detail::errHandler(error, __CREATE_IMAGE_ERR);
5769+
if (err != nullptr) {
5770+
*err = error;
5771+
}
57975772
}
57985773
#endif //#if CL_HPP_TARGET_OPENCL_VERSION >= 300
57995774

tests/test_openclhpp.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,51 @@ void testBufferWithProperties(void)
14051405
#endif //CL_HPP_TARGET_OPENCL_VERSION >= 300
14061406
}
14071407

1408+
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
1409+
static cl_mem clCreateBufferWithProperties_EmptyProperties(
1410+
cl_context context,
1411+
const cl_mem_properties *properties,
1412+
cl_mem_flags flags,
1413+
size_t size,
1414+
void *host_ptr,
1415+
cl_int *errcode_ret,
1416+
int num_calls)
1417+
{
1418+
(void) num_calls;
1419+
1420+
TEST_ASSERT_EQUAL(context, make_context(1));
1421+
TEST_ASSERT_EQUAL(properties, nullptr);
1422+
TEST_ASSERT_EQUAL_HEX(flags, CL_MEM_READ_WRITE);
1423+
TEST_ASSERT_EQUAL(size, 42);
1424+
TEST_ASSERT_NULL(host_ptr);
1425+
if (errcode_ret)
1426+
*errcode_ret = CL_SUCCESS;
1427+
1428+
return make_mem(0);
1429+
}
1430+
#endif //CL_HPP_TARGET_OPENCL_VERSION >= 300
1431+
1432+
void testBufferWithEmptyProperties(void)
1433+
{
1434+
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
1435+
clCreateBufferWithProperties_StubWithCallback(clCreateBufferWithProperties_EmptyProperties);
1436+
clReleaseContext_ExpectAndReturn(make_context(1), CL_SUCCESS);
1437+
1438+
cl::Context context(make_context(1));
1439+
1440+
cl_int err;
1441+
1442+
VECTOR_CLASS<cl_mem_properties> props;
1443+
cl::Buffer buffer(context, props, CL_MEM_READ_WRITE, 42, nullptr, &err);
1444+
1445+
TEST_ASSERT_EQUAL_PTR(make_mem(0), buffer());
1446+
TEST_ASSERT_EQUAL(CL_SUCCESS, err);
1447+
1448+
// prevent destructor from interfering with the test
1449+
buffer() = nullptr;
1450+
#endif //CL_HPP_TARGET_OPENCL_VERSION >= 300
1451+
}
1452+
14081453
/****************************************************************************
14091454
* Tests for cl::Image1DBuffer
14101455
****************************************************************************/

0 commit comments

Comments
 (0)