r300g: Don't pass hw_prim around in the 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 "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 assert(count < 65536);
79
80 BEGIN_CS(4);
81 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, count);
82 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
83 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
84 r300_translate_primitive(mode));
85 END_CS;
86 }
87
88 static void r300_emit_draw_elements(struct r300_context *r300,
89 struct pipe_buffer* indexBuffer,
90 unsigned indexSize,
91 unsigned minIndex,
92 unsigned maxIndex,
93 unsigned mode,
94 unsigned start,
95 unsigned count)
96 {
97 CS_LOCALS(r300);
98 assert(indexSize == 4 || indexSize == 2);
99 assert(count < 65536);
100 assert((start * indexSize) % 4 == 0);
101
102 uint32_t size_dwords;
103 uint32_t skip_dwords = indexSize * start / sizeof(uint32_t);
104 assert(skip_dwords == 0);
105
106 BEGIN_CS(10);
107 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, maxIndex);
108 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, 0);
109 if (indexSize == 4) {
110 size_dwords = count + start;
111 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
112 R300_VAP_VF_CNTL__INDEX_SIZE_32bit |
113 r300_translate_primitive(mode));
114 } else {
115 size_dwords = (count + start + 1) / 2;
116 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
117 r300_translate_primitive(mode));
118 }
119
120 OUT_CS_PKT3(R300_PACKET3_INDX_BUFFER, 2);
121 OUT_CS(R300_INDX_BUFFER_ONE_REG_WR | (R300_VAP_PORT_IDX0 >> 2) |
122 (0 << R300_INDX_BUFFER_SKIP_SHIFT));
123 OUT_CS(skip_dwords);
124 OUT_CS(size_dwords);
125 /* XXX hax */
126 cs_winsys->write_cs_reloc(cs_winsys,
127 indexBuffer,
128 RADEON_GEM_DOMAIN_GTT,
129 0,
130 0);
131 cs_count -= 2;
132
133 END_CS;
134 }
135
136
137 static boolean setup_vertex_buffers(struct r300_context *r300)
138 {
139 unsigned vbuf_count = r300->aos_count;
140 struct pipe_vertex_buffer *vbuf= r300->vertex_buffer;
141 struct pipe_vertex_element *velem= r300->vertex_element;
142 bool invalid = false;
143
144 validate:
145 for (int i = 0; i < vbuf_count; i++) {
146 if (!r300->winsys->add_buffer(r300->winsys, vbuf[velem[i].vertex_buffer_index].buffer,
147 RADEON_GEM_DOMAIN_GTT, 0)) {
148 r300->context.flush(&r300->context, 0, NULL);
149 goto validate;
150 }
151 }
152
153 if (!r300->winsys->validate(r300->winsys)) {
154 r300->context.flush(&r300->context, 0, NULL);
155 if (invalid) {
156 /* Well, hell. */
157 debug_printf("r300: Stuck in validation loop, gonna quit now.");
158 exit(1);
159 }
160 invalid = true;
161 goto validate;
162 }
163
164 return invalid;
165 }
166
167 /* This is the fast-path drawing & emission for HW TCL. */
168 boolean r300_draw_range_elements(struct pipe_context* pipe,
169 struct pipe_buffer* indexBuffer,
170 unsigned indexSize,
171 unsigned minIndex,
172 unsigned maxIndex,
173 unsigned mode,
174 unsigned start,
175 unsigned count)
176 {
177 struct r300_context* r300 = r300_context(pipe);
178
179 if (!u_trim_pipe_prim(mode, &count))
180 return false;
181
182 r300_update_derived_state(r300);
183
184 setup_vertex_buffers(r300);
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 r300_update_derived_state(r300);
219
220 setup_vertex_buffers(r300);
221
222 setup_vertex_attributes(r300);
223
224 r300_emit_dirty_state(r300);
225
226 r300_emit_aos(r300, start);
227
228 r300_emit_draw_arrays(r300, mode, count);
229
230 return TRUE;
231 }
232
233 /****************************************************************************
234 * The rest of this file is for SW TCL rendering only. Please be polite and *
235 * keep these functions separated so that they are easier to locate. ~C. *
236 ***************************************************************************/
237
238 /* Draw-based drawing for SW TCL chipsets. */
239 boolean r300_swtcl_draw_range_elements(struct pipe_context* pipe,
240 struct pipe_buffer* indexBuffer,
241 unsigned indexSize,
242 unsigned minIndex,
243 unsigned maxIndex,
244 unsigned mode,
245 unsigned start,
246 unsigned count)
247 {
248 assert(0);
249 #if 0
250 struct r300_context* r300 = r300_context(pipe);
251 int i;
252
253 if (!u_trim_pipe_prim(mode, &count)) {
254 return FALSE;
255 }
256
257 for (i = 0; i < r300->vertex_buffer_count; i++) {
258 void* buf = pipe_buffer_map(pipe->screen,
259 r300->vertex_buffers[i].buffer,
260 PIPE_BUFFER_USAGE_CPU_READ);
261 draw_set_mapped_vertex_buffer(r300->draw, i, buf);
262 }
263
264 if (indexBuffer) {
265 void* indices = pipe_buffer_map(pipe->screen, indexBuffer,
266 PIPE_BUFFER_USAGE_CPU_READ);
267 draw_set_mapped_element_buffer_range(r300->draw, indexSize,
268 minIndex, maxIndex, indices);
269 } else {
270 draw_set_mapped_element_buffer(r300->draw, 0, NULL);
271 }
272
273 draw_set_mapped_constant_buffer(r300->draw,
274 r300->shader_constants[PIPE_SHADER_VERTEX].constants,
275 r300->shader_constants[PIPE_SHADER_VERTEX].count *
276 (sizeof(float) * 4));
277
278 draw_arrays(r300->draw, mode, start, count);
279
280 for (i = 0; i < r300->vertex_buffer_count; i++) {
281 pipe_buffer_unmap(pipe->screen, r300->vertex_buffers[i].buffer);
282 draw_set_mapped_vertex_buffer(r300->draw, i, NULL);
283 }
284
285 if (indexBuffer) {
286 pipe_buffer_unmap(pipe->screen, indexBuffer);
287 draw_set_mapped_element_buffer_range(r300->draw, 0, start,
288 start + count - 1, NULL);
289 }
290 #endif
291 return TRUE;
292 }
293
294 /* Object for rendering using Draw. */
295 struct r300_render {
296 /* Parent class */
297 struct vbuf_render base;
298
299 /* Pipe context */
300 struct r300_context* r300;
301
302 /* Vertex information */
303 size_t vertex_size;
304 unsigned prim;
305 unsigned hwprim;
306
307 /* VBO */
308 struct pipe_buffer* vbo;
309 size_t vbo_size;
310 size_t vbo_offset;
311 size_t vbo_max_used;
312 void * vbo_ptr;
313 };
314
315 static INLINE struct r300_render*
316 r300_render(struct vbuf_render* render)
317 {
318 return (struct r300_render*)render;
319 }
320
321 static const struct vertex_info*
322 r300_render_get_vertex_info(struct vbuf_render* render)
323 {
324 struct r300_render* r300render = r300_render(render);
325 struct r300_context* r300 = r300render->r300;
326
327 r300_update_derived_state(r300);
328
329 return &r300->vertex_info->vinfo;
330 }
331
332 static boolean r300_render_allocate_vertices(struct vbuf_render* render,
333 ushort vertex_size,
334 ushort count)
335 {
336 struct r300_render* r300render = r300_render(render);
337 struct r300_context* r300 = r300render->r300;
338 struct pipe_screen* screen = r300->context.screen;
339 size_t size = (size_t)vertex_size * (size_t)count;
340
341 if (size + r300render->vbo_offset > r300render->vbo_size)
342 {
343 pipe_buffer_reference(&r300->vbo, NULL);
344 r300render->vbo = pipe_buffer_create(screen,
345 64,
346 PIPE_BUFFER_USAGE_VERTEX,
347 R300_MAX_VBO_SIZE);
348 r300render->vbo_offset = 0;
349 r300render->vbo_size = R300_MAX_VBO_SIZE;
350 }
351
352 r300render->vertex_size = vertex_size;
353 r300->vbo = r300render->vbo;
354 r300->vbo_offset = r300render->vbo_offset;
355
356 return (r300render->vbo) ? TRUE : FALSE;
357 }
358
359 static void* r300_render_map_vertices(struct vbuf_render* render)
360 {
361 struct r300_render* r300render = r300_render(render);
362 struct pipe_screen* screen = r300render->r300->context.screen;
363
364 r300render->vbo_ptr = pipe_buffer_map(screen, r300render->vbo,
365 PIPE_BUFFER_USAGE_CPU_WRITE);
366
367 return (r300render->vbo_ptr + r300render->vbo_offset);
368 }
369
370 static void r300_render_unmap_vertices(struct vbuf_render* render,
371 ushort min,
372 ushort max)
373 {
374 struct r300_render* r300render = r300_render(render);
375 struct pipe_screen* screen = r300render->r300->context.screen;
376 CS_LOCALS(r300render->r300);
377 BEGIN_CS(2);
378 OUT_CS_REG(R300_VAP_VF_MAX_VTX_INDX, max);
379 END_CS;
380
381 r300render->vbo_max_used = MAX2(r300render->vbo_max_used,
382 r300render->vertex_size * (max + 1));
383 pipe_buffer_unmap(screen, r300render->vbo);
384 }
385
386 static void r300_render_release_vertices(struct vbuf_render* render)
387 {
388 struct r300_render* r300render = r300_render(render);
389
390 r300render->vbo_offset += r300render->vbo_max_used;
391 r300render->vbo_max_used = 0;
392 }
393
394 static boolean r300_render_set_primitive(struct vbuf_render* render,
395 unsigned prim)
396 {
397 struct r300_render* r300render = r300_render(render);
398
399 r300render->prim = prim;
400 r300render->hwprim = r300_translate_primitive(prim);
401
402 return TRUE;
403 }
404
405 static void r300_render_draw_arrays(struct vbuf_render* render,
406 unsigned start,
407 unsigned count)
408 {
409 struct r300_render* r300render = r300_render(render);
410 struct r300_context* r300 = r300render->r300;
411
412 CS_LOCALS(r300);
413
414 r300_emit_dirty_state(r300);
415
416 DBG(r300, DBG_DRAW, "r300: Doing vbuf render, count %d\n", count);
417
418 BEGIN_CS(2);
419 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
420 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (count << 16) |
421 r300render->hwprim);
422 END_CS;
423 }
424
425 static void r300_render_draw(struct vbuf_render* render,
426 const ushort* indices,
427 uint count)
428 {
429 struct r300_render* r300render = r300_render(render);
430 struct r300_context* r300 = r300render->r300;
431 int i;
432
433 CS_LOCALS(r300);
434
435 r300_emit_dirty_state(r300);
436
437 BEGIN_CS(2 + (count+1)/2);
438 OUT_CS_PKT3(R300_PACKET3_3D_DRAW_INDX_2, (count+1)/2);
439 OUT_CS(R300_VAP_VF_CNTL__PRIM_WALK_INDICES | (count << 16) |
440 r300render->hwprim);
441 for (i = 0; i < count-1; i += 2) {
442 OUT_CS(indices[i+1] << 16 | indices[i]);
443 }
444 if (count % 2) {
445 OUT_CS(indices[count-1]);
446 }
447 END_CS;
448 }
449
450 static void r300_render_destroy(struct vbuf_render* render)
451 {
452 FREE(render);
453 }
454
455 static struct vbuf_render* r300_render_create(struct r300_context* r300)
456 {
457 struct r300_render* r300render = CALLOC_STRUCT(r300_render);
458
459 r300render->r300 = r300;
460
461 /* XXX find real numbers plz */
462 r300render->base.max_vertex_buffer_bytes = 128 * 1024;
463 r300render->base.max_indices = 16 * 1024;
464
465 r300render->base.get_vertex_info = r300_render_get_vertex_info;
466 r300render->base.allocate_vertices = r300_render_allocate_vertices;
467 r300render->base.map_vertices = r300_render_map_vertices;
468 r300render->base.unmap_vertices = r300_render_unmap_vertices;
469 r300render->base.set_primitive = r300_render_set_primitive;
470 r300render->base.draw = r300_render_draw;
471 r300render->base.draw_arrays = r300_render_draw_arrays;
472 r300render->base.release_vertices = r300_render_release_vertices;
473 r300render->base.destroy = r300_render_destroy;
474
475 r300render->vbo = NULL;
476 r300render->vbo_size = 0;
477 r300render->vbo_offset = 0;
478
479 return &r300render->base;
480 }
481
482 struct draw_stage* r300_draw_stage(struct r300_context* r300)
483 {
484 struct vbuf_render* render;
485 struct draw_stage* stage;
486
487 render = r300_render_create(r300);
488
489 if (!render) {
490 return NULL;
491 }
492
493 stage = draw_vbuf_stage(r300->draw, render);
494
495 if (!stage) {
496 render->destroy(render);
497 return NULL;
498 }
499
500 draw_set_render(r300->draw, render);
501
502 return stage;
503 }