1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // CLSampler.cpp: Implements the cl::Sampler class.
7
8 #include "libANGLE/CLSampler.h"
9
10 #include "libANGLE/CLContext.h"
11 #include "libANGLE/cl_utils.h"
12
13 #include <cstring>
14
15 namespace cl
16 {
17
getInfo(SamplerInfo name,size_t valueSize,void * value,size_t * valueSizeRet) const18 angle::Result Sampler::getInfo(SamplerInfo name,
19 size_t valueSize,
20 void *value,
21 size_t *valueSizeRet) const
22 {
23 static_assert(std::is_same<cl_uint, cl_addressing_mode>::value &&
24 std::is_same<cl_uint, cl_filter_mode>::value,
25 "OpenCL type mismatch");
26
27 cl_uint valUInt = 0u;
28 void *valPointer = nullptr;
29 const void *copyValue = nullptr;
30 size_t copySize = 0u;
31
32 switch (name)
33 {
34 case SamplerInfo::ReferenceCount:
35 valUInt = getRefCount();
36 copyValue = &valUInt;
37 copySize = sizeof(valUInt);
38 break;
39 case SamplerInfo::Context:
40 valPointer = mContext->getNative();
41 copyValue = &valPointer;
42 copySize = sizeof(valPointer);
43 break;
44 case SamplerInfo::NormalizedCoords:
45 copyValue = &mNormalizedCoords;
46 copySize = sizeof(mNormalizedCoords);
47 break;
48 case SamplerInfo::AddressingMode:
49 valUInt = ToCLenum(mAddressingMode);
50 copyValue = &valUInt;
51 copySize = sizeof(valUInt);
52 break;
53 case SamplerInfo::FilterMode:
54 valUInt = ToCLenum(mFilterMode);
55 copyValue = &valUInt;
56 copySize = sizeof(valUInt);
57 break;
58 case SamplerInfo::Properties:
59 copyValue = mProperties.data();
60 copySize = mProperties.size() * sizeof(decltype(mProperties)::value_type);
61 break;
62 default:
63 ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
64 }
65
66 if (value != nullptr)
67 {
68 // CL_INVALID_VALUE if size in bytes specified by param_value_size is < size of return type
69 // as described in the Sampler Object Queries table and param_value is not NULL.
70 if (valueSize < copySize)
71 {
72 ANGLE_CL_RETURN_ERROR(CL_INVALID_VALUE);
73 }
74 if (copyValue != nullptr)
75 {
76 std::memcpy(value, copyValue, copySize);
77 }
78 }
79 if (valueSizeRet != nullptr)
80 {
81 *valueSizeRet = copySize;
82 }
83 return angle::Result::Continue;
84 }
85
86 Sampler::~Sampler() = default;
87
Sampler(Context & context,PropArray && properties,cl_bool normalizedCoords,AddressingMode addressingMode,FilterMode filterMode)88 Sampler::Sampler(Context &context,
89 PropArray &&properties,
90 cl_bool normalizedCoords,
91 AddressingMode addressingMode,
92 FilterMode filterMode)
93 : mContext(&context),
94 mProperties(std::move(properties)),
95 mNormalizedCoords(normalizedCoords),
96 mAddressingMode(addressingMode),
97 mFilterMode(filterMode),
98 mImpl(nullptr)
99 {
100 ANGLE_CL_IMPL_TRY(context.getImpl().createSampler(*this, &mImpl));
101 }
102
103 } // namespace cl
104