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_texture.h"
36 #include "r300_screen_buffer.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 WRITE_CS_TABLE(blend
->cb
, size
);
50 WRITE_CS_TABLE(blend
->cb_no_readwrite
, size
);
54 void r300_emit_blend_color_state(struct r300_context
* r300
,
55 unsigned size
, void* state
)
57 struct r300_blend_color_state
* bc
= (struct r300_blend_color_state
*)state
;
60 WRITE_CS_TABLE(bc
->cb
, size
);
63 void r300_emit_clip_state(struct r300_context
* r300
,
64 unsigned size
, void* state
)
66 struct r300_clip_state
* clip
= (struct r300_clip_state
*)state
;
69 WRITE_CS_TABLE(clip
->cb
, size
);
72 void r300_emit_dsa_state(struct r300_context
* r300
, unsigned size
, void* state
)
74 struct r300_dsa_state
* dsa
= (struct r300_dsa_state
*)state
;
75 struct pipe_framebuffer_state
* fb
=
76 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
80 WRITE_CS_TABLE(&dsa
->cb_begin
, size
);
82 WRITE_CS_TABLE(dsa
->cb_no_readwrite
, size
);
86 static const float * get_rc_constant_state(
87 struct r300_context
* r300
,
88 struct rc_constant
* constant
)
90 struct r300_textures_state
* texstate
= r300
->textures_state
.state
;
91 static float vec
[4] = { 0.0, 0.0, 0.0, 1.0 };
92 struct pipe_resource
*tex
;
94 assert(constant
->Type
== RC_CONSTANT_STATE
);
96 switch (constant
->u
.State
[0]) {
97 /* Factor for converting rectangle coords to
98 * normalized coords. Should only show up on non-r500. */
99 case RC_STATE_R300_TEXRECT_FACTOR
:
100 tex
= texstate
->sampler_views
[constant
->u
.State
[1]]->base
.texture
;
101 vec
[0] = 1.0 / tex
->width0
;
102 vec
[1] = 1.0 / tex
->height0
;
105 case RC_STATE_R300_VIEWPORT_SCALE
:
106 vec
[0] = r300
->viewport
.scale
[0];
107 vec
[1] = r300
->viewport
.scale
[1];
108 vec
[2] = r300
->viewport
.scale
[2];
111 case RC_STATE_R300_VIEWPORT_OFFSET
:
112 vec
[0] = r300
->viewport
.translate
[0];
113 vec
[1] = r300
->viewport
.translate
[1];
114 vec
[2] = r300
->viewport
.translate
[2];
118 fprintf(stderr
, "r300: Implementation error: "
119 "Unknown RC_CONSTANT type %d\n", constant
->u
.State
[0]);
122 /* This should either be (0, 0, 0, 1), which should be a relatively safe
123 * RGBA or STRQ value, or it could be one of the RC_CONSTANT_STATE
128 /* Convert a normal single-precision float into the 7.16 format
129 * used by the R300 fragment shader.
131 uint32_t pack_float24(float f
)
139 uint32_t float24
= 0;
146 mantissa
= frexpf(f
, &exponent
);
150 float24
|= (1 << 23);
151 mantissa
= mantissa
* -1.0;
153 /* Handle exponent, bias of 63 */
155 float24
|= (exponent
<< 16);
156 /* Kill 7 LSB of mantissa */
157 float24
|= (u
.u
& 0x7FFFFF) >> 7;
162 void r300_emit_fs(struct r300_context
* r300
, unsigned size
, void *state
)
164 struct r300_fragment_shader
*fs
= r300_fs(r300
);
167 WRITE_CS_TABLE(fs
->shader
->cb_code
, fs
->shader
->cb_code_size
);
170 void r300_emit_fs_constants(struct r300_context
* r300
, unsigned size
, void *state
)
172 struct r300_fragment_shader
*fs
= r300_fs(r300
);
173 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
174 unsigned count
= fs
->shader
->externals_count
* 4;
181 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X
, count
);
182 OUT_CS_TABLE(buf
->constants
, count
);
186 void r300_emit_fs_rc_constant_state(struct r300_context
* r300
, unsigned size
, void *state
)
188 struct r300_fragment_shader
*fs
= r300_fs(r300
);
189 struct rc_constant_list
*constants
= &fs
->shader
->code
.constants
;
191 unsigned count
= fs
->shader
->rc_state_count
;
192 unsigned first
= fs
->shader
->externals_count
;
193 unsigned end
= constants
->Count
;
202 for(i
= first
; i
< end
; ++i
) {
203 if (constants
->Constants
[i
].Type
== RC_CONSTANT_STATE
) {
205 get_rc_constant_state(r300
, &constants
->Constants
[i
]);
207 for (j
= 0; j
< 4; j
++)
208 cdata
[j
] = pack_float24(data
[j
]);
210 OUT_CS_REG_SEQ(R300_PFS_PARAM_0_X
+ i
* 16, 4);
211 OUT_CS_TABLE(cdata
, 4);
217 void r500_emit_fs(struct r300_context
* r300
, unsigned size
, void *state
)
219 struct r300_fragment_shader
*fs
= r300_fs(r300
);
222 WRITE_CS_TABLE(fs
->shader
->cb_code
, fs
->shader
->cb_code_size
);
225 void r500_emit_fs_constants(struct r300_context
* r300
, unsigned size
, void *state
)
227 struct r300_fragment_shader
*fs
= r300_fs(r300
);
228 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
229 unsigned count
= fs
->shader
->externals_count
* 4;
236 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
, R500_GA_US_VECTOR_INDEX_TYPE_CONST
);
237 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, count
);
238 OUT_CS_TABLE(buf
->constants
, count
);
242 void r500_emit_fs_rc_constant_state(struct r300_context
* r300
, unsigned size
, void *state
)
244 struct r300_fragment_shader
*fs
= r300_fs(r300
);
245 struct rc_constant_list
*constants
= &fs
->shader
->code
.constants
;
247 unsigned count
= fs
->shader
->rc_state_count
;
248 unsigned first
= fs
->shader
->externals_count
;
249 unsigned end
= constants
->Count
;
256 for(i
= first
; i
< end
; ++i
) {
257 if (constants
->Constants
[i
].Type
== RC_CONSTANT_STATE
) {
259 get_rc_constant_state(r300
, &constants
->Constants
[i
]);
261 OUT_CS_REG(R500_GA_US_VECTOR_INDEX
,
262 R500_GA_US_VECTOR_INDEX_TYPE_CONST
|
263 (i
& R500_GA_US_VECTOR_INDEX_MASK
));
264 OUT_CS_ONE_REG(R500_GA_US_VECTOR_DATA
, 4);
265 OUT_CS_TABLE(data
, 4);
271 void r300_emit_gpu_flush(struct r300_context
*r300
, unsigned size
, void *state
)
273 struct r300_gpu_flush
*gpuflush
= (struct r300_gpu_flush
*)state
;
274 struct pipe_framebuffer_state
* fb
=
275 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
276 uint32_t height
= fb
->height
;
277 uint32_t width
= fb
->width
;
280 if (r300
->cbzb_clear
) {
281 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
283 height
= surf
->cbzb_height
;
284 width
= surf
->cbzb_width
;
290 * By writing to the SC registers, SC & US assert idle. */
291 OUT_CS_REG_SEQ(R300_SC_SCISSORS_TL
, 2);
292 if (r300
->screen
->caps
.is_r500
) {
294 OUT_CS(((width
- 1) << R300_SCISSORS_X_SHIFT
) |
295 ((height
- 1) << R300_SCISSORS_Y_SHIFT
));
297 OUT_CS((1440 << R300_SCISSORS_X_SHIFT
) |
298 (1440 << R300_SCISSORS_Y_SHIFT
));
299 OUT_CS(((width
+ 1440-1) << R300_SCISSORS_X_SHIFT
) |
300 ((height
+ 1440-1) << R300_SCISSORS_Y_SHIFT
));
303 /* Flush CB & ZB caches and wait until the 3D engine is idle and clean. */
304 OUT_CS_TABLE(gpuflush
->cb_flush_clean
, 6);
308 void r300_emit_aa_state(struct r300_context
*r300
, unsigned size
, void *state
)
310 struct r300_aa_state
*aa
= (struct r300_aa_state
*)state
;
314 OUT_CS_REG(R300_GB_AA_CONFIG
, aa
->aa_config
);
317 OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_OFFSET
, 1);
318 OUT_CS_RELOC(aa
->dest
->buffer
, aa
->dest
->offset
, 0, aa
->dest
->domain
, 0);
320 OUT_CS_REG_SEQ(R300_RB3D_AARESOLVE_PITCH
, 1);
321 OUT_CS_RELOC(aa
->dest
->buffer
, aa
->dest
->pitch
, 0, aa
->dest
->domain
, 0);
324 OUT_CS_REG(R300_RB3D_AARESOLVE_CTL
, aa
->aaresolve_ctl
);
328 void r300_emit_fb_state(struct r300_context
* r300
, unsigned size
, void* state
)
330 struct pipe_framebuffer_state
* fb
= (struct pipe_framebuffer_state
*)state
;
331 struct r300_surface
* surf
;
337 /* NUM_MULTIWRITES replicates COLOR[0] to all colorbuffers, which is not
338 * what we usually want. */
339 if (r300
->screen
->caps
.is_r500
) {
340 OUT_CS_REG(R300_RB3D_CCTL
,
341 R300_RB3D_CCTL_INDEPENDENT_COLORFORMAT_ENABLE_ENABLE
);
343 OUT_CS_REG(R300_RB3D_CCTL
, 0);
346 /* Set up colorbuffers. */
347 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
348 surf
= r300_surface(fb
->cbufs
[i
]);
350 OUT_CS_REG_SEQ(R300_RB3D_COLOROFFSET0
+ (4 * i
), 1);
351 OUT_CS_RELOC(surf
->buffer
, surf
->offset
, 0, surf
->domain
, 0);
353 OUT_CS_REG_SEQ(R300_RB3D_COLORPITCH0
+ (4 * i
), 1);
354 OUT_CS_RELOC(surf
->buffer
, surf
->pitch
, 0, surf
->domain
, 0);
357 /* Set up the ZB part of the CBZB clear. */
358 if (r300
->cbzb_clear
) {
359 surf
= r300_surface(fb
->cbufs
[0]);
361 OUT_CS_REG(R300_ZB_FORMAT
, surf
->cbzb_format
);
363 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET
, 1);
364 OUT_CS_RELOC(surf
->buffer
, surf
->cbzb_midpoint_offset
, 0, surf
->domain
, 0);
366 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH
, 1);
367 OUT_CS_RELOC(surf
->buffer
, surf
->cbzb_pitch
, 0, surf
->domain
, 0);
369 /* Set up a zbuffer. */
370 else if (fb
->zsbuf
) {
371 surf
= r300_surface(fb
->zsbuf
);
373 OUT_CS_REG(R300_ZB_FORMAT
, surf
->format
);
375 OUT_CS_REG_SEQ(R300_ZB_DEPTHOFFSET
, 1);
376 OUT_CS_RELOC(surf
->buffer
, surf
->offset
, 0, surf
->domain
, 0);
378 OUT_CS_REG_SEQ(R300_ZB_DEPTHPITCH
, 1);
379 OUT_CS_RELOC(surf
->buffer
, surf
->pitch
, 0, surf
->domain
, 0);
382 if (r300
->screen
->caps
.has_hiz
) {
383 OUT_CS_REG(R300_ZB_HIZ_OFFSET
, 0);
384 OUT_CS_REG(R300_ZB_HIZ_PITCH
, 0);
387 /* Z Mask RAM. (compressed zbuffer) */
388 OUT_CS_REG(R300_ZB_ZMASK_OFFSET
, 0);
389 OUT_CS_REG(R300_ZB_ZMASK_PITCH
, 0);
395 void r300_emit_hyperz_state(struct r300_context
*r300
,
396 unsigned size
, void *state
)
399 WRITE_CS_TABLE(state
, size
);
402 void r300_emit_hyperz_end(struct r300_context
*r300
)
404 struct r300_hyperz_state z
=
405 *(struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
408 z
.zb_depthclearvalue
= 0;
409 z
.sc_hyperz
= R300_SC_HYPERZ_ADJ_2
;
411 r300_emit_hyperz_state(r300
, r300
->hyperz_state
.size
, &z
);
414 void r300_emit_fb_state_pipelined(struct r300_context
*r300
,
415 unsigned size
, void *state
)
417 struct pipe_framebuffer_state
* fb
=
418 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
424 /* Colorbuffer format in the US block.
425 * (must be written after unpipelined regs) */
426 OUT_CS_REG_SEQ(R300_US_OUT_FMT_0
, 4);
427 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
428 OUT_CS(r300_surface(fb
->cbufs
[i
])->format
);
431 OUT_CS(R300_US_OUT_FMT_UNUSED
);
434 /* Multisampling. Depends on framebuffer sample count.
435 * These are pipelined regs and as such cannot be moved
436 * to the AA state. */
437 if (r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
)) {
438 unsigned mspos0
= 0x66666666;
439 unsigned mspos1
= 0x6666666;
441 if (fb
->nr_cbufs
&& fb
->cbufs
[0]->texture
->nr_samples
> 1) {
442 /* Subsample placement. These may not be optimal. */
443 switch (fb
->cbufs
[0]->texture
->nr_samples
) {
461 debug_printf("r300: Bad number of multisamples!\n");
465 OUT_CS_REG_SEQ(R300_GB_MSPOS0
, 2);
472 void r300_emit_query_start(struct r300_context
*r300
, unsigned size
, void*state
)
474 struct r300_query
*query
= r300
->query_current
;
481 if (r300
->screen
->caps
.family
== CHIP_FAMILY_RV530
) {
482 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
484 OUT_CS_REG(R300_SU_REG_DEST
, R300_RASTER_PIPE_SELECT_ALL
);
486 OUT_CS_REG(R300_ZB_ZPASS_DATA
, 0);
488 query
->begin_emitted
= TRUE
;
489 query
->flushed
= FALSE
;
492 static void r300_emit_query_end_frag_pipes(struct r300_context
*r300
,
493 struct r300_query
*query
)
495 struct r300_capabilities
* caps
= &r300
->screen
->caps
;
496 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
499 assert(caps
->num_frag_pipes
);
501 BEGIN_CS(6 * caps
->num_frag_pipes
+ 2);
502 /* I'm not so sure I like this switch, but it's hard to be elegant
503 * when there's so many special cases...
505 * So here's the basic idea. For each pipe, enable writes to it only,
506 * then put out the relocation for ZPASS_ADDR, taking into account a
507 * 4-byte offset for each pipe. RV380 and older are special; they have
508 * only two pipes, and the second pipe's enable is on bit 3, not bit 1,
509 * so there's a chipset cap for that. */
510 switch (caps
->num_frag_pipes
) {
513 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 3);
514 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
515 OUT_CS_RELOC(buf
, (query
->num_results
+ 3) * 4,
516 0, query
->domain
, 0);
519 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 2);
520 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
521 OUT_CS_RELOC(buf
, (query
->num_results
+ 2) * 4,
522 0, query
->domain
, 0);
525 /* As mentioned above, accomodate RV380 and older. */
526 OUT_CS_REG(R300_SU_REG_DEST
,
527 1 << (caps
->high_second_pipe
? 3 : 1));
528 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
529 OUT_CS_RELOC(buf
, (query
->num_results
+ 1) * 4,
530 0, query
->domain
, 0);
533 OUT_CS_REG(R300_SU_REG_DEST
, 1 << 0);
534 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
535 OUT_CS_RELOC(buf
, (query
->num_results
+ 0) * 4,
536 0, query
->domain
, 0);
539 fprintf(stderr
, "r300: Implementation error: Chipset reports %d"
540 " pixel pipes!\n", caps
->num_frag_pipes
);
544 /* And, finally, reset it to normal... */
545 OUT_CS_REG(R300_SU_REG_DEST
, 0xF);
549 static void rv530_emit_query_end_single_z(struct r300_context
*r300
,
550 struct r300_query
*query
)
552 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
556 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
557 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
558 OUT_CS_RELOC(buf
, query
->num_results
* 4, 0, query
->domain
, 0);
559 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
563 static void rv530_emit_query_end_double_z(struct r300_context
*r300
,
564 struct r300_query
*query
)
566 struct r300_winsys_buffer
*buf
= r300
->query_current
->buffer
;
570 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_0
);
571 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
572 OUT_CS_RELOC(buf
, (query
->num_results
+ 0) * 4, 0, query
->domain
, 0);
573 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_1
);
574 OUT_CS_REG_SEQ(R300_ZB_ZPASS_ADDR
, 1);
575 OUT_CS_RELOC(buf
, (query
->num_results
+ 1) * 4, 0, query
->domain
, 0);
576 OUT_CS_REG(RV530_FG_ZBREG_DEST
, RV530_FG_ZBREG_DEST_PIPE_SELECT_ALL
);
580 void r300_emit_query_end(struct r300_context
* r300
)
582 struct r300_capabilities
*caps
= &r300
->screen
->caps
;
583 struct r300_query
*query
= r300
->query_current
;
588 if (query
->begin_emitted
== FALSE
)
591 if (caps
->family
== CHIP_FAMILY_RV530
) {
592 if (caps
->num_z_pipes
== 2)
593 rv530_emit_query_end_double_z(r300
, query
);
595 rv530_emit_query_end_single_z(r300
, query
);
597 r300_emit_query_end_frag_pipes(r300
, query
);
599 query
->begin_emitted
= FALSE
;
600 query
->num_results
+= query
->num_pipes
;
602 /* XXX grab all the results and reset the counter. */
603 if (query
->num_results
>= query
->buffer_size
/ 4 - 4) {
604 query
->num_results
= (query
->buffer_size
/ 4) / 2;
605 fprintf(stderr
, "r300: Rewinding OQBO...\n");
609 void r300_emit_invariant_state(struct r300_context
*r300
,
610 unsigned size
, void *state
)
613 WRITE_CS_TABLE(state
, size
);
616 void r300_emit_rs_state(struct r300_context
* r300
, unsigned size
, void* state
)
618 struct r300_rs_state
* rs
= state
;
622 OUT_CS_TABLE(rs
->cb_main
, 25);
623 if (rs
->polygon_offset_enable
) {
624 if (r300
->zbuffer_bpp
== 16) {
625 OUT_CS_TABLE(rs
->cb_poly_offset_zb16
, 5);
627 OUT_CS_TABLE(rs
->cb_poly_offset_zb24
, 5);
633 void r300_emit_rs_block_state(struct r300_context
* r300
,
634 unsigned size
, void* state
)
636 struct r300_rs_block
* rs
= (struct r300_rs_block
*)state
;
638 /* It's the same for both INST and IP tables */
639 unsigned count
= (rs
->inst_count
& R300_RS_INST_COUNT_MASK
) + 1;
642 if (SCREEN_DBG_ON(r300
->screen
, DBG_DRAW
)) {
643 r500_dump_rs_block(rs
);
645 fprintf(stderr
, "r300: RS emit:\n");
647 for (i
= 0; i
< count
; i
++)
648 fprintf(stderr
, " : ip %d: 0x%08x\n", i
, rs
->ip
[i
]);
650 for (i
= 0; i
< count
; i
++)
651 fprintf(stderr
, " : inst %d: 0x%08x\n", i
, rs
->inst
[i
]);
653 fprintf(stderr
, " : count: 0x%08x inst_count: 0x%08x\n",
654 rs
->count
, rs
->inst_count
);
658 OUT_CS_REG_SEQ(R300_VAP_VTX_STATE_CNTL
, 2);
659 OUT_CS(rs
->vap_vtx_state_cntl
);
660 OUT_CS(rs
->vap_vsm_vtx_assm
);
661 OUT_CS_REG_SEQ(R300_VAP_OUTPUT_VTX_FMT_0
, 2);
662 OUT_CS(rs
->vap_out_vtx_fmt
[0]);
663 OUT_CS(rs
->vap_out_vtx_fmt
[1]);
665 if (r300
->screen
->caps
.is_r500
) {
666 OUT_CS_REG_SEQ(R500_RS_IP_0
, count
);
668 OUT_CS_REG_SEQ(R300_RS_IP_0
, count
);
670 OUT_CS_TABLE(rs
->ip
, count
);
672 OUT_CS_REG_SEQ(R300_RS_COUNT
, 2);
674 OUT_CS(rs
->inst_count
);
676 if (r300
->screen
->caps
.is_r500
) {
677 OUT_CS_REG_SEQ(R500_RS_INST_0
, count
);
679 OUT_CS_REG_SEQ(R300_RS_INST_0
, count
);
681 OUT_CS_TABLE(rs
->inst
, count
);
685 void r300_emit_scissor_state(struct r300_context
* r300
,
686 unsigned size
, void* state
)
688 struct pipe_scissor_state
* scissor
= (struct pipe_scissor_state
*)state
;
692 OUT_CS_REG_SEQ(R300_SC_CLIPRECT_TL_0
, 2);
693 if (r300
->screen
->caps
.is_r500
) {
694 OUT_CS((scissor
->minx
<< R300_CLIPRECT_X_SHIFT
) |
695 (scissor
->miny
<< R300_CLIPRECT_Y_SHIFT
));
696 OUT_CS(((scissor
->maxx
- 1) << R300_CLIPRECT_X_SHIFT
) |
697 ((scissor
->maxy
- 1) << R300_CLIPRECT_Y_SHIFT
));
699 OUT_CS(((scissor
->minx
+ 1440) << R300_CLIPRECT_X_SHIFT
) |
700 ((scissor
->miny
+ 1440) << R300_CLIPRECT_Y_SHIFT
));
701 OUT_CS(((scissor
->maxx
+ 1440-1) << R300_CLIPRECT_X_SHIFT
) |
702 ((scissor
->maxy
+ 1440-1) << R300_CLIPRECT_Y_SHIFT
));
707 void r300_emit_textures_state(struct r300_context
*r300
,
708 unsigned size
, void *state
)
710 struct r300_textures_state
*allstate
= (struct r300_textures_state
*)state
;
711 struct r300_texture_sampler_state
*texstate
;
712 struct r300_texture
*tex
;
717 OUT_CS_REG(R300_TX_ENABLE
, allstate
->tx_enable
);
719 for (i
= 0; i
< allstate
->count
; i
++) {
720 if ((1 << i
) & allstate
->tx_enable
) {
721 texstate
= &allstate
->regs
[i
];
722 tex
= r300_texture(allstate
->sampler_views
[i
]->base
.texture
);
724 OUT_CS_REG(R300_TX_FILTER0_0
+ (i
* 4), texstate
->filter0
);
725 OUT_CS_REG(R300_TX_FILTER1_0
+ (i
* 4), texstate
->filter1
);
726 OUT_CS_REG(R300_TX_BORDER_COLOR_0
+ (i
* 4),
727 texstate
->border_color
);
729 OUT_CS_REG(R300_TX_FORMAT0_0
+ (i
* 4), texstate
->format
.format0
);
730 OUT_CS_REG(R300_TX_FORMAT1_0
+ (i
* 4), texstate
->format
.format1
);
731 OUT_CS_REG(R300_TX_FORMAT2_0
+ (i
* 4), texstate
->format
.format2
);
733 OUT_CS_REG_SEQ(R300_TX_OFFSET_0
+ (i
* 4), 1);
734 OUT_CS_TEX_RELOC(tex
, texstate
->format
.tile_config
, tex
->domain
,
741 void r300_emit_aos(struct r300_context
* r300
, int offset
, boolean indexed
)
743 struct pipe_vertex_buffer
*vb1
, *vb2
, *vbuf
= r300
->vertex_buffer
;
744 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
745 struct r300_buffer
*buf
;
747 unsigned *hw_format_size
= r300
->velems
->hw_format_size
;
748 unsigned size1
, size2
, aos_count
= r300
->velems
->count
;
749 unsigned packet_size
= (aos_count
* 3 + 1) / 2;
752 BEGIN_CS(2 + packet_size
+ aos_count
* 2);
753 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, packet_size
);
754 OUT_CS(aos_count
| (!indexed
? R300_VC_FORCE_PREFETCH
: 0));
756 for (i
= 0; i
< aos_count
- 1; i
+= 2) {
757 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
758 vb2
= &vbuf
[velem
[i
+1].vertex_buffer_index
];
759 size1
= hw_format_size
[i
];
760 size2
= hw_format_size
[i
+1];
762 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
) |
763 R300_VBPNTR_SIZE1(size2
) | R300_VBPNTR_STRIDE1(vb2
->stride
));
764 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
765 OUT_CS(vb2
->buffer_offset
+ velem
[i
+1].src_offset
+ offset
* vb2
->stride
);
769 vb1
= &vbuf
[velem
[i
].vertex_buffer_index
];
770 size1
= hw_format_size
[i
];
772 OUT_CS(R300_VBPNTR_SIZE0(size1
) | R300_VBPNTR_STRIDE0(vb1
->stride
));
773 OUT_CS(vb1
->buffer_offset
+ velem
[i
].src_offset
+ offset
* vb1
->stride
);
776 for (i
= 0; i
< aos_count
; i
++) {
777 buf
= r300_buffer(vbuf
[velem
[i
].vertex_buffer_index
].buffer
);
778 OUT_CS_BUF_RELOC_NO_OFFSET(&buf
->b
.b
, buf
->domain
, 0, 0);
783 void r300_emit_aos_swtcl(struct r300_context
*r300
, boolean indexed
)
787 DBG(r300
, DBG_DRAW
, "r300: Preparing vertex buffer %p for render, "
788 "vertex size %d\n", r300
->vbo
,
789 r300
->vertex_info
.size
);
790 /* Set the pointer to our vertex buffer. The emitted values are this:
791 * PACKET3 [3D_LOAD_VBPNTR]
793 * FORMAT [size | stride << 8]
794 * OFFSET [offset into BO]
795 * VBPNTR [relocated BO]
798 OUT_CS_PKT3(R300_PACKET3_3D_LOAD_VBPNTR
, 3);
799 OUT_CS(1 | (!indexed
? R300_VC_FORCE_PREFETCH
: 0));
800 OUT_CS(r300
->vertex_info
.size
|
801 (r300
->vertex_info
.size
<< 8));
802 OUT_CS(r300
->vbo_offset
);
803 OUT_CS_BUF_RELOC(r300
->vbo
, 0, r300_buffer(r300
->vbo
)->domain
, 0, 0);
807 void r300_emit_vertex_stream_state(struct r300_context
* r300
,
808 unsigned size
, void* state
)
810 struct r300_vertex_stream_state
*streams
=
811 (struct r300_vertex_stream_state
*)state
;
815 if (DBG_ON(r300
, DBG_DRAW
)) {
816 fprintf(stderr
, "r300: PSC emit:\n");
818 for (i
= 0; i
< streams
->count
; i
++) {
819 fprintf(stderr
, " : prog_stream_cntl%d: 0x%08x\n", i
,
820 streams
->vap_prog_stream_cntl
[i
]);
823 for (i
= 0; i
< streams
->count
; i
++) {
824 fprintf(stderr
, " : prog_stream_cntl_ext%d: 0x%08x\n", i
,
825 streams
->vap_prog_stream_cntl_ext
[i
]);
830 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_0
, streams
->count
);
831 OUT_CS_TABLE(streams
->vap_prog_stream_cntl
, streams
->count
);
832 OUT_CS_REG_SEQ(R300_VAP_PROG_STREAM_CNTL_EXT_0
, streams
->count
);
833 OUT_CS_TABLE(streams
->vap_prog_stream_cntl_ext
, streams
->count
);
837 void r300_emit_pvs_flush(struct r300_context
* r300
, unsigned size
, void* state
)
842 OUT_CS_REG(R300_VAP_PVS_STATE_FLUSH_REG
, 0x0);
846 void r300_emit_vap_invariant_state(struct r300_context
*r300
,
847 unsigned size
, void *state
)
850 WRITE_CS_TABLE(state
, size
);
853 void r300_emit_vs_state(struct r300_context
* r300
, unsigned size
, void* state
)
855 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)state
;
856 struct r300_vertex_program_code
* code
= &vs
->code
;
857 struct r300_screen
* r300screen
= r300
->screen
;
858 unsigned instruction_count
= code
->length
/ 4;
861 unsigned vtx_mem_size
= r300screen
->caps
.is_r500
? 128 : 72;
862 unsigned input_count
= MAX2(util_bitcount(code
->InputsRead
), 1);
863 unsigned output_count
= MAX2(util_bitcount(code
->OutputsWritten
), 1);
864 unsigned temp_count
= MAX2(code
->num_temporaries
, 1);
866 unsigned pvs_num_slots
= MIN3(vtx_mem_size
/ input_count
,
867 vtx_mem_size
/ output_count
, 10);
868 unsigned pvs_num_controllers
= MIN2(vtx_mem_size
/ temp_count
, 6);
870 unsigned imm_first
= vs
->externals_count
;
871 unsigned imm_end
= vs
->code
.constants
.Count
;
872 unsigned imm_count
= vs
->immediates_count
;
878 /* R300_VAP_PVS_CODE_CNTL_0
879 * R300_VAP_PVS_CONST_CNTL
880 * R300_VAP_PVS_CODE_CNTL_1
881 * See the r5xx docs for instructions on how to use these. */
882 OUT_CS_REG_SEQ(R300_VAP_PVS_CODE_CNTL_0
, 3);
883 OUT_CS(R300_PVS_FIRST_INST(0) |
884 R300_PVS_XYZW_VALID_INST(instruction_count
- 1) |
885 R300_PVS_LAST_INST(instruction_count
- 1));
886 OUT_CS(R300_PVS_MAX_CONST_ADDR(code
->constants
.Count
- 1));
887 OUT_CS(instruction_count
- 1);
889 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
, 0);
890 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, code
->length
);
891 OUT_CS_TABLE(code
->body
.d
, code
->length
);
893 OUT_CS_REG(R300_VAP_CNTL
, R300_PVS_NUM_SLOTS(pvs_num_slots
) |
894 R300_PVS_NUM_CNTLRS(pvs_num_controllers
) |
895 R300_PVS_NUM_FPUS(r300screen
->caps
.num_vert_fpus
) |
896 R300_PVS_VF_MAX_VTX_NUM(12) |
897 (r300screen
->caps
.is_r500
? R500_TCL_STATE_OPTIMIZATION
: 0));
899 /* Emit immediates. */
901 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
902 (r300
->screen
->caps
.is_r500
?
903 R500_PVS_CONST_START
: R300_PVS_CONST_START
) +
905 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, imm_count
* 4);
906 for (i
= imm_first
; i
< imm_end
; i
++) {
907 const float *data
= vs
->code
.constants
.Constants
[i
].u
.Immediate
;
908 OUT_CS_TABLE(data
, 4);
914 void r300_emit_vs_constants(struct r300_context
* r300
,
915 unsigned size
, void *state
)
918 ((struct r300_vertex_shader
*)r300
->vs_state
.state
)->externals_count
;
919 struct r300_constant_buffer
*buf
= (struct r300_constant_buffer
*)state
;
926 OUT_CS_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
927 (r300
->screen
->caps
.is_r500
?
928 R500_PVS_CONST_START
: R300_PVS_CONST_START
));
929 OUT_CS_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, count
* 4);
930 OUT_CS_TABLE(buf
->constants
, count
* 4);
934 void r300_emit_viewport_state(struct r300_context
* r300
,
935 unsigned size
, void* state
)
937 struct r300_viewport_state
* viewport
= (struct r300_viewport_state
*)state
;
941 OUT_CS_REG_SEQ(R300_SE_VPORT_XSCALE
, 6);
942 OUT_CS_TABLE(&viewport
->xscale
, 6);
943 OUT_CS_REG(R300_VAP_VTE_CNTL
, viewport
->vte_control
);
947 void r300_emit_ztop_state(struct r300_context
* r300
,
948 unsigned size
, void* state
)
950 struct r300_ztop_state
* ztop
= (struct r300_ztop_state
*)state
;
954 OUT_CS_REG(R300_ZB_ZTOP
, ztop
->z_buffer_top
);
958 void r300_emit_texture_cache_inval(struct r300_context
* r300
, unsigned size
, void* state
)
963 OUT_CS_REG(R300_TX_INVALTAGS
, 0);
967 void r300_emit_buffer_validate(struct r300_context
*r300
,
968 boolean do_validate_vertex_buffers
,
969 struct pipe_resource
*index_buffer
)
971 struct pipe_framebuffer_state
* fb
=
972 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
973 struct r300_textures_state
*texstate
=
974 (struct r300_textures_state
*)r300
->textures_state
.state
;
975 struct r300_texture
* tex
;
976 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
977 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
978 struct pipe_resource
*pbuf
;
980 boolean invalid
= FALSE
;
982 /* upload buffers first */
983 if (r300
->screen
->caps
.has_tcl
&& r300
->any_user_vbs
) {
984 r300_upload_user_buffers(r300
);
985 r300
->any_user_vbs
= false;
989 r300
->rws
->reset_bos(r300
->rws
);
992 /* Color buffers... */
993 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
994 tex
= r300_texture(fb
->cbufs
[i
]->texture
);
995 assert(tex
&& tex
->buffer
&& "cbuf is marked, but NULL!");
996 if (!r300_add_texture(r300
->rws
, tex
, 0,
997 r300_surface(fb
->cbufs
[i
])->domain
)) {
998 r300
->context
.flush(&r300
->context
, 0, NULL
);
1002 /* ...depth buffer... */
1004 tex
= r300_texture(fb
->zsbuf
->texture
);
1005 assert(tex
&& tex
->buffer
&& "zsbuf is marked, but NULL!");
1006 if (!r300_add_texture(r300
->rws
, tex
, 0,
1007 r300_surface(fb
->zsbuf
)->domain
)) {
1008 r300
->context
.flush(&r300
->context
, 0, NULL
);
1012 /* ...textures... */
1013 for (i
= 0; i
< texstate
->count
; i
++) {
1014 if (!(texstate
->tx_enable
& (1 << i
))) {
1018 tex
= r300_texture(texstate
->sampler_views
[i
]->base
.texture
);
1019 if (!r300_add_texture(r300
->rws
, tex
, tex
->domain
, 0)) {
1020 r300
->context
.flush(&r300
->context
, 0, NULL
);
1024 /* ...occlusion query buffer... */
1025 if (r300
->query_current
) {
1026 if (!r300
->rws
->add_buffer(r300
->rws
, r300
->query_current
->buffer
,
1027 0, r300
->query_current
->domain
)) {
1028 r300
->context
.flush(&r300
->context
, 0, NULL
);
1032 /* ...vertex buffer for SWTCL path... */
1034 if (!r300_add_buffer(r300
->rws
, r300
->vbo
,
1035 r300_buffer(r300
->vbo
)->domain
, 0)) {
1036 r300
->context
.flush(&r300
->context
, 0, NULL
);
1040 /* ...vertex buffers for HWTCL path... */
1041 if (do_validate_vertex_buffers
) {
1042 for (i
= 0; i
< r300
->velems
->count
; i
++) {
1043 pbuf
= vbuf
[velem
[i
].vertex_buffer_index
].buffer
;
1045 if (!r300_add_buffer(r300
->rws
, pbuf
,
1046 r300_buffer(pbuf
)->domain
, 0)) {
1047 r300
->context
.flush(&r300
->context
, 0, NULL
);
1052 /* ...and index buffer for HWTCL path. */
1054 if (!r300_add_buffer(r300
->rws
, index_buffer
,
1055 r300_buffer(index_buffer
)->domain
, 0)) {
1056 r300
->context
.flush(&r300
->context
, 0, NULL
);
1060 if (!r300
->rws
->validate(r300
->rws
)) {
1061 r300
->context
.flush(&r300
->context
, 0, NULL
);
1064 fprintf(stderr
, "r300: Stuck in validation loop, gonna quit now.\n");
1072 unsigned r300_get_num_dirty_dwords(struct r300_context
*r300
)
1074 struct r300_atom
* atom
;
1075 unsigned dwords
= 0;
1077 foreach(atom
, &r300
->atom_list
) {
1079 dwords
+= atom
->size
;
1083 /* let's reserve some more, just in case */
1089 /* Emit all dirty state. */
1090 void r300_emit_dirty_state(struct r300_context
* r300
)
1092 struct r300_atom
* atom
;
1094 foreach(atom
, &r300
->atom_list
) {
1096 atom
->emit(r300
, atom
->size
, atom
->state
);
1097 if (SCREEN_DBG_ON(r300
->screen
, DBG_STATS
)) {
1100 atom
->dirty
= FALSE
;