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.

svga_draw_elements.c 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**********************************************************
  2. * Copyright 2008-2009 VMware, Inc. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. **********************************************************/
  25. #include "util/u_inlines.h"
  26. #include "util/u_upload_mgr.h"
  27. #include "indices/u_indices.h"
  28. #include "svga_cmd.h"
  29. #include "svga_draw.h"
  30. #include "svga_draw_private.h"
  31. #include "svga_resource_buffer.h"
  32. #include "svga_winsys.h"
  33. #include "svga_context.h"
  34. #include "svga_hw_reg.h"
  35. static enum pipe_error
  36. translate_indices( struct svga_hwtnl *hwtnl,
  37. struct pipe_resource *src,
  38. unsigned offset,
  39. unsigned nr,
  40. unsigned index_size,
  41. u_translate_func translate,
  42. struct pipe_resource **out_buf )
  43. {
  44. struct pipe_context *pipe = &hwtnl->svga->pipe;
  45. struct pipe_transfer *src_transfer = NULL;
  46. struct pipe_transfer *dst_transfer = NULL;
  47. unsigned size = index_size * nr;
  48. const void *src_map = NULL;
  49. struct pipe_resource *dst = NULL;
  50. void *dst_map = NULL;
  51. dst = pipe_buffer_create( pipe->screen,
  52. PIPE_BIND_INDEX_BUFFER,
  53. size );
  54. if (dst == NULL)
  55. goto fail;
  56. src_map = pipe_buffer_map( pipe, src, PIPE_TRANSFER_READ, &src_transfer );
  57. if (src_map == NULL)
  58. goto fail;
  59. dst_map = pipe_buffer_map( pipe, dst, PIPE_TRANSFER_WRITE, &dst_transfer );
  60. if (dst_map == NULL)
  61. goto fail;
  62. translate( (const char *)src_map + offset,
  63. nr,
  64. dst_map );
  65. pipe_buffer_unmap( pipe, src, src_transfer );
  66. pipe_buffer_unmap( pipe, dst, dst_transfer );
  67. *out_buf = dst;
  68. return PIPE_OK;
  69. fail:
  70. if (src_map)
  71. pipe_buffer_unmap( pipe, src, src_transfer );
  72. if (dst_map)
  73. pipe_buffer_unmap( pipe, dst, dst_transfer );
  74. if (dst)
  75. pipe->screen->resource_destroy( pipe->screen, dst );
  76. return PIPE_ERROR_OUT_OF_MEMORY;
  77. }
  78. enum pipe_error
  79. svga_hwtnl_simple_draw_range_elements( struct svga_hwtnl *hwtnl,
  80. struct pipe_resource *index_buffer,
  81. unsigned index_size,
  82. int index_bias,
  83. unsigned min_index,
  84. unsigned max_index,
  85. unsigned prim,
  86. unsigned start,
  87. unsigned count )
  88. {
  89. struct pipe_resource *upload_buffer = NULL;
  90. SVGA3dPrimitiveRange range;
  91. unsigned hw_prim;
  92. unsigned hw_count;
  93. unsigned index_offset = start * index_size;
  94. int ret = PIPE_OK;
  95. hw_prim = svga_translate_prim(prim, count, &hw_count);
  96. if (hw_count == 0)
  97. goto done;
  98. if (index_buffer &&
  99. svga_buffer_is_user_buffer(index_buffer))
  100. {
  101. assert( index_buffer->width0 >= index_offset + count * index_size );
  102. ret = u_upload_buffer( hwtnl->upload_ib,
  103. index_offset,
  104. count * index_size,
  105. index_buffer,
  106. &index_offset,
  107. &upload_buffer );
  108. if (ret)
  109. goto done;
  110. /* Don't need to worry about refcounting index_buffer as this is
  111. * just a stack variable without a counted reference of its own.
  112. * The caller holds the reference.
  113. */
  114. index_buffer = upload_buffer;
  115. }
  116. range.primType = hw_prim;
  117. range.primitiveCount = hw_count;
  118. range.indexArray.offset = index_offset;
  119. range.indexArray.stride = index_size;
  120. range.indexWidth = index_size;
  121. range.indexBias = index_bias;
  122. ret = svga_hwtnl_prim( hwtnl, &range, min_index, max_index, index_buffer );
  123. if (ret)
  124. goto done;
  125. done:
  126. if (upload_buffer)
  127. pipe_resource_reference( &upload_buffer, NULL );
  128. return ret;
  129. }
  130. enum pipe_error
  131. svga_hwtnl_draw_range_elements( struct svga_hwtnl *hwtnl,
  132. struct pipe_resource *index_buffer,
  133. unsigned index_size,
  134. int index_bias,
  135. unsigned min_index,
  136. unsigned max_index,
  137. unsigned prim, unsigned start, unsigned count)
  138. {
  139. unsigned gen_prim, gen_size, gen_nr, gen_type;
  140. u_translate_func gen_func;
  141. enum pipe_error ret = PIPE_OK;
  142. if (hwtnl->api_fillmode != PIPE_POLYGON_MODE_FILL &&
  143. prim >= PIPE_PRIM_TRIANGLES)
  144. {
  145. gen_type = u_unfilled_translator( prim,
  146. index_size,
  147. count,
  148. hwtnl->api_fillmode,
  149. &gen_prim,
  150. &gen_size,
  151. &gen_nr,
  152. &gen_func );
  153. }
  154. else
  155. {
  156. gen_type = u_index_translator( svga_hw_prims,
  157. prim,
  158. index_size,
  159. count,
  160. hwtnl->api_pv,
  161. hwtnl->hw_pv,
  162. &gen_prim,
  163. &gen_size,
  164. &gen_nr,
  165. &gen_func );
  166. }
  167. if (gen_type == U_TRANSLATE_MEMCPY) {
  168. /* No need for translation, just pass through to hardware:
  169. */
  170. return svga_hwtnl_simple_draw_range_elements( hwtnl, index_buffer,
  171. index_size,
  172. index_bias,
  173. min_index,
  174. max_index,
  175. gen_prim, start, count );
  176. }
  177. else {
  178. struct pipe_resource *gen_buf = NULL;
  179. /* Need to allocate a new index buffer and run the translate
  180. * func to populate it. Could potentially cache this translated
  181. * index buffer with the original to avoid future
  182. * re-translations. Not much point if we're just accelerating
  183. * GL though, as index buffers are typically used only once
  184. * there.
  185. */
  186. ret = translate_indices( hwtnl,
  187. index_buffer,
  188. start * index_size,
  189. gen_nr,
  190. gen_size,
  191. gen_func,
  192. &gen_buf );
  193. if (ret)
  194. goto done;
  195. ret = svga_hwtnl_simple_draw_range_elements( hwtnl,
  196. gen_buf,
  197. gen_size,
  198. index_bias,
  199. min_index,
  200. max_index,
  201. gen_prim,
  202. 0,
  203. gen_nr );
  204. if (ret)
  205. goto done;
  206. done:
  207. if (gen_buf)
  208. pipe_resource_reference( &gen_buf, NULL );
  209. return ret;
  210. }
  211. }