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.

229 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 HEXCODE_HEADER "// Hex: "
  12. // Option bits
  13. #define D3DToGL_OptionUseEnvParams 0x001
  14. #define D3DToGL_OptionDoFixupZ 0x002 // Add instructions to put Z in the right interval for GL
  15. #define D3DToGL_OptionDoFixupY 0x004 // Add instructions to flip the Y over for GL
  16. #define D3DToGL_OptionDoUserClipPlanes 0x008 // ARB mode: Include OPTION vertex_program_2 and append DP4's to write into oCLP[0] and oCLP[1]
  17. // GLSL mode: generate code to write gl_ClipVertex
  18. #define D3DToGL_OptionGLSL 0x010 // Output GLSL, rather than ASM
  19. #define D3DToGL_AddHexComments 0x020 // Include hex comments in the code for debugging
  20. #define D3DToGL_PutHexCommentsAfterLines 0x040 // If D3DToGL_AddHexComments is set, this puts the codes to the right, rather than on separate lines
  21. #define D3DToGL_GeneratingDebugText 0x080 // This tells it that we're just getting info for debugging so go easy on asserts and errors
  22. #define D3DToGL_OptionAllowStaticControlFlow 0x100
  23. #define D3DToGL_OptionUseBindableUniforms 0x200 // add "bindable" in front of "vc" / "pc" constant arrays (GLSL only)
  24. #define D3DToGL_OptionSRGBWriteSuffix 0x400 // Tack sRGB conversion suffix on to pixel shaders
  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_bUseBindableUniforms; // set D3DToGL_OptionUseBindableUniforms
  49. bool m_bGenerateSRGBWriteSuffix; // set D3DToGL_OptionSRGBWriteSuffix
  50. // Default: false
  51. // If you set this to true, it'll convert to GLSL instead of GL ASM.
  52. bool m_bGLSL; // set D3DToGL_OptionGLSL
  53. // Default: false
  54. bool m_bAllowStaticControlFlow; // set D3DToGL_OptionAllowStaticControlFlow
  55. // Counter for dealing with nested loops
  56. int m_nLoopDepth;
  57. // Add "// Hex: 0xFFEEF00"-type statements after each instruction is parsed.
  58. bool m_bAddHexCodeComments; // set D3DToGL_AddHexComments
  59. // Only applicable if m_bAddHexCodeComments is true.
  60. // If this is true, then it puts the hex code comments to the right of the instructions in a comment
  61. // rather than preceding the instructions.
  62. // Defaults to FALSE.
  63. bool m_bPutHexCodesAfterLines; // set D3DToGL_PutHexCommentsAtEnd
  64. // This tells it that we're just getting info for debugging so go easy on asserts and errors.
  65. // Defaults to FALSE.
  66. bool m_bGeneratingDebugText;
  67. // Various scratch temps needed to handle mis-matches in instruction sets between D3D and OpenGL
  68. bool m_bNeedsD2AddTemp;
  69. bool m_bNeedsNRMTemp;
  70. bool m_bDeclareAddressReg;
  71. bool m_bNeedsLerpTemp;
  72. bool m_bNeedsSinCosDeclarations;
  73. // Keep track of which vs outputs are used so we can declare them
  74. bool m_bDeclareVSOPos;
  75. bool m_bDeclareVSOFog;
  76. uint32 m_dwTexCoordOutMask;
  77. // Mask of varyings which need centroid decoration
  78. uint32 m_nCentroidMask;
  79. // Keep track of which temps are used so they can be declared
  80. uint32 m_dwTempUsageMask;
  81. bool m_bOutputColorRegister[2];
  82. bool m_bOutputDepthRegister;
  83. // Declaration of integer and bool constants
  84. uint32 m_dwConstIntUsageMask;
  85. uint32 m_dwConstBoolUsageMask;
  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_nShadowDepthSampler;
  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. // GLSL does indentation for readability
  105. int m_NumIndentTabs;
  106. // Output buffers.
  107. CUtlBuffer *m_pBufHeaderCode;
  108. CUtlBuffer *m_pBufAttribCode;
  109. CUtlBuffer *m_pBufParamCode;
  110. CUtlBuffer *m_pBufALUCode;
  111. char *m_pFinalAssignmentsCode;
  112. int m_nFinalAssignmentsBufSize;
  113. // Recorded positions for debugging.
  114. uint32* m_pRecordedInputTokenStart;
  115. int m_nRecordedParamCodeStrlen;
  116. int m_nRecordedALUCodeStrlen;
  117. int m_nRecordedAttribCodeStrlen;
  118. // In GLSL mode, these store the semantic attached to each oN register.
  119. // They are the values that you pass to GetUsageIndexAndString.
  120. uint32 m_DeclaredOutputs[MAX_DECLARED_OUTPUTS];
  121. // Have they used the tangent input semantic (i.e. is g_pTangentAttributeName declared)?
  122. bool m_bTangentInputUsed;
  123. private:
  124. // Utilities to aid in decoding token stream
  125. uint32 GetNextToken( void );
  126. void SkipTokens( uint32 numToSkip );
  127. uint32 Opcode( uint32 dwToken );
  128. uint32 OpcodeSpecificData( uint32 dwToken );
  129. uint32 TextureType ( uint32 dwToken );
  130. uint32 GetRegType( uint32 dwRegToken );
  131. // Write to the different buffers.
  132. void StrcatToHeaderCode( const char *pBuf );
  133. void StrcatToALUCode( const char *pBuf );
  134. void StrcatToParamCode( const char *pBuf );
  135. void StrcatToAttribCode( const char *pBuf );
  136. // This helps write the token hex codes into the output stream for debugging.
  137. void AddTokenHexCodeToBuffer( char *pBuffer, int nSize, int nLastStrlen );
  138. void RecordInputAndOutputPositions();
  139. void AddTokenHexCode();
  140. // Utilities for decoding tokens in to strings according to ASM syntax
  141. void PrintOpcode( uint32 inst, char* buff, int nBufLen );
  142. // fSemanticFlags is SEMANTIC_INPUT or SEMANTIC_OUTPUT.
  143. void PrintUsageAndIndexToString( uint32 dwToken, char* strUsageUsageIndexName, int nBufLen, int fSemanticFlags );
  144. CUtlString GetUsageAndIndexString( uint32 dwToken, int fSemanticFlags );
  145. CUtlString GetParameterString( uint32 dwToken, uint32 dwSourceOrDest, bool bForceScalarSource, int *pARLDestReg );
  146. const char* GetGLSLOperatorString( uint32 inst );
  147. void PrintParameterToString ( uint32 dwToken, uint32 dwSourceOrDest, char *pRegisterName, int nBufLen, bool bForceScalarSource, int *pARLDestReg );
  148. void InsertMoveFromAddressRegister( CUtlBuffer *pCode, int nARLComp0, int nARLComp1, int nARLComp2 = ARL_DEST_NONE );
  149. void InsertMoveInstruction( CUtlBuffer *pCode, int nARLComponent );
  150. void FlagIndirectRegister( uint32 dwToken, int *pARLDestReg );
  151. // Utilities for decoding tokens in to strings according to GLSL syntax
  152. bool OpenIntrinsic( uint32 inst, char* buff, int nBufLen, uint32 destDimension, uint32 nArgumentDimension );
  153. void PrintIndentation( char *pBuf, int nBufLen );
  154. uint32 MaintainAttributeMap( uint32 dwToken, uint32 dwRegToken );
  155. CUtlString FixGLSLSwizzle( const char *pDestRegisterName, const char *pSrcRegisterName );
  156. void WriteGLSLCmp( const char *pDestReg, const char *pSrc0Reg, const char *pSrc1Reg, const char *pSrc2Reg );
  157. void WriteGLSLSamplerDefinitions();
  158. void WriteGLSLOutputVariableAssignments();
  159. void NoteTangentInputUsed();
  160. void Handle_DCL();
  161. void Handle_DEF();
  162. void Handle_MAD( uint32 nInstruction );
  163. void Handle_DP2ADD();
  164. void Handle_SINCOS();
  165. void Handle_LRP( uint32 nInstruction );
  166. void Handle_TEX( uint32 dwToken, bool bIsTexLDL );
  167. void Handle_TexLDD( uint32 nInstruction );
  168. void Handle_TexCoord();
  169. void Handle_UnaryOp( uint32 nInstruction );
  170. void HandleBinaryOp_GLSL( uint32 nInstruction );
  171. void HandleBinaryOp_ASM( uint32 nInstruction );
  172. void Handle_CMP();
  173. void Handle_NRM();
  174. void Handle_DeclarativeNonDclOp( uint32 nInstruction );
  175. public:
  176. D3DToGL();
  177. int TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bool *bVertexShader, uint32 options, int32 nShadowDepthSampler, uint32 nCentroidMask, char *debugLabel );
  178. };
  179. #endif // DX9_ASM_TO_GL_2_H