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