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 a1b3cff
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
16 changes: 10 additions & 6 deletions source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
//===----------------------------------------------------------------------===//
#include "common.hpp"
#include "device.hpp"
#include "memory.hpp"
#include "program.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_int RetErr = clSetKernelArg(
cl_adapter::cast<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), cl_adapter::cast<const cl_mem *>(&hArgValue));
cl_mem CLArgValue = hArgValue->get();
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
cl_adapter::cast<cl_uint>(argIndex),
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_int RetErr = clSetKernelArg(
cl_adapter::cast<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
sizeof(hArgValue), cl_adapter::cast<const cl_sampler *>(&hArgValue));
cl_sampler CLArgSampler = hArgValue->get();
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
cl_adapter::cast<cl_uint>(argIndex),
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
31 changes: 16 additions & 15 deletions source/adapters/opencl/sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//

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

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,10 +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,
propSize, pPropValue, &CheckPropSize));
ur_result_t Err = mapCLErrorToUR(clGetSamplerInfo(
hSampler->get(), SamplerInfo, propSize, pPropValue, &CheckPropSize));
if (pPropValue && CheckPropSize != propSize) {
return UR_RESULT_ERROR_INVALID_SIZE;
}
Expand All @@ -178,27 +182,24 @@ 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)));
return mapCLErrorToUR(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)));
return mapCLErrorToUR(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 a1b3cff

Please sign in to comment.