r300g: fix blending when SRC_ALPHA_SATURATE is used
[mesa.git] / src / gallium / drivers / r300 / r300_state.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "draw/draw_context.h"
24
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
27 #include "util/u_pack_color.h"
28
29 #include "tgsi/tgsi_parse.h"
30
31 #include "pipe/p_config.h"
32 #include "pipe/internal/p_winsys_screen.h"
33
34 #include "r300_context.h"
35 #include "r300_reg.h"
36 #include "r300_screen.h"
37 #include "r300_state_inlines.h"
38 #include "r300_fs.h"
39 #include "r300_vs.h"
40
41 /* r300_state: Functions used to intialize state context by translating
42 * Gallium state objects into semi-native r300 state objects. */
43
44 /* Create a new blend state based on the CSO blend state.
45 *
46 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
47 static void* r300_create_blend_state(struct pipe_context* pipe,
48 const struct pipe_blend_state* state)
49 {
50 struct r300_blend_state* blend = CALLOC_STRUCT(r300_blend_state);
51
52 if (state->blend_enable)
53 {
54 unsigned eqRGB = state->rgb_func;
55 unsigned srcRGB = state->rgb_src_factor;
56 unsigned dstRGB = state->rgb_dst_factor;
57
58 unsigned eqA = state->alpha_func;
59 unsigned srcA = state->alpha_src_factor;
60 unsigned dstA = state->alpha_dst_factor;
61
62 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
63 * this is just the crappy D3D naming */
64 blend->blend_control = R300_ALPHA_BLEND_ENABLE |
65 r300_translate_blend_function(eqRGB) |
66 ( r300_translate_blend_factor(srcRGB) << R300_SRC_BLEND_SHIFT) |
67 ( r300_translate_blend_factor(dstRGB) << R300_DST_BLEND_SHIFT);
68
69 /* Optimization: some operations do not require the destination color.
70 *
71 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
72 * otherwise blending gives incorrect results. It seems to be
73 * a hardware bug. */
74 if (eqRGB == PIPE_BLEND_MIN || eqA == PIPE_BLEND_MIN ||
75 eqRGB == PIPE_BLEND_MAX || eqA == PIPE_BLEND_MAX ||
76 dstRGB != PIPE_BLENDFACTOR_ZERO ||
77 dstA != PIPE_BLENDFACTOR_ZERO ||
78 srcRGB == PIPE_BLENDFACTOR_DST_COLOR ||
79 srcRGB == PIPE_BLENDFACTOR_DST_ALPHA ||
80 srcRGB == PIPE_BLENDFACTOR_INV_DST_COLOR ||
81 srcRGB == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
82 srcA == PIPE_BLENDFACTOR_DST_COLOR ||
83 srcA == PIPE_BLENDFACTOR_DST_ALPHA ||
84 srcA == PIPE_BLENDFACTOR_INV_DST_COLOR ||
85 srcA == PIPE_BLENDFACTOR_INV_DST_ALPHA ||
86 srcRGB == PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE)
87 blend->blend_control |= R300_READ_ENABLE;
88
89 /* XXX implement the optimization with DISCARD_SRC_PIXELS*/
90 /* XXX implement the optimization with SRC_ALPHA_?_NO_READ */
91
92 /* separate alpha */
93 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB) {
94 blend->blend_control |= R300_SEPARATE_ALPHA_ENABLE;
95 blend->alpha_blend_control =
96 r300_translate_blend_function(eqA) |
97 (r300_translate_blend_factor(srcA) << R300_SRC_BLEND_SHIFT) |
98 (r300_translate_blend_factor(dstA) << R300_DST_BLEND_SHIFT);
99 }
100 }
101
102 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
103 if (state->logicop_enable) {
104 blend->rop = R300_RB3D_ROPCNTL_ROP_ENABLE |
105 (state->logicop_func) << R300_RB3D_ROPCNTL_ROP_SHIFT;
106 }
107
108 /* Color Channel Mask */
109 if (state->colormask & PIPE_MASK_R) {
110 blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_RED_MASK0;
111 }
112 if (state->colormask & PIPE_MASK_G) {
113 blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0;
114 }
115 if (state->colormask & PIPE_MASK_B) {
116 blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0;
117 }
118 if (state->colormask & PIPE_MASK_A) {
119 blend->color_channel_mask |= RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0;
120 }
121
122 if (state->dither) {
123 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
124 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
125 }
126
127 return (void*)blend;
128 }
129
130 /* Bind blend state. */
131 static void r300_bind_blend_state(struct pipe_context* pipe,
132 void* state)
133 {
134 struct r300_context* r300 = r300_context(pipe);
135
136 r300->blend_state = (struct r300_blend_state*)state;
137 r300->dirty_state |= R300_NEW_BLEND;
138 }
139
140 /* Free blend state. */
141 static void r300_delete_blend_state(struct pipe_context* pipe,
142 void* state)
143 {
144 FREE(state);
145 }
146
147 /* Convert float to 10bit integer */
148 static unsigned float_to_fixed10(float f)
149 {
150 return CLAMP((unsigned)(f * 1023.9f), 0, 1023);
151 }
152
153 /* Set blend color.
154 * Setup both R300 and R500 registers, figure out later which one to write. */
155 static void r300_set_blend_color(struct pipe_context* pipe,
156 const struct pipe_blend_color* color)
157 {
158 struct r300_context* r300 = r300_context(pipe);
159 union util_color uc;
160
161 util_pack_color(color->color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
162 r300->blend_color_state->blend_color = uc.ui;
163
164 /* XXX if FP16 blending is enabled, we should use the FP16 format */
165 r300->blend_color_state->blend_color_red_alpha =
166 float_to_fixed10(color->color[0]) |
167 (float_to_fixed10(color->color[3]) << 16);
168 r300->blend_color_state->blend_color_green_blue =
169 float_to_fixed10(color->color[2]) |
170 (float_to_fixed10(color->color[1]) << 16);
171
172 r300->dirty_state |= R300_NEW_BLEND_COLOR;
173 }
174
175 static void r300_set_clip_state(struct pipe_context* pipe,
176 const struct pipe_clip_state* state)
177 {
178 struct r300_context* r300 = r300_context(pipe);
179
180 if (r300_screen(pipe->screen)->caps->has_tcl) {
181 r300->clip_state = *state;
182 r300->dirty_state |= R300_NEW_CLIP;
183 } else {
184 draw_flush(r300->draw);
185 draw_set_clip_state(r300->draw, state);
186 }
187 }
188
189 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
190 *
191 * This contains the depth buffer, stencil buffer, alpha test, and such.
192 * On the Radeon, depth and stencil buffer setup are intertwined, which is
193 * the reason for some of the strange-looking assignments across registers. */
194 static void*
195 r300_create_dsa_state(struct pipe_context* pipe,
196 const struct pipe_depth_stencil_alpha_state* state)
197 {
198 struct r300_capabilities *caps =
199 r300_screen(r300_context(pipe)->context.screen)->caps;
200 struct r300_dsa_state* dsa = CALLOC_STRUCT(r300_dsa_state);
201
202 /* Depth test setup. */
203 if (state->depth.enabled) {
204 dsa->z_buffer_control |= R300_Z_ENABLE;
205
206 if (state->depth.writemask) {
207 dsa->z_buffer_control |= R300_Z_WRITE_ENABLE;
208 }
209
210 dsa->z_stencil_control |=
211 (r300_translate_depth_stencil_function(state->depth.func) <<
212 R300_Z_FUNC_SHIFT);
213 }
214
215 /* Stencil buffer setup. */
216 if (state->stencil[0].enabled) {
217 dsa->z_buffer_control |= R300_STENCIL_ENABLE;
218 dsa->z_stencil_control |=
219 (r300_translate_depth_stencil_function(state->stencil[0].func) <<
220 R300_S_FRONT_FUNC_SHIFT) |
221 (r300_translate_stencil_op(state->stencil[0].fail_op) <<
222 R300_S_FRONT_SFAIL_OP_SHIFT) |
223 (r300_translate_stencil_op(state->stencil[0].zpass_op) <<
224 R300_S_FRONT_ZPASS_OP_SHIFT) |
225 (r300_translate_stencil_op(state->stencil[0].zfail_op) <<
226 R300_S_FRONT_ZFAIL_OP_SHIFT);
227
228 dsa->stencil_ref_mask = (state->stencil[0].ref_value) |
229 (state->stencil[0].valuemask << R300_STENCILMASK_SHIFT) |
230 (state->stencil[0].writemask << R300_STENCILWRITEMASK_SHIFT);
231
232 if (state->stencil[1].enabled) {
233 dsa->z_buffer_control |= R300_STENCIL_FRONT_BACK;
234 dsa->z_stencil_control |=
235 (r300_translate_depth_stencil_function(state->stencil[1].func) <<
236 R300_S_BACK_FUNC_SHIFT) |
237 (r300_translate_stencil_op(state->stencil[1].fail_op) <<
238 R300_S_BACK_SFAIL_OP_SHIFT) |
239 (r300_translate_stencil_op(state->stencil[1].zpass_op) <<
240 R300_S_BACK_ZPASS_OP_SHIFT) |
241 (r300_translate_stencil_op(state->stencil[1].zfail_op) <<
242 R300_S_BACK_ZFAIL_OP_SHIFT);
243
244 /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
245 if (caps->is_r500)
246 {
247 dsa->z_buffer_control |= R500_STENCIL_REFMASK_FRONT_BACK;
248 dsa->stencil_ref_bf = (state->stencil[1].ref_value) |
249 (state->stencil[1].valuemask <<
250 R300_STENCILMASK_SHIFT) |
251 (state->stencil[1].writemask <<
252 R300_STENCILWRITEMASK_SHIFT);
253 }
254 }
255 }
256
257 /* Alpha test setup. */
258 if (state->alpha.enabled) {
259 dsa->alpha_function =
260 r300_translate_alpha_function(state->alpha.func) |
261 R300_FG_ALPHA_FUNC_ENABLE;
262
263 /* XXX figure out why emitting 10bit alpha ref causes CS to dump */
264 /* always use 8bit alpha ref */
265 dsa->alpha_function |= float_to_ubyte(state->alpha.ref_value);
266
267 if (caps->is_r500)
268 dsa->alpha_function |= R500_FG_ALPHA_FUNC_8BIT;
269 }
270
271 return (void*)dsa;
272 }
273
274 /* Bind DSA state. */
275 static void r300_bind_dsa_state(struct pipe_context* pipe,
276 void* state)
277 {
278 struct r300_context* r300 = r300_context(pipe);
279
280 r300->dsa_state = (struct r300_dsa_state*)state;
281 r300->dirty_state |= R300_NEW_DSA;
282 }
283
284 /* Free DSA state. */
285 static void r300_delete_dsa_state(struct pipe_context* pipe,
286 void* state)
287 {
288 FREE(state);
289 }
290
291 static void r300_set_scissor_regs(const struct pipe_scissor_state* state,
292 struct r300_scissor_regs *scissor,
293 boolean is_r500)
294 {
295 if (is_r500) {
296 scissor->top_left =
297 (state->minx << R300_SCISSORS_X_SHIFT) |
298 (state->miny << R300_SCISSORS_Y_SHIFT);
299 scissor->bottom_right =
300 ((state->maxx - 1) << R300_SCISSORS_X_SHIFT) |
301 ((state->maxy - 1) << R300_SCISSORS_Y_SHIFT);
302 } else {
303 /* Offset of 1440 in non-R500 chipsets. */
304 scissor->top_left =
305 ((state->minx + 1440) << R300_SCISSORS_X_SHIFT) |
306 ((state->miny + 1440) << R300_SCISSORS_Y_SHIFT);
307 scissor->bottom_right =
308 (((state->maxx - 1) + 1440) << R300_SCISSORS_X_SHIFT) |
309 (((state->maxy - 1) + 1440) << R300_SCISSORS_Y_SHIFT);
310 }
311
312 scissor->empty_area = state->minx >= state->maxx ||
313 state->miny >= state->maxy;
314 }
315
316 static void
317 r300_set_framebuffer_state(struct pipe_context* pipe,
318 const struct pipe_framebuffer_state* state)
319 {
320 struct r300_context* r300 = r300_context(pipe);
321 struct pipe_scissor_state scissor;
322
323 if (r300->draw) {
324 draw_flush(r300->draw);
325 }
326
327 r300->framebuffer_state = *state;
328
329 scissor.minx = scissor.miny = 0;
330 scissor.maxx = state->width;
331 scissor.maxy = state->height;
332 r300_set_scissor_regs(&scissor, &r300->scissor_state->framebuffer,
333 r300_screen(r300->context.screen)->caps->is_r500);
334
335 /* Don't rely on the order of states being set for the first time. */
336 if (!r300->rs_state || !r300->rs_state->rs.scissor) {
337 r300->dirty_state |= R300_NEW_SCISSOR;
338 }
339 r300->dirty_state |= R300_NEW_FRAMEBUFFERS;
340 r300->dirty_state |= R300_NEW_BLEND;
341 }
342
343 /* Create fragment shader state. */
344 static void* r300_create_fs_state(struct pipe_context* pipe,
345 const struct pipe_shader_state* shader)
346 {
347 struct r300_fragment_shader* fs = NULL;
348
349 fs = (struct r300_fragment_shader*)CALLOC_STRUCT(r300_fragment_shader);
350
351 /* Copy state directly into shader. */
352 fs->state = *shader;
353 fs->state.tokens = tgsi_dup_tokens(shader->tokens);
354
355 tgsi_scan_shader(shader->tokens, &fs->info);
356 r300_shader_read_fs_inputs(&fs->info, &fs->inputs);
357
358 return (void*)fs;
359 }
360
361 /* Bind fragment shader state. */
362 static void r300_bind_fs_state(struct pipe_context* pipe, void* shader)
363 {
364 struct r300_context* r300 = r300_context(pipe);
365 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
366
367 if (fs == NULL) {
368 r300->fs = NULL;
369 return;
370 }
371
372 r300->fs = fs;
373 r300_pick_fragment_shader(r300);
374
375 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER | R300_NEW_FRAGMENT_SHADER_CONSTANTS;
376 }
377
378 /* Delete fragment shader state. */
379 static void r300_delete_fs_state(struct pipe_context* pipe, void* shader)
380 {
381 struct r300_fragment_shader* fs = (struct r300_fragment_shader*)shader;
382 struct r300_fragment_shader_code *tmp, *ptr = fs->first;
383
384 while (ptr) {
385 tmp = ptr;
386 ptr = ptr->next;
387 rc_constants_destroy(&tmp->code.constants);
388 FREE(tmp);
389 }
390 FREE((void*)fs->state.tokens);
391 FREE(shader);
392 }
393
394 static void r300_set_polygon_stipple(struct pipe_context* pipe,
395 const struct pipe_poly_stipple* state)
396 {
397 /* XXX no idea how to set this up, but not terribly important */
398 }
399
400 /* Create a new rasterizer state based on the CSO rasterizer state.
401 *
402 * This is a very large chunk of state, and covers most of the graphics
403 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
404 *
405 * In a not entirely unironic sidenote, this state has nearly nothing to do
406 * with the actual block on the Radeon called the rasterizer (RS). */
407 static void* r300_create_rs_state(struct pipe_context* pipe,
408 const struct pipe_rasterizer_state* state)
409 {
410 struct r300_rs_state* rs = CALLOC_STRUCT(r300_rs_state);
411
412 /* Copy rasterizer state for Draw. */
413 rs->rs = *state;
414
415 rs->enable_vte = !state->bypass_vs_clip_and_viewport;
416
417 #ifdef PIPE_ARCH_LITTLE_ENDIAN
418 rs->vap_control_status = R300_VC_NO_SWAP;
419 #else
420 rs->vap_control_status = R300_VC_32BIT_SWAP;
421 #endif
422
423 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
424 * Else, enable HW TCL and force Draw's TCL off. */
425 if (state->bypass_vs_clip_and_viewport ||
426 !r300_screen(pipe->screen)->caps->has_tcl) {
427 rs->vap_control_status |= R300_VAP_TCL_BYPASS;
428 }
429
430 rs->point_size = pack_float_16_6x(state->point_size) |
431 (pack_float_16_6x(state->point_size) << R300_POINTSIZE_X_SHIFT);
432
433 rs->point_minmax =
434 ((int)(state->point_size_min * 6.0) <<
435 R300_GA_POINT_MINMAX_MIN_SHIFT) |
436 ((int)(state->point_size_max * 6.0) <<
437 R300_GA_POINT_MINMAX_MAX_SHIFT);
438
439 rs->line_control = pack_float_16_6x(state->line_width) |
440 R300_GA_LINE_CNTL_END_TYPE_COMP;
441
442 /* XXX I think there is something wrong with the polygon mode,
443 * XXX re-test when r300g is in a better shape */
444
445 /* Enable polygon mode */
446 if (state->fill_cw != PIPE_POLYGON_MODE_FILL ||
447 state->fill_ccw != PIPE_POLYGON_MODE_FILL) {
448 rs->polygon_mode = R300_GA_POLY_MODE_DUAL;
449 }
450
451 /* Radeons don't think in "CW/CCW", they think in "front/back". */
452 if (state->front_winding == PIPE_WINDING_CW) {
453 rs->cull_mode = R300_FRONT_FACE_CW;
454
455 /* Polygon offset */
456 if (state->offset_cw) {
457 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
458 }
459 if (state->offset_ccw) {
460 rs->polygon_offset_enable |= R300_BACK_ENABLE;
461 }
462
463 /* Polygon mode */
464 if (rs->polygon_mode) {
465 rs->polygon_mode |=
466 r300_translate_polygon_mode_front(state->fill_cw);
467 rs->polygon_mode |=
468 r300_translate_polygon_mode_back(state->fill_ccw);
469 }
470 } else {
471 rs->cull_mode = R300_FRONT_FACE_CCW;
472
473 /* Polygon offset */
474 if (state->offset_ccw) {
475 rs->polygon_offset_enable |= R300_FRONT_ENABLE;
476 }
477 if (state->offset_cw) {
478 rs->polygon_offset_enable |= R300_BACK_ENABLE;
479 }
480
481 /* Polygon mode */
482 if (rs->polygon_mode) {
483 rs->polygon_mode |=
484 r300_translate_polygon_mode_front(state->fill_ccw);
485 rs->polygon_mode |=
486 r300_translate_polygon_mode_back(state->fill_cw);
487 }
488 }
489 if (state->front_winding & state->cull_mode) {
490 rs->cull_mode |= R300_CULL_FRONT;
491 }
492 if (~(state->front_winding) & state->cull_mode) {
493 rs->cull_mode |= R300_CULL_BACK;
494 }
495
496 if (rs->polygon_offset_enable) {
497 rs->depth_offset_front = rs->depth_offset_back =
498 fui(state->offset_units);
499 rs->depth_scale_front = rs->depth_scale_back =
500 fui(state->offset_scale);
501 }
502
503 if (state->line_stipple_enable) {
504 rs->line_stipple_config =
505 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE |
506 (fui((float)state->line_stipple_factor) &
507 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
508 /* XXX this might need to be scaled up */
509 rs->line_stipple_value = state->line_stipple_pattern;
510 }
511
512 if (state->flatshade) {
513 rs->color_control = R300_SHADE_MODEL_FLAT;
514 } else {
515 rs->color_control = R300_SHADE_MODEL_SMOOTH;
516 }
517
518 return (void*)rs;
519 }
520
521 /* Bind rasterizer state. */
522 static void r300_bind_rs_state(struct pipe_context* pipe, void* state)
523 {
524 struct r300_context* r300 = r300_context(pipe);
525 struct r300_rs_state* rs = (struct r300_rs_state*)state;
526
527 if (r300->draw) {
528 draw_flush(r300->draw);
529 draw_set_rasterizer_state(r300->draw, &rs->rs);
530 }
531
532 r300->rs_state = rs;
533 /* XXX Clean these up when we move to atom emits */
534 r300->dirty_state |= R300_NEW_RASTERIZER;
535 r300->dirty_state |= R300_NEW_RS_BLOCK;
536 r300->dirty_state |= R300_NEW_SCISSOR;
537 r300->dirty_state |= R300_NEW_VIEWPORT;
538 }
539
540 /* Free rasterizer state. */
541 static void r300_delete_rs_state(struct pipe_context* pipe, void* state)
542 {
543 FREE(state);
544 }
545
546 static void*
547 r300_create_sampler_state(struct pipe_context* pipe,
548 const struct pipe_sampler_state* state)
549 {
550 struct r300_context* r300 = r300_context(pipe);
551 struct r300_sampler_state* sampler = CALLOC_STRUCT(r300_sampler_state);
552 int lod_bias;
553 union util_color uc;
554
555 sampler->state = *state;
556
557 sampler->filter0 |=
558 (r300_translate_wrap(state->wrap_s) << R300_TX_WRAP_S_SHIFT) |
559 (r300_translate_wrap(state->wrap_t) << R300_TX_WRAP_T_SHIFT) |
560 (r300_translate_wrap(state->wrap_r) << R300_TX_WRAP_R_SHIFT);
561
562 sampler->filter0 |= r300_translate_tex_filters(state->min_img_filter,
563 state->mag_img_filter,
564 state->min_mip_filter,
565 state->max_anisotropy > 1.0);
566
567 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
568 /* We must pass these to the emit function to clamp them properly. */
569 sampler->min_lod = MAX2((unsigned)state->min_lod, 0);
570 sampler->max_lod = MAX2((unsigned)ceilf(state->max_lod), 0);
571
572 lod_bias = CLAMP((int)(state->lod_bias * 32), -(1 << 9), (1 << 9) - 1);
573
574 sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
575
576 sampler->filter1 |= r300_anisotropy(state->max_anisotropy);
577
578 util_pack_color(state->border_color, PIPE_FORMAT_A8R8G8B8_UNORM, &uc);
579 sampler->border_color = uc.ui;
580
581 /* R500-specific fixups and optimizations */
582 if (r300_screen(r300->context.screen)->caps->is_r500) {
583 sampler->filter1 |= R500_BORDER_FIX;
584 }
585
586 return (void*)sampler;
587 }
588
589 static void r300_bind_sampler_states(struct pipe_context* pipe,
590 unsigned count,
591 void** states)
592 {
593 struct r300_context* r300 = r300_context(pipe);
594 int i;
595
596 if (count > 8) {
597 return;
598 }
599
600 for (i = 0; i < count; i++) {
601 if (r300->sampler_states[i] != states[i]) {
602 r300->sampler_states[i] = (struct r300_sampler_state*)states[i];
603 r300->dirty_state |= (R300_NEW_SAMPLER << i);
604 }
605 }
606
607 r300->sampler_count = count;
608
609 /* Pick a fragment shader based on the texture compare state. */
610 if (r300->fs && (r300->dirty_state & R300_ANY_NEW_SAMPLERS)) {
611 if (r300_pick_fragment_shader(r300)) {
612 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER |
613 R300_NEW_FRAGMENT_SHADER_CONSTANTS;
614 }
615 }
616 }
617
618 static void r300_lacks_vertex_textures(struct pipe_context* pipe,
619 unsigned count,
620 void** states)
621 {
622 }
623
624 static void r300_delete_sampler_state(struct pipe_context* pipe, void* state)
625 {
626 FREE(state);
627 }
628
629 static void r300_set_sampler_textures(struct pipe_context* pipe,
630 unsigned count,
631 struct pipe_texture** texture)
632 {
633 struct r300_context* r300 = r300_context(pipe);
634 boolean is_r500 = r300_screen(r300->context.screen)->caps->is_r500;
635 int i;
636
637 /* XXX magic num */
638 if (count > 8) {
639 return;
640 }
641
642 for (i = 0; i < count; i++) {
643 if (r300->textures[i] != (struct r300_texture*)texture[i]) {
644 pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
645 texture[i]);
646 r300->dirty_state |= (R300_NEW_TEXTURE << i);
647
648 /* R300-specific - set the texrect factor in a fragment shader */
649 if (!is_r500 && r300->textures[i]->is_npot) {
650 /* XXX It would be nice to re-emit just 1 constant,
651 * XXX not all of them */
652 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
653 }
654 }
655 }
656
657 for (i = count; i < 8; i++) {
658 if (r300->textures[i]) {
659 pipe_texture_reference((struct pipe_texture**)&r300->textures[i],
660 NULL);
661 r300->dirty_state |= (R300_NEW_TEXTURE << i);
662 }
663 }
664
665 r300->texture_count = count;
666 }
667
668 static void r300_set_scissor_state(struct pipe_context* pipe,
669 const struct pipe_scissor_state* state)
670 {
671 struct r300_context* r300 = r300_context(pipe);
672
673 r300_set_scissor_regs(state, &r300->scissor_state->scissor,
674 r300_screen(r300->context.screen)->caps->is_r500);
675
676 /* Don't rely on the order of states being set for the first time. */
677 if (!r300->rs_state || r300->rs_state->rs.scissor) {
678 r300->dirty_state |= R300_NEW_SCISSOR;
679 }
680 }
681
682 static void r300_set_viewport_state(struct pipe_context* pipe,
683 const struct pipe_viewport_state* state)
684 {
685 struct r300_context* r300 = r300_context(pipe);
686
687 /* Do the transform in HW. */
688 r300->viewport_state->vte_control = R300_VTX_W0_FMT;
689
690 if (state->scale[0] != 1.0f) {
691 r300->viewport_state->xscale = state->scale[0];
692 r300->viewport_state->vte_control |= R300_VPORT_X_SCALE_ENA;
693 }
694 if (state->scale[1] != 1.0f) {
695 r300->viewport_state->yscale = state->scale[1];
696 r300->viewport_state->vte_control |= R300_VPORT_Y_SCALE_ENA;
697 }
698 if (state->scale[2] != 1.0f) {
699 r300->viewport_state->zscale = state->scale[2];
700 r300->viewport_state->vte_control |= R300_VPORT_Z_SCALE_ENA;
701 }
702 if (state->translate[0] != 0.0f) {
703 r300->viewport_state->xoffset = state->translate[0];
704 r300->viewport_state->vte_control |= R300_VPORT_X_OFFSET_ENA;
705 }
706 if (state->translate[1] != 0.0f) {
707 r300->viewport_state->yoffset = state->translate[1];
708 r300->viewport_state->vte_control |= R300_VPORT_Y_OFFSET_ENA;
709 }
710 if (state->translate[2] != 0.0f) {
711 r300->viewport_state->zoffset = state->translate[2];
712 r300->viewport_state->vte_control |= R300_VPORT_Z_OFFSET_ENA;
713 }
714
715 r300->dirty_state |= R300_NEW_VIEWPORT;
716 }
717
718 static void r300_set_vertex_buffers(struct pipe_context* pipe,
719 unsigned count,
720 const struct pipe_vertex_buffer* buffers)
721 {
722 struct r300_context* r300 = r300_context(pipe);
723
724 memcpy(r300->vertex_buffer, buffers,
725 sizeof(struct pipe_vertex_buffer) * count);
726 r300->vertex_buffer_count = count;
727
728 if (r300->draw) {
729 draw_flush(r300->draw);
730 draw_set_vertex_buffers(r300->draw, count, buffers);
731 }
732
733 r300->dirty_state |= R300_NEW_VERTEX_FORMAT;
734 }
735
736 static void r300_set_vertex_elements(struct pipe_context* pipe,
737 unsigned count,
738 const struct pipe_vertex_element* elements)
739 {
740 struct r300_context* r300 = r300_context(pipe);
741
742 memcpy(r300->vertex_element,
743 elements,
744 sizeof(struct pipe_vertex_element) * count);
745 r300->vertex_element_count = count;
746
747 if (r300->draw) {
748 draw_flush(r300->draw);
749 draw_set_vertex_elements(r300->draw, count, elements);
750 }
751 }
752
753 static void* r300_create_vs_state(struct pipe_context* pipe,
754 const struct pipe_shader_state* shader)
755 {
756 struct r300_context* r300 = r300_context(pipe);
757
758 if (r300_screen(pipe->screen)->caps->has_tcl) {
759 struct r300_vertex_shader* vs = CALLOC_STRUCT(r300_vertex_shader);
760 /* Copy state directly into shader. */
761 vs->state = *shader;
762 vs->state.tokens = tgsi_dup_tokens(shader->tokens);
763
764 tgsi_scan_shader(shader->tokens, &vs->info);
765
766 return (void*)vs;
767 } else {
768 return draw_create_vertex_shader(r300->draw, shader);
769 }
770 }
771
772 static void r300_bind_vs_state(struct pipe_context* pipe, void* shader)
773 {
774 struct r300_context* r300 = r300_context(pipe);
775
776 if (r300_screen(pipe->screen)->caps->has_tcl) {
777 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
778
779 if (vs == NULL) {
780 r300->vs = NULL;
781 return;
782 } else if (!vs->translated) {
783 r300_translate_vertex_shader(r300, vs);
784 }
785
786 r300->vs = vs;
787 r300->dirty_state |= R300_NEW_VERTEX_SHADER | R300_NEW_VERTEX_SHADER_CONSTANTS;
788 } else {
789 draw_flush(r300->draw);
790 draw_bind_vertex_shader(r300->draw,
791 (struct draw_vertex_shader*)shader);
792 }
793 }
794
795 static void r300_delete_vs_state(struct pipe_context* pipe, void* shader)
796 {
797 struct r300_context* r300 = r300_context(pipe);
798
799 if (r300_screen(pipe->screen)->caps->has_tcl) {
800 struct r300_vertex_shader* vs = (struct r300_vertex_shader*)shader;
801
802 rc_constants_destroy(&vs->code.constants);
803 FREE((void*)vs->state.tokens);
804 FREE(shader);
805 } else {
806 draw_delete_vertex_shader(r300->draw,
807 (struct draw_vertex_shader*)shader);
808 }
809 }
810
811 static void r300_set_constant_buffer(struct pipe_context *pipe,
812 uint shader, uint index,
813 const struct pipe_constant_buffer *buf)
814 {
815 struct r300_context* r300 = r300_context(pipe);
816 void *mapped;
817
818 if (buf == NULL || buf->buffer->size == 0 ||
819 (mapped = pipe_buffer_map(pipe->screen, buf->buffer, PIPE_BUFFER_USAGE_CPU_READ)) == NULL)
820 {
821 r300->shader_constants[shader].count = 0;
822 return;
823 }
824
825 assert((buf->buffer->size % 4 * sizeof(float)) == 0);
826 memcpy(r300->shader_constants[shader].constants, mapped, buf->buffer->size);
827 r300->shader_constants[shader].count = buf->buffer->size / (4 * sizeof(float));
828 pipe_buffer_unmap(pipe->screen, buf->buffer);
829
830 if (shader == PIPE_SHADER_VERTEX)
831 r300->dirty_state |= R300_NEW_VERTEX_SHADER_CONSTANTS;
832 else if (shader == PIPE_SHADER_FRAGMENT)
833 r300->dirty_state |= R300_NEW_FRAGMENT_SHADER_CONSTANTS;
834 }
835
836 void r300_init_state_functions(struct r300_context* r300)
837 {
838 r300->context.create_blend_state = r300_create_blend_state;
839 r300->context.bind_blend_state = r300_bind_blend_state;
840 r300->context.delete_blend_state = r300_delete_blend_state;
841
842 r300->context.set_blend_color = r300_set_blend_color;
843
844 r300->context.set_clip_state = r300_set_clip_state;
845
846 r300->context.set_constant_buffer = r300_set_constant_buffer;
847
848 r300->context.create_depth_stencil_alpha_state = r300_create_dsa_state;
849 r300->context.bind_depth_stencil_alpha_state = r300_bind_dsa_state;
850 r300->context.delete_depth_stencil_alpha_state = r300_delete_dsa_state;
851
852 r300->context.set_framebuffer_state = r300_set_framebuffer_state;
853
854 r300->context.create_fs_state = r300_create_fs_state;
855 r300->context.bind_fs_state = r300_bind_fs_state;
856 r300->context.delete_fs_state = r300_delete_fs_state;
857
858 r300->context.set_polygon_stipple = r300_set_polygon_stipple;
859
860 r300->context.create_rasterizer_state = r300_create_rs_state;
861 r300->context.bind_rasterizer_state = r300_bind_rs_state;
862 r300->context.delete_rasterizer_state = r300_delete_rs_state;
863
864 r300->context.create_sampler_state = r300_create_sampler_state;
865 r300->context.bind_fragment_sampler_states = r300_bind_sampler_states;
866 r300->context.bind_vertex_sampler_states = r300_lacks_vertex_textures;
867 r300->context.delete_sampler_state = r300_delete_sampler_state;
868
869 r300->context.set_fragment_sampler_textures = r300_set_sampler_textures;
870
871 r300->context.set_scissor_state = r300_set_scissor_state;
872
873 r300->context.set_viewport_state = r300_set_viewport_state;
874
875 r300->context.set_vertex_buffers = r300_set_vertex_buffers;
876 r300->context.set_vertex_elements = r300_set_vertex_elements;
877
878 r300->context.create_vs_state = r300_create_vs_state;
879 r300->context.bind_vs_state = r300_bind_vs_state;
880 r300->context.delete_vs_state = r300_delete_vs_state;
881 }