ShaderBinary.cpp 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ShaderBinary.cpp //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Vertex shader binary format parsing and encoding. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. // HLSL change start
  12. #include "ShaderBinaryIncludes.h"
  13. // HLSL change end
  14. /*==========================================================================;
  15. *
  16. * D3D10ShaderBinary namespace
  17. *
  18. ***************************************************************************/
  19. namespace D3D10ShaderBinary
  20. {
  21. BOOL IsOpCodeValid(D3D10_SB_OPCODE_TYPE OpCode)
  22. {
  23. return OpCode < D3D10_SB_NUM_OPCODES;
  24. }
  25. UINT GetNumInstructionOperands(D3D10_SB_OPCODE_TYPE OpCode)
  26. {
  27. if (IsOpCodeValid(OpCode))
  28. return g_InstructionInfo[OpCode].m_NumOperands;
  29. else
  30. throw E_FAIL;
  31. }
  32. CInstructionInfo g_InstructionInfo[D3D10_SB_NUM_OPCODES];
  33. void InitInstructionInfo()
  34. {
  35. #define SET(OpCode, Name, NumOperands, PrecMask, OpClass) \
  36. (g_InstructionInfo[OpCode].Set(NumOperands, Name, OpClass, PrecMask))
  37. SET (D3D10_SB_OPCODE_ADD, "add", 3, 0x06, D3D10_SB_FLOAT_OP);
  38. SET (D3D10_SB_OPCODE_AND, "and", 3, 0x06, D3D10_SB_BIT_OP);
  39. SET (D3D10_SB_OPCODE_BREAK, "break", 0, 0x00, D3D10_SB_FLOW_OP);
  40. SET (D3D10_SB_OPCODE_BREAKC, "breakc", 1, 0x00, D3D10_SB_FLOW_OP);
  41. SET (D3D10_SB_OPCODE_CALL, "call", 1, 0x00, D3D10_SB_FLOW_OP);
  42. SET (D3D10_SB_OPCODE_CALLC, "callc", 2, 0x00, D3D10_SB_FLOW_OP);
  43. SET (D3D10_SB_OPCODE_CONTINUE, "continue", 0, 0x00, D3D10_SB_FLOW_OP);
  44. SET (D3D10_SB_OPCODE_CONTINUEC, "continuec", 1, 0x00, D3D10_SB_FLOW_OP);
  45. SET (D3D10_SB_OPCODE_CASE, "case", 1, 0x00, D3D10_SB_FLOW_OP);
  46. SET (D3D10_SB_OPCODE_CUT, "cut", 0, 0x00, D3D10_SB_FLOW_OP);
  47. SET (D3D10_SB_OPCODE_DEFAULT, "default", 0, 0x00, D3D10_SB_FLOW_OP);
  48. SET (D3D10_SB_OPCODE_DISCARD, "discard", 1, 0x00, D3D10_SB_FLOW_OP);
  49. SET (D3D10_SB_OPCODE_DIV, "div", 3, 0x06, D3D10_SB_FLOAT_OP);
  50. SET (D3D10_SB_OPCODE_DP2, "dp2", 3, 0x06, D3D10_SB_FLOAT_OP);
  51. SET (D3D10_SB_OPCODE_DP3, "dp3", 3, 0x06, D3D10_SB_FLOAT_OP);
  52. SET (D3D10_SB_OPCODE_DP4, "dp4", 3, 0x06, D3D10_SB_FLOAT_OP);
  53. SET (D3D10_SB_OPCODE_ELSE, "else", 0, 0x00, D3D10_SB_FLOW_OP);
  54. SET (D3D10_SB_OPCODE_EMIT, "emit", 0, 0x00, D3D10_SB_FLOW_OP);
  55. SET (D3D10_SB_OPCODE_EMITTHENCUT, "emit_then_cut", 0, 0x00, D3D10_SB_FLOW_OP);
  56. SET (D3D10_SB_OPCODE_ENDIF, "endif", 0, 0x00, D3D10_SB_FLOW_OP);
  57. SET (D3D10_SB_OPCODE_ENDLOOP, "endloop", 0, 0x00, D3D10_SB_FLOW_OP);
  58. SET (D3D10_SB_OPCODE_ENDSWITCH, "endswitch", 0, 0x00, D3D10_SB_FLOW_OP);
  59. SET (D3D10_SB_OPCODE_EQ, "eq", 3, 0x00, D3D10_SB_FLOAT_OP);
  60. SET (D3D10_SB_OPCODE_EXP, "exp", 2, 0x02, D3D10_SB_FLOAT_OP);
  61. SET (D3D10_SB_OPCODE_FRC, "frc", 2, 0x02, D3D10_SB_FLOAT_OP);
  62. SET (D3D10_SB_OPCODE_FTOI, "ftoi", 2, 0x00, D3D10_SB_FLOAT_OP);
  63. SET (D3D10_SB_OPCODE_FTOU, "ftou", 2, 0x00, D3D10_SB_FLOAT_OP);
  64. SET (D3D10_SB_OPCODE_GE, "ge", 3, 0x00, D3D10_SB_FLOAT_OP);
  65. SET (D3D10_SB_OPCODE_DERIV_RTX, "deriv_rtx", 2, 0x02, D3D10_SB_FLOAT_OP);
  66. SET (D3D10_SB_OPCODE_DERIV_RTY, "deriv_rty", 2, 0x02, D3D10_SB_FLOAT_OP);
  67. SET (D3D10_SB_OPCODE_IADD, "iadd", 3, 0x06, D3D10_SB_INT_OP);
  68. SET (D3D10_SB_OPCODE_IF, "if", 1, 0x00, D3D10_SB_FLOW_OP);
  69. SET (D3D10_SB_OPCODE_IEQ, "ieq", 3, 0x00, D3D10_SB_INT_OP);
  70. SET (D3D10_SB_OPCODE_IGE, "ige", 3, 0x00, D3D10_SB_INT_OP);
  71. SET (D3D10_SB_OPCODE_ILT, "ilt", 3, 0x00, D3D10_SB_INT_OP);
  72. SET (D3D10_SB_OPCODE_IMAD, "imad", 4, 0x0e, D3D10_SB_INT_OP);
  73. SET (D3D10_SB_OPCODE_IMAX, "imax", 3, 0x06, D3D10_SB_INT_OP);
  74. SET (D3D10_SB_OPCODE_IMIN, "imin", 3, 0x06, D3D10_SB_INT_OP);
  75. SET (D3D10_SB_OPCODE_IMUL, "imul", 4, 0x0c, D3D10_SB_INT_OP);
  76. SET (D3D10_SB_OPCODE_INE, "ine", 3, 0x00, D3D10_SB_INT_OP);
  77. SET (D3D10_SB_OPCODE_INEG, "ineg", 2, 0x02, D3D10_SB_INT_OP);
  78. SET (D3D10_SB_OPCODE_ISHL, "ishl", 3, 0x02, D3D10_SB_INT_OP);
  79. SET (D3D10_SB_OPCODE_ISHR, "ishr", 3, 0x02, D3D10_SB_INT_OP);
  80. SET (D3D10_SB_OPCODE_ITOF, "itof", 2, 0x00, D3D10_SB_INT_OP);
  81. SET (D3D10_SB_OPCODE_LABEL, "label", 1, 0x00, D3D10_SB_FLOW_OP);
  82. SET (D3D10_SB_OPCODE_LD, "ld", 3, 0x00, D3D10_SB_TEX_OP);
  83. SET (D3D10_SB_OPCODE_LD_MS, "ldms", 4, 0x00, D3D10_SB_TEX_OP);
  84. SET (D3D10_SB_OPCODE_LOG, "log", 2, 0x02, D3D10_SB_FLOAT_OP);
  85. SET (D3D10_SB_OPCODE_LOOP, "loop", 0, 0x00, D3D10_SB_FLOW_OP);
  86. SET (D3D10_SB_OPCODE_LT, "lt", 3, 0x00, D3D10_SB_FLOAT_OP);
  87. SET (D3D10_SB_OPCODE_MAD, "mad", 4, 0x0e, D3D10_SB_FLOAT_OP);
  88. SET (D3D10_SB_OPCODE_MAX, "max", 3, 0x06, D3D10_SB_FLOAT_OP);
  89. SET (D3D10_SB_OPCODE_MIN, "min", 3, 0x06, D3D10_SB_FLOAT_OP);
  90. SET (D3D10_SB_OPCODE_MOV, "mov", 2, 0x02, D3D10_SB_FLOAT_OP);
  91. SET (D3D10_SB_OPCODE_MOVC, "movc", 4, 0x0c, D3D10_SB_FLOAT_OP);
  92. SET (D3D10_SB_OPCODE_MUL, "mul", 3, 0x06, D3D10_SB_FLOAT_OP);
  93. SET (D3D10_SB_OPCODE_NE, "ne", 3, 0x00, D3D10_SB_FLOAT_OP);
  94. SET (D3D10_SB_OPCODE_NOP, "nop", 0, 0x00, D3D10_SB_FLOW_OP);
  95. SET (D3D10_SB_OPCODE_NOT, "not", 2, 0x02, D3D10_SB_BIT_OP);
  96. SET (D3D10_SB_OPCODE_OR, "or", 3, 0x06, D3D10_SB_BIT_OP);
  97. SET (D3D10_SB_OPCODE_RESINFO, "resinfo", 3, 0x00, D3D10_SB_TEX_OP);
  98. SET (D3D10_SB_OPCODE_RET, "ret", 0, 0x00, D3D10_SB_FLOW_OP);
  99. SET (D3D10_SB_OPCODE_RETC, "retc", 1, 0x00, D3D10_SB_FLOW_OP);
  100. SET (D3D10_SB_OPCODE_ROUND_NE, "round_ne", 2, 0x02, D3D10_SB_FLOAT_OP);
  101. SET (D3D10_SB_OPCODE_ROUND_NI, "round_ni", 2, 0x02, D3D10_SB_FLOAT_OP);
  102. SET (D3D10_SB_OPCODE_ROUND_PI, "round_pi", 2, 0x02, D3D10_SB_FLOAT_OP);
  103. SET (D3D10_SB_OPCODE_ROUND_Z, "round_z", 2, 0x02, D3D10_SB_FLOAT_OP);
  104. SET (D3D10_SB_OPCODE_RSQ, "rsq", 2, 0x02, D3D10_SB_FLOAT_OP);
  105. SET (D3D10_SB_OPCODE_SAMPLE, "sample", 4, 0x00, D3D10_SB_TEX_OP);
  106. SET (D3D10_SB_OPCODE_SAMPLE_B, "sample_b", 5, 0x00, D3D10_SB_TEX_OP);
  107. SET (D3D10_SB_OPCODE_SAMPLE_L, "sample_l", 5, 0x00, D3D10_SB_TEX_OP);
  108. SET (D3D10_SB_OPCODE_SAMPLE_D, "sample_d", 6, 0x00, D3D10_SB_TEX_OP);
  109. SET (D3D10_SB_OPCODE_SAMPLE_C, "sample_c", 5, 0x00, D3D10_SB_TEX_OP);
  110. SET (D3D10_SB_OPCODE_SAMPLE_C_LZ, "sample_c_lz", 5, 0x00, D3D10_SB_TEX_OP);
  111. SET (D3D10_SB_OPCODE_SQRT, "sqrt", 2, 0x02, D3D10_SB_FLOAT_OP);
  112. SET (D3D10_SB_OPCODE_SWITCH, "switch", 1, 0x00, D3D10_SB_FLOW_OP);
  113. SET (D3D10_SB_OPCODE_SINCOS, "sincos", 3, 0x04, D3D10_SB_FLOAT_OP);
  114. SET (D3D10_SB_OPCODE_UDIV, "udiv", 4, 0x0c, D3D10_SB_UINT_OP);
  115. SET (D3D10_SB_OPCODE_ULT, "ult", 3, 0x00, D3D10_SB_UINT_OP);
  116. SET (D3D10_SB_OPCODE_UGE, "uge", 3, 0x00, D3D10_SB_UINT_OP);
  117. SET (D3D10_SB_OPCODE_UMAX, "umax", 3, 0x06, D3D10_SB_UINT_OP);
  118. SET (D3D10_SB_OPCODE_UMIN, "umin", 3, 0x06, D3D10_SB_UINT_OP);
  119. SET (D3D10_SB_OPCODE_UMUL, "umul", 4, 0x0c, D3D10_SB_UINT_OP);
  120. SET (D3D10_SB_OPCODE_UMAD, "umad", 4, 0x0e, D3D10_SB_UINT_OP);
  121. SET (D3D10_SB_OPCODE_USHR, "ushr", 3, 0x02, D3D10_SB_UINT_OP);
  122. SET (D3D10_SB_OPCODE_UTOF, "utof", 2, 0x00, D3D10_SB_UINT_OP);
  123. SET (D3D10_SB_OPCODE_XOR, "xor", 3, 0x06, D3D10_SB_BIT_OP);
  124. SET (D3D10_SB_OPCODE_RESERVED0, "jmp", 0, 0x00, D3D10_SB_FLOW_OP);
  125. SET (D3D10_SB_OPCODE_DCL_INPUT, "dcl_input", 1, 0x00, D3D10_SB_DCL_OP);
  126. SET (D3D10_SB_OPCODE_DCL_OUTPUT, "dcl_output", 1, 0x00, D3D10_SB_DCL_OP);
  127. SET (D3D10_SB_OPCODE_DCL_INPUT_SGV, "dcl_input_sgv", 1, 0x00, D3D10_SB_DCL_OP);
  128. SET (D3D10_SB_OPCODE_DCL_INPUT_PS_SGV, "dcl_input_ps_sgv", 1, 0x00, D3D10_SB_DCL_OP);
  129. SET (D3D10_SB_OPCODE_DCL_GS_INPUT_PRIMITIVE, "dcl_inputprimitive", 0, 0x00, D3D10_SB_DCL_OP);
  130. SET (D3D10_SB_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY, "dcl_outputtopology", 0, 0x00, D3D10_SB_DCL_OP);
  131. SET (D3D10_SB_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT, "dcl_maxout", 0, 0x00, D3D10_SB_DCL_OP);
  132. SET (D3D10_SB_OPCODE_DCL_INPUT_PS, "dcl_input_ps", 1, 0x00, D3D10_SB_DCL_OP);
  133. SET (D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER, "dcl_constantbuffer", 1, 0x00, D3D10_SB_DCL_OP);
  134. SET (D3D10_SB_OPCODE_DCL_SAMPLER, "dcl_sampler", 1, 0x00, D3D10_SB_DCL_OP);
  135. SET (D3D10_SB_OPCODE_DCL_RESOURCE, "dcl_resource", 1, 0x00, D3D10_SB_DCL_OP);
  136. SET (D3D10_SB_OPCODE_DCL_INPUT_SIV, "dcl_input_siv", 1, 0x00, D3D10_SB_DCL_OP);
  137. SET (D3D10_SB_OPCODE_DCL_INPUT_PS_SIV, "dcl_input_ps_siv", 1, 0x00, D3D10_SB_DCL_OP);
  138. SET (D3D10_SB_OPCODE_DCL_OUTPUT_SIV, "dcl_output_siv", 1, 0x00, D3D10_SB_DCL_OP);
  139. SET (D3D10_SB_OPCODE_DCL_OUTPUT_SGV, "dcl_output_sgv", 1, 0x00, D3D10_SB_DCL_OP);
  140. SET (D3D10_SB_OPCODE_DCL_TEMPS, "dcl_temps", 0, 0x00, D3D10_SB_DCL_OP);
  141. SET (D3D10_SB_OPCODE_DCL_INDEXABLE_TEMP, "dcl_indexableTemp", 0, 0x00, D3D10_SB_DCL_OP);
  142. SET (D3D10_SB_OPCODE_DCL_INDEX_RANGE, "dcl_indexrange", 1, 0x00, D3D10_SB_DCL_OP);
  143. SET (D3D10_SB_OPCODE_DCL_GLOBAL_FLAGS, "dcl_globalFlags", 0, 0x00, D3D10_SB_DCL_OP);
  144. SET (D3D10_1_SB_OPCODE_SAMPLE_INFO, "sampleinfo", 2, 0x00, D3D10_SB_TEX_OP);
  145. SET (D3D10_1_SB_OPCODE_SAMPLE_POS, "samplepos", 3, 0x00, D3D10_SB_TEX_OP);
  146. SET (D3D10_1_SB_OPCODE_GATHER4, "gather4", 4, 0x00, D3D10_SB_TEX_OP);
  147. SET (D3D10_1_SB_OPCODE_LOD, "lod", 4, 0x00, D3D10_SB_TEX_OP);
  148. SET (D3D11_SB_OPCODE_EMIT_STREAM, "emit_stream", 1, 0x00, D3D10_SB_FLOW_OP);
  149. SET (D3D11_SB_OPCODE_CUT_STREAM, "cut_stream", 1, 0x00, D3D10_SB_FLOW_OP);
  150. SET (D3D11_SB_OPCODE_EMITTHENCUT_STREAM, "emit_then_cut_stream", 1, 0x00, D3D10_SB_FLOW_OP);
  151. SET (D3D11_SB_OPCODE_INTERFACE_CALL, "fcall", 1, 0x00, D3D10_SB_FLOW_OP);
  152. SET (D3D11_SB_OPCODE_DCL_STREAM, "dcl_stream", 1, 0x00, D3D10_SB_DCL_OP);
  153. SET (D3D11_SB_OPCODE_DCL_FUNCTION_BODY, "dcl_function_body", 0, 0x00, D3D10_SB_DCL_OP);
  154. SET (D3D11_SB_OPCODE_DCL_FUNCTION_TABLE, "dcl_function_table", 0, 0x00, D3D10_SB_DCL_OP);
  155. SET (D3D11_SB_OPCODE_DCL_INTERFACE, "dcl_interface", 0, 0x00, D3D10_SB_DCL_OP);
  156. SET (D3D11_SB_OPCODE_BUFINFO, "bufinfo", 2, 0x00, D3D10_SB_TEX_OP);
  157. SET (D3D11_SB_OPCODE_DERIV_RTX_COARSE, "deriv_rtx_coarse", 2, 0x02, D3D10_SB_FLOAT_OP);
  158. SET (D3D11_SB_OPCODE_DERIV_RTX_FINE, "deriv_rtx_fine", 2, 0x02, D3D10_SB_FLOAT_OP);
  159. SET (D3D11_SB_OPCODE_DERIV_RTY_COARSE, "deriv_rty_coarse", 2, 0x02, D3D10_SB_FLOAT_OP);
  160. SET (D3D11_SB_OPCODE_DERIV_RTY_FINE, "deriv_rty_fine", 2, 0x02, D3D10_SB_FLOAT_OP);
  161. SET (D3D11_SB_OPCODE_GATHER4_C, "gather4_c", 5, 0x00, D3D10_SB_TEX_OP);
  162. SET (D3D11_SB_OPCODE_GATHER4_PO, "gather4_po", 5, 0x00, D3D10_SB_TEX_OP);
  163. SET (D3D11_SB_OPCODE_GATHER4_PO_C, "gather4_po_c", 6, 0x00, D3D10_SB_TEX_OP);
  164. SET (D3D11_SB_OPCODE_RCP, "rcp", 2, 0x02, D3D10_SB_FLOAT_OP);
  165. SET (D3D11_SB_OPCODE_F32TOF16, "f32tof16", 2, 0x00, D3D10_SB_FLOAT_OP);
  166. SET (D3D11_SB_OPCODE_F16TOF32, "f16tof32", 2, 0x00, D3D10_SB_FLOAT_OP);
  167. SET (D3D11_SB_OPCODE_UADDC, "uaddc", 4, 0x0c, D3D10_SB_UINT_OP);
  168. SET (D3D11_SB_OPCODE_USUBB, "usubb", 4, 0x0c, D3D10_SB_UINT_OP);
  169. SET (D3D11_SB_OPCODE_COUNTBITS, "countbits", 2, 0x02, D3D10_SB_BIT_OP);
  170. SET (D3D11_SB_OPCODE_FIRSTBIT_HI, "firstbit_hi", 2, 0x02, D3D10_SB_BIT_OP);
  171. SET (D3D11_SB_OPCODE_FIRSTBIT_LO, "firstbit_lo", 2, 0x02, D3D10_SB_BIT_OP);
  172. SET (D3D11_SB_OPCODE_FIRSTBIT_SHI, "firstbit_shi", 2, 0x02, D3D10_SB_BIT_OP);
  173. SET (D3D11_SB_OPCODE_UBFE, "ubfe", 4, 0x02, D3D10_SB_BIT_OP);
  174. SET (D3D11_SB_OPCODE_IBFE, "ibfe", 4, 0x02, D3D10_SB_BIT_OP);
  175. SET (D3D11_SB_OPCODE_BFI, "bfi", 5, 0x02, D3D10_SB_BIT_OP);
  176. SET (D3D11_SB_OPCODE_BFREV, "bfrev", 2, 0x02, D3D10_SB_BIT_OP);
  177. SET (D3D11_SB_OPCODE_SWAPC, "swapc", 5, 0x02, D3D10_SB_FLOAT_OP);
  178. SET (D3D11_SB_OPCODE_HS_DECLS, "hs_decls", 0, 0x00, D3D10_SB_DCL_OP);
  179. SET (D3D11_SB_OPCODE_HS_CONTROL_POINT_PHASE, "hs_control_point_phase", 0, 0x00, D3D10_SB_DCL_OP);
  180. SET (D3D11_SB_OPCODE_HS_FORK_PHASE, "hs_fork_phase", 0, 0x00, D3D10_SB_DCL_OP);
  181. SET (D3D11_SB_OPCODE_HS_JOIN_PHASE, "hs_join_phase", 0, 0x00, D3D10_SB_DCL_OP);
  182. SET (D3D11_SB_OPCODE_DCL_INPUT_CONTROL_POINT_COUNT, "dcl_input_control_point_count", 0, 0x00, D3D10_SB_DCL_OP);
  183. SET (D3D11_SB_OPCODE_DCL_OUTPUT_CONTROL_POINT_COUNT, "dcl_output_control_point_count", 0, 0x00, D3D10_SB_DCL_OP);
  184. SET (D3D11_SB_OPCODE_DCL_TESS_DOMAIN, "dcl_tessellator_domain", 0, 0x00, D3D10_SB_DCL_OP);
  185. SET (D3D11_SB_OPCODE_DCL_TESS_PARTITIONING, "dcl_tessellator_partitioning", 0, 0x00, D3D10_SB_DCL_OP);
  186. SET (D3D11_SB_OPCODE_DCL_TESS_OUTPUT_PRIMITIVE, "dcl_tessellator_output_primitive", 0, 0x00, D3D10_SB_DCL_OP);
  187. SET (D3D11_SB_OPCODE_DCL_HS_MAX_TESSFACTOR, "dcl_hs_max_tessfactor", 0, 0x00, D3D10_SB_DCL_OP);
  188. SET (D3D11_SB_OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT, "dcl_hs_fork_phase_instance_count", 0, 0x00, D3D10_SB_DCL_OP);
  189. SET (D3D11_SB_OPCODE_DCL_HS_JOIN_PHASE_INSTANCE_COUNT, "dcl_hs_join_phase_instance_count", 0, 0x00, D3D10_SB_DCL_OP);
  190. SET (D3D11_SB_OPCODE_DCL_THREAD_GROUP, "dcl_thread_group", 0, 0x00, D3D10_SB_DCL_OP);
  191. SET (D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_TYPED, "dcl_uav_typed", 1, 0x00, D3D10_SB_DCL_OP);
  192. SET (D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_RAW, "dcl_uav_raw", 1, 0x00, D3D10_SB_DCL_OP);
  193. SET (D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED, "dcl_uav_structured", 1, 0x00, D3D10_SB_DCL_OP);
  194. SET (D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_RAW, "dcl_tgsm_raw", 1, 0x00, D3D10_SB_DCL_OP);
  195. SET (D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED, "dcl_tgsm_structured", 1, 0x00, D3D10_SB_DCL_OP);
  196. SET (D3D11_SB_OPCODE_DCL_RESOURCE_RAW, "dcl_resource_raw", 1, 0x00, D3D10_SB_DCL_OP);
  197. SET (D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED, "dcl_resource_structured", 1, 0x00, D3D10_SB_DCL_OP);
  198. SET (D3D11_SB_OPCODE_LD_UAV_TYPED, "ld_uav_typed", 3, 0x00, D3D11_SB_MEM_OP);
  199. SET (D3D11_SB_OPCODE_STORE_UAV_TYPED, "store_uav_typed", 3, 0x00, D3D11_SB_MEM_OP);
  200. SET (D3D11_SB_OPCODE_LD_RAW, "ld_raw", 3, 0x00, D3D11_SB_MEM_OP);
  201. SET (D3D11_SB_OPCODE_STORE_RAW, "store_raw", 3, 0x00, D3D11_SB_MEM_OP);
  202. SET (D3D11_SB_OPCODE_LD_STRUCTURED, "ld_structured", 4, 0x00, D3D11_SB_MEM_OP);
  203. SET (D3D11_SB_OPCODE_STORE_STRUCTURED, "store_structured", 4, 0x00, D3D11_SB_MEM_OP);
  204. SET (D3D11_SB_OPCODE_ATOMIC_AND, "atomic_and", 3, 0x00, D3D11_SB_ATOMIC_OP);
  205. SET (D3D11_SB_OPCODE_ATOMIC_OR, "atomic_or", 3, 0x00, D3D11_SB_ATOMIC_OP);
  206. SET (D3D11_SB_OPCODE_ATOMIC_XOR, "atomic_xor", 3, 0x00, D3D11_SB_ATOMIC_OP);
  207. SET (D3D11_SB_OPCODE_ATOMIC_CMP_STORE, "atomic_cmp_store", 4, 0x00, D3D11_SB_ATOMIC_OP);
  208. SET (D3D11_SB_OPCODE_ATOMIC_IADD, "atomic_iadd", 3, 0x00, D3D11_SB_ATOMIC_OP);
  209. SET (D3D11_SB_OPCODE_ATOMIC_IMAX, "atomic_imax", 3, 0x00, D3D11_SB_ATOMIC_OP);
  210. SET (D3D11_SB_OPCODE_ATOMIC_IMIN, "atomic_imin", 3, 0x00, D3D11_SB_ATOMIC_OP);
  211. SET (D3D11_SB_OPCODE_ATOMIC_UMAX, "atomic_umax", 3, 0x00, D3D11_SB_ATOMIC_OP);
  212. SET (D3D11_SB_OPCODE_ATOMIC_UMIN, "atomic_umin", 3, 0x00, D3D11_SB_ATOMIC_OP);
  213. SET (D3D11_SB_OPCODE_IMM_ATOMIC_ALLOC, "imm_atomic_alloc", 2, 0x00, D3D11_SB_ATOMIC_OP);
  214. SET (D3D11_SB_OPCODE_IMM_ATOMIC_CONSUME, "imm_atomic_consume", 2, 0x00, D3D11_SB_ATOMIC_OP);
  215. SET (D3D11_SB_OPCODE_IMM_ATOMIC_IADD, "imm_atomic_iadd", 4, 0x00, D3D11_SB_ATOMIC_OP);
  216. SET (D3D11_SB_OPCODE_IMM_ATOMIC_AND, "imm_atomic_and", 4, 0x00, D3D11_SB_ATOMIC_OP);
  217. SET (D3D11_SB_OPCODE_IMM_ATOMIC_OR, "imm_atomic_or", 4, 0x00, D3D11_SB_ATOMIC_OP);
  218. SET (D3D11_SB_OPCODE_IMM_ATOMIC_XOR, "imm_atomic_xor", 4, 0x00, D3D11_SB_ATOMIC_OP);
  219. SET (D3D11_SB_OPCODE_IMM_ATOMIC_EXCH, "imm_atomic_exch", 4, 0x00, D3D11_SB_ATOMIC_OP);
  220. SET (D3D11_SB_OPCODE_IMM_ATOMIC_CMP_EXCH, "imm_atomic_cmp_exch", 5, 0x00, D3D11_SB_ATOMIC_OP);
  221. SET (D3D11_SB_OPCODE_IMM_ATOMIC_IMAX, "imm_atomic_imax", 4, 0x00, D3D11_SB_ATOMIC_OP);
  222. SET (D3D11_SB_OPCODE_IMM_ATOMIC_IMIN, "imm_atomic_imin", 4, 0x00, D3D11_SB_ATOMIC_OP);
  223. SET (D3D11_SB_OPCODE_IMM_ATOMIC_UMAX, "imm_atomic_umax", 4, 0x00, D3D11_SB_ATOMIC_OP);
  224. SET (D3D11_SB_OPCODE_IMM_ATOMIC_UMIN, "imm_atomic_umin", 4, 0x00, D3D11_SB_ATOMIC_OP);
  225. SET (D3D11_SB_OPCODE_SYNC, "sync", 0, 0x00, D3D10_SB_FLOW_OP);
  226. SET (D3D11_SB_OPCODE_EVAL_SNAPPED, "eval_snapped", 3, 0x02, D3D10_SB_FLOAT_OP);
  227. SET (D3D11_SB_OPCODE_EVAL_SAMPLE_INDEX, "eval_sample_index", 3, 0x02, D3D10_SB_FLOAT_OP);
  228. SET (D3D11_SB_OPCODE_EVAL_CENTROID, "eval_centroid", 2, 0x02, D3D10_SB_FLOAT_OP);
  229. SET (D3D11_SB_OPCODE_DCL_GS_INSTANCE_COUNT, "dcl_gsinstances", 0, 0x00, D3D10_SB_DCL_OP);
  230. SET (D3D11_SB_OPCODE_DADD, "dadd", 3, 0x06, D3D11_SB_DOUBLE_OP);
  231. SET (D3D11_SB_OPCODE_DMAX, "dmax", 3, 0x06, D3D11_SB_DOUBLE_OP);
  232. SET (D3D11_SB_OPCODE_DMIN, "dmin", 3, 0x06, D3D11_SB_DOUBLE_OP);
  233. SET (D3D11_SB_OPCODE_DMUL, "dmul", 3, 0x06, D3D11_SB_DOUBLE_OP);
  234. SET (D3D11_SB_OPCODE_DEQ, "deq", 3, 0x00, D3D11_SB_DOUBLE_OP);
  235. SET (D3D11_SB_OPCODE_DGE, "dge", 3, 0x00, D3D11_SB_DOUBLE_OP);
  236. SET (D3D11_SB_OPCODE_DLT, "dlt", 3, 0x00, D3D11_SB_DOUBLE_OP);
  237. SET (D3D11_SB_OPCODE_DNE, "dne", 3, 0x00, D3D11_SB_DOUBLE_OP);
  238. SET (D3D11_SB_OPCODE_DMOV, "dmov", 2, 0x02, D3D11_SB_DOUBLE_OP);
  239. SET (D3D11_SB_OPCODE_DMOVC, "dmovc", 4, 0x0c, D3D11_SB_DOUBLE_OP);
  240. SET (D3D11_SB_OPCODE_DTOF, "dtof", 2, 0x02, D3D11_SB_DOUBLE_TO_FLOAT_OP);
  241. SET (D3D11_SB_OPCODE_FTOD, "ftod", 2, 0x00, D3D11_SB_FLOAT_TO_DOUBLE_OP);
  242. SET (D3D11_SB_OPCODE_ABORT, "abort", 0, 0x00, D3D11_SB_DEBUG_OP);
  243. SET (D3D11_SB_OPCODE_DEBUG_BREAK, "debug_break", 0, 0x00, D3D11_SB_DEBUG_OP);
  244. SET (D3D11_1_SB_OPCODE_DDIV, "ddiv", 3, 0x06, D3D11_SB_DOUBLE_OP);
  245. SET (D3D11_1_SB_OPCODE_DFMA, "dfma", 4, 0x0e, D3D11_SB_DOUBLE_OP);
  246. SET (D3D11_1_SB_OPCODE_DRCP, "drcp", 2, 0x02, D3D11_SB_DOUBLE_OP);
  247. SET (D3D11_1_SB_OPCODE_MSAD, "msad", 4, 0x0e, D3D10_SB_UINT_OP);
  248. SET (D3D11_1_SB_OPCODE_DTOI, "dtoi", 2, 0x00, D3D11_SB_DOUBLE_OP);
  249. SET (D3D11_1_SB_OPCODE_DTOU, "dtou", 2, 0x00, D3D11_SB_DOUBLE_OP);
  250. SET (D3D11_1_SB_OPCODE_ITOD, "itod", 2, 0x00, D3D10_SB_INT_OP);
  251. SET (D3D11_1_SB_OPCODE_UTOD, "utod", 2, 0x00, D3D10_SB_UINT_OP);
  252. SET (D3DWDDM1_3_SB_OPCODE_GATHER4_FEEDBACK,"gather4_s", 5, 0x00, D3D10_SB_TEX_OP);
  253. SET (D3DWDDM1_3_SB_OPCODE_GATHER4_C_FEEDBACK,"gather4_c_s", 6, 0x00, D3D10_SB_TEX_OP);
  254. SET (D3DWDDM1_3_SB_OPCODE_GATHER4_PO_FEEDBACK,"gather4_po_s", 6, 0x00, D3D10_SB_TEX_OP);
  255. SET (D3DWDDM1_3_SB_OPCODE_GATHER4_PO_C_FEEDBACK,"gather4_po_c_s", 7, 0x00, D3D10_SB_TEX_OP);
  256. SET (D3DWDDM1_3_SB_OPCODE_LD_FEEDBACK,"ld_s", 4, 0x00, D3D10_SB_TEX_OP);
  257. SET (D3DWDDM1_3_SB_OPCODE_LD_MS_FEEDBACK,"ldms_s", 5, 0x00, D3D10_SB_TEX_OP);
  258. SET (D3DWDDM1_3_SB_OPCODE_LD_UAV_TYPED_FEEDBACK,"ld_uav_typed_s", 4, 0x00, D3D11_SB_MEM_OP);
  259. SET (D3DWDDM1_3_SB_OPCODE_LD_RAW_FEEDBACK,"ld_raw_s", 4, 0x00, D3D11_SB_MEM_OP);
  260. SET (D3DWDDM1_3_SB_OPCODE_LD_STRUCTURED_FEEDBACK,"ld_structured_s", 5, 0x00, D3D11_SB_MEM_OP);
  261. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_L_FEEDBACK,"sample_l_s", 6, 0x00, D3D10_SB_TEX_OP);
  262. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_C_LZ_FEEDBACK,"sample_c_lz_s", 6, 0x00, D3D10_SB_TEX_OP);
  263. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_CLAMP_FEEDBACK, "sample_cl_s", 6, 0x00, D3D10_SB_TEX_OP);
  264. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_B_CLAMP_FEEDBACK, "sample_b_cl_s", 7, 0x00, D3D10_SB_TEX_OP);
  265. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_D_CLAMP_FEEDBACK,"sample_d_cl_s", 8, 0x00, D3D10_SB_TEX_OP);
  266. SET (D3DWDDM1_3_SB_OPCODE_SAMPLE_C_CLAMP_FEEDBACK,"sample_c_cl_s", 7, 0x00, D3D10_SB_TEX_OP);
  267. SET (D3DWDDM1_3_SB_OPCODE_CHECK_ACCESS_FULLY_MAPPED, "check_access_fully_mapped",2, 0x00, D3D10_SB_TEX_OP);
  268. }
  269. //*****************************************************************************
  270. //
  271. // CShaderCodeParser
  272. //
  273. //*****************************************************************************
  274. void CShaderCodeParser::SetShader(CONST CShaderToken* pBuffer)
  275. {
  276. m_pShaderCode = (CShaderToken*)pBuffer;
  277. m_pShaderEndToken = (CShaderToken*)pBuffer + pBuffer[1];
  278. // First OpCode token
  279. m_pCurrentToken = (CShaderToken*)&pBuffer[2];
  280. }
  281. D3D10_SB_TOKENIZED_PROGRAM_TYPE CShaderCodeParser::ShaderType()
  282. {
  283. return (D3D10_SB_TOKENIZED_PROGRAM_TYPE)DECODE_D3D10_SB_TOKENIZED_PROGRAM_TYPE(*m_pShaderCode);
  284. }
  285. UINT CShaderCodeParser::CurrentTokenOffset()
  286. {
  287. return (UINT)(m_pCurrentToken - m_pShaderCode);
  288. }
  289. void CShaderCodeParser::SetCurrentTokenOffset(UINT Offset)
  290. {
  291. m_pCurrentToken = m_pShaderCode + Offset;
  292. }
  293. UINT CShaderCodeParser::ShaderLengthInTokens()
  294. {
  295. return m_pShaderCode[1];
  296. }
  297. UINT CShaderCodeParser::ShaderMinorVersion()
  298. {
  299. return DECODE_D3D10_SB_TOKENIZED_PROGRAM_MINOR_VERSION(m_pShaderCode[0]);
  300. }
  301. UINT CShaderCodeParser::ShaderMajorVersion()
  302. {
  303. return DECODE_D3D10_SB_TOKENIZED_PROGRAM_MAJOR_VERSION(m_pShaderCode[0]);
  304. }
  305. void CShaderCodeParser::ParseIndex(COperandIndex* pOperandIndex, D3D10_SB_OPERAND_INDEX_REPRESENTATION IndexType)
  306. {
  307. switch (IndexType)
  308. {
  309. case D3D10_SB_OPERAND_INDEX_IMMEDIATE32:
  310. pOperandIndex->m_RegIndex = *m_pCurrentToken++;
  311. pOperandIndex->m_ComponentName = D3D10_SB_4_COMPONENT_X;
  312. pOperandIndex->m_RelRegType = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  313. break;
  314. case D3D10_SB_OPERAND_INDEX_IMMEDIATE64:
  315. pOperandIndex->m_RegIndexA[0] = *m_pCurrentToken++;
  316. pOperandIndex->m_RegIndexA[1] = *m_pCurrentToken++;
  317. pOperandIndex->m_ComponentName = D3D10_SB_4_COMPONENT_X;
  318. pOperandIndex->m_RelRegType = D3D10_SB_OPERAND_TYPE_IMMEDIATE64;
  319. break;
  320. case D3D10_SB_OPERAND_INDEX_RELATIVE:
  321. {
  322. COperand operand;
  323. ParseOperand(&operand);
  324. pOperandIndex->m_RelIndex = operand.m_Index[0].m_RegIndex;
  325. pOperandIndex->m_RelIndex1 = operand.m_Index[1].m_RegIndex;
  326. pOperandIndex->m_RelRegType = operand.m_Type;
  327. pOperandIndex->m_IndexDimension = operand.m_IndexDimension;
  328. pOperandIndex->m_ComponentName = operand.m_ComponentName;
  329. pOperandIndex->m_MinPrecision = operand.m_MinPrecision;
  330. break;
  331. }
  332. case D3D10_SB_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE:
  333. {
  334. pOperandIndex->m_RegIndex = *m_pCurrentToken++;
  335. COperand operand;
  336. ParseOperand(&operand);
  337. pOperandIndex->m_RelIndex = operand.m_Index[0].m_RegIndex;
  338. pOperandIndex->m_RelIndex1 = operand.m_Index[1].m_RegIndex;
  339. pOperandIndex->m_RelRegType = operand.m_Type;
  340. pOperandIndex->m_IndexDimension = operand.m_IndexDimension;
  341. pOperandIndex->m_ComponentName = operand.m_ComponentName;
  342. pOperandIndex->m_MinPrecision = operand.m_MinPrecision;
  343. }
  344. break;
  345. default:
  346. throw E_FAIL;
  347. }
  348. }
  349. void CShaderCodeParser::ParseOperand(COperandBase* pOperand)
  350. {
  351. CShaderToken Token = *m_pCurrentToken++;
  352. pOperand->m_Type = DECODE_D3D10_SB_OPERAND_TYPE(Token);
  353. pOperand->m_NumComponents = DECODE_D3D10_SB_OPERAND_NUM_COMPONENTS(Token);
  354. pOperand->m_bExtendedOperand = DECODE_IS_D3D10_SB_OPERAND_EXTENDED(Token);
  355. UINT NumComponents = 0;
  356. switch (pOperand->m_NumComponents)
  357. {
  358. case D3D10_SB_OPERAND_1_COMPONENT: NumComponents = 1; break;
  359. case D3D10_SB_OPERAND_4_COMPONENT: NumComponents = 4; break;
  360. }
  361. switch (pOperand->m_Type)
  362. {
  363. case D3D10_SB_OPERAND_TYPE_IMMEDIATE32:
  364. case D3D10_SB_OPERAND_TYPE_IMMEDIATE64:
  365. break;
  366. default:
  367. {
  368. if (pOperand->m_NumComponents == D3D10_SB_OPERAND_4_COMPONENT)
  369. {
  370. // Component selection mode
  371. pOperand->m_ComponentSelection = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SELECTION_MODE(Token);
  372. switch(pOperand->m_ComponentSelection)
  373. {
  374. case D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE:
  375. pOperand->m_WriteMask = DECODE_D3D10_SB_OPERAND_4_COMPONENT_MASK(Token);
  376. break;
  377. case D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_MODE:
  378. pOperand->m_Swizzle[0] = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(Token, 0);
  379. pOperand->m_Swizzle[1] = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(Token, 1);
  380. pOperand->m_Swizzle[2] = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(Token, 2);
  381. pOperand->m_Swizzle[3] = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_SOURCE(Token, 3);
  382. break;
  383. case D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE:
  384. {
  385. D3D10_SB_4_COMPONENT_NAME Component = DECODE_D3D10_SB_OPERAND_4_COMPONENT_SELECT_1(Token);
  386. pOperand->m_Swizzle[0] = static_cast<BYTE>(Component);
  387. pOperand->m_Swizzle[1] = static_cast<BYTE>(Component);
  388. pOperand->m_Swizzle[2] = static_cast<BYTE>(Component);
  389. pOperand->m_Swizzle[3] = static_cast<BYTE>(Component);
  390. pOperand->m_ComponentName = Component;
  391. break;
  392. }
  393. default:
  394. throw E_FAIL;
  395. }
  396. }
  397. pOperand->m_IndexDimension = DECODE_D3D10_SB_OPERAND_INDEX_DIMENSION(Token);
  398. if (pOperand->m_IndexDimension != D3D10_SB_OPERAND_INDEX_0D)
  399. {
  400. UINT NumDimensions = pOperand->m_IndexDimension;
  401. // Index representation
  402. for (UINT i=0; i < NumDimensions; i++)
  403. {
  404. pOperand->m_IndexType[i] = DECODE_D3D10_SB_OPERAND_INDEX_REPRESENTATION(i, Token);
  405. }
  406. }
  407. break;
  408. }
  409. }
  410. // Extended operand
  411. if (pOperand->m_bExtendedOperand)
  412. {
  413. Token = *m_pCurrentToken++;
  414. pOperand->m_ExtendedOperandType = DECODE_D3D10_SB_EXTENDED_OPERAND_TYPE(Token);
  415. if (pOperand->m_ExtendedOperandType == D3D10_SB_EXTENDED_OPERAND_MODIFIER)
  416. {
  417. pOperand->m_Modifier = DECODE_D3D10_SB_OPERAND_MODIFIER(Token);
  418. pOperand->m_MinPrecision = DECODE_D3D11_SB_OPERAND_MIN_PRECISION(Token);
  419. pOperand->m_Nonuniform = DECODE_D3D12_SB_OPERAND_NON_UNIFORM(Token);
  420. }
  421. }
  422. switch( pOperand->m_Type )
  423. {
  424. case D3D10_SB_OPERAND_TYPE_IMMEDIATE32:
  425. case D3D10_SB_OPERAND_TYPE_IMMEDIATE64:
  426. for (UINT i=0 ; i < NumComponents; i++)
  427. {
  428. pOperand->m_Value[i] = *m_pCurrentToken++;
  429. }
  430. break;
  431. }
  432. // Operand indices
  433. if (pOperand->m_IndexDimension != D3D10_SB_OPERAND_INDEX_0D)
  434. {
  435. const UINT NumDimensions = pOperand->m_IndexDimension;
  436. // Index representation
  437. for (UINT i=0; i < NumDimensions; i++)
  438. {
  439. ParseIndex(&pOperand->m_Index[i], pOperand->m_IndexType[i]);
  440. }
  441. }
  442. }
  443. void CShaderCodeParser::ParseInstruction(CInstruction* pInstruction)
  444. {
  445. pInstruction->Clear(true);
  446. CShaderToken* pStart = m_pCurrentToken;
  447. CShaderToken Token = *m_pCurrentToken++;
  448. pInstruction->m_OpCode = DECODE_D3D10_SB_OPCODE_TYPE(Token);
  449. pInstruction->m_PreciseMask = DECODE_D3D11_SB_INSTRUCTION_PRECISE_VALUES(Token);
  450. pInstruction->m_bSaturate = DECODE_IS_D3D10_SB_INSTRUCTION_SATURATE_ENABLED(Token);
  451. UINT InstructionLength = DECODE_D3D10_SB_TOKENIZED_INSTRUCTION_LENGTH(Token);
  452. pInstruction->m_NumOperands = GetNumInstructionOperands(pInstruction->m_OpCode);
  453. BOOL b51PlusShader = (ShaderMajorVersion() > 5 || (ShaderMajorVersion() == 5 && ShaderMinorVersion() > 0));
  454. BOOL bExtended = DECODE_IS_D3D10_SB_OPCODE_EXTENDED(Token);
  455. if( bExtended && (
  456. (pInstruction->m_OpCode == D3D11_SB_OPCODE_DCL_INTERFACE)||
  457. (pInstruction->m_OpCode == D3D11_SB_OPCODE_DCL_FUNCTION_TABLE)))
  458. {
  459. pInstruction->m_ExtendedOpCodeCount = 1;
  460. #pragma prefast (suppress : __WARNING_LOCALDECLHIDESLOCAL, "This uses the same variable name for continuity.")
  461. CShaderToken Token = *m_pCurrentToken++;
  462. // these instructions may be longer than can fit in the normal instructionlength field
  463. InstructionLength = (UINT)(Token);
  464. }
  465. else
  466. {
  467. pInstruction->m_ExtendedOpCodeCount = 0;
  468. for(int i = 0; i < (bExtended ? D3D11_SB_MAX_SIMULTANEOUS_EXTENDED_OPCODES : 0); i++)
  469. {
  470. pInstruction->m_ExtendedOpCodeCount++;
  471. #pragma prefast (suppress : __WARNING_LOCALDECLHIDESLOCAL, "This uses the same variable name for continuity.")
  472. CShaderToken Token = *m_pCurrentToken++;
  473. bExtended = DECODE_IS_D3D10_SB_OPCODE_EXTENDED(Token);
  474. pInstruction->m_OpCodeEx[i] = DECODE_D3D10_SB_EXTENDED_OPCODE_TYPE(Token);
  475. switch(pInstruction->m_OpCodeEx[i])
  476. {
  477. case D3D10_SB_EXTENDED_OPCODE_SAMPLE_CONTROLS:
  478. {
  479. pInstruction->m_TexelOffset[0] = (INT8)DECODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_U, Token);
  480. pInstruction->m_TexelOffset[1] = (INT8)DECODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_V, Token);
  481. pInstruction->m_TexelOffset[2] = (INT8)DECODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_W, Token);
  482. for(UINT i = 0;i < 3;i++)
  483. {
  484. if(pInstruction->m_TexelOffset[i] & 0x8)
  485. pInstruction->m_TexelOffset[i] |= 0xfffffff0;
  486. }
  487. break;
  488. }
  489. break;
  490. case D3D11_SB_EXTENDED_OPCODE_RESOURCE_DIM:
  491. {
  492. pInstruction->m_ResourceDimEx = DECODE_D3D11_SB_EXTENDED_RESOURCE_DIMENSION(Token);
  493. pInstruction->m_ResourceDimStructureStrideEx = DECODE_D3D11_SB_EXTENDED_RESOURCE_DIMENSION_STRUCTURE_STRIDE(Token);
  494. }
  495. break;
  496. case D3D11_SB_EXTENDED_OPCODE_RESOURCE_RETURN_TYPE:
  497. {
  498. for(UINT j = 0; j < 4; j++)
  499. {
  500. pInstruction->m_ResourceReturnTypeEx[j] = DECODE_D3D11_SB_EXTENDED_RESOURCE_RETURN_TYPE(Token,j);
  501. }
  502. }
  503. break;
  504. }
  505. if( !bExtended )
  506. {
  507. break;
  508. }
  509. }
  510. }
  511. switch (pInstruction->m_OpCode)
  512. {
  513. case D3D10_SB_OPCODE_CUSTOMDATA:
  514. pInstruction->m_PreciseMask = 0;
  515. pInstruction->m_bSaturate = false;
  516. pInstruction->m_NumOperands = 0;
  517. // not bothering to keep custom-data for now. TODO: store
  518. pInstruction->m_CustomData.Type = DECODE_D3D10_SB_CUSTOMDATA_CLASS(Token);
  519. InstructionLength = *m_pCurrentToken;
  520. if (*m_pCurrentToken <2)
  521. {
  522. InstructionLength = 2;
  523. pInstruction->m_CustomData.pData = 0;
  524. pInstruction->m_CustomData.DataSizeInBytes = 0;
  525. }
  526. else
  527. {
  528. pInstruction->m_CustomData.DataSizeInBytes = (*m_pCurrentToken-2)*4;
  529. pInstruction->m_CustomData.pData = malloc((*m_pCurrentToken - 2)*sizeof(UINT));
  530. if( NULL == pInstruction->m_CustomData.pData )
  531. {
  532. throw E_OUTOFMEMORY;
  533. }
  534. memcpy(pInstruction->m_CustomData.pData, m_pCurrentToken+1, (*m_pCurrentToken - 2)*4);
  535. switch(pInstruction->m_CustomData.Type)
  536. {
  537. case D3D11_SB_CUSTOMDATA_SHADER_MESSAGE:
  538. {
  539. CShaderMessage* pMessage = &pInstruction->m_CustomData.ShaderMessage;
  540. UINT Length = pInstruction->m_CustomData.DataSizeInBytes / 4;
  541. UINT* pData = (UINT*)pInstruction->m_CustomData.pData;
  542. ZeroMemory(pMessage, sizeof(*pMessage));
  543. if (Length < 6)
  544. {
  545. break;
  546. }
  547. UINT StrChars = pData[2];
  548. // Add one for the terminator and then round up.
  549. UINT StrWords = (StrChars + sizeof(DWORD)) / sizeof(DWORD);
  550. UINT NumOperands = pData[3];
  551. UINT OpLength = pData[4];
  552. // Enforce some basic sanity size limits.
  553. if (OpLength >= 0x10000 ||
  554. NumOperands >= 0x1000 ||
  555. StrWords >= 0x10000 ||
  556. Length < 5 + OpLength + StrWords)
  557. {
  558. break;
  559. }
  560. UINT* pOpEnd = &pData[5 + OpLength];
  561. pMessage->pOperands = (COperand*)malloc(NumOperands * sizeof(COperand));
  562. if (!pMessage->pOperands)
  563. {
  564. throw E_OUTOFMEMORY;
  565. }
  566. CONST CShaderToken* pOperands = (CShaderToken*)&pData[5];
  567. for (UINT i = 0; i < NumOperands; i++)
  568. {
  569. if (pOperands >= pOpEnd)
  570. {
  571. break;
  572. }
  573. pMessage->pOperands[i].Clear();
  574. pOperands = ParseOperandAt(&pMessage->pOperands[i],
  575. pOperands,
  576. pOpEnd);
  577. }
  578. if (pOperands != pOpEnd)
  579. {
  580. free(pMessage->pOperands);
  581. pMessage->pOperands = NULL;
  582. break;
  583. }
  584. // Now that we're sure everything is valid we can
  585. // fill in the message info.
  586. pMessage->MessageID = (D3D11_SB_SHADER_MESSAGE_ID)pData[0];
  587. pMessage->FormatStyle = (D3D11_SB_SHADER_MESSAGE_FORMAT)pData[1];
  588. pMessage->pFormatString = (PCSTR)pOpEnd;
  589. pMessage->NumOperands = NumOperands;
  590. break;
  591. }
  592. case D3D10_SB_CUSTOMDATA_COMMENT:
  593. {
  594. // Guarantee that the C string comment is Null-terminated
  595. *((LPSTR)pInstruction->m_CustomData.pData + pInstruction->m_CustomData.DataSizeInBytes - 1) = '\0';
  596. break;
  597. }
  598. }
  599. }
  600. break;
  601. case D3D11_SB_OPCODE_DCL_FUNCTION_BODY:
  602. pInstruction->m_FunctionBodyDecl.FunctionBodyNumber = (UINT)(*m_pCurrentToken);
  603. m_pCurrentToken++;
  604. break;
  605. case D3D11_SB_OPCODE_DCL_FUNCTION_TABLE:
  606. pInstruction->m_FunctionTableDecl.FunctionTableNumber = (UINT)(*m_pCurrentToken);
  607. m_pCurrentToken++;
  608. pInstruction->m_FunctionTableDecl.TableLength = (UINT)(*m_pCurrentToken);
  609. // opcode
  610. // instruction length if extended instruction
  611. // table ID
  612. // table length
  613. // data
  614. assert(InstructionLength == (3 + (bExtended?1:0) + pInstruction->m_FunctionTableDecl.TableLength));
  615. pInstruction->m_FunctionTableDecl.pFunctionIdentifiers =
  616. (UINT*) malloc(pInstruction->m_FunctionTableDecl.TableLength*sizeof(UINT));
  617. if( NULL == pInstruction->m_FunctionTableDecl.pFunctionIdentifiers )
  618. {
  619. throw E_OUTOFMEMORY;
  620. }
  621. m_pCurrentToken++;
  622. memcpy(pInstruction->m_FunctionTableDecl.pFunctionIdentifiers, m_pCurrentToken,
  623. pInstruction->m_FunctionTableDecl.TableLength*sizeof(UINT));
  624. break;
  625. case D3D11_SB_OPCODE_DCL_INTERFACE:
  626. pInstruction->m_InterfaceDecl.bDynamicallyIndexed = DECODE_D3D11_SB_INTERFACE_INDEXED_BIT(Token);
  627. pInstruction->m_InterfaceDecl.InterfaceNumber = (WORD)(*m_pCurrentToken);
  628. m_pCurrentToken++;
  629. pInstruction->m_InterfaceDecl.ExpectedTableSize = (UINT)(*m_pCurrentToken);
  630. m_pCurrentToken++;
  631. // there's a limit of 64k types, so that gives a max length on this table.
  632. pInstruction->m_InterfaceDecl.TableLength = DECODE_D3D11_SB_INTERFACE_TABLE_LENGTH(*m_pCurrentToken);
  633. // this puts a limit on the size of interface arrays at 64k
  634. pInstruction->m_InterfaceDecl.ArrayLength = DECODE_D3D11_SB_INTERFACE_ARRAY_LENGTH(*m_pCurrentToken);
  635. // opcode
  636. // instruction length if extended instruction
  637. // interface ID
  638. // table size
  639. // num types/array length
  640. // data
  641. assert(InstructionLength == (4 + (bExtended?1:0) + pInstruction->m_InterfaceDecl.TableLength));
  642. pInstruction->m_InterfaceDecl.pFunctionTableIdentifiers =
  643. (UINT*) malloc(pInstruction->m_InterfaceDecl.TableLength*sizeof(UINT));
  644. if( NULL == pInstruction->m_InterfaceDecl.pFunctionTableIdentifiers )
  645. {
  646. throw E_OUTOFMEMORY;
  647. }
  648. m_pCurrentToken++;
  649. memcpy(pInstruction->m_InterfaceDecl.pFunctionTableIdentifiers, m_pCurrentToken,
  650. pInstruction->m_InterfaceDecl.TableLength*sizeof(UINT));
  651. break;
  652. case D3D11_SB_OPCODE_INTERFACE_CALL:
  653. pInstruction->m_InterfaceCall.FunctionIndex = *m_pCurrentToken++;
  654. pInstruction->m_InterfaceCall.pInterfaceOperand =
  655. pInstruction->m_Operands;
  656. ParseOperand(pInstruction->m_InterfaceCall.pInterfaceOperand);
  657. break;
  658. case D3D10_SB_OPCODE_DCL_RESOURCE:
  659. pInstruction->m_ResourceDecl.Dimension = DECODE_D3D10_SB_RESOURCE_DIMENSION(Token);
  660. ParseOperand(&pInstruction->m_Operands[0]);
  661. pInstruction->m_ResourceDecl.ReturnType[0] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 0);
  662. pInstruction->m_ResourceDecl.ReturnType[1] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 1);
  663. pInstruction->m_ResourceDecl.ReturnType[2] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 2);
  664. pInstruction->m_ResourceDecl.ReturnType[3] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 3);
  665. pInstruction->m_ResourceDecl.SampleCount = DECODE_D3D10_SB_RESOURCE_SAMPLE_COUNT(Token);
  666. m_pCurrentToken++;
  667. pInstruction->m_ResourceDecl.Space = 0;
  668. if(b51PlusShader)
  669. {
  670. pInstruction->m_ResourceDecl.Space = (UINT)(*m_pCurrentToken++);
  671. }
  672. break;
  673. case D3D10_SB_OPCODE_DCL_SAMPLER:
  674. pInstruction->m_SamplerDecl.SamplerMode = DECODE_D3D10_SB_SAMPLER_MODE(Token);
  675. ParseOperand(&pInstruction->m_Operands[0]);
  676. pInstruction->m_SamplerDecl.Space = 0;
  677. if(b51PlusShader)
  678. {
  679. pInstruction->m_SamplerDecl.Space = (UINT)(*m_pCurrentToken++);
  680. }
  681. break;
  682. case D3D11_SB_OPCODE_DCL_STREAM:
  683. ParseOperand(&pInstruction->m_Operands[0]);
  684. break;
  685. case D3D10_SB_OPCODE_DCL_TEMPS:
  686. pInstruction->m_TempsDecl.NumTemps = (UINT)(*m_pCurrentToken);
  687. m_pCurrentToken++;
  688. break;
  689. case D3D10_SB_OPCODE_DCL_INDEXABLE_TEMP:
  690. pInstruction->m_IndexableTempDecl.IndexableTempNumber = (UINT)(*m_pCurrentToken);
  691. m_pCurrentToken++;
  692. pInstruction->m_IndexableTempDecl.NumRegisters = (UINT)(*m_pCurrentToken);
  693. m_pCurrentToken++;
  694. switch( min( 4u, max( 1u, (UINT)(*m_pCurrentToken) ) ) )
  695. {
  696. case 1:
  697. pInstruction->m_IndexableTempDecl.Mask = D3D10_SB_OPERAND_4_COMPONENT_MASK_X;
  698. break;
  699. case 2:
  700. pInstruction->m_IndexableTempDecl.Mask = D3D10_SB_OPERAND_4_COMPONENT_MASK_X |
  701. D3D10_SB_OPERAND_4_COMPONENT_MASK_Y;
  702. break;
  703. case 3:
  704. pInstruction->m_IndexableTempDecl.Mask = D3D10_SB_OPERAND_4_COMPONENT_MASK_X |
  705. D3D10_SB_OPERAND_4_COMPONENT_MASK_Y |
  706. D3D10_SB_OPERAND_4_COMPONENT_MASK_Z;
  707. break;
  708. case 4:
  709. pInstruction->m_IndexableTempDecl.Mask = D3D10_SB_OPERAND_4_COMPONENT_MASK_ALL;
  710. break;
  711. }
  712. m_pCurrentToken++;
  713. break;
  714. case D3D10_SB_OPCODE_DCL_INPUT:
  715. case D3D10_SB_OPCODE_DCL_OUTPUT:
  716. ParseOperand(&pInstruction->m_Operands[0]);
  717. break;
  718. case D3D10_SB_OPCODE_DCL_INPUT_SIV:
  719. ParseOperand(&pInstruction->m_Operands[0]);
  720. pInstruction->m_InputDeclSIV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  721. m_pCurrentToken++;
  722. break;
  723. case D3D10_SB_OPCODE_DCL_INPUT_SGV:
  724. ParseOperand(&pInstruction->m_Operands[0]);
  725. pInstruction->m_InputDeclSIV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  726. m_pCurrentToken++;
  727. break;
  728. case D3D10_SB_OPCODE_DCL_INPUT_PS:
  729. pInstruction->m_InputPSDecl.InterpolationMode = DECODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Token);
  730. ParseOperand(&pInstruction->m_Operands[0]);
  731. break;
  732. case D3D10_SB_OPCODE_DCL_INPUT_PS_SIV:
  733. pInstruction->m_InputPSDeclSIV.InterpolationMode = DECODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Token);
  734. ParseOperand(&pInstruction->m_Operands[0]);
  735. pInstruction->m_InputPSDeclSIV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  736. m_pCurrentToken++;
  737. break;
  738. case D3D10_SB_OPCODE_DCL_INPUT_PS_SGV:
  739. pInstruction->m_InputPSDeclSGV.InterpolationMode = DECODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Token);
  740. ParseOperand(&pInstruction->m_Operands[0]);
  741. pInstruction->m_InputPSDeclSGV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  742. m_pCurrentToken++;
  743. break;
  744. case D3D10_SB_OPCODE_DCL_OUTPUT_SIV:
  745. ParseOperand(&pInstruction->m_Operands[0]);
  746. pInstruction->m_OutputDeclSIV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  747. m_pCurrentToken++;
  748. break;
  749. case D3D10_SB_OPCODE_DCL_OUTPUT_SGV:
  750. ParseOperand(&pInstruction->m_Operands[0]);
  751. pInstruction->m_OutputDeclSGV.Name = DECODE_D3D10_SB_NAME(*m_pCurrentToken);
  752. m_pCurrentToken++;
  753. break;
  754. case D3D10_SB_OPCODE_DCL_INDEX_RANGE:
  755. ParseOperand(&pInstruction->m_Operands[0]);
  756. pInstruction->m_IndexRangeDecl.RegCount = (UINT)(*m_pCurrentToken);
  757. m_pCurrentToken++;
  758. break;
  759. case D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER:
  760. pInstruction->m_ConstantBufferDecl.AccessPattern = DECODE_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(Token);
  761. ParseOperand(&pInstruction->m_Operands[0]);
  762. pInstruction->m_ConstantBufferDecl.Space = 0;
  763. if(b51PlusShader)
  764. {
  765. pInstruction->m_ConstantBufferDecl.Size = (UINT)(*m_pCurrentToken++);
  766. pInstruction->m_ConstantBufferDecl.Space = (UINT)(*m_pCurrentToken++);
  767. }
  768. else
  769. {
  770. pInstruction->m_ConstantBufferDecl.Size = pInstruction->m_Operands[0].m_Index[1].m_RegIndex;
  771. }
  772. break;
  773. case D3D10_SB_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY:
  774. pInstruction->m_OutputTopologyDecl.Topology = DECODE_D3D10_SB_GS_OUTPUT_PRIMITIVE_TOPOLOGY(Token);
  775. break;
  776. case D3D10_SB_OPCODE_DCL_GS_INPUT_PRIMITIVE:
  777. pInstruction->m_InputPrimitiveDecl.Primitive = DECODE_D3D10_SB_GS_INPUT_PRIMITIVE(Token);
  778. break;
  779. case D3D10_SB_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT:
  780. pInstruction->m_GSMaxOutputVertexCountDecl.MaxOutputVertexCount = (UINT)(*m_pCurrentToken);
  781. m_pCurrentToken++;
  782. break;
  783. case D3D11_SB_OPCODE_DCL_GS_INSTANCE_COUNT:
  784. pInstruction->m_GSInstanceCountDecl.InstanceCount = (UINT)(*m_pCurrentToken);
  785. m_pCurrentToken++;
  786. break;
  787. case D3D10_SB_OPCODE_DCL_GLOBAL_FLAGS:
  788. pInstruction->m_GlobalFlagsDecl.Flags = DECODE_D3D10_SB_GLOBAL_FLAGS(Token);
  789. break;
  790. case D3D11_SB_OPCODE_DCL_INPUT_CONTROL_POINT_COUNT:
  791. pInstruction->m_InputControlPointCountDecl.InputControlPointCount = DECODE_D3D11_SB_INPUT_CONTROL_POINT_COUNT(Token);
  792. break;
  793. case D3D11_SB_OPCODE_DCL_OUTPUT_CONTROL_POINT_COUNT:
  794. pInstruction->m_OutputControlPointCountDecl.OutputControlPointCount = DECODE_D3D11_SB_OUTPUT_CONTROL_POINT_COUNT(Token);
  795. break;
  796. case D3D11_SB_OPCODE_DCL_TESS_DOMAIN:
  797. pInstruction->m_TessellatorDomainDecl.TessellatorDomain = DECODE_D3D11_SB_TESS_DOMAIN(Token);
  798. break;
  799. case D3D11_SB_OPCODE_DCL_TESS_PARTITIONING:
  800. pInstruction->m_TessellatorPartitioningDecl.TessellatorPartitioning = DECODE_D3D11_SB_TESS_PARTITIONING(Token);
  801. break;
  802. case D3D11_SB_OPCODE_DCL_TESS_OUTPUT_PRIMITIVE:
  803. pInstruction->m_TessellatorOutputPrimitiveDecl.TessellatorOutputPrimitive = DECODE_D3D11_SB_TESS_OUTPUT_PRIMITIVE(Token);
  804. break;
  805. case D3D11_SB_OPCODE_DCL_HS_MAX_TESSFACTOR:
  806. pInstruction->m_HSMaxTessFactorDecl.MaxTessFactor = *(float*)m_pCurrentToken;
  807. m_pCurrentToken++;
  808. break;
  809. case D3D11_SB_OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT:
  810. pInstruction->m_HSForkPhaseInstanceCountDecl.InstanceCount = *(UINT*)m_pCurrentToken;
  811. m_pCurrentToken++;
  812. break;
  813. case D3D11_SB_OPCODE_DCL_HS_JOIN_PHASE_INSTANCE_COUNT:
  814. pInstruction->m_HSJoinPhaseInstanceCountDecl.InstanceCount = *(UINT*)m_pCurrentToken;
  815. m_pCurrentToken++;
  816. break;
  817. case D3D11_SB_OPCODE_DCL_THREAD_GROUP:
  818. pInstruction->m_ThreadGroupDecl.x = *(UINT*)m_pCurrentToken++;
  819. pInstruction->m_ThreadGroupDecl.y = *(UINT*)m_pCurrentToken++;
  820. pInstruction->m_ThreadGroupDecl.z = *(UINT*)m_pCurrentToken++;
  821. break;
  822. case D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_TYPED:
  823. pInstruction->m_TypedUAVDecl.Dimension = DECODE_D3D10_SB_RESOURCE_DIMENSION(Token);
  824. pInstruction->m_TypedUAVDecl.Flags = DECODE_D3D11_SB_RESOURCE_FLAGS(Token);
  825. ParseOperand(&pInstruction->m_Operands[0]);
  826. pInstruction->m_TypedUAVDecl.ReturnType[0] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 0);
  827. pInstruction->m_TypedUAVDecl.ReturnType[1] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 1);
  828. pInstruction->m_TypedUAVDecl.ReturnType[2] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 2);
  829. pInstruction->m_TypedUAVDecl.ReturnType[3] = DECODE_D3D10_SB_RESOURCE_RETURN_TYPE(*m_pCurrentToken, 3);
  830. m_pCurrentToken++;
  831. pInstruction->m_TypedUAVDecl.Space = 0;
  832. if(b51PlusShader)
  833. {
  834. pInstruction->m_TypedUAVDecl.Space = (UINT)(*m_pCurrentToken++);
  835. }
  836. break;
  837. case D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_RAW:
  838. pInstruction->m_RawUAVDecl.Flags = DECODE_D3D11_SB_RESOURCE_FLAGS(Token);
  839. ParseOperand(&pInstruction->m_Operands[0]);
  840. pInstruction->m_RawUAVDecl.Space = 0;
  841. if(b51PlusShader)
  842. {
  843. pInstruction->m_RawUAVDecl.Space = (UINT)(*m_pCurrentToken++);
  844. }
  845. break;
  846. case D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED:
  847. pInstruction->m_StructuredUAVDecl.Flags = DECODE_D3D11_SB_RESOURCE_FLAGS(Token);
  848. ParseOperand(&pInstruction->m_Operands[0]);
  849. pInstruction->m_StructuredUAVDecl.ByteStride = *(UINT*)m_pCurrentToken++;
  850. pInstruction->m_StructuredUAVDecl.Space = 0;
  851. if(b51PlusShader)
  852. {
  853. pInstruction->m_StructuredUAVDecl.Space = (UINT)(*m_pCurrentToken++);
  854. }
  855. break;
  856. case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_RAW:
  857. ParseOperand(&pInstruction->m_Operands[0]);
  858. pInstruction->m_RawTGSMDecl.ByteCount = *(UINT*)m_pCurrentToken++;
  859. break;
  860. case D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED:
  861. ParseOperand(&pInstruction->m_Operands[0]);
  862. pInstruction->m_StructuredTGSMDecl.StructByteStride = *(UINT*)m_pCurrentToken++;
  863. pInstruction->m_StructuredTGSMDecl.StructCount = *(UINT*)m_pCurrentToken++;
  864. break;
  865. case D3D11_SB_OPCODE_DCL_RESOURCE_RAW:
  866. ParseOperand(&pInstruction->m_Operands[0]);
  867. pInstruction->m_RawSRVDecl.Space = 0;
  868. if(b51PlusShader)
  869. {
  870. pInstruction->m_RawSRVDecl.Space = (UINT)(*m_pCurrentToken++);
  871. }
  872. break;
  873. case D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED:
  874. ParseOperand(&pInstruction->m_Operands[0]);
  875. pInstruction->m_StructuredSRVDecl.ByteStride = *(UINT*)m_pCurrentToken++;
  876. pInstruction->m_StructuredSRVDecl.Space = 0;
  877. if(b51PlusShader)
  878. {
  879. pInstruction->m_StructuredSRVDecl.Space = (UINT)(*m_pCurrentToken++);
  880. }
  881. break;
  882. case D3D11_SB_OPCODE_SYNC:
  883. {
  884. DWORD flags = DECODE_D3D11_SB_SYNC_FLAGS(Token);
  885. pInstruction->m_SyncFlags.bThreadsInGroup = (flags & D3D11_SB_SYNC_THREADS_IN_GROUP) ? true : false;
  886. pInstruction->m_SyncFlags.bThreadGroupSharedMemory = (flags & D3D11_SB_SYNC_THREAD_GROUP_SHARED_MEMORY) ? true : false;
  887. pInstruction->m_SyncFlags.bUnorderedAccessViewMemoryGroup = (flags & D3D11_SB_SYNC_UNORDERED_ACCESS_VIEW_MEMORY_GROUP ) ? true : false;
  888. pInstruction->m_SyncFlags.bUnorderedAccessViewMemoryGlobal = (flags & D3D11_SB_SYNC_UNORDERED_ACCESS_VIEW_MEMORY_GLOBAL ) ? true : false;
  889. }
  890. break;
  891. case D3D10_SB_OPCODE_RESINFO:
  892. pInstruction->m_ResInfoReturnType = DECODE_D3D10_SB_RESINFO_INSTRUCTION_RETURN_TYPE(Token);
  893. ParseOperand(&pInstruction->m_Operands[0]);
  894. ParseOperand(&pInstruction->m_Operands[1]);
  895. ParseOperand(&pInstruction->m_Operands[2]);
  896. break;
  897. case D3D10_1_SB_OPCODE_SAMPLE_INFO:
  898. pInstruction->m_InstructionReturnType = DECODE_D3D10_SB_INSTRUCTION_RETURN_TYPE(Token);
  899. ParseOperand(&pInstruction->m_Operands[0]);
  900. ParseOperand(&pInstruction->m_Operands[1]);
  901. break;
  902. case D3D10_SB_OPCODE_IF:
  903. case D3D10_SB_OPCODE_BREAKC:
  904. case D3D10_SB_OPCODE_CONTINUEC:
  905. case D3D10_SB_OPCODE_RETC:
  906. case D3D10_SB_OPCODE_DISCARD:
  907. pInstruction->SetTest(DECODE_D3D10_SB_INSTRUCTION_TEST_BOOLEAN(Token));
  908. ParseOperand(&pInstruction->m_Operands[0]);
  909. break;
  910. case D3D10_SB_OPCODE_CALLC:
  911. pInstruction->SetTest(DECODE_D3D10_SB_INSTRUCTION_TEST_BOOLEAN(Token));
  912. ParseOperand(&pInstruction->m_Operands[0]);
  913. ParseOperand(&pInstruction->m_Operands[1]);
  914. break;
  915. default:
  916. {
  917. for (UINT i=0; i < pInstruction->m_NumOperands; i++)
  918. {
  919. ParseOperand(&pInstruction->m_Operands[i]);
  920. }
  921. break;
  922. }
  923. }
  924. m_pCurrentToken = pStart + InstructionLength;
  925. }
  926. // ****************************************************************************
  927. //
  928. // class CShaderAsm
  929. //
  930. // ****************************************************************************
  931. void CShaderAsm::EmitOperand(const COperandBase& operand)
  932. {
  933. CShaderToken Token = ENCODE_D3D10_SB_OPERAND_TYPE(operand.m_Type) |
  934. ENCODE_D3D10_SB_OPERAND_NUM_COMPONENTS(operand.m_NumComponents) |
  935. ENCODE_D3D10_SB_OPERAND_EXTENDED(operand.m_bExtendedOperand);
  936. BOOL bProcessOperandIndices = FALSE;
  937. if (!(operand.m_Type == D3D10_SB_OPERAND_TYPE_IMMEDIATE32 ||
  938. operand.m_Type == D3D10_SB_OPERAND_TYPE_IMMEDIATE64))
  939. {
  940. Token |= ENCODE_D3D10_SB_OPERAND_INDEX_DIMENSION(operand.m_IndexDimension);
  941. if (operand.m_NumComponents == D3D10_SB_OPERAND_4_COMPONENT)
  942. {
  943. // Component selection mode
  944. Token |= ENCODE_D3D10_SB_OPERAND_4_COMPONENT_SELECTION_MODE(operand.m_ComponentSelection);
  945. switch(operand.m_ComponentSelection)
  946. {
  947. case D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE:
  948. Token |= ENCODE_D3D10_SB_OPERAND_4_COMPONENT_MASK(operand.m_WriteMask );
  949. break;
  950. case D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_MODE:
  951. Token |= ENCODE_D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE(operand.m_Swizzle[0],
  952. operand.m_Swizzle[1],
  953. operand.m_Swizzle[2],
  954. operand.m_Swizzle[3]);
  955. break;
  956. case D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE:
  957. {
  958. Token |= ENCODE_D3D10_SB_OPERAND_4_COMPONENT_SELECT_1(operand.m_ComponentName);
  959. break;
  960. }
  961. default:
  962. throw E_FAIL;
  963. }
  964. }
  965. UINT NumDimensions = operand.m_IndexDimension;
  966. if (NumDimensions > 0)
  967. {
  968. bProcessOperandIndices = TRUE;
  969. // Encode index representation
  970. for (UINT i=0; i < NumDimensions; i++)
  971. {
  972. Token |= ENCODE_D3D10_SB_OPERAND_INDEX_REPRESENTATION(i, operand.m_IndexType[i]);
  973. }
  974. }
  975. FUNC(Token);
  976. }
  977. // Extended operand
  978. if (operand.m_bExtendedOperand)
  979. {
  980. Token = ENCODE_D3D10_SB_EXTENDED_OPERAND_TYPE(operand.m_ExtendedOperandType);
  981. if (operand.m_ExtendedOperandType == D3D10_SB_EXTENDED_OPERAND_MODIFIER)
  982. {
  983. Token |= ENCODE_D3D10_SB_EXTENDED_OPERAND_MODIFIER(operand.m_Modifier);
  984. Token |= ENCODE_D3D11_SB_OPERAND_MIN_PRECISION(operand.m_MinPrecision);
  985. Token |= ENCODE_D3D12_SB_OPERAND_NON_UNIFORM(operand.m_Nonuniform);
  986. }
  987. FUNC(Token);
  988. }
  989. if( operand.m_Type == D3D10_SB_OPERAND_TYPE_IMMEDIATE32 ||
  990. operand.m_Type == D3D10_SB_OPERAND_TYPE_IMMEDIATE64)
  991. {
  992. FUNC(Token);
  993. UINT n = 0;
  994. if (operand.m_NumComponents == D3D10_SB_OPERAND_4_COMPONENT)
  995. n = 4;
  996. else
  997. if (operand.m_NumComponents == D3D10_SB_OPERAND_1_COMPONENT)
  998. n = 1;
  999. else
  1000. {
  1001. throw E_FAIL;
  1002. }
  1003. for (UINT i=0 ; i < n; i++)
  1004. {
  1005. FUNC(operand.m_Value[i]);
  1006. }
  1007. }
  1008. // Operand indices
  1009. if (bProcessOperandIndices)
  1010. {
  1011. const UINT NumDimensions = operand.m_IndexDimension;
  1012. // Encode index representation
  1013. for (UINT i=0; i < NumDimensions; i++)
  1014. {
  1015. switch (operand.m_IndexType[i])
  1016. {
  1017. case D3D10_SB_OPERAND_INDEX_IMMEDIATE32:
  1018. FUNC(operand.m_Index[i].m_RegIndex);
  1019. break;
  1020. case D3D10_SB_OPERAND_INDEX_IMMEDIATE64:
  1021. FUNC(operand.m_Index[i].m_RegIndexA[0]);
  1022. FUNC(operand.m_Index[i].m_RegIndexA[1]);
  1023. break;
  1024. case D3D10_SB_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE:
  1025. FUNC(operand.m_Index[i].m_RegIndex);
  1026. // Fall through
  1027. case D3D10_SB_OPERAND_INDEX_RELATIVE:
  1028. {
  1029. D3D10_SB_OPERAND_TYPE RelRegType = operand.m_Index[i].m_RelRegType;
  1030. if( operand.m_Index[i].m_IndexDimension == D3D10_SB_OPERAND_INDEX_2D )
  1031. {
  1032. EmitOperand(COperand2D(RelRegType,
  1033. operand.m_Index[i].m_RelIndex,
  1034. operand.m_Index[i].m_RelIndex1,
  1035. operand.m_Index[i].m_ComponentName,
  1036. operand.m_Index[i].m_MinPrecision));
  1037. }
  1038. else
  1039. {
  1040. EmitOperand(COperand4(RelRegType,
  1041. operand.m_Index[i].m_RelIndex,
  1042. operand.m_Index[i].m_ComponentName,
  1043. operand.m_Index[i].m_MinPrecision));
  1044. }
  1045. }
  1046. break;
  1047. default:
  1048. throw E_FAIL;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. //-----------------------------------------------------------------------------
  1054. void CShaderAsm::EmitInstruction(const CInstruction& instruction)
  1055. {
  1056. UINT OpCode;
  1057. if(instruction.m_OpCode == D3D10_SB_OPCODE_CUSTOMDATA)
  1058. {
  1059. OPCODE(D3D10_SB_OPCODE_CUSTOMDATA);
  1060. FUNC(instruction.m_CustomData.DataSizeInBytes/4 + 2);
  1061. for(UINT i = 0;i < instruction.m_CustomData.DataSizeInBytes/4; i++)
  1062. FUNC(((UINT*)instruction.m_CustomData.pData)[i]);
  1063. ENDINSTRUCTION();
  1064. return;
  1065. }
  1066. OpCode = ENCODE_D3D10_SB_OPCODE_TYPE(instruction.m_OpCode) | ENCODE_D3D10_SB_OPCODE_EXTENDED(instruction.m_ExtendedOpCodeCount > 0 ? true : false);
  1067. switch (instruction.m_OpCode)
  1068. {
  1069. case D3D10_SB_OPCODE_IF:
  1070. case D3D10_SB_OPCODE_BREAKC:
  1071. case D3D10_SB_OPCODE_CALLC:
  1072. case D3D10_SB_OPCODE_CONTINUEC:
  1073. case D3D10_SB_OPCODE_RETC:
  1074. case D3D10_SB_OPCODE_DISCARD:
  1075. OpCode |= ENCODE_D3D10_SB_INSTRUCTION_TEST_BOOLEAN(instruction.Test());
  1076. break;
  1077. case D3D10_SB_OPCODE_RESINFO:
  1078. OpCode |= ENCODE_D3D10_SB_RESINFO_INSTRUCTION_RETURN_TYPE(instruction.m_ResInfoReturnType);
  1079. break;
  1080. case D3D10_1_SB_OPCODE_SAMPLE_INFO:
  1081. OpCode |= ENCODE_D3D10_SB_INSTRUCTION_RETURN_TYPE(instruction.m_InstructionReturnType);
  1082. break;
  1083. case D3D11_SB_OPCODE_SYNC:
  1084. OpCode |= ENCODE_D3D11_SB_SYNC_FLAGS(
  1085. ( instruction.m_SyncFlags.bThreadsInGroup ? D3D11_SB_SYNC_THREADS_IN_GROUP : 0 ) |
  1086. ( instruction.m_SyncFlags.bThreadGroupSharedMemory ? D3D11_SB_SYNC_THREAD_GROUP_SHARED_MEMORY : 0 ) |
  1087. ( instruction.m_SyncFlags.bUnorderedAccessViewMemoryGlobal ? D3D11_SB_SYNC_UNORDERED_ACCESS_VIEW_MEMORY_GLOBAL : 0 ) |
  1088. ( instruction.m_SyncFlags.bUnorderedAccessViewMemoryGroup ? D3D11_SB_SYNC_UNORDERED_ACCESS_VIEW_MEMORY_GROUP : 0 ) );
  1089. break;
  1090. };
  1091. OpCode |= ENCODE_D3D10_SB_INSTRUCTION_SATURATE(instruction.m_bSaturate);
  1092. OpCode |= ENCODE_D3D11_SB_INSTRUCTION_PRECISE_VALUES(instruction.m_PreciseMask);
  1093. OPCODE(OpCode);
  1094. for(UINT i = 0; i < min(instruction.m_ExtendedOpCodeCount,(UINT)D3D11_SB_MAX_SIMULTANEOUS_EXTENDED_OPCODES); i++)
  1095. {
  1096. UINT Extended = ENCODE_D3D10_SB_EXTENDED_OPCODE_TYPE(instruction.m_OpCodeEx[i]);
  1097. switch( instruction.m_OpCodeEx[i] )
  1098. {
  1099. case D3D10_SB_EXTENDED_OPCODE_SAMPLE_CONTROLS:
  1100. {
  1101. Extended |= ENCODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_U, instruction.m_TexelOffset[0]);
  1102. Extended |= ENCODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_V, instruction.m_TexelOffset[1]);
  1103. Extended |= ENCODE_IMMEDIATE_D3D10_SB_ADDRESS_OFFSET(D3D10_SB_IMMEDIATE_ADDRESS_OFFSET_W, instruction.m_TexelOffset[2]);
  1104. }
  1105. break;
  1106. case D3D11_SB_EXTENDED_OPCODE_RESOURCE_DIM:
  1107. {
  1108. Extended |= ENCODE_D3D11_SB_EXTENDED_RESOURCE_DIMENSION(instruction.m_ResourceDimEx) |
  1109. ENCODE_D3D11_SB_EXTENDED_RESOURCE_DIMENSION_STRUCTURE_STRIDE(instruction.m_ResourceDimStructureStrideEx);
  1110. }
  1111. break;
  1112. case D3D11_SB_EXTENDED_OPCODE_RESOURCE_RETURN_TYPE:
  1113. {
  1114. for(UINT j = 0; j < 4; j++)
  1115. {
  1116. Extended |= ENCODE_D3D11_SB_EXTENDED_RESOURCE_RETURN_TYPE(instruction.m_ResourceReturnTypeEx[j],j);
  1117. }
  1118. }
  1119. break;
  1120. }
  1121. Extended |= ENCODE_D3D10_SB_OPCODE_EXTENDED((i + 1 < instruction.m_ExtendedOpCodeCount) ? true : false);
  1122. FUNC(Extended);
  1123. }
  1124. for (UINT i=0; i < instruction.m_NumOperands; i++)
  1125. {
  1126. EmitOperand(instruction.m_Operands[i]);
  1127. }
  1128. ENDINSTRUCTION();
  1129. }
  1130. //*****************************************************************************
  1131. //
  1132. // CInstruction
  1133. //
  1134. //*****************************************************************************
  1135. BOOL CInstruction::Disassemble( __out_ecount(StringSize) LPSTR pString, UINT StringSize)
  1136. {
  1137. StringCchCopyA(pString, StringSize, g_InstructionInfo[m_OpCode].m_Name);
  1138. return TRUE;
  1139. }
  1140. }; // name space D3D10ShaderBinary
  1141. // End of file : ShaderBinary.cpp