Clone of mesa.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

svga_state_vs.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "pipe/p_defines.h"
  27. #include "util/u_math.h"
  28. #include "util/u_memory.h"
  29. #include "util/u_bitmask.h"
  30. #include "translate/translate.h"
  31. #include "tgsi/tgsi_ureg.h"
  32. #include "svga_context.h"
  33. #include "svga_state.h"
  34. #include "svga_cmd.h"
  35. #include "svga_shader.h"
  36. #include "svga_tgsi.h"
  37. #include "svga_hw_reg.h"
  38. /**
  39. * If we fail to compile a vertex shader we'll use a dummy/fallback shader
  40. * that simply emits a (0,0,0,1) vertex position.
  41. */
  42. static const struct tgsi_token *
  43. get_dummy_vertex_shader(void)
  44. {
  45. static const float zero[4] = { 0.0, 0.0, 0.0, 1.0 };
  46. struct ureg_program *ureg;
  47. const struct tgsi_token *tokens;
  48. struct ureg_src src;
  49. struct ureg_dst dst;
  50. unsigned num_tokens;
  51. ureg = ureg_create(PIPE_SHADER_VERTEX);
  52. if (!ureg)
  53. return NULL;
  54. dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
  55. src = ureg_DECL_immediate(ureg, zero, 4);
  56. ureg_MOV(ureg, dst, src);
  57. ureg_END(ureg);
  58. tokens = ureg_get_tokens(ureg, &num_tokens);
  59. ureg_destroy(ureg);
  60. return tokens;
  61. }
  62. static struct svga_shader_variant *
  63. translate_vertex_program(struct svga_context *svga,
  64. const struct svga_vertex_shader *vs,
  65. const struct svga_compile_key *key)
  66. {
  67. if (svga_have_vgpu10(svga)) {
  68. return svga_tgsi_vgpu10_translate(svga, &vs->base, key,
  69. PIPE_SHADER_VERTEX);
  70. }
  71. else {
  72. return svga_tgsi_vgpu9_translate(svga, &vs->base, key,
  73. PIPE_SHADER_VERTEX);
  74. }
  75. }
  76. /**
  77. * Replace the given shader's instruction with a simple / dummy shader.
  78. * We use this when normal shader translation fails.
  79. */
  80. static struct svga_shader_variant *
  81. get_compiled_dummy_vertex_shader(struct svga_context *svga,
  82. struct svga_vertex_shader *vs,
  83. const struct svga_compile_key *key)
  84. {
  85. const struct tgsi_token *dummy = get_dummy_vertex_shader();
  86. struct svga_shader_variant *variant;
  87. if (!dummy) {
  88. return NULL;
  89. }
  90. FREE((void *) vs->base.tokens);
  91. vs->base.tokens = dummy;
  92. variant = translate_vertex_program(svga, vs, key);
  93. return variant;
  94. }
  95. /**
  96. * Translate TGSI shader into an svga shader variant.
  97. */
  98. static enum pipe_error
  99. compile_vs(struct svga_context *svga,
  100. struct svga_vertex_shader *vs,
  101. const struct svga_compile_key *key,
  102. struct svga_shader_variant **out_variant)
  103. {
  104. struct svga_shader_variant *variant;
  105. enum pipe_error ret = PIPE_ERROR;
  106. variant = translate_vertex_program(svga, vs, key);
  107. if (variant == NULL) {
  108. debug_printf("Failed to compile vertex shader,"
  109. " using dummy shader instead.\n");
  110. variant = get_compiled_dummy_vertex_shader(svga, vs, key);
  111. }
  112. else if (svga_shader_too_large(svga, variant)) {
  113. /* too big, use dummy shader */
  114. debug_printf("Shader too large (%u bytes),"
  115. " using dummy shader instead.\n",
  116. (unsigned) (variant->nr_tokens
  117. * sizeof(variant->tokens[0])));
  118. /* Free the too-large variant */
  119. svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
  120. /* Use simple pass-through shader instead */
  121. variant = get_compiled_dummy_vertex_shader(svga, vs, key);
  122. }
  123. if (!variant) {
  124. return PIPE_ERROR;
  125. }
  126. ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
  127. if (ret != PIPE_OK) {
  128. svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_VS, variant);
  129. return ret;
  130. }
  131. *out_variant = variant;
  132. return PIPE_OK;
  133. }
  134. /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_FS
  135. */
  136. static void
  137. make_vs_key(struct svga_context *svga, struct svga_compile_key *key)
  138. {
  139. const enum pipe_shader_type shader = PIPE_SHADER_VERTEX;
  140. memset(key, 0, sizeof *key);
  141. if (svga->state.sw.need_swtnl && svga_have_vgpu10(svga)) {
  142. /* Set both of these flags, to match compile_passthrough_vs() */
  143. key->vs.passthrough = 1;
  144. key->vs.undo_viewport = 1;
  145. return;
  146. }
  147. /* SVGA_NEW_PRESCALE */
  148. key->vs.need_prescale = svga->state.hw_clear.prescale.enabled &&
  149. (svga->curr.gs == NULL);
  150. /* SVGA_NEW_RAST */
  151. key->vs.allow_psiz = svga->curr.rast->templ.point_size_per_vertex;
  152. /* SVGA_NEW_FS */
  153. key->vs.fs_generic_inputs = svga->curr.fs->generic_inputs;
  154. svga_remap_generics(key->vs.fs_generic_inputs, key->generic_remap_table);
  155. /* SVGA_NEW_VELEMENT */
  156. key->vs.adjust_attrib_range = svga->curr.velems->adjust_attrib_range;
  157. key->vs.adjust_attrib_w_1 = svga->curr.velems->adjust_attrib_w_1;
  158. key->vs.attrib_is_pure_int = svga->curr.velems->attrib_is_pure_int;
  159. key->vs.adjust_attrib_itof = svga->curr.velems->adjust_attrib_itof;
  160. key->vs.adjust_attrib_utof = svga->curr.velems->adjust_attrib_utof;
  161. key->vs.attrib_is_bgra = svga->curr.velems->attrib_is_bgra;
  162. key->vs.attrib_puint_to_snorm = svga->curr.velems->attrib_puint_to_snorm;
  163. key->vs.attrib_puint_to_uscaled = svga->curr.velems->attrib_puint_to_uscaled;
  164. key->vs.attrib_puint_to_sscaled = svga->curr.velems->attrib_puint_to_sscaled;
  165. /* SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER */
  166. svga_init_shader_key_common(svga, shader, key);
  167. /* SVGA_NEW_RAST */
  168. key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
  169. }
  170. /**
  171. * svga_reemit_vs_bindings - Reemit the vertex shader bindings
  172. */
  173. enum pipe_error
  174. svga_reemit_vs_bindings(struct svga_context *svga)
  175. {
  176. enum pipe_error ret;
  177. struct svga_winsys_gb_shader *gbshader = NULL;
  178. SVGA3dShaderId shaderId = SVGA3D_INVALID_ID;
  179. assert(svga->rebind.flags.vs);
  180. assert(svga_have_gb_objects(svga));
  181. if (svga->state.hw_draw.vs) {
  182. gbshader = svga->state.hw_draw.vs->gb_shader;
  183. shaderId = svga->state.hw_draw.vs->id;
  184. }
  185. if (!svga_need_to_rebind_resources(svga)) {
  186. ret = svga->swc->resource_rebind(svga->swc, NULL, gbshader,
  187. SVGA_RELOC_READ);
  188. goto out;
  189. }
  190. if (svga_have_vgpu10(svga))
  191. ret = SVGA3D_vgpu10_SetShader(svga->swc, SVGA3D_SHADERTYPE_VS,
  192. gbshader, shaderId);
  193. else
  194. ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_VS, gbshader);
  195. out:
  196. if (ret != PIPE_OK)
  197. return ret;
  198. svga->rebind.flags.vs = FALSE;
  199. return PIPE_OK;
  200. }
  201. /**
  202. * The current vertex shader is already executed by the 'draw'
  203. * module, so we just need to generate a simple vertex shader
  204. * to pass through all those VS outputs that will
  205. * be consumed by the fragment shader.
  206. * Used when we employ the 'draw' module.
  207. */
  208. static enum pipe_error
  209. compile_passthrough_vs(struct svga_context *svga,
  210. struct svga_vertex_shader *vs,
  211. struct svga_fragment_shader *fs,
  212. struct svga_shader_variant **out_variant)
  213. {
  214. struct svga_shader_variant *variant = NULL;
  215. unsigned num_inputs;
  216. unsigned i;
  217. unsigned num_elements;
  218. struct svga_vertex_shader new_vs;
  219. struct ureg_src src[PIPE_MAX_SHADER_INPUTS];
  220. struct ureg_dst dst[PIPE_MAX_SHADER_OUTPUTS];
  221. struct ureg_program *ureg;
  222. unsigned num_tokens;
  223. struct svga_compile_key key;
  224. enum pipe_error ret;
  225. assert(svga_have_vgpu10(svga));
  226. assert(fs);
  227. num_inputs = fs->base.info.num_inputs;
  228. ureg = ureg_create(PIPE_SHADER_VERTEX);
  229. if (!ureg)
  230. return PIPE_ERROR_OUT_OF_MEMORY;
  231. /* draw will always add position */
  232. dst[0] = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
  233. src[0] = ureg_DECL_vs_input(ureg, 0);
  234. num_elements = 1;
  235. /**
  236. * swtnl backend redefines the input layout based on the
  237. * fragment shader's inputs. So we only need to passthrough
  238. * those inputs that will be consumed by the fragment shader.
  239. * Note: DX10 requires the number of vertex elements
  240. * specified in the input layout to be no less than the
  241. * number of inputs to the vertex shader.
  242. */
  243. for (i = 0; i < num_inputs; i++) {
  244. switch (fs->base.info.input_semantic_name[i]) {
  245. case TGSI_SEMANTIC_COLOR:
  246. case TGSI_SEMANTIC_GENERIC:
  247. case TGSI_SEMANTIC_FOG:
  248. dst[num_elements] = ureg_DECL_output(ureg,
  249. fs->base.info.input_semantic_name[i],
  250. fs->base.info.input_semantic_index[i]);
  251. src[num_elements] = ureg_DECL_vs_input(ureg, num_elements);
  252. num_elements++;
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. for (i = 0; i < num_elements; i++) {
  259. ureg_MOV(ureg, dst[i], src[i]);
  260. }
  261. ureg_END(ureg);
  262. memset(&new_vs, 0, sizeof(new_vs));
  263. new_vs.base.tokens = ureg_get_tokens(ureg, &num_tokens);
  264. tgsi_scan_shader(new_vs.base.tokens, &new_vs.base.info);
  265. memset(&key, 0, sizeof(key));
  266. key.vs.undo_viewport = 1;
  267. ret = compile_vs(svga, &new_vs, &key, &variant);
  268. if (ret != PIPE_OK)
  269. return ret;
  270. ureg_free_tokens(new_vs.base.tokens);
  271. ureg_destroy(ureg);
  272. /* Overwrite the variant key to indicate it's a pass-through VS */
  273. memset(&variant->key, 0, sizeof(variant->key));
  274. variant->key.vs.passthrough = 1;
  275. variant->key.vs.undo_viewport = 1;
  276. *out_variant = variant;
  277. return PIPE_OK;
  278. }
  279. static enum pipe_error
  280. emit_hw_vs(struct svga_context *svga, unsigned dirty)
  281. {
  282. struct svga_shader_variant *variant;
  283. struct svga_vertex_shader *vs = svga->curr.vs;
  284. struct svga_fragment_shader *fs = svga->curr.fs;
  285. enum pipe_error ret = PIPE_OK;
  286. struct svga_compile_key key;
  287. SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITVS);
  288. /* If there is an active geometry shader, and it has stream output
  289. * defined, then we will skip the stream output from the vertex shader
  290. */
  291. if (!svga_have_gs_streamout(svga)) {
  292. /* No GS stream out */
  293. if (svga_have_vs_streamout(svga)) {
  294. /* Set VS stream out */
  295. svga_set_stream_output(svga, vs->base.stream_output);
  296. }
  297. else {
  298. /* turn off stream out */
  299. svga_set_stream_output(svga, NULL);
  300. }
  301. }
  302. /* SVGA_NEW_NEED_SWTNL */
  303. if (svga->state.sw.need_swtnl && !svga_have_vgpu10(svga)) {
  304. /* No vertex shader is needed */
  305. variant = NULL;
  306. }
  307. else {
  308. make_vs_key(svga, &key);
  309. /* See if we already have a VS variant that matches the key */
  310. variant = svga_search_shader_key(&vs->base, &key);
  311. if (!variant) {
  312. /* Create VS variant now */
  313. if (key.vs.passthrough) {
  314. ret = compile_passthrough_vs(svga, vs, fs, &variant);
  315. }
  316. else {
  317. ret = compile_vs(svga, vs, &key, &variant);
  318. }
  319. if (ret != PIPE_OK)
  320. goto done;
  321. /* insert the new variant at head of linked list */
  322. assert(variant);
  323. variant->next = vs->base.variants;
  324. vs->base.variants = variant;
  325. }
  326. }
  327. if (variant != svga->state.hw_draw.vs) {
  328. /* Bind the new variant */
  329. if (variant) {
  330. ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
  331. if (ret != PIPE_OK)
  332. goto done;
  333. svga->rebind.flags.vs = FALSE;
  334. }
  335. svga->dirty |= SVGA_NEW_VS_VARIANT;
  336. svga->state.hw_draw.vs = variant;
  337. }
  338. done:
  339. SVGA_STATS_TIME_POP(svga_sws(svga));
  340. return ret;
  341. }
  342. struct svga_tracked_state svga_hw_vs =
  343. {
  344. "vertex shader (hwtnl)",
  345. (SVGA_NEW_VS |
  346. SVGA_NEW_FS |
  347. SVGA_NEW_TEXTURE_BINDING |
  348. SVGA_NEW_SAMPLER |
  349. SVGA_NEW_RAST |
  350. SVGA_NEW_PRESCALE |
  351. SVGA_NEW_VELEMENT |
  352. SVGA_NEW_NEED_SWTNL),
  353. emit_hw_vs
  354. };