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_tgsi_decl_sm30.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 "pipe/p_shader_tokens.h"
  26. #include "tgsi/tgsi_parse.h"
  27. #include "util/u_memory.h"
  28. #include "svga_tgsi_emit.h"
  29. #include "svga_context.h"
  30. static boolean translate_vs_ps_semantic( struct tgsi_declaration_semantic semantic,
  31. unsigned *usage,
  32. unsigned *idx )
  33. {
  34. switch (semantic.Name) {
  35. case TGSI_SEMANTIC_POSITION:
  36. *idx = semantic.Index;
  37. *usage = SVGA3D_DECLUSAGE_POSITION;
  38. break;
  39. case TGSI_SEMANTIC_COLOR:
  40. *idx = semantic.Index;
  41. *usage = SVGA3D_DECLUSAGE_COLOR;
  42. break;
  43. case TGSI_SEMANTIC_BCOLOR:
  44. *idx = semantic.Index + 2; /* sharing with COLOR */
  45. *usage = SVGA3D_DECLUSAGE_COLOR;
  46. break;
  47. case TGSI_SEMANTIC_FOG:
  48. *idx = 0;
  49. assert(semantic.Index == 0);
  50. *usage = SVGA3D_DECLUSAGE_TEXCOORD;
  51. break;
  52. case TGSI_SEMANTIC_PSIZE:
  53. *idx = semantic.Index;
  54. *usage = SVGA3D_DECLUSAGE_PSIZE;
  55. break;
  56. case TGSI_SEMANTIC_GENERIC:
  57. *idx = semantic.Index + 1; /* texcoord[0] is reserved for fog */
  58. *usage = SVGA3D_DECLUSAGE_TEXCOORD;
  59. break;
  60. case TGSI_SEMANTIC_NORMAL:
  61. *idx = semantic.Index;
  62. *usage = SVGA3D_DECLUSAGE_NORMAL;
  63. break;
  64. default:
  65. assert(0);
  66. *usage = SVGA3D_DECLUSAGE_TEXCOORD;
  67. *idx = 0;
  68. return FALSE;
  69. }
  70. return TRUE;
  71. }
  72. static boolean emit_decl( struct svga_shader_emitter *emit,
  73. SVGA3dShaderDestToken reg,
  74. unsigned usage,
  75. unsigned index )
  76. {
  77. SVGA3DOpDclArgs dcl;
  78. SVGA3dShaderInstToken opcode;
  79. opcode = inst_token( SVGA3DOP_DCL );
  80. dcl.values[0] = 0;
  81. dcl.values[1] = 0;
  82. dcl.dst = reg;
  83. dcl.usage = usage;
  84. dcl.index = index;
  85. dcl.values[0] |= 1<<31;
  86. return (emit_instruction(emit, opcode) &&
  87. svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
  88. }
  89. static boolean emit_vface_decl( struct svga_shader_emitter *emit )
  90. {
  91. if (!emit->emitted_vface) {
  92. SVGA3dShaderDestToken reg =
  93. dst_register( SVGA3DREG_MISCTYPE,
  94. SVGA3DMISCREG_FACE );
  95. if (!emit_decl( emit, reg, 0, 0 ))
  96. return FALSE;
  97. emit->emitted_vface = TRUE;
  98. }
  99. return TRUE;
  100. }
  101. static boolean ps30_input( struct svga_shader_emitter *emit,
  102. struct tgsi_declaration_semantic semantic,
  103. unsigned idx )
  104. {
  105. unsigned usage, index;
  106. SVGA3dShaderDestToken reg;
  107. if (semantic.Name == TGSI_SEMANTIC_POSITION) {
  108. emit->input_map[idx] = src_register( SVGA3DREG_MISCTYPE,
  109. SVGA3DMISCREG_POSITION );
  110. emit->input_map[idx].base.swizzle = TRANSLATE_SWIZZLE( TGSI_SWIZZLE_X,
  111. TGSI_SWIZZLE_Y,
  112. TGSI_SWIZZLE_Y,
  113. TGSI_SWIZZLE_Y );
  114. reg = writemask( dst(emit->input_map[idx]),
  115. TGSI_WRITEMASK_XY );
  116. return emit_decl( emit, reg, 0, 0 );
  117. }
  118. else if (emit->key.fkey.light_twoside &&
  119. (semantic.Name == TGSI_SEMANTIC_COLOR)) {
  120. if (!translate_vs_ps_semantic( semantic, &usage, &index ))
  121. return FALSE;
  122. emit->internal_color_idx[emit->internal_color_count] = idx;
  123. emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count );
  124. emit->ps30_input_count++;
  125. emit->internal_color_count++;
  126. reg = dst( emit->input_map[idx] );
  127. if (!emit_decl( emit, reg, usage, index ))
  128. return FALSE;
  129. semantic.Name = TGSI_SEMANTIC_BCOLOR;
  130. if (!translate_vs_ps_semantic( semantic, &usage, &index ))
  131. return FALSE;
  132. reg = dst_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
  133. if (!emit_decl( emit, reg, usage, index ))
  134. return FALSE;
  135. if (!emit_vface_decl( emit ))
  136. return FALSE;
  137. return TRUE;
  138. }
  139. else if (semantic.Name == TGSI_SEMANTIC_FACE) {
  140. if (!emit_vface_decl( emit ))
  141. return FALSE;
  142. emit->emit_frontface = TRUE;
  143. emit->internal_frontface_idx = idx;
  144. return TRUE;
  145. }
  146. else {
  147. if (!translate_vs_ps_semantic( semantic, &usage, &index ))
  148. return FALSE;
  149. emit->input_map[idx] = src_register( SVGA3DREG_INPUT, emit->ps30_input_count++ );
  150. reg = dst( emit->input_map[idx] );
  151. return emit_decl( emit, reg, usage, index );
  152. }
  153. }
  154. /* PS output registers are the same as 2.0
  155. */
  156. static boolean ps30_output( struct svga_shader_emitter *emit,
  157. struct tgsi_declaration_semantic semantic,
  158. unsigned idx )
  159. {
  160. SVGA3dShaderDestToken reg;
  161. switch (semantic.Name) {
  162. case TGSI_SEMANTIC_COLOR:
  163. emit->output_map[idx] = dst_register( SVGA3DREG_COLOROUT,
  164. semantic.Index );
  165. break;
  166. case TGSI_SEMANTIC_POSITION:
  167. emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
  168. emit->nr_hw_temp++ );
  169. emit->temp_pos = emit->output_map[idx];
  170. emit->true_pos = dst_register( SVGA3DREG_DEPTHOUT,
  171. semantic.Index );
  172. break;
  173. default:
  174. assert(0);
  175. reg = dst_register( SVGA3DREG_COLOROUT, 0 );
  176. break;
  177. }
  178. return TRUE;
  179. }
  180. /* We still make up the input semantics the same as in 2.0
  181. */
  182. static boolean vs30_input( struct svga_shader_emitter *emit,
  183. struct tgsi_declaration_semantic semantic,
  184. unsigned idx )
  185. {
  186. SVGA3DOpDclArgs dcl;
  187. SVGA3dShaderInstToken opcode;
  188. unsigned usage, index;
  189. opcode = inst_token( SVGA3DOP_DCL );
  190. dcl.values[0] = 0;
  191. dcl.values[1] = 0;
  192. if (emit->key.vkey.zero_stride_vertex_elements & (1 << idx)) {
  193. unsigned i;
  194. unsigned offset = 0;
  195. unsigned start_idx = emit->info.file_max[TGSI_FILE_CONSTANT] + 1;
  196. /* adjust for prescale constants */
  197. start_idx += emit->key.vkey.need_prescale ? 2 : 0;
  198. /* compute the offset from the start of zero stride constants */
  199. for (i = 0; i < PIPE_MAX_ATTRIBS && i < idx; ++i) {
  200. if (emit->key.vkey.zero_stride_vertex_elements & (1<<i))
  201. ++offset;
  202. }
  203. emit->input_map[idx] = src_register( SVGA3DREG_CONST,
  204. start_idx + offset );
  205. } else {
  206. emit->input_map[idx] = src_register( SVGA3DREG_INPUT, idx );
  207. dcl.dst = dst_register( SVGA3DREG_INPUT, idx );
  208. assert(dcl.dst.reserved0);
  209. svga_generate_vdecl_semantics( idx, &usage, &index );
  210. dcl.usage = usage;
  211. dcl.index = index;
  212. dcl.values[0] |= 1<<31;
  213. return (emit_instruction(emit, opcode) &&
  214. svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
  215. }
  216. return TRUE;
  217. }
  218. /* VS3.0 outputs have proper declarations and semantic info for
  219. * matching against PS inputs.
  220. */
  221. static boolean vs30_output( struct svga_shader_emitter *emit,
  222. struct tgsi_declaration_semantic semantic,
  223. unsigned idx )
  224. {
  225. SVGA3DOpDclArgs dcl;
  226. SVGA3dShaderInstToken opcode;
  227. unsigned usage, index;
  228. opcode = inst_token( SVGA3DOP_DCL );
  229. dcl.values[0] = 0;
  230. dcl.values[1] = 0;
  231. if (!translate_vs_ps_semantic( semantic, &usage, &index ))
  232. return FALSE;
  233. dcl.dst = dst_register( SVGA3DREG_OUTPUT, idx );
  234. dcl.usage = usage;
  235. dcl.index = index;
  236. dcl.values[0] |= 1<<31;
  237. if (semantic.Name == TGSI_SEMANTIC_POSITION) {
  238. assert(idx == 0);
  239. emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
  240. emit->nr_hw_temp++ );
  241. emit->temp_pos = emit->output_map[idx];
  242. emit->true_pos = dcl.dst;
  243. }
  244. else if (semantic.Name == TGSI_SEMANTIC_PSIZE) {
  245. emit->output_map[idx] = dst_register( SVGA3DREG_TEMP,
  246. emit->nr_hw_temp++ );
  247. emit->temp_psiz = emit->output_map[idx];
  248. /* This has the effect of not declaring psiz (below) and not
  249. * emitting the final MOV to true_psiz in the postamble.
  250. */
  251. if (!emit->key.vkey.allow_psiz)
  252. return TRUE;
  253. emit->true_psiz = dcl.dst;
  254. }
  255. else {
  256. emit->output_map[idx] = dcl.dst;
  257. }
  258. return (emit_instruction(emit, opcode) &&
  259. svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
  260. }
  261. static boolean ps30_sampler( struct svga_shader_emitter *emit,
  262. struct tgsi_declaration_semantic semantic,
  263. unsigned idx )
  264. {
  265. SVGA3DOpDclArgs dcl;
  266. SVGA3dShaderInstToken opcode;
  267. opcode = inst_token( SVGA3DOP_DCL );
  268. dcl.values[0] = 0;
  269. dcl.values[1] = 0;
  270. dcl.dst = dst_register( SVGA3DREG_SAMPLER, idx );
  271. dcl.type = svga_tgsi_sampler_type( emit, idx );
  272. dcl.values[0] |= 1<<31;
  273. return (emit_instruction(emit, opcode) &&
  274. svga_shader_emit_dwords( emit, dcl.values, Elements(dcl.values)));
  275. }
  276. boolean svga_translate_decl_sm30( struct svga_shader_emitter *emit,
  277. const struct tgsi_full_declaration *decl )
  278. {
  279. unsigned first = decl->DeclarationRange.First;
  280. unsigned last = decl->DeclarationRange.Last;
  281. unsigned semantic = 0;
  282. unsigned semantic_idx = 0;
  283. unsigned idx;
  284. if (decl->Declaration.Semantic) {
  285. semantic = decl->Semantic.Name;
  286. semantic_idx = decl->Semantic.Index;
  287. }
  288. for( idx = first; idx <= last; idx++ ) {
  289. boolean ok;
  290. switch (decl->Declaration.File) {
  291. case TGSI_FILE_SAMPLER:
  292. assert (emit->unit == PIPE_SHADER_FRAGMENT);
  293. ok = ps30_sampler( emit, decl->Semantic, idx );
  294. break;
  295. case TGSI_FILE_INPUT:
  296. if (emit->unit == PIPE_SHADER_VERTEX)
  297. ok = vs30_input( emit, decl->Semantic, idx );
  298. else
  299. ok = ps30_input( emit, decl->Semantic, idx );
  300. break;
  301. case TGSI_FILE_OUTPUT:
  302. if (emit->unit == PIPE_SHADER_VERTEX)
  303. ok = vs30_output( emit, decl->Semantic, idx );
  304. else
  305. ok = ps30_output( emit, decl->Semantic, idx );
  306. break;
  307. default:
  308. /* don't need to declare other vars */
  309. ok = TRUE;
  310. }
  311. if (!ok)
  312. return FALSE;
  313. }
  314. return TRUE;
  315. }