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