draw_context.c revision 7c4208c3a0f48955720f41b3cb320a120c505ba6
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28 /* 29 * Authors: 30 * Keith Whitwell <keith@tungstengraphics.com> 31 */ 32 33 34#include "pipe/p_context.h" 35#include "util/u_memory.h" 36#include "util/u_math.h" 37#include "draw_context.h" 38#include "draw_vs.h" 39#include "draw_gs.h" 40 41#if HAVE_LLVM 42#include "gallivm/lp_bld_init.h" 43#endif 44 45struct draw_context *draw_create( struct pipe_context *pipe ) 46{ 47 struct draw_context *draw = CALLOC_STRUCT( draw_context ); 48 if (draw == NULL) 49 goto fail; 50 51#if HAVE_LLVM 52 assert(lp_build_engine); 53 draw->engine = lp_build_engine; 54#endif 55 56 if (!draw_init(draw)) 57 goto fail; 58 59 draw->pipe = pipe; 60 61 return draw; 62 63fail: 64 draw_destroy( draw ); 65 return NULL; 66} 67 68boolean draw_init(struct draw_context *draw) 69{ 70 /* 71 * Note that several functions compute the clipmask of the predefined 72 * formats with hardcoded formulas instead of using these. So modifications 73 * here must be reflected there too. 74 */ 75 76 ASSIGN_4V( draw->plane[0], -1, 0, 0, 1 ); 77 ASSIGN_4V( draw->plane[1], 1, 0, 0, 1 ); 78 ASSIGN_4V( draw->plane[2], 0, -1, 0, 1 ); 79 ASSIGN_4V( draw->plane[3], 0, 1, 0, 1 ); 80 ASSIGN_4V( draw->plane[4], 0, 0, 1, 1 ); /* yes these are correct */ 81 ASSIGN_4V( draw->plane[5], 0, 0, -1, 1 ); /* mesa's a bit wonky */ 82 draw->nr_planes = 6; 83 84 85 draw->reduced_prim = ~0; /* != any of PIPE_PRIM_x */ 86 87 88 if (!draw_pipeline_init( draw )) 89 return FALSE; 90 91 if (!draw_pt_init( draw )) 92 return FALSE; 93 94 if (!draw_vs_init( draw )) 95 return FALSE; 96 97 if (!draw_gs_init( draw )) 98 return FALSE; 99 100 return TRUE; 101} 102 103 104void draw_destroy( struct draw_context *draw ) 105{ 106 struct pipe_context *pipe = draw->pipe; 107 int i, j; 108 109 if (!draw) 110 return; 111 112 /* free any rasterizer CSOs that we may have created. 113 */ 114 for (i = 0; i < 2; i++) { 115 for (j = 0; j < 2; j++) { 116 if (draw->rasterizer_no_cull[i][j]) { 117 pipe->delete_rasterizer_state(pipe, draw->rasterizer_no_cull[i][j]); 118 } 119 } 120 } 121 122 /* Not so fast -- we're just borrowing this at the moment. 123 * 124 if (draw->render) 125 draw->render->destroy( draw->render ); 126 */ 127 128 draw_pipeline_destroy( draw ); 129 draw_pt_destroy( draw ); 130 draw_vs_destroy( draw ); 131 draw_gs_destroy( draw ); 132 133 FREE( draw ); 134} 135 136 137 138void draw_flush( struct draw_context *draw ) 139{ 140 draw_do_flush( draw, DRAW_FLUSH_BACKEND ); 141} 142 143 144/** 145 * Specify the Minimum Resolvable Depth factor for polygon offset. 146 * This factor potentially depends on the number of Z buffer bits, 147 * the rasterization algorithm and the arithmetic performed on Z 148 * values between vertex shading and rasterization. It will vary 149 * from one driver to another. 150 */ 151void draw_set_mrd(struct draw_context *draw, double mrd) 152{ 153 draw->mrd = mrd; 154} 155 156 157/** 158 * Register new primitive rasterization/rendering state. 159 * This causes the drawing pipeline to be rebuilt. 160 */ 161void draw_set_rasterizer_state( struct draw_context *draw, 162 const struct pipe_rasterizer_state *raster, 163 void *rast_handle ) 164{ 165 if (!draw->suspend_flushing) { 166 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 167 168 draw->rasterizer = raster; 169 draw->rast_handle = rast_handle; 170 171 draw->bypass_clipping = draw->driver.bypass_clipping; 172 } 173} 174 175 176void draw_set_driver_clipping( struct draw_context *draw, 177 boolean bypass_clipping ) 178{ 179 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 180 181 draw->driver.bypass_clipping = bypass_clipping; 182 draw->bypass_clipping = draw->driver.bypass_clipping; 183} 184 185 186/** 187 * Plug in the primitive rendering/rasterization stage (which is the last 188 * stage in the drawing pipeline). 189 * This is provided by the device driver. 190 */ 191void draw_set_rasterize_stage( struct draw_context *draw, 192 struct draw_stage *stage ) 193{ 194 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 195 196 draw->pipeline.rasterize = stage; 197} 198 199 200/** 201 * Set the draw module's clipping state. 202 */ 203void draw_set_clip_state( struct draw_context *draw, 204 const struct pipe_clip_state *clip ) 205{ 206 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 207 208 assert(clip->nr <= PIPE_MAX_CLIP_PLANES); 209 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0])); 210 draw->nr_planes = 6 + clip->nr; 211} 212 213 214/** 215 * Set the draw module's viewport state. 216 */ 217void draw_set_viewport_state( struct draw_context *draw, 218 const struct pipe_viewport_state *viewport ) 219{ 220 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 221 draw->viewport = *viewport; /* struct copy */ 222 draw->identity_viewport = (viewport->scale[0] == 1.0f && 223 viewport->scale[1] == 1.0f && 224 viewport->scale[2] == 1.0f && 225 viewport->scale[3] == 1.0f && 226 viewport->translate[0] == 0.0f && 227 viewport->translate[1] == 0.0f && 228 viewport->translate[2] == 0.0f && 229 viewport->translate[3] == 0.0f); 230 231 draw_vs_set_viewport( draw, viewport ); 232} 233 234 235 236void 237draw_set_vertex_buffers(struct draw_context *draw, 238 unsigned count, 239 const struct pipe_vertex_buffer *buffers) 240{ 241 assert(count <= PIPE_MAX_ATTRIBS); 242 243 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0])); 244 draw->pt.nr_vertex_buffers = count; 245} 246 247 248void 249draw_set_vertex_elements(struct draw_context *draw, 250 unsigned count, 251 const struct pipe_vertex_element *elements) 252{ 253 assert(count <= PIPE_MAX_ATTRIBS); 254 255 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0])); 256 draw->pt.nr_vertex_elements = count; 257} 258 259 260/** 261 * Tell drawing context where to find mapped vertex buffers. 262 */ 263void 264draw_set_mapped_vertex_buffer(struct draw_context *draw, 265 unsigned attr, const void *buffer) 266{ 267 draw->pt.user.vbuffer[attr] = buffer; 268} 269 270 271void 272draw_set_mapped_constant_buffer(struct draw_context *draw, 273 unsigned shader_type, 274 unsigned slot, 275 const void *buffer, 276 unsigned size ) 277{ 278 debug_assert(shader_type == PIPE_SHADER_VERTEX || 279 shader_type == PIPE_SHADER_GEOMETRY); 280 debug_assert(slot < PIPE_MAX_CONSTANT_BUFFERS); 281 282 if (shader_type == PIPE_SHADER_VERTEX) { 283 draw->pt.user.vs_constants[slot] = buffer; 284 draw_vs_set_constants(draw, slot, buffer, size); 285 } else if (shader_type == PIPE_SHADER_GEOMETRY) { 286 draw->pt.user.gs_constants[slot] = buffer; 287 draw_gs_set_constants(draw, slot, buffer, size); 288 } 289} 290 291 292/** 293 * Tells the draw module to draw points with triangles if their size 294 * is greater than this threshold. 295 */ 296void 297draw_wide_point_threshold(struct draw_context *draw, float threshold) 298{ 299 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 300 draw->pipeline.wide_point_threshold = threshold; 301} 302 303 304/** 305 * Tells the draw module to draw lines with triangles if their width 306 * is greater than this threshold. 307 */ 308void 309draw_wide_line_threshold(struct draw_context *draw, float threshold) 310{ 311 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 312 draw->pipeline.wide_line_threshold = threshold; 313} 314 315 316/** 317 * Tells the draw module whether or not to implement line stipple. 318 */ 319void 320draw_enable_line_stipple(struct draw_context *draw, boolean enable) 321{ 322 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 323 draw->pipeline.line_stipple = enable; 324} 325 326 327/** 328 * Tells draw module whether to convert points to quads for sprite mode. 329 */ 330void 331draw_enable_point_sprites(struct draw_context *draw, boolean enable) 332{ 333 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 334 draw->pipeline.point_sprite = enable; 335} 336 337 338void 339draw_set_force_passthrough( struct draw_context *draw, boolean enable ) 340{ 341 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 342 draw->force_passthrough = enable; 343} 344 345 346/** 347 * Ask the draw module for the location/slot of the given vertex attribute in 348 * a post-transformed vertex. 349 * 350 * With this function, drivers that use the draw module should have no reason 351 * to track the current vertex/geometry shader. 352 * 353 * Note that the draw module may sometimes generate vertices with extra 354 * attributes (such as texcoords for AA lines). The driver can call this 355 * function to find those attributes. 356 * 357 * Zero is returned if the attribute is not found since this is 358 * a don't care / undefined situtation. Returning -1 would be a bit more 359 * work for the drivers. 360 */ 361int 362draw_find_shader_output(const struct draw_context *draw, 363 uint semantic_name, uint semantic_index) 364{ 365 const struct draw_vertex_shader *vs = draw->vs.vertex_shader; 366 const struct draw_geometry_shader *gs = draw->gs.geometry_shader; 367 uint i; 368 const struct tgsi_shader_info *info = &vs->info; 369 370 if (gs) 371 info = &gs->info; 372 373 for (i = 0; i < info->num_outputs; i++) { 374 if (info->output_semantic_name[i] == semantic_name && 375 info->output_semantic_index[i] == semantic_index) 376 return i; 377 } 378 379 /* XXX there may be more than one extra vertex attrib. 380 * For example, simulated gl_FragCoord and gl_PointCoord. 381 */ 382 if (draw->extra_shader_outputs.semantic_name == semantic_name && 383 draw->extra_shader_outputs.semantic_index == semantic_index) { 384 return draw->extra_shader_outputs.slot; 385 } 386 387 return 0; 388} 389 390 391/** 392 * Return total number of the shader outputs. This function is similar to 393 * draw_current_shader_outputs() but this function also counts any extra 394 * vertex/geometry output attributes that may be filled in by some draw 395 * stages (such as AA point, AA line). 396 * 397 * If geometry shader is present, its output will be returned, 398 * if not vertex shader is used. 399 */ 400uint 401draw_num_shader_outputs(const struct draw_context *draw) 402{ 403 uint count = draw->vs.vertex_shader->info.num_outputs; 404 405 /* If a geometry shader is present, its outputs go to the 406 * driver, else the vertex shader's outputs. 407 */ 408 if (draw->gs.geometry_shader) 409 count = draw->gs.geometry_shader->info.num_outputs; 410 411 if (draw->extra_shader_outputs.slot > 0) 412 count++; 413 return count; 414} 415 416 417/** 418 * Provide TGSI sampler objects for vertex/geometry shaders that use 419 * texture fetches. 420 * This might only be used by software drivers for the time being. 421 */ 422void 423draw_texture_samplers(struct draw_context *draw, 424 uint num_samplers, 425 struct tgsi_sampler **samplers) 426{ 427 draw->vs.num_samplers = num_samplers; 428 draw->vs.samplers = samplers; 429 draw->gs.num_samplers = num_samplers; 430 draw->gs.samplers = samplers; 431} 432 433 434 435 436void draw_set_render( struct draw_context *draw, 437 struct vbuf_render *render ) 438{ 439 draw->render = render; 440} 441 442 443 444/** 445 * Tell the drawing context about the index/element buffer to use 446 * (ala glDrawElements) 447 * If no element buffer is to be used (i.e. glDrawArrays) then this 448 * should be called with eltSize=0 and elements=NULL. 449 * 450 * \param draw the drawing context 451 * \param eltSize size of each element (1, 2 or 4 bytes) 452 * \param elements the element buffer ptr 453 */ 454void 455draw_set_mapped_element_buffer_range( struct draw_context *draw, 456 unsigned eltSize, 457 unsigned min_index, 458 unsigned max_index, 459 const void *elements ) 460{ 461 draw->pt.user.elts = elements; 462 draw->pt.user.eltSize = eltSize; 463 draw->pt.user.min_index = min_index; 464 draw->pt.user.max_index = max_index; 465} 466 467 468void 469draw_set_mapped_element_buffer( struct draw_context *draw, 470 unsigned eltSize, 471 const void *elements ) 472{ 473 draw->pt.user.elts = elements; 474 draw->pt.user.eltSize = eltSize; 475 draw->pt.user.min_index = 0; 476 draw->pt.user.max_index = 0xffffffff; 477} 478 479 480/* Revamp me please: 481 */ 482void draw_do_flush( struct draw_context *draw, unsigned flags ) 483{ 484 if (!draw->suspend_flushing) 485 { 486 assert(!draw->flushing); /* catch inadvertant recursion */ 487 488 draw->flushing = TRUE; 489 490 draw_pipeline_flush( draw, flags ); 491 492 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */ 493 494 draw->flushing = FALSE; 495 } 496} 497 498 499/** 500 * Return the number of output attributes produced by the geometry 501 * shader, if present. If no geometry shader, return the number of 502 * outputs from the vertex shader. 503 * \sa draw_num_shader_outputs 504 */ 505uint 506draw_current_shader_outputs(const struct draw_context *draw) 507{ 508 if (draw->gs.geometry_shader) 509 return draw->gs.num_gs_outputs; 510 return draw->vs.num_vs_outputs; 511} 512 513 514/** 515 * Return the index of the shader output which will contain the 516 * vertex position. 517 */ 518uint 519draw_current_shader_position_output(const struct draw_context *draw) 520{ 521 if (draw->gs.geometry_shader) 522 return draw->gs.position_output; 523 return draw->vs.position_output; 524} 525 526 527/** 528 * Return a pointer/handle for a driver/CSO rasterizer object which 529 * disabled culling, stippling, unfilled tris, etc. 530 * This is used by some pipeline stages (such as wide_point, aa_line 531 * and aa_point) which convert points/lines into triangles. In those 532 * cases we don't want to accidentally cull the triangles. 533 * 534 * \param scissor should the rasterizer state enable scissoring? 535 * \param flatshade should the rasterizer state use flat shading? 536 * \return rasterizer CSO handle 537 */ 538void * 539draw_get_rasterizer_no_cull( struct draw_context *draw, 540 boolean scissor, 541 boolean flatshade ) 542{ 543 if (!draw->rasterizer_no_cull[scissor][flatshade]) { 544 /* create now */ 545 struct pipe_context *pipe = draw->pipe; 546 struct pipe_rasterizer_state rast; 547 548 memset(&rast, 0, sizeof(rast)); 549 rast.scissor = scissor; 550 rast.flatshade = flatshade; 551 rast.front_winding = PIPE_WINDING_CCW; 552 rast.gl_rasterization_rules = draw->rasterizer->gl_rasterization_rules; 553 554 draw->rasterizer_no_cull[scissor][flatshade] = 555 pipe->create_rasterizer_state(pipe, &rast); 556 } 557 return draw->rasterizer_no_cull[scissor][flatshade]; 558} 559