shader_dxbc.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bgfx_p.h"
  6. #include "shader_dxbc.h"
  7. namespace bgfx
  8. {
  9. struct DxbcOpcodeInfo
  10. {
  11. uint8_t numOperands;
  12. uint8_t numValues;
  13. };
  14. static const DxbcOpcodeInfo s_dxbcOpcodeInfo[] =
  15. {
  16. { 3, 0 }, // ADD
  17. { 3, 0 }, // AND
  18. { 0, 0 }, // BREAK
  19. { 1, 0 }, // BREAKC
  20. { 0, 0 }, // CALL
  21. { 0, 0 }, // CALLC
  22. { 1, 0 }, // CASE
  23. { 0, 0 }, // CONTINUE
  24. { 1, 0 }, // CONTINUEC
  25. { 0, 0 }, // CUT
  26. { 0, 0 }, // DEFAULT
  27. { 2, 0 }, // DERIV_RTX
  28. { 2, 0 }, // DERIV_RTY
  29. { 1, 0 }, // DISCARD
  30. { 3, 0 }, // DIV
  31. { 3, 0 }, // DP2
  32. { 3, 0 }, // DP3
  33. { 3, 0 }, // DP4
  34. { 0, 0 }, // ELSE
  35. { 0, 0 }, // EMIT
  36. { 0, 0 }, // EMITTHENCUT
  37. { 0, 0 }, // ENDIF
  38. { 0, 0 }, // ENDLOOP
  39. { 0, 0 }, // ENDSWITCH
  40. { 3, 0 }, // EQ
  41. { 2, 0 }, // EXP
  42. { 2, 0 }, // FRC
  43. { 2, 0 }, // FTOI
  44. { 2, 0 }, // FTOU
  45. { 3, 0 }, // GE
  46. { 3, 0 }, // IADD
  47. { 1, 0 }, // IF
  48. { 3, 0 }, // IEQ
  49. { 3, 0 }, // IGE
  50. { 3, 0 }, // ILT
  51. { 4, 0 }, // IMAD
  52. { 3, 0 }, // IMAX
  53. { 3, 0 }, // IMIN
  54. { 4, 0 }, // IMUL
  55. { 3, 0 }, // INE
  56. { 2, 0 }, // INEG
  57. { 3, 0 }, // ISHL
  58. { 3, 0 }, // ISHR
  59. { 2, 0 }, // ITOF
  60. { 0, 0 }, // LABEL
  61. { 3, 0 }, // LD
  62. { 4, 0 }, // LD_MS
  63. { 2, 0 }, // LOG
  64. { 0, 0 }, // LOOP
  65. { 3, 0 }, // LT
  66. { 4, 0 }, // MAD
  67. { 3, 0 }, // MIN
  68. { 3, 0 }, // MAX
  69. { 0, 1 }, // CUSTOMDATA
  70. { 2, 0 }, // MOV
  71. { 4, 0 }, // MOVC
  72. { 3, 0 }, // MUL
  73. { 3, 0 }, // NE
  74. { 0, 0 }, // NOP
  75. { 2, 0 }, // NOT
  76. { 3, 0 }, // OR
  77. { 3, 0 }, // RESINFO
  78. { 0, 0 }, // RET
  79. { 1, 0 }, // RETC
  80. { 2, 0 }, // ROUND_NE
  81. { 2, 0 }, // ROUND_NI
  82. { 2, 0 }, // ROUND_PI
  83. { 2, 0 }, // ROUND_Z
  84. { 2, 0 }, // RSQ
  85. { 4, 0 }, // SAMPLE
  86. { 5, 0 }, // SAMPLE_C
  87. { 5, 0 }, // SAMPLE_C_LZ
  88. { 5, 0 }, // SAMPLE_L
  89. { 6, 0 }, // SAMPLE_D
  90. { 5, 0 }, // SAMPLE_B
  91. { 2, 0 }, // SQRT
  92. { 1, 0 }, // SWITCH
  93. { 3, 0 }, // SINCOS
  94. { 4, 0 }, // UDIV
  95. { 3, 0 }, // ULT
  96. { 3, 0 }, // UGE
  97. { 4, 0 }, // UMUL
  98. { 4, 0 }, // UMAD
  99. { 3, 0 }, // UMAX
  100. { 3, 0 }, // UMIN
  101. { 3, 0 }, // USHR
  102. { 2, 0 }, // UTOF
  103. { 3, 0 }, // XOR
  104. { 1, 1 }, // DCL_RESOURCE
  105. { 1, 0 }, // DCL_CONSTANT_BUFFER
  106. { 1, 0 }, // DCL_SAMPLER
  107. { 1, 1 }, // DCL_INDEX_RANGE
  108. { 1, 0 }, // DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY
  109. { 1, 0 }, // DCL_GS_INPUT_PRIMITIVE
  110. { 0, 1 }, // DCL_MAX_OUTPUT_VERTEX_COUNT
  111. { 1, 0 }, // DCL_INPUT
  112. { 1, 1 }, // DCL_INPUT_SGV
  113. { 1, 0 }, // DCL_INPUT_SIV
  114. { 1, 0 }, // DCL_INPUT_PS
  115. { 1, 1 }, // DCL_INPUT_PS_SGV
  116. { 1, 1 }, // DCL_INPUT_PS_SIV
  117. { 1, 0 }, // DCL_OUTPUT
  118. { 1, 0 }, // DCL_OUTPUT_SGV
  119. { 1, 1 }, // DCL_OUTPUT_SIV
  120. { 0, 1 }, // DCL_TEMPS
  121. { 0, 3 }, // DCL_INDEXABLE_TEMP
  122. { 0, 0 }, // DCL_GLOBAL_FLAGS
  123. { 0, 0 }, // InstrD3D10
  124. { 4, 0 }, // LOD
  125. { 4, 0 }, // GATHER4
  126. { 0, 0 }, // SAMPLE_POS
  127. { 0, 0 }, // SAMPLE_INFO
  128. { 0, 0 }, // InstrD3D10_1
  129. { 0, 0 }, // HS_DECLS
  130. { 0, 0 }, // HS_CONTROL_POINT_PHASE
  131. { 0, 0 }, // HS_FORK_PHASE
  132. { 0, 0 }, // HS_JOIN_PHASE
  133. { 0, 0 }, // EMIT_STREAM
  134. { 0, 0 }, // CUT_STREAM
  135. { 1, 0 }, // EMITTHENCUT_STREAM
  136. { 1, 0 }, // INTERFACE_CALL
  137. { 0, 0 }, // BUFINFO
  138. { 2, 0 }, // DERIV_RTX_COARSE
  139. { 2, 0 }, // DERIV_RTX_FINE
  140. { 2, 0 }, // DERIV_RTY_COARSE
  141. { 2, 0 }, // DERIV_RTY_FINE
  142. { 5, 0 }, // GATHER4_C
  143. { 5, 0 }, // GATHER4_PO
  144. { 0, 0 }, // GATHER4_PO_C
  145. { 2, 0 }, // RCP
  146. { 0, 0 }, // F32TOF16
  147. { 0, 0 }, // F16TOF32
  148. { 0, 0 }, // UADDC
  149. { 0, 0 }, // USUBB
  150. { 0, 0 }, // COUNTBITS
  151. { 0, 0 }, // FIRSTBIT_HI
  152. { 0, 0 }, // FIRSTBIT_LO
  153. { 0, 0 }, // FIRSTBIT_SHI
  154. { 0, 0 }, // UBFE
  155. { 0, 0 }, // IBFE
  156. { 5, 0 }, // BFI
  157. { 0, 0 }, // BFREV
  158. { 5, 0 }, // SWAPC
  159. { 0, 0 }, // DCL_STREAM
  160. { 1, 0 }, // DCL_FUNCTION_BODY
  161. { 0, 0 }, // DCL_FUNCTION_TABLE
  162. { 0, 0 }, // DCL_INTERFACE
  163. { 0, 0 }, // DCL_INPUT_CONTROL_POINT_COUNT
  164. { 0, 0 }, // DCL_OUTPUT_CONTROL_POINT_COUNT
  165. { 0, 0 }, // DCL_TESS_DOMAIN
  166. { 0, 0 }, // DCL_TESS_PARTITIONING
  167. { 0, 0 }, // DCL_TESS_OUTPUT_PRIMITIVE
  168. { 0, 0 }, // DCL_HS_MAX_TESSFACTOR
  169. { 0, 0 }, // DCL_HS_FORK_PHASE_INSTANCE_COUNT
  170. { 0, 0 }, // DCL_HS_JOIN_PHASE_INSTANCE_COUNT
  171. { 0, 3 }, // DCL_THREAD_GROUP
  172. { 1, 1 }, // DCL_UNORDERED_ACCESS_VIEW_TYPED
  173. { 1, 0 }, // DCL_UNORDERED_ACCESS_VIEW_RAW
  174. { 1, 1 }, // DCL_UNORDERED_ACCESS_VIEW_STRUCTURED
  175. { 1, 1 }, // DCL_THREAD_GROUP_SHARED_MEMORY_RAW
  176. { 1, 2 }, // DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED
  177. { 1, 0 }, // DCL_RESOURCE_RAW
  178. { 1, 1 }, // DCL_RESOURCE_STRUCTURED
  179. { 3, 0 }, // LD_UAV_TYPED
  180. { 3, 0 }, // STORE_UAV_TYPED
  181. { 3, 0 }, // LD_RAW
  182. { 3, 0 }, // STORE_RAW
  183. { 4, 0 }, // LD_STRUCTURED
  184. { 4, 0 }, // STORE_STRUCTURED
  185. { 3, 0 }, // ATOMIC_AND
  186. { 3, 0 }, // ATOMIC_OR
  187. { 3, 0 }, // ATOMIC_XOR
  188. { 3, 0 }, // ATOMIC_CMP_STORE
  189. { 3, 0 }, // ATOMIC_IADD
  190. { 3, 0 }, // ATOMIC_IMAX
  191. { 3, 0 }, // ATOMIC_IMIN
  192. { 3, 0 }, // ATOMIC_UMAX
  193. { 3, 0 }, // ATOMIC_UMIN
  194. { 2, 0 }, // IMM_ATOMIC_ALLOC
  195. { 2, 0 }, // IMM_ATOMIC_CONSUME
  196. { 0, 0 }, // IMM_ATOMIC_IADD
  197. { 0, 0 }, // IMM_ATOMIC_AND
  198. { 0, 0 }, // IMM_ATOMIC_OR
  199. { 0, 0 }, // IMM_ATOMIC_XOR
  200. { 0, 0 }, // IMM_ATOMIC_EXCH
  201. { 0, 0 }, // IMM_ATOMIC_CMP_EXCH
  202. { 0, 0 }, // IMM_ATOMIC_IMAX
  203. { 0, 0 }, // IMM_ATOMIC_IMIN
  204. { 0, 0 }, // IMM_ATOMIC_UMAX
  205. { 0, 0 }, // IMM_ATOMIC_UMIN
  206. { 0, 0 }, // SYNC
  207. { 3, 0 }, // DADD
  208. { 3, 0 }, // DMAX
  209. { 3, 0 }, // DMIN
  210. { 3, 0 }, // DMUL
  211. { 3, 0 }, // DEQ
  212. { 3, 0 }, // DGE
  213. { 3, 0 }, // DLT
  214. { 3, 0 }, // DNE
  215. { 2, 0 }, // DMOV
  216. { 4, 0 }, // DMOVC
  217. { 0, 0 }, // DTOF
  218. { 0, 0 }, // FTOD
  219. { 3, 0 }, // EVAL_SNAPPED
  220. { 3, 0 }, // EVAL_SAMPLE_INDEX
  221. { 2, 0 }, // EVAL_CENTROID
  222. { 0, 1 }, // DCL_GS_INSTANCE_COUNT
  223. { 0, 0 }, // ABORT
  224. { 0, 0 }, // DEBUG_BREAK
  225. { 0, 0 }, // InstrD3D11
  226. { 0, 0 }, // DDIV
  227. { 0, 0 }, // DFMA
  228. { 0, 0 }, // DRCP
  229. { 0, 0 }, // MSAD
  230. { 0, 0 }, // DTOI
  231. { 0, 0 }, // DTOU
  232. { 0, 0 }, // ITOD
  233. { 0, 0 }, // UTOD
  234. };
  235. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcOpcodeInfo) == DxbcOpcode::Count);
  236. static const char* s_dxbcOpcode[] =
  237. {
  238. "add",
  239. "and",
  240. "break",
  241. "breakc",
  242. "call",
  243. "callc",
  244. "case",
  245. "continue",
  246. "continuec",
  247. "cut",
  248. "default",
  249. "deriv_rtx",
  250. "deriv_rty",
  251. "discard",
  252. "div",
  253. "dp2",
  254. "dp3",
  255. "dp4",
  256. "else",
  257. "emit",
  258. "emitthencut",
  259. "endif",
  260. "endloop",
  261. "endswitch",
  262. "eq",
  263. "exp",
  264. "frc",
  265. "ftoi",
  266. "ftou",
  267. "ge",
  268. "iadd",
  269. "if",
  270. "ieq",
  271. "ige",
  272. "ilt",
  273. "imad",
  274. "imax",
  275. "imin",
  276. "imul",
  277. "ine",
  278. "ineg",
  279. "ishl",
  280. "ishr",
  281. "itof",
  282. "label",
  283. "ld",
  284. "ld_ms",
  285. "log",
  286. "loop",
  287. "lt",
  288. "mad",
  289. "min",
  290. "max",
  291. "customdata",
  292. "mov",
  293. "movc",
  294. "mul",
  295. "ne",
  296. "nop",
  297. "not",
  298. "or",
  299. "resinfo",
  300. "ret",
  301. "retc",
  302. "round_ne",
  303. "round_ni",
  304. "round_pi",
  305. "round_z",
  306. "rsq",
  307. "sample",
  308. "sample_c",
  309. "sample_c_lz",
  310. "sample_l",
  311. "sample_d",
  312. "sample_b",
  313. "sqrt",
  314. "switch",
  315. "sincos",
  316. "udiv",
  317. "ult",
  318. "uge",
  319. "umul",
  320. "umad",
  321. "umax",
  322. "umin",
  323. "ushr",
  324. "utof",
  325. "xor",
  326. "dcl_resource",
  327. "dcl_constantbuffer",
  328. "dcl_sampler",
  329. "dcl_index_range",
  330. "dcl_gs_output_primitive_topology",
  331. "dcl_gs_input_primitive",
  332. "dcl_max_output_vertex_count",
  333. "dcl_input",
  334. "dcl_input_sgv",
  335. "dcl_input_siv",
  336. "dcl_input_ps",
  337. "dcl_input_ps_sgv",
  338. "dcl_input_ps_siv",
  339. "dcl_output",
  340. "dcl_output_sgv",
  341. "dcl_output_siv",
  342. "dcl_temps",
  343. "dcl_indexable_temp",
  344. "dcl_global_flags",
  345. NULL,
  346. "lod",
  347. "gather4",
  348. "sample_pos",
  349. "sample_info",
  350. NULL,
  351. "hs_decls",
  352. "hs_control_point_phase",
  353. "hs_fork_phase",
  354. "hs_join_phase",
  355. "emit_stream",
  356. "cut_stream",
  357. "emitthencut_stream",
  358. "interface_call",
  359. "bufinfo",
  360. "deriv_rtx_coarse",
  361. "deriv_rtx_fine",
  362. "deriv_rty_coarse",
  363. "deriv_rty_fine",
  364. "gather4_c",
  365. "gather4_po",
  366. "gather4_po_c",
  367. "rcp",
  368. "f32tof16",
  369. "f16tof32",
  370. "uaddc",
  371. "usubb",
  372. "countbits",
  373. "firstbit_hi",
  374. "firstbit_lo",
  375. "firstbit_shi",
  376. "ubfe",
  377. "ibfe",
  378. "bfi",
  379. "bfrev",
  380. "swapc",
  381. "dcl_stream",
  382. "dcl_function_body",
  383. "dcl_function_table",
  384. "dcl_interface",
  385. "dcl_input_control_point_count",
  386. "dcl_output_control_point_count",
  387. "dcl_tess_domain",
  388. "dcl_tess_partitioning",
  389. "dcl_tess_output_primitive",
  390. "dcl_hs_max_tessfactor",
  391. "dcl_hs_fork_phase_instance_count",
  392. "dcl_hs_join_phase_instance_count",
  393. "dcl_thread_group",
  394. "dcl_unordered_access_view_typed",
  395. "dcl_unordered_access_view_raw",
  396. "dcl_unordered_access_view_structured",
  397. "dcl_thread_group_shared_memory_raw",
  398. "dcl_thread_group_shared_memory_structured",
  399. "dcl_resource_raw",
  400. "dcl_resource_structured",
  401. "ld_uav_typed",
  402. "store_uav_typed",
  403. "ld_raw",
  404. "store_raw",
  405. "ld_structured",
  406. "store_structured",
  407. "atomic_and",
  408. "atomic_or",
  409. "atomic_xor",
  410. "atomic_cmp_store",
  411. "atomic_iadd",
  412. "atomic_imax",
  413. "atomic_imin",
  414. "atomic_umax",
  415. "atomic_umin",
  416. "imm_atomic_alloc",
  417. "imm_atomic_consume",
  418. "imm_atomic_iadd",
  419. "imm_atomic_and",
  420. "imm_atomic_or",
  421. "imm_atomic_xor",
  422. "imm_atomic_exch",
  423. "imm_atomic_cmp_exch",
  424. "imm_atomic_imax",
  425. "imm_atomic_imin",
  426. "imm_atomic_umax",
  427. "imm_atomic_umin",
  428. "sync",
  429. "dadd",
  430. "dmax",
  431. "dmin",
  432. "dmul",
  433. "deq",
  434. "dge",
  435. "dlt",
  436. "dne",
  437. "dmov",
  438. "dmovc",
  439. "dtof",
  440. "ftod",
  441. "eval_snapped",
  442. "eval_sample_index",
  443. "eval_centroid",
  444. "dcl_gs_instance_count",
  445. "abort",
  446. "debug_break",
  447. NULL,
  448. "ddiv",
  449. "dfma",
  450. "drcp",
  451. "msad",
  452. "dtoi",
  453. "dtou",
  454. "itod",
  455. "utod",
  456. };
  457. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcOpcode) == DxbcOpcode::Count);
  458. const char* getName(DxbcOpcode::Enum _opcode)
  459. {
  460. BX_CHECK(_opcode < DxbcOpcode::Count, "Unknown opcode id %d.", _opcode);
  461. return s_dxbcOpcode[_opcode];
  462. }
  463. static const char* s_dxbcSrvType[] =
  464. {
  465. "", // Unknown
  466. "Buffer", // Buffer
  467. "Texture1D", // Texture1D
  468. "Texture2D", // Texture2D
  469. "Texture2DMS", // Texture2DMS
  470. "Texture3D", // Texture3D
  471. "TextureCube", // TextureCube
  472. "Texture1DArray", // Texture1DArray
  473. "Texture2DArray", // Texture2DArray
  474. "Texture2DMSArray", // Texture2DMSArray
  475. "TextureCubearray", // TextureCubearray
  476. "RawBuffer", // RawBuffer
  477. "StructuredBuffer", // StructuredBuffer
  478. };
  479. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcSrvType) == DxbcResourceDim::Count);
  480. const char* s_dxbcInterpolationName[] =
  481. {
  482. "",
  483. "constant",
  484. "linear",
  485. "linear centroid",
  486. "linear noperspective",
  487. "linear noperspective centroid",
  488. "linear sample",
  489. "linear noperspective sample",
  490. };
  491. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcInterpolationName) == DxbcInterpolation::Count);
  492. // mesa/src/gallium/state_trackers/d3d1x/d3d1xshader/defs/shortfiles.txt
  493. static const char* s_dxbcOperandType[] =
  494. {
  495. "r", // Temp
  496. "v", // Input
  497. "o", // Output
  498. "x", // TempArray
  499. "l", // Imm32
  500. "d", // Imm64
  501. "s", // Sampler
  502. "t", // Resource
  503. "cb", // ConstantBuffer
  504. "icb", // ImmConstantBuffer
  505. "label", // Label
  506. "vPrim", // PrimitiveID
  507. "oDepth", // OutputDepth
  508. "null", // Null
  509. "rasterizer", // Rasterizer
  510. "oMask", // CoverageMask
  511. "stream", // Stream
  512. "function_body", // FunctionBody
  513. "function_table", // FunctionTable
  514. "interface", // Interface
  515. "function_input", // FunctionInput
  516. "function_output", // FunctionOutput
  517. "vOutputControlPointID", // OutputControlPointId
  518. "vForkInstanceID", // InputForkInstanceId
  519. "vJoinInstanceID", // InputJoinInstanceId
  520. "vicp", // InputControlPoint
  521. "vocp", // OutputControlPoint
  522. "vpc", // InputPatchConstant
  523. "vDomain", // InputDomainPoint
  524. "this", // ThisPointer
  525. "u", // UnorderedAccessView
  526. "g", // ThreadGroupSharedMemory
  527. "vThreadID", // InputThreadId
  528. "vThreadGrouID", // InputThreadGroupId
  529. "vThreadIDInGroup", // InputThreadIdInGroup
  530. "vCoverage", // InputCoverageMask
  531. "vThreadIDInGroupFlattened", // InputThreadIdInGroupFlattened
  532. "vGSInstanceID", // InputGsInstanceId
  533. "oDepthGE", // OutputDepthGreaterEqual
  534. "oDepthLE", // OutputDepthLessEqual
  535. "vCycleCounter", // CycleCounter
  536. };
  537. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcOperandType) == DxbcOperandType::Count);
  538. static const char* s_dxbcCustomDataClass[] =
  539. {
  540. "Comment",
  541. "DebugInfo",
  542. "Opaque",
  543. "dcl_immediateConstantBuffer",
  544. "ShaderMessage",
  545. "ClipPlaneConstantMappingsForDx9",
  546. };
  547. BX_STATIC_ASSERT(BX_COUNTOF(s_dxbcCustomDataClass) == DxbcCustomDataClass::Count);
  548. #define DXBC_MAX_NAME_STRING 512
  549. int32_t readString(bx::ReaderSeekerI* _reader, int64_t _offset, char* _out, uint32_t _max, bx::Error* _err)
  550. {
  551. int64_t oldOffset = bx::seek(_reader);
  552. bx::seek(_reader, _offset, bx::Whence::Begin);
  553. int32_t size = 0;
  554. for (uint32_t ii = 0; ii < _max-1; ++ii)
  555. {
  556. char ch;
  557. size += bx::read(_reader, ch, _err);
  558. *_out++ = ch;
  559. if ('\0' == ch)
  560. {
  561. break;
  562. }
  563. }
  564. *_out = '\0';
  565. bx::seek(_reader, oldOffset, bx::Whence::Begin);
  566. return size;
  567. }
  568. inline uint32_t dxbcMixF(uint32_t _b, uint32_t _c, uint32_t _d)
  569. {
  570. const uint32_t tmp0 = bx::uint32_xor(_c, _d);
  571. const uint32_t tmp1 = bx::uint32_and(_b, tmp0);
  572. const uint32_t result = bx::uint32_xor(_d, tmp1);
  573. return result;
  574. }
  575. inline uint32_t dxbcMixG(uint32_t _b, uint32_t _c, uint32_t _d)
  576. {
  577. return dxbcMixF(_d, _b, _c);
  578. }
  579. inline uint32_t dxbcMixH(uint32_t _b, uint32_t _c, uint32_t _d)
  580. {
  581. const uint32_t tmp0 = bx::uint32_xor(_b, _c);
  582. const uint32_t result = bx::uint32_xor(_d, tmp0);
  583. return result;
  584. }
  585. inline uint32_t dxbcMixI(uint32_t _b, uint32_t _c, uint32_t _d)
  586. {
  587. const uint32_t tmp0 = bx::uint32_orc(_b, _d);
  588. const uint32_t result = bx::uint32_xor(_c, tmp0);
  589. return result;
  590. }
  591. void dxbcHashBlock(const uint32_t* data, uint32_t* hash)
  592. {
  593. const uint32_t d0 = data[ 0];
  594. const uint32_t d1 = data[ 1];
  595. const uint32_t d2 = data[ 2];
  596. const uint32_t d3 = data[ 3];
  597. const uint32_t d4 = data[ 4];
  598. const uint32_t d5 = data[ 5];
  599. const uint32_t d6 = data[ 6];
  600. const uint32_t d7 = data[ 7];
  601. const uint32_t d8 = data[ 8];
  602. const uint32_t d9 = data[ 9];
  603. const uint32_t d10 = data[10];
  604. const uint32_t d11 = data[11];
  605. const uint32_t d12 = data[12];
  606. const uint32_t d13 = data[13];
  607. const uint32_t d14 = data[14];
  608. const uint32_t d15 = data[15];
  609. uint32_t aa = hash[0];
  610. uint32_t bb = hash[1];
  611. uint32_t cc = hash[2];
  612. uint32_t dd = hash[3];
  613. aa = bb + bx::uint32_rol(aa + dxbcMixF(bb, cc, dd) + d0 + 0xd76aa478, 7);
  614. dd = aa + bx::uint32_rol(dd + dxbcMixF(aa, bb, cc) + d1 + 0xe8c7b756, 12);
  615. cc = dd + bx::uint32_ror(cc + dxbcMixF(dd, aa, bb) + d2 + 0x242070db, 15);
  616. bb = cc + bx::uint32_ror(bb + dxbcMixF(cc, dd, aa) + d3 + 0xc1bdceee, 10);
  617. aa = bb + bx::uint32_rol(aa + dxbcMixF(bb, cc, dd) + d4 + 0xf57c0faf, 7);
  618. dd = aa + bx::uint32_rol(dd + dxbcMixF(aa, bb, cc) + d5 + 0x4787c62a, 12);
  619. cc = dd + bx::uint32_ror(cc + dxbcMixF(dd, aa, bb) + d6 + 0xa8304613, 15);
  620. bb = cc + bx::uint32_ror(bb + dxbcMixF(cc, dd, aa) + d7 + 0xfd469501, 10);
  621. aa = bb + bx::uint32_rol(aa + dxbcMixF(bb, cc, dd) + d8 + 0x698098d8, 7);
  622. dd = aa + bx::uint32_rol(dd + dxbcMixF(aa, bb, cc) + d9 + 0x8b44f7af, 12);
  623. cc = dd + bx::uint32_ror(cc + dxbcMixF(dd, aa, bb) + d10 + 0xffff5bb1, 15);
  624. bb = cc + bx::uint32_ror(bb + dxbcMixF(cc, dd, aa) + d11 + 0x895cd7be, 10);
  625. aa = bb + bx::uint32_rol(aa + dxbcMixF(bb, cc, dd) + d12 + 0x6b901122, 7);
  626. dd = aa + bx::uint32_rol(dd + dxbcMixF(aa, bb, cc) + d13 + 0xfd987193, 12);
  627. cc = dd + bx::uint32_ror(cc + dxbcMixF(dd, aa, bb) + d14 + 0xa679438e, 15);
  628. bb = cc + bx::uint32_ror(bb + dxbcMixF(cc, dd, aa) + d15 + 0x49b40821, 10);
  629. aa = bb + bx::uint32_rol(aa + dxbcMixG(bb, cc, dd) + d1 + 0xf61e2562, 5);
  630. dd = aa + bx::uint32_rol(dd + dxbcMixG(aa, bb, cc) + d6 + 0xc040b340, 9);
  631. cc = dd + bx::uint32_rol(cc + dxbcMixG(dd, aa, bb) + d11 + 0x265e5a51, 14);
  632. bb = cc + bx::uint32_ror(bb + dxbcMixG(cc, dd, aa) + d0 + 0xe9b6c7aa, 12);
  633. aa = bb + bx::uint32_rol(aa + dxbcMixG(bb, cc, dd) + d5 + 0xd62f105d, 5);
  634. dd = aa + bx::uint32_rol(dd + dxbcMixG(aa, bb, cc) + d10 + 0x02441453, 9);
  635. cc = dd + bx::uint32_rol(cc + dxbcMixG(dd, aa, bb) + d15 + 0xd8a1e681, 14);
  636. bb = cc + bx::uint32_ror(bb + dxbcMixG(cc, dd, aa) + d4 + 0xe7d3fbc8, 12);
  637. aa = bb + bx::uint32_rol(aa + dxbcMixG(bb, cc, dd) + d9 + 0x21e1cde6, 5);
  638. dd = aa + bx::uint32_rol(dd + dxbcMixG(aa, bb, cc) + d14 + 0xc33707d6, 9);
  639. cc = dd + bx::uint32_rol(cc + dxbcMixG(dd, aa, bb) + d3 + 0xf4d50d87, 14);
  640. bb = cc + bx::uint32_ror(bb + dxbcMixG(cc, dd, aa) + d8 + 0x455a14ed, 12);
  641. aa = bb + bx::uint32_rol(aa + dxbcMixG(bb, cc, dd) + d13 + 0xa9e3e905, 5);
  642. dd = aa + bx::uint32_rol(dd + dxbcMixG(aa, bb, cc) + d2 + 0xfcefa3f8, 9);
  643. cc = dd + bx::uint32_rol(cc + dxbcMixG(dd, aa, bb) + d7 + 0x676f02d9, 14);
  644. bb = cc + bx::uint32_ror(bb + dxbcMixG(cc, dd, aa) + d12 + 0x8d2a4c8a, 12);
  645. aa = bb + bx::uint32_rol(aa + dxbcMixH(bb, cc, dd) + d5 + 0xfffa3942, 4);
  646. dd = aa + bx::uint32_rol(dd + dxbcMixH(aa, bb, cc) + d8 + 0x8771f681, 11);
  647. cc = dd + bx::uint32_rol(cc + dxbcMixH(dd, aa, bb) + d11 + 0x6d9d6122, 16);
  648. bb = cc + bx::uint32_ror(bb + dxbcMixH(cc, dd, aa) + d14 + 0xfde5380c, 9);
  649. aa = bb + bx::uint32_rol(aa + dxbcMixH(bb, cc, dd) + d1 + 0xa4beea44, 4);
  650. dd = aa + bx::uint32_rol(dd + dxbcMixH(aa, bb, cc) + d4 + 0x4bdecfa9, 11);
  651. cc = dd + bx::uint32_rol(cc + dxbcMixH(dd, aa, bb) + d7 + 0xf6bb4b60, 16);
  652. bb = cc + bx::uint32_ror(bb + dxbcMixH(cc, dd, aa) + d10 + 0xbebfbc70, 9);
  653. aa = bb + bx::uint32_rol(aa + dxbcMixH(bb, cc, dd) + d13 + 0x289b7ec6, 4);
  654. dd = aa + bx::uint32_rol(dd + dxbcMixH(aa, bb, cc) + d0 + 0xeaa127fa, 11);
  655. cc = dd + bx::uint32_rol(cc + dxbcMixH(dd, aa, bb) + d3 + 0xd4ef3085, 16);
  656. bb = cc + bx::uint32_ror(bb + dxbcMixH(cc, dd, aa) + d6 + 0x04881d05, 9);
  657. aa = bb + bx::uint32_rol(aa + dxbcMixH(bb, cc, dd) + d9 + 0xd9d4d039, 4);
  658. dd = aa + bx::uint32_rol(dd + dxbcMixH(aa, bb, cc) + d12 + 0xe6db99e5, 11);
  659. cc = dd + bx::uint32_rol(cc + dxbcMixH(dd, aa, bb) + d15 + 0x1fa27cf8, 16);
  660. bb = cc + bx::uint32_ror(bb + dxbcMixH(cc, dd, aa) + d2 + 0xc4ac5665, 9);
  661. aa = bb + bx::uint32_rol(aa + dxbcMixI(bb, cc, dd) + d0 + 0xf4292244, 6);
  662. dd = aa + bx::uint32_rol(dd + dxbcMixI(aa, bb, cc) + d7 + 0x432aff97, 10);
  663. cc = dd + bx::uint32_rol(cc + dxbcMixI(dd, aa, bb) + d14 + 0xab9423a7, 15);
  664. bb = cc + bx::uint32_ror(bb + dxbcMixI(cc, dd, aa) + d5 + 0xfc93a039, 11);
  665. aa = bb + bx::uint32_rol(aa + dxbcMixI(bb, cc, dd) + d12 + 0x655b59c3, 6);
  666. dd = aa + bx::uint32_rol(dd + dxbcMixI(aa, bb, cc) + d3 + 0x8f0ccc92, 10);
  667. cc = dd + bx::uint32_rol(cc + dxbcMixI(dd, aa, bb) + d10 + 0xffeff47d, 15);
  668. bb = cc + bx::uint32_ror(bb + dxbcMixI(cc, dd, aa) + d1 + 0x85845dd1, 11);
  669. aa = bb + bx::uint32_rol(aa + dxbcMixI(bb, cc, dd) + d8 + 0x6fa87e4f, 6);
  670. dd = aa + bx::uint32_rol(dd + dxbcMixI(aa, bb, cc) + d15 + 0xfe2ce6e0, 10);
  671. cc = dd + bx::uint32_rol(cc + dxbcMixI(dd, aa, bb) + d6 + 0xa3014314, 15);
  672. bb = cc + bx::uint32_ror(bb + dxbcMixI(cc, dd, aa) + d13 + 0x4e0811a1, 11);
  673. aa = bb + bx::uint32_rol(aa + dxbcMixI(bb, cc, dd) + d4 + 0xf7537e82, 6);
  674. dd = aa + bx::uint32_rol(dd + dxbcMixI(aa, bb, cc) + d11 + 0xbd3af235, 10);
  675. cc = dd + bx::uint32_rol(cc + dxbcMixI(dd, aa, bb) + d2 + 0x2ad7d2bb, 15);
  676. bb = cc + bx::uint32_ror(bb + dxbcMixI(cc, dd, aa) + d9 + 0xeb86d391, 11);
  677. hash[0] += aa;
  678. hash[1] += bb;
  679. hash[2] += cc;
  680. hash[3] += dd;
  681. }
  682. // dxbc hash function is slightly modified version of MD5 hash.
  683. // https://tools.ietf.org/html/rfc1321
  684. // http://www.efgh.com/software/md5.txt
  685. //
  686. // Assumption is that data pointer, size are both 4-byte aligned,
  687. // and little endian.
  688. //
  689. void dxbcHash(const void* _data, uint32_t _size, void* _digest)
  690. {
  691. uint32_t hash[4] =
  692. {
  693. 0x67452301,
  694. 0xefcdab89,
  695. 0x98badcfe,
  696. 0x10325476,
  697. };
  698. const uint32_t* data = (const uint32_t*)_data;
  699. for (uint32_t ii = 0, num = _size/64; ii < num; ++ii)
  700. {
  701. dxbcHashBlock(data, hash);
  702. data += 16;
  703. }
  704. uint32_t last[16];
  705. bx::memSet(last, 0, sizeof(last) );
  706. const uint32_t remaining = _size & 0x3f;
  707. if (remaining >= 56)
  708. {
  709. bx::memCopy(&last[0], data, remaining);
  710. last[remaining/4] = 0x80;
  711. dxbcHashBlock(last, hash);
  712. bx::memSet(&last[1], 0, 56);
  713. }
  714. else
  715. {
  716. bx::memCopy(&last[1], data, remaining);
  717. last[1 + remaining/4] = 0x80;
  718. }
  719. last[ 0] = _size * 8;
  720. last[15] = _size * 2 + 1;
  721. dxbcHashBlock(last, hash);
  722. bx::memCopy(_digest, hash, 16);
  723. }
  724. int32_t read(bx::ReaderI* _reader, DxbcSubOperand& _subOperand, bx::Error* _err)
  725. {
  726. uint32_t token;
  727. int32_t size = 0;
  728. // 0 1 2 3
  729. // 76543210765432107654321076543210
  730. // e222111000nnttttttttssssssssmmoo
  731. // ^^ ^ ^ ^ ^ ^ ^ ^-- number of operands
  732. // || | | | | | +---- operand mode
  733. // || | | | | +------------ operand mode bits
  734. // || | | | +-------------------- type
  735. // || | | +---------------------- number of addressing modes
  736. // || | +------------------------- addressing mode 0
  737. // || +---------------------------- addressing mode 1
  738. // |+------------------------------- addressing mode 2
  739. // +-------------------------------- extended
  740. size += bx::read(_reader, token, _err);
  741. _subOperand.type = DxbcOperandType::Enum( (token & UINT32_C(0x000ff000) ) >> 12);
  742. _subOperand.numAddrModes = uint8_t( (token & UINT32_C(0x00300000) ) >> 20);
  743. _subOperand.addrMode = uint8_t( (token & UINT32_C(0x01c00000) ) >> 22);
  744. _subOperand.mode = DxbcOperandMode::Enum( (token & UINT32_C(0x0000000c) ) >> 2);
  745. _subOperand.modeBits = uint8_t( (token & UINT32_C(0x00000ff0) ) >> 4) & "\x0f\xff\x03\x00"[_subOperand.mode];
  746. _subOperand.num = uint8_t( (token & UINT32_C(0x00000003) ) );
  747. switch (_subOperand.addrMode)
  748. {
  749. case DxbcOperandAddrMode::Imm32:
  750. size += bx::read(_reader, _subOperand.regIndex, _err);
  751. break;
  752. case DxbcOperandAddrMode::Reg:
  753. {
  754. DxbcSubOperand subOperand;
  755. size += read(_reader, subOperand, _err);
  756. }
  757. break;
  758. case DxbcOperandAddrMode::RegImm32:
  759. {
  760. size += bx::read(_reader, _subOperand.regIndex, _err);
  761. DxbcSubOperand subOperand;
  762. size += read(_reader, subOperand, _err);
  763. }
  764. break;
  765. case DxbcOperandAddrMode::RegImm64:
  766. {
  767. size += bx::read(_reader, _subOperand.regIndex, _err);
  768. size += bx::read(_reader, _subOperand.regIndex, _err);
  769. DxbcSubOperand subOperand;
  770. size += read(_reader, subOperand, _err);
  771. }
  772. break;
  773. default:
  774. BX_CHECK(false, "sub operand addressing mode %d", _subOperand.addrMode);
  775. break;
  776. }
  777. return size;
  778. }
  779. int32_t write(bx::WriterI* _writer, const DxbcSubOperand& _subOperand, bx::Error* _err)
  780. {
  781. int32_t size = 0;
  782. uint32_t token = 0;
  783. token |= (_subOperand.type << 12) & UINT32_C(0x000ff000);
  784. token |= (_subOperand.numAddrModes << 20) & UINT32_C(0x00300000);
  785. token |= (_subOperand.addrMode << 22) & UINT32_C(0x01c00000);
  786. token |= (_subOperand.mode << 2) & UINT32_C(0x0000000c);
  787. token |= (_subOperand.modeBits << 4) & UINT32_C(0x00000ff0);
  788. token |= _subOperand.num & UINT32_C(0x00000003);
  789. size += bx::write(_writer, token, _err);
  790. switch (_subOperand.addrMode)
  791. {
  792. case DxbcOperandAddrMode::Imm32:
  793. size += bx::write(_writer, _subOperand.regIndex, _err);
  794. break;
  795. case DxbcOperandAddrMode::Reg:
  796. {
  797. DxbcSubOperand subOperand;
  798. size += write(_writer, subOperand, _err);
  799. }
  800. break;
  801. case DxbcOperandAddrMode::RegImm32:
  802. {
  803. size += bx::write(_writer, _subOperand.regIndex, _err);
  804. DxbcSubOperand subOperand;
  805. size += write(_writer, subOperand, _err);
  806. }
  807. break;
  808. case DxbcOperandAddrMode::RegImm64:
  809. {
  810. size += bx::write(_writer, _subOperand.regIndex, _err);
  811. size += bx::write(_writer, _subOperand.regIndex, _err);
  812. DxbcSubOperand subOperand;
  813. size += write(_writer, subOperand, _err);
  814. }
  815. break;
  816. default:
  817. BX_CHECK(false, "sub operand addressing mode %d", _subOperand.addrMode);
  818. break;
  819. }
  820. return size;
  821. }
  822. int32_t read(bx::ReaderI* _reader, DxbcOperand& _operand, bx::Error* _err)
  823. {
  824. int32_t size = 0;
  825. uint32_t token;
  826. size += bx::read(_reader, token, _err);
  827. // 0 1 2 3
  828. // 76543210765432107654321076543210
  829. // e222111000nnttttttttssssssssmmoo
  830. // ^^ ^ ^ ^ ^ ^ ^ ^-- number of operands
  831. // || | | | | | +---- operand mode
  832. // || | | | | +------------ operand mode bits
  833. // || | | | +-------------------- type
  834. // || | | +---------------------- number of addressing modes
  835. // || | +------------------------- addressing mode 0
  836. // || +---------------------------- addressing mode 1
  837. // |+------------------------------- addressing mode 2
  838. // +-------------------------------- extended
  839. _operand.numAddrModes = uint8_t( (token & UINT32_C(0x00300000) ) >> 20);
  840. _operand.addrMode[0] = uint8_t( (token & UINT32_C(0x01c00000) ) >> 22);
  841. _operand.addrMode[1] = uint8_t( (token & UINT32_C(0x0e000000) ) >> 25);
  842. _operand.addrMode[2] = uint8_t( (token & UINT32_C(0x70000000) ) >> 28);
  843. _operand.type = DxbcOperandType::Enum( (token & UINT32_C(0x000ff000) ) >> 12);
  844. _operand.mode = DxbcOperandMode::Enum( (token & UINT32_C(0x0000000c) ) >> 2);
  845. _operand.modeBits = uint8_t( (token & UINT32_C(0x00000ff0) ) >> 4) & "\x0f\xff\x03\x00"[_operand.mode];
  846. _operand.num = uint8_t( (token & UINT32_C(0x00000003) ) );
  847. const bool extended = 0 != (token & UINT32_C(0x80000000) );
  848. if (extended)
  849. {
  850. uint32_t extBits = 0;
  851. size += bx::read(_reader, extBits, _err);
  852. _operand.modifier = DxbcOperandModifier::Enum( (extBits & UINT32_C(0x00003fc0) ) >> 6);
  853. }
  854. else
  855. {
  856. _operand.modifier = DxbcOperandModifier::None;
  857. }
  858. switch (_operand.type)
  859. {
  860. case DxbcOperandType::Imm32:
  861. _operand.num = 2 == _operand.num ? 4 : _operand.num;
  862. for (uint32_t ii = 0; ii < _operand.num; ++ii)
  863. {
  864. size += bx::read(_reader, _operand.un.imm32[ii], _err);
  865. }
  866. break;
  867. case DxbcOperandType::Imm64:
  868. _operand.num = 2 == _operand.num ? 4 : _operand.num;
  869. for (uint32_t ii = 0; ii < _operand.num; ++ii)
  870. {
  871. size += bx::read(_reader, _operand.un.imm64[ii], _err);
  872. }
  873. break;
  874. default:
  875. break;
  876. }
  877. for (uint32_t ii = 0; ii < _operand.numAddrModes; ++ii)
  878. {
  879. switch (_operand.addrMode[ii])
  880. {
  881. case DxbcOperandAddrMode::Imm32:
  882. size += bx::read(_reader, _operand.regIndex[ii], _err);
  883. break;
  884. case DxbcOperandAddrMode::Reg:
  885. size += read(_reader, _operand.subOperand[ii], _err);
  886. break;
  887. case DxbcOperandAddrMode::RegImm32:
  888. size += bx::read(_reader, _operand.regIndex[ii], _err);
  889. size += read(_reader, _operand.subOperand[ii], _err);
  890. break;
  891. default:
  892. BX_CHECK(false, "operand %d addressing mode %d", ii, _operand.addrMode[ii]);
  893. break;
  894. }
  895. }
  896. return size;
  897. }
  898. int32_t write(bx::WriterI* _writer, const DxbcOperand& _operand, bx::Error* _err)
  899. {
  900. int32_t size = 0;
  901. const bool extended = _operand.modifier != DxbcOperandModifier::None;
  902. uint32_t token = 0;
  903. token |= extended ? UINT32_C(0x80000000) : 0;
  904. token |= (_operand.numAddrModes << 20) & UINT32_C(0x00300000);
  905. token |= (_operand.addrMode[0] << 22) & UINT32_C(0x01c00000);
  906. token |= (_operand.addrMode[1] << 25) & UINT32_C(0x0e000000);
  907. token |= (_operand.addrMode[2] << 28) & UINT32_C(0x70000000);
  908. token |= (_operand.type << 12) & UINT32_C(0x000ff000);
  909. token |= (_operand.mode << 2) & UINT32_C(0x0000000c);
  910. token |= (4 == _operand.num ? 2 : _operand.num) & UINT32_C(0x00000003);
  911. token |= ( (_operand.modeBits & "\x0f\xff\x03\x00"[_operand.mode]) << 4) & UINT32_C(0x00000ff0);
  912. size += bx::write(_writer, token, _err);
  913. if (extended)
  914. {
  915. uint32_t extBits = 0
  916. | ( (_operand.modifier << 6) & UINT32_C(0x00003fc0) )
  917. | 1 /* 1 == has extended operand modifier */
  918. ;
  919. size += bx::write(_writer, extBits, _err);
  920. }
  921. switch (_operand.type)
  922. {
  923. case DxbcOperandType::Imm32:
  924. for (uint32_t ii = 0; ii < _operand.num; ++ii)
  925. {
  926. size += bx::write(_writer, _operand.un.imm32[ii], _err);
  927. }
  928. break;
  929. case DxbcOperandType::Imm64:
  930. for (uint32_t ii = 0; ii < _operand.num; ++ii)
  931. {
  932. size += bx::write(_writer, _operand.un.imm64[ii], _err);
  933. }
  934. break;
  935. default:
  936. break;
  937. }
  938. for (uint32_t ii = 0, num = bx::uint32_min(_operand.numAddrModes, BX_COUNTOF(_operand.addrMode) ); ii < num; ++ii)
  939. {
  940. switch (_operand.addrMode[ii])
  941. {
  942. case DxbcOperandAddrMode::Imm32:
  943. size += bx::write(_writer, _operand.regIndex[ii], _err);
  944. break;
  945. case DxbcOperandAddrMode::Reg:
  946. size += write(_writer, _operand.subOperand[ii], _err);
  947. break;
  948. case DxbcOperandAddrMode::RegImm32:
  949. size += bx::write(_writer, _operand.regIndex[ii], _err);
  950. size += write(_writer, _operand.subOperand[ii], _err);
  951. break;
  952. default:
  953. BX_CHECK(false, "operand %d addressing mode %d", ii, _operand.addrMode[ii]);
  954. break;
  955. }
  956. }
  957. return size;
  958. }
  959. int32_t read(bx::ReaderI* _reader, DxbcInstruction& _instruction, bx::Error* _err)
  960. {
  961. int32_t size = 0;
  962. uint32_t token;
  963. size += bx::read(_reader, token, _err);
  964. // 0 1 2 3
  965. // 76543210765432107654321076543210
  966. // elllllll.............ooooooooooo
  967. // ^^ ^----------- opcode
  968. // |+------------------------------- length
  969. // +-------------------------------- extended
  970. _instruction.opcode = DxbcOpcode::Enum( (token & UINT32_C(0x000007ff) ) );
  971. _instruction.length = uint8_t( (token & UINT32_C(0x7f000000) ) >> 24);
  972. bool extended = 0 != (token & UINT32_C(0x80000000) );
  973. _instruction.srv = DxbcResourceDim::Unknown;
  974. _instruction.samples = 0;
  975. _instruction.shadow = false;
  976. _instruction.mono = false;
  977. _instruction.allowRefactoring = false;
  978. _instruction.fp64 = false;
  979. _instruction.earlyDepth = false;
  980. _instruction.enableBuffers = false;
  981. _instruction.skipOptimization = false;
  982. _instruction.enableMinPrecision = false;
  983. _instruction.enableDoubleExtensions = false;
  984. _instruction.enableShaderExtensions = false;
  985. _instruction.threadsInGroup = false;
  986. _instruction.sharedMemory = false;
  987. _instruction.uavGroup = false;
  988. _instruction.uavGlobal = false;
  989. _instruction.saturate = false;
  990. _instruction.testNZ = false;
  991. _instruction.retType = DxbcResourceReturnType::Unused;
  992. _instruction.customDataClass = DxbcCustomDataClass::Comment;
  993. _instruction.customData.clear();
  994. switch (_instruction.opcode)
  995. {
  996. case DxbcOpcode::CUSTOMDATA:
  997. {
  998. _instruction.customDataClass = DxbcCustomDataClass::Enum( (token & UINT32_C(0xfffff800) ) >> 11);
  999. _instruction.numOperands = 0;
  1000. size += bx::read(_reader, _instruction.length, _err);
  1001. for (uint32_t ii = 0, num = (_instruction.length-2); ii < num && _err->isOk(); ++ii)
  1002. {
  1003. uint32_t temp;
  1004. size += bx::read(_reader, temp, _err);
  1005. if (_err->isOk() )
  1006. {
  1007. _instruction.customData.push_back(temp);
  1008. }
  1009. }
  1010. }
  1011. return size;
  1012. case DxbcOpcode::DCL_CONSTANT_BUFFER:
  1013. // 0 1 2 3
  1014. // 76543210765432107654321076543210
  1015. // ........ a...........
  1016. // ^------------ Allow refactoring
  1017. _instruction.allowRefactoring = 0 != (token & UINT32_C(0x00000800) );
  1018. break;
  1019. case DxbcOpcode::DCL_GLOBAL_FLAGS:
  1020. // 0 1 2 3
  1021. // 76543210765432107654321076543210
  1022. // ........ sxmoudfa...........
  1023. // ^^^^^^^^------------ Allow refactoring
  1024. // ||||||+------------- FP64
  1025. // |||||+-------------- Force early depth/stencil
  1026. // ||||+--------------- Enable raw and structured buffers
  1027. // |||+---------------- Skip optimizations
  1028. // ||+----------------- Enable minimum precision
  1029. // |+------------------ Enable double extension
  1030. // +------------------- Enable shader extension
  1031. _instruction.allowRefactoring = 0 != (token & UINT32_C(0x00000800) );
  1032. _instruction.fp64 = 0 != (token & UINT32_C(0x00001000) );
  1033. _instruction.earlyDepth = 0 != (token & UINT32_C(0x00002000) );
  1034. _instruction.enableBuffers = 0 != (token & UINT32_C(0x00004000) );
  1035. _instruction.skipOptimization = 0 != (token & UINT32_C(0x00008000) );
  1036. _instruction.enableMinPrecision = 0 != (token & UINT32_C(0x00010000) );
  1037. _instruction.enableDoubleExtensions = 0 != (token & UINT32_C(0x00020000) );
  1038. _instruction.enableShaderExtensions = 0 != (token & UINT32_C(0x00040000) );
  1039. break;
  1040. case DxbcOpcode::DCL_INPUT_PS:
  1041. // 0 1 2 3
  1042. // 76543210765432107654321076543210
  1043. // ........ iiiii...........
  1044. // ^---------------- Interploation
  1045. _instruction.interpolation = DxbcInterpolation::Enum( (token & UINT32_C(0x0000f800) ) >> 11);
  1046. break;
  1047. case DxbcOpcode::DCL_RESOURCE:
  1048. // 0 1 2 3
  1049. // 76543210765432107654321076543210
  1050. // ........ sssssssrrrrr...........
  1051. // ^ ^---------------- SRV
  1052. // +----------------------- MSAA samples
  1053. _instruction.srv = DxbcResourceDim::Enum( (token & UINT32_C(0x0000f800) ) >> 11);
  1054. _instruction.samples = uint8_t( (token & UINT32_C(0x007f0000) ) >> 16);
  1055. break;
  1056. case DxbcOpcode::DCL_SAMPLER:
  1057. // 0 1 2 3
  1058. // 76543210765432107654321076543210
  1059. // ........ ms...........
  1060. // ^^------------ Shadow sampler
  1061. // +------------- Mono
  1062. _instruction.shadow = 0 != (token & UINT32_C(0x00000800) );
  1063. _instruction.mono = 0 != (token & UINT32_C(0x00001000) );
  1064. break;
  1065. case DxbcOpcode::SYNC:
  1066. // 0 1 2 3
  1067. // 76543210765432107654321076543210
  1068. // ........ gust...........
  1069. // ^^^^------------ Threads in group
  1070. // ||+------------- Shared memory
  1071. // |+-------------- UAV group
  1072. // +--------------- UAV global
  1073. _instruction.threadsInGroup = 0 != (token & UINT32_C(0x00000800) );
  1074. _instruction.sharedMemory = 0 != (token & UINT32_C(0x00001000) );
  1075. _instruction.uavGroup = 0 != (token & UINT32_C(0x00002000) );
  1076. _instruction.uavGlobal = 0 != (token & UINT32_C(0x00004000) );
  1077. break;
  1078. default:
  1079. // 0 1 2 3
  1080. // 76543210765432107654321076543210
  1081. // ........ ppppn stt...........
  1082. // ^ ^ ^^------------- Resource info return type
  1083. // | | +-------------- Saturate
  1084. // | +------------------- Test not zero
  1085. // +----------------------- Precise mask
  1086. _instruction.retType = DxbcResourceReturnType::Enum( (token & UINT32_C(0x00001800) ) >> 11);
  1087. _instruction.saturate = 0 != (token & UINT32_C(0x00002000) );
  1088. _instruction.testNZ = 0 != (token & UINT32_C(0x00040000) );
  1089. // _instruction.precise = uint8_t( (token & UINT32_C(0x00780000) ) >> 19);
  1090. break;
  1091. }
  1092. _instruction.extended[0] = DxbcInstruction::ExtendedType::Count;
  1093. for (uint32_t ii = 0; extended; ++ii)
  1094. {
  1095. // 0 1 2 3
  1096. // 76543210765432107654321076543210
  1097. // e..........................ttttt
  1098. // ^ ^
  1099. // | +----- type
  1100. // +-------------------------------- extended
  1101. uint32_t extBits;
  1102. size += bx::read(_reader, extBits, _err);
  1103. extended = 0 != (extBits & UINT32_C(0x80000000) );
  1104. _instruction.extended[ii ] = DxbcInstruction::ExtendedType::Enum(extBits & UINT32_C(0x0000001f) );
  1105. _instruction.extended[ii+1] = DxbcInstruction::ExtendedType::Count;
  1106. switch (_instruction.extended[ii])
  1107. {
  1108. case DxbcInstruction::ExtendedType::SampleControls:
  1109. // 0 1 2 3
  1110. // 76543210765432107654321076543210
  1111. // . zzzzyyyyxxxx .....
  1112. // ^ ^ ^
  1113. // | | +------------- x
  1114. // | +----------------- y
  1115. // +--------------------- z
  1116. _instruction.sampleOffsets[0] = uint8_t( (extBits & UINT32_C(0x00001e00) ) >> 9);
  1117. _instruction.sampleOffsets[1] = uint8_t( (extBits & UINT32_C(0x0001e000) ) >> 13);
  1118. _instruction.sampleOffsets[2] = uint8_t( (extBits & UINT32_C(0x001e0000) ) >> 17);
  1119. break;
  1120. case DxbcInstruction::ExtendedType::ResourceDim:
  1121. // 0 1 2 3
  1122. // 76543210765432107654321076543210
  1123. // . .....
  1124. //
  1125. _instruction.resourceTarget = uint8_t( (extBits & UINT32_C(0x000003e0) ) >> 6);
  1126. _instruction.resourceStride = uint8_t( (extBits & UINT32_C(0x0000f800) ) >> 11);
  1127. break;
  1128. case DxbcInstruction::ExtendedType::ResourceReturnType:
  1129. // 0 1 2 3
  1130. // 76543210765432107654321076543210
  1131. // . 3333222211110000.....
  1132. // ^ ^ ^
  1133. // | | +------------- x
  1134. // | +----------------- y
  1135. // +--------------------- z
  1136. _instruction.resourceReturnTypes[0] = DxbcResourceReturnType::Enum( (extBits & UINT32_C(0x000001e0) ) >> 6);
  1137. _instruction.resourceReturnTypes[1] = DxbcResourceReturnType::Enum( (extBits & UINT32_C(0x00001e00) ) >> 9);
  1138. _instruction.resourceReturnTypes[2] = DxbcResourceReturnType::Enum( (extBits & UINT32_C(0x0001e000) ) >> 13);
  1139. _instruction.resourceReturnTypes[3] = DxbcResourceReturnType::Enum( (extBits & UINT32_C(0x001e0000) ) >> 17);
  1140. break;
  1141. default:
  1142. break;
  1143. }
  1144. }
  1145. switch (_instruction.opcode)
  1146. {
  1147. case DxbcOpcode::DCL_FUNCTION_TABLE:
  1148. {
  1149. uint32_t tableId;
  1150. size += read(_reader, tableId, _err);
  1151. uint32_t num;
  1152. size += read(_reader, num);
  1153. for (uint32_t ii = 0; ii < num; ++ii)
  1154. {
  1155. uint32_t bodyId;
  1156. size += read(_reader, bodyId, _err);
  1157. }
  1158. }
  1159. break;
  1160. case DxbcOpcode::DCL_INTERFACE:
  1161. {
  1162. uint32_t interfaceId;
  1163. size += read(_reader, interfaceId, _err);
  1164. uint32_t num;
  1165. size += read(_reader, num, _err);
  1166. BX_CHECK(false, "not implemented.");
  1167. }
  1168. break;
  1169. default:
  1170. break;
  1171. };
  1172. uint32_t currOp = 0;
  1173. const DxbcOpcodeInfo& info = s_dxbcOpcodeInfo[_instruction.opcode];
  1174. _instruction.numOperands = info.numOperands;
  1175. switch (info.numOperands)
  1176. {
  1177. case 6: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1178. case 5: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1179. case 4: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1180. case 3: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1181. case 2: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1182. case 1: size += read(_reader, _instruction.operand[currOp++], _err); BX_FALLTHROUGH;
  1183. case 0:
  1184. if (0 < info.numValues)
  1185. {
  1186. size += read(_reader, _instruction.value, info.numValues*sizeof(uint32_t), _err);
  1187. }
  1188. break;
  1189. default:
  1190. BX_CHECK(false, "Instruction %s with invalid number of operands %d (numValues %d)."
  1191. , getName(_instruction.opcode)
  1192. , info.numOperands
  1193. , info.numValues
  1194. );
  1195. break;
  1196. }
  1197. return size;
  1198. }
  1199. int32_t write(bx::WriterI* _writer, const DxbcInstruction& _instruction, bx::Error* _err)
  1200. {
  1201. uint32_t token = 0;
  1202. token |= (_instruction.opcode ) & UINT32_C(0x000007ff);
  1203. token |= (_instruction.length << 24) & UINT32_C(0x7f000000);
  1204. token |= DxbcInstruction::ExtendedType::Count != _instruction.extended[0]
  1205. ? UINT32_C(0x80000000)
  1206. : 0
  1207. ;
  1208. int32_t size =0;
  1209. switch (_instruction.opcode)
  1210. {
  1211. case DxbcOpcode::CUSTOMDATA:
  1212. {
  1213. token &= UINT32_C(0x000007ff);
  1214. token |= _instruction.customDataClass << 11;
  1215. size += bx::write(_writer, token);
  1216. uint32_t len = uint32_t(_instruction.customData.size()*sizeof(uint32_t) );
  1217. size += bx::write(_writer, len/4+2, _err);
  1218. size += bx::write(_writer, _instruction.customData.data(), len, _err);
  1219. }
  1220. return size;
  1221. case DxbcOpcode::DCL_CONSTANT_BUFFER:
  1222. token |= _instruction.allowRefactoring ? UINT32_C(0x00000800) : 0;
  1223. break;
  1224. case DxbcOpcode::DCL_GLOBAL_FLAGS:
  1225. token |= _instruction.allowRefactoring ? UINT32_C(0x00000800) : 0;
  1226. token |= _instruction.fp64 ? UINT32_C(0x00001000) : 0;
  1227. token |= _instruction.earlyDepth ? UINT32_C(0x00002000) : 0;
  1228. token |= _instruction.enableBuffers ? UINT32_C(0x00004000) : 0;
  1229. token |= _instruction.skipOptimization ? UINT32_C(0x00008000) : 0;
  1230. token |= _instruction.enableMinPrecision ? UINT32_C(0x00010000) : 0;
  1231. token |= _instruction.enableDoubleExtensions ? UINT32_C(0x00020000) : 0;
  1232. token |= _instruction.enableShaderExtensions ? UINT32_C(0x00040000) : 0;
  1233. break;
  1234. case DxbcOpcode::DCL_INPUT_PS:
  1235. token |= (_instruction.interpolation << 11) & UINT32_C(0x0000f800);
  1236. break;
  1237. case DxbcOpcode::DCL_RESOURCE:
  1238. token |= (_instruction.srv << 11) & UINT32_C(0x0000f800);
  1239. token |= (_instruction.samples << 16) & UINT32_C(0x007f0000);
  1240. break;
  1241. case DxbcOpcode::DCL_SAMPLER:
  1242. token |= _instruction.shadow ? (0x00000800) : 0;
  1243. token |= _instruction.mono ? (0x00001000) : 0;
  1244. break;
  1245. case DxbcOpcode::SYNC:
  1246. token |= _instruction.threadsInGroup ? UINT32_C(0x00000800) : 0;
  1247. token |= _instruction.sharedMemory ? UINT32_C(0x00001000) : 0;
  1248. token |= _instruction.uavGroup ? UINT32_C(0x00002000) : 0;
  1249. token |= _instruction.uavGlobal ? UINT32_C(0x00004000) : 0;
  1250. break;
  1251. default:
  1252. token |= (_instruction.retType << 11) & UINT32_C(0x00001800);
  1253. token |= _instruction.saturate ? UINT32_C(0x00002000) : 0;
  1254. token |= _instruction.testNZ ? UINT32_C(0x00040000) : 0;
  1255. // _instruction.precise = uint8_t( (token & UINT32_C(0x00780000) ) >> 19);
  1256. break;
  1257. }
  1258. size += bx::write(_writer, token);
  1259. for (uint32_t ii = 0; _instruction.extended[ii] != DxbcInstruction::ExtendedType::Count; ++ii)
  1260. {
  1261. // 0 1 2 3
  1262. // 76543210765432107654321076543210
  1263. // e..........................ttttt
  1264. // ^ ^
  1265. // | +----- type
  1266. // +-------------------------------- extended
  1267. token = _instruction.extended[ii+1] == DxbcInstruction::ExtendedType::Count
  1268. ? 0
  1269. : UINT32_C(0x80000000)
  1270. ;
  1271. token |= uint8_t(_instruction.extended[ii]);
  1272. switch (_instruction.extended[ii])
  1273. {
  1274. case DxbcInstruction::ExtendedType::SampleControls:
  1275. // 0 1 2 3
  1276. // 76543210765432107654321076543210
  1277. // . zzzzyyyyxxxx .....
  1278. // ^ ^ ^
  1279. // | | +------------- x
  1280. // | +----------------- y
  1281. // +--------------------- z
  1282. token |= (uint32_t(_instruction.sampleOffsets[0]) << 9) & UINT32_C(0x00001e00);
  1283. token |= (uint32_t(_instruction.sampleOffsets[1]) << 13) & UINT32_C(0x0001e000);
  1284. token |= (uint32_t(_instruction.sampleOffsets[2]) << 17) & UINT32_C(0x001e0000);
  1285. break;
  1286. case DxbcInstruction::ExtendedType::ResourceDim:
  1287. // 0 1 2 3
  1288. // 76543210765432107654321076543210
  1289. // . .....
  1290. //
  1291. token |= (uint32_t(_instruction.resourceTarget << 6) & UINT32_C(0x000003e0) );
  1292. token |= (uint32_t(_instruction.resourceStride << 11) & UINT32_C(0x0000f800) );
  1293. break;
  1294. case DxbcInstruction::ExtendedType::ResourceReturnType:
  1295. // 0 1 2 3
  1296. // 76543210765432107654321076543210
  1297. // . 3333222211110000.....
  1298. // ^ ^ ^
  1299. // | | +------------- x
  1300. // | +----------------- y
  1301. // +--------------------- z
  1302. token |= (uint32_t(_instruction.resourceReturnTypes[0]) << 6) & UINT32_C(0x000001e0);
  1303. token |= (uint32_t(_instruction.resourceReturnTypes[1]) << 9) & UINT32_C(0x00001e00);
  1304. token |= (uint32_t(_instruction.resourceReturnTypes[2]) << 13) & UINT32_C(0x0001e000);
  1305. token |= (uint32_t(_instruction.resourceReturnTypes[3]) << 17) & UINT32_C(0x001e0000);
  1306. break;
  1307. default:
  1308. break;
  1309. }
  1310. size += bx::write(_writer, token, _err);
  1311. }
  1312. for (uint32_t ii = 0; ii < _instruction.numOperands; ++ii)
  1313. {
  1314. size += write(_writer, _instruction.operand[ii], _err);
  1315. }
  1316. const DxbcOpcodeInfo& info = s_dxbcOpcodeInfo[_instruction.opcode];
  1317. if (0 < info.numValues)
  1318. {
  1319. size += bx::write(_writer, _instruction.value, info.numValues*sizeof(uint32_t), _err);
  1320. }
  1321. return size;
  1322. }
  1323. int32_t toString(char* _out, int32_t _size, DxbcOperandMode::Enum _mode, uint8_t _modeBits)
  1324. {
  1325. int32_t size = 0;
  1326. switch (_mode)
  1327. {
  1328. case DxbcOperandMode::Mask:
  1329. if (0xf > _modeBits
  1330. && 0 < _modeBits)
  1331. {
  1332. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1333. , ".%s%s%s%s"
  1334. , 0 == (_modeBits & 1) ? "" : "x"
  1335. , 0 == (_modeBits & 2) ? "" : "y"
  1336. , 0 == (_modeBits & 4) ? "" : "z"
  1337. , 0 == (_modeBits & 8) ? "" : "w"
  1338. );
  1339. }
  1340. break;
  1341. case DxbcOperandMode::Swizzle:
  1342. if (0xe4 != _modeBits)
  1343. {
  1344. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1345. , ".%c%c%c%c"
  1346. , "xyzw"[(_modeBits )&0x3]
  1347. , "xyzw"[(_modeBits>>2)&0x3]
  1348. , "xyzw"[(_modeBits>>4)&0x3]
  1349. , "xyzw"[(_modeBits>>6)&0x3]
  1350. );
  1351. }
  1352. break;
  1353. case DxbcOperandMode::Scalar:
  1354. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1355. , ".%c"
  1356. , "xyzw"[_modeBits]
  1357. );
  1358. break;
  1359. default:
  1360. break;
  1361. }
  1362. return size;
  1363. }
  1364. int32_t toString(char* _out, int32_t _size, const DxbcInstruction& _instruction)
  1365. {
  1366. int32_t size = 0;
  1367. switch (_instruction.opcode)
  1368. {
  1369. case DxbcOpcode::CUSTOMDATA:
  1370. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1371. , "%s"
  1372. , s_dxbcCustomDataClass[_instruction.customDataClass]
  1373. );
  1374. break;
  1375. case DxbcOpcode::IF:
  1376. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1377. , "%s%s"
  1378. , getName(_instruction.opcode)
  1379. , _instruction.testNZ ? "_nz" : "_z"
  1380. );
  1381. break;
  1382. default:
  1383. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1384. , "%s%s%s"
  1385. , getName(_instruction.opcode)
  1386. , _instruction.saturate ? "_sat" : ""
  1387. , _instruction.testNZ ? "_nz" : ""
  1388. );
  1389. break;
  1390. }
  1391. if (DxbcResourceDim::Unknown != _instruction.srv)
  1392. {
  1393. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1394. , " %s<%x>"
  1395. , s_dxbcSrvType[_instruction.srv]
  1396. , _instruction.value[0]
  1397. );
  1398. }
  1399. else if (0 < s_dxbcOpcodeInfo[_instruction.opcode].numValues)
  1400. {
  1401. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1402. , " %d"
  1403. , _instruction.value[0]
  1404. );
  1405. }
  1406. for (uint32_t ii = 0; ii < _instruction.numOperands; ++ii)
  1407. {
  1408. const DxbcOperand& operand = _instruction.operand[ii];
  1409. const bool array = false
  1410. || 1 < operand.numAddrModes
  1411. || DxbcOperandAddrMode::Imm32 != operand.addrMode[0]
  1412. ;
  1413. const char* preOperand = "";
  1414. const char* postOperand = "";
  1415. switch (operand.modifier)
  1416. {
  1417. case DxbcOperandModifier::Neg: preOperand = "-"; postOperand = ""; break;
  1418. case DxbcOperandModifier::Abs: preOperand = "abs("; postOperand = ")"; break;
  1419. case DxbcOperandModifier::AbsNeg: preOperand = "-abs("; postOperand = ")"; break;
  1420. default: break;
  1421. }
  1422. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1423. , "%s%s%s"
  1424. , 0 == ii ? " " : ", "
  1425. , preOperand
  1426. , s_dxbcOperandType[operand.type]
  1427. );
  1428. switch (operand.type)
  1429. {
  1430. case DxbcOperandType::Imm32:
  1431. case DxbcOperandType::Imm64:
  1432. for (uint32_t jj = 0; jj < operand.num; ++jj)
  1433. {
  1434. union { uint32_t i; float f; } cast = { operand.un.imm32[jj] };
  1435. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1436. , "%s%f"
  1437. , 0 == jj ? "(" : ", "
  1438. , cast.f
  1439. );
  1440. }
  1441. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1442. , ")"
  1443. );
  1444. break;
  1445. default:
  1446. break;
  1447. }
  1448. const uint32_t first = false
  1449. || DxbcOperandType::ImmConstantBuffer == operand.type
  1450. || DxbcOperandAddrMode::RegImm32 == operand.addrMode[0]
  1451. ? 0 : 1
  1452. ;
  1453. if (0 == first)
  1454. {
  1455. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1456. , "["
  1457. );
  1458. }
  1459. else
  1460. {
  1461. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1462. , "%d%s"
  1463. , operand.regIndex[0]
  1464. , array ? "[" : ""
  1465. );
  1466. }
  1467. for (uint32_t jj = first, num = bx::uint32_min(operand.numAddrModes, BX_COUNTOF(operand.addrMode) ); jj < num; ++jj)
  1468. {
  1469. switch (operand.addrMode[jj])
  1470. {
  1471. case DxbcOperandAddrMode::Imm32:
  1472. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1473. , "%d"
  1474. , operand.regIndex[jj]
  1475. );
  1476. break;
  1477. case DxbcOperandAddrMode::Reg:
  1478. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1479. , "%s%d"
  1480. , s_dxbcOperandType[operand.subOperand[jj].type]
  1481. , operand.subOperand[jj].regIndex
  1482. );
  1483. size += toString(&_out[size], bx::uint32_imax(0, _size-size)
  1484. , DxbcOperandMode::Enum(operand.subOperand[jj].mode)
  1485. , operand.subOperand[jj].modeBits
  1486. );
  1487. break;
  1488. case DxbcOperandAddrMode::RegImm32:
  1489. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1490. , "%d + %s%d"
  1491. , operand.regIndex[jj]
  1492. , s_dxbcOperandType[operand.subOperand[jj].type]
  1493. , operand.subOperand[jj].regIndex
  1494. );
  1495. size += toString(&_out[size], bx::uint32_imax(0, _size-size)
  1496. , DxbcOperandMode::Enum(operand.subOperand[jj].mode)
  1497. , operand.subOperand[jj].modeBits
  1498. );
  1499. break;
  1500. default:
  1501. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size), "???");
  1502. break;
  1503. }
  1504. }
  1505. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1506. , "%s"
  1507. , array ? "]" : ""
  1508. );
  1509. size += toString(&_out[size], bx::uint32_imax(0, _size-size), operand.mode, operand.modeBits);
  1510. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1511. , "%s"
  1512. , postOperand
  1513. );
  1514. }
  1515. if (_instruction.opcode == DxbcOpcode::DCL_CONSTANT_BUFFER
  1516. && _instruction.allowRefactoring)
  1517. {
  1518. size += bx::snprintf(&_out[size], bx::uint32_imax(0, _size-size)
  1519. , ", dynamicIndexed"
  1520. );
  1521. }
  1522. return size;
  1523. }
  1524. int32_t read(bx::ReaderSeekerI* _reader, DxbcSignature& _signature, bx::Error* _err)
  1525. {
  1526. int32_t size = 0;
  1527. int64_t offset = bx::seek(_reader);
  1528. uint32_t num;
  1529. size += bx::read(_reader, num, _err);
  1530. size += bx::read(_reader, _signature.key, _err);
  1531. for (uint32_t ii = 0; ii < num; ++ii)
  1532. {
  1533. DxbcSignature::Element element;
  1534. uint32_t nameOffset;
  1535. size += bx::read(_reader, nameOffset);
  1536. char name[DXBC_MAX_NAME_STRING];
  1537. readString(_reader, offset + nameOffset, name, DXBC_MAX_NAME_STRING, _err);
  1538. element.name = name;
  1539. size += bx::read(_reader, element.semanticIndex, _err);
  1540. size += bx::read(_reader, element.valueType, _err);
  1541. size += bx::read(_reader, element.componentType, _err);
  1542. size += bx::read(_reader, element.registerIndex, _err);
  1543. size += bx::read(_reader, element.mask, _err);
  1544. size += bx::read(_reader, element.readWriteMask, _err);
  1545. size += bx::read(_reader, element.stream, _err);
  1546. // padding
  1547. uint8_t padding;
  1548. size += bx::read(_reader, padding, _err);
  1549. _signature.elements.push_back(element);
  1550. }
  1551. return size;
  1552. }
  1553. int32_t write(bx::WriterI* _writer, const DxbcSignature& _signature, bx::Error* _err)
  1554. {
  1555. int32_t size = 0;
  1556. const uint32_t num = uint32_t(_signature.elements.size() );
  1557. size += bx::write(_writer, num, _err);
  1558. size += bx::write(_writer, _signature.key, _err);
  1559. typedef stl::unordered_map<stl::string, uint32_t> NameOffsetMap;
  1560. NameOffsetMap nom;
  1561. const uint8_t pad = 0;
  1562. uint32_t nameOffset = num * 24 + 8;
  1563. for (uint32_t ii = 0; ii < num; ++ii)
  1564. {
  1565. const DxbcSignature::Element& element = _signature.elements[ii];
  1566. NameOffsetMap::iterator it = nom.find(element.name);
  1567. if (it == nom.end() )
  1568. {
  1569. nom.insert(stl::make_pair(element.name, nameOffset) );
  1570. size += bx::write(_writer, nameOffset, _err);
  1571. nameOffset += uint32_t(element.name.size() + 1);
  1572. }
  1573. else
  1574. {
  1575. size += bx::write(_writer, it->second);
  1576. }
  1577. size += bx::write(_writer, element.semanticIndex, _err);
  1578. size += bx::write(_writer, element.valueType, _err);
  1579. size += bx::write(_writer, element.componentType, _err);
  1580. size += bx::write(_writer, element.registerIndex, _err);
  1581. size += bx::write(_writer, element.mask, _err);
  1582. size += bx::write(_writer, element.readWriteMask, _err);
  1583. size += bx::write(_writer, element.stream, _err);
  1584. size += bx::write(_writer, pad, _err);
  1585. }
  1586. uint32_t len = 0;
  1587. for (uint32_t ii = 0; ii < num; ++ii)
  1588. {
  1589. const DxbcSignature::Element& element = _signature.elements[ii];
  1590. NameOffsetMap::iterator it = nom.find(element.name);
  1591. if (it != nom.end() )
  1592. {
  1593. nom.erase(it);
  1594. size += bx::write(_writer, element.name.c_str(), uint32_t(element.name.size() + 1), _err);
  1595. len += uint32_t(element.name.size() + 1);
  1596. }
  1597. }
  1598. // align 4 bytes
  1599. size += bx::writeRep(_writer, 0xab, (len+3)/4*4 - len, _err);
  1600. return size;
  1601. }
  1602. int32_t read(bx::ReaderSeekerI* _reader, DxbcShader& _shader, bx::Error* _err)
  1603. {
  1604. int32_t size = 0;
  1605. size += bx::read(_reader, _shader.version, _err);
  1606. uint32_t bcLength;
  1607. size += bx::read(_reader, bcLength, _err);
  1608. uint32_t len = (bcLength-2)*sizeof(uint32_t);
  1609. _shader.byteCode.resize(len);
  1610. size += bx::read(_reader, _shader.byteCode.data(), len, _err);
  1611. return size;
  1612. }
  1613. int32_t write(bx::WriterI* _writer, const DxbcShader& _shader, bx::Error* _err)
  1614. {
  1615. const uint32_t len = uint32_t(_shader.byteCode.size() );
  1616. const uint32_t bcLength = len / sizeof(uint32_t) + 2;
  1617. int32_t size = 0;
  1618. size += bx::write(_writer, _shader.version, _err);
  1619. size += bx::write(_writer, bcLength, _err);
  1620. size += bx::write(_writer, _shader.byteCode.data(), len, _err);
  1621. return size;
  1622. }
  1623. #define DXBC_CHUNK_SHADER BX_MAKEFOURCC('S', 'H', 'D', 'R')
  1624. #define DXBC_CHUNK_SHADER_EX BX_MAKEFOURCC('S', 'H', 'E', 'X')
  1625. #define DXBC_CHUNK_INPUT_SIGNATURE BX_MAKEFOURCC('I', 'S', 'G', 'N')
  1626. #define DXBC_CHUNK_OUTPUT_SIGNATURE BX_MAKEFOURCC('O', 'S', 'G', 'N')
  1627. int32_t read(bx::ReaderSeekerI* _reader, DxbcContext& _dxbc, bx::Error* _err)
  1628. {
  1629. int32_t size = 0;
  1630. size += bx::read(_reader, _dxbc.header, _err);
  1631. _dxbc.shader.shex = false;
  1632. _dxbc.shader.aon9 = false;
  1633. for (uint32_t ii = 0; ii < _dxbc.header.numChunks; ++ii)
  1634. {
  1635. bx::seek(_reader, sizeof(DxbcContext::Header) + ii*sizeof(uint32_t), bx::Whence::Begin);
  1636. uint32_t chunkOffset;
  1637. size += bx::read(_reader, chunkOffset, _err);
  1638. bx::seek(_reader, chunkOffset, bx::Whence::Begin);
  1639. uint32_t fourcc;
  1640. size += bx::read(_reader, fourcc, _err);
  1641. uint32_t chunkSize;
  1642. size += bx::read(_reader, chunkSize, _err);
  1643. switch (fourcc)
  1644. {
  1645. case DXBC_CHUNK_SHADER_EX:
  1646. _dxbc.shader.shex = true;
  1647. BX_FALLTHROUGH;
  1648. case DXBC_CHUNK_SHADER:
  1649. size += read(_reader, _dxbc.shader, _err);
  1650. break;
  1651. case BX_MAKEFOURCC('I', 'S', 'G', '1'):
  1652. case DXBC_CHUNK_INPUT_SIGNATURE:
  1653. size += read(_reader, _dxbc.inputSignature, _err);
  1654. break;
  1655. case BX_MAKEFOURCC('O', 'S', 'G', '1'):
  1656. case BX_MAKEFOURCC('O', 'S', 'G', '5'):
  1657. case DXBC_CHUNK_OUTPUT_SIGNATURE:
  1658. size += read(_reader, _dxbc.outputSignature, _err);
  1659. break;
  1660. case BX_MAKEFOURCC('A', 'o', 'n', '9'): // Contains DX9BC for feature level 9.x (*s_4_0_level_9_*) shaders.
  1661. _dxbc.shader.aon9 = true;
  1662. break;
  1663. case BX_MAKEFOURCC('I', 'F', 'C', 'E'): // Interface.
  1664. case BX_MAKEFOURCC('R', 'D', 'E', 'F'): // Resource definition.
  1665. case BX_MAKEFOURCC('S', 'D', 'G', 'B'): // Shader debugging info (old).
  1666. case BX_MAKEFOURCC('S', 'P', 'D', 'B'): // Shader debugging info (new).
  1667. case BX_MAKEFOURCC('S', 'F', 'I', '0'): // ?
  1668. case BX_MAKEFOURCC('S', 'T', 'A', 'T'): // Statistics.
  1669. case BX_MAKEFOURCC('P', 'C', 'S', 'G'): // Patch constant signature.
  1670. case BX_MAKEFOURCC('P', 'S', 'O', '1'): // Pipeline State Object 1
  1671. case BX_MAKEFOURCC('P', 'S', 'O', '2'): // Pipeline State Object 2
  1672. case BX_MAKEFOURCC('X', 'N', 'A', 'P'): // ?
  1673. case BX_MAKEFOURCC('X', 'N', 'A', 'S'): // ?
  1674. size += chunkSize;
  1675. break;
  1676. default:
  1677. size += chunkSize;
  1678. BX_CHECK(false, "UNKNOWN FOURCC %c%c%c%c %d"
  1679. , ( (char*)&fourcc)[0]
  1680. , ( (char*)&fourcc)[1]
  1681. , ( (char*)&fourcc)[2]
  1682. , ( (char*)&fourcc)[3]
  1683. , size
  1684. );
  1685. break;
  1686. }
  1687. }
  1688. return size;
  1689. }
  1690. int32_t write(bx::WriterSeekerI* _writer, const DxbcContext& _dxbc, bx::Error* _err)
  1691. {
  1692. int32_t size = 0;
  1693. int64_t dxbcOffset = bx::seek(_writer);
  1694. size += bx::write(_writer, DXBC_CHUNK_HEADER);
  1695. size += bx::writeRep(_writer, 0, 16, _err);
  1696. size += bx::write(_writer, UINT32_C(1), _err);
  1697. int64_t sizeOffset = bx::seek(_writer);
  1698. size += bx::writeRep(_writer, 0, 4, _err);
  1699. uint32_t numChunks = 3;
  1700. size += bx::write(_writer, numChunks, _err);
  1701. int64_t chunksOffsets = bx::seek(_writer);
  1702. size += bx::writeRep(_writer, 0, numChunks*sizeof(uint32_t), _err);
  1703. uint32_t chunkOffset[3];
  1704. uint32_t chunkSize[3];
  1705. chunkOffset[0] = uint32_t(bx::seek(_writer) - dxbcOffset);
  1706. size += write(_writer, DXBC_CHUNK_INPUT_SIGNATURE, _err);
  1707. size += write(_writer, UINT32_C(0), _err);
  1708. chunkSize[0] = write(_writer, _dxbc.inputSignature, _err);
  1709. chunkOffset[1] = uint32_t(bx::seek(_writer) - dxbcOffset);
  1710. size += write(_writer, DXBC_CHUNK_OUTPUT_SIGNATURE, _err);
  1711. size += write(_writer, UINT32_C(0), _err);
  1712. chunkSize[1] = write(_writer, _dxbc.outputSignature, _err);
  1713. chunkOffset[2] = uint32_t(bx::seek(_writer) - dxbcOffset);
  1714. size += write(_writer, _dxbc.shader.shex ? DXBC_CHUNK_SHADER_EX : DXBC_CHUNK_SHADER, _err);
  1715. size += write(_writer, UINT32_C(0), _err);
  1716. chunkSize[2] = write(_writer, _dxbc.shader, _err);
  1717. size += 0
  1718. + chunkSize[0]
  1719. + chunkSize[1]
  1720. + chunkSize[2]
  1721. ;
  1722. int64_t eof = bx::seek(_writer);
  1723. bx::seek(_writer, sizeOffset, bx::Whence::Begin);
  1724. bx::write(_writer, size, _err);
  1725. bx::seek(_writer, chunksOffsets, bx::Whence::Begin);
  1726. bx::write(_writer, chunkOffset, sizeof(chunkOffset), _err);
  1727. for (uint32_t ii = 0; ii < BX_COUNTOF(chunkOffset); ++ii)
  1728. {
  1729. bx::seek(_writer, chunkOffset[ii]+4, bx::Whence::Begin);
  1730. bx::write(_writer, chunkSize[ii], _err);
  1731. }
  1732. bx::seek(_writer, eof, bx::Whence::Begin);
  1733. return size;
  1734. }
  1735. void parse(const DxbcShader& _src, DxbcParseFn _fn, void* _userData, bx::Error* _err)
  1736. {
  1737. BX_ERROR_SCOPE(_err);
  1738. bx::MemoryReader reader(_src.byteCode.data(), uint32_t(_src.byteCode.size() ) );
  1739. for (uint32_t token = 0, numTokens = uint32_t(_src.byteCode.size() / sizeof(uint32_t) ); token < numTokens;)
  1740. {
  1741. DxbcInstruction instruction;
  1742. uint32_t size = read(&reader, instruction, _err);
  1743. BX_CHECK(size/4 == instruction.length, "read %d, expected %d", size/4, instruction.length); BX_UNUSED(size);
  1744. bool cont = _fn(token * sizeof(uint32_t), instruction, _userData);
  1745. if (!cont)
  1746. {
  1747. return;
  1748. }
  1749. token += instruction.length;
  1750. }
  1751. }
  1752. void filter(DxbcShader& _dst, const DxbcShader& _src, DxbcFilterFn _fn, void* _userData, bx::Error* _err)
  1753. {
  1754. BX_ERROR_SCOPE(_err);
  1755. bx::MemoryReader reader(_src.byteCode.data(), uint32_t(_src.byteCode.size() ) );
  1756. bx::MemoryBlock mb(g_allocator);
  1757. bx::MemoryWriter writer(&mb);
  1758. int32_t total = 0;
  1759. for (uint32_t token = 0, numTokens = uint32_t(_src.byteCode.size() / sizeof(uint32_t) ); token < numTokens;)
  1760. {
  1761. DxbcInstruction instruction;
  1762. uint32_t size = read(&reader, instruction, _err);
  1763. BX_CHECK(size/4 == instruction.length, "read %d, expected %d", size/4, instruction.length); BX_UNUSED(size);
  1764. _fn(instruction, _userData);
  1765. bx::SizerWriter sw;
  1766. uint32_t length = instruction.length;
  1767. instruction.length = uint32_t(write(&sw, instruction, _err)/4);
  1768. total += write(&writer, instruction, _err);
  1769. token += length;
  1770. }
  1771. uint8_t* data = (uint8_t*)mb.more();
  1772. _dst.byteCode.resize(total);
  1773. bx::memCopy(_dst.byteCode.data(), data, total);
  1774. }
  1775. } // namespace bgfx