Skip to content

Commit

Permalink
Add sampler handle
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Dec 13, 2023
1 parent 90a6d7e commit 086ef06
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
8 changes: 6 additions & 2 deletions source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "common.hpp"
#include "device.hpp"
#include "program.hpp"
#include "memory.hpp"
#include "sampler.hpp"

#include <algorithm>
#include <memory>
Expand Down Expand Up @@ -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<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), cl_adapter::cast<const cl_mem *>(&hArgValue));
sizeof(hArgValue), &CLArgValue);
CL_RETURN_ON_FAILURE(RetErr);
return UR_RESULT_SUCCESS;
}
Expand All @@ -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<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), cl_adapter::cast<const cl_sampler *>(&hArgValue));
sizeof(hArgValue), &CLArgSampler);
CL_RETURN_ON_FAILURE(RetErr);
return UR_RESULT_SUCCESS;
}
2 changes: 1 addition & 1 deletion source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cl_uint>(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;
}
Expand Down
26 changes: 15 additions & 11 deletions source/adapters/opencl/sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "common.hpp"
#include "context.hpp"
#include "sampler.hpp"

namespace {

Expand Down Expand Up @@ -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<ur_sampler_handle_t>(clCreateSampler(
cl_sampler Sampler = clCreateSampler(
hContext->get(), static_cast<cl_bool>(pDesc->normalizedCoords),
AddressingMode, FilterMode, cl_adapter::cast<cl_int *>(&ErrorCode)));

AddressingMode, FilterMode, cl_adapter::cast<cl_int *>(&ErrorCode));
*phSampler = new ur_sampler_handle_t_(Sampler, hContext);
return mapCLErrorToUR(ErrorCode);
}

Expand All @@ -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<cl_sampler>(hSampler), SamplerInfo,
clGetSamplerInfo(hSampler->get(), SamplerInfo,
propSize, pPropValue, &CheckPropSize));
if (pPropValue && CheckPropSize != propSize) {
return UR_RESULT_ERROR_INVALID_SIZE;
Expand All @@ -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<cl_sampler>(hSampler)));
clRetainSampler(hSampler->get()));
}

UR_APIEXPORT ur_result_t UR_APICALL
urSamplerRelease(ur_sampler_handle_t hSampler) {
return mapCLErrorToUR(
clReleaseSampler(cl_adapter::cast<cl_sampler>(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<ur_native_handle_t>(
cl_adapter::cast<cl_sampler>(hSampler));
*phNativeSampler = reinterpret_cast<ur_native_handle_t>(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<ur_sampler_handle_t>(
cl_adapter::cast<cl_sampler>(hNativeSampler));
cl_sampler NativeHandle = reinterpret_cast<cl_sampler>(hNativeSampler);
*phSampler = new ur_sampler_handle_t_(NativeHandle, hContext);
return UR_RESULT_SUCCESS;
}
27 changes: 27 additions & 0 deletions source/adapters/opencl/sampler.hpp
Original file line number Diff line number Diff line change
@@ -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 <vector>

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; }
};

0 comments on commit 086ef06

Please sign in to comment.