draw_pt_fetch_emit.c revision 488dd2c1912132fe7ee5e81b05fb64ba62a46098
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#include "util/u_memory.h" 34#include "draw/draw_context.h" 35#include "draw/draw_private.h" 36#include "draw/draw_vbuf.h" 37#include "draw/draw_vertex.h" 38#include "draw/draw_pt.h" 39#include "draw/draw_gs.h" 40#include "translate/translate.h" 41#include "translate/translate_cache.h" 42 43/* The simplest 'middle end' in the new vertex code. 44 * 45 * The responsibilities of a middle end are to: 46 * - perform vertex fetch using 47 * - draw vertex element/buffer state 48 * - a list of fetch indices we received as an input 49 * - run the vertex shader 50 * - cliptest, 51 * - clip coord calculation 52 * - viewport transformation 53 * - if necessary, run the primitive pipeline, passing it: 54 * - a linear array of vertex_header vertices constructed here 55 * - a set of draw indices we received as an input 56 * - otherwise, drive the hw backend, 57 * - allocate space for hardware format vertices 58 * - translate the vertex-shader output vertices to hw format 59 * - calling the backend draw functions. 60 * 61 * For convenience, we provide a helper function to drive the hardware 62 * backend given similar inputs to those required to run the pipeline. 63 * 64 * In the case of passthrough mode, many of these actions are disabled 65 * or noops, so we end up doing: 66 * 67 * - perform vertex fetch 68 * - drive the hw backend 69 * 70 * IE, basically just vertex fetch to post-vs-format vertices, 71 * followed by a call to the backend helper function. 72 */ 73 74 75struct fetch_emit_middle_end { 76 struct draw_pt_middle_end base; 77 struct draw_context *draw; 78 79 struct translate *translate; 80 const struct vertex_info *vinfo; 81 82 /* Cache point size somewhere it's address won't change: 83 */ 84 float point_size; 85 86 struct translate_cache *cache; 87}; 88 89 90 91 92static void fetch_emit_prepare( struct draw_pt_middle_end *middle, 93 unsigned prim, 94 unsigned opt, 95 unsigned *max_vertices ) 96{ 97 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle; 98 struct draw_context *draw = feme->draw; 99 const struct vertex_info *vinfo; 100 unsigned i, dst_offset; 101 struct translate_key key; 102 103 unsigned gs_out_prim = (draw->gs.geometry_shader ? 104 draw->gs.geometry_shader->output_primitive : 105 prim); 106 107 108 109 draw->render->set_primitive(draw->render, gs_out_prim); 110 111 /* Must do this after set_primitive() above: 112 */ 113 vinfo = feme->vinfo = draw->render->get_vertex_info(draw->render); 114 115 116 117 /* Transform from API vertices to HW vertices, skipping the 118 * pipeline_vertex intermediate step. 119 */ 120 dst_offset = 0; 121 memset(&key, 0, sizeof(key)); 122 123 for (i = 0; i < vinfo->num_attribs; i++) { 124 const struct pipe_vertex_element *src = &draw->pt.vertex_element[vinfo->attrib[i].src_index]; 125 126 unsigned emit_sz = 0; 127 unsigned input_format = src->src_format; 128 unsigned input_buffer = src->vertex_buffer_index; 129 unsigned input_offset = src->src_offset; 130 unsigned output_format; 131 132 output_format = draw_translate_vinfo_format(vinfo->attrib[i].emit); 133 emit_sz = draw_translate_vinfo_size(vinfo->attrib[i].emit); 134 135 if (vinfo->attrib[i].emit == EMIT_OMIT) 136 continue; 137 138 if (vinfo->attrib[i].emit == EMIT_1F_PSIZE) { 139 input_format = PIPE_FORMAT_R32_FLOAT; 140 input_buffer = draw->pt.nr_vertex_buffers; 141 input_offset = 0; 142 } 143 144 key.element[i].type = TRANSLATE_ELEMENT_NORMAL; 145 key.element[i].input_format = input_format; 146 key.element[i].input_buffer = input_buffer; 147 key.element[i].input_offset = input_offset; 148 key.element[i].instance_divisor = src->instance_divisor; 149 key.element[i].output_format = output_format; 150 key.element[i].output_offset = dst_offset; 151 152 dst_offset += emit_sz; 153 } 154 155 key.nr_elements = vinfo->num_attribs; 156 key.output_stride = vinfo->size * 4; 157 158 /* Don't bother with caching at this stage: 159 */ 160 if (!feme->translate || 161 translate_key_compare(&feme->translate->key, &key) != 0) 162 { 163 translate_key_sanitize(&key); 164 feme->translate = translate_cache_find(feme->cache, 165 &key); 166 167 168 feme->translate->set_buffer(feme->translate, 169 draw->pt.nr_vertex_buffers, 170 &feme->point_size, 171 0, 172 ~0); 173 } 174 175 feme->point_size = draw->rasterizer->point_size; 176 177 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) { 178 feme->translate->set_buffer(feme->translate, 179 i, 180 ((char *)draw->pt.user.vbuffer[i] + 181 draw->pt.vertex_buffer[i].buffer_offset), 182 draw->pt.vertex_buffer[i].stride, 183 draw->pt.max_index); 184 } 185 186 *max_vertices = (draw->render->max_vertex_buffer_bytes / 187 (vinfo->size * 4)); 188} 189 190 191 192 193 194static void fetch_emit_run( struct draw_pt_middle_end *middle, 195 const unsigned *fetch_elts, 196 unsigned fetch_count, 197 const ushort *draw_elts, 198 unsigned draw_count, 199 unsigned prim_flags ) 200{ 201 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle; 202 struct draw_context *draw = feme->draw; 203 void *hw_verts; 204 205 /* XXX: need to flush to get prim_vbuf.c to release its allocation?? 206 */ 207 draw_do_flush( draw, DRAW_FLUSH_BACKEND ); 208 209 draw->render->allocate_vertices( draw->render, 210 (ushort)feme->translate->key.output_stride, 211 (ushort)fetch_count ); 212 213 hw_verts = draw->render->map_vertices( draw->render ); 214 if (!hw_verts) { 215 assert(0); 216 return; 217 } 218 219 220 /* Single routine to fetch vertices and emit HW verts. 221 */ 222 feme->translate->run_elts( feme->translate, 223 fetch_elts, 224 fetch_count, 225 draw->instance_id, 226 hw_verts ); 227 228 if (0) { 229 unsigned i; 230 for (i = 0; i < fetch_count; i++) { 231 debug_printf("\n\nvertex %d:\n", i); 232 draw_dump_emitted_vertex( feme->vinfo, 233 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i ); 234 } 235 } 236 237 draw->render->unmap_vertices( draw->render, 238 0, 239 (ushort)(fetch_count - 1) ); 240 241 /* XXX: Draw arrays path to avoid re-emitting index list again and 242 * again. 243 */ 244 draw->render->draw_elements( draw->render, 245 draw_elts, 246 draw_count ); 247 248 /* Done -- that was easy, wasn't it: 249 */ 250 draw->render->release_vertices( draw->render ); 251 252} 253 254 255static void fetch_emit_run_linear( struct draw_pt_middle_end *middle, 256 unsigned start, 257 unsigned count, 258 unsigned prim_flags ) 259{ 260 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle; 261 struct draw_context *draw = feme->draw; 262 void *hw_verts; 263 264 /* XXX: need to flush to get prim_vbuf.c to release its allocation?? 265 */ 266 draw_do_flush( draw, DRAW_FLUSH_BACKEND ); 267 268 if (!draw->render->allocate_vertices( draw->render, 269 (ushort)feme->translate->key.output_stride, 270 (ushort)count )) 271 goto fail; 272 273 hw_verts = draw->render->map_vertices( draw->render ); 274 if (!hw_verts) 275 goto fail; 276 277 /* Single routine to fetch vertices and emit HW verts. 278 */ 279 feme->translate->run( feme->translate, 280 start, 281 count, 282 draw->instance_id, 283 hw_verts ); 284 285 if (0) { 286 unsigned i; 287 for (i = 0; i < count; i++) { 288 debug_printf("\n\nvertex %d:\n", i); 289 draw_dump_emitted_vertex( feme->vinfo, 290 (const uint8_t *)hw_verts + feme->vinfo->size * 4 * i ); 291 } 292 } 293 294 draw->render->unmap_vertices( draw->render, 0, count - 1 ); 295 296 /* XXX: Draw arrays path to avoid re-emitting index list again and 297 * again. 298 */ 299 draw->render->draw_arrays( draw->render, 0, count ); 300 301 /* Done -- that was easy, wasn't it: 302 */ 303 draw->render->release_vertices( draw->render ); 304 return; 305 306fail: 307 assert(0); 308 return; 309} 310 311 312static boolean fetch_emit_run_linear_elts( struct draw_pt_middle_end *middle, 313 unsigned start, 314 unsigned count, 315 const ushort *draw_elts, 316 unsigned draw_count, 317 unsigned prim_flags ) 318{ 319 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle; 320 struct draw_context *draw = feme->draw; 321 void *hw_verts; 322 323 /* XXX: need to flush to get prim_vbuf.c to release its allocation?? 324 */ 325 draw_do_flush( draw, DRAW_FLUSH_BACKEND ); 326 327 if (!draw->render->allocate_vertices( draw->render, 328 (ushort)feme->translate->key.output_stride, 329 (ushort)count )) 330 return FALSE; 331 332 hw_verts = draw->render->map_vertices( draw->render ); 333 if (!hw_verts) 334 return FALSE; 335 336 /* Single routine to fetch vertices and emit HW verts. 337 */ 338 feme->translate->run( feme->translate, 339 start, 340 count, 341 draw->instance_id, 342 hw_verts ); 343 344 draw->render->unmap_vertices( draw->render, 0, (ushort)(count - 1) ); 345 346 /* XXX: Draw arrays path to avoid re-emitting index list again and 347 * again. 348 */ 349 draw->render->draw_elements( draw->render, 350 draw_elts, 351 draw_count ); 352 353 /* Done -- that was easy, wasn't it: 354 */ 355 draw->render->release_vertices( draw->render ); 356 357 return TRUE; 358} 359 360 361 362 363static void fetch_emit_finish( struct draw_pt_middle_end *middle ) 364{ 365 /* nothing to do */ 366} 367 368static void fetch_emit_destroy( struct draw_pt_middle_end *middle ) 369{ 370 struct fetch_emit_middle_end *feme = (struct fetch_emit_middle_end *)middle; 371 372 if (feme->cache) 373 translate_cache_destroy(feme->cache); 374 375 FREE(middle); 376} 377 378 379struct draw_pt_middle_end *draw_pt_fetch_emit( struct draw_context *draw ) 380{ 381 struct fetch_emit_middle_end *fetch_emit = CALLOC_STRUCT( fetch_emit_middle_end ); 382 if (fetch_emit == NULL) 383 return NULL; 384 385 fetch_emit->cache = translate_cache_create(); 386 if (!fetch_emit->cache) { 387 FREE(fetch_emit); 388 return NULL; 389 } 390 391 fetch_emit->base.prepare = fetch_emit_prepare; 392 fetch_emit->base.run = fetch_emit_run; 393 fetch_emit->base.run_linear = fetch_emit_run_linear; 394 fetch_emit->base.run_linear_elts = fetch_emit_run_linear_elts; 395 fetch_emit->base.finish = fetch_emit_finish; 396 fetch_emit->base.destroy = fetch_emit_destroy; 397 398 fetch_emit->draw = draw; 399 400 return &fetch_emit->base; 401} 402 403