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