Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added suffixed enums for cl_khr_image2d_from_buffer #44

Merged

Conversation

bashbaug
Copy link
Contributor

This is a partial fix for #42.

This change adds KHR suffixed enums for cl_khr_imaged2d_from_buffer to cl_ext.h.

It also moves the un-suffixed enums for cl_khr_image2d_from_buffer in cl.h to a section protected by #ifdef CL_VERSION_2_0, not #ifdef CL_VERSION_1_2, since the extension became a core feature in OpenCL 2.0.

Copy link
Contributor

@alycm alycm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Reviewed in conjunction with KhronosGroup/OpenCL-Docs#60.

@jrprice jrprice merged commit 49b601b into KhronosGroup:master Mar 26, 2019
@Smeany
Copy link

Smeany commented Dec 28, 2021

There are CPUs (Intel i3-3250) that have support for OpenCL 1.2 and mentioned extension. I don't see why the extention becoming a feature in 2.0 justifies exclusion of the defines from versions prior to 2.0.

@Smeany
Copy link

Smeany commented Jan 2, 2022

It gets even worse when trying to implement zero copy image from buffer with the c++ headers in OpenCL 1.2 as it requires you to set the cl_image_desc structs buffer/mem_object union to the desired buffer object that needs to be transformed into an image (like in this example). The C++ Image2d Implementation simply uses the c versions clCreateImage function but with a fixed cl_image_desc which in turn doesn't let you specify the buffer/mem_object union...

@bashbaug bashbaug deleted the cl_khr_image2d_from_buffer-enums branch January 4, 2022 01:10
@bashbaug
Copy link
Contributor Author

bashbaug commented Jan 4, 2022

Hi @Smeany, regarding:

I don't see why the extention becoming a feature in 2.0 justifies exclusion of the defines from versions prior to 2.0.

The basic idea is that you would use the KHR-suffixed versions in cl_ext.h for devices that supports the extension and the non-suffixed versions in cl.h for devices that supports the core feature.

This is slightly academic in that all devices that support the core feature also must support the extension, and both the suffixed and un-suffixed enums have the same value. For maximal compatibility you could use the suffixed enums exclusively - this would be my recommendation unless you're compiling for OpenCL 2.0 or newer devices.

I'm not quite following the issue with cl_image_desc and the C++ bindings, but please feel free to file an issue in the OpenCL-LHPP repo if you have ideas for improvement. This will likely get more attention than here, since this PR is merged.

Thanks!

@Smeany
Copy link

Smeany commented Jan 4, 2022

Thank you @bashbaug , I was not aware of the cl_ext.h header.
The problem I tried to solve revolves around finding a way to enable read and write access to an image2d object, but opencl 1.2 limits the acces to either read or write and doesn't support both at the same time. So the workaround I came across was to just use a buffer object instead for r/w capability, which can then be turned into an image2d object via the cl_khr_image2d_from_buffer extension without the need for an unnecessary copy.
Following the description in step 3 before creating the image from a buffer you have to assign the cl_buffer object to a cl_image_desc object and its mem_object property. This cl_image_desc object is then used to call the C versions clCreateImage. As I've written everything in C++ and wanted to continue doing so I looked for a way to access the cl_image_desc property of the C++ Image2d until I stumbled upon the Sourcecode for the Image2d C++ Constructor for opencl 1.2.
It creates it's own intermediary cl_image_desc object and theres no overloaded Image2d Constructor which would allow assigning a custom cl_image_desc object. To come around this I had to use the C versions clCreateImage function and then wrap it into a C++ Object. Correct me if I'm wrong, but it seems that theres no other way around using C.

#if CL_HPP_TARGET_OPENCL_VERSION >= 120
        if (useCreateImage)
        {
            cl_image_desc desc = {0};
            desc.image_type = CL_MEM_OBJECT_IMAGE2D;
            desc.image_width = width;
            desc.image_height = height;
            desc.image_row_pitch = row_pitch;

            object_ = ::clCreateImage(
                context(),
                flags,
                &format,
                &desc,
                host_ptr,
                &error);

            detail::errHandler(error, __CREATE_IMAGE_ERR);
            if (err != NULL) {
                *err = error;
            }
        }
#endif // CL_HPP_TARGET_OPENCL_VERSION >= 120```

@bashbaug
Copy link
Contributor Author

bashbaug commented Jan 4, 2022

it seems that theres no other way around using C.

Could you use this Image2D constructor?

https://github.com/KhronosGroup/OpenCL-CLHPP/blob/master/include/CL/opencl.hpp#L4856

    /*! \brief Constructs a 2D Image from a buffer.
    * \note This will share storage with the underlying buffer.
    *
    *  Wraps clCreateImage().
    */
    Image2D(
        const Context& context,
        ImageFormat format,
        const Buffer &sourceBuffer,
        size_type width,
        size_type height,
        size_type row_pitch = 0,
        cl_int* err = nullptr)
...

Note, you will need to define CL_HPP_USE_CL_IMAGE2D_FROM_BUFFER_KHR or compile for OpenCL 2.0 or newer to use this constructor, at least until KhronosGroup/OpenCL-CLHPP#148 is merged.

EwanC pushed a commit to EwanC/OpenCL-Headers that referenced this pull request Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants