Skip to content

Commit a1b3cff

Browse files
committed
Add sampler handle
1 parent 90a6d7e commit a1b3cff

File tree

4 files changed

+54
-22
lines changed

4 files changed

+54
-22
lines changed

source/adapters/opencl/kernel.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
//===----------------------------------------------------------------------===//
1010
#include "common.hpp"
1111
#include "device.hpp"
12+
#include "memory.hpp"
1213
#include "program.hpp"
14+
#include "sampler.hpp"
1315

1416
#include <algorithm>
1517
#include <memory>
@@ -391,9 +393,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgMemObj(
391393
ur_kernel_handle_t hKernel, uint32_t argIndex,
392394
const ur_kernel_arg_mem_obj_properties_t *, ur_mem_handle_t hArgValue) {
393395

394-
cl_int RetErr = clSetKernelArg(
395-
cl_adapter::cast<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
396-
sizeof(hArgValue), cl_adapter::cast<const cl_mem *>(&hArgValue));
396+
cl_mem CLArgValue = hArgValue->get();
397+
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
398+
cl_adapter::cast<cl_uint>(argIndex),
399+
sizeof(hArgValue), &CLArgValue);
397400
CL_RETURN_ON_FAILURE(RetErr);
398401
return UR_RESULT_SUCCESS;
399402
}
@@ -402,9 +405,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgSampler(
402405
ur_kernel_handle_t hKernel, uint32_t argIndex,
403406
const ur_kernel_arg_sampler_properties_t *, ur_sampler_handle_t hArgValue) {
404407

405-
cl_int RetErr = clSetKernelArg(
406-
cl_adapter::cast<cl_kernel>(hKernel), cl_adapter::cast<cl_uint>(argIndex),
407-
sizeof(hArgValue), cl_adapter::cast<const cl_sampler *>(&hArgValue));
408+
cl_sampler CLArgSampler = hArgValue->get();
409+
cl_int RetErr = clSetKernelArg(cl_adapter::cast<cl_kernel>(hKernel),
410+
cl_adapter::cast<cl_uint>(argIndex),
411+
sizeof(hArgValue), &CLArgSampler);
408412
CL_RETURN_ON_FAILURE(RetErr);
409413
return UR_RESULT_SUCCESS;
410414
}

source/adapters/opencl/program.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count,
229229
cl_program Program = clLinkProgram(
230230
hContext->get(), 0, nullptr, pOptions, cl_adapter::cast<cl_uint>(count),
231231
CLPrograms.data(), nullptr, nullptr, &CLResult);
232-
*phProgram = new ur_program_handle_t_(Program, hContext);
233232
CL_RETURN_ON_FAILURE(CLResult);
233+
*phProgram = new ur_program_handle_t_(Program, hContext);
234234

235235
return UR_RESULT_SUCCESS;
236236
}

source/adapters/opencl/sampler.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11+
#include "sampler.hpp"
1112
#include "common.hpp"
1213
#include "context.hpp"
1314

@@ -144,10 +145,10 @@ ur_result_t urSamplerCreate(ur_context_handle_t hContext,
144145
cl_filter_mode FilterMode = ur2CLFilterMode(pDesc->filterMode);
145146

146147
// Always call OpenCL 1.0 API
147-
*phSampler = cl_adapter::cast<ur_sampler_handle_t>(clCreateSampler(
148+
cl_sampler Sampler = clCreateSampler(
148149
hContext->get(), static_cast<cl_bool>(pDesc->normalizedCoords),
149-
AddressingMode, FilterMode, cl_adapter::cast<cl_int *>(&ErrorCode)));
150-
150+
AddressingMode, FilterMode, cl_adapter::cast<cl_int *>(&ErrorCode));
151+
*phSampler = new ur_sampler_handle_t_(Sampler, hContext);
151152
return mapCLErrorToUR(ErrorCode);
152153
}
153154

@@ -158,10 +159,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName,
158159
static_assert(sizeof(cl_addressing_mode) ==
159160
sizeof(ur_sampler_addressing_mode_t));
160161

162+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
163+
if (SamplerInfo == CL_SAMPLER_CONTEXT) {
164+
return ReturnValue(hSampler->Context);
165+
}
161166
size_t CheckPropSize = 0;
162-
ur_result_t Err = mapCLErrorToUR(
163-
clGetSamplerInfo(cl_adapter::cast<cl_sampler>(hSampler), SamplerInfo,
164-
propSize, pPropValue, &CheckPropSize));
167+
ur_result_t Err = mapCLErrorToUR(clGetSamplerInfo(
168+
hSampler->get(), SamplerInfo, propSize, pPropValue, &CheckPropSize));
165169
if (pPropValue && CheckPropSize != propSize) {
166170
return UR_RESULT_ERROR_INVALID_SIZE;
167171
}
@@ -178,27 +182,24 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName,
178182

179183
UR_APIEXPORT ur_result_t UR_APICALL
180184
urSamplerRetain(ur_sampler_handle_t hSampler) {
181-
return mapCLErrorToUR(
182-
clRetainSampler(cl_adapter::cast<cl_sampler>(hSampler)));
185+
return mapCLErrorToUR(clRetainSampler(hSampler->get()));
183186
}
184187

185188
UR_APIEXPORT ur_result_t UR_APICALL
186189
urSamplerRelease(ur_sampler_handle_t hSampler) {
187-
return mapCLErrorToUR(
188-
clReleaseSampler(cl_adapter::cast<cl_sampler>(hSampler)));
190+
return mapCLErrorToUR(clReleaseSampler(hSampler->get()));
189191
}
190192

191193
UR_APIEXPORT ur_result_t UR_APICALL urSamplerGetNativeHandle(
192194
ur_sampler_handle_t hSampler, ur_native_handle_t *phNativeSampler) {
193-
*phNativeSampler = reinterpret_cast<ur_native_handle_t>(
194-
cl_adapter::cast<cl_sampler>(hSampler));
195+
*phNativeSampler = reinterpret_cast<ur_native_handle_t>(hSampler->get());
195196
return UR_RESULT_SUCCESS;
196197
}
197198

198199
UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle(
199-
ur_native_handle_t hNativeSampler, ur_context_handle_t,
200+
ur_native_handle_t hNativeSampler, ur_context_handle_t hContext,
200201
const ur_sampler_native_properties_t *, ur_sampler_handle_t *phSampler) {
201-
*phSampler = reinterpret_cast<ur_sampler_handle_t>(
202-
cl_adapter::cast<cl_sampler>(hNativeSampler));
202+
cl_sampler NativeHandle = reinterpret_cast<cl_sampler>(hNativeSampler);
203+
*phSampler = new ur_sampler_handle_t_(NativeHandle, hContext);
203204
return UR_RESULT_SUCCESS;
204205
}

source/adapters/opencl/sampler.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===--------- sampler.hpp - OpenCL Adapter ---------------------------===//
2+
//
3+
// Copyright (C) 2023 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
#pragma once
11+
12+
#include "common.hpp"
13+
14+
#include <vector>
15+
16+
struct ur_sampler_handle_t_ {
17+
using native_type = cl_sampler;
18+
native_type Sampler;
19+
ur_context_handle_t Context;
20+
21+
ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx)
22+
: Sampler(Sampler), Context(Ctx) {}
23+
24+
~ur_sampler_handle_t_() {}
25+
26+
native_type get() { return Sampler; }
27+
};

0 commit comments

Comments
 (0)