Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw_pipe_vbuf.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. * \file
  29. * Vertex buffer drawing stage.
  30. *
  31. * \author José Fonseca <jrfonsec@tungstengraphics.com>
  32. * \author Keith Whitwell <keith@tungstengraphics.com>
  33. */
  34. #include "pipe/p_debug.h"
  35. #include "util/u_math.h"
  36. #include "util/u_memory.h"
  37. #include "draw_vbuf.h"
  38. #include "draw_private.h"
  39. #include "draw_vertex.h"
  40. #include "draw_pipe.h"
  41. #include "translate/translate.h"
  42. #include "translate/translate_cache.h"
  43. /**
  44. * Vertex buffer emit stage.
  45. */
  46. struct vbuf_stage {
  47. struct draw_stage stage; /**< This must be first (base class) */
  48. struct vbuf_render *render;
  49. const struct vertex_info *vinfo;
  50. /** Vertex size in bytes */
  51. unsigned vertex_size;
  52. struct translate *translate;
  53. /* FIXME: we have no guarantee that 'unsigned' is 32bit */
  54. /** Vertices in hardware format */
  55. unsigned *vertices;
  56. unsigned *vertex_ptr;
  57. unsigned max_vertices;
  58. unsigned nr_vertices;
  59. /** Indices */
  60. ushort *indices;
  61. unsigned max_indices;
  62. unsigned nr_indices;
  63. /* Cache point size somewhere it's address won't change:
  64. */
  65. float point_size;
  66. struct translate_cache *cache;
  67. };
  68. /**
  69. * Basically a cast wrapper.
  70. */
  71. static INLINE struct vbuf_stage *
  72. vbuf_stage( struct draw_stage *stage )
  73. {
  74. assert(stage);
  75. return (struct vbuf_stage *)stage;
  76. }
  77. static void vbuf_flush_indices( struct vbuf_stage *vbuf );
  78. static void vbuf_flush_vertices( struct vbuf_stage *vbuf );
  79. static void vbuf_alloc_vertices( struct vbuf_stage *vbuf );
  80. static INLINE boolean
  81. overflow( void *map, void *ptr, unsigned bytes, unsigned bufsz )
  82. {
  83. unsigned long used = (unsigned long) ((char *)ptr - (char *)map);
  84. return (used + bytes) > bufsz;
  85. }
  86. static INLINE void
  87. check_space( struct vbuf_stage *vbuf, unsigned nr )
  88. {
  89. if (vbuf->nr_vertices + nr > vbuf->max_vertices ) {
  90. vbuf_flush_vertices(vbuf);
  91. vbuf_alloc_vertices(vbuf);
  92. }
  93. if (vbuf->nr_indices + nr > vbuf->max_indices )
  94. vbuf_flush_indices(vbuf);
  95. }
  96. /**
  97. * Extract the needed fields from post-transformed vertex and emit
  98. * a hardware(driver) vertex.
  99. * Recall that the vertices are constructed by the 'draw' module and
  100. * have a couple of slots at the beginning (1-dword header, 4-dword
  101. * clip pos) that we ignore here. We only use the vertex->data[] fields.
  102. */
  103. static INLINE ushort
  104. emit_vertex( struct vbuf_stage *vbuf,
  105. struct vertex_header *vertex )
  106. {
  107. if(vertex->vertex_id == UNDEFINED_VERTEX_ID) {
  108. /* Hmm - vertices are emitted one at a time - better make sure
  109. * set_buffer is efficient. Consider a special one-shot mode for
  110. * translate.
  111. */
  112. /* Note: we really do want data[0] here, not data[pos]:
  113. */
  114. vbuf->translate->set_buffer(vbuf->translate, 0, vertex->data[0], 0);
  115. vbuf->translate->run(vbuf->translate, 0, 1, vbuf->vertex_ptr);
  116. if (0) draw_dump_emitted_vertex(vbuf->vinfo, (uint8_t *)vbuf->vertex_ptr);
  117. vbuf->vertex_ptr += vbuf->vertex_size/4;
  118. vertex->vertex_id = vbuf->nr_vertices++;
  119. }
  120. return (ushort)vertex->vertex_id;
  121. }
  122. static void
  123. vbuf_tri( struct draw_stage *stage,
  124. struct prim_header *prim )
  125. {
  126. struct vbuf_stage *vbuf = vbuf_stage( stage );
  127. unsigned i;
  128. check_space( vbuf, 3 );
  129. for (i = 0; i < 3; i++) {
  130. vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] );
  131. }
  132. }
  133. static void
  134. vbuf_line( struct draw_stage *stage,
  135. struct prim_header *prim )
  136. {
  137. struct vbuf_stage *vbuf = vbuf_stage( stage );
  138. unsigned i;
  139. check_space( vbuf, 2 );
  140. for (i = 0; i < 2; i++) {
  141. vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[i] );
  142. }
  143. }
  144. static void
  145. vbuf_point( struct draw_stage *stage,
  146. struct prim_header *prim )
  147. {
  148. struct vbuf_stage *vbuf = vbuf_stage( stage );
  149. check_space( vbuf, 1 );
  150. vbuf->indices[vbuf->nr_indices++] = emit_vertex( vbuf, prim->v[0] );
  151. }
  152. /**
  153. * Set the prim type for subsequent vertices.
  154. * This may result in a new vertex size. The existing vbuffer (if any)
  155. * will be flushed if needed and a new one allocated.
  156. */
  157. static void
  158. vbuf_set_prim( struct vbuf_stage *vbuf, uint prim )
  159. {
  160. struct translate_key hw_key;
  161. unsigned dst_offset;
  162. unsigned i;
  163. vbuf->render->set_primitive(vbuf->render, prim);
  164. /* Must do this after set_primitive() above:
  165. *
  166. * XXX: need some state managment to track when this needs to be
  167. * recalculated. The driver should tell us whether there was a
  168. * state change.
  169. */
  170. vbuf->vinfo = vbuf->render->get_vertex_info(vbuf->render);
  171. if (vbuf->vertex_size != vbuf->vinfo->size * sizeof(float)) {
  172. vbuf_flush_vertices(vbuf);
  173. vbuf->vertex_size = vbuf->vinfo->size * sizeof(float);
  174. }
  175. /* Translate from pipeline vertices to hw vertices.
  176. */
  177. dst_offset = 0;
  178. for (i = 0; i < vbuf->vinfo->num_attribs; i++) {
  179. unsigned emit_sz = 0;
  180. unsigned src_buffer = 0;
  181. unsigned output_format;
  182. unsigned src_offset = (vbuf->vinfo->attrib[i].src_index * 4 * sizeof(float) );
  183. switch (vbuf->vinfo->attrib[i].emit) {
  184. case EMIT_4F:
  185. output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
  186. emit_sz = 4 * sizeof(float);
  187. break;
  188. case EMIT_3F:
  189. output_format = PIPE_FORMAT_R32G32B32_FLOAT;
  190. emit_sz = 3 * sizeof(float);
  191. break;
  192. case EMIT_2F:
  193. output_format = PIPE_FORMAT_R32G32_FLOAT;
  194. emit_sz = 2 * sizeof(float);
  195. break;
  196. case EMIT_1F:
  197. output_format = PIPE_FORMAT_R32_FLOAT;
  198. emit_sz = 1 * sizeof(float);
  199. break;
  200. case EMIT_1F_PSIZE:
  201. output_format = PIPE_FORMAT_R32_FLOAT;
  202. emit_sz = 1 * sizeof(float);
  203. src_buffer = 1;
  204. src_offset = 0;
  205. break;
  206. case EMIT_4UB:
  207. output_format = PIPE_FORMAT_B8G8R8A8_UNORM;
  208. emit_sz = 4 * sizeof(ubyte);
  209. break;
  210. default:
  211. assert(0);
  212. output_format = PIPE_FORMAT_NONE;
  213. emit_sz = 0;
  214. break;
  215. }
  216. hw_key.element[i].input_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
  217. hw_key.element[i].input_buffer = src_buffer;
  218. hw_key.element[i].input_offset = src_offset;
  219. hw_key.element[i].output_format = output_format;
  220. hw_key.element[i].output_offset = dst_offset;
  221. dst_offset += emit_sz;
  222. }
  223. hw_key.nr_elements = vbuf->vinfo->num_attribs;
  224. hw_key.output_stride = vbuf->vinfo->size * 4;
  225. /* Don't bother with caching at this stage:
  226. */
  227. if (!vbuf->translate ||
  228. translate_key_compare(&vbuf->translate->key, &hw_key) != 0)
  229. {
  230. translate_key_sanitize(&hw_key);
  231. vbuf->translate = translate_cache_find(vbuf->cache, &hw_key);
  232. vbuf->translate->set_buffer(vbuf->translate, 1, &vbuf->point_size, 0);
  233. }
  234. vbuf->point_size = vbuf->stage.draw->rasterizer->point_size;
  235. /* Allocate new buffer?
  236. */
  237. if (!vbuf->vertices)
  238. vbuf_alloc_vertices(vbuf);
  239. }
  240. static void
  241. vbuf_first_tri( struct draw_stage *stage,
  242. struct prim_header *prim )
  243. {
  244. struct vbuf_stage *vbuf = vbuf_stage( stage );
  245. vbuf_flush_indices( vbuf );
  246. stage->tri = vbuf_tri;
  247. vbuf_set_prim(vbuf, PIPE_PRIM_TRIANGLES);
  248. stage->tri( stage, prim );
  249. }
  250. static void
  251. vbuf_first_line( struct draw_stage *stage,
  252. struct prim_header *prim )
  253. {
  254. struct vbuf_stage *vbuf = vbuf_stage( stage );
  255. vbuf_flush_indices( vbuf );
  256. stage->line = vbuf_line;
  257. vbuf_set_prim(vbuf, PIPE_PRIM_LINES);
  258. stage->line( stage, prim );
  259. }
  260. static void
  261. vbuf_first_point( struct draw_stage *stage,
  262. struct prim_header *prim )
  263. {
  264. struct vbuf_stage *vbuf = vbuf_stage( stage );
  265. vbuf_flush_indices( vbuf );
  266. stage->point = vbuf_point;
  267. vbuf_set_prim(vbuf, PIPE_PRIM_POINTS);
  268. stage->point( stage, prim );
  269. }
  270. static void
  271. vbuf_flush_indices( struct vbuf_stage *vbuf )
  272. {
  273. if(!vbuf->nr_indices)
  274. return;
  275. assert((uint) (vbuf->vertex_ptr - vbuf->vertices) ==
  276. vbuf->nr_vertices * vbuf->vertex_size / sizeof(unsigned));
  277. vbuf->render->draw(vbuf->render, vbuf->indices, vbuf->nr_indices);
  278. vbuf->nr_indices = 0;
  279. }
  280. /**
  281. * Flush existing vertex buffer and allocate a new one.
  282. *
  283. * XXX: We separate flush-on-index-full and flush-on-vb-full, but may
  284. * raise issues uploading vertices if the hardware wants to flush when
  285. * we flush.
  286. */
  287. static void
  288. vbuf_flush_vertices( struct vbuf_stage *vbuf )
  289. {
  290. if(vbuf->vertices) {
  291. vbuf_flush_indices(vbuf);
  292. /* Reset temporary vertices ids */
  293. if(vbuf->nr_vertices)
  294. draw_reset_vertex_ids( vbuf->stage.draw );
  295. /* Free the vertex buffer */
  296. vbuf->render->release_vertices(vbuf->render,
  297. vbuf->vertices,
  298. vbuf->vertex_size,
  299. vbuf->nr_vertices);
  300. vbuf->max_vertices = vbuf->nr_vertices = 0;
  301. vbuf->vertex_ptr = vbuf->vertices = NULL;
  302. }
  303. }
  304. static void
  305. vbuf_alloc_vertices( struct vbuf_stage *vbuf )
  306. {
  307. assert(!vbuf->nr_indices);
  308. assert(!vbuf->vertices);
  309. /* Allocate a new vertex buffer */
  310. vbuf->max_vertices = vbuf->render->max_vertex_buffer_bytes / vbuf->vertex_size;
  311. /* even number */
  312. vbuf->max_vertices = vbuf->max_vertices & ~1;
  313. /* Must always succeed -- driver gives us a
  314. * 'max_vertex_buffer_bytes' which it guarantees it can allocate,
  315. * and it will flush itself if necessary to do so. If this does
  316. * fail, we are basically without usable hardware.
  317. */
  318. vbuf->vertices = (uint *) vbuf->render->allocate_vertices(vbuf->render,
  319. (ushort) vbuf->vertex_size,
  320. (ushort) vbuf->max_vertices);
  321. vbuf->vertex_ptr = vbuf->vertices;
  322. }
  323. static void
  324. vbuf_flush( struct draw_stage *stage, unsigned flags )
  325. {
  326. struct vbuf_stage *vbuf = vbuf_stage( stage );
  327. vbuf_flush_indices( vbuf );
  328. stage->point = vbuf_first_point;
  329. stage->line = vbuf_first_line;
  330. stage->tri = vbuf_first_tri;
  331. if (flags & DRAW_FLUSH_BACKEND)
  332. vbuf_flush_vertices( vbuf );
  333. }
  334. static void
  335. vbuf_reset_stipple_counter( struct draw_stage *stage )
  336. {
  337. /* XXX: Need to do something here for hardware with linestipple.
  338. */
  339. (void) stage;
  340. }
  341. static void vbuf_destroy( struct draw_stage *stage )
  342. {
  343. struct vbuf_stage *vbuf = vbuf_stage( stage );
  344. if(vbuf->indices)
  345. align_free( vbuf->indices );
  346. if (vbuf->render)
  347. vbuf->render->destroy( vbuf->render );
  348. if (vbuf->cache)
  349. translate_cache_destroy(vbuf->cache);
  350. FREE( stage );
  351. }
  352. /**
  353. * Create a new primitive vbuf/render stage.
  354. */
  355. struct draw_stage *draw_vbuf_stage( struct draw_context *draw,
  356. struct vbuf_render *render )
  357. {
  358. struct vbuf_stage *vbuf = CALLOC_STRUCT(vbuf_stage);
  359. if (vbuf == NULL)
  360. goto fail;
  361. vbuf->stage.draw = draw;
  362. vbuf->stage.point = vbuf_first_point;
  363. vbuf->stage.line = vbuf_first_line;
  364. vbuf->stage.tri = vbuf_first_tri;
  365. vbuf->stage.flush = vbuf_flush;
  366. vbuf->stage.reset_stipple_counter = vbuf_reset_stipple_counter;
  367. vbuf->stage.destroy = vbuf_destroy;
  368. vbuf->render = render;
  369. vbuf->max_indices = MAX2(render->max_indices, UNDEFINED_VERTEX_ID-1);
  370. vbuf->indices = (ushort *) align_malloc( vbuf->max_indices *
  371. sizeof(vbuf->indices[0]),
  372. 16 );
  373. if (!vbuf->indices)
  374. goto fail;
  375. vbuf->cache = translate_cache_create();
  376. if (!vbuf->cache)
  377. goto fail;
  378. vbuf->vertices = NULL;
  379. vbuf->vertex_ptr = vbuf->vertices;
  380. return &vbuf->stage;
  381. fail:
  382. if (vbuf)
  383. vbuf_destroy(&vbuf->stage);
  384. return NULL;
  385. }