r300g: decouple vertex stream setup (PSC) and VS output mapping (VAP_OUT)
[mesa.git] / src / gallium / drivers / r300 / r300_render.c
1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 /* r300_render: Vertex and index buffer primitive emission. Contains both
24 * HW TCL fastpath rendering, and SW TCL Draw-assisted rendering. */
25
26 #include "draw/draw_context.h"
27 #include "draw/draw_vbuf.h"
28
29 #include "util/u_inlines.h"
30
31 #include "util/u_format.h"
32 #include "util/u_memory.h"
33 #include "util/u_prim.h"
34
35 #include "r300_cs.h"
36 #include "r300_context.h"
37 #include "r300_emit.h"
38 #include "r300_reg.h"
39 #include "r300_render.h"
40 #include "r300_state_derived.h"
41
42 /* r300_render: Vertex and index buffer primitive emission. */
43 #define R300_MAX_VBO_SIZE (1024 * 1024)
44
45 /* XXX The DRM rejects VAP_ALT_NUM_VERTICES.. */
46 //#define ENABLE_ALT_NUM_VERTS
47
48 uint32_t r300_translate_primitive(unsigned prim)
49 {
50 switch (prim) {
51 case PIPE_PRIM_POINTS:
52 return R300_VAP_VF_CNTL__PRIM_POINTS;
53 case PIPE_PRIM_LINES:
54 return R300_VAP_VF_CNTL__PRIM_LINES;
55 case PIPE_PRIM_LINE_LOOP:
56 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
57 case PIPE_PRIM_LINE_STRIP:
58 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
59 case PIPE_PRIM_TRIANGLES:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
61 case PIPE_PRIM_TRIANGLE_STRIP:
62 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
63 case PIPE_PRIM_TRIANGLE_FAN:
64 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
65 case PIPE_PRIM_QUADS:
66 return R300_VAP_VF_CNTL__PRIM_QUADS;
67 case PIPE_PRIM_QUAD_STRIP:
68 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
69 case PIPE_PRIM_POLYGON:
70 return R300_VAP_VF_CNTL__PRIM_POLYGON;
71 default:
72 return 0;
73 }
74 }
75
76 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
77 unsigned mode)
78 {
79 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
80 uint32_t color_control = rs->color_control;
81
82 /* By default (see r300_state.c:r300_create_rs_state) color_control is
83 * initialized to provoking the first vertex.
84 *
85 * Triangle fans must be reduced to the second vertex, not the first, in
86 * Gallium flatshade-first mode, as per the GL spec.
87 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
88 *
89 * Quads never provoke correctly in flatshade-first mode. The first
90 * vertex is never considered as provoking, so only the second, third,
91 * and fourth vertices can be selected, and both "third" and "last" modes
92 * select the fourth vertex. This is probably due to D3D lacking quads.
93 *
94 * Similarly, polygons reduce to the first, not the last, vertex, when in
95 * "last" mode, and all other modes start from the second vertex.
96 *
97 * ~ C.
98 */
99
100 if (rs->rs.flatshade_first) {
101 switch (mode) {
102 case PIPE_PRIM_TRIANGLE_FAN:
103 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
104 break;
105 case PIPE_PRIM_QUADS:
106 case PIPE_PRIM_QUAD_STRIP:
107 case PIPE_PRIM_POLYGON:
108 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
109 break;
110 default:
111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
112 break;
113 }
114 } else {
115 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
116 }
117
118 return color_control;
119 }
120
121 /* Check if the requested number of dwords is available in the CS and
122 * if not, flush. Return TRUE if the flush occured. */
123 static boolean r300_reserve_cs_space(struct r300_context *r300,
124 unsigned dwords)
125 {
126 if (!r300->winsys->check_cs(r300->winsys, dwords)) {
127 r300->context.flush(&r300->context, 0, NULL);
128 return TRUE;
129 }
130 return FALSE;
131 }
132
133 static boolean immd_is_good_idea(struct r300_context *r300,
134 unsigned count)
135 {
136 return count <= 4;
137 }
138
139 static void r300_emit_draw_arrays_immediate(struct r300_context *r300,
140 unsigned mode,
141 unsigned start,
142 unsigned count)
143 {
144 struct pipe_vertex_element* velem;
145 struct pipe_vertex_buffer* vbuf;
146 unsigned vertex_element_count = r300->vertex_element_count;
147 unsigned i, v, vbi, dw, elem_offset, dwords;
148
149 /* Size of the vertex, in dwords. */
150 unsigned vertex_size = 0;
151
152 /* Offsets of the attribute, in dwords, from the start of the vertex. */
153 unsigned offset[PIPE_MAX_ATTRIBS];
154
155 /* Size of the vertex element, in dwords. */
156 unsigned size[PIPE_MAX_ATTRIBS];
157
158 /* Stride to the same attrib in the next vertex in the vertex buffer,
159 * in dwords. */
160 unsigned stride[PIPE_MAX_ATTRIBS] = {0};
161
162 /* Mapped vertex buffers. */
163 uint32_t* map[PIPE_MAX_ATTRIBS] = {0};
164
165 CS_LOCALS(r300);
166
167 /* Calculate the vertex size, offsets, strides etc. and map the buffers. */
168 for (i = 0; i < vertex_element_count; i++) {
169 velem = &r300->vertex_element[i];
170 offset[i] = velem->src_offset / 4;
171 size[i] = util_format_get_blocksize(velem->src_format) / 4;
172 vertex_size += size[i];
173 vbi = velem->vertex_buffer_index;
174
175 /* Map the buffer. */
176 if (!map[vbi]) {
177 vbuf = &r300->vertex_buffer[vbi];
178 map[vbi] = (uint32_t*)pipe_buffer_map(r300->context.screen,
179 vbuf->buffer,
180 PIPE_BUFFER_USAGE_CPU_READ);
181 map[vbi] += vbuf->buffer_offset / 4;
182 stride[vbi] = vbuf->stride / 4;
183 }
184 }
185
186 dwords = 10 + count * vertex_size;
187
188 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
189 r300_emit_buffer_validate(r300, FALSE, 0);
190 r300_emit_dirty_state(r300);
191
192 BEGIN_CS(dwords);
193 OUT_CS_REG(R300_GA_COLOR_CONTROL,
194 r300_provoking_vertex_fixes(r300, mode));
195 OUT_CS_REG(R300_VAP_VTX_SIZE, vertex_size);
196 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
197 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
198 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_IMMD_2, count * vertex_size);
199 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_EMBEDDED | (count << 16) |
200 r300_translate_primitive(mode));
201
202 /* Emit vertices. */
203 for (v = 0; v < count; v++) {
204 for (i = 0; i < vertex_element_count; i++) {
205 velem = &r300->vertex_element[i];
206 vbi = velem->vertex_buffer_index;
207 elem_offset = offset[i] + stride[vbi] * (v + start);
208
209 for (dw = 0; dw < size[i]; dw++) {
210 OUT_CS(map[vbi][elem_offset + dw]);
211 }
212 }
213 }
214 END_CS;
215
216 /* Unmap buffers. */
217 for (i = 0; i < vertex_element_count; i++) {
218 vbi = r300->vertex_element[i].vertex_buffer_index;
219
220 if (map[vbi]) {
221 vbuf = &r300->vertex_buffer[vbi];
222 pipe_buffer_unmap(r300->context.screen, vbuf->buffer);
223 map[vbi] = NULL;
224 }
225 }
226 }
227
228 static void r300_emit_draw_arrays(struct r300_context *r300,
229 unsigned mode,
230 unsigned count)
231 {
232 #if defined(ENABLE_ALT_NUM_VERTS)
233 boolean alt_num_verts = count > 65535;
234 #else
235 boolean alt_num_verts = FALSE;
236 #endif
237 CS_LOCALS(r300);
238
239 if (alt_num_verts) {
240 assert(count < (1 << 24));
241 BEGIN_CS(10);
242 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
243 } else {
244 BEGIN_CS(8);
245 }
246 OUT_CS_REG(R300_GA_COLOR_CONTROL,
247 r300_provoking_vertex_fixes(r300, mode));
248 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
249 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
250 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
251 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
252 r300_translate_primitive(mode) |
253 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
254 END_CS;
255 }
256
257 static void r300_emit_draw_elements(struct r300_context *r300,
258 struct pipe_buffer* indexBuffer,
259 unsigned indexSize,
260 unsigned minIndex,
261 unsigned maxIndex,
262 unsigned mode,
263 unsigned start,
264 unsigned count)
265 {
266 uint32_t count_dwords;
267 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
268 #if defined(ENABLE_ALT_NUM_VERTS)
269 boolean alt_num_verts = count > 65535;
270 #else
271 boolean alt_num_verts = FALSE;
272 #endif
273 CS_LOCALS(r300);
274
275 assert((start * indexSize) % 4 == 0);
276
277 if (alt_num_verts) {
278 assert(count < (1 << 24));
279 BEGIN_CS(16);
280 OUT_CS_REG(R500_VAP_ALT_NUM_VERTICES, count);
281 } else {
282 BEGIN_CS(14);
283 }
284 OUT_CS_REG(R300_GA_COLOR_CONTROL,
285 r300_provoking_vertex_fixes(r300, mode));
286 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex);
287 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
288 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
289 if (indexSize == 4) {
290 count_dwords = count;
291 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
292 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
293 r300_translate_primitive(mode) |
294 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
295 } else {
296 count_dwords = (count + 1) / 2;
297 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
298 r300_translate_primitive(mode) |
299 (alt_num_verts ? R500_VAP_VF_CNTL__USE_ALT_NUM_VERTS : 0));
300 }
301
302 /* INDX_BUFFER is a truly special packet3.
303 * Unlike most other packet3, where the offset is after the count,
304 * the order is reversed, so the relocation ends up carrying the
305 * size of the indexbuf instead of the offset.
306 */
307 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
308 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
309 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
310 OUT_CS(offset_dwords << 2);
311 OUT_CS_RELOC(indexBuffer, count_dwords,
312 RADEON_GEM_DOMAIN_GTT, 0, 0);
313
314 END_CS;
315 }
316
317 static void r300_shorten_ubyte_elts(struct r300_context* r300,
318 struct pipe_buffer** elts,
319 unsigned count)
320 {
321 struct pipe_screen* screen = r300->context.screen;
322 struct pipe_buffer* new_elts;
323 unsigned char *in_map;
324 unsigned short *out_map;
325 unsigned i;
326
327 new_elts = screen->buffer_create(screen, 32,
328 PIPE_BUFFER_USAGE_INDEX |
329 PIPE_BUFFER_USAGE_CPU_WRITE |
330 PIPE_BUFFER_USAGE_GPU_READ,
331 2 * count);
332
333 in_map = pipe_buffer_map(screen, *elts, PIPE_BUFFER_USAGE_CPU_READ);
334 out_map = pipe_buffer_map(screen, new_elts, PIPE_BUFFER_USAGE_CPU_WRITE);
335
336 for (i = 0; i < count; i++) {
337 *out_map = (unsigned short)*in_map;
338 in_map++;
339 out_map++;
340 }
341
342 pipe_buffer_unmap(screen, *elts);
343 pipe_buffer_unmap(screen, new_elts);
344
345 *elts = new_elts;
346 }
347
348 /* This is the fast-path drawing & emission for HW TCL. */
349 void r300_draw_range_elements(struct pipe_context* pipe,
350 struct pipe_buffer* indexBuffer,
351 unsigned indexSize,
352 unsigned minIndex,
353 unsigned maxIndex,
354 unsigned mode,
355 unsigned start,
356 unsigned count)
357 {
358 struct r300_context* r300 = r300_context(pipe);
359 struct pipe_buffer* orgIndexBuffer = indexBuffer;
360 #if defined(ENABLE_ALT_NUM_VERTS)
361 boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
362 count > 65536;
363 #else
364 boolean alt_num_verts = FALSE;
365 #endif
366 unsigned short_count;
367
368 if (!u_trim_pipe_prim(mode, &count)) {
369 return;
370 }
371
372 if (indexSize == 1) {
373 r300_shorten_ubyte_elts(r300, &indexBuffer, count);
374 indexSize = 2;
375 }
376
377 r300_update_derived_state(r300);
378
379 /* 128 dwords for emit_aos and emit_draw_elements */
380 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
381 r300_emit_buffer_validate(r300, TRUE, indexBuffer);
382 r300_emit_dirty_state(r300);
383 r300_emit_aos(r300, 0);
384
385 if (alt_num_verts || count <= 65535) {
386 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
387 maxIndex, mode, start, count);
388 } else {
389 do {
390 short_count = MIN2(count, 65534);
391 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex,
392 maxIndex, mode, start, short_count);
393
394 start += short_count;
395 count -= short_count;
396
397 /* 16 spare dwords are enough for emit_draw_elements. */
398 if (count && r300_reserve_cs_space(r300, 16)) {
399 r300_emit_buffer_validate(r300, TRUE, indexBuffer);
400 r300_emit_dirty_state(r300);
401 r300_emit_aos(r300, 0);
402 }
403 } while (count);
404 }
405
406 if (indexBuffer != orgIndexBuffer) {
407 pipe->screen->buffer_destroy(indexBuffer);
408 }
409 }
410
411 /* Simple helpers for context setup. Should probably be moved to util. */
412 void r300_draw_elements(struct pipe_context* pipe,
413 struct pipe_buffer* indexBuffer,
414 unsigned indexSize, unsigned mode,
415 unsigned start, unsigned count)
416 {
417 struct r300_context *r300 = r300_context(pipe);
418
419 pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0,
420 r300->vertex_buffer_max_index,
421 mode, start, count);
422 }
423
424 void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
425 unsigned start, unsigned count)
426 {
427 struct r300_context* r300 = r300_context(pipe);
428 #if defined(ENABLE_ALT_NUM_VERTS)
429 boolean alt_num_verts = r300_screen(pipe->screen)->caps->is_r500 &&
430 count > 65536;
431 #else
432 boolean alt_num_verts = FALSE;
433 #endif
434 unsigned short_count;
435
436 if (!u_trim_pipe_prim(mode, &count)) {
437 return;
438 }
439
440 r300_update_derived_state(r300);
441
442 if (immd_is_good_idea(r300, count)) {
443 r300_emit_draw_arrays_immediate(r300, mode, start, count);
444 } else {
445 /* Make sure there are at least 128 spare dwords in the command buffer.
446 * (most of it being consumed by emit_aos) */
447 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 128);
448 r300_emit_buffer_validate(r300, TRUE, 0);
449 r300_emit_dirty_state(r300);
450
451 if (alt_num_verts || count <= 65535) {
452 r300_emit_aos(r300, start);
453 r300_emit_draw_arrays(r300, mode, count);
454 } else {
455 do {
456 short_count = MIN2(count, 65535);
457 r300_emit_aos(r300, start);
458 r300_emit_draw_arrays(r300, mode, short_count);
459
460 start += short_count;
461 count -= short_count;
462
463 /* Again, we emit both AOS and draw_arrays so there should be
464 * at least 128 spare dwords. */
465 if (count && r300_reserve_cs_space(r300, 128)) {
466 r300_emit_buffer_validate(r300, TRUE, 0);
467 r300_emit_dirty_state(r300);
468 }
469 } while (count);
470 }
471 }
472 }
473
474 /****************************************************************************
475 * The rest of this file is for SW TCL rendering only. Please be polite and *
476 * keep these functions separated so that they are easier to locate. ~C. *
477 ***************************************************************************/
478
479 /* SW TCL arrays, using Draw. */
480 void r300_swtcl_draw_arrays(struct pipe_context* pipe,
481 unsigned mode,
482 unsigned start,
483 unsigned count)
484 {
485 struct r300_context* r300 = r300_context(pipe);
486 int i;
487
488 if (!u_trim_pipe_prim(mode, &count)) {
489 return;
490 }
491
492 for (i = 0; i < r300->vertex_buffer_count; i++) {
493 void* buf = pipe_buffer_map(pipe->screen,
494 r300->vertex_buffer[i].buffer,
495 PIPE_BUFFER_USAGE_CPU_READ);
496 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
497 }
498
499 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
500
501 draw_set_mapped_constant_buffer(r300->draw,
502 PIPE_SHADER_VERTEX,
503 0,
504 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
505 r300->shader_constants[PIPE_SHADER_VERTEX].count *
506 (sizeof(float) * 4));
507
508 draw_arrays(r300->draw, mode, start, count);
509
510 for (i = 0; i < r300->vertex_buffer_count; i++) {
511 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
512 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
513 }
514 }
515
516 /* SW TCL elements, using Draw. */
517 void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
518 struct pipe_buffer* indexBuffer,
519 unsigned indexSize,
520 unsigned minIndex,
521 unsigned maxIndex,
522 unsigned mode,
523 unsigned start,
524 unsigned count)
525 {
526 struct r300_context* r300 = r300_context(pipe);
527 int i;
528 void* indices;
529
530 if (!u_trim_pipe_prim(mode, &count)) {
531 return;
532 }
533
534 for (i = 0; i < r300->vertex_buffer_count; i++) {
535 void* buf = pipe_buffer_map(pipe->screen,
536 r300->vertex_buffer[i].buffer,
537 PIPE_BUFFER_USAGE_CPU_READ);
538 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
539 }
540
541 indices = pipe_buffer_map(pipe->screen, indexBuffer,
542 PIPE_BUFFER_USAGE_CPU_READ);
543 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
544 minIndex, maxIndex, indices);
545
546 draw_set_mapped_constant_buffer(r300->draw,
547 PIPE_SHADER_VERTEX,
548 0,
549 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
550 r300->shader_constants[PIPE_SHADER_VERTEX].count *
551 (sizeof(float) * 4));
552
553 draw_arrays(r300->draw, mode, start, count);
554
555 for (i = 0; i < r300->vertex_buffer_count; i++) {
556 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
557 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
558 }
559
560 pipe_buffer_unmap(pipe->screen, indexBuffer);
561 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
562 start + count - 1, NULL);
563 }
564
565 /* Object for rendering using Draw. */
566 struct r300_render {
567 /* Parent class */
568 struct vbuf_render base;
569
570 /* Pipe context */
571 struct r300_context* r300;
572
573 /* Vertex information */
574 size_t vertex_size;
575 unsigned prim;
576 unsigned hwprim;
577
578 /* VBO */
579 struct pipe_buffer* vbo;
580 size_t vbo_size;
581 size_t vbo_offset;
582 size_t vbo_max_used;
583 void * vbo_ptr;
584 };
585
586 static INLINE struct r300_render*
587 r300_render(struct vbuf_render* render)
588 {
589 return (struct r300_render*)render;
590 }
591
592 static const struct vertex_info*
593 r300_render_get_vertex_info(struct vbuf_render* render)
594 {
595 struct r300_render* r300render = r300_render(render);
596 struct r300_context* r300 = r300render->r300;
597
598 r300_update_derived_state(r300);
599
600 return &r300->vertex_info;
601 }
602
603 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
604 ushort vertex_size,
605 ushort count)
606 {
607 struct r300_render* r300render = r300_render(render);
608 struct r300_context* r300 = r300render->r300;
609 struct pipe_screen* screen = r300->context.screen;
610 size_t size = (size_t)vertex_size * (size_t)count;
611
612 if (size + r300render->vbo_offset > r300render->vbo_size)
613 {
614 pipe_buffer_reference(&r300->vbo, NULL);
615 r300render->vbo = pipe_buffer_create(screen,
616 64,
617 PIPE_BUFFER_USAGE_VERTEX,
618 R300_MAX_VBO_SIZE);
619 r300render->vbo_offset = 0;
620 r300render->vbo_size = R300_MAX_VBO_SIZE;
621 }
622
623 r300render->vertex_size = vertex_size;
624 r300->vbo = r300render->vbo;
625 r300->vbo_offset = r300render->vbo_offset;
626
627 return (r300render->vbo) ? TRUE : FALSE;
628 }
629
630 static void* r300_render_map_vertices(struct vbuf_render* render)
631 {
632 struct r300_render* r300render = r300_render(render);
633 struct pipe_screen* screen = r300render->r300->context.screen;
634
635 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
636 PIPE_BUFFER_USAGE_CPU_WRITE);
637
638 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
639 }
640
641 static void r300_render_unmap_vertices(struct vbuf_render* render,
642 ushort min,
643 ushort max)
644 {
645 struct r300_render* r300render = r300_render(render);
646 struct pipe_screen* screen = r300render->r300->context.screen;
647 CS_LOCALS(r300render->r300);
648 BEGIN_CS(2);
649 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
650 END_CS;
651
652 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
653 r300render->vertex_size * (max + 1));
654 pipe_buffer_unmap(screen, r300render->vbo);
655 }
656
657 static void r300_render_release_vertices(struct vbuf_render* render)
658 {
659 struct r300_render* r300render = r300_render(render);
660
661 r300render->vbo_offset += r300render->vbo_max_used;
662 r300render->vbo_max_used = 0;
663 }
664
665 static boolean r300_render_set_primitive(struct vbuf_render* render,
666 unsigned prim)
667 {
668 struct r300_render* r300render = r300_render(render);
669
670 r300render->prim = prim;
671 r300render->hwprim = r300_translate_primitive(prim);
672
673 return TRUE;
674 }
675
676 static void r300_render_draw_arrays(struct vbuf_render* render,
677 unsigned start,
678 unsigned count)
679 {
680 struct r300_render* r300render = r300_render(render);
681 struct r300_context* r300 = r300render->r300;
682
683 CS_LOCALS(r300);
684
685 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + 2);
686 r300_emit_dirty_state(r300);
687
688 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
689
690 BEGIN_CS(2);
691 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
692 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
693 r300render->hwprim);
694 END_CS;
695 }
696
697 static void r300_render_draw(struct vbuf_render* render,
698 const ushort* indices,
699 uint count)
700 {
701 struct r300_render* r300render = r300_render(render);
702 struct r300_context* r300 = r300render->r300;
703 int i;
704 unsigned dwords = 2 + (count+1)/2;
705
706 CS_LOCALS(r300);
707
708 r300_reserve_cs_space(r300, r300_get_num_dirty_dwords(r300) + dwords);
709 r300_emit_dirty_state(r300);
710
711 BEGIN_CS(dwords);
712 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
713 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
714 r300render->hwprim);
715 for (i = 0; i < count-1; i += 2) {
716 OUT_CS(indices[i+1] << 16 | indices[i]);
717 }
718 if (count % 2) {
719 OUT_CS(indices[count-1]);
720 }
721 END_CS;
722 }
723
724 static void r300_render_destroy(struct vbuf_render* render)
725 {
726 FREE(render);
727 }
728
729 static struct vbuf_render* r300_render_create(struct r300_context* r300)
730 {
731 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
732
733 r300render->r300 = r300;
734
735 /* XXX find real numbers plz */
736 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
737 r300render->base.max_indices = 16 * 1024;
738
739 r300render->base.get_vertex_info = r300_render_get_vertex_info;
740 r300render->base.allocate_vertices = r300_render_allocate_vertices;
741 r300render->base.map_vertices = r300_render_map_vertices;
742 r300render->base.unmap_vertices = r300_render_unmap_vertices;
743 r300render->base.set_primitive = r300_render_set_primitive;
744 r300render->base.draw = r300_render_draw;
745 r300render->base.draw_arrays = r300_render_draw_arrays;
746 r300render->base.release_vertices = r300_render_release_vertices;
747 r300render->base.destroy = r300_render_destroy;
748
749 r300render->vbo = NULL;
750 r300render->vbo_size = 0;
751 r300render->vbo_offset = 0;
752
753 return &r300render->base;
754 }
755
756 struct draw_stage* r300_draw_stage(struct r300_context* r300)
757 {
758 struct vbuf_render* render;
759 struct draw_stage* stage;
760
761 render = r300_render_create(r300);
762
763 if (!render) {
764 return NULL;
765 }
766
767 stage = draw_vbuf_stage(r300->draw, render);
768
769 if (!stage) {
770 render->destroy(render);
771 return NULL;
772 }
773
774 draw_set_render(r300->draw, render);
775
776 return stage;
777 }