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.

tgsi_dump.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /**************************************************************************
  2. *
  3. * Copyright 2007-2008 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. #include "util/u_debug.h"
  28. #include "util/u_string.h"
  29. #include "util/u_math.h"
  30. #include "util/u_memory.h"
  31. #include "tgsi_dump.h"
  32. #include "tgsi_info.h"
  33. #include "tgsi_iterate.h"
  34. /** Number of spaces to indent for IF/LOOP/etc */
  35. static const int indent_spaces = 3;
  36. struct dump_ctx
  37. {
  38. struct tgsi_iterate_context iter;
  39. uint instno;
  40. int indent;
  41. uint indentation;
  42. void (*printf)(struct dump_ctx *ctx, const char *format, ...);
  43. };
  44. static void
  45. dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
  46. {
  47. va_list ap;
  48. (void)ctx;
  49. va_start(ap, format);
  50. debug_vprintf(format, ap);
  51. va_end(ap);
  52. }
  53. static void
  54. dump_enum(
  55. struct dump_ctx *ctx,
  56. uint e,
  57. const char **enums,
  58. uint enum_count )
  59. {
  60. if (e >= enum_count)
  61. ctx->printf( ctx, "%u", e );
  62. else
  63. ctx->printf( ctx, "%s", enums[e] );
  64. }
  65. #define EOL() ctx->printf( ctx, "\n" )
  66. #define TXT(S) ctx->printf( ctx, "%s", S )
  67. #define CHR(C) ctx->printf( ctx, "%c", C )
  68. #define UIX(I) ctx->printf( ctx, "0x%x", I )
  69. #define UID(I) ctx->printf( ctx, "%u", I )
  70. #define INSTID(I) ctx->printf( ctx, "% 3u", I )
  71. #define SID(I) ctx->printf( ctx, "%d", I )
  72. #define FLT(F) ctx->printf( ctx, "%10.4f", F )
  73. #define ENM(E,ENUMS) dump_enum( ctx, E, ENUMS, sizeof( ENUMS ) / sizeof( *ENUMS ) )
  74. static const char *processor_type_names[] =
  75. {
  76. "FRAG",
  77. "VERT",
  78. "GEOM"
  79. };
  80. static const char *file_names[TGSI_FILE_COUNT] =
  81. {
  82. "NULL",
  83. "CONST",
  84. "IN",
  85. "OUT",
  86. "TEMP",
  87. "SAMP",
  88. "ADDR",
  89. "IMM",
  90. "LOOP",
  91. "PRED",
  92. "SV"
  93. };
  94. static const char *interpolate_names[] =
  95. {
  96. "CONSTANT",
  97. "LINEAR",
  98. "PERSPECTIVE"
  99. };
  100. static const char *semantic_names[] =
  101. {
  102. "POSITION",
  103. "COLOR",
  104. "BCOLOR",
  105. "FOG",
  106. "PSIZE",
  107. "GENERIC",
  108. "NORMAL",
  109. "FACE",
  110. "EDGEFLAG",
  111. "PRIM_ID"
  112. };
  113. static const char *immediate_type_names[] =
  114. {
  115. "FLT32",
  116. "UINT32",
  117. "INT32"
  118. };
  119. static const char *swizzle_names[] =
  120. {
  121. "x",
  122. "y",
  123. "z",
  124. "w"
  125. };
  126. static const char *texture_names[] =
  127. {
  128. "UNKNOWN",
  129. "1D",
  130. "2D",
  131. "3D",
  132. "CUBE",
  133. "RECT",
  134. "SHADOW1D",
  135. "SHADOW2D",
  136. "SHADOWRECT"
  137. };
  138. static const char *property_names[] =
  139. {
  140. "GS_INPUT_PRIMITIVE",
  141. "GS_OUTPUT_PRIMITIVE",
  142. "GS_MAX_OUTPUT_VERTICES"
  143. };
  144. static const char *primitive_names[] =
  145. {
  146. "POINTS",
  147. "LINES",
  148. "LINE_LOOP",
  149. "LINE_STRIP",
  150. "TRIANGLES",
  151. "TRIANGLE_STRIP",
  152. "TRIANGLE_FAN",
  153. "QUADS",
  154. "QUAD_STRIP",
  155. "POLYGON"
  156. };
  157. static void
  158. _dump_register_decl(
  159. struct dump_ctx *ctx,
  160. uint file,
  161. int first,
  162. int last )
  163. {
  164. ENM( file, file_names );
  165. /* all geometry shader inputs are two dimensional */
  166. if (file == TGSI_FILE_INPUT &&
  167. ctx->iter.processor.Processor == TGSI_PROCESSOR_GEOMETRY)
  168. TXT("[]");
  169. CHR( '[' );
  170. SID( first );
  171. if (first != last) {
  172. TXT( ".." );
  173. SID( last );
  174. }
  175. CHR( ']' );
  176. }
  177. static void
  178. _dump_register_dst(
  179. struct dump_ctx *ctx,
  180. uint file,
  181. int index)
  182. {
  183. ENM( file, file_names );
  184. CHR( '[' );
  185. SID( index );
  186. CHR( ']' );
  187. }
  188. static void
  189. _dump_register_src(
  190. struct dump_ctx *ctx,
  191. const struct tgsi_full_src_register *src )
  192. {
  193. if (src->Register.Indirect) {
  194. ENM( src->Register.File, file_names );
  195. CHR( '[' );
  196. ENM( src->Indirect.File, file_names );
  197. CHR( '[' );
  198. SID( src->Indirect.Index );
  199. TXT( "]." );
  200. ENM( src->Indirect.SwizzleX, swizzle_names );
  201. if (src->Register.Index != 0) {
  202. if (src->Register.Index > 0)
  203. CHR( '+' );
  204. SID( src->Register.Index );
  205. }
  206. CHR( ']' );
  207. } else {
  208. ENM( src->Register.File, file_names );
  209. CHR( '[' );
  210. SID( src->Register.Index );
  211. CHR( ']' );
  212. }
  213. if (src->Register.Dimension) {
  214. CHR( '[' );
  215. SID( src->Dimension.Index );
  216. CHR( ']' );
  217. }
  218. }
  219. static void
  220. _dump_register_ind(
  221. struct dump_ctx *ctx,
  222. uint file,
  223. int index,
  224. uint ind_file,
  225. int ind_index,
  226. uint ind_swizzle )
  227. {
  228. ENM( file, file_names );
  229. CHR( '[' );
  230. ENM( ind_file, file_names );
  231. CHR( '[' );
  232. SID( ind_index );
  233. TXT( "]." );
  234. ENM( ind_swizzle, swizzle_names );
  235. if (index != 0) {
  236. if (index > 0)
  237. CHR( '+' );
  238. SID( index );
  239. }
  240. CHR( ']' );
  241. }
  242. static void
  243. _dump_writemask(
  244. struct dump_ctx *ctx,
  245. uint writemask )
  246. {
  247. if (writemask != TGSI_WRITEMASK_XYZW) {
  248. CHR( '.' );
  249. if (writemask & TGSI_WRITEMASK_X)
  250. CHR( 'x' );
  251. if (writemask & TGSI_WRITEMASK_Y)
  252. CHR( 'y' );
  253. if (writemask & TGSI_WRITEMASK_Z)
  254. CHR( 'z' );
  255. if (writemask & TGSI_WRITEMASK_W)
  256. CHR( 'w' );
  257. }
  258. }
  259. static boolean
  260. iter_declaration(
  261. struct tgsi_iterate_context *iter,
  262. struct tgsi_full_declaration *decl )
  263. {
  264. struct dump_ctx *ctx = (struct dump_ctx *)iter;
  265. assert(Elements(semantic_names) == TGSI_SEMANTIC_COUNT);
  266. assert(Elements(interpolate_names) == TGSI_INTERPOLATE_COUNT);
  267. TXT( "DCL " );
  268. _dump_register_decl(
  269. ctx,
  270. decl->Declaration.File,
  271. decl->Range.First,
  272. decl->Range.Last );
  273. _dump_writemask(
  274. ctx,
  275. decl->Declaration.UsageMask );
  276. if (decl->Declaration.Semantic) {
  277. TXT( ", " );
  278. ENM( decl->Semantic.Name, semantic_names );
  279. if (decl->Semantic.Index != 0 ||
  280. decl->Semantic.Name == TGSI_SEMANTIC_GENERIC) {
  281. CHR( '[' );
  282. UID( decl->Semantic.Index );
  283. CHR( ']' );
  284. }
  285. }
  286. if (iter->processor.Processor == TGSI_PROCESSOR_FRAGMENT &&
  287. decl->Declaration.File == TGSI_FILE_INPUT)
  288. {
  289. TXT( ", " );
  290. ENM( decl->Declaration.Interpolate, interpolate_names );
  291. }
  292. if (decl->Declaration.Centroid) {
  293. TXT( ", CENTROID" );
  294. }
  295. if (decl->Declaration.Invariant) {
  296. TXT( ", INVARIANT" );
  297. }
  298. EOL();
  299. return TRUE;
  300. }
  301. void
  302. tgsi_dump_declaration(
  303. const struct tgsi_full_declaration *decl )
  304. {
  305. struct dump_ctx ctx;
  306. ctx.printf = dump_ctx_printf;
  307. iter_declaration( &ctx.iter, (struct tgsi_full_declaration *)decl );
  308. }
  309. static boolean
  310. iter_property(
  311. struct tgsi_iterate_context *iter,
  312. struct tgsi_full_property *prop )
  313. {
  314. int i;
  315. struct dump_ctx *ctx = (struct dump_ctx *)iter;
  316. assert(Elements(property_names) == TGSI_PROPERTY_COUNT);
  317. TXT( "PROPERTY " );
  318. ENM(prop->Property.PropertyName, property_names);
  319. if (prop->Property.NrTokens > 1)
  320. TXT(" ");
  321. for (i = 0; i < prop->Property.NrTokens - 1; ++i) {
  322. switch (prop->Property.PropertyName) {
  323. case TGSI_PROPERTY_GS_INPUT_PRIM:
  324. case TGSI_PROPERTY_GS_OUTPUT_PRIM:
  325. ENM(prop->u[i].Data, primitive_names);
  326. break;
  327. default:
  328. SID( prop->u[i].Data );
  329. break;
  330. }
  331. if (i < prop->Property.NrTokens - 2)
  332. TXT( ", " );
  333. }
  334. EOL();
  335. return TRUE;
  336. }
  337. void tgsi_dump_property(
  338. const struct tgsi_full_property *prop )
  339. {
  340. struct dump_ctx ctx;
  341. ctx.printf = dump_ctx_printf;
  342. iter_property( &ctx.iter, (struct tgsi_full_property *)prop );
  343. }
  344. static boolean
  345. iter_immediate(
  346. struct tgsi_iterate_context *iter,
  347. struct tgsi_full_immediate *imm )
  348. {
  349. struct dump_ctx *ctx = (struct dump_ctx *) iter;
  350. uint i;
  351. TXT( "IMM " );
  352. ENM( imm->Immediate.DataType, immediate_type_names );
  353. TXT( " { " );
  354. assert( imm->Immediate.NrTokens <= 4 + 1 );
  355. for (i = 0; i < imm->Immediate.NrTokens - 1; i++) {
  356. switch (imm->Immediate.DataType) {
  357. case TGSI_IMM_FLOAT32:
  358. FLT( imm->u[i].Float );
  359. break;
  360. case TGSI_IMM_UINT32:
  361. UID(imm->u[i].Uint);
  362. break;
  363. case TGSI_IMM_INT32:
  364. SID(imm->u[i].Int);
  365. break;
  366. default:
  367. assert( 0 );
  368. }
  369. if (i < imm->Immediate.NrTokens - 2)
  370. TXT( ", " );
  371. }
  372. TXT( " }" );
  373. EOL();
  374. return TRUE;
  375. }
  376. void
  377. tgsi_dump_immediate(
  378. const struct tgsi_full_immediate *imm )
  379. {
  380. struct dump_ctx ctx;
  381. ctx.printf = dump_ctx_printf;
  382. iter_immediate( &ctx.iter, (struct tgsi_full_immediate *)imm );
  383. }
  384. static boolean
  385. iter_instruction(
  386. struct tgsi_iterate_context *iter,
  387. struct tgsi_full_instruction *inst )
  388. {
  389. struct dump_ctx *ctx = (struct dump_ctx *) iter;
  390. uint instno = ctx->instno++;
  391. const struct tgsi_opcode_info *info = tgsi_get_opcode_info( inst->Instruction.Opcode );
  392. uint i;
  393. boolean first_reg = TRUE;
  394. INSTID( instno );
  395. TXT( ": " );
  396. ctx->indent -= info->pre_dedent;
  397. for(i = 0; (int)i < ctx->indent; ++i)
  398. TXT( " " );
  399. ctx->indent += info->post_indent;
  400. TXT( info->mnemonic );
  401. switch (inst->Instruction.Saturate) {
  402. case TGSI_SAT_NONE:
  403. break;
  404. case TGSI_SAT_ZERO_ONE:
  405. TXT( "_SAT" );
  406. break;
  407. case TGSI_SAT_MINUS_PLUS_ONE:
  408. TXT( "_SATNV" );
  409. break;
  410. default:
  411. assert( 0 );
  412. }
  413. for (i = 0; i < inst->Instruction.NumDstRegs; i++) {
  414. const struct tgsi_full_dst_register *dst = &inst->Dst[i];
  415. if (!first_reg)
  416. CHR( ',' );
  417. CHR( ' ' );
  418. if (dst->Register.Indirect) {
  419. _dump_register_ind(
  420. ctx,
  421. dst->Register.File,
  422. dst->Register.Index,
  423. dst->Indirect.File,
  424. dst->Indirect.Index,
  425. dst->Indirect.SwizzleX );
  426. }
  427. else {
  428. _dump_register_dst(
  429. ctx,
  430. dst->Register.File,
  431. dst->Register.Index );
  432. }
  433. _dump_writemask( ctx, dst->Register.WriteMask );
  434. first_reg = FALSE;
  435. }
  436. for (i = 0; i < inst->Instruction.NumSrcRegs; i++) {
  437. const struct tgsi_full_src_register *src = &inst->Src[i];
  438. if (!first_reg)
  439. CHR( ',' );
  440. CHR( ' ' );
  441. if (src->Register.Negate)
  442. CHR( '-' );
  443. if (src->Register.Absolute)
  444. CHR( '|' );
  445. _dump_register_src(ctx, src);
  446. if (src->Register.SwizzleX != TGSI_SWIZZLE_X ||
  447. src->Register.SwizzleY != TGSI_SWIZZLE_Y ||
  448. src->Register.SwizzleZ != TGSI_SWIZZLE_Z ||
  449. src->Register.SwizzleW != TGSI_SWIZZLE_W) {
  450. CHR( '.' );
  451. ENM( src->Register.SwizzleX, swizzle_names );
  452. ENM( src->Register.SwizzleY, swizzle_names );
  453. ENM( src->Register.SwizzleZ, swizzle_names );
  454. ENM( src->Register.SwizzleW, swizzle_names );
  455. }
  456. if (src->Register.Absolute)
  457. CHR( '|' );
  458. first_reg = FALSE;
  459. }
  460. if (inst->Instruction.Texture) {
  461. TXT( ", " );
  462. ENM( inst->Texture.Texture, texture_names );
  463. }
  464. switch (inst->Instruction.Opcode) {
  465. case TGSI_OPCODE_IF:
  466. case TGSI_OPCODE_ELSE:
  467. case TGSI_OPCODE_BGNLOOP:
  468. case TGSI_OPCODE_ENDLOOP:
  469. case TGSI_OPCODE_CAL:
  470. TXT( " :" );
  471. UID( inst->Label.Label );
  472. break;
  473. }
  474. /* update indentation */
  475. if (inst->Instruction.Opcode == TGSI_OPCODE_IF ||
  476. inst->Instruction.Opcode == TGSI_OPCODE_ELSE ||
  477. inst->Instruction.Opcode == TGSI_OPCODE_BGNFOR ||
  478. inst->Instruction.Opcode == TGSI_OPCODE_BGNLOOP) {
  479. ctx->indentation += indent_spaces;
  480. }
  481. EOL();
  482. return TRUE;
  483. }
  484. void
  485. tgsi_dump_instruction(
  486. const struct tgsi_full_instruction *inst,
  487. uint instno )
  488. {
  489. struct dump_ctx ctx;
  490. ctx.instno = instno;
  491. ctx.indent = 0;
  492. ctx.printf = dump_ctx_printf;
  493. ctx.indentation = 0;
  494. iter_instruction( &ctx.iter, (struct tgsi_full_instruction *)inst );
  495. }
  496. static boolean
  497. prolog(
  498. struct tgsi_iterate_context *iter )
  499. {
  500. struct dump_ctx *ctx = (struct dump_ctx *) iter;
  501. ENM( iter->processor.Processor, processor_type_names );
  502. EOL();
  503. return TRUE;
  504. }
  505. void
  506. tgsi_dump(
  507. const struct tgsi_token *tokens,
  508. uint flags )
  509. {
  510. struct dump_ctx ctx;
  511. ctx.iter.prolog = prolog;
  512. ctx.iter.iterate_instruction = iter_instruction;
  513. ctx.iter.iterate_declaration = iter_declaration;
  514. ctx.iter.iterate_immediate = iter_immediate;
  515. ctx.iter.iterate_property = iter_property;
  516. ctx.iter.epilog = NULL;
  517. ctx.instno = 0;
  518. ctx.indent = 0;
  519. ctx.printf = dump_ctx_printf;
  520. ctx.indentation = 0;
  521. tgsi_iterate_shader( tokens, &ctx.iter );
  522. }
  523. struct str_dump_ctx
  524. {
  525. struct dump_ctx base;
  526. char *str;
  527. char *ptr;
  528. int left;
  529. };
  530. static void
  531. str_dump_ctx_printf(struct dump_ctx *ctx, const char *format, ...)
  532. {
  533. struct str_dump_ctx *sctx = (struct str_dump_ctx *)ctx;
  534. if(sctx->left > 1) {
  535. int written;
  536. va_list ap;
  537. va_start(ap, format);
  538. written = util_vsnprintf(sctx->ptr, sctx->left, format, ap);
  539. va_end(ap);
  540. /* Some complicated logic needed to handle the return value of
  541. * vsnprintf:
  542. */
  543. if (written > 0) {
  544. written = MIN2(sctx->left, written);
  545. sctx->ptr += written;
  546. sctx->left -= written;
  547. }
  548. }
  549. }
  550. void
  551. tgsi_dump_str(
  552. const struct tgsi_token *tokens,
  553. uint flags,
  554. char *str,
  555. size_t size)
  556. {
  557. struct str_dump_ctx ctx;
  558. ctx.base.iter.prolog = prolog;
  559. ctx.base.iter.iterate_instruction = iter_instruction;
  560. ctx.base.iter.iterate_declaration = iter_declaration;
  561. ctx.base.iter.iterate_immediate = iter_immediate;
  562. ctx.base.iter.iterate_property = iter_property;
  563. ctx.base.iter.epilog = NULL;
  564. ctx.base.instno = 0;
  565. ctx.base.indent = 0;
  566. ctx.base.printf = &str_dump_ctx_printf;
  567. ctx.base.indentation = 0;
  568. ctx.str = str;
  569. ctx.str[0] = 0;
  570. ctx.ptr = str;
  571. ctx.left = (int)size;
  572. tgsi_iterate_shader( tokens, &ctx.base.iter );
  573. }