2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
24 /* r300_emit: Functions for emitting state. */
26 #include "util/u_format.h"
27 #include "util/u_math.h"
28 #include "util/u_simple_list.h"
30 #include "r300_context.h"
32 #include "r300_emit.h"
34 #include "r300_screen.h"
35 #include "r300_screen_buffer.h"
36 #include "r300_state_inlines.h"
39 void r300_emit_blend_state(struct r300_context
* r300
,
40 unsigned size
, void* state
)
42 struct r300_blend_state
* blend
= (struct r300_blend_state
*)state
;
43 struct pipe_framebuffer_state
* fb
=
44 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
48 OUT_CS_REG(R300_RB3D_ROPCNTL
, blend
->rop
);
49 OUT_CS_REG_SEQ(R300_RB3D_CBLEND
, 3);
51 OUT_CS(blend
->blend_control
);
52 OUT_CS(blend
->alpha_blend_control
);
53 OUT_CS(blend
->color_channel_mask
);
58 /* XXX also disable fastfill here once it's supported */
60 OUT_CS_REG(R300_RB3D_DITHER_CTL
, blend
->dither
);
64 void r300_emit_blend_color_state(struct r300_context
* r300
,
65 unsigned size
, void* state
)
67 struct r300_blend_color_state
* bc
= (struct r300_blend_color_state
*)state
;
68 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
71 if (r300screen
->caps
->is_r500
) {
73 OUT_CS_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR
, 2);
74 OUT_CS(bc
->blend_color_red_alpha
);
75 OUT_CS(bc
->blend_color_green_blue
);
79 OUT_CS_REG(R300_RB3D_BLEND_COLOR
, bc
->blend_color
);
84 void r300_emit_clip_state(struct r300_context
* r300
,
85 unsigned size
, void* state
)
87 struct pipe_clip_state
* clip
= (struct pipe_clip_state
*)state
;
89 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
92 if (r300screen
->caps
->has_tcl
) {
94 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
95 (r300screen
->caps
->is_r500
?
96 R500_PVS_UCP_START
: R300_PVS_UCP_START
));
97 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, 6 * 4);
98 for (i
= 0; i
< 6; i
++) {
99 OUT_CS_32F(clip
->ucp
[i
][0]);
100 OUT_CS_32F(clip
->ucp
[i
][1]);
101 OUT_CS_32F(clip
->ucp
[i
][2]);
102 OUT_CS_32F(clip
->ucp
[i
][3]);
104 OUT_CS_REG(R300_VAP_CLIP_CNTL
, ((1 << clip
->nr
) - 1) |
105 R300_PS_UCP_MODE_CLIP_AS_TRIFAN
);
109 OUT_CS_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
115 void r300_emit_dsa_state(struct r300_context
* r300
, unsigned size
, void* state
)
117 struct r300_dsa_state
* dsa
= (struct r300_dsa_state
*)state
;
118 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
119 struct pipe_framebuffer_state
* fb
=
120 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
121 struct pipe_stencil_ref stencil_ref
= r300
->stencil_ref
;
125 OUT_CS_REG(R300_FG_ALPHA_FUNC
, dsa
->alpha_function
);
126 OUT_CS_REG_SEQ(R300_ZB_CNTL
, 3);
129 OUT_CS(dsa
->z_buffer_control
);
130 OUT_CS(dsa
->z_stencil_control
);
136 OUT_CS(dsa
->stencil_ref_mask
| stencil_ref
.ref_value
[0]);
138 if (r300screen
->caps
->is_r500
) {
139 OUT_CS_REG(R500_ZB_STENCILREFMASK_BF
, dsa
->stencil_ref_bf
| stencil_ref
.ref_value
[1]);
144 static const float * get_shader_constant(
145 struct r300_context
* r300
,
146 struct rc_constant
* constant
,
147 struct r300_constant_buffer
* externals
)
149 struct r300_viewport_state
* viewport
=
150 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
151 struct r300_textures_state
* texstate
=
152 (struct r300_textures_state
*)r300
->textures_state
.state
;
153 static float vec
[4] = { 0.0, 0.0, 0.0, 1.0 };
154 struct pipe_texture
*tex
;
156 switch(constant
->Type
) {
157 case RC_CONSTANT_EXTERNAL
:
158 return externals
->constants
[constant
->u
.External
];
160 case RC_CONSTANT_IMMEDIATE
:
161 return constant
->u
.Immediate
;
163 case RC_CONSTANT_STATE
:
164 switch (constant
->u
.State
[0]) {
165 /* Factor for converting rectangle coords to
166 * normalized coords. Should only show up on non-r500. */
167 case RC_STATE_R300_TEXRECT_FACTOR
:
168 tex
= texstate
->fragment_sampler_views
[constant
->u
.State
[1]]->texture
;
169 vec
[0] = 1.0 / tex
->width0
;
170 vec
[1] = 1.0 / tex
->height0
;
173 /* Texture compare-fail value. Shouldn't ever show up, but if
174 * it does, we'll be ready. */
175 case RC_STATE_SHADOW_AMBIENT
:
179 case RC_STATE_R300_VIEWPORT_SCALE
:
180 vec
[0] = viewport
->xscale
;
181 vec
[1] = viewport
->yscale
;
182 vec
[2] = viewport
->zscale
;
185 case RC_STATE_R300_VIEWPORT_OFFSET
:
186 vec
[0] = viewport
->xoffset
;
187 vec
[1] = viewport
->yoffset
;
188 vec
[2] = viewport
->zoffset
;
192 debug_printf("r300: Implementation error: "
193 "Unknown RC_CONSTANT type %d\n", constant
->u
.State
[0]);
198 debug_printf("r300: Implementation error: "
199 "Unhandled constant type %d\n", constant
->Type
);
202 /* This should either be (0, 0, 0, 1), which should be a relatively safe
203 * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
208 /* Convert a normal single-precision float into the 7.16 format
209 * used by the R300 fragment shader.
211 static uint32_t pack_float24(float f
)
219 uint32_t float24
= 0;
226 mantissa
= frexpf(f
, &exponent
);
230 float24
|= (1 << 23);
231 mantissa
= mantissa
* -1.0;
233 /* Handle exponent, bias of 63 */
235 float24
|= (exponent
<< 16);
236 /* Kill 7 LSB of mantissa */
237 float24
|= (u
.u
& 0x7FFFFF) >> 7;
242 void r300_emit_fragment_program_code(struct r300_context
* r300
,
243 struct rX00_fragment_program_code
* generic_code
)
245 struct r300_fragment_program_code
* code
= &generic_code
->code
.r300
;
250 code
->alu
.length
* 4 +
251 (code
->tex
.length
? (1 + code
->tex
.length
) : 0));
253 OUT_CS_REG(R300_US_CONFIG
, code
->config
);
254 OUT_CS_REG(R300_US_PIXSIZE
, code
->pixsize
);
255 OUT_CS_REG(R300_US_CODE_OFFSET
, code
->code_offset
);
257 OUT_CS_REG_SEQ(R300_US_CODE_ADDR_0
, 4);
258 for(i
= 0; i
< 4; ++i
)
259 OUT_CS(code
->code_addr
[i
]);
261 OUT_CS_REG_SEQ(R300_US_ALU_RGB_INST_0
, code
->alu
.length
);
262 for (i
= 0; i
< code
->alu
.length
; i
++)
263 OUT_CS(code
->alu
.inst
[i
].rgb_inst
);
265 OUT_CS_REG_SEQ(R300_US_ALU_RGB_ADDR_0
, code
->alu
.length
);
266 for (i
= 0; i
< code
->alu
.length
; i
++)
267 OUT_CS(code
->alu
.inst
[i
].rgb_addr
);
269 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_INST_0
, code
->alu
.length
);
270 for (i
= 0; i
< code
->alu
.length
; i
++)
271 OUT_CS(code
->alu
.inst
[i
].alpha_inst
);
273 OUT_CS_REG_SEQ(R300_US_ALU_ALPHA_ADDR_0
, code
->alu
.length
);
274 for (i
= 0; i
< code
->alu
.length
; i
++)
275 OUT_CS(code
->alu
.inst
[i
].alpha_addr
);
277 if (code
->tex
.length
) {
278 OUT_CS_REG_SEQ(R300_US_TEX_INST_0
, code
->tex
.length
);
279 for(i
= 0; i
< code
->tex
.length
; ++i
)
280 OUT_CS(code
->tex
.inst
[i
]);
286 void r300_emit_fs_constant_buffer(struct r300_context
* r300
,
287 struct rc_constant_list
* constants
)
292 if (constants
->Count
== 0)
295 BEGIN_CS(constants
->Count
* 4 + 1);
296 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X
, constants
->Count
* 4);
297 for(i
= 0; i
< constants
->Count
; ++i
) {
298 const float * data
= get_shader_constant(r300
,
299 &constants
->Constants
[i
],
300 &r300
->shader_constants
[PIPE_SHADER_FRAGMENT
]);
301 OUT_CS(pack_float24(data
[0]));
302 OUT_CS(pack_float24(data
[1]));
303 OUT_CS(pack_float24(data
[2]));
304 OUT_CS(pack_float24(data
[3]));
309 static void r300_emit_fragment_depth_config(struct r300_context
* r300
,
310 struct r300_fragment_shader
* fs
)
315 if (r300_fragment_shader_writes_depth(fs
)) {
316 OUT_CS_REG(R300_FG_DEPTH_SRC
, R300_FG_DEPTH_SRC_SHADER
);
317 OUT_CS_REG(R300_US_W_FMT
, R300_W_FMT_W24
| R300_W_SRC_US
);
319 OUT_CS_REG(R300_FG_DEPTH_SRC
, R300_FG_DEPTH_SRC_SCAN
);
320 OUT_CS_REG(R300_US_W_FMT
, R300_W_FMT_W0
| R300_W_SRC_US
);
325 void r500_emit_fragment_program_code(struct r300_context
* r300
,
326 struct rX00_fragment_program_code
* generic_code
)
328 struct r500_fragment_program_code
* code
= &generic_code
->code
.r500
;
333 ((code
->inst_end
+ 1) * 6));
334 OUT_CS_REG(R500_US_CONFIG
, R500_ZERO_TIMES_ANYTHING_EQUALS_ZERO
);
335 OUT_CS_REG(R500_US_PIXSIZE
, code
->max_temp_idx
);
336 OUT_CS_REG(R500_US_CODE_RANGE
,
337 R500_US_CODE_RANGE_ADDR(0) | R500_US_CODE_RANGE_SIZE(code
->inst_end
));
338 OUT_CS_REG(R500_US_CODE_OFFSET
, 0);
339 OUT_CS_REG(R500_US_CODE_ADDR
,
340 R500_US_CODE_START_ADDR(0) | R500_US_CODE_END_ADDR(code
->inst_end
));
342 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
, R500_GA_US_VECTOR_INDEX_TYPE_INSTR
);
343 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, (code
->inst_end
+ 1) * 6);
344 for (i
= 0; i
<= code
->inst_end
; i
++) {
345 OUT_CS(code
->inst
[i
].inst0
);
346 OUT_CS(code
->inst
[i
].inst1
);
347 OUT_CS(code
->inst
[i
].inst2
);
348 OUT_CS(code
->inst
[i
].inst3
);
349 OUT_CS(code
->inst
[i
].inst4
);
350 OUT_CS(code
->inst
[i
].inst5
);
356 void r500_emit_fs_constant_buffer(struct r300_context
* r300
,
357 struct rc_constant_list
* constants
)
362 if (constants
->Count
== 0)
365 BEGIN_CS(constants
->Count
* 4 + 3);
366 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
, R500_GA_US_VECTOR_INDEX_TYPE_CONST
);
367 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, constants
->Count
* 4);
368 for (i
= 0; i
< constants
->Count
; i
++) {
369 const float * data
= get_shader_constant(r300
,
370 &constants
->Constants
[i
],
371 &r300
->shader_constants
[PIPE_SHADER_FRAGMENT
]);
380 void r300_emit_fb_state(struct r300_context
* r300
, unsigned size
, void* state
)
382 struct pipe_framebuffer_state
* fb
= (struct pipe_framebuffer_state
*)state
;
383 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
384 struct r300_texture
* tex
;
385 struct pipe_surface
* surf
;
391 /* Flush and free renderbuffer caches. */
392 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT
,
393 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS
|
394 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D
);
395 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT
,
396 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE
|
397 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE
);
399 /* Set the number of colorbuffers. */
400 if (fb
->nr_cbufs
> 1) {
401 if (r300screen
->caps
->is_r500
) {
402 OUT_CS_REG(R300_RB3D_CCTL
,
403 R300_RB3D_CCTL_NUM_MULTIWRITES(fb
->nr_cbufs
) |
404 R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE
);
406 OUT_CS_REG(R300_RB3D_CCTL
,
407 R300_RB3D_CCTL_NUM_MULTIWRITES(fb
->nr_cbufs
));
410 OUT_CS_REG(R300_RB3D_CCTL
, 0x0);
413 /* Set up colorbuffers. */
414 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
416 tex
= (struct r300_texture
*)surf
->texture
;
417 assert(tex
&& tex
->buffer
&& "cbuf is marked, but NULL!");
419 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0
+ (4 * i
), 1);
420 OUT_CS_TEX_RELOC(tex
, surf
->offset
, 0, RADEON_GEM_DOMAIN_VRAM
, 0);
422 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0
+ (4 * i
), 1);
423 OUT_CS_TEX_RELOC(tex
, tex
->fb_state
.colorpitch
[surf
->level
],
424 0, RADEON_GEM_DOMAIN_VRAM
, 0);
426 OUT_CS_REG(R300_US_OUT_FMT_0
+ (4 * i
), tex
->fb_state
.us_out_fmt
);
429 OUT_CS_REG(R300_US_OUT_FMT_0
+ (4 * i
), R300_US_OUT_FMT_UNUSED
);
432 /* Set up a zbuffer. */
435 tex
= (struct r300_texture
*)surf
->texture
;
436 assert(tex
&& tex
->buffer
&& "zsbuf is marked, but NULL!");
438 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET
, 1);
439 OUT_CS_TEX_RELOC(tex
, surf
->offset
, 0, RADEON_GEM_DOMAIN_VRAM
, 0);
441 OUT_CS_REG(R300_ZB_FORMAT
, tex
->fb_state
.zb_format
);
443 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH
, 1);
444 OUT_CS_TEX_RELOC(tex
, tex
->fb_state
.depthpitch
[surf
->level
],
445 0, RADEON_GEM_DOMAIN_VRAM
, 0);
448 OUT_CS_REG(R300_GA_POINT_MINMAX
,
449 (MAX2(fb
->width
, fb
->height
) * 6) << R300_GA_POINT_MINMAX_MAX_SHIFT
);
453 void r300_emit_query_start(struct r300_context
*r300
)
455 struct r300_capabilities
*caps
= r300_screen(r300
->context
.screen
)->caps
;
456 struct r300_query
*query
= r300
->query_current
;
463 if (caps
->family
== CHIP_FAMILY_RV530
) {
464 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
466 OUT_CS_REG(R300_SU_REG_DEST
, R300_RASTER_PIPE_SELECT_ALL
);
468 OUT_CS_REG(R300_ZB_ZPASS_DATA
, 0);
470 query
->begin_emitted
= TRUE
;
474 static void r300_emit_query_finish(struct r300_context
*r300
,
475 struct r300_query
*query
)
477 struct r300_capabilities
* caps
= r300_screen(r300
->context
.screen
)->caps
;
480 assert(caps
->num_frag_pipes
);
482 BEGIN_CS(6 * caps
->num_frag_pipes
+ 2);
483 /* I'm not so sure I like this switch, but it's hard to be elegant
484 * when there's so many special cases...
486 * So here's the basic idea. For each pipe, enable writes to it only,
487 * then put out the relocation for ZPASS_ADDR, taking into account a
488 * 4-byte offset for each pipe. RV380 and older are special; they have
489 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
490 * so there's a chipset cap for that. */
491 switch (caps
->num_frag_pipes
) {
494 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 3);
495 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
496 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
+ (sizeof(uint32_t) * 3),
497 0, RADEON_GEM_DOMAIN_GTT
, 0);
500 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 2);
501 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
502 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
+ (sizeof(uint32_t) * 2),
503 0, RADEON_GEM_DOMAIN_GTT
, 0);
506 /* As mentioned above, accomodate RV380 and older. */
507 OUT_CS_REG(R300_SU_REG_DEST
,
508 1 << (caps
->high_second_pipe
? 3 : 1));
509 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
510 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
+ (sizeof(uint32_t) * 1),
511 0, RADEON_GEM_DOMAIN_GTT
, 0);
514 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 0);
515 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
516 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
+ (sizeof(uint32_t) * 0),
517 0, RADEON_GEM_DOMAIN_GTT
, 0);
520 debug_printf("r300: Implementation error: Chipset reports %d"
521 " pixel pipes!\n", caps
->num_frag_pipes
);
525 /* And, finally, reset it to normal... */
526 OUT_CS_REG(R300_SU_REG_DEST
, 0xF);
530 static void rv530_emit_query_single(struct r300_context
*r300
,
531 struct r300_query
*query
)
536 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
537 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
538 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
, 0, RADEON_GEM_DOMAIN_GTT
, 0);
539 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
543 static void rv530_emit_query_double(struct r300_context
*r300
,
544 struct r300_query
*query
)
549 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
550 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
551 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
, 0, RADEON_GEM_DOMAIN_GTT
, 0);
552 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_1
);
553 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
554 OUT_CS_BUF_RELOC(r300
->oqbo
, query
->offset
+ sizeof(uint32_t), 0, RADEON_GEM_DOMAIN_GTT
, 0);
555 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
559 void r300_emit_query_end(struct r300_context
* r300
)
561 struct r300_capabilities
*caps
= r300_screen(r300
->context
.screen
)->caps
;
562 struct r300_query
*query
= r300
->query_current
;
567 if (query
->begin_emitted
== FALSE
)
570 if (caps
->family
== CHIP_FAMILY_RV530
) {
571 if (caps
->num_z_pipes
== 2)
572 rv530_emit_query_double(r300
, query
);
574 rv530_emit_query_single(r300
, query
);
576 r300_emit_query_finish(r300
, query
);
579 void r300_emit_rs_state(struct r300_context
* r300
, unsigned size
, void* state
)
581 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
586 OUT_CS_REG(R300_VAP_CNTL_STATUS
, rs
->vap_control_status
);
588 OUT_CS_REG(R300_GB_AA_CONFIG
, rs
->antialiasing_config
);
590 OUT_CS_REG(R300_GA_POINT_SIZE
, rs
->point_size
);
591 OUT_CS_REG(R300_GA_LINE_CNTL
, rs
->line_control
);
593 if (rs
->polygon_offset_enable
) {
594 scale
= rs
->depth_scale
* 12;
595 offset
= rs
->depth_offset
;
597 switch (r300
->zbuffer_bpp
) {
606 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE
, 4);
613 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE
, 2);
614 OUT_CS(rs
->polygon_offset_enable
);
615 OUT_CS(rs
->cull_mode
);
616 OUT_CS_REG(R300_GA_LINE_STIPPLE_CONFIG
, rs
->line_stipple_config
);
617 OUT_CS_REG(R300_GA_LINE_STIPPLE_VALUE
, rs
->line_stipple_value
);
618 OUT_CS_REG(R300_GA_POLY_MODE
, rs
->polygon_mode
);
622 void r300_emit_rs_block_state(struct r300_context
* r300
,
623 unsigned size
, void* state
)
625 struct r300_rs_block
* rs
= (struct r300_rs_block
*)state
;
627 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
628 /* It's the same for both INST and IP tables */
629 unsigned count
= (rs
->inst_count
& R300_RS_INST_COUNT_MASK
) + 1;
632 DBG(r300
, DBG_DRAW
, "r300: RS emit:\n");
635 if (r300screen
->caps
->is_r500
) {
636 OUT_CS_REG_SEQ(R500_RS_IP_0
, count
);
638 OUT_CS_REG_SEQ(R300_RS_IP_0
, count
);
640 for (i
= 0; i
< count
; i
++) {
642 DBG(r300
, DBG_DRAW
, " : ip %d: 0x%08x\n", i
, rs
->ip
[i
]);
645 OUT_CS_REG_SEQ(R300_RS_COUNT
, 2);
647 OUT_CS(rs
->inst_count
);
649 if (r300screen
->caps
->is_r500
) {
650 OUT_CS_REG_SEQ(R500_RS_INST_0
, count
);
652 OUT_CS_REG_SEQ(R300_RS_INST_0
, count
);
654 for (i
= 0; i
< count
; i
++) {
656 DBG(r300
, DBG_DRAW
, " : inst %d: 0x%08x\n", i
, rs
->inst
[i
]);
659 DBG(r300
, DBG_DRAW
, " : count: 0x%08x inst_count: 0x%08x\n",
660 rs
->count
, rs
->inst_count
);
665 void r300_emit_scissor_state(struct r300_context
* r300
,
666 unsigned size
, void* state
)
668 unsigned minx
, miny
, maxx
, maxy
;
669 uint32_t top_left
, bottom_right
;
670 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
671 struct pipe_scissor_state
* scissor
= (struct pipe_scissor_state
*)state
;
672 struct pipe_framebuffer_state
* fb
=
673 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
680 if (r300
->scissor_enabled
) {
681 minx
= MAX2(minx
, scissor
->minx
);
682 miny
= MAX2(miny
, scissor
->miny
);
683 maxx
= MIN2(maxx
, scissor
->maxx
);
684 maxy
= MIN2(maxy
, scissor
->maxy
);
687 /* Special case for zero-area scissor.
689 * We can't allow the variables maxx and maxy to be zero because they are
690 * subtracted from later in the code, which would cause emitting ~0 and
691 * making the kernel checker angry.
693 * Let's consider we change maxx and maxy to 1, which is effectively
694 * a one-pixel area. We must then change minx and miny to a number which is
695 * greater than 1 to get the zero area back. */
696 if (!maxx
|| !maxy
) {
703 if (r300screen
->caps
->is_r500
) {
705 (minx
<< R300_SCISSORS_X_SHIFT
) |
706 (miny
<< R300_SCISSORS_Y_SHIFT
);
708 ((maxx
- 1) << R300_SCISSORS_X_SHIFT
) |
709 ((maxy
- 1) << R300_SCISSORS_Y_SHIFT
);
711 /* Offset of 1440 in non-R500 chipsets. */
713 ((minx
+ 1440) << R300_SCISSORS_X_SHIFT
) |
714 ((miny
+ 1440) << R300_SCISSORS_Y_SHIFT
);
716 (((maxx
- 1) + 1440) << R300_SCISSORS_X_SHIFT
) |
717 (((maxy
- 1) + 1440) << R300_SCISSORS_Y_SHIFT
);
721 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL
, 2);
723 OUT_CS(bottom_right
);
727 void r300_emit_textures_state(struct r300_context
*r300
,
728 unsigned size
, void *state
)
730 struct r300_textures_state
*allstate
= (struct r300_textures_state
*)state
;
731 struct r300_texture_sampler_state
*texstate
;
736 OUT_CS_REG(R300_TX_ENABLE
, allstate
->tx_enable
);
738 for (i
= 0; i
< allstate
->count
; i
++) {
739 if ((1 << i
) & allstate
->tx_enable
) {
740 texstate
= &allstate
->regs
[i
];
742 OUT_CS_REG(R300_TX_FILTER0_0
+ (i
* 4), texstate
->filter
[0]);
743 OUT_CS_REG(R300_TX_FILTER1_0
+ (i
* 4), texstate
->filter
[1]);
744 OUT_CS_REG(R300_TX_BORDER_COLOR_0
+ (i
* 4),
745 texstate
->border_color
);
747 OUT_CS_REG(R300_TX_FORMAT0_0
+ (i
* 4), texstate
->format
[0]);
748 OUT_CS_REG(R300_TX_FORMAT1_0
+ (i
* 4), texstate
->format
[1]);
749 OUT_CS_REG(R300_TX_FORMAT2_0
+ (i
* 4), texstate
->format
[2]);
751 OUT_CS_REG_SEQ(R300_TX_OFFSET_0
+ (i
* 4), 1);
752 OUT_CS_TEX_RELOC((struct r300_texture
*)allstate
->fragment_sampler_views
[i
]->texture
,
753 texstate
->tile_config
,
754 RADEON_GEM_DOMAIN_GTT
| RADEON_GEM_DOMAIN_VRAM
, 0, 0);
760 void r300_emit_aos(struct r300_context
* r300
, unsigned offset
)
762 struct pipe_vertex_buffer
*vb1
, *vb2
, *vbuf
= r300
->vertex_buffer
;
763 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
765 unsigned size1
, size2
, aos_count
= r300
->velems
->count
;
766 unsigned packet_size
= (aos_count
* 3 + 1) / 2;
769 BEGIN_CS(2 + packet_size
+ aos_count
* 2);
770 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, packet_size
);
773 for (i
= 0; i
< aos_count
- 1; i
+= 2) {
774 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
775 vb2
= &vbuf
[velem
[i
+1].vertex_buffer_index
];
776 size1
= util_format_get_blocksize(velem
[i
].src_format
);
777 size2
= util_format_get_blocksize(velem
[i
+1].src_format
);
779 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
) |
780 R300_VBPNTR_SIZE1(size2
) | R300_VBPNTR_STRIDE1(vb2
->stride
));
781 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
782 OUT_CS(vb2
->buffer_offset
+ velem
[i
+1].src_offset
+ offset
* vb2
->stride
);
786 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
787 size1
= util_format_get_blocksize(velem
[i
].src_format
);
789 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
));
790 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
793 for (i
= 0; i
< aos_count
; i
++) {
794 OUT_CS_BUF_RELOC_NO_OFFSET(vbuf
[velem
[i
].vertex_buffer_index
].buffer
,
795 RADEON_GEM_DOMAIN_GTT
, 0, 0);
800 void r300_emit_vertex_buffer(struct r300_context
* r300
)
804 DBG(r300
, DBG_DRAW
, "r300: Preparing vertex buffer %p for render, "
805 "vertex size %d\n", r300
->vbo
,
806 r300
->vertex_info
.size
);
807 /* Set the pointer to our vertex buffer. The emitted values are this:
808 * PACKET3 [3D_LOAD_VBPNTR]
810 * FORMAT [size | stride << 8]
811 * OFFSET [offset into BO]
812 * VBPNTR [relocated BO]
815 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, 3);
817 OUT_CS(r300
->vertex_info
.size
|
818 (r300
->vertex_info
.size
<< 8));
819 OUT_CS(r300
->vbo_offset
);
820 OUT_CS_BUF_RELOC(r300
->vbo
, 0, RADEON_GEM_DOMAIN_GTT
, 0, 0);
824 void r300_emit_vertex_stream_state(struct r300_context
* r300
,
825 unsigned size
, void* state
)
827 struct r300_vertex_stream_state
*streams
=
828 (struct r300_vertex_stream_state
*)state
;
832 DBG(r300
, DBG_DRAW
, "r300: PSC emit:\n");
835 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0
, streams
->count
);
836 for (i
= 0; i
< streams
->count
; i
++) {
837 OUT_CS(streams
->vap_prog_stream_cntl
[i
]);
838 DBG(r300
, DBG_DRAW
, " : prog_stream_cntl%d: 0x%08x\n", i
,
839 streams
->vap_prog_stream_cntl
[i
]);
841 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0
, streams
->count
);
842 for (i
= 0; i
< streams
->count
; i
++) {
843 OUT_CS(streams
->vap_prog_stream_cntl_ext
[i
]);
844 DBG(r300
, DBG_DRAW
, " : prog_stream_cntl_ext%d: 0x%08x\n", i
,
845 streams
->vap_prog_stream_cntl_ext
[i
]);
850 void r300_emit_vap_output_state(struct r300_context
* r300
,
851 unsigned size
, void* state
)
853 struct r300_vap_output_state
*vap_out_state
=
854 (struct r300_vap_output_state
*)state
;
857 DBG(r300
, DBG_DRAW
, "r300: VAP emit:\n");
860 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL
, 2);
861 OUT_CS(vap_out_state
->vap_vtx_state_cntl
);
862 OUT_CS(vap_out_state
->vap_vsm_vtx_assm
);
863 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0
, 2);
864 OUT_CS(vap_out_state
->vap_out_vtx_fmt
[0]);
865 OUT_CS(vap_out_state
->vap_out_vtx_fmt
[1]);
869 void r300_emit_pvs_flush(struct r300_context
* r300
, unsigned size
, void* state
)
874 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG
, 0x0);
878 void r300_emit_vs_state(struct r300_context
* r300
, unsigned size
, void* state
)
880 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)state
;
881 struct r300_vertex_program_code
* code
= &vs
->code
;
882 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
883 unsigned instruction_count
= code
->length
/ 4;
886 unsigned vtx_mem_size
= r300screen
->caps
->is_r500
? 128 : 72;
887 unsigned input_count
= MAX2(util_bitcount(code
->InputsRead
), 1);
888 unsigned output_count
= MAX2(util_bitcount(code
->OutputsWritten
), 1);
889 unsigned temp_count
= MAX2(code
->num_temporaries
, 1);
891 unsigned pvs_num_slots
= MIN3(vtx_mem_size
/ input_count
,
892 vtx_mem_size
/ output_count
, 10);
893 unsigned pvs_num_controllers
= MIN2(vtx_mem_size
/ temp_count
, 6);
897 if (!r300screen
->caps
->has_tcl
) {
898 debug_printf("r300: Implementation error: emit_vs_state called,"
899 " but has_tcl is FALSE!\n");
904 /* R300_VAP_PVS_CODE_CNTL_0
905 * R300_VAP_PVS_CONST_CNTL
906 * R300_VAP_PVS_CODE_CNTL_1
907 * See the r5xx docs for instructions on how to use these. */
908 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0
, 3);
909 OUT_CS(R300_PVS_FIRST_INST(0) |
910 R300_PVS_XYZW_VALID_INST(instruction_count
- 1) |
911 R300_PVS_LAST_INST(instruction_count
- 1));
912 OUT_CS(R300_PVS_MAX_CONST_ADDR(code
->constants
.Count
- 1));
913 OUT_CS(instruction_count
- 1);
915 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
, 0);
916 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, code
->length
);
917 for (i
= 0; i
< code
->length
; i
++) {
918 OUT_CS(code
->body
.d
[i
]);
921 OUT_CS_REG(R300_VAP_CNTL
, R300_PVS_NUM_SLOTS(pvs_num_slots
) |
922 R300_PVS_NUM_CNTLRS(pvs_num_controllers
) |
923 R300_PVS_NUM_FPUS(r300screen
->caps
->num_vert_fpus
) |
924 R300_PVS_VF_MAX_VTX_NUM(12) |
925 (r300screen
->caps
->is_r500
? R500_TCL_STATE_OPTIMIZATION
: 0));
929 void r300_emit_vs_constant_buffer(struct r300_context
* r300
,
930 struct rc_constant_list
* constants
)
933 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
936 if (!r300screen
->caps
->has_tcl
) {
937 debug_printf("r300: Implementation error: emit_vs_constant_buffer called,"
938 " but has_tcl is FALSE!\n");
942 if (constants
->Count
== 0)
945 BEGIN_CS(constants
->Count
* 4 + 3);
946 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
947 (r300screen
->caps
->is_r500
?
948 R500_PVS_CONST_START
: R300_PVS_CONST_START
));
949 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, constants
->Count
* 4);
950 for (i
= 0; i
< constants
->Count
; i
++) {
951 const float * data
= get_shader_constant(r300
,
952 &constants
->Constants
[i
],
953 &r300
->shader_constants
[PIPE_SHADER_VERTEX
]);
962 void r300_emit_viewport_state(struct r300_context
* r300
,
963 unsigned size
, void* state
)
965 struct r300_viewport_state
* viewport
= (struct r300_viewport_state
*)state
;
969 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE
, 6);
970 OUT_CS_32F(viewport
->xscale
);
971 OUT_CS_32F(viewport
->xoffset
);
972 OUT_CS_32F(viewport
->yscale
);
973 OUT_CS_32F(viewport
->yoffset
);
974 OUT_CS_32F(viewport
->zscale
);
975 OUT_CS_32F(viewport
->zoffset
);
976 OUT_CS_REG(R300_VAP_VTE_CNTL
, viewport
->vte_control
);
980 void r300_emit_ztop_state(struct r300_context
* r300
,
981 unsigned size
, void* state
)
983 struct r300_ztop_state
* ztop
= (struct r300_ztop_state
*)state
;
987 OUT_CS_REG(R300_ZB_ZTOP
, ztop
->z_buffer_top
);
991 void r300_emit_texture_cache_inval(struct r300_context
* r300
, unsigned size
, void* state
)
996 OUT_CS_REG(R300_TX_INVALTAGS
, 0);
1000 void r300_emit_buffer_validate(struct r300_context
*r300
,
1001 boolean do_validate_vertex_buffers
,
1002 struct pipe_buffer
*index_buffer
)
1004 struct pipe_framebuffer_state
* fb
=
1005 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
1006 struct r300_textures_state
*texstate
=
1007 (struct r300_textures_state
*)r300
->textures_state
.state
;
1008 struct r300_texture
* tex
;
1009 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
1010 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
1011 struct pipe_buffer
*pbuf
;
1013 boolean invalid
= FALSE
;
1015 /* upload buffers first */
1016 if (r300
->any_user_vbs
) {
1017 r300_upload_user_buffers(r300
);
1018 r300
->any_user_vbs
= false;
1021 /* Clean out BOs. */
1022 r300
->rws
->reset_bos(r300
->rws
);
1025 /* Color buffers... */
1026 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
1027 tex
= (struct r300_texture
*)fb
->cbufs
[i
]->texture
;
1028 assert(tex
&& tex
->buffer
&& "cbuf is marked, but NULL!");
1029 if (!r300_add_texture(r300
->rws
, tex
,
1030 0, RADEON_GEM_DOMAIN_VRAM
)) {
1031 r300
->context
.flush(&r300
->context
, 0, NULL
);
1035 /* ...depth buffer... */
1037 tex
= (struct r300_texture
*)fb
->zsbuf
->texture
;
1038 assert(tex
&& tex
->buffer
&& "zsbuf is marked, but NULL!");
1039 if (!r300_add_texture(r300
->rws
, tex
,
1040 0, RADEON_GEM_DOMAIN_VRAM
)) {
1041 r300
->context
.flush(&r300
->context
, 0, NULL
);
1045 /* ...textures... */
1046 for (i
= 0; i
< texstate
->count
; i
++) {
1047 tex
= (struct r300_texture
*)texstate
->fragment_sampler_views
[i
]->texture
;
1048 if (!tex
|| !texstate
->sampler_states
[i
])
1050 if (!r300_add_texture(r300
->rws
, tex
,
1051 RADEON_GEM_DOMAIN_GTT
| RADEON_GEM_DOMAIN_VRAM
, 0)) {
1052 r300
->context
.flush(&r300
->context
, 0, NULL
);
1056 /* ...occlusion query buffer... */
1057 if (r300
->dirty_state
& R300_NEW_QUERY
) {
1058 if (!r300_add_buffer(r300
->rws
, r300
->oqbo
,
1059 0, RADEON_GEM_DOMAIN_GTT
)) {
1060 r300
->context
.flush(&r300
->context
, 0, NULL
);
1064 /* ...vertex buffer for SWTCL path... */
1066 if (!r300_add_buffer(r300
->rws
, r300
->vbo
,
1067 RADEON_GEM_DOMAIN_GTT
, 0)) {
1068 r300
->context
.flush(&r300
->context
, 0, NULL
);
1072 /* ...vertex buffers for HWTCL path... */
1073 if (do_validate_vertex_buffers
) {
1074 for (i
= 0; i
< r300
->velems
->count
; i
++) {
1075 pbuf
= vbuf
[velem
[i
].vertex_buffer_index
].buffer
;
1077 if (!r300_add_buffer(r300
->rws
, pbuf
,
1078 RADEON_GEM_DOMAIN_GTT
, 0)) {
1079 r300
->context
.flush(&r300
->context
, 0, NULL
);
1084 /* ...and index buffer for HWTCL path. */
1086 if (!r300_add_buffer(r300
->rws
, index_buffer
,
1087 RADEON_GEM_DOMAIN_GTT
, 0)) {
1088 r300
->context
.flush(&r300
->context
, 0, NULL
);
1092 if (!r300
->rws
->validate(r300
->rws
)) {
1093 r300
->context
.flush(&r300
->context
, 0, NULL
);
1096 debug_printf("r300: Stuck in validation loop, gonna quit now.");
1104 unsigned r300_get_num_dirty_dwords(struct r300_context
*r300
)
1106 struct r300_atom
* atom
;
1107 unsigned dwords
= 0;
1109 foreach(atom
, &r300
->atom_list
) {
1110 if (atom
->dirty
|| atom
->always_dirty
) {
1111 dwords
+= atom
->size
;
1115 /* XXX This is the compensation for the non-atomized states. */
1121 /* Emit all dirty state. */
1122 void r300_emit_dirty_state(struct r300_context
* r300
)
1124 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
1125 struct r300_atom
* atom
;
1127 if (r300
->dirty_state
& R300_NEW_QUERY
) {
1128 r300_emit_query_start(r300
);
1129 r300
->dirty_state
&= ~R300_NEW_QUERY
;
1132 foreach(atom
, &r300
->atom_list
) {
1133 if (atom
->dirty
|| atom
->always_dirty
) {
1134 atom
->emit(r300
, atom
->size
, atom
->state
);
1135 atom
->dirty
= FALSE
;
1139 if (r300
->dirty_state
& R300_NEW_FRAGMENT_SHADER
) {
1140 r300_emit_fragment_depth_config(r300
, r300
->fs
);
1141 if (r300screen
->caps
->is_r500
) {
1142 r500_emit_fragment_program_code(r300
, &r300
->fs
->shader
->code
);
1144 r300_emit_fragment_program_code(r300
, &r300
->fs
->shader
->code
);
1146 r300
->dirty_state
&= ~R300_NEW_FRAGMENT_SHADER
;
1149 if (r300
->dirty_state
& R300_NEW_FRAGMENT_SHADER_CONSTANTS
) {
1150 if (r300screen
->caps
->is_r500
) {
1151 r500_emit_fs_constant_buffer(r300
,
1152 &r300
->fs
->shader
->code
.constants
);
1154 r300_emit_fs_constant_buffer(r300
,
1155 &r300
->fs
->shader
->code
.constants
);
1157 r300
->dirty_state
&= ~R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
1160 if (r300
->dirty_state
& R300_NEW_VERTEX_SHADER_CONSTANTS
) {
1161 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
1162 r300_emit_vs_constant_buffer(r300
, &vs
->code
.constants
);
1163 r300
->dirty_state
&= ~R300_NEW_VERTEX_SHADER_CONSTANTS
;
1167 assert(r300->dirty_state == 0);
1170 /* Emit the VBO for SWTCL. */
1171 if (!r300screen
->caps
->has_tcl
) {
1172 r300_emit_vertex_buffer(r300
);