Merge branch 'mesa_7_7_branch'
[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 #include "r300_vbo.h"
41
42 /* r300_render: Vertex and index buffer primitive emission. */
43 #define R300_MAX_VBO_SIZE (1024 * 1024)
44
45 uint32_t r300_translate_primitive(unsigned prim)
46 {
47 switch (prim) {
48 case PIPE_PRIM_POINTS:
49 return R300_VAP_VF_CNTL__PRIM_POINTS;
50 case PIPE_PRIM_LINES:
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;
62 case PIPE_PRIM_QUADS:
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;
68 default:
69 return 0;
70 }
71 }
72
73 static void r300_emit_draw_arrays(struct r300_context *r300,
74 unsigned mode,
75 unsigned count)
76 {
77 CS_LOCALS(r300);
78
79 BEGIN_CS(4);
80 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count);
81 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
82 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
83 r300_translate_primitive(mode));
84 END_CS;
85 }
86
87 static void r300_emit_draw_elements(struct r300_context *r300,
88 struct pipe_buffer* indexBuffer,
89 unsigned indexSize,
90 unsigned minIndex,
91 unsigned maxIndex,
92 unsigned mode,
93 unsigned start,
94 unsigned count)
95 {
96 uint32_t count_dwords;
97 uint32_t offset_dwords = indexSize * start / sizeof(uint32_t);
98 CS_LOCALS(r300);
99
100 /* XXX most of these are stupid */
101 assert(indexSize == 4 || indexSize == 2);
102 assert((start * indexSize) % 4 == 0);
103 assert(offset_dwords == 0);
104
105 BEGIN_CS(10);
106 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
107 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
108 if (indexSize == 4) {
109 count_dwords = count + start;
110 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
111 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
112 r300_translate_primitive(mode));
113 } else {
114 count_dwords = (count + start + 1) / 2;
115 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
116 r300_translate_primitive(mode));
117 }
118
119 /* INDX_BUFFER is a truly special packet3.
120 * Unlike most other packet3, where the offset is after the count,
121 * the order is reversed, so the relocation ends up carrying the
122 * size of the indexbuf instead of the offset.
123 *
124 * XXX Fix offset
125 */
126 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
127 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
128 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
129 OUT_CS(offset_dwords);
130 OUT_CS_RELOC(indexBuffer, count_dwords,
131 RADEON_GEM_DOMAIN_GTT, 0, 0);
132
133 END_CS;
134 }
135
136
137 static boolean r300_setup_vertex_buffers(struct r300_context *r300)
138 {
139 struct pipe_vertex_buffer *vbuf = r300->vertex_buffer;
140 struct pipe_vertex_element *velem = r300->vertex_element;
141
142 validate:
143 for (int i = 0; i < r300->vertex_element_count; i++) {
144 if (!r300->winsys->add_buffer(r300->winsys,
145 vbuf[velem[i].vertex_buffer_index].buffer,
146 RADEON_GEM_DOMAIN_GTT, 0)) {
147 r300->context.flush(&r300->context, 0, NULL);
148 goto validate;
149 }
150 }
151
152 if (!r300->winsys->validate(r300->winsys)) {
153 r300->context.flush(&r300->context, 0, NULL);
154 return r300->winsys->validate(r300->winsys);
155 }
156
157 return TRUE;
158 }
159
160 /* This is the fast-path drawing & emission for HW TCL. */
161 boolean r300_draw_range_elements(struct pipe_context* pipe,
162 struct pipe_buffer* indexBuffer,
163 unsigned indexSize,
164 unsigned minIndex,
165 unsigned maxIndex,
166 unsigned mode,
167 unsigned start,
168 unsigned count)
169 {
170 struct r300_context* r300 = r300_context(pipe);
171
172 if (!u_trim_pipe_prim(mode, &count)) {
173 return FALSE;
174 }
175
176 if (count > 65535) {
177 return FALSE;
178 }
179
180 r300_update_derived_state(r300);
181
182 if (!r300_setup_vertex_buffers(r300)) {
183 return FALSE;
184 }
185
186 setup_index_buffer(r300, indexBuffer, indexSize);
187
188 r300_emit_dirty_state(r300);
189
190 r300_emit_aos(r300, 0);
191
192 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, maxIndex,
193 mode, start, count);
194
195 return TRUE;
196 }
197
198 /* Simple helpers for context setup. Should probably be moved to util. */
199 boolean r300_draw_elements(struct pipe_context* pipe,
200 struct pipe_buffer* indexBuffer,
201 unsigned indexSize, unsigned mode,
202 unsigned start, unsigned count)
203 {
204 return pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0,
205 mode, start, count);
206 }
207
208 boolean r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
209 unsigned start, unsigned count)
210 {
211 struct r300_context* r300 = r300_context(pipe);
212
213 if (!u_trim_pipe_prim(mode, &count)) {
214 return FALSE;
215 }
216
217 if (count > 65535) {
218 return FALSE;
219 }
220
221 r300_update_derived_state(r300);
222
223 if (!r300_setup_vertex_buffers(r300)) {
224 return FALSE;
225 }
226
227 r300_emit_dirty_state(r300);
228
229 r300_emit_aos(r300, start);
230
231 r300_emit_draw_arrays(r300, mode, count);
232
233 return TRUE;
234 }
235
236 /****************************************************************************
237 * The rest of this file is for SW TCL rendering only. Please be polite and *
238 * keep these functions separated so that they are easier to locate. ~C. *
239 ***************************************************************************/
240
241 /* SW TCL arrays, using Draw. */
242 boolean r300_swtcl_draw_arrays(struct pipe_context* pipe,
243 unsigned mode,
244 unsigned start,
245 unsigned count)
246 {
247 struct r300_context* r300 = r300_context(pipe);
248 int i;
249
250 if (!u_trim_pipe_prim(mode, &count)) {
251 return FALSE;
252 }
253
254 for (i = 0; i < r300->vertex_buffer_count; i++) {
255 void* buf = pipe_buffer_map(pipe->screen,
256 r300->vertex_buffer[i].buffer,
257 PIPE_BUFFER_USAGE_CPU_READ);
258 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
259 }
260
261 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
262
263 draw_set_mapped_constant_buffer(r300->draw,
264 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
265 r300->shader_constants[PIPE_SHADER_VERTEX].count *
266 (sizeof(float) * 4));
267
268 draw_arrays(r300->draw, mode, start, count);
269
270 for (i = 0; i < r300->vertex_buffer_count; i++) {
271 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
272 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
273 }
274
275 return TRUE;
276 }
277
278 /* SW TCL elements, using Draw. */
279 boolean r300_swtcl_draw_range_elements(struct pipe_context* pipe,
280 struct pipe_buffer* indexBuffer,
281 unsigned indexSize,
282 unsigned minIndex,
283 unsigned maxIndex,
284 unsigned mode,
285 unsigned start,
286 unsigned count)
287 {
288 struct r300_context* r300 = r300_context(pipe);
289 int i;
290
291 if (!u_trim_pipe_prim(mode, &count)) {
292 return FALSE;
293 }
294
295 for (i = 0; i < r300->vertex_buffer_count; i++) {
296 void* buf = pipe_buffer_map(pipe->screen,
297 r300->vertex_buffer[i].buffer,
298 PIPE_BUFFER_USAGE_CPU_READ);
299 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
300 }
301
302 void* indices = pipe_buffer_map(pipe->screen, indexBuffer,
303 PIPE_BUFFER_USAGE_CPU_READ);
304 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
305 minIndex, maxIndex, indices);
306
307 draw_set_mapped_constant_buffer(r300->draw,
308 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
309 r300->shader_constants[PIPE_SHADER_VERTEX].count *
310 (sizeof(float) * 4));
311
312 draw_arrays(r300->draw, mode, start, count);
313
314 for (i = 0; i < r300->vertex_buffer_count; i++) {
315 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
316 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
317 }
318
319 pipe_buffer_unmap(pipe->screen, indexBuffer);
320 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
321 start + count - 1, NULL);
322
323 return TRUE;
324 }
325
326 /* Object for rendering using Draw. */
327 struct r300_render {
328 /* Parent class */
329 struct vbuf_render base;
330
331 /* Pipe context */
332 struct r300_context* r300;
333
334 /* Vertex information */
335 size_t vertex_size;
336 unsigned prim;
337 unsigned hwprim;
338
339 /* VBO */
340 struct pipe_buffer* vbo;
341 size_t vbo_size;
342 size_t vbo_offset;
343 size_t vbo_max_used;
344 void * vbo_ptr;
345 };
346
347 static INLINE struct r300_render*
348 r300_render(struct vbuf_render* render)
349 {
350 return (struct r300_render*)render;
351 }
352
353 static const struct vertex_info*
354 r300_render_get_vertex_info(struct vbuf_render* render)
355 {
356 struct r300_render* r300render = r300_render(render);
357 struct r300_context* r300 = r300render->r300;
358
359 r300_update_derived_state(r300);
360
361 return &r300->vertex_info->vinfo;
362 }
363
364 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
365 ushort vertex_size,
366 ushort count)
367 {
368 struct r300_render* r300render = r300_render(render);
369 struct r300_context* r300 = r300render->r300;
370 struct pipe_screen* screen = r300->context.screen;
371 size_t size = (size_t)vertex_size * (size_t)count;
372
373 if (size + r300render->vbo_offset > r300render->vbo_size)
374 {
375 pipe_buffer_reference(&r300->vbo, NULL);
376 r300render->vbo = pipe_buffer_create(screen,
377 64,
378 PIPE_BUFFER_USAGE_VERTEX,
379 R300_MAX_VBO_SIZE);
380 r300render->vbo_offset = 0;
381 r300render->vbo_size = R300_MAX_VBO_SIZE;
382 }
383
384 r300render->vertex_size = vertex_size;
385 r300->vbo = r300render->vbo;
386 r300->vbo_offset = r300render->vbo_offset;
387
388 return (r300render->vbo) ? TRUE : FALSE;
389 }
390
391 static void* r300_render_map_vertices(struct vbuf_render* render)
392 {
393 struct r300_render* r300render = r300_render(render);
394 struct pipe_screen* screen = r300render->r300->context.screen;
395
396 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
397 PIPE_BUFFER_USAGE_CPU_WRITE);
398
399 return (r300render->vbo_ptr + r300render->vbo_offset);
400 }
401
402 static void r300_render_unmap_vertices(struct vbuf_render* render,
403 ushort min,
404 ushort max)
405 {
406 struct r300_render* r300render = r300_render(render);
407 struct pipe_screen* screen = r300render->r300->context.screen;
408 CS_LOCALS(r300render->r300);
409 BEGIN_CS(2);
410 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
411 END_CS;
412
413 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
414 r300render->vertex_size * (max + 1));
415 pipe_buffer_unmap(screen, r300render->vbo);
416 }
417
418 static void r300_render_release_vertices(struct vbuf_render* render)
419 {
420 struct r300_render* r300render = r300_render(render);
421
422 r300render->vbo_offset += r300render->vbo_max_used;
423 r300render->vbo_max_used = 0;
424 }
425
426 static boolean r300_render_set_primitive(struct vbuf_render* render,
427 unsigned prim)
428 {
429 struct r300_render* r300render = r300_render(render);
430
431 r300render->prim = prim;
432 r300render->hwprim = r300_translate_primitive(prim);
433
434 return TRUE;
435 }
436
437 static void r300_render_draw_arrays(struct vbuf_render* render,
438 unsigned start,
439 unsigned count)
440 {
441 struct r300_render* r300render = r300_render(render);
442 struct r300_context* r300 = r300render->r300;
443
444 CS_LOCALS(r300);
445
446 r300_emit_dirty_state(r300);
447
448 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
449
450 BEGIN_CS(2);
451 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
452 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
453 r300render->hwprim);
454 END_CS;
455 }
456
457 static void r300_render_draw(struct vbuf_render* render,
458 const ushort* indices,
459 uint count)
460 {
461 struct r300_render* r300render = r300_render(render);
462 struct r300_context* r300 = r300render->r300;
463 int i;
464
465 CS_LOCALS(r300);
466
467 r300_emit_dirty_state(r300);
468
469 BEGIN_CS(2 + (count+1)/2);
470 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
471 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
472 r300render->hwprim);
473 for (i = 0; i < count-1; i += 2) {
474 OUT_CS(indices[i+1] << 16 | indices[i]);
475 }
476 if (count % 2) {
477 OUT_CS(indices[count-1]);
478 }
479 END_CS;
480 }
481
482 static void r300_render_destroy(struct vbuf_render* render)
483 {
484 FREE(render);
485 }
486
487 static struct vbuf_render* r300_render_create(struct r300_context* r300)
488 {
489 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
490
491 r300render->r300 = r300;
492
493 /* XXX find real numbers plz */
494 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
495 r300render->base.max_indices = 16 * 1024;
496
497 r300render->base.get_vertex_info = r300_render_get_vertex_info;
498 r300render->base.allocate_vertices = r300_render_allocate_vertices;
499 r300render->base.map_vertices = r300_render_map_vertices;
500 r300render->base.unmap_vertices = r300_render_unmap_vertices;
501 r300render->base.set_primitive = r300_render_set_primitive;
502 r300render->base.draw = r300_render_draw;
503 r300render->base.draw_arrays = r300_render_draw_arrays;
504 r300render->base.release_vertices = r300_render_release_vertices;
505 r300render->base.destroy = r300_render_destroy;
506
507 r300render->vbo = NULL;
508 r300render->vbo_size = 0;
509 r300render->vbo_offset = 0;
510
511 return &r300render->base;
512 }
513
514 struct draw_stage* r300_draw_stage(struct r300_context* r300)
515 {
516 struct vbuf_render* render;
517 struct draw_stage* stage;
518
519 render = r300_render_create(r300);
520
521 if (!render) {
522 return NULL;
523 }
524
525 stage = draw_vbuf_stage(r300->draw, render);
526
527 if (!stage) {
528 render->destroy(render);
529 return NULL;
530 }
531
532 draw_set_render(r300->draw, render);
533
534 return stage;
535 }