Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / mesa / drivers / dri / nouveau / nv04_state_frag.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_util.h"
30 #include "nouveau_class.h"
31 #include "nv04_driver.h"
32
33 #define COMBINER_SHIFT(in) \
34 (NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT##in##_SHIFT \
35 - NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0_SHIFT)
36 #define COMBINER_SOURCE(reg) \
37 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0_##reg
38 #define COMBINER_INVERT \
39 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_INVERSE0
40 #define COMBINER_ALPHA \
41 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ALPHA0
42
43 struct combiner_state {
44 struct gl_context *ctx;
45 int unit;
46 GLboolean alpha;
47 GLboolean premodulate;
48
49 /* GL state */
50 GLenum mode;
51 GLenum *source;
52 GLenum *operand;
53 GLuint logscale;
54
55 /* Derived HW state */
56 uint32_t hw;
57 };
58
59 #define __INIT_COMBINER_ALPHA_A GL_TRUE
60 #define __INIT_COMBINER_ALPHA_RGB GL_FALSE
61
62 /* Initialize a combiner_state struct from the texture unit
63 * context. */
64 #define INIT_COMBINER(chan, ctx, rc, i) do { \
65 struct gl_tex_env_combine_state *c = \
66 ctx->Texture.Unit[i]._CurrentCombine; \
67 (rc)->ctx = ctx; \
68 (rc)->unit = i; \
69 (rc)->alpha = __INIT_COMBINER_ALPHA_##chan; \
70 (rc)->premodulate = c->_NumArgs##chan == 4; \
71 (rc)->mode = c->Mode##chan; \
72 (rc)->source = c->Source##chan; \
73 (rc)->operand = c->Operand##chan; \
74 (rc)->logscale = c->ScaleShift##chan; \
75 (rc)->hw = 0; \
76 } while (0)
77
78 /* Get the combiner source for the specified EXT_texture_env_combine
79 * source. */
80 static uint32_t
81 get_input_source(struct combiner_state *rc, int source)
82 {
83 switch (source) {
84 case GL_ZERO:
85 return COMBINER_SOURCE(ZERO);
86
87 case GL_TEXTURE:
88 return rc->unit ? COMBINER_SOURCE(TEXTURE1) :
89 COMBINER_SOURCE(TEXTURE0);
90
91 case GL_TEXTURE0:
92 return COMBINER_SOURCE(TEXTURE0);
93
94 case GL_TEXTURE1:
95 return COMBINER_SOURCE(TEXTURE1);
96
97 case GL_CONSTANT:
98 return COMBINER_SOURCE(CONSTANT);
99
100 case GL_PRIMARY_COLOR:
101 return COMBINER_SOURCE(PRIMARY_COLOR);
102
103 case GL_PREVIOUS:
104 return rc->unit ? COMBINER_SOURCE(PREVIOUS) :
105 COMBINER_SOURCE(PRIMARY_COLOR);
106
107 default:
108 assert(0);
109 }
110 }
111
112 /* Get the (possibly inverted) combiner input mapping for the
113 * specified EXT_texture_env_combine operand. */
114 #define INVERT 0x1
115
116 static uint32_t
117 get_input_mapping(struct combiner_state *rc, int operand, int flags)
118 {
119 int map = 0;
120
121 if (!is_color_operand(operand) && !rc->alpha)
122 map |= COMBINER_ALPHA;
123
124 if (is_negative_operand(operand) == !(flags & INVERT))
125 map |= COMBINER_INVERT;
126
127 return map;
128 }
129
130 static uint32_t
131 get_input_arg(struct combiner_state *rc, int arg, int flags)
132 {
133 int source = rc->source[arg];
134 int operand = rc->operand[arg];
135
136 /* Fake several unsupported texture formats. */
137 if (is_texture_source(source)) {
138 int i = (source == GL_TEXTURE ?
139 rc->unit : source - GL_TEXTURE0);
140 struct gl_texture_object *t = rc->ctx->Texture.Unit[i]._Current;
141 gl_format format = t->Image[0][t->BaseLevel]->TexFormat;
142
143 if (format == MESA_FORMAT_A8) {
144 /* Emulated using I8. */
145 if (is_color_operand(operand))
146 return COMBINER_SOURCE(ZERO) |
147 get_input_mapping(rc, operand, flags);
148
149 } else if (format == MESA_FORMAT_L8) {
150 /* Emulated using I8. */
151 if (!is_color_operand(operand))
152 return COMBINER_SOURCE(ZERO) |
153 get_input_mapping(rc, operand,
154 flags ^ INVERT);
155 }
156 }
157
158 return get_input_source(rc, source) |
159 get_input_mapping(rc, operand, flags);
160 }
161
162 /* Bind the combiner input <in> to the combiner source <src>,
163 * possibly inverted. */
164 #define INPUT_SRC(rc, in, src, flags) \
165 (rc)->hw |= ((flags & INVERT ? COMBINER_INVERT : 0) | \
166 COMBINER_SOURCE(src)) << COMBINER_SHIFT(in)
167
168 /* Bind the combiner input <in> to the EXT_texture_env_combine
169 * argument <arg>, possibly inverted. */
170 #define INPUT_ARG(rc, in, arg, flags) \
171 (rc)->hw |= get_input_arg(rc, arg, flags) << COMBINER_SHIFT(in)
172
173 #define UNSIGNED_OP(rc) \
174 (rc)->hw |= ((rc)->logscale ? \
175 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_SCALE2 : \
176 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_IDENTITY)
177 #define SIGNED_OP(rc) \
178 (rc)->hw |= ((rc)->logscale ? \
179 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS_SCALE2 : \
180 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS)
181
182 static void
183 setup_combiner(struct combiner_state *rc)
184 {
185 switch (rc->mode) {
186 case GL_REPLACE:
187 INPUT_ARG(rc, 0, 0, 0);
188 INPUT_SRC(rc, 1, ZERO, INVERT);
189 INPUT_SRC(rc, 2, ZERO, 0);
190 INPUT_SRC(rc, 3, ZERO, 0);
191 UNSIGNED_OP(rc);
192 break;
193
194 case GL_MODULATE:
195 INPUT_ARG(rc, 0, 0, 0);
196 INPUT_ARG(rc, 1, 1, 0);
197 INPUT_SRC(rc, 2, ZERO, 0);
198 INPUT_SRC(rc, 3, ZERO, 0);
199 UNSIGNED_OP(rc);
200 break;
201
202 case GL_ADD:
203 case GL_ADD_SIGNED:
204 if (rc->premodulate) {
205 INPUT_ARG(rc, 0, 0, 0);
206 INPUT_ARG(rc, 1, 1, 0);
207 INPUT_ARG(rc, 2, 2, 0);
208 INPUT_ARG(rc, 3, 3, 0);
209 } else {
210 INPUT_ARG(rc, 0, 0, 0);
211 INPUT_SRC(rc, 1, ZERO, INVERT);
212 INPUT_ARG(rc, 2, 1, 0);
213 INPUT_SRC(rc, 3, ZERO, INVERT);
214 }
215
216 if (rc->mode == GL_ADD_SIGNED)
217 SIGNED_OP(rc);
218 else
219 UNSIGNED_OP(rc);
220
221 break;
222
223 case GL_INTERPOLATE:
224 INPUT_ARG(rc, 0, 0, 0);
225 INPUT_ARG(rc, 1, 2, 0);
226 INPUT_ARG(rc, 2, 1, 0);
227 INPUT_ARG(rc, 3, 2, INVERT);
228 UNSIGNED_OP(rc);
229 break;
230
231 default:
232 assert(0);
233 }
234 }
235
236 void
237 nv04_emit_tex_env(struct gl_context *ctx, int emit)
238 {
239 const int i = emit - NOUVEAU_STATE_TEX_ENV0;
240 struct nouveau_channel *chan = context_chan(ctx);
241 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
242 struct combiner_state rc_a = {}, rc_c = {};
243
244 if (!nv04_mtex_engine(fahrenheit)) {
245 context_dirty(ctx, BLEND);
246 return;
247 }
248
249 /* Compute the new combiner state. */
250 if (ctx->Texture.Unit[i]._ReallyEnabled) {
251 INIT_COMBINER(A, ctx, &rc_a, i);
252 setup_combiner(&rc_a);
253
254 INIT_COMBINER(RGB, ctx, &rc_c, i);
255 setup_combiner(&rc_c);
256
257 } else {
258 if (i == 0) {
259 INPUT_SRC(&rc_a, 0, PRIMARY_COLOR, 0);
260 INPUT_SRC(&rc_c, 0, PRIMARY_COLOR, 0);
261 } else {
262 INPUT_SRC(&rc_a, 0, PREVIOUS, 0);
263 INPUT_SRC(&rc_c, 0, PREVIOUS, 0);
264 }
265
266 INPUT_SRC(&rc_a, 1, ZERO, INVERT);
267 INPUT_SRC(&rc_c, 1, ZERO, INVERT);
268 INPUT_SRC(&rc_a, 2, ZERO, 0);
269 INPUT_SRC(&rc_c, 2, ZERO, 0);
270 INPUT_SRC(&rc_a, 3, ZERO, 0);
271 INPUT_SRC(&rc_c, 3, ZERO, 0);
272
273 UNSIGNED_OP(&rc_a);
274 UNSIGNED_OP(&rc_c);
275 }
276
277 /* Write the register combiner state out to the hardware. */
278 BEGIN_RING(chan, fahrenheit,
279 NV04_MULTITEX_TRIANGLE_COMBINE_ALPHA(i), 2);
280 OUT_RING(chan, rc_a.hw);
281 OUT_RING(chan, rc_c.hw);
282
283 BEGIN_RING(chan, fahrenheit,
284 NV04_MULTITEX_TRIANGLE_COMBINE_FACTOR, 1);
285 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
286 ctx->Texture.Unit[0].EnvColor));
287 }