2 * Copyright 2010 Red Hat Inc.
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 * Authors: Dave Airlie <airlied@redhat.com>
25 * Jerome Glisse <jglisse@redhat.com>
27 #include "r600_formats.h"
30 #include "util/u_draw_quad.h"
31 #include "util/u_upload_mgr.h"
32 #include "tgsi/tgsi_parse.h"
35 #define R600_PRIM_RECTANGLE_LIST PIPE_PRIM_MAX
37 static void r600_emit_command_buffer(struct r600_context
*rctx
, struct r600_atom
*atom
)
39 struct radeon_winsys_cs
*cs
= rctx
->cs
;
40 struct r600_command_buffer
*cb
= (struct r600_command_buffer
*)atom
;
42 assert(cs
->cdw
+ cb
->atom
.num_dw
<= RADEON_MAX_CMDBUF_DWORDS
);
43 memcpy(cs
->buf
+ cs
->cdw
, cb
->buf
, 4 * cb
->atom
.num_dw
);
44 cs
->cdw
+= cb
->atom
.num_dw
;
47 void r600_init_command_buffer(struct r600_context
*rctx
, struct r600_command_buffer
*cb
, unsigned id
, unsigned num_dw
)
49 r600_init_atom(rctx
, &cb
->atom
, id
, r600_emit_command_buffer
, 0);
50 cb
->buf
= CALLOC(1, 4 * num_dw
);
51 cb
->max_num_dw
= num_dw
;
54 void r600_release_command_buffer(struct r600_command_buffer
*cb
)
59 void r600_init_atom(struct r600_context
*rctx
,
60 struct r600_atom
*atom
,
62 void (*emit
)(struct r600_context
*ctx
, struct r600_atom
*state
),
65 assert(id
< R600_NUM_ATOMS
);
66 assert(rctx
->atoms
[id
] == NULL
);
67 rctx
->atoms
[id
] = atom
;
70 atom
->num_dw
= num_dw
;
74 void r600_emit_alphatest_state(struct r600_context
*rctx
, struct r600_atom
*atom
)
76 struct radeon_winsys_cs
*cs
= rctx
->cs
;
77 struct r600_alphatest_state
*a
= (struct r600_alphatest_state
*)atom
;
78 unsigned alpha_ref
= a
->sx_alpha_ref
;
80 if (rctx
->chip_class
>= EVERGREEN
&& a
->cb0_export_16bpc
) {
84 r600_write_context_reg(cs
, R_028410_SX_ALPHA_TEST_CONTROL
,
85 a
->sx_alpha_test_control
|
86 S_028410_ALPHA_TEST_BYPASS(a
->bypass
));
87 r600_write_context_reg(cs
, R_028438_SX_ALPHA_REF
, alpha_ref
);
90 static void r600_texture_barrier(struct pipe_context
*ctx
)
92 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
94 rctx
->flags
|= R600_CONTEXT_CB_FLUSH
| R600_CONTEXT_TEX_FLUSH
;
97 if (rctx
->chip_class
== R600
) {
98 rctx
->flags
|= R600_CONTEXT_FLUSH_AND_INV
;
102 static bool r600_conv_pipe_prim(unsigned pprim
, unsigned *prim
)
104 static const int prim_conv
[] = {
105 V_008958_DI_PT_POINTLIST
,
106 V_008958_DI_PT_LINELIST
,
107 V_008958_DI_PT_LINELOOP
,
108 V_008958_DI_PT_LINESTRIP
,
109 V_008958_DI_PT_TRILIST
,
110 V_008958_DI_PT_TRISTRIP
,
111 V_008958_DI_PT_TRIFAN
,
112 V_008958_DI_PT_QUADLIST
,
113 V_008958_DI_PT_QUADSTRIP
,
114 V_008958_DI_PT_POLYGON
,
119 V_008958_DI_PT_RECTLIST
122 *prim
= prim_conv
[pprim
];
124 fprintf(stderr
, "%s:%d unsupported %d\n", __func__
, __LINE__
, pprim
);
130 /* common state between evergreen and r600 */
132 static void r600_bind_blend_state_internal(struct r600_context
*rctx
,
133 struct r600_pipe_blend
*blend
)
135 struct r600_pipe_state
*rstate
;
136 bool update_cb
= false;
138 rstate
= &blend
->rstate
;
139 rctx
->states
[rstate
->id
] = rstate
;
140 r600_context_pipe_state_set(rctx
, rstate
);
142 if (rctx
->cb_misc_state
.blend_colormask
!= blend
->cb_target_mask
) {
143 rctx
->cb_misc_state
.blend_colormask
= blend
->cb_target_mask
;
146 if (rctx
->chip_class
<= R700
&&
147 rctx
->cb_misc_state
.cb_color_control
!= blend
->cb_color_control
) {
148 rctx
->cb_misc_state
.cb_color_control
= blend
->cb_color_control
;
151 if (rctx
->cb_misc_state
.dual_src_blend
!= blend
->dual_src_blend
) {
152 rctx
->cb_misc_state
.dual_src_blend
= blend
->dual_src_blend
;
156 r600_atom_dirty(rctx
, &rctx
->cb_misc_state
.atom
);
160 static void r600_bind_blend_state(struct pipe_context
*ctx
, void *state
)
162 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
163 struct r600_pipe_blend
*blend
= (struct r600_pipe_blend
*)state
;
169 rctx
->alpha_to_one
= blend
->alpha_to_one
;
170 rctx
->dual_src_blend
= blend
->dual_src_blend
;
172 if (!rctx
->blend_override
)
173 r600_bind_blend_state_internal(rctx
, blend
);
176 static void r600_set_blend_color(struct pipe_context
*ctx
,
177 const struct pipe_blend_color
*state
)
179 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
180 struct r600_pipe_state
*rstate
= CALLOC_STRUCT(r600_pipe_state
);
185 rstate
->id
= R600_PIPE_STATE_BLEND_COLOR
;
186 r600_pipe_state_add_reg(rstate
, R_028414_CB_BLEND_RED
, fui(state
->color
[0]));
187 r600_pipe_state_add_reg(rstate
, R_028418_CB_BLEND_GREEN
, fui(state
->color
[1]));
188 r600_pipe_state_add_reg(rstate
, R_02841C_CB_BLEND_BLUE
, fui(state
->color
[2]));
189 r600_pipe_state_add_reg(rstate
, R_028420_CB_BLEND_ALPHA
, fui(state
->color
[3]));
191 free(rctx
->states
[R600_PIPE_STATE_BLEND_COLOR
]);
192 rctx
->states
[R600_PIPE_STATE_BLEND_COLOR
] = rstate
;
193 r600_context_pipe_state_set(rctx
, rstate
);
196 static void r600_set_stencil_ref(struct pipe_context
*ctx
,
197 const struct r600_stencil_ref
*state
)
199 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
201 rctx
->stencil_ref
.state
= *state
;
202 r600_atom_dirty(rctx
, &rctx
->stencil_ref
.atom
);
205 void r600_emit_stencil_ref(struct r600_context
*rctx
, struct r600_atom
*atom
)
207 struct radeon_winsys_cs
*cs
= rctx
->cs
;
208 struct r600_stencil_ref_state
*a
= (struct r600_stencil_ref_state
*)atom
;
210 r600_write_context_reg_seq(cs
, R_028430_DB_STENCILREFMASK
, 2);
211 r600_write_value(cs
, /* R_028430_DB_STENCILREFMASK */
212 S_028430_STENCILREF(a
->state
.ref_value
[0]) |
213 S_028430_STENCILMASK(a
->state
.valuemask
[0]) |
214 S_028430_STENCILWRITEMASK(a
->state
.writemask
[0]));
215 r600_write_value(cs
, /* R_028434_DB_STENCILREFMASK_BF */
216 S_028434_STENCILREF_BF(a
->state
.ref_value
[1]) |
217 S_028434_STENCILMASK_BF(a
->state
.valuemask
[1]) |
218 S_028434_STENCILWRITEMASK_BF(a
->state
.writemask
[1]));
221 static void r600_set_pipe_stencil_ref(struct pipe_context
*ctx
,
222 const struct pipe_stencil_ref
*state
)
224 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
225 struct r600_pipe_dsa
*dsa
= (struct r600_pipe_dsa
*)rctx
->states
[R600_PIPE_STATE_DSA
];
226 struct r600_stencil_ref ref
;
228 rctx
->stencil_ref
.pipe_state
= *state
;
233 ref
.ref_value
[0] = state
->ref_value
[0];
234 ref
.ref_value
[1] = state
->ref_value
[1];
235 ref
.valuemask
[0] = dsa
->valuemask
[0];
236 ref
.valuemask
[1] = dsa
->valuemask
[1];
237 ref
.writemask
[0] = dsa
->writemask
[0];
238 ref
.writemask
[1] = dsa
->writemask
[1];
240 r600_set_stencil_ref(ctx
, &ref
);
243 static void r600_bind_dsa_state(struct pipe_context
*ctx
, void *state
)
245 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
246 struct r600_pipe_dsa
*dsa
= state
;
247 struct r600_pipe_state
*rstate
;
248 struct r600_stencil_ref ref
;
252 rstate
= &dsa
->rstate
;
253 rctx
->states
[rstate
->id
] = rstate
;
254 r600_context_pipe_state_set(rctx
, rstate
);
256 ref
.ref_value
[0] = rctx
->stencil_ref
.pipe_state
.ref_value
[0];
257 ref
.ref_value
[1] = rctx
->stencil_ref
.pipe_state
.ref_value
[1];
258 ref
.valuemask
[0] = dsa
->valuemask
[0];
259 ref
.valuemask
[1] = dsa
->valuemask
[1];
260 ref
.writemask
[0] = dsa
->writemask
[0];
261 ref
.writemask
[1] = dsa
->writemask
[1];
263 r600_set_stencil_ref(ctx
, &ref
);
265 /* Update alphatest state. */
266 if (rctx
->alphatest_state
.sx_alpha_test_control
!= dsa
->sx_alpha_test_control
||
267 rctx
->alphatest_state
.sx_alpha_ref
!= dsa
->alpha_ref
) {
268 rctx
->alphatest_state
.sx_alpha_test_control
= dsa
->sx_alpha_test_control
;
269 rctx
->alphatest_state
.sx_alpha_ref
= dsa
->alpha_ref
;
270 r600_atom_dirty(rctx
, &rctx
->alphatest_state
.atom
);
274 void r600_set_max_scissor(struct r600_context
*rctx
)
276 /* Set a scissor state such that it doesn't do anything. */
277 struct pipe_scissor_state scissor
;
283 r600_set_scissor_state(rctx
, &scissor
);
286 static void r600_bind_rs_state(struct pipe_context
*ctx
, void *state
)
288 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
289 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
294 rctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
295 rctx
->two_side
= rs
->two_side
;
296 rctx
->pa_sc_line_stipple
= rs
->pa_sc_line_stipple
;
297 rctx
->pa_cl_clip_cntl
= rs
->pa_cl_clip_cntl
;
298 rctx
->multisample_enable
= rs
->multisample_enable
;
300 rctx
->rasterizer
= rs
;
302 rctx
->states
[rs
->rstate
.id
] = &rs
->rstate
;
303 r600_context_pipe_state_set(rctx
, &rs
->rstate
);
305 if (rctx
->chip_class
>= EVERGREEN
) {
306 evergreen_polygon_offset_update(rctx
);
308 r600_polygon_offset_update(rctx
);
311 /* Workaround for a missing scissor enable on r600. */
312 if (rctx
->chip_class
== R600
) {
313 if (rs
->scissor_enable
!= rctx
->scissor_enable
) {
314 rctx
->scissor_enable
= rs
->scissor_enable
;
316 if (rs
->scissor_enable
) {
317 r600_set_scissor_state(rctx
, &rctx
->scissor_state
);
319 r600_set_max_scissor(rctx
);
325 static void r600_delete_rs_state(struct pipe_context
*ctx
, void *state
)
327 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
328 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
330 if (rctx
->rasterizer
== rs
) {
331 rctx
->rasterizer
= NULL
;
333 if (rctx
->states
[rs
->rstate
.id
] == &rs
->rstate
) {
334 rctx
->states
[rs
->rstate
.id
] = NULL
;
339 static void r600_sampler_view_destroy(struct pipe_context
*ctx
,
340 struct pipe_sampler_view
*state
)
342 struct r600_pipe_sampler_view
*resource
= (struct r600_pipe_sampler_view
*)state
;
344 pipe_resource_reference(&state
->texture
, NULL
);
348 void r600_sampler_states_dirty(struct r600_context
*rctx
,
349 struct r600_sampler_states
*state
)
351 if (state
->dirty_mask
) {
352 if (state
->dirty_mask
& state
->has_bordercolor_mask
) {
353 rctx
->flags
|= R600_CONTEXT_PS_PARTIAL_FLUSH
;
356 util_bitcount(state
->dirty_mask
& state
->has_bordercolor_mask
) * 11 +
357 util_bitcount(state
->dirty_mask
& ~state
->has_bordercolor_mask
) * 5;
358 r600_atom_dirty(rctx
, &state
->atom
);
362 static void r600_bind_sampler_states(struct pipe_context
*pipe
,
365 unsigned count
, void **states
)
367 struct r600_context
*rctx
= (struct r600_context
*)pipe
;
368 struct r600_textures_info
*dst
= &rctx
->samplers
[shader
];
369 struct r600_pipe_sampler_state
**rstates
= (struct r600_pipe_sampler_state
**)states
;
370 int seamless_cube_map
= -1;
372 /* This sets 1-bit for states with index >= count. */
373 uint32_t disable_mask
= ~((1ull << count
) - 1);
374 /* These are the new states set by this function. */
375 uint32_t new_mask
= 0;
377 assert(start
== 0); /* XXX fix below */
379 for (i
= 0; i
< count
; i
++) {
380 struct r600_pipe_sampler_state
*rstate
= rstates
[i
];
382 if (rstate
== dst
->states
.states
[i
]) {
387 if (rstate
->border_color_use
) {
388 dst
->states
.has_bordercolor_mask
|= 1 << i
;
390 dst
->states
.has_bordercolor_mask
&= ~(1 << i
);
392 seamless_cube_map
= rstate
->seamless_cube_map
;
396 disable_mask
|= 1 << i
;
400 memcpy(dst
->states
.states
, rstates
, sizeof(void*) * count
);
401 memset(dst
->states
.states
+ count
, 0, sizeof(void*) * (NUM_TEX_UNITS
- count
));
403 dst
->states
.enabled_mask
&= ~disable_mask
;
404 dst
->states
.dirty_mask
&= dst
->states
.enabled_mask
;
405 dst
->states
.enabled_mask
|= new_mask
;
406 dst
->states
.dirty_mask
|= new_mask
;
407 dst
->states
.has_bordercolor_mask
&= dst
->states
.enabled_mask
;
409 r600_sampler_states_dirty(rctx
, &dst
->states
);
411 /* Seamless cubemap state. */
412 if (rctx
->chip_class
<= R700
&&
413 seamless_cube_map
!= -1 &&
414 seamless_cube_map
!= rctx
->seamless_cube_map
.enabled
) {
415 /* change in TA_CNTL_AUX need a pipeline flush */
416 rctx
->flags
|= R600_CONTEXT_PS_PARTIAL_FLUSH
;
417 rctx
->seamless_cube_map
.enabled
= seamless_cube_map
;
418 r600_atom_dirty(rctx
, &rctx
->seamless_cube_map
.atom
);
422 static void r600_bind_vs_sampler_states(struct pipe_context
*ctx
, unsigned count
, void **states
)
424 r600_bind_sampler_states(ctx
, PIPE_SHADER_VERTEX
, 0, count
, states
);
427 static void r600_bind_ps_sampler_states(struct pipe_context
*ctx
, unsigned count
, void **states
)
429 r600_bind_sampler_states(ctx
, PIPE_SHADER_FRAGMENT
, 0, count
, states
);
432 static void r600_delete_sampler_state(struct pipe_context
*ctx
, void *state
)
437 static void r600_delete_state(struct pipe_context
*ctx
, void *state
)
439 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
440 struct r600_pipe_state
*rstate
= (struct r600_pipe_state
*)state
;
442 if (rctx
->states
[rstate
->id
] == rstate
) {
443 rctx
->states
[rstate
->id
] = NULL
;
445 for (int i
= 0; i
< rstate
->nregs
; i
++) {
446 pipe_resource_reference((struct pipe_resource
**)&rstate
->regs
[i
].bo
, NULL
);
451 static void r600_bind_vertex_elements(struct pipe_context
*ctx
, void *state
)
453 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
454 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
456 rctx
->vertex_elements
= v
;
458 rctx
->states
[v
->rstate
.id
] = &v
->rstate
;
459 r600_context_pipe_state_set(rctx
, &v
->rstate
);
463 static void r600_delete_vertex_elements(struct pipe_context
*ctx
, void *state
)
465 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
466 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
468 if (rctx
->states
[v
->rstate
.id
] == &v
->rstate
) {
469 rctx
->states
[v
->rstate
.id
] = NULL
;
471 if (rctx
->vertex_elements
== state
)
472 rctx
->vertex_elements
= NULL
;
474 pipe_resource_reference((struct pipe_resource
**)&v
->fetch_shader
, NULL
);
478 static void r600_set_index_buffer(struct pipe_context
*ctx
,
479 const struct pipe_index_buffer
*ib
)
481 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
484 pipe_resource_reference(&rctx
->index_buffer
.buffer
, ib
->buffer
);
485 memcpy(&rctx
->index_buffer
, ib
, sizeof(*ib
));
487 pipe_resource_reference(&rctx
->index_buffer
.buffer
, NULL
);
491 void r600_vertex_buffers_dirty(struct r600_context
*rctx
)
493 if (rctx
->vertex_buffer_state
.dirty_mask
) {
494 rctx
->flags
|= rctx
->has_vertex_cache
? R600_CONTEXT_VTX_FLUSH
: R600_CONTEXT_TEX_FLUSH
;
495 rctx
->vertex_buffer_state
.atom
.num_dw
= (rctx
->chip_class
>= EVERGREEN
? 12 : 11) *
496 util_bitcount(rctx
->vertex_buffer_state
.dirty_mask
);
497 r600_atom_dirty(rctx
, &rctx
->vertex_buffer_state
.atom
);
501 static void r600_set_vertex_buffers(struct pipe_context
*ctx
, unsigned count
,
502 const struct pipe_vertex_buffer
*input
)
504 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
505 struct r600_vertexbuf_state
*state
= &rctx
->vertex_buffer_state
;
506 struct pipe_vertex_buffer
*vb
= state
->vb
;
508 /* This sets 1-bit for buffers with index >= count. */
509 uint32_t disable_mask
= ~((1ull << count
) - 1);
510 /* These are the new buffers set by this function. */
511 uint32_t new_buffer_mask
= 0;
513 /* Set buffers with index >= count to NULL. */
514 uint32_t remaining_buffers_mask
=
515 rctx
->vertex_buffer_state
.enabled_mask
& disable_mask
;
517 while (remaining_buffers_mask
) {
518 i
= u_bit_scan(&remaining_buffers_mask
);
519 pipe_resource_reference(&vb
[i
].buffer
, NULL
);
522 /* Set vertex buffers. */
523 for (i
= 0; i
< count
; i
++) {
524 if (memcmp(&input
[i
], &vb
[i
], sizeof(struct pipe_vertex_buffer
))) {
525 if (input
[i
].buffer
) {
526 vb
[i
].stride
= input
[i
].stride
;
527 vb
[i
].buffer_offset
= input
[i
].buffer_offset
;
528 pipe_resource_reference(&vb
[i
].buffer
, input
[i
].buffer
);
529 new_buffer_mask
|= 1 << i
;
531 pipe_resource_reference(&vb
[i
].buffer
, NULL
);
532 disable_mask
|= 1 << i
;
537 rctx
->vertex_buffer_state
.enabled_mask
&= ~disable_mask
;
538 rctx
->vertex_buffer_state
.dirty_mask
&= rctx
->vertex_buffer_state
.enabled_mask
;
539 rctx
->vertex_buffer_state
.enabled_mask
|= new_buffer_mask
;
540 rctx
->vertex_buffer_state
.dirty_mask
|= new_buffer_mask
;
542 r600_vertex_buffers_dirty(rctx
);
545 void r600_sampler_views_dirty(struct r600_context
*rctx
,
546 struct r600_samplerview_state
*state
)
548 if (state
->dirty_mask
) {
549 rctx
->flags
|= R600_CONTEXT_TEX_FLUSH
;
550 state
->atom
.num_dw
= (rctx
->chip_class
>= EVERGREEN
? 14 : 13) *
551 util_bitcount(state
->dirty_mask
);
552 r600_atom_dirty(rctx
, &state
->atom
);
556 static void r600_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
557 unsigned start
, unsigned count
,
558 struct pipe_sampler_view
**views
)
560 struct r600_context
*rctx
= (struct r600_context
*) pipe
;
561 struct r600_textures_info
*dst
= &rctx
->samplers
[shader
];
562 struct r600_pipe_sampler_view
**rviews
= (struct r600_pipe_sampler_view
**)views
;
563 uint32_t dirty_sampler_states_mask
= 0;
565 /* This sets 1-bit for textures with index >= count. */
566 uint32_t disable_mask
= ~((1ull << count
) - 1);
567 /* These are the new textures set by this function. */
568 uint32_t new_mask
= 0;
570 /* Set textures with index >= count to NULL. */
571 uint32_t remaining_mask
;
573 assert(start
== 0); /* XXX fix below */
575 remaining_mask
= dst
->views
.enabled_mask
& disable_mask
;
577 while (remaining_mask
) {
578 i
= u_bit_scan(&remaining_mask
);
579 assert(dst
->views
.views
[i
]);
581 pipe_sampler_view_reference((struct pipe_sampler_view
**)&dst
->views
.views
[i
], NULL
);
584 for (i
= 0; i
< count
; i
++) {
585 if (rviews
[i
] == dst
->views
.views
[i
]) {
590 struct r600_texture
*rtex
=
591 (struct r600_texture
*)rviews
[i
]->base
.texture
;
593 if (rtex
->is_depth
&& !rtex
->is_flushing_texture
) {
594 dst
->views
.compressed_depthtex_mask
|= 1 << i
;
596 dst
->views
.compressed_depthtex_mask
&= ~(1 << i
);
599 /* Track compressed colorbuffers for Evergreen (Cayman doesn't need this). */
600 if (rctx
->chip_class
!= CAYMAN
&& rtex
->cmask_size
&& rtex
->fmask_size
) {
601 dst
->views
.compressed_colortex_mask
|= 1 << i
;
603 dst
->views
.compressed_colortex_mask
&= ~(1 << i
);
606 /* Changing from array to non-arrays textures and vice versa requires
607 * updating TEX_ARRAY_OVERRIDE in sampler states on R6xx-R7xx. */
608 if (rctx
->chip_class
<= R700
&&
609 (dst
->states
.enabled_mask
& (1 << i
)) &&
610 (rviews
[i
]->base
.texture
->target
== PIPE_TEXTURE_1D_ARRAY
||
611 rviews
[i
]->base
.texture
->target
== PIPE_TEXTURE_2D_ARRAY
) != dst
->is_array_sampler
[i
]) {
612 dirty_sampler_states_mask
|= 1 << i
;
615 pipe_sampler_view_reference((struct pipe_sampler_view
**)&dst
->views
.views
[i
], views
[i
]);
618 pipe_sampler_view_reference((struct pipe_sampler_view
**)&dst
->views
.views
[i
], NULL
);
619 disable_mask
|= 1 << i
;
623 dst
->views
.enabled_mask
&= ~disable_mask
;
624 dst
->views
.dirty_mask
&= dst
->views
.enabled_mask
;
625 dst
->views
.enabled_mask
|= new_mask
;
626 dst
->views
.dirty_mask
|= new_mask
;
627 dst
->views
.compressed_depthtex_mask
&= dst
->views
.enabled_mask
;
628 dst
->views
.compressed_colortex_mask
&= dst
->views
.enabled_mask
;
630 r600_sampler_views_dirty(rctx
, &dst
->views
);
632 if (dirty_sampler_states_mask
) {
633 dst
->states
.dirty_mask
|= dirty_sampler_states_mask
;
634 r600_sampler_states_dirty(rctx
, &dst
->states
);
638 static void r600_set_vs_sampler_views(struct pipe_context
*ctx
, unsigned count
,
639 struct pipe_sampler_view
**views
)
641 r600_set_sampler_views(ctx
, PIPE_SHADER_VERTEX
, 0, count
, views
);
644 static void r600_set_ps_sampler_views(struct pipe_context
*ctx
, unsigned count
,
645 struct pipe_sampler_view
**views
)
647 r600_set_sampler_views(ctx
, PIPE_SHADER_FRAGMENT
, 0, count
, views
);
650 static void r600_set_viewport_state(struct pipe_context
*ctx
,
651 const struct pipe_viewport_state
*state
)
653 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
655 rctx
->viewport
.state
= *state
;
656 r600_atom_dirty(rctx
, &rctx
->viewport
.atom
);
659 void r600_emit_viewport_state(struct r600_context
*rctx
, struct r600_atom
*atom
)
661 struct radeon_winsys_cs
*cs
= rctx
->cs
;
662 struct pipe_viewport_state
*state
= &rctx
->viewport
.state
;
664 r600_write_context_reg_seq(cs
, R_02843C_PA_CL_VPORT_XSCALE_0
, 6);
665 r600_write_value(cs
, fui(state
->scale
[0])); /* R_02843C_PA_CL_VPORT_XSCALE_0 */
666 r600_write_value(cs
, fui(state
->translate
[0])); /* R_028440_PA_CL_VPORT_XOFFSET_0 */
667 r600_write_value(cs
, fui(state
->scale
[1])); /* R_028444_PA_CL_VPORT_YSCALE_0 */
668 r600_write_value(cs
, fui(state
->translate
[1])); /* R_028448_PA_CL_VPORT_YOFFSET_0 */
669 r600_write_value(cs
, fui(state
->scale
[2])); /* R_02844C_PA_CL_VPORT_ZSCALE_0 */
670 r600_write_value(cs
, fui(state
->translate
[2])); /* R_028450_PA_CL_VPORT_ZOFFSET_0 */
673 static void *r600_create_vertex_elements(struct pipe_context
*ctx
, unsigned count
,
674 const struct pipe_vertex_element
*elements
)
676 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
677 struct r600_vertex_element
*v
= CALLOC_STRUCT(r600_vertex_element
);
684 memcpy(v
->elements
, elements
, sizeof(struct pipe_vertex_element
) * count
);
686 if (r600_vertex_elements_build_fetch_shader(rctx
, v
)) {
694 /* Compute the key for the hw shader variant */
695 static INLINE
unsigned r600_shader_selector_key(struct pipe_context
* ctx
,
696 struct r600_pipe_shader_selector
* sel
)
698 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
701 if (sel
->type
== PIPE_SHADER_FRAGMENT
) {
702 key
= rctx
->two_side
|
703 ((rctx
->alpha_to_one
&& rctx
->multisample_enable
&& !rctx
->cb0_is_integer
) << 1) |
704 (MIN2(sel
->nr_ps_max_color_exports
, rctx
->nr_cbufs
+ rctx
->dual_src_blend
) << 2);
711 /* Select the hw shader variant depending on the current state.
712 * (*dirty) is set to 1 if current variant was changed */
713 static int r600_shader_select(struct pipe_context
*ctx
,
714 struct r600_pipe_shader_selector
* sel
,
718 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
719 struct r600_pipe_shader
* shader
= NULL
;
722 key
= r600_shader_selector_key(ctx
, sel
);
724 /* Check if we don't need to change anything.
725 * This path is also used for most shaders that don't need multiple
726 * variants, it will cost just a computation of the key and this
728 if (likely(sel
->current
&& sel
->current
->key
== key
)) {
732 /* lookup if we have other variants in the list */
733 if (sel
->num_shaders
> 1) {
734 struct r600_pipe_shader
*p
= sel
->current
, *c
= p
->next_variant
;
736 while (c
&& c
->key
!= key
) {
742 p
->next_variant
= c
->next_variant
;
747 if (unlikely(!shader
)) {
748 shader
= CALLOC(1, sizeof(struct r600_pipe_shader
));
749 shader
->selector
= sel
;
751 r
= r600_pipe_shader_create(ctx
, shader
);
753 R600_ERR("Failed to build shader variant (type=%u, key=%u) %d\n",
759 /* We don't know the value of nr_ps_max_color_exports until we built
760 * at least one variant, so we may need to recompute the key after
761 * building first variant. */
762 if (sel
->type
== PIPE_SHADER_FRAGMENT
&&
763 sel
->num_shaders
== 0) {
764 sel
->nr_ps_max_color_exports
= shader
->shader
.nr_ps_max_color_exports
;
765 key
= r600_shader_selector_key(ctx
, sel
);
775 shader
->next_variant
= sel
->current
;
776 sel
->current
= shader
;
778 if (rctx
->chip_class
< EVERGREEN
&& rctx
->ps_shader
&& rctx
->vs_shader
) {
779 r600_adjust_gprs(rctx
);
782 if (rctx
->ps_shader
&&
783 rctx
->cb_misc_state
.nr_ps_color_outputs
!= rctx
->ps_shader
->current
->nr_ps_color_outputs
) {
784 rctx
->cb_misc_state
.nr_ps_color_outputs
= rctx
->ps_shader
->current
->nr_ps_color_outputs
;
785 r600_atom_dirty(rctx
, &rctx
->cb_misc_state
.atom
);
790 static void *r600_create_shader_state(struct pipe_context
*ctx
,
791 const struct pipe_shader_state
*state
,
792 unsigned pipe_shader_type
)
794 struct r600_pipe_shader_selector
*sel
= CALLOC_STRUCT(r600_pipe_shader_selector
);
797 sel
->type
= pipe_shader_type
;
798 sel
->tokens
= tgsi_dup_tokens(state
->tokens
);
799 sel
->so
= state
->stream_output
;
801 r
= r600_shader_select(ctx
, sel
, NULL
);
808 static void *r600_create_ps_state(struct pipe_context
*ctx
,
809 const struct pipe_shader_state
*state
)
811 return r600_create_shader_state(ctx
, state
, PIPE_SHADER_FRAGMENT
);
814 static void *r600_create_vs_state(struct pipe_context
*ctx
,
815 const struct pipe_shader_state
*state
)
817 return r600_create_shader_state(ctx
, state
, PIPE_SHADER_VERTEX
);
820 static void r600_bind_ps_state(struct pipe_context
*ctx
, void *state
)
822 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
825 state
= rctx
->dummy_pixel_shader
;
827 rctx
->ps_shader
= (struct r600_pipe_shader_selector
*)state
;
828 r600_context_pipe_state_set(rctx
, &rctx
->ps_shader
->current
->rstate
);
830 if (rctx
->chip_class
<= R700
) {
831 bool multiwrite
= rctx
->ps_shader
->current
->shader
.fs_write_all
;
833 if (rctx
->cb_misc_state
.multiwrite
!= multiwrite
) {
834 rctx
->cb_misc_state
.multiwrite
= multiwrite
;
835 r600_atom_dirty(rctx
, &rctx
->cb_misc_state
.atom
);
839 r600_adjust_gprs(rctx
);
842 if (rctx
->cb_misc_state
.nr_ps_color_outputs
!= rctx
->ps_shader
->current
->nr_ps_color_outputs
) {
843 rctx
->cb_misc_state
.nr_ps_color_outputs
= rctx
->ps_shader
->current
->nr_ps_color_outputs
;
844 r600_atom_dirty(rctx
, &rctx
->cb_misc_state
.atom
);
848 static void r600_bind_vs_state(struct pipe_context
*ctx
, void *state
)
850 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
852 rctx
->vs_shader
= (struct r600_pipe_shader_selector
*)state
;
854 r600_context_pipe_state_set(rctx
, &rctx
->vs_shader
->current
->rstate
);
856 if (rctx
->chip_class
< EVERGREEN
&& rctx
->ps_shader
)
857 r600_adjust_gprs(rctx
);
861 static void r600_delete_shader_selector(struct pipe_context
*ctx
,
862 struct r600_pipe_shader_selector
*sel
)
864 struct r600_pipe_shader
*p
= sel
->current
, *c
;
867 r600_pipe_shader_destroy(ctx
, p
);
877 static void r600_delete_ps_state(struct pipe_context
*ctx
, void *state
)
879 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
880 struct r600_pipe_shader_selector
*sel
= (struct r600_pipe_shader_selector
*)state
;
882 if (rctx
->ps_shader
== sel
) {
883 rctx
->ps_shader
= NULL
;
886 r600_delete_shader_selector(ctx
, sel
);
889 static void r600_delete_vs_state(struct pipe_context
*ctx
, void *state
)
891 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
892 struct r600_pipe_shader_selector
*sel
= (struct r600_pipe_shader_selector
*)state
;
894 if (rctx
->vs_shader
== sel
) {
895 rctx
->vs_shader
= NULL
;
898 r600_delete_shader_selector(ctx
, sel
);
901 void r600_constant_buffers_dirty(struct r600_context
*rctx
, struct r600_constbuf_state
*state
)
903 if (state
->dirty_mask
) {
904 rctx
->flags
|= R600_CONTEXT_SHADERCONST_FLUSH
;
905 state
->atom
.num_dw
= rctx
->chip_class
>= EVERGREEN
? util_bitcount(state
->dirty_mask
)*20
906 : util_bitcount(state
->dirty_mask
)*19;
907 r600_atom_dirty(rctx
, &state
->atom
);
911 static void r600_set_constant_buffer(struct pipe_context
*ctx
, uint shader
, uint index
,
912 struct pipe_constant_buffer
*input
)
914 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
915 struct r600_constbuf_state
*state
= &rctx
->constbuf_state
[shader
];
916 struct pipe_constant_buffer
*cb
;
919 /* Note that the state tracker can unbind constant buffers by
922 if (unlikely(!input
)) {
923 state
->enabled_mask
&= ~(1 << index
);
924 state
->dirty_mask
&= ~(1 << index
);
925 pipe_resource_reference(&state
->cb
[index
].buffer
, NULL
);
929 cb
= &state
->cb
[index
];
930 cb
->buffer_size
= input
->buffer_size
;
932 ptr
= input
->user_buffer
;
935 /* Upload the user buffer. */
936 if (R600_BIG_ENDIAN
) {
938 unsigned i
, size
= input
->buffer_size
;
940 if (!(tmpPtr
= malloc(size
))) {
941 R600_ERR("Failed to allocate BE swap buffer.\n");
945 for (i
= 0; i
< size
/ 4; ++i
) {
946 tmpPtr
[i
] = bswap_32(((uint32_t *)ptr
)[i
]);
949 u_upload_data(rctx
->uploader
, 0, size
, tmpPtr
, &cb
->buffer_offset
, &cb
->buffer
);
952 u_upload_data(rctx
->uploader
, 0, input
->buffer_size
, ptr
, &cb
->buffer_offset
, &cb
->buffer
);
955 /* Setup the hw buffer. */
956 cb
->buffer_offset
= input
->buffer_offset
;
957 pipe_resource_reference(&cb
->buffer
, input
->buffer
);
960 state
->enabled_mask
|= 1 << index
;
961 state
->dirty_mask
|= 1 << index
;
962 r600_constant_buffers_dirty(rctx
, state
);
965 static struct pipe_stream_output_target
*
966 r600_create_so_target(struct pipe_context
*ctx
,
967 struct pipe_resource
*buffer
,
968 unsigned buffer_offset
,
969 unsigned buffer_size
)
971 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
972 struct r600_so_target
*t
;
975 t
= CALLOC_STRUCT(r600_so_target
);
980 t
->b
.reference
.count
= 1;
982 pipe_resource_reference(&t
->b
.buffer
, buffer
);
983 t
->b
.buffer_offset
= buffer_offset
;
984 t
->b
.buffer_size
= buffer_size
;
986 t
->filled_size
= (struct r600_resource
*)
987 pipe_buffer_create(ctx
->screen
, PIPE_BIND_CUSTOM
, PIPE_USAGE_STATIC
, 4);
988 ptr
= rctx
->ws
->buffer_map(t
->filled_size
->cs_buf
, rctx
->cs
, PIPE_TRANSFER_WRITE
);
989 memset(ptr
, 0, t
->filled_size
->buf
->size
);
990 rctx
->ws
->buffer_unmap(t
->filled_size
->cs_buf
);
995 static void r600_so_target_destroy(struct pipe_context
*ctx
,
996 struct pipe_stream_output_target
*target
)
998 struct r600_so_target
*t
= (struct r600_so_target
*)target
;
999 pipe_resource_reference(&t
->b
.buffer
, NULL
);
1000 pipe_resource_reference((struct pipe_resource
**)&t
->filled_size
, NULL
);
1004 static void r600_set_so_targets(struct pipe_context
*ctx
,
1005 unsigned num_targets
,
1006 struct pipe_stream_output_target
**targets
,
1007 unsigned append_bitmask
)
1009 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
1012 /* Stop streamout. */
1013 if (rctx
->num_so_targets
&& !rctx
->streamout_start
) {
1014 r600_context_streamout_end(rctx
);
1017 /* Set the new targets. */
1018 for (i
= 0; i
< num_targets
; i
++) {
1019 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->so_targets
[i
], targets
[i
]);
1021 for (; i
< rctx
->num_so_targets
; i
++) {
1022 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->so_targets
[i
], NULL
);
1025 rctx
->num_so_targets
= num_targets
;
1026 rctx
->streamout_start
= num_targets
!= 0;
1027 rctx
->streamout_append_bitmask
= append_bitmask
;
1030 static void r600_set_sample_mask(struct pipe_context
*pipe
, unsigned sample_mask
)
1032 struct r600_context
*rctx
= (struct r600_context
*)pipe
;
1034 if (rctx
->sample_mask
.sample_mask
== (uint16_t)sample_mask
)
1037 rctx
->sample_mask
.sample_mask
= sample_mask
;
1038 r600_atom_dirty(rctx
, &rctx
->sample_mask
.atom
);
1041 static void r600_update_derived_state(struct r600_context
*rctx
)
1043 struct pipe_context
* ctx
= (struct pipe_context
*)rctx
;
1044 unsigned ps_dirty
= 0, blend_override
;
1046 if (!rctx
->blitter
->running
) {
1049 /* Decompress textures if needed. */
1050 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
1051 struct r600_samplerview_state
*views
= &rctx
->samplers
[i
].views
;
1052 if (views
->compressed_depthtex_mask
) {
1053 r600_decompress_depth_textures(rctx
, views
);
1055 if (views
->compressed_colortex_mask
) {
1056 r600_decompress_color_textures(rctx
, views
);
1061 r600_shader_select(ctx
, rctx
->ps_shader
, &ps_dirty
);
1063 if (rctx
->ps_shader
&& ((rctx
->sprite_coord_enable
&&
1064 (rctx
->ps_shader
->current
->sprite_coord_enable
!= rctx
->sprite_coord_enable
)) ||
1065 (rctx
->rasterizer
&& rctx
->rasterizer
->flatshade
!= rctx
->ps_shader
->current
->flatshade
))) {
1067 if (rctx
->chip_class
>= EVERGREEN
)
1068 evergreen_pipe_shader_ps(ctx
, rctx
->ps_shader
->current
);
1070 r600_pipe_shader_ps(ctx
, rctx
->ps_shader
->current
);
1076 r600_context_pipe_state_set(rctx
, &rctx
->ps_shader
->current
->rstate
);
1078 blend_override
= (rctx
->dual_src_blend
&&
1079 rctx
->ps_shader
->current
->nr_ps_color_outputs
< 2);
1081 if (blend_override
!= rctx
->blend_override
) {
1082 rctx
->blend_override
= blend_override
;
1083 r600_bind_blend_state_internal(rctx
,
1084 blend_override
? rctx
->no_blend
: rctx
->blend
);
1087 if (rctx
->chip_class
>= EVERGREEN
) {
1088 evergreen_update_dual_export_state(rctx
);
1090 r600_update_dual_export_state(rctx
);
1094 static unsigned r600_conv_prim_to_gs_out(unsigned mode
)
1096 static const int prim_conv
[] = {
1097 V_028A6C_OUTPRIM_TYPE_POINTLIST
,
1098 V_028A6C_OUTPRIM_TYPE_LINESTRIP
,
1099 V_028A6C_OUTPRIM_TYPE_LINESTRIP
,
1100 V_028A6C_OUTPRIM_TYPE_LINESTRIP
,
1101 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1102 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1103 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1104 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1105 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1106 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1107 V_028A6C_OUTPRIM_TYPE_LINESTRIP
,
1108 V_028A6C_OUTPRIM_TYPE_LINESTRIP
,
1109 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1110 V_028A6C_OUTPRIM_TYPE_TRISTRIP
,
1111 V_028A6C_OUTPRIM_TYPE_TRISTRIP
1113 assert(mode
< Elements(prim_conv
));
1115 return prim_conv
[mode
];
1118 static void r600_draw_vbo(struct pipe_context
*ctx
, const struct pipe_draw_info
*dinfo
)
1120 struct r600_context
*rctx
= (struct r600_context
*)ctx
;
1121 struct pipe_draw_info info
= *dinfo
;
1122 struct pipe_index_buffer ib
= {};
1123 unsigned prim
, ls_mask
= 0, i
;
1124 struct r600_block
*dirty_block
= NULL
, *next_block
= NULL
;
1125 struct radeon_winsys_cs
*cs
= rctx
->cs
;
1129 if ((!info
.count
&& (info
.indexed
|| !info
.count_from_stream_output
)) ||
1130 !r600_conv_pipe_prim(info
.mode
, &prim
)) {
1135 if (!rctx
->vs_shader
) {
1140 r600_update_derived_state(rctx
);
1143 /* Initialize the index buffer struct. */
1144 pipe_resource_reference(&ib
.buffer
, rctx
->index_buffer
.buffer
);
1145 ib
.user_buffer
= rctx
->index_buffer
.user_buffer
;
1146 ib
.index_size
= rctx
->index_buffer
.index_size
;
1147 ib
.offset
= rctx
->index_buffer
.offset
+ info
.start
* ib
.index_size
;
1149 /* Translate or upload, if needed. */
1150 r600_translate_index_buffer(rctx
, &ib
, info
.count
);
1152 ptr
= (uint8_t*)ib
.user_buffer
;
1153 if (!ib
.buffer
&& ptr
) {
1154 u_upload_data(rctx
->uploader
, 0, info
.count
* ib
.index_size
,
1155 ptr
, &ib
.offset
, &ib
.buffer
);
1158 info
.index_bias
= info
.start
;
1161 if (rctx
->vgt
.id
!= R600_PIPE_STATE_VGT
) {
1162 rctx
->vgt
.id
= R600_PIPE_STATE_VGT
;
1163 rctx
->vgt
.nregs
= 0;
1164 r600_pipe_state_add_reg(&rctx
->vgt
, R_008958_VGT_PRIMITIVE_TYPE
, prim
);
1165 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A6C_VGT_GS_OUT_PRIM_TYPE
, 0);
1166 r600_pipe_state_add_reg(&rctx
->vgt
, R_028408_VGT_INDX_OFFSET
, info
.index_bias
);
1167 r600_pipe_state_add_reg(&rctx
->vgt
, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX
, info
.restart_index
);
1168 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN
, info
.primitive_restart
);
1169 r600_pipe_state_add_reg(&rctx
->vgt
, R_03CFF4_SQ_VTX_START_INST_LOC
, info
.start_instance
);
1170 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A0C_PA_SC_LINE_STIPPLE
, 0);
1171 r600_pipe_state_add_reg(&rctx
->vgt
, R_02881C_PA_CL_VS_OUT_CNTL
, 0);
1172 r600_pipe_state_add_reg(&rctx
->vgt
, R_028810_PA_CL_CLIP_CNTL
, 0);
1175 rctx
->vgt
.nregs
= 0;
1176 r600_pipe_state_mod_reg(&rctx
->vgt
, prim
);
1177 r600_pipe_state_mod_reg(&rctx
->vgt
, r600_conv_prim_to_gs_out(info
.mode
));
1178 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.index_bias
);
1179 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.restart_index
);
1180 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.primitive_restart
);
1181 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.start_instance
);
1183 if (prim
== V_008958_DI_PT_LINELIST
)
1185 else if (prim
== V_008958_DI_PT_LINESTRIP
||
1186 prim
== V_008958_DI_PT_LINELOOP
)
1188 r600_pipe_state_mod_reg(&rctx
->vgt
, S_028A0C_AUTO_RESET_CNTL(ls_mask
) | rctx
->pa_sc_line_stipple
);
1189 r600_pipe_state_mod_reg(&rctx
->vgt
,
1190 rctx
->vs_shader
->current
->pa_cl_vs_out_cntl
|
1191 (rctx
->rasterizer
->clip_plane_enable
& rctx
->vs_shader
->current
->shader
.clip_dist_write
));
1192 r600_pipe_state_mod_reg(&rctx
->vgt
,
1193 rctx
->pa_cl_clip_cntl
|
1194 (rctx
->vs_shader
->current
->shader
.clip_dist_write
||
1195 rctx
->vs_shader
->current
->shader
.vs_prohibit_ucps
?
1196 0 : rctx
->rasterizer
->clip_plane_enable
& 0x3F));
1198 r600_context_pipe_state_set(rctx
, &rctx
->vgt
);
1200 /* Enable stream out if needed. */
1201 if (rctx
->streamout_start
) {
1202 r600_context_streamout_begin(rctx
);
1203 rctx
->streamout_start
= FALSE
;
1206 /* Emit states (the function expects that we emit at most 17 dwords here). */
1207 r600_need_cs_space(rctx
, 0, TRUE
);
1208 r600_flush_emit(rctx
);
1210 for (i
= 0; i
< R600_NUM_ATOMS
; i
++) {
1211 if (rctx
->atoms
[i
] == NULL
|| !rctx
->atoms
[i
]->dirty
) {
1214 r600_emit_atom(rctx
, rctx
->atoms
[i
]);
1216 LIST_FOR_EACH_ENTRY_SAFE(dirty_block
, next_block
, &rctx
->dirty
,list
) {
1217 r600_context_block_emit_dirty(rctx
, dirty_block
, 0 /* pkt_flags */);
1219 rctx
->pm4_dirty_cdwords
= 0;
1222 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NUM_INSTANCES
, 0, rctx
->predicate_drawing
);
1223 cs
->buf
[cs
->cdw
++] = info
.instance_count
;
1225 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_INDEX_TYPE
, 0, rctx
->predicate_drawing
);
1226 cs
->buf
[cs
->cdw
++] = ib
.index_size
== 4 ?
1227 (VGT_INDEX_32
| (R600_BIG_ENDIAN
? VGT_DMA_SWAP_32_BIT
: 0)) :
1228 (VGT_INDEX_16
| (R600_BIG_ENDIAN
? VGT_DMA_SWAP_16_BIT
: 0));
1230 va
= r600_resource_va(ctx
->screen
, ib
.buffer
);
1232 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_DRAW_INDEX
, 3, rctx
->predicate_drawing
);
1233 cs
->buf
[cs
->cdw
++] = va
;
1234 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFF;
1235 cs
->buf
[cs
->cdw
++] = info
.count
;
1236 cs
->buf
[cs
->cdw
++] = V_0287F0_DI_SRC_SEL_DMA
;
1237 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, rctx
->predicate_drawing
);
1238 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(rctx
, (struct r600_resource
*)ib
.buffer
, RADEON_USAGE_READ
);
1240 if (info
.count_from_stream_output
) {
1241 struct r600_so_target
*t
= (struct r600_so_target
*)info
.count_from_stream_output
;
1242 uint64_t va
= r600_resource_va(&rctx
->screen
->screen
, (void*)t
->filled_size
);
1244 r600_write_context_reg(cs
, R_028B30_VGT_STRMOUT_DRAW_OPAQUE_VERTEX_STRIDE
, t
->stride_in_dw
);
1246 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_COPY_DW
, 4, 0);
1247 cs
->buf
[cs
->cdw
++] = COPY_DW_SRC_IS_MEM
| COPY_DW_DST_IS_REG
;
1248 cs
->buf
[cs
->cdw
++] = va
& 0xFFFFFFFFUL
; /* src address lo */
1249 cs
->buf
[cs
->cdw
++] = (va
>> 32UL) & 0xFFUL
; /* src address hi */
1250 cs
->buf
[cs
->cdw
++] = R_028B2C_VGT_STRMOUT_DRAW_OPAQUE_BUFFER_FILLED_SIZE
>> 2; /* dst register */
1251 cs
->buf
[cs
->cdw
++] = 0; /* unused */
1253 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_NOP
, 0, 0);
1254 cs
->buf
[cs
->cdw
++] = r600_context_bo_reloc(rctx
, t
->filled_size
, RADEON_USAGE_READ
);
1257 cs
->buf
[cs
->cdw
++] = PKT3(PKT3_DRAW_INDEX_AUTO
, 1, rctx
->predicate_drawing
);
1258 cs
->buf
[cs
->cdw
++] = info
.count
;
1259 cs
->buf
[cs
->cdw
++] = V_0287F0_DI_SRC_SEL_AUTO_INDEX
|
1260 (info
.count_from_stream_output
? S_0287F0_USE_OPAQUE(1) : 0);
1263 /* Set the depth buffer as dirty. */
1264 if (rctx
->framebuffer
.zsbuf
) {
1265 struct pipe_surface
*surf
= rctx
->framebuffer
.zsbuf
;
1266 struct r600_texture
*rtex
= (struct r600_texture
*)surf
->texture
;
1268 rtex
->dirty_level_mask
|= 1 << surf
->u
.tex
.level
;
1270 if (rctx
->compressed_cb_mask
) {
1271 struct pipe_surface
*surf
;
1272 struct r600_texture
*rtex
;
1273 unsigned mask
= rctx
->compressed_cb_mask
;
1276 unsigned i
= u_bit_scan(&mask
);
1277 surf
= rctx
->framebuffer
.cbufs
[i
];
1278 rtex
= (struct r600_texture
*)surf
->texture
;
1280 rtex
->dirty_level_mask
|= 1 << surf
->u
.tex
.level
;
1285 pipe_resource_reference(&ib
.buffer
, NULL
);
1288 void r600_draw_rectangle(struct blitter_context
*blitter
,
1289 unsigned x1
, unsigned y1
, unsigned x2
, unsigned y2
, float depth
,
1290 enum blitter_attrib_type type
, const union pipe_color_union
*attrib
)
1292 struct r600_context
*rctx
= (struct r600_context
*)util_blitter_get_pipe(blitter
);
1293 struct pipe_viewport_state viewport
;
1294 struct pipe_resource
*buf
= NULL
;
1295 unsigned offset
= 0;
1298 if (type
== UTIL_BLITTER_ATTRIB_TEXCOORD
) {
1299 util_blitter_draw_rectangle(blitter
, x1
, y1
, x2
, y2
, depth
, type
, attrib
);
1303 /* Some operations (like color resolve on r6xx) don't work
1304 * with the conventional primitive types.
1305 * One that works is PT_RECTLIST, which we use here. */
1307 /* setup viewport */
1308 viewport
.scale
[0] = 1.0f
;
1309 viewport
.scale
[1] = 1.0f
;
1310 viewport
.scale
[2] = 1.0f
;
1311 viewport
.scale
[3] = 1.0f
;
1312 viewport
.translate
[0] = 0.0f
;
1313 viewport
.translate
[1] = 0.0f
;
1314 viewport
.translate
[2] = 0.0f
;
1315 viewport
.translate
[3] = 0.0f
;
1316 rctx
->context
.set_viewport_state(&rctx
->context
, &viewport
);
1318 /* Upload vertices. The hw rectangle has only 3 vertices,
1319 * I guess the 4th one is derived from the first 3.
1320 * The vertex specification should match u_blitter's vertex element state. */
1321 u_upload_alloc(rctx
->uploader
, 0, sizeof(float) * 24, &offset
, &buf
, (void**)&vb
);
1338 memcpy(vb
+4, attrib
->f
, sizeof(float)*4);
1339 memcpy(vb
+12, attrib
->f
, sizeof(float)*4);
1340 memcpy(vb
+20, attrib
->f
, sizeof(float)*4);
1344 util_draw_vertex_buffer(&rctx
->context
, NULL
, buf
, offset
,
1345 R600_PRIM_RECTANGLE_LIST
, 3, 2);
1346 pipe_resource_reference(&buf
, NULL
);
1349 void _r600_pipe_state_add_reg_bo(struct r600_context
*ctx
,
1350 struct r600_pipe_state
*state
,
1351 uint32_t offset
, uint32_t value
,
1352 uint32_t range_id
, uint32_t block_id
,
1353 struct r600_resource
*bo
,
1354 enum radeon_bo_usage usage
)
1357 struct r600_range
*range
;
1358 struct r600_block
*block
;
1360 if (bo
) assert(usage
);
1362 range
= &ctx
->range
[range_id
];
1363 block
= range
->blocks
[block_id
];
1364 state
->regs
[state
->nregs
].block
= block
;
1365 state
->regs
[state
->nregs
].id
= (offset
- block
->start_offset
) >> 2;
1367 state
->regs
[state
->nregs
].value
= value
;
1368 state
->regs
[state
->nregs
].bo
= bo
;
1369 state
->regs
[state
->nregs
].bo_usage
= usage
;
1372 assert(state
->nregs
< R600_BLOCK_MAX_REG
);
1375 void _r600_pipe_state_add_reg(struct r600_context
*ctx
,
1376 struct r600_pipe_state
*state
,
1377 uint32_t offset
, uint32_t value
,
1378 uint32_t range_id
, uint32_t block_id
)
1380 _r600_pipe_state_add_reg_bo(ctx
, state
, offset
, value
,
1381 range_id
, block_id
, NULL
, 0);
1384 uint32_t r600_translate_stencil_op(int s_op
)
1387 case PIPE_STENCIL_OP_KEEP
:
1388 return V_028800_STENCIL_KEEP
;
1389 case PIPE_STENCIL_OP_ZERO
:
1390 return V_028800_STENCIL_ZERO
;
1391 case PIPE_STENCIL_OP_REPLACE
:
1392 return V_028800_STENCIL_REPLACE
;
1393 case PIPE_STENCIL_OP_INCR
:
1394 return V_028800_STENCIL_INCR
;
1395 case PIPE_STENCIL_OP_DECR
:
1396 return V_028800_STENCIL_DECR
;
1397 case PIPE_STENCIL_OP_INCR_WRAP
:
1398 return V_028800_STENCIL_INCR_WRAP
;
1399 case PIPE_STENCIL_OP_DECR_WRAP
:
1400 return V_028800_STENCIL_DECR_WRAP
;
1401 case PIPE_STENCIL_OP_INVERT
:
1402 return V_028800_STENCIL_INVERT
;
1404 R600_ERR("Unknown stencil op %d", s_op
);
1411 uint32_t r600_translate_fill(uint32_t func
)
1414 case PIPE_POLYGON_MODE_FILL
:
1416 case PIPE_POLYGON_MODE_LINE
:
1418 case PIPE_POLYGON_MODE_POINT
:
1426 unsigned r600_tex_wrap(unsigned wrap
)
1430 case PIPE_TEX_WRAP_REPEAT
:
1431 return V_03C000_SQ_TEX_WRAP
;
1432 case PIPE_TEX_WRAP_CLAMP
:
1433 return V_03C000_SQ_TEX_CLAMP_HALF_BORDER
;
1434 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
1435 return V_03C000_SQ_TEX_CLAMP_LAST_TEXEL
;
1436 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
1437 return V_03C000_SQ_TEX_CLAMP_BORDER
;
1438 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
1439 return V_03C000_SQ_TEX_MIRROR
;
1440 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
1441 return V_03C000_SQ_TEX_MIRROR_ONCE_HALF_BORDER
;
1442 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
1443 return V_03C000_SQ_TEX_MIRROR_ONCE_LAST_TEXEL
;
1444 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
1445 return V_03C000_SQ_TEX_MIRROR_ONCE_BORDER
;
1449 unsigned r600_tex_filter(unsigned filter
)
1453 case PIPE_TEX_FILTER_NEAREST
:
1454 return V_03C000_SQ_TEX_XY_FILTER_POINT
;
1455 case PIPE_TEX_FILTER_LINEAR
:
1456 return V_03C000_SQ_TEX_XY_FILTER_BILINEAR
;
1460 unsigned r600_tex_mipfilter(unsigned filter
)
1463 case PIPE_TEX_MIPFILTER_NEAREST
:
1464 return V_03C000_SQ_TEX_Z_FILTER_POINT
;
1465 case PIPE_TEX_MIPFILTER_LINEAR
:
1466 return V_03C000_SQ_TEX_Z_FILTER_LINEAR
;
1468 case PIPE_TEX_MIPFILTER_NONE
:
1469 return V_03C000_SQ_TEX_Z_FILTER_NONE
;
1473 unsigned r600_tex_compare(unsigned compare
)
1477 case PIPE_FUNC_NEVER
:
1478 return V_03C000_SQ_TEX_DEPTH_COMPARE_NEVER
;
1479 case PIPE_FUNC_LESS
:
1480 return V_03C000_SQ_TEX_DEPTH_COMPARE_LESS
;
1481 case PIPE_FUNC_EQUAL
:
1482 return V_03C000_SQ_TEX_DEPTH_COMPARE_EQUAL
;
1483 case PIPE_FUNC_LEQUAL
:
1484 return V_03C000_SQ_TEX_DEPTH_COMPARE_LESSEQUAL
;
1485 case PIPE_FUNC_GREATER
:
1486 return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATER
;
1487 case PIPE_FUNC_NOTEQUAL
:
1488 return V_03C000_SQ_TEX_DEPTH_COMPARE_NOTEQUAL
;
1489 case PIPE_FUNC_GEQUAL
:
1490 return V_03C000_SQ_TEX_DEPTH_COMPARE_GREATEREQUAL
;
1491 case PIPE_FUNC_ALWAYS
:
1492 return V_03C000_SQ_TEX_DEPTH_COMPARE_ALWAYS
;
1496 /* keep this at the end of this file, please */
1497 void r600_init_common_state_functions(struct r600_context
*rctx
)
1499 rctx
->context
.create_fs_state
= r600_create_ps_state
;
1500 rctx
->context
.create_vs_state
= r600_create_vs_state
;
1501 rctx
->context
.create_vertex_elements_state
= r600_create_vertex_elements
;
1502 rctx
->context
.bind_blend_state
= r600_bind_blend_state
;
1503 rctx
->context
.bind_depth_stencil_alpha_state
= r600_bind_dsa_state
;
1504 rctx
->context
.bind_fragment_sampler_states
= r600_bind_ps_sampler_states
;
1505 rctx
->context
.bind_fs_state
= r600_bind_ps_state
;
1506 rctx
->context
.bind_rasterizer_state
= r600_bind_rs_state
;
1507 rctx
->context
.bind_vertex_elements_state
= r600_bind_vertex_elements
;
1508 rctx
->context
.bind_vertex_sampler_states
= r600_bind_vs_sampler_states
;
1509 rctx
->context
.bind_vs_state
= r600_bind_vs_state
;
1510 rctx
->context
.delete_blend_state
= r600_delete_state
;
1511 rctx
->context
.delete_depth_stencil_alpha_state
= r600_delete_state
;
1512 rctx
->context
.delete_fs_state
= r600_delete_ps_state
;
1513 rctx
->context
.delete_rasterizer_state
= r600_delete_rs_state
;
1514 rctx
->context
.delete_sampler_state
= r600_delete_sampler_state
;
1515 rctx
->context
.delete_vertex_elements_state
= r600_delete_vertex_elements
;
1516 rctx
->context
.delete_vs_state
= r600_delete_vs_state
;
1517 rctx
->context
.set_blend_color
= r600_set_blend_color
;
1518 rctx
->context
.set_constant_buffer
= r600_set_constant_buffer
;
1519 rctx
->context
.set_sample_mask
= r600_set_sample_mask
;
1520 rctx
->context
.set_stencil_ref
= r600_set_pipe_stencil_ref
;
1521 rctx
->context
.set_viewport_state
= r600_set_viewport_state
;
1522 rctx
->context
.set_vertex_buffers
= r600_set_vertex_buffers
;
1523 rctx
->context
.set_index_buffer
= r600_set_index_buffer
;
1524 rctx
->context
.set_fragment_sampler_views
= r600_set_ps_sampler_views
;
1525 rctx
->context
.set_vertex_sampler_views
= r600_set_vs_sampler_views
;
1526 rctx
->context
.sampler_view_destroy
= r600_sampler_view_destroy
;
1527 rctx
->context
.texture_barrier
= r600_texture_barrier
;
1528 rctx
->context
.create_stream_output_target
= r600_create_so_target
;
1529 rctx
->context
.stream_output_target_destroy
= r600_so_target_destroy
;
1530 rctx
->context
.set_stream_output_targets
= r600_set_so_targets
;
1531 rctx
->context
.draw_vbo
= r600_draw_vbo
;