r300g: Fix up SW TCL rendering functions.
[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_vertex_attributes(r300);
187
188 setup_index_buffer(r300, indexBuffer, indexSize);
189
190 r300_emit_dirty_state(r300);
191
192 r300_emit_aos(r300, 0);
193
194 r300_emit_draw_elements(r300, indexBuffer, indexSize, minIndex, maxIndex,
195 mode, start, count);
196
197 return TRUE;
198 }
199
200 /* Simple helpers for context setup. Should probably be moved to util. */
201 boolean r300_draw_elements(struct pipe_context* pipe,
202 struct pipe_buffer* indexBuffer,
203 unsigned indexSize, unsigned mode,
204 unsigned start, unsigned count)
205 {
206 return pipe->draw_range_elements(pipe, indexBuffer, indexSize, 0, ~0,
207 mode, start, count);
208 }
209
210 boolean r300_draw_arrays(struct pipe_context* pipe, unsigned mode,
211 unsigned start, unsigned count)
212 {
213 struct r300_context* r300 = r300_context(pipe);
214
215 if (!u_trim_pipe_prim(mode, &count)) {
216 return FALSE;
217 }
218
219 if (count > 65535) {
220 return FALSE;
221 }
222
223 r300_update_derived_state(r300);
224
225 if (!r300_setup_vertex_buffers(r300)) {
226 return FALSE;
227 }
228
229 setup_vertex_attributes(r300);
230
231 r300_emit_dirty_state(r300);
232
233 r300_emit_aos(r300, start);
234
235 r300_emit_draw_arrays(r300, mode, count);
236
237 return TRUE;
238 }
239
240 /****************************************************************************
241 * The rest of this file is for SW TCL rendering only. Please be polite and *
242 * keep these functions separated so that they are easier to locate. ~C. *
243 ***************************************************************************/
244
245 /* SW TCL arrays, using Draw. */
246 boolean r300_swtcl_draw_arrays(struct pipe_context* pipe,
247 unsigned mode,
248 unsigned start,
249 unsigned count)
250 {
251 struct r300_context* r300 = r300_context(pipe);
252 int i;
253
254 if (!u_trim_pipe_prim(mode, &count)) {
255 return FALSE;
256 }
257
258 for (i = 0; i < r300->vertex_buffer_count; i++) {
259 void* buf = pipe_buffer_map(pipe->screen,
260 r300->vertex_buffer[i].buffer,
261 PIPE_BUFFER_USAGE_CPU_READ);
262 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
263 }
264
265 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
266
267 draw_set_mapped_constant_buffer(r300->draw,
268 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
269 r300->shader_constants[PIPE_SHADER_VERTEX].count *
270 (sizeof(float) * 4));
271
272 draw_arrays(r300->draw, mode, start, count);
273
274 for (i = 0; i < r300->vertex_buffer_count; i++) {
275 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
276 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
277 }
278
279 return TRUE;
280 }
281
282 /* SW TCL elements, using Draw. */
283 boolean r300_swtcl_draw_range_elements(struct pipe_context* pipe,
284 struct pipe_buffer* indexBuffer,
285 unsigned indexSize,
286 unsigned minIndex,
287 unsigned maxIndex,
288 unsigned mode,
289 unsigned start,
290 unsigned count)
291 {
292 struct r300_context* r300 = r300_context(pipe);
293 int i;
294
295 if (!u_trim_pipe_prim(mode, &count)) {
296 return FALSE;
297 }
298
299 for (i = 0; i < r300->vertex_buffer_count; i++) {
300 void* buf = pipe_buffer_map(pipe->screen,
301 r300->vertex_buffer[i].buffer,
302 PIPE_BUFFER_USAGE_CPU_READ);
303 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
304 }
305
306 void* indices = pipe_buffer_map(pipe->screen, indexBuffer,
307 PIPE_BUFFER_USAGE_CPU_READ);
308 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
309 minIndex, maxIndex, indices);
310
311 draw_set_mapped_constant_buffer(r300->draw,
312 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
313 r300->shader_constants[PIPE_SHADER_VERTEX].count *
314 (sizeof(float) * 4));
315
316 draw_arrays(r300->draw, mode, start, count);
317
318 for (i = 0; i < r300->vertex_buffer_count; i++) {
319 pipe_buffer_unmap(pipe->screen, r300->vertex_buffer[i].buffer);
320 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
321 }
322
323 pipe_buffer_unmap(pipe->screen, indexBuffer);
324 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
325 start + count - 1, NULL);
326
327 return TRUE;
328 }
329
330 /* Object for rendering using Draw. */
331 struct r300_render {
332 /* Parent class */
333 struct vbuf_render base;
334
335 /* Pipe context */
336 struct r300_context* r300;
337
338 /* Vertex information */
339 size_t vertex_size;
340 unsigned prim;
341 unsigned hwprim;
342
343 /* VBO */
344 struct pipe_buffer* vbo;
345 size_t vbo_size;
346 size_t vbo_offset;
347 size_t vbo_max_used;
348 void * vbo_ptr;
349 };
350
351 static INLINE struct r300_render*
352 r300_render(struct vbuf_render* render)
353 {
354 return (struct r300_render*)render;
355 }
356
357 static const struct vertex_info*
358 r300_render_get_vertex_info(struct vbuf_render* render)
359 {
360 struct r300_render* r300render = r300_render(render);
361 struct r300_context* r300 = r300render->r300;
362
363 r300_update_derived_state(r300);
364
365 return &r300->vertex_info->vinfo;
366 }
367
368 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
369 ushort vertex_size,
370 ushort count)
371 {
372 struct r300_render* r300render = r300_render(render);
373 struct r300_context* r300 = r300render->r300;
374 struct pipe_screen* screen = r300->context.screen;
375 size_t size = (size_t)vertex_size * (size_t)count;
376
377 if (size + r300render->vbo_offset > r300render->vbo_size)
378 {
379 pipe_buffer_reference(&r300->vbo, NULL);
380 r300render->vbo = pipe_buffer_create(screen,
381 64,
382 PIPE_BUFFER_USAGE_VERTEX,
383 R300_MAX_VBO_SIZE);
384 r300render->vbo_offset = 0;
385 r300render->vbo_size = R300_MAX_VBO_SIZE;
386 }
387
388 r300render->vertex_size = vertex_size;
389 r300->vbo = r300render->vbo;
390 r300->vbo_offset = r300render->vbo_offset;
391
392 return (r300render->vbo) ? TRUE : FALSE;
393 }
394
395 static void* r300_render_map_vertices(struct vbuf_render* render)
396 {
397 struct r300_render* r300render = r300_render(render);
398 struct pipe_screen* screen = r300render->r300->context.screen;
399
400 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
401 PIPE_BUFFER_USAGE_CPU_WRITE);
402
403 return (r300render->vbo_ptr + r300render->vbo_offset);
404 }
405
406 static void r300_render_unmap_vertices(struct vbuf_render* render,
407 ushort min,
408 ushort max)
409 {
410 struct r300_render* r300render = r300_render(render);
411 struct pipe_screen* screen = r300render->r300->context.screen;
412 CS_LOCALS(r300render->r300);
413 BEGIN_CS(2);
414 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
415 END_CS;
416
417 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
418 r300render->vertex_size * (max + 1));
419 pipe_buffer_unmap(screen, r300render->vbo);
420 }
421
422 static void r300_render_release_vertices(struct vbuf_render* render)
423 {
424 struct r300_render* r300render = r300_render(render);
425
426 r300render->vbo_offset += r300render->vbo_max_used;
427 r300render->vbo_max_used = 0;
428 }
429
430 static boolean r300_render_set_primitive(struct vbuf_render* render,
431 unsigned prim)
432 {
433 struct r300_render* r300render = r300_render(render);
434
435 r300render->prim = prim;
436 r300render->hwprim = r300_translate_primitive(prim);
437
438 return TRUE;
439 }
440
441 static void r300_render_draw_arrays(struct vbuf_render* render,
442 unsigned start,
443 unsigned count)
444 {
445 struct r300_render* r300render = r300_render(render);
446 struct r300_context* r300 = r300render->r300;
447
448 CS_LOCALS(r300);
449
450 r300_emit_dirty_state(r300);
451
452 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
453
454 BEGIN_CS(2);
455 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
456 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
457 r300render->hwprim);
458 END_CS;
459 }
460
461 static void r300_render_draw(struct vbuf_render* render,
462 const ushort* indices,
463 uint count)
464 {
465 struct r300_render* r300render = r300_render(render);
466 struct r300_context* r300 = r300render->r300;
467 int i;
468
469 CS_LOCALS(r300);
470
471 r300_emit_dirty_state(r300);
472
473 BEGIN_CS(2 + (count+1)/2);
474 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
475 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
476 r300render->hwprim);
477 for (i = 0; i < count-1; i += 2) {
478 OUT_CS(indices[i+1] << 16 | indices[i]);
479 }
480 if (count % 2) {
481 OUT_CS(indices[count-1]);
482 }
483 END_CS;
484 }
485
486 static void r300_render_destroy(struct vbuf_render* render)
487 {
488 FREE(render);
489 }
490
491 static struct vbuf_render* r300_render_create(struct r300_context* r300)
492 {
493 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
494
495 r300render->r300 = r300;
496
497 /* XXX find real numbers plz */
498 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
499 r300render->base.max_indices = 16 * 1024;
500
501 r300render->base.get_vertex_info = r300_render_get_vertex_info;
502 r300render->base.allocate_vertices = r300_render_allocate_vertices;
503 r300render->base.map_vertices = r300_render_map_vertices;
504 r300render->base.unmap_vertices = r300_render_unmap_vertices;
505 r300render->base.set_primitive = r300_render_set_primitive;
506 r300render->base.draw = r300_render_draw;
507 r300render->base.draw_arrays = r300_render_draw_arrays;
508 r300render->base.release_vertices = r300_render_release_vertices;
509 r300render->base.destroy = r300_render_destroy;
510
511 r300render->vbo = NULL;
512 r300render->vbo_size = 0;
513 r300render->vbo_offset = 0;
514
515 return &r300render->base;
516 }
517
518 struct draw_stage* r300_draw_stage(struct r300_context* r300)
519 {
520 struct vbuf_render* render;
521 struct draw_stage* stage;
522
523 render = r300_render_create(r300);
524
525 if (!render) {
526 return NULL;
527 }
528
529 stage = draw_vbuf_stage(r300->draw, render);
530
531 if (!stage) {
532 render->destroy(render);
533 return NULL;
534 }
535
536 draw_set_render(r300->draw, render);
537
538 return stage;
539 }