From 086ef06d8f198aa8dfb8a32d0e1b79ea8c749046 Mon Sep 17 00:00:00 2001 From: omarahmed1111 Date: Wed, 13 Dec 2023 14:40:06 +0000 Subject: [PATCH] Add sampler handle --- source/adapters/opencl/kernel.cpp | 8 ++++++-- source/adapters/opencl/program.cpp | 2 +- source/adapters/opencl/sampler.cpp | 26 +++++++++++++++----------- source/adapters/opencl/sampler.hpp | 27 +++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 source/adapters/opencl/sampler.hpp diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index a589ffd70f..fc5c0426f3 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -10,6 +10,8 @@ #include "common.hpp" #include "device.hpp" #include "program.hpp" +#include "memory.hpp" +#include "sampler.hpp" #include #include @@ -391,9 +393,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgMemObj( ur_kernel_handle_t hKernel, uint32_t argIndex, const ur_kernel_arg_mem_obj_properties_t *, ur_mem_handle_t hArgValue) { + cl_mem CLArgValue = hArgValue->get(); cl_int RetErr = clSetKernelArg( cl_adapter::cast(hKernel), cl_adapter::cast(argIndex), - sizeof(hArgValue), cl_adapter::cast(&hArgValue)); + sizeof(hArgValue), &CLArgValue); CL_RETURN_ON_FAILURE(RetErr); return UR_RESULT_SUCCESS; } @@ -402,9 +405,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgSampler( ur_kernel_handle_t hKernel, uint32_t argIndex, const ur_kernel_arg_sampler_properties_t *, ur_sampler_handle_t hArgValue) { + cl_sampler CLArgSampler = hArgValue->get(); cl_int RetErr = clSetKernelArg( cl_adapter::cast(hKernel), cl_adapter::cast(argIndex), - sizeof(hArgValue), cl_adapter::cast(&hArgValue)); + sizeof(hArgValue), &CLArgSampler); CL_RETURN_ON_FAILURE(RetErr); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index 277d4fbd33..07b2841d91 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -229,8 +229,8 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count, cl_program Program = clLinkProgram( hContext->get(), 0, nullptr, pOptions, cl_adapter::cast(count), CLPrograms.data(), nullptr, nullptr, &CLResult); - *phProgram = new ur_program_handle_t_(Program, hContext); CL_RETURN_ON_FAILURE(CLResult); + *phProgram = new ur_program_handle_t_(Program, hContext); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/sampler.cpp b/source/adapters/opencl/sampler.cpp index cfec86ed37..901ab10dd2 100644 --- a/source/adapters/opencl/sampler.cpp +++ b/source/adapters/opencl/sampler.cpp @@ -10,6 +10,7 @@ #include "common.hpp" #include "context.hpp" +#include "sampler.hpp" namespace { @@ -144,10 +145,10 @@ ur_result_t urSamplerCreate(ur_context_handle_t hContext, cl_filter_mode FilterMode = ur2CLFilterMode(pDesc->filterMode); // Always call OpenCL 1.0 API - *phSampler = cl_adapter::cast(clCreateSampler( + cl_sampler Sampler = clCreateSampler( hContext->get(), static_cast(pDesc->normalizedCoords), - AddressingMode, FilterMode, cl_adapter::cast(&ErrorCode))); - + AddressingMode, FilterMode, cl_adapter::cast(&ErrorCode)); + *phSampler = new ur_sampler_handle_t_(Sampler, hContext); return mapCLErrorToUR(ErrorCode); } @@ -158,9 +159,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, static_assert(sizeof(cl_addressing_mode) == sizeof(ur_sampler_addressing_mode_t)); + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); + if (SamplerInfo == CL_SAMPLER_CONTEXT) { + return ReturnValue(hSampler->Context); + } size_t CheckPropSize = 0; ur_result_t Err = mapCLErrorToUR( - clGetSamplerInfo(cl_adapter::cast(hSampler), SamplerInfo, + clGetSamplerInfo(hSampler->get(), SamplerInfo, propSize, pPropValue, &CheckPropSize)); if (pPropValue && CheckPropSize != propSize) { return UR_RESULT_ERROR_INVALID_SIZE; @@ -179,26 +184,25 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { return mapCLErrorToUR( - clRetainSampler(cl_adapter::cast(hSampler))); + clRetainSampler(hSampler->get())); } UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { return mapCLErrorToUR( - clReleaseSampler(cl_adapter::cast(hSampler))); + clReleaseSampler(hSampler->get())); } UR_APIEXPORT ur_result_t UR_APICALL urSamplerGetNativeHandle( ur_sampler_handle_t hSampler, ur_native_handle_t *phNativeSampler) { - *phNativeSampler = reinterpret_cast( - cl_adapter::cast(hSampler)); + *phNativeSampler = reinterpret_cast(hSampler->get()); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( - ur_native_handle_t hNativeSampler, ur_context_handle_t, + ur_native_handle_t hNativeSampler, ur_context_handle_t hContext, const ur_sampler_native_properties_t *, ur_sampler_handle_t *phSampler) { - *phSampler = reinterpret_cast( - cl_adapter::cast(hNativeSampler)); + cl_sampler NativeHandle = reinterpret_cast(hNativeSampler); + *phSampler = new ur_sampler_handle_t_(NativeHandle, hContext); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/sampler.hpp b/source/adapters/opencl/sampler.hpp new file mode 100644 index 0000000000..005f14ac97 --- /dev/null +++ b/source/adapters/opencl/sampler.hpp @@ -0,0 +1,27 @@ +//===--------- sampler.hpp - OpenCL Adapter ---------------------------===// +// +// Copyright (C) 2023 Intel Corporation +// +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM +// Exceptions. See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#pragma once + +#include "common.hpp" + +#include + +struct ur_sampler_handle_t_ { + using native_type = cl_sampler; + native_type Sampler; + ur_context_handle_t Context; + + ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) + : Sampler(Sampler), Context(Ctx){} + + ~ur_sampler_handle_t_() {} + + native_type get() { return Sampler; } +};