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"
38 void r300_emit_blend_state(struct r300_context
* r300
,
39 unsigned size
, void* state
)
41 struct r300_blend_state
* blend
= (struct r300_blend_state
*)state
;
42 struct pipe_framebuffer_state
* fb
=
43 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
47 WRITE_CS_TABLE(blend
->cb
, size
);
49 WRITE_CS_TABLE(blend
->cb_no_readwrite
, size
);
53 void r300_emit_blend_color_state(struct r300_context
* r300
,
54 unsigned size
, void* state
)
56 struct r300_blend_color_state
* bc
= (struct r300_blend_color_state
*)state
;
59 WRITE_CS_TABLE(bc
->cb
, size
);
62 void r300_emit_clip_state(struct r300_context
* r300
,
63 unsigned size
, void* state
)
65 struct r300_clip_state
* clip
= (struct r300_clip_state
*)state
;
68 WRITE_CS_TABLE(clip
->cb
, size
);
71 void r300_emit_dsa_state(struct r300_context
* r300
, unsigned size
, void* state
)
73 struct r300_dsa_state
* dsa
= (struct r300_dsa_state
*)state
;
74 struct pipe_framebuffer_state
* fb
=
75 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
79 WRITE_CS_TABLE(&dsa
->cb_begin
, size
);
81 WRITE_CS_TABLE(dsa
->cb_no_readwrite
, size
);
85 static const float * get_rc_constant_state(
86 struct r300_context
* r300
,
87 struct rc_constant
* constant
)
89 struct r300_textures_state
* texstate
= r300
->textures_state
.state
;
90 static float vec
[4] = { 0.0, 0.0, 0.0, 1.0 };
91 struct pipe_resource
*tex
;
93 assert(constant
->Type
== RC_CONSTANT_STATE
);
95 switch (constant
->u
.State
[0]) {
96 /* Factor for converting rectangle coords to
97 * normalized coords. Should only show up on non-r500. */
98 case RC_STATE_R300_TEXRECT_FACTOR
:
99 tex
= texstate
->sampler_views
[constant
->u
.State
[1]]->base
.texture
;
100 vec
[0] = 1.0 / tex
->width0
;
101 vec
[1] = 1.0 / tex
->height0
;
104 case RC_STATE_R300_VIEWPORT_SCALE
:
105 vec
[0] = r300
->viewport
.scale
[0];
106 vec
[1] = r300
->viewport
.scale
[1];
107 vec
[2] = r300
->viewport
.scale
[2];
110 case RC_STATE_R300_VIEWPORT_OFFSET
:
111 vec
[0] = r300
->viewport
.translate
[0];
112 vec
[1] = r300
->viewport
.translate
[1];
113 vec
[2] = r300
->viewport
.translate
[2];
117 fprintf(stderr
, "r300: Implementation error: "
118 "Unknown RC_CONSTANT type %d\n", constant
->u
.State
[0]);
121 /* This should either be (0, 0, 0, 1), which should be a relatively safe
122 * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
127 /* Convert a normal single-precision float into the 7.16 format
128 * used by the R300 fragment shader.
130 uint32_t pack_float24(float f
)
138 uint32_t float24
= 0;
145 mantissa
= frexpf(f
, &exponent
);
149 float24
|= (1 << 23);
150 mantissa
= mantissa
* -1.0;
152 /* Handle exponent, bias of 63 */
154 float24
|= (exponent
<< 16);
155 /* Kill 7 LSB of mantissa */
156 float24
|= (u
.u
& 0x7FFFFF) >> 7;
161 void r300_emit_fs(struct r300_context
* r300
, unsigned size
, void *state
)
163 struct r300_fragment_shader
*fs
= r300_fs(r300
);
166 WRITE_CS_TABLE(fs
->shader
->cb_code
, fs
->shader
->cb_code_size
);
169 void r300_emit_fs_constants(struct r300_context
* r300
, unsigned size
, void *state
)
171 struct r300_fragment_shader
*fs
= r300_fs(r300
);
172 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
173 unsigned count
= fs
->shader
->externals_count
* 4;
180 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X
, count
);
181 OUT_CS_TABLE(buf
->constants
, count
);
185 void r300_emit_fs_rc_constant_state(struct r300_context
* r300
, unsigned size
, void *state
)
187 struct r300_fragment_shader
*fs
= r300_fs(r300
);
188 struct rc_constant_list
*constants
= &fs
->shader
->code
.constants
;
190 unsigned count
= fs
->shader
->rc_state_count
;
191 unsigned first
= fs
->shader
->externals_count
;
192 unsigned end
= constants
->Count
;
201 for(i
= first
; i
< end
; ++i
) {
202 if (constants
->Constants
[i
].Type
== RC_CONSTANT_STATE
) {
204 get_rc_constant_state(r300
, &constants
->Constants
[i
]);
206 for (j
= 0; j
< 4; j
++)
207 cdata
[j
] = pack_float24(data
[j
]);
209 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X
+ i
* 16, 4);
210 OUT_CS_TABLE(cdata
, 4);
216 void r500_emit_fs(struct r300_context
* r300
, unsigned size
, void *state
)
218 struct r300_fragment_shader
*fs
= r300_fs(r300
);
221 WRITE_CS_TABLE(fs
->shader
->cb_code
, fs
->shader
->cb_code_size
);
224 void r500_emit_fs_constants(struct r300_context
* r300
, unsigned size
, void *state
)
226 struct r300_fragment_shader
*fs
= r300_fs(r300
);
227 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
228 unsigned count
= fs
->shader
->externals_count
* 4;
235 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
, R500_GA_US_VECTOR_INDEX_TYPE_CONST
);
236 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, count
);
237 OUT_CS_TABLE(buf
->constants
, count
);
241 void r500_emit_fs_rc_constant_state(struct r300_context
* r300
, unsigned size
, void *state
)
243 struct r300_fragment_shader
*fs
= r300_fs(r300
);
244 struct rc_constant_list
*constants
= &fs
->shader
->code
.constants
;
246 unsigned count
= fs
->shader
->rc_state_count
;
247 unsigned first
= fs
->shader
->externals_count
;
248 unsigned end
= constants
->Count
;
255 for(i
= first
; i
< end
; ++i
) {
256 if (constants
->Constants
[i
].Type
== RC_CONSTANT_STATE
) {
258 get_rc_constant_state(r300
, &constants
->Constants
[i
]);
260 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
,
261 R500_GA_US_VECTOR_INDEX_TYPE_CONST
|
262 (i
& R500_GA_US_VECTOR_INDEX_MASK
));
263 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, 4);
264 OUT_CS_TABLE(data
, 4);
270 void r300_emit_fb_state(struct r300_context
* r300
, unsigned size
, void* state
)
272 struct pipe_framebuffer_state
* fb
= (struct pipe_framebuffer_state
*)state
;
273 struct r300_surface
* surf
;
280 * By writing to the SC registers, SC & US assert idle. */
281 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL
, 2);
282 if (r300
->screen
->caps
.is_r500
) {
284 OUT_CS(((fb
->width
- 1) << R300_SCISSORS_X_SHIFT
) |
285 ((fb
->height
- 1) << R300_SCISSORS_Y_SHIFT
));
287 OUT_CS((1440 << R300_SCISSORS_X_SHIFT
) |
288 (1440 << R300_SCISSORS_Y_SHIFT
));
289 OUT_CS(((fb
->width
+ 1440-1) << R300_SCISSORS_X_SHIFT
) |
290 ((fb
->height
+ 1440-1) << R300_SCISSORS_Y_SHIFT
));
293 /* Flush and free renderbuffer caches. */
294 OUT_CS_REG(R300_RB3D_DSTCACHE_CTLSTAT
,
295 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS
|
296 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D
);
297 OUT_CS_REG(R300_ZB_ZCACHE_CTLSTAT
,
298 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE
|
299 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE
);
301 /* Wait until the GPU is idle.
302 * This fixes random pixels sometimes appearing probably caused
303 * by incomplete rendering. */
304 OUT_CS_REG(RADEON_WAIT_UNTIL
, RADEON_WAIT_3D_IDLECLEAN
);
306 /* XXX unpipelined regs
308 rb3d_aaresolve_offset
313 /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers, which is not
314 * what we usually want. */
315 if (r300
->screen
->caps
.is_r500
) {
316 OUT_CS_REG(R300_RB3D_CCTL
,
317 R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE
);
319 OUT_CS_REG(R300_RB3D_CCTL
, 0);
322 /* Set up colorbuffers. */
323 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
324 surf
= r300_surface(fb
->cbufs
[i
]);
326 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0
+ (4 * i
), 1);
327 OUT_CS_RELOC(surf
->buffer
, surf
->offset
, 0, surf
->domain
, 0);
329 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0
+ (4 * i
), 1);
330 OUT_CS_RELOC(surf
->buffer
, surf
->pitch
, 0, surf
->domain
, 0);
333 /* Set up a zbuffer. */
335 surf
= r300_surface(fb
->zsbuf
);
337 OUT_CS_REG(R300_ZB_FORMAT
, surf
->format
);
338 OUT_CS_REG(R300_ZB_BW_CNTL
, 0);
340 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET
, 1);
341 OUT_CS_RELOC(surf
->buffer
, surf
->offset
, 0, surf
->domain
, 0);
343 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH
, 1);
344 OUT_CS_RELOC(surf
->buffer
, surf
->pitch
, 0, surf
->domain
, 0);
346 OUT_CS_REG(R300_ZB_DEPTHCLEARVALUE
, 0);
349 if (r300
->screen
->caps
.has_hiz
) {
350 OUT_CS_REG(R300_ZB_HIZ_OFFSET
, 0);
351 OUT_CS_REG(R300_ZB_HIZ_PITCH
, 0);
354 /* Z Mask RAM. (compressed zbuffer) */
355 OUT_CS_REG(R300_ZB_ZMASK_OFFSET
, 0);
356 OUT_CS_REG(R300_ZB_ZMASK_PITCH
, 0);
359 /* Colorbuffer format in the US block.
360 * (must be written after unpipelined regs) */
361 OUT_CS_REG_SEQ(R300_US_OUT_FMT_0
, 4);
362 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
363 OUT_CS(r300_surface(fb
->cbufs
[i
])->format
);
366 OUT_CS(R300_US_OUT_FMT_UNUSED
);
371 void r300_emit_query_start(struct r300_context
*r300
, unsigned size
, void*state
)
373 struct r300_query
*query
= r300
->query_current
;
380 if (r300
->screen
->caps
.family
== CHIP_FAMILY_RV530
) {
381 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
383 OUT_CS_REG(R300_SU_REG_DEST
, R300_RASTER_PIPE_SELECT_ALL
);
385 OUT_CS_REG(R300_ZB_ZPASS_DATA
, 0);
387 query
->begin_emitted
= TRUE
;
388 query
->flushed
= FALSE
;
391 static void r300_emit_query_end_frag_pipes(struct r300_context
*r300
,
392 struct r300_query
*query
)
394 struct r300_capabilities
* caps
= &r300
->screen
->caps
;
395 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
398 assert(caps
->num_frag_pipes
);
400 BEGIN_CS(6 * caps
->num_frag_pipes
+ 2);
401 /* I'm not so sure I like this switch, but it's hard to be elegant
402 * when there's so many special cases...
404 * So here's the basic idea. For each pipe, enable writes to it only,
405 * then put out the relocation for ZPASS_ADDR, taking into account a
406 * 4-byte offset for each pipe. RV380 and older are special; they have
407 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
408 * so there's a chipset cap for that. */
409 switch (caps
->num_frag_pipes
) {
412 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 3);
413 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
414 OUT_CS_RELOC(buf
, (query
->num_results
+ 3) * 4,
415 0, query
->domain
, 0);
418 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 2);
419 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
420 OUT_CS_RELOC(buf
, (query
->num_results
+ 2) * 4,
421 0, query
->domain
, 0);
424 /* As mentioned above, accomodate RV380 and older. */
425 OUT_CS_REG(R300_SU_REG_DEST
,
426 1 << (caps
->high_second_pipe
? 3 : 1));
427 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
428 OUT_CS_RELOC(buf
, (query
->num_results
+ 1) * 4,
429 0, query
->domain
, 0);
432 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 0);
433 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
434 OUT_CS_RELOC(buf
, (query
->num_results
+ 0) * 4,
435 0, query
->domain
, 0);
438 fprintf(stderr
, "r300: Implementation error: Chipset reports %d"
439 " pixel pipes!\n", caps
->num_frag_pipes
);
443 /* And, finally, reset it to normal... */
444 OUT_CS_REG(R300_SU_REG_DEST
, 0xF);
448 static void rv530_emit_query_end_single_z(struct r300_context
*r300
,
449 struct r300_query
*query
)
451 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
455 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
456 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
457 OUT_CS_RELOC(buf
, query
->num_results
* 4, 0, query
->domain
, 0);
458 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
462 static void rv530_emit_query_end_double_z(struct r300_context
*r300
,
463 struct r300_query
*query
)
465 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
469 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
470 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
471 OUT_CS_RELOC(buf
, (query
->num_results
+ 0) * 4, 0, query
->domain
, 0);
472 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_1
);
473 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
474 OUT_CS_RELOC(buf
, (query
->num_results
+ 1) * 4, 0, query
->domain
, 0);
475 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
479 void r300_emit_query_end(struct r300_context
* r300
)
481 struct r300_capabilities
*caps
= &r300
->screen
->caps
;
482 struct r300_query
*query
= r300
->query_current
;
487 if (query
->begin_emitted
== FALSE
)
490 if (caps
->family
== CHIP_FAMILY_RV530
) {
491 if (caps
->num_z_pipes
== 2)
492 rv530_emit_query_end_double_z(r300
, query
);
494 rv530_emit_query_end_single_z(r300
, query
);
496 r300_emit_query_end_frag_pipes(r300
, query
);
498 query
->begin_emitted
= FALSE
;
499 query
->num_results
+= query
->num_pipes
;
501 /* XXX grab all the results and reset the counter. */
502 if (query
->num_results
>= query
->buffer_size
/ 4 - 4) {
503 query
->num_results
= (query
->buffer_size
/ 4) / 2;
504 fprintf(stderr
, "r300: Rewinding OQBO...\n");
508 void r300_emit_rs_state(struct r300_context
* r300
, unsigned size
, void* state
)
510 struct r300_rs_state
* rs
= state
;
511 struct pipe_framebuffer_state
* fb
= r300
->fb_state
.state
;
513 unsigned mspos0
, mspos1
, aa_config
;
517 OUT_CS_REG(R300_VAP_CNTL_STATUS
, rs
->vap_control_status
);
519 /* Multisampling. Depends on framebuffer sample count. */
520 if (r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
)) {
521 if (fb
->nr_cbufs
&& fb
->cbufs
[0]->texture
->nr_samples
> 1) {
522 aa_config
= R300_GB_AA_CONFIG_AA_ENABLE
;
523 /* Subsample placement. These may not be optimal. */
524 switch (fb
->cbufs
[0]->texture
->nr_samples
) {
526 aa_config
|= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_2
;
531 aa_config
|= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_3
;
536 aa_config
|= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_4
;
541 aa_config
|= R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_6
;
546 debug_printf("r300: Bad number of multisamples!\n");
547 mspos0
= rs
->multisample_position_0
;
548 mspos1
= rs
->multisample_position_1
;
552 OUT_CS_REG_SEQ(R300_GB_MSPOS0
, 2);
556 OUT_CS_REG(R300_GB_AA_CONFIG
, aa_config
);
558 OUT_CS_REG_SEQ(R300_GB_MSPOS0
, 2);
559 OUT_CS(rs
->multisample_position_0
);
560 OUT_CS(rs
->multisample_position_1
);
562 OUT_CS_REG(R300_GB_AA_CONFIG
, rs
->antialiasing_config
);
566 OUT_CS_REG(R300_GA_POINT_SIZE
, rs
->point_size
);
567 OUT_CS_REG_SEQ(R300_GA_POINT_MINMAX
, 2);
568 OUT_CS(rs
->point_minmax
);
569 OUT_CS(rs
->line_control
);
571 if (rs
->polygon_offset_enable
) {
572 scale
= rs
->depth_scale
* 12;
573 offset
= rs
->depth_offset
;
575 switch (r300
->zbuffer_bpp
) {
584 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE
, 4);
591 OUT_CS_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE
, 2);
592 OUT_CS(rs
->polygon_offset_enable
);
593 OUT_CS(rs
->cull_mode
);
594 OUT_CS_REG(R300_GA_LINE_STIPPLE_CONFIG
, rs
->line_stipple_config
);
595 OUT_CS_REG(R300_GA_LINE_STIPPLE_VALUE
, rs
->line_stipple_value
);
596 OUT_CS_REG(R300_GA_POLY_MODE
, rs
->polygon_mode
);
597 OUT_CS_REG(R300_SC_CLIP_RULE
, rs
->clip_rule
);
598 OUT_CS_REG(R300_GB_ENABLE
, rs
->stuffing_enable
);
599 OUT_CS_REG_SEQ(R300_GA_POINT_S0
, 4);
600 OUT_CS_32F(rs
->point_texcoord_left
);
601 OUT_CS_32F(rs
->point_texcoord_bottom
);
602 OUT_CS_32F(rs
->point_texcoord_right
);
603 OUT_CS_32F(rs
->point_texcoord_top
);
607 void r300_emit_rs_block_state(struct r300_context
* r300
,
608 unsigned size
, void* state
)
610 struct r300_rs_block
* rs
= (struct r300_rs_block
*)state
;
612 /* It's the same for both INST and IP tables */
613 unsigned count
= (rs
->inst_count
& R300_RS_INST_COUNT_MASK
) + 1;
616 if (SCREEN_DBG_ON(r300
->screen
, DBG_DRAW
)) {
617 r500_dump_rs_block(rs
);
620 DBG(r300
, DBG_DRAW
, "r300: RS emit:\n");
623 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL
, 2);
624 OUT_CS(rs
->vap_vtx_state_cntl
);
625 OUT_CS(rs
->vap_vsm_vtx_assm
);
626 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0
, 2);
627 OUT_CS(rs
->vap_out_vtx_fmt
[0]);
628 OUT_CS(rs
->vap_out_vtx_fmt
[1]);
630 if (r300
->screen
->caps
.is_r500
) {
631 OUT_CS_REG_SEQ(R500_RS_IP_0
, count
);
633 OUT_CS_REG_SEQ(R300_RS_IP_0
, count
);
635 OUT_CS_TABLE(rs
->ip
, count
);
636 for (i
= 0; i
< count
; i
++) {
637 DBG(r300
, DBG_DRAW
, " : ip %d: 0x%08x\n", i
, rs
->ip
[i
]);
640 OUT_CS_REG_SEQ(R300_RS_COUNT
, 2);
642 OUT_CS(rs
->inst_count
);
644 if (r300
->screen
->caps
.is_r500
) {
645 OUT_CS_REG_SEQ(R500_RS_INST_0
, count
);
647 OUT_CS_REG_SEQ(R300_RS_INST_0
, count
);
649 OUT_CS_TABLE(rs
->inst
, count
);
650 for (i
= 0; i
< count
; i
++) {
651 DBG(r300
, DBG_DRAW
, " : inst %d: 0x%08x\n", i
, rs
->inst
[i
]);
654 DBG(r300
, DBG_DRAW
, " : count: 0x%08x inst_count: 0x%08x\n",
655 rs
->count
, rs
->inst_count
);
660 void r300_emit_scissor_state(struct r300_context
* r300
,
661 unsigned size
, void* state
)
663 struct pipe_scissor_state
* scissor
= (struct pipe_scissor_state
*)state
;
667 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0
, 2);
668 if (r300
->screen
->caps
.is_r500
) {
669 OUT_CS((scissor
->minx
<< R300_CLIPRECT_X_SHIFT
) |
670 (scissor
->miny
<< R300_CLIPRECT_Y_SHIFT
));
671 OUT_CS(((scissor
->maxx
- 1) << R300_CLIPRECT_X_SHIFT
) |
672 ((scissor
->maxy
- 1) << R300_CLIPRECT_Y_SHIFT
));
674 OUT_CS(((scissor
->minx
+ 1440) << R300_CLIPRECT_X_SHIFT
) |
675 ((scissor
->miny
+ 1440) << R300_CLIPRECT_Y_SHIFT
));
676 OUT_CS(((scissor
->maxx
+ 1440-1) << R300_CLIPRECT_X_SHIFT
) |
677 ((scissor
->maxy
+ 1440-1) << R300_CLIPRECT_Y_SHIFT
));
682 void r300_emit_textures_state(struct r300_context
*r300
,
683 unsigned size
, void *state
)
685 struct r300_textures_state
*allstate
= (struct r300_textures_state
*)state
;
686 struct r300_texture_sampler_state
*texstate
;
687 struct r300_texture
*tex
;
692 OUT_CS_REG(R300_TX_ENABLE
, allstate
->tx_enable
);
694 for (i
= 0; i
< allstate
->count
; i
++) {
695 if ((1 << i
) & allstate
->tx_enable
) {
696 texstate
= &allstate
->regs
[i
];
697 tex
= r300_texture(allstate
->sampler_views
[i
]->base
.texture
);
699 OUT_CS_REG(R300_TX_FILTER0_0
+ (i
* 4), texstate
->filter0
);
700 OUT_CS_REG(R300_TX_FILTER1_0
+ (i
* 4), texstate
->filter1
);
701 OUT_CS_REG(R300_TX_BORDER_COLOR_0
+ (i
* 4),
702 texstate
->border_color
);
704 OUT_CS_REG(R300_TX_FORMAT0_0
+ (i
* 4), texstate
->format
.format0
);
705 OUT_CS_REG(R300_TX_FORMAT1_0
+ (i
* 4), texstate
->format
.format1
);
706 OUT_CS_REG(R300_TX_FORMAT2_0
+ (i
* 4), texstate
->format
.format2
);
708 OUT_CS_REG_SEQ(R300_TX_OFFSET_0
+ (i
* 4), 1);
709 OUT_CS_TEX_RELOC(tex
, texstate
->format
.tile_config
, tex
->domain
,
716 void r300_emit_aos(struct r300_context
* r300
, int offset
, boolean indexed
)
718 struct pipe_vertex_buffer
*vb1
, *vb2
, *vbuf
= r300
->vertex_buffer
;
719 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
720 struct r300_buffer
*buf
;
722 unsigned *hw_format_size
= r300
->velems
->hw_format_size
;
723 unsigned size1
, size2
, aos_count
= r300
->velems
->count
;
724 unsigned packet_size
= (aos_count
* 3 + 1) / 2;
727 BEGIN_CS(2 + packet_size
+ aos_count
* 2);
728 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, packet_size
);
729 OUT_CS(aos_count
| (!indexed
? R300_VC_FORCE_PREFETCH
: 0));
731 for (i
= 0; i
< aos_count
- 1; i
+= 2) {
732 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
733 vb2
= &vbuf
[velem
[i
+1].vertex_buffer_index
];
734 size1
= hw_format_size
[i
];
735 size2
= hw_format_size
[i
+1];
737 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
) |
738 R300_VBPNTR_SIZE1(size2
) | R300_VBPNTR_STRIDE1(vb2
->stride
));
739 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
740 OUT_CS(vb2
->buffer_offset
+ velem
[i
+1].src_offset
+ offset
* vb2
->stride
);
744 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
745 size1
= hw_format_size
[i
];
747 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
));
748 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
751 for (i
= 0; i
< aos_count
; i
++) {
752 buf
= r300_buffer(vbuf
[velem
[i
].vertex_buffer_index
].buffer
);
753 OUT_CS_BUF_RELOC_NO_OFFSET(&buf
->b
.b
, buf
->domain
, 0, 0);
758 void r300_emit_aos_swtcl(struct r300_context
*r300
, boolean indexed
)
762 DBG(r300
, DBG_DRAW
, "r300: Preparing vertex buffer %p for render, "
763 "vertex size %d\n", r300
->vbo
,
764 r300
->vertex_info
.size
);
765 /* Set the pointer to our vertex buffer. The emitted values are this:
766 * PACKET3 [3D_LOAD_VBPNTR]
768 * FORMAT [size | stride << 8]
769 * OFFSET [offset into BO]
770 * VBPNTR [relocated BO]
773 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, 3);
774 OUT_CS(1 | (!indexed
? R300_VC_FORCE_PREFETCH
: 0));
775 OUT_CS(r300
->vertex_info
.size
|
776 (r300
->vertex_info
.size
<< 8));
777 OUT_CS(r300
->vbo_offset
);
778 OUT_CS_BUF_RELOC(r300
->vbo
, 0, r300_buffer(r300
->vbo
)->domain
, 0, 0);
782 void r300_emit_vertex_stream_state(struct r300_context
* r300
,
783 unsigned size
, void* state
)
785 struct r300_vertex_stream_state
*streams
=
786 (struct r300_vertex_stream_state
*)state
;
790 DBG(r300
, DBG_DRAW
, "r300: PSC emit:\n");
793 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0
, streams
->count
);
794 OUT_CS_TABLE(streams
->vap_prog_stream_cntl
, streams
->count
);
795 for (i
= 0; i
< streams
->count
; i
++) {
796 DBG(r300
, DBG_DRAW
, " : prog_stream_cntl%d: 0x%08x\n", i
,
797 streams
->vap_prog_stream_cntl
[i
]);
799 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0
, streams
->count
);
800 OUT_CS_TABLE(streams
->vap_prog_stream_cntl_ext
, streams
->count
);
801 for (i
= 0; i
< streams
->count
; i
++) {
802 DBG(r300
, DBG_DRAW
, " : prog_stream_cntl_ext%d: 0x%08x\n", i
,
803 streams
->vap_prog_stream_cntl_ext
[i
]);
808 void r300_emit_pvs_flush(struct r300_context
* r300
, unsigned size
, void* state
)
813 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG
, 0x0);
817 void r300_emit_vs_state(struct r300_context
* r300
, unsigned size
, void* state
)
819 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)state
;
820 struct r300_vertex_program_code
* code
= &vs
->code
;
821 struct r300_screen
* r300screen
= r300
->screen
;
822 unsigned instruction_count
= code
->length
/ 4;
825 unsigned vtx_mem_size
= r300screen
->caps
.is_r500
? 128 : 72;
826 unsigned input_count
= MAX2(util_bitcount(code
->InputsRead
), 1);
827 unsigned output_count
= MAX2(util_bitcount(code
->OutputsWritten
), 1);
828 unsigned temp_count
= MAX2(code
->num_temporaries
, 1);
830 unsigned pvs_num_slots
= MIN3(vtx_mem_size
/ input_count
,
831 vtx_mem_size
/ output_count
, 10);
832 unsigned pvs_num_controllers
= MIN2(vtx_mem_size
/ temp_count
, 6);
834 unsigned imm_first
= vs
->externals_count
;
835 unsigned imm_end
= vs
->code
.constants
.Count
;
836 unsigned imm_count
= vs
->immediates_count
;
841 /* Amount of time to wait for vertex fetches in PVS */
842 OUT_CS_REG(VAP_PVS_VTX_TIMEOUT_REG
, 0xffff);
844 OUT_CS_REG_SEQ(R300_VAP_GB_VERT_CLIP_ADJ
, 4);
850 OUT_CS_REG(R300_VAP_PSC_SGN_NORM_CNTL
, R300_SGN_NORM_NO_ZERO
);
852 /* R300_VAP_PVS_CODE_CNTL_0
853 * R300_VAP_PVS_CONST_CNTL
854 * R300_VAP_PVS_CODE_CNTL_1
855 * See the r5xx docs for instructions on how to use these. */
856 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0
, 3);
857 OUT_CS(R300_PVS_FIRST_INST(0) |
858 R300_PVS_XYZW_VALID_INST(instruction_count
- 1) |
859 R300_PVS_LAST_INST(instruction_count
- 1));
860 OUT_CS(R300_PVS_MAX_CONST_ADDR(code
->constants
.Count
- 1));
861 OUT_CS(instruction_count
- 1);
863 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
, 0);
864 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, code
->length
);
865 OUT_CS_TABLE(code
->body
.d
, code
->length
);
867 OUT_CS_REG(R300_VAP_CNTL
, R300_PVS_NUM_SLOTS(pvs_num_slots
) |
868 R300_PVS_NUM_CNTLRS(pvs_num_controllers
) |
869 R300_PVS_NUM_FPUS(r300screen
->caps
.num_vert_fpus
) |
870 R300_PVS_VF_MAX_VTX_NUM(12) |
871 (r300screen
->caps
.is_r500
? R500_TCL_STATE_OPTIMIZATION
: 0));
873 /* Emit immediates. */
875 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
876 (r300
->screen
->caps
.is_r500
?
877 R500_PVS_CONST_START
: R300_PVS_CONST_START
) +
879 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, imm_count
* 4);
880 for (i
= imm_first
; i
< imm_end
; i
++) {
881 const float *data
= vs
->code
.constants
.Constants
[i
].u
.Immediate
;
882 OUT_CS_TABLE(data
, 4);
888 void r300_emit_vs_constants(struct r300_context
* r300
,
889 unsigned size
, void *state
)
892 ((struct r300_vertex_shader
*)r300
->vs_state
.state
)->externals_count
;
893 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
900 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
901 (r300
->screen
->caps
.is_r500
?
902 R500_PVS_CONST_START
: R300_PVS_CONST_START
));
903 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, count
* 4);
904 OUT_CS_TABLE(buf
->constants
, count
* 4);
908 void r300_emit_viewport_state(struct r300_context
* r300
,
909 unsigned size
, void* state
)
911 struct r300_viewport_state
* viewport
= (struct r300_viewport_state
*)state
;
915 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE
, 6);
916 OUT_CS_TABLE(&viewport
->xscale
, 6);
917 OUT_CS_REG(R300_VAP_VTE_CNTL
, viewport
->vte_control
);
921 void r300_emit_ztop_state(struct r300_context
* r300
,
922 unsigned size
, void* state
)
924 struct r300_ztop_state
* ztop
= (struct r300_ztop_state
*)state
;
928 OUT_CS_REG(R300_ZB_ZTOP
, ztop
->z_buffer_top
);
932 void r300_emit_texture_cache_inval(struct r300_context
* r300
, unsigned size
, void* state
)
937 OUT_CS_REG(R300_TX_INVALTAGS
, 0);
941 void r300_emit_buffer_validate(struct r300_context
*r300
,
942 boolean do_validate_vertex_buffers
,
943 struct pipe_resource
*index_buffer
)
945 struct pipe_framebuffer_state
* fb
=
946 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
947 struct r300_textures_state
*texstate
=
948 (struct r300_textures_state
*)r300
->textures_state
.state
;
949 struct r300_texture
* tex
;
950 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
951 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
952 struct pipe_resource
*pbuf
;
954 boolean invalid
= FALSE
;
956 /* upload buffers first */
957 if (r300
->screen
->caps
.has_tcl
&& r300
->any_user_vbs
) {
958 r300_upload_user_buffers(r300
);
959 r300
->any_user_vbs
= false;
963 r300
->rws
->reset_bos(r300
->rws
);
966 /* Color buffers... */
967 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
968 tex
= r300_texture(fb
->cbufs
[i
]->texture
);
969 assert(tex
&& tex
->buffer
&& "cbuf is marked, but NULL!");
970 if (!r300_add_texture(r300
->rws
, tex
, 0, tex
->domain
)) {
971 r300
->context
.flush(&r300
->context
, 0, NULL
);
975 /* ...depth buffer... */
977 tex
= r300_texture(fb
->zsbuf
->texture
);
978 assert(tex
&& tex
->buffer
&& "zsbuf is marked, but NULL!");
979 if (!r300_add_texture(r300
->rws
, tex
,
981 r300
->context
.flush(&r300
->context
, 0, NULL
);
986 for (i
= 0; i
< texstate
->count
; i
++) {
987 if (!(texstate
->tx_enable
& (1 << i
))) {
991 tex
= r300_texture(texstate
->sampler_views
[i
]->base
.texture
);
992 if (!r300_add_texture(r300
->rws
, tex
, tex
->domain
, 0)) {
993 r300
->context
.flush(&r300
->context
, 0, NULL
);
997 /* ...occlusion query buffer... */
998 if (r300
->query_current
) {
999 if (!r300
->rws
->add_buffer(r300
->rws
, r300
->query_current
->buffer
,
1000 0, r300
->query_current
->domain
)) {
1001 r300
->context
.flush(&r300
->context
, 0, NULL
);
1005 /* ...vertex buffer for SWTCL path... */
1007 if (!r300_add_buffer(r300
->rws
, r300
->vbo
,
1008 r300_buffer(r300
->vbo
)->domain
, 0)) {
1009 r300
->context
.flush(&r300
->context
, 0, NULL
);
1013 /* ...vertex buffers for HWTCL path... */
1014 if (do_validate_vertex_buffers
) {
1015 for (i
= 0; i
< r300
->velems
->count
; i
++) {
1016 pbuf
= vbuf
[velem
[i
].vertex_buffer_index
].buffer
;
1018 if (!r300_add_buffer(r300
->rws
, pbuf
,
1019 r300_buffer(pbuf
)->domain
, 0)) {
1020 r300
->context
.flush(&r300
->context
, 0, NULL
);
1025 /* ...and index buffer for HWTCL path. */
1027 if (!r300_add_buffer(r300
->rws
, index_buffer
,
1028 r300_buffer(index_buffer
)->domain
, 0)) {
1029 r300
->context
.flush(&r300
->context
, 0, NULL
);
1033 if (!r300
->rws
->validate(r300
->rws
)) {
1034 r300
->context
.flush(&r300
->context
, 0, NULL
);
1037 fprintf(stderr
, "r300: Stuck in validation loop, gonna quit now.\n");
1045 unsigned r300_get_num_dirty_dwords(struct r300_context
*r300
)
1047 struct r300_atom
* atom
;
1048 unsigned dwords
= 0;
1050 foreach(atom
, &r300
->atom_list
) {
1052 dwords
+= atom
->size
;
1056 /* let's reserve some more, just in case */
1062 /* Emit all dirty state. */
1063 void r300_emit_dirty_state(struct r300_context
* r300
)
1065 struct r300_atom
* atom
;
1067 foreach(atom
, &r300
->atom_list
) {
1069 atom
->emit(r300
, atom
->size
, atom
->state
);
1070 if (SCREEN_DBG_ON(r300
->screen
, DBG_STATS
)) {
1073 atom
->dirty
= FALSE
;