Merge branch 'master' of ssh://people.freedesktop.org/~jbarnes/mesa
[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 "pipe/p_inlines.h"
30
31 #include "util/u_memory.h"
32 #include "util/u_prim.h"
33
34 #include "r300_cs.h"
35 #include "r300_context.h"
36 #include "r300_emit.h"
37 #include "r300_reg.h"
38 #include "r300_render.h"
39 #include "r300_state_derived.h"
40
41 /* r300_render: Vertex and index buffer primitive emission. */
42 #define R300_MAX_VBO_SIZE (1024 * 1024)
43
44 uint32_t r300_translate_primitive(unsigned prim)
45 {
46 switch (prim) {
47 case PIPE_PRIM_POINTS:
48 return R300_VAP_VF_CNTL__PRIM_POINTS;
49 case PIPE_PRIM_LINES:
50 return R300_VAP_VF_CNTL__PRIM_LINES;
51 case PIPE_PRIM_LINE_LOOP:
52 return R300_VAP_VF_CNTL__PRIM_LINE_LOOP;
53 case PIPE_PRIM_LINE_STRIP:
54 return R300_VAP_VF_CNTL__PRIM_LINE_STRIP;
55 case PIPE_PRIM_TRIANGLES:
56 return R300_VAP_VF_CNTL__PRIM_TRIANGLES;
57 case PIPE_PRIM_TRIANGLE_STRIP:
58 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_STRIP;
59 case PIPE_PRIM_TRIANGLE_FAN:
60 return R300_VAP_VF_CNTL__PRIM_TRIANGLE_FAN;
61 case PIPE_PRIM_QUADS:
62 return R300_VAP_VF_CNTL__PRIM_QUADS;
63 case PIPE_PRIM_QUAD_STRIP:
64 return R300_VAP_VF_CNTL__PRIM_QUAD_STRIP;
65 case PIPE_PRIM_POLYGON:
66 return R300_VAP_VF_CNTL__PRIM_POLYGON;
67 default:
68 return 0;
69 }
70 }
71
72 static uint32_t r300_provoking_vertex_fixes(struct r300_context *r300,
73 unsigned mode)
74 {
75 struct r300_rs_state* rs = (struct r300_rs_state*)r300->rs_state.state;
76 uint32_t color_control = rs->color_control;
77
78 /* By default (see r300_state.c:r300_create_rs_state) color_control is
79 * initialized to provoking the first vertex.
80 *
81 * Triangle fans must be reduced to the second vertex, not the first, in
82 * Gallium flatshade-first mode, as per the GL spec.
83 * (http://www.opengl.org/registry/specs/ARB/provoking_vertex.txt)
84 *
85 * Quads never provoke correctly in flatshade-first mode. The first
86 * vertex is never considered as provoking, so only the second, third,
87 * and fourth vertices can be selected, and both "third" and "last" modes
88 * select the fourth vertex. This is probably due to D3D lacking quads.
89 *
90 * Similarly, polygons reduce to the first, not the last, vertex, when in
91 * "last" mode, and all other modes start from the second vertex.
92 *
93 * ~ C.
94 */
95
96 if (rs->rs.flatshade_first) {
97 switch (mode) {
98 case PIPE_PRIM_TRIANGLE_FAN:
99 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_SECOND;
100 break;
101 case PIPE_PRIM_QUADS:
102 case PIPE_PRIM_QUAD_STRIP:
103 case PIPE_PRIM_POLYGON:
104 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
105 break;
106 default:
107 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_FIRST;
108 break;
109 }
110 } else {
111 color_control |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST;
112 }
113
114 return color_control;
115 }
116
117 static void r300_emit_draw_arrays(struct r300_context *r300,
118 unsigned mode,
119 unsigned count)
120 {
121 CS_LOCALS(r300);
122
123 BEGIN_CS(8);
124 OUT_CS_REG(R300_GA_COLOR_CONTROL,
125 r300_provoking_vertex_fixes(r300, mode));
126 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, 0);
127 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count - 1);
128 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
129 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
130 r300_translate_primitive(mode));
131 END_CS;
132 }
133
134 static void r300_emit_draw_elements(struct r300_context *r300,
135 struct pipe_buffer* indexBuffer,
136 unsigned indexSize,
137 unsigned minIndex,
138 unsigned maxIndex,
139 unsigned mode,
140 unsigned start,
141 unsigned count)
142 {
143 uint32_t count_dwords;
144 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
145 CS_LOCALS(r300);
146
147 /* XXX most of these are stupid */
148 assert(indexSize == 4 || indexSize == 2);
149 assert((start * indexSize) % 4 == 0);
150 assert(offset_dwords == 0);
151
152 BEGIN_CS(14);
153 OUT_CS_REG(R300_GA_COLOR_CONTROL,
154 r300_provoking_vertex_fixes(r300, mode));
155 OUT_CS_REG(R300_VAP_VF_MIN_VTX_INDX, minIndex);
156 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
157 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
158 if (indexSize == 4) {
159 count_dwords = count + start;
160 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
161 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
162 r300_translate_primitive(mode));
163 } else {
164 count_dwords = (count + start + 1) / 2;
165 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
166 r300_translate_primitive(mode));
167 }
168
169 /* INDX_BUFFER is a truly special packet3.
170 * Unlike most other packet3, where the offset is after the count,
171 * the order is reversed, so the relocation ends up carrying the
172 * size of the indexbuf instead of the offset.
173 *
174 * XXX Fix offset
175 */
176 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
177 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
178 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
179 OUT_CS(offset_dwords);
180 OUT_CS_RELOC(indexBuffer, count_dwords,
181 RADEON_GEM_DOMAIN_GTT, 0, 0);
182
183 END_CS;
184 }
185
186
187 static boolean r300_setup_vertex_buffers(struct r300_context *r300)
188 {
189 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
190 struct pipe_vertex_element *velem = r300->vertex_element;
191
192 validate:
193 for (int i = 0; i < r300->vertex_element_count; i++) {
194 if (!r300->winsys->add_buffer(r300->winsys,
195 vbuf[velem[i].vertex_buffer_index].buffer,
196 RADEON_GEM_DOMAIN_GTT, 0)) {
197 r300->context.flush(&r300->context, 0, NULL);
198 goto validate;
199 }
200 }
201
202 if (!r300->winsys->validate(r300->winsys)) {
203 r300->context.flush(&r300->context, 0, NULL);
204 return r300->winsys->validate(r300->winsys);
205 }
206
207 return TRUE;
208 }
209
210 /* This is the fast-path drawing & emission for HW TCL. */
211 void r300_draw_range_elements(struct pipe_context* pipe,
212 struct pipe_buffer* indexBuffer,
213 unsigned indexSize,
214 unsigned minIndex,
215 unsigned maxIndex,
216 unsigned mode,
217 unsigned start,
218 unsigned count)
219 {
220 struct r300_context* r300 = r300_context(pipe);
221
222 if (!u_trim_pipe_prim(mode, &count)) {
223 return;
224 }
225
226 if (count > 65535) {
227 /* XXX: use aux/indices functions to split this into smaller
228 * primitives.
229 */
230 return;
231 }
232
233 r300_update_derived_state(r300);
234
235 if (!r300_setup_vertex_buffers(r300)) {
236 return;
237 }
238
239 if (!r300->winsys->add_buffer(r300->winsys, indexBuffer,
240 RADEON_GEM_DOMAIN_GTT, 0)) {
241 return;
242 }
243
244 if (!r300->winsys->validate(r300->winsys)) {
245 return;
246 }
247
248 r300_emit_dirty_state(r300);
249
250 r300_emit_aos(r300, 0);
251
252 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, maxIndex,
253 mode, start, count);
254 }
255
256 /* Simple helpers for context setup. Should probably be moved to util. */
257 void r300_draw_elements(struct pipe_context* pipe,
258 struct pipe_buffer* indexBuffer,
259 unsigned indexSize, unsigned mode,
260 unsigned start, unsigned count)
261 {
262 pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0,
263 mode, start, count);
264 }
265
266 void r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
267 unsigned start, unsigned count)
268 {
269 struct r300_context* r300 = r300_context(pipe);
270
271 if (!u_trim_pipe_prim(mode, &count)) {
272 return;
273 }
274
275 if (count > 65535) {
276 /* XXX: driver needs to handle this -- use the functions in
277 * aux/indices to split this into several smaller primitives.
278 */
279 return;
280 }
281
282 r300_update_derived_state(r300);
283
284 if (!r300_setup_vertex_buffers(r300)) {
285 return;
286 }
287
288 r300_emit_dirty_state(r300);
289
290 r300_emit_aos(r300, start);
291
292 r300_emit_draw_arrays(r300, mode, count);
293 }
294
295 /****************************************************************************
296 * The rest of this file is for SW TCL rendering only. Please be polite and *
297 * keep these functions separated so that they are easier to locate. ~C. *
298 ***************************************************************************/
299
300 /* SW TCL arrays, using Draw. */
301 void r300_swtcl_draw_arrays(struct pipe_context* pipe,
302 unsigned mode,
303 unsigned start,
304 unsigned count)
305 {
306 struct r300_context* r300 = r300_context(pipe);
307 int i;
308
309 if (!u_trim_pipe_prim(mode, &count)) {
310 return;
311 }
312
313 for (i = 0; i < r300->vertex_buffer_count; i++) {
314 void* buf = pipe_buffer_map(pipe->screen,
315 r300->vertex_buffer[i].buffer,
316 PIPE_BUFFER_USAGE_CPU_READ);
317 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
318 }
319
320 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
321
322 draw_set_mapped_constant_buffer(r300->draw,
323 PIPE_SHADER_VERTEX,
324 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
325 r300->shader_constants[PIPE_SHADER_VERTEX].count *
326 (sizeof(float) * 4));
327
328 draw_arrays(r300->draw, mode, start, count);
329
330 for (i = 0; i < r300->vertex_buffer_count; i++) {
331 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
332 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
333 }
334 }
335
336 /* SW TCL elements, using Draw. */
337 void r300_swtcl_draw_range_elements(struct pipe_context* pipe,
338 struct pipe_buffer* indexBuffer,
339 unsigned indexSize,
340 unsigned minIndex,
341 unsigned maxIndex,
342 unsigned mode,
343 unsigned start,
344 unsigned count)
345 {
346 struct r300_context* r300 = r300_context(pipe);
347 int i;
348 void* indices;
349
350 if (!u_trim_pipe_prim(mode, &count)) {
351 return;
352 }
353
354 for (i = 0; i < r300->vertex_buffer_count; i++) {
355 void* buf = pipe_buffer_map(pipe->screen,
356 r300->vertex_buffer[i].buffer,
357 PIPE_BUFFER_USAGE_CPU_READ);
358 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
359 }
360
361 indices = pipe_buffer_map(pipe->screen, indexBuffer,
362 PIPE_BUFFER_USAGE_CPU_READ);
363 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
364 minIndex, maxIndex, indices);
365
366 draw_set_mapped_constant_buffer(r300->draw,
367 PIPE_SHADER_VERTEX,
368 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
369 r300->shader_constants[PIPE_SHADER_VERTEX].count *
370 (sizeof(float) * 4));
371
372 draw_arrays(r300->draw, mode, start, count);
373
374 for (i = 0; i < r300->vertex_buffer_count; i++) {
375 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
376 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
377 }
378
379 pipe_buffer_unmap(pipe->screen, indexBuffer);
380 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
381 start + count - 1, NULL);
382 }
383
384 /* Object for rendering using Draw. */
385 struct r300_render {
386 /* Parent class */
387 struct vbuf_render base;
388
389 /* Pipe context */
390 struct r300_context* r300;
391
392 /* Vertex information */
393 size_t vertex_size;
394 unsigned prim;
395 unsigned hwprim;
396
397 /* VBO */
398 struct pipe_buffer* vbo;
399 size_t vbo_size;
400 size_t vbo_offset;
401 size_t vbo_max_used;
402 void * vbo_ptr;
403 };
404
405 static INLINE struct r300_render*
406 r300_render(struct vbuf_render* render)
407 {
408 return (struct r300_render*)render;
409 }
410
411 static const struct vertex_info*
412 r300_render_get_vertex_info(struct vbuf_render* render)
413 {
414 struct r300_render* r300render = r300_render(render);
415 struct r300_context* r300 = r300render->r300;
416
417 r300_update_derived_state(r300);
418
419 return &r300->vertex_info->vinfo;
420 }
421
422 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
423 ushort vertex_size,
424 ushort count)
425 {
426 struct r300_render* r300render = r300_render(render);
427 struct r300_context* r300 = r300render->r300;
428 struct pipe_screen* screen = r300->context.screen;
429 size_t size = (size_t)vertex_size * (size_t)count;
430
431 if (size + r300render->vbo_offset > r300render->vbo_size)
432 {
433 pipe_buffer_reference(&r300->vbo, NULL);
434 r300render->vbo = pipe_buffer_create(screen,
435 64,
436 PIPE_BUFFER_USAGE_VERTEX,
437 R300_MAX_VBO_SIZE);
438 r300render->vbo_offset = 0;
439 r300render->vbo_size = R300_MAX_VBO_SIZE;
440 }
441
442 r300render->vertex_size = vertex_size;
443 r300->vbo = r300render->vbo;
444 r300->vbo_offset = r300render->vbo_offset;
445
446 return (r300render->vbo) ? TRUE : FALSE;
447 }
448
449 static void* r300_render_map_vertices(struct vbuf_render* render)
450 {
451 struct r300_render* r300render = r300_render(render);
452 struct pipe_screen* screen = r300render->r300->context.screen;
453
454 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
455 PIPE_BUFFER_USAGE_CPU_WRITE);
456
457 return ((uint8_t*)r300render->vbo_ptr + r300render->vbo_offset);
458 }
459
460 static void r300_render_unmap_vertices(struct vbuf_render* render,
461 ushort min,
462 ushort max)
463 {
464 struct r300_render* r300render = r300_render(render);
465 struct pipe_screen* screen = r300render->r300->context.screen;
466 CS_LOCALS(r300render->r300);
467 BEGIN_CS(2);
468 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
469 END_CS;
470
471 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
472 r300render->vertex_size * (max + 1));
473 pipe_buffer_unmap(screen, r300render->vbo);
474 }
475
476 static void r300_render_release_vertices(struct vbuf_render* render)
477 {
478 struct r300_render* r300render = r300_render(render);
479
480 r300render->vbo_offset += r300render->vbo_max_used;
481 r300render->vbo_max_used = 0;
482 }
483
484 static boolean r300_render_set_primitive(struct vbuf_render* render,
485 unsigned prim)
486 {
487 struct r300_render* r300render = r300_render(render);
488
489 r300render->prim = prim;
490 r300render->hwprim = r300_translate_primitive(prim);
491
492 return TRUE;
493 }
494
495 static void r300_render_draw_arrays(struct vbuf_render* render,
496 unsigned start,
497 unsigned count)
498 {
499 struct r300_render* r300render = r300_render(render);
500 struct r300_context* r300 = r300render->r300;
501
502 CS_LOCALS(r300);
503
504 r300_emit_dirty_state(r300);
505
506 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
507
508 BEGIN_CS(2);
509 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
510 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
511 r300render->hwprim);
512 END_CS;
513 }
514
515 static void r300_render_draw(struct vbuf_render* render,
516 const ushort* indices,
517 uint count)
518 {
519 struct r300_render* r300render = r300_render(render);
520 struct r300_context* r300 = r300render->r300;
521 int i;
522
523 CS_LOCALS(r300);
524
525 r300_emit_dirty_state(r300);
526
527 BEGIN_CS(2 + (count+1)/2);
528 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
529 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
530 r300render->hwprim);
531 for (i = 0; i < count-1; i += 2) {
532 OUT_CS(indices[i+1] << 16 | indices[i]);
533 }
534 if (count % 2) {
535 OUT_CS(indices[count-1]);
536 }
537 END_CS;
538 }
539
540 static void r300_render_destroy(struct vbuf_render* render)
541 {
542 FREE(render);
543 }
544
545 static struct vbuf_render* r300_render_create(struct r300_context* r300)
546 {
547 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
548
549 r300render->r300 = r300;
550
551 /* XXX find real numbers plz */
552 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
553 r300render->base.max_indices = 16 * 1024;
554
555 r300render->base.get_vertex_info = r300_render_get_vertex_info;
556 r300render->base.allocate_vertices = r300_render_allocate_vertices;
557 r300render->base.map_vertices = r300_render_map_vertices;
558 r300render->base.unmap_vertices = r300_render_unmap_vertices;
559 r300render->base.set_primitive = r300_render_set_primitive;
560 r300render->base.draw = r300_render_draw;
561 r300render->base.draw_arrays = r300_render_draw_arrays;
562 r300render->base.release_vertices = r300_render_release_vertices;
563 r300render->base.destroy = r300_render_destroy;
564
565 r300render->vbo = NULL;
566 r300render->vbo_size = 0;
567 r300render->vbo_offset = 0;
568
569 return &r300render->base;
570 }
571
572 struct draw_stage* r300_draw_stage(struct r300_context* r300)
573 {
574 struct vbuf_render* render;
575 struct draw_stage* stage;
576
577 render = r300_render_create(r300);
578
579 if (!render) {
580 return NULL;
581 }
582
583 stage = draw_vbuf_stage(r300->draw, render);
584
585 if (!stage) {
586 render->destroy(render);
587 return NULL;
588 }
589
590 draw_set_render(r300->draw, render);
591
592 return stage;
593 }