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 rctx
->states
[v
->rstate
.id
] = &v
->rstate
;
127 r600_context_pipe_state_set(&rctx
->ctx
, &v
->rstate
);
131 void r600_delete_vertex_element(struct pipe_context
*ctx
, void *state
)
133 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
134 struct r600_vertex_element
*v
= (struct r600_vertex_element
*)state
;
136 if (rctx
->states
[v
->rstate
.id
] == &v
->rstate
) {
137 rctx
->states
[v
->rstate
.id
] = NULL
;
139 if (rctx
->vertex_elements
== state
)
140 rctx
->vertex_elements
= NULL
;
142 r600_bo_reference(rctx
->radeon
, &v
->fetch_shader
, NULL
);
147 void r600_set_index_buffer(struct pipe_context
*ctx
,
148 const struct pipe_index_buffer
*ib
)
150 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
153 pipe_resource_reference(&rctx
->index_buffer
.buffer
, ib
->buffer
);
154 memcpy(&rctx
->index_buffer
, ib
, sizeof(rctx
->index_buffer
));
156 pipe_resource_reference(&rctx
->index_buffer
.buffer
, NULL
);
157 memset(&rctx
->index_buffer
, 0, sizeof(rctx
->index_buffer
));
160 /* TODO make this more like a state */
163 void r600_set_vertex_buffers(struct pipe_context
*ctx
, unsigned count
,
164 const struct pipe_vertex_buffer
*buffers
)
166 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
167 struct pipe_vertex_buffer
*vbo
;
168 unsigned max_index
= ~0;
171 for (i
= 0; i
< count
; i
++) {
172 vbo
= (struct pipe_vertex_buffer
*)&buffers
[i
];
174 pipe_resource_reference(&rctx
->vertex_buffer
[i
].buffer
, vbo
->buffer
);
175 pipe_resource_reference(&rctx
->real_vertex_buffer
[i
], NULL
);
179 if (rctx
->family
>= CHIP_CEDAR
) {
180 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
182 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
187 if (r600_is_user_buffer(vbo
->buffer
)) {
188 rctx
->any_user_vbs
= TRUE
;
192 pipe_resource_reference(&rctx
->real_vertex_buffer
[i
], vbo
->buffer
);
194 /* The stride of zero means we will be fetching only the first
195 * vertex, so don't care about max_index. */
200 /* Update the maximum index. */
202 unsigned vbo_max_index
=
203 (vbo
->buffer
->width0
- vbo
->buffer_offset
) / vbo
->stride
;
204 max_index
= MIN2(max_index
, vbo_max_index
);
208 for (; i
< rctx
->nvertex_buffer
; i
++) {
209 pipe_resource_reference(&rctx
->vertex_buffer
[i
].buffer
, NULL
);
210 pipe_resource_reference(&rctx
->real_vertex_buffer
[i
], NULL
);
213 if (rctx
->family
>= CHIP_CEDAR
) {
214 evergreen_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
216 r600_context_pipe_state_set_fs_resource(&rctx
->ctx
, NULL
, i
);
220 memcpy(rctx
->vertex_buffer
, buffers
, sizeof(struct pipe_vertex_buffer
) * count
);
222 rctx
->nvertex_buffer
= count
;
223 rctx
->vb_max_index
= max_index
;
227 #define FORMAT_REPLACE(what, withwhat) \
228 case PIPE_FORMAT_##what: *format = PIPE_FORMAT_##withwhat; break
230 void *r600_create_vertex_elements(struct pipe_context
*ctx
,
232 const struct pipe_vertex_element
*elements
)
234 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
235 struct r600_vertex_element
*v
= CALLOC_STRUCT(r600_vertex_element
);
236 enum pipe_format
*format
;
244 memcpy(v
->elements
, elements
, count
* sizeof(struct pipe_vertex_element
));
246 for (i
= 0; i
< count
; i
++) {
247 v
->hw_format
[i
] = v
->elements
[i
].src_format
;
248 format
= &v
->hw_format
[i
];
251 FORMAT_REPLACE(R64_FLOAT
, R32_FLOAT
);
252 FORMAT_REPLACE(R64G64_FLOAT
, R32G32_FLOAT
);
253 FORMAT_REPLACE(R64G64B64_FLOAT
, R32G32B32_FLOAT
);
254 FORMAT_REPLACE(R64G64B64A64_FLOAT
, R32G32B32A32_FLOAT
);
257 v
->incompatible_layout
=
258 v
->incompatible_layout
||
259 v
->elements
[i
].src_format
!= v
->hw_format
[i
];
261 v
->hw_format_size
[i
] = align(util_format_get_blocksize(v
->hw_format
[i
]), 4);
264 if (r600_vertex_elements_build_fetch_shader(rctx
, v
)) {
272 void *r600_create_shader_state(struct pipe_context
*ctx
,
273 const struct pipe_shader_state
*state
)
275 struct r600_pipe_shader
*shader
= CALLOC_STRUCT(r600_pipe_shader
);
278 r
= r600_pipe_shader_create(ctx
, shader
, state
->tokens
);
285 void r600_bind_ps_shader(struct pipe_context
*ctx
, void *state
)
287 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
289 /* TODO delete old shader */
290 rctx
->ps_shader
= (struct r600_pipe_shader
*)state
;
292 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_shader
->rstate
);
296 void r600_bind_vs_shader(struct pipe_context
*ctx
, void *state
)
298 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
300 /* TODO delete old shader */
301 rctx
->vs_shader
= (struct r600_pipe_shader
*)state
;
303 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_shader
->rstate
);
307 void r600_delete_ps_shader(struct pipe_context
*ctx
, void *state
)
309 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
310 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
312 if (rctx
->ps_shader
== shader
) {
313 rctx
->ps_shader
= NULL
;
316 r600_pipe_shader_destroy(ctx
, shader
);
320 void r600_delete_vs_shader(struct pipe_context
*ctx
, void *state
)
322 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
323 struct r600_pipe_shader
*shader
= (struct r600_pipe_shader
*)state
;
325 if (rctx
->vs_shader
== shader
) {
326 rctx
->vs_shader
= NULL
;
329 r600_pipe_shader_destroy(ctx
, shader
);
333 /* FIXME optimize away spi update when it's not needed */
334 void r600_spi_update(struct r600_pipe_context
*rctx
)
336 struct r600_pipe_shader
*shader
= rctx
->ps_shader
;
337 struct r600_pipe_state rstate
;
338 struct r600_shader
*rshader
= &shader
->shader
;
342 for (i
= 0; i
< rshader
->ninput
; i
++) {
343 tmp
= S_028644_SEMANTIC(r600_find_vs_semantic_index(&rctx
->vs_shader
->shader
, rshader
, i
));
345 if (rshader
->input
[i
].name
== TGSI_SEMANTIC_COLOR
||
346 rshader
->input
[i
].name
== TGSI_SEMANTIC_BCOLOR
||
347 rshader
->input
[i
].name
== TGSI_SEMANTIC_POSITION
) {
348 tmp
|= S_028644_FLAT_SHADE(rctx
->flatshade
);
351 if (rshader
->input
[i
].name
== TGSI_SEMANTIC_GENERIC
&&
352 rctx
->sprite_coord_enable
& (1 << rshader
->input
[i
].sid
)) {
353 tmp
|= S_028644_PT_SPRITE_TEX(1);
356 if (rctx
->family
< CHIP_CEDAR
) {
357 if (rshader
->input
[i
].centroid
)
358 tmp
|= S_028644_SEL_CENTROID(1);
360 if (rshader
->input
[i
].interpolate
== TGSI_INTERPOLATE_LINEAR
)
361 tmp
|= S_028644_SEL_LINEAR(1);
364 r600_pipe_state_add_reg(&rstate
, R_028644_SPI_PS_INPUT_CNTL_0
+ i
* 4, tmp
, 0xFFFFFFFF, NULL
);
366 r600_context_pipe_state_set(&rctx
->ctx
, &rstate
);
369 void r600_set_constant_buffer(struct pipe_context
*ctx
, uint shader
, uint index
,
370 struct pipe_resource
*buffer
)
372 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
373 struct r600_resource_buffer
*rbuffer
= r600_buffer(buffer
);
376 /* Note that the state tracker can unbind constant buffers by
379 if (buffer
== NULL
) {
383 r600_upload_const_buffer(rctx
, &rbuffer
, &offset
);
386 case PIPE_SHADER_VERTEX
:
387 rctx
->vs_const_buffer
.nregs
= 0;
388 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
389 R_028180_ALU_CONST_BUFFER_SIZE_VS_0
,
390 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
392 r600_pipe_state_add_reg(&rctx
->vs_const_buffer
,
393 R_028980_ALU_CONST_CACHE_VS_0
,
394 (r600_bo_offset(rbuffer
->r
.bo
) + offset
) >> 8, 0xFFFFFFFF, rbuffer
->r
.bo
);
395 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->vs_const_buffer
);
397 case PIPE_SHADER_FRAGMENT
:
398 rctx
->ps_const_buffer
.nregs
= 0;
399 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
400 R_028140_ALU_CONST_BUFFER_SIZE_PS_0
,
401 ALIGN_DIVUP(buffer
->width0
>> 4, 16),
403 r600_pipe_state_add_reg(&rctx
->ps_const_buffer
,
404 R_028940_ALU_CONST_CACHE_PS_0
,
405 (r600_bo_offset(rbuffer
->r
.bo
) + offset
) >> 8, 0xFFFFFFFF, rbuffer
->r
.bo
);
406 r600_context_pipe_state_set(&rctx
->ctx
, &rctx
->ps_const_buffer
);
409 R600_ERR("unsupported %d\n", shader
);
413 if (!rbuffer
->user_buffer
)
414 pipe_resource_reference((struct pipe_resource
**)&rbuffer
, NULL
);
417 static void r600_vertex_buffer_update(struct r600_pipe_context
*rctx
)
419 struct r600_pipe_state
*rstate
;
420 struct r600_resource
*rbuffer
;
421 struct pipe_vertex_buffer
*vertex_buffer
;
424 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
425 /* one resource per vertex elements */
426 rctx
->nvs_resource
= rctx
->vertex_elements
->count
;
428 /* bind vertex buffer once */
429 rctx
->nvs_resource
= rctx
->nvertex_buffer
;
432 for (i
= 0 ; i
< rctx
->nvs_resource
; i
++) {
433 rstate
= &rctx
->vs_resource
[i
];
434 rstate
->id
= R600_PIPE_STATE_RESOURCE
;
437 if (rctx
->vertex_elements
->vbuffer_need_offset
) {
438 /* one resource per vertex elements */
439 unsigned vbuffer_index
;
440 vbuffer_index
= rctx
->vertex_elements
->elements
[i
].vertex_buffer_index
;
441 vertex_buffer
= &rctx
->vertex_buffer
[vbuffer_index
];
442 rbuffer
= (struct r600_resource
*)rctx
->real_vertex_buffer
[vbuffer_index
];
443 offset
= rctx
->vertex_elements
->vbuffer_offset
[i
];
445 /* bind vertex buffer once */
446 vertex_buffer
= &rctx
->vertex_buffer
[i
];
447 rbuffer
= (struct r600_resource
*)rctx
->real_vertex_buffer
[i
];
450 if (vertex_buffer
== NULL
|| rbuffer
== NULL
)
452 offset
+= vertex_buffer
->buffer_offset
+ r600_bo_offset(rbuffer
->bo
);
454 if (rctx
->family
>= CHIP_CEDAR
) {
455 evergreen_pipe_add_vertex_attrib(rctx
, rstate
, i
,
457 vertex_buffer
->stride
);
459 r600_pipe_add_vertex_attrib(rctx
, rstate
, i
,
461 vertex_buffer
->stride
);
466 void r600_draw_vbo(struct pipe_context
*ctx
, const struct pipe_draw_info
*info
)
468 struct r600_pipe_context
*rctx
= (struct r600_pipe_context
*)ctx
;
469 struct r600_resource
*rbuffer
;
470 u32 vgt_dma_index_type
, vgt_draw_initiator
, mask
;
471 struct r600_draw rdraw
;
472 struct r600_pipe_state vgt
;
473 struct r600_drawl draw
= {};
476 if (rctx
->vertex_elements
->incompatible_layout
) {
477 r600_begin_vertex_translate(rctx
);
480 if (rctx
->any_user_vbs
) {
481 r600_upload_user_buffers(rctx
, info
->min_index
, info
->max_index
);
484 r600_vertex_buffer_update(rctx
);
488 if (info
->indexed
&& rctx
->index_buffer
.buffer
) {
489 draw
.info
.start
+= rctx
->index_buffer
.offset
/ rctx
->index_buffer
.index_size
;
491 r600_translate_index_buffer(rctx
, &rctx
->index_buffer
.buffer
,
492 &rctx
->index_buffer
.index_size
,
496 draw
.index_size
= rctx
->index_buffer
.index_size
;
497 pipe_resource_reference(&draw
.index_buffer
, rctx
->index_buffer
.buffer
);
498 draw
.index_buffer_offset
= draw
.info
.start
* draw
.index_size
;
501 if (r600_is_user_buffer(draw
.index_buffer
)) {
502 r600_upload_index_buffer(rctx
, &draw
);
505 draw
.info
.index_bias
= info
->start
;
508 switch (draw
.index_size
) {
510 vgt_draw_initiator
= 0;
511 vgt_dma_index_type
= 0;
514 vgt_draw_initiator
= 0;
515 vgt_dma_index_type
= 1;
518 vgt_draw_initiator
= 2;
519 vgt_dma_index_type
= 0;
522 R600_ERR("unsupported index size %d\n", draw
.index_size
);
525 if (r600_conv_pipe_prim(draw
.info
.mode
, &prim
))
527 if (unlikely(rctx
->ps_shader
== NULL
)) {
528 R600_ERR("missing vertex shader\n");
531 if (unlikely(rctx
->vs_shader
== NULL
)) {
532 R600_ERR("missing vertex shader\n");
535 /* there should be enough input */
536 if (rctx
->vertex_elements
->count
< rctx
->vs_shader
->shader
.bc
.nresource
) {
537 R600_ERR("%d resources provided, expecting %d\n",
538 rctx
->vertex_elements
->count
, rctx
->vs_shader
->shader
.bc
.nresource
);
542 r600_spi_update(rctx
);
545 for (int i
= 0; i
< rctx
->framebuffer
.nr_cbufs
; i
++) {
546 mask
|= (0xF << (i
* 4));
549 vgt
.id
= R600_PIPE_STATE_VGT
;
551 r600_pipe_state_add_reg(&vgt
, R_008958_VGT_PRIMITIVE_TYPE
, prim
, 0xFFFFFFFF, NULL
);
552 r600_pipe_state_add_reg(&vgt
, R_028408_VGT_INDX_OFFSET
, draw
.info
.index_bias
, 0xFFFFFFFF, NULL
);
553 r600_pipe_state_add_reg(&vgt
, R_028400_VGT_MAX_VTX_INDX
, draw
.info
.max_index
, 0xFFFFFFFF, NULL
);
554 r600_pipe_state_add_reg(&vgt
, R_028404_VGT_MIN_VTX_INDX
, draw
.info
.min_index
, 0xFFFFFFFF, NULL
);
555 r600_pipe_state_add_reg(&vgt
, R_028238_CB_TARGET_MASK
, rctx
->cb_target_mask
& mask
, 0xFFFFFFFF, NULL
);
556 r600_pipe_state_add_reg(&vgt
, R_03CFF0_SQ_VTX_BASE_VTX_LOC
, 0, 0xFFFFFFFF, NULL
);
557 r600_pipe_state_add_reg(&vgt
, R_03CFF4_SQ_VTX_START_INST_LOC
, 0, 0xFFFFFFFF, NULL
);
558 r600_context_pipe_state_set(&rctx
->ctx
, &vgt
);
560 rdraw
.vgt_num_indices
= draw
.info
.count
;
561 rdraw
.vgt_num_instances
= 1;
562 rdraw
.vgt_index_type
= vgt_dma_index_type
;
563 rdraw
.vgt_draw_initiator
= vgt_draw_initiator
;
564 rdraw
.indices
= NULL
;
565 if (draw
.index_buffer
) {
566 rbuffer
= (struct r600_resource
*)draw
.index_buffer
;
567 rdraw
.indices
= rbuffer
->bo
;
568 rdraw
.indices_bo_offset
= draw
.index_buffer_offset
;
571 if (rctx
->family
>= CHIP_CEDAR
) {
572 evergreen_context_draw(&rctx
->ctx
, &rdraw
);
574 r600_context_draw(&rctx
->ctx
, &rdraw
);
577 pipe_resource_reference(&draw
.index_buffer
, NULL
);
579 /* delete previous translated vertex elements */
580 if (rctx
->tran
.new_velems
) {
581 r600_end_vertex_translate(rctx
);