draw_context.c revision d8f2e400cfe6e32e82d1656d3483905343124b97
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_util.h" 35#include "draw_context.h" 36#include "draw_vbuf.h" 37#include "draw_vs.h" 38#include "draw_pt.h" 39#include "draw_pipe.h" 40 41 42struct draw_context *draw_create( void ) 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 tgsi_exec_machine_init(&draw->machine); 60 61 /* FIXME: give this machine thing a proper constructor: 62 */ 63 draw->machine.Inputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16); 64 draw->machine.Outputs = align_malloc(PIPE_MAX_ATTRIBS * sizeof(struct tgsi_exec_vector), 16); 65 66 if (!draw_pipeline_init( draw )) 67 goto fail; 68 69 if (!draw_pt_init( draw )) 70 goto fail; 71 72 return draw; 73 74fail: 75 draw_destroy( draw ); 76 return NULL; 77} 78 79 80void draw_destroy( struct draw_context *draw ) 81{ 82 if (!draw) 83 return; 84 85 86 if (draw->machine.Inputs) 87 align_free(draw->machine.Inputs); 88 89 if (draw->machine.Outputs) 90 align_free(draw->machine.Outputs); 91 92 tgsi_exec_machine_free_data(&draw->machine); 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 103 FREE( draw ); 104} 105 106 107 108void draw_flush( struct draw_context *draw ) 109{ 110 draw_do_flush( draw, DRAW_FLUSH_BACKEND ); 111} 112 113 114 115/** 116 * Register new primitive rasterization/rendering state. 117 * This causes the drawing pipeline to be rebuilt. 118 */ 119void draw_set_rasterizer_state( struct draw_context *draw, 120 const struct pipe_rasterizer_state *raster ) 121{ 122 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 123 124 draw->rasterizer = raster; 125 draw->bypass_clipping = 126 ((draw->rasterizer && draw->rasterizer->bypass_clipping) || 127 draw->driver.bypass_clipping); 128} 129 130 131void draw_set_driver_clipping( struct draw_context *draw, 132 boolean bypass_clipping ) 133{ 134 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 135 136 draw->driver.bypass_clipping = bypass_clipping; 137 draw->bypass_clipping = (draw->rasterizer->bypass_clipping || 138 draw->driver.bypass_clipping); 139} 140 141 142/** 143 * Plug in the primitive rendering/rasterization stage (which is the last 144 * stage in the drawing pipeline). 145 * This is provided by the device driver. 146 */ 147void draw_set_rasterize_stage( struct draw_context *draw, 148 struct draw_stage *stage ) 149{ 150 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 151 152 draw->pipeline.rasterize = stage; 153} 154 155 156/** 157 * Set the draw module's clipping state. 158 */ 159void draw_set_clip_state( struct draw_context *draw, 160 const struct pipe_clip_state *clip ) 161{ 162 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 163 164 assert(clip->nr <= PIPE_MAX_CLIP_PLANES); 165 memcpy(&draw->plane[6], clip->ucp, clip->nr * sizeof(clip->ucp[0])); 166 draw->nr_planes = 6 + clip->nr; 167} 168 169 170/** 171 * Set the draw module's viewport state. 172 */ 173void draw_set_viewport_state( struct draw_context *draw, 174 const struct pipe_viewport_state *viewport ) 175{ 176 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 177 draw->viewport = *viewport; /* struct copy */ 178 draw->identity_viewport = (viewport->scale[0] == 1.0f && 179 viewport->scale[1] == 1.0f && 180 viewport->scale[2] == 1.0f && 181 viewport->scale[3] == 1.0f && 182 viewport->translate[0] == 0.0f && 183 viewport->translate[1] == 0.0f && 184 viewport->translate[2] == 0.0f && 185 viewport->translate[3] == 0.0f); 186} 187 188 189 190void 191draw_set_vertex_buffers(struct draw_context *draw, 192 unsigned count, 193 const struct pipe_vertex_buffer *buffers) 194{ 195 assert(count <= PIPE_MAX_ATTRIBS); 196 197 memcpy(draw->pt.vertex_buffer, buffers, count * sizeof(buffers[0])); 198 draw->pt.nr_vertex_buffers = count; 199} 200 201 202void 203draw_set_vertex_elements(struct draw_context *draw, 204 unsigned count, 205 const struct pipe_vertex_element *elements) 206{ 207 assert(count <= PIPE_MAX_ATTRIBS); 208 209 memcpy(draw->pt.vertex_element, elements, count * sizeof(elements[0])); 210 draw->pt.nr_vertex_elements = count; 211} 212 213 214/** 215 * Tell drawing context where to find mapped vertex buffers. 216 */ 217void 218draw_set_mapped_vertex_buffer(struct draw_context *draw, 219 unsigned attr, const void *buffer) 220{ 221 draw->pt.user.vbuffer[attr] = buffer; 222} 223 224 225void 226draw_set_mapped_constant_buffer(struct draw_context *draw, 227 const void *buffer) 228{ 229 draw->pt.user.constants = buffer; 230} 231 232 233/** 234 * Tells the draw module to draw points with triangles if their size 235 * is greater than this threshold. 236 */ 237void 238draw_wide_point_threshold(struct draw_context *draw, float threshold) 239{ 240 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 241 draw->pipeline.wide_point_threshold = threshold; 242} 243 244 245/** 246 * Tells the draw module to draw lines with triangles if their width 247 * is greater than this threshold. 248 */ 249void 250draw_wide_line_threshold(struct draw_context *draw, float threshold) 251{ 252 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 253 draw->pipeline.wide_line_threshold = threshold; 254} 255 256 257/** 258 * Tells the draw module whether or not to implement line stipple. 259 */ 260void 261draw_enable_line_stipple(struct draw_context *draw, boolean enable) 262{ 263 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 264 draw->pipeline.line_stipple = enable; 265} 266 267 268/** 269 * Tells draw module whether to convert points to quads for sprite mode. 270 */ 271void 272draw_enable_point_sprites(struct draw_context *draw, boolean enable) 273{ 274 draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE ); 275 draw->pipeline.point_sprite = enable; 276} 277 278 279/** 280 * Ask the draw module for the location/slot of the given vertex attribute in 281 * a post-transformed vertex. 282 * 283 * With this function, drivers that use the draw module should have no reason 284 * to track the current vertex shader. 285 * 286 * Note that the draw module may sometimes generate vertices with extra 287 * attributes (such as texcoords for AA lines). The driver can call this 288 * function to find those attributes. 289 * 290 * Zero is returned if the attribute is not found since this is 291 * a don't care / undefined situtation. Returning -1 would be a bit more 292 * work for the drivers. 293 */ 294int 295draw_find_vs_output(struct draw_context *draw, 296 uint semantic_name, uint semantic_index) 297{ 298 const struct draw_vertex_shader *vs = draw->vertex_shader; 299 uint i; 300 for (i = 0; i < vs->info.num_outputs; i++) { 301 if (vs->info.output_semantic_name[i] == semantic_name && 302 vs->info.output_semantic_index[i] == semantic_index) 303 return i; 304 } 305 306 /* XXX there may be more than one extra vertex attrib. 307 * For example, simulated gl_FragCoord and gl_PointCoord. 308 */ 309 if (draw->extra_vp_outputs.semantic_name == semantic_name && 310 draw->extra_vp_outputs.semantic_index == semantic_index) { 311 return draw->extra_vp_outputs.slot; 312 } 313 return 0; 314} 315 316 317/** 318 * Return number of vertex shader outputs. 319 */ 320uint 321draw_num_vs_outputs(struct draw_context *draw) 322{ 323 uint count = draw->vertex_shader->info.num_outputs; 324 if (draw->extra_vp_outputs.slot > 0) 325 count++; 326 return count; 327} 328 329 330 331void draw_set_render( struct draw_context *draw, 332 struct vbuf_render *render ) 333{ 334 draw->render = render; 335} 336 337void draw_set_edgeflags( struct draw_context *draw, 338 const unsigned *edgeflag ) 339{ 340 draw->pt.user.edgeflag = edgeflag; 341} 342 343 344 345 346/** 347 * Tell the drawing context about the index/element buffer to use 348 * (ala glDrawElements) 349 * If no element buffer is to be used (i.e. glDrawArrays) then this 350 * should be called with eltSize=0 and elements=NULL. 351 * 352 * \param draw the drawing context 353 * \param eltSize size of each element (1, 2 or 4 bytes) 354 * \param elements the element buffer ptr 355 */ 356void 357draw_set_mapped_element_buffer( struct draw_context *draw, 358 unsigned eltSize, void *elements ) 359{ 360 draw->pt.user.elts = elements; 361 draw->pt.user.eltSize = eltSize; 362} 363 364 365 366/* Revamp me please: 367 */ 368void draw_do_flush( struct draw_context *draw, unsigned flags ) 369{ 370 if (!draw->flushing) 371 { 372 draw->flushing = TRUE; 373 374 draw_pipeline_flush( draw, flags ); 375 376 draw->reduced_prim = ~0; /* is reduced_prim needed any more? */ 377 378 draw->flushing = FALSE; 379 } 380} 381