Skip to content

Commit 75a5e28

Browse files
authored
[SYCL][E2E][Bindless] Fix access to mapped memory of 3-channel vulkan image (#16899)
We can't use sycl::vec<T, 3> to access mapped memory of 3-channel vulkan image because the sycl type is aligned to 4 elements but there are only 3 elements per pixel in vulkan image memory.
1 parent 6177c62 commit 75a5e28

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

sycl/test-e2e/bindless_images/helpers/common.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ bool equal_vec(sycl::vec<DType, NChannels> v1, sycl::vec<DType, NChannels> v2) {
102102
return true;
103103
}
104104

105-
template <typename DType, int NChannels>
106-
static void fill_rand(std::vector<sycl::vec<DType, NChannels>> &v,
105+
template <typename T>
106+
static void fill_rand(std::vector<T> &v,
107107
int seed = std::default_random_engine::default_seed) {
108108
assert(!v.empty());
109+
using DType = sycl::detail::get_elem_type_t<T>;
110+
constexpr int NChannels = sycl::detail::get_vec_size<T>::size;
109111
std::default_random_engine generator;
110112
generator.seed(seed);
111113
auto distribution = [&]() {
@@ -120,10 +122,12 @@ static void fill_rand(std::vector<sycl::vec<DType, NChannels>> &v,
120122
}
121123
}();
122124
for (int i = 0; i < v.size(); ++i) {
123-
sycl::vec<DType, NChannels> temp;
124-
125-
for (int j = 0; j < NChannels; j++) {
126-
temp[j] = static_cast<DType>(distribution(generator));
125+
T temp;
126+
if constexpr (NChannels == 1) {
127+
temp = static_cast<DType>(distribution(generator));
128+
} else {
129+
for (int j = 0; j < NChannels; ++j)
130+
temp[j] = static_cast<DType>(distribution(generator));
127131
}
128132

129133
v[i] = temp;

sycl/test-e2e/bindless_images/vulkan_interop/sampled_images.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,6 @@ bool run_test(sycl::range<NDims> dims, sycl::range<NDims> localSize,
275275
imgType = VK_IMAGE_TYPE_3D;
276276
}
277277

278-
using VecType = sycl::vec<DType, NChannels>;
279-
280278
VkFormat format = vkutil::to_vulkan_format(COrder, CType);
281279
const size_t imageSizeBytes = numElems * NChannels * sizeof(DType);
282280

@@ -316,7 +314,7 @@ bool run_test(sycl::range<NDims> dims, sycl::range<NDims> localSize,
316314

317315
printString("Populating staging buffer\n");
318316
// Populate staging memory
319-
VecType *inputStagingData = nullptr;
317+
DType *inputStagingData = nullptr;
320318
VK_CHECK_CALL(vkMapMemory(vk_device, inputStagingMemory, 0 /*offset*/,
321319
imageSizeBytes, 0 /*flags*/,
322320
(void **)&inputStagingData));
@@ -329,8 +327,9 @@ bool run_test(sycl::range<NDims> dims, sycl::range<NDims> localSize,
329327
return i;
330328
};
331329
for (int i = 0; i < numElems; ++i) {
332-
inputStagingData[i] =
333-
bindless_helpers::init_vector<DType, NChannels>(getInputValue(i));
330+
DType v = getInputValue(i);
331+
for (int j = 0; j < NChannels; ++j)
332+
inputStagingData[i * NChannels + j] = v;
334333
}
335334
vkUnmapMemory(vk_device, inputStagingMemory);
336335

sycl/test-e2e/bindless_images/vulkan_interop/unsampled_images.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -303,31 +303,29 @@ bool run_test(sycl::range<NDims> dims, sycl::range<NDims> local_size,
303303

304304
printString("Populating staging buffer\n");
305305
// Populate staging memory
306-
using VecType = sycl::vec<DType, NChannels>;
307-
auto init =
308-
bindless_helpers::init_vector<DType, NChannels>(static_cast<DType>(0));
309-
310-
std::vector<VecType> input_vector_0(num_elems, init);
306+
std::vector<DType> input_vector_0(num_elems * NChannels,
307+
static_cast<DType>(0));
311308
std::srand(seed);
312309
bindless_helpers::fill_rand(input_vector_0);
313310

314-
VecType *inputStagingData = nullptr;
311+
DType *inputStagingData = nullptr;
315312
VK_CHECK_CALL(vkMapMemory(vk_device, inVkImgRes1.stagingMemory, 0 /*offset*/,
316313
imageSizeBytes, 0 /*flags*/,
317314
(void **)&inputStagingData));
318-
for (int i = 0; i < num_elems; ++i) {
315+
for (int i = 0; i < (num_elems * NChannels); ++i) {
319316
inputStagingData[i] = input_vector_0[i];
320317
}
321318
vkUnmapMemory(vk_device, inVkImgRes1.stagingMemory);
322319

323-
std::vector<VecType> input_vector_1(num_elems, init);
320+
std::vector<DType> input_vector_1(num_elems * NChannels,
321+
static_cast<DType>(0));
324322
std::srand(seed);
325323
bindless_helpers::fill_rand(input_vector_1);
326324

327325
VK_CHECK_CALL(vkMapMemory(vk_device, inVkImgRes2.stagingMemory, 0 /*offset*/,
328326
imageSizeBytes, 0 /*flags*/,
329327
(void **)&inputStagingData));
330-
for (int i = 0; i < num_elems; ++i) {
328+
for (int i = 0; i < (num_elems * NChannels); ++i) {
331329
inputStagingData[i] = input_vector_1[i];
332330
}
333331
vkUnmapMemory(vk_device, inVkImgRes2.stagingMemory);
@@ -535,22 +533,21 @@ bool run_test(sycl::range<NDims> dims, sycl::range<NDims> local_size,
535533
printString("Validating\n");
536534
// Validate that SYCL made changes to the memory
537535
bool validated = true;
538-
VecType *outputStagingData = nullptr;
536+
DType *outputStagingData = nullptr;
539537
VK_CHECK_CALL(vkMapMemory(vk_device, outVkImgRes.stagingMemory, 0 /*offset*/,
540538
imageSizeBytes, 0 /*flags*/,
541539
(void **)&outputStagingData));
542-
for (int i = 0; i < num_elems; ++i) {
543-
VecType expected = input_vector_0[i] + input_vector_1[i];
544-
for (int j = 0; j < NChannels; ++j) {
545-
// Use helper function to determine if data is accepted
546-
// For integers, exact results are expected
547-
// For floats, accepted error variance is passed
548-
if (!util::is_equal(outputStagingData[i][j], expected[j])) {
549-
std::cerr << "Result mismatch! actual[" << i << "][" << j
550-
<< "] == " << outputStagingData[i][j]
551-
<< " : expected == " << expected[j] << "\n";
552-
validated = false;
553-
}
540+
541+
for (int i = 0; i < (num_elems * NChannels); ++i) {
542+
DType expected = input_vector_0[i] + input_vector_1[i];
543+
// Use helper function to determine if data is accepted
544+
// For integers, exact results are expected
545+
// For floats, accepted error variance is passed
546+
if (!util::is_equal(outputStagingData[i], expected)) {
547+
std::cerr << "Result mismatch! actual[" << i
548+
<< "] == " << outputStagingData[i]
549+
<< " : expected == " << expected << "\n";
550+
validated = false;
554551
}
555552
if (!validated)
556553
break;

0 commit comments

Comments
 (0)