diff options
author | Francisco Jerez <[email protected]> | 2015-02-22 01:02:41 +0200 |
---|---|---|
committer | Francisco Jerez <[email protected]> | 2015-02-24 16:15:48 +0200 |
commit | 80d3c1e5372d56a55701b10b937173c27615e4b0 (patch) | |
tree | dc52a7f4209afff9699ca446a00ec7500a1526ea | |
parent | 49d3c6a8e63ead43fafb7d52e2c770a1320eb2ee (diff) |
clover: Factor out memory object flags validation to a helper function.
And define constants for commonly used subsets of flags to save some
typing.
Reviewed-and-tested-by: EdB <[email protected]>
-rw-r--r-- | src/gallium/state_trackers/clover/api/memory.cpp | 66 |
1 files changed, 32 insertions, 34 deletions
diff --git a/src/gallium/state_trackers/clover/api/memory.cpp b/src/gallium/state_trackers/clover/api/memory.cpp index a094e74dc67..551a51006c3 100644 --- a/src/gallium/state_trackers/clover/api/memory.cpp +++ b/src/gallium/state_trackers/clover/api/memory.cpp @@ -27,11 +27,33 @@ using namespace clover; +namespace { + const cl_mem_flags dev_access_flags = + CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY; + const cl_mem_flags host_ptr_flags = + CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | CL_MEM_COPY_HOST_PTR; + const cl_mem_flags all_mem_flags = + dev_access_flags | host_ptr_flags; + + void + validate_flags(cl_mem_flags flags, cl_mem_flags valid) { + if ((flags & ~valid) || + util_bitcount(flags & dev_access_flags) > 1) + throw error(CL_INVALID_VALUE); + + if ((flags & CL_MEM_USE_HOST_PTR) && + (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR))) + throw error(CL_INVALID_VALUE); + } +} + CLOVER_API cl_mem clCreateBuffer(cl_context d_ctx, cl_mem_flags flags, size_t size, void *host_ptr, cl_int *r_errcode) try { auto &ctx = obj(d_ctx); + validate_flags(flags, all_mem_flags); + if (bool(host_ptr) != bool(flags & (CL_MEM_USE_HOST_PTR | CL_MEM_COPY_HOST_PTR))) throw error(CL_INVALID_HOST_PTR); @@ -42,19 +64,6 @@ clCreateBuffer(cl_context d_ctx, cl_mem_flags flags, size_t size, )) throw error(CL_INVALID_BUFFER_SIZE); - if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | - CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | - CL_MEM_COPY_HOST_PTR)) - throw error(CL_INVALID_VALUE); - - if (util_bitcount(flags & (CL_MEM_READ_ONLY | CL_MEM_WRITE_ONLY | - CL_MEM_READ_WRITE)) > 1) - throw error(CL_INVALID_VALUE); - - if ((flags & CL_MEM_USE_HOST_PTR) && - (flags & (CL_MEM_COPY_HOST_PTR | CL_MEM_ALLOC_HOST_PTR))) - throw error(CL_INVALID_VALUE); - ret_error(r_errcode, CL_SUCCESS); return new root_buffer(ctx, flags, size, host_ptr); @@ -69,11 +78,10 @@ clCreateSubBuffer(cl_mem d_mem, cl_mem_flags flags, const void *op_info, cl_int *r_errcode) try { auto &parent = obj<root_buffer>(d_mem); - if ((flags & (CL_MEM_USE_HOST_PTR | - CL_MEM_ALLOC_HOST_PTR | - CL_MEM_COPY_HOST_PTR)) || - (~flags & parent.flags() & (CL_MEM_READ_ONLY | - CL_MEM_WRITE_ONLY))) + validate_flags(flags, dev_access_flags); + + if (~flags & parent.flags() & + (dev_access_flags & ~CL_MEM_READ_WRITE)) throw error(CL_INVALID_VALUE); if (op == CL_BUFFER_CREATE_TYPE_REGION) { @@ -106,14 +114,11 @@ clCreateImage2D(cl_context d_ctx, cl_mem_flags flags, void *host_ptr, cl_int *r_errcode) try { auto &ctx = obj(d_ctx); + validate_flags(flags, all_mem_flags); + if (!any_of(std::mem_fn(&device::image_support), ctx.devices())) throw error(CL_INVALID_OPERATION); - if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | - CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | - CL_MEM_COPY_HOST_PTR)) - throw error(CL_INVALID_VALUE); - if (!format) throw error(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR); @@ -144,14 +149,11 @@ clCreateImage3D(cl_context d_ctx, cl_mem_flags flags, void *host_ptr, cl_int *r_errcode) try { auto &ctx = obj(d_ctx); + validate_flags(flags, all_mem_flags); + if (!any_of(std::mem_fn(&device::image_support), ctx.devices())) throw error(CL_INVALID_OPERATION); - if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | - CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | - CL_MEM_COPY_HOST_PTR)) - throw error(CL_INVALID_VALUE); - if (!format) throw error(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR); @@ -179,17 +181,13 @@ clGetSupportedImageFormats(cl_context d_ctx, cl_mem_flags flags, cl_mem_object_type type, cl_uint count, cl_image_format *r_buf, cl_uint *r_count) try { auto &ctx = obj(d_ctx); + auto formats = supported_formats(ctx, type); - if (flags & ~(CL_MEM_READ_WRITE | CL_MEM_WRITE_ONLY | CL_MEM_READ_ONLY | - CL_MEM_USE_HOST_PTR | CL_MEM_ALLOC_HOST_PTR | - CL_MEM_COPY_HOST_PTR)) - throw error(CL_INVALID_VALUE); + validate_flags(flags, all_mem_flags); if (r_buf && !r_count) throw error(CL_INVALID_VALUE); - auto formats = supported_formats(ctx, type); - if (r_buf) std::copy_n(formats.begin(), std::min((cl_uint)formats.size(), count), |