• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2008 Nicolai Haehnle.
3  * SPDX-License-Identifier: MIT
4  */
5 /**
6  * @file
7  * Utilities to deal with the somewhat odd restriction on R300 fragment
8  * program swizzles.
9  */
10 
11 #include "r300_fragprog_swizzle.h"
12 
13 #include <stdio.h>
14 
15 #include "util/macros.h"
16 
17 #include "r300_reg.h"
18 #include "radeon_compiler.h"
19 
20 #define MAKE_SWZ3(x, y, z)                                                                         \
21    (RC_MAKE_SWIZZLE(RC_SWIZZLE_##x, RC_SWIZZLE_##y, RC_SWIZZLE_##z, RC_SWIZZLE_ZERO))
22 
23 struct swizzle_data {
24    unsigned int hash;        /**< swizzle value this matches */
25    unsigned int base;        /**< base value for hw swizzle */
26    unsigned int stride;      /**< difference in base between arg0/1/2 */
27    unsigned int srcp_stride; /**< difference in base between arg0/scrp */
28 };
29 
30 static const struct swizzle_data native_swizzles[] = {
31    {MAKE_SWZ3(X, Y, Z), R300_ALU_ARGC_SRC0C_XYZ, 4, 15},
32    {MAKE_SWZ3(X, X, X), R300_ALU_ARGC_SRC0C_XXX, 4, 15},
33    {MAKE_SWZ3(Y, Y, Y), R300_ALU_ARGC_SRC0C_YYY, 4, 15},
34    {MAKE_SWZ3(Z, Z, Z), R300_ALU_ARGC_SRC0C_ZZZ, 4, 15},
35    {MAKE_SWZ3(W, W, W), R300_ALU_ARGC_SRC0A, 1, 7},
36    {MAKE_SWZ3(Y, Z, X), R300_ALU_ARGC_SRC0C_YZX, 1, 0},
37    {MAKE_SWZ3(Z, X, Y), R300_ALU_ARGC_SRC0C_ZXY, 1, 0},
38    {MAKE_SWZ3(W, Z, Y), R300_ALU_ARGC_SRC0CA_WZY, 1, 0},
39    {MAKE_SWZ3(ONE, ONE, ONE), R300_ALU_ARGC_ONE, 0, 0},
40    {MAKE_SWZ3(ZERO, ZERO, ZERO), R300_ALU_ARGC_ZERO, 0, 0},
41    {MAKE_SWZ3(HALF, HALF, HALF), R300_ALU_ARGC_HALF, 0, 0}};
42 
43 static const int num_native_swizzles = ARRAY_SIZE(native_swizzles);
44 /* Only swizzles with srcp_stride != 0 can be used for presub, so
45  * just the first five from the list. */
46 static const int num_presub_swizzles = 5;
47 
48 /**
49  * Find a native RGB swizzle that matches the given swizzle.
50  * Returns 0 if none found.
51  */
52 static const struct swizzle_data *
lookup_native_swizzle(unsigned int swizzle)53 lookup_native_swizzle(unsigned int swizzle)
54 {
55    int i, comp;
56 
57    for (i = 0; i < num_native_swizzles; ++i) {
58       const struct swizzle_data *sd = &native_swizzles[i];
59       for (comp = 0; comp < 3; ++comp) {
60          unsigned int swz = GET_SWZ(swizzle, comp);
61          if (swz == RC_SWIZZLE_UNUSED)
62             continue;
63          if (swz != GET_SWZ(sd->hash, comp))
64             break;
65       }
66       if (comp == 3)
67          return sd;
68    }
69 
70    return NULL;
71 }
72 
73 /**
74  * Determines if the given swizzle is valid for r300/r400.  In most situations
75  * it is better to use r300_swizzle_is_native() which can be accessed via
76  * struct radeon_compiler *c; c->SwizzleCaps->IsNative().
77  */
78 int
r300_swizzle_is_native_basic(unsigned int swizzle)79 r300_swizzle_is_native_basic(unsigned int swizzle)
80 {
81    if (lookup_native_swizzle(swizzle))
82       return 1;
83    else
84       return 0;
85 }
86 
87 /**
88  * Check whether the given instruction supports the swizzle and negate
89  * combinations in the given source register.
90  */
91 static int
r300_swizzle_is_native(rc_opcode opcode,struct rc_src_register reg)92 r300_swizzle_is_native(rc_opcode opcode, struct rc_src_register reg)
93 {
94    const struct swizzle_data *sd;
95    unsigned int relevant;
96    int j;
97 
98    if (opcode == RC_OPCODE_KIL || opcode == RC_OPCODE_TEX || opcode == RC_OPCODE_TXB ||
99        opcode == RC_OPCODE_TXP) {
100       if (reg.Abs || reg.Negate)
101          return 0;
102 
103       /* Texture coordinates can be only read from temporary file,
104        * input is just a temporary with varying in it.
105        */
106       if (reg.File != RC_FILE_TEMPORARY && reg.File != RC_FILE_INPUT)
107          return 0;
108 
109       for (j = 0; j < 4; ++j) {
110          unsigned int swz = GET_SWZ(reg.Swizzle, j);
111          if (swz == RC_SWIZZLE_UNUSED)
112             continue;
113          if (swz != j)
114             return 0;
115       }
116 
117       return 1;
118    }
119 
120    relevant = 0;
121 
122    for (j = 0; j < 3; ++j)
123       if (GET_SWZ(reg.Swizzle, j) != RC_SWIZZLE_UNUSED)
124          relevant |= 1 << j;
125 
126    if ((reg.Negate & relevant) && ((reg.Negate & relevant) != relevant))
127       return 0;
128 
129    sd = lookup_native_swizzle(reg.Swizzle);
130    if (!sd || (reg.File == RC_FILE_PRESUB && sd->srcp_stride == 0))
131       return 0;
132 
133    return 1;
134 }
135 
136 static void
r300_swizzle_split(struct rc_src_register src,unsigned int mask,struct rc_swizzle_split * split)137 r300_swizzle_split(struct rc_src_register src, unsigned int mask, struct rc_swizzle_split *split)
138 {
139    split->NumPhases = 0;
140 
141    while (mask) {
142       unsigned int best_matchcount = 0;
143       unsigned int best_matchmask = 0;
144       int i, comp;
145 
146       unsigned num_swizzles =
147          src.File == RC_FILE_PRESUB ? num_presub_swizzles : num_native_swizzles;
148 
149       for (i = 0; i < num_swizzles; ++i) {
150          const struct swizzle_data *sd = &native_swizzles[i];
151          unsigned int matchcount = 0;
152          unsigned int matchmask = 0;
153          for (comp = 0; comp < 3; ++comp) {
154             unsigned int swz;
155             if (!GET_BIT(mask, comp))
156                continue;
157             swz = GET_SWZ(src.Swizzle, comp);
158             if (swz == RC_SWIZZLE_UNUSED)
159                continue;
160             if (swz == GET_SWZ(sd->hash, comp)) {
161                /* check if the negate bit of current component
162                 * is the same for already matched components */
163                if (matchmask && (!!(src.Negate & matchmask) != !!(src.Negate & (1 << comp))))
164                   continue;
165 
166                matchcount++;
167                matchmask |= 1 << comp;
168             }
169          }
170          if (matchcount > best_matchcount) {
171             best_matchcount = matchcount;
172             best_matchmask = matchmask;
173             if (matchmask == (mask & RC_MASK_XYZ))
174                break;
175          }
176       }
177 
178       if (mask & RC_MASK_W)
179          best_matchmask |= RC_MASK_W;
180 
181       split->Phase[split->NumPhases++] = best_matchmask;
182       mask &= ~best_matchmask;
183    }
184 }
185 
186 const struct rc_swizzle_caps r300_swizzle_caps = {.IsNative = r300_swizzle_is_native,
187                                                   .Split = r300_swizzle_split};
188 
189 /**
190  * Translate an RGB (XYZ) swizzle into the hardware code for the given
191  * instruction source.
192  */
193 unsigned int
r300FPTranslateRGBSwizzle(unsigned int src,unsigned int swizzle)194 r300FPTranslateRGBSwizzle(unsigned int src, unsigned int swizzle)
195 {
196    const struct swizzle_data *sd = lookup_native_swizzle(swizzle);
197 
198    if (!sd || (src == RC_PAIR_PRESUB_SRC && sd->srcp_stride == 0)) {
199       fprintf(stderr, "Not a native swizzle: %08x\n", swizzle);
200       return 0;
201    }
202 
203    if (src == RC_PAIR_PRESUB_SRC) {
204       return sd->base + sd->srcp_stride;
205    } else {
206       return sd->base + src * sd->stride;
207    }
208 }
209 
210 /**
211  * Translate an Alpha (W) swizzle into the hardware code for the given
212  * instruction source.
213  */
214 unsigned int
r300FPTranslateAlphaSwizzle(unsigned int src,unsigned int swizzle)215 r300FPTranslateAlphaSwizzle(unsigned int src, unsigned int swizzle)
216 {
217    unsigned int swz = GET_SWZ(swizzle, 0);
218    if (src == RC_PAIR_PRESUB_SRC) {
219       return R300_ALU_ARGA_SRCP_X + swz;
220    }
221    if (swz < 3)
222       return swz + 3 * src;
223 
224    switch (swz) {
225    case RC_SWIZZLE_W:
226       return R300_ALU_ARGA_SRC0A + src;
227    case RC_SWIZZLE_ONE:
228       return R300_ALU_ARGA_ONE;
229    case RC_SWIZZLE_ZERO:
230       return R300_ALU_ARGA_ZERO;
231    case RC_SWIZZLE_HALF:
232       return R300_ALU_ARGA_HALF;
233    default:
234       return R300_ALU_ARGA_ONE;
235    }
236 }
237