2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 /* r300_render: Vertex and index buffer primitive emission. Contains both
24 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
26 #include "draw/draw_context.h"
27 #include "draw/draw_vbuf.h"
29 #include "pipe/p_inlines.h"
31 #include "util/u_memory.h"
32 #include "util/u_prim.h"
35 #include "r300_context.h"
36 #include "r300_emit.h"
38 #include "r300_render.h"
39 #include "r300_state_derived.h"
42 /* r300_render: Vertex and index buffer primitive emission. */
43 #define R300_MAX_VBO_SIZE (1024 * 1024)
45 uint32_t r300_translate_primitive(unsigned prim
)
48 case PIPE_PRIM_POINTS
:
49 return R300_VAP_VF_CNTL__PRIM_POINTS
;
51 return R300_VAP_VF_CNTL__PRIM_LINES
;
52 case PIPE_PRIM_LINE_LOOP
:
53 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP
;
54 case PIPE_PRIM_LINE_STRIP
:
55 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP
;
56 case PIPE_PRIM_TRIANGLES
:
57 return R300_VAP_VF_CNTL__PRIM_TRIANGLES
;
58 case PIPE_PRIM_TRIANGLE_STRIP
:
59 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP
;
60 case PIPE_PRIM_TRIANGLE_FAN
:
61 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN
;
63 return R300_VAP_VF_CNTL__PRIM_QUADS
;
64 case PIPE_PRIM_QUAD_STRIP
:
65 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP
;
66 case PIPE_PRIM_POLYGON
:
67 return R300_VAP_VF_CNTL__PRIM_POLYGON
;
73 static boolean
r300_nothing_to_draw(struct r300_context
*r300
)
75 return r300
->rs_state
->rs
.scissor
&&
76 r300
->scissor_state
->scissor
.empty_area
;
79 static void r300_emit_draw_arrays(struct r300_context
*r300
,
86 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, count
);
87 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
88 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
89 r300_translate_primitive(mode
));
93 static void r300_emit_draw_elements(struct r300_context
*r300
,
94 struct pipe_buffer
* indexBuffer
,
102 uint32_t count_dwords
;
103 uint32_t offset_dwords
= indexSize
* start
/ sizeof(uint32_t);
106 /* XXX most of these are stupid */
107 assert(indexSize
== 4 || indexSize
== 2);
108 assert((start
* indexSize
) % 4 == 0);
109 assert(offset_dwords
== 0);
112 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, maxIndex
);
113 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, 0);
114 if (indexSize
== 4) {
115 count_dwords
= count
+ start
;
116 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
117 R300_VAP_VF_CNTL__INDEX_SIZE_32bit
|
118 r300_translate_primitive(mode
));
120 count_dwords
= (count
+ start
+ 1) / 2;
121 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
122 r300_translate_primitive(mode
));
125 /* INDX_BUFFER is a truly special packet3.
126 * Unlike most other packet3, where the offset is after the count,
127 * the order is reversed, so the relocation ends up carrying the
128 * size of the indexbuf instead of the offset.
132 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER
, 2);
133 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR
| (R300_VAP_PORT_IDX0
>> 2) |
134 (0 << R300_INDX_BUFFER_SKIP_SHIFT
));
135 OUT_CS(offset_dwords
);
136 OUT_CS_RELOC(indexBuffer
, count_dwords
,
137 RADEON_GEM_DOMAIN_GTT
, 0, 0);
143 static boolean
r300_setup_vertex_buffers(struct r300_context
*r300
)
145 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
146 struct pipe_vertex_element
*velem
= r300
->vertex_element
;
149 for (int i
= 0; i
< r300
->vertex_element_count
; i
++) {
150 if (!r300
->winsys
->add_buffer(r300
->winsys
,
151 vbuf
[velem
[i
].vertex_buffer_index
].buffer
,
152 RADEON_GEM_DOMAIN_GTT
, 0)) {
153 r300
->context
.flush(&r300
->context
, 0, NULL
);
158 if (!r300
->winsys
->validate(r300
->winsys
)) {
159 r300
->context
.flush(&r300
->context
, 0, NULL
);
160 return r300
->winsys
->validate(r300
->winsys
);
166 /* This is the fast-path drawing & emission for HW TCL. */
167 boolean
r300_draw_range_elements(struct pipe_context
* pipe
,
168 struct pipe_buffer
* indexBuffer
,
176 struct r300_context
* r300
= r300_context(pipe
);
178 if (!u_trim_pipe_prim(mode
, &count
)) {
187 if (r300_nothing_to_draw(r300
)) {
191 r300_update_derived_state(r300
);
193 if (!r300_setup_vertex_buffers(r300
)) {
197 setup_index_buffer(r300
, indexBuffer
, indexSize
);
199 r300_emit_dirty_state(r300
);
201 r300_emit_aos(r300
, 0);
203 r300_emit_draw_elements(r300
, indexBuffer
, indexSize
, minIndex
, maxIndex
,
209 /* Simple helpers for context setup. Should probably be moved to util. */
210 boolean
r300_draw_elements(struct pipe_context
* pipe
,
211 struct pipe_buffer
* indexBuffer
,
212 unsigned indexSize
, unsigned mode
,
213 unsigned start
, unsigned count
)
215 return pipe
->draw_range_elements(pipe
, indexBuffer
, indexSize
, 0, ~0,
219 boolean
r300_draw_arrays(struct pipe_context
* pipe
, unsigned mode
,
220 unsigned start
, unsigned count
)
222 struct r300_context
* r300
= r300_context(pipe
);
224 if (!u_trim_pipe_prim(mode
, &count
)) {
232 if (r300_nothing_to_draw(r300
)) {
236 r300_update_derived_state(r300
);
238 if (!r300_setup_vertex_buffers(r300
)) {
242 r300_emit_dirty_state(r300
);
244 r300_emit_aos(r300
, start
);
246 r300_emit_draw_arrays(r300
, mode
, count
);
251 /****************************************************************************
252 * The rest of this file is for SW TCL rendering only. Please be polite and *
253 * keep these functions separated so that they are easier to locate. ~C. *
254 ***************************************************************************/
256 /* SW TCL arrays, using Draw. */
257 boolean
r300_swtcl_draw_arrays(struct pipe_context
* pipe
,
262 struct r300_context
* r300
= r300_context(pipe
);
265 if (!u_trim_pipe_prim(mode
, &count
)) {
269 if (r300_nothing_to_draw(r300
)) {
273 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
274 void* buf
= pipe_buffer_map(pipe
->screen
,
275 r300
->vertex_buffer
[i
].buffer
,
276 PIPE_BUFFER_USAGE_CPU_READ
);
277 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
280 draw_set_mapped_element_buffer(r300
->draw
, 0, NULL
);
282 draw_set_mapped_constant_buffer(r300
->draw
,
283 r300
->shader_constants
[PIPE_SHADER_VERTEX
].constants
,
284 r300
->shader_constants
[PIPE_SHADER_VERTEX
].count
*
285 (sizeof(float) * 4));
287 draw_arrays(r300
->draw
, mode
, start
, count
);
289 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
290 pipe_buffer_unmap(pipe
->screen
, r300
->vertex_buffer
[i
].buffer
);
291 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
297 /* SW TCL elements, using Draw. */
298 boolean
r300_swtcl_draw_range_elements(struct pipe_context
* pipe
,
299 struct pipe_buffer
* indexBuffer
,
307 struct r300_context
* r300
= r300_context(pipe
);
310 if (!u_trim_pipe_prim(mode
, &count
)) {
314 if (r300_nothing_to_draw(r300
)) {
318 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
319 void* buf
= pipe_buffer_map(pipe
->screen
,
320 r300
->vertex_buffer
[i
].buffer
,
321 PIPE_BUFFER_USAGE_CPU_READ
);
322 draw_set_mapped_vertex_buffer(r300
->draw
, i
, buf
);
325 void* indices
= pipe_buffer_map(pipe
->screen
, indexBuffer
,
326 PIPE_BUFFER_USAGE_CPU_READ
);
327 draw_set_mapped_element_buffer_range(r300
->draw
, indexSize
,
328 minIndex
, maxIndex
, indices
);
330 draw_set_mapped_constant_buffer(r300
->draw
,
331 r300
->shader_constants
[PIPE_SHADER_VERTEX
].constants
,
332 r300
->shader_constants
[PIPE_SHADER_VERTEX
].count
*
333 (sizeof(float) * 4));
335 draw_arrays(r300
->draw
, mode
, start
, count
);
337 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
338 pipe_buffer_unmap(pipe
->screen
, r300
->vertex_buffer
[i
].buffer
);
339 draw_set_mapped_vertex_buffer(r300
->draw
, i
, NULL
);
342 pipe_buffer_unmap(pipe
->screen
, indexBuffer
);
343 draw_set_mapped_element_buffer_range(r300
->draw
, 0, start
,
344 start
+ count
- 1, NULL
);
349 /* Object for rendering using Draw. */
352 struct vbuf_render base
;
355 struct r300_context
* r300
;
357 /* Vertex information */
363 struct pipe_buffer
* vbo
;
370 static INLINE
struct r300_render
*
371 r300_render(struct vbuf_render
* render
)
373 return (struct r300_render
*)render
;
376 static const struct vertex_info
*
377 r300_render_get_vertex_info(struct vbuf_render
* render
)
379 struct r300_render
* r300render
= r300_render(render
);
380 struct r300_context
* r300
= r300render
->r300
;
382 r300_update_derived_state(r300
);
384 return &r300
->vertex_info
->vinfo
;
387 static boolean
r300_render_allocate_vertices(struct vbuf_render
* render
,
391 struct r300_render
* r300render
= r300_render(render
);
392 struct r300_context
* r300
= r300render
->r300
;
393 struct pipe_screen
* screen
= r300
->context
.screen
;
394 size_t size
= (size_t)vertex_size
* (size_t)count
;
396 if (size
+ r300render
->vbo_offset
> r300render
->vbo_size
)
398 pipe_buffer_reference(&r300
->vbo
, NULL
);
399 r300render
->vbo
= pipe_buffer_create(screen
,
401 PIPE_BUFFER_USAGE_VERTEX
,
403 r300render
->vbo_offset
= 0;
404 r300render
->vbo_size
= R300_MAX_VBO_SIZE
;
407 r300render
->vertex_size
= vertex_size
;
408 r300
->vbo
= r300render
->vbo
;
409 r300
->vbo_offset
= r300render
->vbo_offset
;
411 return (r300render
->vbo
) ? TRUE
: FALSE
;
414 static void* r300_render_map_vertices(struct vbuf_render
* render
)
416 struct r300_render
* r300render
= r300_render(render
);
417 struct pipe_screen
* screen
= r300render
->r300
->context
.screen
;
419 r300render
->vbo_ptr
= pipe_buffer_map(screen
, r300render
->vbo
,
420 PIPE_BUFFER_USAGE_CPU_WRITE
);
422 return (r300render
->vbo_ptr
+ r300render
->vbo_offset
);
425 static void r300_render_unmap_vertices(struct vbuf_render
* render
,
429 struct r300_render
* r300render
= r300_render(render
);
430 struct pipe_screen
* screen
= r300render
->r300
->context
.screen
;
431 CS_LOCALS(r300render
->r300
);
433 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX
, max
);
436 r300render
->vbo_max_used
= MAX2(r300render
->vbo_max_used
,
437 r300render
->vertex_size
* (max
+ 1));
438 pipe_buffer_unmap(screen
, r300render
->vbo
);
441 static void r300_render_release_vertices(struct vbuf_render
* render
)
443 struct r300_render
* r300render
= r300_render(render
);
445 r300render
->vbo_offset
+= r300render
->vbo_max_used
;
446 r300render
->vbo_max_used
= 0;
449 static boolean
r300_render_set_primitive(struct vbuf_render
* render
,
452 struct r300_render
* r300render
= r300_render(render
);
454 r300render
->prim
= prim
;
455 r300render
->hwprim
= r300_translate_primitive(prim
);
460 static void r300_render_draw_arrays(struct vbuf_render
* render
,
464 struct r300_render
* r300render
= r300_render(render
);
465 struct r300_context
* r300
= r300render
->r300
;
469 r300_emit_dirty_state(r300
);
471 DBG(r300
, DBG_DRAW
, "r300: Doing vbuf render, count %d\n", count
);
474 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2
, 0);
475 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST
| (count
<< 16) |
480 static void r300_render_draw(struct vbuf_render
* render
,
481 const ushort
* indices
,
484 struct r300_render
* r300render
= r300_render(render
);
485 struct r300_context
* r300
= r300render
->r300
;
490 r300_emit_dirty_state(r300
);
492 BEGIN_CS(2 + (count
+1)/2);
493 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2
, (count
+1)/2);
494 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES
| (count
<< 16) |
496 for (i
= 0; i
< count
-1; i
+= 2) {
497 OUT_CS(indices
[i
+1] << 16 | indices
[i
]);
500 OUT_CS(indices
[count
-1]);
505 static void r300_render_destroy(struct vbuf_render
* render
)
510 static struct vbuf_render
* r300_render_create(struct r300_context
* r300
)
512 struct r300_render
* r300render
= CALLOC_STRUCT(r300_render
);
514 r300render
->r300
= r300
;
516 /* XXX find real numbers plz */
517 r300render
->base
.max_vertex_buffer_bytes
= 128 * 1024;
518 r300render
->base
.max_indices
= 16 * 1024;
520 r300render
->base
.get_vertex_info
= r300_render_get_vertex_info
;
521 r300render
->base
.allocate_vertices
= r300_render_allocate_vertices
;
522 r300render
->base
.map_vertices
= r300_render_map_vertices
;
523 r300render
->base
.unmap_vertices
= r300_render_unmap_vertices
;
524 r300render
->base
.set_primitive
= r300_render_set_primitive
;
525 r300render
->base
.draw
= r300_render_draw
;
526 r300render
->base
.draw_arrays
= r300_render_draw_arrays
;
527 r300render
->base
.release_vertices
= r300_render_release_vertices
;
528 r300render
->base
.destroy
= r300_render_destroy
;
530 r300render
->vbo
= NULL
;
531 r300render
->vbo_size
= 0;
532 r300render
->vbo_offset
= 0;
534 return &r300render
->base
;
537 struct draw_stage
* r300_draw_stage(struct r300_context
* r300
)
539 struct vbuf_render
* render
;
540 struct draw_stage
* stage
;
542 render
= r300_render_create(r300
);
548 stage
= draw_vbuf_stage(r300
->draw
, render
);
551 render
->destroy(render
);
555 draw_set_render(r300
->draw
, render
);