d7c86d4178d86e9aa306503273d053bbdfe462b4
[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 GLcontext *ctx;
45 int unit;
46 GLboolean alpha;
47
48 /* GL state */
49 GLenum mode;
50 GLenum *source;
51 GLenum *operand;
52 GLuint logscale;
53
54 /* Derived HW state */
55 uint32_t hw;
56 };
57
58 #define __INIT_COMBINER_ALPHA_A GL_TRUE
59 #define __INIT_COMBINER_ALPHA_RGB GL_FALSE
60
61 /* Initialize a combiner_state struct from the texture unit
62 * context. */
63 #define INIT_COMBINER(chan, ctx, rc, i) do { \
64 struct gl_tex_env_combine_state *c = \
65 ctx->Texture.Unit[i]._CurrentCombine; \
66 (rc)->ctx = ctx; \
67 (rc)->unit = i; \
68 (rc)->alpha = __INIT_COMBINER_ALPHA_##chan; \
69 (rc)->mode = c->Mode##chan; \
70 (rc)->source = c->Source##chan; \
71 (rc)->operand = c->Operand##chan; \
72 (rc)->logscale = c->ScaleShift##chan; \
73 (rc)->hw = 0; \
74 } while (0)
75
76 /* Get the combiner source for the specified EXT_texture_env_combine
77 * source. */
78 static uint32_t
79 get_input_source(struct combiner_state *rc, int source)
80 {
81 switch (source) {
82 case GL_TEXTURE:
83 return rc->unit ? COMBINER_SOURCE(TEXTURE1) :
84 COMBINER_SOURCE(TEXTURE0);
85
86 case GL_TEXTURE0:
87 return COMBINER_SOURCE(TEXTURE0);
88
89 case GL_TEXTURE1:
90 return COMBINER_SOURCE(TEXTURE1);
91
92 case GL_CONSTANT:
93 return COMBINER_SOURCE(CONSTANT);
94
95 case GL_PRIMARY_COLOR:
96 return COMBINER_SOURCE(PRIMARY_COLOR);
97
98 case GL_PREVIOUS:
99 return rc->unit ? COMBINER_SOURCE(PREVIOUS) :
100 COMBINER_SOURCE(PRIMARY_COLOR);
101
102 default:
103 assert(0);
104 }
105 }
106
107 /* Get the (possibly inverted) combiner input mapping for the
108 * specified EXT_texture_env_combine operand. */
109 #define INVERT 0x1
110
111 static uint32_t
112 get_input_mapping(struct combiner_state *rc, int operand, int flags)
113 {
114 int map = 0;
115
116 if (!is_color_operand(operand) && !rc->alpha)
117 map |= COMBINER_ALPHA;
118
119 if (is_negative_operand(operand) == !(flags & INVERT))
120 map |= COMBINER_INVERT;
121
122 return map;
123 }
124
125 static uint32_t
126 get_input_arg(struct combiner_state *rc, int arg, int flags)
127 {
128 int source = rc->source[arg];
129 int operand = rc->operand[arg];
130
131 /* Fake several unsupported texture formats. */
132 if (is_texture_source(source)) {
133 int i = (source == GL_TEXTURE ?
134 rc->unit : source - GL_TEXTURE0);
135 struct gl_texture_object *t = rc->ctx->Texture.Unit[i]._Current;
136 gl_format format = t->Image[0][t->BaseLevel]->TexFormat;
137
138 if (format == MESA_FORMAT_A8) {
139 /* Emulated using I8. */
140 if (is_color_operand(operand))
141 return COMBINER_SOURCE(ZERO) |
142 get_input_mapping(rc, operand, flags);
143
144 } else if (format == MESA_FORMAT_L8) {
145 /* Emulated using I8. */
146 if (!is_color_operand(operand))
147 return COMBINER_SOURCE(ZERO) |
148 get_input_mapping(rc, operand,
149 flags ^ INVERT);
150 }
151 }
152
153 return get_input_source(rc, source) |
154 get_input_mapping(rc, operand, flags);
155 }
156
157 /* Bind the combiner input <in> to the combiner source <src>,
158 * possibly inverted. */
159 #define INPUT_SRC(rc, in, src, flags) \
160 (rc)->hw |= ((flags & INVERT ? COMBINER_INVERT : 0) | \
161 COMBINER_SOURCE(src)) << COMBINER_SHIFT(in)
162
163 /* Bind the combiner input <in> to the EXT_texture_env_combine
164 * argument <arg>, possibly inverted. */
165 #define INPUT_ARG(rc, in, arg, flags) \
166 (rc)->hw |= get_input_arg(rc, arg, flags) << COMBINER_SHIFT(in)
167
168 #define UNSIGNED_OP(rc) \
169 (rc)->hw |= ((rc)->logscale ? \
170 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_SCALE2 : \
171 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_IDENTITY)
172 #define SIGNED_OP(rc) \
173 (rc)->hw |= ((rc)->logscale ? \
174 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS_SCALE2 : \
175 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS)
176
177 static void
178 setup_combiner(struct combiner_state *rc)
179 {
180 switch (rc->mode) {
181 case GL_REPLACE:
182 INPUT_ARG(rc, 0, 0, 0);
183 INPUT_SRC(rc, 1, ZERO, INVERT);
184 INPUT_SRC(rc, 2, ZERO, 0);
185 INPUT_SRC(rc, 3, ZERO, 0);
186 UNSIGNED_OP(rc);
187 break;
188
189 case GL_MODULATE:
190 INPUT_ARG(rc, 0, 0, 0);
191 INPUT_ARG(rc, 1, 1, 0);
192 INPUT_SRC(rc, 2, ZERO, 0);
193 INPUT_SRC(rc, 3, ZERO, 0);
194 UNSIGNED_OP(rc);
195 break;
196
197 case GL_ADD:
198 INPUT_ARG(rc, 0, 0, 0);
199 INPUT_SRC(rc, 1, ZERO, INVERT);
200 INPUT_ARG(rc, 2, 1, 0);
201 INPUT_SRC(rc, 3, ZERO, INVERT);
202 UNSIGNED_OP(rc);
203 break;
204
205 case GL_INTERPOLATE:
206 INPUT_ARG(rc, 0, 0, 0);
207 INPUT_ARG(rc, 1, 2, 0);
208 INPUT_ARG(rc, 2, 1, 0);
209 INPUT_ARG(rc, 3, 2, INVERT);
210 UNSIGNED_OP(rc);
211 break;
212
213 case GL_ADD_SIGNED:
214 INPUT_ARG(rc, 0, 0, 0);
215 INPUT_SRC(rc, 1, ZERO, INVERT);
216 INPUT_ARG(rc, 2, 1, 0);
217 INPUT_SRC(rc, 3, ZERO, INVERT);
218 SIGNED_OP(rc);
219 break;
220
221 default:
222 assert(0);
223 }
224 }
225
226 void
227 nv04_emit_tex_env(GLcontext *ctx, int emit)
228 {
229 const int i = emit - NOUVEAU_STATE_TEX_ENV0;
230 struct nouveau_channel *chan = context_chan(ctx);
231 struct nouveau_grobj *fahrenheit = nv04_context_engine(ctx);
232 struct combiner_state rc_a = {}, rc_c = {};
233
234 if (!nv04_mtex_engine(fahrenheit)) {
235 context_dirty(ctx, BLEND);
236 return;
237 }
238
239 /* Compute the new combiner state. */
240 if (ctx->Texture.Unit[i]._ReallyEnabled) {
241 INIT_COMBINER(A, ctx, &rc_a, i);
242 setup_combiner(&rc_a);
243
244 INIT_COMBINER(RGB, ctx, &rc_c, i);
245 setup_combiner(&rc_c);
246
247 } else {
248 if (i == 0) {
249 INPUT_SRC(&rc_a, 0, PRIMARY_COLOR, 0);
250 INPUT_SRC(&rc_c, 0, PRIMARY_COLOR, 0);
251 } else {
252 INPUT_SRC(&rc_a, 0, PREVIOUS, 0);
253 INPUT_SRC(&rc_c, 0, PREVIOUS, 0);
254 }
255
256 INPUT_SRC(&rc_a, 1, ZERO, INVERT);
257 INPUT_SRC(&rc_c, 1, ZERO, INVERT);
258 INPUT_SRC(&rc_a, 2, ZERO, 0);
259 INPUT_SRC(&rc_c, 2, ZERO, 0);
260 INPUT_SRC(&rc_a, 3, ZERO, 0);
261 INPUT_SRC(&rc_c, 3, ZERO, 0);
262
263 UNSIGNED_OP(&rc_a);
264 UNSIGNED_OP(&rc_c);
265 }
266
267 /* Write the register combiner state out to the hardware. */
268 BEGIN_RING(chan, fahrenheit,
269 NV04_MULTITEX_TRIANGLE_COMBINE_ALPHA(i), 2);
270 OUT_RING(chan, rc_a.hw);
271 OUT_RING(chan, rc_c.hw);
272
273 BEGIN_RING(chan, fahrenheit,
274 NV04_MULTITEX_TRIANGLE_COMBINE_FACTOR, 1);
275 OUT_RING(chan, pack_rgba_f(MESA_FORMAT_ARGB8888,
276 ctx->Texture.Unit[0].EnvColor));
277 }