|
| 1 | +// RUN: %clangxx -fsycl %s -o %t.out |
| 2 | +// RUN: env SYCL_DEVICE_TYPE=HOST %t.out |
| 3 | +// RUN: %CPU_RUN_PLACEHOLDER %t.out |
| 4 | +// RUN: %GPU_RUN_PLACEHOLDER %t.out |
| 5 | +// RUN: %ACC_RUN_PLACEHOLDER %t.out |
| 6 | +//==------------------- image_accessor_readsampler.cpp ---------------------==// |
| 7 | +//==-----------------image_accessor read API test with sampler--------------==// |
| 8 | +// |
| 9 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 | +// See https://llvm.org/LICENSE.txt for license information. |
| 11 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include <CL/sycl.hpp> |
| 16 | + |
| 17 | +#include <cassert> |
| 18 | +#include <iomanip> |
| 19 | +#include <iostream> |
| 20 | + |
| 21 | +namespace s = cl::sycl; |
| 22 | + |
| 23 | +template <int unique_number> class kernel_class; |
| 24 | + |
| 25 | +void validateReadData(s::cl_float4 ReadData, s::cl_float4 ExpectedColor) { |
| 26 | + // Maximum difference of 1.5 ULP is allowed. |
| 27 | + s::cl_int4 PixelDataInt = ReadData.template as<s::cl_int4>(); |
| 28 | + s::cl_int4 ExpectedDataInt = ExpectedColor.template as<s::cl_int4>(); |
| 29 | + s::cl_int4 Diff = ExpectedDataInt - PixelDataInt; |
| 30 | +#if DEBUG_OUTPUT |
| 31 | + { |
| 32 | + if (((s::cl_int)Diff.x() <= 1 && (s::cl_int)Diff.x() >= -1) && |
| 33 | + ((s::cl_int)Diff.y() <= 1 && (s::cl_int)Diff.y() >= -1) && |
| 34 | + ((s::cl_int)Diff.z() <= 1 && (s::cl_int)Diff.z() >= -1) && |
| 35 | + ((s::cl_int)Diff.w() <= 1 && (s::cl_int)Diff.w() >= -1)) { |
| 36 | + std::cout << "Read Data is correct within precision: " << std::endl; |
| 37 | + } else { |
| 38 | + std::cout << "Read Data is WRONG/ outside precision: " << std::endl; |
| 39 | + } |
| 40 | + std::cout << "ReadData: \t" |
| 41 | + << std::setprecision(std::numeric_limits<long double>::digits10 + |
| 42 | + 1) |
| 43 | + << (float)ReadData.x() * 127 << " " << (float)ReadData.y() * 127 |
| 44 | + << " " << (float)ReadData.z() * 127 << " " |
| 45 | + << (float)ReadData.w() * 127 << std::endl; |
| 46 | + |
| 47 | + std::cout << "ExpectedColor: \t" << (float)ExpectedColor.x() * 127 << " " |
| 48 | + << (float)ExpectedColor.y() * 127 << " " |
| 49 | + << (float)ExpectedColor.z() * 127 << " " |
| 50 | + << (float)ExpectedColor.w() * 127 << std::endl; |
| 51 | + } |
| 52 | +#else |
| 53 | + { |
| 54 | + assert((s::cl_int)Diff.x() <= 1 && (s::cl_int)Diff.x() >= -1); |
| 55 | + assert((s::cl_int)Diff.y() <= 1 && (s::cl_int)Diff.y() >= -1); |
| 56 | + assert((s::cl_int)Diff.z() <= 1 && (s::cl_int)Diff.z() >= -1); |
| 57 | + assert((s::cl_int)Diff.w() <= 1 && (s::cl_int)Diff.w() >= -1); |
| 58 | + } |
| 59 | +#endif |
| 60 | +} |
| 61 | + |
| 62 | +template <int i> |
| 63 | +void checkReadSampler(char *host_ptr, s::sampler Sampler, s::cl_float4 Coord, |
| 64 | + s::cl_float4 ExpectedColor) { |
| 65 | + |
| 66 | + s::cl_float4 ReadData; |
| 67 | + { |
| 68 | + // image with dim = 3 |
| 69 | + s::image<3> Img(host_ptr, s::image_channel_order::rgba, |
| 70 | + s::image_channel_type::snorm_int8, s::range<3>{2, 3, 4}); |
| 71 | + s::queue myQueue; |
| 72 | + s::buffer<s::cl_float4, 1> ReadDataBuf(&ReadData, s::range<1>(1)); |
| 73 | + myQueue.submit([&](s::handler &cgh) { |
| 74 | + auto ReadAcc = Img.get_access<s::cl_float4, s::access::mode::read>(cgh); |
| 75 | + s::accessor<s::cl_float4, 1, s::access::mode::write> ReadDataBufAcc( |
| 76 | + ReadDataBuf, cgh); |
| 77 | + |
| 78 | + cgh.single_task<class kernel_class<i>>([=](){ |
| 79 | + s::cl_float4 RetColor = ReadAcc.read(Coord, Sampler); |
| 80 | + ReadDataBufAcc[0] = RetColor; |
| 81 | + }); |
| 82 | + }); |
| 83 | + } |
| 84 | + validateReadData(ReadData, ExpectedColor); |
| 85 | +} |
| 86 | + |
| 87 | +void checkSamplerNearest() { |
| 88 | + |
| 89 | + // create image: |
| 90 | + char host_ptr[100]; |
| 91 | + for (int i = 0; i < 100; i++) |
| 92 | + host_ptr[i] = i; |
| 93 | + |
| 94 | + // Calling only valid configurations. |
| 95 | + // A. coordinate normalization mode::normalized |
| 96 | + // addressing_mode::mirrored_repeat |
| 97 | + { |
| 98 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, |
| 99 | + 0.0f); // Out-of-range mirrored_repeat mode |
| 100 | + auto Sampler = s::sampler(s::coordinate_normalization_mode::normalized, |
| 101 | + s::addressing_mode::mirrored_repeat, |
| 102 | + s::filtering_mode::nearest); |
| 103 | + checkReadSampler<1>(host_ptr, Sampler, Coord, |
| 104 | + s::cl_float4((56.0f / 127.0f), (57.0f / 127.0f), |
| 105 | + (58.0f / 127.0f), |
| 106 | + (59.0f / 127.0f)) /*Expected Value*/); |
| 107 | + } |
| 108 | + |
| 109 | + // addressing_mode::repeat |
| 110 | + { |
| 111 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, 0.0f); // Out-of-range repeat mode |
| 112 | + auto Sampler = |
| 113 | + s::sampler(s::coordinate_normalization_mode::normalized, |
| 114 | + s::addressing_mode::repeat, s::filtering_mode::nearest); |
| 115 | + checkReadSampler<2>(host_ptr, Sampler, Coord, |
| 116 | + s::cl_float4((56.0f / 127.0f), (57.0f / 127.0f), |
| 117 | + (58.0f / 127.0f), |
| 118 | + (59.0f / 127.0f)) /*Expected Value*/); |
| 119 | + } |
| 120 | + |
| 121 | + // addressing_mode::clamp_to_edge |
| 122 | + { |
| 123 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, 0.0f); // Out-of-range Edge Color |
| 124 | + auto Sampler = s::sampler(s::coordinate_normalization_mode::normalized, |
| 125 | + s::addressing_mode::clamp_to_edge, |
| 126 | + s::filtering_mode::nearest); |
| 127 | + checkReadSampler<3>(host_ptr, Sampler, Coord, |
| 128 | + s::cl_float4((88.0f / 127.0f), (89.0f / 127.0f), |
| 129 | + (90.0f / 127.0f), |
| 130 | + (91.0f / 127.0f)) /*Expected Value*/); |
| 131 | + } |
| 132 | + |
| 133 | + // addressing_mode::clamp |
| 134 | + { |
| 135 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, 0.0f); // Out-of-range Border Color |
| 136 | + auto Sampler = |
| 137 | + s::sampler(s::coordinate_normalization_mode::normalized, |
| 138 | + s::addressing_mode::clamp, s::filtering_mode::nearest); |
| 139 | + checkReadSampler<4>( |
| 140 | + host_ptr, Sampler, Coord, |
| 141 | + s::cl_float4(0.0f, 0.0f, 0.0f, 0.0f) /*Expected Value*/); |
| 142 | + } |
| 143 | + |
| 144 | + // addressing_mode::none |
| 145 | + { |
| 146 | + s::cl_float4 Coord(0.0f, 0.5f, 0.75f, |
| 147 | + 0.0f); // In-range for consistent return value. |
| 148 | + auto Sampler = |
| 149 | + s::sampler(s::coordinate_normalization_mode::normalized, |
| 150 | + s::addressing_mode::none, s::filtering_mode::nearest); |
| 151 | + checkReadSampler<5>(host_ptr, Sampler, Coord, |
| 152 | + s::cl_float4((80.0f / 127.0f), (81.0f / 127.0f), |
| 153 | + (82.0f / 127.0f), |
| 154 | + (83.0f / 127.0f)) /*Expected Value*/); |
| 155 | + } |
| 156 | + |
| 157 | + // B. coordinate_normalization_mode::unnormalized |
| 158 | + // addressing_mode::clamp_to_edge |
| 159 | + { |
| 160 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, 0.0f); |
| 161 | + auto Sampler = s::sampler(s::coordinate_normalization_mode::unnormalized, |
| 162 | + s::addressing_mode::clamp_to_edge, |
| 163 | + s::filtering_mode::nearest); |
| 164 | + checkReadSampler<6>(host_ptr, Sampler, Coord, |
| 165 | + s::cl_float4((56.0f / 127.0f), (57.0f / 127.0f), |
| 166 | + (58.0f / 127.0f), |
| 167 | + (59.0f / 127.0f)) /*Expected Value*/); |
| 168 | + } |
| 169 | + |
| 170 | + // addressing_mode::clamp |
| 171 | + { |
| 172 | + s::cl_float4 Coord(0.0f, 1.5f, 2.5f, 0.0f); |
| 173 | + auto Sampler = |
| 174 | + s::sampler(s::coordinate_normalization_mode::unnormalized, |
| 175 | + s::addressing_mode::clamp, s::filtering_mode::nearest); |
| 176 | + checkReadSampler<7>(host_ptr, Sampler, Coord, |
| 177 | + s::cl_float4((56.0f / 127.0f), (57.0f / 127.0f), |
| 178 | + (58.0f / 127.0f), |
| 179 | + (59.0f / 127.0f)) /*Expected Value*/); |
| 180 | + } |
| 181 | + |
| 182 | + // addressing_mode::none |
| 183 | + { |
| 184 | + s::cl_float4 Coord(0.0f, 1.0f, 2.0f, |
| 185 | + 0.0f); // In-range for consistent return value. |
| 186 | + auto Sampler = |
| 187 | + s::sampler(s::coordinate_normalization_mode::unnormalized, |
| 188 | + s::addressing_mode::none, s::filtering_mode::nearest); |
| 189 | + checkReadSampler<8>(host_ptr, Sampler, Coord, |
| 190 | + s::cl_float4((56.0f / 127.0f), (57.0f / 127.0f), |
| 191 | + (58.0f / 127.0f), |
| 192 | + (59.0f / 127.0f)) /*Expected Value*/); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +void checkSamplerLinear(){ |
| 197 | + // TODO. Implement this code. |
| 198 | +}; |
| 199 | + |
| 200 | +int main() { |
| 201 | + |
| 202 | + checkSamplerNearest(); |
| 203 | + // checkSamplerLinear(); |
| 204 | +} |
0 commit comments