ShaderBinary.h 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // ShaderBinary.h //
  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. #pragma once
  12. //has dependencies on D3D10TokenizedProgramFormat.hpp! make sure to include that too!
  13. typedef UINT CShaderToken;
  14. //*****************************************************************************
  15. //
  16. // GetNumVertices
  17. //
  18. // Returns the number of vertices in a complete primitive
  19. //
  20. //*****************************************************************************
  21. inline UINT GetNumVertices( D3D10_SB_PRIMITIVE PrimType )
  22. {
  23. switch( PrimType )
  24. {
  25. case D3D10_SB_PRIMITIVE_POINT: return 1;
  26. case D3D10_SB_PRIMITIVE_LINE: return 2;
  27. case D3D10_SB_PRIMITIVE_TRIANGLE: return 3;
  28. case D3D10_SB_PRIMITIVE_LINE_ADJ: return 4;
  29. case D3D10_SB_PRIMITIVE_TRIANGLE_ADJ: return 6;
  30. case D3D11_SB_PRIMITIVE_1_CONTROL_POINT_PATCH: return 1;
  31. case D3D11_SB_PRIMITIVE_2_CONTROL_POINT_PATCH: return 2;
  32. case D3D11_SB_PRIMITIVE_3_CONTROL_POINT_PATCH: return 3;
  33. case D3D11_SB_PRIMITIVE_4_CONTROL_POINT_PATCH: return 4;
  34. case D3D11_SB_PRIMITIVE_5_CONTROL_POINT_PATCH: return 5;
  35. case D3D11_SB_PRIMITIVE_6_CONTROL_POINT_PATCH: return 6;
  36. case D3D11_SB_PRIMITIVE_7_CONTROL_POINT_PATCH: return 7;
  37. case D3D11_SB_PRIMITIVE_8_CONTROL_POINT_PATCH: return 8;
  38. case D3D11_SB_PRIMITIVE_9_CONTROL_POINT_PATCH: return 9;
  39. case D3D11_SB_PRIMITIVE_10_CONTROL_POINT_PATCH: return 10;
  40. case D3D11_SB_PRIMITIVE_11_CONTROL_POINT_PATCH: return 11;
  41. case D3D11_SB_PRIMITIVE_12_CONTROL_POINT_PATCH: return 12;
  42. case D3D11_SB_PRIMITIVE_13_CONTROL_POINT_PATCH: return 13;
  43. case D3D11_SB_PRIMITIVE_14_CONTROL_POINT_PATCH: return 14;
  44. case D3D11_SB_PRIMITIVE_15_CONTROL_POINT_PATCH: return 15;
  45. case D3D11_SB_PRIMITIVE_16_CONTROL_POINT_PATCH: return 16;
  46. case D3D11_SB_PRIMITIVE_17_CONTROL_POINT_PATCH: return 17;
  47. case D3D11_SB_PRIMITIVE_18_CONTROL_POINT_PATCH: return 18;
  48. case D3D11_SB_PRIMITIVE_19_CONTROL_POINT_PATCH: return 19;
  49. case D3D11_SB_PRIMITIVE_20_CONTROL_POINT_PATCH: return 20;
  50. case D3D11_SB_PRIMITIVE_21_CONTROL_POINT_PATCH: return 21;
  51. case D3D11_SB_PRIMITIVE_22_CONTROL_POINT_PATCH: return 22;
  52. case D3D11_SB_PRIMITIVE_23_CONTROL_POINT_PATCH: return 23;
  53. case D3D11_SB_PRIMITIVE_24_CONTROL_POINT_PATCH: return 24;
  54. case D3D11_SB_PRIMITIVE_25_CONTROL_POINT_PATCH: return 25;
  55. case D3D11_SB_PRIMITIVE_26_CONTROL_POINT_PATCH: return 26;
  56. case D3D11_SB_PRIMITIVE_27_CONTROL_POINT_PATCH: return 27;
  57. case D3D11_SB_PRIMITIVE_28_CONTROL_POINT_PATCH: return 28;
  58. case D3D11_SB_PRIMITIVE_29_CONTROL_POINT_PATCH: return 29;
  59. case D3D11_SB_PRIMITIVE_30_CONTROL_POINT_PATCH: return 30;
  60. case D3D11_SB_PRIMITIVE_31_CONTROL_POINT_PATCH: return 31;
  61. case D3D11_SB_PRIMITIVE_32_CONTROL_POINT_PATCH: return 32;
  62. default: return 0;
  63. }
  64. }
  65. /*==========================================================================;
  66. *
  67. * D3D10ShaderBinary namespace
  68. *
  69. * File: ShaderBinary.h
  70. * Content: Vertex shader assembler support
  71. *
  72. ***************************************************************************/
  73. namespace D3D10ShaderBinary
  74. {
  75. const UINT MAX_INSTRUCTION_LENGTH = 128;
  76. const UINT D3D10_SB_MAX_INSTRUCTION_OPERANDS = 8;
  77. const UINT D3D11_SB_MAX_CALL_OPERANDS = 0x10000;
  78. const UINT D3D11_SB_MAX_NUM_TYPES = 0x10000;
  79. typedef enum D3D10_SB_OPCODE_CLASS
  80. {
  81. D3D10_SB_FLOAT_OP,
  82. D3D10_SB_INT_OP,
  83. D3D10_SB_UINT_OP,
  84. D3D10_SB_BIT_OP,
  85. D3D10_SB_FLOW_OP,
  86. D3D10_SB_TEX_OP,
  87. D3D10_SB_DCL_OP,
  88. D3D11_SB_ATOMIC_OP,
  89. D3D11_SB_MEM_OP,
  90. D3D11_SB_DOUBLE_OP,
  91. D3D11_SB_FLOAT_TO_DOUBLE_OP,
  92. D3D11_SB_DOUBLE_TO_FLOAT_OP,
  93. D3D11_SB_DEBUG_OP,
  94. } D3D10_SB_OPCODE_CLASS;
  95. struct CInstructionInfo
  96. {
  97. void Set (BYTE NumOperands,
  98. LPCSTR Name,
  99. D3D10_SB_OPCODE_CLASS OpClass,
  100. BYTE InPrecisionFromOutMask)
  101. {
  102. m_NumOperands = NumOperands;
  103. m_InPrecisionFromOutMask = InPrecisionFromOutMask;
  104. StringCchCopyA(m_Name, sizeof(m_Name), Name);
  105. m_OpClass = OpClass;
  106. }
  107. char m_Name[64];
  108. BYTE m_NumOperands;
  109. BYTE m_InPrecisionFromOutMask;
  110. D3D10_SB_OPCODE_CLASS m_OpClass;
  111. };
  112. extern CInstructionInfo g_InstructionInfo[D3D10_SB_NUM_OPCODES];
  113. UINT GetNumInstructionOperands(D3D10_SB_OPCODE_TYPE OpCode);
  114. void InitInstructionInfo();
  115. //*****************************************************************************
  116. //
  117. // class COperandIndex
  118. //
  119. // Represents a dimension index of an operand
  120. //
  121. //*****************************************************************************
  122. class COperandIndex
  123. {
  124. public:
  125. COperandIndex() : m_bExtendedOperand(FALSE) {}
  126. // Value for the immediate index type
  127. union
  128. {
  129. UINT m_RegIndex;
  130. UINT m_RegIndexA[2];
  131. INT64 m_RegIndex64;
  132. };
  133. // Data for the relative index type
  134. D3D10_SB_OPERAND_TYPE m_RelRegType;
  135. D3D10_SB_4_COMPONENT_NAME m_ComponentName;
  136. D3D10_SB_OPERAND_INDEX_DIMENSION m_IndexDimension;
  137. BOOL m_bExtendedOperand;
  138. D3D11_SB_OPERAND_MIN_PRECISION m_MinPrecision;
  139. BOOL m_Nonuniform;
  140. D3D10_SB_EXTENDED_OPERAND_TYPE m_ExtendedOperandType;
  141. // First index of the relative register
  142. union
  143. {
  144. UINT m_RelIndex;
  145. UINT m_RelIndexA[2];
  146. INT64 m_RelIndex64;
  147. };
  148. // Second index of the relative register
  149. union
  150. {
  151. UINT m_RelIndex1;
  152. UINT m_RelIndexA1[2];
  153. INT64 m_RelIndex641;
  154. };
  155. void SetMinPrecision(D3D11_SB_OPERAND_MIN_PRECISION MinPrec)
  156. {
  157. m_MinPrecision = MinPrec;
  158. if( MinPrec != D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  159. {
  160. m_bExtendedOperand = true;
  161. m_ExtendedOperandType = D3D10_SB_EXTENDED_OPERAND_MODIFIER; // piggybacking on modifier token for minprecision
  162. }
  163. }
  164. void SetNonuniformIndex(bool bNonuniform = false)
  165. {
  166. m_Nonuniform = bNonuniform;
  167. if(bNonuniform)
  168. {
  169. m_bExtendedOperand = true;
  170. m_ExtendedOperandType = D3D10_SB_EXTENDED_OPERAND_MODIFIER;
  171. }
  172. }
  173. };
  174. enum MinPrecQuantizeFunctionIndex // Used by reference rasterizer (IHVs can ignore)
  175. {
  176. MinPrecFuncDefault = 0,
  177. MinPrecFunc2_8,
  178. MinPrecFunc16,
  179. MinPrecFuncUint16,
  180. MinPrecFuncInt16,
  181. };
  182. //*****************************************************************************
  183. //
  184. // class COperandBase
  185. //
  186. // A base class for shader instruction operands
  187. //
  188. //*****************************************************************************
  189. class COperandBase
  190. {
  191. public:
  192. COperandBase() {Clear();}
  193. COperandBase(const COperandBase & Op) { memcpy(this, &Op, sizeof(*this)); }
  194. D3D10_SB_OPERAND_TYPE OperandType() const {return m_Type;}
  195. const COperandIndex* OperandIndex(UINT Index) const {return &m_Index[Index];}
  196. D3D10_SB_OPERAND_INDEX_REPRESENTATION OperandIndexType(UINT Index) const {return m_IndexType[Index];}
  197. D3D10_SB_OPERAND_INDEX_DIMENSION OperandIndexDimension() const {return m_IndexDimension;}
  198. D3D10_SB_OPERAND_NUM_COMPONENTS NumComponents() const {return m_NumComponents;}
  199. // Get the register index for a given dimension
  200. UINT RegIndex(UINT Dimension = 0) const {return m_Index[Dimension].m_RegIndex;}
  201. // Get the register index from the lowest dimension
  202. UINT RegIndexForMinorDimension() const
  203. {
  204. switch (m_IndexDimension)
  205. {
  206. default:
  207. case D3D10_SB_OPERAND_INDEX_1D:
  208. return RegIndex(0);
  209. case D3D10_SB_OPERAND_INDEX_2D:
  210. return RegIndex(1);
  211. case D3D10_SB_OPERAND_INDEX_3D:
  212. return RegIndex(2);
  213. }
  214. }
  215. // Get the write mask
  216. UINT WriteMask() const {return m_WriteMask;}
  217. // Get the swizzle
  218. UINT SwizzleComponent(UINT index) const {return m_Swizzle[index];}
  219. // Get immediate 32 bit value
  220. UINT Imm32() const {return m_Value[0];}
  221. void SetModifier(D3D10_SB_OPERAND_MODIFIER Modifier)
  222. {
  223. m_Modifier = Modifier;
  224. if (Modifier != D3D10_SB_OPERAND_MODIFIER_NONE)
  225. {
  226. m_bExtendedOperand = true;
  227. m_ExtendedOperandType = D3D10_SB_EXTENDED_OPERAND_MODIFIER;
  228. }
  229. }
  230. void SetMinPrecision(D3D11_SB_OPERAND_MIN_PRECISION MinPrec)
  231. {
  232. m_MinPrecision = MinPrec;
  233. if( m_MinPrecision != D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  234. {
  235. m_bExtendedOperand = true;
  236. m_ExtendedOperandType = D3D10_SB_EXTENDED_OPERAND_MODIFIER; // reusing same extended operand token as modifiers.
  237. }
  238. }
  239. void SetNonuniform(bool bNonuniform = false)
  240. {
  241. m_Nonuniform = bNonuniform;
  242. if(bNonuniform)
  243. {
  244. m_bExtendedOperand = true;
  245. m_ExtendedOperandType = D3D10_SB_EXTENDED_OPERAND_MODIFIER;
  246. }
  247. }
  248. D3D10_SB_OPERAND_MODIFIER Modifier() const {return m_Modifier;}
  249. void SetSwizzle(BYTE SwizzleX=D3D10_SB_4_COMPONENT_X,
  250. BYTE SwizzleY=D3D10_SB_4_COMPONENT_Y,
  251. BYTE SwizzleZ=D3D10_SB_4_COMPONENT_Z,
  252. BYTE SwizzleW=D3D10_SB_4_COMPONENT_W)
  253. {
  254. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_SWIZZLE_MODE;
  255. m_Swizzle[0] = SwizzleX;
  256. m_Swizzle[1] = SwizzleY;
  257. m_Swizzle[2] = SwizzleZ;
  258. m_Swizzle[3] = SwizzleW;
  259. }
  260. void SelectComponent(D3D10_SB_4_COMPONENT_NAME ComponentName=D3D10_SB_4_COMPONENT_X)
  261. {
  262. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE;
  263. m_ComponentName = ComponentName;
  264. }
  265. void SetMask(UINT Mask=D3D10_SB_OPERAND_4_COMPONENT_MASK_ALL)
  266. {
  267. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE;
  268. m_WriteMask = Mask;
  269. }
  270. void SetIndex(UINT Dim, UINT Imm32)
  271. {
  272. m_IndexType[Dim] = D3D10_SB_OPERAND_INDEX_IMMEDIATE32;
  273. m_Index[Dim].m_RegIndex = Imm32;
  274. }
  275. void SetIndex( UINT Dim,
  276. UINT Offset,
  277. D3D10_SB_OPERAND_TYPE RelRegType,
  278. UINT RelRegIndex0,
  279. UINT RelRegIndex1,
  280. D3D10_SB_4_COMPONENT_NAME RelComponentName,
  281. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  282. {
  283. m_IndexType[Dim] = D3D10_SB_OPERAND_INDEX_IMMEDIATE32;
  284. if (Offset == 0)
  285. m_IndexType[Dim] = D3D10_SB_OPERAND_INDEX_RELATIVE;
  286. else
  287. m_IndexType[Dim] = D3D10_SB_OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE;
  288. m_Index[Dim].m_RegIndex = Offset; // immediate offset, such as the 3 in cb0[x1[2].x + 3] or cb0[r1.x + 3]
  289. m_Index[Dim].m_RelRegType = RelRegType;
  290. if( RelRegType == D3D10_SB_OPERAND_TYPE_INDEXABLE_TEMP )
  291. m_Index[Dim].m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  292. else
  293. m_Index[Dim].m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  294. m_Index[Dim].m_RelIndex = RelRegIndex0; // relative register index, such as the 1 in cb0[x1[2].x + 3] or cb0[r1.x + 3]
  295. m_Index[Dim].m_RelIndex1 = RelRegIndex1; // relative register second dimension index, such as the 2 in cb0[x1[2].x + 3]
  296. m_Index[Dim].m_ComponentName = RelComponentName;
  297. m_Index[Dim].SetMinPrecision(RelRegMinPrecision);
  298. }
  299. public: //esp in the unions...it's just redundant to not directly access things
  300. void Clear()
  301. {
  302. memset(this, 0, sizeof(*this));
  303. }
  304. MinPrecQuantizeFunctionIndex m_MinPrecQuantizeFunctionIndex; // used by ref for low precision (IHVs can ignore)
  305. D3D10_SB_OPERAND_TYPE m_Type;
  306. COperandIndex m_Index[3];
  307. D3D10_SB_OPERAND_NUM_COMPONENTS m_NumComponents;
  308. D3D10_SB_OPERAND_4_COMPONENT_SELECTION_MODE m_ComponentSelection;
  309. BOOL m_bExtendedOperand;
  310. D3D10_SB_OPERAND_MODIFIER m_Modifier;
  311. D3D11_SB_OPERAND_MIN_PRECISION m_MinPrecision;
  312. BOOL m_Nonuniform;
  313. D3D10_SB_EXTENDED_OPERAND_TYPE m_ExtendedOperandType;
  314. union
  315. {
  316. UINT m_WriteMask;
  317. BYTE m_Swizzle[4];
  318. };
  319. D3D10_SB_4_COMPONENT_NAME m_ComponentName;
  320. union
  321. {
  322. UINT m_Value[4];
  323. float m_Valuef[4];
  324. INT64 m_Value64[2];
  325. double m_Valued[2];
  326. };
  327. struct
  328. {
  329. D3D10_SB_OPERAND_INDEX_REPRESENTATION m_IndexType[3];
  330. D3D10_SB_OPERAND_INDEX_DIMENSION m_IndexDimension;
  331. #pragma warning(suppress: 4201) // Warning about nameless structure.
  332. };
  333. friend class CShaderAsm;
  334. friend class CShaderCodeParser;
  335. friend class CInstruction;
  336. friend class COperand;
  337. friend class COperandDst;
  338. };
  339. //*****************************************************************************
  340. //
  341. // class COperand
  342. //
  343. // Encapsulates a source operand in shader instructions
  344. //
  345. //*****************************************************************************
  346. class COperand: public COperandBase
  347. {
  348. public:
  349. COperand(): COperandBase() {}
  350. COperand(UINT Imm32): COperandBase()
  351. {
  352. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  353. m_WriteMask = 0;
  354. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  355. m_bExtendedOperand = FALSE;
  356. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  357. m_Value[0] = Imm32;
  358. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  359. }
  360. COperand(int Imm32): COperandBase()
  361. {
  362. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  363. m_WriteMask = 0;
  364. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  365. m_bExtendedOperand = FALSE;
  366. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  367. m_Value[0] = Imm32;
  368. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  369. }
  370. COperand(float Imm32): COperandBase()
  371. {
  372. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  373. m_WriteMask = 0;
  374. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  375. m_bExtendedOperand = FALSE;
  376. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  377. m_Valuef[0] = Imm32;
  378. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  379. }
  380. COperand(INT64 Imm64): COperandBase()
  381. {
  382. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  383. m_WriteMask = 0;
  384. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE64;
  385. m_bExtendedOperand = FALSE;
  386. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  387. m_Value64[0] = Imm64;
  388. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  389. }
  390. COperand(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  391. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  392. : COperandBase()
  393. {
  394. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  395. m_Type = Type;
  396. SetMinPrecision(MinPrecision);
  397. m_NumComponents = D3D10_SB_OPERAND_0_COMPONENT;
  398. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  399. SetIndex(0, RegIndex);
  400. }
  401. // Immediate constant
  402. COperand(float v1, float v2, float v3, float v4): COperandBase()
  403. {
  404. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  405. SetSwizzle();
  406. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  407. m_bExtendedOperand = FALSE;
  408. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  409. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  410. m_Valuef[0] = v1;
  411. m_Valuef[1] = v2;
  412. m_Valuef[2] = v3;
  413. m_Valuef[3] = v4;
  414. }
  415. // Immediate constant
  416. COperand(double v1, double v2): COperandBase()
  417. {
  418. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  419. SetSwizzle();
  420. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE64;
  421. m_bExtendedOperand = FALSE;
  422. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  423. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  424. m_Valued[0] = v1;
  425. m_Valued[1] = v2;
  426. }
  427. // Immediate constant
  428. COperand(float v1, float v2, float v3, float v4,
  429. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW): COperandBase()
  430. {
  431. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  432. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  433. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  434. m_bExtendedOperand = FALSE;
  435. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  436. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  437. m_Valuef[0] = v1;
  438. m_Valuef[1] = v2;
  439. m_Valuef[2] = v3;
  440. m_Valuef[3] = v4;
  441. }
  442. // Immediate constant
  443. COperand(int v1, int v2, int v3, int v4): COperandBase()
  444. {
  445. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  446. SetSwizzle();
  447. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  448. m_bExtendedOperand = FALSE;
  449. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  450. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  451. m_Value[0] = v1;
  452. m_Value[1] = v2;
  453. m_Value[2] = v3;
  454. m_Value[3] = v4;
  455. }
  456. // Immediate constant
  457. COperand(int v1, int v2, int v3, int v4,
  458. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW): COperandBase()
  459. {
  460. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  461. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  462. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE32;
  463. m_bExtendedOperand = FALSE;
  464. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  465. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  466. m_Value[0] = v1;
  467. m_Value[1] = v2;
  468. m_Value[2] = v3;
  469. m_Value[3] = v4;
  470. }
  471. COperand(INT64 v1, INT64 v2): COperandBase()
  472. {
  473. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  474. SetSwizzle();
  475. m_Type = D3D10_SB_OPERAND_TYPE_IMMEDIATE64;
  476. m_bExtendedOperand = FALSE;
  477. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  478. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  479. m_Value64[0] = v1;
  480. m_Value64[1] = v2;
  481. }
  482. COperand(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  483. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW,
  484. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  485. {
  486. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  487. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  488. m_Type = Type;
  489. m_bExtendedOperand = FALSE;
  490. SetMinPrecision(MinPrecision);
  491. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  492. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  493. SetIndex(0, RegIndex);
  494. }
  495. // Used for operands without indices
  496. COperand(D3D10_SB_OPERAND_TYPE Type,
  497. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  498. {
  499. m_Type = Type;
  500. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  501. m_bExtendedOperand = FALSE;
  502. SetMinPrecision(MinPrecision);
  503. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  504. if( (Type == D3D10_SB_OPERAND_TYPE_INPUT_PRIMITIVEID) ||
  505. (Type == D3D11_SB_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID) ||
  506. (Type == D3D11_SB_OPERAND_TYPE_INPUT_COVERAGE_MASK) ||
  507. (Type == D3D11_SB_OPERAND_TYPE_INNER_COVERAGE) ||
  508. (Type == D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP_FLATTENED) ||
  509. (Type == D3D11_SB_OPERAND_TYPE_INPUT_GS_INSTANCE_ID) )
  510. {
  511. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  512. }
  513. else if( (Type == D3D11_SB_OPERAND_TYPE_INPUT_DOMAIN_POINT) ||
  514. (Type == D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID) ||
  515. (Type == D3D11_SB_OPERAND_TYPE_INPUT_THREAD_GROUP_ID) ||
  516. (Type == D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP) ||
  517. (Type == D3D11_SB_OPERAND_TYPE_CYCLE_COUNTER) )
  518. {
  519. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  520. }
  521. else
  522. {
  523. m_NumComponents = D3D10_SB_OPERAND_0_COMPONENT;
  524. }
  525. }
  526. // source operand with relative addressing
  527. COperand(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  528. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, D3D10_SB_4_COMPONENT_NAME RelComponentName,
  529. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  530. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  531. {
  532. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  533. m_Type = Type;
  534. m_bExtendedOperand = FALSE;
  535. SetMinPrecision(MinPrecision);
  536. m_NumComponents = D3D10_SB_OPERAND_0_COMPONENT;
  537. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  538. SetIndex(0, RegIndex, RelRegType, RelRegIndex, 0xFFFFFFFF, RelComponentName, RelRegMinPrecision);
  539. }
  540. friend class CShaderAsm;
  541. friend class CShaderCodeParser;
  542. friend class CInstruction;
  543. };
  544. //*****************************************************************************
  545. //
  546. // class COperand4
  547. //
  548. // Encapsulates a source operand with 4 components in shader instructions
  549. //
  550. //*****************************************************************************
  551. class COperand4: public COperandBase
  552. {
  553. public:
  554. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  555. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  556. {
  557. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  558. SetSwizzle();
  559. m_Type = Type;
  560. m_bExtendedOperand = FALSE;
  561. SetMinPrecision(MinPrecision);
  562. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  563. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  564. SetIndex(0, RegIndex);
  565. }
  566. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex, D3D10_SB_4_COMPONENT_NAME Component,
  567. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  568. {
  569. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  570. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE;
  571. m_ComponentName = Component;
  572. m_Type = Type;
  573. m_bExtendedOperand = FALSE;
  574. SetMinPrecision(MinPrecision);
  575. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  576. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  577. SetIndex(0, RegIndex);
  578. }
  579. // single component select on reg, 1D indexing on address
  580. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex, D3D10_SB_4_COMPONENT_NAME Component,
  581. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, D3D10_SB_4_COMPONENT_NAME RelComponentName,
  582. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  583. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  584. {
  585. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  586. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE;
  587. m_ComponentName = Component;
  588. m_Type = Type;
  589. m_bExtendedOperand = FALSE;
  590. SetMinPrecision(MinPrecision);
  591. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  592. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  593. SetIndex(0, RegIndex, RelRegType, RelRegIndex, 0xFFFFFFFF, RelComponentName, RelRegMinPrecision);
  594. }
  595. // 4-component source operand with relative addressing
  596. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  597. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, D3D10_SB_4_COMPONENT_NAME RelComponentName,
  598. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  599. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  600. {
  601. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  602. SetSwizzle();
  603. m_Type = Type;
  604. m_bExtendedOperand = FALSE;
  605. SetMinPrecision(MinPrecision);
  606. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  607. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  608. SetIndex(0, RegIndex, RelRegType, RelRegIndex, 0xFFFFFFFF, RelComponentName, RelRegMinPrecision);
  609. }
  610. // 4-component source operand with relative addressing
  611. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  612. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, UINT RelRegIndex1, D3D10_SB_4_COMPONENT_NAME RelComponentName,
  613. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  614. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  615. {
  616. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  617. SetSwizzle();
  618. m_Type = Type;
  619. m_bExtendedOperand = FALSE;
  620. SetMinPrecision(MinPrecision);
  621. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  622. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  623. SetIndex(0, RegIndex, RelRegType, RelRegIndex, RelRegIndex1, RelComponentName, RelRegMinPrecision);
  624. }
  625. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  626. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW,
  627. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  628. {
  629. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  630. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  631. m_Type = Type;
  632. m_bExtendedOperand = FALSE;
  633. SetMinPrecision(MinPrecision);
  634. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  635. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  636. SetIndex(0, RegIndex);
  637. }
  638. // 4-component source operand with relative addressing
  639. COperand4(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  640. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW,
  641. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, UINT RelRegIndex1,
  642. D3D10_SB_4_COMPONENT_NAME RelComponentName,
  643. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  644. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  645. {
  646. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  647. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  648. m_Type = Type;
  649. m_bExtendedOperand = FALSE;
  650. SetMinPrecision(MinPrecision);
  651. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  652. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  653. SetIndex(0, RegIndex, RelRegType, RelRegIndex, RelRegIndex1, RelComponentName, RelRegMinPrecision);
  654. }
  655. friend class CShaderAsm;
  656. friend class CShaderCodeParser;
  657. friend class CInstruction;
  658. };
  659. //*****************************************************************************
  660. //
  661. // class COperandDst
  662. //
  663. // Encapsulates a destination operand in shader instructions
  664. //
  665. //*****************************************************************************
  666. class COperandDst: public COperandBase
  667. {
  668. public:
  669. COperandDst(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex,
  670. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  671. {
  672. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  673. SetMask();
  674. m_Type = Type;
  675. m_bExtendedOperand = FALSE;
  676. SetMinPrecision(MinPrecision);
  677. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  678. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  679. SetIndex(0, RegIndex);
  680. }
  681. COperandDst(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex, UINT WriteMask,
  682. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  683. {
  684. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  685. SetMask(WriteMask);
  686. m_Type = Type;
  687. m_bExtendedOperand = FALSE;
  688. SetMinPrecision(MinPrecision);
  689. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  690. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  691. SetIndex(0, RegIndex);
  692. }
  693. COperandDst(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex, UINT WriteMask,
  694. D3D10_SB_OPERAND_TYPE RelRegType,
  695. UINT RelRegIndex, UINT RelRegIndex1,
  696. D3D10_SB_4_COMPONENT_NAME RelComponentName,
  697. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  698. D3D11_SB_OPERAND_MIN_PRECISION RelRegMinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  699. :COperandBase()
  700. {
  701. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  702. SetMask(WriteMask);
  703. m_Type = Type;
  704. m_bExtendedOperand = FALSE;
  705. SetMinPrecision(MinPrecision);
  706. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  707. m_IndexDimension = D3D10_SB_OPERAND_INDEX_1D;
  708. SetIndex(0, RegIndex, RelRegType, RelRegIndex, RelRegIndex1, RelComponentName, RelRegMinPrecision);
  709. }
  710. COperandDst(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex, UINT WriteMask,
  711. D3D10_SB_OPERAND_TYPE RelRegType, UINT RelRegIndex, UINT RelRegIndex1,
  712. D3D10_SB_4_COMPONENT_NAME RelComponentName, UINT,
  713. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  714. D3D11_SB_OPERAND_MIN_PRECISION RelReg1MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  715. : COperandBase()
  716. {
  717. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  718. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE;
  719. m_WriteMask = WriteMask;
  720. m_Type = Type;
  721. m_bExtendedOperand = FALSE;
  722. SetMinPrecision(MinPrecision);
  723. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  724. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  725. SetIndex(0, RegIndex);
  726. SetIndex(1, RelRegIndex, RelRegType, RelRegIndex1, 0, RelComponentName, RelReg1MinPrecision);
  727. }
  728. // 2D dst (e.g. for GS input decl)
  729. COperandDst(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex0, UINT RegIndex1,UINT WriteMask,
  730. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  731. {
  732. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  733. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE;
  734. m_WriteMask = WriteMask;
  735. m_Type = Type;
  736. m_bExtendedOperand = FALSE;
  737. SetMinPrecision(MinPrecision);
  738. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  739. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  740. SetIndex(0, RegIndex0);
  741. SetIndex(1, RegIndex1);
  742. }
  743. // Used for operands without indices
  744. COperandDst(D3D10_SB_OPERAND_TYPE Type,
  745. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  746. {
  747. switch( Type )
  748. {
  749. case D3D10_SB_OPERAND_TYPE_OUTPUT_DEPTH:
  750. case D3D11_SB_OPERAND_TYPE_OUTPUT_DEPTH_GREATER_EQUAL:
  751. case D3D11_SB_OPERAND_TYPE_OUTPUT_DEPTH_LESS_EQUAL:
  752. case D3D11_SB_OPERAND_TYPE_OUTPUT_STENCIL_REF:
  753. m_NumComponents = D3D10_SB_OPERAND_1_COMPONENT;
  754. break;
  755. default:
  756. m_NumComponents = D3D10_SB_OPERAND_0_COMPONENT;
  757. break;
  758. }
  759. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  760. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  761. m_Type = Type;
  762. m_bExtendedOperand = FALSE;
  763. SetMinPrecision(MinPrecision);
  764. }
  765. COperandDst(UINT WriteMask, D3D10_SB_OPERAND_TYPE Type,
  766. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  767. : COperandBase() // param order disambiguates from another constructor.
  768. {
  769. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  770. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_MASK_MODE;
  771. m_WriteMask = WriteMask;
  772. m_Type = Type;
  773. m_bExtendedOperand = FALSE;
  774. SetMinPrecision(MinPrecision);
  775. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  776. m_IndexDimension = D3D10_SB_OPERAND_INDEX_0D;
  777. }
  778. friend class CShaderAsm;
  779. friend class CShaderCodeParser;
  780. friend class CInstruction;
  781. };
  782. //*****************************************************************************
  783. //
  784. // class COperand2D
  785. //
  786. // Encapsulates 2 dimensional source operand with 4 components in shader instructions
  787. //
  788. //*****************************************************************************
  789. class COperand2D: public COperandBase
  790. {
  791. public:
  792. COperand2D(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex0, UINT RegIndex1,
  793. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  794. : COperandBase()
  795. {
  796. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  797. SetSwizzle();
  798. m_Type = Type;
  799. m_bExtendedOperand = FALSE;
  800. SetMinPrecision(MinPrecision);
  801. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  802. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  803. SetIndex(0, RegIndex0);
  804. SetIndex(1, RegIndex1);
  805. }
  806. COperand2D(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex0, UINT RegIndex1, D3D10_SB_4_COMPONENT_NAME Component,
  807. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  808. : COperandBase()
  809. {
  810. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  811. m_ComponentSelection = D3D10_SB_OPERAND_4_COMPONENT_SELECT_1_MODE;
  812. m_ComponentName = Component;
  813. m_Type = Type;
  814. m_bExtendedOperand = FALSE;
  815. SetMinPrecision(MinPrecision);
  816. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  817. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  818. SetIndex(0, RegIndex0);
  819. SetIndex(1, RegIndex1);
  820. }
  821. // 2-dimensional 4-component operand with relative addressing the second index
  822. // For example:
  823. // c2[x12[3].w + 7]
  824. // Type = c
  825. // RelRegType = x
  826. // RegIndex0 = 2
  827. // RegIndex1 = 7
  828. // RelRegIndex = 12
  829. // RelRegIndex1 = 3
  830. // RelComponentName = w
  831. //
  832. COperand2D(D3D10_SB_OPERAND_TYPE Type,
  833. UINT RegIndex0,
  834. UINT RegIndex1,
  835. D3D10_SB_OPERAND_TYPE RelRegType,
  836. UINT RelRegIndex,
  837. UINT RelRegIndex1,
  838. D3D10_SB_4_COMPONENT_NAME RelComponentName,
  839. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  840. D3D11_SB_OPERAND_MIN_PRECISION RelReg1MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  841. : COperandBase()
  842. {
  843. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  844. SetSwizzle();
  845. m_Type = Type;
  846. m_bExtendedOperand = FALSE;
  847. SetMinPrecision(MinPrecision);
  848. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  849. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  850. SetIndex(0, RegIndex0);
  851. SetIndex(1, RegIndex1, RelRegType, RelRegIndex, RelRegIndex1, RelComponentName, RelReg1MinPrecision);
  852. }
  853. // 2-dimensional 4-component operand with relative addressing a second index
  854. // For example:
  855. // c2[r12.y + 7]
  856. // Type = c
  857. // RelRegType = r
  858. // RegIndex0 = 2
  859. // RegIndex1 = 7
  860. // RelRegIndex = 12
  861. // RelRegIndex1 = 3
  862. // RelComponentName = y
  863. //
  864. COperand2D(D3D10_SB_OPERAND_TYPE Type,
  865. UINT RegIndex0,
  866. UINT RegIndex1,
  867. D3D10_SB_OPERAND_TYPE RelRegType,
  868. UINT RelRegIndex,
  869. D3D10_SB_4_COMPONENT_NAME RelComponentName,
  870. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  871. D3D11_SB_OPERAND_MIN_PRECISION RelReg1MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  872. : COperandBase()
  873. {
  874. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  875. SetSwizzle();
  876. m_Type = Type;
  877. m_bExtendedOperand = FALSE;
  878. SetMinPrecision(MinPrecision);
  879. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  880. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  881. SetIndex(0, RegIndex0);
  882. SetIndex(1, RegIndex1, RelRegType, RelRegIndex, 0, RelComponentName, RelReg1MinPrecision);
  883. }
  884. // 2-dimensional 4-component operand with relative addressing both operands
  885. COperand2D(D3D10_SB_OPERAND_TYPE Type,
  886. BOOL bIndexRelative0, BOOL bIndexRelative1,
  887. UINT RegIndex0, UINT RegIndex1,
  888. D3D10_SB_OPERAND_TYPE RelRegType0, UINT RelRegIndex0, UINT RelRegIndex10, D3D10_SB_4_COMPONENT_NAME RelComponentName0,
  889. D3D10_SB_OPERAND_TYPE RelRegType1, UINT RelRegIndex1, UINT RelRegIndex11, D3D10_SB_4_COMPONENT_NAME RelComponentName1,
  890. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  891. D3D11_SB_OPERAND_MIN_PRECISION RelReg0MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  892. D3D11_SB_OPERAND_MIN_PRECISION RelReg1MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  893. : COperandBase()
  894. {
  895. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  896. SetSwizzle();
  897. m_Type = Type;
  898. m_bExtendedOperand = FALSE;
  899. SetMinPrecision(MinPrecision);
  900. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  901. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  902. if (bIndexRelative0)
  903. SetIndex(0, RegIndex0, RelRegType0, RelRegIndex0, RelRegIndex10, RelComponentName0, RelReg0MinPrecision);
  904. else
  905. SetIndex(0, RegIndex0);
  906. if (bIndexRelative1)
  907. SetIndex(1, RegIndex1, RelRegType1, RelRegIndex1, RelRegIndex11, RelComponentName1, RelReg1MinPrecision);
  908. else
  909. SetIndex(1, RegIndex1);
  910. }
  911. COperand2D(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex0, UINT RegIndex1,
  912. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW,
  913. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT): COperandBase()
  914. {
  915. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  916. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  917. m_Type = Type;
  918. m_bExtendedOperand = FALSE;
  919. SetMinPrecision(MinPrecision);
  920. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  921. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  922. SetIndex(0, RegIndex0);
  923. SetIndex(1, RegIndex1);
  924. }
  925. // 2-dimensional 4-component operand with relative addressing and swizzle
  926. COperand2D(D3D10_SB_OPERAND_TYPE Type,
  927. BYTE SwizzleX, BYTE SwizzleY, BYTE SwizzleZ, BYTE SwizzleW,
  928. BOOL bIndexRelative0, BOOL bIndexRelative1,
  929. UINT RegIndex0, D3D10_SB_OPERAND_TYPE RelRegType0, UINT RelRegIndex0, UINT RelRegIndex10, D3D10_SB_4_COMPONENT_NAME RelComponentName0,
  930. UINT RegIndex1, D3D10_SB_OPERAND_TYPE RelRegType1, UINT RelRegIndex1, UINT RelRegIndex11, D3D10_SB_4_COMPONENT_NAME RelComponentName1,
  931. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  932. D3D11_SB_OPERAND_MIN_PRECISION RelReg0MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT,
  933. D3D11_SB_OPERAND_MIN_PRECISION RelReg1MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT )
  934. : COperandBase()
  935. {
  936. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  937. SetSwizzle(SwizzleX, SwizzleY, SwizzleZ, SwizzleW);
  938. m_Type = Type;
  939. m_bExtendedOperand = FALSE;
  940. SetMinPrecision(MinPrecision);
  941. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  942. m_IndexDimension = D3D10_SB_OPERAND_INDEX_2D;
  943. if (bIndexRelative0)
  944. SetIndex(0, RegIndex0, RelRegType0, RelRegIndex0, RelRegIndex10, RelComponentName0, RelReg0MinPrecision);
  945. else
  946. SetIndex(0, RegIndex0);
  947. if (bIndexRelative1)
  948. SetIndex(1, RegIndex1, RelRegType1, RelRegIndex1, RelRegIndex11, RelComponentName1, RelReg1MinPrecision);
  949. else
  950. SetIndex(1, RegIndex1);
  951. }
  952. friend class CShaderAsm;
  953. friend class CShaderCodeParser;
  954. friend class CInstruction;
  955. };
  956. class COperand3D: public COperandBase
  957. {
  958. public:
  959. COperand3D(D3D10_SB_OPERAND_TYPE Type, UINT RegIndex0, UINT RegIndex1, UINT RegIndex2,
  960. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  961. : COperandBase()
  962. {
  963. m_Modifier = D3D10_SB_OPERAND_MODIFIER_NONE;
  964. SetSwizzle();
  965. m_Type = Type;
  966. m_bExtendedOperand = FALSE;
  967. SetMinPrecision(MinPrecision);
  968. m_NumComponents = D3D10_SB_OPERAND_4_COMPONENT;
  969. m_IndexDimension = D3D10_SB_OPERAND_INDEX_3D;
  970. SetIndex(0, RegIndex0);
  971. SetIndex(1, RegIndex1);
  972. SetIndex(2, RegIndex2);
  973. }
  974. friend class CShaderAsm;
  975. friend class CShaderCodeParser;
  976. friend class CInstruction;
  977. };
  978. //*****************************************************************************
  979. //
  980. // CInstruction
  981. //
  982. //*****************************************************************************
  983. // Structures for additional per-instruction fields unioned in CInstruction.
  984. // These structures don't contain ALL info used by the particular instruction,
  985. // only additional info not already in CInstruction. Some instructions don't
  986. // need such structures because CInstruction already has the correct data
  987. // fields.
  988. struct CGlobalFlagsDecl
  989. {
  990. UINT Flags;
  991. };
  992. struct CInputSystemInterpretedValueDecl
  993. {
  994. D3D10_SB_NAME Name;
  995. };
  996. struct CInputSystemGeneratedValueDecl
  997. {
  998. D3D10_SB_NAME Name;
  999. };
  1000. struct CInputPSDecl
  1001. {
  1002. D3D10_SB_INTERPOLATION_MODE InterpolationMode;
  1003. };
  1004. struct CInputPSSystemInterpretedValueDecl
  1005. {
  1006. D3D10_SB_NAME Name;
  1007. D3D10_SB_INTERPOLATION_MODE InterpolationMode;
  1008. };
  1009. struct CInputPSSystemGeneratedValueDecl
  1010. {
  1011. D3D10_SB_NAME Name;
  1012. D3D10_SB_INTERPOLATION_MODE InterpolationMode;
  1013. };
  1014. struct COutputSystemInterpretedValueDecl
  1015. {
  1016. D3D10_SB_NAME Name;
  1017. };
  1018. struct COutputSystemGeneratedValueDecl
  1019. {
  1020. D3D10_SB_NAME Name;
  1021. };
  1022. struct CIndexRangeDecl
  1023. {
  1024. UINT RegCount;
  1025. };
  1026. struct CResourceDecl
  1027. {
  1028. D3D10_SB_RESOURCE_DIMENSION Dimension;
  1029. D3D10_SB_RESOURCE_RETURN_TYPE ReturnType[4];
  1030. UINT SampleCount;
  1031. UINT Space;
  1032. };
  1033. struct CConstantBufferDecl
  1034. {
  1035. D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN AccessPattern;
  1036. UINT Size;
  1037. UINT Space;
  1038. };
  1039. struct COutputTopologyDecl
  1040. {
  1041. D3D10_SB_PRIMITIVE_TOPOLOGY Topology;
  1042. };
  1043. struct CInputPrimitiveDecl
  1044. {
  1045. D3D10_SB_PRIMITIVE Primitive;
  1046. };
  1047. struct CGSMaxOutputVertexCountDecl
  1048. {
  1049. UINT MaxOutputVertexCount;
  1050. };
  1051. struct CGSInstanceCountDecl
  1052. {
  1053. UINT InstanceCount;
  1054. };
  1055. struct CSamplerDecl
  1056. {
  1057. D3D10_SB_SAMPLER_MODE SamplerMode;
  1058. UINT Space;
  1059. };
  1060. struct CStreamDecl
  1061. {
  1062. UINT Stream;
  1063. };
  1064. struct CTempsDecl
  1065. {
  1066. UINT NumTemps;
  1067. };
  1068. struct CIndexableTempDecl
  1069. {
  1070. UINT IndexableTempNumber;
  1071. UINT NumRegisters;
  1072. UINT Mask; // .x, .xy, .xzy or .xyzw (D3D10_SB_OPERAND_4_COMPONENT_MASK_* )
  1073. };
  1074. struct CHSDSInputControlPointCountDecl
  1075. {
  1076. UINT InputControlPointCount;
  1077. };
  1078. struct CHSOutputControlPointCountDecl
  1079. {
  1080. UINT OutputControlPointCount;
  1081. };
  1082. struct CTessellatorDomainDecl
  1083. {
  1084. D3D11_SB_TESSELLATOR_DOMAIN TessellatorDomain;
  1085. };
  1086. struct CTessellatorPartitioningDecl
  1087. {
  1088. D3D11_SB_TESSELLATOR_PARTITIONING TessellatorPartitioning;
  1089. };
  1090. struct CTessellatorOutputPrimitiveDecl
  1091. {
  1092. D3D11_SB_TESSELLATOR_OUTPUT_PRIMITIVE TessellatorOutputPrimitive;
  1093. };
  1094. struct CHSMaxTessFactorDecl
  1095. {
  1096. float MaxTessFactor;
  1097. };
  1098. struct CHSForkPhaseInstanceCountDecl
  1099. {
  1100. UINT InstanceCount;
  1101. };
  1102. struct CHSJoinPhaseInstanceCountDecl
  1103. {
  1104. UINT InstanceCount;
  1105. };
  1106. struct CShaderMessage
  1107. {
  1108. D3D11_SB_SHADER_MESSAGE_ID MessageID;
  1109. D3D11_SB_SHADER_MESSAGE_FORMAT FormatStyle;
  1110. PCSTR pFormatString;
  1111. UINT NumOperands;
  1112. COperandBase* pOperands;
  1113. };
  1114. struct CCustomData
  1115. {
  1116. D3D10_SB_CUSTOMDATA_CLASS Type;
  1117. UINT DataSizeInBytes;
  1118. void* pData;
  1119. union
  1120. {
  1121. CShaderMessage ShaderMessage;
  1122. };
  1123. };
  1124. struct CFunctionTableDecl
  1125. {
  1126. UINT FunctionTableNumber;
  1127. UINT TableLength;
  1128. UINT* pFunctionIdentifiers;
  1129. };
  1130. struct CInterfaceDecl
  1131. {
  1132. WORD InterfaceNumber;
  1133. WORD ArrayLength;
  1134. UINT ExpectedTableSize;
  1135. UINT TableLength;
  1136. UINT* pFunctionTableIdentifiers;
  1137. bool bDynamicallyIndexed;
  1138. };
  1139. struct CFunctionBodyDecl
  1140. {
  1141. UINT FunctionBodyNumber;
  1142. };
  1143. struct CInterfaceCall
  1144. {
  1145. UINT FunctionIndex;
  1146. COperandBase* pInterfaceOperand;
  1147. };
  1148. struct CThreadGroupDeclaration
  1149. {
  1150. UINT x;
  1151. UINT y;
  1152. UINT z;
  1153. };
  1154. struct CTypedUAVDeclaration
  1155. {
  1156. D3D10_SB_RESOURCE_DIMENSION Dimension;
  1157. D3D10_SB_RESOURCE_RETURN_TYPE ReturnType[4];
  1158. UINT Flags;
  1159. UINT Space;
  1160. };
  1161. struct CStructuredUAVDeclaration
  1162. {
  1163. UINT ByteStride;
  1164. UINT Flags;
  1165. UINT Space;
  1166. };
  1167. struct CRawUAVDeclaration
  1168. {
  1169. UINT Flags;
  1170. UINT Space;
  1171. };
  1172. struct CRawTGSMDeclaration
  1173. {
  1174. UINT ByteCount;
  1175. };
  1176. struct CStructuredTGSMDeclaration
  1177. {
  1178. UINT StructByteStride;
  1179. UINT StructCount;
  1180. };
  1181. struct CRawSRVDeclaration
  1182. {
  1183. UINT Space;
  1184. };
  1185. struct CStructuredSRVDeclaration
  1186. {
  1187. UINT ByteStride;
  1188. UINT Space;
  1189. };
  1190. struct CSyncFlags
  1191. {
  1192. bool bThreadsInGroup;
  1193. bool bThreadGroupSharedMemory;
  1194. bool bUnorderedAccessViewMemoryGlobal;
  1195. bool bUnorderedAccessViewMemoryGroup; // exclusive to global
  1196. };
  1197. class CInstruction
  1198. {
  1199. protected:
  1200. static const UINT MAX_PRIVATE_DATA_COUNT = 2;
  1201. public:
  1202. CInstruction():m_OpCode(D3D10_SB_OPCODE_ADD) { Clear(); }
  1203. CInstruction(D3D10_SB_OPCODE_TYPE OpCode)
  1204. {
  1205. Clear();
  1206. m_OpCode = OpCode;
  1207. m_NumOperands = 0;
  1208. m_ExtendedOpCodeCount = 0;
  1209. }
  1210. CInstruction(D3D10_SB_OPCODE_TYPE OpCode, COperandBase& Operand0,
  1211. D3D10_SB_INSTRUCTION_TEST_BOOLEAN Test)
  1212. {
  1213. Clear();
  1214. m_OpCode = OpCode;
  1215. m_NumOperands = 1;
  1216. m_ExtendedOpCodeCount = 0;
  1217. m_Test = Test;
  1218. m_Operands[0] = Operand0;
  1219. }
  1220. CInstruction(D3D10_SB_OPCODE_TYPE OpCode, COperandBase& Operand0, COperandBase& Operand1)
  1221. {
  1222. Clear();
  1223. m_OpCode = OpCode;
  1224. m_NumOperands = 2;
  1225. m_ExtendedOpCodeCount = 0;
  1226. m_Operands[0] = Operand0;
  1227. m_Operands[1] = Operand1;
  1228. }
  1229. CInstruction(D3D10_SB_OPCODE_TYPE OpCode, COperandBase& Operand0, COperandBase& Operand1, COperandBase& Operand2)
  1230. {
  1231. Clear();
  1232. m_OpCode = OpCode;
  1233. m_NumOperands = 3;
  1234. m_ExtendedOpCodeCount = 0;
  1235. m_Operands[0] = Operand0;
  1236. m_Operands[1] = Operand1;
  1237. m_Operands[2] = Operand2;
  1238. }
  1239. CInstruction(D3D10_SB_OPCODE_TYPE OpCode, COperandBase& Operand0, COperandBase& Operand1,
  1240. COperandBase& Operand2, COperandBase& Operand3)
  1241. {
  1242. Clear();
  1243. m_OpCode = OpCode;
  1244. m_NumOperands = 4;
  1245. m_ExtendedOpCodeCount = 0;
  1246. m_Operands[0] = Operand0;
  1247. m_Operands[1] = Operand1;
  1248. m_Operands[2] = Operand2;
  1249. m_Operands[3] = Operand3;
  1250. memset(m_TexelOffset, 0, sizeof(m_TexelOffset));
  1251. }
  1252. void ClearAllocations()
  1253. {
  1254. if (m_OpCode == D3D10_SB_OPCODE_CUSTOMDATA)
  1255. {
  1256. free(m_CustomData.pData);
  1257. if (m_CustomData.Type == D3D11_SB_CUSTOMDATA_SHADER_MESSAGE)
  1258. {
  1259. free(m_CustomData.ShaderMessage.pOperands);
  1260. }
  1261. }
  1262. else if( m_OpCode == D3D11_SB_OPCODE_DCL_FUNCTION_TABLE )
  1263. {
  1264. free(m_FunctionTableDecl.pFunctionIdentifiers);
  1265. }
  1266. else if( m_OpCode == D3D11_SB_OPCODE_DCL_INTERFACE )
  1267. {
  1268. free(m_InterfaceDecl.pFunctionTableIdentifiers);
  1269. }
  1270. }
  1271. void Clear(bool bIncludeCustomData = false)
  1272. {
  1273. if( bIncludeCustomData ) // don't need to do this on initial constructor, only if recycling the object.
  1274. {
  1275. ClearAllocations();
  1276. }
  1277. memset (this, 0, sizeof(*this));
  1278. }
  1279. ~CInstruction()
  1280. {
  1281. ClearAllocations();
  1282. }
  1283. const COperandBase& Operand(UINT Index) const {return m_Operands[Index];}
  1284. D3D10_SB_OPCODE_TYPE OpCode() const {return m_OpCode;}
  1285. void SetNumOperands(UINT NumOperands) {m_NumOperands = NumOperands;}
  1286. UINT NumOperands() const {return m_NumOperands;}
  1287. void SetTest(D3D10_SB_INSTRUCTION_TEST_BOOLEAN Test) {m_Test = Test;}
  1288. void SetPreciseMask(UINT PreciseMask) {m_PreciseMask = PreciseMask;}
  1289. D3D10_SB_INSTRUCTION_TEST_BOOLEAN Test() const {return m_Test;}
  1290. void SetTexelOffset( const INT8 texelOffset[3] )
  1291. {
  1292. m_OpCodeEx[m_ExtendedOpCodeCount++] = D3D10_SB_EXTENDED_OPCODE_SAMPLE_CONTROLS;
  1293. memcpy(m_TexelOffset, texelOffset,sizeof(m_TexelOffset));
  1294. }
  1295. void SetTexelOffset( INT8 x, INT8 y, INT8 z)
  1296. {
  1297. m_OpCodeEx[m_ExtendedOpCodeCount++] = D3D10_SB_EXTENDED_OPCODE_SAMPLE_CONTROLS;
  1298. m_TexelOffset[0] = x;
  1299. m_TexelOffset[1] = y;
  1300. m_TexelOffset[2] = z;
  1301. }
  1302. void SetResourceDim(D3D10_SB_RESOURCE_DIMENSION Dim,
  1303. D3D10_SB_RESOURCE_RETURN_TYPE RetType[4],
  1304. UINT StructureStride)
  1305. {
  1306. m_OpCodeEx[m_ExtendedOpCodeCount++] = D3D11_SB_EXTENDED_OPCODE_RESOURCE_DIM;
  1307. m_OpCodeEx[m_ExtendedOpCodeCount++] = D3D11_SB_EXTENDED_OPCODE_RESOURCE_RETURN_TYPE;
  1308. m_ResourceDimEx = Dim;
  1309. m_ResourceDimStructureStrideEx = StructureStride;
  1310. memcpy(m_ResourceReturnTypeEx, RetType,4*sizeof(D3D10_SB_RESOURCE_RETURN_TYPE));
  1311. }
  1312. BOOL Disassemble(__out_ecount(StringSize) LPSTR pString, UINT StringSize);
  1313. // Private data is used by D3D runtime
  1314. void SetPrivateData(UINT Value, UINT index = 0)
  1315. {
  1316. if (index < MAX_PRIVATE_DATA_COUNT)
  1317. {
  1318. m_PrivateData[index] = Value;
  1319. }
  1320. }
  1321. UINT PrivateData(UINT index = 0) const
  1322. {
  1323. if (index >= MAX_PRIVATE_DATA_COUNT)
  1324. return 0xFFFFFFFF;
  1325. return m_PrivateData[index];
  1326. }
  1327. // Get the precise mask
  1328. UINT GetPreciseMask() const {return m_PreciseMask;}
  1329. D3D10_SB_OPCODE_TYPE m_OpCode;
  1330. COperandBase m_Operands[D3D10_SB_MAX_INSTRUCTION_OPERANDS];
  1331. UINT m_NumOperands;
  1332. UINT m_ExtendedOpCodeCount;
  1333. UINT m_PreciseMask;
  1334. D3D10_SB_EXTENDED_OPCODE_TYPE m_OpCodeEx[D3D11_SB_MAX_SIMULTANEOUS_EXTENDED_OPCODES];
  1335. INT8 m_TexelOffset[3]; // for extended opcode only
  1336. D3D10_SB_RESOURCE_DIMENSION m_ResourceDimEx; // for extended opcode only
  1337. UINT m_ResourceDimStructureStrideEx; // for extended opcode only
  1338. D3D10_SB_RESOURCE_RETURN_TYPE m_ResourceReturnTypeEx[4]; // for extended opcode only
  1339. BOOL m_bNonuniformResourceIndex; // for extended opcode only
  1340. BOOL m_bNonuniformSamplerIndex; // for extended opcode only
  1341. UINT m_PrivateData[MAX_PRIVATE_DATA_COUNT];
  1342. BOOL m_bSaturate;
  1343. union // extra info needed by some instructions
  1344. {
  1345. CInputSystemInterpretedValueDecl m_InputDeclSIV;
  1346. CInputSystemGeneratedValueDecl m_InputDeclSGV;
  1347. CInputPSDecl m_InputPSDecl;
  1348. CInputPSSystemInterpretedValueDecl m_InputPSDeclSIV;
  1349. CInputPSSystemGeneratedValueDecl m_InputPSDeclSGV;
  1350. COutputSystemInterpretedValueDecl m_OutputDeclSIV;
  1351. COutputSystemGeneratedValueDecl m_OutputDeclSGV;
  1352. CIndexRangeDecl m_IndexRangeDecl;
  1353. CResourceDecl m_ResourceDecl;
  1354. CConstantBufferDecl m_ConstantBufferDecl;
  1355. CInputPrimitiveDecl m_InputPrimitiveDecl;
  1356. COutputTopologyDecl m_OutputTopologyDecl;
  1357. CGSMaxOutputVertexCountDecl m_GSMaxOutputVertexCountDecl;
  1358. CGSInstanceCountDecl m_GSInstanceCountDecl;
  1359. CSamplerDecl m_SamplerDecl;
  1360. CStreamDecl m_StreamDecl;
  1361. CTempsDecl m_TempsDecl;
  1362. CIndexableTempDecl m_IndexableTempDecl;
  1363. CGlobalFlagsDecl m_GlobalFlagsDecl;
  1364. CCustomData m_CustomData;
  1365. CInterfaceDecl m_InterfaceDecl;
  1366. CFunctionTableDecl m_FunctionTableDecl;
  1367. CFunctionBodyDecl m_FunctionBodyDecl;
  1368. CInterfaceCall m_InterfaceCall;
  1369. D3D10_SB_INSTRUCTION_TEST_BOOLEAN m_Test;
  1370. D3D10_SB_RESINFO_INSTRUCTION_RETURN_TYPE m_ResInfoReturnType;
  1371. D3D10_SB_INSTRUCTION_RETURN_TYPE m_InstructionReturnType;
  1372. CHSDSInputControlPointCountDecl m_InputControlPointCountDecl;
  1373. CHSOutputControlPointCountDecl m_OutputControlPointCountDecl;
  1374. CTessellatorDomainDecl m_TessellatorDomainDecl;
  1375. CTessellatorPartitioningDecl m_TessellatorPartitioningDecl;
  1376. CTessellatorOutputPrimitiveDecl m_TessellatorOutputPrimitiveDecl;
  1377. CHSMaxTessFactorDecl m_HSMaxTessFactorDecl;
  1378. CHSForkPhaseInstanceCountDecl m_HSForkPhaseInstanceCountDecl;
  1379. CHSJoinPhaseInstanceCountDecl m_HSJoinPhaseInstanceCountDecl;
  1380. CThreadGroupDeclaration m_ThreadGroupDecl;
  1381. CTypedUAVDeclaration m_TypedUAVDecl;
  1382. CStructuredUAVDeclaration m_StructuredUAVDecl;
  1383. CRawUAVDeclaration m_RawUAVDecl;
  1384. CStructuredTGSMDeclaration m_StructuredTGSMDecl;
  1385. CRawSRVDeclaration m_RawSRVDecl;
  1386. CStructuredSRVDeclaration m_StructuredSRVDecl;
  1387. CRawTGSMDeclaration m_RawTGSMDecl;
  1388. CSyncFlags m_SyncFlags;
  1389. };
  1390. };
  1391. // ****************************************************************************
  1392. //
  1393. // class CShaderAsm
  1394. //
  1395. // The class is used to build a binary representation of a shader.
  1396. // Usage scenario:
  1397. // 1. Call Init with the initial internal buffer size in UINTs. The
  1398. // internal buffer will grow if needed
  1399. // 2. Call StartShader()
  1400. // 3. Call Emit*() functions to assemble a shader
  1401. // 4. Call EndShader()
  1402. // 5. Call GetShader() to get the binary representation
  1403. //
  1404. //
  1405. // ****************************************************************************
  1406. class CShaderAsm
  1407. {
  1408. public:
  1409. CShaderAsm():
  1410. m_dwFunc(NULL),
  1411. m_Index(0),
  1412. m_StartOpIndex(0),
  1413. m_BufferSize(0)
  1414. {
  1415. Init(1024);
  1416. };
  1417. ~CShaderAsm()
  1418. {
  1419. free(m_dwFunc);
  1420. };
  1421. // Initializes the object with the initial buffer size in UINTs
  1422. HRESULT Init(UINT BufferSize)
  1423. {
  1424. if( BufferSize >= UINT( -1 ) / sizeof( UINT ) )
  1425. {
  1426. return E_OUTOFMEMORY;
  1427. }
  1428. m_dwFunc = (UINT*)malloc(BufferSize*sizeof(UINT));
  1429. if (m_dwFunc == NULL)
  1430. {
  1431. return E_OUTOFMEMORY;
  1432. }
  1433. m_BufferSize = BufferSize;
  1434. Reset();
  1435. return S_OK;
  1436. }
  1437. UINT* GetShader() {return m_dwFunc;}
  1438. UINT ShaderSizeInDWORDs() {return m_Index;}
  1439. UINT ShaderSizeInBytes() {return ShaderSizeInDWORDs() * sizeof(*m_dwFunc);}
  1440. UINT LastInstOffsetInDWORDs() {return m_StartOpIndex;}
  1441. UINT LastInstOffsetInBytes() {return LastInstOffsetInDWORDs() * sizeof(*m_dwFunc);}
  1442. // This function should be called to mark the start of a shader
  1443. void StartShader(D3D10_SB_TOKENIZED_PROGRAM_TYPE ShaderType, UINT vermajor,UINT verminor)
  1444. {
  1445. Reset();
  1446. UINT Token = ENCODE_D3D10_SB_TOKENIZED_PROGRAM_VERSION_TOKEN(ShaderType, vermajor, verminor);
  1447. OPCODE(Token);
  1448. OPCODE(0); // Reserve space for length
  1449. }
  1450. // Should be called at the end of the shader
  1451. void EndShader()
  1452. {
  1453. if (1 < m_BufferSize)
  1454. m_dwFunc[1] = ENCODE_D3D10_SB_TOKENIZED_PROGRAM_LENGTH(m_Index);
  1455. }
  1456. // Emit a resource declaration
  1457. void EmitResourceDecl(D3D10_SB_RESOURCE_DIMENSION Dimension, UINT TRegIndex,
  1458. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  1459. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  1460. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  1461. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW)
  1462. {
  1463. m_bExecutableInstruction = FALSE;
  1464. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_RESOURCE) |
  1465. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) );
  1466. EmitOperand(COperand(D3D10_SB_OPERAND_TYPE_RESOURCE, TRegIndex));
  1467. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  1468. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  1469. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  1470. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  1471. ENDINSTRUCTION();
  1472. }
  1473. // Emit D3D12 resource declaration
  1474. void EmitIndexableResourceDecl(UINT uTable, UINT uTableLB, UINT uTableUB,
  1475. D3D10_SB_RESOURCE_DIMENSION Dimension,
  1476. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  1477. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  1478. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  1479. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW,
  1480. UINT uSpace)
  1481. {
  1482. m_bExecutableInstruction = FALSE;
  1483. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_RESOURCE) |
  1484. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) );
  1485. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_RESOURCE, uTable, uTableLB, uTableUB));
  1486. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  1487. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  1488. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  1489. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  1490. FUNC(uSpace);
  1491. ENDINSTRUCTION();
  1492. }
  1493. // Emit a resource declaration (multisampled)
  1494. void EmitResourceMSDecl(D3D10_SB_RESOURCE_DIMENSION Dimension, UINT TRegIndex,
  1495. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  1496. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  1497. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  1498. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW,
  1499. UINT SampleCount)
  1500. {
  1501. m_bExecutableInstruction = FALSE;
  1502. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_RESOURCE) |
  1503. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) |
  1504. ENCODE_D3D10_SB_RESOURCE_SAMPLE_COUNT(SampleCount));
  1505. EmitOperand(COperand(D3D10_SB_OPERAND_TYPE_RESOURCE, TRegIndex));
  1506. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  1507. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  1508. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  1509. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  1510. ENDINSTRUCTION();
  1511. }
  1512. // Emit D3D12 resource declaration (multisampled)
  1513. void EmitIndexableResourceMSDecl(UINT uTable, UINT uTableLB, UINT uTableUB,
  1514. D3D10_SB_RESOURCE_DIMENSION Dimension,
  1515. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  1516. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  1517. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  1518. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW,
  1519. UINT SampleCount,
  1520. UINT uSpace)
  1521. {
  1522. m_bExecutableInstruction = FALSE;
  1523. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_RESOURCE) |
  1524. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) |
  1525. ENCODE_D3D10_SB_RESOURCE_SAMPLE_COUNT(SampleCount));
  1526. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_RESOURCE, uTable, uTableLB, uTableUB));
  1527. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  1528. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  1529. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  1530. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  1531. FUNC(uSpace);
  1532. ENDINSTRUCTION();
  1533. }
  1534. // Emit a sampler declaration
  1535. void EmitSamplerDecl(UINT SRegIndex, D3D10_SB_SAMPLER_MODE Mode)
  1536. {
  1537. m_bExecutableInstruction = FALSE;
  1538. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_SAMPLER) |
  1539. ENCODE_D3D10_SB_SAMPLER_MODE(Mode) );
  1540. EmitOperand(COperand(D3D10_SB_OPERAND_TYPE_SAMPLER, SRegIndex));
  1541. ENDINSTRUCTION();
  1542. }
  1543. // Emit D3D12 sampler declaration
  1544. void EmitIndexableSamplerDecl(UINT uTable, UINT uTableLB, UINT uTableUB, D3D10_SB_SAMPLER_MODE Mode, UINT uSpace)
  1545. {
  1546. m_bExecutableInstruction = FALSE;
  1547. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_SAMPLER) |
  1548. ENCODE_D3D10_SB_SAMPLER_MODE(Mode) );
  1549. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_SAMPLER, uTable, uTableLB, uTableUB));
  1550. FUNC(uSpace);
  1551. ENDINSTRUCTION();
  1552. }
  1553. // Emit a stream declaration
  1554. void EmitStreamDecl(UINT SRegIndex)
  1555. {
  1556. m_bExecutableInstruction = FALSE;
  1557. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_STREAM) );
  1558. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_STREAM, SRegIndex));
  1559. ENDINSTRUCTION();
  1560. }
  1561. // Emit an input declaration
  1562. void EmitInputDecl(D3D10_SB_OPERAND_TYPE RegType, UINT RegIndex, UINT WriteMask,
  1563. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1564. {
  1565. m_bExecutableInstruction = FALSE;
  1566. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1567. EmitOperand(COperandDst(RegType, RegIndex, WriteMask, MinPrecision));
  1568. ENDINSTRUCTION();
  1569. }
  1570. void EmitInputDecl2D(D3D10_SB_OPERAND_TYPE RegType, UINT RegIndex, UINT RegIndex2, UINT WriteMask,
  1571. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1572. {
  1573. m_bExecutableInstruction = FALSE;
  1574. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1575. EmitOperand(COperandDst(RegType, RegIndex, RegIndex2, WriteMask, MinPrecision));
  1576. ENDINSTRUCTION();
  1577. }
  1578. // Emit an input declaration for a system interpreted value
  1579. void EmitInputSystemInterpretedValueDecl(D3D10_SB_OPERAND_TYPE RegType, UINT RegIndex, UINT WriteMask, D3D10_SB_NAME Name,
  1580. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1581. {
  1582. m_bExecutableInstruction = FALSE;
  1583. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_SIV));
  1584. EmitOperand(COperandDst(RegType, RegIndex, WriteMask, MinPrecision));
  1585. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1586. ENDINSTRUCTION();
  1587. }
  1588. void EmitInputSystemInterpretedValueDecl2D(UINT RegIndex, UINT RegIndex2, UINT WriteMask, D3D10_SB_NAME Name,
  1589. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1590. {
  1591. m_bExecutableInstruction = FALSE;
  1592. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_SIV));
  1593. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, RegIndex2, WriteMask, MinPrecision));
  1594. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1595. ENDINSTRUCTION();
  1596. }
  1597. // Emit an input declaration for a system generated value
  1598. void EmitInputSystemGeneratedValueDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_NAME Name,
  1599. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1600. {
  1601. m_bExecutableInstruction = FALSE;
  1602. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_SGV));
  1603. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, WriteMask, MinPrecision));
  1604. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1605. ENDINSTRUCTION();
  1606. }
  1607. void EmitInputSystemGeneratedValueDecl2D(UINT RegIndex, UINT RegIndex2, UINT WriteMask, D3D10_SB_NAME Name,
  1608. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1609. {
  1610. m_bExecutableInstruction = FALSE;
  1611. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_SGV));
  1612. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, RegIndex2, WriteMask, MinPrecision));
  1613. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1614. ENDINSTRUCTION();
  1615. }
  1616. // Emit a PS input declaration
  1617. void EmitPSInputDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_INTERPOLATION_MODE Mode,
  1618. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1619. {
  1620. m_bExecutableInstruction = FALSE;
  1621. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_PS) |
  1622. ENCODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Mode));
  1623. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, WriteMask, MinPrecision));
  1624. ENDINSTRUCTION();
  1625. }
  1626. // Emit a PS input declaration for a system interpreted value
  1627. void EmitPSInputSystemInterpretedValueDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_INTERPOLATION_MODE Mode, D3D10_SB_NAME Name,
  1628. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1629. {
  1630. m_bExecutableInstruction = FALSE;
  1631. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_PS_SIV) |
  1632. ENCODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Mode));
  1633. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, WriteMask, MinPrecision));
  1634. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1635. ENDINSTRUCTION();
  1636. }
  1637. // Emit a PS input declaration for a system generated value
  1638. void EmitPSInputSystemGeneratedValueDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_INTERPOLATION_MODE Mode, D3D10_SB_NAME Name,
  1639. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1640. {
  1641. m_bExecutableInstruction = FALSE;
  1642. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT_PS_SGV) |
  1643. ENCODE_D3D10_SB_INPUT_INTERPOLATION_MODE(Mode));
  1644. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, WriteMask, MinPrecision));
  1645. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1646. ENDINSTRUCTION();
  1647. }
  1648. // Emit input coverage mask declaration
  1649. void EmitInputCoverageMaskDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1650. {
  1651. m_bExecutableInstruction = FALSE;
  1652. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1653. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_INPUT_COVERAGE_MASK, MinPrecision));
  1654. ENDINSTRUCTION();
  1655. }
  1656. // Emit inner coverage declaration
  1657. void EmitInnerCoverageDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1658. {
  1659. m_bExecutableInstruction = FALSE;
  1660. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1661. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_INNER_COVERAGE, MinPrecision));
  1662. ENDINSTRUCTION();
  1663. }
  1664. // Emit cycle counter decl
  1665. void EmitCycleCounterDecl(UINT WriteMask)
  1666. {
  1667. m_bExecutableInstruction = FALSE;
  1668. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1669. EmitOperand(COperandDst(WriteMask,D3D11_SB_OPERAND_TYPE_CYCLE_COUNTER));
  1670. ENDINSTRUCTION();
  1671. }
  1672. // Emit input primitive id declaration
  1673. void EmitInputPrimIdDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1674. {
  1675. m_bExecutableInstruction = FALSE;
  1676. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1677. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT_PRIMITIVEID, MinPrecision));
  1678. ENDINSTRUCTION();
  1679. }
  1680. // Emit input domain point declaration
  1681. void EmitInputDomainPointDecl(UINT WriteMask,
  1682. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1683. {
  1684. m_bExecutableInstruction = FALSE;
  1685. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1686. EmitOperand(COperandDst(WriteMask,D3D11_SB_OPERAND_TYPE_INPUT_DOMAIN_POINT, MinPrecision));
  1687. ENDINSTRUCTION();
  1688. }
  1689. // Emit and oDepth declaration
  1690. void EmitODepthDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1691. {
  1692. m_bExecutableInstruction = FALSE;
  1693. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1694. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT_DEPTH, MinPrecision));
  1695. ENDINSTRUCTION();
  1696. }
  1697. // Emit and oDepthGE declaration
  1698. void EmitODepthDeclGE(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1699. {
  1700. m_bExecutableInstruction = FALSE;
  1701. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1702. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_OUTPUT_DEPTH_GREATER_EQUAL, MinPrecision));
  1703. ENDINSTRUCTION();
  1704. }
  1705. // Emit and oDepthLE declaration
  1706. void EmitODepthDeclLE(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1707. {
  1708. m_bExecutableInstruction = FALSE;
  1709. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1710. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_OUTPUT_DEPTH_LESS_EQUAL, MinPrecision));
  1711. ENDINSTRUCTION();
  1712. }
  1713. // Emit an oMask declaration
  1714. void EmitOMaskDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1715. {
  1716. m_bExecutableInstruction = FALSE;
  1717. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1718. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT_COVERAGE_MASK, MinPrecision));
  1719. ENDINSTRUCTION();
  1720. }
  1721. // Emit an oStencilRef declaration
  1722. void EmitOStencilRefDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1723. {
  1724. m_bExecutableInstruction = FALSE;
  1725. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1726. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_OUTPUT_STENCIL_REF, MinPrecision));
  1727. ENDINSTRUCTION();
  1728. }
  1729. // Emit an output declaration
  1730. void EmitOutputDecl(UINT RegIndex, UINT WriteMask,
  1731. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1732. {
  1733. m_bExecutableInstruction = FALSE;
  1734. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT));
  1735. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT, RegIndex, WriteMask, MinPrecision));
  1736. ENDINSTRUCTION();
  1737. }
  1738. // Emit an output declaration for a system interpreted value
  1739. void EmitOutputSystemInterpretedValueDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_NAME Name,
  1740. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1741. {
  1742. m_bExecutableInstruction = FALSE;
  1743. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT_SIV));
  1744. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT, RegIndex, WriteMask, MinPrecision));
  1745. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1746. ENDINSTRUCTION();
  1747. }
  1748. // Emit an output declaration for a system generated value
  1749. void EmitOutputSystemGeneratedValueDecl(UINT RegIndex, UINT WriteMask, D3D10_SB_NAME Name,
  1750. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1751. {
  1752. m_bExecutableInstruction = FALSE;
  1753. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_OUTPUT_SGV));
  1754. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT, RegIndex, WriteMask, MinPrecision));
  1755. FUNC(ENCODE_D3D10_SB_NAME(Name));
  1756. ENDINSTRUCTION();
  1757. }
  1758. // Emit an input register indexing range declaration
  1759. void EmitInputIndexingRangeDecl(UINT RegIndex, UINT Count, UINT WriteMask)
  1760. {
  1761. m_bExecutableInstruction = FALSE;
  1762. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEX_RANGE));
  1763. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, WriteMask));
  1764. FUNC((UINT)Count);
  1765. ENDINSTRUCTION();
  1766. }
  1767. // 2D indexing range decl (indexing is for second dimension)
  1768. void EmitInputIndexingRangeDecl2D(UINT RegIndex, UINT RegIndex2Min, UINT Reg2Count, UINT WriteMask)
  1769. {
  1770. m_bExecutableInstruction = FALSE;
  1771. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEX_RANGE));
  1772. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_INPUT, RegIndex, RegIndex2Min, WriteMask));
  1773. FUNC((UINT)Reg2Count);
  1774. ENDINSTRUCTION();
  1775. }
  1776. // Emit an output register indexing range declaration
  1777. void EmitOutputIndexingRangeDecl(UINT RegIndex, UINT Count, UINT WriteMask)
  1778. {
  1779. m_bExecutableInstruction = FALSE;
  1780. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEX_RANGE));
  1781. EmitOperand(COperandDst(D3D10_SB_OPERAND_TYPE_OUTPUT, RegIndex, WriteMask));
  1782. FUNC((UINT)Count);
  1783. ENDINSTRUCTION();
  1784. }
  1785. // Emit indexing range decl taking reg type as parameter
  1786. // (for things other than plain input or output regs)
  1787. void EmitIndexingRangeDecl(D3D10_SB_OPERAND_TYPE RegType, UINT RegIndex, UINT Count, UINT WriteMask)
  1788. {
  1789. m_bExecutableInstruction = FALSE;
  1790. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEX_RANGE));
  1791. EmitOperand(COperandDst(RegType, RegIndex, WriteMask));
  1792. FUNC((UINT)Count);
  1793. ENDINSTRUCTION();
  1794. }
  1795. // 2D indexing range decl (indexing is for second dimension)
  1796. // Emit indexing range decl taking reg type as parameter
  1797. // (for things other than plain input or output regs)
  1798. void EmitIndexingRangeDecl2D(D3D10_SB_OPERAND_TYPE RegType, UINT RegIndex, UINT RegIndex2Min, UINT Reg2Count, UINT WriteMask)
  1799. {
  1800. m_bExecutableInstruction = FALSE;
  1801. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEX_RANGE));
  1802. EmitOperand(COperandDst(RegType, RegIndex, RegIndex2Min, WriteMask));
  1803. FUNC((UINT)Reg2Count);
  1804. ENDINSTRUCTION();
  1805. }
  1806. // Emit a temp registers ( r0...r(n-1) ) declaration
  1807. void EmitTempsDecl(UINT NumTemps)
  1808. {
  1809. m_bExecutableInstruction = FALSE;
  1810. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_TEMPS));
  1811. FUNC((UINT)NumTemps);
  1812. ENDINSTRUCTION();
  1813. }
  1814. // Emit an indexable temp register (x#) declaration
  1815. void EmitIndexableTempDecl(UINT TempNumber, UINT RegCount, UINT ComponentCount )
  1816. {
  1817. m_bExecutableInstruction = FALSE;
  1818. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INDEXABLE_TEMP));
  1819. FUNC((UINT)TempNumber);
  1820. FUNC((UINT)RegCount);
  1821. FUNC((UINT)ComponentCount);
  1822. ENDINSTRUCTION();
  1823. }
  1824. // Emit a constant buffer (cb#) declaration
  1825. void EmitConstantBufferDecl(UINT RegIndex, UINT Size, // size 0 means unknown/any size
  1826. D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN AccessPattern)
  1827. {
  1828. m_bExecutableInstruction = FALSE;
  1829. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER) |
  1830. ENCODE_D3D10_SB_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(AccessPattern));
  1831. EmitOperand(COperand2D(D3D10_SB_OPERAND_TYPE_CONSTANT_BUFFER, RegIndex, Size));
  1832. ENDINSTRUCTION();
  1833. }
  1834. // Emit D3D12 constant buffer (cb#) declaration.
  1835. void EmitIndexableConstantBufferDecl(UINT uCBufferVarIndex, UINT uLB, UINT uUB,
  1836. UINT Size, // size 0 means unknown/any size
  1837. D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN AccessPattern,
  1838. UINT uSpace)
  1839. {
  1840. m_bExecutableInstruction = FALSE;
  1841. OPCODE( ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_CONSTANT_BUFFER) |
  1842. ENCODE_D3D10_SB_D3D10_SB_CONSTANT_BUFFER_ACCESS_PATTERN(AccessPattern) );
  1843. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_CONSTANT_BUFFER, uCBufferVarIndex, uLB, uUB));
  1844. FUNC(Size);
  1845. FUNC(uSpace);
  1846. ENDINSTRUCTION();
  1847. }
  1848. // Emit Immediate Constant Buffer (icb) declaration
  1849. void EmitImmediateConstantBufferDecl(UINT Num4Tuples, const UINT* pImmediateConstantBufferData)
  1850. {
  1851. m_bExecutableInstruction = FALSE;
  1852. EmitCustomData( D3D10_SB_CUSTOMDATA_DCL_IMMEDIATE_CONSTANT_BUFFER,
  1853. 4*Num4Tuples /*2 UINTS will be added during encoding */,
  1854. pImmediateConstantBufferData);
  1855. }
  1856. // Emit a GS input primitive declaration
  1857. void EmitGSInputPrimitiveDecl(D3D10_SB_PRIMITIVE Primitive)
  1858. {
  1859. m_bExecutableInstruction = FALSE;
  1860. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_GS_INPUT_PRIMITIVE) |
  1861. ENCODE_D3D10_SB_GS_INPUT_PRIMITIVE(Primitive));
  1862. ENDINSTRUCTION();
  1863. }
  1864. // Emit a GS output topology declaration
  1865. void EmitGSOutputTopologyDecl(D3D10_SB_PRIMITIVE_TOPOLOGY Topology)
  1866. {
  1867. m_bExecutableInstruction = FALSE;
  1868. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY) |
  1869. ENCODE_D3D10_SB_GS_OUTPUT_PRIMITIVE_TOPOLOGY(Topology));
  1870. ENDINSTRUCTION();
  1871. }
  1872. // Emit GS Maximum Output Vertex Count declaration
  1873. void EmitGSMaxOutputVertexCountDecl(UINT Count)
  1874. {
  1875. m_bExecutableInstruction = FALSE;
  1876. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT));
  1877. FUNC((UINT)Count);
  1878. ENDINSTRUCTION();
  1879. }
  1880. // Emit input GS instance count declaration
  1881. void EmitInputGSInstanceCountDecl( UINT Instances )
  1882. {
  1883. m_bExecutableInstruction = FALSE;
  1884. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_GS_INSTANCE_COUNT));
  1885. FUNC(Instances);
  1886. ENDINSTRUCTION();
  1887. }
  1888. // Emit input GS instance ID declaration
  1889. void EmitInputGSInstanceIDDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  1890. {
  1891. m_bExecutableInstruction = FALSE;
  1892. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  1893. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_INPUT_GS_INSTANCE_ID, MinPrecision));
  1894. ENDINSTRUCTION();
  1895. }
  1896. // Emit global flags declaration
  1897. void EmitGlobalFlagsDecl(UINT Flags)
  1898. {
  1899. m_bExecutableInstruction = FALSE;
  1900. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_GLOBAL_FLAGS) |
  1901. ENCODE_D3D10_SB_GLOBAL_FLAGS(Flags));
  1902. ENDINSTRUCTION();
  1903. }
  1904. // Emit interface function body declaration
  1905. void EmitFunctionBodyDecl(UINT uFunctionID)
  1906. {
  1907. m_bExecutableInstruction = FALSE;
  1908. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_FUNCTION_BODY));
  1909. FUNC(uFunctionID);
  1910. ENDINSTRUCTION();
  1911. }
  1912. void EmitFunctionTableDecl(UINT uFunctionTableID, UINT uTableSize, UINT *pTableEntries)
  1913. {
  1914. m_bExecutableInstruction = FALSE;
  1915. bool bExtended = (3 + uTableSize) > MAX_INSTRUCTION_LENGTH;
  1916. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_FUNCTION_TABLE) |
  1917. ENCODE_D3D10_SB_OPCODE_EXTENDED(bExtended));
  1918. if( bExtended )
  1919. FUNC(0);
  1920. FUNC(uFunctionTableID);
  1921. FUNC(uTableSize);
  1922. if( m_Index + uTableSize >= m_BufferSize )
  1923. {
  1924. Reserve(uTableSize);
  1925. }
  1926. memcpy(&m_dwFunc[m_Index],pTableEntries,sizeof(UINT)*uTableSize);
  1927. m_Index += uTableSize;
  1928. ENDLONGINSTRUCTION(bExtended);
  1929. }
  1930. void EmitInterfaceDecl(UINT uInterfaceID,
  1931. bool bDynamicIndexed,
  1932. UINT uArrayLength,
  1933. UINT uExpectedTableSize,
  1934. __in_range(0, D3D11_SB_MAX_NUM_TYPES) UINT uNumTypes,
  1935. __in_ecount(uNumTypes) UINT *pTableEntries)
  1936. {
  1937. m_bExecutableInstruction = FALSE;
  1938. bool bExtended = (4 + uNumTypes) > MAX_INSTRUCTION_LENGTH;
  1939. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_INTERFACE) |
  1940. ENCODE_D3D11_SB_INTERFACE_INDEXED_BIT(bDynamicIndexed) |
  1941. ENCODE_D3D10_SB_OPCODE_EXTENDED(bExtended));
  1942. if( bExtended )
  1943. FUNC(0);
  1944. FUNC(uInterfaceID);
  1945. FUNC(uExpectedTableSize);
  1946. FUNC(ENCODE_D3D11_SB_INTERFACE_TABLE_LENGTH(uNumTypes) |
  1947. ENCODE_D3D11_SB_INTERFACE_ARRAY_LENGTH(uArrayLength));
  1948. if( m_Index + uNumTypes >= m_BufferSize )
  1949. {
  1950. Reserve(uNumTypes);
  1951. }
  1952. memcpy(&m_dwFunc[m_Index],pTableEntries,sizeof(UINT)*uNumTypes);
  1953. m_Index += uNumTypes;
  1954. ENDLONGINSTRUCTION(bExtended);
  1955. }
  1956. void EmitInterfaceCall(COperandBase &InterfaceOperand,
  1957. UINT uFunctionIndex)
  1958. {
  1959. m_bExecutableInstruction = TRUE;
  1960. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_INTERFACE_CALL));
  1961. FUNC(uFunctionIndex);
  1962. EmitOperand(InterfaceOperand);
  1963. ENDINSTRUCTION();
  1964. }
  1965. void EmitInputControlPointCountDecl(UINT Count)
  1966. {
  1967. m_bExecutableInstruction = FALSE;
  1968. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_INPUT_CONTROL_POINT_COUNT) |
  1969. ENCODE_D3D11_SB_INPUT_CONTROL_POINT_COUNT(Count));
  1970. ENDINSTRUCTION();
  1971. }
  1972. void EmitOutputControlPointCountDecl(UINT Count)
  1973. {
  1974. m_bExecutableInstruction = FALSE;
  1975. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_OUTPUT_CONTROL_POINT_COUNT) |
  1976. ENCODE_D3D11_SB_OUTPUT_CONTROL_POINT_COUNT(Count));
  1977. ENDINSTRUCTION();
  1978. }
  1979. void EmitTessellatorDomainDecl(D3D11_SB_TESSELLATOR_DOMAIN Domain)
  1980. {
  1981. m_bExecutableInstruction = FALSE;
  1982. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_TESS_DOMAIN) |
  1983. ENCODE_D3D11_SB_TESS_DOMAIN(Domain));
  1984. ENDINSTRUCTION();
  1985. }
  1986. void EmitTessellatorPartitioningDecl(D3D11_SB_TESSELLATOR_PARTITIONING Partitioning)
  1987. {
  1988. m_bExecutableInstruction = FALSE;
  1989. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_TESS_PARTITIONING) |
  1990. ENCODE_D3D11_SB_TESS_PARTITIONING(Partitioning));
  1991. ENDINSTRUCTION();
  1992. }
  1993. void EmitTessellatorOutputPrimitiveDecl(D3D11_SB_TESSELLATOR_OUTPUT_PRIMITIVE OutputPrimitive)
  1994. {
  1995. m_bExecutableInstruction = FALSE;
  1996. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_TESS_OUTPUT_PRIMITIVE) |
  1997. ENCODE_D3D11_SB_TESS_OUTPUT_PRIMITIVE(OutputPrimitive));
  1998. ENDINSTRUCTION();
  1999. }
  2000. void EmitHSMaxTessFactorDecl(float MaxTessFactor)
  2001. {
  2002. m_bExecutableInstruction = FALSE;
  2003. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_HS_MAX_TESSFACTOR));
  2004. UINT uTemp = *(UINT*)&MaxTessFactor;
  2005. FUNC(uTemp);
  2006. ENDINSTRUCTION();
  2007. }
  2008. void EmitHSForkPhaseInstanceCountDecl(UINT InstanceCount)
  2009. {
  2010. m_bExecutableInstruction = FALSE;
  2011. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT));
  2012. FUNC(InstanceCount);
  2013. ENDINSTRUCTION();
  2014. }
  2015. void EmitHSJoinPhaseInstanceCountDecl(UINT InstanceCount)
  2016. {
  2017. m_bExecutableInstruction = FALSE;
  2018. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_HS_JOIN_PHASE_INSTANCE_COUNT));
  2019. FUNC(InstanceCount);
  2020. ENDINSTRUCTION();
  2021. }
  2022. void EmitHSBeginPhase(D3D10_SB_OPCODE_TYPE Phase)
  2023. {
  2024. m_bExecutableInstruction = FALSE;
  2025. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(Phase));
  2026. ENDINSTRUCTION();
  2027. }
  2028. void EmitInputOutputControlPointIDDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2029. {
  2030. m_bExecutableInstruction = FALSE;
  2031. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2032. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID, MinPrecision));
  2033. ENDINSTRUCTION();
  2034. }
  2035. void EmitInputForkInstanceIDDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2036. {
  2037. m_bExecutableInstruction = FALSE;
  2038. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2039. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_INPUT_FORK_INSTANCE_ID, MinPrecision));
  2040. ENDINSTRUCTION();
  2041. }
  2042. void EmitInputJoinInstanceIDDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2043. {
  2044. m_bExecutableInstruction = FALSE;
  2045. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2046. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_INPUT_JOIN_INSTANCE_ID, MinPrecision));
  2047. ENDINSTRUCTION();
  2048. }
  2049. void EmitThreadGroupDecl(UINT x, UINT y, UINT z)
  2050. {
  2051. m_bExecutableInstruction = FALSE;
  2052. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_THREAD_GROUP));
  2053. FUNC(x);
  2054. FUNC(y);
  2055. FUNC(z);
  2056. ENDINSTRUCTION();
  2057. }
  2058. void EmitInputThreadIDDecl(UINT WriteMask,
  2059. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2060. {
  2061. m_bExecutableInstruction = FALSE;
  2062. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2063. EmitOperand(COperandDst(WriteMask, D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID, MinPrecision));
  2064. ENDINSTRUCTION();
  2065. }
  2066. void EmitInputThreadGroupIDDecl(UINT WriteMask,
  2067. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2068. {
  2069. m_bExecutableInstruction = FALSE;
  2070. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2071. EmitOperand(COperandDst(WriteMask, D3D11_SB_OPERAND_TYPE_INPUT_THREAD_GROUP_ID, MinPrecision));
  2072. ENDINSTRUCTION();
  2073. }
  2074. void EmitInputThreadIDInGroupDecl(UINT WriteMask,
  2075. D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2076. {
  2077. m_bExecutableInstruction = FALSE;
  2078. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2079. EmitOperand(COperandDst(WriteMask, D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP, MinPrecision));
  2080. ENDINSTRUCTION();
  2081. }
  2082. void EmitInputThreadIDInGroupFlattenedDecl(D3D11_SB_OPERAND_MIN_PRECISION MinPrecision = D3D11_SB_OPERAND_MIN_PRECISION_DEFAULT)
  2083. {
  2084. m_bExecutableInstruction = FALSE;
  2085. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D10_SB_OPCODE_DCL_INPUT));
  2086. EmitOperand(COperandDst(D3D11_SB_OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP_FLATTENED, MinPrecision));
  2087. ENDINSTRUCTION();
  2088. }
  2089. void EmitTypedUnorderedAccessViewDecl(D3D10_SB_RESOURCE_DIMENSION Dimension, UINT URegIndex,
  2090. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  2091. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  2092. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  2093. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW,
  2094. UINT Flags)
  2095. {
  2096. m_bExecutableInstruction = FALSE;
  2097. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_TYPED) |
  2098. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) |
  2099. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2100. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, URegIndex));
  2101. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  2102. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  2103. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  2104. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  2105. ENDINSTRUCTION();
  2106. }
  2107. // Emit D3D12 UAV declaration.
  2108. void EmitIndexableTypedUnorderedAccessViewDecl(UINT uTable, UINT uTableLB, UINT uTableUB,
  2109. D3D10_SB_RESOURCE_DIMENSION Dimension,
  2110. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForX,
  2111. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForY,
  2112. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForZ,
  2113. D3D10_SB_RESOURCE_RETURN_TYPE ReturnTypeForW,
  2114. UINT Flags,
  2115. UINT uSpace)
  2116. {
  2117. m_bExecutableInstruction = FALSE;
  2118. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_TYPED) |
  2119. ENCODE_D3D10_SB_RESOURCE_DIMENSION(Dimension) |
  2120. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2121. EmitOperand(COperand3D(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, uTable, uTableLB, uTableUB));
  2122. FUNC(ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForX, 0) |
  2123. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForY, 1) |
  2124. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForZ, 2) |
  2125. ENCODE_D3D10_SB_RESOURCE_RETURN_TYPE(ReturnTypeForW, 3));
  2126. FUNC(uSpace);
  2127. ENDINSTRUCTION();
  2128. }
  2129. void EmitRawUnorderedAccessViewDecl(UINT URegIndex, UINT Flags)
  2130. {
  2131. m_bExecutableInstruction = FALSE;
  2132. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_RAW) |
  2133. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2134. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, URegIndex));
  2135. ENDINSTRUCTION();
  2136. }
  2137. // Emit D3D12 raw UAV declaration.
  2138. void EmitIndexableRawUnorderedAccessViewDecl(UINT uTable, UINT uTableLB, UINT uTableUB, UINT Flags, UINT uSpace)
  2139. {
  2140. m_bExecutableInstruction = FALSE;
  2141. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_RAW) |
  2142. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2143. EmitOperand(COperand3D(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, uTable, uTableLB, uTableUB));
  2144. FUNC(uSpace);
  2145. ENDINSTRUCTION();
  2146. }
  2147. void EmitStructuredUnorderedAccessViewDecl(UINT URegIndex, UINT ByteStride, UINT Flags )
  2148. {
  2149. m_bExecutableInstruction = FALSE;
  2150. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED)|
  2151. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2152. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, URegIndex));
  2153. FUNC(ByteStride);
  2154. ENDINSTRUCTION();
  2155. }
  2156. // Emit D3D12 structured UAV declaration.
  2157. void EmitIndexableStructuredUnorderedAccessViewDecl(UINT uTable, UINT uTableLB, UINT uTableUB,
  2158. UINT ByteStride, UINT Flags, UINT uSpace )
  2159. {
  2160. m_bExecutableInstruction = FALSE;
  2161. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED)|
  2162. ENCODE_D3D11_SB_RESOURCE_FLAGS(Flags));
  2163. EmitOperand(COperand3D(D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW, uTable, uTableLB, uTableUB));
  2164. FUNC(ByteStride);
  2165. FUNC(uSpace);
  2166. ENDINSTRUCTION();
  2167. }
  2168. void EmitRawThreadGroupSharedMemoryDecl(UINT GRegIndex, UINT ByteCount )
  2169. {
  2170. m_bExecutableInstruction = FALSE;
  2171. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_RAW));
  2172. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY, GRegIndex));
  2173. FUNC(ByteCount);
  2174. ENDINSTRUCTION();
  2175. }
  2176. void EmitStructuredThreadGroupSharedMemoryDecl(UINT GRegIndex, UINT ByteStride, UINT StructCount )
  2177. {
  2178. m_bExecutableInstruction = FALSE;
  2179. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED));
  2180. EmitOperand(COperand(D3D11_SB_OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY, GRegIndex));
  2181. FUNC(ByteStride);
  2182. FUNC(StructCount);
  2183. ENDINSTRUCTION();
  2184. }
  2185. void EmitRawShaderResourceViewDecl(UINT TRegIndex)
  2186. {
  2187. m_bExecutableInstruction = FALSE;
  2188. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_RESOURCE_RAW));
  2189. EmitOperand(COperand(D3D10_SB_OPERAND_TYPE_RESOURCE, TRegIndex));
  2190. ENDINSTRUCTION();
  2191. }
  2192. // Emit D3D12 byte address buffer declaration
  2193. void EmitIndexableRawShaderResourceViewDecl(UINT uTable, UINT uTableLB, UINT uTableUB, UINT uSpace)
  2194. {
  2195. m_bExecutableInstruction = FALSE;
  2196. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_RESOURCE_RAW));
  2197. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_RESOURCE, uTable, uTableLB, uTableUB));
  2198. FUNC(uSpace);
  2199. ENDINSTRUCTION();
  2200. }
  2201. void EmitStructuredShaderResourceViewDecl(UINT TRegIndex, UINT ByteStride)
  2202. {
  2203. m_bExecutableInstruction = FALSE;
  2204. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED));
  2205. EmitOperand(COperand(D3D10_SB_OPERAND_TYPE_RESOURCE, TRegIndex));
  2206. FUNC(ByteStride);
  2207. ENDINSTRUCTION();
  2208. }
  2209. // Emit D3D12 structured buffer declaration
  2210. void EmitIndexableStructuredShaderResourceViewDecl(UINT uTable, UINT uTableLB, UINT uTableUB, UINT ByteStride, UINT uSpace)
  2211. {
  2212. m_bExecutableInstruction = FALSE;
  2213. OPCODE(ENCODE_D3D10_SB_OPCODE_TYPE(D3D11_SB_OPCODE_DCL_RESOURCE_STRUCTURED));
  2214. EmitOperand(COperand3D(D3D10_SB_OPERAND_TYPE_RESOURCE, uTable, uTableLB, uTableUB));
  2215. FUNC(ByteStride);
  2216. FUNC(uSpace);
  2217. ENDINSTRUCTION();
  2218. }
  2219. // Emit an instruction. Custom-data is not handled by this function.
  2220. void EmitInstruction(const CInstruction& instruction);
  2221. // Emit an operand
  2222. void EmitOperand(const COperandBase& operand);
  2223. // Emit an instruction without operands
  2224. void Emit(UINT OpCode)
  2225. {
  2226. OPCODE(OpCode);
  2227. ENDINSTRUCTION();
  2228. }
  2229. void StartComplexEmit(UINT OpCode,
  2230. UINT ReserveCount = MAX_INSTRUCTION_LENGTH)
  2231. {
  2232. OPCODE(OpCode);
  2233. Reserve(ReserveCount);
  2234. }
  2235. void AddComplexEmit(UINT Data)
  2236. {
  2237. FUNC(Data);
  2238. }
  2239. void EndComplexEmit(bool bPatchLength = false)
  2240. {
  2241. ENDLONGINSTRUCTION(bPatchLength, !bPatchLength);
  2242. }
  2243. UINT GetComplexEmitPosition()
  2244. {
  2245. return m_Index;
  2246. }
  2247. void UpdateComplexEmitPosition(UINT Pos,
  2248. UINT Data)
  2249. {
  2250. if (Pos < m_BufferSize)
  2251. {
  2252. m_dwFunc[Pos] = Data;
  2253. }
  2254. }
  2255. void EmitCustomData( D3D10_SB_CUSTOMDATA_CLASS CustomDataClass,
  2256. UINT SizeInUINTs /*2 UINTS will be added during encoding */,
  2257. const UINT* pCustomData)
  2258. {
  2259. UINT FullSizeInUINTs = SizeInUINTs + 2; // include opcode and size
  2260. if( FullSizeInUINTs < SizeInUINTs || FullSizeInUINTs + m_Index < FullSizeInUINTs ) // check for overflow
  2261. {
  2262. throw E_FAIL;
  2263. }
  2264. if( m_Index + FullSizeInUINTs >= m_BufferSize ) // If custom data is going to overflow the buffer, reserve more memory
  2265. {
  2266. Reserve(FullSizeInUINTs);
  2267. }
  2268. __analysis_assume(m_Index + FullSizeInUINTs < m_BufferSize); // Otherwise there's a bug in Reserve()
  2269. m_dwFunc[m_Index++] = ENCODE_D3D10_SB_CUSTOMDATA_CLASS(CustomDataClass);
  2270. m_dwFunc[m_Index++] = FullSizeInUINTs;
  2271. memcpy(&m_dwFunc[m_Index],pCustomData,sizeof(UINT)*SizeInUINTs);
  2272. m_Index += SizeInUINTs;
  2273. if (m_Index >= m_BufferSize) // If custom data is exactly fully filled the buffer, reserve more memory
  2274. {
  2275. Reserve(1024);
  2276. }
  2277. }
  2278. // Returns number of executable instructions in the current shader
  2279. UINT GetNumExecutableInstructions() {return m_NumExecutableInstructions;}
  2280. protected:
  2281. void OPCODE(UINT x)
  2282. {
  2283. if (m_Index < m_BufferSize)
  2284. {
  2285. m_dwFunc[m_Index] = x;
  2286. m_StartOpIndex = m_Index++;
  2287. }
  2288. if (m_Index >= m_BufferSize)
  2289. Reserve(1024);
  2290. }
  2291. // Should be called after end of each instruction
  2292. void ENDINSTRUCTION()
  2293. {
  2294. if (m_StartOpIndex < m_Index)
  2295. {
  2296. m_dwFunc[m_StartOpIndex] |= ENCODE_D3D10_SB_TOKENIZED_INSTRUCTION_LENGTH(m_Index - m_StartOpIndex);
  2297. Reserve(MAX_INSTRUCTION_LENGTH);
  2298. m_StatementIndex++;
  2299. if (m_bExecutableInstruction)
  2300. m_NumExecutableInstructions++;
  2301. m_bExecutableInstruction = true;
  2302. }
  2303. }
  2304. void ENDLONGINSTRUCTION(bool bExtendedLength,
  2305. bool bBaseLength = true)
  2306. {
  2307. if (m_StartOpIndex < m_Index)
  2308. {
  2309. if (bBaseLength)
  2310. {
  2311. m_dwFunc[m_StartOpIndex] |= ENCODE_D3D10_SB_TOKENIZED_INSTRUCTION_LENGTH(m_Index - m_StartOpIndex);
  2312. }
  2313. if( bExtendedLength )
  2314. {
  2315. __analysis_assume(m_StartOpIndex + 1 < m_Index);
  2316. m_dwFunc[m_StartOpIndex + 1] = m_Index - m_StartOpIndex;
  2317. }
  2318. Reserve(MAX_INSTRUCTION_LENGTH);
  2319. m_StatementIndex++;
  2320. if (m_bExecutableInstruction)
  2321. m_NumExecutableInstructions++;
  2322. m_bExecutableInstruction = true;
  2323. }
  2324. }
  2325. void FUNC(UINT x)
  2326. {
  2327. if (m_Index < m_BufferSize)
  2328. m_dwFunc[m_Index++] = x;
  2329. if (m_Index >= m_BufferSize)
  2330. Reserve(1024);
  2331. }
  2332. // Prepare assembler for a new shader
  2333. void Reset()
  2334. {
  2335. m_Index = 0;
  2336. m_StartOpIndex = 0;
  2337. m_StatementIndex = 1;
  2338. m_NumExecutableInstructions = 0;
  2339. m_bExecutableInstruction = TRUE;
  2340. }
  2341. // Reserve SizeInUINTs UINTs in the m_dwFunc array
  2342. void Reserve(UINT SizeInUINTs)
  2343. {
  2344. // The following overflow check may be sligltly over-cautious when (m_Index + SizeInUINTs < m_BufferSize),
  2345. // but this only matters when m_BufferSize cannot be grown one more step without overflowing.
  2346. UINT NewSize = m_BufferSize + SizeInUINTs + 1024;
  2347. if( m_Index > m_BufferSize || // invalid state
  2348. (m_BufferSize + SizeInUINTs) < m_BufferSize || // overflow with adding SizeInUINTs
  2349. NewSize < (m_BufferSize + SizeInUINTs) ) // overflow with adding 1024
  2350. {
  2351. throw E_FAIL;
  2352. }
  2353. if (m_BufferSize < (m_Index + SizeInUINTs))
  2354. {
  2355. UINT* pNewBuffer = (UINT*)malloc(NewSize*sizeof(UINT));
  2356. if (pNewBuffer == NULL)
  2357. {
  2358. throw E_OUTOFMEMORY;
  2359. }
  2360. memcpy(pNewBuffer, m_dwFunc, sizeof(UINT)*m_Index);
  2361. free(m_dwFunc);
  2362. m_dwFunc = pNewBuffer;
  2363. m_BufferSize = NewSize;
  2364. }
  2365. }
  2366. // Buffer where the binary representation is built
  2367. __field_ecount_part(m_BufferSize, m_Index) UINT* m_dwFunc;
  2368. // Index where to place the next token in the m_dwFunc array
  2369. UINT m_Index;
  2370. // Index of the start of the current instruction in the m_dwFunc array
  2371. UINT m_StartOpIndex;
  2372. // Current buffer size in UINTs
  2373. UINT m_BufferSize;
  2374. // Current statement index of the current vertex shader
  2375. UINT m_StatementIndex;
  2376. // Number of executable instructions in the shader
  2377. UINT m_NumExecutableInstructions;
  2378. // "true" when the current instruction is executable
  2379. bool m_bExecutableInstruction;
  2380. };
  2381. //*****************************************************************************
  2382. //
  2383. // CShaderCodeParser
  2384. //
  2385. //*****************************************************************************
  2386. class CShaderCodeParser
  2387. {
  2388. public:
  2389. CShaderCodeParser():
  2390. m_pCurrentToken(NULL),
  2391. m_pShaderCode(NULL),
  2392. m_pShaderEndToken(NULL)
  2393. {
  2394. InitInstructionInfo();
  2395. }
  2396. CShaderCodeParser(CONST CShaderToken* pBuffer):
  2397. m_pCurrentToken(NULL),
  2398. m_pShaderCode(NULL),
  2399. m_pShaderEndToken(NULL)
  2400. {
  2401. InitInstructionInfo();
  2402. SetShader(pBuffer);
  2403. }
  2404. ~CShaderCodeParser() {}
  2405. void SetShader(CONST CShaderToken* pBuffer);
  2406. void ParseInstruction(CInstruction* pInstruction);
  2407. void ParseIndex(COperandIndex* pOperandIndex, D3D10_SB_OPERAND_INDEX_REPRESENTATION IndexType);
  2408. void ParseOperand(COperandBase* pOperand);
  2409. BOOL EndOfShader() {return m_pCurrentToken >= m_pShaderEndToken;}
  2410. D3D10_SB_TOKENIZED_PROGRAM_TYPE ShaderType();
  2411. UINT ShaderMinorVersion();
  2412. UINT ShaderMajorVersion();
  2413. UINT ShaderLengthInTokens();
  2414. UINT CurrentTokenOffset();
  2415. UINT CurrentTokenOffsetInBytes() { return CurrentTokenOffset() * sizeof(CShaderToken); }
  2416. void SetCurrentTokenOffset(UINT Offset);
  2417. CONST CShaderToken* ParseOperandAt(COperandBase* pOperand,
  2418. CONST CShaderToken* pBuffer,
  2419. CONST CShaderToken* pBufferEnd)
  2420. {
  2421. CShaderToken* pCurTok = m_pCurrentToken;
  2422. CShaderToken* pEndTok = m_pShaderEndToken;
  2423. CShaderToken* pRet;
  2424. m_pCurrentToken = (CShaderToken*)pBuffer;
  2425. m_pShaderEndToken = (CShaderToken*)pBufferEnd;
  2426. ParseOperand(pOperand);
  2427. pRet = m_pCurrentToken;
  2428. m_pCurrentToken = pCurTok;
  2429. m_pShaderEndToken = pEndTok;
  2430. return pRet;
  2431. }
  2432. protected:
  2433. CShaderToken* m_pCurrentToken;
  2434. CShaderToken* m_pShaderCode;
  2435. // Points to the last token of the current shader
  2436. CShaderToken* m_pShaderEndToken;
  2437. };
  2438. }; // name space D3D10ShaderBinary