Merge remote branch 'origin/master' into lp-binning
[mesa.git] / src / gallium / drivers / r300 / r300_state.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "draw/draw_context.h"
25
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/u_pack_color.h"
29
30 #include "tgsi/tgsi_parse.h"
31
32 #include "pipe/p_config.h"
33
34 #include "r300_context.h"
35 #include "r300_reg.h"
36 #include "r300_screen.h"
37 #include "r300_state_inlines.h"
38 #include "r300_fs.h"
39 #include "r300_vs.h"
40
41 /* r300_state: Functions used to intialize state context by translating
42 * Gallium state objects into semi-native r300 state objects. */
43
44 static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
45 unsigned dstRGB, unsigned dstA)
46 {
47 /* If the blend equation is ADD or REVERSE_SUBTRACT,
48 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
49 * will not be changed.
50 * Notice that the dst factors are the src factors inverted. */
51 return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
52 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
53 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
54 (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
55 srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
56 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
57 srcA == PIPE_BLENDFACTOR_ZERO) &&
58 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
59 dstRGB == PIPE_BLENDFACTOR_ONE) &&
60 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
61 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
62 dstA == PIPE_BLENDFACTOR_ONE);
63 }
64
65 static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
66 unsigned dstRGB, unsigned dstA)
67 {
68 /* If the blend equation is ADD or REVERSE_SUBTRACT,
69 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
70 * will not be changed.
71 * Notice that the dst factors are the src factors inverted. */
72 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
73 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
74 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
75 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
76 srcA == PIPE_BLENDFACTOR_ZERO) &&
77 (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
78 dstRGB == PIPE_BLENDFACTOR_ONE) &&
79 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
80 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
81 dstA == PIPE_BLENDFACTOR_ONE);
82 }
83
84 static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
85 unsigned dstRGB, unsigned dstA)
86 {
87 /* If the blend equation is ADD or REVERSE_SUBTRACT,
88 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
89 * will not be changed.
90 * Notice that the dst factors are the src factors inverted. */
91 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
92 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
93 (srcA == PIPE_BLENDFACTOR_ZERO) &&
94 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
95 dstRGB == PIPE_BLENDFACTOR_ONE) &&
96 (dstA == PIPE_BLENDFACTOR_ONE);
97 }
98
99 static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
100 unsigned dstRGB, unsigned dstA)
101 {
102 /* If the blend equation is ADD or REVERSE_SUBTRACT,
103 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
104 * will not be changed.
105 * Notice that the dst factors are the src factors inverted. */
106 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
107 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
108 (srcA == PIPE_BLENDFACTOR_ZERO) &&
109 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
110 dstRGB == PIPE_BLENDFACTOR_ONE) &&
111 (dstA == PIPE_BLENDFACTOR_ONE);
112 }
113
114 static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
115 unsigned dstRGB, unsigned dstA)
116 {
117 /* If the blend equation is ADD or REVERSE_SUBTRACT,
118 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
119 * the colorbuffer will not be changed.
120 * Notice that the dst factors are the src factors inverted. */
121 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
122 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
123 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
124 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
125 (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
126 srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
127 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
128 srcA == PIPE_BLENDFACTOR_ZERO) &&
129 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
130 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
131 dstRGB == PIPE_BLENDFACTOR_ONE) &&
132 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
133 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
134 dstA == PIPE_BLENDFACTOR_ONE);
135 }
136
137 static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
138 unsigned dstRGB, unsigned dstA)
139 {
140 /* If the blend equation is ADD or REVERSE_SUBTRACT,
141 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
142 * the colorbuffer will not be changed.
143 * Notice that the dst factors are the src factors inverted. */
144 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
145 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
146 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
147 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
148 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
149 srcA == PIPE_BLENDFACTOR_ZERO) &&
150 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
151 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
152 dstRGB == PIPE_BLENDFACTOR_ONE) &&
153 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
154 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
155 dstA == PIPE_BLENDFACTOR_ONE);
156 }
157
158 /* Create a new blend state based on the CSO blend state.
159 *
160 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
161 static void* r300_create_blend_state(struct pipe_context* pipe,
162 const struct pipe_blend_state* state)
163 {
164 struct r300_screen* r300screen = r300_screen(pipe->screen);
165 struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
166
167 if (state->rt[0].blend_enable)
168 {
169 unsigned eqRGB = state->rt[0].rgb_func;
170 unsigned srcRGB = state->rt[0].rgb_src_factor;
171 unsigned dstRGB = state->rt[0].rgb_dst_factor;
172
173 unsigned eqA = state->rt[0].alpha_func;
174 unsigned srcA = state->rt[0].alpha_src_factor;
175 unsigned dstA = state->rt[0].alpha_dst_factor;
176
177 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
178 * this is just the crappy D3D naming */
179 blend->blend_control = R300_ALPHA_BLEND_ENABLE |
180 r300_translate_blend_function(eqRGB) |
181 ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
182 ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
183
184 /* Optimization: some operations do not require the destination color.
185 *
186 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
187 * otherwise blending gives incorrect results. It seems to be
188 * a hardware bug. */
189 if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
190 eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
191 dstRGB != PIPE_BLENDFACTOR_ZERO ||
192 dstA != PIPE_BLENDFACTOR_ZERO ||
193 srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
194 srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
195 srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
196 srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
197 srcA == PIPE_BLENDFACTOR_DST_COLOR ||
198 srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
199 srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
200 srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
201 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
202 /* Enable reading from the colorbuffer. */
203 blend->blend_control |= R300_READ_ENABLE;
204
205 if (r300_screen(r300_context(pipe)->context.screen)->caps->is_r500) {
206 /* Optimization: Depending on incoming pixels, we can
207 * conditionally disable the reading in hardware... */
208 if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
209 eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
210 /* Disable reading if SRC_ALPHA == 0. */
211 if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
212 dstRGB == PIPE_BLENDFACTOR_ZERO) &&
213 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
214 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
215 dstA == PIPE_BLENDFACTOR_ZERO)) {
216 blend->blend_control |= R500_SRC_ALPHA_0_NO_READ;
217 }
218
219 /* Disable reading if SRC_ALPHA == 1. */
220 if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
221 dstRGB == PIPE_BLENDFACTOR_ZERO) &&
222 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
223 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
224 dstA == PIPE_BLENDFACTOR_ZERO)) {
225 blend->blend_control |= R500_SRC_ALPHA_1_NO_READ;
226 }
227 }
228 }
229 }
230
231 /* Optimization: discard pixels which don't change the colorbuffer.
232 *
233 * The code below is non-trivial and some math is involved.
234 *
235 * Discarding pixels must be disabled when FP16 AA is enabled.
236 * This is a hardware bug. Also, this implementation wouldn't work
237 * with FP blending enabled and equation clamping disabled.
238 *
239 * Equations other than ADD are rarely used and therefore won't be
240 * optimized. */
241 if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
242 (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
243 /* ADD: X+Y
244 * REVERSE_SUBTRACT: Y-X
245 *
246 * The idea is:
247 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
248 * then CB will not be changed.
249 *
250 * Given the srcFactor and dstFactor variables, we can derive
251 * what src and dst should be equal to and discard appropriate
252 * pixels.
253 */
254 if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
255 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
256 } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
257 dstRGB, dstA)) {
258 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
259 } else if (blend_discard_if_src_color_0(srcRGB, srcA,
260 dstRGB, dstA)) {
261 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
262 } else if (blend_discard_if_src_color_1(srcRGB, srcA,
263 dstRGB, dstA)) {
264 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
265 } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
266 dstRGB, dstA)) {
267 blend->blend_control |=
268 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
269 } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
270 dstRGB, dstA)) {
271 blend->blend_control |=
272 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
273 }
274 }
275
276 /* separate alpha */
277 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
278 blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
279 blend->alpha_blend_control =
280 r300_translate_blend_function(eqA) |
281 (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
282 (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
283 }
284 }
285
286 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
287 if (state->logicop_enable) {
288 blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
289 (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
290 }
291
292 /* Color channel masks for all MRTs. */
293 blend->color_channel_mask = state->rt[0].colormask;
294 if (r300screen->caps->is_r500 && state->independent_blend_enable) {
295 if (state->rt[1].blend_enable) {
296 blend->color_channel_mask |= (state->rt[1].colormask << 4);
297 }
298 if (state->rt[2].blend_enable) {
299 blend->color_channel_mask |= (state->rt[2].colormask << 8);
300 }
301 if (state->rt[3].blend_enable) {
302 blend->color_channel_mask |= (state->rt[3].colormask << 12);
303 }
304 }
305
306 if (state->dither) {
307 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
308 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
309 }
310
311 return (void*)blend;
312 }
313
314 /* Bind blend state. */
315 static void r300_bind_blend_state(struct pipe_context* pipe,
316 void* state)
317 {
318 struct r300_context* r300 = r300_context(pipe);
319
320 r300->blend_state.state = state;
321 r300->blend_state.dirty = TRUE;
322 }
323
324 /* Free blend state. */
325 static void r300_delete_blend_state(struct pipe_context* pipe,
326 void* state)
327 {
328 FREE(state);
329 }
330
331 /* Convert float to 10bit integer */
332 static unsigned float_to_fixed10(float f)
333 {
334 return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
335 }
336
337 /* Set blend color.
338 * Setup both R300 and R500 registers, figure out later which one to write. */
339 static void r300_set_blend_color(struct pipe_context* pipe,
340 const struct pipe_blend_color* color)
341 {
342 struct r300_context* r300 = r300_context(pipe);
343 struct r300_screen* r300screen = r300_screen(pipe->screen);
344 struct r300_blend_color_state* state =
345 (struct r300_blend_color_state*)r300->blend_color_state.state;
346 union util_color uc;
347
348 util_pack_color(color->color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
349 state->blend_color = uc.ui;
350
351 /* XXX if FP16 blending is enabled, we should use the FP16 format */
352 state->blend_color_red_alpha =
353 float_to_fixed10(color->color[0]) |
354 (float_to_fixed10(color->color[3]) << 16);
355 state->blend_color_green_blue =
356 float_to_fixed10(color->color[2]) |
357 (float_to_fixed10(color->color[1]) << 16);
358
359 r300->blend_color_state.size = r300screen->caps->is_r500 ? 3 : 2;
360 r300->blend_color_state.dirty = TRUE;
361 }
362
363 static void r300_set_clip_state(struct pipe_context* pipe,
364 const struct pipe_clip_state* state)
365 {
366 struct r300_context* r300 = r300_context(pipe);
367
368 if (r300_screen(pipe->screen)->caps->has_tcl) {
369 memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state));
370 r300->clip_state.size = 29;
371 } else {
372 draw_flush(r300->draw);
373 draw_set_clip_state(r300->draw, state);
374 r300->clip_state.size = 2;
375 }
376
377 r300->clip_state.dirty = TRUE;
378 }
379
380 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
381 *
382 * This contains the depth buffer, stencil buffer, alpha test, and such.
383 * On the Radeon, depth and stencil buffer setup are intertwined, which is
384 * the reason for some of the strange-looking assignments across registers. */
385 static void*
386 r300_create_dsa_state(struct pipe_context* pipe,
387 const struct pipe_depth_stencil_alpha_state* state)
388 {
389 struct r300_capabilities *caps =
390 r300_screen(r300_context(pipe)->context.screen)->caps;
391 struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
392
393 /* Depth test setup. */
394 if (state->depth.enabled) {
395 dsa->z_buffer_control |= R300_Z_ENABLE;
396
397 if (state->depth.writemask) {
398 dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
399 }
400
401 dsa->z_stencil_control |=
402 (r300_translate_depth_stencil_function(state->depth.func) <<
403 R300_Z_FUNC_SHIFT);
404 }
405
406 /* Stencil buffer setup. */
407 if (state->stencil[0].enabled) {
408 dsa->z_buffer_control |= R300_STENCIL_ENABLE;
409 dsa->z_stencil_control |=
410 (r300_translate_depth_stencil_function(state->stencil[0].func) <<
411 R300_S_FRONT_FUNC_SHIFT) |
412 (r300_translate_stencil_op(state->stencil[0].fail_op) <<
413 R300_S_FRONT_SFAIL_OP_SHIFT) |
414 (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
415 R300_S_FRONT_ZPASS_OP_SHIFT) |
416 (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
417 R300_S_FRONT_ZFAIL_OP_SHIFT);
418
419 dsa->stencil_ref_mask = (state->stencil[0].ref_value) |
420 (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
421 (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
422
423 if (state->stencil[1].enabled) {
424 dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
425 dsa->z_stencil_control |=
426 (r300_translate_depth_stencil_function(state->stencil[1].func) <<
427 R300_S_BACK_FUNC_SHIFT) |
428 (r300_translate_stencil_op(state->stencil[1].fail_op) <<
429 R300_S_BACK_SFAIL_OP_SHIFT) |
430 (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
431 R300_S_BACK_ZPASS_OP_SHIFT) |
432 (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
433 R300_S_BACK_ZFAIL_OP_SHIFT);
434
435 /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
436 if (caps->is_r500)
437 {
438 dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
439 dsa->stencil_ref_bf = (state->stencil[1].ref_value) |
440 (state->stencil[1].valuemask <<
441 R300_STENCILMASK_SHIFT) |
442 (state->stencil[1].writemask <<
443 R300_STENCILWRITEMASK_SHIFT);
444 }
445 }
446 }
447
448 /* Alpha test setup. */
449 if (state->alpha.enabled) {
450 dsa->alpha_function =
451 r300_translate_alpha_function(state->alpha.func) |
452 R300_FG_ALPHA_FUNC_ENABLE;
453
454 /* XXX figure out why emitting 10bit alpha ref causes CS to dump */
455 /* always use 8bit alpha ref */
456 dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
457
458 if (caps->is_r500)
459 dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
460 }
461
462 return (void*)dsa;
463 }
464
465 /* Bind DSA state. */
466 static void r300_bind_dsa_state(struct pipe_context* pipe,
467 void* state)
468 {
469 struct r300_context* r300 = r300_context(pipe);
470 struct r300_screen* r300screen = r300_screen(pipe->screen);
471
472 r300->dsa_state.state = state;
473 r300->dsa_state.size = r300screen->caps->is_r500 ? 8 : 6;
474 r300->dsa_state.dirty = TRUE;
475 }
476
477 /* Free DSA state. */
478 static void r300_delete_dsa_state(struct pipe_context* pipe,
479 void* state)
480 {
481 FREE(state);
482 }
483
484 static void
485 r300_set_framebuffer_state(struct pipe_context* pipe,
486 const struct pipe_framebuffer_state* state)
487 {
488 struct r300_context* r300 = r300_context(pipe);
489 uint32_t zbuffer_bpp = 0;
490
491 r300->fb_state.size = (10 * state->nr_cbufs) +
492 (2 * (4 - state->nr_cbufs)) +
493 (state->zsbuf ? 10 : 0) + 6;
494
495 if (state->nr_cbufs > 4) {
496 debug_printf("r300: Implementation error: Too many MRTs in %s, "
497 "refusing to bind framebuffer state!\n", __FUNCTION__);
498 return;
499 }
500
501 if (r300->draw) {
502 draw_flush(r300->draw);
503 }
504
505 r300->fb_state.state = state;
506
507 /* Don't rely on the order of states being set for the first time. */
508 /* XXX wait what */
509 r300->blend_state.dirty = TRUE;
510 r300->dsa_state.dirty = TRUE;
511 r300->fb_state.dirty = TRUE;
512 r300->scissor_state.dirty = TRUE;
513
514 /* Polygon offset depends on the zbuffer bit depth. */
515 if (state->zsbuf && r300->polygon_offset_enabled) {
516 switch (util_format_get_blocksize(state->zsbuf->texture->format)) {
517 case 2:
518 zbuffer_bpp = 16;
519 break;
520 case 4:
521 zbuffer_bpp = 24;
522 break;
523 }
524
525 if (r300->zbuffer_bpp != zbuffer_bpp) {
526 r300->zbuffer_bpp = zbuffer_bpp;
527 r300->rs_state.dirty = TRUE;
528 }
529 }
530 }
531
532 /* Create fragment shader state. */
533 static void* r300_create_fs_state(struct pipe_context* pipe,
534 const struct pipe_shader_state* shader)
535 {
536 struct r300_fragment_shader* fs = NULL;
537
538 fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
539
540 /* Copy state directly into shader. */
541 fs->state = *shader;
542 fs->state.tokens = tgsi_dup_tokens(shader->tokens);
543
544 tgsi_scan_shader(shader->tokens, &fs->info);
545 r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
546
547 return (void*)fs;
548 }
549
550 /* Bind fragment shader state. */
551 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
552 {
553 struct r300_context* r300 = r300_context(pipe);
554 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
555
556 if (fs == NULL) {
557 r300->fs = NULL;
558 return;
559 }
560
561 r300->fs = fs;
562 r300_pick_fragment_shader(r300);
563
564 if (r300->vs && r300_vertex_shader_setup_wpos(r300)) {
565 r300->vertex_format_state.dirty = TRUE;
566 }
567
568 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS;
569 }
570
571 /* Delete fragment shader state. */
572 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
573 {
574 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
575 struct r300_fragment_shader_code *tmp, *ptr = fs->first;
576
577 while (ptr) {
578 tmp = ptr;
579 ptr = ptr->next;
580 rc_constants_destroy(&tmp->code.constants);
581 FREE(tmp);
582 }
583 FREE((void*)fs->state.tokens);
584 FREE(shader);
585 }
586
587 static void r300_set_polygon_stipple(struct pipe_context* pipe,
588 const struct pipe_poly_stipple* state)
589 {
590 /* XXX no idea how to set this up, but not terribly important */
591 }
592
593 /* Create a new rasterizer state based on the CSO rasterizer state.
594 *
595 * This is a very large chunk of state, and covers most of the graphics
596 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
597 *
598 * In a not entirely unironic sidenote, this state has nearly nothing to do
599 * with the actual block on the Radeon called the rasterizer (RS). */
600 static void* r300_create_rs_state(struct pipe_context* pipe,
601 const struct pipe_rasterizer_state* state)
602 {
603 struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
604
605 /* Copy rasterizer state for Draw. */
606 rs->rs = *state;
607
608 #ifdef PIPE_ARCH_LITTLE_ENDIAN
609 rs->vap_control_status = R300_VC_NO_SWAP;
610 #else
611 rs->vap_control_status = R300_VC_32BIT_SWAP;
612 #endif
613
614 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
615 * Else, enable HW TCL and force Draw's TCL off. */
616 if (state->bypass_vs_clip_and_viewport ||
617 !r300_screen(pipe->screen)->caps->has_tcl) {
618 rs->vap_control_status |= R300_VAP_TCL_BYPASS;
619 }
620
621 rs->point_size = pack_float_16_6x(state->point_size) |
622 (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
623
624 rs->point_minmax =
625 ((int)(state->point_size_min * 6.0) <<
626 R300_GA_POINT_MINMAX_MIN_SHIFT) |
627 ((int)(state->point_size_max * 6.0) <<
628 R300_GA_POINT_MINMAX_MAX_SHIFT);
629
630 rs->line_control = pack_float_16_6x(state->line_width) |
631 R300_GA_LINE_CNTL_END_TYPE_COMP;
632
633 /* Enable polygon mode */
634 if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
635 state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
636 rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
637 }
638
639 /* Radeons don't think in "CW/CCW", they think in "front/back". */
640 if (state->front_winding == PIPE_WINDING_CW) {
641 rs->cull_mode = R300_FRONT_FACE_CW;
642
643 /* Polygon offset */
644 if (state->offset_cw) {
645 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
646 }
647 if (state->offset_ccw) {
648 rs->polygon_offset_enable |= R300_BACK_ENABLE;
649 }
650
651 /* Polygon mode */
652 if (rs->polygon_mode) {
653 rs->polygon_mode |=
654 r300_translate_polygon_mode_front(state->fill_cw);
655 rs->polygon_mode |=
656 r300_translate_polygon_mode_back(state->fill_ccw);
657 }
658 } else {
659 rs->cull_mode = R300_FRONT_FACE_CCW;
660
661 /* Polygon offset */
662 if (state->offset_ccw) {
663 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
664 }
665 if (state->offset_cw) {
666 rs->polygon_offset_enable |= R300_BACK_ENABLE;
667 }
668
669 /* Polygon mode */
670 if (rs->polygon_mode) {
671 rs->polygon_mode |=
672 r300_translate_polygon_mode_front(state->fill_ccw);
673 rs->polygon_mode |=
674 r300_translate_polygon_mode_back(state->fill_cw);
675 }
676 }
677 if (state->front_winding & state->cull_mode) {
678 rs->cull_mode |= R300_CULL_FRONT;
679 }
680 if (~(state->front_winding) & state->cull_mode) {
681 rs->cull_mode |= R300_CULL_BACK;
682 }
683
684 if (rs->polygon_offset_enable) {
685 rs->depth_offset = state->offset_units;
686 rs->depth_scale = state->offset_scale;
687 }
688
689 if (state->line_stipple_enable) {
690 rs->line_stipple_config =
691 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
692 (fui((float)state->line_stipple_factor) &
693 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
694 /* XXX this might need to be scaled up */
695 rs->line_stipple_value = state->line_stipple_pattern;
696 }
697
698 if (state->flatshade) {
699 rs->color_control = R300_SHADE_MODEL_FLAT;
700 } else {
701 rs->color_control = R300_SHADE_MODEL_SMOOTH;
702 }
703
704 return (void*)rs;
705 }
706
707 /* Bind rasterizer state. */
708 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
709 {
710 struct r300_context* r300 = r300_context(pipe);
711 struct r300_rs_state* rs = (struct r300_rs_state*)state;
712
713 if (r300->draw) {
714 draw_flush(r300->draw);
715 draw_set_rasterizer_state(r300->draw, &rs->rs);
716 }
717
718 if (rs) {
719 r300->tcl_bypass = rs->rs.bypass_vs_clip_and_viewport;
720 r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw;
721 } else {
722 r300->tcl_bypass = FALSE;
723 r300->polygon_offset_enabled = FALSE;
724 }
725
726 r300->rs_state.state = rs;
727 r300->rs_state.dirty = TRUE;
728 /* XXX Why is this still needed, dammit!? */
729 r300->scissor_state.dirty = TRUE;
730 r300->viewport_state.dirty = TRUE;
731
732 /* XXX Clean these up when we move to atom emits */
733 if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
734 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
735 }
736 }
737
738 /* Free rasterizer state. */
739 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
740 {
741 FREE(state);
742 }
743
744 static void*
745 r300_create_sampler_state(struct pipe_context* pipe,
746 const struct pipe_sampler_state* state)
747 {
748 struct r300_context* r300 = r300_context(pipe);
749 struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
750 int lod_bias;
751 union util_color uc;
752
753 sampler->state = *state;
754
755 sampler->filter0 |=
756 (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
757 (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
758 (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
759
760 sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
761 state->mag_img_filter,
762 state->min_mip_filter,
763 state->max_anisotropy > 1.0);
764
765 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
766 /* We must pass these to the emit function to clamp them properly. */
767 sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
768 sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
769
770 lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
771
772 sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
773
774 sampler->filter1 |= r300_anisotropy(state->max_anisotropy);
775
776 util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
777 sampler->border_color = uc.ui;
778
779 /* R500-specific fixups and optimizations */
780 if (r300_screen(r300->context.screen)->caps->is_r500) {
781 sampler->filter1 |= R500_BORDER_FIX;
782 }
783
784 return (void*)sampler;
785 }
786
787 static void r300_bind_sampler_states(struct pipe_context* pipe,
788 unsigned count,
789 void** states)
790 {
791 struct r300_context* r300 = r300_context(pipe);
792 int i;
793
794 if (count > 8) {
795 return;
796 }
797
798 for (i = 0; i < count; i++) {
799 if (r300->sampler_states[i] != states[i]) {
800 r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
801 r300->dirty_state |= (R300_NEW_SAMPLER << i);
802 }
803 }
804
805 r300->sampler_count = count;
806
807 /* Pick a fragment shader based on the texture compare state. */
808 if (r300->fs && (r300->dirty_state & R300_ANY_NEW_SAMPLERS)) {
809 if (r300_pick_fragment_shader(r300)) {
810 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER |
811 R300_NEW_FRAGMENT_SHADER_CONSTANTS;
812 }
813 }
814 }
815
816 static void r300_lacks_vertex_textures(struct pipe_context* pipe,
817 unsigned count,
818 void** states)
819 {
820 }
821
822 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
823 {
824 FREE(state);
825 }
826
827 static void r300_set_sampler_textures(struct pipe_context* pipe,
828 unsigned count,
829 struct pipe_texture** texture)
830 {
831 struct r300_context* r300 = r300_context(pipe);
832 boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
833 int i;
834
835 /* XXX magic num */
836 if (count > 8) {
837 return;
838 }
839
840 for (i = 0; i < count; i++) {
841 if (r300->textures[i] != (struct r300_texture*)texture[i]) {
842 pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
843 texture[i]);
844 r300->dirty_state |= (R300_NEW_TEXTURE << i);
845
846 /* R300-specific - set the texrect factor in a fragment shader */
847 if (!is_r500 && r300->textures[i]->is_npot) {
848 /* XXX It would be nice to re-emit just 1 constant,
849 * XXX not all of them */
850 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
851 }
852 }
853 }
854
855 for (i = count; i < 8; i++) {
856 if (r300->textures[i]) {
857 pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
858 NULL);
859 r300->dirty_state |= (R300_NEW_TEXTURE << i);
860 }
861 }
862
863 r300->texture_count = count;
864 }
865
866 static void r300_set_scissor_state(struct pipe_context* pipe,
867 const struct pipe_scissor_state* state)
868 {
869 struct r300_context* r300 = r300_context(pipe);
870
871 memcpy(r300->scissor_state.state, state,
872 sizeof(struct pipe_scissor_state));
873
874 r300->scissor_state.dirty = TRUE;
875 }
876
877 static void r300_set_viewport_state(struct pipe_context* pipe,
878 const struct pipe_viewport_state* state)
879 {
880 struct r300_context* r300 = r300_context(pipe);
881 struct r300_viewport_state* viewport =
882 (struct r300_viewport_state*)r300->viewport_state.state;
883
884 /* Do the transform in HW. */
885 viewport->vte_control = R300_VTX_W0_FMT;
886
887 if (state->scale[0] != 1.0f) {
888 viewport->xscale = state->scale[0];
889 viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
890 }
891 if (state->scale[1] != 1.0f) {
892 viewport->yscale = state->scale[1];
893 viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
894 }
895 if (state->scale[2] != 1.0f) {
896 viewport->zscale = state->scale[2];
897 viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
898 }
899 if (state->translate[0] != 0.0f) {
900 viewport->xoffset = state->translate[0];
901 viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
902 }
903 if (state->translate[1] != 0.0f) {
904 viewport->yoffset = state->translate[1];
905 viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
906 }
907 if (state->translate[2] != 0.0f) {
908 viewport->zoffset = state->translate[2];
909 viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
910 }
911
912 r300->viewport_state.dirty = TRUE;
913 if (r300->fs && r300->fs->inputs.wpos != ATTR_UNUSED) {
914 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
915 }
916 }
917
918 static void r300_set_vertex_buffers(struct pipe_context* pipe,
919 unsigned count,
920 const struct pipe_vertex_buffer* buffers)
921 {
922 struct r300_context* r300 = r300_context(pipe);
923
924 memcpy(r300->vertex_buffer, buffers,
925 sizeof(struct pipe_vertex_buffer) * count);
926 r300->vertex_buffer_count = count;
927
928 if (r300->draw) {
929 draw_flush(r300->draw);
930 draw_set_vertex_buffers(r300->draw, count, buffers);
931 }
932
933 r300->vertex_format_state.dirty = TRUE;
934 }
935
936 static boolean r300_validate_aos(struct r300_context *r300)
937 {
938 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
939 struct pipe_vertex_element *velem = r300->vertex_element;
940 int i;
941
942 /* Check if formats and strides are aligned to the size of DWORD. */
943 for (i = 0; i < r300->vertex_element_count; i++) {
944 if (vbuf[velem[i].vertex_buffer_index].stride % 4 != 0 ||
945 util_format_get_blocksize(velem[i].src_format) % 4 != 0) {
946 return FALSE;
947 }
948 }
949 return TRUE;
950 }
951
952 static void r300_set_vertex_elements(struct pipe_context* pipe,
953 unsigned count,
954 const struct pipe_vertex_element* elements)
955 {
956 struct r300_context* r300 = r300_context(pipe);
957
958 memcpy(r300->vertex_element,
959 elements,
960 sizeof(struct pipe_vertex_element) * count);
961 r300->vertex_element_count = count;
962
963 if (r300->draw) {
964 draw_flush(r300->draw);
965 draw_set_vertex_elements(r300->draw, count, elements);
966 }
967
968 if (!r300_validate_aos(r300)) {
969 /* XXX We should fallback using draw. */
970 assert(0);
971 abort();
972 }
973 }
974
975 static void* r300_create_vs_state(struct pipe_context* pipe,
976 const struct pipe_shader_state* shader)
977 {
978 struct r300_context* r300 = r300_context(pipe);
979
980 if (r300_screen(pipe->screen)->caps->has_tcl) {
981 struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
982 /* Copy state directly into shader. */
983 vs->state = *shader;
984 vs->state.tokens = tgsi_dup_tokens(shader->tokens);
985
986 tgsi_scan_shader(shader->tokens, &vs->info);
987
988 return (void*)vs;
989 } else {
990 return draw_create_vertex_shader(r300->draw, shader);
991 }
992 }
993
994 static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
995 {
996 struct r300_context* r300 = r300_context(pipe);
997
998 if (r300_screen(pipe->screen)->caps->has_tcl) {
999 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1000
1001 if (vs == NULL) {
1002 r300->vs = NULL;
1003 return;
1004 } else if (!vs->translated) {
1005 r300_translate_vertex_shader(r300, vs);
1006 }
1007
1008 r300->vs = vs;
1009 if (r300->fs) {
1010 r300_vertex_shader_setup_wpos(r300);
1011 }
1012
1013 r300->vertex_format_state.dirty = TRUE;
1014
1015 r300->dirty_state |=
1016 R300_NEW_VERTEX_SHADER | R300_NEW_VERTEX_SHADER_CONSTANTS;
1017 } else {
1018 draw_flush(r300->draw);
1019 draw_bind_vertex_shader(r300->draw,
1020 (struct draw_vertex_shader*)shader);
1021 }
1022 }
1023
1024 static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
1025 {
1026 struct r300_context* r300 = r300_context(pipe);
1027
1028 if (r300_screen(pipe->screen)->caps->has_tcl) {
1029 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1030
1031 rc_constants_destroy(&vs->code.constants);
1032 FREE((void*)vs->state.tokens);
1033 FREE(shader);
1034 } else {
1035 draw_delete_vertex_shader(r300->draw,
1036 (struct draw_vertex_shader*)shader);
1037 }
1038 }
1039
1040 static void r300_set_constant_buffer(struct pipe_context *pipe,
1041 uint shader, uint index,
1042 struct pipe_buffer *buf)
1043 {
1044 struct r300_context* r300 = r300_context(pipe);
1045 void *mapped;
1046
1047 if (buf == NULL || buf->size == 0 ||
1048 (mapped = pipe_buffer_map(pipe->screen, buf, PIPE_BUFFER_USAGE_CPU_READ)) == NULL)
1049 {
1050 r300->shader_constants[shader].count = 0;
1051 return;
1052 }
1053
1054 assert((buf->size % 4 * sizeof(float)) == 0);
1055 memcpy(r300->shader_constants[shader].constants, mapped, buf->size);
1056 r300->shader_constants[shader].count = buf->size / (4 * sizeof(float));
1057 pipe_buffer_unmap(pipe->screen, buf);
1058
1059 if (shader == PIPE_SHADER_VERTEX)
1060 r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
1061 else if (shader == PIPE_SHADER_FRAGMENT)
1062 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
1063 }
1064
1065 void r300_init_state_functions(struct r300_context* r300)
1066 {
1067 r300->context.create_blend_state = r300_create_blend_state;
1068 r300->context.bind_blend_state = r300_bind_blend_state;
1069 r300->context.delete_blend_state = r300_delete_blend_state;
1070
1071 r300->context.set_blend_color = r300_set_blend_color;
1072
1073 r300->context.set_clip_state = r300_set_clip_state;
1074
1075 r300->context.set_constant_buffer = r300_set_constant_buffer;
1076
1077 r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
1078 r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
1079 r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
1080
1081 r300->context.set_framebuffer_state = r300_set_framebuffer_state;
1082
1083 r300->context.create_fs_state = r300_create_fs_state;
1084 r300->context.bind_fs_state = r300_bind_fs_state;
1085 r300->context.delete_fs_state = r300_delete_fs_state;
1086
1087 r300->context.set_polygon_stipple = r300_set_polygon_stipple;
1088
1089 r300->context.create_rasterizer_state = r300_create_rs_state;
1090 r300->context.bind_rasterizer_state = r300_bind_rs_state;
1091 r300->context.delete_rasterizer_state = r300_delete_rs_state;
1092
1093 r300->context.create_sampler_state = r300_create_sampler_state;
1094 r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
1095 r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
1096 r300->context.delete_sampler_state = r300_delete_sampler_state;
1097
1098 r300->context.set_fragment_sampler_textures = r300_set_sampler_textures;
1099
1100 r300->context.set_scissor_state = r300_set_scissor_state;
1101
1102 r300->context.set_viewport_state = r300_set_viewport_state;
1103
1104 r300->context.set_vertex_buffers = r300_set_vertex_buffers;
1105 r300->context.set_vertex_elements = r300_set_vertex_elements;
1106
1107 r300->context.create_vs_state = r300_create_vs_state;
1108 r300->context.bind_vs_state = r300_bind_vs_state;
1109 r300->context.delete_vs_state = r300_delete_vs_state;
1110 }