1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <keithw@vmware.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 "util/u_inlines.h"
39 #include "util/u_helpers.h"
40 #include "util/u_prim.h"
41 #include "util/u_format.h"
42 #include "draw_context.h"
43 #include "draw_pipe.h"
44 #include "draw_prim_assembler.h"
45 #include "draw_vs.h"
46 #include "draw_gs.h"
47
48 #if HAVE_LLVM
49 #include "gallivm/lp_bld_init.h"
50 #include "gallivm/lp_bld_limits.h"
51 #include "draw_llvm.h"
52
53 boolean
draw_get_option_use_llvm(void)54 draw_get_option_use_llvm(void)
55 {
56 return debug_get_bool_option("DRAW_USE_LLVM", TRUE);
57 }
58 #else
59 boolean
draw_get_option_use_llvm(void)60 draw_get_option_use_llvm(void)
61 {
62 return FALSE;
63 }
64 #endif
65
66
67 /**
68 * Create new draw module context with gallivm state for LLVM JIT.
69 */
70 static struct draw_context *
draw_create_context(struct pipe_context * pipe,void * context,boolean try_llvm)71 draw_create_context(struct pipe_context *pipe, void *context,
72 boolean try_llvm)
73 {
74 struct draw_context *draw = CALLOC_STRUCT( draw_context );
75 if (!draw)
76 goto err_out;
77
78 /* we need correct cpu caps for disabling denorms in draw_vbo() */
79 util_cpu_detect();
80
81 #if HAVE_LLVM
82 if (try_llvm && draw_get_option_use_llvm()) {
83 draw->llvm = draw_llvm_create(draw, (LLVMContextRef)context);
84 }
85 #endif
86
87 draw->pipe = pipe;
88
89 if (!draw_init(draw))
90 goto err_destroy;
91
92 draw->ia = draw_prim_assembler_create(draw);
93 if (!draw->ia)
94 goto err_destroy;
95
96 return draw;
97
98 err_destroy:
99 draw_destroy( draw );
100 err_out:
101 return NULL;
102 }
103
104
105 /**
106 * Create new draw module context, with LLVM JIT.
107 */
108 struct draw_context *
draw_create(struct pipe_context * pipe)109 draw_create(struct pipe_context *pipe)
110 {
111 return draw_create_context(pipe, NULL, TRUE);
112 }
113
114
115 #if HAVE_LLVM
116 struct draw_context *
draw_create_with_llvm_context(struct pipe_context * pipe,void * context)117 draw_create_with_llvm_context(struct pipe_context *pipe,
118 void *context)
119 {
120 return draw_create_context(pipe, context, TRUE);
121 }
122 #endif
123
124 /**
125 * Create a new draw context, without LLVM JIT.
126 */
127 struct draw_context *
draw_create_no_llvm(struct pipe_context * pipe)128 draw_create_no_llvm(struct pipe_context *pipe)
129 {
130 return draw_create_context(pipe, NULL, FALSE);
131 }
132
133
draw_init(struct draw_context * draw)134 boolean draw_init(struct draw_context *draw)
135 {
136 /*
137 * Note that several functions compute the clipmask of the predefined
138 * formats with hardcoded formulas instead of using these. So modifications
139 * here must be reflected there too.
140 */
141
142 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 );
143 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 );
144 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 );
145 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 );
146 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */
147 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */
148 draw->clip_xy = TRUE;
149 draw->clip_z = TRUE;
150
151 draw->pt.user.planes = (float (*) [DRAW_TOTAL_CLIP_PLANES][4]) &(draw->plane[0]);
152 draw->pt.user.eltMax = ~0;
153
154 if (!draw_pipeline_init( draw ))
155 return FALSE;
156
157 if (!draw_pt_init( draw ))
158 return FALSE;
159
160 if (!draw_vs_init( draw ))
161 return FALSE;
162
163 if (!draw_gs_init( draw ))
164 return FALSE;
165
166 draw->quads_always_flatshade_last = !draw->pipe->screen->get_param(
167 draw->pipe->screen, PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION);
168
169 draw->floating_point_depth = false;
170
171 return TRUE;
172 }
173
174 /*
175 * Called whenever we're starting to draw a new instance.
176 * Some internal structures don't want to have to reset internal
177 * members on each invocation (because their state might have to persist
178 * between multiple primitive restart rendering call) but might have to
179 * for each new instance.
180 * This is particularly the case for primitive id's in geometry shader.
181 */
draw_new_instance(struct draw_context * draw)182 void draw_new_instance(struct draw_context *draw)
183 {
184 draw_geometry_shader_new_instance(draw->gs.geometry_shader);
185 draw_prim_assembler_new_instance(draw->ia);
186 }
187
188
draw_destroy(struct draw_context * draw)189 void draw_destroy( struct draw_context *draw )
190 {
191 struct pipe_context *pipe;
192 unsigned i, j;
193
194 if (!draw)
195 return;
196
197 pipe = draw->pipe;
198
199 /* free any rasterizer CSOs that we may have created.
200 */
201 for (i = 0; i < 2; i++) {
202 for (j = 0; j < 2; j++) {
203 if (draw->rasterizer_no_cull[i][j]) {
204 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]);
205 }
206 }
207 }
208
209 for (i = 0; i < draw->pt.nr_vertex_buffers; i++)
210 pipe_vertex_buffer_unreference(&draw->pt.vertex_buffer[i]);
211
212 /* Not so fast -- we're just borrowing this at the moment.
213 *
214 if (draw->render)
215 draw->render->destroy( draw->render );
216 */
217
218 draw_prim_assembler_destroy(draw->ia);
219 draw_pipeline_destroy( draw );
220 draw_pt_destroy( draw );
221 draw_vs_destroy( draw );
222 draw_gs_destroy( draw );
223 #ifdef HAVE_LLVM
224 if (draw->llvm)
225 draw_llvm_destroy( draw->llvm );
226 #endif
227
228 FREE( draw );
229 }
230
231
232
draw_flush(struct draw_context * draw)233 void draw_flush( struct draw_context *draw )
234 {
235 draw_do_flush( draw, DRAW_FLUSH_BACKEND );
236 }
237
238
239 /**
240 * Specify the depth stencil format for the draw pipeline. This function
241 * determines the Minimum Resolvable Depth factor for polygon offset.
242 * This factor potentially depends on the number of Z buffer bits,
243 * the rasterization algorithm and the arithmetic performed on Z
244 * values between vertex shading and rasterization.
245 */
draw_set_zs_format(struct draw_context * draw,enum pipe_format format)246 void draw_set_zs_format(struct draw_context *draw, enum pipe_format format)
247 {
248 const struct util_format_description *desc = util_format_description(format);
249
250 draw->floating_point_depth =
251 (util_get_depth_format_type(desc) == UTIL_FORMAT_TYPE_FLOAT);
252
253 draw->mrd = util_get_depth_format_mrd(desc);
254 }
255
256
257 static bool
draw_is_vs_window_space(struct draw_context * draw)258 draw_is_vs_window_space(struct draw_context *draw)
259 {
260 if (draw->vs.vertex_shader) {
261 struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
262
263 return info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] != 0;
264 }
265 return false;
266 }
267
268
269 void
draw_update_clip_flags(struct draw_context * draw)270 draw_update_clip_flags(struct draw_context *draw)
271 {
272 bool window_space = draw_is_vs_window_space(draw);
273
274 draw->clip_xy = !draw->driver.bypass_clip_xy && !window_space;
275 draw->guard_band_xy = (!draw->driver.bypass_clip_xy &&
276 draw->driver.guard_band_xy);
277 draw->clip_z = (!draw->driver.bypass_clip_z &&
278 draw->rasterizer && draw->rasterizer->depth_clip) &&
279 !window_space;
280 draw->clip_user = draw->rasterizer &&
281 draw->rasterizer->clip_plane_enable != 0 &&
282 !window_space;
283 draw->guard_band_points_xy = draw->guard_band_xy ||
284 (draw->driver.bypass_clip_points &&
285 (draw->rasterizer &&
286 draw->rasterizer->point_tri_clip));
287 }
288
289
290 void
draw_update_viewport_flags(struct draw_context * draw)291 draw_update_viewport_flags(struct draw_context *draw)
292 {
293 bool window_space = draw_is_vs_window_space(draw);
294
295 draw->bypass_viewport = window_space || draw->identity_viewport;
296 }
297
298
299 /**
300 * Register new primitive rasterization/rendering state.
301 * This causes the drawing pipeline to be rebuilt.
302 */
draw_set_rasterizer_state(struct draw_context * draw,const struct pipe_rasterizer_state * raster,void * rast_handle)303 void draw_set_rasterizer_state( struct draw_context *draw,
304 const struct pipe_rasterizer_state *raster,
305 void *rast_handle )
306 {
307 if (!draw->suspend_flushing) {
308 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
309
310 draw->rasterizer = raster;
311 draw->rast_handle = rast_handle;
312 draw_update_clip_flags(draw);
313 }
314 }
315
316 /* With a little more work, llvmpipe will be able to turn this off and
317 * do its own x/y clipping.
318 *
319 * Some hardware can turn off clipping altogether - in particular any
320 * hardware with a TNL unit can do its own clipping, even if it is
321 * relying on the draw module for some other reason.
322 * Setting bypass_clip_points to achieve d3d-style point clipping (the driver
323 * will need to do the "vp scissoring") _requires_ the driver to implement
324 * wide points / point sprites itself (points will still be clipped if rasterizer
325 * point_tri_clip isn't set). Only relevant if bypass_clip_xy isn't set.
326 */
draw_set_driver_clipping(struct draw_context * draw,boolean bypass_clip_xy,boolean bypass_clip_z,boolean guard_band_xy,boolean bypass_clip_points)327 void draw_set_driver_clipping( struct draw_context *draw,
328 boolean bypass_clip_xy,
329 boolean bypass_clip_z,
330 boolean guard_band_xy,
331 boolean bypass_clip_points)
332 {
333 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
334
335 draw->driver.bypass_clip_xy = bypass_clip_xy;
336 draw->driver.bypass_clip_z = bypass_clip_z;
337 draw->driver.guard_band_xy = guard_band_xy;
338 draw->driver.bypass_clip_points = bypass_clip_points;
339 draw_update_clip_flags(draw);
340 }
341
342
343 /**
344 * Plug in the primitive rendering/rasterization stage (which is the last
345 * stage in the drawing pipeline).
346 * This is provided by the device driver.
347 */
draw_set_rasterize_stage(struct draw_context * draw,struct draw_stage * stage)348 void draw_set_rasterize_stage( struct draw_context *draw,
349 struct draw_stage *stage )
350 {
351 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
352
353 draw->pipeline.rasterize = stage;
354 }
355
356
357 /**
358 * Set the draw module's clipping state.
359 */
draw_set_clip_state(struct draw_context * draw,const struct pipe_clip_state * clip)360 void draw_set_clip_state( struct draw_context *draw,
361 const struct pipe_clip_state *clip )
362 {
363 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
364
365 memcpy(&draw->plane[6], clip->ucp, sizeof(clip->ucp));
366 }
367
368
369 /**
370 * Set the draw module's viewport state.
371 */
draw_set_viewport_states(struct draw_context * draw,unsigned start_slot,unsigned num_viewports,const struct pipe_viewport_state * vps)372 void draw_set_viewport_states( struct draw_context *draw,
373 unsigned start_slot,
374 unsigned num_viewports,
375 const struct pipe_viewport_state *vps )
376 {
377 const struct pipe_viewport_state *viewport = vps;
378 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
379
380 debug_assert(start_slot < PIPE_MAX_VIEWPORTS);
381 debug_assert((start_slot + num_viewports) <= PIPE_MAX_VIEWPORTS);
382
383 memcpy(draw->viewports + start_slot, vps,
384 sizeof(struct pipe_viewport_state) * num_viewports);
385
386 draw->identity_viewport = (num_viewports == 1) &&
387 (viewport->scale[0] == 1.0f &&
388 viewport->scale[1] == 1.0f &&
389 viewport->scale[2] == 1.0f &&
390 viewport->translate[0] == 0.0f &&
391 viewport->translate[1] == 0.0f &&
392 viewport->translate[2] == 0.0f);
393 draw_update_viewport_flags(draw);
394 }
395
396
397
398 void
draw_set_vertex_buffers(struct draw_context * draw,unsigned start_slot,unsigned count,const struct pipe_vertex_buffer * buffers)399 draw_set_vertex_buffers(struct draw_context *draw,
400 unsigned start_slot, unsigned count,
401 const struct pipe_vertex_buffer *buffers)
402 {
403 assert(start_slot + count <= PIPE_MAX_ATTRIBS);
404
405 util_set_vertex_buffers_count(draw->pt.vertex_buffer,
406 &draw->pt.nr_vertex_buffers,
407 buffers, start_slot, count);
408 }
409
410
411 void
draw_set_vertex_elements(struct draw_context * draw,unsigned count,const struct pipe_vertex_element * elements)412 draw_set_vertex_elements(struct draw_context *draw,
413 unsigned count,
414 const struct pipe_vertex_element *elements)
415 {
416 assert(count <= PIPE_MAX_ATTRIBS);
417
418 /* We could improve this by only flushing the frontend and the fetch part
419 * of the middle. This would avoid recalculating the emit keys.*/
420 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
421
422 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0]));
423 draw->pt.nr_vertex_elements = count;
424 }
425
426
427 /**
428 * Tell drawing context where to find mapped vertex buffers.
429 */
430 void
draw_set_mapped_vertex_buffer(struct draw_context * draw,unsigned attr,const void * buffer,size_t size)431 draw_set_mapped_vertex_buffer(struct draw_context *draw,
432 unsigned attr, const void *buffer,
433 size_t size)
434 {
435 draw->pt.user.vbuffer[attr].map = buffer;
436 draw->pt.user.vbuffer[attr].size = size;
437 }
438
439
440 void
draw_set_mapped_constant_buffer(struct draw_context * draw,enum pipe_shader_type shader_type,unsigned slot,const void * buffer,unsigned size)441 draw_set_mapped_constant_buffer(struct draw_context *draw,
442 enum pipe_shader_type shader_type,
443 unsigned slot,
444 const void *buffer,
445 unsigned size )
446 {
447 debug_assert(shader_type == PIPE_SHADER_VERTEX ||
448 shader_type == PIPE_SHADER_GEOMETRY);
449 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS);
450
451 draw_do_flush(draw, DRAW_FLUSH_PARAMETER_CHANGE);
452
453 switch (shader_type) {
454 case PIPE_SHADER_VERTEX:
455 draw->pt.user.vs_constants[slot] = buffer;
456 draw->pt.user.vs_constants_size[slot] = size;
457 break;
458 case PIPE_SHADER_GEOMETRY:
459 draw->pt.user.gs_constants[slot] = buffer;
460 draw->pt.user.gs_constants_size[slot] = size;
461 break;
462 default:
463 assert(0 && "invalid shader type in draw_set_mapped_constant_buffer");
464 }
465 }
466
467
468 /**
469 * Tells the draw module to draw points with triangles if their size
470 * is greater than this threshold.
471 */
472 void
draw_wide_point_threshold(struct draw_context * draw,float threshold)473 draw_wide_point_threshold(struct draw_context *draw, float threshold)
474 {
475 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
476 draw->pipeline.wide_point_threshold = threshold;
477 }
478
479
480 /**
481 * Should the draw module handle point->quad conversion for drawing sprites?
482 */
483 void
draw_wide_point_sprites(struct draw_context * draw,boolean draw_sprite)484 draw_wide_point_sprites(struct draw_context *draw, boolean draw_sprite)
485 {
486 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
487 draw->pipeline.wide_point_sprites = draw_sprite;
488 }
489
490
491 /**
492 * Tells the draw module to draw lines with triangles if their width
493 * is greater than this threshold.
494 */
495 void
draw_wide_line_threshold(struct draw_context * draw,float threshold)496 draw_wide_line_threshold(struct draw_context *draw, float threshold)
497 {
498 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
499 draw->pipeline.wide_line_threshold = roundf(threshold);
500 }
501
502
503 /**
504 * Tells the draw module whether or not to implement line stipple.
505 */
506 void
draw_enable_line_stipple(struct draw_context * draw,boolean enable)507 draw_enable_line_stipple(struct draw_context *draw, boolean enable)
508 {
509 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
510 draw->pipeline.line_stipple = enable;
511 }
512
513
514 /**
515 * Tells draw module whether to convert points to quads for sprite mode.
516 */
517 void
draw_enable_point_sprites(struct draw_context * draw,boolean enable)518 draw_enable_point_sprites(struct draw_context *draw, boolean enable)
519 {
520 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
521 draw->pipeline.point_sprite = enable;
522 }
523
524
525 void
draw_set_force_passthrough(struct draw_context * draw,boolean enable)526 draw_set_force_passthrough( struct draw_context *draw, boolean enable )
527 {
528 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
529 draw->force_passthrough = enable;
530 }
531
532
533
534 /**
535 * Allocate an extra vertex/geometry shader vertex attribute, if it doesn't
536 * exist already.
537 *
538 * This is used by some of the optional draw module stages such
539 * as wide_point which may need to allocate additional generic/texcoord
540 * attributes.
541 */
542 int
draw_alloc_extra_vertex_attrib(struct draw_context * draw,uint semantic_name,uint semantic_index)543 draw_alloc_extra_vertex_attrib(struct draw_context *draw,
544 uint semantic_name, uint semantic_index)
545 {
546 int slot;
547 uint num_outputs;
548 uint n;
549
550 slot = draw_find_shader_output(draw, semantic_name, semantic_index);
551 if (slot >= 0) {
552 return slot;
553 }
554
555 num_outputs = draw_current_shader_outputs(draw);
556 n = draw->extra_shader_outputs.num;
557
558 assert(n < ARRAY_SIZE(draw->extra_shader_outputs.semantic_name));
559
560 draw->extra_shader_outputs.semantic_name[n] = semantic_name;
561 draw->extra_shader_outputs.semantic_index[n] = semantic_index;
562 draw->extra_shader_outputs.slot[n] = num_outputs + n;
563 draw->extra_shader_outputs.num++;
564
565 return draw->extra_shader_outputs.slot[n];
566 }
567
568
569 /**
570 * Remove all extra vertex attributes that were allocated with
571 * draw_alloc_extra_vertex_attrib().
572 */
573 void
draw_remove_extra_vertex_attribs(struct draw_context * draw)574 draw_remove_extra_vertex_attribs(struct draw_context *draw)
575 {
576 draw->extra_shader_outputs.num = 0;
577 }
578
579
580 /**
581 * If a geometry shader is present, return its info, else the vertex shader's
582 * info.
583 */
584 struct tgsi_shader_info *
draw_get_shader_info(const struct draw_context * draw)585 draw_get_shader_info(const struct draw_context *draw)
586 {
587
588 if (draw->gs.geometry_shader) {
589 return &draw->gs.geometry_shader->info;
590 } else {
591 return &draw->vs.vertex_shader->info;
592 }
593 }
594
595 /**
596 * Prepare outputs slots from the draw module
597 *
598 * Certain parts of the draw module can emit additional
599 * outputs that can be quite useful to the backends, a good
600 * example of it is the process of decomposing primitives
601 * into wireframes (aka. lines) which normally would lose
602 * the face-side information, but using this method we can
603 * inject another shader output which passes the original
604 * face side information to the backend.
605 */
606 void
draw_prepare_shader_outputs(struct draw_context * draw)607 draw_prepare_shader_outputs(struct draw_context *draw)
608 {
609 draw_remove_extra_vertex_attribs(draw);
610 draw_prim_assembler_prepare_outputs(draw->ia);
611 draw_unfilled_prepare_outputs(draw, draw->pipeline.unfilled);
612 if (draw->pipeline.aapoint)
613 draw_aapoint_prepare_outputs(draw, draw->pipeline.aapoint);
614 if (draw->pipeline.aaline)
615 draw_aaline_prepare_outputs(draw, draw->pipeline.aaline);
616 }
617
618 /**
619 * Ask the draw module for the location/slot of the given vertex attribute in
620 * a post-transformed vertex.
621 *
622 * With this function, drivers that use the draw module should have no reason
623 * to track the current vertex/geometry shader.
624 *
625 * Note that the draw module may sometimes generate vertices with extra
626 * attributes (such as texcoords for AA lines). The driver can call this
627 * function to find those attributes.
628 *
629 * -1 is returned if the attribute is not found since this is
630 * an undefined situation. Note, that zero is valid and can
631 * be used by any of the attributes, because position is not
632 * required to be attribute 0 or even at all present.
633 */
634 int
draw_find_shader_output(const struct draw_context * draw,uint semantic_name,uint semantic_index)635 draw_find_shader_output(const struct draw_context *draw,
636 uint semantic_name, uint semantic_index)
637 {
638 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
639 uint i;
640
641 for (i = 0; i < info->num_outputs; i++) {
642 if (info->output_semantic_name[i] == semantic_name &&
643 info->output_semantic_index[i] == semantic_index)
644 return i;
645 }
646
647 /* Search the extra vertex attributes */
648 for (i = 0; i < draw->extra_shader_outputs.num; i++) {
649 if (draw->extra_shader_outputs.semantic_name[i] == semantic_name &&
650 draw->extra_shader_outputs.semantic_index[i] == semantic_index) {
651 return draw->extra_shader_outputs.slot[i];
652 }
653 }
654
655 return -1;
656 }
657
658
659 /**
660 * Return total number of the shader outputs. This function is similar to
661 * draw_current_shader_outputs() but this function also counts any extra
662 * vertex/geometry output attributes that may be filled in by some draw
663 * stages (such as AA point, AA line).
664 *
665 * If geometry shader is present, its output will be returned,
666 * if not vertex shader is used.
667 */
668 uint
draw_num_shader_outputs(const struct draw_context * draw)669 draw_num_shader_outputs(const struct draw_context *draw)
670 {
671 const struct tgsi_shader_info *info = draw_get_shader_info(draw);
672 uint count;
673
674 count = info->num_outputs;
675 count += draw->extra_shader_outputs.num;
676
677 return count;
678 }
679
680
681 /**
682 * Return total number of the vertex shader outputs. This function
683 * also counts any extra vertex output attributes that may
684 * be filled in by some draw stages (such as AA point, AA line,
685 * front face).
686 */
687 uint
draw_total_vs_outputs(const struct draw_context * draw)688 draw_total_vs_outputs(const struct draw_context *draw)
689 {
690 const struct tgsi_shader_info *info = &draw->vs.vertex_shader->info;
691
692 return info->num_outputs + draw->extra_shader_outputs.num;
693 }
694
695 /**
696 * Return total number of the geometry shader outputs. This function
697 * also counts any extra geometry output attributes that may
698 * be filled in by some draw stages (such as AA point, AA line, front
699 * face).
700 */
701 uint
draw_total_gs_outputs(const struct draw_context * draw)702 draw_total_gs_outputs(const struct draw_context *draw)
703 {
704 const struct tgsi_shader_info *info;
705
706 if (!draw->gs.geometry_shader)
707 return 0;
708
709 info = &draw->gs.geometry_shader->info;
710
711 return info->num_outputs + draw->extra_shader_outputs.num;
712 }
713
714
715 /**
716 * Provide TGSI sampler objects for vertex/geometry shaders that use
717 * texture fetches. This state only needs to be set once per context.
718 * This might only be used by software drivers for the time being.
719 */
720 void
draw_texture_sampler(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_sampler * sampler)721 draw_texture_sampler(struct draw_context *draw,
722 enum pipe_shader_type shader,
723 struct tgsi_sampler *sampler)
724 {
725 if (shader == PIPE_SHADER_VERTEX) {
726 draw->vs.tgsi.sampler = sampler;
727 } else {
728 debug_assert(shader == PIPE_SHADER_GEOMETRY);
729 draw->gs.tgsi.sampler = sampler;
730 }
731 }
732
733 /**
734 * Provide TGSI image objects for vertex/geometry shaders that use
735 * texture fetches. This state only needs to be set once per context.
736 * This might only be used by software drivers for the time being.
737 */
738 void
draw_image(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_image * image)739 draw_image(struct draw_context *draw,
740 enum pipe_shader_type shader,
741 struct tgsi_image *image)
742 {
743 if (shader == PIPE_SHADER_VERTEX) {
744 draw->vs.tgsi.image = image;
745 } else {
746 debug_assert(shader == PIPE_SHADER_GEOMETRY);
747 draw->gs.tgsi.image = image;
748 }
749 }
750
751 /**
752 * Provide TGSI buffer objects for vertex/geometry shaders that use
753 * load/store/atomic ops. This state only needs to be set once per context.
754 * This might only be used by software drivers for the time being.
755 */
756 void
draw_buffer(struct draw_context * draw,enum pipe_shader_type shader,struct tgsi_buffer * buffer)757 draw_buffer(struct draw_context *draw,
758 enum pipe_shader_type shader,
759 struct tgsi_buffer *buffer)
760 {
761 if (shader == PIPE_SHADER_VERTEX) {
762 draw->vs.tgsi.buffer = buffer;
763 } else {
764 debug_assert(shader == PIPE_SHADER_GEOMETRY);
765 draw->gs.tgsi.buffer = buffer;
766 }
767 }
768
769
draw_set_render(struct draw_context * draw,struct vbuf_render * render)770 void draw_set_render( struct draw_context *draw,
771 struct vbuf_render *render )
772 {
773 draw->render = render;
774 }
775
776
777 /**
778 * Tell the draw module where vertex indexes/elements are located, and
779 * their size (in bytes).
780 */
781 void
draw_set_indexes(struct draw_context * draw,const void * elements,unsigned elem_size,unsigned elem_buffer_space)782 draw_set_indexes(struct draw_context *draw,
783 const void *elements, unsigned elem_size,
784 unsigned elem_buffer_space)
785 {
786 assert(elem_size == 0 ||
787 elem_size == 1 ||
788 elem_size == 2 ||
789 elem_size == 4);
790 draw->pt.user.elts = elements;
791 draw->pt.user.eltSizeIB = elem_size;
792 if (elem_size)
793 draw->pt.user.eltMax = elem_buffer_space / elem_size;
794 else
795 draw->pt.user.eltMax = 0;
796 }
797
798
799 /* Revamp me please:
800 */
draw_do_flush(struct draw_context * draw,unsigned flags)801 void draw_do_flush( struct draw_context *draw, unsigned flags )
802 {
803 if (!draw->suspend_flushing)
804 {
805 assert(!draw->flushing); /* catch inadvertant recursion */
806
807 draw->flushing = TRUE;
808
809 draw_pipeline_flush( draw, flags );
810
811 draw_pt_flush( draw, flags );
812
813 draw->flushing = FALSE;
814 }
815 }
816
817
818 /**
819 * Return the number of output attributes produced by the geometry
820 * shader, if present. If no geometry shader, return the number of
821 * outputs from the vertex shader.
822 * \sa draw_num_shader_outputs
823 */
824 uint
draw_current_shader_outputs(const struct draw_context * draw)825 draw_current_shader_outputs(const struct draw_context *draw)
826 {
827 if (draw->gs.geometry_shader)
828 return draw->gs.num_gs_outputs;
829 return draw->vs.num_vs_outputs;
830 }
831
832
833 /**
834 * Return the index of the shader output which will contain the
835 * vertex position.
836 */
837 uint
draw_current_shader_position_output(const struct draw_context * draw)838 draw_current_shader_position_output(const struct draw_context *draw)
839 {
840 if (draw->gs.geometry_shader)
841 return draw->gs.position_output;
842 return draw->vs.position_output;
843 }
844
845
846 /**
847 * Return the index of the shader output which will contain the
848 * viewport index.
849 */
850 uint
draw_current_shader_viewport_index_output(const struct draw_context * draw)851 draw_current_shader_viewport_index_output(const struct draw_context *draw)
852 {
853 if (draw->gs.geometry_shader)
854 return draw->gs.geometry_shader->viewport_index_output;
855 return draw->vs.vertex_shader->viewport_index_output;
856 }
857
858 /**
859 * Returns true if there's a geometry shader bound and the geometry
860 * shader writes out a viewport index.
861 */
862 boolean
draw_current_shader_uses_viewport_index(const struct draw_context * draw)863 draw_current_shader_uses_viewport_index(const struct draw_context *draw)
864 {
865 if (draw->gs.geometry_shader)
866 return draw->gs.geometry_shader->info.writes_viewport_index;
867 return draw->vs.vertex_shader->info.writes_viewport_index;
868 }
869
870
871 /**
872 * Return the index of the shader output which will contain the
873 * clip vertex position.
874 * Note we don't support clipvertex output in the gs. For clipping
875 * to work correctly hence we return ordinary position output instead.
876 */
877 uint
draw_current_shader_clipvertex_output(const struct draw_context * draw)878 draw_current_shader_clipvertex_output(const struct draw_context *draw)
879 {
880 if (draw->gs.geometry_shader)
881 return draw->gs.position_output;
882 return draw->vs.clipvertex_output;
883 }
884
885 uint
draw_current_shader_ccdistance_output(const struct draw_context * draw,int index)886 draw_current_shader_ccdistance_output(const struct draw_context *draw, int index)
887 {
888 debug_assert(index < PIPE_MAX_CLIP_OR_CULL_DISTANCE_ELEMENT_COUNT);
889 if (draw->gs.geometry_shader)
890 return draw->gs.geometry_shader->ccdistance_output[index];
891 return draw->vs.ccdistance_output[index];
892 }
893
894
895 uint
draw_current_shader_num_written_clipdistances(const struct draw_context * draw)896 draw_current_shader_num_written_clipdistances(const struct draw_context *draw)
897 {
898 if (draw->gs.geometry_shader)
899 return draw->gs.geometry_shader->info.num_written_clipdistance;
900 return draw->vs.vertex_shader->info.num_written_clipdistance;
901 }
902
903 uint
draw_current_shader_num_written_culldistances(const struct draw_context * draw)904 draw_current_shader_num_written_culldistances(const struct draw_context *draw)
905 {
906 if (draw->gs.geometry_shader)
907 return draw->gs.geometry_shader->info.num_written_culldistance;
908 return draw->vs.vertex_shader->info.num_written_culldistance;
909 }
910
911 /**
912 * Return a pointer/handle for a driver/CSO rasterizer object which
913 * disabled culling, stippling, unfilled tris, etc.
914 * This is used by some pipeline stages (such as wide_point, aa_line
915 * and aa_point) which convert points/lines into triangles. In those
916 * cases we don't want to accidentally cull the triangles.
917 *
918 * \param scissor should the rasterizer state enable scissoring?
919 * \param flatshade should the rasterizer state use flat shading?
920 * \return rasterizer CSO handle
921 */
922 void *
draw_get_rasterizer_no_cull(struct draw_context * draw,boolean scissor,boolean flatshade)923 draw_get_rasterizer_no_cull( struct draw_context *draw,
924 boolean scissor,
925 boolean flatshade )
926 {
927 if (!draw->rasterizer_no_cull[scissor][flatshade]) {
928 /* create now */
929 struct pipe_context *pipe = draw->pipe;
930 struct pipe_rasterizer_state rast;
931
932 memset(&rast, 0, sizeof(rast));
933 rast.scissor = scissor;
934 rast.flatshade = flatshade;
935 rast.front_ccw = 1;
936 rast.half_pixel_center = draw->rasterizer->half_pixel_center;
937 rast.bottom_edge_rule = draw->rasterizer->bottom_edge_rule;
938 rast.clip_halfz = draw->rasterizer->clip_halfz;
939
940 draw->rasterizer_no_cull[scissor][flatshade] =
941 pipe->create_rasterizer_state(pipe, &rast);
942 }
943 return draw->rasterizer_no_cull[scissor][flatshade];
944 }
945
946 void
draw_set_mapped_so_targets(struct draw_context * draw,int num_targets,struct draw_so_target * targets[PIPE_MAX_SO_BUFFERS])947 draw_set_mapped_so_targets(struct draw_context *draw,
948 int num_targets,
949 struct draw_so_target *targets[PIPE_MAX_SO_BUFFERS])
950 {
951 int i;
952
953 for (i = 0; i < num_targets; i++)
954 draw->so.targets[i] = targets[i];
955 for (i = num_targets; i < PIPE_MAX_SO_BUFFERS; i++)
956 draw->so.targets[i] = NULL;
957
958 draw->so.num_targets = num_targets;
959 }
960
961 void
draw_set_sampler_views(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_view ** views,unsigned num)962 draw_set_sampler_views(struct draw_context *draw,
963 enum pipe_shader_type shader_stage,
964 struct pipe_sampler_view **views,
965 unsigned num)
966 {
967 unsigned i;
968
969 debug_assert(shader_stage < PIPE_SHADER_TYPES);
970 debug_assert(num <= PIPE_MAX_SHADER_SAMPLER_VIEWS);
971
972 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
973
974 for (i = 0; i < num; ++i)
975 draw->sampler_views[shader_stage][i] = views[i];
976 for (i = num; i < PIPE_MAX_SHADER_SAMPLER_VIEWS; ++i)
977 draw->sampler_views[shader_stage][i] = NULL;
978
979 draw->num_sampler_views[shader_stage] = num;
980 }
981
982 void
draw_set_samplers(struct draw_context * draw,enum pipe_shader_type shader_stage,struct pipe_sampler_state ** samplers,unsigned num)983 draw_set_samplers(struct draw_context *draw,
984 enum pipe_shader_type shader_stage,
985 struct pipe_sampler_state **samplers,
986 unsigned num)
987 {
988 unsigned i;
989
990 debug_assert(shader_stage < PIPE_SHADER_TYPES);
991 debug_assert(num <= PIPE_MAX_SAMPLERS);
992
993 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
994
995 for (i = 0; i < num; ++i)
996 draw->samplers[shader_stage][i] = samplers[i];
997 for (i = num; i < PIPE_MAX_SAMPLERS; ++i)
998 draw->samplers[shader_stage][i] = NULL;
999
1000 draw->num_samplers[shader_stage] = num;
1001
1002 #ifdef HAVE_LLVM
1003 if (draw->llvm)
1004 draw_llvm_set_sampler_state(draw, shader_stage);
1005 #endif
1006 }
1007
1008 void
draw_set_mapped_texture(struct draw_context * draw,enum pipe_shader_type shader_stage,unsigned sview_idx,uint32_t width,uint32_t height,uint32_t depth,uint32_t first_level,uint32_t last_level,const void * base_ptr,uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])1009 draw_set_mapped_texture(struct draw_context *draw,
1010 enum pipe_shader_type shader_stage,
1011 unsigned sview_idx,
1012 uint32_t width, uint32_t height, uint32_t depth,
1013 uint32_t first_level, uint32_t last_level,
1014 const void *base_ptr,
1015 uint32_t row_stride[PIPE_MAX_TEXTURE_LEVELS],
1016 uint32_t img_stride[PIPE_MAX_TEXTURE_LEVELS],
1017 uint32_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS])
1018 {
1019 #ifdef HAVE_LLVM
1020 if (draw->llvm)
1021 draw_llvm_set_mapped_texture(draw,
1022 shader_stage,
1023 sview_idx,
1024 width, height, depth, first_level,
1025 last_level, base_ptr,
1026 row_stride, img_stride, mip_offsets);
1027 #endif
1028 }
1029
1030 /**
1031 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1032 * different ways of setting textures, and drivers typically only support one.
1033 */
1034 int
draw_get_shader_param_no_llvm(enum pipe_shader_type shader,enum pipe_shader_cap param)1035 draw_get_shader_param_no_llvm(enum pipe_shader_type shader,
1036 enum pipe_shader_cap param)
1037 {
1038 switch(shader) {
1039 case PIPE_SHADER_VERTEX:
1040 case PIPE_SHADER_GEOMETRY:
1041 return tgsi_exec_get_shader_param(param);
1042 default:
1043 return 0;
1044 }
1045 }
1046
1047 /**
1048 * XXX: Results for PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS because there are two
1049 * different ways of setting textures, and drivers typically only support one.
1050 * Drivers requesting a draw context explicitly without llvm must call
1051 * draw_get_shader_param_no_llvm instead.
1052 */
1053 int
draw_get_shader_param(enum pipe_shader_type shader,enum pipe_shader_cap param)1054 draw_get_shader_param(enum pipe_shader_type shader, enum pipe_shader_cap param)
1055 {
1056
1057 #ifdef HAVE_LLVM
1058 if (draw_get_option_use_llvm()) {
1059 switch(shader) {
1060 case PIPE_SHADER_VERTEX:
1061 case PIPE_SHADER_GEOMETRY:
1062 return gallivm_get_shader_param(param);
1063 default:
1064 return 0;
1065 }
1066 }
1067 #endif
1068
1069 return draw_get_shader_param_no_llvm(shader, param);
1070 }
1071
1072 /**
1073 * Enables or disables collection of statistics.
1074 *
1075 * Draw module is capable of generating statistics for the vertex
1076 * processing pipeline. Collection of that data isn't free and so
1077 * it's disabled by default. The users of the module can enable
1078 * (or disable) this functionality through this function.
1079 * The actual data will be emitted through the VBUF interface,
1080 * the 'pipeline_statistics' callback to be exact.
1081 */
1082 void
draw_collect_pipeline_statistics(struct draw_context * draw,boolean enable)1083 draw_collect_pipeline_statistics(struct draw_context *draw,
1084 boolean enable)
1085 {
1086 draw->collect_statistics = enable;
1087 }
1088
1089 /**
1090 * Computes clipper invocation statistics.
1091 *
1092 * Figures out how many primitives would have been
1093 * sent to the clipper given the specified
1094 * prim info data.
1095 */
1096 void
draw_stats_clipper_primitives(struct draw_context * draw,const struct draw_prim_info * prim_info)1097 draw_stats_clipper_primitives(struct draw_context *draw,
1098 const struct draw_prim_info *prim_info)
1099 {
1100 if (draw->collect_statistics) {
1101 unsigned i;
1102 for (i = 0; i < prim_info->primitive_count; i++) {
1103 draw->statistics.c_invocations +=
1104 u_decomposed_prims_for_vertices(prim_info->prim,
1105 prim_info->primitive_lengths[i]);
1106 }
1107 }
1108 }
1109
1110
1111 /**
1112 * Returns true if the draw module will inject the frontface
1113 * info into the outputs.
1114 *
1115 * Given the specified primitive and rasterizer state
1116 * the function will figure out if the draw module
1117 * will inject the front-face information into shader
1118 * outputs. This is done to preserve the front-facing
1119 * info when decomposing primitives into wireframes.
1120 */
1121 boolean
draw_will_inject_frontface(const struct draw_context * draw)1122 draw_will_inject_frontface(const struct draw_context *draw)
1123 {
1124 unsigned reduced_prim = u_reduced_prim(draw->pt.prim);
1125 const struct pipe_rasterizer_state *rast = draw->rasterizer;
1126
1127 if (reduced_prim != PIPE_PRIM_TRIANGLES) {
1128 return FALSE;
1129 }
1130
1131 return (rast &&
1132 (rast->fill_front != PIPE_POLYGON_MODE_FILL ||
1133 rast->fill_back != PIPE_POLYGON_MODE_FILL));
1134 }
1135