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