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