Counter Strike : Global Offensive Source Code
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.

238 lines
8.7 KiB

  1. //------------------------------------------------------------------------------
  2. // DX9AsmToGL2.h
  3. //------------------------------------------------------------------------------
  4. #ifndef DX9_ASM_TO_GL_2_H
  5. #define DX9_ASM_TO_GL_2_H
  6. #include "tier1/utlstring.h"
  7. #define DISASM_OK 0
  8. #define DISASM_ERROR 1
  9. #define MAX_SHADER_CONSTANTS 512
  10. #define MAX_DECLARED_OUTPUTS 32
  11. #define MAX_DECLARED_INPUTS 32
  12. #define HEXCODE_HEADER "// Hex: "
  13. // Option bits
  14. #define D3DToGL_OptionUseEnvParams 0x0001
  15. #define D3DToGL_OptionDoFixupZ 0x0002 // Add instructions to put Z in the right interval for GL
  16. #define D3DToGL_OptionDoFixupY 0x0004 // Add instructions to flip the Y over for GL
  17. #define D3DToGL_OptionDoUserClipPlanes 0x0008 // ARB mode: Include OPTION vertex_program_2 and append DP4's to write into oCLP[0] and oCLP[1]
  18. // GLSL mode: generate code to write gl_ClipVertex
  19. #define D3DToGL_AddHexComments 0x0020 // Include hex comments in the code for debugging
  20. #define D3DToGL_PutHexCommentsAfterLines 0x0040 // If D3DToGL_AddHexComments is set, this puts the codes to the right, rather than on separate lines
  21. #define D3DToGL_GeneratingDebugText 0x0080 // This tells it that we're just getting info for debugging so go easy on asserts and errors
  22. #define D3DToGL_OptionSRGBWriteSuffix 0x0400 // Tack sRGB conversion suffix on to pixel shaders
  23. #define D3DToGL_OptionGenerateBoneUniformBuffer 0x0800 // if enabled, the vertex shader "bone" registers (all regs DXABSTRACT_VS_FIRST_BONE_SLOT and higher) will be separated out into another uniform buffer (vcbone)
  24. #define D3DToGL_OptionUseBindlessTexturing 0x1000
  25. #define D3DToGL_OptionSpew 0x80000000
  26. // Code for which component of the "dummy" address register is needed by an instruction
  27. #define ARL_DEST_NONE -1
  28. #define ARL_DEST_X 0
  29. #define ARL_DEST_Y 1
  30. #define ARL_DEST_Z 2
  31. #define ARL_DEST_W 3
  32. class D3DToGL
  33. {
  34. private:
  35. // Pointers for dwToken stream management
  36. uint32* m_pdwBaseToken;
  37. uint32* m_pdwNextToken;
  38. // Vertex shader or pixel shader, and version (necessary because some opcodes alias)
  39. bool m_bVertexShader;
  40. uint32 m_dwMinorVersion;
  41. uint32 m_dwMajorVersion;
  42. // Option flags
  43. bool m_bUseEnvParams; // set D3DToGL_OptionUseEnvParams in 'options' to use
  44. bool m_bDoFixupZ; // set D3DToGL_OptionDoFixupZ
  45. bool m_bDoFixupY; // set D3DToGL_OptionDoFixupZ
  46. bool m_bDoUserClipPlanes; // set D3DToGL_OptionDoUserClipPlanes
  47. bool m_bSpew; // set D3DToGL_OptionSpew
  48. bool m_bGenerateSRGBWriteSuffix; // set D3DToGL_OptionSRGBWriteSuffix
  49. bool m_bGenerateBoneUniformBuffer;
  50. bool m_bUseBindlessTexturing;
  51. // Counter for dealing with nested loops
  52. int m_nLoopDepth;
  53. // Add "// Hex: 0xFFEEF00"-type statements after each instruction is parsed.
  54. bool m_bAddHexCodeComments; // set D3DToGL_AddHexComments
  55. // Only applicable if m_bAddHexCodeComments is true.
  56. // If this is true, then it puts the hex code comments to the right of the instructions in a comment
  57. // rather than preceding the instructions.
  58. // Defaults to FALSE.
  59. bool m_bPutHexCodesAfterLines; // set D3DToGL_PutHexCommentsAtEnd
  60. // This tells it that we're just getting info for debugging so go easy on asserts and errors.
  61. // Defaults to FALSE.
  62. bool m_bGeneratingDebugText;
  63. // Various scratch temps needed to handle mis-matches in instruction sets between D3D and OpenGL
  64. bool m_bNeedsD2AddTemp;
  65. bool m_bNeedsNRMTemp;
  66. bool m_bDeclareAddressReg;
  67. bool m_bNeedsLerpTemp;
  68. bool m_bNeedsSinCosDeclarations;
  69. // Keep track of which vs outputs are used so we can declare them
  70. bool m_bDeclareVSOPos;
  71. bool m_bDeclareVSOFog;
  72. uint32 m_dwTexCoordOutMask;
  73. int32 m_nVSPositionOutput;
  74. // Mask of varyings which need centroid decoration
  75. uint32 m_nCentroidMask;
  76. // Keep track of which temps are used so they can be declared
  77. uint32 m_dwTempUsageMask;
  78. uint32 m_dwTempBoolUsageMask;
  79. bool m_bOutputColorRegister[4];
  80. bool m_bOutputDepthRegister;
  81. // Declaration of integer and bool constants
  82. uint32 m_dwConstIntUsageMask;
  83. uint32 m_dwConstBoolUsageMask;
  84. uint32 m_dwDefConstIntUsageMask;
  85. uint32 m_dwDefConstIntIterCount[32];
  86. // Did we use atomic_temp_var?
  87. bool m_bUsedAtomicTempVar;
  88. // Track constants so we know how to declare them
  89. bool m_bConstantRegisterDefined[MAX_SHADER_CONSTANTS];
  90. // Track sampler types when declared so we can properly decorate TEX instructions
  91. uint32 m_dwSamplerTypes[32];
  92. // Track sampler usage
  93. uint32 m_dwSamplerUsageMask;
  94. // Track shadow sampler usage
  95. int m_nShadowDepthSamplerMask;
  96. bool m_bDeclareShadowOption;
  97. // Track attribute references
  98. // init to 0xFFFFFFFF (unhit)
  99. // index by (dwRegToken & D3DSP_REGNUM_MASK) in VS DCL insns
  100. // fill with (usage<<4) | (usage index).
  101. uint32 m_dwAttribMap[16];
  102. // Register high water mark
  103. uint32 m_nHighestRegister;
  104. int32 m_nHighestBoneRegister;
  105. // GLSL does indentation for readability
  106. int m_NumIndentTabs;
  107. // Output buffers.
  108. CUtlBuffer *m_pBufHeaderCode;
  109. CUtlBuffer *m_pBufAttribCode;
  110. CUtlBuffer *m_pBufParamCode;
  111. CUtlBuffer *m_pBufALUCode;
  112. char *m_pFinalAssignmentsCode;
  113. int m_nFinalAssignmentsBufSize;
  114. // Recorded positions for debugging.
  115. uint32* m_pRecordedInputTokenStart;
  116. int m_nRecordedParamCodeStrlen;
  117. int m_nRecordedALUCodeStrlen;
  118. int m_nRecordedAttribCodeStrlen;
  119. // In GLSL mode, these store the semantic attached to each oN register.
  120. // They are the values that you pass to GetUsageIndexAndString.
  121. uint32 m_DeclaredOutputs[MAX_DECLARED_OUTPUTS];
  122. uint32 m_DeclaredInputs[MAX_DECLARED_INPUTS];
  123. // Have they used the tangent input semantic (i.e. is g_pTangentAttributeName declared)?
  124. bool m_bTangentInputUsed;
  125. bool m_bUsesDSTInstruction;
  126. private:
  127. // Utilities to aid in decoding token stream
  128. uint32 GetNextToken( void );
  129. void SkipTokens( uint32 numToSkip );
  130. uint32 Opcode( uint32 dwToken );
  131. uint32 OpcodeSpecificData( uint32 dwToken );
  132. uint32 TextureType ( uint32 dwToken );
  133. uint32 GetRegType( uint32 dwRegToken );
  134. // Write to the different buffers.
  135. void StrcatToHeaderCode( const char *pBuf );
  136. void StrcatToALUCode( const char *pBuf );
  137. void StrcatToParamCode( const char *pBuf );
  138. void StrcatToAttribCode( const char *pBuf );
  139. void PrintToBufWithIndents( CUtlBuffer &buf, const char *pFormat, ... );
  140. // This helps write the token hex codes into the output stream for debugging.
  141. void AddTokenHexCodeToBuffer( char *pBuffer, int nSize, int nLastStrlen );
  142. void RecordInputAndOutputPositions();
  143. void AddTokenHexCode();
  144. // Utilities for decoding tokens in to strings according to ASM syntax
  145. void PrintOpcode( uint32 inst, char* buff, int nBufLen );
  146. // fSemanticFlags is SEMANTIC_INPUT or SEMANTIC_OUTPUT.
  147. void PrintUsageAndIndexToString( uint32 dwToken, char* strUsageUsageIndexName, int nBufLen, int fSemanticFlags );
  148. CUtlString GetUsageAndIndexString( uint32 dwToken, int fSemanticFlags );
  149. CUtlString GetParameterString( uint32 dwToken, uint32 dwSourceOrDest, bool bForceScalarSource, int *pARLDestReg );
  150. const char* GetGLSLOperatorString( uint32 inst );
  151. void PrintParameterToString ( uint32 dwToken, uint32 dwSourceOrDest, char *pRegisterName, int nBufLen, bool bForceScalarSource, int *pARLDestReg );
  152. void InsertMoveFromAddressRegister( CUtlBuffer *pCode, int nARLComp0, int nARLComp1, int nARLComp2 = ARL_DEST_NONE );
  153. void InsertMoveInstruction( CUtlBuffer *pCode, int nARLComponent );
  154. void FlagIndirectRegister( uint32 dwToken, int *pARLDestReg );
  155. // Utilities for decoding tokens in to strings according to GLSL syntax
  156. bool OpenIntrinsic( uint32 inst, char* buff, int nBufLen, uint32 destDimension, uint32 nArgumentDimension );
  157. void PrintIndentation( char *pBuf, int nBufLen );
  158. uint32 MaintainAttributeMap( uint32 dwToken, uint32 dwRegToken );
  159. CUtlString FixGLSLSwizzle( const char *pDestRegisterName, const char *pSrcRegisterName );
  160. void WriteGLSLCmp( const char *pDestReg, const char *pSrc0Reg, const char *pSrc1Reg, const char *pSrc2Reg );
  161. void WriteGLSLSamplerDefinitions();
  162. void WriteGLSLOutputVariableAssignments();
  163. void WriteGLSLInputVariableAssignments();
  164. void NoteTangentInputUsed();
  165. void Handle_DCL();
  166. void Handle_DEF();
  167. void Handle_DEFIB( uint32 nInstruction );
  168. void Handle_MAD( uint32 nInstruction );
  169. void Handle_DP2ADD();
  170. void Handle_SINCOS();
  171. void Handle_LRP( uint32 nInstruction );
  172. void Handle_TEX( uint32 dwToken, bool bIsTexLDL );
  173. void Handle_TexLDD( uint32 nInstruction );
  174. void Handle_TexCoord();
  175. void Handle_UnaryOp( uint32 nInstruction );
  176. void Handle_BREAKC( uint32 dwToken );
  177. void HandleBinaryOp_GLSL( uint32 nInstruction );
  178. void HandleBinaryOp_ASM( uint32 nInstruction );
  179. void Handle_CMP();
  180. void Handle_NRM();
  181. void Handle_DeclarativeNonDclOp( uint32 nInstruction );
  182. public:
  183. D3DToGL();
  184. int TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bool *bVertexShader, uint32 options, int32 nShadowDepthSamplerMask, uint32 nCentroidMask, char *debugLabel );
  185. };
  186. #endif // DX9_ASM_TO_GL_2_H