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 static const std::map<cl_image_format, pipe_format> formats { 30 { { CL_BGRA, CL_UNORM_INT8 }, PIPE_FORMAT_B8G8R8A8_UNORM }, 31 { { CL_ARGB, CL_UNORM_INT8 }, PIPE_FORMAT_A8R8G8B8_UNORM }, 32 { { CL_RGB, CL_UNORM_SHORT_565 }, PIPE_FORMAT_B5G6R5_UNORM }, 33 { { CL_LUMINANCE, CL_UNORM_INT8 }, PIPE_FORMAT_L8_UNORM }, 34 { { CL_A, CL_UNORM_INT8 }, PIPE_FORMAT_A8_UNORM }, 35 { { CL_INTENSITY, CL_UNORM_INT8 }, PIPE_FORMAT_I8_UNORM }, 36 { { CL_LUMINANCE, CL_UNORM_INT16 }, PIPE_FORMAT_L16_UNORM }, 37 { { CL_R, CL_FLOAT }, PIPE_FORMAT_R32_FLOAT }, 38 { { CL_RG, CL_FLOAT }, PIPE_FORMAT_R32G32_FLOAT }, 39 { { CL_RGB, CL_FLOAT }, PIPE_FORMAT_R32G32B32_FLOAT }, 40 { { CL_RGBA, CL_FLOAT }, PIPE_FORMAT_R32G32B32A32_FLOAT }, 41 { { CL_R, CL_UNORM_INT16 }, PIPE_FORMAT_R16_UNORM }, 42 { { CL_RG, CL_UNORM_INT16 }, PIPE_FORMAT_R16G16_UNORM }, 43 { { CL_RGB, CL_UNORM_INT16 }, PIPE_FORMAT_R16G16B16_UNORM }, 44 { { CL_RGBA, CL_UNORM_INT16 }, PIPE_FORMAT_R16G16B16A16_UNORM }, 45 { { CL_R, CL_SNORM_INT16 }, PIPE_FORMAT_R16_SNORM }, 46 { { CL_RG, CL_SNORM_INT16 }, PIPE_FORMAT_R16G16_SNORM }, 47 { { CL_RGB, CL_SNORM_INT16 }, PIPE_FORMAT_R16G16B16_SNORM }, 48 { { CL_RGBA, CL_SNORM_INT16 }, PIPE_FORMAT_R16G16B16A16_SNORM }, 49 { { CL_R, CL_UNORM_INT8 }, PIPE_FORMAT_R8_UNORM }, 50 { { CL_RG, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8_UNORM }, 51 { { CL_RGB, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8B8_UNORM }, 52 { { CL_RGBA, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8B8A8_UNORM }, 53 { { CL_R, CL_SNORM_INT8 }, PIPE_FORMAT_R8_SNORM }, 54 { { CL_RG, CL_SNORM_INT8 }, PIPE_FORMAT_R8G8_SNORM }, 55 { { CL_RGB, CL_SNORM_INT8 }, PIPE_FORMAT_R8G8B8_SNORM }, 56 { { CL_RGBA, CL_SNORM_INT8 }, PIPE_FORMAT_R8G8B8A8_SNORM }, 57 { { CL_R, CL_HALF_FLOAT }, PIPE_FORMAT_R16_FLOAT }, 58 { { CL_RG, CL_HALF_FLOAT }, PIPE_FORMAT_R16G16_FLOAT }, 59 { { CL_RGB, CL_HALF_FLOAT }, PIPE_FORMAT_R16G16B16_FLOAT }, 60 { { CL_RGBA, CL_HALF_FLOAT }, PIPE_FORMAT_R16G16B16A16_FLOAT }, 61 { { CL_RGBx, CL_UNORM_SHORT_555 }, PIPE_FORMAT_B5G5R5X1_UNORM }, 62 { { CL_RGBx, CL_UNORM_INT8 }, PIPE_FORMAT_R8G8B8X8_UNORM }, 63 { { CL_A, CL_UNORM_INT16 }, PIPE_FORMAT_A16_UNORM }, 64 { { CL_INTENSITY, CL_UNORM_INT16 }, PIPE_FORMAT_I16_UNORM }, 65 { { CL_LUMINANCE, CL_SNORM_INT8 }, PIPE_FORMAT_L8_SNORM }, 66 { { CL_INTENSITY, CL_SNORM_INT8 }, PIPE_FORMAT_I8_SNORM }, 67 { { CL_A, CL_SNORM_INT16 }, PIPE_FORMAT_A16_SNORM }, 68 { { CL_LUMINANCE, CL_SNORM_INT16 }, PIPE_FORMAT_L16_SNORM }, 69 { { CL_INTENSITY, CL_SNORM_INT16 }, PIPE_FORMAT_I16_SNORM }, 70 { { CL_A, CL_HALF_FLOAT }, PIPE_FORMAT_A16_FLOAT }, 71 { { CL_LUMINANCE, CL_HALF_FLOAT }, PIPE_FORMAT_L16_FLOAT }, 72 { { CL_INTENSITY, CL_HALF_FLOAT }, PIPE_FORMAT_I16_FLOAT }, 73 { { CL_A, CL_FLOAT }, PIPE_FORMAT_A32_FLOAT }, 74 { { CL_LUMINANCE, CL_FLOAT }, PIPE_FORMAT_L32_FLOAT }, 75 { { CL_INTENSITY, CL_FLOAT }, PIPE_FORMAT_I32_FLOAT }, 76 { { CL_RA, CL_UNORM_INT8 }, PIPE_FORMAT_R8A8_UNORM }, 77 { { CL_R, CL_UNSIGNED_INT8 }, PIPE_FORMAT_R8_UINT }, 78 { { CL_RG, CL_UNSIGNED_INT8 }, PIPE_FORMAT_R8G8_UINT }, 79 { { CL_RGB, CL_UNSIGNED_INT8 }, PIPE_FORMAT_R8G8B8_UINT }, 80 { { CL_RGBA, CL_UNSIGNED_INT8 }, PIPE_FORMAT_R8G8B8A8_UINT }, 81 { { CL_R, CL_SIGNED_INT8 }, PIPE_FORMAT_R8_SINT }, 82 { { CL_RG, CL_SIGNED_INT8 }, PIPE_FORMAT_R8G8_SINT }, 83 { { CL_RGB, CL_SIGNED_INT8 }, PIPE_FORMAT_R8G8B8_SINT }, 84 { { CL_RGBA, CL_SIGNED_INT8 }, PIPE_FORMAT_R8G8B8A8_SINT }, 85 { { CL_R, CL_UNSIGNED_INT16 }, PIPE_FORMAT_R16_UINT }, 86 { { CL_RG, CL_UNSIGNED_INT16 }, PIPE_FORMAT_R16G16_UINT }, 87 { { CL_RGB, CL_UNSIGNED_INT16 }, PIPE_FORMAT_R16G16B16_UINT }, 88 { { CL_RGBA, CL_UNSIGNED_INT16 }, PIPE_FORMAT_R16G16B16A16_UINT }, 89 { { CL_R, CL_SIGNED_INT16 }, PIPE_FORMAT_R16_SINT }, 90 { { CL_RG, CL_SIGNED_INT16 }, PIPE_FORMAT_R16G16_SINT }, 91 { { CL_RGB, CL_SIGNED_INT16 }, PIPE_FORMAT_R16G16B16_SINT }, 92 { { CL_RGBA, CL_SIGNED_INT16 }, PIPE_FORMAT_R16G16B16A16_SINT }, 93 { { CL_R, CL_UNSIGNED_INT32 }, PIPE_FORMAT_R32_UINT }, 94 { { CL_RG, CL_UNSIGNED_INT32 }, PIPE_FORMAT_R32G32_UINT }, 95 { { CL_RGB, CL_UNSIGNED_INT32 }, PIPE_FORMAT_R32G32B32_UINT }, 96 { { CL_RGBA, CL_UNSIGNED_INT32 }, PIPE_FORMAT_R32G32B32A32_UINT }, 97 { { CL_R, CL_SIGNED_INT32 }, PIPE_FORMAT_R32_SINT }, 98 { { CL_RG, CL_SIGNED_INT32 }, PIPE_FORMAT_R32G32_SINT }, 99 { { CL_RGB, CL_SIGNED_INT32 }, PIPE_FORMAT_R32G32B32_SINT }, 100 { { CL_RGBA, CL_SIGNED_INT32 }, PIPE_FORMAT_R32G32B32A32_SINT }, 101 { { CL_A, CL_UNSIGNED_INT8 }, PIPE_FORMAT_A8_UINT }, 102 { { CL_INTENSITY, CL_UNSIGNED_INT8 }, PIPE_FORMAT_I8_UINT }, 103 { { CL_LUMINANCE, CL_UNSIGNED_INT8 }, PIPE_FORMAT_L8_UINT }, 104 { { CL_A, CL_SIGNED_INT8 }, PIPE_FORMAT_A8_SINT }, 105 { { CL_INTENSITY, CL_SIGNED_INT8 }, PIPE_FORMAT_I8_SINT }, 106 { { CL_LUMINANCE, CL_SIGNED_INT8 }, PIPE_FORMAT_L8_SINT }, 107 { { CL_A, CL_UNSIGNED_INT16 }, PIPE_FORMAT_A16_UINT }, 108 { { CL_INTENSITY, CL_UNSIGNED_INT16 }, PIPE_FORMAT_I16_UINT }, 109 { { CL_LUMINANCE, CL_UNSIGNED_INT16 }, PIPE_FORMAT_L16_UINT }, 110 { { CL_A, CL_SIGNED_INT16 }, PIPE_FORMAT_A16_SINT }, 111 { { CL_INTENSITY, CL_SIGNED_INT16 }, PIPE_FORMAT_I16_SINT }, 112 { { CL_LUMINANCE, CL_SIGNED_INT16 }, PIPE_FORMAT_L16_SINT }, 113 { { CL_A, CL_UNSIGNED_INT32 }, PIPE_FORMAT_A32_UINT }, 114 { { CL_INTENSITY, CL_UNSIGNED_INT32 }, PIPE_FORMAT_I32_UINT }, 115 { { CL_LUMINANCE, CL_UNSIGNED_INT32 }, PIPE_FORMAT_L32_UINT }, 116 { { CL_A, CL_SIGNED_INT32 }, PIPE_FORMAT_A32_SINT }, 117 { { CL_INTENSITY, CL_SIGNED_INT32 }, PIPE_FORMAT_I32_SINT }, 118 { { CL_LUMINANCE, CL_SIGNED_INT32 }, PIPE_FORMAT_L32_SINT } 119 }; 120 121 pipe_texture_target translate_target(cl_mem_object_type type)122 translate_target(cl_mem_object_type type) { 123 switch (type) { 124 case CL_MEM_OBJECT_BUFFER: 125 return PIPE_BUFFER; 126 case CL_MEM_OBJECT_IMAGE2D: 127 return PIPE_TEXTURE_2D; 128 case CL_MEM_OBJECT_IMAGE3D: 129 return PIPE_TEXTURE_3D; 130 default: 131 throw error(CL_INVALID_VALUE); 132 } 133 } 134 135 pipe_format translate_format(const cl_image_format & format)136 translate_format(const cl_image_format &format) { 137 auto it = formats.find(format); 138 139 if (it == formats.end()) 140 throw error(CL_IMAGE_FORMAT_NOT_SUPPORTED); 141 142 return it->second; 143 } 144 145 std::set<cl_image_format> supported_formats(const context & ctx,cl_mem_object_type type)146 supported_formats(const context &ctx, cl_mem_object_type type) { 147 std::set<cl_image_format> s; 148 pipe_texture_target target = translate_target(type); 149 unsigned bindings = (PIPE_BIND_SAMPLER_VIEW | 150 PIPE_BIND_COMPUTE_RESOURCE); 151 152 for (auto f : formats) { 153 if (all_of([=](const device &dev) { 154 return dev.pipe->is_format_supported( 155 dev.pipe, f.second, target, 1, bindings); 156 }, ctx.devices())) 157 s.insert(f.first); 158 } 159 160 return s; 161 } 162 } 163