gallium: Use draw_set_index_buffer and others.
[mesa.git] / src / gallium / auxiliary / draw / draw_context.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
34 #include "pipe/p_context.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
37 #include "util/u_cpu_detect.h"
38 #include "draw_context.h"
39 #include "draw_vs.h"
40 #include "draw_gs.h"
41
42 #if HAVE_LLVM
43 #include "gallivm/lp_bld_init.h"
44 #include "draw_llvm.h"
45
46 static boolean
47 draw_get_option_use_llvm(void)
48 {
49 static boolean first = TRUE;
50 static boolean value;
51 if (first) {
52 first = FALSE;
53 value = debug_get_bool_option("DRAW_USE_LLVM", TRUE);
54
55 #ifdef PIPE_ARCH_X86
56 util_cpu_detect();
57 /* require SSE2 due to LLVM PR6960. */
58 if (!util_cpu_caps.has_sse2)
59 value = FALSE;
60 #endif
61 }
62 return value;
63 }
64 #endif
65
66 struct draw_context *draw_create( struct pipe_context *pipe )
67 {
68 struct draw_context *draw = CALLOC_STRUCT( draw_context );
69 if (draw == NULL)
70 goto fail;
71
72 #if HAVE_LLVM
73 if(draw_get_option_use_llvm())
74 {
75 lp_build_init();
76 assert(lp_build_engine);
77 draw->engine = lp_build_engine;
78 draw->llvm = draw_llvm_create(draw);
79 }
80 #endif
81
82 if (!draw_init(draw))
83 goto fail;
84
85 draw->pipe = pipe;
86
87 return draw;
88
89 fail:
90 draw_destroy( draw );
91 return NULL;
92 }
93
94 boolean draw_init(struct draw_context *draw)
95 {
96 /*
97 * Note that several functions compute the clipmask of the predefined
98 * formats with hardcoded formulas instead of using these. So modifications
99 * here must be reflected there too.
100 */
101
102 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
103 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
104 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
105 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
106 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
107 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
108 draw->nr_planes = 6;
109
110
111 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */
112
113
114 if (!draw_pipeline_init( draw ))
115 return FALSE;
116
117 if (!draw_pt_init( draw ))
118 return FALSE;
119
120 if (!draw_vs_init( draw ))
121 return FALSE;
122
123 if (!draw_gs_init( draw ))
124 return FALSE;
125
126 return TRUE;
127 }
128
129
130 void draw_destroy( struct draw_context *draw )
131 {
132 struct pipe_context *pipe;
133 int i, j;
134
135 if (!draw)
136 return;
137
138 pipe = draw->pipe;
139
140 /* free any rasterizer CSOs that we may have created.
141 */
142 for (i = 0; i < 2; i++) {
143 for (j = 0; j < 2; j++) {
144 if (draw->rasterizer_no_cull[i][j]) {
145 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
146 }
147 }
148 }
149
150 /* Not so fast -- we're just borrowing this at the moment.
151 *
152 if (draw->render)
153 draw->render->destroy( draw->render );
154 */
155
156 draw_pipeline_destroy( draw );
157 draw_pt_destroy( draw );
158 draw_vs_destroy( draw );
159 draw_gs_destroy( draw );
160 #ifdef HAVE_LLVM
161 if(draw->llvm)
162 draw_llvm_destroy( draw->llvm );
163 #endif
164
165 FREE( draw );
166 }
167
168
169
170 void draw_flush( struct draw_context *draw )
171 {
172 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
173 }
174
175
176 /**
177 * Specify the Minimum Resolvable Depth factor for polygon offset.
178 * This factor potentially depends on the number of Z buffer bits,
179 * the rasterization algorithm and the arithmetic performed on Z
180 * values between vertex shading and rasterization. It will vary
181 * from one driver to another.
182 */
183 void draw_set_mrd(struct draw_context *draw, double mrd)
184 {
185 draw->mrd = mrd;
186 }
187
188
189 /**
190 * Register new primitive rasterization/rendering state.
191 * This causes the drawing pipeline to be rebuilt.
192 */
193 void draw_set_rasterizer_state( struct draw_context *draw,
194 const struct pipe_rasterizer_state *raster,
195 void *rast_handle )
196 {
197 if (!draw->suspend_flushing) {
198 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
199
200 draw->rasterizer = raster;
201 draw->rast_handle = rast_handle;
202
203 draw->bypass_clipping = draw->driver.bypass_clipping;
204 }
205 }
206
207
208 void draw_set_driver_clipping( struct draw_context *draw,
209 boolean bypass_clipping )
210 {
211 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
212
213 draw->driver.bypass_clipping = bypass_clipping;
214 draw->bypass_clipping = draw->driver.bypass_clipping;
215 }
216
217
218 /**
219 * Plug in the primitive rendering/rasterization stage (which is the last
220 * stage in the drawing pipeline).
221 * This is provided by the device driver.
222 */
223 void draw_set_rasterize_stage( struct draw_context *draw,
224 struct draw_stage *stage )
225 {
226 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
227
228 draw->pipeline.rasterize = stage;
229 }
230
231
232 /**
233 * Set the draw module's clipping state.
234 */
235 void draw_set_clip_state( struct draw_context *draw,
236 const struct pipe_clip_state *clip )
237 {
238 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
239
240 assert(clip->nr <= PIPE_MAX_CLIP_PLANES);
241 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0]));
242 draw->nr_planes = 6 + clip->nr;
243 draw->depth_clamp = clip->depth_clamp;
244 }
245
246
247 /**
248 * Set the draw module's viewport state.
249 */
250 void draw_set_viewport_state( struct draw_context *draw,
251 const struct pipe_viewport_state *viewport )
252 {
253 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
254 draw->viewport = *viewport; /* struct copy */
255 draw->identity_viewport = (viewport->scale[0] == 1.0f &&
256 viewport->scale[1] == 1.0f &&
257 viewport->scale[2] == 1.0f &&
258 viewport->scale[3] == 1.0f &&
259 viewport->translate[0] == 0.0f &&
260 viewport->translate[1] == 0.0f &&
261 viewport->translate[2] == 0.0f &&
262 viewport->translate[3] == 0.0f);
263
264 draw_vs_set_viewport( draw, viewport );
265 }
266
267
268
269 void
270 draw_set_vertex_buffers(struct draw_context *draw,
271 unsigned count,
272 const struct pipe_vertex_buffer *buffers)
273 {
274 assert(count <= PIPE_MAX_ATTRIBS);
275
276 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0]));
277 draw->pt.nr_vertex_buffers = count;
278 }
279
280
281 void
282 draw_set_vertex_elements(struct draw_context *draw,
283 unsigned count,
284 const struct pipe_vertex_element *elements)
285 {
286 assert(count <= PIPE_MAX_ATTRIBS);
287
288 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
289 draw->pt.nr_vertex_elements = count;
290 }
291
292
293 /**
294 * Tell drawing context where to find mapped vertex buffers.
295 */
296 void
297 draw_set_mapped_vertex_buffer(struct draw_context *draw,
298 unsigned attr, const void *buffer)
299 {
300 draw->pt.user.vbuffer[attr] = buffer;
301 }
302
303
304 void
305 draw_set_mapped_constant_buffer(struct draw_context *draw,
306 unsigned shader_type,
307 unsigned slot,
308 const void *buffer,
309 unsigned size )
310 {
311 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
312 shader_type == PIPE_SHADER_GEOMETRY);
313 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
314
315 switch (shader_type) {
316 case PIPE_SHADER_VERTEX:
317 draw->pt.user.vs_constants[slot] = buffer;
318 draw->pt.user.vs_constants_size[slot] = size;
319 draw_vs_set_constants(draw, slot, buffer, size);
320 break;
321 case PIPE_SHADER_GEOMETRY:
322 draw->pt.user.gs_constants[slot] = buffer;
323 draw->pt.user.gs_constants_size[slot] = size;
324 draw_gs_set_constants(draw, slot, buffer, size);
325 break;
326 default:
327 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
328 }
329 }
330
331
332 /**
333 * Tells the draw module to draw points with triangles if their size
334 * is greater than this threshold.
335 */
336 void
337 draw_wide_point_threshold(struct draw_context *draw, float threshold)
338 {
339 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
340 draw->pipeline.wide_point_threshold = threshold;
341 }
342
343
344 /**
345 * Should the draw module handle point->quad conversion for drawing sprites?
346 */
347 void
348 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
349 {
350 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
351 draw->pipeline.wide_point_sprites = draw_sprite;
352 }
353
354
355 /**
356 * Tells the draw module to draw lines with triangles if their width
357 * is greater than this threshold.
358 */
359 void
360 draw_wide_line_threshold(struct draw_context *draw, float threshold)
361 {
362 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
363 draw->pipeline.wide_line_threshold = threshold;
364 }
365
366
367 /**
368 * Tells the draw module whether or not to implement line stipple.
369 */
370 void
371 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
372 {
373 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
374 draw->pipeline.line_stipple = enable;
375 }
376
377
378 /**
379 * Tells draw module whether to convert points to quads for sprite mode.
380 */
381 void
382 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
383 {
384 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
385 draw->pipeline.point_sprite = enable;
386 }
387
388
389 void
390 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
391 {
392 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
393 draw->force_passthrough = enable;
394 }
395
396
397 /**
398 * Ask the draw module for the location/slot of the given vertex attribute in
399 * a post-transformed vertex.
400 *
401 * With this function, drivers that use the draw module should have no reason
402 * to track the current vertex/geometry shader.
403 *
404 * Note that the draw module may sometimes generate vertices with extra
405 * attributes (such as texcoords for AA lines). The driver can call this
406 * function to find those attributes.
407 *
408 * Zero is returned if the attribute is not found since this is
409 * a don't care / undefined situtation. Returning -1 would be a bit more
410 * work for the drivers.
411 */
412 int
413 draw_find_shader_output(const struct draw_context *draw,
414 uint semantic_name, uint semantic_index)
415 {
416 const struct draw_vertex_shader *vs = draw->vs.vertex_shader;
417 const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
418 uint i;
419 const struct tgsi_shader_info *info = &vs->info;
420
421 if (gs)
422 info = &gs->info;
423
424 for (i = 0; i < info->num_outputs; i++) {
425 if (info->output_semantic_name[i] == semantic_name &&
426 info->output_semantic_index[i] == semantic_index)
427 return i;
428 }
429
430 /* XXX there may be more than one extra vertex attrib.
431 * For example, simulated gl_FragCoord and gl_PointCoord.
432 */
433 if (draw->extra_shader_outputs.semantic_name == semantic_name &&
434 draw->extra_shader_outputs.semantic_index == semantic_index) {
435 return draw->extra_shader_outputs.slot;
436 }
437
438 return 0;
439 }
440
441
442 /**
443 * Return total number of the shader outputs. This function is similar to
444 * draw_current_shader_outputs() but this function also counts any extra
445 * vertex/geometry output attributes that may be filled in by some draw
446 * stages (such as AA point, AA line).
447 *
448 * If geometry shader is present, its output will be returned,
449 * if not vertex shader is used.
450 */
451 uint
452 draw_num_shader_outputs(const struct draw_context *draw)
453 {
454 uint count = draw->vs.vertex_shader->info.num_outputs;
455
456 /* If a geometry shader is present, its outputs go to the
457 * driver, else the vertex shader's outputs.
458 */
459 if (draw->gs.geometry_shader)
460 count = draw->gs.geometry_shader->info.num_outputs;
461
462 if (draw->extra_shader_outputs.slot > 0)
463 count++;
464 return count;
465 }
466
467
468 /**
469 * Provide TGSI sampler objects for vertex/geometry shaders that use
470 * texture fetches.
471 * This might only be used by software drivers for the time being.
472 */
473 void
474 draw_texture_samplers(struct draw_context *draw,
475 uint shader,
476 uint num_samplers,
477 struct tgsi_sampler **samplers)
478 {
479 if (shader == PIPE_SHADER_VERTEX) {
480 draw->vs.num_samplers = num_samplers;
481 draw->vs.samplers = samplers;
482 } else {
483 debug_assert(shader == PIPE_SHADER_GEOMETRY);
484 draw->gs.num_samplers = num_samplers;
485 draw->gs.samplers = samplers;
486 }
487 }
488
489
490
491
492 void draw_set_render( struct draw_context *draw,
493 struct vbuf_render *render )
494 {
495 draw->render = render;
496 }
497
498
499 void
500 draw_set_index_buffer(struct draw_context *draw,
501 const struct pipe_index_buffer *ib)
502 {
503 if (ib)
504 memcpy(&draw->pt.index_buffer, ib, sizeof(draw->pt.index_buffer));
505 else
506 memset(&draw->pt.index_buffer, 0, sizeof(draw->pt.index_buffer));
507 }
508
509
510 /**
511 * Tell drawing context where to find mapped index/element buffer.
512 */
513 void
514 draw_set_mapped_index_buffer(struct draw_context *draw,
515 const void *elements)
516 {
517 draw->pt.user.elts = elements;
518 }
519
520
521 /* Revamp me please:
522 */
523 void draw_do_flush( struct draw_context *draw, unsigned flags )
524 {
525 if (!draw->suspend_flushing)
526 {
527 assert(!draw->flushing); /* catch inadvertant recursion */
528
529 draw->flushing = TRUE;
530
531 draw_pipeline_flush( draw, flags );
532
533 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */
534
535 draw->flushing = FALSE;
536 }
537 }
538
539
540 /**
541 * Return the number of output attributes produced by the geometry
542 * shader, if present. If no geometry shader, return the number of
543 * outputs from the vertex shader.
544 * \sa draw_num_shader_outputs
545 */
546 uint
547 draw_current_shader_outputs(const struct draw_context *draw)
548 {
549 if (draw->gs.geometry_shader)
550 return draw->gs.num_gs_outputs;
551 return draw->vs.num_vs_outputs;
552 }
553
554
555 /**
556 * Return the index of the shader output which will contain the
557 * vertex position.
558 */
559 uint
560 draw_current_shader_position_output(const struct draw_context *draw)
561 {
562 if (draw->gs.geometry_shader)
563 return draw->gs.position_output;
564 return draw->vs.position_output;
565 }
566
567
568 /**
569 * Return a pointer/handle for a driver/CSO rasterizer object which
570 * disabled culling, stippling, unfilled tris, etc.
571 * This is used by some pipeline stages (such as wide_point, aa_line
572 * and aa_point) which convert points/lines into triangles. In those
573 * cases we don't want to accidentally cull the triangles.
574 *
575 * \param scissor should the rasterizer state enable scissoring?
576 * \param flatshade should the rasterizer state use flat shading?
577 * \return rasterizer CSO handle
578 */
579 void *
580 draw_get_rasterizer_no_cull( struct draw_context *draw,
581 boolean scissor,
582 boolean flatshade )
583 {
584 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
585 /* create now */
586 struct pipe_context *pipe = draw->pipe;
587 struct pipe_rasterizer_state rast;
588
589 memset(&rast, 0, sizeof(rast));
590 rast.scissor = scissor;
591 rast.flatshade = flatshade;
592 rast.front_ccw = 1;
593 rast.gl_rasterization_rules = draw->rasterizer->gl_rasterization_rules;
594
595 draw->rasterizer_no_cull[scissor][flatshade] =
596 pipe->create_rasterizer_state(pipe, &rast);
597 }
598 return draw->rasterizer_no_cull[scissor][flatshade];
599 }
600
601 void
602 draw_set_mapped_so_buffers(struct draw_context *draw,
603 void *buffers[PIPE_MAX_SO_BUFFERS],
604 unsigned num_buffers)
605 {
606 int i;
607
608 for (i = 0; i < num_buffers; ++i) {
609 draw->so.buffers[i] = buffers[i];
610 }
611 draw->so.num_buffers = num_buffers;
612 }
613
614 void
615 draw_set_so_state(struct draw_context *draw,
616 struct pipe_stream_output_state *state)
617 {
618 memcpy(&draw->so.state,
619 state,
620 sizeof(struct pipe_stream_output_state));
621 }
622
623 void
624 draw_set_sampler_views(struct draw_context *draw,
625 struct pipe_sampler_view **views,
626 unsigned num)
627 {
628 unsigned i;
629
630 debug_assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
631
632 for (i = 0; i < num; ++i)
633 draw->sampler_views[i] = views[i];
634 for (i = num; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
635 draw->sampler_views[i] = NULL;
636
637 draw->num_sampler_views = num;
638 }
639
640 void
641 draw_set_samplers(struct draw_context *draw,
642 struct pipe_sampler_state **samplers,
643 unsigned num)
644 {
645 unsigned i;
646
647 debug_assert(num <= PIPE_MAX_VERTEX_SAMPLERS);
648
649 for (i = 0; i < num; ++i)
650 draw->samplers[i] = samplers[i];
651 for (i = num; i < PIPE_MAX_VERTEX_SAMPLERS; ++i)
652 draw->samplers[i] = NULL;
653
654 draw->num_samplers = num;
655 }
656
657 void
658 draw_set_mapped_texture(struct draw_context *draw,
659 unsigned sampler_idx,
660 uint32_t width, uint32_t height, uint32_t depth,
661 uint32_t last_level,
662 uint32_t row_stride[DRAW_MAX_TEXTURE_LEVELS],
663 uint32_t img_stride[DRAW_MAX_TEXTURE_LEVELS],
664 const void *data[DRAW_MAX_TEXTURE_LEVELS])
665 {
666 #ifdef HAVE_LLVM
667 if(draw->llvm)
668 draw_llvm_set_mapped_texture(draw,
669 sampler_idx,
670 width, height, depth, last_level,
671 row_stride, img_stride, data);
672 #endif
673 }