Merge remote branch 'main/master' into radeon-rewrite
[mesa.git] / src / gallium / auxiliary / tgsi / tgsi_util.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 #include "util/u_debug.h"
29 #include "pipe/p_shader_tokens.h"
30 #include "tgsi_parse.h"
31 #include "tgsi_build.h"
32 #include "tgsi_util.h"
33
34 union pointer_hack
35 {
36 void *pointer;
37 uint64_t uint64;
38 };
39
40 void *
41 tgsi_align_128bit(
42 void *unaligned )
43 {
44 union pointer_hack ph;
45
46 ph.uint64 = 0;
47 ph.pointer = unaligned;
48 ph.uint64 = (ph.uint64 + 15) & ~15;
49 return ph.pointer;
50 }
51
52 unsigned
53 tgsi_util_get_src_register_swizzle(
54 const struct tgsi_src_register *reg,
55 unsigned component )
56 {
57 switch( component ) {
58 case 0:
59 return reg->SwizzleX;
60 case 1:
61 return reg->SwizzleY;
62 case 2:
63 return reg->SwizzleZ;
64 case 3:
65 return reg->SwizzleW;
66 default:
67 assert( 0 );
68 }
69 return 0;
70 }
71
72 unsigned
73 tgsi_util_get_src_register_extswizzle(
74 const struct tgsi_src_register_ext_swz *reg,
75 unsigned component )
76 {
77 switch( component ) {
78 case 0:
79 return reg->ExtSwizzleX;
80 case 1:
81 return reg->ExtSwizzleY;
82 case 2:
83 return reg->ExtSwizzleZ;
84 case 3:
85 return reg->ExtSwizzleW;
86 default:
87 assert( 0 );
88 }
89 return 0;
90 }
91
92 unsigned
93 tgsi_util_get_full_src_register_extswizzle(
94 const struct tgsi_full_src_register *reg,
95 unsigned component )
96 {
97 unsigned swizzle;
98
99 /*
100 * First, calculate the extended swizzle for a given channel. This will give
101 * us either a channel index into the simple swizzle or a constant 1 or 0.
102 */
103 swizzle = tgsi_util_get_src_register_extswizzle(
104 &reg->SrcRegisterExtSwz,
105 component );
106
107 assert (TGSI_SWIZZLE_X == TGSI_EXTSWIZZLE_X);
108 assert (TGSI_SWIZZLE_Y == TGSI_EXTSWIZZLE_Y);
109 assert (TGSI_SWIZZLE_Z == TGSI_EXTSWIZZLE_Z);
110 assert (TGSI_SWIZZLE_W == TGSI_EXTSWIZZLE_W);
111 assert (TGSI_EXTSWIZZLE_ZERO > TGSI_SWIZZLE_W);
112 assert (TGSI_EXTSWIZZLE_ONE > TGSI_SWIZZLE_W);
113
114 /*
115 * Second, calculate the simple swizzle for the unswizzled channel index.
116 * Leave the constants intact, they are not affected by the simple swizzle.
117 */
118 if( swizzle <= TGSI_SWIZZLE_W ) {
119 swizzle = tgsi_util_get_src_register_swizzle(
120 &reg->SrcRegister,
121 swizzle );
122 }
123
124 return swizzle;
125 }
126
127 void
128 tgsi_util_set_src_register_swizzle(
129 struct tgsi_src_register *reg,
130 unsigned swizzle,
131 unsigned component )
132 {
133 switch( component ) {
134 case 0:
135 reg->SwizzleX = swizzle;
136 break;
137 case 1:
138 reg->SwizzleY = swizzle;
139 break;
140 case 2:
141 reg->SwizzleZ = swizzle;
142 break;
143 case 3:
144 reg->SwizzleW = swizzle;
145 break;
146 default:
147 assert( 0 );
148 }
149 }
150
151 void
152 tgsi_util_set_src_register_extswizzle(
153 struct tgsi_src_register_ext_swz *reg,
154 unsigned swizzle,
155 unsigned component )
156 {
157 switch( component ) {
158 case 0:
159 reg->ExtSwizzleX = swizzle;
160 break;
161 case 1:
162 reg->ExtSwizzleY = swizzle;
163 break;
164 case 2:
165 reg->ExtSwizzleZ = swizzle;
166 break;
167 case 3:
168 reg->ExtSwizzleW = swizzle;
169 break;
170 default:
171 assert( 0 );
172 }
173 }
174
175 unsigned
176 tgsi_util_get_src_register_extnegate(
177 const struct tgsi_src_register_ext_swz *reg,
178 unsigned component )
179 {
180 switch( component ) {
181 case 0:
182 return reg->NegateX;
183 case 1:
184 return reg->NegateY;
185 case 2:
186 return reg->NegateZ;
187 case 3:
188 return reg->NegateW;
189 default:
190 assert( 0 );
191 }
192 return 0;
193 }
194
195 void
196 tgsi_util_set_src_register_extnegate(
197 struct tgsi_src_register_ext_swz *reg,
198 unsigned negate,
199 unsigned component )
200 {
201 switch( component ) {
202 case 0:
203 reg->NegateX = negate;
204 break;
205 case 1:
206 reg->NegateY = negate;
207 break;
208 case 2:
209 reg->NegateZ = negate;
210 break;
211 case 3:
212 reg->NegateW = negate;
213 break;
214 default:
215 assert( 0 );
216 }
217 }
218
219 unsigned
220 tgsi_util_get_full_src_register_sign_mode(
221 const struct tgsi_full_src_register *reg,
222 unsigned component )
223 {
224 unsigned sign_mode;
225
226 if( reg->SrcRegisterExtMod.Absolute ) {
227 /* Consider only the post-abs negation. */
228
229 if( reg->SrcRegisterExtMod.Negate ) {
230 sign_mode = TGSI_UTIL_SIGN_SET;
231 }
232 else {
233 sign_mode = TGSI_UTIL_SIGN_CLEAR;
234 }
235 }
236 else {
237 /* Accumulate the three negations. */
238
239 unsigned negate;
240
241 negate = reg->SrcRegister.Negate;
242 if( tgsi_util_get_src_register_extnegate( &reg->SrcRegisterExtSwz, component ) ) {
243 negate = !negate;
244 }
245 if( reg->SrcRegisterExtMod.Negate ) {
246 negate = !negate;
247 }
248
249 if( negate ) {
250 sign_mode = TGSI_UTIL_SIGN_TOGGLE;
251 }
252 else {
253 sign_mode = TGSI_UTIL_SIGN_KEEP;
254 }
255 }
256
257 return sign_mode;
258 }
259
260 void
261 tgsi_util_set_full_src_register_sign_mode(
262 struct tgsi_full_src_register *reg,
263 unsigned sign_mode )
264 {
265 reg->SrcRegisterExtSwz.NegateX = 0;
266 reg->SrcRegisterExtSwz.NegateY = 0;
267 reg->SrcRegisterExtSwz.NegateZ = 0;
268 reg->SrcRegisterExtSwz.NegateW = 0;
269
270 switch (sign_mode)
271 {
272 case TGSI_UTIL_SIGN_CLEAR:
273 reg->SrcRegister.Negate = 0;
274 reg->SrcRegisterExtMod.Absolute = 1;
275 reg->SrcRegisterExtMod.Negate = 0;
276 break;
277
278 case TGSI_UTIL_SIGN_SET:
279 reg->SrcRegister.Negate = 0;
280 reg->SrcRegisterExtMod.Absolute = 1;
281 reg->SrcRegisterExtMod.Negate = 1;
282 break;
283
284 case TGSI_UTIL_SIGN_TOGGLE:
285 reg->SrcRegister.Negate = 1;
286 reg->SrcRegisterExtMod.Absolute = 0;
287 reg->SrcRegisterExtMod.Negate = 0;
288 break;
289
290 case TGSI_UTIL_SIGN_KEEP:
291 reg->SrcRegister.Negate = 0;
292 reg->SrcRegisterExtMod.Absolute = 0;
293 reg->SrcRegisterExtMod.Negate = 0;
294 break;
295
296 default:
297 assert( 0 );
298 }
299 }