CommandBufferCompiler.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #ifndef COMPILE_PROPS_INCLUDED
  2. #error CompileProps.h must be included
  3. #endif
  4. #if defined (_XBOX) && defined(_PRECOMPILED_COMMAND_BUFFER_BAKER)
  5. #ifndef _COMMAND_BUFFER_COMPILER___
  6. #define _COMMAND_BUFFER_COMPILER___
  7. #include "..\..\..\common_h\core.h"
  8. #include "..\GraphicsApi.h"
  9. class IFileService;
  10. class precompiledBatch;
  11. class CommandBufferCompiler
  12. {
  13. struct renderState
  14. {
  15. D3DRENDERSTATETYPE state;
  16. DWORD val;
  17. };
  18. struct microcode
  19. {
  20. char* pMicrocodeBody;
  21. XGMICROCODESHADERPARTS parts;
  22. microcode()
  23. {
  24. pMicrocodeBody = NULL;
  25. }
  26. ~microcode()
  27. {
  28. if (pMicrocodeBody)
  29. {
  30. delete [] pMicrocodeBody;
  31. pMicrocodeBody = NULL;
  32. }
  33. }
  34. char* Allocate (dword dwSize)
  35. {
  36. pMicrocodeBody = new char[dwSize];
  37. return pMicrocodeBody;
  38. }
  39. };
  40. struct texture
  41. {
  42. D3DTEXTUREFILTERTYPE minFilter;
  43. D3DTEXTUREFILTERTYPE magFilter;
  44. D3DTEXTUREFILTERTYPE mipFilter;
  45. D3DTEXTUREADDRESS clampU;
  46. D3DTEXTUREADDRESS clampV;
  47. DWORD dwStage;
  48. string variableName;
  49. texture()
  50. {
  51. dwStage = 0;
  52. minFilter = D3DTEXF_LINEAR;
  53. magFilter = D3DTEXF_LINEAR;
  54. mipFilter = D3DTEXF_LINEAR;
  55. clampU = D3DTADDRESS_WRAP;
  56. clampV = D3DTADDRESS_WRAP;
  57. }
  58. };
  59. public:
  60. enum ConstantType
  61. {
  62. CT_UNKNOWN = 0,
  63. //матрица 4x4
  64. CT_MATRIX4x4 = 1,
  65. //float4
  66. CT_FLOAT4 = 2,
  67. //float3 -> float4
  68. CT_FLOAT3 = 3,
  69. //float -> float4
  70. CT_FLOAT = 4,
  71. //bool
  72. CT_BOOL = 5,
  73. //int
  74. CT_INT4 = 6,
  75. CT_FORCE_DWORD = 0x7fffffff, /* force 32-bit size enum */
  76. };
  77. private:
  78. struct shaderConstant
  79. {
  80. bool bPredefinedInShaderBody;
  81. string szName;
  82. CommandBufferCompiler::ConstantType type;
  83. DWORD dwRegisterIndex;
  84. DWORD dwRegisterCounts;
  85. DWORD dwElements;
  86. shaderConstant()
  87. {
  88. bPredefinedInShaderBody = false;
  89. type = CT_UNKNOWN;
  90. dwRegisterIndex = 0;
  91. dwRegisterCounts = 0;
  92. dwElements = 0;
  93. }
  94. shaderConstant(const char * _name, CommandBufferCompiler::ConstantType _type, DWORD _dwRegisterIndex, DWORD _dwRegisterCounts, DWORD _dwElements)
  95. {
  96. bPredefinedInShaderBody = false;
  97. szName = _name;
  98. type = _type;
  99. dwRegisterIndex = _dwRegisterIndex;
  100. dwRegisterCounts = _dwRegisterCounts;
  101. dwElements = _dwElements;
  102. }
  103. };
  104. struct pass
  105. {
  106. array<renderState> r_states;
  107. array<shaderConstant> vs_consts;
  108. array<shaderConstant> ps_consts;
  109. array<texture> textures;
  110. CONST DWORD *pPixelShaderFunc;
  111. CONST DWORD *pVertexShaderFunc;
  112. microcode microcodeVS;
  113. microcode microcodePS;
  114. IDirect3DVertexShader9 *pBoundedVS;
  115. IDirect3DPixelShader9 * pBoundedPS;
  116. pass() : r_states(_FL_),
  117. textures(_FL_),
  118. vs_consts(_FL_),
  119. ps_consts(_FL_)
  120. {
  121. pBoundedVS = NULL;
  122. pBoundedPS = NULL;
  123. }
  124. ~pass()
  125. {
  126. if (pBoundedVS)
  127. {
  128. pBoundedVS->Release();
  129. }
  130. pBoundedVS = NULL;
  131. if (pBoundedPS)
  132. {
  133. pBoundedPS->Release();
  134. }
  135. pBoundedPS = NULL;
  136. }
  137. };
  138. pass shaderStates;
  139. enum MicrocodeType
  140. {
  141. MT_PIXEL_SHADER = 0,
  142. MT_VERTEX_SHADER = 1,
  143. MT_FORCE_DWORD = 0x7fffffff
  144. };
  145. string techName;
  146. IFileService * pFS;
  147. D3DCommandBuffer* commandBuffer;
  148. DWORD dwHeaderSize;
  149. DWORD dwPhysicalSize;
  150. DWORD dwInitializationSize;
  151. DWORD dwUsedSpaceInCmdBuffer;
  152. bool ExtractConstants (CONST DWORD *pShaderFunction, MicrocodeType type);
  153. void ExtractMicrocode(CONST DWORD *pShaderFunction, MicrocodeType type);
  154. void ResetToDefaultStates(D3DDevice* pCommandBufferDevice);
  155. void ParsePredefinedConstants (const char* szShaderBody, array<shaderConstant> & shaderConsts, bool bPixelShader);
  156. public:
  157. CommandBufferCompiler();
  158. ~CommandBufferCompiler();
  159. void SetName (const char* szTechName);
  160. void ExtractPixelShader(CONST DWORD *pShaderFunction);
  161. void ExtractVertexShader(CONST DWORD *pShaderFunction);
  162. void ExtractRenderState (D3DRENDERSTATETYPE state, DWORD dwVal, bool bLog);
  163. void ExtractSamplerState (DWORD sampler, D3DSAMPLERSTATETYPE type, DWORD dwVal);
  164. void PatchShadersAndDumpMicrocode(IDirect3DVertexDeclaration9 * pVDecl, array<DWORD> & vertexDeclStrides);
  165. void Finalize(D3DDevice* pCommandBufferDevice, precompiledBatch * pBatch);
  166. void ExtractTexture (DWORD dwIndex, const char* texvariableName);
  167. };
  168. #endif
  169. #endif