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