draw: Add draw_set_index_buffer and others.
[mesa.git] / src / gallium / auxiliary / draw / draw_pt.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 */
32
33 #include "draw/draw_context.h"
34 #include "draw/draw_gs.h"
35 #include "draw/draw_private.h"
36 #include "draw/draw_pt.h"
37 #include "draw/draw_vs.h"
38 #include "tgsi/tgsi_dump.h"
39 #include "util/u_math.h"
40 #include "util/u_prim.h"
41 #include "util/u_format.h"
42 #include "util/u_draw.h"
43
44
45 DEBUG_GET_ONCE_BOOL_OPTION(draw_fse, "DRAW_FSE", FALSE)
46 DEBUG_GET_ONCE_BOOL_OPTION(draw_no_fse, "DRAW_NO_FSE", FALSE)
47
48 /* Overall we split things into:
49 * - frontend -- prepare fetch_elts, draw_elts - eg vsplit
50 * - middle -- fetch, shade, cliptest, viewport
51 * - pipeline -- the prim pipeline: clipping, wide lines, etc
52 * - backend -- the vbuf_render provided by the driver.
53 */
54 static boolean
55 draw_pt_arrays(struct draw_context *draw,
56 unsigned prim,
57 unsigned start,
58 unsigned count)
59 {
60 struct draw_pt_front_end *frontend = NULL;
61 struct draw_pt_middle_end *middle = NULL;
62 unsigned opt = 0;
63
64 /* Sanitize primitive length:
65 */
66 {
67 unsigned first, incr;
68 draw_pt_split_prim(prim, &first, &incr);
69 count = draw_pt_trim_count(count, first, incr);
70 if (count < first)
71 return TRUE;
72 }
73
74 if (!draw->force_passthrough) {
75 unsigned gs_out_prim = (draw->gs.geometry_shader ?
76 draw->gs.geometry_shader->output_primitive :
77 prim);
78
79 if (!draw->render) {
80 opt |= PT_PIPELINE;
81 }
82
83 if (draw_need_pipeline(draw,
84 draw->rasterizer,
85 gs_out_prim)) {
86 opt |= PT_PIPELINE;
87 }
88
89 if (!draw->bypass_clipping && !draw->pt.test_fse) {
90 opt |= PT_CLIPTEST;
91 }
92
93 opt |= PT_SHADE;
94 }
95
96 if (draw->pt.middle.llvm) {
97 middle = draw->pt.middle.llvm;
98 } else {
99 if (opt == 0)
100 middle = draw->pt.middle.fetch_emit;
101 else if (opt == PT_SHADE && !draw->pt.no_fse)
102 middle = draw->pt.middle.fetch_shade_emit;
103 else
104 middle = draw->pt.middle.general;
105 }
106
107 frontend = draw->pt.front.vsplit;
108
109 frontend->prepare( frontend, prim, middle, opt );
110
111 frontend->run(frontend, start, count);
112
113 frontend->finish( frontend );
114
115 return TRUE;
116 }
117
118
119 boolean draw_pt_init( struct draw_context *draw )
120 {
121 draw->pt.test_fse = debug_get_option_draw_fse();
122 draw->pt.no_fse = debug_get_option_draw_no_fse();
123
124 draw->pt.front.vsplit = draw_pt_vsplit(draw);
125 if (!draw->pt.front.vsplit)
126 return FALSE;
127
128 draw->pt.middle.fetch_emit = draw_pt_fetch_emit( draw );
129 if (!draw->pt.middle.fetch_emit)
130 return FALSE;
131
132 draw->pt.middle.fetch_shade_emit = draw_pt_middle_fse( draw );
133 if (!draw->pt.middle.fetch_shade_emit)
134 return FALSE;
135
136 draw->pt.middle.general = draw_pt_fetch_pipeline_or_emit( draw );
137 if (!draw->pt.middle.general)
138 return FALSE;
139
140 #if HAVE_LLVM
141 if (draw->llvm)
142 draw->pt.middle.llvm = draw_pt_fetch_pipeline_or_emit_llvm( draw );
143 #endif
144
145 return TRUE;
146 }
147
148
149 void draw_pt_destroy( struct draw_context *draw )
150 {
151 if (draw->pt.middle.llvm) {
152 draw->pt.middle.llvm->destroy( draw->pt.middle.llvm );
153 draw->pt.middle.llvm = NULL;
154 }
155
156 if (draw->pt.middle.general) {
157 draw->pt.middle.general->destroy( draw->pt.middle.general );
158 draw->pt.middle.general = NULL;
159 }
160
161 if (draw->pt.middle.fetch_emit) {
162 draw->pt.middle.fetch_emit->destroy( draw->pt.middle.fetch_emit );
163 draw->pt.middle.fetch_emit = NULL;
164 }
165
166 if (draw->pt.middle.fetch_shade_emit) {
167 draw->pt.middle.fetch_shade_emit->destroy( draw->pt.middle.fetch_shade_emit );
168 draw->pt.middle.fetch_shade_emit = NULL;
169 }
170
171 if (draw->pt.front.vsplit) {
172 draw->pt.front.vsplit->destroy( draw->pt.front.vsplit );
173 draw->pt.front.vsplit = NULL;
174 }
175 }
176
177
178 /**
179 * Debug- print the first 'count' vertices.
180 */
181 static void
182 draw_print_arrays(struct draw_context *draw, uint prim, int start, uint count)
183 {
184 uint i;
185
186 debug_printf("Draw arrays(prim = %u, start = %u, count = %u)\n",
187 prim, start, count);
188
189 for (i = 0; i < count; i++) {
190 uint ii = 0;
191 uint j;
192
193 if (draw->pt.user.eltSize) {
194 const char *elts;
195
196 /* indexed arrays */
197 elts = (const char *) draw->pt.user.elts;
198 elts += draw->pt.index_buffer.offset;
199
200 switch (draw->pt.user.eltSize) {
201 case 1:
202 {
203 const ubyte *elem = (const ubyte *) elts;
204 ii = elem[start + i];
205 }
206 break;
207 case 2:
208 {
209 const ushort *elem = (const ushort *) elts;
210 ii = elem[start + i];
211 }
212 break;
213 case 4:
214 {
215 const uint *elem = (const uint *) elts;
216 ii = elem[start + i];
217 }
218 break;
219 default:
220 assert(0);
221 return;
222 }
223 ii += draw->pt.user.eltBias;
224 debug_printf("Element[%u + %u] + %i -> Vertex %u:\n", start, i,
225 draw->pt.user.eltBias, ii);
226 }
227 else {
228 /* non-indexed arrays */
229 ii = start + i;
230 debug_printf("Vertex %u:\n", ii);
231 }
232
233 for (j = 0; j < draw->pt.nr_vertex_elements; j++) {
234 uint buf = draw->pt.vertex_element[j].vertex_buffer_index;
235 ubyte *ptr = (ubyte *) draw->pt.user.vbuffer[buf];
236
237 if (draw->pt.vertex_element[j].instance_divisor) {
238 ii = draw->instance_id / draw->pt.vertex_element[j].instance_divisor;
239 }
240
241 ptr += draw->pt.vertex_buffer[buf].buffer_offset;
242 ptr += draw->pt.vertex_buffer[buf].stride * ii;
243 ptr += draw->pt.vertex_element[j].src_offset;
244
245 debug_printf(" Attr %u: ", j);
246 switch (draw->pt.vertex_element[j].src_format) {
247 case PIPE_FORMAT_R32_FLOAT:
248 {
249 float *v = (float *) ptr;
250 debug_printf("R %f @ %p\n", v[0], (void *) v);
251 }
252 break;
253 case PIPE_FORMAT_R32G32_FLOAT:
254 {
255 float *v = (float *) ptr;
256 debug_printf("RG %f %f @ %p\n", v[0], v[1], (void *) v);
257 }
258 break;
259 case PIPE_FORMAT_R32G32B32_FLOAT:
260 {
261 float *v = (float *) ptr;
262 debug_printf("RGB %f %f %f @ %p\n", v[0], v[1], v[2], (void *) v);
263 }
264 break;
265 case PIPE_FORMAT_R32G32B32A32_FLOAT:
266 {
267 float *v = (float *) ptr;
268 debug_printf("RGBA %f %f %f %f @ %p\n", v[0], v[1], v[2], v[3],
269 (void *) v);
270 }
271 break;
272 case PIPE_FORMAT_B8G8R8A8_UNORM:
273 {
274 ubyte *u = (ubyte *) ptr;
275 debug_printf("BGRA %d %d %d %d @ %p\n", u[0], u[1], u[2], u[3],
276 (void *) u);
277 }
278 break;
279 default:
280 debug_printf("other format %s (fix me)\n",
281 util_format_name(draw->pt.vertex_element[j].src_format));
282 }
283 }
284 }
285 }
286
287
288 /**
289 * Non-instanced drawing.
290 * \sa draw_arrays_instanced
291 */
292 void
293 draw_arrays(struct draw_context *draw, unsigned prim,
294 unsigned start, unsigned count)
295 {
296 draw_arrays_instanced(draw, prim, start, count, 0, 1);
297 }
298
299
300 /**
301 * Instanced drawing.
302 * draw_set_mapped_element_buffer must be called before calling this function.
303 * \sa draw_vbo
304 */
305 void
306 draw_arrays_instanced(struct draw_context *draw,
307 unsigned mode,
308 unsigned start,
309 unsigned count,
310 unsigned startInstance,
311 unsigned instanceCount)
312 {
313 struct pipe_draw_info info;
314
315 util_draw_init_info(&info);
316
317 info.mode = mode;
318 info.start = start;
319 info.count = count;
320 info.start_instance = startInstance;
321 info.instance_count = instanceCount;
322
323 info.indexed = (draw->pt.user.elts != NULL);
324 info.index_bias = draw->pt.user.eltBias;
325 info.min_index = draw->pt.user.min_index;
326 info.max_index = draw->pt.user.max_index;
327
328 draw_vbo(draw, &info);
329 }
330
331
332 /**
333 * Draw vertex arrays.
334 * This is the main entrypoint into the drawing module. If drawing an indexed
335 * primitive, the draw_set_index_buffer() and draw_set_mapped_index_buffer()
336 * functions should have already been called to specify the element/index
337 * buffer information.
338 */
339 void
340 draw_vbo(struct draw_context *draw,
341 const struct pipe_draw_info *info)
342 {
343 unsigned reduced_prim = u_reduced_prim(info->mode);
344 unsigned instance;
345
346 assert(info->instance_count > 0);
347 if (info->indexed)
348 assert(draw->pt.user.elts);
349
350 draw->pt.user.eltSize =
351 (info->indexed) ? draw->pt.index_buffer.index_size : 0;
352
353 draw->pt.user.eltBias = info->index_bias;
354 draw->pt.user.min_index = info->min_index;
355 draw->pt.user.max_index = info->max_index;
356
357 if (reduced_prim != draw->reduced_prim) {
358 draw_do_flush(draw, DRAW_FLUSH_STATE_CHANGE);
359 draw->reduced_prim = reduced_prim;
360 }
361
362 if (0)
363 debug_printf("draw_vbo(mode=%u start=%u count=%u):\n",
364 info->mode, info->start, info->count);
365
366 if (0)
367 tgsi_dump(draw->vs.vertex_shader->state.tokens, 0);
368
369 if (0) {
370 unsigned int i;
371 debug_printf("Elements:\n");
372 for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
373 debug_printf(" %u: src_offset=%u inst_div=%u vbuf=%u format=%s\n",
374 i,
375 draw->pt.vertex_element[i].src_offset,
376 draw->pt.vertex_element[i].instance_divisor,
377 draw->pt.vertex_element[i].vertex_buffer_index,
378 util_format_name(draw->pt.vertex_element[i].src_format));
379 }
380 debug_printf("Buffers:\n");
381 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
382 debug_printf(" %u: stride=%u maxindex=%u offset=%u ptr=%p\n",
383 i,
384 draw->pt.vertex_buffer[i].stride,
385 draw->pt.vertex_buffer[i].max_index,
386 draw->pt.vertex_buffer[i].buffer_offset,
387 draw->pt.user.vbuffer[i]);
388 }
389 }
390
391 if (0)
392 draw_print_arrays(draw, info->mode, info->start, MIN2(info->count, 20));
393
394 for (instance = 0; instance < info->instance_count; instance++) {
395 draw->instance_id = instance + info->start_instance;
396 draw_pt_arrays(draw, info->mode, info->start, info->count);
397 }
398 }