Search code examples
texturesopenclgpuamd-processor

Converting supported image/texture memory format information to readable string in opencl


I want to get the supported image formats of my device. But the opencl function returns results in cl_image_format. I want to know the results in string.

Currently I am using the following code:

cl_uint uiNumSupportedFormats = 0;

// 2D
clGetSupportedImageFormats((cl_context)image.clCxt->oclContext(),
                           CL_MEM_WRITE_ONLY,
                           CL_MEM_OBJECT_IMAGE2D,
                           NULL, NULL, &uiNumSupportedFormats);

cl_image_format* ImageFormats = new cl_image_format[uiNumSupportedFormats];
clGetSupportedImageFormats((cl_context)image.clCxt->oclContext(),
                           CL_MEM_WRITE_ONLY,
                           CL_MEM_OBJECT_IMAGE2D,
                           uiNumSupportedFormats, ImageFormats, NULL);

for(unsigned int i = 0; i < uiNumSupportedFormats; i++)
{

    cout<<ImageFormats[i].image_channel_order<<endl;

    cout<<ImageFormats[i].image_channel_data_type<<endl;

}

delete [] ImageFormats;

Right now I am getting only integer values. How can I get corresponding string value? For example: CL_RA CL_UNORM_INT8

I am using an AMD GPU device.


Solution

  • There is no OpenCL standard way of getting the string representation of these constants (or any others), so you'll have to either write your own or use some utility that does it for you. It's pretty forward to write you're own 'human-readable' print method for image formats. You could do something like this:

    void printImageFormat(cl_image_format format)
    {
    #define CASE(order) case order: cout << #order; break;
      switch (format.image_channel_order)
      {
        CASE(CL_R);
        CASE(CL_A);
        CASE(CL_RG);
        CASE(CL_RA);
        CASE(CL_RGB);
        CASE(CL_RGBA);
        CASE(CL_BGRA);
        CASE(CL_ARGB);
        CASE(CL_INTENSITY);
        CASE(CL_LUMINANCE);
        CASE(CL_Rx);
        CASE(CL_RGx);
        CASE(CL_RGBx);
        CASE(CL_DEPTH);
        CASE(CL_DEPTH_STENCIL);
      }
    #undef CASE
    
      cout << " - ";
    
    #define CASE(type) case type: cout << #type; break;
      switch (format.image_channel_data_type)
      {
        CASE(CL_SNORM_INT8);
        CASE(CL_SNORM_INT16);
        CASE(CL_UNORM_INT8);
        CASE(CL_UNORM_INT16);
        CASE(CL_UNORM_SHORT_565);
        CASE(CL_UNORM_SHORT_555);
        CASE(CL_UNORM_INT_101010);
        CASE(CL_SIGNED_INT8);
        CASE(CL_SIGNED_INT16);
        CASE(CL_SIGNED_INT32);
        CASE(CL_UNSIGNED_INT8);
        CASE(CL_UNSIGNED_INT16);
        CASE(CL_UNSIGNED_INT32);
        CASE(CL_HALF_FLOAT);
        CASE(CL_FLOAT);
        CASE(CL_UNORM_INT24);
      }
    #undef CASE
    
      cout << endl;
    }
    

    And then just call printImageFormat(ImageFormats[i]); inside your existing loop.