Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
9.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) Microsoft Corporation, 2000.
  3. //
  4. // valbase.hpp
  5. //
  6. // Direct3D Reference Device - Vertex/PixelShader validation common infrastructure
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef __VALBASE_HPP__
  10. #define __VALBASE_HPP__
  11. #define NUM_COMPONENTS_IN_REGISTER 4
  12. #define SHADER_INSTRUCTION_MAX_PARAMS 4
  13. #define SHADER_INSTRUCTION_MAX_SRCPARAMS (SHADER_INSTRUCTION_MAX_PARAMS - 1)
  14. typedef enum _SPEW_TYPE
  15. {
  16. SPEW_INSTRUCTION_ERROR,
  17. SPEW_INSTRUCTION_WARNING,
  18. SPEW_GLOBAL_ERROR,
  19. SPEW_GLOBAL_WARNING
  20. } SPEW_TYPE;
  21. typedef enum _SHADER_VALIDATOR_FLAGS
  22. {
  23. SHADER_VALIDATOR_LOG_ERRORS = 0x1,
  24. SHADER_VALIDATOR_OPTIMIZE_WRITEMASKS = 0x2
  25. } SHADER_VALIDATOR_FLAGS;
  26. typedef enum _DSTSHIFT
  27. {
  28. DSTSHIFT_NONE = 0x0,
  29. DSTSHIFT_X2 = 0x1,
  30. DSTSHIFT_X4 = 0x2,
  31. DSTSHIFT_X8 = 0x3,
  32. DSTSHIFT_D2 = 0xF,
  33. DSTSHIFT_D4 = 0xE,
  34. DSTSHIFT_D8 = 0xD
  35. } DSTSHIFT;
  36. const DWORD COMPONENT_MASKS[4] = {D3DSP_WRITEMASK_0, D3DSP_WRITEMASK_1, D3DSP_WRITEMASK_2, D3DSP_WRITEMASK_3};
  37. //-----------------------------------------------------------------------------
  38. // DSTPARAM - part of CBaseInstruction
  39. //-----------------------------------------------------------------------------
  40. class DSTPARAM
  41. {
  42. public:
  43. DSTPARAM();
  44. BOOL m_bParamUsed; // Does instruction have dest param?
  45. UINT m_RegNum;
  46. DWORD m_WriteMask; // writemasks (D3DSP_WRITEMASK_*)
  47. D3DSHADER_PARAM_DSTMOD_TYPE m_DstMod; // D3DSPDM_NONE, D3DSPDM_SATURATE (PShader)
  48. DSTSHIFT m_DstShift; // _x2, _x4, etc. (PShader)
  49. D3DSHADER_PARAM_REGISTER_TYPE m_RegType; // _TEMP, _ADDRESS, etc.
  50. DWORD m_ComponentReadMask; // Which components instruction needs to read
  51. // This seems strage, but in ps.2.0 there are some ops
  52. // that have one parameter (dest), but they read from it, and write back to it.
  53. };
  54. //-----------------------------------------------------------------------------
  55. // SRCPARAM - part of CBaseInstruction
  56. //-----------------------------------------------------------------------------
  57. class SRCPARAM
  58. {
  59. public:
  60. SRCPARAM();
  61. BOOL m_bParamUsed; // Does instruction have this src param?
  62. UINT m_RegNum;
  63. DWORD m_SwizzleShift; // D3DVS_*_*, or D3DSP_NOSWIZZLE/
  64. // D3DSP_REPLICATERED/GREEN/BLUE/ALPHA
  65. D3DVS_ADDRESSMODE_TYPE m_AddressMode; // D3DVS_ADDRMODE_ABSOLUTE / _RELATIVE (VShader)
  66. DWORD m_RelativeAddrComponent; // One of D3DSP_WRITEMASK_0, 1, 2, or 3. (VShader)
  67. D3DSHADER_PARAM_SRCMOD_TYPE m_SrcMod; // _NEG, _BIAS, etc.
  68. D3DSHADER_PARAM_REGISTER_TYPE m_RegType; // _TEMP, _CONST, etc.
  69. DWORD m_ComponentReadMask; // Which components instruction needs to read
  70. };
  71. //-----------------------------------------------------------------------------
  72. // CBaseInstruction
  73. //-----------------------------------------------------------------------------
  74. class CBaseInstruction
  75. {
  76. public:
  77. CBaseInstruction(CBaseInstruction* pPrevInst); // Append to linked list
  78. void SetSpewFileNameAndLineNumber(const char* pFileName, const DWORD* pLineNumber);
  79. char* MakeInstructionLocatorString();
  80. virtual void CalculateComponentReadMasks(DWORD dwVersion) = 0; // which components to each source read?
  81. // Instruction Description
  82. D3DSHADER_INSTRUCTION_OPCODE_TYPE m_Type;
  83. UINT m_DstParamCount;
  84. UINT m_SrcParamCount;
  85. CBaseInstruction* m_pPrevInst;
  86. CBaseInstruction* m_pNextInst;
  87. const DWORD* m_pSpewLineNumber; // points to line number embedded in shader by assembler (if present)
  88. const char* m_pSpewFileName; // points to file name embedded in shader (if present)
  89. UINT m_SpewInstructionCount; // only used for spew, not for any limit checking
  90. // Destination Parameter Description
  91. DSTPARAM m_DstParam;
  92. // Source Parameters
  93. SRCPARAM m_SrcParam[SHADER_INSTRUCTION_MAX_SRCPARAMS];
  94. };
  95. //-----------------------------------------------------------------------------
  96. // CAccessHistoryNode
  97. //-----------------------------------------------------------------------------
  98. class CAccessHistoryNode
  99. {
  100. public:
  101. CAccessHistoryNode(CAccessHistoryNode* pPreviousAccess,
  102. CAccessHistoryNode* pPreviousWriter,
  103. CAccessHistoryNode* pPreviousReader,
  104. CBaseInstruction* pInst,
  105. BOOL bWrite );
  106. CAccessHistoryNode* m_pPreviousAccess;
  107. CAccessHistoryNode* m_pNextAccess;
  108. CAccessHistoryNode* m_pPreviousWriter;
  109. CAccessHistoryNode* m_pPreviousReader;
  110. CBaseInstruction* m_pInst;
  111. BOOL m_bWrite;
  112. BOOL m_bRead;
  113. };
  114. //-----------------------------------------------------------------------------
  115. // CAccessHistory
  116. //-----------------------------------------------------------------------------
  117. class CAccessHistory
  118. {
  119. public:
  120. CAccessHistory();
  121. ~CAccessHistory();
  122. CAccessHistoryNode* m_pFirstAccess;
  123. CAccessHistoryNode* m_pMostRecentAccess;
  124. CAccessHistoryNode* m_pMostRecentWriter;
  125. CAccessHistoryNode* m_pMostRecentReader;
  126. BOOL m_bPreShaderInitialized;
  127. BOOL NewAccess(CBaseInstruction* pInst, BOOL bWrite );
  128. BOOL InsertReadBeforeWrite(CAccessHistoryNode* pWriteNode, CBaseInstruction* pInst);
  129. };
  130. //-----------------------------------------------------------------------------
  131. // CRegisterFile
  132. //-----------------------------------------------------------------------------
  133. class CRegisterFile
  134. {
  135. UINT m_NumRegisters;
  136. BOOL m_bWritable;
  137. UINT m_NumReadPorts;
  138. BOOL m_bInitOk;
  139. public:
  140. CRegisterFile(UINT NumRegisters, BOOL bWritable, UINT NumReadPorts, BOOL bPreShaderInitialized );
  141. ~CRegisterFile();
  142. inline UINT GetNumRegs() {return m_NumRegisters;};
  143. inline BOOL IsWritable() {return m_bWritable;};
  144. inline UINT GetNumReadPorts() {return m_NumReadPorts;};
  145. inline BOOL InitOk() {return m_bInitOk;};
  146. CAccessHistory* m_pAccessHistory[NUM_COMPONENTS_IN_REGISTER];
  147. };
  148. //-----------------------------------------------------------------------------
  149. // CBaseShaderValidator
  150. //-----------------------------------------------------------------------------
  151. class CBaseShaderValidator
  152. {
  153. protected:
  154. BOOL m_bBaseInitOk;
  155. DWORD m_Version;
  156. UINT m_SpewInstructionCount; // only used for spew, not for any limit checking
  157. CBaseInstruction* m_pInstructionList;
  158. const DWORD* m_pCurrToken;
  159. HRESULT m_ReturnCode;
  160. BOOL m_bSeenAllInstructions;
  161. DWORD m_ErrorCount;
  162. CErrorLog* m_pLog;
  163. CBaseInstruction* m_pCurrInst;
  164. const D3DCAPS8* m_pCaps; // can be NULL if not provided.
  165. const DWORD* m_pLatestSpewLineNumber; // points to latest line number sent in comment from D3DX Assembler
  166. const char* m_pLatestSpewFileName; // points to latest file name sent in comment from D3DX Assembler
  167. // m_bSrcParamError needed by Rule_SrcInitialized (in both vshader and pshader)
  168. BOOL m_bSrcParamError[SHADER_INSTRUCTION_MAX_SRCPARAMS];
  169. virtual BOOL DecodeNextInstruction() = 0;
  170. void DecodeDstParam( DSTPARAM* pDstParam, DWORD Token );
  171. void DecodeSrcParam( SRCPARAM* pSrcParam, DWORD Token );
  172. virtual BOOL InitValidation() = 0;
  173. void ValidateShader();
  174. virtual BOOL ApplyPerInstructionRules() = 0;
  175. virtual void ApplyPostInstructionsRules() = 0;
  176. virtual CBaseInstruction* AllocateNewInstruction(CBaseInstruction* pPrevInst) = 0;
  177. void ParseCommentForAssemblerMessages(const DWORD* pComment);
  178. void Spew( SPEW_TYPE SpewType,
  179. CBaseInstruction* pInst /* can be NULL */,
  180. const char* pszFormat, ... );
  181. char* MakeAffectedComponentsText( DWORD ComponentMask,
  182. BOOL bColorLabels = TRUE,
  183. BOOL bPositionLabels = TRUE);
  184. public:
  185. CBaseShaderValidator( const DWORD* pCode, const D3DCAPS8* pCaps, DWORD Flags );
  186. ~CBaseShaderValidator();
  187. DWORD GetRequiredLogBufferSize()
  188. {
  189. if( m_pLog )
  190. return m_pLog->GetRequiredLogBufferSize();
  191. else
  192. return 0;
  193. }
  194. void WriteLogToBuffer( char* pBuffer )
  195. {
  196. if( m_pLog ) m_pLog->WriteLogToBuffer( pBuffer );
  197. }
  198. HRESULT GetStatus() { return m_ReturnCode; };
  199. };
  200. #endif //__VALBASE_HPP__