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