From: Junyan He <junyan...@intel.com> Signed-off-by: Junyan He <junyan...@intel.com> --- runtime/cl_sampler.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ runtime/cl_sampler.h | 57 ++++++++++++++++++++++ 2 files changed, 190 insertions(+) create mode 100644 runtime/cl_sampler.c create mode 100644 runtime/cl_sampler.h
diff --git a/runtime/cl_sampler.c b/runtime/cl_sampler.c new file mode 100644 index 0000000..f9f63ad --- /dev/null +++ b/runtime/cl_sampler.c @@ -0,0 +1,133 @@ +/* + * Copyright © 2012 Intel Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + * Author: Benjamin Segovia <benjamin.sego...@intel.com> + */ + +#include "cl_context.h" +#include "cl_sampler.h" +#include "cl_device_id.h" +#include "cl_alloc.h" + +static uint32_t +sampler_cl_to_clk(cl_bool normalized_coords, cl_addressing_mode address, cl_filter_mode filter) +{ + int clk_address = CLK_ADDRESS_NONE; + int clk_filter = CLK_FILTER_NEAREST; + switch (address) { + case CL_ADDRESS_NONE: + clk_address = CLK_ADDRESS_NONE; + break; + case CL_ADDRESS_CLAMP: + clk_address = CLK_ADDRESS_CLAMP; + break; + case CL_ADDRESS_CLAMP_TO_EDGE: + clk_address = CLK_ADDRESS_CLAMP_TO_EDGE; + break; + case CL_ADDRESS_REPEAT: + clk_address = CLK_ADDRESS_REPEAT; + break; + case CL_ADDRESS_MIRRORED_REPEAT: + clk_address = CLK_ADDRESS_MIRRORED_REPEAT; + break; + default: + assert(0); + } + switch (filter) { + case CL_FILTER_NEAREST: + clk_filter = CLK_FILTER_NEAREST; + break; + case CL_FILTER_LINEAR: + clk_filter = CLK_FILTER_LINEAR; + break; + default: + assert(0); + } + return (clk_address << __CLK_ADDRESS_BASE) | (normalized_coords << __CLK_NORMALIZED_BASE) | (clk_filter); +} + +LOCAL cl_sampler +cl_create_sampler(cl_context ctx, cl_bool normalized_coords, cl_addressing_mode address, + cl_filter_mode filter, cl_int *errcode_ret) +{ + cl_sampler sampler = NULL; + cl_int err = CL_SUCCESS; + cl_int i; + + /* Allocate and inialize the structure itself */ + sampler = CL_CALLOC(1, sizeof(_cl_sampler)); + if (sampler == NULL) { + *errcode_ret = CL_OUT_OF_HOST_MEMORY; + return NULL; + } + + sampler->each_device = CL_CALLOC(ctx->device_num, sizeof(cl_sampler_for_device)); + if (sampler->each_device == NULL) { + CL_FREE(sampler); + *errcode_ret = CL_OUT_OF_HOST_MEMORY; + return NULL; + } + + CL_OBJECT_INIT_BASE(sampler, CL_OBJECT_SAMPLER_MAGIC); + sampler->normalized_coords = normalized_coords; + sampler->address = address; + sampler->filter = filter; + + sampler->clkSamplerValue = sampler_cl_to_clk(normalized_coords, address, filter); + /* Append the sampler in the context sampler list */ + cl_context_add_sampler(ctx, sampler); + + for (i = 0; i < ctx->device_num; i++) { + err = (ctx->devices[i]->api.sampler_create)(ctx->devices[i], sampler); + if (err != CL_SUCCESS) { + *errcode_ret = err; + cl_sampler_delete(sampler); + return NULL; + } + } + + *errcode_ret = CL_SUCCESS; + return sampler; +} + +LOCAL void +cl_sampler_delete(cl_sampler sampler) +{ + cl_int i; + + if (UNLIKELY(sampler == NULL)) + return; + if (CL_OBJECT_DEC_REF(sampler) > 1) + return; + + for (i = 0; i < sampler->each_device_num; i++) { + if (sampler->each_device[i]) + (sampler->each_device[i]->device->api.sampler_delete)(sampler->each_device[i]->device, sampler); + } + CL_FREE(sampler->each_device); + + cl_context_remove_sampler(sampler->ctx, sampler); + + CL_OBJECT_DESTROY_BASE(sampler); + CL_FREE(sampler); +} + +LOCAL void +cl_sampler_add_ref(cl_sampler sampler) +{ + assert(sampler); + CL_OBJECT_INC_REF(sampler); +} diff --git a/runtime/cl_sampler.h b/runtime/cl_sampler.h new file mode 100644 index 0000000..8ef2554 --- /dev/null +++ b/runtime/cl_sampler.h @@ -0,0 +1,57 @@ +/* + * Copyright © 2012 Intel Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see <http://www.gnu.org/licenses/>. + * + * Author: Benjamin Segovia <benjamin.sego...@intel.com> + */ + +#ifndef __CL_SAMPLER_H__ +#define __CL_SAMPLER_H__ + +#include "cl_base_object.h" +#include "../backend/src/ocl_common_defines.h" +#include "CL/cl.h" + +typedef struct _cl_sampler_for_device { + cl_device_id device; /* Point to the device it belong to */ +} _cl_sampler_for_device; +typedef _cl_sampler_for_device *cl_sampler_for_device; + +typedef struct _cl_sampler { + _cl_base_object base; + cl_context ctx; /* Context it belongs to */ + cl_bool normalized_coords; /* Are coordinates normalized? */ + cl_addressing_mode address; /* CLAMP / REPEAT and so on... */ + cl_filter_mode filter; /* LINEAR / NEAREST mostly */ + cl_uint clkSamplerValue; + cl_uint each_device_num; /* Each device number */ + cl_sampler_for_device *each_device; /* Context content interpreted by device */ +} _cl_sampler; + +#define CL_OBJECT_SAMPLER_MAGIC 0x686a0ecba79ce32fLL +#define CL_OBJECT_IS_SAMPLER(obj) ((obj && \ + ((cl_base_object)obj)->magic == CL_OBJECT_SAMPLER_MAGIC && \ + CL_OBJECT_GET_REF(obj) >= 1)) + +/* Create a new sampler object */ +extern cl_sampler cl_create_sampler(cl_context, cl_bool, cl_addressing_mode, cl_filter_mode, cl_int *err); +/* Unref the object and delete it if no more reference on it */ +extern void cl_sampler_delete(cl_sampler); +/* Add one more reference to this object */ +extern void cl_sampler_add_ref(cl_sampler); +/* set a sampler kernel argument */ +int cl_set_sampler_arg_slot(cl_kernel k, int index, cl_sampler sampler); + +#endif /* __CL_SAMPLER_H__ */ -- 2.7.4 _______________________________________________ Beignet mailing list Beignet@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/beignet