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 "util/u_blitter.h"
28 #include "util/u_memory.h"
29 #include "util/u_format.h"
30 #include "pipebuffer/pb_buffer.h"
31 #include "pipe/p_shader_tokens.h"
32 #include "tgsi/tgsi_parse.h"
33 #include "r600_formats.h"
34 #include "r600_pipe.h"
37 static bool r600_conv_pipe_prim(unsigned pprim
, unsigned *prim
)
39 static const int prim_conv
[] = {
40 V_008958_DI_PT_POINTLIST
,
41 V_008958_DI_PT_LINELIST
,
42 V_008958_DI_PT_LINELOOP
,
43 V_008958_DI_PT_LINESTRIP
,
44 V_008958_DI_PT_TRILIST
,
45 V_008958_DI_PT_TRISTRIP
,
46 V_008958_DI_PT_TRIFAN
,
47 V_008958_DI_PT_QUADLIST
,
48 V_008958_DI_PT_QUADSTRIP
,
49 V_008958_DI_PT_POLYGON
,
56 *prim
= prim_conv
[pprim
];
58 fprintf(stderr
, "%s:%d unsupported %d\n", __func__
, __LINE__
, pprim
);
64 /* common state between evergreen and r600 */
65 void r600_bind_blend_state(struct pipe_context
*ctx
, void *state
)
67 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
68 struct r600_pipe_blend
*blend
= (struct r600_pipe_blend
*)state
;
69 struct r600_pipe_state
*rstate
;
73 rstate
= &blend
->rstate
;
74 rctx
->states
[rstate
->id
] = rstate
;
75 rctx
->cb_target_mask
= blend
->cb_target_mask
;
77 /* Replace every bit except MULTIWRITE_ENABLE. */
78 rctx
->cb_color_control
&= ~C_028808_MULTIWRITE_ENABLE
;
79 rctx
->cb_color_control
|= blend
->cb_color_control
& C_028808_MULTIWRITE_ENABLE
;
81 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
84 static void r600_set_stencil_ref(struct pipe_context
*ctx
,
85 const struct r600_stencil_ref
*state
)
87 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
88 struct r600_pipe_state
*rstate
= CALLOC_STRUCT(r600_pipe_state
);
93 rstate
->id
= R600_PIPE_STATE_STENCIL_REF
;
94 r600_pipe_state_add_reg(rstate
,
95 R_028430_DB_STENCILREFMASK
,
96 S_028430_STENCILREF(state
->ref_value
[0]) |
97 S_028430_STENCILMASK(state
->valuemask
[0]) |
98 S_028430_STENCILWRITEMASK(state
->writemask
[0]),
100 r600_pipe_state_add_reg(rstate
,
101 R_028434_DB_STENCILREFMASK_BF
,
102 S_028434_STENCILREF_BF(state
->ref_value
[1]) |
103 S_028434_STENCILMASK_BF(state
->valuemask
[1]) |
104 S_028434_STENCILWRITEMASK_BF(state
->writemask
[1]),
105 0xFFFFFFFF, NULL
, 0);
107 free(rctx
->states
[R600_PIPE_STATE_STENCIL_REF
]);
108 rctx
->states
[R600_PIPE_STATE_STENCIL_REF
] = rstate
;
109 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
112 void r600_set_pipe_stencil_ref(struct pipe_context
*ctx
,
113 const struct pipe_stencil_ref
*state
)
115 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
116 struct r600_pipe_dsa
*dsa
= (struct r600_pipe_dsa
*)rctx
->states
[R600_PIPE_STATE_DSA
];
117 struct r600_stencil_ref ref
;
119 rctx
->stencil_ref
= *state
;
124 ref
.ref_value
[0] = state
->ref_value
[0];
125 ref
.ref_value
[1] = state
->ref_value
[1];
126 ref
.valuemask
[0] = dsa
->valuemask
[0];
127 ref
.valuemask
[1] = dsa
->valuemask
[1];
128 ref
.writemask
[0] = dsa
->writemask
[0];
129 ref
.writemask
[1] = dsa
->writemask
[1];
131 r600_set_stencil_ref(ctx
, &ref
);
134 void r600_bind_dsa_state(struct pipe_context
*ctx
, void *state
)
136 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
137 struct r600_pipe_dsa
*dsa
= state
;
138 struct r600_pipe_state
*rstate
;
139 struct r600_stencil_ref ref
;
143 rstate
= &dsa
->rstate
;
144 rctx
->states
[rstate
->id
] = rstate
;
145 rctx
->alpha_ref
= dsa
->alpha_ref
;
146 rctx
->alpha_ref_dirty
= true;
147 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
149 ref
.ref_value
[0] = rctx
->stencil_ref
.ref_value
[0];
150 ref
.ref_value
[1] = rctx
->stencil_ref
.ref_value
[1];
151 ref
.valuemask
[0] = dsa
->valuemask
[0];
152 ref
.valuemask
[1] = dsa
->valuemask
[1];
153 ref
.writemask
[0] = dsa
->writemask
[0];
154 ref
.writemask
[1] = dsa
->writemask
[1];
156 r600_set_stencil_ref(ctx
, &ref
);
159 void r600_bind_rs_state(struct pipe_context
*ctx
, void *state
)
161 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
162 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
167 rctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
168 rctx
->two_side
= rs
->two_side
;
169 rctx
->pa_sc_line_stipple
= rs
->pa_sc_line_stipple
;
170 rctx
->pa_su_sc_mode_cntl
= rs
->pa_su_sc_mode_cntl
;
172 rctx
->rasterizer
= rs
;
174 rctx
->states
[rs
->rstate
.id
] = &rs
->rstate
;
175 r600_context_pipe_state_set(&rctx
->ctx
, &rs
->rstate
);
177 if (rctx
->chip_class
>= EVERGREEN
) {
178 evergreen_polygon_offset_update(rctx
);
180 r600_polygon_offset_update(rctx
);
184 void r600_delete_rs_state(struct pipe_context
*ctx
, void *state
)
186 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
187 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
189 if (rctx
->rasterizer
== rs
) {
190 rctx
->rasterizer
= NULL
;
192 if (rctx
->states
[rs
->rstate
.id
] == &rs
->rstate
) {
193 rctx
->states
[rs
->rstate
.id
] = NULL
;
198 void r600_sampler_view_destroy(struct pipe_context
*ctx
,
199 struct pipe_sampler_view
*state
)
201 struct r600_pipe_sampler_view
*resource
= (struct r600_pipe_sampler_view
*)state
;
203 pipe_resource_reference(&state
->texture
, NULL
);
207 void r600_delete_state(struct pipe_context
*ctx
, void *state
)
209 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
210 struct r600_pipe_state
*rstate
= (struct r600_pipe_state
*)state
;
212 if (rctx
->states
[rstate
->id
] == rstate
) {
213 rctx
->states
[rstate
->id
] = NULL
;
215 for (int i
= 0; i
< rstate
->nregs
; i
++) {
216 pipe_resource_reference((struct pipe_resource
**)&rstate
->regs
[i
].bo
, NULL
);
221 void r600_bind_vertex_elements(struct pipe_context
*ctx
, void *state
)
223 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
224 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
226 rctx
->vertex_elements
= v
;
228 u_vbuf_bind_vertex_elements(rctx
->vbuf_mgr
, state
,
231 rctx
->states
[v
->rstate
.id
] = &v
->rstate
;
232 r600_context_pipe_state_set(&rctx
->ctx
, &v
->rstate
);
236 void r600_delete_vertex_element(struct pipe_context
*ctx
, void *state
)
238 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
239 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
241 if (rctx
->states
[v
->rstate
.id
] == &v
->rstate
) {
242 rctx
->states
[v
->rstate
.id
] = NULL
;
244 if (rctx
->vertex_elements
== state
)
245 rctx
->vertex_elements
= NULL
;
247 pipe_resource_reference((struct pipe_resource
**)&v
->fetch_shader
, NULL
);
248 u_vbuf_destroy_vertex_elements(rctx
->vbuf_mgr
, v
->vmgr_elements
);
253 void r600_set_index_buffer(struct pipe_context
*ctx
,
254 const struct pipe_index_buffer
*ib
)
256 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
258 u_vbuf_set_index_buffer(rctx
->vbuf_mgr
, ib
);
261 void r600_set_vertex_buffers(struct pipe_context
*ctx
, unsigned count
,
262 const struct pipe_vertex_buffer
*buffers
)
264 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
268 for (i
= 0; i
< count
; i
++) {
269 if (!buffers
[i
].buffer
) {
270 if (rctx
->chip_class
>= EVERGREEN
) {
271 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
273 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
277 for (; i
< rctx
->vbuf_mgr
->nr_real_vertex_buffers
; i
++) {
278 if (rctx
->chip_class
>= EVERGREEN
) {
279 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
281 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
285 u_vbuf_set_vertex_buffers(rctx
->vbuf_mgr
, count
, buffers
);
288 void *r600_create_vertex_elements(struct pipe_context
*ctx
,
290 const struct pipe_vertex_element
*elements
)
292 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
293 struct r600_vertex_element
*v
= CALLOC_STRUCT(r600_vertex_element
);
301 u_vbuf_create_vertex_elements(rctx
->vbuf_mgr
, count
,
302 elements
, v
->elements
);
304 if (r600_vertex_elements_build_fetch_shader(rctx
, v
)) {
312 void *r600_create_shader_state(struct pipe_context
*ctx
,
313 const struct pipe_shader_state
*state
)
315 struct r600_pipe_shader
*shader
= CALLOC_STRUCT(r600_pipe_shader
);
318 shader
->tokens
= tgsi_dup_tokens(state
->tokens
);
319 shader
->so
= state
->stream_output
;
321 r
= r600_pipe_shader_create(ctx
, shader
);
328 void r600_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
330 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
332 /* TODO delete old shader */
333 rctx
->ps_shader
= (struct r600_pipe_shader
*)state
;
335 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
337 rctx
->cb_color_control
&= C_028808_MULTIWRITE_ENABLE
;
338 rctx
->cb_color_control
|= S_028808_MULTIWRITE_ENABLE(!!rctx
->ps_shader
->shader
.fs_write_all
);
340 if (rctx
->ps_shader
&& rctx
->vs_shader
) {
341 r600_adjust_gprs(rctx
);
345 void r600_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
347 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
349 /* TODO delete old shader */
350 rctx
->vs_shader
= (struct r600_pipe_shader
*)state
;
352 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_shader
->rstate
);
354 if (rctx
->ps_shader
&& rctx
->vs_shader
) {
355 r600_adjust_gprs(rctx
);
359 void r600_delete_ps_shader(struct pipe_context
*ctx
, void *state
)
361 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
362 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
364 if (rctx
->ps_shader
== shader
) {
365 rctx
->ps_shader
= NULL
;
368 free(shader
->tokens
);
369 r600_pipe_shader_destroy(ctx
, shader
);
373 void r600_delete_vs_shader(struct pipe_context
*ctx
, void *state
)
375 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
376 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
378 if (rctx
->vs_shader
== shader
) {
379 rctx
->vs_shader
= NULL
;
382 free(shader
->tokens
);
383 r600_pipe_shader_destroy(ctx
, shader
);
387 static void r600_update_alpha_ref(struct r600_pipe_context
*rctx
)
390 struct r600_pipe_state rstate
;
392 alpha_ref
= rctx
->alpha_ref
;
394 if (rctx
->export_16bpc
)
395 alpha_ref
&= ~0x1FFF;
396 r600_pipe_state_add_reg(&rstate
, R_028438_SX_ALPHA_REF
, alpha_ref
, 0xFFFFFFFF, NULL
, 0);
398 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
399 rctx
->alpha_ref_dirty
= false;
402 void r600_set_constant_buffer(struct pipe_context
*ctx
, uint shader
, uint index
,
403 struct pipe_resource
*buffer
)
405 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
406 struct r600_resource
*rbuffer
= r600_resource(buffer
);
407 struct r600_pipe_resource_state
*rstate
;
411 /* Note that the state tracker can unbind constant buffers by
414 if (buffer
== NULL
) {
418 r600_upload_const_buffer(rctx
, &rbuffer
, &offset
);
419 va_offset
= r600_resource_va(ctx
->screen
, (void*)rbuffer
);
424 case PIPE_SHADER_VERTEX
:
425 rctx
->vs_const_buffer
.nregs
= 0;
426 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
427 R_028180_ALU_CONST_BUFFER_SIZE_VS_0
+ index
* 4,
428 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
429 0xFFFFFFFF, NULL
, 0);
430 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
431 R_028980_ALU_CONST_CACHE_VS_0
+ index
* 4,
432 va_offset
, 0xFFFFFFFF, rbuffer
, RADEON_USAGE_READ
);
433 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_const_buffer
);
435 rstate
= &rctx
->vs_const_buffer_resource
[index
];
437 if (rctx
->chip_class
>= EVERGREEN
) {
438 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
440 r600_pipe_init_buffer_resource(rctx
, rstate
);
444 if (rctx
->chip_class
>= EVERGREEN
) {
445 evergreen_pipe_mod_buffer_resource(ctx
, rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
446 evergreen_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
448 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
449 r600_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
452 case PIPE_SHADER_FRAGMENT
:
453 rctx
->ps_const_buffer
.nregs
= 0;
454 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
455 R_028140_ALU_CONST_BUFFER_SIZE_PS_0
,
456 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
457 0xFFFFFFFF, NULL
, 0);
458 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
459 R_028940_ALU_CONST_CACHE_PS_0
,
460 va_offset
, 0xFFFFFFFF, rbuffer
, RADEON_USAGE_READ
);
461 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_const_buffer
);
463 rstate
= &rctx
->ps_const_buffer_resource
[index
];
465 if (rctx
->chip_class
>= EVERGREEN
) {
466 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
468 r600_pipe_init_buffer_resource(rctx
, rstate
);
471 if (rctx
->chip_class
>= EVERGREEN
) {
472 evergreen_pipe_mod_buffer_resource(ctx
, rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
473 evergreen_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
475 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
476 r600_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
480 R600_ERR("unsupported %d\n", shader
);
484 if (buffer
!= &rbuffer
->b
.b
.b
)
485 pipe_resource_reference((struct pipe_resource
**)&rbuffer
, NULL
);
488 struct pipe_stream_output_target
*
489 r600_create_so_target(struct pipe_context
*ctx
,
490 struct pipe_resource
*buffer
,
491 unsigned buffer_offset
,
492 unsigned buffer_size
)
494 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
495 struct r600_so_target
*t
;
498 t
= CALLOC_STRUCT(r600_so_target
);
503 t
->b
.reference
.count
= 1;
505 pipe_resource_reference(&t
->b
.buffer
, buffer
);
506 t
->b
.buffer_offset
= buffer_offset
;
507 t
->b
.buffer_size
= buffer_size
;
509 t
->filled_size
= (struct r600_resource
*)
510 pipe_buffer_create(ctx
->screen
, PIPE_BIND_CUSTOM
, PIPE_USAGE_STATIC
, 4);
511 ptr
= rctx
->ws
->buffer_map(t
->filled_size
->buf
, rctx
->ctx
.cs
, PIPE_TRANSFER_WRITE
);
512 memset(ptr
, 0, t
->filled_size
->buf
->size
);
513 rctx
->ws
->buffer_unmap(t
->filled_size
->buf
);
518 void r600_so_target_destroy(struct pipe_context
*ctx
,
519 struct pipe_stream_output_target
*target
)
521 struct r600_so_target
*t
= (struct r600_so_target
*)target
;
522 pipe_resource_reference(&t
->b
.buffer
, NULL
);
523 pipe_resource_reference((struct pipe_resource
**)&t
->filled_size
, NULL
);
527 void r600_set_so_targets(struct pipe_context
*ctx
,
528 unsigned num_targets
,
529 struct pipe_stream_output_target
**targets
,
530 unsigned append_bitmask
)
532 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
535 /* Stop streamout. */
536 if (rctx
->ctx
.num_so_targets
) {
537 r600_context_streamout_end(&rctx
->ctx
);
540 /* Set the new targets. */
541 for (i
= 0; i
< num_targets
; i
++) {
542 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->ctx
.so_targets
[i
], targets
[i
]);
544 for (; i
< rctx
->ctx
.num_so_targets
; i
++) {
545 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->ctx
.so_targets
[i
], NULL
);
548 rctx
->ctx
.num_so_targets
= num_targets
;
549 rctx
->ctx
.streamout_start
= num_targets
!= 0;
550 rctx
->ctx
.streamout_append_bitmask
= append_bitmask
;
553 static void r600_vertex_buffer_update(struct r600_pipe_context
*rctx
)
555 struct r600_pipe_resource_state
*rstate
;
556 struct r600_resource
*rbuffer
;
557 struct pipe_vertex_buffer
*vertex_buffer
;
558 unsigned i
, count
, offset
;
560 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
561 /* one resource per vertex elements */
562 count
= rctx
->vertex_elements
->count
;
564 /* bind vertex buffer once */
565 count
= rctx
->vbuf_mgr
->nr_real_vertex_buffers
;
568 for (i
= 0 ; i
< count
; i
++) {
569 rstate
= &rctx
->fs_resource
[i
];
571 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
572 /* one resource per vertex elements */
573 unsigned vbuffer_index
;
574 vbuffer_index
= rctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
575 vertex_buffer
= &rctx
->vbuf_mgr
->real_vertex_buffer
[vbuffer_index
];
576 rbuffer
= (struct r600_resource
*)vertex_buffer
->buffer
;
577 offset
= rctx
->vertex_elements
->vbuffer_offset
[i
];
579 /* bind vertex buffer once */
580 vertex_buffer
= &rctx
->vbuf_mgr
->real_vertex_buffer
[i
];
581 rbuffer
= (struct r600_resource
*)vertex_buffer
->buffer
;
584 if (vertex_buffer
== NULL
|| rbuffer
== NULL
)
586 offset
+= vertex_buffer
->buffer_offset
;
589 if (rctx
->chip_class
>= EVERGREEN
) {
590 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
592 r600_pipe_init_buffer_resource(rctx
, rstate
);
596 if (rctx
->chip_class
>= EVERGREEN
) {
597 evergreen_pipe_mod_buffer_resource(&rctx
->context
, rstate
, rbuffer
, offset
, vertex_buffer
->stride
, RADEON_USAGE_READ
);
598 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
600 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, vertex_buffer
->stride
, RADEON_USAGE_READ
);
601 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
606 static int r600_shader_rebuild(struct pipe_context
* ctx
, struct r600_pipe_shader
* shader
)
608 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
611 r600_pipe_shader_destroy(ctx
, shader
);
612 r
= r600_pipe_shader_create(ctx
, shader
);
616 r600_context_pipe_state_set(&rctx
->ctx
, &shader
->rstate
);
621 static void r600_update_derived_state(struct r600_pipe_context
*rctx
)
623 struct pipe_context
* ctx
= (struct pipe_context
*)rctx
;
624 struct r600_pipe_state rstate
;
625 unsigned user_clip_plane_enable
;
627 if (rctx
->vs_shader
->shader
.clip_dist_write
|| rctx
->vs_shader
->shader
.vs_prohibit_ucps
)
628 user_clip_plane_enable
= 0;
630 user_clip_plane_enable
= rctx
->rasterizer
->clip_plane_enable
& 0x3F;
634 if (user_clip_plane_enable
!= rctx
->user_clip_plane_enable
) {
635 r600_pipe_state_add_reg(&rstate
, R_028810_PA_CL_CLIP_CNTL
, user_clip_plane_enable
, 0x3F, NULL
, 0);
636 rctx
->user_clip_plane_enable
= user_clip_plane_enable
;
640 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
642 if (!rctx
->blitter
->running
) {
643 if (rctx
->have_depth_fb
|| rctx
->have_depth_texture
)
644 r600_flush_depth_textures(rctx
);
647 if (rctx
->chip_class
< EVERGREEN
) {
648 r600_update_sampler_states(rctx
);
651 if ((rctx
->ps_shader
->shader
.two_side
!= rctx
->two_side
) ||
652 ((rctx
->chip_class
>= EVERGREEN
) && rctx
->ps_shader
->shader
.fs_write_all
&&
653 (rctx
->ps_shader
->shader
.nr_cbufs
!= rctx
->nr_cbufs
))) {
654 r600_shader_rebuild(&rctx
->context
, rctx
->ps_shader
);
657 if (rctx
->alpha_ref_dirty
) {
658 r600_update_alpha_ref(rctx
);
661 if (rctx
->ps_shader
&& ((rctx
->sprite_coord_enable
&&
662 (rctx
->ps_shader
->sprite_coord_enable
!= rctx
->sprite_coord_enable
)) ||
663 (rctx
->rasterizer
&& rctx
->rasterizer
->flatshade
!= rctx
->ps_shader
->flatshade
))) {
665 if (rctx
->chip_class
>= EVERGREEN
)
666 evergreen_pipe_shader_ps(ctx
, rctx
->ps_shader
);
668 r600_pipe_shader_ps(ctx
, rctx
->ps_shader
);
670 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
675 void r600_draw_vbo(struct pipe_context
*ctx
, const struct pipe_draw_info
*dinfo
)
677 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
678 struct pipe_draw_info info
= *dinfo
;
679 struct r600_draw rdraw
= {};
680 struct pipe_index_buffer ib
= {};
681 unsigned prim
, mask
, ls_mask
= 0;
683 if ((!info
.count
&& (info
.indexed
|| !info
.count_from_stream_output
)) ||
684 (info
.indexed
&& !rctx
->vbuf_mgr
->index_buffer
.buffer
) ||
685 !r600_conv_pipe_prim(info
.mode
, &prim
)) {
689 if (!rctx
->ps_shader
|| !rctx
->vs_shader
)
692 r600_update_derived_state(rctx
);
694 u_vbuf_draw_begin(rctx
->vbuf_mgr
, &info
);
695 r600_vertex_buffer_update(rctx
);
697 rdraw
.vgt_num_indices
= info
.count
;
698 rdraw
.vgt_num_instances
= info
.instance_count
;
701 /* Initialize the index buffer struct. */
702 pipe_resource_reference(&ib
.buffer
, rctx
->vbuf_mgr
->index_buffer
.buffer
);
703 ib
.index_size
= rctx
->vbuf_mgr
->index_buffer
.index_size
;
704 ib
.offset
= rctx
->vbuf_mgr
->index_buffer
.offset
+ info
.start
* ib
.index_size
;
706 /* Translate or upload, if needed. */
707 r600_translate_index_buffer(rctx
, &ib
, info
.count
);
709 if (u_vbuf_resource(ib
.buffer
)->user_ptr
) {
710 r600_upload_index_buffer(rctx
, &ib
, info
.count
);
713 /* Initialize the r600_draw struct with index buffer info. */
714 if (ib
.index_size
== 4) {
715 rdraw
.vgt_index_type
= VGT_INDEX_32
|
716 (R600_BIG_ENDIAN
? VGT_DMA_SWAP_32_BIT
: 0);
718 rdraw
.vgt_index_type
= VGT_INDEX_16
|
719 (R600_BIG_ENDIAN
? VGT_DMA_SWAP_16_BIT
: 0);
721 rdraw
.indices
= (struct r600_resource
*)ib
.buffer
;
722 rdraw
.indices_bo_offset
= ib
.offset
;
723 rdraw
.vgt_draw_initiator
= V_0287F0_DI_SRC_SEL_DMA
;
725 info
.index_bias
= info
.start
;
726 rdraw
.vgt_draw_initiator
= V_0287F0_DI_SRC_SEL_AUTO_INDEX
;
727 if (info
.count_from_stream_output
) {
728 rdraw
.vgt_draw_initiator
|= S_0287F0_USE_OPAQUE(1);
730 r600_context_draw_opaque_count(&rctx
->ctx
, (struct r600_so_target
*)info
.count_from_stream_output
);
734 rctx
->ctx
.vs_so_stride_in_dw
= rctx
->vs_shader
->so
.stride
;
736 mask
= (1ULL << ((unsigned)rctx
->framebuffer
.nr_cbufs
* 4)) - 1;
738 if (rctx
->vgt
.id
!= R600_PIPE_STATE_VGT
) {
739 rctx
->vgt
.id
= R600_PIPE_STATE_VGT
;
741 r600_pipe_state_add_reg(&rctx
->vgt
, R_008958_VGT_PRIMITIVE_TYPE
, prim
, 0xFFFFFFFF, NULL
, 0);
742 r600_pipe_state_add_reg(&rctx
->vgt
, R_028238_CB_TARGET_MASK
, rctx
->cb_target_mask
& mask
, 0xFFFFFFFF, NULL
, 0);
743 r600_pipe_state_add_reg(&rctx
->vgt
, R_028400_VGT_MAX_VTX_INDX
, ~0, 0xFFFFFFFF, NULL
, 0);
744 r600_pipe_state_add_reg(&rctx
->vgt
, R_028404_VGT_MIN_VTX_INDX
, 0, 0xFFFFFFFF, NULL
, 0);
745 r600_pipe_state_add_reg(&rctx
->vgt
, R_028408_VGT_INDX_OFFSET
, info
.index_bias
, 0xFFFFFFFF, NULL
, 0);
746 r600_pipe_state_add_reg(&rctx
->vgt
, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX
, info
.restart_index
, 0xFFFFFFFF, NULL
, 0);
747 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN
, info
.primitive_restart
, 0xFFFFFFFF, NULL
, 0);
748 r600_pipe_state_add_reg(&rctx
->vgt
, R_03CFF0_SQ_VTX_BASE_VTX_LOC
, 0, 0xFFFFFFFF, NULL
, 0);
749 r600_pipe_state_add_reg(&rctx
->vgt
, R_03CFF4_SQ_VTX_START_INST_LOC
, info
.start_instance
, 0xFFFFFFFF, NULL
, 0);
750 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A0C_PA_SC_LINE_STIPPLE
, 0, 0xFFFFFFFF, NULL
, 0);
751 r600_pipe_state_add_reg(&rctx
->vgt
, R_028814_PA_SU_SC_MODE_CNTL
, 0, 0xFFFFFFFF, NULL
, 0);
752 if (rctx
->chip_class
<= R700
)
753 r600_pipe_state_add_reg(&rctx
->vgt
, R_028808_CB_COLOR_CONTROL
, rctx
->cb_color_control
, 0xFFFFFFFF, NULL
, 0);
754 r600_pipe_state_add_reg(&rctx
->vgt
, R_02881C_PA_CL_VS_OUT_CNTL
, 0, 0xFFFFFFFF, NULL
, 0);
758 r600_pipe_state_mod_reg(&rctx
->vgt
, prim
);
759 r600_pipe_state_mod_reg(&rctx
->vgt
, rctx
->cb_target_mask
& mask
);
760 r600_pipe_state_mod_reg(&rctx
->vgt
, ~0);
761 r600_pipe_state_mod_reg(&rctx
->vgt
, 0);
762 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.index_bias
);
763 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.restart_index
);
764 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.primitive_restart
);
765 r600_pipe_state_mod_reg(&rctx
->vgt
, 0);
766 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.start_instance
);
768 if (prim
== V_008958_DI_PT_LINELIST
)
770 else if (prim
== V_008958_DI_PT_LINESTRIP
)
772 r600_pipe_state_mod_reg(&rctx
->vgt
, S_028A0C_AUTO_RESET_CNTL(ls_mask
) | rctx
->pa_sc_line_stipple
);
774 if (info
.mode
== PIPE_PRIM_QUADS
|| info
.mode
== PIPE_PRIM_QUAD_STRIP
|| info
.mode
== PIPE_PRIM_POLYGON
) {
775 r600_pipe_state_mod_reg(&rctx
->vgt
, S_028814_PROVOKING_VTX_LAST(1) | rctx
->pa_su_sc_mode_cntl
);
777 r600_pipe_state_mod_reg(&rctx
->vgt
, rctx
->pa_su_sc_mode_cntl
);
779 if (rctx
->chip_class
<= R700
)
780 r600_pipe_state_mod_reg(&rctx
->vgt
, rctx
->cb_color_control
);
781 r600_pipe_state_mod_reg(&rctx
->vgt
,
782 rctx
->vs_shader
->pa_cl_vs_out_cntl
|
783 (rctx
->rasterizer
->clip_plane_enable
& rctx
->vs_shader
->shader
.clip_dist_write
));
785 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vgt
);
787 if (rctx
->chip_class
>= EVERGREEN
) {
788 evergreen_context_draw(&rctx
->ctx
, &rdraw
);
790 r600_context_draw(&rctx
->ctx
, &rdraw
);
793 if (rctx
->framebuffer
.zsbuf
)
795 struct pipe_resource
*tex
= rctx
->framebuffer
.zsbuf
->texture
;
796 ((struct r600_resource_texture
*)tex
)->dirty_db
= TRUE
;
799 pipe_resource_reference(&ib
.buffer
, NULL
);
800 u_vbuf_draw_end(rctx
->vbuf_mgr
);
803 void _r600_pipe_state_add_reg(struct r600_context
*ctx
,
804 struct r600_pipe_state
*state
,
805 u32 offset
, u32 value
, u32 mask
,
806 u32 range_id
, u32 block_id
,
807 struct r600_resource
*bo
,
808 enum radeon_bo_usage usage
)
810 struct r600_range
*range
;
811 struct r600_block
*block
;
813 if (bo
) assert(usage
);
815 range
= &ctx
->range
[range_id
];
816 block
= range
->blocks
[block_id
];
817 state
->regs
[state
->nregs
].block
= block
;
818 state
->regs
[state
->nregs
].id
= (offset
- block
->start_offset
) >> 2;
820 state
->regs
[state
->nregs
].value
= value
;
821 state
->regs
[state
->nregs
].mask
= mask
;
822 state
->regs
[state
->nregs
].bo
= bo
;
823 state
->regs
[state
->nregs
].bo_usage
= usage
;
826 assert(state
->nregs
< R600_BLOCK_MAX_REG
);
829 void r600_pipe_state_add_reg_noblock(struct r600_pipe_state
*state
,
830 u32 offset
, u32 value
, u32 mask
,
831 struct r600_resource
*bo
,
832 enum radeon_bo_usage usage
)
834 if (bo
) assert(usage
);
836 state
->regs
[state
->nregs
].id
= offset
;
837 state
->regs
[state
->nregs
].block
= NULL
;
838 state
->regs
[state
->nregs
].value
= value
;
839 state
->regs
[state
->nregs
].mask
= mask
;
840 state
->regs
[state
->nregs
].bo
= bo
;
841 state
->regs
[state
->nregs
].bo_usage
= usage
;
844 assert(state
->nregs
< R600_BLOCK_MAX_REG
);