blob: 64151d169da9b904781ef72c083e85ffddcc434e [file] [log] [blame]
José Fonseca866fbac2009-09-07 14:24:31 +01001/**************************************************************************
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
28
29#include "util/u_format.h"
30
31#include "lp_bld_type.h"
32#include "lp_bld_const.h"
33#include "lp_bld_conv.h"
34#include "lp_bld_format.h"
35
36
José Fonseca48f19c02009-09-10 12:14:53 +010037static LLVMValueRef
José Fonsecaabff4212009-10-25 09:06:05 +000038lp_build_format_swizzle_chan_soa(struct lp_type type,
39 const LLVMValueRef *unswizzled,
40 enum util_format_swizzle swizzle)
José Fonseca48f19c02009-09-10 12:14:53 +010041{
42 switch (swizzle) {
43 case UTIL_FORMAT_SWIZZLE_X:
44 case UTIL_FORMAT_SWIZZLE_Y:
45 case UTIL_FORMAT_SWIZZLE_Z:
46 case UTIL_FORMAT_SWIZZLE_W:
José Fonsecaabff4212009-10-25 09:06:05 +000047 return unswizzled[swizzle];
José Fonseca48f19c02009-09-10 12:14:53 +010048 case UTIL_FORMAT_SWIZZLE_0:
49 return lp_build_zero(type);
50 case UTIL_FORMAT_SWIZZLE_1:
51 return lp_build_one(type);
52 case UTIL_FORMAT_SWIZZLE_NONE:
53 return lp_build_undef(type);
54 default:
55 assert(0);
56 return lp_build_undef(type);
57 }
58}
59
60
José Fonseca866fbac2009-09-07 14:24:31 +010061void
José Fonsecaabff4212009-10-25 09:06:05 +000062lp_build_format_swizzle_soa(const struct util_format_description *format_desc,
63 struct lp_type type,
64 const LLVMValueRef *unswizzled,
65 LLVMValueRef *swizzled)
66{
67 if(format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS) {
68 enum util_format_swizzle swizzle = format_desc->swizzle[0];
69 LLVMValueRef depth = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
70 swizzled[2] = swizzled[1] = swizzled[0] = depth;
71 swizzled[3] = lp_build_one(type);
72 }
73 else {
74 unsigned chan;
75 for (chan = 0; chan < 4; ++chan) {
76 enum util_format_swizzle swizzle = format_desc->swizzle[chan];
77 swizzled[chan] = lp_build_format_swizzle_chan_soa(type, unswizzled, swizzle);
78 }
79 }
80}
81
82
83void
José Fonseca866fbac2009-09-07 14:24:31 +010084lp_build_unpack_rgba_soa(LLVMBuilderRef builder,
85 const struct util_format_description *format_desc,
José Fonsecab4835ea2009-09-14 11:05:06 +010086 struct lp_type type,
José Fonseca866fbac2009-09-07 14:24:31 +010087 LLVMValueRef packed,
88 LLVMValueRef *rgba)
89{
90 LLVMValueRef inputs[4];
91 unsigned start;
92 unsigned chan;
93
94 /* FIXME: Support more formats */
José Fonsecacd0a3962009-10-03 22:15:17 +010095 assert(format_desc->layout == UTIL_FORMAT_LAYOUT_ARITH ||
96 (format_desc->layout == UTIL_FORMAT_LAYOUT_ARRAY &&
97 format_desc->block.bits == format_desc->channel[0].size));
José Fonseca866fbac2009-09-07 14:24:31 +010098 assert(format_desc->block.width == 1);
99 assert(format_desc->block.height == 1);
100 assert(format_desc->block.bits <= 32);
101
102 /* Decode the input vector components */
103 start = 0;
104 for (chan = 0; chan < 4; ++chan) {
105 unsigned width = format_desc->channel[chan].size;
106 unsigned stop = start + width;
107 LLVMValueRef input;
108
109 input = packed;
110
111 switch(format_desc->channel[chan].type) {
112 case UTIL_FORMAT_TYPE_VOID:
113 input = NULL;
114 break;
115
116 case UTIL_FORMAT_TYPE_UNSIGNED:
117 if(type.floating) {
118 if(start)
119 input = LLVMBuildLShr(builder, input, lp_build_int_const_scalar(type, start), "");
120 if(stop < format_desc->block.bits) {
121 unsigned mask = ((unsigned long long)1 << width) - 1;
122 input = LLVMBuildAnd(builder, input, lp_build_int_const_scalar(type, mask), "");
123 }
124
125 if(format_desc->channel[chan].normalized)
126 input = lp_build_unsigned_norm_to_float(builder, width, type, input);
127 else
128 input = LLVMBuildFPToSI(builder, input, lp_build_vec_type(type), "");
129 }
130 else {
131 /* FIXME */
132 assert(0);
133 input = lp_build_undef(type);
134 }
135 break;
136
137 default:
138 /* fall through */
139 input = lp_build_undef(type);
140 break;
141 }
142
143 inputs[chan] = input;
144
145 start = stop;
146 }
147
José Fonsecaabff4212009-10-25 09:06:05 +0000148 lp_build_format_swizzle_soa(format_desc, type, inputs, rgba);
José Fonseca866fbac2009-09-07 14:24:31 +0100149}