2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
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:
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
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. */
23 #include "draw/draw_context.h"
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
27 #include "util/u_pack_color.h"
29 #include "tgsi/tgsi_parse.h"
31 #include "pipe/p_config.h"
32 #include "pipe/internal/p_winsys_screen.h"
34 #include "r300_context.h"
36 #include "r300_screen.h"
37 #include "r300_state_inlines.h"
41 /* r300_state: Functions used to intialize state context by translating
42 * Gallium state objects into semi-native r300 state objects. */
44 /* Create a new blend state based on the CSO blend state.
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
)
50 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
52 if (state
->blend_enable
)
54 unsigned eqRGB
= state
->rgb_func
;
55 unsigned srcRGB
= state
->rgb_src_factor
;
56 unsigned dstRGB
= state
->rgb_dst_factor
;
58 unsigned eqA
= state
->alpha_func
;
59 unsigned srcA
= state
->alpha_src_factor
;
60 unsigned dstA
= state
->alpha_dst_factor
;
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
);
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
;
84 /* XXX implement the optimization with DISCARD_SRC_PIXELS*/
85 /* XXX implement the optimization with SRC_ALPHA_?_NO_READ */
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
);
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
;
103 /* Color Channel Mask */
104 if (state
->colormask
& PIPE_MASK_R
) {
105 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_RED_MASK0
;
107 if (state
->colormask
& PIPE_MASK_G
) {
108 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0
;
110 if (state
->colormask
& PIPE_MASK_B
) {
111 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0
;
113 if (state
->colormask
& PIPE_MASK_A
) {
114 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0
;
118 blend
->dither
= R300_RB3D_DITHER_CTL_DITHER_MODE_LUT
|
119 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT
;
125 /* Bind blend state. */
126 static void r300_bind_blend_state(struct pipe_context
* pipe
,
129 struct r300_context
* r300
= r300_context(pipe
);
131 r300
->blend_state
= (struct r300_blend_state
*)state
;
132 r300
->dirty_state
|= R300_NEW_BLEND
;
135 /* Free blend state. */
136 static void r300_delete_blend_state(struct pipe_context
* pipe
,
142 /* Convert float to 10bit integer */
143 static unsigned float_to_fixed10(float f
)
145 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
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
)
153 struct r300_context
* r300
= r300_context(pipe
);
156 util_pack_color(color
->color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
157 r300
->blend_color_state
->blend_color
= uc
.ui
;
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);
167 r300
->dirty_state
|= R300_NEW_BLEND_COLOR
;
170 static void r300_set_clip_state(struct pipe_context
* pipe
,
171 const struct pipe_clip_state
* state
)
173 struct r300_context
* r300
= r300_context(pipe
);
175 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
176 r300
->clip_state
= *state
;
177 r300
->dirty_state
|= R300_NEW_CLIP
;
179 draw_flush(r300
->draw
);
180 draw_set_clip_state(r300
->draw
, state
);
184 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
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. */
190 r300_create_dsa_state(struct pipe_context
* pipe
,
191 const struct pipe_depth_stencil_alpha_state
* state
)
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
);
197 /* Depth test setup. */
198 if (state
->depth
.enabled
) {
199 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
201 if (state
->depth
.writemask
) {
202 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
205 dsa
->z_stencil_control
|=
206 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
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
);
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
);
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
);
239 /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
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
);
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
;
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
);
263 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
269 /* Bind DSA state. */
270 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
273 struct r300_context
* r300
= r300_context(pipe
);
275 r300
->dsa_state
= (struct r300_dsa_state
*)state
;
276 r300
->dirty_state
|= R300_NEW_DSA
;
279 /* Free DSA state. */
280 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
286 static void r300_set_edgeflags(struct pipe_context
* pipe
,
287 const unsigned* bitfield
)
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 */
293 static void r300_set_scissor_regs(const struct pipe_scissor_state
* state
,
294 struct r300_scissor_regs
*scissor
,
299 (state
->minx
<< R300_SCISSORS_X_SHIFT
) |
300 (state
->miny
<< R300_SCISSORS_Y_SHIFT
);
301 scissor
->bottom_right
=
302 ((state
->maxx
- 1) << R300_SCISSORS_X_SHIFT
) |
303 ((state
->maxy
- 1) << R300_SCISSORS_Y_SHIFT
);
305 /* Offset of 1440 in non-R500 chipsets. */
307 ((state
->minx
+ 1440) << R300_SCISSORS_X_SHIFT
) |
308 ((state
->miny
+ 1440) << R300_SCISSORS_Y_SHIFT
);
309 scissor
->bottom_right
=
310 (((state
->maxx
- 1) + 1440) << R300_SCISSORS_X_SHIFT
) |
311 (((state
->maxy
- 1) + 1440) << R300_SCISSORS_Y_SHIFT
);
314 scissor
->empty_area
= state
->minx
>= state
->maxx
||
315 state
->miny
>= state
->maxy
;
319 r300_set_framebuffer_state(struct pipe_context
* pipe
,
320 const struct pipe_framebuffer_state
* state
)
322 struct r300_context
* r300
= r300_context(pipe
);
323 struct pipe_scissor_state scissor
;
326 draw_flush(r300
->draw
);
329 r300
->framebuffer_state
= *state
;
331 scissor
.minx
= scissor
.miny
= 0;
332 scissor
.maxx
= state
->width
;
333 scissor
.maxy
= state
->height
;
334 r300_set_scissor_regs(&scissor
, &r300
->scissor_state
->framebuffer
,
335 r300_screen(r300
->context
.screen
)->caps
->is_r500
);
337 /* Don't rely on the order of states being set for the first time. */
338 if (!r300
->rs_state
|| !r300
->rs_state
->rs
.scissor
) {
339 r300
->dirty_state
|= R300_NEW_SCISSOR
;
341 r300
->dirty_state
|= R300_NEW_FRAMEBUFFERS
;
342 r300
->dirty_state
|= R300_NEW_BLEND
;
345 /* Create fragment shader state. */
346 static void* r300_create_fs_state(struct pipe_context
* pipe
,
347 const struct pipe_shader_state
* shader
)
349 struct r300_fragment_shader
* fs
= NULL
;
351 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
353 /* Copy state directly into shader. */
355 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
357 tgsi_scan_shader(shader
->tokens
, &fs
->info
);
358 r300_shader_read_fs_inputs(&fs
->info
, &fs
->inputs
);
363 /* Bind fragment shader state. */
364 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
366 struct r300_context
* r300
= r300_context(pipe
);
367 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
375 r300_pick_fragment_shader(r300
);
377 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
| R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
380 /* Delete fragment shader state. */
381 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
383 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
384 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
389 rc_constants_destroy(&tmp
->code
.constants
);
392 FREE((void*)fs
->state
.tokens
);
396 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
397 const struct pipe_poly_stipple
* state
)
399 /* XXX no idea how to set this up, but not terribly important */
402 /* Create a new rasterizer state based on the CSO rasterizer state.
404 * This is a very large chunk of state, and covers most of the graphics
405 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
407 * In a not entirely unironic sidenote, this state has nearly nothing to do
408 * with the actual block on the Radeon called the rasterizer (RS). */
409 static void* r300_create_rs_state(struct pipe_context
* pipe
,
410 const struct pipe_rasterizer_state
* state
)
412 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
414 /* Copy rasterizer state for Draw. */
417 rs
->enable_vte
= !state
->bypass_vs_clip_and_viewport
;
419 #ifdef PIPE_ARCH_LITTLE_ENDIAN
420 rs
->vap_control_status
= R300_VC_NO_SWAP
;
422 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
425 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
426 * Else, enable HW TCL and force Draw's TCL off. */
427 if (state
->bypass_vs_clip_and_viewport
||
428 !r300_screen(pipe
->screen
)->caps
->has_tcl
) {
429 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
432 rs
->point_size
= pack_float_16_6x(state
->point_size
) |
433 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
436 ((int)(state
->point_size_min
* 6.0) <<
437 R300_GA_POINT_MINMAX_MIN_SHIFT
) |
438 ((int)(state
->point_size_max
* 6.0) <<
439 R300_GA_POINT_MINMAX_MAX_SHIFT
);
441 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
442 R300_GA_LINE_CNTL_END_TYPE_COMP
;
444 /* XXX I think there is something wrong with the polygon mode,
445 * XXX re-test when r300g is in a better shape */
447 /* Enable polygon mode */
448 if (state
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
449 state
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
) {
450 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
453 /* Radeons don't think in "CW/CCW", they think in "front/back". */
454 if (state
->front_winding
== PIPE_WINDING_CW
) {
455 rs
->cull_mode
= R300_FRONT_FACE_CW
;
458 if (state
->offset_cw
) {
459 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
461 if (state
->offset_ccw
) {
462 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
466 if (rs
->polygon_mode
) {
468 r300_translate_polygon_mode_front(state
->fill_cw
);
470 r300_translate_polygon_mode_back(state
->fill_ccw
);
473 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
476 if (state
->offset_ccw
) {
477 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
479 if (state
->offset_cw
) {
480 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
484 if (rs
->polygon_mode
) {
486 r300_translate_polygon_mode_front(state
->fill_ccw
);
488 r300_translate_polygon_mode_back(state
->fill_cw
);
491 if (state
->front_winding
& state
->cull_mode
) {
492 rs
->cull_mode
|= R300_CULL_FRONT
;
494 if (~(state
->front_winding
) & state
->cull_mode
) {
495 rs
->cull_mode
|= R300_CULL_BACK
;
498 if (rs
->polygon_offset_enable
) {
499 rs
->depth_offset_front
= rs
->depth_offset_back
=
500 fui(state
->offset_units
);
501 rs
->depth_scale_front
= rs
->depth_scale_back
=
502 fui(state
->offset_scale
);
505 if (state
->line_stipple_enable
) {
506 rs
->line_stipple_config
=
507 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
508 (fui((float)state
->line_stipple_factor
) &
509 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
510 /* XXX this might need to be scaled up */
511 rs
->line_stipple_value
= state
->line_stipple_pattern
;
514 if (state
->flatshade
) {
515 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
517 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
523 /* Bind rasterizer state. */
524 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
526 struct r300_context
* r300
= r300_context(pipe
);
527 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
530 draw_flush(r300
->draw
);
531 draw_set_rasterizer_state(r300
->draw
, &rs
->rs
);
535 /* XXX Clean these up when we move to atom emits */
536 r300
->dirty_state
|= R300_NEW_RASTERIZER
;
537 r300
->dirty_state
|= R300_NEW_RS_BLOCK
;
538 r300
->dirty_state
|= R300_NEW_SCISSOR
;
539 r300
->dirty_state
|= R300_NEW_VIEWPORT
;
542 /* Free rasterizer state. */
543 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
549 r300_create_sampler_state(struct pipe_context
* pipe
,
550 const struct pipe_sampler_state
* state
)
552 struct r300_context
* r300
= r300_context(pipe
);
553 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
557 sampler
->state
= *state
;
560 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
561 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
562 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
564 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
565 state
->mag_img_filter
,
566 state
->min_mip_filter
);
568 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
569 /* We must pass these to the emit function to clamp them properly. */
570 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
571 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
573 lod_bias
= CLAMP((int)(state
->lod_bias
* 32), -(1 << 9), (1 << 9) - 1);
575 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
577 sampler
->filter1
|= r300_anisotropy(state
->max_anisotropy
);
579 util_pack_color(state
->border_color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
580 sampler
->border_color
= uc
.ui
;
582 /* R500-specific fixups and optimizations */
583 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
584 sampler
->filter1
|= R500_BORDER_FIX
;
587 return (void*)sampler
;
590 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
594 struct r300_context
* r300
= r300_context(pipe
);
601 for (i
= 0; i
< count
; i
++) {
602 if (r300
->sampler_states
[i
] != states
[i
]) {
603 r300
->sampler_states
[i
] = (struct r300_sampler_state
*)states
[i
];
604 r300
->dirty_state
|= (R300_NEW_SAMPLER
<< i
);
608 r300
->sampler_count
= count
;
610 /* Pick a fragment shader based on the texture compare state. */
611 if (r300
->fs
&& (r300
->dirty_state
& R300_ANY_NEW_SAMPLERS
)) {
612 if (r300_pick_fragment_shader(r300
)) {
613 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
|
614 R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
619 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
625 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
630 static void r300_set_sampler_textures(struct pipe_context
* pipe
,
632 struct pipe_texture
** texture
)
634 struct r300_context
* r300
= r300_context(pipe
);
635 boolean is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
643 for (i
= 0; i
< count
; i
++) {
644 if (r300
->textures
[i
] != (struct r300_texture
*)texture
[i
]) {
645 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
647 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
649 /* R300-specific - set the texrect factor in a fragment shader */
650 if (!is_r500
&& r300
->textures
[i
]->is_npot
) {
651 /* XXX It would be nice to re-emit just 1 constant,
652 * XXX not all of them */
653 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
658 for (i
= count
; i
< 8; i
++) {
659 if (r300
->textures
[i
]) {
660 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
662 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
666 r300
->texture_count
= count
;
669 static void r300_set_scissor_state(struct pipe_context
* pipe
,
670 const struct pipe_scissor_state
* state
)
672 struct r300_context
* r300
= r300_context(pipe
);
674 r300_set_scissor_regs(state
, &r300
->scissor_state
->scissor
,
675 r300_screen(r300
->context
.screen
)->caps
->is_r500
);
677 /* Don't rely on the order of states being set for the first time. */
678 if (!r300
->rs_state
|| r300
->rs_state
->rs
.scissor
) {
679 r300
->dirty_state
|= R300_NEW_SCISSOR
;
683 static void r300_set_viewport_state(struct pipe_context
* pipe
,
684 const struct pipe_viewport_state
* state
)
686 struct r300_context
* r300
= r300_context(pipe
);
688 /* Do the transform in HW. */
689 r300
->viewport_state
->vte_control
= R300_VTX_W0_FMT
;
691 if (state
->scale
[0] != 1.0f
) {
692 r300
->viewport_state
->xscale
= state
->scale
[0];
693 r300
->viewport_state
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
695 if (state
->scale
[1] != 1.0f
) {
696 r300
->viewport_state
->yscale
= state
->scale
[1];
697 r300
->viewport_state
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
699 if (state
->scale
[2] != 1.0f
) {
700 r300
->viewport_state
->zscale
= state
->scale
[2];
701 r300
->viewport_state
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
703 if (state
->translate
[0] != 0.0f
) {
704 r300
->viewport_state
->xoffset
= state
->translate
[0];
705 r300
->viewport_state
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
707 if (state
->translate
[1] != 0.0f
) {
708 r300
->viewport_state
->yoffset
= state
->translate
[1];
709 r300
->viewport_state
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
711 if (state
->translate
[2] != 0.0f
) {
712 r300
->viewport_state
->zoffset
= state
->translate
[2];
713 r300
->viewport_state
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
716 r300
->dirty_state
|= R300_NEW_VIEWPORT
;
719 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
721 const struct pipe_vertex_buffer
* buffers
)
723 struct r300_context
* r300
= r300_context(pipe
);
725 memcpy(r300
->vertex_buffer
, buffers
,
726 sizeof(struct pipe_vertex_buffer
) * count
);
727 r300
->vertex_buffer_count
= count
;
730 draw_flush(r300
->draw
);
731 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
734 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
737 static void r300_set_vertex_elements(struct pipe_context
* pipe
,
739 const struct pipe_vertex_element
* elements
)
741 struct r300_context
* r300
= r300_context(pipe
);
743 memcpy(r300
->vertex_element
,
745 sizeof(struct pipe_vertex_element
) * count
);
746 r300
->vertex_element_count
= count
;
749 draw_flush(r300
->draw
);
750 draw_set_vertex_elements(r300
->draw
, count
, elements
);
754 static void* r300_create_vs_state(struct pipe_context
* pipe
,
755 const struct pipe_shader_state
* shader
)
757 struct r300_context
* r300
= r300_context(pipe
);
759 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
760 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
761 /* Copy state directly into shader. */
763 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
765 tgsi_scan_shader(shader
->tokens
, &vs
->info
);
769 return draw_create_vertex_shader(r300
->draw
, shader
);
773 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
775 struct r300_context
* r300
= r300_context(pipe
);
777 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
778 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
783 } else if (!vs
->translated
) {
784 r300_translate_vertex_shader(r300
, vs
);
788 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER
| R300_NEW_VERTEX_SHADER_CONSTANTS
;
790 draw_flush(r300
->draw
);
791 draw_bind_vertex_shader(r300
->draw
,
792 (struct draw_vertex_shader
*)shader
);
796 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
798 struct r300_context
* r300
= r300_context(pipe
);
800 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
801 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
803 rc_constants_destroy(&vs
->code
.constants
);
804 FREE((void*)vs
->state
.tokens
);
807 draw_delete_vertex_shader(r300
->draw
,
808 (struct draw_vertex_shader
*)shader
);
812 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
813 uint shader
, uint index
,
814 const struct pipe_constant_buffer
*buf
)
816 struct r300_context
* r300
= r300_context(pipe
);
819 if (buf
== NULL
|| buf
->buffer
->size
== 0 ||
820 (mapped
= pipe_buffer_map(pipe
->screen
, buf
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
)) == NULL
)
822 r300
->shader_constants
[shader
].count
= 0;
826 assert((buf
->buffer
->size
% 4 * sizeof(float)) == 0);
827 memcpy(r300
->shader_constants
[shader
].constants
, mapped
, buf
->buffer
->size
);
828 r300
->shader_constants
[shader
].count
= buf
->buffer
->size
/ (4 * sizeof(float));
829 pipe_buffer_unmap(pipe
->screen
, buf
->buffer
);
831 if (shader
== PIPE_SHADER_VERTEX
)
832 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER_CONSTANTS
;
833 else if (shader
== PIPE_SHADER_FRAGMENT
)
834 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
837 void r300_init_state_functions(struct r300_context
* r300
)
839 r300
->context
.create_blend_state
= r300_create_blend_state
;
840 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
841 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
843 r300
->context
.set_blend_color
= r300_set_blend_color
;
845 r300
->context
.set_clip_state
= r300_set_clip_state
;
847 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
849 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
850 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
851 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
853 r300
->context
.set_edgeflags
= r300_set_edgeflags
;
855 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
857 r300
->context
.create_fs_state
= r300_create_fs_state
;
858 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
859 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
861 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
863 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
864 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
865 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
867 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
868 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
869 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
870 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
872 r300
->context
.set_fragment_sampler_textures
= r300_set_sampler_textures
;
874 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
876 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
878 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
879 r300
->context
.set_vertex_elements
= r300_set_vertex_elements
;
881 r300
->context
.create_vs_state
= r300_create_vs_state
;
882 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
883 r300
->context
.delete_vs_state
= r300_delete_vs_state
;