1 // 2 // Copyright 2012 Francisco Jerez 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 // OTHER DEALINGS IN THE SOFTWARE. 21 // 22 23 #include "core/format.hpp" 24 #include "core/memory.hpp" 25 #include "pipe/p_screen.h" 26 #include "pipe/p_context.h" 27 28 namespace clover { 29 // see table 16 and 17 in the 3.0 CL spec under "5.3.1.1. Image Format Descriptor" 30 // TODO optional channel orders: 31 // * CL_Rx 32 // * CL_RGx 33 // * CL_RGBx 34 // * CL_sRGBx 35 #define _FF(c, b, g) \ 36 { { CL_R, c }, PIPE_FORMAT_R##b##_##g }, \ 37 { { CL_A, c }, PIPE_FORMAT_A##b##_##g }, \ 38 { { CL_RG, c }, PIPE_FORMAT_R##b##G##b##_##g }, \ 39 { { CL_RA, c }, PIPE_FORMAT_R##b##A##b##_##g }, \ 40 { { CL_RGB, c }, PIPE_FORMAT_R##b##G##b##B##b##_##g }, \ 41 { { CL_RGBA, c }, PIPE_FORMAT_R##b##G##b##B##b##A##b##_##g } 42 // broken but also optional 43 //{ { CL_LUMINANCE, c }, PIPE_FORMAT_L##b##_##g }, 44 //{ { CL_INTENSITY, c }, PIPE_FORMAT_I##b##_##g }, 45 46 #define _FI(c, b, g) \ 47 _FF(c##b, b, g) 48 49 static const std::map<cl_image_format, pipe_format> formats { 50 //required in CL 2.0 but broken 51 //_FI(CL_SNORM_INT, 8, SNORM), 52 //_FI(CL_SNORM_INT, 16, SNORM), 53 _FI(CL_UNORM_INT, 8, UNORM), 54 _FI(CL_UNORM_INT, 16, UNORM), 55 _FI(CL_SIGNED_INT, 8, SINT), 56 _FI(CL_SIGNED_INT, 16, SINT), 57 _FI(CL_SIGNED_INT, 32, SINT), 58 _FI(CL_UNSIGNED_INT, 8, UINT), 59 _FI(CL_UNSIGNED_INT, 16, UINT), 60 _FI(CL_UNSIGNED_INT, 32, UINT), 61 _FF(CL_HALF_FLOAT, 16, FLOAT), 62 _FF(CL_FLOAT, 32, FLOAT), 63 64 // TODO: next three can be CL_RGBx as well 65 { { CL_RGB, CL_UNORM_SHORT_565 }, PIPE_FORMAT_B5G6R5_UNORM }, 66 { { CL_RGB, CL_UNORM_SHORT_555 }, PIPE_FORMAT_B5G5R5A1_UNORM }, 67 { { CL_RGB, CL_UNORM_INT_101010 }, PIPE_FORMAT_B10G10R10X2_UNORM }, 68 69 { { CL_RGBA, CL_UNORM_INT_101010_2 }, PIPE_FORMAT_B10G10R10A2_UNORM }, 70 71 { { CL_ARGB, CL_UNORM_INT8 }, PIPE_FORMAT_A8R8G8B8_UNORM }, 72 { { CL_ARGB, CL_UNSIGNED_INT8 }, PIPE_FORMAT_A8R8G8B8_UINT }, 73 74 { { CL_BGRA, CL_SNORM_INT8 }, PIPE_FORMAT_B8G8R8A8_SNORM }, 75 { { CL_BGRA, CL_UNORM_INT8 }, PIPE_FORMAT_B8G8R8A8_UNORM }, 76 { { CL_BGRA, CL_SIGNED_INT8 }, PIPE_FORMAT_B8G8R8A8_SINT }, 77 { { CL_BGRA, CL_UNSIGNED_INT8 }, PIPE_FORMAT_B8G8R8A8_UINT }, 78 79 { { CL_ABGR, CL_SNORM_INT8 }, PIPE_FORMAT_A8B8G8R8_SNORM }, 80 { { CL_ABGR, CL_UNORM_INT8 }, PIPE_FORMAT_A8B8G8R8_UNORM }, 81 { { CL_ABGR, CL_SIGNED_INT8 }, PIPE_FORMAT_A8B8G8R8_SINT }, 82 { { CL_ABGR, CL_UNSIGNED_INT8 }, PIPE_FORMAT_A8B8G8R8_UINT }, 83 84 // disable for now as it needs CL C 2.0 support 85 //{ { CL_DEPTH, CL_UNORM_INT16 }, PIPE_FORMAT_Z16_UNORM }, 86 //{ { CL_DEPTH, CL_FLOAT }, PIPE_FORMAT_Z32_FLOAT }, 87 88 // required in CL 2.0 but broken 89 //{ { CL_sRGBA, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8B8A8_SRGB }, 90 // optional but broken 91 //{ { CL_sRGB, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8B8_SRGB }, 92 //{ { CL_sBGRA, CL_UNORM_INT8 }, PIPE_FORMAT_B8G8R8A8_SRGB }, 93 }; 94 #undef _FF 95 #undef _FI 96 97 pipe_texture_target translate_target(cl_mem_object_type type)98 translate_target(cl_mem_object_type type) { 99 switch (type) { 100 case CL_MEM_OBJECT_BUFFER: 101 case CL_MEM_OBJECT_IMAGE1D_BUFFER: 102 return PIPE_BUFFER; 103 case CL_MEM_OBJECT_IMAGE1D: 104 return PIPE_TEXTURE_1D; 105 case CL_MEM_OBJECT_IMAGE2D: 106 return PIPE_TEXTURE_2D; 107 case CL_MEM_OBJECT_IMAGE3D: 108 return PIPE_TEXTURE_3D; 109 case CL_MEM_OBJECT_IMAGE1D_ARRAY: 110 return PIPE_TEXTURE_1D_ARRAY; 111 case CL_MEM_OBJECT_IMAGE2D_ARRAY: 112 return PIPE_TEXTURE_2D_ARRAY; 113 default: 114 throw error(CL_INVALID_VALUE); 115 } 116 } 117 118 pipe_format translate_format(const cl_image_format & format)119 translate_format(const cl_image_format &format) { 120 auto it = formats.find(format); 121 122 if (it == formats.end()) 123 throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED); 124 125 return it->second; 126 } 127 128 std::set<cl_image_format> supported_formats(const context & ctx,cl_mem_object_type type,cl_mem_flags flags)129 supported_formats(const context &ctx, cl_mem_object_type type, cl_mem_flags flags) { 130 std::set<cl_image_format> s; 131 pipe_texture_target target = translate_target(type); 132 unsigned bindings = 0; 133 134 if (flags & (CL_MEM_READ_ONLY | CL_MEM_READ_WRITE | CL_MEM_KERNEL_READ_AND_WRITE)) 135 bindings |= PIPE_BIND_SAMPLER_VIEW; 136 if (flags & (CL_MEM_WRITE_ONLY | CL_MEM_READ_WRITE | CL_MEM_KERNEL_READ_AND_WRITE)) 137 bindings |= PIPE_BIND_SHADER_IMAGE; 138 139 for (auto f : formats) { 140 if (all_of([=](const device &dev) { 141 return dev.pipe->is_format_supported( 142 dev.pipe, f.second, target, 1, 1, bindings); 143 }, ctx.devices())) 144 s.insert(f.first); 145 } 146 147 return s; 148 } 149 } 150