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_memory.h>
28 #include <util/u_format.h>
29 #include <pipebuffer/pb_buffer.h>
30 #include "pipe/p_shader_tokens.h"
31 #include "r600_pipe.h"
34 /* common state between evergreen and r600 */
35 void r600_bind_blend_state(struct pipe_context
*ctx
, void *state
)
37 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
38 struct r600_pipe_blend
*blend
= (struct r600_pipe_blend
*)state
;
39 struct r600_pipe_state
*rstate
;
43 rstate
= &blend
->rstate
;
44 rctx
->states
[rstate
->id
] = rstate
;
45 rctx
->cb_target_mask
= blend
->cb_target_mask
;
46 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
49 void r600_bind_rs_state(struct pipe_context
*ctx
, void *state
)
51 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
52 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
57 rctx
->flatshade
= rs
->flatshade
;
58 rctx
->sprite_coord_enable
= rs
->sprite_coord_enable
;
59 rctx
->rasterizer
= rs
;
61 rctx
->states
[rs
->rstate
.id
] = &rs
->rstate
;
62 r600_context_pipe_state_set(&rctx
->ctx
, &rs
->rstate
);
64 if (rctx
->family
>= CHIP_CEDAR
) {
65 evergreen_polygon_offset_update(rctx
);
67 r600_polygon_offset_update(rctx
);
71 void r600_delete_rs_state(struct pipe_context
*ctx
, void *state
)
73 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
74 struct r600_pipe_rasterizer
*rs
= (struct r600_pipe_rasterizer
*)state
;
76 if (rctx
->rasterizer
== rs
) {
77 rctx
->rasterizer
= NULL
;
79 if (rctx
->states
[rs
->rstate
.id
] == &rs
->rstate
) {
80 rctx
->states
[rs
->rstate
.id
] = NULL
;
85 void r600_sampler_view_destroy(struct pipe_context
*ctx
,
86 struct pipe_sampler_view
*state
)
88 struct r600_pipe_sampler_view
*resource
= (struct r600_pipe_sampler_view
*)state
;
90 pipe_resource_reference(&state
->texture
, NULL
);
94 void r600_bind_state(struct pipe_context
*ctx
, void *state
)
96 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
97 struct r600_pipe_state
*rstate
= (struct r600_pipe_state
*)state
;
101 rctx
->states
[rstate
->id
] = rstate
;
102 r600_context_pipe_state_set(&rctx
->ctx
, rstate
);
105 void r600_delete_state(struct pipe_context
*ctx
, void *state
)
107 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
108 struct r600_pipe_state
*rstate
= (struct r600_pipe_state
*)state
;
110 if (rctx
->states
[rstate
->id
] == rstate
) {
111 rctx
->states
[rstate
->id
] = NULL
;
113 for (int i
= 0; i
< rstate
->nregs
; i
++) {
114 r600_bo_reference(rctx
->radeon
, &rstate
->regs
[i
].bo
, NULL
);
119 void r600_bind_vertex_elements(struct pipe_context
*ctx
, void *state
)
121 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
122 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
124 rctx
->vertex_elements
= v
;
126 u_vbuf_mgr_bind_vertex_elements(rctx
->vbuf_mgr
, state
,
129 rctx
->states
[v
->rstate
.id
] = &v
->rstate
;
130 r600_context_pipe_state_set(&rctx
->ctx
, &v
->rstate
);
134 void r600_delete_vertex_element(struct pipe_context
*ctx
, void *state
)
136 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
137 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
139 if (rctx
->states
[v
->rstate
.id
] == &v
->rstate
) {
140 rctx
->states
[v
->rstate
.id
] = NULL
;
142 if (rctx
->vertex_elements
== state
)
143 rctx
->vertex_elements
= NULL
;
145 r600_bo_reference(rctx
->radeon
, &v
->fetch_shader
, NULL
);
146 u_vbuf_mgr_destroy_vertex_elements(rctx
->vbuf_mgr
, v
->vmgr_elements
);
151 void r600_set_index_buffer(struct pipe_context
*ctx
,
152 const struct pipe_index_buffer
*ib
)
154 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
157 pipe_resource_reference(&rctx
->index_buffer
.buffer
, ib
->buffer
);
158 memcpy(&rctx
->index_buffer
, ib
, sizeof(rctx
->index_buffer
));
160 pipe_resource_reference(&rctx
->index_buffer
.buffer
, NULL
);
161 memset(&rctx
->index_buffer
, 0, sizeof(rctx
->index_buffer
));
164 /* TODO make this more like a state */
167 void r600_set_vertex_buffers(struct pipe_context
*ctx
, unsigned count
,
168 const struct pipe_vertex_buffer
*buffers
)
170 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
174 for (i
= 0; i
< count
; i
++) {
175 if (!buffers
[i
].buffer
) {
176 if (rctx
->family
>= CHIP_CEDAR
) {
177 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
179 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
183 for (; i
< rctx
->vbuf_mgr
->nr_real_vertex_buffers
; i
++) {
184 if (rctx
->family
>= CHIP_CEDAR
) {
185 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
187 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
191 u_vbuf_mgr_set_vertex_buffers(rctx
->vbuf_mgr
, count
, buffers
);
194 void *r600_create_vertex_elements(struct pipe_context
*ctx
,
196 const struct pipe_vertex_element
*elements
)
198 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
199 struct r600_vertex_element
*v
= CALLOC_STRUCT(r600_vertex_element
);
207 u_vbuf_mgr_create_vertex_elements(rctx
->vbuf_mgr
, count
,
208 elements
, v
->elements
);
210 if (r600_vertex_elements_build_fetch_shader(rctx
, v
)) {
218 void *r600_create_shader_state(struct pipe_context
*ctx
,
219 const struct pipe_shader_state
*state
)
221 struct r600_pipe_shader
*shader
= CALLOC_STRUCT(r600_pipe_shader
);
224 r
= r600_pipe_shader_create(ctx
, shader
, state
->tokens
);
231 void r600_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
233 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
235 /* TODO delete old shader */
236 rctx
->ps_shader
= (struct r600_pipe_shader
*)state
;
238 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
242 void r600_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
244 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
246 /* TODO delete old shader */
247 rctx
->vs_shader
= (struct r600_pipe_shader
*)state
;
249 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_shader
->rstate
);
253 void r600_delete_ps_shader(struct pipe_context
*ctx
, void *state
)
255 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
256 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
258 if (rctx
->ps_shader
== shader
) {
259 rctx
->ps_shader
= NULL
;
262 r600_pipe_shader_destroy(ctx
, shader
);
266 void r600_delete_vs_shader(struct pipe_context
*ctx
, void *state
)
268 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
269 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
271 if (rctx
->vs_shader
== shader
) {
272 rctx
->vs_shader
= NULL
;
275 r600_pipe_shader_destroy(ctx
, shader
);
279 /* FIXME optimize away spi update when it's not needed */
280 void r600_spi_update(struct r600_pipe_context
*rctx
)
282 struct r600_pipe_shader
*shader
= rctx
->ps_shader
;
283 struct r600_pipe_state rstate
;
284 struct r600_shader
*rshader
= &shader
->shader
;
288 for (i
= 0; i
< rshader
->ninput
; i
++) {
289 tmp
= S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx
->vs_shader
->shader
, rshader
, i
));
291 if (rshader
->input
[i
].name
== TGSI_SEMANTIC_COLOR
||
292 rshader
->input
[i
].name
== TGSI_SEMANTIC_BCOLOR
||
293 rshader
->input
[i
].name
== TGSI_SEMANTIC_POSITION
) {
294 tmp
|= S_028644_FLAT_SHADE(rctx
->flatshade
);
297 if (rshader
->input
[i
].name
== TGSI_SEMANTIC_GENERIC
&&
298 rctx
->sprite_coord_enable
& (1 << rshader
->input
[i
].sid
)) {
299 tmp
|= S_028644_PT_SPRITE_TEX(1);
302 if (rctx
->family
< CHIP_CEDAR
) {
303 if (rshader
->input
[i
].centroid
)
304 tmp
|= S_028644_SEL_CENTROID(1);
306 if (rshader
->input
[i
].interpolate
== TGSI_INTERPOLATE_LINEAR
)
307 tmp
|= S_028644_SEL_LINEAR(1);
310 r600_pipe_state_add_reg(&rstate
, R_028644_SPI_PS_INPUT_CNTL_0
+ i
* 4, tmp
, 0xFFFFFFFF, NULL
);
312 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
315 void r600_set_constant_buffer(struct pipe_context
*ctx
, uint shader
, uint index
,
316 struct pipe_resource
*buffer
)
318 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
319 struct r600_resource_buffer
*rbuffer
= r600_buffer(buffer
);
320 struct r600_pipe_state
*rstate
;
323 /* Note that the state tracker can unbind constant buffers by
326 if (buffer
== NULL
) {
330 r600_upload_const_buffer(rctx
, &rbuffer
, &offset
);
331 offset
+= r600_bo_offset(rbuffer
->r
.bo
);
334 case PIPE_SHADER_VERTEX
:
335 rctx
->vs_const_buffer
.nregs
= 0;
336 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
337 R_028180_ALU_CONST_BUFFER_SIZE_VS_0
,
338 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
340 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
341 R_028980_ALU_CONST_CACHE_VS_0
,
342 offset
>> 8, 0xFFFFFFFF, rbuffer
->r
.bo
);
343 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_const_buffer
);
345 rstate
= &rctx
->vs_const_buffer_resource
[index
];
346 rstate
->id
= R600_PIPE_STATE_RESOURCE
;
348 if (rctx
->family
>= CHIP_CEDAR
) {
349 evergreen_pipe_set_buffer_resource(rctx
, rstate
, &rbuffer
->r
, offset
, 16);
350 evergreen_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
352 r600_pipe_set_buffer_resource(rctx
, rstate
, &rbuffer
->r
, offset
, 16);
353 r600_context_pipe_state_set_vs_resource(&rctx
->ctx
, rstate
, index
);
356 case PIPE_SHADER_FRAGMENT
:
357 rctx
->ps_const_buffer
.nregs
= 0;
358 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
359 R_028140_ALU_CONST_BUFFER_SIZE_PS_0
,
360 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
362 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
363 R_028940_ALU_CONST_CACHE_PS_0
,
364 offset
>> 8, 0xFFFFFFFF, rbuffer
->r
.bo
);
365 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_const_buffer
);
367 rstate
= &rctx
->ps_const_buffer_resource
[index
];
368 rstate
->id
= R600_PIPE_STATE_RESOURCE
;
370 if (rctx
->family
>= CHIP_CEDAR
) {
371 evergreen_pipe_set_buffer_resource(rctx
, rstate
, &rbuffer
->r
, offset
, 16);
372 evergreen_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
374 r600_pipe_set_buffer_resource(rctx
, rstate
, &rbuffer
->r
, offset
, 16);
375 r600_context_pipe_state_set_ps_resource(&rctx
->ctx
, rstate
, index
);
379 R600_ERR("unsupported %d\n", shader
);
383 if (buffer
!= &rbuffer
->r
.b
.b
.b
)
384 pipe_resource_reference((struct pipe_resource
**)&rbuffer
, NULL
);
387 static void r600_vertex_buffer_update(struct r600_pipe_context
*rctx
)
389 struct r600_pipe_state
*rstate
;
390 struct r600_resource
*rbuffer
;
391 struct pipe_vertex_buffer
*vertex_buffer
;
392 unsigned i
, count
, offset
;
394 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
395 /* one resource per vertex elements */
396 count
= rctx
->vertex_elements
->count
;
398 /* bind vertex buffer once */
399 count
= rctx
->vbuf_mgr
->nr_real_vertex_buffers
;
402 for (i
= 0 ; i
< count
; i
++) {
403 rstate
= &rctx
->fs_resource
[i
];
404 rstate
->id
= R600_PIPE_STATE_RESOURCE
;
407 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
408 /* one resource per vertex elements */
409 unsigned vbuffer_index
;
410 vbuffer_index
= rctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
411 vertex_buffer
= &rctx
->vbuf_mgr
->vertex_buffer
[vbuffer_index
];
412 rbuffer
= (struct r600_resource
*)rctx
->vbuf_mgr
->real_vertex_buffer
[vbuffer_index
];
413 offset
= rctx
->vertex_elements
->vbuffer_offset
[i
];
415 /* bind vertex buffer once */
416 vertex_buffer
= &rctx
->vbuf_mgr
->vertex_buffer
[i
];
417 rbuffer
= (struct r600_resource
*)rctx
->vbuf_mgr
->real_vertex_buffer
[i
];
420 if (vertex_buffer
== NULL
|| rbuffer
== NULL
)
422 offset
+= vertex_buffer
->buffer_offset
+ r600_bo_offset(rbuffer
->bo
);
424 if (rctx
->family
>= CHIP_CEDAR
) {
425 evergreen_pipe_set_buffer_resource(rctx
, rstate
, rbuffer
, offset
, vertex_buffer
->stride
);
426 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
428 r600_pipe_set_buffer_resource(rctx
, rstate
, rbuffer
, offset
, vertex_buffer
->stride
);
429 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, rstate
, i
);
434 void r600_draw_vbo(struct pipe_context
*ctx
, const struct pipe_draw_info
*info
)
436 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
437 struct r600_resource
*rbuffer
;
438 u32 vgt_dma_index_type
, vgt_draw_initiator
, mask
;
439 struct r600_draw rdraw
;
440 struct r600_pipe_state vgt
;
441 struct r600_drawl draw
= {};
444 r600_flush_depth_textures(rctx
);
445 u_vbuf_mgr_draw_begin(rctx
->vbuf_mgr
, info
, NULL
, NULL
);
446 r600_vertex_buffer_update(rctx
);
450 if (info
->indexed
&& rctx
->index_buffer
.buffer
) {
451 draw
.info
.start
+= rctx
->index_buffer
.offset
/ rctx
->index_buffer
.index_size
;
452 pipe_resource_reference(&draw
.index_buffer
, rctx
->index_buffer
.buffer
);
454 r600_translate_index_buffer(rctx
, &draw
.index_buffer
,
455 &rctx
->index_buffer
.index_size
,
459 draw
.index_size
= rctx
->index_buffer
.index_size
;
460 draw
.index_buffer_offset
= draw
.info
.start
* draw
.index_size
;
463 if (u_vbuf_resource(draw
.index_buffer
)->user_ptr
) {
464 r600_upload_index_buffer(rctx
, &draw
);
467 draw
.info
.index_bias
= info
->start
;
470 switch (draw
.index_size
) {
472 vgt_draw_initiator
= 0;
473 vgt_dma_index_type
= 0;
476 vgt_draw_initiator
= 0;
477 vgt_dma_index_type
= 1;
480 vgt_draw_initiator
= 2;
481 vgt_dma_index_type
= 0;
484 R600_ERR("unsupported index size %d\n", draw
.index_size
);
487 if (r600_conv_pipe_prim(draw
.info
.mode
, &prim
))
489 if (unlikely(rctx
->ps_shader
== NULL
)) {
490 R600_ERR("missing vertex shader\n");
493 if (unlikely(rctx
->vs_shader
== NULL
)) {
494 R600_ERR("missing vertex shader\n");
497 /* there should be enough input */
498 if (rctx
->vertex_elements
->count
< rctx
->vs_shader
->shader
.bc
.nresource
) {
499 R600_ERR("%d resources provided, expecting %d\n",
500 rctx
->vertex_elements
->count
, rctx
->vs_shader
->shader
.bc
.nresource
);
504 r600_spi_update(rctx
);
507 for (int i
= 0; i
< rctx
->framebuffer
.nr_cbufs
; i
++) {
508 mask
|= (0xF << (i
* 4));
511 vgt
.id
= R600_PIPE_STATE_VGT
;
513 r600_pipe_state_add_reg(&vgt
, R_008958_VGT_PRIMITIVE_TYPE
, prim
, 0xFFFFFFFF, NULL
);
514 r600_pipe_state_add_reg(&vgt
, R_028408_VGT_INDX_OFFSET
, draw
.info
.index_bias
, 0xFFFFFFFF, NULL
);
515 r600_pipe_state_add_reg(&vgt
, R_028400_VGT_MAX_VTX_INDX
, draw
.info
.max_index
, 0xFFFFFFFF, NULL
);
516 r600_pipe_state_add_reg(&vgt
, R_028404_VGT_MIN_VTX_INDX
, draw
.info
.min_index
, 0xFFFFFFFF, NULL
);
517 r600_pipe_state_add_reg(&vgt
, R_028238_CB_TARGET_MASK
, rctx
->cb_target_mask
& mask
, 0xFFFFFFFF, NULL
);
518 r600_pipe_state_add_reg(&vgt
, R_03CFF0_SQ_VTX_BASE_VTX_LOC
, 0, 0xFFFFFFFF, NULL
);
519 r600_pipe_state_add_reg(&vgt
, R_03CFF4_SQ_VTX_START_INST_LOC
, 0, 0xFFFFFFFF, NULL
);
520 r600_context_pipe_state_set(&rctx
->ctx
, &vgt
);
522 rdraw
.vgt_num_indices
= draw
.info
.count
;
523 rdraw
.vgt_num_instances
= draw
.info
.instance_count
;
524 rdraw
.vgt_index_type
= vgt_dma_index_type
;
525 rdraw
.vgt_draw_initiator
= vgt_draw_initiator
;
526 rdraw
.indices
= NULL
;
527 if (draw
.index_buffer
) {
528 rbuffer
= (struct r600_resource
*)draw
.index_buffer
;
529 rdraw
.indices
= rbuffer
->bo
;
530 rdraw
.indices_bo_offset
= draw
.index_buffer_offset
;
533 if (rctx
->family
>= CHIP_CEDAR
) {
534 evergreen_context_draw(&rctx
->ctx
, &rdraw
);
536 r600_context_draw(&rctx
->ctx
, &rdraw
);
539 if (rctx
->framebuffer
.zsbuf
)
541 struct pipe_resource
*tex
= rctx
->framebuffer
.zsbuf
->texture
;
542 ((struct r600_resource_texture
*)tex
)->dirty_db
= TRUE
;
545 pipe_resource_reference(&draw
.index_buffer
, NULL
);
547 u_vbuf_mgr_draw_end(rctx
->vbuf_mgr
);