2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_util.h"
30 #include "nv_object.xml.h"
31 #include "nv04_3d.xml.h"
32 #include "nv04_driver.h"
34 #define COMBINER_SHIFT(in) \
35 (NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT##in##__SHIFT \
36 - NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0__SHIFT)
37 #define COMBINER_SOURCE(reg) \
38 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ARGUMENT0_##reg
39 #define COMBINER_INVERT \
40 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_INVERSE0
41 #define COMBINER_ALPHA \
42 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_ALPHA0
44 struct combiner_state
{
45 struct gl_context
*ctx
;
48 GLboolean premodulate
;
56 /* Derived HW state */
60 #define __INIT_COMBINER_ALPHA_A GL_TRUE
61 #define __INIT_COMBINER_ALPHA_RGB GL_FALSE
63 /* Initialize a combiner_state struct from the texture unit
65 #define INIT_COMBINER(chan, ctx, rc, i) do { \
66 struct gl_tex_env_combine_state *c = \
67 ctx->Texture.Unit[i]._CurrentCombine; \
70 (rc)->alpha = __INIT_COMBINER_ALPHA_##chan; \
71 (rc)->premodulate = c->_NumArgs##chan == 4; \
72 (rc)->mode = c->Mode##chan; \
73 (rc)->source = c->Source##chan; \
74 (rc)->operand = c->Operand##chan; \
75 (rc)->logscale = c->ScaleShift##chan; \
79 /* Get the combiner source for the specified EXT_texture_env_combine
82 get_input_source(struct combiner_state
*rc
, int source
)
86 return COMBINER_SOURCE(ZERO
);
89 return rc
->unit
? COMBINER_SOURCE(TEXTURE1
) :
90 COMBINER_SOURCE(TEXTURE0
);
93 return COMBINER_SOURCE(TEXTURE0
);
96 return COMBINER_SOURCE(TEXTURE1
);
99 return COMBINER_SOURCE(CONSTANT
);
101 case GL_PRIMARY_COLOR
:
102 return COMBINER_SOURCE(PRIMARY_COLOR
);
105 return rc
->unit
? COMBINER_SOURCE(PREVIOUS
) :
106 COMBINER_SOURCE(PRIMARY_COLOR
);
113 /* Get the (possibly inverted) combiner input mapping for the
114 * specified EXT_texture_env_combine operand. */
118 get_input_mapping(struct combiner_state
*rc
, int operand
, int flags
)
122 if (!is_color_operand(operand
) && !rc
->alpha
)
123 map
|= COMBINER_ALPHA
;
125 if (is_negative_operand(operand
) == !(flags
& INVERT
))
126 map
|= COMBINER_INVERT
;
132 get_input_arg(struct combiner_state
*rc
, int arg
, int flags
)
134 int source
= rc
->source
[arg
];
135 int operand
= rc
->operand
[arg
];
137 /* Fake several unsupported texture formats. */
138 if (is_texture_source(source
)) {
139 int i
= (source
== GL_TEXTURE
?
140 rc
->unit
: source
- GL_TEXTURE0
);
141 struct gl_texture_object
*t
= rc
->ctx
->Texture
.Unit
[i
]._Current
;
142 gl_format format
= t
->Image
[0][t
->BaseLevel
]->TexFormat
;
144 if (format
== MESA_FORMAT_A8
) {
145 /* Emulated using I8. */
146 if (is_color_operand(operand
))
147 return COMBINER_SOURCE(ZERO
) |
148 get_input_mapping(rc
, operand
, flags
);
150 } else if (format
== MESA_FORMAT_L8
) {
151 /* Emulated using I8. */
152 if (!is_color_operand(operand
))
153 return COMBINER_SOURCE(ZERO
) |
154 get_input_mapping(rc
, operand
,
159 return get_input_source(rc
, source
) |
160 get_input_mapping(rc
, operand
, flags
);
163 /* Bind the combiner input <in> to the combiner source <src>,
164 * possibly inverted. */
165 #define INPUT_SRC(rc, in, src, flags) \
166 (rc)->hw |= ((flags & INVERT ? COMBINER_INVERT : 0) | \
167 COMBINER_SOURCE(src)) << COMBINER_SHIFT(in)
169 /* Bind the combiner input <in> to the EXT_texture_env_combine
170 * argument <arg>, possibly inverted. */
171 #define INPUT_ARG(rc, in, arg, flags) \
172 (rc)->hw |= get_input_arg(rc, arg, flags) << COMBINER_SHIFT(in)
174 #define UNSIGNED_OP(rc) \
175 (rc)->hw |= ((rc)->logscale ? \
176 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_SCALE2 : \
177 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_IDENTITY)
178 #define SIGNED_OP(rc) \
179 (rc)->hw |= ((rc)->logscale ? \
180 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS_SCALE2 : \
181 NV04_MULTITEX_TRIANGLE_COMBINE_COLOR_MAP_BIAS)
184 setup_combiner(struct combiner_state
*rc
)
188 INPUT_ARG(rc
, 0, 0, 0);
189 INPUT_SRC(rc
, 1, ZERO
, INVERT
);
190 INPUT_SRC(rc
, 2, ZERO
, 0);
191 INPUT_SRC(rc
, 3, ZERO
, 0);
196 INPUT_ARG(rc
, 0, 0, 0);
197 INPUT_ARG(rc
, 1, 1, 0);
198 INPUT_SRC(rc
, 2, ZERO
, 0);
199 INPUT_SRC(rc
, 3, ZERO
, 0);
205 if (rc
->premodulate
) {
206 INPUT_ARG(rc
, 0, 0, 0);
207 INPUT_ARG(rc
, 1, 1, 0);
208 INPUT_ARG(rc
, 2, 2, 0);
209 INPUT_ARG(rc
, 3, 3, 0);
211 INPUT_ARG(rc
, 0, 0, 0);
212 INPUT_SRC(rc
, 1, ZERO
, INVERT
);
213 INPUT_ARG(rc
, 2, 1, 0);
214 INPUT_SRC(rc
, 3, ZERO
, INVERT
);
217 if (rc
->mode
== GL_ADD_SIGNED
)
225 INPUT_ARG(rc
, 0, 0, 0);
226 INPUT_ARG(rc
, 1, 2, 0);
227 INPUT_ARG(rc
, 2, 1, 0);
228 INPUT_ARG(rc
, 3, 2, INVERT
);
238 nv04_emit_tex_env(struct gl_context
*ctx
, int emit
)
240 const int i
= emit
- NOUVEAU_STATE_TEX_ENV0
;
241 struct nouveau_channel
*chan
= context_chan(ctx
);
242 struct nouveau_grobj
*fahrenheit
= nv04_context_engine(ctx
);
243 struct combiner_state rc_a
= {}, rc_c
= {};
245 if (!nv04_mtex_engine(fahrenheit
)) {
246 context_dirty(ctx
, BLEND
);
250 /* Compute the new combiner state. */
251 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
252 INIT_COMBINER(A
, ctx
, &rc_a
, i
);
253 setup_combiner(&rc_a
);
255 INIT_COMBINER(RGB
, ctx
, &rc_c
, i
);
256 setup_combiner(&rc_c
);
260 INPUT_SRC(&rc_a
, 0, PRIMARY_COLOR
, 0);
261 INPUT_SRC(&rc_c
, 0, PRIMARY_COLOR
, 0);
263 INPUT_SRC(&rc_a
, 0, PREVIOUS
, 0);
264 INPUT_SRC(&rc_c
, 0, PREVIOUS
, 0);
267 INPUT_SRC(&rc_a
, 1, ZERO
, INVERT
);
268 INPUT_SRC(&rc_c
, 1, ZERO
, INVERT
);
269 INPUT_SRC(&rc_a
, 2, ZERO
, 0);
270 INPUT_SRC(&rc_c
, 2, ZERO
, 0);
271 INPUT_SRC(&rc_a
, 3, ZERO
, 0);
272 INPUT_SRC(&rc_c
, 3, ZERO
, 0);
278 /* Write the register combiner state out to the hardware. */
279 BEGIN_RING(chan
, fahrenheit
,
280 NV04_MULTITEX_TRIANGLE_COMBINE_ALPHA(i
), 2);
281 OUT_RING(chan
, rc_a
.hw
);
282 OUT_RING(chan
, rc_c
.hw
);
284 BEGIN_RING(chan
, fahrenheit
,
285 NV04_MULTITEX_TRIANGLE_COMBINE_FACTOR
, 1);
286 OUT_RING(chan
, pack_rgba_f(MESA_FORMAT_ARGB8888
,
287 ctx
->Texture
.Unit
[0].EnvColor
));