From f3b94f587ef998270a98798bbec0b82e5527a433 Mon Sep 17 00:00:00 2001 From: Junyan He Date: Fri, 13 Jun 2014 15:07:10 +0800 Subject: [PATCH] Add checks for clCreateImage and add 1d image creating logic Add more check for Image creating according to the spec. Update the according image utest cases to pass it. The 1d image creating is also be added. Signed-off-by: Junyan He Reviewed-by: Zhigang Gong --- src/cl_api.c | 36 ++++++++++++++++++++++++++++++++++++ src/cl_mem.c | 24 ++++++++++++++++++------ utests/compiler_copy_image.cpp | 16 +++++++++++++--- utests/compiler_copy_image1.cpp | 22 ++++++++++++++++------ utests/compiler_copy_image_3d.cpp | 23 ++++++++++++++++++++--- utests/compiler_fill_image.cpp | 11 ++++++++++- utests/compiler_fill_image0.cpp | 11 ++++++++++- 7 files changed, 123 insertions(+), 20 deletions(-) diff --git a/src/cl_api.c b/src/cl_api.c index e607a91..5ca8374 100644 --- a/src/cl_api.c +++ b/src/cl_api.c @@ -516,7 +516,43 @@ clCreateImage(cl_context context, cl_mem mem = NULL; cl_int err = CL_SUCCESS; CHECK_CONTEXT (context); + if (image_format == NULL) { + err = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR; + goto error; + } + if (image_format->image_channel_order < CL_R || + image_format->image_channel_order > CL_RGBx) { + err = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR; + goto error; + } + if (image_format->image_channel_data_type < CL_SNORM_INT8 || + image_format->image_channel_data_type > CL_FLOAT) { + err = CL_INVALID_IMAGE_FORMAT_DESCRIPTOR; + goto error; + } + + if (image_desc == NULL) { + err = CL_INVALID_IMAGE_DESCRIPTOR; + goto error; + } + if (image_desc->image_type <= CL_MEM_OBJECT_BUFFER || + image_desc->image_type > CL_MEM_OBJECT_IMAGE1D_BUFFER) { + err = CL_INVALID_IMAGE_DESCRIPTOR; + goto error; + } + /* buffer refers to a valid buffer memory object if image_type is + CL_MEM_OBJECT_IMAGE1D_BUFFER. Otherwise it must be NULL. */ + if (image_desc->image_type != CL_MEM_OBJECT_IMAGE1D_BUFFER && + image_desc->buffer) { + err = CL_INVALID_IMAGE_DESCRIPTOR; + goto error; + } + if (image_desc->num_mip_levels || image_desc->num_samples) { + err = CL_INVALID_IMAGE_DESCRIPTOR; + goto error; + } + /* Other details check for image_desc will leave to image create. */ mem = cl_mem_new_image(context, flags, image_format, diff --git a/src/cl_mem.c b/src/cl_mem.c index f0fd372..810c8a9 100644 --- a/src/cl_mem.c +++ b/src/cl_mem.c @@ -571,10 +571,22 @@ _cl_mem_new_image(cl_context ctx, err = CL_INVALID_IMAGE_SIZE; \ goto error; \ } while (0); + if (UNLIKELY(w == 0)) DO_IMAGE_ERROR; - if (UNLIKELY(h == 0)) DO_IMAGE_ERROR; + if (UNLIKELY(h == 0 && image_type != CL_MEM_OBJECT_IMAGE1D)) DO_IMAGE_ERROR; + + if (image_type == CL_MEM_OBJECT_IMAGE1D) { + size_t min_pitch = bpp * w; + if (data && pitch == 0) + pitch = min_pitch; - if (image_type == CL_MEM_OBJECT_IMAGE2D) { + depth = 1; + h = 1; + if (UNLIKELY(w > ctx->device->image2d_max_width)) DO_IMAGE_ERROR; + if (UNLIKELY(data && min_pitch > pitch)) DO_IMAGE_ERROR; + if (UNLIKELY(!data && pitch != 0)) DO_IMAGE_ERROR; + tiling = CL_NO_TILE; + } else if (image_type == CL_MEM_OBJECT_IMAGE2D) { size_t min_pitch = bpp * w; if (data && pitch == 0) pitch = min_pitch; @@ -587,9 +599,7 @@ _cl_mem_new_image(cl_context ctx, if (cl_driver_get_ver(ctx->drv) != 6) tiling = cl_get_default_tiling(); depth = 1; - } - - if (image_type == CL_MEM_OBJECT_IMAGE3D) { + } else if (image_type == CL_MEM_OBJECT_IMAGE3D) { size_t min_pitch = bpp * w; if (data && pitch == 0) pitch = min_pitch; @@ -607,7 +617,9 @@ _cl_mem_new_image(cl_context ctx, /* Pick up tiling mode (we do only linear on SNB) */ if (cl_driver_get_ver(ctx->drv) != 6) tiling = cl_get_default_tiling(); - } + } else + assert(0); + #undef DO_IMAGE_ERROR /* Tiling requires to align both pitch and height */ diff --git a/utests/compiler_copy_image.cpp b/utests/compiler_copy_image.cpp index 82fd02b..150fd8a 100644 --- a/utests/compiler_copy_image.cpp +++ b/utests/compiler_copy_image.cpp @@ -1,3 +1,4 @@ +#include #include "utest_helper.hpp" static void compiler_copy_image(void) @@ -5,8 +6,12 @@ static void compiler_copy_image(void) const size_t w = 512; const size_t h = 512; cl_image_format format; + cl_image_desc desc; cl_sampler sampler; + memset(&desc, 0x0, sizeof(cl_image_desc)); + memset(&format, 0x0, sizeof(cl_image_format)); + // Setup kernel and images OCL_CREATE_KERNEL("test_copy_image"); buf_data[0] = (uint32_t*) malloc(sizeof(uint32_t) * w * h); @@ -16,9 +21,14 @@ static void compiler_copy_image(void) format.image_channel_order = CL_RGBA; format.image_channel_data_type = CL_UNSIGNED_INT8; - OCL_CREATE_IMAGE2D(buf[0], CL_MEM_COPY_HOST_PTR, &format, w, h, w * sizeof(uint32_t), buf_data[0]); - - OCL_CREATE_IMAGE2D(buf[1], 0, &format, w, h, 0, NULL); + desc.image_type = CL_MEM_OBJECT_IMAGE2D; + desc.image_width = w; + desc.image_height = h; + desc.image_row_pitch = w * sizeof(uint32_t); + OCL_CREATE_IMAGE(buf[0], CL_MEM_COPY_HOST_PTR, &format, &desc, buf_data[0]); + + desc.image_row_pitch = 0; + OCL_CREATE_IMAGE(buf[1], 0, &format, &desc, NULL); OCL_CREATE_SAMPLER(sampler, CL_ADDRESS_REPEAT, CL_FILTER_NEAREST); free(buf_data[0]); buf_data[0] = NULL; diff --git a/utests/compiler_copy_image1.cpp b/utests/compiler_copy_image1.cpp index df146ca..659dddc 100644 --- a/utests/compiler_copy_image1.cpp +++ b/utests/compiler_copy_image1.cpp @@ -1,3 +1,4 @@ +#include #include "utest_helper.hpp" static void compiler_copy_image1(void) @@ -5,8 +6,12 @@ static void compiler_copy_image1(void) const size_t w = 512; const size_t h = 512; cl_image_format format; + cl_image_desc desc; cl_sampler sampler; + memset(&desc, 0x0, sizeof(cl_image_desc)); + memset(&format, 0x0, sizeof(cl_image_format)); + // Setup kernel and images OCL_CREATE_KERNEL("test_copy_image1"); buf_data[0] = (uint32_t*) malloc(sizeof(uint32_t) * w * h); @@ -16,14 +21,19 @@ static void compiler_copy_image1(void) format.image_channel_order = CL_RGBA; format.image_channel_data_type = CL_UNSIGNED_INT8; - OCL_CREATE_IMAGE2D(buf[0], CL_MEM_COPY_HOST_PTR, &format, w, h, w * sizeof(uint32_t), buf_data[0]); + desc.image_type = CL_MEM_OBJECT_IMAGE2D; + desc.image_width = w; + desc.image_height = h; + desc.image_row_pitch = w * sizeof(uint32_t); + OCL_CREATE_IMAGE(buf[0], CL_MEM_COPY_HOST_PTR, &format, &desc, buf_data[0]); OCL_CREATE_SAMPLER(sampler, CL_ADDRESS_REPEAT, CL_FILTER_NEAREST); - OCL_CREATE_IMAGE2D(buf[1], 0, &format, w, h, 0, NULL); - OCL_CREATE_IMAGE2D(buf[2], 0, &format, w, h, 0, NULL); - OCL_CREATE_IMAGE2D(buf[3], 0, &format, w, h, 0, NULL); - OCL_CREATE_IMAGE2D(buf[4], 0, &format, w, h, 0, NULL); - OCL_CREATE_IMAGE2D(buf[5], 0, &format, w, h, 0, NULL); + desc.image_row_pitch = 0; + OCL_CREATE_IMAGE(buf[1], 0, &format, &desc, NULL); + OCL_CREATE_IMAGE(buf[2], 0, &format, &desc, NULL); + OCL_CREATE_IMAGE(buf[3], 0, &format, &desc, NULL); + OCL_CREATE_IMAGE(buf[4], 0, &format, &desc, NULL); + OCL_CREATE_IMAGE(buf[5], 0, &format, &desc, NULL); free(buf_data[0]); buf_data[0] = NULL; diff --git a/utests/compiler_copy_image_3d.cpp b/utests/compiler_copy_image_3d.cpp index 072c99a..de7cd45 100644 --- a/utests/compiler_copy_image_3d.cpp +++ b/utests/compiler_copy_image_3d.cpp @@ -7,8 +7,12 @@ static void compiler_copy_image_3d(void) const size_t h = 512; const size_t depth = 4; cl_image_format format; + cl_image_desc desc; cl_sampler sampler; + memset(&desc, 0x0, sizeof(cl_image_desc)); + memset(&format, 0x0, sizeof(cl_image_format)); + // Setup kernel and images OCL_CREATE_KERNEL("test_copy_image_3d"); buf_data[0] = (uint32_t*) malloc(sizeof(uint32_t) * w * h * depth); @@ -19,10 +23,23 @@ static void compiler_copy_image_3d(void) format.image_channel_order = CL_RGBA; format.image_channel_data_type = CL_UNORM_INT8; - OCL_CREATE_IMAGE3D(buf[0], CL_MEM_COPY_HOST_PTR, &format, w, h, depth, w*4, w*h*4, buf_data[0]); - OCL_CREATE_IMAGE3D(buf[1], 0, &format, w, h, depth, 0, 0, NULL); + desc.image_type = CL_MEM_OBJECT_IMAGE3D; + desc.image_width = w; + desc.image_height = h; + desc.image_depth = depth; + desc.image_row_pitch = 0; + desc.image_slice_pitch = 0; + + OCL_CREATE_IMAGE(buf[0], CL_MEM_COPY_HOST_PTR, &format, &desc, buf_data[0]); + OCL_CREATE_IMAGE(buf[1], 0, &format, &desc, NULL); + memset(&desc, 0, sizeof(desc)); + desc.image_type = CL_MEM_OBJECT_IMAGE2D; + desc.image_width = w; + desc.image_height = h; + desc.image_depth = 1; for(uint32_t i = 0; i < depth; i++) - OCL_CREATE_IMAGE2D(buf[2 + i], 0, &format, w, h, 0, NULL); + OCL_CREATE_IMAGE(buf[2 + i], 0, &format, &desc, NULL); + OCL_CREATE_SAMPLER(sampler, CL_ADDRESS_REPEAT, CL_FILTER_NEAREST); free(buf_data[0]); buf_data[0] = NULL; diff --git a/utests/compiler_fill_image.cpp b/utests/compiler_fill_image.cpp index 2f9fe3d..5a38b8c 100644 --- a/utests/compiler_fill_image.cpp +++ b/utests/compiler_fill_image.cpp @@ -1,3 +1,4 @@ +#include #include "utest_helper.hpp" static void compiler_fill_image(void) @@ -6,14 +7,22 @@ static void compiler_fill_image(void) const size_t h = 512; uint32_t color = 0x12345678; cl_image_format format; + cl_image_desc desc; + + memset(&desc, 0x0, sizeof(cl_image_desc)); + memset(&format, 0x0, sizeof(cl_image_format)); format.image_channel_order = CL_RGBA; format.image_channel_data_type = CL_UNSIGNED_INT8; + desc.image_type = CL_MEM_OBJECT_IMAGE2D; + desc.image_width = w; + desc.image_height = h; + desc.image_row_pitch = 0; // Setup kernel and images OCL_CREATE_KERNEL("test_fill_image"); - OCL_CREATE_IMAGE2D(buf[0], 0, &format, w, h, 0, NULL); + OCL_CREATE_IMAGE(buf[0], 0, &format, &desc, NULL); // Run the kernel OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]); diff --git a/utests/compiler_fill_image0.cpp b/utests/compiler_fill_image0.cpp index 1ab13be..e6e0b1d 100644 --- a/utests/compiler_fill_image0.cpp +++ b/utests/compiler_fill_image0.cpp @@ -1,3 +1,4 @@ +#include #include "utest_helper.hpp" static void compiler_fill_image0(void) @@ -5,14 +6,22 @@ static void compiler_fill_image0(void) const size_t w = 512; const size_t h = 512; cl_image_format format; + cl_image_desc desc; + + memset(&desc, 0x0, sizeof(cl_image_desc)); + memset(&format, 0x0, sizeof(cl_image_format)); format.image_channel_order = CL_RGBA; format.image_channel_data_type = CL_UNSIGNED_INT8; + desc.image_type = CL_MEM_OBJECT_IMAGE2D; + desc.image_width = w; + desc.image_height = h; + desc.image_row_pitch = 0; // Setup kernel and images OCL_CREATE_KERNEL("test_fill_image0"); - OCL_CREATE_IMAGE2D(buf[0], 0, &format, w, h, 0, NULL); + OCL_CREATE_IMAGE(buf[0], 0, &format, &desc, NULL); // Run the kernel OCL_SET_ARG(0, sizeof(cl_mem), &buf[0]); -- 2.7.4