DPC++ Runtime
Runtime libraries for oneAPI DPC++
bindless_images.cpp
Go to the documentation of this file.
1 //==----------- bindless_images.hpp --- SYCL bindless images ---------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <sycl/detail/common.hpp>
10 #include <sycl/detail/pi.hpp>
12 #include <sycl/sampler.hpp>
13 
14 #include <detail/context_impl.hpp>
15 #include <detail/image_impl.hpp>
17 #include <detail/queue_impl.hpp>
18 
19 #include <memory>
20 
21 namespace sycl {
22 inline namespace _V1 {
23 namespace ext::oneapi::experimental {
24 
26  pi_image_format &piFormat, size_t pitch = 0) {
27  piDesc = {};
28  piDesc.image_width = desc.width;
29  piDesc.image_height = desc.height;
30  piDesc.image_depth = desc.depth;
31 
32  if (desc.array_size > 1) {
33  // Image array or cubemap
34  piDesc.image_type = desc.type == image_type::cubemap
38  } else {
39  piDesc.image_type =
40  desc.depth > 0
43  }
44 
45  piDesc.image_row_pitch = pitch;
46  piDesc.image_array_size = desc.array_size;
47  piDesc.image_slice_pitch = 0;
48  piDesc.num_mip_levels = desc.num_levels;
49  piDesc.num_samples = 0;
50  piDesc.buffer = nullptr;
51 
52  piFormat = {};
53  piFormat.image_channel_data_type =
55  piFormat.image_channel_order =
57 }
58 
60  const device &syclDevice,
61  const context &syclContext)
62  : descriptor(desc), syclDevice(syclDevice), syclContext(syclContext) {
63  handle = alloc_image_mem(desc, syclDevice, syclContext);
64 }
65 
67  free_image_mem(this->get_handle(), this->get_descriptor().type,
68  this->get_device(), this->get_context());
69 }
70 
71 __SYCL_EXPORT
73  const sycl::device &syclDevice,
74  const sycl::context &syclContext) {
75  impl =
76  std::make_shared<detail::image_mem_impl>(desc, syclDevice, syclContext);
77 }
78 
79 __SYCL_EXPORT
80 image_mem::image_mem(const image_descriptor &desc, const sycl::queue &syclQueue)
81  : image_mem(desc, syclQueue.get_device(), syclQueue.get_context()) {}
82 
83 __SYCL_EXPORT sycl::range<3> image_mem::get_range() const {
84  auto desc = impl->get_descriptor();
85  return {desc.width, desc.height, desc.depth};
86 }
87 
89  return impl->get_descriptor().channel_type;
90 }
91 
93  return impl->get_descriptor().channel_order;
94 }
95 
96 __SYCL_EXPORT unsigned int image_mem::get_num_channels() const {
98  impl->get_descriptor().channel_order);
99 }
100 
101 __SYCL_EXPORT image_type image_mem::get_type() const {
102  return impl->get_descriptor().type;
103 }
104 
105 __SYCL_EXPORT image_mem_handle
106 image_mem::get_mip_level_mem_handle(const unsigned int level) const {
108  impl->get_handle(), level, impl->get_device(), impl->get_context());
109 }
110 
111 __SYCL_EXPORT void destroy_image_handle(unsampled_image_handle &imageHandle,
112  const sycl::device &syclDevice,
113  const sycl::context &syclContext) {
114  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
115  sycl::detail::getSyclObjImpl(syclContext);
116  pi_context C = CtxImpl->getHandleRef();
117  std::shared_ptr<sycl::detail::device_impl> DevImpl =
118  sycl::detail::getSyclObjImpl(syclDevice);
119  pi_device Device = DevImpl->getHandleRef();
120  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
121  pi_image_handle piImageHandle = imageHandle.raw_handle;
122 
123  Plugin->call<sycl::errc::runtime,
125  C, Device, piImageHandle);
126 }
127 
128 __SYCL_EXPORT void destroy_image_handle(unsampled_image_handle &imageHandle,
129  const sycl::queue &syclQueue) {
130  destroy_image_handle(imageHandle, syclQueue.get_device(),
131  syclQueue.get_context());
132 }
133 
134 __SYCL_EXPORT void destroy_image_handle(sampled_image_handle &imageHandle,
135  const sycl::device &syclDevice,
136  const sycl::context &syclContext) {
137  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
138  sycl::detail::getSyclObjImpl(syclContext);
139  pi_context C = CtxImpl->getHandleRef();
140  std::shared_ptr<sycl::detail::device_impl> DevImpl =
141  sycl::detail::getSyclObjImpl(syclDevice);
142  pi_device Device = DevImpl->getHandleRef();
143  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
144  pi_image_handle piImageHandle = imageHandle.raw_handle;
145 
146  Plugin->call<sycl::errc::runtime,
148  C, Device, piImageHandle);
149 }
150 
151 __SYCL_EXPORT void destroy_image_handle(sampled_image_handle &imageHandle,
152  const sycl::queue &syclQueue) {
153  destroy_image_handle(imageHandle, syclQueue.get_device(),
154  syclQueue.get_context());
155 }
156 
157 __SYCL_EXPORT image_mem_handle
158 alloc_image_mem(const image_descriptor &desc, const sycl::device &syclDevice,
159  const sycl::context &syclContext) {
160  desc.verify();
161 
162  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
163  sycl::detail::getSyclObjImpl(syclContext);
164  pi_context C = CtxImpl->getHandleRef();
165  std::shared_ptr<sycl::detail::device_impl> DevImpl =
166  sycl::detail::getSyclObjImpl(syclDevice);
167  pi_device Device = DevImpl->getHandleRef();
168  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
169 
170  pi_image_desc piDesc;
171  pi_image_format piFormat;
172  populate_pi_structs(desc, piDesc, piFormat);
173 
174  image_mem_handle retHandle;
175 
176  // Call impl.
177  Plugin->call<sycl::errc::memory_allocation,
179  C, Device, &piFormat, &piDesc, &retHandle.raw_handle);
180 
181  return retHandle;
182 }
183 
185  const sycl::queue &syclQueue) {
186  return alloc_image_mem(desc, syclQueue.get_device(), syclQueue.get_context());
187 }
188 
189 __SYCL_EXPORT_DEPRECATED("Distinct mipmap allocs are deprecated. "
190  "Instead use alloc_image_mem().")
192  const sycl::device &syclDevice,
193  const sycl::context &syclContext) {
194  desc.verify();
195 
196  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
197  sycl::detail::getSyclObjImpl(syclContext);
198  pi_context C = CtxImpl->getHandleRef();
199  std::shared_ptr<sycl::detail::device_impl> DevImpl =
200  sycl::detail::getSyclObjImpl(syclDevice);
201  pi_device Device = DevImpl->getHandleRef();
202  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
203 
204  pi_image_desc piDesc;
205  pi_image_format piFormat;
206  populate_pi_structs(desc, piDesc, piFormat);
207 
208  // Call impl.
209  image_mem_handle retHandle;
210  Plugin->call<sycl::errc::memory_allocation,
212  C, Device, &piFormat, &piDesc, &retHandle.raw_handle);
213 
214  return retHandle;
215 }
216 
217 __SYCL_EXPORT_DEPRECATED("Distinct mipmap allocs are deprecated. "
218  "Instead use alloc_image_mem().")
220  const sycl::queue &syclQueue) {
221  return alloc_mipmap_mem(desc, syclQueue.get_device(),
222  syclQueue.get_context());
223 }
224 
226  const image_mem_handle mipMem, unsigned int level,
227  const sycl::device &syclDevice, const sycl::context &syclContext) {
228 
229  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
230  sycl::detail::getSyclObjImpl(syclContext);
231  pi_context C = CtxImpl->getHandleRef();
232  std::shared_ptr<sycl::detail::device_impl> DevImpl =
233  sycl::detail::getSyclObjImpl(syclDevice);
234  pi_device Device = DevImpl->getHandleRef();
235  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
236 
237  // Call impl.
238  image_mem_handle individual_image;
239  Plugin->call<sycl::errc::runtime,
241  C, Device, mipMem.raw_handle, level, &individual_image.raw_handle);
242 
243  return individual_image;
244 }
245 
246 __SYCL_EXPORT image_mem_handle
248  const sycl::queue &syclQueue) {
249  return get_mip_level_mem_handle(mipMem, level, syclQueue.get_device(),
250  syclQueue.get_context());
251 }
252 
253 __SYCL_EXPORT void free_image_mem(image_mem_handle memHandle,
254  image_type imageType,
255  const sycl::device &syclDevice,
256  const sycl::context &syclContext) {
257  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
258  sycl::detail::getSyclObjImpl(syclContext);
259  pi_context C = CtxImpl->getHandleRef();
260  std::shared_ptr<sycl::detail::device_impl> DevImpl =
261  sycl::detail::getSyclObjImpl(syclDevice);
262  pi_device Device = DevImpl->getHandleRef();
263  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
264 
265  if (memHandle.raw_handle != nullptr) {
266  if (imageType == image_type::mipmap) {
267  Plugin->call<sycl::errc::memory_allocation,
269  C, Device, memHandle.raw_handle);
270  } else if (imageType == image_type::standard ||
271  imageType == image_type::array ||
272  imageType == image_type::cubemap) {
273  Plugin->call<sycl::errc::memory_allocation,
275  C, Device, memHandle.raw_handle);
276  } else {
277  throw sycl::exception(sycl::make_error_code(sycl::errc::invalid),
278  "Invalid image type to free");
279  }
280  }
281 }
282 
283 __SYCL_EXPORT void free_image_mem(image_mem_handle memHandle,
284  image_type imageType,
285  const sycl::queue &syclQueue) {
286  free_image_mem(memHandle, imageType, syclQueue.get_device(),
287  syclQueue.get_context());
288 }
289 
290 __SYCL_EXPORT_DEPRECATED("Distinct image frees are deprecated. "
291  "Instead use overload that accepts image_type.")
292 void free_image_mem(image_mem_handle memHandle, const sycl::device &syclDevice,
293  const sycl::context &syclContext) {
294  return free_image_mem(memHandle, image_type::standard, syclDevice,
295  syclContext);
296 }
297 
298 __SYCL_EXPORT_DEPRECATED("Distinct image frees are deprecated. "
299  "Instead use overload that accepts image_type.")
300 void free_image_mem(image_mem_handle memHandle, const sycl::queue &syclQueue) {
301  free_image_mem(memHandle, syclQueue.get_device(), syclQueue.get_context());
302 }
303 
304 __SYCL_EXPORT_DEPRECATED(
305  "Distinct mipmap frees are deprecated. "
306  "Instead use free_image_mem() that accepts image_type.")
308  const sycl::device &syclDevice,
309  const sycl::context &syclContext) {
310  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
311  sycl::detail::getSyclObjImpl(syclContext);
312  pi_context C = CtxImpl->getHandleRef();
313  std::shared_ptr<sycl::detail::device_impl> DevImpl =
314  sycl::detail::getSyclObjImpl(syclDevice);
315  pi_device Device = DevImpl->getHandleRef();
316  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
317 
318  Plugin->call<sycl::errc::memory_allocation,
320  C, Device, memoryHandle.raw_handle);
321 }
322 
323 __SYCL_EXPORT_DEPRECATED(
324  "Distinct mipmap frees are deprecated. "
325  "Instead use free_image_mem() that accepts image_type.")
327  const sycl::queue &syclQueue) {
328  free_mipmap_mem(memoryHandle, syclQueue.get_device(),
329  syclQueue.get_context());
330 }
331 
332 __SYCL_EXPORT unsampled_image_handle
334  const sycl::device &syclDevice, const sycl::context &syclContext) {
335  return create_image(imgMem.get_handle(), desc, syclDevice, syclContext);
336 }
337 
338 __SYCL_EXPORT unsampled_image_handle
340  const sycl::queue &syclQueue) {
341  return create_image(imgMem.get_handle(), desc, syclQueue.get_device(),
342  syclQueue.get_context());
343 }
344 
345 __SYCL_EXPORT unsampled_image_handle
347  const sycl::device &syclDevice, const sycl::context &syclContext) {
348  desc.verify();
349 
350  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
351  sycl::detail::getSyclObjImpl(syclContext);
352  pi_context C = CtxImpl->getHandleRef();
353  std::shared_ptr<sycl::detail::device_impl> DevImpl =
354  sycl::detail::getSyclObjImpl(syclDevice);
355  pi_device Device = DevImpl->getHandleRef();
356  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
357 
358  pi_image_desc piDesc;
359  pi_image_format piFormat;
360  populate_pi_structs(desc, piDesc, piFormat);
361 
362  // Call impl.
363  pi_image_handle piImageHandle;
364  pi_mem piImage;
365  Plugin->call<sycl::errc::runtime,
367  C, Device, memHandle.raw_handle, &piFormat, &piDesc, &piImage,
368  &piImageHandle);
369 
370  return unsampled_image_handle{piImageHandle};
371 }
372 
373 __SYCL_EXPORT unsampled_image_handle
375  const sycl::queue &syclQueue) {
376  return create_image(memHandle, desc, syclQueue.get_device(),
377  syclQueue.get_context());
378 }
379 
380 __SYCL_EXPORT sampled_image_handle
382  const image_descriptor &desc, const sycl::device &syclDevice,
383  const sycl::context &syclContext) {
384  return create_image(memHandle.raw_handle, 0 /*pitch*/, sampler, desc,
385  syclDevice, syclContext);
386 }
387 
388 __SYCL_EXPORT sampled_image_handle
390  const image_descriptor &desc, const sycl::queue &syclQueue) {
391  return create_image(memHandle, sampler, desc, syclQueue.get_device(),
392  syclQueue.get_context());
393 }
394 
395 __SYCL_EXPORT sampled_image_handle
397  const image_descriptor &desc, const sycl::device &syclDevice,
398  const sycl::context &syclContext) {
399  return create_image(imgMem.get_handle().raw_handle, 0 /*pitch*/, sampler,
400  desc, syclDevice, syclContext);
401 }
402 
403 __SYCL_EXPORT sampled_image_handle
405  const image_descriptor &desc, const sycl::queue &syclQueue) {
406  return create_image(imgMem.get_handle().raw_handle, 0 /*pitch*/, sampler,
407  desc, syclQueue.get_device(), syclQueue.get_context());
408 }
409 
410 __SYCL_EXPORT sampled_image_handle
411 create_image(void *devPtr, size_t pitch, const bindless_image_sampler &sampler,
412  const image_descriptor &desc, const sycl::device &syclDevice,
413  const sycl::context &syclContext) {
414  desc.verify();
415 
416  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
417  sycl::detail::getSyclObjImpl(syclContext);
418  pi_context C = CtxImpl->getHandleRef();
419  std::shared_ptr<sycl::detail::device_impl> DevImpl =
420  sycl::detail::getSyclObjImpl(syclDevice);
421  pi_device Device = DevImpl->getHandleRef();
422  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
423 
424  const pi_sampler_properties sProps[] = {
426  static_cast<pi_sampler_properties>(sampler.coordinate),
428  static_cast<pi_sampler_properties>(sampler.addressing[0]),
430  static_cast<pi_sampler_properties>(sampler.addressing[1]),
432  static_cast<pi_sampler_properties>(sampler.addressing[2]),
434  static_cast<pi_sampler_properties>(sampler.filtering),
436  static_cast<pi_sampler_properties>(sampler.mipmap_filtering),
438  static_cast<pi_sampler_properties>(sampler.cubemap_filtering),
439  0};
440 
441  pi_sampler piSampler = {};
442  Plugin->call<sycl::errc::runtime,
444  C, sProps, sampler.min_mipmap_level_clamp, sampler.max_mipmap_level_clamp,
445  sampler.max_anisotropy, &piSampler);
446 
447  pi_image_desc piDesc;
448  pi_image_format piFormat;
449  populate_pi_structs(desc, piDesc, piFormat, pitch);
450 
451  // Call impl.
452  pi_mem piImage;
453  pi_image_handle piImageHandle;
454  Plugin->call<sycl::errc::runtime,
456  C, Device, devPtr, &piFormat, &piDesc, piSampler, &piImage,
457  &piImageHandle);
458 
459  return sampled_image_handle{piImageHandle};
460 }
461 
462 __SYCL_EXPORT sampled_image_handle
463 create_image(void *devPtr, size_t pitch, const bindless_image_sampler &sampler,
464  const image_descriptor &desc, const sycl::queue &syclQueue) {
465  return create_image(devPtr, pitch, sampler, desc, syclQueue.get_device(),
466  syclQueue.get_context());
467 }
468 
469 template <>
472  const sycl::device &syclDevice, const sycl::context &syclContext) {
473  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
474  sycl::detail::getSyclObjImpl(syclContext);
475  pi_context C = CtxImpl->getHandleRef();
476  std::shared_ptr<sycl::detail::device_impl> DevImpl =
477  sycl::detail::getSyclObjImpl(syclDevice);
478  pi_device Device = DevImpl->getHandleRef();
479  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
480 
481  pi_interop_mem_handle piInteropMem;
482  Plugin->call<sycl::errc::invalid,
484  C, Device, externalMem.size_in_bytes,
485  externalMem.external_resource.file_descriptor, &piInteropMem);
486 
487  return interop_mem_handle{piInteropMem};
488 }
489 
490 template <>
493  const sycl::queue &syclQueue) {
495  externalMem, syclQueue.get_device(), syclQueue.get_context());
496 }
497 
498 template <>
499 __SYCL_EXPORT_DEPRECATED(
500  "import_external_memory templated by external_mem_fd is deprecated."
501  "Template with resource_fd instead.")
504  const sycl::device &syclDevice, const sycl::context &syclContext) {
505 
507  extMem.external_resource.file_descriptor =
508  externalMem.external_resource.file_descriptor;
509  extMem.size_in_bytes = externalMem.size_in_bytes;
510  return import_external_memory<resource_fd>(extMem, syclDevice, syclContext);
511 }
512 
513 template <>
514 __SYCL_EXPORT_DEPRECATED(
515  "import_external_memory templated by external_mem_fd is deprecated."
516  "Template with resource_fd instead.")
519  const sycl::queue &syclQueue) {
521  externalMem, syclQueue.get_device(), syclQueue.get_context());
522 }
523 
524 __SYCL_EXPORT
526  const image_descriptor &desc,
527  const sycl::device &syclDevice,
528  const sycl::context &syclContext) {
529  desc.verify();
530 
531  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
532  sycl::detail::getSyclObjImpl(syclContext);
533  pi_context C = CtxImpl->getHandleRef();
534  std::shared_ptr<sycl::detail::device_impl> DevImpl =
535  sycl::detail::getSyclObjImpl(syclDevice);
536  pi_device Device = DevImpl->getHandleRef();
537  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
538 
539  pi_image_desc piDesc;
540  pi_image_format piFormat;
541  populate_pi_structs(desc, piDesc, piFormat);
542 
543  pi_interop_mem_handle piInteropMem{memHandle.raw_handle};
544 
545  image_mem_handle retHandle;
546  Plugin->call<sycl::errc::invalid,
548  C, Device, &piFormat, &piDesc, piInteropMem, &retHandle.raw_handle);
549 
550  return image_mem_handle{retHandle};
551 }
552 
553 __SYCL_EXPORT
555  const image_descriptor &desc,
556  const sycl::queue &syclQueue) {
557  return map_external_image_memory(memHandle, desc, syclQueue.get_device(),
558  syclQueue.get_context());
559 }
560 
561 __SYCL_EXPORT_DEPRECATED("map_external_memory_array is deprecated."
562  "use map_external_image_memory")
564  const image_descriptor &desc,
565  const sycl::device &syclDevice,
566  const sycl::context &syclContext) {
567  return map_external_image_memory(memHandle, desc, syclDevice, syclContext);
568 }
569 
570 __SYCL_EXPORT_DEPRECATED("map_external_memory_array is deprecated."
571  "use map_external_image_memory")
573  const image_descriptor &desc,
574  const sycl::queue &syclQueue) {
575  return map_external_memory_array(memHandle, desc, syclQueue.get_device(),
576  syclQueue.get_context());
577 }
578 
579 __SYCL_EXPORT void release_external_memory(interop_mem_handle interopMem,
580  const sycl::device &syclDevice,
581  const sycl::context &syclContext) {
582  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
583  sycl::detail::getSyclObjImpl(syclContext);
584  pi_context C = CtxImpl->getHandleRef();
585  std::shared_ptr<sycl::detail::device_impl> DevImpl =
586  sycl::detail::getSyclObjImpl(syclDevice);
587  pi_device Device = DevImpl->getHandleRef();
588  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
589 
590  Plugin->call<sycl::errc::invalid,
592  C, Device, (pi_interop_mem_handle)interopMem.raw_handle);
593 }
594 
595 __SYCL_EXPORT void release_external_memory(interop_mem_handle interopMem,
596  const sycl::queue &syclQueue) {
597  release_external_memory(interopMem, syclQueue.get_device(),
598  syclQueue.get_context());
599 }
600 
601 template <>
603  external_semaphore_descriptor<resource_fd> externalSemaphoreDesc,
604  const sycl::device &syclDevice, const sycl::context &syclContext) {
605  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
606  sycl::detail::getSyclObjImpl(syclContext);
607  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
608  pi_context C = CtxImpl->getHandleRef();
609  std::shared_ptr<sycl::detail::device_impl> DevImpl =
610  sycl::detail::getSyclObjImpl(syclDevice);
611  pi_device Device = DevImpl->getHandleRef();
612 
613  pi_interop_semaphore_handle piInteropSemaphore;
614 
615  Plugin->call<sycl::errc::invalid,
617  C, Device, externalSemaphoreDesc.external_resource.file_descriptor,
618  &piInteropSemaphore);
619 
620  return interop_semaphore_handle{piInteropSemaphore};
621 }
622 
623 template <>
625  external_semaphore_descriptor<resource_fd> externalSemaphoreDesc,
626  const sycl::queue &syclQueue) {
628  externalSemaphoreDesc, syclQueue.get_device(), syclQueue.get_context());
629 }
630 
631 template <>
632 __SYCL_EXPORT_DEPRECATED("import_external_semaphore templated by "
633  "external_semaphore_fd is deprecated."
634  "Template with resource_fd instead.")
637  const sycl::device &syclDevice, const sycl::context &syclContext) {
638 
640  extSem.external_resource.file_descriptor =
641  externalSemaphoreDesc.external_resource.file_descriptor;
642  return import_external_semaphore<resource_fd>(extSem, syclDevice,
643  syclContext);
644 }
645 
646 template <>
647 __SYCL_EXPORT_DEPRECATED("import_external_semaphore templated by "
648  "external_semaphore_fd is deprecated."
649  "Template with resource_fd instead.")
652  const sycl::queue &syclQueue) {
654  externalSemaphoreDesc, syclQueue.get_device(), syclQueue.get_context());
655 }
656 
657 __SYCL_EXPORT void
659  const sycl::device &syclDevice,
660  const sycl::context &syclContext) {
661  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
662  sycl::detail::getSyclObjImpl(syclContext);
663  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
664  pi_context C = CtxImpl->getHandleRef();
665  std::shared_ptr<sycl::detail::device_impl> DevImpl =
666  sycl::detail::getSyclObjImpl(syclDevice);
667  pi_device Device = DevImpl->getHandleRef();
668 
669  Plugin->call<sycl::errc::invalid,
671  C, Device, (pi_interop_semaphore_handle)semaphoreHandle.raw_handle);
672 }
673 
674 __SYCL_EXPORT void
676  const sycl::queue &syclQueue) {
677  destroy_external_semaphore(semaphoreHandle, syclQueue.get_device(),
678  syclQueue.get_context());
679 }
680 
681 __SYCL_EXPORT sycl::range<3> get_image_range(const image_mem_handle memHandle,
682  const sycl::device &syclDevice,
683  const sycl::context &syclContext) {
684  std::ignore = syclDevice;
685  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
686  sycl::detail::getSyclObjImpl(syclContext);
687  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
688 
689  size_t Width, Height, Depth;
690 
691  Plugin->call<sycl::errc::invalid,
693  memHandle.raw_handle, PI_IMAGE_INFO_WIDTH, &Width, nullptr);
694 
695  Plugin->call<sycl::errc::invalid,
697  memHandle.raw_handle, PI_IMAGE_INFO_HEIGHT, &Height, nullptr);
698 
699  Plugin->call<sycl::errc::invalid,
701  memHandle.raw_handle, PI_IMAGE_INFO_DEPTH, &Depth, nullptr);
702 
703  return {Width, Height, Depth};
704 }
705 
706 __SYCL_EXPORT sycl::range<3> get_image_range(const image_mem_handle memHandle,
707  const sycl::queue &syclQueue) {
708  return get_image_range(memHandle, syclQueue.get_device(),
709  syclQueue.get_context());
710 }
711 
712 __SYCL_EXPORT sycl::image_channel_type
714  const sycl::device &syclDevice,
715  const sycl::context &syclContext) {
716  std::ignore = syclDevice;
717  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
718  sycl::detail::getSyclObjImpl(syclContext);
719  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
720 
721  pi_image_format PIFormat;
722 
723  Plugin->call<sycl::errc::invalid,
725  memHandle.raw_handle, PI_IMAGE_INFO_FORMAT, &PIFormat, nullptr);
726 
727  image_channel_type ChannelType =
729 
730  return ChannelType;
731 }
732 
733 __SYCL_EXPORT sycl::image_channel_type
735  const sycl::queue &syclQueue) {
736  return get_image_channel_type(memHandle, syclQueue.get_device(),
737  syclQueue.get_context());
738 }
739 
740 __SYCL_EXPORT void *pitched_alloc_device(size_t *resultPitch,
741  size_t widthInBytes, size_t height,
742  unsigned int elementSizeBytes,
743  const sycl::device &syclDevice,
744  const sycl::context &syclContext) {
745  void *RetVal = nullptr;
746  if (widthInBytes == 0 || height == 0 || elementSizeBytes == 0) {
747  throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation),
748  "Cannot allocate pitched memory with zero size!");
749  }
750 
751  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
752  sycl::detail::getSyclObjImpl(syclContext);
753  if (CtxImpl->is_host()) {
754  throw sycl::exception(sycl::make_error_code(sycl::errc::memory_allocation),
755  "Cannot allocate pitched memory on host!");
756  }
757 
758  pi_context PiContext = CtxImpl->getHandleRef();
759  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
761 
762  PiDevice = sycl::detail::getSyclObjImpl(syclDevice)->getHandleRef();
763 
764  Plugin->call<sycl::errc::memory_allocation,
766  &RetVal, resultPitch, PiContext, PiDevice, nullptr, widthInBytes, height,
767  elementSizeBytes);
768 
769  return RetVal;
770 }
771 
772 __SYCL_EXPORT void *pitched_alloc_device(size_t *resultPitch,
773  size_t widthInBytes, size_t height,
774  unsigned int elementSizeBytes,
775  const sycl::queue &syclQueue) {
776  return pitched_alloc_device(resultPitch, widthInBytes, height,
777  elementSizeBytes, syclQueue.get_device(),
778  syclQueue.get_context());
779 }
780 
781 __SYCL_EXPORT void *pitched_alloc_device(size_t *resultPitch,
782  const image_descriptor &desc,
783  const sycl::queue &syclQueue) {
784  return pitched_alloc_device(resultPitch, desc, syclQueue.get_device(),
785  syclQueue.get_context());
786 }
787 
788 __SYCL_EXPORT void *pitched_alloc_device(size_t *resultPitch,
789  const image_descriptor &desc,
790  const sycl::device &syclDevice,
791  const sycl::context &syclContext) {
792  uint8_t numChannels =
794  unsigned int elementSizeBytes =
796 
797  size_t widthInBytes = desc.width * elementSizeBytes;
798  size_t height = desc.height;
799 
800  return pitched_alloc_device(resultPitch, widthInBytes, height,
801  elementSizeBytes, syclDevice, syclContext);
802 }
803 
804 __SYCL_EXPORT unsigned int
806  const sycl::device &syclDevice,
807  const sycl::context &syclContext) {
808  std::ignore = syclDevice;
809 
810  std::shared_ptr<sycl::detail::context_impl> CtxImpl =
811  sycl::detail::getSyclObjImpl(syclContext);
812  const sycl::detail::PluginPtr &Plugin = CtxImpl->getPlugin();
813  pi_image_format PIFormat;
814 
815  Plugin->call<sycl::errc::runtime,
817  memHandle.raw_handle, PI_IMAGE_INFO_FORMAT, &PIFormat, nullptr);
818 
819  image_channel_order Order =
821 
822  return static_cast<unsigned int>(sycl::detail::getImageNumberChannels(Order));
823 }
824 
825 __SYCL_EXPORT unsigned int
827  const sycl::queue &syclQueue) {
828  return get_image_num_channels(memHandle, syclQueue.get_device(),
829  syclQueue.get_context());
830 }
831 
832 } // namespace ext::oneapi::experimental
833 } // namespace _V1
834 } // namespace sycl
The context class represents a SYCL context on which kernel functions may be executed.
Definition: context.hpp:50
The SYCL device class encapsulates a single SYCL device on which kernels may be executed.
Definition: device.hpp:64
image_mem_impl(const image_descriptor &desc, const device &syclDevice, const context &syclContext)
sycl::image_channel_type get_channel_type() const
sycl::image_channel_order get_channel_order() const
raw_handle_type get_mip_level_mem_handle(const unsigned int level) const
Encapsulates a single SYCL queue which schedules kernels on a SYCL device.
Definition: queue.hpp:111
device get_device() const
Definition: queue.cpp:76
context get_context() const
Definition: queue.cpp:74
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
::pi_device PiDevice
Definition: pi.hpp:131
::pi_context PiContext
Definition: pi.hpp:135
uint8_t getImageElementSize(uint8_t NumChannels, image_channel_type Type)
Definition: image_impl.cpp:74
sycl::detail::pi::PiMemImageChannelOrder convertChannelOrder(image_channel_order Order)
Definition: image_impl.cpp:111
decltype(Obj::impl) getSyclObjImpl(const Obj &SyclObject)
Definition: impl_utils.hpp:30
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
sycl::detail::pi::PiMemImageChannelType convertChannelType(image_channel_type Type)
Definition: image_impl.cpp:187
uint8_t getImageNumberChannels(image_channel_order Order)
Definition: image_impl.cpp:47
void populate_pi_structs(const image_descriptor &desc, pi_image_desc &piDesc, pi_image_format &piFormat, size_t pitch=0)
image_mem_handle map_external_image_memory(interop_mem_handle memHandle, const image_descriptor &desc, const sycl::device &syclDevice, const sycl::context &syclContext)
Maps an interop memory handle to an image memory handle (which may have a device optimized memory lay...
sycl::range< 3 > get_image_range(const image_mem_handle memHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Get the range that describes the image's dimensions.
unsigned int get_image_num_channels(const image_mem_handle memHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Get the number of channels that describes the image memory.
interop_mem_handle import_external_memory< resource_fd >(external_mem_descriptor< resource_fd > externalMem, const sycl::device &syclDevice, const sycl::context &syclContext)
void free_image_mem(image_mem_handle handle, const sycl::device &syclDevice, const sycl::context &syclContext)
[Deprecated] Free image memory
interop_mem_handle import_external_memory(external_mem_descriptor< ExternalMemHandleType > externalMem, const sycl::device &syclDevice, const sycl::context &syclContext)
Import external memory taking an external memory handle (the type of which is dependent on the OS & e...
unsampled_image_handle create_image(image_mem &memHandle, const image_descriptor &desc, const sycl::device &syclDevice, const sycl::context &syclContext)
Create an image and return the device image handle.
void * pitched_alloc_device(size_t *resultPitch, size_t widthInBytes, size_t height, unsigned int elementSizeBytes, const sycl::queue &syclQueue)
Allocate pitched USM image memory.
image_mem_handle map_external_memory_array(interop_mem_handle memHandle, const image_descriptor &desc, const sycl::device &syclDevice, const sycl::context &syclContext)
[Deprecated] Maps an interop memory handle to an image memory handle (which may have a device optimiz...
image_mem_handle alloc_image_mem(const image_descriptor &desc, const sycl::device &syclDevice, const sycl::context &syclContext)
Allocate image memory based on image_descriptor.
interop_mem_handle import_external_memory< external_mem_fd >(external_mem_descriptor< external_mem_fd > externalMem, const sycl::device &syclDevice, const sycl::context &syclContext)
void release_external_memory(interop_mem_handle interopHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Release external memory.
void destroy_external_semaphore(interop_semaphore_handle semaphoreHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Destroy the external semaphore handle.
image_mem_handle get_mip_level_mem_handle(const image_mem_handle mipMem, const unsigned int level, const sycl::device &syclDevice, const sycl::context &syclContext)
Retrieve the memory handle to an individual mipmap image.
void free_mipmap_mem(image_mem_handle handle, const sycl::device &syclDevice, const sycl::context &syclContext)
[Deprecated] Free mipmap memory
interop_semaphore_handle import_external_semaphore(external_semaphore_descriptor< ExternalSemaphoreHandleType > externalSemaphoreDesc, const sycl::device &syclDevice, const sycl::context &syclContext)
Import external semaphore taking an external semaphore handle (the type of which is dependent on the ...
void destroy_image_handle(unsampled_image_handle &imageHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Destroy an unsampled image handle.
sycl::image_channel_type get_image_channel_type(const image_mem_handle memHandle, const sycl::device &syclDevice, const sycl::context &syclContext)
Get the channel type that describes the image memory.
image_mem_handle alloc_mipmap_mem(const image_descriptor &desc, const sycl::device &syclDevice, const sycl::context &syclContext)
[Deprecated] Allocate mipmap memory based on image_descriptor
image_channel_order
Definition: image.hpp:56
image_channel_type
Definition: image.hpp:74
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
Definition: exception.cpp:91
Definition: access.hpp:18
static device_ext & get_device(unsigned int id)
Util function to get a device by id.
Definition: device.hpp:777
constexpr pi_sampler_properties PI_SAMPLER_PROPERTIES_NORMALIZED_COORDS
Definition: pi.h:725
constexpr pi_sampler_properties PI_SAMPLER_PROPERTIES_ADDRESSING_MODE
Definition: pi.h:727
pi_result piextMemImageAllocate(pi_context context, pi_device device, pi_image_format *image_format, pi_image_desc *image_desc, pi_image_mem_handle *ret_mem)
API to allocate memory for bindless images.
Definition: pi_cuda.cpp:374
pi_result piextMemImportOpaqueFD(pi_context context, pi_device device, size_t size, int file_descriptor, pi_interop_mem_handle *ret_handle)
API to import external memory in the form of a file descriptor.
Definition: pi_cuda.cpp:459
pi_uint64 pi_image_handle
Definition: pi.h:1157
pi_result piextMemMapExternalArray(pi_context context, pi_device device, pi_image_format *image_format, pi_image_desc *image_desc, pi_interop_mem_handle mem_handle, pi_image_mem_handle *ret_mem)
API to map an interop memory handle to an image memory handle.
Definition: pi_cuda.cpp:465
pi_result piextMemSampledImageCreate(pi_context context, pi_device device, pi_image_mem_handle img_mem, pi_image_format *image_format, pi_image_desc *image_desc, pi_sampler sampler, pi_mem *ret_mem, pi_image_handle *ret_handle)
API to create sampled bindless image handles.
Definition: pi_cuda.cpp:391
pi_result piextDestroyExternalSemaphore(pi_context context, pi_device device, pi_interop_semaphore_handle sem_handle)
API to destroy the external semaphore handle.
Definition: pi_cuda.cpp:487
@ PI_IMAGE_INFO_HEIGHT
Definition: pi.h:534
@ PI_IMAGE_INFO_WIDTH
Definition: pi.h:533
@ PI_IMAGE_INFO_FORMAT
Definition: pi.h:529
@ PI_IMAGE_INFO_DEPTH
Definition: pi.h:535
pi_bitfield pi_sampler_properties
Definition: pi.h:724
pi_result piextMemImageFree(pi_context context, pi_device device, pi_image_mem_handle memory_handle)
API to free memory for bindless images.
Definition: pi_cuda.cpp:417
pi_result piextMemUnsampledImageCreate(pi_context context, pi_device device, pi_image_mem_handle img_mem, pi_image_format *image_format, pi_image_desc *image_desc, pi_mem *ret_mem, pi_image_handle *ret_handle)
API to create bindless image handles.
Definition: pi_cuda.cpp:383
pi_result piextMemSampledImageHandleDestroy(pi_context context, pi_device device, pi_image_handle handle)
API to destroy bindless sampled image handles.
Definition: pi_cuda.cpp:445
@ PI_MEM_TYPE_IMAGE_CUBEMAP
Definition: pi.h:596
@ PI_MEM_TYPE_IMAGE1D
Definition: pi.h:593
@ PI_MEM_TYPE_IMAGE1D_ARRAY
Definition: pi.h:594
@ PI_MEM_TYPE_IMAGE2D
Definition: pi.h:590
@ PI_MEM_TYPE_IMAGE2D_ARRAY
Definition: pi.h:592
@ PI_MEM_TYPE_IMAGE3D
Definition: pi.h:591
pi_result piextMemUnsampledImageHandleDestroy(pi_context context, pi_device device, pi_image_handle handle)
API to destroy bindless unsampled image handles.
Definition: pi_cuda.cpp:440
pi_result piextMemMipmapFree(pi_context context, pi_device device, pi_image_mem_handle memory_handle)
API to free mipmap memory for bindless images.
Definition: pi_cuda.cpp:422
pi_result piextMemMipmapGetLevel(pi_context context, pi_device device, pi_image_mem_handle mip_mem, unsigned int level, pi_image_mem_handle *ret_mem)
API to retrieve individual image from mipmap.
Definition: pi_cuda.cpp:409
constexpr pi_sampler_properties PI_SAMPLER_PROPERTIES_FILTER_MODE
Definition: pi.h:728
constexpr pi_sampler_properties PI_SAMPLER_PROPERTIES_CUBEMAP_FILTER_MODE
Definition: pi.h:730
pi_result piextMemReleaseInterop(pi_context context, pi_device device, pi_interop_mem_handle memory_handle)
API to destroy interop memory.
Definition: pi_cuda.cpp:473
pi_result piextBindlessImageSamplerCreate(pi_context context, const pi_sampler_properties *sampler_properties, float min_mipmap_level_clamp, float max_mipmap_level_clamp, float max_anisotropy, pi_sampler *result_sampler)
API to create samplers for bindless images.
Definition: pi_cuda.cpp:400
pi_result piextImportExternalSemaphoreOpaqueFD(pi_context context, pi_device device, int file_descriptor, pi_interop_semaphore_handle *ret_handle)
API to import an external semaphore in the form of a file descriptor.
Definition: pi_cuda.cpp:479
pi_result piextMemImageGetInfo(const pi_image_mem_handle mem_handle, pi_image_info param_name, void *param_value, size_t *param_value_size_ret)
API to query an image memory handle for specific properties.
Definition: pi_cuda.cpp:450
constexpr pi_sampler_properties PI_SAMPLER_PROPERTIES_MIP_FILTER_MODE
Definition: pi.h:729
pi_result piextUSMPitchedAlloc(void **result_ptr, size_t *result_pitch, pi_context context, pi_device device, pi_usm_mem_properties *properties, size_t width_in_bytes, size_t height, unsigned int element_size_bytes)
Allocates memory accessible on device.
Definition: pi_cuda.cpp:878
pi_uint64 pi_interop_semaphore_handle
Definition: pi.h:1160
pi_uint64 pi_interop_mem_handle
Definition: pi.h:1159
C++ wrapper of extern "C" PI interfaces.
size_t image_slice_pitch
Definition: pi.h:1174
pi_uint32 num_mip_levels
Definition: pi.h:1175
size_t image_height
Definition: pi.h:1170
size_t image_row_pitch
Definition: pi.h:1173
pi_uint32 num_samples
Definition: pi.h:1176
size_t image_depth
Definition: pi.h:1171
pi_mem buffer
Definition: pi.h:1177
size_t image_width
Definition: pi.h:1169
pi_mem_type image_type
Definition: pi.h:1168
size_t image_array_size
Definition: pi.h:1172
pi_image_channel_type image_channel_data_type
Definition: pi.h:1164
pi_image_channel_order image_channel_order
Definition: pi.h:1163
A struct to describe the properties of an image.