CLCREATEFROMDX9MEDIA(3clc)

πŸ“¦
version 1.0~svn33624 Β· 3 versions
, in ubuntu jammy, ubuntu noble

🌸 CLCREATEFROMDX9MEDIA β€” 3clc ✨

okay bestie, buckle UP β€” this one's from the OpenCL Manual and it's lowkey the glow-up moment where your D3D9 media surfaces become actual OpenCL image objects. iconic behavior πŸ’…


πŸ’– NAME πŸ’–

clCreateFromDX9MediaSurfaceKHR β€” creates an OpenCL image object from a media surface. πŸŽ€

cl_mem clCreateFromDX9MediaSurfaceKHR(cl_context context,
                                      cl_mem_flags flags,
                                      cl_dx9_media_adapter_type_khr adapter_type,
                                      void *surface_info, cl_uint plane,
                                      cl_int *errcode_ret);

πŸ¦‹ PARAMETERS πŸ¦‹

  • context β€” a valid OpenCL context, and no cap it has to be one that was created from a media adapter. ✨
  • flags β€” a bit-field for usage info. Go look at the table of allowed values for flags over in clCreateBuffer(3clc). BUT β€” and this is the part people always fumble β€” only CL_MEM_READ_ONLY, CL_MEM_WRITE_ONLY, and CL_MEM_READ_WRITE from that table are allowed here. That's it. That's the whole vibe check. πŸ™ˆ
  • adapter_type β€” a value from the enumeration of supported adapters described in the table of cl_dx9_media_adapter_type_khr values for clGetDeviceIDsFromDX9MediaAdapterKHR(3clc). The type of surface_info is determined by the adapter type. The implementation does not need to support all adapter types β€” this approach is genuinely such a serve because it provides flexibility to support additional adapter types in the future. 🌈

The supported adapter types are CL_ADAPTER_D3D9_KHR, CL_ADAPTER_D3D9EX_KHR, and CL_ADAPTER_DXVA_KHR.

If adapter_type is CL_ADAPTER_D3D9_KHR, CL_ADAPTER_D3D9EX_KHR, or CL_ADAPTER_DXVA_KHR, the surface_info points to the following structure:

For D3D9 surfaces, we need both the handle to the resource and the resource itself to have a sufficient amount of information to eliminate a copy of the surface for sharing in cases where this is possible. Elimination of the copy is driver dependent β€” so like, sometimes it slays, sometimes it doesn't. 🫢

shared_handle may be NULL and this may result in sub-optimal performance. πŸ’€ Not the end of the world but bestie, know that going in.
  • surface_info β€” a pointer to one of the structures defined in the adapter_type description above, passed in as a void *. ✨
  • plane β€” the plane of resource to share for planar surface formats. For planar formats, we use the plane parameter to obtain a handle to the specific plane (Y, U or V for example). For nonplanar formats used by media, plane must be 0. πŸŽ€
  • errcode_ret β€” an appropriate error code. If errcode_ret is NULL, no error code is returned. πŸ¦‹

⭐ NOTES ⭐

The width and height of the returned OpenCL 2D image object are determined by the width and height of the plane of resource. The channel type and order of the returned image object is determined by the format and plane of resource and are described in tables 9.10.3 and 9.10.4. πŸ‘‘

This call will increment the internal media surface count on resource. The internal media surface reference count on resource will be decremented when the OpenCL reference count on the returned OpenCL memory object drops to zero. πŸ”₯

🧚 DX9 Media Surface Sharing 🧚

The goal of this extension is to allow applications to use media surfaces as OpenCL memory objects. This allows efficient sharing of data between OpenCL and selected adapter APIs (only DX9 for now). If this extension is supported, an OpenCL image object can be created from a media surface and the OpenCL API can be used to execute kernels that read and/or write memory objects that are media surfaces.

Note that OpenCL memory objects may be created from the adapter media surface if and only if the OpenCL context has been created from that adapter. No exceptions, periodt. πŸ’…

If this extension is supported by an implementation, the string "cl_khr_media_sharing" will be present in the CL_PLATFORM_EXTENSIONS or CL_DEVICE_EXTENSIONS string described in the table of allowed values for param_name for clGetDeviceInfo(3clc). ✨

As currently proposed the interfaces for this extension would be provided in cl_dx9_media_sharing.h. 🌸

🌈 D3D Surface Formats 🌈

This section includes the D3D surface formats that are supported when the adapter type is one of the Direct 3D lineage. Using a D3D surface format not listed here is an error. 🚨

To extend the use of this extension to support media adapters beyond DirectX9, tables similar to the ones in this section will need to be defined for the surface formats supported by the new media adapter.

All implementations that support this extension are required to support the NV12 surface format β€” that one's non-negotiable bestie. The other surface formats supported are the same surface formats that the adapter you are sharing with supports, as long as they are listed in the two tables below. πŸ‘‘

πŸ’œ Table one β€” FOUR CC codes πŸ’œ

  • FOURCC('N', 'V', '1', '2'), Plane 0 β€” CL_R, CL_UNORM_INT8
  • FOURCC('N', 'V', '1', '2'), Plane 1 β€” CL_RG, CL_UNORM_INT8
  • FOURCC('Y', 'V', '1', '2'), Plane 0 β€” CL_R, CL_UNORM_INT8
  • FOURCC('Y', 'V', '1', '2'), Plane 1 β€” CL_R, CL_UNORM_INT8
  • FOURCC('Y', 'V', '1', '2'), Plane 2 β€” CL_R, CL_UNORM_INT8

In the table above, NV12 Plane 0 corresponds to the luminance (Y) channel and Plane 1 corresponds to the UV channels. The YV12 Plane 0 corresponds to the Y channel, Plane 1 to the U channel and Plane 2 to the V channel. 🌸

Note that the YUV formats map to CL_R and CL_RG but do not perform any YUV to RGB conversion and vice-versa. It's giving "map, don't transform". πŸ™ˆ

πŸŽ€ Table two β€” D3D formats πŸŽ€

  • D3DFMT_R32F β€” CL_R, CL_FLOAT
  • D3DFMT_R16F β€” CL_R, CL_HALF_FLOAT
  • D3DFMT_L16 β€” CL_R, CL_UNORM_INT16
  • D3DFMT_A8 β€” CL_A, CL_UNORM_INT8
  • D3DFMT_L8 β€” CL_R, CL_UNORM_INT8
  • D3DFMT_G32R32F β€” CL_RG, CL_FLOAT
  • D3DFMT_G16R16F β€” CL_RG, CL_HALF_FLOAT
  • D3DFMT_G16R16 β€” CL_RG, CL_UNORM_INT16
  • D3DFMT_A8L8 β€” CL_RG, CL_UNORM_INT8
  • D3DFMT_A32B32G32R32F β€” CL_RGBA, CL_FLOAT
  • D3DFMT_A16B16G16R16F β€” CL_RGBA, CL_HALF_FLOAT
  • D3DFMT_A16B16G16R16 β€” CL_RGBA, CL_UNORM_INT16
  • D3DFMT_A8B8G8R8 β€” CL_RGBA, CL_UNORM_INT8
  • D3DFMT_X8B8G8R8 β€” CL_RGBA, CL_UNORM_INT8
  • D3DFMT_A8R8G8B8 β€” CL_BGRA, CL_UNORM_INT8
  • D3DFMT_X8R8G8B8 β€” CL_BGRA, CL_UNORM_INT8

okay so bestie, real talk: the D3D9 format names? They're out here acting like the order of the color channels is switched compared to OpenCL, but that is not the case. πŸ™…β€β™€οΈ For example, the layout of channels for each pixel for D3DFMT_A32FB32FG32FR32F is literally the same as CL_RGBA, CL_FLOAT. no cap.

Properties of media surface objects? You can query those using clGetMemObjectInfo(3clc) and clGetImageInfo(3clc) with param_name set to CL_MEM_MEDIA_ADAPTER_TYPE_KHR, CL_MEM_MEDIA_SURFACE_INFO_KHR, and CL_IMAGE_MEDIA_SURFACE_PLANE_KHR, just like it says for clGetImageInfo(3clc) and in the info below about accessing mapped regions of a memory object. So like, accessing mapped regions of a memory object? This section describes the behavior of OpenCL commands that access mapped regions of a memory object. πŸ’–


The contents of the region of a memory object and associated memory objects (sub-buffer objects or 1D image buffer objects that overlap this region) mapped for writing (i.e. CL_MAP_WRITE or CL_MAP_WRITE_INVALIDATE_REGION is set in map_flags argument to clEnqueueMapBuffer(3clc) or clEnqueueMapImage(3clc)) are considered to be undefined until this region is unmapped. πŸ’€

Multiple commands in command-queues can map a region or overlapping regions of a memory object and associated memory objects (sub-buffer objects or 1D image buffer objects that overlap this region) for reading (i.e. map_flags = CL_MAP_READ). The contents of the regions of a memory object mapped for reading can also be read by kernels and other OpenCL commands (such as clEnqueueCopyBuffer(3clc)) executing on a device(s). πŸ¦‹

Mapping (and unmapping) overlapped regions in a memory object and/or associated memory objects (sub-buffer objects or 1D image buffer objects that overlap this region) for writing is an error and will result in CL_INVALID_OPERATION error returned by clEnqueueMapBuffer(3clc) or clEnqueueMapImage(3clc). 😭

If a memory object is currently mapped for writing, the application must ensure that the memory object is unmapped before any enqueued kernels or commands that read from or write to this memory object or any of its associated memory objects (sub-buffer or 1D image buffer objects) or its parent object (if the memory object is a sub-buffer or 1D image buffer object) begin execution; otherwise the behavior is undefined. 🚨

If a memory object is currently mapped for reading, the application must ensure that the memory object is unmapped before any enqueued kernels or commands that write to this memory object or any of its associated memory objects (sub-buffer or 1D image buffer objects) or its parent object (if the memory object is a sub-buffer or 1D image buffer object) begin execution; otherwise the behavior is undefined. 🚨

Accessing the contents of the memory region referred to by the mapped pointer that has been unmapped is undefined. πŸ™ˆ

The mapped pointer returned by clEnqueueMapBuffer(3clc) or clEnqueueMapImage(3clc) can be used as ptr argument value to clEnqueueReadBuffer(3clc), clEnqueueWriteBuffer(3clc), clEnqueueReadBufferRect(3clc), clEnqueueWriteBufferRect(3clc), clEnqueueReadImage(3clc), and clEnqueueWriteImage(3clc), provided the rules described above are adhered to. πŸ’…


✨ ERRORS ✨

Returns CL_SUCCESS returns a valid non-zero 2D image object and errcode_ret is set to CL_SUCCESS if the 2D image object is created successfully. Otherwise, it returns a NULL value with one of the following error values returned in errcode_ret:

  • CL_INVALID_CONTEXT β€” if context is not a valid context. πŸ’€
  • CL_INVALID_VALUE β€” if values specified in flags are not valid or if plane is not a valid plane of resource specified in surface_info... 🫠
  • CL_INVALID_DX9_MEDIA_SURFACE_KHR β€” if resource specified in surface_info is not a valid resource or is not associated with adapter_type (e.g., adapter_type is set to CL_ADAPTER_D3D9_KHR and resource is not a Direct3D 9 surface created in D3DPOOL_DEFAULT). 😡
  • CL_INVALID_DX9_MEDIA_SURFACE_KHR β€” if shared_handle specified in surface_info is not NULL or a valid handle value. πŸ™…
  • CL_INVALID_IMAGE_FORMAT_DESCRIPTOR β€” if the texture format of resource is not listed in tables 9.10.3 and 9.10.4. πŸ“‹
  • CL_INVALID_OPERATION β€” if there are no devices in context that support adapter_type. 🚫
  • CL_OUT_OF_RESOURCES β€” if there is a failure to allocate resources required by the OpenCL implementation on the device. 😭
  • CL_OUT_OF_HOST_MEMORY β€” if there is a failure to allocate resources required by the OpenCL implementation on the host. πŸ’€

πŸ“œ SPECIFICATION πŸ“œ

OpenCL Specification[1]


πŸ‘€ SEE ALSO πŸ‘€


✍️ AUTHORS ✍️

The Khronos Group


Copyright Β© 2007-2011 The Khronos Group Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.


πŸ“ NOTES πŸ“

  1. OpenCL Specification page 89, section 9.10.7.2 Creating Media Resources as OpenCL Image Objects

and that's the whole vibe! πŸ’– this page was brought to you by The Khronos Group, dated 01/14/2021, for CLCREATEFROMDX9MEDIA(3clc). bye bestie! 🫢

πŸ“¦ where this page comes from