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
;
76 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
79 static void r600_set_stencil_ref(struct pipe_context
*ctx
,
80 const struct r600_stencil_ref
*state
)
82 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
83 struct r600_pipe_state
*rstate
= CALLOC_STRUCT(r600_pipe_state
);
88 rstate
->id
= R600_PIPE_STATE_STENCIL_REF
;
89 r600_pipe_state_add_reg(rstate
,
90 R_028430_DB_STENCILREFMASK
,
91 S_028430_STENCILREF(state
->ref_value
[0]) |
92 S_028430_STENCILMASK(state
->valuemask
[0]) |
93 S_028430_STENCILWRITEMASK(state
->writemask
[0]),
95 r600_pipe_state_add_reg(rstate
,
96 R_028434_DB_STENCILREFMASK_BF
,
97 S_028434_STENCILREF_BF(state
->ref_value
[1]) |
98 S_028434_STENCILMASK_BF(state
->valuemask
[1]) |
99 S_028434_STENCILWRITEMASK_BF(state
->writemask
[1]),
100 0xFFFFFFFF, NULL
, 0);
102 free(rctx
->states
[R600_PIPE_STATE_STENCIL_REF
]);
103 rctx
->states
[R600_PIPE_STATE_STENCIL_REF
] = rstate
;
104 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
107 void r600_set_pipe_stencil_ref(struct pipe_context
*ctx
,
108 const struct pipe_stencil_ref
*state
)
110 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
111 struct r600_pipe_dsa
*dsa
= (struct r600_pipe_dsa
*)rctx
->states
[R600_PIPE_STATE_DSA
];
112 struct r600_stencil_ref ref
;
114 rctx
->stencil_ref
= *state
;
119 ref
.ref_value
[0] = state
->ref_value
[0];
120 ref
.ref_value
[1] = state
->ref_value
[1];
121 ref
.valuemask
[0] = dsa
->valuemask
[0];
122 ref
.valuemask
[1] = dsa
->valuemask
[1];
123 ref
.writemask
[0] = dsa
->writemask
[0];
124 ref
.writemask
[1] = dsa
->writemask
[1];
126 r600_set_stencil_ref(ctx
, &ref
);
129 void r600_bind_dsa_state(struct pipe_context
*ctx
, void *state
)
131 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
132 struct r600_pipe_dsa
*dsa
= state
;
133 struct r600_pipe_state
*rstate
;
134 struct r600_stencil_ref ref
;
138 rstate
= &dsa
->rstate
;
139 rctx
->states
[rstate
->id
] = rstate
;
140 rctx
->alpha_ref
= dsa
->alpha_ref
;
141 rctx
->alpha_ref_dirty
= true;
142 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
144 ref
.ref_value
[0] = rctx
->stencil_ref
.ref_value
[0];
145 ref
.ref_value
[1] = rctx
->stencil_ref
.ref_value
[1];
146 ref
.valuemask
[0] = dsa
->valuemask
[0];
147 ref
.valuemask
[1] = dsa
->valuemask
[1];
148 ref
.writemask
[0] = dsa
->writemask
[0];
149 ref
.writemask
[1] = dsa
->writemask
[1];
151 r600_set_stencil_ref(ctx
, &ref
);
154 void r600_bind_rs_state(struct pipe_context
*ctx
, void *state
)
156 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
157 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
162 rctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
163 rctx
->two_side
= rs
->two_side
;
165 rctx
->rasterizer
= rs
;
167 rctx
->states
[rs
->rstate
.id
] = &rs
->rstate
;
168 r600_context_pipe_state_set(&rctx
->ctx
, &rs
->rstate
);
170 if (rctx
->chip_class
>= EVERGREEN
) {
171 evergreen_polygon_offset_update(rctx
);
173 r600_polygon_offset_update(rctx
);
177 void r600_delete_rs_state(struct pipe_context
*ctx
, void *state
)
179 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
180 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
182 if (rctx
->rasterizer
== rs
) {
183 rctx
->rasterizer
= NULL
;
185 if (rctx
->states
[rs
->rstate
.id
] == &rs
->rstate
) {
186 rctx
->states
[rs
->rstate
.id
] = NULL
;
191 void r600_sampler_view_destroy(struct pipe_context
*ctx
,
192 struct pipe_sampler_view
*state
)
194 struct r600_pipe_sampler_view
*resource
= (struct r600_pipe_sampler_view
*)state
;
196 pipe_resource_reference(&state
->texture
, NULL
);
200 void r600_delete_state(struct pipe_context
*ctx
, void *state
)
202 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
203 struct r600_pipe_state
*rstate
= (struct r600_pipe_state
*)state
;
205 if (rctx
->states
[rstate
->id
] == rstate
) {
206 rctx
->states
[rstate
->id
] = NULL
;
208 for (int i
= 0; i
< rstate
->nregs
; i
++) {
209 pipe_resource_reference((struct pipe_resource
**)&rstate
->regs
[i
].bo
, NULL
);
214 void r600_bind_vertex_elements(struct pipe_context
*ctx
, void *state
)
216 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
217 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
219 rctx
->vertex_elements
= v
;
221 u_vbuf_bind_vertex_elements(rctx
->vbuf_mgr
, state
,
224 rctx
->states
[v
->rstate
.id
] = &v
->rstate
;
225 r600_context_pipe_state_set(&rctx
->ctx
, &v
->rstate
);
229 void r600_delete_vertex_element(struct pipe_context
*ctx
, void *state
)
231 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
232 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
234 if (rctx
->states
[v
->rstate
.id
] == &v
->rstate
) {
235 rctx
->states
[v
->rstate
.id
] = NULL
;
237 if (rctx
->vertex_elements
== state
)
238 rctx
->vertex_elements
= NULL
;
240 pipe_resource_reference((struct pipe_resource
**)&v
->fetch_shader
, NULL
);
241 u_vbuf_destroy_vertex_elements(rctx
->vbuf_mgr
, v
->vmgr_elements
);
246 void r600_set_index_buffer(struct pipe_context
*ctx
,
247 const struct pipe_index_buffer
*ib
)
249 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
251 u_vbuf_set_index_buffer(rctx
->vbuf_mgr
, ib
);
254 void r600_set_vertex_buffers(struct pipe_context
*ctx
, unsigned count
,
255 const struct pipe_vertex_buffer
*buffers
)
257 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
261 for (i
= 0; i
< count
; i
++) {
262 if (!buffers
[i
].buffer
) {
263 if (rctx
->chip_class
>= EVERGREEN
) {
264 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
266 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
270 for (; i
< rctx
->vbuf_mgr
->nr_real_vertex_buffers
; i
++) {
271 if (rctx
->chip_class
>= EVERGREEN
) {
272 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
274 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
278 u_vbuf_set_vertex_buffers(rctx
->vbuf_mgr
, count
, buffers
);
281 void *r600_create_vertex_elements(struct pipe_context
*ctx
,
283 const struct pipe_vertex_element
*elements
)
285 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
286 struct r600_vertex_element
*v
= CALLOC_STRUCT(r600_vertex_element
);
294 u_vbuf_create_vertex_elements(rctx
->vbuf_mgr
, count
,
295 elements
, v
->elements
);
297 if (r600_vertex_elements_build_fetch_shader(rctx
, v
)) {
305 void *r600_create_shader_state(struct pipe_context
*ctx
,
306 const struct pipe_shader_state
*state
)
308 struct r600_pipe_shader
*shader
= CALLOC_STRUCT(r600_pipe_shader
);
311 shader
->tokens
= tgsi_dup_tokens(state
->tokens
);
312 shader
->so
= state
->stream_output
;
314 r
= r600_pipe_shader_create(ctx
, shader
);
321 void r600_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
323 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
325 /* TODO delete old shader */
326 rctx
->ps_shader
= (struct r600_pipe_shader
*)state
;
328 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
330 if (rctx
->ps_shader
&& rctx
->vs_shader
) {
331 r600_adjust_gprs(rctx
);
335 void r600_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
337 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
339 /* TODO delete old shader */
340 rctx
->vs_shader
= (struct r600_pipe_shader
*)state
;
342 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_shader
->rstate
);
344 if (rctx
->ps_shader
&& rctx
->vs_shader
) {
345 r600_adjust_gprs(rctx
);
349 void r600_delete_ps_shader(struct pipe_context
*ctx
, void *state
)
351 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
352 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
354 if (rctx
->ps_shader
== shader
) {
355 rctx
->ps_shader
= NULL
;
358 free(shader
->tokens
);
359 r600_pipe_shader_destroy(ctx
, shader
);
363 void r600_delete_vs_shader(struct pipe_context
*ctx
, void *state
)
365 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
366 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
368 if (rctx
->vs_shader
== shader
) {
369 rctx
->vs_shader
= NULL
;
372 free(shader
->tokens
);
373 r600_pipe_shader_destroy(ctx
, shader
);
377 static void r600_update_alpha_ref(struct r600_pipe_context
*rctx
)
380 struct r600_pipe_state rstate
;
382 alpha_ref
= rctx
->alpha_ref
;
384 if (rctx
->export_16bpc
)
385 alpha_ref
&= ~0x1FFF;
386 r600_pipe_state_add_reg(&rstate
, R_028438_SX_ALPHA_REF
, alpha_ref
, 0xFFFFFFFF, NULL
, 0);
388 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
389 rctx
->alpha_ref_dirty
= false;
392 void r600_set_constant_buffer(struct pipe_context
*ctx
, uint shader
, uint index
,
393 struct pipe_resource
*buffer
)
395 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
396 struct r600_resource
*rbuffer
= r600_resource(buffer
);
397 struct r600_pipe_resource_state
*rstate
;
401 /* Note that the state tracker can unbind constant buffers by
404 if (buffer
== NULL
) {
408 r600_upload_const_buffer(rctx
, &rbuffer
, &offset
);
409 va_offset
= r600_resource_va(ctx
->screen
, (void*)rbuffer
);
414 case PIPE_SHADER_VERTEX
:
415 rctx
->vs_const_buffer
.nregs
= 0;
416 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
417 R_028180_ALU_CONST_BUFFER_SIZE_VS_0
+ index
* 4,
418 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
419 0xFFFFFFFF, NULL
, 0);
420 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
421 R_028980_ALU_CONST_CACHE_VS_0
+ index
* 4,
422 va_offset
, 0xFFFFFFFF, rbuffer
, RADEON_USAGE_READ
);
423 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_const_buffer
);
425 rstate
= &rctx
->vs_const_buffer_resource
[index
];
427 if (rctx
->chip_class
>= EVERGREEN
) {
428 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
430 r600_pipe_init_buffer_resource(rctx
, rstate
);
434 if (rctx
->chip_class
>= EVERGREEN
) {
435 evergreen_pipe_mod_buffer_resource(ctx
, rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
436 evergreen_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
438 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
439 r600_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
442 case PIPE_SHADER_FRAGMENT
:
443 rctx
->ps_const_buffer
.nregs
= 0;
444 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
445 R_028140_ALU_CONST_BUFFER_SIZE_PS_0
,
446 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
447 0xFFFFFFFF, NULL
, 0);
448 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
449 R_028940_ALU_CONST_CACHE_PS_0
,
450 va_offset
, 0xFFFFFFFF, rbuffer
, RADEON_USAGE_READ
);
451 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_const_buffer
);
453 rstate
= &rctx
->ps_const_buffer_resource
[index
];
455 if (rctx
->chip_class
>= EVERGREEN
) {
456 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
458 r600_pipe_init_buffer_resource(rctx
, rstate
);
461 if (rctx
->chip_class
>= EVERGREEN
) {
462 evergreen_pipe_mod_buffer_resource(ctx
, rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
463 evergreen_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
465 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, 16, RADEON_USAGE_READ
);
466 r600_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
470 R600_ERR("unsupported %d\n", shader
);
474 if (buffer
!= &rbuffer
->b
.b
.b
)
475 pipe_resource_reference((struct pipe_resource
**)&rbuffer
, NULL
);
478 struct pipe_stream_output_target
*
479 r600_create_so_target(struct pipe_context
*ctx
,
480 struct pipe_resource
*buffer
,
481 unsigned buffer_offset
,
482 unsigned buffer_size
)
484 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
485 struct r600_so_target
*t
;
488 t
= CALLOC_STRUCT(r600_so_target
);
493 t
->b
.reference
.count
= 1;
495 pipe_resource_reference(&t
->b
.buffer
, buffer
);
496 t
->b
.buffer_offset
= buffer_offset
;
497 t
->b
.buffer_size
= buffer_size
;
499 t
->filled_size
= (struct r600_resource
*)
500 pipe_buffer_create(ctx
->screen
, PIPE_BIND_CUSTOM
, PIPE_USAGE_STATIC
, 4);
501 ptr
= rctx
->ws
->buffer_map(t
->filled_size
->buf
, rctx
->ctx
.cs
, PIPE_TRANSFER_WRITE
);
502 memset(ptr
, 0, t
->filled_size
->buf
->size
);
503 rctx
->ws
->buffer_unmap(t
->filled_size
->buf
);
508 void r600_so_target_destroy(struct pipe_context
*ctx
,
509 struct pipe_stream_output_target
*target
)
511 struct r600_so_target
*t
= (struct r600_so_target
*)target
;
512 pipe_resource_reference(&t
->b
.buffer
, NULL
);
513 pipe_resource_reference((struct pipe_resource
**)&t
->filled_size
, NULL
);
517 void r600_set_so_targets(struct pipe_context
*ctx
,
518 unsigned num_targets
,
519 struct pipe_stream_output_target
**targets
,
520 unsigned append_bitmask
)
522 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
525 /* Stop streamout. */
526 if (rctx
->ctx
.num_so_targets
) {
527 r600_context_streamout_end(&rctx
->ctx
);
530 /* Set the new targets. */
531 for (i
= 0; i
< num_targets
; i
++) {
532 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->ctx
.so_targets
[i
], targets
[i
]);
534 for (; i
< rctx
->ctx
.num_so_targets
; i
++) {
535 pipe_so_target_reference((struct pipe_stream_output_target
**)&rctx
->ctx
.so_targets
[i
], NULL
);
538 rctx
->ctx
.num_so_targets
= num_targets
;
539 rctx
->ctx
.streamout_start
= num_targets
!= 0;
540 rctx
->ctx
.streamout_append_bitmask
= append_bitmask
;
543 static void r600_vertex_buffer_update(struct r600_pipe_context
*rctx
)
545 struct r600_pipe_resource_state
*rstate
;
546 struct r600_resource
*rbuffer
;
547 struct pipe_vertex_buffer
*vertex_buffer
;
548 unsigned i
, count
, offset
;
550 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
551 /* one resource per vertex elements */
552 count
= rctx
->vertex_elements
->count
;
554 /* bind vertex buffer once */
555 count
= rctx
->vbuf_mgr
->nr_real_vertex_buffers
;
558 for (i
= 0 ; i
< count
; i
++) {
559 rstate
= &rctx
->fs_resource
[i
];
561 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
562 /* one resource per vertex elements */
563 unsigned vbuffer_index
;
564 vbuffer_index
= rctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
565 vertex_buffer
= &rctx
->vbuf_mgr
->real_vertex_buffer
[vbuffer_index
];
566 rbuffer
= (struct r600_resource
*)vertex_buffer
->buffer
;
567 offset
= rctx
->vertex_elements
->vbuffer_offset
[i
];
569 /* bind vertex buffer once */
570 vertex_buffer
= &rctx
->vbuf_mgr
->real_vertex_buffer
[i
];
571 rbuffer
= (struct r600_resource
*)vertex_buffer
->buffer
;
574 if (vertex_buffer
== NULL
|| rbuffer
== NULL
)
576 offset
+= vertex_buffer
->buffer_offset
;
579 if (rctx
->chip_class
>= EVERGREEN
) {
580 evergreen_pipe_init_buffer_resource(rctx
, rstate
);
582 r600_pipe_init_buffer_resource(rctx
, rstate
);
586 if (rctx
->chip_class
>= EVERGREEN
) {
587 evergreen_pipe_mod_buffer_resource(&rctx
->context
, rstate
, rbuffer
, offset
, vertex_buffer
->stride
, RADEON_USAGE_READ
);
588 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
590 r600_pipe_mod_buffer_resource(rstate
, rbuffer
, offset
, vertex_buffer
->stride
, RADEON_USAGE_READ
);
591 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
596 static int r600_shader_rebuild(struct pipe_context
* ctx
, struct r600_pipe_shader
* shader
)
598 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
601 r600_pipe_shader_destroy(ctx
, shader
);
602 r
= r600_pipe_shader_create(ctx
, shader
);
606 r600_context_pipe_state_set(&rctx
->ctx
, &shader
->rstate
);
611 static void r600_update_derived_state(struct r600_pipe_context
*rctx
)
613 struct pipe_context
* ctx
= (struct pipe_context
*)rctx
;
614 struct r600_pipe_state rstate
;
615 unsigned user_clip_plane_enable
;
616 unsigned clip_dist_enable
;
618 if (rctx
->vs_shader
->shader
.clip_dist_write
|| rctx
->vs_shader
->shader
.vs_prohibit_ucps
)
619 user_clip_plane_enable
= 0;
621 user_clip_plane_enable
= rctx
->rasterizer
->clip_plane_enable
& 0x3F;
623 clip_dist_enable
= rctx
->rasterizer
->clip_plane_enable
& rctx
->vs_shader
->shader
.clip_dist_write
;
626 if (user_clip_plane_enable
!= rctx
->user_clip_plane_enable
) {
627 r600_pipe_state_add_reg(&rstate
, R_028810_PA_CL_CLIP_CNTL
, user_clip_plane_enable
, 0x3F, NULL
, 0);
628 rctx
->user_clip_plane_enable
= user_clip_plane_enable
;
631 if (clip_dist_enable
!= rctx
->clip_dist_enable
) {
632 r600_pipe_state_add_reg(&rstate
, R_02881C_PA_CL_VS_OUT_CNTL
, clip_dist_enable
, 0xFF, NULL
, 0);
633 rctx
->clip_dist_enable
= clip_dist_enable
;
637 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
639 if (!rctx
->blitter
->running
) {
640 if (rctx
->have_depth_fb
|| rctx
->have_depth_texture
)
641 r600_flush_depth_textures(rctx
);
644 if (rctx
->chip_class
< EVERGREEN
) {
645 r600_update_sampler_states(rctx
);
648 if ((rctx
->ps_shader
->shader
.two_side
!= rctx
->two_side
) ||
649 ((rctx
->chip_class
>= EVERGREEN
) && rctx
->ps_shader
->shader
.fs_write_all
&&
650 (rctx
->ps_shader
->shader
.nr_cbufs
!= rctx
->nr_cbufs
))) {
651 r600_shader_rebuild(&rctx
->context
, rctx
->ps_shader
);
654 if (rctx
->alpha_ref_dirty
) {
655 r600_update_alpha_ref(rctx
);
658 if (rctx
->ps_shader
&& ((rctx
->sprite_coord_enable
&&
659 (rctx
->ps_shader
->sprite_coord_enable
!= rctx
->sprite_coord_enable
)) ||
660 (rctx
->rasterizer
&& rctx
->rasterizer
->flatshade
!= rctx
->ps_shader
->flatshade
))) {
662 if (rctx
->chip_class
>= EVERGREEN
)
663 evergreen_pipe_shader_ps(ctx
, rctx
->ps_shader
);
665 r600_pipe_shader_ps(ctx
, rctx
->ps_shader
);
667 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
672 void r600_draw_vbo(struct pipe_context
*ctx
, const struct pipe_draw_info
*dinfo
)
674 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
675 struct pipe_draw_info info
= *dinfo
;
676 struct r600_draw rdraw
= {};
677 struct pipe_index_buffer ib
= {};
678 unsigned prim
, mask
, ls_mask
= 0;
680 if ((!info
.count
&& (info
.indexed
|| !info
.count_from_stream_output
)) ||
681 (info
.indexed
&& !rctx
->vbuf_mgr
->index_buffer
.buffer
) ||
682 !r600_conv_pipe_prim(info
.mode
, &prim
)) {
686 if (!rctx
->ps_shader
|| !rctx
->vs_shader
)
689 r600_update_derived_state(rctx
);
691 u_vbuf_draw_begin(rctx
->vbuf_mgr
, &info
);
692 r600_vertex_buffer_update(rctx
);
694 rdraw
.vgt_num_indices
= info
.count
;
695 rdraw
.vgt_num_instances
= info
.instance_count
;
698 /* Initialize the index buffer struct. */
699 pipe_resource_reference(&ib
.buffer
, rctx
->vbuf_mgr
->index_buffer
.buffer
);
700 ib
.index_size
= rctx
->vbuf_mgr
->index_buffer
.index_size
;
701 ib
.offset
= rctx
->vbuf_mgr
->index_buffer
.offset
+ info
.start
* ib
.index_size
;
703 /* Translate or upload, if needed. */
704 r600_translate_index_buffer(rctx
, &ib
, info
.count
);
706 if (u_vbuf_resource(ib
.buffer
)->user_ptr
) {
707 r600_upload_index_buffer(rctx
, &ib
, info
.count
);
710 /* Initialize the r600_draw struct with index buffer info. */
711 if (ib
.index_size
== 4) {
712 rdraw
.vgt_index_type
= VGT_INDEX_32
|
713 (R600_BIG_ENDIAN
? VGT_DMA_SWAP_32_BIT
: 0);
715 rdraw
.vgt_index_type
= VGT_INDEX_16
|
716 (R600_BIG_ENDIAN
? VGT_DMA_SWAP_16_BIT
: 0);
718 rdraw
.indices
= (struct r600_resource
*)ib
.buffer
;
719 rdraw
.indices_bo_offset
= ib
.offset
;
720 rdraw
.vgt_draw_initiator
= V_0287F0_DI_SRC_SEL_DMA
;
722 info
.index_bias
= info
.start
;
723 rdraw
.vgt_draw_initiator
= V_0287F0_DI_SRC_SEL_AUTO_INDEX
;
724 if (info
.count_from_stream_output
) {
725 rdraw
.vgt_draw_initiator
|= S_0287F0_USE_OPAQUE(1);
727 r600_context_draw_opaque_count(&rctx
->ctx
, (struct r600_so_target
*)info
.count_from_stream_output
);
731 rctx
->ctx
.vs_so_stride_in_dw
= rctx
->vs_shader
->so
.stride
;
733 mask
= (1ULL << ((unsigned)rctx
->framebuffer
.nr_cbufs
* 4)) - 1;
735 if (rctx
->vgt
.id
!= R600_PIPE_STATE_VGT
) {
736 rctx
->vgt
.id
= R600_PIPE_STATE_VGT
;
738 r600_pipe_state_add_reg(&rctx
->vgt
, R_008958_VGT_PRIMITIVE_TYPE
, prim
, 0xFFFFFFFF, NULL
, 0);
739 r600_pipe_state_add_reg(&rctx
->vgt
, R_028238_CB_TARGET_MASK
, rctx
->cb_target_mask
& mask
, 0xFFFFFFFF, NULL
, 0);
740 r600_pipe_state_add_reg(&rctx
->vgt
, R_028400_VGT_MAX_VTX_INDX
, ~0, 0xFFFFFFFF, NULL
, 0);
741 r600_pipe_state_add_reg(&rctx
->vgt
, R_028404_VGT_MIN_VTX_INDX
, 0, 0xFFFFFFFF, NULL
, 0);
742 r600_pipe_state_add_reg(&rctx
->vgt
, R_028408_VGT_INDX_OFFSET
, info
.index_bias
, 0xFFFFFFFF, NULL
, 0);
743 r600_pipe_state_add_reg(&rctx
->vgt
, R_02840C_VGT_MULTI_PRIM_IB_RESET_INDX
, info
.restart_index
, 0xFFFFFFFF, NULL
, 0);
744 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A94_VGT_MULTI_PRIM_IB_RESET_EN
, info
.primitive_restart
, 0xFFFFFFFF, NULL
, 0);
745 r600_pipe_state_add_reg(&rctx
->vgt
, R_03CFF0_SQ_VTX_BASE_VTX_LOC
, 0, 0xFFFFFFFF, NULL
, 0);
746 r600_pipe_state_add_reg(&rctx
->vgt
, R_03CFF4_SQ_VTX_START_INST_LOC
, info
.start_instance
, 0xFFFFFFFF, NULL
, 0);
747 r600_pipe_state_add_reg(&rctx
->vgt
, R_028A0C_PA_SC_LINE_STIPPLE
,
749 S_028A0C_AUTO_RESET_CNTL(3), NULL
, 0);
750 r600_pipe_state_add_reg(&rctx
->vgt
, R_028814_PA_SU_SC_MODE_CNTL
,
752 S_028814_PROVOKING_VTX_LAST(1), NULL
, 0);
756 r600_pipe_state_mod_reg(&rctx
->vgt
, prim
);
757 r600_pipe_state_mod_reg(&rctx
->vgt
, rctx
->cb_target_mask
& mask
);
758 r600_pipe_state_mod_reg(&rctx
->vgt
, ~0);
759 r600_pipe_state_mod_reg(&rctx
->vgt
, 0);
760 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.index_bias
);
761 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.restart_index
);
762 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.primitive_restart
);
763 r600_pipe_state_mod_reg(&rctx
->vgt
, 0);
764 r600_pipe_state_mod_reg(&rctx
->vgt
, info
.start_instance
);
766 if (prim
== V_008958_DI_PT_LINELIST
)
768 else if (prim
== V_008958_DI_PT_LINESTRIP
)
770 r600_pipe_state_mod_reg(&rctx
->vgt
, S_028A0C_AUTO_RESET_CNTL(ls_mask
));
772 if (info
.mode
== PIPE_PRIM_QUADS
|| info
.mode
== PIPE_PRIM_QUAD_STRIP
|| info
.mode
== PIPE_PRIM_POLYGON
) {
773 r600_pipe_state_mod_reg(&rctx
->vgt
, S_028814_PROVOKING_VTX_LAST(1));
776 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vgt
);
778 if (rctx
->chip_class
>= EVERGREEN
) {
779 evergreen_context_draw(&rctx
->ctx
, &rdraw
);
781 r600_context_draw(&rctx
->ctx
, &rdraw
);
784 if (rctx
->framebuffer
.zsbuf
)
786 struct pipe_resource
*tex
= rctx
->framebuffer
.zsbuf
->texture
;
787 ((struct r600_resource_texture
*)tex
)->dirty_db
= TRUE
;
790 pipe_resource_reference(&ib
.buffer
, NULL
);
791 u_vbuf_draw_end(rctx
->vbuf_mgr
);
794 void _r600_pipe_state_add_reg(struct r600_context
*ctx
,
795 struct r600_pipe_state
*state
,
796 u32 offset
, u32 value
, u32 mask
,
797 u32 range_id
, u32 block_id
,
798 struct r600_resource
*bo
,
799 enum radeon_bo_usage usage
)
801 struct r600_range
*range
;
802 struct r600_block
*block
;
804 if (bo
) assert(usage
);
806 range
= &ctx
->range
[range_id
];
807 block
= range
->blocks
[block_id
];
808 state
->regs
[state
->nregs
].block
= block
;
809 state
->regs
[state
->nregs
].id
= (offset
- block
->start_offset
) >> 2;
811 state
->regs
[state
->nregs
].value
= value
;
812 state
->regs
[state
->nregs
].mask
= mask
;
813 state
->regs
[state
->nregs
].bo
= bo
;
814 state
->regs
[state
->nregs
].bo_usage
= usage
;
817 assert(state
->nregs
< R600_BLOCK_MAX_REG
);
820 void r600_pipe_state_add_reg_noblock(struct r600_pipe_state
*state
,
821 u32 offset
, u32 value
, u32 mask
,
822 struct r600_resource
*bo
,
823 enum radeon_bo_usage usage
)
825 if (bo
) assert(usage
);
827 state
->regs
[state
->nregs
].id
= offset
;
828 state
->regs
[state
->nregs
].block
= NULL
;
829 state
->regs
[state
->nregs
].value
= value
;
830 state
->regs
[state
->nregs
].mask
= mask
;
831 state
->regs
[state
->nregs
].bo
= bo
;
832 state
->regs
[state
->nregs
].bo_usage
= usage
;
835 assert(state
->nregs
< R600_BLOCK_MAX_REG
);