ac4e87abe09419888977f2cb0830fbdcea32092a
[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_emit.h"
36 #include "r300_reg.h"
37 #include "r300_screen.h"
38 #include "r300_screen_buffer.h"
39 #include "r300_state.h"
40 #include "r300_state_inlines.h"
41 #include "r300_fs.h"
42 #include "r300_texture.h"
43 #include "r300_vs.h"
44 #include "r300_winsys.h"
45
46 /* r300_state: Functions used to intialize state context by translating
47 * Gallium state objects into semi-native r300 state objects. */
48
49 #define UPDATE_STATE(cso, atom) \
50 if (cso != atom.state) { \
51 atom.state = cso; \
52 atom.dirty = TRUE; \
53 }
54
55 static boolean blend_discard_if_src_alpha_0(unsigned srcRGB, unsigned srcA,
56 unsigned dstRGB, unsigned dstA)
57 {
58 /* If the blend equation is ADD or REVERSE_SUBTRACT,
59 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
60 * will not be changed.
61 * Notice that the dst factors are the src factors inverted. */
62 return (srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
63 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
64 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
65 (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
66 srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
67 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
68 srcA == PIPE_BLENDFACTOR_ZERO) &&
69 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
70 dstRGB == PIPE_BLENDFACTOR_ONE) &&
71 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
72 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
73 dstA == PIPE_BLENDFACTOR_ONE);
74 }
75
76 static boolean blend_discard_if_src_alpha_1(unsigned srcRGB, unsigned srcA,
77 unsigned dstRGB, unsigned dstA)
78 {
79 /* If the blend equation is ADD or REVERSE_SUBTRACT,
80 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
81 * will not be changed.
82 * Notice that the dst factors are the src factors inverted. */
83 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
84 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
85 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
86 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
87 srcA == PIPE_BLENDFACTOR_ZERO) &&
88 (dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
89 dstRGB == PIPE_BLENDFACTOR_ONE) &&
90 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
91 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
92 dstA == PIPE_BLENDFACTOR_ONE);
93 }
94
95 static boolean blend_discard_if_src_color_0(unsigned srcRGB, unsigned srcA,
96 unsigned dstRGB, unsigned dstA)
97 {
98 /* If the blend equation is ADD or REVERSE_SUBTRACT,
99 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
100 * will not be changed.
101 * Notice that the dst factors are the src factors inverted. */
102 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
103 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
104 (srcA == PIPE_BLENDFACTOR_ZERO) &&
105 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
106 dstRGB == PIPE_BLENDFACTOR_ONE) &&
107 (dstA == PIPE_BLENDFACTOR_ONE);
108 }
109
110 static boolean blend_discard_if_src_color_1(unsigned srcRGB, unsigned srcA,
111 unsigned dstRGB, unsigned dstA)
112 {
113 /* If the blend equation is ADD or REVERSE_SUBTRACT,
114 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
115 * will not be changed.
116 * Notice that the dst factors are the src factors inverted. */
117 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
118 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
119 (srcA == PIPE_BLENDFACTOR_ZERO) &&
120 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
121 dstRGB == PIPE_BLENDFACTOR_ONE) &&
122 (dstA == PIPE_BLENDFACTOR_ONE);
123 }
124
125 static boolean blend_discard_if_src_alpha_color_0(unsigned srcRGB, unsigned srcA,
126 unsigned dstRGB, unsigned dstA)
127 {
128 /* If the blend equation is ADD or REVERSE_SUBTRACT,
129 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
130 * the colorbuffer will not be changed.
131 * Notice that the dst factors are the src factors inverted. */
132 return (srcRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
133 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
134 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
135 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
136 (srcA == PIPE_BLENDFACTOR_SRC_COLOR ||
137 srcA == PIPE_BLENDFACTOR_SRC_ALPHA ||
138 srcA == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE ||
139 srcA == PIPE_BLENDFACTOR_ZERO) &&
140 (dstRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
141 dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
142 dstRGB == PIPE_BLENDFACTOR_ONE) &&
143 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
144 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
145 dstA == PIPE_BLENDFACTOR_ONE);
146 }
147
148 static boolean blend_discard_if_src_alpha_color_1(unsigned srcRGB, unsigned srcA,
149 unsigned dstRGB, unsigned dstA)
150 {
151 /* If the blend equation is ADD or REVERSE_SUBTRACT,
152 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
153 * the colorbuffer will not be changed.
154 * Notice that the dst factors are the src factors inverted. */
155 return (srcRGB == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
156 srcRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
157 srcRGB == PIPE_BLENDFACTOR_ZERO) &&
158 (srcA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
159 srcA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
160 srcA == PIPE_BLENDFACTOR_ZERO) &&
161 (dstRGB == PIPE_BLENDFACTOR_SRC_COLOR ||
162 dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
163 dstRGB == PIPE_BLENDFACTOR_ONE) &&
164 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
165 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
166 dstA == PIPE_BLENDFACTOR_ONE);
167 }
168
169 static unsigned bgra_cmask(unsigned mask)
170 {
171 /* Gallium uses RGBA color ordering while R300 expects BGRA. */
172
173 return ((mask & PIPE_MASK_R) << 2) |
174 ((mask & PIPE_MASK_B) >> 2) |
175 (mask & (PIPE_MASK_G | PIPE_MASK_A));
176 }
177
178 /* Create a new blend state based on the CSO blend state.
179 *
180 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
181 static void* r300_create_blend_state(struct pipe_context* pipe,
182 const struct pipe_blend_state* state)
183 {
184 struct r300_screen* r300screen = r300_screen(pipe->screen);
185 struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
186
187 if (state->rt[0].blend_enable)
188 {
189 unsigned eqRGB = state->rt[0].rgb_func;
190 unsigned srcRGB = state->rt[0].rgb_src_factor;
191 unsigned dstRGB = state->rt[0].rgb_dst_factor;
192
193 unsigned eqA = state->rt[0].alpha_func;
194 unsigned srcA = state->rt[0].alpha_src_factor;
195 unsigned dstA = state->rt[0].alpha_dst_factor;
196
197 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
198 * this is just the crappy D3D naming */
199 blend->blend_control = R300_ALPHA_BLEND_ENABLE |
200 r300_translate_blend_function(eqRGB) |
201 ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
202 ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
203
204 /* Optimization: some operations do not require the destination color.
205 *
206 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
207 * otherwise blending gives incorrect results. It seems to be
208 * a hardware bug. */
209 if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
210 eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
211 dstRGB != PIPE_BLENDFACTOR_ZERO ||
212 dstA != PIPE_BLENDFACTOR_ZERO ||
213 srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
214 srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
215 srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
216 srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
217 srcA == PIPE_BLENDFACTOR_DST_COLOR ||
218 srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
219 srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
220 srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
221 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE) {
222 /* Enable reading from the colorbuffer. */
223 blend->blend_control |= R300_READ_ENABLE;
224
225 if (r300screen->caps.is_r500) {
226 /* Optimization: Depending on incoming pixels, we can
227 * conditionally disable the reading in hardware... */
228 if (eqRGB != PIPE_BLEND_MIN && eqA != PIPE_BLEND_MIN &&
229 eqRGB != PIPE_BLEND_MAX && eqA != PIPE_BLEND_MAX) {
230 /* Disable reading if SRC_ALPHA == 0. */
231 if ((dstRGB == PIPE_BLENDFACTOR_SRC_ALPHA ||
232 dstRGB == PIPE_BLENDFACTOR_ZERO) &&
233 (dstA == PIPE_BLENDFACTOR_SRC_COLOR ||
234 dstA == PIPE_BLENDFACTOR_SRC_ALPHA ||
235 dstA == PIPE_BLENDFACTOR_ZERO)) {
236 blend->blend_control |= R500_SRC_ALPHA_0_NO_READ;
237 }
238
239 /* Disable reading if SRC_ALPHA == 1. */
240 if ((dstRGB == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
241 dstRGB == PIPE_BLENDFACTOR_ZERO) &&
242 (dstA == PIPE_BLENDFACTOR_INV_SRC_COLOR ||
243 dstA == PIPE_BLENDFACTOR_INV_SRC_ALPHA ||
244 dstA == PIPE_BLENDFACTOR_ZERO)) {
245 blend->blend_control |= R500_SRC_ALPHA_1_NO_READ;
246 }
247 }
248 }
249 }
250
251 /* Optimization: discard pixels which don't change the colorbuffer.
252 *
253 * The code below is non-trivial and some math is involved.
254 *
255 * Discarding pixels must be disabled when FP16 AA is enabled.
256 * This is a hardware bug. Also, this implementation wouldn't work
257 * with FP blending enabled and equation clamping disabled.
258 *
259 * Equations other than ADD are rarely used and therefore won't be
260 * optimized. */
261 if ((eqRGB == PIPE_BLEND_ADD || eqRGB == PIPE_BLEND_REVERSE_SUBTRACT) &&
262 (eqA == PIPE_BLEND_ADD || eqA == PIPE_BLEND_REVERSE_SUBTRACT)) {
263 /* ADD: X+Y
264 * REVERSE_SUBTRACT: Y-X
265 *
266 * The idea is:
267 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
268 * then CB will not be changed.
269 *
270 * Given the srcFactor and dstFactor variables, we can derive
271 * what src and dst should be equal to and discard appropriate
272 * pixels.
273 */
274 if (blend_discard_if_src_alpha_0(srcRGB, srcA, dstRGB, dstA)) {
275 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
276 } else if (blend_discard_if_src_alpha_1(srcRGB, srcA,
277 dstRGB, dstA)) {
278 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1;
279 } else if (blend_discard_if_src_color_0(srcRGB, srcA,
280 dstRGB, dstA)) {
281 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0;
282 } else if (blend_discard_if_src_color_1(srcRGB, srcA,
283 dstRGB, dstA)) {
284 blend->blend_control |= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1;
285 } else if (blend_discard_if_src_alpha_color_0(srcRGB, srcA,
286 dstRGB, dstA)) {
287 blend->blend_control |=
288 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0;
289 } else if (blend_discard_if_src_alpha_color_1(srcRGB, srcA,
290 dstRGB, dstA)) {
291 blend->blend_control |=
292 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1;
293 }
294 }
295
296 /* separate alpha */
297 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
298 blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
299 blend->alpha_blend_control =
300 r300_translate_blend_function(eqA) |
301 (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
302 (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
303 }
304 }
305
306 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
307 if (state->logicop_enable) {
308 blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
309 (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
310 }
311
312 /* Color channel masks for all MRTs. */
313 blend->color_channel_mask = bgra_cmask(state->rt[0].colormask);
314 if (r300screen->caps.is_r500 && state->independent_blend_enable) {
315 if (state->rt[1].blend_enable) {
316 blend->color_channel_mask |= bgra_cmask(state->rt[1].colormask) << 4;
317 }
318 if (state->rt[2].blend_enable) {
319 blend->color_channel_mask |= bgra_cmask(state->rt[2].colormask) << 8;
320 }
321 if (state->rt[3].blend_enable) {
322 blend->color_channel_mask |= bgra_cmask(state->rt[3].colormask) << 12;
323 }
324 }
325
326 /* Neither fglrx nor classic r300 ever set this, regardless of dithering
327 * state. Since it's an optional implementation detail, we can leave it
328 * out and never dither.
329 *
330 * This could be revisited if we ever get quality or conformance hints.
331 *
332 if (state->dither) {
333 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
334 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
335 }
336 */
337
338 return (void*)blend;
339 }
340
341 /* Bind blend state. */
342 static void r300_bind_blend_state(struct pipe_context* pipe,
343 void* state)
344 {
345 struct r300_context* r300 = r300_context(pipe);
346
347 UPDATE_STATE(state, r300->blend_state);
348 }
349
350 /* Free blend state. */
351 static void r300_delete_blend_state(struct pipe_context* pipe,
352 void* state)
353 {
354 FREE(state);
355 }
356
357 /* Convert float to 10bit integer */
358 static unsigned float_to_fixed10(float f)
359 {
360 return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
361 }
362
363 /* Set blend color.
364 * Setup both R300 and R500 registers, figure out later which one to write. */
365 static void r300_set_blend_color(struct pipe_context* pipe,
366 const struct pipe_blend_color* color)
367 {
368 struct r300_context* r300 = r300_context(pipe);
369 struct r300_blend_color_state* state =
370 (struct r300_blend_color_state*)r300->blend_color_state.state;
371 union util_color uc;
372
373 util_pack_color(color->color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
374 state->blend_color = uc.ui;
375
376 /* XXX if FP16 blending is enabled, we should use the FP16 format */
377 state->blend_color_red_alpha =
378 float_to_fixed10(color->color[0]) |
379 (float_to_fixed10(color->color[3]) << 16);
380 state->blend_color_green_blue =
381 float_to_fixed10(color->color[2]) |
382 (float_to_fixed10(color->color[1]) << 16);
383
384 r300->blend_color_state.size = r300->screen->caps.is_r500 ? 3 : 2;
385 r300->blend_color_state.dirty = TRUE;
386 }
387
388 static void r300_set_clip_state(struct pipe_context* pipe,
389 const struct pipe_clip_state* state)
390 {
391 struct r300_context* r300 = r300_context(pipe);
392
393 r300->clip = *state;
394
395 if (r300->screen->caps.has_tcl) {
396 memcpy(r300->clip_state.state, state, sizeof(struct pipe_clip_state));
397 r300->clip_state.size = 29;
398
399 r300->clip_state.dirty = TRUE;
400 } else {
401 draw_flush(r300->draw);
402 draw_set_clip_state(r300->draw, state);
403 r300->clip_state.size = 2;
404 }
405 }
406
407 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
408 *
409 * This contains the depth buffer, stencil buffer, alpha test, and such.
410 * On the Radeon, depth and stencil buffer setup are intertwined, which is
411 * the reason for some of the strange-looking assignments across registers. */
412 static void*
413 r300_create_dsa_state(struct pipe_context* pipe,
414 const struct pipe_depth_stencil_alpha_state* state)
415 {
416 struct r300_capabilities *caps = &r300_screen(pipe->screen)->caps;
417 struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
418
419 /* Depth test setup. */
420 if (state->depth.enabled) {
421 dsa->z_buffer_control |= R300_Z_ENABLE;
422
423 if (state->depth.writemask) {
424 dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
425 }
426
427 dsa->z_stencil_control |=
428 (r300_translate_depth_stencil_function(state->depth.func) <<
429 R300_Z_FUNC_SHIFT);
430 }
431
432 /* Stencil buffer setup. */
433 if (state->stencil[0].enabled) {
434 dsa->z_buffer_control |= R300_STENCIL_ENABLE;
435 dsa->z_stencil_control |=
436 (r300_translate_depth_stencil_function(state->stencil[0].func) <<
437 R300_S_FRONT_FUNC_SHIFT) |
438 (r300_translate_stencil_op(state->stencil[0].fail_op) <<
439 R300_S_FRONT_SFAIL_OP_SHIFT) |
440 (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
441 R300_S_FRONT_ZPASS_OP_SHIFT) |
442 (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
443 R300_S_FRONT_ZFAIL_OP_SHIFT);
444
445 dsa->stencil_ref_mask =
446 (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
447 (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
448
449 if (state->stencil[1].enabled) {
450 dsa->two_sided = TRUE;
451
452 dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
453 dsa->z_stencil_control |=
454 (r300_translate_depth_stencil_function(state->stencil[1].func) <<
455 R300_S_BACK_FUNC_SHIFT) |
456 (r300_translate_stencil_op(state->stencil[1].fail_op) <<
457 R300_S_BACK_SFAIL_OP_SHIFT) |
458 (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
459 R300_S_BACK_ZPASS_OP_SHIFT) |
460 (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
461 R300_S_BACK_ZFAIL_OP_SHIFT);
462
463 dsa->stencil_ref_bf =
464 (state->stencil[1].valuemask << R300_STENCILMASK_SHIFT) |
465 (state->stencil[1].writemask << R300_STENCILWRITEMASK_SHIFT);
466
467 if (caps->is_r500) {
468 dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
469 } else {
470 dsa->stencil_ref_bf_fallback =
471 (state->stencil[0].valuemask != state->stencil[1].valuemask ||
472 state->stencil[0].writemask != state->stencil[1].writemask);
473 }
474 }
475 }
476
477 /* Alpha test setup. */
478 if (state->alpha.enabled) {
479 dsa->alpha_function =
480 r300_translate_alpha_function(state->alpha.func) |
481 R300_FG_ALPHA_FUNC_ENABLE;
482
483 /* We could use 10bit alpha ref but who needs that? */
484 dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
485
486 if (caps->is_r500)
487 dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
488 }
489
490 return (void*)dsa;
491 }
492
493 static void r300_update_stencil_ref_fallback_status(struct r300_context *r300)
494 {
495 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
496
497 if (r300->screen->caps.is_r500) {
498 return;
499 }
500
501 r300->stencil_ref_bf_fallback =
502 dsa->stencil_ref_bf_fallback ||
503 (dsa->two_sided &&
504 r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
505 }
506
507 /* Bind DSA state. */
508 static void r300_bind_dsa_state(struct pipe_context* pipe,
509 void* state)
510 {
511 struct r300_context* r300 = r300_context(pipe);
512
513 if (!state) {
514 return;
515 }
516
517 UPDATE_STATE(state, r300->dsa_state);
518
519 r300_update_stencil_ref_fallback_status(r300);
520 }
521
522 /* Free DSA state. */
523 static void r300_delete_dsa_state(struct pipe_context* pipe,
524 void* state)
525 {
526 FREE(state);
527 }
528
529 static void r300_set_stencil_ref(struct pipe_context* pipe,
530 const struct pipe_stencil_ref* sr)
531 {
532 struct r300_context* r300 = r300_context(pipe);
533
534 r300->stencil_ref = *sr;
535 r300->dsa_state.dirty = TRUE;
536
537 r300_update_stencil_ref_fallback_status(r300);
538 }
539
540 /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
541 static void r300_fb_set_tiling_flags(struct r300_context *r300,
542 const struct pipe_framebuffer_state *old_state,
543 const struct pipe_framebuffer_state *new_state)
544 {
545 struct r300_texture *tex;
546 unsigned i, level;
547
548 /* Set tiling flags for new surfaces. */
549 for (i = 0; i < new_state->nr_cbufs; i++) {
550 tex = r300_texture(new_state->cbufs[i]->texture);
551 level = new_state->cbufs[i]->level;
552
553 r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
554 tex->pitch[0],
555 tex->microtile,
556 tex->mip_macrotile[level]);
557 }
558 if (new_state->zsbuf) {
559 tex = r300_texture(new_state->zsbuf->texture);
560 level = new_state->zsbuf->level;
561
562 r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
563 tex->pitch[0],
564 tex->microtile,
565 tex->mip_macrotile[level]);
566 }
567 }
568
569 static void r300_print_fb_surf_info(struct pipe_surface *surf, unsigned index,
570 const char *binding)
571 {
572 struct pipe_resource *tex = surf->texture;
573 struct r300_texture *rtex = r300_texture(tex);
574
575 fprintf(stderr,
576 "r300: %s[%i] Dim: %ix%i, Offset: %i, ZSlice: %i, "
577 "Face: %i, Level: %i, Format: %s\n"
578
579 "r300: TEX: Macro: %s, Micro: %s, Pitch: %i, "
580 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
581
582 binding, index, surf->width, surf->height, surf->offset,
583 surf->zslice, surf->face, surf->level,
584 util_format_short_name(surf->format),
585
586 rtex->macrotile ? "YES" : " NO", rtex->microtile ? "YES" : " NO",
587 rtex->hwpitch[0], tex->width0, tex->height0, tex->depth0,
588 tex->last_level, util_format_short_name(tex->format));
589 }
590
591 static void
592 r300_set_framebuffer_state(struct pipe_context* pipe,
593 const struct pipe_framebuffer_state* state)
594 {
595 struct r300_context* r300 = r300_context(pipe);
596 struct pipe_framebuffer_state *old_state = r300->fb_state.state;
597 unsigned max_width, max_height, i;
598 uint32_t zbuffer_bpp = 0;
599
600 if (state->nr_cbufs > 4) {
601 fprintf(stderr, "r300: Implementation error: Too many MRTs in %s, "
602 "refusing to bind framebuffer state!\n", __FUNCTION__);
603 return;
604 }
605
606 if (r300->screen->caps.is_r500) {
607 max_width = max_height = 4096;
608 } else if (r300->screen->caps.is_r400) {
609 max_width = max_height = 4021;
610 } else {
611 max_width = max_height = 2560;
612 }
613
614 if (state->width > max_width || state->height > max_height) {
615 fprintf(stderr, "r300: Implementation error: Render targets are too "
616 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__);
617 return;
618 }
619
620 if (r300->draw) {
621 draw_flush(r300->draw);
622 }
623
624 r300->fb_state.dirty = TRUE;
625
626 /* If nr_cbufs is changed from zero to non-zero or vice versa... */
627 if (!!old_state->nr_cbufs != !!state->nr_cbufs) {
628 r300->blend_state.dirty = TRUE;
629 }
630 /* If zsbuf is set from NULL to non-NULL or vice versa.. */
631 if (!!old_state->zsbuf != !!state->zsbuf) {
632 r300->dsa_state.dirty = TRUE;
633 }
634
635 /* The tiling flags are dependent on the surface miplevel, unfortunately. */
636 r300_fb_set_tiling_flags(r300, r300->fb_state.state, state);
637
638 memcpy(r300->fb_state.state, state, sizeof(struct pipe_framebuffer_state));
639
640 r300->fb_state.size = (10 * state->nr_cbufs) + (2 * (4 - state->nr_cbufs)) +
641 (state->zsbuf ? 10 : 0) + 9;
642
643 /* Polygon offset depends on the zbuffer bit depth. */
644 if (state->zsbuf && r300->polygon_offset_enabled) {
645 switch (util_format_get_blocksize(state->zsbuf->texture->format)) {
646 case 2:
647 zbuffer_bpp = 16;
648 break;
649 case 4:
650 zbuffer_bpp = 24;
651 break;
652 }
653
654 if (r300->zbuffer_bpp != zbuffer_bpp) {
655 r300->zbuffer_bpp = zbuffer_bpp;
656 r300->rs_state.dirty = TRUE;
657 }
658 }
659
660 if (DBG_ON(r300, DBG_FB)) {
661 fprintf(stderr, "r300: set_framebuffer_state:\n");
662 for (i = 0; i < state->nr_cbufs; i++) {
663 r300_print_fb_surf_info(state->cbufs[i], i, "CB");
664 }
665 if (state->zsbuf) {
666 r300_print_fb_surf_info(state->zsbuf, 0, "ZB");
667 }
668 }
669 }
670
671 /* Create fragment shader state. */
672 static void* r300_create_fs_state(struct pipe_context* pipe,
673 const struct pipe_shader_state* shader)
674 {
675 struct r300_fragment_shader* fs = NULL;
676
677 fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
678
679 /* Copy state directly into shader. */
680 fs->state = *shader;
681 fs->state.tokens = tgsi_dup_tokens(shader->tokens);
682
683 return (void*)fs;
684 }
685
686 void r300_mark_fs_code_dirty(struct r300_context *r300)
687 {
688 struct r300_fragment_shader* fs = r300_fs(r300);
689
690 r300->fs.dirty = TRUE;
691 r300->fs_rc_constant_state.dirty = TRUE;
692 r300->fs_constants.dirty = TRUE;
693
694 if (r300->screen->caps.is_r500) {
695 r300->fs.size = r500_get_fs_atom_size(r300);
696 r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 7;
697 r300->fs_constants.size = fs->shader->externals_count * 4 + 3;
698 } else {
699 r300->fs.size = r300_get_fs_atom_size(r300);
700 r300->fs_rc_constant_state.size = fs->shader->rc_state_count * 5;
701 r300->fs_constants.size = fs->shader->externals_count * 4 + 1;
702 }
703 }
704
705 /* Bind fragment shader state. */
706 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
707 {
708 struct r300_context* r300 = r300_context(pipe);
709 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
710
711 if (fs == NULL) {
712 r300->fs.state = NULL;
713 return;
714 }
715
716 r300->fs.state = fs;
717 r300_pick_fragment_shader(r300);
718 r300_mark_fs_code_dirty(r300);
719
720 r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
721 }
722
723 /* Delete fragment shader state. */
724 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
725 {
726 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
727 struct r300_fragment_shader_code *tmp, *ptr = fs->first;
728
729 while (ptr) {
730 tmp = ptr;
731 ptr = ptr->next;
732 rc_constants_destroy(&tmp->code.constants);
733 FREE(tmp);
734 }
735 FREE((void*)fs->state.tokens);
736 FREE(shader);
737 }
738
739 static void r300_set_polygon_stipple(struct pipe_context* pipe,
740 const struct pipe_poly_stipple* state)
741 {
742 /* XXX no idea how to set this up, but not terribly important */
743 }
744
745 /* Create a new rasterizer state based on the CSO rasterizer state.
746 *
747 * This is a very large chunk of state, and covers most of the graphics
748 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
749 *
750 * In a not entirely unironic sidenote, this state has nearly nothing to do
751 * with the actual block on the Radeon called the rasterizer (RS). */
752 static void* r300_create_rs_state(struct pipe_context* pipe,
753 const struct pipe_rasterizer_state* state)
754 {
755 struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
756 int i;
757 float psiz;
758
759 /* Copy rasterizer state. */
760 rs->rs = *state;
761 rs->rs_draw = *state;
762
763 /* Override some states for Draw. */
764 rs->rs_draw.sprite_coord_enable = 0; /* We can do this in HW. */
765
766 #ifdef PIPE_ARCH_LITTLE_ENDIAN
767 rs->vap_control_status = R300_VC_NO_SWAP;
768 #else
769 rs->vap_control_status = R300_VC_32BIT_SWAP;
770 #endif
771
772 /* If no TCL engine is present, turn off the HW TCL. */
773 if (!r300_screen(pipe->screen)->caps.has_tcl) {
774 rs->vap_control_status |= R300_VAP_TCL_BYPASS;
775 }
776
777 /* Point size width and height. */
778 rs->point_size =
779 pack_float_16_6x(state->point_size) |
780 (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
781
782 /* Point size clamping. */
783 if (state->point_size_per_vertex) {
784 /* Per-vertex point size.
785 * Clamp to [0, max FB size] */
786 psiz = pipe->screen->get_paramf(pipe->screen,
787 PIPE_CAP_MAX_POINT_WIDTH);
788 rs->point_minmax =
789 pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT;
790 } else {
791 /* We cannot disable the point-size vertex output,
792 * so clamp it. */
793 psiz = state->point_size;
794 rs->point_minmax =
795 (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MIN_SHIFT) |
796 (pack_float_16_6x(psiz) << R300_GA_POINT_MINMAX_MAX_SHIFT);
797 }
798
799 /* Line control. */
800 rs->line_control = pack_float_16_6x(state->line_width) |
801 R300_GA_LINE_CNTL_END_TYPE_COMP;
802
803 /* Enable polygon mode */
804 if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
805 state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
806 rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
807 }
808
809 /* Radeons don't think in "CW/CCW", they think in "front/back". */
810 if (state->front_winding == PIPE_WINDING_CW) {
811 rs->cull_mode = R300_FRONT_FACE_CW;
812
813 /* Polygon offset */
814 if (state->offset_cw) {
815 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
816 }
817 if (state->offset_ccw) {
818 rs->polygon_offset_enable |= R300_BACK_ENABLE;
819 }
820
821 /* Polygon mode */
822 if (rs->polygon_mode) {
823 rs->polygon_mode |=
824 r300_translate_polygon_mode_front(state->fill_cw);
825 rs->polygon_mode |=
826 r300_translate_polygon_mode_back(state->fill_ccw);
827 }
828 } else {
829 rs->cull_mode = R300_FRONT_FACE_CCW;
830
831 /* Polygon offset */
832 if (state->offset_ccw) {
833 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
834 }
835 if (state->offset_cw) {
836 rs->polygon_offset_enable |= R300_BACK_ENABLE;
837 }
838
839 /* Polygon mode */
840 if (rs->polygon_mode) {
841 rs->polygon_mode |=
842 r300_translate_polygon_mode_front(state->fill_ccw);
843 rs->polygon_mode |=
844 r300_translate_polygon_mode_back(state->fill_cw);
845 }
846 }
847 if (state->front_winding & state->cull_mode) {
848 rs->cull_mode |= R300_CULL_FRONT;
849 }
850 if (~(state->front_winding) & state->cull_mode) {
851 rs->cull_mode |= R300_CULL_BACK;
852 }
853
854 if (rs->polygon_offset_enable) {
855 rs->depth_offset = state->offset_units;
856 rs->depth_scale = state->offset_scale;
857 }
858
859 if (state->line_stipple_enable) {
860 rs->line_stipple_config =
861 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
862 (fui((float)state->line_stipple_factor) &
863 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
864 /* XXX this might need to be scaled up */
865 rs->line_stipple_value = state->line_stipple_pattern;
866 }
867
868 if (state->flatshade) {
869 rs->color_control = R300_SHADE_MODEL_FLAT;
870 } else {
871 rs->color_control = R300_SHADE_MODEL_SMOOTH;
872 }
873
874 rs->clip_rule = state->scissor ? 0xAAAA : 0xFFFF;
875
876 /* Point sprites */
877 if (state->sprite_coord_enable) {
878 rs->stuffing_enable = R300_GB_POINT_STUFF_ENABLE;
879 for (i = 0; i < 8; i++) {
880 if (state->sprite_coord_enable & (1 << i))
881 rs->stuffing_enable |=
882 R300_GB_TEX_STR << (R300_GB_TEX0_SOURCE_SHIFT + (i*2));
883 }
884
885 rs->point_texcoord_left = 0.0f;
886 rs->point_texcoord_right = 1.0f;
887
888 switch (state->sprite_coord_mode) {
889 case PIPE_SPRITE_COORD_UPPER_LEFT:
890 rs->point_texcoord_top = 0.0f;
891 rs->point_texcoord_bottom = 1.0f;
892 break;
893 case PIPE_SPRITE_COORD_LOWER_LEFT:
894 rs->point_texcoord_top = 1.0f;
895 rs->point_texcoord_bottom = 0.0f;
896 break;
897 }
898 }
899
900 return (void*)rs;
901 }
902
903 /* Bind rasterizer state. */
904 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
905 {
906 struct r300_context* r300 = r300_context(pipe);
907 struct r300_rs_state* rs = (struct r300_rs_state*)state;
908 int last_sprite_coord_enable = r300->sprite_coord_enable;
909 boolean last_two_sided_color = r300->two_sided_color;
910
911 if (r300->draw && rs) {
912 draw_flush(r300->draw);
913 draw_set_rasterizer_state(r300->draw, &rs->rs_draw, state);
914 }
915
916 if (rs) {
917 r300->polygon_offset_enabled = rs->rs.offset_cw || rs->rs.offset_ccw;
918 r300->sprite_coord_enable = rs->rs.sprite_coord_enable;
919 r300->two_sided_color = rs->rs.light_twoside;
920 } else {
921 r300->polygon_offset_enabled = FALSE;
922 r300->sprite_coord_enable = 0;
923 r300->two_sided_color = FALSE;
924 }
925
926 UPDATE_STATE(state, r300->rs_state);
927 r300->rs_state.size = 27 + (r300->polygon_offset_enabled ? 5 : 0);
928
929 if (last_sprite_coord_enable != r300->sprite_coord_enable ||
930 last_two_sided_color != r300->two_sided_color) {
931 r300->rs_block_state.dirty = TRUE;
932 }
933 }
934
935 /* Free rasterizer state. */
936 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
937 {
938 FREE(state);
939 }
940
941 static void*
942 r300_create_sampler_state(struct pipe_context* pipe,
943 const struct pipe_sampler_state* state)
944 {
945 struct r300_context* r300 = r300_context(pipe);
946 struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
947 boolean is_r500 = r300->screen->caps.is_r500;
948 int lod_bias;
949 union util_color uc;
950
951 sampler->state = *state;
952
953 sampler->filter0 |=
954 (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
955 (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
956 (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
957
958 sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
959 state->mag_img_filter,
960 state->min_mip_filter,
961 state->max_anisotropy > 0);
962
963 sampler->filter0 |= r300_anisotropy(state->max_anisotropy);
964
965 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
966 /* We must pass these to the merge function to clamp them properly. */
967 sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
968 sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
969
970 lod_bias = CLAMP((int)(state->lod_bias * 32 + 1), -(1 << 9), (1 << 9) - 1);
971
972 sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
973
974 /* This is very high quality anisotropic filtering for R5xx.
975 * It's good for benchmarking the performance of texturing but
976 * in practice we don't want to slow down the driver because it's
977 * a pretty good performance killer. Feel free to play with it. */
978 if (DBG_ON(r300, DBG_ANISOHQ) && is_r500) {
979 sampler->filter1 |= r500_anisotropy(state->max_anisotropy);
980 }
981
982 util_pack_color(state->border_color, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
983 sampler->border_color = uc.ui;
984
985 /* R500-specific fixups and optimizations */
986 if (r300->screen->caps.is_r500) {
987 sampler->filter1 |= R500_BORDER_FIX;
988 }
989
990 return (void*)sampler;
991 }
992
993 static void r300_bind_sampler_states(struct pipe_context* pipe,
994 unsigned count,
995 void** states)
996 {
997 struct r300_context* r300 = r300_context(pipe);
998 struct r300_textures_state* state =
999 (struct r300_textures_state*)r300->textures_state.state;
1000 unsigned tex_units = r300->screen->caps.num_tex_units;
1001
1002 if (count > tex_units) {
1003 return;
1004 }
1005
1006 memcpy(state->sampler_states, states, sizeof(void*) * count);
1007 state->sampler_state_count = count;
1008
1009 r300->textures_state.dirty = TRUE;
1010 }
1011
1012 static void r300_lacks_vertex_textures(struct pipe_context* pipe,
1013 unsigned count,
1014 void** states)
1015 {
1016 }
1017
1018 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
1019 {
1020 FREE(state);
1021 }
1022
1023 static void r300_set_fragment_sampler_views(struct pipe_context* pipe,
1024 unsigned count,
1025 struct pipe_sampler_view** views)
1026 {
1027 struct r300_context* r300 = r300_context(pipe);
1028 struct r300_textures_state* state =
1029 (struct r300_textures_state*)r300->textures_state.state;
1030 struct r300_texture *texture;
1031 unsigned i;
1032 unsigned tex_units = r300->screen->caps.num_tex_units;
1033 boolean dirty_tex = FALSE;
1034
1035 if (count > tex_units) {
1036 return;
1037 }
1038
1039 for (i = 0; i < count; i++) {
1040 if (&state->sampler_views[i]->base != views[i]) {
1041 pipe_sampler_view_reference(
1042 (struct pipe_sampler_view**)&state->sampler_views[i],
1043 views[i]);
1044
1045 if (!views[i]) {
1046 continue;
1047 }
1048
1049 /* A new sampler view (= texture)... */
1050 dirty_tex = TRUE;
1051
1052 /* Set the texrect factor in the fragment shader.
1053 * Needed for RECT and NPOT fallback. */
1054 texture = r300_texture(views[i]->texture);
1055 if (texture->uses_pitch) {
1056 r300->fs_rc_constant_state.dirty = TRUE;
1057 }
1058 }
1059 }
1060
1061 for (i = count; i < tex_units; i++) {
1062 if (state->sampler_views[i]) {
1063 pipe_sampler_view_reference(
1064 (struct pipe_sampler_view**)&state->sampler_views[i],
1065 NULL);
1066 }
1067 }
1068
1069 state->sampler_view_count = count;
1070
1071 r300->textures_state.dirty = TRUE;
1072
1073 if (dirty_tex) {
1074 r300->texture_cache_inval.dirty = TRUE;
1075 }
1076 }
1077
1078 static struct pipe_sampler_view *
1079 r300_create_sampler_view(struct pipe_context *pipe,
1080 struct pipe_resource *texture,
1081 const struct pipe_sampler_view *templ)
1082 {
1083 struct r300_sampler_view *view = CALLOC_STRUCT(r300_sampler_view);
1084 struct r300_texture *tex = r300_texture(texture);
1085
1086 if (view) {
1087 view->base = *templ;
1088 view->base.reference.count = 1;
1089 view->base.context = pipe;
1090 view->base.texture = NULL;
1091 pipe_resource_reference(&view->base.texture, texture);
1092
1093 view->swizzle[0] = templ->swizzle_r;
1094 view->swizzle[1] = templ->swizzle_g;
1095 view->swizzle[2] = templ->swizzle_b;
1096 view->swizzle[3] = templ->swizzle_a;
1097
1098 view->format = tex->tx_format;
1099 view->format.format1 |= r300_translate_texformat(templ->format,
1100 view->swizzle);
1101 if (r300_screen(pipe->screen)->caps.is_r500) {
1102 view->format.format2 |= r500_tx_format_msb_bit(templ->format);
1103 }
1104 }
1105
1106 return (struct pipe_sampler_view*)view;
1107 }
1108
1109 static void
1110 r300_sampler_view_destroy(struct pipe_context *pipe,
1111 struct pipe_sampler_view *view)
1112 {
1113 pipe_resource_reference(&view->texture, NULL);
1114 FREE(view);
1115 }
1116
1117 static void r300_set_scissor_state(struct pipe_context* pipe,
1118 const struct pipe_scissor_state* state)
1119 {
1120 struct r300_context* r300 = r300_context(pipe);
1121
1122 memcpy(r300->scissor_state.state, state,
1123 sizeof(struct pipe_scissor_state));
1124
1125 r300->scissor_state.dirty = TRUE;
1126 }
1127
1128 static void r300_set_viewport_state(struct pipe_context* pipe,
1129 const struct pipe_viewport_state* state)
1130 {
1131 struct r300_context* r300 = r300_context(pipe);
1132 struct r300_viewport_state* viewport =
1133 (struct r300_viewport_state*)r300->viewport_state.state;
1134
1135 r300->viewport = *state;
1136
1137 if (r300->draw) {
1138 draw_flush(r300->draw);
1139 draw_set_viewport_state(r300->draw, state);
1140 viewport->vte_control = R300_VTX_XY_FMT | R300_VTX_Z_FMT;
1141 return;
1142 }
1143
1144 /* Do the transform in HW. */
1145 viewport->vte_control = R300_VTX_W0_FMT;
1146
1147 if (state->scale[0] != 1.0f) {
1148 viewport->xscale = state->scale[0];
1149 viewport->vte_control |= R300_VPORT_X_SCALE_ENA;
1150 }
1151 if (state->scale[1] != 1.0f) {
1152 viewport->yscale = state->scale[1];
1153 viewport->vte_control |= R300_VPORT_Y_SCALE_ENA;
1154 }
1155 if (state->scale[2] != 1.0f) {
1156 viewport->zscale = state->scale[2];
1157 viewport->vte_control |= R300_VPORT_Z_SCALE_ENA;
1158 }
1159 if (state->translate[0] != 0.0f) {
1160 viewport->xoffset = state->translate[0];
1161 viewport->vte_control |= R300_VPORT_X_OFFSET_ENA;
1162 }
1163 if (state->translate[1] != 0.0f) {
1164 viewport->yoffset = state->translate[1];
1165 viewport->vte_control |= R300_VPORT_Y_OFFSET_ENA;
1166 }
1167 if (state->translate[2] != 0.0f) {
1168 viewport->zoffset = state->translate[2];
1169 viewport->vte_control |= R300_VPORT_Z_OFFSET_ENA;
1170 }
1171
1172 r300->viewport_state.dirty = TRUE;
1173 if (r300->fs.state && r300_fs(r300)->shader->inputs.wpos != ATTR_UNUSED) {
1174 r300->fs_rc_constant_state.dirty = TRUE;
1175 }
1176 }
1177
1178 static void r300_set_vertex_buffers(struct pipe_context* pipe,
1179 unsigned count,
1180 const struct pipe_vertex_buffer* buffers)
1181 {
1182 struct r300_context* r300 = r300_context(pipe);
1183 struct pipe_vertex_buffer *vbo;
1184 unsigned i, max_index = (1 << 24) - 1;
1185 boolean any_user_buffer = FALSE;
1186
1187 if (count == r300->vertex_buffer_count &&
1188 memcmp(r300->vertex_buffer, buffers,
1189 sizeof(struct pipe_vertex_buffer) * count) == 0) {
1190 return;
1191 }
1192
1193 /* Check if the stride is aligned to the size of DWORD. */
1194 for (i = 0; i < count; i++) {
1195 if (buffers[i].buffer) {
1196 if (buffers[i].stride % 4 != 0) {
1197 // XXX Shouldn't we align the buffer?
1198 fprintf(stderr, "r300: set_vertex_buffers: "
1199 "Unaligned buffer stride %i isn't supported.\n",
1200 buffers[i].stride);
1201 abort();
1202 }
1203 }
1204 }
1205
1206 for (i = 0; i < count; i++) {
1207 /* Why, yes, I AM casting away constness. How did you know? */
1208 vbo = (struct pipe_vertex_buffer*)&buffers[i];
1209
1210 /* Reference our buffer. */
1211 pipe_resource_reference(&r300->vertex_buffer[i].buffer, vbo->buffer);
1212
1213 /* Skip NULL buffers */
1214 if (!buffers[i].buffer) {
1215 continue;
1216 }
1217
1218 if (r300_buffer_is_user_buffer(vbo->buffer)) {
1219 any_user_buffer = TRUE;
1220 }
1221
1222 if (vbo->max_index == ~0) {
1223 /* if no VBO stride then only one vertex value so max index is 1 */
1224 /* should think about converting to VS constants like svga does */
1225 if (!vbo->stride)
1226 vbo->max_index = 1;
1227 else
1228 vbo->max_index =
1229 (vbo->buffer->width0 - vbo->buffer_offset) / vbo->stride;
1230 }
1231
1232 max_index = MIN2(vbo->max_index, max_index);
1233 }
1234
1235 for (; i < r300->vertex_buffer_count; i++) {
1236 /* Dereference any old buffers. */
1237 pipe_resource_reference(&r300->vertex_buffer[i].buffer, NULL);
1238 }
1239
1240 memcpy(r300->vertex_buffer, buffers,
1241 sizeof(struct pipe_vertex_buffer) * count);
1242
1243 r300->vertex_buffer_count = count;
1244 r300->vertex_buffer_max_index = max_index;
1245 r300->any_user_vbs = any_user_buffer;
1246
1247 if (r300->draw) {
1248 draw_flush(r300->draw);
1249 draw_set_vertex_buffers(r300->draw, count, buffers);
1250 }
1251 }
1252
1253 /* Initialize the PSC tables. */
1254 static void r300_vertex_psc(struct r300_vertex_element_state *velems)
1255 {
1256 struct r300_vertex_stream_state *vstream = &velems->vertex_stream;
1257 uint16_t type, swizzle;
1258 enum pipe_format format;
1259 unsigned i;
1260
1261 if (velems->count > 16) {
1262 fprintf(stderr, "r300: More than 16 vertex elements are not supported,"
1263 " requested %i, using 16.\n", velems->count);
1264 velems->count = 16;
1265 }
1266
1267 /* Vertex shaders have no semantics on their inputs,
1268 * so PSC should just route stuff based on the vertex elements,
1269 * and not on attrib information. */
1270 for (i = 0; i < velems->count; i++) {
1271 format = velems->velem[i].src_format;
1272
1273 type = r300_translate_vertex_data_type(format) |
1274 (i << R300_DST_VEC_LOC_SHIFT);
1275 swizzle = r300_translate_vertex_data_swizzle(format);
1276
1277 if (i & 1) {
1278 vstream->vap_prog_stream_cntl[i >> 1] |= type << 16;
1279 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle << 16;
1280 } else {
1281 vstream->vap_prog_stream_cntl[i >> 1] |= type;
1282 vstream->vap_prog_stream_cntl_ext[i >> 1] |= swizzle;
1283 }
1284 }
1285
1286 /* Set the last vector in the PSC. */
1287 if (i) {
1288 i -= 1;
1289 }
1290 vstream->vap_prog_stream_cntl[i >> 1] |=
1291 (R300_LAST_VEC << (i & 1 ? 16 : 0));
1292
1293 vstream->count = (i >> 1) + 1;
1294 }
1295
1296 static void* r300_create_vertex_elements_state(struct pipe_context* pipe,
1297 unsigned count,
1298 const struct pipe_vertex_element* attribs)
1299 {
1300 struct r300_vertex_element_state *velems;
1301 unsigned i, size;
1302 enum pipe_format *format;
1303
1304 assert(count <= PIPE_MAX_ATTRIBS);
1305 velems = CALLOC_STRUCT(r300_vertex_element_state);
1306 if (velems != NULL) {
1307 velems->count = count;
1308 memcpy(velems->velem, attribs, sizeof(struct pipe_vertex_element) * count);
1309
1310 if (r300_screen(pipe->screen)->caps.has_tcl) {
1311 r300_vertex_psc(velems);
1312
1313 /* Check if the format is aligned to the size of DWORD.
1314 * We only care about the blocksizes of the formats since
1315 * swizzles are already set up. */
1316 for (i = 0; i < count; i++) {
1317 format = &velems->velem[i].src_format;
1318
1319 /* Replace some formats with their aligned counterparts,
1320 * this is OK because we check for aligned strides too. */
1321 switch (*format) {
1322 /* Align to RGBA8. */
1323 case PIPE_FORMAT_R8_UNORM:
1324 case PIPE_FORMAT_R8G8_UNORM:
1325 case PIPE_FORMAT_R8G8B8_UNORM:
1326 *format = PIPE_FORMAT_R8G8B8A8_UNORM;
1327 continue;
1328 case PIPE_FORMAT_R8_SNORM:
1329 case PIPE_FORMAT_R8G8_SNORM:
1330 case PIPE_FORMAT_R8G8B8_SNORM:
1331 *format = PIPE_FORMAT_R8G8B8A8_SNORM;
1332 continue;
1333 case PIPE_FORMAT_R8_USCALED:
1334 case PIPE_FORMAT_R8G8_USCALED:
1335 case PIPE_FORMAT_R8G8B8_USCALED:
1336 *format = PIPE_FORMAT_R8G8B8A8_USCALED;
1337 continue;
1338 case PIPE_FORMAT_R8_SSCALED:
1339 case PIPE_FORMAT_R8G8_SSCALED:
1340 case PIPE_FORMAT_R8G8B8_SSCALED:
1341 *format = PIPE_FORMAT_R8G8B8A8_SSCALED;
1342 continue;
1343
1344 /* Align to RG16. */
1345 case PIPE_FORMAT_R16_UNORM:
1346 *format = PIPE_FORMAT_R16G16_UNORM;
1347 continue;
1348 case PIPE_FORMAT_R16_SNORM:
1349 *format = PIPE_FORMAT_R16G16_SNORM;
1350 continue;
1351 case PIPE_FORMAT_R16_USCALED:
1352 *format = PIPE_FORMAT_R16G16_USCALED;
1353 continue;
1354 case PIPE_FORMAT_R16_SSCALED:
1355 *format = PIPE_FORMAT_R16G16_SSCALED;
1356 continue;
1357 case PIPE_FORMAT_R16_FLOAT:
1358 *format = PIPE_FORMAT_R16G16_FLOAT;
1359 continue;
1360
1361 /* Align to RGBA16. */
1362 case PIPE_FORMAT_R16G16B16_UNORM:
1363 *format = PIPE_FORMAT_R16G16B16A16_UNORM;
1364 continue;
1365 case PIPE_FORMAT_R16G16B16_SNORM:
1366 *format = PIPE_FORMAT_R16G16B16A16_SNORM;
1367 continue;
1368 case PIPE_FORMAT_R16G16B16_USCALED:
1369 *format = PIPE_FORMAT_R16G16B16A16_USCALED;
1370 continue;
1371 case PIPE_FORMAT_R16G16B16_SSCALED:
1372 *format = PIPE_FORMAT_R16G16B16A16_SSCALED;
1373 continue;
1374 case PIPE_FORMAT_R16G16B16_FLOAT:
1375 *format = PIPE_FORMAT_R16G16B16A16_FLOAT;
1376 continue;
1377
1378 default:;
1379 }
1380
1381 size = util_format_get_blocksize(*format);
1382
1383 if (size % 4 != 0) {
1384 /* XXX Shouldn't we align the format? */
1385 fprintf(stderr, "r300_create_vertex_elements_state: "
1386 "Unaligned format %s:%i isn't supported\n",
1387 util_format_short_name(*format), size);
1388 assert(0);
1389 abort();
1390 }
1391 }
1392 }
1393 }
1394 return velems;
1395 }
1396
1397 static void r300_bind_vertex_elements_state(struct pipe_context *pipe,
1398 void *state)
1399 {
1400 struct r300_context *r300 = r300_context(pipe);
1401 struct r300_vertex_element_state *velems = state;
1402
1403 if (velems == NULL) {
1404 return;
1405 }
1406
1407 r300->velems = velems;
1408
1409 if (r300->draw) {
1410 draw_flush(r300->draw);
1411 draw_set_vertex_elements(r300->draw, velems->count, velems->velem);
1412 return;
1413 }
1414
1415 UPDATE_STATE(&velems->vertex_stream, r300->vertex_stream_state);
1416 r300->vertex_stream_state.size = (1 + velems->vertex_stream.count) * 2;
1417 }
1418
1419 static void r300_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
1420 {
1421 FREE(state);
1422 }
1423
1424 static void* r300_create_vs_state(struct pipe_context* pipe,
1425 const struct pipe_shader_state* shader)
1426 {
1427 struct r300_context* r300 = r300_context(pipe);
1428
1429 struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
1430
1431 /* Copy state directly into shader. */
1432 vs->state = *shader;
1433 vs->state.tokens = tgsi_dup_tokens(shader->tokens);
1434
1435 r300_init_vs_outputs(vs);
1436
1437 if (r300->screen->caps.has_tcl) {
1438 r300_translate_vertex_shader(r300, vs);
1439 } else {
1440 vs->draw_vs = draw_create_vertex_shader(r300->draw, shader);
1441 }
1442
1443 return vs;
1444 }
1445
1446 static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
1447 {
1448 struct r300_context* r300 = r300_context(pipe);
1449 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1450
1451 if (vs == NULL) {
1452 r300->vs_state.state = NULL;
1453 return;
1454 }
1455 if (vs == r300->vs_state.state) {
1456 return;
1457 }
1458 r300->vs_state.state = vs;
1459
1460 /* The majority of the RS block bits is dependent on the vertex shader. */
1461 r300->rs_block_state.dirty = TRUE; /* Will be updated before the emission. */
1462
1463 if (r300->screen->caps.has_tcl) {
1464 r300->vs_state.dirty = TRUE;
1465 r300->vs_state.size =
1466 vs->code.length + 9 +
1467 (vs->immediates_count ? vs->immediates_count * 4 + 3 : 0);
1468
1469 if (vs->externals_count) {
1470 r300->vs_constants.dirty = TRUE;
1471 r300->vs_constants.size = vs->externals_count * 4 + 3;
1472 } else {
1473 r300->vs_constants.size = 0;
1474 }
1475
1476 r300->pvs_flush.dirty = TRUE;
1477 } else {
1478 draw_flush(r300->draw);
1479 draw_bind_vertex_shader(r300->draw,
1480 (struct draw_vertex_shader*)vs->draw_vs);
1481 }
1482 }
1483
1484 static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
1485 {
1486 struct r300_context* r300 = r300_context(pipe);
1487 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
1488
1489 if (r300->screen->caps.has_tcl) {
1490 rc_constants_destroy(&vs->code.constants);
1491 } else {
1492 draw_delete_vertex_shader(r300->draw,
1493 (struct draw_vertex_shader*)vs->draw_vs);
1494 }
1495
1496 FREE((void*)vs->state.tokens);
1497 FREE(shader);
1498 }
1499
1500 static void r300_set_constant_buffer(struct pipe_context *pipe,
1501 uint shader, uint index,
1502 struct pipe_resource *buf)
1503 {
1504 struct r300_context* r300 = r300_context(pipe);
1505 struct r300_constant_buffer *cbuf;
1506 struct pipe_transfer *tr;
1507 void *mapped;
1508 int max_size = 0, max_size_bytes = 0, clamped_size = 0;
1509
1510 switch (shader) {
1511 case PIPE_SHADER_VERTEX:
1512 cbuf = (struct r300_constant_buffer*)r300->vs_constants.state;
1513 max_size = 256;
1514 break;
1515 case PIPE_SHADER_FRAGMENT:
1516 cbuf = (struct r300_constant_buffer*)r300->fs_constants.state;
1517 if (r300->screen->caps.is_r500) {
1518 max_size = 256;
1519 } else {
1520 max_size = 32;
1521 }
1522 break;
1523 default:
1524 assert(0);
1525 return;
1526 }
1527 max_size_bytes = max_size * 4 * sizeof(float);
1528
1529 if (buf == NULL || buf->width0 == 0 ||
1530 (mapped = pipe_buffer_map(pipe, buf, PIPE_TRANSFER_READ, &tr)) == NULL)
1531 {
1532 cbuf->count = 0;
1533 return;
1534 }
1535
1536 if (shader == PIPE_SHADER_FRAGMENT ||
1537 (shader == PIPE_SHADER_VERTEX && r300->screen->caps.has_tcl)) {
1538 assert((buf->width0 % (4 * sizeof(float))) == 0);
1539
1540 /* Check the size of the constant buffer. */
1541 /* XXX Subtract immediates and RC_STATE_* variables. */
1542 if (buf->width0 > max_size_bytes) {
1543 fprintf(stderr, "r300: Max size of the constant buffer is "
1544 "%i*4 floats.\n", max_size);
1545 }
1546 clamped_size = MIN2(buf->width0, max_size_bytes);
1547
1548 memcpy(cbuf->constants, mapped, clamped_size);
1549 cbuf->count = clamped_size / (4 * sizeof(float));
1550 }
1551
1552 if (shader == PIPE_SHADER_VERTEX) {
1553 if (r300->screen->caps.has_tcl) {
1554 if (r300->vs_constants.size) {
1555 r300->vs_constants.dirty = TRUE;
1556 }
1557 r300->pvs_flush.dirty = TRUE;
1558 } else if (r300->draw) {
1559 draw_set_mapped_constant_buffer(r300->draw, PIPE_SHADER_VERTEX,
1560 0, mapped, buf->width0);
1561 }
1562 } else if (shader == PIPE_SHADER_FRAGMENT) {
1563 r300->fs_constants.dirty = TRUE;
1564 }
1565
1566 pipe_buffer_unmap(pipe, buf, tr);
1567 }
1568
1569 void r300_init_state_functions(struct r300_context* r300)
1570 {
1571 r300->context.create_blend_state = r300_create_blend_state;
1572 r300->context.bind_blend_state = r300_bind_blend_state;
1573 r300->context.delete_blend_state = r300_delete_blend_state;
1574
1575 r300->context.set_blend_color = r300_set_blend_color;
1576
1577 r300->context.set_clip_state = r300_set_clip_state;
1578
1579 r300->context.set_constant_buffer = r300_set_constant_buffer;
1580
1581 r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
1582 r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
1583 r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
1584
1585 r300->context.set_stencil_ref = r300_set_stencil_ref;
1586
1587 r300->context.set_framebuffer_state = r300_set_framebuffer_state;
1588
1589 r300->context.create_fs_state = r300_create_fs_state;
1590 r300->context.bind_fs_state = r300_bind_fs_state;
1591 r300->context.delete_fs_state = r300_delete_fs_state;
1592
1593 r300->context.set_polygon_stipple = r300_set_polygon_stipple;
1594
1595 r300->context.create_rasterizer_state = r300_create_rs_state;
1596 r300->context.bind_rasterizer_state = r300_bind_rs_state;
1597 r300->context.delete_rasterizer_state = r300_delete_rs_state;
1598
1599 r300->context.create_sampler_state = r300_create_sampler_state;
1600 r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
1601 r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
1602 r300->context.delete_sampler_state = r300_delete_sampler_state;
1603
1604 r300->context.set_fragment_sampler_views = r300_set_fragment_sampler_views;
1605 r300->context.create_sampler_view = r300_create_sampler_view;
1606 r300->context.sampler_view_destroy = r300_sampler_view_destroy;
1607
1608 r300->context.set_scissor_state = r300_set_scissor_state;
1609
1610 r300->context.set_viewport_state = r300_set_viewport_state;
1611
1612 r300->context.set_vertex_buffers = r300_set_vertex_buffers;
1613
1614 r300->context.create_vertex_elements_state = r300_create_vertex_elements_state;
1615 r300->context.bind_vertex_elements_state = r300_bind_vertex_elements_state;
1616 r300->context.delete_vertex_elements_state = r300_delete_vertex_elements_state;
1617
1618 r300->context.create_vs_state = r300_create_vs_state;
1619 r300->context.bind_vs_state = r300_bind_vs_state;
1620 r300->context.delete_vs_state = r300_delete_vs_state;
1621 }