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