summaryrefslogtreecommitdiff
path: root/src/mesa/pipe/tgsi/exec/tgsi_util.c
blob: d1bc93552655adce58ad71556b6aa8a32712eff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "tgsi_platform.h"
#include "tgsi_core.h"

union pointer_hack
{
   void *pointer;
   unsigned long long uint64;
};

void *
tgsi_align_128bit(
   void *unaligned )
{
   union pointer_hack ph;

   ph.uint64 = 0;
   ph.pointer = unaligned;
   ph.uint64 = (ph.uint64 + 15) & ~15;
   return ph.pointer;
}

unsigned
tgsi_util_get_src_register_swizzle(
   const struct tgsi_src_register *reg,
   unsigned component )
{
   switch( component ) {
   case 0:
      return reg->SwizzleX;
   case 1:
      return reg->SwizzleY;
   case 2:
      return reg->SwizzleZ;
   case 3:
      return reg->SwizzleW;
   default:
      assert( 0 );
   }
   return 0;
}

unsigned
tgsi_util_get_src_register_extswizzle(
   const struct tgsi_src_register_ext_swz *reg,
   unsigned component )
{
   switch( component ) {
   case 0:
      return reg->ExtSwizzleX;
   case 1:
      return reg->ExtSwizzleY;
   case 2:
      return reg->ExtSwizzleZ;
   case 3:
      return reg->ExtSwizzleW;
   default:
      assert( 0 );
   }
   return 0;
}

unsigned
tgsi_util_get_full_src_register_extswizzle(
   const struct tgsi_full_src_register  *reg,
   unsigned component )
{
   unsigned swizzle;

   /*
    * First, calculate  the   extended swizzle for a given channel. This will give
    * us either a channel index into the simple swizzle or  a constant 1 or   0.
    */
   swizzle = tgsi_util_get_src_register_extswizzle(
      &reg->SrcRegisterExtSwz,
      component );

   assert (TGSI_SWIZZLE_X == TGSI_EXTSWIZZLE_X);
   assert (TGSI_SWIZZLE_Y == TGSI_EXTSWIZZLE_Y);
   assert (TGSI_SWIZZLE_Z == TGSI_EXTSWIZZLE_Z);
   assert (TGSI_SWIZZLE_W == TGSI_EXTSWIZZLE_W);
   assert (TGSI_EXTSWIZZLE_ZERO > TGSI_SWIZZLE_W);
   assert (TGSI_EXTSWIZZLE_ONE > TGSI_SWIZZLE_W);

   /*
    * Second, calculate the simple  swizzle  for   the   unswizzled channel index.
    * Leave the constants intact, they are   not   affected by the   simple swizzle.
    */
   if( swizzle <= TGSI_SWIZZLE_W ) {
      swizzle = tgsi_util_get_src_register_swizzle(
         &reg->SrcRegister,
         component );
   }

   return swizzle;
}

void
tgsi_util_set_src_register_swizzle(
   struct tgsi_src_register *reg,
   unsigned swizzle,
   unsigned component )
{
   switch( component ) {
   case 0:
      reg->SwizzleX = swizzle;
      break;
   case 1:
      reg->SwizzleY = swizzle;
      break;
   case 2:
      reg->SwizzleZ = swizzle;
      break;
   case 3:
      reg->SwizzleW = swizzle;
      break;
   default:
      assert( 0 );
   }
}

void
tgsi_util_set_src_register_extswizzle(
   struct tgsi_src_register_ext_swz *reg,
   unsigned swizzle,
   unsigned component )
{
   switch( component ) {
   case 0:
      reg->ExtSwizzleX = swizzle;
      break;
   case 1:
      reg->ExtSwizzleY = swizzle;
      break;
   case 2:
      reg->ExtSwizzleZ = swizzle;
      break;
   case 3:
      reg->ExtSwizzleW = swizzle;
      break;
   default:
      assert( 0 );
   }
}

unsigned
tgsi_util_get_src_register_extnegate(
   const  struct tgsi_src_register_ext_swz *reg,
   unsigned component )
{
   switch( component ) {
   case 0:
      return reg->NegateX;
   case 1:
      return reg->NegateY;
   case 2:
      return reg->NegateZ;
   case 3:
      return reg->NegateW;
   default:
      assert( 0 );
   }
   return 0;
}

void
tgsi_util_set_src_register_extnegate(
   struct tgsi_src_register_ext_swz *reg,
   unsigned negate,
   unsigned component )
{
   switch( component ) {
   case 0:
      reg->NegateX = negate;
      break;
   case 1:
      reg->NegateY = negate;
      break;
   case 2:
      reg->NegateZ = negate;
      break;
   case 3:
      reg->NegateW = negate;
      break;
   default:
      assert( 0 );
   }
}

unsigned
tgsi_util_get_full_src_register_sign_mode(
   const struct  tgsi_full_src_register *reg,
   unsigned component )
{
   unsigned sign_mode;

   if( reg->SrcRegisterExtMod.Absolute ) {
      /* Consider only the post-abs negation. */

      if( reg->SrcRegisterExtMod.Negate ) {
         sign_mode = TGSI_UTIL_SIGN_SET;
      }
      else {
         sign_mode = TGSI_UTIL_SIGN_CLEAR;
      }
   }
   else {
      /* Accumulate the three negations. */

      unsigned negate;

      negate = reg->SrcRegister.Negate;
      if( tgsi_util_get_src_register_extnegate( &reg->SrcRegisterExtSwz, component ) ) {
         negate = !negate;
      }
      if( reg->SrcRegisterExtMod.Negate ) {
         negate = !negate;
      }

      if( negate ) {
         sign_mode = TGSI_UTIL_SIGN_TOGGLE;
      }
      else {
         sign_mode = TGSI_UTIL_SIGN_KEEP;
      }
   }

   return sign_mode;
}

void
tgsi_util_set_full_src_register_sign_mode(
   struct tgsi_full_src_register *reg,
   unsigned sign_mode )
{
   reg->SrcRegisterExtSwz.NegateX = 0;
   reg->SrcRegisterExtSwz.NegateY = 0;
   reg->SrcRegisterExtSwz.NegateZ = 0;
   reg->SrcRegisterExtSwz.NegateW = 0;

   switch (sign_mode)
   {
   case TGSI_UTIL_SIGN_CLEAR:
      reg->SrcRegister.Negate = 0;
      reg->SrcRegisterExtMod.Absolute = 1;
      reg->SrcRegisterExtMod.Negate = 0;
      break;

   case TGSI_UTIL_SIGN_SET:
      reg->SrcRegister.Negate = 0;
      reg->SrcRegisterExtMod.Absolute = 1;
      reg->SrcRegisterExtMod.Negate = 1;
      break;

   case TGSI_UTIL_SIGN_TOGGLE:
      reg->SrcRegister.Negate = 1;
      reg->SrcRegisterExtMod.Absolute = 0;
      reg->SrcRegisterExtMod.Negate = 0;
      break;

   case TGSI_UTIL_SIGN_KEEP:
      reg->SrcRegister.Negate = 0;
      reg->SrcRegisterExtMod.Absolute = 0;
      reg->SrcRegisterExtMod.Negate = 0;
      break;

   default:
      assert( 0 );
   }
}