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_pipe_draw.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_format.h"
  26. #include "util/u_inlines.h"
  27. #include "util/u_prim.h"
  28. #include "util/u_prim_restart.h"
  29. #include "util/u_time.h"
  30. #include "util/u_upload_mgr.h"
  31. #include "indices/u_indices.h"
  32. #include "svga_hw_reg.h"
  33. #include "svga_cmd.h"
  34. #include "svga_context.h"
  35. #include "svga_screen.h"
  36. #include "svga_draw.h"
  37. #include "svga_shader.h"
  38. #include "svga_state.h"
  39. #include "svga_swtnl.h"
  40. #include "svga_debug.h"
  41. #include "svga_resource_buffer.h"
  42. static enum pipe_error
  43. retry_draw_range_elements( struct svga_context *svga,
  44. struct pipe_resource *index_buffer,
  45. unsigned index_size,
  46. int index_bias,
  47. unsigned min_index,
  48. unsigned max_index,
  49. enum pipe_prim_type prim,
  50. unsigned start,
  51. unsigned count,
  52. unsigned start_instance,
  53. unsigned instance_count,
  54. boolean do_retry )
  55. {
  56. enum pipe_error ret = PIPE_OK;
  57. SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWELEMENTS);
  58. svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
  59. ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
  60. if (ret != PIPE_OK)
  61. goto retry;
  62. /** determine if flatshade is to be used after svga_update_state()
  63. * in case the fragment shader is changed.
  64. */
  65. svga_hwtnl_set_flatshade(svga->hwtnl,
  66. svga->curr.rast->templ.flatshade ||
  67. svga->state.hw_draw.fs->uses_flat_interp,
  68. svga->curr.rast->templ.flatshade_first);
  69. ret = svga_hwtnl_draw_range_elements( svga->hwtnl,
  70. index_buffer, index_size, index_bias,
  71. min_index, max_index,
  72. prim, start, count,
  73. start_instance, instance_count);
  74. if (ret != PIPE_OK)
  75. goto retry;
  76. goto done;
  77. retry:
  78. svga_context_flush( svga, NULL );
  79. if (do_retry)
  80. {
  81. ret = retry_draw_range_elements(svga,
  82. index_buffer, index_size, index_bias,
  83. min_index, max_index,
  84. prim, start, count,
  85. start_instance, instance_count, FALSE);
  86. }
  87. done:
  88. SVGA_STATS_TIME_POP(svga_sws(svga));
  89. return ret;
  90. }
  91. static enum pipe_error
  92. retry_draw_arrays( struct svga_context *svga,
  93. enum pipe_prim_type prim, unsigned start, unsigned count,
  94. unsigned start_instance, unsigned instance_count,
  95. boolean do_retry )
  96. {
  97. enum pipe_error ret;
  98. SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWARRAYS);
  99. svga_hwtnl_set_fillmode(svga->hwtnl, svga->curr.rast->hw_fillmode);
  100. ret = svga_update_state( svga, SVGA_STATE_HW_DRAW );
  101. if (ret != PIPE_OK)
  102. goto retry;
  103. /** determine if flatshade is to be used after svga_update_state()
  104. * in case the fragment shader is changed.
  105. */
  106. svga_hwtnl_set_flatshade(svga->hwtnl,
  107. svga->curr.rast->templ.flatshade ||
  108. svga->state.hw_draw.fs->uses_flat_interp,
  109. svga->curr.rast->templ.flatshade_first);
  110. ret = svga_hwtnl_draw_arrays(svga->hwtnl, prim, start, count,
  111. start_instance, instance_count);
  112. if (ret != PIPE_OK)
  113. goto retry;
  114. goto done;
  115. retry:
  116. if (ret == PIPE_ERROR_OUT_OF_MEMORY && do_retry)
  117. {
  118. svga_context_flush( svga, NULL );
  119. ret = retry_draw_arrays(svga, prim, start, count,
  120. start_instance, instance_count,
  121. FALSE);
  122. }
  123. done:
  124. SVGA_STATS_TIME_POP(svga_sws(svga));
  125. return ret;
  126. }
  127. /**
  128. * Determine if we need to implement primitive restart with a fallback
  129. * path which breaks the original primitive into sub-primitive at the
  130. * restart indexes.
  131. */
  132. static boolean
  133. need_fallback_prim_restart(const struct svga_context *svga,
  134. const struct pipe_draw_info *info)
  135. {
  136. if (info->primitive_restart && info->indexed) {
  137. if (!svga_have_vgpu10(svga))
  138. return TRUE;
  139. else if (!svga->state.sw.need_swtnl) {
  140. if (svga->curr.ib.index_size == 1)
  141. return TRUE; /* no device support for 1-byte indexes */
  142. else if (svga->curr.ib.index_size == 2)
  143. return info->restart_index != 0xffff;
  144. else
  145. return info->restart_index != 0xffffffff;
  146. }
  147. }
  148. return FALSE;
  149. }
  150. static void
  151. svga_draw_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
  152. {
  153. struct svga_context *svga = svga_context( pipe );
  154. unsigned reduced_prim = u_reduced_prim( info->mode );
  155. unsigned count = info->count;
  156. enum pipe_error ret = 0;
  157. boolean needed_swtnl;
  158. SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_DRAWVBO);
  159. svga->hud.num_draw_calls++; /* for SVGA_QUERY_NUM_DRAW_CALLS */
  160. if (u_reduced_prim(info->mode) == PIPE_PRIM_TRIANGLES &&
  161. svga->curr.rast->templ.cull_face == PIPE_FACE_FRONT_AND_BACK)
  162. goto done;
  163. /*
  164. * Mark currently bound target surfaces as dirty
  165. * doesn't really matter if it is done before drawing.
  166. *
  167. * TODO If we ever normaly return something other then
  168. * true we should not mark it as dirty then.
  169. */
  170. svga_mark_surfaces_dirty(svga_context(pipe));
  171. if (svga->curr.reduced_prim != reduced_prim) {
  172. svga->curr.reduced_prim = reduced_prim;
  173. svga->dirty |= SVGA_NEW_REDUCED_PRIMITIVE;
  174. }
  175. if (need_fallback_prim_restart(svga, info)) {
  176. enum pipe_error r;
  177. r = util_draw_vbo_without_prim_restart(pipe, &svga->curr.ib, info);
  178. assert(r == PIPE_OK);
  179. (void) r;
  180. goto done;
  181. }
  182. if (!u_trim_pipe_prim( info->mode, &count ))
  183. goto done;
  184. needed_swtnl = svga->state.sw.need_swtnl;
  185. svga_update_state_retry( svga, SVGA_STATE_NEED_SWTNL );
  186. #ifdef DEBUG
  187. if (svga->curr.vs->base.id == svga->debug.disable_shader ||
  188. svga->curr.fs->base.id == svga->debug.disable_shader)
  189. goto done;
  190. #endif
  191. if (svga->state.sw.need_swtnl) {
  192. svga->hud.num_fallbacks++; /* for SVGA_QUERY_NUM_FALLBACKS */
  193. if (!needed_swtnl) {
  194. /*
  195. * We're switching from HW to SW TNL. SW TNL will require mapping all
  196. * currently bound vertex buffers, some of which may already be
  197. * referenced in the current command buffer as result of previous HW
  198. * TNL. So flush now, to prevent the context to flush while a referred
  199. * vertex buffer is mapped.
  200. */
  201. svga_context_flush(svga, NULL);
  202. }
  203. /* Avoid leaking the previous hwtnl bias to swtnl */
  204. svga_hwtnl_set_index_bias( svga->hwtnl, 0 );
  205. ret = svga_swtnl_draw_vbo( svga, info );
  206. }
  207. else {
  208. if (info->indexed && svga->curr.ib.buffer) {
  209. unsigned offset;
  210. assert(svga->curr.ib.offset % svga->curr.ib.index_size == 0);
  211. offset = svga->curr.ib.offset / svga->curr.ib.index_size;
  212. ret = retry_draw_range_elements( svga,
  213. svga->curr.ib.buffer,
  214. svga->curr.ib.index_size,
  215. info->index_bias,
  216. info->min_index,
  217. info->max_index,
  218. info->mode,
  219. info->start + offset,
  220. count,
  221. info->start_instance,
  222. info->instance_count,
  223. TRUE );
  224. }
  225. else {
  226. ret = retry_draw_arrays(svga, info->mode, info->start, count,
  227. info->start_instance, info->instance_count,
  228. TRUE);
  229. }
  230. }
  231. /* XXX: Silence warnings, do something sensible here? */
  232. (void)ret;
  233. if (SVGA_DEBUG & DEBUG_FLUSH) {
  234. svga_hwtnl_flush_retry( svga );
  235. svga_context_flush(svga, NULL);
  236. }
  237. done:
  238. SVGA_STATS_TIME_POP(svga_sws(svga));
  239. ;
  240. }
  241. void svga_init_draw_functions( struct svga_context *svga )
  242. {
  243. svga->pipe.draw_vbo = svga_draw_vbo;
  244. }