Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lp_bld_format_soa.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************
  2. *
  3. * Copyright 2009 VMware, Inc.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21. * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
  22. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. #include "util/u_format.h"
  28. #include "lp_bld_type.h"
  29. #include "lp_bld_const.h"
  30. #include "lp_bld_conv.h"
  31. #include "lp_bld_format.h"
  32. static LLVMValueRef
  33. lp_build_format_swizzle_chan_soa(struct lp_type type,
  34. const LLVMValueRef *unswizzled,
  35. enum util_format_swizzle swizzle)
  36. {
  37. switch (swizzle) {
  38. case UTIL_FORMAT_SWIZZLE_X:
  39. case UTIL_FORMAT_SWIZZLE_Y:
  40. case UTIL_FORMAT_SWIZZLE_Z:
  41. case UTIL_FORMAT_SWIZZLE_W:
  42. return unswizzled[swizzle];
  43. case UTIL_FORMAT_SWIZZLE_0:
  44. return lp_build_zero(type);
  45. case UTIL_FORMAT_SWIZZLE_1:
  46. return lp_build_one(type);
  47. case UTIL_FORMAT_SWIZZLE_NONE:
  48. return lp_build_undef(type);
  49. default:
  50. assert(0);
  51. return lp_build_undef(type);
  52. }
  53. }
  54. void
  55. lp_build_format_swizzle_soa(const struct util_format_description *format_desc,
  56. struct lp_type type,
  57. const LLVMValueRef *unswizzled,
  58. LLVMValueRef *swizzled)
  59. {
  60. if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
  61. enum util_format_swizzle swizzle = format_desc->swizzle[0];
  62. LLVMValueRef depth = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
  63. swizzled[2] = swizzled[1] = swizzled[0] = depth;
  64. swizzled[3] = lp_build_one(type);
  65. }
  66. else {
  67. unsigned chan;
  68. for (chan = 0; chan < 4; ++chan) {
  69. enum util_format_swizzle swizzle = format_desc->swizzle[chan];
  70. swizzled[chan] = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
  71. }
  72. }
  73. }
  74. void
  75. lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
  76. const struct util_format_description *format_desc,
  77. struct lp_type type,
  78. LLVMValueRef packed,
  79. LLVMValueRef *rgba)
  80. {
  81. LLVMValueRef inputs[4];
  82. unsigned start;
  83. unsigned chan;
  84. /* FIXME: Support more formats */
  85. assert(format_desc->is_bitmask);
  86. assert(format_desc->block.width == 1);
  87. assert(format_desc->block.height == 1);
  88. assert(format_desc->block.bits <= 32);
  89. /* Decode the input vector components */
  90. start = 0;
  91. for (chan = 0; chan < format_desc->nr_channels; ++chan) {
  92. unsigned width = format_desc->channel[chan].size;
  93. unsigned stop = start + width;
  94. LLVMValueRef input;
  95. input = packed;
  96. switch(format_desc->channel[chan].type) {
  97. case UTIL_FORMAT_TYPE_VOID:
  98. input = lp_build_undef(type);
  99. break;
  100. case UTIL_FORMAT_TYPE_UNSIGNED:
  101. /*
  102. * Align the LSB
  103. */
  104. if (start) {
  105. input = LLVMBuildLShr(builder, input, lp_build_const_int_vec(type, start), "");
  106. }
  107. /*
  108. * Zero the MSBs
  109. */
  110. if (stop < format_desc->block.bits) {
  111. unsigned mask = ((unsigned long long)1 << width) - 1;
  112. input = LLVMBuildAnd(builder, input, lp_build_const_int_vec(type, mask), "");
  113. }
  114. /*
  115. * Type conversion
  116. */
  117. if (type.floating) {
  118. if(format_desc->channel[chan].normalized)
  119. input = lp_build_unsigned_norm_to_float(builder, width, type, input);
  120. else
  121. input = LLVMBuildSIToFP(builder, input, lp_build_vec_type(type), "");
  122. }
  123. else {
  124. /* FIXME */
  125. assert(0);
  126. input = lp_build_undef(type);
  127. }
  128. break;
  129. case UTIL_FORMAT_TYPE_SIGNED:
  130. /*
  131. * Align the sign bit first.
  132. */
  133. if (stop < type.width) {
  134. unsigned bits = type.width - stop;
  135. LLVMValueRef bits_val = lp_build_const_int_vec(type, bits);
  136. input = LLVMBuildShl(builder, input, bits_val, "");
  137. }
  138. /*
  139. * Align the LSB (with an arithmetic shift to preserve the sign)
  140. */
  141. if (format_desc->channel[chan].size < type.width) {
  142. unsigned bits = type.width - format_desc->channel[chan].size;
  143. LLVMValueRef bits_val = lp_build_const_int_vec(type, bits);
  144. input = LLVMBuildAShr(builder, input, bits_val, "");
  145. }
  146. /*
  147. * Type conversion
  148. */
  149. if (type.floating) {
  150. input = LLVMBuildSIToFP(builder, input, lp_build_vec_type(type), "");
  151. if (format_desc->channel[chan].normalized) {
  152. double scale = 1.0 / ((1 << (format_desc->channel[chan].size - 1)) - 1);
  153. LLVMValueRef scale_val = lp_build_const_vec(type, scale);
  154. input = LLVMBuildMul(builder, input, scale_val, "");
  155. }
  156. }
  157. else {
  158. /* FIXME */
  159. assert(0);
  160. input = lp_build_undef(type);
  161. }
  162. break;
  163. default:
  164. input = lp_build_undef(type);
  165. break;
  166. }
  167. inputs[chan] = input;
  168. start = stop;
  169. }
  170. lp_build_format_swizzle_soa(format_desc, type, inputs, rgba);
  171. }