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.

3834 lines
113 KiB

  1. //------------------------------------------------------------------------------
  2. // DX9AsmToGL2.cpp
  3. //------------------------------------------------------------------------------
  4. // Immediately include gl.h, etc. here to avoid compilation warnings.
  5. #include <GL/gl.h>
  6. #include <GL/glext.h>
  7. #include "togl/rendermechanism.h"
  8. #include "tier0/dbg.h"
  9. #include "tier1/strtools.h"
  10. #include "tier1/utlbuffer.h"
  11. #include "dx9asmtogl2.h"
  12. #include "materialsystem/IShader.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. #ifdef POSIX
  16. #define strcat_s( a, b, c) V_strcat( a, c, b )
  17. #endif
  18. #define DST_REGISTER 0
  19. #define SRC_REGISTER 1
  20. // Flags to PrintUsageAndIndexToString.
  21. #define SEMANTIC_OUTPUT 0x01
  22. #define SEMANTIC_INPUT 0x02
  23. #define UNDECLARED_OUTPUT 0xFFFFFFFF
  24. #define UNDECLARED_INPUT 0xFFFFFFFF
  25. #ifndef POSIX
  26. #define Debugger() Assert(0)
  27. #endif
  28. //#define Assert(n) if( !(n) ){ TranslationError(); }
  29. static char *g_szVecZeros[] = { NULL, "0.0", "vec2( 0.0, 0.0 )", "vec3( 0.0, 0.0, 0.0 )", "vec4( 0.0, 0.0, 0.0, 0.0 )" };
  30. static char *g_szVecOnes[] = { NULL, "1.0", "vec2( 1.0, 1.0 )", "vec3( 1.0, 1.0, 1.0 )", "vec4( 1.0, 1.0, 1.0, 1.0 )" };
  31. static char *g_szDefaultSwizzle = "xyzw";
  32. static char *g_szDefaultSwizzleStrings[] = { "x", "y", "z", "w" };
  33. static char *g_szSamplerStrings[] = { "2D", "CUBE", "3D" };
  34. static const char *g_pAtomicTempVarName = "atomic_temp_var";
  35. static const char *g_pTangentAttributeName = "g_tangent";
  36. int __cdecl SortInts( const int *a, const int *b )
  37. {
  38. if ( *a < *b )
  39. return -1;
  40. else if ( *a > *b )
  41. return 1;
  42. else
  43. return 0;
  44. }
  45. void StripExtraTrailingZeros( char *pStr )
  46. {
  47. int len = (int)V_strlen( pStr );
  48. while ( len >= 2 && pStr[len-1] == '0' && pStr[len-2] != '.' )
  49. {
  50. pStr[len-1] = 0;
  51. --len;
  52. }
  53. }
  54. void D3DToGL::PrintToBufWithIndents( CUtlBuffer &buf, const char *pFormat, ... )
  55. {
  56. va_list marker;
  57. va_start( marker, pFormat );
  58. char szTemp[1024];
  59. V_vsnprintf( szTemp, sizeof( szTemp ), pFormat, marker );
  60. va_end( marker );
  61. PrintIndentation( (char*)buf.Base(), buf.Size() );
  62. strcat_s( (char*)buf.Base(), buf.Size(), szTemp );
  63. }
  64. void PrintToBuf( CUtlBuffer &buf, const char *pFormat, ... )
  65. {
  66. va_list marker;
  67. va_start( marker, pFormat );
  68. char szTemp[1024];
  69. V_vsnprintf( szTemp, sizeof( szTemp ), pFormat, marker );
  70. va_end( marker );
  71. strcat_s( (char*)buf.Base(), buf.Size(), szTemp );
  72. }
  73. void PrintToBuf( char *pOut, int nOutSize, const char *pFormat, ... )
  74. {
  75. int nStrlen = V_strlen( pOut );
  76. pOut += nStrlen;
  77. nOutSize -= nStrlen;
  78. va_list marker;
  79. va_start( marker, pFormat );
  80. V_vsnprintf( pOut, nOutSize, pFormat, marker );
  81. va_end( marker );
  82. }
  83. // Return the number of letters following the dot.
  84. // Returns 4 if there is no dot.
  85. // (So "r0.xy" returns 2 and "r0" returns 4).
  86. int GetNumWriteMaskEntries( const char *pParam )
  87. {
  88. const char *pDot = strchr( pParam, '.' );
  89. if ( pDot )
  90. return V_strlen( pDot + 1 );
  91. else
  92. return 4;
  93. }
  94. const char* GetSwizzleDot( const char *pParam )
  95. {
  96. const char *pDot = strrchr( pParam, '.' );
  97. const char *pSquareClose = strrchr( pParam, ']' );
  98. if ( pSquareClose )
  99. {
  100. // The test against ']' catches cases like, so we point to the last dot vc[int(va_r.x) + 29].x
  101. if ( pDot && ( pSquareClose < pDot ) )
  102. return pDot;
  103. else
  104. return NULL;
  105. }
  106. // Make sure the next character is a valid swizzle since we want to treat strings like vec4( gl_Normal, 0.0 ) as a whole param name.
  107. if ( pDot && ( ( *(pDot+1) == 'x' ) || ( *(pDot+1) == 'y' ) || ( *(pDot+1) == 'z' ) || ( *(pDot+1) == 'w' ) ||
  108. ( *(pDot+1) == 'r' ) || ( *(pDot+1) == 'g' ) || ( *(pDot+1) == 'b' ) || ( *(pDot+1) == 'z' ) ) )
  109. {
  110. return pDot;
  111. }
  112. return NULL;
  113. }
  114. int GetNumSwizzleComponents( const char *pParam )
  115. {
  116. // Special scalar output which won't accept a swizzle
  117. if ( !V_stricmp( pParam, "gl_FogFragCoord" ) )
  118. return 1;
  119. // Special scalar output which won't accept a swizzle
  120. if ( !V_stricmp( pParam, "gl_FragDepth" ) )
  121. return 1;
  122. // Special scalar output which won't accept a swizzle
  123. if ( !V_stricmp( pParam, "a0" ) )
  124. return 1;
  125. const char *pDot = GetSwizzleDot( pParam );
  126. if ( pDot )
  127. {
  128. pDot++; // Step over the dot
  129. int nNumSwizzleComponents = 0;
  130. while ( ( *pDot == 'x' ) || ( *pDot == 'y' ) || ( *pDot == 'z' ) || ( *pDot == 'w' ) ||
  131. ( *pDot == 'r' ) || ( *pDot == 'g' ) || ( *pDot == 'b' ) || ( *pDot == 'z' ) )
  132. {
  133. nNumSwizzleComponents++;
  134. pDot++;
  135. }
  136. return nNumSwizzleComponents;
  137. }
  138. return 0;
  139. }
  140. char GetSwizzleComponent( const char *pParam, int n )
  141. {
  142. Assert( n < 4 );
  143. const char *pDot = GetSwizzleDot( pParam );
  144. if ( pDot )
  145. {
  146. ++pDot;
  147. int nComponents = (int)V_strlen( pDot );
  148. Assert( nComponents > 0 );
  149. if ( n < nComponents )
  150. return pDot[n];
  151. else
  152. return pDot[nComponents-1];
  153. }
  154. return g_szDefaultSwizzle[n];
  155. }
  156. // Replace the parameter name and leave the swizzle intact.
  157. // So "somevar.xyz" becomes "othervar.xyz".
  158. void ReplaceParamName( const char *pSrc, const char *pNewParamName, char *pOut, int nOutLen )
  159. {
  160. // Start with the new parameter name.
  161. V_strncpy( pOut, pNewParamName, nOutLen );
  162. // Now add the swizzle if necessary.
  163. const char *pDot = GetSwizzleDot( pSrc );
  164. if ( pDot )
  165. {
  166. V_strncat( pOut, pDot, nOutLen );
  167. }
  168. }
  169. void GetParamNameWithoutSwizzle( const char *pParam, char *pOut, int nOutLen )
  170. {
  171. char *pParamStart = (char *) pParam;
  172. const char *pParamEnd = GetSwizzleDot( pParam ); // dot followed by valid swizzle characters
  173. bool bAbsWrapper = false;
  174. // Check for abs() or -abs() wrapper and strip it off during the fixup
  175. if ( !V_strncmp( pParam, "abs(", 4 ) || !V_strncmp( pParam, "-abs(", 5 ) )
  176. {
  177. const char *pOpenParen = strchr( pParam, '(' ); // FIRST opening paren
  178. const char *pClosingParen = strrchr( pParam, ')' ); // LAST closing paren
  179. Assert ( pOpenParen && pClosingParen );
  180. pParamStart = (char *) pOpenParen;
  181. pParamStart++;
  182. bAbsWrapper = true;
  183. if ( !pParamEnd )
  184. {
  185. pParamEnd = pClosingParen;
  186. }
  187. }
  188. if ( pParamEnd )
  189. {
  190. int nToCopy = MIN( nOutLen-1, pParamEnd - pParamStart );
  191. memcpy( pOut, pParamStart, nToCopy );
  192. pOut[nToCopy] = 0;
  193. }
  194. else
  195. {
  196. V_strncpy( pOut, pParamStart, nOutLen );
  197. }
  198. }
  199. bool DoParamNamesMatch( const char *pParam1, const char *pParam2 )
  200. {
  201. char szTemp[2][256];
  202. GetParamNameWithoutSwizzle( pParam1, szTemp[0], sizeof( szTemp[0] ) );
  203. GetParamNameWithoutSwizzle( pParam2, szTemp[1], sizeof( szTemp[1] ) );
  204. return ( V_stricmp( szTemp[0], szTemp[1] ) == 0 );
  205. }
  206. // Extract the n'th component of the swizzle mask.
  207. // If n would exceed the length of the swizzle mask, then it looks up into "xyzw".
  208. void WriteParamWithSingleMaskEntry( const char *pParam, int n, char *pOut, int nOutLen )
  209. {
  210. bool bCloseParen = false;
  211. if ( !V_strncmp( pParam, "-abs(", 5 ) )
  212. {
  213. V_strcpy( pOut, "-abs(" );
  214. bCloseParen = true;
  215. pOut += 5; nOutLen -= 5;
  216. }
  217. else if ( !V_strncmp( pParam, "abs(", 4 ) )
  218. {
  219. V_strcpy( pOut, "abs(" );
  220. bCloseParen = true;
  221. pOut += 4; nOutLen -= 4;
  222. }
  223. GetParamNameWithoutSwizzle( pParam, pOut, nOutLen );
  224. PrintToBuf( pOut, nOutLen, "." );
  225. PrintToBuf( pOut, nOutLen, "%c", GetSwizzleComponent( pParam, n ) );
  226. if ( bCloseParen )
  227. {
  228. PrintToBuf( pOut, nOutLen, ")" );
  229. }
  230. }
  231. float uint32ToFloat( uint32 dw )
  232. {
  233. return *((float*)&dw);
  234. }
  235. CUtlString EnsureNumSwizzleComponents( const char *pSrcRegisterName, int nComponents )
  236. {
  237. int nExisting = GetNumSwizzleComponents( pSrcRegisterName );
  238. if ( nExisting == nComponents )
  239. return pSrcRegisterName;
  240. bool bAbsWrapper = false; // Parameter wrapped in an abs()
  241. bool bAbsNegative = false; // -abs()
  242. char szSrcRegister[128];
  243. V_strncpy( szSrcRegister, pSrcRegisterName, sizeof(szSrcRegister) );
  244. // Check for abs() or -abs() wrapper and strip it off during the fixup
  245. if ( !V_strncmp( pSrcRegisterName, "abs(", 4 ) || !V_strncmp( pSrcRegisterName, "-abs(", 5 ) )
  246. {
  247. bAbsWrapper = true;
  248. bAbsNegative = pSrcRegisterName[0] == '-';
  249. const char *pOpenParen = strchr( pSrcRegisterName, '(' ); // FIRST opening paren
  250. const char *pClosingParen = strrchr( pSrcRegisterName, ')' ); // LAST closing paren
  251. Assert ( pOpenParen && pClosingParen ); // If we start with abs( and don't get both parens, something is very wrong
  252. // Copy out just the register name with no abs()
  253. int nRegNameLength = pClosingParen - pOpenParen - 1;
  254. V_strncpy( szSrcRegister, pOpenParen+1, nRegNameLength + 1 ); // Kind of a weird function...copy more than you need and slam the last char to NULL-terminate
  255. }
  256. char szReg[256];
  257. GetParamNameWithoutSwizzle( szSrcRegister, szReg, sizeof( szReg ) );
  258. if ( nComponents == 0 )
  259. return szReg;
  260. PrintToBuf( szReg, sizeof( szReg ), "." );
  261. if ( nExisting > nComponents )
  262. {
  263. // DX ASM will sometimes have statements like "NRM r0.xyz, r1.yzww", where it just doesn't use the last part of r1. So we won't either.
  264. for ( int i=0; i < nComponents; i++ )
  265. {
  266. PrintToBuf( szReg, sizeof( szReg ), "%c", GetSwizzleComponent( szSrcRegister, i ) );
  267. }
  268. }
  269. else
  270. {
  271. if ( nExisting == 0 )
  272. {
  273. // We've got something like r0 and need N more components, so add as much of "xyzw" is needed.
  274. for ( int i=0; i < nComponents; i++ )
  275. PrintToBuf( szReg, sizeof( szReg ), "%c", g_szDefaultSwizzle[i] );
  276. }
  277. else
  278. {
  279. // We've got something like r0.x and need N more components, so replicate the X so it looks like r0.xxx
  280. V_strncpy( szReg, szSrcRegister, sizeof( szReg ) );
  281. char cLast = szSrcRegister[ V_strlen( szSrcRegister ) - 1 ];
  282. for ( int i=nExisting; i < nComponents; i++ )
  283. {
  284. PrintToBuf( szReg, sizeof( szReg ), "%c", cLast );
  285. }
  286. }
  287. }
  288. if ( bAbsWrapper )
  289. {
  290. char szTemp[128];
  291. V_strncpy( szTemp, szReg, sizeof(szTemp) );
  292. V_snprintf( szReg, sizeof( szReg ), "%sabs(%s)", bAbsNegative ? "-" : "", szTemp ) ;
  293. }
  294. return szReg;
  295. }
  296. static void TranslationError()
  297. {
  298. Plat_DebugString( "D3DToGL: GLSL translation error!\n" );
  299. DebuggerBreakIfDebugging();
  300. Error( "D3DToGL: GLSL translation error!\n" );
  301. }
  302. D3DToGL::D3DToGL()
  303. {
  304. }
  305. uint32 D3DToGL::GetNextToken( void )
  306. {
  307. uint32 dwToken = *m_pdwNextToken;
  308. m_pdwNextToken++;
  309. return dwToken;
  310. }
  311. void D3DToGL::SkipTokens( uint32 numToSkip )
  312. {
  313. m_pdwNextToken += numToSkip;
  314. }
  315. uint32 D3DToGL::Opcode( uint32 dwToken )
  316. {
  317. return ( dwToken & D3DSI_OPCODE_MASK );
  318. }
  319. uint32 D3DToGL::OpcodeSpecificData (uint32 dwToken)
  320. {
  321. return ( ( dwToken & D3DSP_OPCODESPECIFICCONTROL_MASK ) >> D3DSP_OPCODESPECIFICCONTROL_SHIFT );
  322. }
  323. uint32 D3DToGL::TextureType ( uint32 dwToken )
  324. {
  325. return ( dwToken & D3DSP_TEXTURETYPE_MASK ); // Note this one doesn't shift due to weird D3DSAMPLER_TEXTURE_TYPE enum
  326. }
  327. // Print GLSL intrinsic corresponding to particular instruction
  328. bool D3DToGL::OpenIntrinsic( uint32 inst, char* buff, int nBufLen, uint32 destDimension, uint32 nArgumentDimension )
  329. {
  330. // Some GLSL intrinsics need type conversion, which we do in this routine
  331. // As a result, the caller must sometimes close both parentheses, not just one
  332. bool bDoubleClose = false;
  333. if ( nArgumentDimension == 0 )
  334. {
  335. nArgumentDimension = 4;
  336. }
  337. switch ( inst )
  338. {
  339. case D3DSIO_RSQ:
  340. V_snprintf( buff, nBufLen, "inversesqrt( " );
  341. break;
  342. case D3DSIO_DP3:
  343. case D3DSIO_DP4:
  344. if ( destDimension == 1 )
  345. {
  346. V_snprintf( buff, nBufLen, "dot( " );
  347. }
  348. else
  349. {
  350. if ( !destDimension )
  351. destDimension = 4;
  352. V_snprintf( buff, nBufLen, "vec%d( dot( ", destDimension );
  353. bDoubleClose = true;
  354. }
  355. break;
  356. case D3DSIO_MIN:
  357. V_snprintf( buff, nBufLen, "min( " );
  358. break;
  359. case D3DSIO_MAX:
  360. V_snprintf( buff, nBufLen, "max( " );
  361. break;
  362. case D3DSIO_SLT:
  363. if ( nArgumentDimension == 1 )
  364. {
  365. V_snprintf( buff, nBufLen, "float( " ); // lessThan doesn't have a scalar version
  366. }
  367. else
  368. {
  369. Assert( nArgumentDimension > 1 );
  370. V_snprintf( buff, nBufLen, "vec%d( lessThan( ", nArgumentDimension );
  371. bDoubleClose = true;
  372. }
  373. break;
  374. case D3DSIO_SGE:
  375. if ( nArgumentDimension == 1 )
  376. {
  377. V_snprintf( buff, nBufLen, "float( " ); // greaterThanEqual doesn't have a scalar version
  378. }
  379. else
  380. {
  381. Assert( nArgumentDimension > 1 );
  382. V_snprintf( buff, nBufLen, "vec%d( greaterThanEqual( ", nArgumentDimension );
  383. bDoubleClose = true;
  384. }
  385. break;
  386. case D3DSIO_EXP:
  387. V_snprintf( buff, nBufLen, "exp( " ); // exp2 ?
  388. break;
  389. case D3DSIO_LOG:
  390. V_snprintf( buff, nBufLen, "log( " ); // log2 ?
  391. break;
  392. case D3DSIO_LIT:
  393. TranslationError();
  394. V_snprintf( buff, nBufLen, "lit( " ); // gonna have to write this one
  395. break;
  396. case D3DSIO_DST:
  397. V_snprintf( buff, nBufLen, "dst( " ); // gonna have to write this one
  398. break;
  399. case D3DSIO_LRP:
  400. Assert( !m_bVertexShader );
  401. V_snprintf( buff, nBufLen, "mix( " );
  402. break;
  403. case D3DSIO_FRC:
  404. V_snprintf( buff, nBufLen, "fract( " );
  405. break;
  406. case D3DSIO_M4x4:
  407. TranslationError();
  408. V_snprintf( buff, nBufLen, "m4x4" );
  409. break;
  410. case D3DSIO_M4x3:
  411. case D3DSIO_M3x4:
  412. case D3DSIO_M3x3:
  413. case D3DSIO_M3x2:
  414. case D3DSIO_CALL:
  415. case D3DSIO_CALLNZ:
  416. case D3DSIO_LOOP:
  417. case D3DSIO_RET:
  418. case D3DSIO_ENDLOOP:
  419. case D3DSIO_LABEL:
  420. case D3DSIO_DCL:
  421. TranslationError();
  422. break;
  423. case D3DSIO_POW:
  424. V_snprintf( buff, nBufLen, "pow( " );
  425. break;
  426. case D3DSIO_CRS:
  427. V_snprintf( buff, nBufLen, "cross( " );
  428. break;
  429. case D3DSIO_SGN:
  430. TranslationError();
  431. V_snprintf( buff, nBufLen, "sign( " );
  432. break;
  433. case D3DSIO_ABS:
  434. V_snprintf( buff, nBufLen, "abs( " );
  435. break;
  436. case D3DSIO_NRM:
  437. TranslationError();
  438. V_snprintf( buff, nBufLen, "normalize( " );
  439. break;
  440. case D3DSIO_SINCOS:
  441. TranslationError();
  442. V_snprintf( buff, nBufLen, "sincos( " ); // gonna have to write this one
  443. break;
  444. case D3DSIO_REP:
  445. case D3DSIO_ENDREP:
  446. case D3DSIO_IF:
  447. case D3DSIO_IFC:
  448. case D3DSIO_ELSE:
  449. case D3DSIO_ENDIF:
  450. case D3DSIO_BREAK:
  451. case D3DSIO_BREAKC: // TODO: these are the reason we even need GLSL...gotta make these work
  452. TranslationError();
  453. break;
  454. case D3DSIO_DEFB:
  455. case D3DSIO_DEFI:
  456. TranslationError();
  457. break;
  458. case D3DSIO_TEXCOORD:
  459. V_snprintf( buff, nBufLen, "texcoord" );
  460. break;
  461. case D3DSIO_TEXKILL:
  462. V_snprintf( buff, nBufLen, "kill( " ); // wrap the discard instruction?
  463. break;
  464. case D3DSIO_TEX:
  465. TranslationError();
  466. V_snprintf( buff, nBufLen, "TEX" ); // We shouldn't get here
  467. break;
  468. case D3DSIO_TEXBEM:
  469. case D3DSIO_TEXBEML:
  470. case D3DSIO_TEXREG2AR:
  471. case D3DSIO_TEXREG2GB:
  472. case D3DSIO_TEXM3x2PAD:
  473. case D3DSIO_TEXM3x2TEX:
  474. case D3DSIO_TEXM3x3PAD:
  475. case D3DSIO_TEXM3x3TEX:
  476. case D3DSIO_TEXM3x3SPEC:
  477. case D3DSIO_TEXM3x3VSPEC:
  478. TranslationError();
  479. break;
  480. case D3DSIO_EXPP:
  481. V_snprintf( buff, nBufLen, "exp( " );
  482. break;
  483. case D3DSIO_LOGP:
  484. V_snprintf( buff, nBufLen, "log( " );
  485. break;
  486. case D3DSIO_CND:
  487. TranslationError();
  488. break;
  489. case D3DSIO_DEF:
  490. TranslationError();
  491. V_snprintf( buff, nBufLen, "DEF" );
  492. break;
  493. case D3DSIO_TEXREG2RGB:
  494. case D3DSIO_TEXDP3TEX:
  495. case D3DSIO_TEXM3x2DEPTH:
  496. case D3DSIO_TEXDP3:
  497. case D3DSIO_TEXM3x3:
  498. TranslationError();
  499. break;
  500. case D3DSIO_TEXDEPTH:
  501. V_snprintf( buff, nBufLen, "texdepth" );
  502. break;
  503. case D3DSIO_CMP:
  504. TranslationError();
  505. Assert( !m_bVertexShader );
  506. V_snprintf( buff, nBufLen, "CMP" );
  507. break;
  508. case D3DSIO_BEM:
  509. TranslationError();
  510. break;
  511. case D3DSIO_DP2ADD:
  512. TranslationError();
  513. break;
  514. case D3DSIO_DSX:
  515. V_snprintf( buff, nBufLen, "dFdx" );
  516. break;
  517. case D3DSIO_DSY:
  518. V_snprintf( buff, nBufLen, "dFdy" );
  519. break;
  520. case D3DSIO_TEXLDD:
  521. V_snprintf( buff, nBufLen, "texldd" );
  522. break;
  523. case D3DSIO_SETP:
  524. TranslationError();
  525. break;
  526. case D3DSIO_TEXLDL:
  527. V_snprintf( buff, nBufLen, "texldl" );
  528. break;
  529. case D3DSIO_BREAKP:
  530. case D3DSIO_PHASE:
  531. TranslationError();
  532. break;
  533. }
  534. return bDoubleClose;
  535. }
  536. const char* D3DToGL::GetGLSLOperatorString( uint32 inst )
  537. {
  538. if ( inst == D3DSIO_ADD )
  539. return "+";
  540. else if ( inst == D3DSIO_SUB )
  541. return "-";
  542. else if ( inst == D3DSIO_MUL )
  543. return "*";
  544. Error( "GetGLSLOperatorString: unknown operator" );
  545. return "zzzz";
  546. }
  547. // Print ASM opcode
  548. void D3DToGL::PrintOpcode( uint32 inst, char* buff, int nBufLen )
  549. {
  550. switch ( inst )
  551. {
  552. case D3DSIO_NOP:
  553. V_snprintf( buff, nBufLen, "NOP" );
  554. TranslationError();
  555. break;
  556. case D3DSIO_MOV:
  557. V_snprintf( buff, nBufLen, "MOV" );
  558. break;
  559. case D3DSIO_ADD:
  560. V_snprintf( buff, nBufLen, "ADD" );
  561. break;
  562. case D3DSIO_SUB:
  563. V_snprintf( buff, nBufLen, "SUB" );
  564. break;
  565. case D3DSIO_MAD:
  566. V_snprintf( buff, nBufLen, "MAD" );
  567. break;
  568. case D3DSIO_MUL:
  569. V_snprintf( buff, nBufLen, "MUL" );
  570. break;
  571. case D3DSIO_RCP:
  572. V_snprintf( buff, nBufLen, "RCP" );
  573. break;
  574. case D3DSIO_RSQ:
  575. V_snprintf( buff, nBufLen, "RSQ" );
  576. break;
  577. case D3DSIO_DP3:
  578. V_snprintf( buff, nBufLen, "DP3" );
  579. break;
  580. case D3DSIO_DP4:
  581. V_snprintf( buff, nBufLen, "DP4" );
  582. break;
  583. case D3DSIO_MIN:
  584. V_snprintf( buff, nBufLen, "MIN" );
  585. break;
  586. case D3DSIO_MAX:
  587. V_snprintf( buff, nBufLen, "MAX" );
  588. break;
  589. case D3DSIO_SLT:
  590. V_snprintf( buff, nBufLen, "SLT" );
  591. break;
  592. case D3DSIO_SGE:
  593. V_snprintf( buff, nBufLen, "SGE" );
  594. break;
  595. case D3DSIO_EXP:
  596. V_snprintf( buff, nBufLen, "EX2" );
  597. break;
  598. case D3DSIO_LOG:
  599. V_snprintf( buff, nBufLen, "LG2" );
  600. break;
  601. case D3DSIO_LIT:
  602. V_snprintf( buff, nBufLen, "LIT" );
  603. break;
  604. case D3DSIO_DST:
  605. V_snprintf( buff, nBufLen, "DST" );
  606. break;
  607. case D3DSIO_LRP:
  608. Assert( !m_bVertexShader );
  609. V_snprintf( buff, nBufLen, "LRP" );
  610. break;
  611. case D3DSIO_FRC:
  612. V_snprintf( buff, nBufLen, "FRC" );
  613. break;
  614. case D3DSIO_M4x4:
  615. V_snprintf( buff, nBufLen, "m4x4" );
  616. break;
  617. case D3DSIO_M4x3:
  618. case D3DSIO_M3x4:
  619. case D3DSIO_M3x3:
  620. case D3DSIO_M3x2:
  621. case D3DSIO_CALL:
  622. case D3DSIO_CALLNZ:
  623. case D3DSIO_LOOP:
  624. case D3DSIO_RET:
  625. case D3DSIO_ENDLOOP:
  626. case D3DSIO_LABEL:
  627. TranslationError();
  628. break;
  629. case D3DSIO_DCL:
  630. V_snprintf( buff, nBufLen, "DCL" );
  631. break;
  632. case D3DSIO_POW:
  633. V_snprintf( buff, nBufLen, "POW" );
  634. break;
  635. case D3DSIO_CRS:
  636. V_snprintf( buff, nBufLen, "XPD" );
  637. break;
  638. case D3DSIO_SGN:
  639. TranslationError();
  640. V_snprintf( buff, nBufLen, "SGN" );
  641. break;
  642. case D3DSIO_ABS:
  643. V_snprintf( buff, nBufLen, "ABS" );
  644. break;
  645. case D3DSIO_NRM:
  646. TranslationError();
  647. V_snprintf( buff, nBufLen, "NRM" );
  648. break;
  649. case D3DSIO_SINCOS:
  650. Assert( !m_bVertexShader );
  651. V_snprintf( buff, nBufLen, "SCS" );
  652. break;
  653. case D3DSIO_REP:
  654. case D3DSIO_ENDREP:
  655. case D3DSIO_IF:
  656. case D3DSIO_IFC:
  657. case D3DSIO_ELSE:
  658. case D3DSIO_ENDIF:
  659. case D3DSIO_BREAK:
  660. case D3DSIO_BREAKC:
  661. TranslationError();
  662. break;
  663. case D3DSIO_MOVA:
  664. Assert( m_bVertexShader );
  665. V_snprintf( buff, nBufLen, "MOV" ); // We're always moving into a temp instead, so this is MOV instead of ARL
  666. break;
  667. case D3DSIO_DEFB:
  668. case D3DSIO_DEFI:
  669. TranslationError();
  670. break;
  671. case D3DSIO_TEXCOORD:
  672. V_snprintf( buff, nBufLen, "texcoord" );
  673. break;
  674. case D3DSIO_TEXKILL:
  675. V_snprintf( buff, nBufLen, "KIL" );
  676. break;
  677. case D3DSIO_TEX:
  678. V_snprintf( buff, nBufLen, "TEX" );
  679. break;
  680. case D3DSIO_TEXBEM:
  681. case D3DSIO_TEXBEML:
  682. case D3DSIO_TEXREG2AR:
  683. case D3DSIO_TEXREG2GB:
  684. case D3DSIO_TEXM3x2PAD:
  685. case D3DSIO_TEXM3x2TEX:
  686. case D3DSIO_TEXM3x3PAD:
  687. case D3DSIO_TEXM3x3TEX:
  688. case D3DSIO_TEXM3x3SPEC:
  689. case D3DSIO_TEXM3x3VSPEC:
  690. TranslationError();
  691. break;
  692. case D3DSIO_EXPP:
  693. V_snprintf( buff, nBufLen, "EXP" );
  694. break;
  695. case D3DSIO_LOGP:
  696. V_snprintf( buff, nBufLen, "LOG" );
  697. break;
  698. case D3DSIO_CND:
  699. TranslationError();
  700. break;
  701. case D3DSIO_DEF:
  702. V_snprintf( buff, nBufLen, "DEF" );
  703. break;
  704. case D3DSIO_TEXREG2RGB:
  705. case D3DSIO_TEXDP3TEX:
  706. case D3DSIO_TEXM3x2DEPTH:
  707. case D3DSIO_TEXDP3:
  708. case D3DSIO_TEXM3x3:
  709. TranslationError();
  710. break;
  711. case D3DSIO_TEXDEPTH:
  712. V_snprintf( buff, nBufLen, "texdepth" );
  713. break;
  714. case D3DSIO_CMP:
  715. Assert( !m_bVertexShader );
  716. V_snprintf( buff, nBufLen, "CMP" );
  717. break;
  718. case D3DSIO_BEM:
  719. TranslationError();
  720. break;
  721. case D3DSIO_DP2ADD:
  722. TranslationError();
  723. break;
  724. case D3DSIO_DSX:
  725. V_snprintf( buff, nBufLen, "dFdx" );
  726. break;
  727. case D3DSIO_DSY:
  728. V_snprintf( buff, nBufLen, "dFdy" );
  729. break;
  730. case D3DSIO_TEXLDD:
  731. V_snprintf( buff, nBufLen, "texldd" );
  732. break;
  733. case D3DSIO_SETP:
  734. TranslationError();
  735. break;
  736. case D3DSIO_TEXLDL:
  737. V_snprintf( buff, nBufLen, "texldl" );
  738. break;
  739. case D3DSIO_BREAKP:
  740. case D3DSIO_PHASE:
  741. TranslationError();
  742. break;
  743. }
  744. }
  745. CUtlString D3DToGL::GetUsageAndIndexString( uint32 dwToken, int fSemanticFlags )
  746. {
  747. char szTemp[1024];
  748. PrintUsageAndIndexToString( dwToken, szTemp, sizeof( szTemp ), fSemanticFlags );
  749. return szTemp;
  750. }
  751. //------------------------------------------------------------------------------
  752. // Helper function which prints ASCII representation of usage-usageindex pair to string
  753. //
  754. // Strictly used by vertex shaders
  755. // not used any more now that we have attribmap metadata
  756. //------------------------------------------------------------------------------
  757. void D3DToGL::PrintUsageAndIndexToString( uint32 dwToken, char* strUsageUsageIndexName, int nBufLen, int fSemanticFlags )
  758. {
  759. uint32 dwUsage = ( dwToken & D3DSP_DCL_USAGE_MASK );
  760. uint32 dwUsageIndex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  761. switch ( dwUsage )
  762. {
  763. case D3DDECLUSAGE_POSITION:
  764. if ( m_bVertexShader )
  765. {
  766. if ( fSemanticFlags & SEMANTIC_OUTPUT )
  767. V_snprintf( strUsageUsageIndexName, nBufLen, "vTempPos" ); // effectively gl_Position
  768. else
  769. V_snprintf( strUsageUsageIndexName, nBufLen, "gl_Vertex" );
  770. }
  771. else
  772. {
  773. // .xy = position in viewport coordinates
  774. // .z = depth
  775. V_snprintf( strUsageUsageIndexName, nBufLen, "gl_FragCoord" );
  776. }
  777. break;
  778. case D3DDECLUSAGE_BLENDWEIGHT:
  779. V_snprintf( strUsageUsageIndexName, nBufLen, "vertex.attrib[1]" ); // "vertex.attrib[12]" ); // or [1]
  780. break;
  781. case D3DDECLUSAGE_BLENDINDICES:
  782. V_snprintf( strUsageUsageIndexName, nBufLen, "vertex.attrib[13]" ); // "vertex.attrib[13]" ); // or [ 7 ]
  783. break;
  784. case D3DDECLUSAGE_NORMAL:
  785. V_snprintf( strUsageUsageIndexName, nBufLen, "vec4( gl_Normal, 0.0 )" );
  786. break;
  787. case D3DDECLUSAGE_PSIZE:
  788. TranslationError();
  789. V_snprintf( strUsageUsageIndexName, nBufLen, "_psize" ); // no analog
  790. break;
  791. case D3DDECLUSAGE_TEXCOORD:
  792. V_snprintf( strUsageUsageIndexName, nBufLen, "oT%d", dwUsageIndex );
  793. break;
  794. case D3DDECLUSAGE_TANGENT:
  795. NoteTangentInputUsed();
  796. V_strncpy( strUsageUsageIndexName, g_pTangentAttributeName, nBufLen );
  797. break;
  798. case D3DDECLUSAGE_BINORMAL:
  799. V_snprintf( strUsageUsageIndexName, nBufLen, "vertex.attrib[14]" ); // aka texc[6]
  800. break;
  801. // case D3DDECLUSAGE_TESSFACTOR:
  802. // TranslationError();
  803. // V_snprintf( strUsageUsageIndexName, nBufLen, "_position" ); // no analog
  804. // break;
  805. // case D3DDECLUSAGE_POSITIONT:
  806. // TranslationError();
  807. // V_snprintf( strUsageUsageIndexName, nBufLen, "_positiont" ); // no analog
  808. // break;
  809. case D3DDECLUSAGE_COLOR:
  810. Assert( dwUsageIndex <= 1 );
  811. // if ( fSemanticFlags & SEMANTIC_OUTPUT )
  812. // V_snprintf( strUsageUsageIndexName, nBufLen, dwUsageIndex != 0 ? "gl_BackColor" : "gl_FrontColor" );
  813. // else
  814. V_snprintf( strUsageUsageIndexName, nBufLen, dwUsageIndex != 0 ? "gl_SecondaryColor" : "gl_Color" );
  815. break;
  816. case D3DDECLUSAGE_FOG:
  817. TranslationError();
  818. break;
  819. case D3DDECLUSAGE_DEPTH:
  820. TranslationError();
  821. V_snprintf( strUsageUsageIndexName, nBufLen, "_depth" ); // no analog
  822. break;
  823. case D3DDECLUSAGE_SAMPLE:
  824. TranslationError();
  825. V_snprintf( strUsageUsageIndexName, nBufLen, "_sample" ); // no analog
  826. break;
  827. default:
  828. Debugger();
  829. break;
  830. }
  831. }
  832. uint32 D3DToGL::GetRegType( uint32 dwRegToken )
  833. {
  834. return ( ( dwRegToken & D3DSP_REGTYPE_MASK2 ) >> D3DSP_REGTYPE_SHIFT2 ) | ( ( dwRegToken & D3DSP_REGTYPE_MASK ) >> D3DSP_REGTYPE_SHIFT );
  835. }
  836. void D3DToGL::PrintIndentation( char *pBuf, int nBufLen )
  837. {
  838. for( int i=0; i<m_NumIndentTabs; i++ )
  839. {
  840. strcat_s( pBuf, nBufLen, "\t" );
  841. }
  842. }
  843. CUtlString D3DToGL::GetParameterString( uint32 dwToken, uint32 dwSourceOrDest, bool bForceScalarSource, int *pARLDestReg )
  844. {
  845. char szTemp[1024];
  846. PrintParameterToString( dwToken, dwSourceOrDest, szTemp, sizeof( szTemp ), bForceScalarSource, pARLDestReg );
  847. return szTemp;
  848. }
  849. // If the register happens to end with ".xyzw", then this strips off the mask.
  850. void SimplifyFourParamRegister( char *pRegister )
  851. {
  852. int nLen = V_strlen( pRegister );
  853. if ( nLen > 5 && V_strcmp( &pRegister[nLen-5], ".xyzw" ) == 0 )
  854. pRegister[nLen-5] = 0;
  855. }
  856. // This returns 0 for x, 1 for y, 2 for z, and 3 for w.
  857. int GetSwizzleComponentVectorIndex( char chMask )
  858. {
  859. if ( chMask == 'x' )
  860. return 0;
  861. else if ( chMask == 'y' )
  862. return 1;
  863. else if ( chMask == 'z' )
  864. return 2;
  865. else if ( chMask == 'w' )
  866. return 3;
  867. Error( "GetSwizzleComponentVectorIndex( '%c' ) - invalid parameter.\n", chMask );
  868. return 0;
  869. }
  870. // GLSL needs the # of src masks to match the dest write mask.
  871. //
  872. // So this:
  873. // r0.xy = r1 + r2;
  874. // becomes:
  875. // r0.xy = r1.xy + r2.xy;
  876. //
  877. //
  878. // Also, and this is the trickier one: GLSL reads the source registers from their first component on
  879. // whereas D3D reads them as referenced in the dest register mask!
  880. //
  881. // So this code in D3D:
  882. // r0.yz = c0.x + c1.wxyz
  883. // Really means:
  884. // r0.y = c0.x + c1.x
  885. // r0.z = c0.x + c1.y
  886. // So we translate it to this in GLSL:
  887. // r0.yz = c0.xx + c1.wx
  888. // r0.yz = c0.xx + c1.xy
  889. //
  890. CUtlString D3DToGL::FixGLSLSwizzle( const char *pDestRegisterName, const char *pSrcRegisterName )
  891. {
  892. bool bAbsWrapper = false; // Parameter wrapped in an abs()
  893. bool bAbsNegative = false; // -abs()
  894. char szSrcRegister[128];
  895. V_strncpy( szSrcRegister, pSrcRegisterName, sizeof(szSrcRegister) );
  896. // Check for abs() or -abs() wrapper and strip it off during the fixup
  897. if ( !V_strncmp( pSrcRegisterName, "abs(", 4 ) || !V_strncmp( pSrcRegisterName, "-abs(", 5 ) )
  898. {
  899. bAbsWrapper = true;
  900. bAbsNegative = pSrcRegisterName[0] == '-';
  901. const char *pOpenParen = strchr( pSrcRegisterName, '(' ); // FIRST opening paren
  902. const char *pClosingParen = strrchr( pSrcRegisterName, ')' ); // LAST closing paren
  903. Assert ( pOpenParen && pClosingParen ); // If we start with abs( and don't get both parens, something is very wrong
  904. // Copy out just the register name with no abs()
  905. int nRegNameLength = pClosingParen - pOpenParen - 1;
  906. V_strncpy( szSrcRegister, pOpenParen+1, nRegNameLength + 1 ); // Kind of a weird function...copy more than you need and slam the last char to NULL-terminate
  907. }
  908. int nSwizzlesInDest = GetNumSwizzleComponents( pDestRegisterName );
  909. if ( nSwizzlesInDest == 0 )
  910. nSwizzlesInDest = 4;
  911. char szFixedSrcRegister[128];
  912. GetParamNameWithoutSwizzle( szSrcRegister, szFixedSrcRegister, sizeof( szFixedSrcRegister ) );
  913. V_strncat( szFixedSrcRegister, ".", sizeof( szFixedSrcRegister ) );
  914. for ( int i=0; i < nSwizzlesInDest; i++ )
  915. {
  916. char chDestWriteMask = GetSwizzleComponent( pDestRegisterName, i );
  917. int nVectorIndex = GetSwizzleComponentVectorIndex( chDestWriteMask );
  918. char ch[2];
  919. ch[0] = GetSwizzleComponent( szSrcRegister, nVectorIndex );
  920. ch[1] = 0;
  921. V_strncat( szFixedSrcRegister, ch, sizeof( szFixedSrcRegister ) );
  922. }
  923. SimplifyFourParamRegister( szFixedSrcRegister );
  924. if ( bAbsWrapper )
  925. {
  926. char szTempSrcRegister[128];
  927. V_strncpy( szTempSrcRegister, szFixedSrcRegister, sizeof(szTempSrcRegister) );
  928. V_snprintf( szFixedSrcRegister, sizeof( szFixedSrcRegister ), "%sabs(%s)", bAbsNegative ? "-" : "", szTempSrcRegister ) ;
  929. }
  930. return szFixedSrcRegister;
  931. }
  932. // Weird encoding...bits are split apart in the dwToken
  933. inline uint32 GetRegTypeFromToken( uint32 dwToken )
  934. {
  935. return ( ( dwToken & D3DSP_REGTYPE_MASK2 ) >> D3DSP_REGTYPE_SHIFT2 ) | ( ( dwToken & D3DSP_REGTYPE_MASK ) >> D3DSP_REGTYPE_SHIFT );
  936. }
  937. void D3DToGL::FlagIndirectRegister( uint32 dwToken, int *pARLDestReg )
  938. {
  939. if ( !pARLDestReg )
  940. return;
  941. switch ( dwToken & D3DVS_SWIZZLE_MASK & D3DVS_X_W )
  942. {
  943. case D3DVS_X_X:
  944. *pARLDestReg = ARL_DEST_X;
  945. break;
  946. case D3DVS_X_Y:
  947. *pARLDestReg = ARL_DEST_Y;
  948. break;
  949. case D3DVS_X_Z:
  950. *pARLDestReg = ARL_DEST_Z;
  951. break;
  952. case D3DVS_X_W:
  953. *pARLDestReg = ARL_DEST_W;
  954. break;
  955. }
  956. }
  957. //------------------------------------------------------------------------------
  958. // PrintParameterToString()
  959. //
  960. // Helper function which prints ASCII representation of passed Parameter dwToken
  961. // to string. Token defines parameter details. The dwSourceOrDest parameter says
  962. // whether or not this is a source or destination register
  963. //------------------------------------------------------------------------------
  964. void D3DToGL::PrintParameterToString ( uint32 dwToken, uint32 dwSourceOrDest, char *pRegisterName, int nBufLen, bool bForceScalarSource, int *pARLDestReg )
  965. {
  966. char buff[32];
  967. bool bAllowWriteMask = true;
  968. bool bAllowSwizzle = true;
  969. uint32 dwRegNum = dwToken & D3DSP_REGNUM_MASK;
  970. uint32 dwRegType, dwSwizzle;
  971. uint32 dwSrcModifier = D3DSPSM_NONE;
  972. // Clear string to zero length
  973. V_snprintf( pRegisterName, nBufLen, "" );
  974. dwRegType = GetRegTypeFromToken( dwToken );
  975. // If this is a dest register
  976. if ( dwSourceOrDest == DST_REGISTER )
  977. {
  978. // Instruction modifiers
  979. if ( dwToken & D3DSPDM_PARTIALPRECISION )
  980. {
  981. // strcat_s( pRegisterName, nBufLen, "_pp" );
  982. }
  983. if ( dwToken & D3DSPDM_MSAMPCENTROID)
  984. {
  985. // strcat_s( pRegisterName, nBufLen, "_centroid" );
  986. }
  987. }
  988. // If this is a source register
  989. if ( dwSourceOrDest == SRC_REGISTER )
  990. {
  991. dwSrcModifier = dwToken & D3DSP_SRCMOD_MASK;
  992. // If there are any source modifiers, check to see if they're at
  993. // least partially "prefix" and prepend appropriately
  994. if ( dwSrcModifier != D3DSPSM_NONE )
  995. {
  996. switch ( dwSrcModifier )
  997. {
  998. // These four start with just minus... (some may result in "postfix" notation as well later on)
  999. case D3DSPSM_NEG: // negate
  1000. strcat_s( pRegisterName, nBufLen, "-" );
  1001. break;
  1002. case D3DSPSM_BIASNEG: // bias and negate
  1003. case D3DSPSM_SIGNNEG: // sign and negate
  1004. case D3DSPSM_X2NEG: // *2 and negate
  1005. TranslationError();
  1006. strcat_s( pRegisterName, nBufLen, "-" );
  1007. break;
  1008. case D3DSPSM_COMP: // complement
  1009. TranslationError();
  1010. strcat_s( pRegisterName, nBufLen, "1-" );
  1011. break;
  1012. case D3DSPSM_ABS: // abs()
  1013. strcat_s( pRegisterName, nBufLen, "abs(" );
  1014. break;
  1015. case D3DSPSM_ABSNEG: // -abs()
  1016. strcat_s( pRegisterName, nBufLen, "-abs(" );
  1017. break;
  1018. case D3DSPSM_NOT: // for predicate register: "!p0"
  1019. TranslationError();
  1020. strcat_s( pRegisterName, nBufLen, "!" );
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. // Register name (from type and number)
  1026. switch ( dwRegType )
  1027. {
  1028. case D3DSPR_TEMP:
  1029. V_snprintf( buff, sizeof( buff ), "r%d", dwRegNum );
  1030. strcat_s( pRegisterName, nBufLen, buff );
  1031. m_dwTempUsageMask |= 0x00000001 << dwRegNum; // Keep track of the use of this temp
  1032. break;
  1033. case D3DSPR_INPUT:
  1034. if ( !m_bVertexShader && ( dwSourceOrDest == SRC_REGISTER ) )
  1035. {
  1036. if ( m_dwMajorVersion == 3 )
  1037. {
  1038. V_snprintf( buff, sizeof( buff ), "oTempT%d", dwRegNum );
  1039. }
  1040. else
  1041. {
  1042. V_snprintf( buff, sizeof( buff ), dwRegNum == 0 ? "gl_Color" : "gl_SecondaryColor" );
  1043. }
  1044. strcat_s( pRegisterName, nBufLen, buff );
  1045. }
  1046. else
  1047. {
  1048. V_snprintf( buff, sizeof( buff ), "v%d", dwRegNum );
  1049. strcat_s( pRegisterName, nBufLen, buff );
  1050. }
  1051. break;
  1052. case D3DSPR_CONST:
  1053. if ( m_bConstantRegisterDefined[dwRegNum] )
  1054. {
  1055. char szConstantRegName[3];
  1056. if ( m_bVertexShader )
  1057. {
  1058. V_snprintf( szConstantRegName, 3, "vd" );
  1059. }
  1060. else
  1061. {
  1062. V_snprintf( szConstantRegName, 3, "pd" );
  1063. }
  1064. // Put defined constants into their own namespace "d"
  1065. V_snprintf( buff, sizeof( buff ), "%s%d", szConstantRegName, dwRegNum );
  1066. strcat_s( pRegisterName, nBufLen, buff );
  1067. }
  1068. else if ( dwToken & D3DSHADER_ADDRESSMODE_MASK ) // Indirect addressing (e.g. skinning in a vertex shader)
  1069. {
  1070. char szConstantRegName[16];
  1071. if ( m_bVertexShader )
  1072. {
  1073. V_snprintf( szConstantRegName, 3, "vc" );
  1074. }
  1075. else // No indirect addressing in PS, this shouldn't happen
  1076. {
  1077. TranslationError();
  1078. V_snprintf( szConstantRegName, 3, "pc" );
  1079. }
  1080. if ( ( m_bGenerateBoneUniformBuffer ) && ( dwRegNum >= DXABSTRACT_VS_FIRST_BONE_SLOT ) )
  1081. {
  1082. if( dwRegNum < DXABSTRACT_VS_LAST_BONE_SLOT )
  1083. {
  1084. dwRegNum -= DXABSTRACT_VS_FIRST_BONE_SLOT;
  1085. V_strcpy( szConstantRegName, "vcbones" );
  1086. m_nHighestBoneRegister = ( DXABSTRACT_VS_PARAM_SLOTS - 1 ) - DXABSTRACT_VS_FIRST_BONE_SLOT;
  1087. }
  1088. else
  1089. {
  1090. dwRegNum -= ( DXABSTRACT_VS_LAST_BONE_SLOT + 1 ) - DXABSTRACT_VS_FIRST_BONE_SLOT;
  1091. m_nHighestRegister = m_bGenerateBoneUniformBuffer ? ( ( DXABSTRACT_VS_PARAM_SLOTS - 1 ) - ( ( DXABSTRACT_VS_LAST_BONE_SLOT + 1 ) - DXABSTRACT_VS_FIRST_BONE_SLOT ) ): ( DXABSTRACT_VS_PARAM_SLOTS - 1 );
  1092. }
  1093. }
  1094. else
  1095. {
  1096. m_nHighestRegister = m_bGenerateBoneUniformBuffer ? ( ( DXABSTRACT_VS_PARAM_SLOTS - 1 ) - ( ( DXABSTRACT_VS_LAST_BONE_SLOT + 1 ) - DXABSTRACT_VS_FIRST_BONE_SLOT ) ): ( DXABSTRACT_VS_PARAM_SLOTS - 1 );
  1097. }
  1098. // Index into single pc/vc[] register array with relative addressing
  1099. int nDstReg = -1;
  1100. FlagIndirectRegister( GetNextToken(), &nDstReg );
  1101. if ( pARLDestReg )
  1102. *pARLDestReg = nDstReg;
  1103. Assert( nDstReg != ARL_DEST_NONE );
  1104. int nSrcSwizzle = 'x';
  1105. if ( nDstReg == ARL_DEST_Y )
  1106. nSrcSwizzle = 'y';
  1107. else if ( nDstReg == ARL_DEST_Z )
  1108. nSrcSwizzle = 'z';
  1109. else if ( nDstReg == ARL_DEST_W )
  1110. nSrcSwizzle = 'w';
  1111. V_snprintf( buff, sizeof( buff ), "%s[int(va_r.%c) + %d]", szConstantRegName, nSrcSwizzle, dwRegNum );
  1112. strcat_s( pRegisterName, nBufLen, buff );
  1113. // Must allow swizzling, otherwise this example doesn't compile right: mad r3.xyz, c27[a0.w].w, r3, r7
  1114. //bAllowSwizzle = false;
  1115. }
  1116. else // Direct addressing of constant array
  1117. {
  1118. char szConstantRegName[16];
  1119. V_snprintf( szConstantRegName, 3, m_bVertexShader ? "vc" : "pc" );
  1120. if ( ( m_bGenerateBoneUniformBuffer ) && ( dwRegNum >= DXABSTRACT_VS_FIRST_BONE_SLOT ) )
  1121. {
  1122. if( dwRegNum < DXABSTRACT_VS_LAST_BONE_SLOT )
  1123. {
  1124. dwRegNum -= DXABSTRACT_VS_FIRST_BONE_SLOT;
  1125. V_strcpy( szConstantRegName, "vcbones" );
  1126. m_nHighestBoneRegister = MAX( m_nHighestBoneRegister, (int)dwRegNum );
  1127. }
  1128. else
  1129. {
  1130. // handles case where constants after the bones are used (c217 onwards), these are to be concatenated with those before the bones (c0-c57)
  1131. // keep track of regnum for concatenated array
  1132. dwRegNum -= ( DXABSTRACT_VS_LAST_BONE_SLOT + 1 ) - DXABSTRACT_VS_FIRST_BONE_SLOT;
  1133. m_nHighestRegister = MAX( m_nHighestRegister, dwRegNum );
  1134. }
  1135. }
  1136. else
  1137. {
  1138. //// NOGO if (dwRegNum != 255) // have seen cases where dwRegNum is 0xFF... need to figure out where those opcodes are coming from
  1139. {
  1140. m_nHighestRegister = MAX( m_nHighestRegister, dwRegNum );
  1141. }
  1142. Assert( m_nHighestRegister < DXABSTRACT_VS_PARAM_SLOTS );
  1143. }
  1144. // Index into single pc/vc[] register array with absolute addressing, same for GLSL and ASM
  1145. V_snprintf( buff, sizeof( buff ), "%s[%d]", szConstantRegName, dwRegNum );
  1146. strcat_s( pRegisterName, nBufLen, buff );
  1147. }
  1148. break;
  1149. case D3DSPR_ADDR: // aliases to D3DSPR_TEXTURE
  1150. if ( m_bVertexShader )
  1151. {
  1152. Assert( dwRegNum == 0 );
  1153. V_snprintf( buff, sizeof( buff ), "va_r" );
  1154. }
  1155. else // D3DSPR_TEXTURE in the pixel shader
  1156. {
  1157. // If dest reg, this is an iterator/varying declaration
  1158. if ( dwSourceOrDest == DST_REGISTER )
  1159. {
  1160. // Is this iterator centroid?
  1161. if ( m_nCentroidMask & ( 0x00000001 << dwRegNum ) )
  1162. {
  1163. V_snprintf( buff, sizeof( buff ), "centroid varying vec4 oT%d", dwRegNum ); // centroid varying
  1164. }
  1165. else
  1166. {
  1167. V_snprintf( buff, sizeof( buff ), "varying vec4 oT%d", dwRegNum );
  1168. }
  1169. bAllowWriteMask = false;
  1170. }
  1171. else // source register
  1172. {
  1173. V_snprintf( buff, sizeof( buff ), "oT%d", dwRegNum );
  1174. }
  1175. }
  1176. strcat_s( pRegisterName, nBufLen, buff );
  1177. break;
  1178. case D3DSPR_RASTOUT: // vertex shader oPos
  1179. Assert( m_bVertexShader );
  1180. Assert( m_dwMajorVersion == 2 );
  1181. switch( dwRegNum )
  1182. {
  1183. case D3DSRO_POSITION:
  1184. strcat_s( pRegisterName, nBufLen, "vTempPos" ); // In GLSL, this ends up in gl_Position later on
  1185. m_bDeclareVSOPos = true;
  1186. break;
  1187. case D3DSRO_FOG:
  1188. strcat_s( pRegisterName, nBufLen, "gl_FogFragCoord" );
  1189. m_bDeclareVSOFog = true;
  1190. break;
  1191. default:
  1192. printf( "\nD3DSPR_RASTOUT: dwRegNum is %08x and token is %08x", dwRegNum, dwToken );
  1193. TranslationError();
  1194. break;
  1195. }
  1196. break;
  1197. case D3DSPR_ATTROUT:
  1198. Assert( m_bVertexShader );
  1199. Assert( m_dwMajorVersion == 2 );
  1200. if ( dwRegNum == 0 )
  1201. {
  1202. V_snprintf( buff, sizeof( buff ), "gl_FrontColor" );
  1203. }
  1204. else if ( dwRegNum == 1 )
  1205. {
  1206. V_snprintf( buff, sizeof( buff ), "gl_FrontSecondaryColor" );
  1207. }
  1208. else
  1209. {
  1210. Error( "Invalid D3DSPR_ATTROUT index" );
  1211. }
  1212. strcat_s( pRegisterName, nBufLen, buff );
  1213. break;
  1214. case D3DSPR_TEXCRDOUT: // aliases to D3DSPR_OUTPUT
  1215. if ( m_bVertexShader )
  1216. {
  1217. if ( m_nVSPositionOutput == (int32) dwRegNum )
  1218. {
  1219. V_snprintf( buff, sizeof( buff ), "vTempPos" ); // This output varying is the position
  1220. }
  1221. else if ( m_dwMajorVersion == 3 )
  1222. {
  1223. V_snprintf( buff, sizeof( buff ), "oTempT%d", dwRegNum );
  1224. }
  1225. else
  1226. {
  1227. V_snprintf( buff, sizeof( buff ), "oT%d", dwRegNum );
  1228. }
  1229. m_dwTexCoordOutMask |= ( 0x00000001 << dwRegNum );
  1230. }
  1231. else
  1232. {
  1233. V_snprintf( buff, sizeof( buff ), "oC%d", dwRegNum );
  1234. }
  1235. strcat_s( pRegisterName, nBufLen, buff );
  1236. break;
  1237. case D3DSPR_CONSTINT:
  1238. V_snprintf( buff, sizeof( buff ), "i%d", dwRegNum ); // Loops use these
  1239. strcat_s( pRegisterName, nBufLen, buff );
  1240. m_dwConstIntUsageMask |= 0x00000001 << dwRegNum; // Keep track of the use of this integer constant
  1241. break;
  1242. case D3DSPR_COLOROUT:
  1243. V_snprintf( buff, sizeof( buff ), "gl_FragData[%d]", dwRegNum );
  1244. strcat_s( pRegisterName, nBufLen, buff );
  1245. m_bOutputColorRegister[dwRegNum] = true;
  1246. break;
  1247. case D3DSPR_DEPTHOUT:
  1248. V_snprintf( buff, sizeof( buff ), "gl_FragDepth" );
  1249. strcat_s( pRegisterName, nBufLen, buff );
  1250. m_bOutputDepthRegister = true;
  1251. break;
  1252. case D3DSPR_SAMPLER:
  1253. V_snprintf( buff, sizeof( buff ), "sampler%d", dwRegNum );
  1254. strcat_s( pRegisterName, nBufLen, buff );
  1255. break;
  1256. case D3DSPR_CONST2:
  1257. TranslationError();
  1258. V_snprintf( buff, sizeof( buff ), "c%d", dwRegNum+2048);
  1259. strcat_s( pRegisterName, nBufLen, buff );
  1260. break;
  1261. case D3DSPR_CONST3:
  1262. TranslationError();
  1263. V_snprintf( buff, sizeof( buff ), "c%d", dwRegNum+4096);
  1264. strcat_s( pRegisterName, nBufLen, buff );
  1265. break;
  1266. case D3DSPR_CONST4:
  1267. TranslationError();
  1268. V_snprintf( buff, sizeof( buff ), "c%d", dwRegNum+6144);
  1269. strcat_s( pRegisterName, nBufLen, buff );
  1270. break;
  1271. case D3DSPR_CONSTBOOL:
  1272. V_snprintf( buff, sizeof( buff ), m_bVertexShader ? "b%d" : "fb%d", dwRegNum );
  1273. strcat_s( pRegisterName, nBufLen, buff );
  1274. m_dwConstBoolUsageMask |= 0x00000001 << dwRegNum; // Keep track of the use of this bool constant
  1275. break;
  1276. case D3DSPR_LOOP:
  1277. TranslationError();
  1278. V_snprintf( buff, sizeof( buff ), "aL%d", dwRegNum );
  1279. strcat_s( pRegisterName, nBufLen, buff );
  1280. break;
  1281. case D3DSPR_TEMPFLOAT16:
  1282. TranslationError();
  1283. V_snprintf( buff, sizeof( buff ), "temp_float16_xxx%d", dwRegNum );
  1284. strcat_s( pRegisterName, nBufLen, buff );
  1285. break;
  1286. case D3DSPR_MISCTYPE:
  1287. Assert( dwRegNum == 0 ); // So far, we know that MISC[0] is gl_FragCoord (aka vPos in DX ASM parlance), but we don't know about any other MISC registers
  1288. V_snprintf( buff, sizeof( buff ), "gl_FragCoord" );
  1289. strcat_s( pRegisterName, nBufLen, buff );
  1290. break;
  1291. case D3DSPR_LABEL:
  1292. TranslationError();
  1293. V_snprintf( buff, sizeof( buff ), "label%d", dwRegNum );
  1294. strcat_s( pRegisterName, nBufLen, buff );
  1295. break;
  1296. case D3DSPR_PREDICATE:
  1297. TranslationError();
  1298. V_snprintf( buff, sizeof( buff ), "p%d", dwRegNum );
  1299. strcat_s( pRegisterName, nBufLen, buff );
  1300. break;
  1301. }
  1302. // If this is a dest register
  1303. if ( dwSourceOrDest == DST_REGISTER )
  1304. {
  1305. //
  1306. // Write masks
  1307. //
  1308. // If some (not all, not none) of the write masks are set, we should include them
  1309. //
  1310. if ( bAllowWriteMask && ( !((dwToken & D3DSP_WRITEMASK_ALL) == D3DSP_WRITEMASK_ALL) || ((dwToken & D3DSP_WRITEMASK_ALL) == 0x00000000) ) )
  1311. {
  1312. // Put the dot on there
  1313. strcat_s( pRegisterName, nBufLen, "." );
  1314. // Optionally put on the x, y, z or w
  1315. int nMasksWritten = 0;
  1316. if ( dwToken & D3DSP_WRITEMASK_0 )
  1317. {
  1318. strcat_s( pRegisterName, nBufLen, "x" );
  1319. ++nMasksWritten;
  1320. }
  1321. if ( dwToken & D3DSP_WRITEMASK_1 )
  1322. {
  1323. strcat_s( pRegisterName, nBufLen, "y" );
  1324. ++nMasksWritten;
  1325. }
  1326. if ( dwToken & D3DSP_WRITEMASK_2 )
  1327. {
  1328. strcat_s( pRegisterName, nBufLen, "z" );
  1329. ++nMasksWritten;
  1330. }
  1331. if ( dwToken & D3DSP_WRITEMASK_3 )
  1332. {
  1333. strcat_s( pRegisterName, nBufLen, "w" );
  1334. ++nMasksWritten;
  1335. }
  1336. }
  1337. }
  1338. else // must be a source register
  1339. {
  1340. if ( bAllowSwizzle ) // relative addressing hard-codes the swizzle on a0.x
  1341. {
  1342. uint32 dwXSwizzle, dwYSwizzle, dwZSwizzle, dwWSwizzle;
  1343. // Mask out the swizzle modifier
  1344. dwSwizzle = dwToken & D3DVS_SWIZZLE_MASK;
  1345. // If there are any swizzles at all, tack on the appropriate notation
  1346. if ( dwSwizzle != D3DVS_NOSWIZZLE )
  1347. {
  1348. // Separate out the two-bit codes for each component swizzle
  1349. dwXSwizzle = dwSwizzle & D3DVS_X_W;
  1350. dwYSwizzle = dwSwizzle & D3DVS_Y_W;
  1351. dwZSwizzle = dwSwizzle & D3DVS_Z_W;
  1352. dwWSwizzle = dwSwizzle & D3DVS_W_W;
  1353. // Put on the dot
  1354. strcat_s( pRegisterName, nBufLen, "." );
  1355. // See where X comes from
  1356. switch ( dwXSwizzle )
  1357. {
  1358. case D3DVS_X_X:
  1359. strcat_s( pRegisterName, nBufLen, "x" );
  1360. break;
  1361. case D3DVS_X_Y:
  1362. strcat_s( pRegisterName, nBufLen, "y" );
  1363. break;
  1364. case D3DVS_X_Z:
  1365. strcat_s( pRegisterName, nBufLen, "z" );
  1366. break;
  1367. case D3DVS_X_W:
  1368. strcat_s( pRegisterName, nBufLen, "w" );
  1369. break;
  1370. }
  1371. if ( !bForceScalarSource )
  1372. {
  1373. // If the source of the remaining components are aren't
  1374. // identical to the source of x, continue with swizzle
  1375. if ( ((dwXSwizzle >> D3DVS_SWIZZLE_SHIFT) != (dwYSwizzle >> (D3DVS_SWIZZLE_SHIFT + 2))) || // X and Y sources match?
  1376. ((dwXSwizzle >> D3DVS_SWIZZLE_SHIFT) != (dwZSwizzle >> (D3DVS_SWIZZLE_SHIFT + 4))) || // X and Z sources match?
  1377. ((dwXSwizzle >> D3DVS_SWIZZLE_SHIFT) != (dwWSwizzle >> (D3DVS_SWIZZLE_SHIFT + 6)))) // X and W sources match?
  1378. {
  1379. // OpenGL seems to want us to have either 1 or 4 components in a swizzle, so just plow on through the rest
  1380. switch ( dwYSwizzle )
  1381. {
  1382. case D3DVS_Y_X:
  1383. strcat_s( pRegisterName, nBufLen, "x" );
  1384. break;
  1385. case D3DVS_Y_Y:
  1386. strcat_s( pRegisterName, nBufLen, "y" );
  1387. break;
  1388. case D3DVS_Y_Z:
  1389. strcat_s( pRegisterName, nBufLen, "z" );
  1390. break;
  1391. case D3DVS_Y_W:
  1392. strcat_s( pRegisterName, nBufLen, "w" );
  1393. break;
  1394. }
  1395. switch ( dwZSwizzle )
  1396. {
  1397. case D3DVS_Z_X:
  1398. strcat_s( pRegisterName, nBufLen, "x" );
  1399. break;
  1400. case D3DVS_Z_Y:
  1401. strcat_s( pRegisterName, nBufLen, "y" );
  1402. break;
  1403. case D3DVS_Z_Z:
  1404. strcat_s( pRegisterName, nBufLen, "z" );
  1405. break;
  1406. case D3DVS_Z_W:
  1407. strcat_s( pRegisterName, nBufLen, "w" );
  1408. break;
  1409. }
  1410. switch ( dwWSwizzle )
  1411. {
  1412. case D3DVS_W_X:
  1413. strcat_s( pRegisterName, nBufLen, "x" );
  1414. break;
  1415. case D3DVS_W_Y:
  1416. strcat_s( pRegisterName, nBufLen, "y" );
  1417. break;
  1418. case D3DVS_W_Z:
  1419. strcat_s( pRegisterName, nBufLen, "z" );
  1420. break;
  1421. case D3DVS_W_W:
  1422. strcat_s( pRegisterName, nBufLen, "w" );
  1423. break;
  1424. }
  1425. }
  1426. } // end !bForceScalarSource
  1427. }
  1428. else // dwSwizzle == D3DVS_NOSWIZZLE
  1429. {
  1430. // If this is a MOVA / ARL, GL on the Mac requires us to tack the .x onto the source register
  1431. if ( bForceScalarSource )
  1432. {
  1433. strcat_s( pRegisterName, nBufLen, ".x" );
  1434. }
  1435. }
  1436. } // bAllowSwizzle
  1437. // If there are any source modifiers, check to see if they're at
  1438. // least partially "postfix" and tack them on as appropriate
  1439. if ( dwSrcModifier != D3DSPSM_NONE )
  1440. {
  1441. switch ( dwSrcModifier )
  1442. {
  1443. case D3DSPSM_BIAS: // bias
  1444. case D3DSPSM_BIASNEG: // bias and negate
  1445. TranslationError();
  1446. strcat_s( pRegisterName, nBufLen, "_bx2" );
  1447. break;
  1448. case D3DSPSM_SIGN: // sign
  1449. case D3DSPSM_SIGNNEG: // sign and negate
  1450. TranslationError();
  1451. strcat_s( pRegisterName, nBufLen, "_sgn" );
  1452. break;
  1453. case D3DSPSM_X2: // *2
  1454. case D3DSPSM_X2NEG: // *2 and negate
  1455. TranslationError();
  1456. strcat_s( pRegisterName, nBufLen, "_x2" );
  1457. break;
  1458. case D3DSPSM_ABS: // abs()
  1459. case D3DSPSM_ABSNEG: // -abs()
  1460. strcat_s( pRegisterName, nBufLen, ")" );
  1461. break;
  1462. case D3DSPSM_DZ: // divide through by z component
  1463. TranslationError();
  1464. strcat_s( pRegisterName, nBufLen, "_dz" );
  1465. break;
  1466. case D3DSPSM_DW: // divide through by w component
  1467. TranslationError();
  1468. strcat_s( pRegisterName, nBufLen, "_dw" );
  1469. break;
  1470. }
  1471. } // end postfix modifiers (really only ps.1.x)
  1472. }
  1473. }
  1474. void D3DToGL::RecordInputAndOutputPositions()
  1475. {
  1476. // Remember where we are in the token stream.
  1477. m_pRecordedInputTokenStart = m_pdwNextToken;
  1478. // Remember where our outputs are.
  1479. m_nRecordedParamCodeStrlen = V_strlen( (char*)m_pBufParamCode->Base() );
  1480. m_nRecordedALUCodeStrlen = V_strlen( (char*)m_pBufALUCode->Base() );
  1481. m_nRecordedAttribCodeStrlen = V_strlen( (char*)m_pBufAttribCode->Base() );
  1482. }
  1483. void D3DToGL::AddTokenHexCodeToBuffer( char *pBuffer, int nSize, int nLastStrlen )
  1484. {
  1485. int nCurStrlen = V_strlen( pBuffer );
  1486. if ( nCurStrlen == nLastStrlen )
  1487. return;
  1488. // Build a string with all the hex codes of the tokens since last time.
  1489. char szHex[512];
  1490. szHex[0] = '\n';
  1491. V_snprintf( &szHex[1], sizeof( szHex )-1, HEXCODE_HEADER );
  1492. int nTokens = MIN( 10, m_pdwNextToken - m_pRecordedInputTokenStart );
  1493. for ( int i=0; i < nTokens; i++ )
  1494. {
  1495. char szTemp[32];
  1496. V_snprintf( szTemp, sizeof( szTemp ), "0x%x ", m_pRecordedInputTokenStart[i] );
  1497. V_strncat( szHex, szTemp, sizeof( szHex ) );
  1498. }
  1499. V_strncat( szHex, "\n", sizeof( szHex ) );
  1500. // Insert the hex codes into the string.
  1501. int nBytesToInsert = V_strlen( szHex );
  1502. if ( nCurStrlen + nBytesToInsert + 1 >= nSize )
  1503. Error( "Buffer overflow writing token hex codes" );
  1504. if ( m_bPutHexCodesAfterLines )
  1505. {
  1506. // Put it at the end of the last line.
  1507. if ( pBuffer[nCurStrlen-1] == '\n' )
  1508. pBuffer[nCurStrlen-1] = 0;
  1509. V_strncat( pBuffer, &szHex[1], nSize );
  1510. }
  1511. else
  1512. {
  1513. memmove( pBuffer + nLastStrlen + nBytesToInsert, pBuffer + nLastStrlen, nCurStrlen - nLastStrlen + 1 );
  1514. memcpy( pBuffer + nLastStrlen, szHex, nBytesToInsert );
  1515. }
  1516. }
  1517. void D3DToGL::AddTokenHexCode()
  1518. {
  1519. if ( m_pdwNextToken > m_pRecordedInputTokenStart )
  1520. {
  1521. AddTokenHexCodeToBuffer( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size(), m_nRecordedParamCodeStrlen );
  1522. AddTokenHexCodeToBuffer( (char*)m_pBufALUCode->Base(), m_pBufALUCode->Size(), m_nRecordedALUCodeStrlen );
  1523. AddTokenHexCodeToBuffer( (char*)m_pBufAttribCode->Base(), m_pBufAttribCode->Size(), m_nRecordedAttribCodeStrlen );
  1524. }
  1525. }
  1526. uint32 D3DToGL::MaintainAttributeMap( uint32 dwToken, uint32 dwRegToken )
  1527. {
  1528. // Check that this reg index has not been used before - if it has, let Houston know
  1529. uint dwRegIndex = dwRegToken & D3DSP_REGNUM_MASK;
  1530. if ( m_dwAttribMap[ dwRegIndex ] == 0xFFFFFFFF )
  1531. {
  1532. // log it
  1533. // semantic/usage in the higher nibble
  1534. // usage index in the low nibble
  1535. uint usage = dwToken & D3DSP_DCL_USAGE_MASK;
  1536. uint usageindex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  1537. m_dwAttribMap[ dwRegIndex ] = ( usage << 4 ) | usageindex;
  1538. // avoid writing 0xBB since runtime code uses that for an 'unused' marker
  1539. if ( m_dwAttribMap[ dwRegIndex ] == 0xBB )
  1540. {
  1541. Debugger();
  1542. }
  1543. }
  1544. else
  1545. {
  1546. //not OK
  1547. Debugger();
  1548. }
  1549. return dwRegIndex;
  1550. }
  1551. void D3DToGL::Handle_DCL()
  1552. {
  1553. uint32 dwToken = GetNextToken(); // What kind of dcl is this...
  1554. uint32 dwRegToken = GetNextToken(); // Look ahead to register token
  1555. uint32 dwUsage = ( dwToken & D3DSP_DCL_USAGE_MASK );
  1556. uint32 dwUsageIndex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  1557. uint32 dwRegNum = dwRegToken & D3DSP_REGNUM_MASK;
  1558. uint32 nRegType = GetRegTypeFromToken( dwRegToken );
  1559. if ( m_bVertexShader )
  1560. {
  1561. // If this is an output, remember the index (what the ASM code calls o0, o1, o2..) and the semantic.
  1562. // When GetParameterString( DST_REGISTER ) hits this one, we'll return "oN".
  1563. // At the end of the main() function, we'll insert a bunch of statements like "gl_Color = o2" based on what we remembered here.
  1564. if ( ( m_dwMajorVersion >= 3 ) && ( nRegType == D3DSPR_OUTPUT ) )
  1565. {
  1566. // uint32 dwRegComponents = ( dwRegToken & D3DSP_WRITEMASK_ALL ) >> 16; // Components used by the output register (1 means float, 3 means vec2, 7 means vec3, f means vec4)
  1567. if ( dwRegNum >= MAX_DECLARED_OUTPUTS )
  1568. Error( "Output register number (%d) too high (only %d supported).", dwRegNum, MAX_DECLARED_OUTPUTS );
  1569. if ( m_DeclaredOutputs[dwRegNum] != UNDECLARED_OUTPUT )
  1570. Error( "Output dcl_ hit for register #%d more than once!", dwRegNum );
  1571. Assert( dwToken != UNDECLARED_OUTPUT );
  1572. m_DeclaredOutputs[dwRegNum] = dwToken;
  1573. //uint32 dwUsage = ( dwToken & D3DSP_DCL_USAGE_MASK );
  1574. //uint32 dwUsageIndex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  1575. // Flag which o# output register maps to gl_Position
  1576. if ( dwUsage == D3DDECLUSAGE_POSITION )
  1577. {
  1578. m_nVSPositionOutput = dwUsageIndex;
  1579. m_bDeclareVSOPos = true;
  1580. }
  1581. if ( m_bAddHexCodeComments )
  1582. {
  1583. CUtlString sParam2 = GetUsageAndIndexString( dwToken, SEMANTIC_OUTPUT );
  1584. PrintToBuf( *m_pBufHeaderCode, "// [GL remembering that oT%d maps to %s]\n", dwRegNum, sParam2.String() );
  1585. }
  1586. }
  1587. else if ( GetRegType( dwRegToken ) == D3DSPR_SAMPLER )
  1588. {
  1589. // We can support vertex texturing if necessary, but I can't find a use case in any branch. (HW morphing in L4D2 isn't enabled, and the comments indicate that r_hwmorph isn't compatible with mat_queue_mode anyway, and CS:GO/DoTA don't use vertex shader texturing.)
  1590. TranslationError();
  1591. int nRegNum = dwRegToken & D3DSP_REGNUM_MASK;
  1592. switch ( TextureType( dwToken ) )
  1593. {
  1594. default:
  1595. case D3DSTT_UNKNOWN:
  1596. case D3DSTT_2D:
  1597. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_2D;
  1598. break;
  1599. case D3DSTT_CUBE:
  1600. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_CUBE;
  1601. break;
  1602. case D3DSTT_VOLUME:
  1603. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_3D;
  1604. break;
  1605. }
  1606. // Track sampler declarations
  1607. m_dwSamplerUsageMask |= 1 << nRegNum;
  1608. }
  1609. else
  1610. {
  1611. Assert( GetRegType( dwRegToken ) == D3DSPR_INPUT);
  1612. CUtlString sParam1 = GetParameterString( dwRegToken, DST_REGISTER, false, NULL );
  1613. CUtlString sParam2 = GetUsageAndIndexString( dwToken, SEMANTIC_INPUT );
  1614. sParam2 = FixGLSLSwizzle( sParam1, sParam2 );
  1615. PrintToBuf( *m_pBufHeaderCode, "attribute vec4 %s; // ", sParam1.String() );
  1616. MaintainAttributeMap( dwToken, dwRegToken );
  1617. char temp[128];
  1618. // regnum goes straight into the vertex.attrib[n] index
  1619. sprintf( temp, "%08x %08x\n", dwToken, dwRegToken );
  1620. StrcatToHeaderCode( temp );
  1621. }
  1622. }
  1623. else // Pixel shader
  1624. {
  1625. // If the register is a sampler, the dcl has a dimension decorator that we have to save for subsequent TEX instructions
  1626. uint32 nRegType = GetRegType( dwRegToken );
  1627. if ( nRegType == D3DSPR_SAMPLER )
  1628. {
  1629. int nRegNum = dwRegToken & D3DSP_REGNUM_MASK;
  1630. switch ( TextureType( dwToken ) )
  1631. {
  1632. default:
  1633. case D3DSTT_UNKNOWN:
  1634. case D3DSTT_2D:
  1635. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_2D;
  1636. break;
  1637. case D3DSTT_CUBE:
  1638. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_CUBE;
  1639. break;
  1640. case D3DSTT_VOLUME:
  1641. m_dwSamplerTypes[nRegNum] = SAMPLER_TYPE_3D;
  1642. break;
  1643. }
  1644. // Track sampler declarations
  1645. m_dwSamplerUsageMask |= 1 << nRegNum;
  1646. }
  1647. else // Not a sampler, we're going to generate varying declaration code
  1648. {
  1649. // In pixel shaders we only declare texture coordinate varyings since they may be using centroid
  1650. if ( ( m_dwMajorVersion == 3 ) && ( nRegType == D3DSPR_INPUT ) )
  1651. {
  1652. Assert( m_DeclaredInputs[dwRegNum] == UNDECLARED_INPUT );
  1653. m_DeclaredInputs[dwRegNum] = dwToken;
  1654. if ( ( dwUsage != D3DDECLUSAGE_COLOR ) && ( dwUsage != D3DDECLUSAGE_TEXCOORD ) )
  1655. {
  1656. TranslationError(); // Not supported yet, but can be if we need it.
  1657. }
  1658. if ( dwUsage == D3DDECLUSAGE_TEXCOORD )
  1659. {
  1660. char buf[256];
  1661. if ( m_nCentroidMask & ( 0x00000001 << dwUsageIndex ) )
  1662. {
  1663. V_snprintf( buf, sizeof( buf ), "centroid varying vec4 oT%d;\n", dwUsageIndex ); // centroid varying
  1664. }
  1665. else
  1666. {
  1667. V_snprintf( buf, sizeof( buf ), "varying vec4 oT%d;\n", dwUsageIndex );
  1668. }
  1669. StrcatToHeaderCode( buf );
  1670. }
  1671. }
  1672. else if ( nRegType == D3DSPR_TEXTURE )
  1673. {
  1674. char buff[256];
  1675. PrintParameterToString( dwRegToken, DST_REGISTER, buff, sizeof( buff ), false, NULL );
  1676. PrintToBuf( *m_pBufHeaderCode, "%s;\n",buff );
  1677. }
  1678. else
  1679. {
  1680. // No need to declare anything (probably D3DSPR_MISCTYPE either VPOS or VFACE)
  1681. }
  1682. }
  1683. }
  1684. }
  1685. static bool IsFloatNaN( float f )
  1686. {
  1687. const uint nBits = *reinterpret_cast<uint*>(&f);
  1688. const uint nExponent = ( nBits >> 23 ) & 0xFF;
  1689. return ( nExponent == 255 );
  1690. }
  1691. static inline bool EqualTol( double a, double b, double t )
  1692. {
  1693. return fabs( a - b ) <= ( ( MAX( fabs( a ), fabs( b ) ) + 1.0 ) * t );
  1694. }
  1695. // Originally written by Bruce Dawson, see:
  1696. // See http://randomascii.wordpress.com/2012/03/08/float-precisionfrom-zero-to-100-digits-2/
  1697. // This class represents a very limited high-precision number with 'count' 32-bit
  1698. // unsigned elements.
  1699. template <int count>
  1700. struct HighPrec
  1701. {
  1702. typedef unsigned T;
  1703. typedef unsigned long long Product_t;
  1704. static const int kWordShift = 32;
  1705. HighPrec()
  1706. {
  1707. memset(m_data, 0, sizeof(m_data));
  1708. m_nLowestNonZeroIndex = ARRAYSIZE(m_data);
  1709. }
  1710. // Insert the bits from value into m_data, shifted in from the bottom (least
  1711. // significant end) by the specified number of bits. A shift of zero or less
  1712. // means that none of the bits will be shifted in. A shift of one means that
  1713. // the high bit of value will be in the bottom of the last element of m_data -
  1714. // the least significant bit. A shift of kWordShift means that value will be
  1715. // in the least significant element of m_data, and so on.
  1716. void InsertLowBits(T value, int shiftAmount)
  1717. {
  1718. if (shiftAmount <= 0)
  1719. return;
  1720. int subShift = shiftAmount & (kWordShift - 1);
  1721. int bigShift = shiftAmount / kWordShift;
  1722. Product_t result = (Product_t)value << subShift;
  1723. T resultLow = (T)result;
  1724. T resultHigh = result >> kWordShift;
  1725. // Use an unsigned type so that negative numbers will become large,
  1726. // which makes the range checking below simpler.
  1727. unsigned highIndex = ARRAYSIZE(m_data) - 1 - bigShift;
  1728. // Write the results to the data array. If the index is too large
  1729. // then that means that the data was shifted off the edge.
  1730. if ( (highIndex < ARRAYSIZE(m_data)) && ( resultHigh ) )
  1731. {
  1732. m_data[highIndex] |= resultHigh;
  1733. m_nLowestNonZeroIndex = MIN( m_nLowestNonZeroIndex, highIndex );
  1734. }
  1735. if ( ( highIndex + 1 < ARRAYSIZE(m_data)) && ( resultLow ) )
  1736. {
  1737. m_data[highIndex + 1] |= resultLow;
  1738. m_nLowestNonZeroIndex = MIN( m_nLowestNonZeroIndex, highIndex + 1 );
  1739. }
  1740. }
  1741. // Insert the bits from value into m_data, shifted in from the top (most
  1742. // significant end) by the specified number of bits. A shift of zero or less
  1743. // means that none of the bits will be shifted in. A shift of one means that
  1744. // the low bit of value will be in the top of the first element of m_data -
  1745. // the most significant bit. A shift of kWordShift means that value will be
  1746. // in the most significant element of m_data, and so on.
  1747. void InsertTopBits(T value, int shiftAmount)
  1748. {
  1749. InsertLowBits(value, (ARRAYSIZE(m_data) + 1) * kWordShift - shiftAmount);
  1750. }
  1751. // Return true if all elements of m_data are zero.
  1752. bool IsZero() const
  1753. {
  1754. bool bIsZero = ( m_nLowestNonZeroIndex == ARRAYSIZE(m_data) );
  1755. #ifdef DEBUG
  1756. for (int i = 0; i < ARRAYSIZE(m_data); ++i)
  1757. {
  1758. if (m_data[i])
  1759. {
  1760. Assert( !bIsZero );
  1761. return false;
  1762. }
  1763. }
  1764. Assert( bIsZero );
  1765. #endif
  1766. return bIsZero;
  1767. }
  1768. // Divide by div and return the remainder, from 0 to div-1.
  1769. // Standard long-division algorithm.
  1770. T DivReturnRemainder(T divisor)
  1771. {
  1772. T remainder = 0;
  1773. #ifdef DEBUG
  1774. for (uint j = 0; j < m_nLowestNonZeroIndex; ++j)
  1775. {
  1776. Assert( m_data[j] == 0 );
  1777. }
  1778. #endif
  1779. int nNewLowestNonZeroIndex = ARRAYSIZE(m_data);
  1780. for (int i = m_nLowestNonZeroIndex; i < ARRAYSIZE(m_data); ++i)
  1781. {
  1782. Product_t dividend = ((Product_t)remainder << kWordShift) + m_data[i];
  1783. Product_t result = dividend / divisor;
  1784. remainder = T(dividend % divisor);
  1785. m_data[i] = T(result);
  1786. if ( ( result ) && ( nNewLowestNonZeroIndex == ARRAYSIZE(m_data) ) )
  1787. nNewLowestNonZeroIndex = i;
  1788. }
  1789. m_nLowestNonZeroIndex = nNewLowestNonZeroIndex;
  1790. return remainder;
  1791. }
  1792. // The individual 'digits' (32-bit unsigned integers actually) that
  1793. // make up the number. The most-significant digit is in m_data[0].
  1794. T m_data[count];
  1795. uint m_nLowestNonZeroIndex;
  1796. };
  1797. union Double_t
  1798. {
  1799. Double_t(double num = 0.0f) : f(num) {}
  1800. // Portable extraction of components.
  1801. bool Negative() const { return (i >> 63) != 0; }
  1802. int64_t RawMantissa() const { return i & ((1LL << 52) - 1); }
  1803. int64_t RawExponent() const { return (i >> 52) & 0x7FF; }
  1804. int64_t i;
  1805. double f;
  1806. };
  1807. static uint PrintDoubleInt( char *pBuf, uint nBufSize, double f, uint nMinChars )
  1808. {
  1809. static const char *pDigits = "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
  1810. Assert( !nMinChars || ( ( nMinChars % 6 ) == 0 ) );
  1811. char *pLastChar = pBuf + nBufSize - 1;
  1812. char *pDst = pLastChar;
  1813. *pDst-- = '\0';
  1814. // Put the double in our magic union so we can grab the components.
  1815. union Double_t num(f);
  1816. // Get the character that represents the sign.
  1817. // Check for NaNs or infinity.
  1818. if (num.RawExponent() == 2047)
  1819. {
  1820. TranslationError();
  1821. }
  1822. // Adjust for the exponent bias.
  1823. int exponentValue = int(num.RawExponent() - 1023);
  1824. // Add the implied one to the mantissa.
  1825. uint64_t mantissaValue = (1ll << 52) + num.RawMantissa();
  1826. // Special-case for denormals - no special exponent value and
  1827. // no implied one.
  1828. if (num.RawExponent() == 0)
  1829. {
  1830. exponentValue = -1022;
  1831. mantissaValue = num.RawMantissa();
  1832. }
  1833. uint32_t mantissaHigh = mantissaValue >> 32;
  1834. uint32_t mantissaLow = mantissaValue & 0xFFFFFFFF;
  1835. // The first bit of the mantissa has an implied value of one and this can
  1836. // be shifted 1023 positions to the left, so that's 1024 bits to the left
  1837. // of the binary point, or 32 32-bit words for the integer part.
  1838. HighPrec<32> intPart;
  1839. // When our exponentValue is zero (a number in the 1.0 to 2.0 range)
  1840. // we have a 53-bit mantissa and the implied value of the highest bit
  1841. // is 1. We need to shift 12 bits in from the bottom to get that 53rd bit
  1842. // into the ones spot in the integral portion.
  1843. // To complicate it a bit more we have to insert the mantissa as two parts.
  1844. intPart.InsertLowBits(mantissaHigh, 12 + exponentValue);
  1845. intPart.InsertLowBits(mantissaLow, 12 + exponentValue - 32);
  1846. bool bAnyDigitsLeft;
  1847. do
  1848. {
  1849. uint remainder = intPart.DivReturnRemainder( 1000000 ); // 10^6
  1850. uint origRemainer = remainder; (void)origRemainer;
  1851. bAnyDigitsLeft = !intPart.IsZero();
  1852. if ( bAnyDigitsLeft )
  1853. {
  1854. uint n = remainder % 100U; remainder /= 100U; *reinterpret_cast<uint16*>(pDst - 1) = reinterpret_cast<const uint16*>(pDigits)[n];
  1855. n = remainder % 100U; remainder /= 100U; *reinterpret_cast<uint16*>(pDst - 1 - 2) = reinterpret_cast<const uint16*>(pDigits)[n];
  1856. Assert( remainder < 100U );
  1857. *reinterpret_cast<uint16*>(pDst - 1 - 4) = reinterpret_cast<const uint16*>(pDigits)[remainder];
  1858. pDst -= 6;
  1859. }
  1860. else
  1861. {
  1862. uint n = remainder % 100U; remainder /= 100U; *reinterpret_cast<uint16*>(pDst - 1) = reinterpret_cast<const uint16*>(pDigits)[n]; --pDst; if ( ( n >= 10 ) || ( remainder ) ) --pDst;
  1863. if ( remainder )
  1864. {
  1865. n = remainder % 100U; remainder /= 100U; *reinterpret_cast<uint16*>(pDst - 1) = reinterpret_cast<const uint16*>(pDigits)[n]; --pDst; if ( ( n >= 10 ) || ( remainder ) ) --pDst;
  1866. if ( remainder )
  1867. {
  1868. Assert( remainder < 100U );
  1869. *reinterpret_cast<uint16*>(pDst - 1) = reinterpret_cast<const uint16*>(pDigits)[remainder]; --pDst; if ( remainder >= 10 ) --pDst;
  1870. }
  1871. }
  1872. }
  1873. } while ( bAnyDigitsLeft );
  1874. uint l = pLastChar - pDst;
  1875. while ( ( l - 1 ) < nMinChars )
  1876. {
  1877. *pDst-- = '0';
  1878. l++;
  1879. }
  1880. Assert( (int)l == ( pLastChar - pDst ) );
  1881. Assert( l <= nBufSize );
  1882. memmove( pBuf, pDst + 1, l );
  1883. return l - 1;
  1884. }
  1885. // FloatToString is equivalent to sprintf( "%.12f" ), but doesn't have any dependencies on the current locale setting.
  1886. // Unfortunately, high accuracy radix conversion is actually pretty tricky to do right.
  1887. // Most importantly, this function has the same max roundtrip (IEEE->ASCII->IEEE) error as the MS CRT functions and can reliably handle extremely large inputs.
  1888. static void FloatToString( char *pBuf, uint nBufSize, double fConst )
  1889. {
  1890. char *pEnd = pBuf + nBufSize;
  1891. char *pDst = pBuf;
  1892. double flVal = fConst;
  1893. if ( IsFloatNaN( flVal ) )
  1894. {
  1895. flVal = 0;
  1896. }
  1897. if ( flVal < 0.0f )
  1898. {
  1899. *pDst++ = '-';
  1900. flVal = -flVal;
  1901. }
  1902. double flInt;
  1903. double flFract = modf( flVal, &flInt );
  1904. flFract = floor( flFract * 1000000000000.0 + .5 );
  1905. if ( !flInt )
  1906. {
  1907. *pDst++ = '0';
  1908. }
  1909. else
  1910. {
  1911. uint l = PrintDoubleInt( pDst, pEnd - pDst, flInt, 0 );
  1912. pDst += l;
  1913. }
  1914. *pDst++ = '.';
  1915. if ( !flFract )
  1916. {
  1917. *pDst++ = '0';
  1918. *pDst++ = '\0';
  1919. }
  1920. else
  1921. {
  1922. uint l = PrintDoubleInt( pDst, pEnd - pDst, flFract, 12 );
  1923. pDst += l;
  1924. StripExtraTrailingZeros( pBuf ); // Turn 1.00000 into 1.0
  1925. }
  1926. }
  1927. #if 0
  1928. #include "vstdlib/random.h"
  1929. static void TestFloatConversion()
  1930. {
  1931. for ( ; ; )
  1932. {
  1933. double fConst;
  1934. switch ( rand() % 4 )
  1935. {
  1936. case 0:
  1937. fConst = RandomFloat( -1e-30, 1e+30 ); break;
  1938. case 1:
  1939. fConst = RandomFloat( -1e-10, 1e+10 ); break;
  1940. case 2:
  1941. fConst = RandomFloat( -1e-5, 1e+5 ); break;
  1942. default:
  1943. fConst = RandomFloat( -1, 1 ); break;
  1944. }
  1945. char szTemp[1024];
  1946. // FloatToString does not rely on V_snprintf(), so it can't be affected by the current locale setting.
  1947. FloatToString( szTemp, sizeof( szTemp ), fConst );
  1948. static double flMaxErr1;
  1949. static double flMaxErr2;
  1950. // Compare FloatToString()'s results vs. V_snprintf()'s, also track maximum error of each.
  1951. double flCheck = atof( szTemp );
  1952. double flErr = fabs( flCheck - fConst );
  1953. flMaxErr1 = MAX( flMaxErr1, flErr );
  1954. Assert( EqualTol( flCheck, fConst, .000000125 ) );
  1955. char szTemp2[256];
  1956. V_snprintf( szTemp2, sizeof( szTemp2 ), "%.12f", fConst );
  1957. StripExtraTrailingZeros( szTemp2 );
  1958. if ( !strchr( szTemp2, '.' ) )
  1959. {
  1960. V_strncat( szTemp2, ".0", sizeof( szTemp2 ) );
  1961. }
  1962. double flCheck2 = atof( szTemp2 );
  1963. double flErr2 = fabs( flCheck2 - fConst );
  1964. flMaxErr2 = MAX( flMaxErr2, flErr2 );
  1965. Assert( EqualTol( flCheck2, fConst, .000000125 ) );
  1966. if ( flMaxErr1 > flMaxErr2 )
  1967. {
  1968. Plat_DebugString( "!\n" );
  1969. }
  1970. }
  1971. }
  1972. #endif
  1973. void D3DToGL::Handle_DEFIB( uint32 instruction )
  1974. {
  1975. Assert( ( instruction == D3DSIO_DEFI ) || ( instruction == D3DSIO_DEFB ) );
  1976. // which register is being defined
  1977. uint32 dwToken = GetNextToken();
  1978. uint32 nRegNum = dwToken & D3DSP_REGNUM_MASK;
  1979. uint32 regType = GetRegTypeFromToken( dwToken );
  1980. if ( regType == D3DSPR_CONSTINT )
  1981. {
  1982. m_dwDefConstIntUsageMask |= ( 1 << nRegNum );
  1983. uint x = GetNextToken();
  1984. uint y = GetNextToken();
  1985. uint z = GetNextToken();
  1986. uint w = GetNextToken();
  1987. NOTE_UNUSED(y); NOTE_UNUSED(z); NOTE_UNUSED(w);
  1988. Assert( nRegNum < 32 );
  1989. if ( nRegNum < 32 )
  1990. {
  1991. m_dwDefConstIntIterCount[nRegNum] = x;
  1992. }
  1993. }
  1994. else
  1995. {
  1996. TranslationError();
  1997. }
  1998. }
  1999. void D3DToGL::Handle_DEF()
  2000. {
  2001. //TestFloatConversion();
  2002. //
  2003. // JasonM TODO: catch D3D's sincos-specific D3DSINCOSCONST1 and D3DSINCOSCONST2 constants and filter them out here
  2004. //
  2005. // Which register is being defined
  2006. uint32 dwToken = GetNextToken();
  2007. // Note that this constant was explicitly defined
  2008. m_bConstantRegisterDefined[dwToken & D3DSP_REGNUM_MASK] = true;
  2009. CUtlString sParamName = GetParameterString( dwToken, DST_REGISTER, false, NULL );
  2010. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  2011. PrintToBuf( *m_pBufParamCode, "vec4 %s = vec4( ", sParamName.String() );
  2012. // Run through the 4 floats
  2013. for ( int i=0; i < 4; i++ )
  2014. {
  2015. float fConst = uint32ToFloat( GetNextToken() );
  2016. char szTemp[1024];
  2017. FloatToString( szTemp, sizeof( szTemp ), fConst );
  2018. #if 0
  2019. static double flMaxErr1;
  2020. static double flMaxErr2;
  2021. // Compare FloatToString()'s results vs. V_snprintf()'s, also track maximum error of each.
  2022. double flCheck = atof( szTemp );
  2023. double flErr = fabs( flCheck - fConst );
  2024. flMaxErr1 = MAX( flMaxErr1, flErr );
  2025. Assert( EqualTol( flCheck, fConst, .000000125 ) );
  2026. char szTemp2[256];
  2027. V_snprintf( szTemp2, sizeof( szTemp2 ), "%.12f", fConst );
  2028. StripExtraTrailingZeros( szTemp2 );
  2029. if ( !strchr( szTemp2, '.' ) )
  2030. {
  2031. V_strncat( szTemp2, ".0", sizeof( szTemp2 ) );
  2032. }
  2033. double flCheck2 = atof( szTemp2 );
  2034. double flErr2 = fabs( flCheck2 - fConst );
  2035. flMaxErr2 = MAX( flMaxErr2, flErr2 );
  2036. Assert( EqualTol( flCheck2, fConst, .000000125 ) );
  2037. if ( flMaxErr1 > flMaxErr2 )
  2038. {
  2039. Plat_DebugString( "!\n" );
  2040. }
  2041. #endif
  2042. PrintToBuf( *m_pBufParamCode, i != 3 ? "%s, " : "%s", szTemp ); // end with comma-space
  2043. }
  2044. PrintToBuf( *m_pBufParamCode, " );\n" );
  2045. }
  2046. void D3DToGL::Handle_MAD( uint32 nInstruction )
  2047. {
  2048. uint32 nDestToken = GetNextToken();
  2049. CUtlString sParam1 = GetParameterString( nDestToken, DST_REGISTER, false, NULL );
  2050. int nARLComp0 = ARL_DEST_NONE;
  2051. CUtlString sParam2 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp0 );
  2052. int nARLComp1 = ARL_DEST_NONE;
  2053. CUtlString sParam3 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp1 );
  2054. int nARLComp2 = ARL_DEST_NONE;
  2055. CUtlString sParam4 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp2 );
  2056. // This optionally inserts a move from our dummy address register to the .x component of the real one
  2057. InsertMoveFromAddressRegister( m_pBufALUCode, nARLComp0, nARLComp1, nARLComp2 );
  2058. sParam2 = FixGLSLSwizzle( sParam1, sParam2 );
  2059. sParam3 = FixGLSLSwizzle( sParam1, sParam3 );
  2060. sParam4 = FixGLSLSwizzle( sParam1, sParam4 );
  2061. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s * %s + %s;\n", sParam1.String(), sParam2.String(), sParam3.String(), sParam4.String() );
  2062. // If the _SAT instruction modifier is used, then do a saturate here.
  2063. if ( nDestToken & D3DSPDM_SATURATE )
  2064. {
  2065. int nComponents = GetNumSwizzleComponents( sParam1.String() );
  2066. if ( nComponents == 0 )
  2067. nComponents = 4;
  2068. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", sParam1.String(), sParam1.String(), g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2069. }
  2070. }
  2071. void D3DToGL::Handle_DP2ADD()
  2072. {
  2073. char pDestReg[64], pSrc0Reg[64], pSrc1Reg[64], pSrc2Reg[64];
  2074. uint32 nDestToken = GetNextToken();
  2075. PrintParameterToString( nDestToken, DST_REGISTER, pDestReg, sizeof( pDestReg ), false, NULL );
  2076. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc0Reg, sizeof( pSrc0Reg ), false, NULL );
  2077. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc1Reg, sizeof( pSrc1Reg ), false, NULL );
  2078. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc2Reg, sizeof( pSrc2Reg ), false, NULL );
  2079. // We should only be assigning to a single component of the dest.
  2080. Assert( GetNumSwizzleComponents( pDestReg ) == 1 );
  2081. Assert( GetNumSwizzleComponents( pSrc2Reg ) == 1 );
  2082. // This is a 2D dot product, so we only want two entries from the middle components.
  2083. CUtlString sArg0 = EnsureNumSwizzleComponents( pSrc0Reg, 2 );
  2084. CUtlString sArg1 = EnsureNumSwizzleComponents( pSrc1Reg, 2 );
  2085. PrintToBufWithIndents( *m_pBufALUCode, "%s = dot( %s, %s ) + %s;\n", pDestReg, sArg0.String(), sArg1.String(), pSrc2Reg );
  2086. // If the _SAT instruction modifier is used, then do a saturate here.
  2087. if ( nDestToken & D3DSPDM_SATURATE )
  2088. {
  2089. int nComponents = GetNumSwizzleComponents( pDestReg );
  2090. if ( nComponents == 0 )
  2091. nComponents = 4;
  2092. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", pDestReg, pDestReg, g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2093. }
  2094. }
  2095. void D3DToGL::Handle_SINCOS()
  2096. {
  2097. char pDestReg[64], pSrc0Reg[64];
  2098. PrintParameterToString( GetNextToken(), DST_REGISTER, pDestReg, sizeof( pDestReg ), false, NULL );
  2099. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc0Reg, sizeof( pSrc0Reg ), true, NULL );
  2100. m_bNeedsSinCosDeclarations = true;
  2101. CUtlString sDest( pDestReg );
  2102. CUtlString sArg0 = EnsureNumSwizzleComponents( pSrc0Reg, 1 );// Ensure input is scalar
  2103. CUtlString sResult( "vSinCosTmp.xy" ); // Always going to populate this
  2104. sResult = FixGLSLSwizzle( sDest, sResult ); // Make sure we match the desired output reg
  2105. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.z = %s * %s;\n", sArg0.String(), sArg0.String() );
  2106. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.xy = vSinCosTmp.zz * scA.xy + scA.wz;\n" );
  2107. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.xy = vSinCosTmp.xy * vSinCosTmp.zz + scB.xy;\n" );
  2108. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.xy = vSinCosTmp.xy * vSinCosTmp.zz + scB.wz;\n" );
  2109. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.x = vSinCosTmp.x * %s;\n", sArg0.String() );
  2110. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.xy = vSinCosTmp.xy * vSinCosTmp.xx;\n" );
  2111. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.xy = vSinCosTmp.xy + vSinCosTmp.xy;\n" );
  2112. PrintToBufWithIndents( *m_pBufALUCode, "vSinCosTmp.x = -vSinCosTmp.x + scB.z;\n" );
  2113. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s;\n", sDest.String(), sResult.String() );
  2114. if ( m_dwMajorVersion < 3 )
  2115. {
  2116. // Eat two more tokens since D3D defines Taylor series constants that we won't need
  2117. // Only valid for pixel and vertex shader version earlier than 3_0
  2118. // (http://msdn.microsoft.com/en-us/library/windows/hardware/ff569710(v=vs.85).aspx)
  2119. SkipTokens( 2 );
  2120. }
  2121. }
  2122. void D3DToGL::Handle_LRP( uint32 nInstruction )
  2123. {
  2124. uint32 nDestToken = GetNextToken();
  2125. CUtlString sDest = GetParameterString( nDestToken, DST_REGISTER, false, NULL );
  2126. int nARLComp0 = ARL_DEST_NONE;
  2127. CUtlString sParam0 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp0 );
  2128. int nARLComp1 = ARL_DEST_NONE;
  2129. CUtlString sParam1 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp1 );
  2130. int nARLComp2 = ARL_DEST_NONE;
  2131. CUtlString sParam2 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp2 );
  2132. // This optionally inserts a move from our dummy address register to the .x component of the real one
  2133. InsertMoveFromAddressRegister( m_pBufALUCode, nARLComp0, nARLComp1, nARLComp2 );
  2134. sParam0 = FixGLSLSwizzle( sDest, sParam0 );
  2135. sParam1 = FixGLSLSwizzle( sDest, sParam1 );
  2136. sParam2 = FixGLSLSwizzle( sDest, sParam2 );
  2137. // dest = src0 * (src1 - src2) + src2;
  2138. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s * ( %s - %s ) + %s;\n", sDest.String(), sParam0.String(), sParam1.String(), sParam2.String(), sParam2.String() );
  2139. // If the _SAT instruction modifier is used, then do a saturate here.
  2140. if ( nDestToken & D3DSPDM_SATURATE )
  2141. {
  2142. int nComponents = GetNumSwizzleComponents( sDest.String() );
  2143. if ( nComponents == 0 )
  2144. nComponents = 4;
  2145. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", sDest.String(), sDest.String(), g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2146. }
  2147. }
  2148. void D3DToGL::Handle_TEX( uint32 dwToken, bool bIsTexLDL )
  2149. {
  2150. char pDestReg[64], pSrc0Reg[64], pSrc1Reg[64];
  2151. PrintParameterToString( GetNextToken(), DST_REGISTER, pDestReg, sizeof( pDestReg ), false, NULL );
  2152. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc0Reg, sizeof( pSrc0Reg ), false, NULL );
  2153. DWORD dwSrc1Token = GetNextToken();
  2154. PrintParameterToString( dwSrc1Token, SRC_REGISTER, pSrc1Reg, sizeof( pSrc1Reg ), false, NULL );
  2155. Assert( (dwSrc1Token & D3DSP_REGNUM_MASK) < ARRAYSIZE( m_dwSamplerTypes ) );
  2156. uint32 nSamplerType = m_dwSamplerTypes[dwSrc1Token & D3DSP_REGNUM_MASK];
  2157. if ( nSamplerType == SAMPLER_TYPE_2D )
  2158. {
  2159. const bool bIsShadowSampler = ( ( 1 << ( (int) ( dwSrc1Token & D3DSP_REGNUM_MASK ) ) ) & m_nShadowDepthSamplerMask ) != 0;
  2160. if ( bIsTexLDL )
  2161. {
  2162. CUtlString sCoordVar = EnsureNumSwizzleComponents( pSrc0Reg, bIsShadowSampler ? 3 : 2 );
  2163. // Strip out the W component of the pSrc0Reg and pass that as the LOD to texture2DLod.
  2164. char szLOD[128], szExtra[8];
  2165. GetParamNameWithoutSwizzle( pSrc0Reg, szLOD, sizeof( szLOD ) );
  2166. V_snprintf( szExtra, sizeof( szExtra ), ".%c", GetSwizzleComponent( pSrc0Reg, 3 ) );
  2167. V_strncat( szLOD, szExtra, sizeof( szLOD ) );
  2168. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s( %s, %s, %s );\n", pDestReg, bIsShadowSampler ? "shadow2DLod" : "texture2DLod", pSrc1Reg, sCoordVar.String(), szLOD );
  2169. }
  2170. else if ( bIsShadowSampler )
  2171. {
  2172. // .z is meant to contain the object depth, while .xy contains the 2D tex coords
  2173. CUtlString sCoordVar3D = EnsureNumSwizzleComponents( pSrc0Reg, 3 );
  2174. PrintToBufWithIndents( *m_pBufALUCode, "%s = shadow2D( %s, %s );\n", pDestReg, pSrc1Reg, sCoordVar3D.String() );
  2175. Assert( m_dwSamplerTypes[dwSrc1Token & D3DSP_REGNUM_MASK] == SAMPLER_TYPE_2D );
  2176. }
  2177. else if( ( OpcodeSpecificData( dwToken ) << D3DSP_OPCODESPECIFICCONTROL_SHIFT ) == D3DSI_TEXLD_PROJECT )
  2178. {
  2179. // This projective case is after the shadow case intentionally, due to the way that "projective"
  2180. // loads are overloaded in our D3D shaders for shadow lookups.
  2181. //
  2182. // We use the vec4 variant of texture2DProj() intentionally here, since it lines up well with Direct3D.
  2183. CUtlString s4DProjCoords = EnsureNumSwizzleComponents( pSrc0Reg, 4 ); // Ensure vec4 variant
  2184. PrintToBufWithIndents( *m_pBufALUCode, "%s = texture2DProj( %s, %s );\n", pDestReg, pSrc1Reg, s4DProjCoords.String() );
  2185. }
  2186. else
  2187. {
  2188. CUtlString sCoordVar = EnsureNumSwizzleComponents( pSrc0Reg, bIsShadowSampler ? 3 : 2 );
  2189. PrintToBufWithIndents( *m_pBufALUCode, "%s = texture2D( %s, %s );\n", pDestReg, pSrc1Reg, sCoordVar.String() );
  2190. }
  2191. }
  2192. else if ( nSamplerType == SAMPLER_TYPE_3D )
  2193. {
  2194. if ( bIsTexLDL )
  2195. {
  2196. TranslationError();
  2197. }
  2198. CUtlString sCoordVar = EnsureNumSwizzleComponents( pSrc0Reg, 3 );
  2199. PrintToBufWithIndents( *m_pBufALUCode, "%s = texture3D( %s, %s );\n", pDestReg, pSrc1Reg, sCoordVar.String() );
  2200. }
  2201. else if ( nSamplerType == SAMPLER_TYPE_CUBE )
  2202. {
  2203. if ( bIsTexLDL )
  2204. {
  2205. TranslationError();
  2206. }
  2207. CUtlString sCoordVar = EnsureNumSwizzleComponents( pSrc0Reg, 3 );
  2208. PrintToBufWithIndents( *m_pBufALUCode, "%s = textureCube( %s, %s );\n", pDestReg, pSrc1Reg, sCoordVar.String() );
  2209. }
  2210. else
  2211. {
  2212. Error( "TEX instruction: unsupported sampler type used" );
  2213. }
  2214. }
  2215. void D3DToGL::StrcatToHeaderCode( const char *pBuf )
  2216. {
  2217. strcat_s( (char*)m_pBufHeaderCode->Base(), m_pBufHeaderCode->Size(), pBuf );
  2218. }
  2219. void D3DToGL::StrcatToALUCode( const char *pBuf )
  2220. {
  2221. PrintIndentation( (char*)m_pBufALUCode->Base(), m_pBufALUCode->Size() );
  2222. strcat_s( (char*)m_pBufALUCode->Base(), m_pBufALUCode->Size(), pBuf );
  2223. }
  2224. void D3DToGL::StrcatToParamCode( const char *pBuf )
  2225. {
  2226. strcat_s( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size(), pBuf );
  2227. }
  2228. void D3DToGL::StrcatToAttribCode( const char *pBuf )
  2229. {
  2230. strcat_s( (char*)m_pBufAttribCode->Base(), m_pBufAttribCode->Size(), pBuf );
  2231. }
  2232. void D3DToGL::Handle_TexLDD( uint32 nInstruction )
  2233. {
  2234. TranslationError(); // Not supported yet, but can be if we need it.
  2235. }
  2236. void D3DToGL::Handle_TexCoord()
  2237. {
  2238. TranslationError();
  2239. // If ps_1_4, this is texcrd
  2240. if ( (m_dwMajorVersion == 1) && (m_dwMinorVersion == 4) && (!m_bVertexShader) )
  2241. {
  2242. StrcatToALUCode( "texcrd" );
  2243. }
  2244. else // else it's texcoord
  2245. {
  2246. TranslationError();
  2247. StrcatToALUCode( "texcoord" );
  2248. }
  2249. char buff[256];
  2250. PrintParameterToString( GetNextToken(), DST_REGISTER, buff, sizeof( buff ), false, NULL );
  2251. StrcatToALUCode( buff );
  2252. // If ps_1_4, texcrd also has a source parameter
  2253. if ((m_dwMajorVersion == 1) && (m_dwMinorVersion == 4) && (!m_bVertexShader))
  2254. {
  2255. StrcatToALUCode( ", " );
  2256. PrintParameterToString( GetNextToken(), SRC_REGISTER, buff, sizeof( buff ), false, NULL );
  2257. StrcatToALUCode( buff );
  2258. }
  2259. StrcatToALUCode( ";\n" );
  2260. }
  2261. void D3DToGL::Handle_BREAKC( uint32 dwToken )
  2262. {
  2263. uint nComparison = ( dwToken & D3DSHADER_COMPARISON_MASK ) >> D3DSHADER_COMPARISON_SHIFT;
  2264. const char *pComparison = "?";
  2265. switch ( nComparison )
  2266. {
  2267. case D3DSPC_GT: pComparison = ">"; break;
  2268. case D3DSPC_EQ: pComparison = "=="; break;
  2269. case D3DSPC_GE: pComparison = ">="; break;
  2270. case D3DSPC_LT: pComparison = "<"; break;
  2271. case D3DSPC_NE: pComparison = "!="; break;
  2272. case D3DSPC_LE: pComparison = "<="; break;
  2273. default:
  2274. TranslationError();
  2275. }
  2276. char src0[256];
  2277. uint32 src0Token = GetNextToken();
  2278. PrintParameterToString( src0Token, SRC_REGISTER, src0, sizeof( src0 ), false, NULL );
  2279. char src1[256];
  2280. uint32 src1Token = GetNextToken();
  2281. PrintParameterToString( src1Token, SRC_REGISTER, src1, sizeof( src1 ), false, NULL );
  2282. PrintToBufWithIndents( *m_pBufALUCode, "if (%s %s %s) break;\n", src0, pComparison, src1 );
  2283. }
  2284. void D3DToGL::HandleBinaryOp_GLSL( uint32 nInstruction )
  2285. {
  2286. uint32 nDestToken = GetNextToken();
  2287. CUtlString sParam1 = GetParameterString( nDestToken, DST_REGISTER, false, NULL );
  2288. int nARLComp0 = ARL_DEST_NONE;
  2289. CUtlString sParam2 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp0 );
  2290. int nARLComp1 = ARL_DEST_NONE;
  2291. CUtlString sParam3 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp1 );
  2292. // This optionally inserts a move from our dummy address register to the .x component of the real one
  2293. InsertMoveFromAddressRegister( m_pBufALUCode, nARLComp0, nARLComp1 );
  2294. // Since DP3 and DP4 have a scalar as the dest and vectors as the src, don't screw with the swizzle specifications.
  2295. if ( nInstruction == D3DSIO_DP3 )
  2296. {
  2297. sParam2 = EnsureNumSwizzleComponents( sParam2, 3 );
  2298. sParam3 = EnsureNumSwizzleComponents( sParam3, 3 );
  2299. }
  2300. else if ( nInstruction == D3DSIO_DP4 )
  2301. {
  2302. sParam2 = EnsureNumSwizzleComponents( sParam2, 4 );
  2303. sParam3 = EnsureNumSwizzleComponents( sParam3, 4 );
  2304. }
  2305. else if ( nInstruction == D3DSIO_DST )
  2306. {
  2307. m_bUsesDSTInstruction = true;
  2308. sParam2 = EnsureNumSwizzleComponents( sParam2, 4 );
  2309. sParam3 = EnsureNumSwizzleComponents( sParam3, 4 );
  2310. }
  2311. else
  2312. {
  2313. sParam2 = FixGLSLSwizzle( sParam1, sParam2 );
  2314. sParam3 = FixGLSLSwizzle( sParam1, sParam3 );
  2315. }
  2316. char buff[256];
  2317. if ( nInstruction == D3DSIO_ADD || nInstruction == D3DSIO_SUB || nInstruction == D3DSIO_MUL )
  2318. {
  2319. // These all look like x = y op z
  2320. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s %s %s;\n", sParam1.String(), sParam2.String(), GetGLSLOperatorString( nInstruction ), sParam3.String() );
  2321. }
  2322. else
  2323. {
  2324. int nDestComponents = GetNumSwizzleComponents( sParam1.String() );
  2325. int nSrcComponents = GetNumSwizzleComponents( sParam2.String() );
  2326. // All remaining instructions can use GLSL intrinsics like dot() and cross().
  2327. bool bDoubleClose = OpenIntrinsic( nInstruction, buff, sizeof( buff ), nDestComponents, nSrcComponents );
  2328. if ( ( nSrcComponents == 1 ) && ( nInstruction == D3DSIO_SGE ) )
  2329. {
  2330. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s%s >= %s );\n", sParam1.String(), buff, sParam2.String(), sParam3.String() );
  2331. }
  2332. else if ( ( nSrcComponents == 1 ) && ( nInstruction == D3DSIO_SLT ) )
  2333. {
  2334. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s%s < %s );\n", sParam1.String(), buff, sParam2.String(), sParam3.String() );
  2335. }
  2336. else
  2337. {
  2338. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s%s, %s %s;\n", sParam1.String(), buff, sParam2.String(), sParam3.String(), bDoubleClose ? ") )" : ")" );
  2339. }
  2340. }
  2341. // If the _SAT instruction modifier is used, then do a saturate here.
  2342. if ( nDestToken & D3DSPDM_SATURATE )
  2343. {
  2344. int nComponents = GetNumSwizzleComponents( sParam1.String() );
  2345. if ( nComponents == 0 )
  2346. nComponents = 4;
  2347. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", sParam1.String(), sParam1.String(), g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2348. }
  2349. }
  2350. void D3DToGL::HandleBinaryOp_ASM( uint32 nInstruction )
  2351. {
  2352. CUtlString sParam1 = GetParameterString( GetNextToken(), DST_REGISTER, false, NULL );
  2353. int nARLComp0 = ARL_DEST_NONE;
  2354. CUtlString sParam2 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp0 );
  2355. int nARLComp1 = ARL_DEST_NONE;
  2356. CUtlString sParam3 = GetParameterString( GetNextToken(), SRC_REGISTER, false, &nARLComp1 );
  2357. // This optionally inserts a move from our dummy address register to the .x component of the real one
  2358. InsertMoveFromAddressRegister( m_pBufALUCode, nARLComp0, nARLComp1 );
  2359. char buff[256];
  2360. PrintOpcode( nInstruction, buff, sizeof( buff ) );
  2361. PrintToBufWithIndents( *m_pBufALUCode, "%s%s, %s, %s;\n", buff, sParam1.String(), sParam2.String(), sParam3.String() );
  2362. }
  2363. void D3DToGL::WriteGLSLCmp( const char *pDestReg, const char *pSrc0Reg, const char *pSrc1Reg, const char *pSrc2Reg )
  2364. {
  2365. int nWriteMaskEntries = GetNumWriteMaskEntries( pDestReg );
  2366. for ( int i=0; i < nWriteMaskEntries; i++ )
  2367. {
  2368. char params[4][256];
  2369. WriteParamWithSingleMaskEntry( pDestReg, i, params[0], sizeof( params[0] ) );
  2370. WriteParamWithSingleMaskEntry( pSrc0Reg, i, params[1], sizeof( params[1] ) );
  2371. WriteParamWithSingleMaskEntry( pSrc1Reg, i, params[2], sizeof( params[2] ) );
  2372. WriteParamWithSingleMaskEntry( pSrc2Reg, i, params[3], sizeof( params[3] ) );
  2373. PrintToBufWithIndents( *m_pBufALUCode, "%s = ( %s >= 0.0 ) ? %s : %s;\n", params[0], params[1], params[2], params[3] );
  2374. }
  2375. }
  2376. void D3DToGL::Handle_CMP()
  2377. {
  2378. // In Direct3D, result = (src0 >= 0.0) ? src1 : src2
  2379. // In OpenGL, result = (src0 < 0.0) ? src1 : src2
  2380. //
  2381. // As a result, arguments are effectively in a different order than Direct3D! !#$&*!%#$&
  2382. char pDestReg[64], pSrc0Reg[64], pSrc1Reg[64], pSrc2Reg[64];
  2383. uint32 nDestToken = GetNextToken();
  2384. PrintParameterToString( nDestToken, DST_REGISTER, pDestReg, sizeof( pDestReg ), false, NULL );
  2385. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc0Reg, sizeof( pSrc0Reg ), false, NULL );
  2386. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc1Reg, sizeof( pSrc1Reg ), false, NULL );
  2387. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc2Reg, sizeof( pSrc2Reg ), false, NULL );
  2388. // These are a tricky case.. we have to expand it out into multiple statements.
  2389. char szDestBase[256];
  2390. GetParamNameWithoutSwizzle( pDestReg, szDestBase, sizeof( szDestBase ) );
  2391. V_strncpy( pSrc0Reg, FixGLSLSwizzle( pDestReg, pSrc0Reg ), sizeof( pSrc0Reg ) );
  2392. V_strncpy( pSrc1Reg, FixGLSLSwizzle( pDestReg, pSrc1Reg ), sizeof( pSrc1Reg ) );
  2393. V_strncpy( pSrc2Reg, FixGLSLSwizzle( pDestReg, pSrc2Reg ), sizeof( pSrc2Reg ) );
  2394. // This isn't reliable!
  2395. //if ( DoParamNamesMatch( pDestReg, pSrc0Reg ) && GetNumSwizzleComponents( pDestReg ) > 1 )
  2396. if ( 1 )
  2397. {
  2398. // So the dest register is the same as the comparand. We're in danger of screwing up our results.
  2399. //
  2400. // For example, this code:
  2401. // CMP r0.xy, r0.xx, r1, r2
  2402. // would generate this:
  2403. // r0.x = (r0.x >= 0) ? r1.x : r2.x;
  2404. // r0.y = (r0.x >= 0) ? r1.x : r2.x;
  2405. //
  2406. // But the first lines changes r0.x and thus screws the atomicity of the CMP instruction for the second line.
  2407. // So we assign r0 to a temporary first and then write to the temporary.
  2408. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s;\n", g_pAtomicTempVarName, szDestBase );
  2409. char szTempVar[256];
  2410. ReplaceParamName( pDestReg, g_pAtomicTempVarName, szTempVar, sizeof( szTempVar ) );
  2411. WriteGLSLCmp( szTempVar, pSrc0Reg, pSrc1Reg, pSrc2Reg );
  2412. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s;\n", szDestBase, g_pAtomicTempVarName );
  2413. m_bUsedAtomicTempVar = true;
  2414. }
  2415. else
  2416. {
  2417. // Just write out the simple expanded version of the CMP. No need to use atomic_temp_var.
  2418. WriteGLSLCmp( pDestReg, pSrc0Reg, pSrc1Reg, pSrc2Reg );
  2419. }
  2420. // If the _SAT instruction modifier is used, then do a saturate here.
  2421. if ( nDestToken & D3DSPDM_SATURATE )
  2422. {
  2423. int nComponents = GetNumSwizzleComponents( pDestReg );
  2424. if ( nComponents == 0 )
  2425. nComponents = 4;
  2426. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", pDestReg, pDestReg, g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2427. }
  2428. }
  2429. void D3DToGL::Handle_NRM()
  2430. {
  2431. char pDestReg[64];
  2432. char pSrc0Reg[64];
  2433. PrintParameterToString( GetNextToken(), DST_REGISTER, pDestReg, sizeof( pDestReg ), false, NULL );
  2434. int nARLSrcComp = ARL_DEST_NONE;
  2435. PrintParameterToString( GetNextToken(), SRC_REGISTER, pSrc0Reg, sizeof( pSrc0Reg ), false, &nARLSrcComp );
  2436. if ( nARLSrcComp != -1 )
  2437. {
  2438. InsertMoveFromAddressRegister( m_pBufALUCode, nARLSrcComp, -1, -1 );
  2439. }
  2440. CUtlString sSrc = EnsureNumSwizzleComponents( pSrc0Reg, 3 );
  2441. PrintToBufWithIndents( *m_pBufALUCode, "%s = normalize( %s );\n", pDestReg, sSrc.String() );
  2442. }
  2443. void D3DToGL::Handle_UnaryOp( uint32 nInstruction )
  2444. {
  2445. uint32 nDestToken = GetNextToken();
  2446. CUtlString sParam1 = GetParameterString( nDestToken, DST_REGISTER, false, NULL );
  2447. CUtlString sParam2 = GetParameterString( GetNextToken(), SRC_REGISTER, false, NULL );
  2448. sParam2 = FixGLSLSwizzle( sParam1, sParam2 );
  2449. if ( nInstruction == D3DSIO_MOV )
  2450. {
  2451. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s;\n", sParam1.String(), sParam2.String() );
  2452. }
  2453. else if ( nInstruction == D3DSIO_RSQ )
  2454. {
  2455. PrintToBufWithIndents( *m_pBufALUCode, "%s = inversesqrt( %s );\n", sParam1.String(), sParam2.String() );
  2456. }
  2457. else if ( nInstruction == D3DSIO_RCP )
  2458. {
  2459. PrintToBufWithIndents( *m_pBufALUCode, "%s = 1.0 / %s;\n", sParam1.String(), sParam2.String() );
  2460. }
  2461. else if ( nInstruction == D3DSIO_EXP )
  2462. {
  2463. PrintToBufWithIndents( *m_pBufALUCode, "%s = exp2( %s );\n", sParam1.String(), sParam2.String() );
  2464. }
  2465. else if ( nInstruction == D3DSIO_FRC )
  2466. {
  2467. PrintToBufWithIndents( *m_pBufALUCode, "%s = fract( %s );\n", sParam1.String(), sParam2.String() );
  2468. }
  2469. else if ( nInstruction == D3DSIO_LOG ) // d3d 'log' is log base 2
  2470. {
  2471. PrintToBufWithIndents( *m_pBufALUCode, "%s = log2( %s );\n", sParam1.String(), sParam2.String() );
  2472. }
  2473. else if ( nInstruction == D3DSIO_ABS ) // rbarris did this one, Jason please check
  2474. {
  2475. PrintToBufWithIndents( *m_pBufALUCode, "%s = abs( %s );\n", sParam1.String(), sParam2.String() );
  2476. }
  2477. else if ( nInstruction == D3DSIO_MOVA )
  2478. {
  2479. m_bDeclareAddressReg = true;
  2480. PrintToBufWithIndents( *m_pBufALUCode, "%s = %s;\n", sParam1.String(), sParam2.String() );
  2481. if ( !m_bGenerateBoneUniformBuffer )
  2482. {
  2483. m_nHighestRegister = DXABSTRACT_VS_PARAM_SLOTS - 1;
  2484. }
  2485. }
  2486. else if ( nInstruction == D3DSIO_DSX )
  2487. {
  2488. PrintToBufWithIndents( *m_pBufALUCode, "%s = dFdx( %s );\n", sParam1.String(), sParam2.String() );
  2489. }
  2490. else if ( nInstruction == D3DSIO_DSY )
  2491. {
  2492. PrintToBufWithIndents( *m_pBufALUCode, "%s = dFdy( %s );\n", sParam1.String(), sParam2.String() );
  2493. }
  2494. else
  2495. {
  2496. Error( "Unsupported instruction" );
  2497. }
  2498. // If the _SAT instruction modifier is used, then do a saturate here.
  2499. if ( nDestToken & D3DSPDM_SATURATE )
  2500. {
  2501. int nComponents = GetNumSwizzleComponents( sParam1.String() );
  2502. if ( nComponents == 0 )
  2503. {
  2504. nComponents = 4;
  2505. }
  2506. PrintToBufWithIndents( *m_pBufALUCode, "%s = clamp( %s, %s, %s );\n", sParam1.String(), sParam1.String(), g_szVecZeros[nComponents], g_szVecOnes[nComponents] );
  2507. }
  2508. }
  2509. void D3DToGL::WriteGLSLSamplerDefinitions()
  2510. {
  2511. int nSamplersWritten = 0;
  2512. for ( int i=0; i < ARRAYSIZE( m_dwSamplerTypes ); i++ )
  2513. {
  2514. if ( m_dwSamplerTypes[i] == SAMPLER_TYPE_2D )
  2515. {
  2516. if ( ( ( 1 << i ) & m_nShadowDepthSamplerMask ) != 0 )
  2517. {
  2518. PrintToBuf( *m_pBufHeaderCode, "uniform sampler2DShadow sampler%d;\n", i );
  2519. }
  2520. else
  2521. {
  2522. PrintToBuf( *m_pBufHeaderCode, "uniform sampler2D sampler%d;\n", i );
  2523. }
  2524. ++nSamplersWritten;
  2525. }
  2526. else if ( m_dwSamplerTypes[i] == SAMPLER_TYPE_3D )
  2527. {
  2528. PrintToBuf( *m_pBufHeaderCode, "uniform sampler3D sampler%d;\n", i );
  2529. ++nSamplersWritten;
  2530. }
  2531. else if ( m_dwSamplerTypes[i] == SAMPLER_TYPE_CUBE )
  2532. {
  2533. PrintToBuf( *m_pBufHeaderCode, "uniform samplerCube sampler%d;\n", i );
  2534. ++nSamplersWritten;
  2535. }
  2536. else if ( m_dwSamplerTypes[i] != SAMPLER_TYPE_UNUSED )
  2537. {
  2538. Error( "Unknown sampler type." );
  2539. }
  2540. }
  2541. if ( nSamplersWritten > 0 )
  2542. PrintToBuf( *m_pBufHeaderCode, "\n\n" );
  2543. }
  2544. void D3DToGL::WriteGLSLOutputVariableAssignments()
  2545. {
  2546. if ( m_bVertexShader )
  2547. {
  2548. // Map output "oN" registers back to GLSL output variables.
  2549. if ( m_bAddHexCodeComments )
  2550. {
  2551. PrintToBuf( *m_pBufAttribCode, "\n// Now we're storing the oN variables from the output dcl_ statements back into their GLSL equivalents.\n" );
  2552. }
  2553. for ( int i=0; i < ARRAYSIZE( m_DeclaredOutputs ); i++ )
  2554. {
  2555. if ( m_DeclaredOutputs[i] == UNDECLARED_OUTPUT )
  2556. continue;
  2557. if ( ( m_dwTexCoordOutMask & ( 1 << i ) ) == 0 )
  2558. continue;
  2559. uint32 dwToken = m_DeclaredOutputs[i];
  2560. uint32 dwUsage = ( dwToken & D3DSP_DCL_USAGE_MASK );
  2561. uint32 dwUsageIndex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  2562. if ( ( dwUsage == D3DDECLUSAGE_FOG ) || ( dwUsage == D3DDECLUSAGE_PSIZE ) )
  2563. {
  2564. TranslationError(); // Not supported yet, but can be if we need it.
  2565. }
  2566. if ( dwUsage == D3DDECLUSAGE_COLOR )
  2567. {
  2568. PrintToBufWithIndents( *m_pBufALUCode, "%s = oTempT%d;\n", dwUsageIndex ? "gl_FrontSecondaryColor" : "gl_FrontColor", i );
  2569. }
  2570. else if ( dwUsage == D3DDECLUSAGE_TEXCOORD )
  2571. {
  2572. char buf[256];
  2573. if ( m_nCentroidMask & ( 0x00000001 << dwUsageIndex ) )
  2574. {
  2575. V_snprintf( buf, sizeof( buf ), "centroid varying vec4 oT%d;\n", dwUsageIndex ); // centroid varying
  2576. }
  2577. else
  2578. {
  2579. V_snprintf( buf, sizeof( buf ), "varying vec4 oT%d;\n", dwUsageIndex );
  2580. }
  2581. StrcatToHeaderCode( buf );
  2582. PrintToBufWithIndents( *m_pBufALUCode, "oT%d = oTempT%d;\n", dwUsageIndex, i );
  2583. }
  2584. }
  2585. }
  2586. }
  2587. void D3DToGL::WriteGLSLInputVariableAssignments()
  2588. {
  2589. if ( m_bVertexShader )
  2590. return;
  2591. for ( int i=0; i < ARRAYSIZE( m_DeclaredInputs ); i++ )
  2592. {
  2593. if ( m_DeclaredInputs[i] == UNDECLARED_INPUT )
  2594. continue;
  2595. uint32 dwToken = m_DeclaredInputs[i];
  2596. uint32 dwUsage = ( dwToken & D3DSP_DCL_USAGE_MASK );
  2597. uint32 dwUsageIndex = ( dwToken & D3DSP_DCL_USAGEINDEX_MASK ) >> D3DSP_DCL_USAGEINDEX_SHIFT;
  2598. if ( dwUsage == D3DDECLUSAGE_COLOR )
  2599. {
  2600. PrintToBufWithIndents( *m_pBufAttribCode, "vec4 oTempT%d = %s;\n", i, dwUsageIndex ? "gl_SecondaryColor" : "gl_Color" );
  2601. }
  2602. else if ( dwUsage == D3DDECLUSAGE_TEXCOORD )
  2603. {
  2604. PrintToBufWithIndents( *m_pBufAttribCode, "vec4 oTempT%d = oT%d;\n", i, dwUsageIndex );
  2605. }
  2606. }
  2607. }
  2608. void D3DToGL::Handle_DeclarativeNonDclOp( uint32 nInstruction )
  2609. {
  2610. char buff[128];
  2611. uint32 dwToken = GetNextToken();
  2612. PrintParameterToString( dwToken, DST_REGISTER, buff, sizeof( buff ), false, NULL );
  2613. if ( nInstruction == D3DSIO_TEXKILL )
  2614. {
  2615. // TEXKILL is supposed to discard the pixel if any of the src register's X, Y, or Z components are less than zero.
  2616. // We have to translate it to something like:
  2617. // if ( r0.x < 0.0 || r0.y < 0.0 )
  2618. // discard;
  2619. char c[3];
  2620. c[0] = GetSwizzleComponent( buff, 0 );
  2621. c[1] = GetSwizzleComponent( buff, 1 );
  2622. c[2] = GetSwizzleComponent( buff, 2 );
  2623. // Get the unique components.
  2624. char cUnique[3];
  2625. cUnique[0] = c[0];
  2626. int nUnique = 1;
  2627. if ( c[1] != c[0] )
  2628. cUnique[nUnique++] = c[1];
  2629. if ( c[2] != c[1] && c[2] != c[0] )
  2630. cUnique[nUnique++] = c[2];
  2631. // Get the src register base name.
  2632. char szBase[256];
  2633. GetParamNameWithoutSwizzle( buff, szBase, sizeof( szBase ) );
  2634. PrintToBufWithIndents( *m_pBufALUCode, "if ( %s.%c < 0.0 ", szBase, cUnique[0] );
  2635. for ( int i=1; i < nUnique; i++ )
  2636. {
  2637. PrintToBuf( *m_pBufALUCode, "|| %s.%c < 0.0 ", szBase, cUnique[i] );
  2638. }
  2639. PrintToBuf( *m_pBufALUCode, ")\n{\n\tdiscard;\n}\n" );
  2640. }
  2641. else
  2642. {
  2643. char szOpcode[128];
  2644. PrintOpcode( nInstruction, szOpcode, sizeof( szOpcode ) );
  2645. StrcatToALUCode( szOpcode );
  2646. StrcatToALUCode( buff );
  2647. StrcatToALUCode( ";\n" );
  2648. }
  2649. }
  2650. void D3DToGL::NoteTangentInputUsed()
  2651. {
  2652. if ( !m_bTangentInputUsed )
  2653. {
  2654. m_bTangentInputUsed = true;
  2655. // PrintToBuf( *m_pBufParamCode, "attribute vec4 %s;\n", g_pTangentAttributeName );
  2656. }
  2657. }
  2658. // These are the only ARL instructions that should appear in the instruction stream
  2659. void D3DToGL::InsertMoveInstruction( CUtlBuffer *pCode, int nARLComponent )
  2660. {
  2661. PrintIndentation( ( char * )pCode->Base(), pCode->Size() );
  2662. switch ( nARLComponent )
  2663. {
  2664. case ARL_DEST_X:
  2665. strcat_s( ( char * )pCode->Base(), pCode->Size(), "a0 = int( va_r.x );\n" );
  2666. break;
  2667. case ARL_DEST_Y:
  2668. strcat_s( ( char * )pCode->Base(), pCode->Size(), "a0 = int( va_r.y );\n" );
  2669. break;
  2670. case ARL_DEST_Z:
  2671. strcat_s( ( char * )pCode->Base(), pCode->Size(), "a0 = int( va_r.z );\n" );
  2672. break;
  2673. case ARL_DEST_W:
  2674. strcat_s( ( char * )pCode->Base(), pCode->Size(), "a0 = int( va_r.w );\n" );
  2675. break;
  2676. }
  2677. }
  2678. // This optionally inserts a move from our dummy address register to the .x component of the real one
  2679. void D3DToGL::InsertMoveFromAddressRegister( CUtlBuffer *pCode, int nARLComp0, int nARLComp1, int nARLComp2 /* = ARL_DEST_NONE */ )
  2680. {
  2681. // We no longer need to do this in GLSL - we put the cast to int from the dummy address register va_r.x, va_r.y, etc. directly into the instruction
  2682. return;
  2683. }
  2684. //------------------------------------------------------------------------------
  2685. // TranslateShader()
  2686. //
  2687. // This is the main function that the outside world sees. A pointer to the
  2688. // uint32 stream returned from the D3DX compile routine is parsed and used
  2689. // to write human-readable asm code into the character array pointed to by
  2690. // pDisassembledCode. An error code is returned.
  2691. //------------------------------------------------------------------------------
  2692. int D3DToGL::TranslateShader( uint32* code, CUtlBuffer *pBufDisassembledCode, bool *bVertexShader, uint32 options, int32 nShadowDepthSamplerMask, uint32 nCentroidMask, char *debugLabel )
  2693. {
  2694. CUtlString sLine, sParamName;
  2695. uint32 i, dwToken, nInstruction, nNumTokensToSkip;
  2696. char buff[256];
  2697. // obey options
  2698. m_bUseEnvParams = (options & D3DToGL_OptionUseEnvParams) != 0;
  2699. m_bDoFixupZ = (options & D3DToGL_OptionDoFixupZ) != 0;
  2700. m_bDoFixupY = (options & D3DToGL_OptionDoFixupY) != 0;
  2701. m_bDoUserClipPlanes = (options & D3DToGL_OptionDoUserClipPlanes) != 0;
  2702. m_bAddHexCodeComments = (options & D3DToGL_AddHexComments) != 0;
  2703. m_bPutHexCodesAfterLines = (options & D3DToGL_PutHexCommentsAfterLines) != 0;
  2704. m_bGeneratingDebugText = (options & D3DToGL_GeneratingDebugText) != 0;
  2705. m_bGenerateSRGBWriteSuffix = (options & D3DToGL_OptionSRGBWriteSuffix) != 0;
  2706. m_NumIndentTabs = 1; // start code indented one tab
  2707. m_nLoopDepth = 0;
  2708. // debugging
  2709. m_bSpew = (options & D3DToGL_OptionSpew) != 0;
  2710. // These are not accessed below in a way that will cause them to glow, so
  2711. // we could overflow these and/or the buffer pointed to by pDisassembledCode
  2712. m_pBufAttribCode = new CUtlBuffer( 100, 10000, CUtlBuffer::TEXT_BUFFER );
  2713. m_pBufParamCode = new CUtlBuffer( 100, 10000, CUtlBuffer::TEXT_BUFFER );
  2714. m_pBufALUCode = new CUtlBuffer( 100, 60000, CUtlBuffer::TEXT_BUFFER );
  2715. // Pointers to text buffers for assembling sections of the program
  2716. m_pBufHeaderCode = pBufDisassembledCode;
  2717. char *pAttribMapStart = NULL;
  2718. ((char*)m_pBufHeaderCode->Base())[0] = 0;
  2719. ((char*)m_pBufAttribCode->Base())[0] = 0;
  2720. ((char*)m_pBufParamCode->Base())[0] = 0;
  2721. ((char*)m_pBufALUCode->Base())[0] = 0;
  2722. for ( i=0; i<MAX_SHADER_CONSTANTS; i++ )
  2723. {
  2724. m_bConstantRegisterDefined[i] = false;
  2725. }
  2726. // Track shadow sampler usage for proper declaration
  2727. m_nShadowDepthSamplerMask = nShadowDepthSamplerMask;
  2728. m_bDeclareShadowOption = false;
  2729. // Various flags set while parsing code to drive various declaration instructions
  2730. m_bNeedsD2AddTemp = false;
  2731. m_bNeedsLerpTemp = false;
  2732. m_bNeedsNRMTemp = false;
  2733. m_bNeedsSinCosDeclarations = false;
  2734. m_bDeclareAddressReg = false;
  2735. m_bDeclareVSOPos = false;
  2736. m_bDeclareVSOFog = false;
  2737. m_dwTexCoordOutMask = 0x00000000;
  2738. m_nVSPositionOutput = -1;
  2739. m_bOutputColorRegister[0] = false;
  2740. m_bOutputColorRegister[1] = false;
  2741. m_bOutputColorRegister[2] = false;
  2742. m_bOutputColorRegister[3] = false;
  2743. m_bOutputDepthRegister = false;
  2744. m_bTangentInputUsed = false;
  2745. m_bUsesDSTInstruction = false;
  2746. m_dwTempUsageMask = 0x00000000;
  2747. m_dwSamplerUsageMask = 0x00000000;
  2748. m_dwConstIntUsageMask = 0x00000000;
  2749. m_dwDefConstIntUsageMask = 0x00000000;
  2750. memset( m_dwDefConstIntIterCount, 0, sizeof( m_dwDefConstIntIterCount ) );
  2751. m_dwConstBoolUsageMask = 0x00000000;
  2752. m_nCentroidMask = nCentroidMask;
  2753. m_nHighestRegister = 0;
  2754. m_nHighestBoneRegister = -1;
  2755. m_bGenerateBoneUniformBuffer = false;
  2756. m_bUseBindlessTexturing = ((options & D3DToGL_OptionUseBindlessTexturing) != 0);
  2757. m_bUsedAtomicTempVar = false;
  2758. for ( int i=0; i < ARRAYSIZE( m_dwSamplerTypes ); i++ )
  2759. {
  2760. m_dwSamplerTypes[i] = SAMPLER_TYPE_UNUSED;
  2761. }
  2762. for ( int i=0; i < ARRAYSIZE( m_DeclaredOutputs ); i++ )
  2763. {
  2764. m_DeclaredOutputs[i] = UNDECLARED_OUTPUT;
  2765. }
  2766. for ( int i=0; i < ARRAYSIZE( m_DeclaredInputs ); i++ )
  2767. {
  2768. m_DeclaredInputs[i] = UNDECLARED_INPUT;
  2769. }
  2770. memset( m_dwAttribMap, 0xFF, sizeof(m_dwAttribMap) );
  2771. m_pdwBaseToken = m_pdwNextToken = code; // Initialize dwToken pointers
  2772. dwToken = GetNextToken();
  2773. m_dwMajorVersion = D3DSHADER_VERSION_MAJOR( dwToken );
  2774. m_dwMinorVersion = D3DSHADER_VERSION_MINOR( dwToken );
  2775. // If pixel shader
  2776. const char *glslExtText = "#extension GL_ARB_shader_texture_lod : require\n";//m_bUseBindlessTexturing ? "#extension GL_NV_bindless_texture : require\n" : "";
  2777. const char *glslVersionText = m_bUseBindlessTexturing ? "330 compatibility" : "120";
  2778. if ( ( dwToken & 0xFFFF0000 ) == 0xFFFF0000 )
  2779. {
  2780. // must explicitly enable extensions if emitting GLSL
  2781. V_snprintf( (char *)m_pBufHeaderCode->Base(), m_pBufHeaderCode->Size(), "#version %s\n%s", glslVersionText, glslExtText );
  2782. m_bVertexShader = false;
  2783. }
  2784. else // vertex shader
  2785. {
  2786. m_bGenerateSRGBWriteSuffix = false;
  2787. V_snprintf( (char *)m_pBufHeaderCode->Base(), m_pBufHeaderCode->Size(), "#version %s\n%s//ATTRIBMAP-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx-xx\n", glslVersionText, glslExtText );
  2788. // find that first '-xx' which is where the attrib map will be written later.
  2789. pAttribMapStart = strstr( (char *)m_pBufHeaderCode->Base(), "-xx" ) + 1;
  2790. m_bVertexShader = true;
  2791. }
  2792. *bVertexShader = m_bVertexShader;
  2793. m_bGenerateBoneUniformBuffer = m_bVertexShader && ((options & D3DToGL_OptionGenerateBoneUniformBuffer) != 0);
  2794. if ( m_bAddHexCodeComments )
  2795. {
  2796. RecordInputAndOutputPositions();
  2797. }
  2798. if ( m_bSpew )
  2799. {
  2800. printf("\n************* translating shader " );
  2801. }
  2802. int opcounter = 0;
  2803. // Loop until we hit the end dwToken...note that D3DPS_END() == D3DVS_END() so this works for either
  2804. while ( dwToken != D3DPS_END() )
  2805. {
  2806. if ( m_bAddHexCodeComments )
  2807. {
  2808. AddTokenHexCode();
  2809. RecordInputAndOutputPositions();
  2810. }
  2811. #ifdef POSIX
  2812. int tokenIndex = m_pdwNextToken - code;
  2813. #endif
  2814. int aluCodeLength0 = V_strlen( (char *) m_pBufALUCode->Base() );
  2815. dwToken = GetNextToken(); // Get next dwToken in the stream
  2816. nInstruction = Opcode( dwToken ); // Mask out the instruction opcode
  2817. if ( m_bSpew )
  2818. {
  2819. #ifdef POSIX
  2820. printf("\n** token# %04x inst# %04d opcode %s (%08x)", tokenIndex, opcounter, GLMDecode(eD3D_SIO, nInstruction), dwToken );
  2821. #endif
  2822. opcounter++;
  2823. }
  2824. switch ( nInstruction )
  2825. {
  2826. // -- No arguments at all -----------------------------------------------
  2827. case D3DSIO_NOP:
  2828. // D3D compiler outputs NOPs when shader debugging/optimizations are disabled.
  2829. break;
  2830. case D3DSIO_PHASE:
  2831. case D3DSIO_RET:
  2832. case D3DSIO_ENDLOOP:
  2833. case D3DSIO_BREAK:
  2834. TranslationError();
  2835. PrintOpcode( nInstruction, buff, sizeof( buff ) );
  2836. StrcatToALUCode( buff );
  2837. StrcatToALUCode( ";\n" );
  2838. break;
  2839. // -- "Declarative" non dcl ops ----------------------------------------
  2840. case D3DSIO_TEXDEPTH:
  2841. case D3DSIO_TEXKILL:
  2842. Handle_DeclarativeNonDclOp( nInstruction );
  2843. break;
  2844. // -- Unary ops -------------------------------------------------
  2845. case D3DSIO_BEM:
  2846. case D3DSIO_TEXBEM:
  2847. case D3DSIO_TEXBEML:
  2848. case D3DSIO_TEXDP3:
  2849. case D3DSIO_TEXDP3TEX:
  2850. case D3DSIO_TEXM3x2DEPTH:
  2851. case D3DSIO_TEXM3x2TEX:
  2852. case D3DSIO_TEXM3x3:
  2853. case D3DSIO_TEXM3x3PAD:
  2854. case D3DSIO_TEXM3x3TEX:
  2855. case D3DSIO_TEXM3x3VSPEC:
  2856. case D3DSIO_TEXREG2AR:
  2857. case D3DSIO_TEXREG2GB:
  2858. case D3DSIO_TEXREG2RGB:
  2859. case D3DSIO_LABEL:
  2860. case D3DSIO_CALL:
  2861. case D3DSIO_LOOP:
  2862. case D3DSIO_BREAKP:
  2863. TranslationError();
  2864. break;
  2865. case D3DSIO_DSX:
  2866. case D3DSIO_DSY:
  2867. Handle_UnaryOp( nInstruction );
  2868. break;
  2869. case D3DSIO_IFC:
  2870. {
  2871. static const char *s_szCompareStrings[ 7 ] =
  2872. {
  2873. "__INVALID__",
  2874. ">",
  2875. "==",
  2876. ">=",
  2877. "<",
  2878. "!=",
  2879. "<="
  2880. };
  2881. // Compare mode is encoded in instruction token
  2882. uint32 dwCompareMode = OpcodeSpecificData( dwToken );
  2883. Assert( ( dwCompareMode >= 1 ) && ( dwCompareMode <= 6 ) );
  2884. // Get left side of compare
  2885. dwToken = GetNextToken();
  2886. char szLeftSide[32];
  2887. PrintParameterToString( dwToken, SRC_REGISTER, szLeftSide, sizeof( szLeftSide ), false, NULL );
  2888. // Get right side of compare
  2889. dwToken = GetNextToken();
  2890. char szRightSide[32];
  2891. PrintParameterToString( dwToken, SRC_REGISTER, szRightSide, sizeof( szRightSide ), false, NULL );
  2892. PrintToBufWithIndents( *m_pBufALUCode, "if ( %s %s %s )\n", szLeftSide, s_szCompareStrings[dwCompareMode], szRightSide );
  2893. StrcatToALUCode( "{\n" );
  2894. m_NumIndentTabs++;
  2895. break;
  2896. }
  2897. case D3DSIO_IF:
  2898. dwToken = GetNextToken();
  2899. PrintParameterToString( dwToken, SRC_REGISTER, buff, sizeof( buff ), false, NULL );
  2900. PrintToBufWithIndents( *m_pBufALUCode, "if ( %s )\n", buff );
  2901. StrcatToALUCode( "{\n" );
  2902. m_NumIndentTabs++;
  2903. break;
  2904. case D3DSIO_ELSE:
  2905. m_NumIndentTabs--;
  2906. StrcatToALUCode( "}\n" );
  2907. StrcatToALUCode( "else\n" );
  2908. StrcatToALUCode( "{\n" );
  2909. m_NumIndentTabs++;
  2910. break;
  2911. case D3DSIO_ENDIF:
  2912. m_NumIndentTabs--;
  2913. StrcatToALUCode( "}\n" );
  2914. break;
  2915. case D3DSIO_REP:
  2916. dwToken = GetNextToken();
  2917. PrintParameterToString( dwToken, SRC_REGISTER, buff, sizeof( buff ), false, NULL );
  2918. // In practice, this is the only form of for loop that will appear in DX asm
  2919. PrintToBufWithIndents( *m_pBufALUCode, "for( int i=0; i < %s; i++ )\n", buff );
  2920. StrcatToALUCode( "{\n" );
  2921. m_nLoopDepth++;
  2922. // For now, we don't deal with loop nesting
  2923. // Easy enough to fix later with an array of loop names i, j, k etc
  2924. Assert( m_nLoopDepth <= 1 );
  2925. m_NumIndentTabs++;
  2926. break;
  2927. case D3DSIO_ENDREP:
  2928. m_nLoopDepth--;
  2929. m_NumIndentTabs--;
  2930. StrcatToALUCode( "}\n" );
  2931. break;
  2932. case D3DSIO_NRM:
  2933. Handle_NRM();
  2934. break;
  2935. case D3DSIO_MOVA:
  2936. Handle_UnaryOp( nInstruction );
  2937. break;
  2938. // Unary operations
  2939. case D3DSIO_MOV:
  2940. case D3DSIO_RCP:
  2941. case D3DSIO_RSQ:
  2942. case D3DSIO_EXP:
  2943. case D3DSIO_EXPP:
  2944. case D3DSIO_LOG:
  2945. case D3DSIO_LOGP:
  2946. case D3DSIO_FRC:
  2947. case D3DSIO_LIT:
  2948. case D3DSIO_ABS:
  2949. Handle_UnaryOp( nInstruction );
  2950. break;
  2951. // -- Binary ops -------------------------------------------------
  2952. case D3DSIO_TEXM3x3SPEC:
  2953. case D3DSIO_M4x4:
  2954. case D3DSIO_M4x3:
  2955. case D3DSIO_M3x4:
  2956. case D3DSIO_M3x3:
  2957. case D3DSIO_M3x2:
  2958. case D3DSIO_CALLNZ:
  2959. case D3DSIO_SETP:
  2960. TranslationError();
  2961. break;
  2962. case D3DSIO_BREAKC:
  2963. Handle_BREAKC( dwToken );
  2964. break;
  2965. // Binary Operations
  2966. case D3DSIO_ADD:
  2967. case D3DSIO_SUB:
  2968. case D3DSIO_MUL:
  2969. case D3DSIO_DP3:
  2970. case D3DSIO_DP4:
  2971. case D3DSIO_MIN:
  2972. case D3DSIO_MAX:
  2973. case D3DSIO_DST:
  2974. case D3DSIO_SLT:
  2975. case D3DSIO_SGE:
  2976. case D3DSIO_CRS:
  2977. case D3DSIO_POW:
  2978. HandleBinaryOp_GLSL( nInstruction );
  2979. break;
  2980. // -- Ternary ops -------------------------------------------------
  2981. case D3DSIO_DP2ADD:
  2982. Handle_DP2ADD();
  2983. break;
  2984. case D3DSIO_LRP:
  2985. Handle_LRP( nInstruction );
  2986. break;
  2987. case D3DSIO_SGN:
  2988. Assert( m_bVertexShader );
  2989. TranslationError(); // TODO emulate with SLT etc
  2990. break;
  2991. case D3DSIO_CND:
  2992. TranslationError();
  2993. break;
  2994. case D3DSIO_CMP:
  2995. Handle_CMP();
  2996. break;
  2997. case D3DSIO_SINCOS:
  2998. Handle_SINCOS();
  2999. break;
  3000. case D3DSIO_MAD:
  3001. Handle_MAD( nInstruction );
  3002. break;
  3003. // -- Quaternary op ------------------------------------------------
  3004. case D3DSIO_TEXLDD:
  3005. Handle_TexLDD( nInstruction );
  3006. break;
  3007. // -- Special cases: texcoord vs texcrd and tex vs texld -----------
  3008. case D3DSIO_TEXCOORD:
  3009. Handle_TexCoord();
  3010. break;
  3011. case D3DSIO_TEX:
  3012. Handle_TEX( dwToken, false );
  3013. break;
  3014. case D3DSIO_TEXLDL:
  3015. Handle_TEX( nInstruction, true );
  3016. break;
  3017. case D3DSIO_DCL:
  3018. Handle_DCL();
  3019. break;
  3020. case D3DSIO_DEFB:
  3021. case D3DSIO_DEFI:
  3022. Handle_DEFIB( nInstruction );
  3023. break;
  3024. case D3DSIO_DEF:
  3025. Handle_DEF();
  3026. break;
  3027. case D3DSIO_COMMENT:
  3028. // Using OpcodeSpecificData() can fail here since the comments can be longer than 0xff dwords
  3029. nNumTokensToSkip = ( dwToken & 0x0fff0000 ) >> 16;
  3030. SkipTokens( nNumTokensToSkip );
  3031. break;
  3032. case D3DSIO_END:
  3033. break;
  3034. }
  3035. if ( m_bSpew )
  3036. {
  3037. int aluCodeLength1 = V_strlen( (char *) m_pBufALUCode->Base() );
  3038. if ( aluCodeLength1 != aluCodeLength0 )
  3039. {
  3040. // code was emitted
  3041. printf( "\n > %s", ((char *)m_pBufALUCode->Base()) + aluCodeLength0 );
  3042. aluCodeLength0 = aluCodeLength1;
  3043. }
  3044. }
  3045. }
  3046. // Note that this constant packing expects .wzyx swizzles in case we ever use the SINCOS code in a ps_2_x shader
  3047. //
  3048. // The Microsoft documentation on this is all kinds of broken and, strangely, these numbers don't even
  3049. // match the D3DSINCOSCONST1 and D3DSINCOSCONST2 constants used by the D3D assembly sincos instruction...
  3050. if ( m_bNeedsSinCosDeclarations )
  3051. {
  3052. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3053. StrcatToParamCode( "vec4 scA = vec4( -1.55009923e-6, -2.17013894e-5, 0.00260416674, 0.00026041668 );\n" );
  3054. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3055. StrcatToParamCode( "vec4 scB = vec4( -0.020833334, -0.125, 1.0, 0.5 );\n" );
  3056. }
  3057. // Stick in the sampler mask in hex
  3058. PrintToBuf( *m_pBufHeaderCode, "%sSAMPLERMASK-%x\n", "//", m_dwSamplerUsageMask );
  3059. uint nSamplerTypes = 0;
  3060. for ( int i = 0; i < 16; i++ )
  3061. {
  3062. Assert( m_dwSamplerTypes[i] < 4);
  3063. nSamplerTypes |= ( m_dwSamplerTypes[i] << ( i * 2 ) );
  3064. }
  3065. PrintToBuf( *m_pBufHeaderCode, "%sSAMPLERTYPES-%x\n", "//", nSamplerTypes );
  3066. // fragData outputs referenced
  3067. uint nFragDataMask = 0;
  3068. for ( int i = 0; i < 4; i++ )
  3069. {
  3070. nFragDataMask |= m_bOutputColorRegister[ i ] ? ( 1 << i ) : 0;
  3071. }
  3072. PrintToBuf( *m_pBufHeaderCode, "%sFRAGDATAMASK-%x\n", "//", nFragDataMask );
  3073. // Uniforms
  3074. PrintToBuf( *m_pBufHeaderCode, "//HIGHWATER-%d\n", m_nHighestRegister + 1 );
  3075. if ( ( m_bVertexShader ) && ( m_bGenerateBoneUniformBuffer ) )
  3076. {
  3077. PrintToBuf( *m_pBufHeaderCode, "//HIGHWATERBONE-%i\n", m_nHighestBoneRegister + 1 );
  3078. }
  3079. PrintToBuf( *m_pBufHeaderCode, "\nuniform vec4 %s[%d];\n", m_bVertexShader ? "vc" : "pc", m_nHighestRegister + 1 );
  3080. if ( ( m_nHighestBoneRegister >= 0 ) && ( m_bVertexShader ) && ( m_bGenerateBoneUniformBuffer ) )
  3081. {
  3082. PrintToBuf( *m_pBufHeaderCode, "\nuniform vec4 %s[%d];\n", "vcbones", m_nHighestBoneRegister + 1 );
  3083. }
  3084. if ( m_bVertexShader )
  3085. {
  3086. PrintToBuf( *m_pBufHeaderCode, "\nuniform vec4 vcscreen;\n" );
  3087. }
  3088. for( int i=0; i<32; i++ )
  3089. {
  3090. if ( ( m_dwConstIntUsageMask & ( 0x00000001 << i ) ) &&
  3091. ( !( m_dwDefConstIntUsageMask & ( 0x00000001 << i ) ) )
  3092. )
  3093. {
  3094. PrintToBuf( *m_pBufHeaderCode, "uniform int i%d ;\n", i );
  3095. }
  3096. }
  3097. for( int i=0; i<32; i++ )
  3098. {
  3099. if ( m_dwDefConstIntUsageMask & ( 0x00000001 << i ) )
  3100. {
  3101. PrintToBuf( *m_pBufHeaderCode, "const int i%d = %i;\n", i, m_dwDefConstIntIterCount[i] );
  3102. }
  3103. }
  3104. for( int i=0; i<32; i++ )
  3105. {
  3106. if ( m_dwConstBoolUsageMask & ( 0x00000001 << i ) )
  3107. {
  3108. PrintToBuf( *m_pBufHeaderCode, m_bVertexShader ? "uniform bool b%d;\n" : "uniform bool fb%d;\n", i );
  3109. }
  3110. }
  3111. // Control bit for sRGB Write suffix
  3112. if ( m_bGenerateSRGBWriteSuffix )
  3113. {
  3114. // R500 Hookup
  3115. // Set this guy to 1 when the sRGBWrite state is true, otherwise 0
  3116. StrcatToHeaderCode( "uniform float flSRGBWrite;\n" );
  3117. }
  3118. PrintToBuf( *m_pBufHeaderCode, "\n" );
  3119. // Write samplers
  3120. WriteGLSLSamplerDefinitions();
  3121. if ( m_bUsesDSTInstruction )
  3122. {
  3123. PrintToBuf( *m_pBufHeaderCode, "vec4 dst(vec4 src0,vec4 src1) { return vec4(1.0f,src0.y*src1.y,src0.z,src1.w); }\n" );
  3124. }
  3125. if ( m_bDeclareAddressReg )
  3126. {
  3127. if ( !m_bGenerateBoneUniformBuffer )
  3128. {
  3129. m_nHighestRegister = DXABSTRACT_VS_PARAM_SLOTS - 1;
  3130. }
  3131. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3132. StrcatToParamCode( "vec4 va_r;\n" );
  3133. }
  3134. char *pTempVarStr = "TEMP";
  3135. pTempVarStr = "vec4";
  3136. // Declare temps in Param code buffer
  3137. for( int i=0; i<32; i++ )
  3138. {
  3139. if ( m_dwTempUsageMask & ( 0x00000001 << i ) )
  3140. {
  3141. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3142. PrintToBuf( *m_pBufParamCode, "%s r%d;\n", pTempVarStr, i );
  3143. }
  3144. }
  3145. if ( m_bVertexShader && (m_bDoUserClipPlanes || m_bDoFixupZ || m_bDoFixupY ) )
  3146. {
  3147. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3148. StrcatToParamCode( "vec4 vTempPos;\n" );
  3149. }
  3150. if ( ( m_bVertexShader ) && ( m_dwMajorVersion == 3 ) )
  3151. {
  3152. for ( int i = 0; i < 32; i++ )
  3153. {
  3154. if ( m_dwTexCoordOutMask & ( 1 << i ) )
  3155. {
  3156. PrintIndentation( (char*)m_pBufParamCode->Base(), m_pBufParamCode->Size() );
  3157. char buf[256];
  3158. V_snprintf( buf, sizeof( buf ), "vec4 oTempT%i = vec4( 0, 0, 0, 0 );\n", i );
  3159. StrcatToParamCode( buf );
  3160. }
  3161. }
  3162. }
  3163. if ( m_bNeedsSinCosDeclarations )
  3164. {
  3165. StrcatToParamCode( "vec3 vSinCosTmp;\n" ); // declare temp used by GLSL sin and cos intrinsics
  3166. }
  3167. // Optional temps needed to emulate d2add instruction in DX pixel shaders
  3168. if ( m_bNeedsD2AddTemp )
  3169. {
  3170. PrintToBuf( *m_pBufParamCode, "%s DP2A0;\n%s DP2A1;\n", pTempVarStr, pTempVarStr );
  3171. }
  3172. // Optional temp needed to emulate lerp instruction in DX vertex shaders
  3173. if ( m_bNeedsLerpTemp )
  3174. {
  3175. PrintToBuf( *m_pBufParamCode, "%s LRP_TEMP;\n", pTempVarStr );
  3176. }
  3177. // Optional temp needed to emulate NRM instruction in DX shaders
  3178. if ( m_bNeedsNRMTemp )
  3179. {
  3180. PrintToBuf( *m_pBufParamCode, "%s NRM_TEMP;\n", pTempVarStr );
  3181. }
  3182. if ( m_bDeclareVSOPos && m_bVertexShader )
  3183. {
  3184. if ( m_bDoUserClipPlanes )
  3185. {
  3186. StrcatToALUCode( "gl_ClipVertex = vTempPos;\n" ); // if user clip is enabled, jam clip space position into gl_ClipVertex
  3187. }
  3188. if ( m_bDoFixupZ || m_bDoFixupY )
  3189. {
  3190. // TODO: insert clip distance computation something like this:
  3191. //
  3192. // StrcatToALUCode( "DP4 oCLP[0].x, oPos, vc[215]; \n" );
  3193. //
  3194. if ( m_bDoFixupZ )
  3195. {
  3196. StrcatToALUCode( "vTempPos.z = vTempPos.z * vc[0].z - vTempPos.w; // z' = (2*z)-w\n" );
  3197. }
  3198. if ( m_bDoFixupY )
  3199. {
  3200. // append instructions to flip Y over
  3201. // new Y = -(old Y)
  3202. StrcatToALUCode( "vTempPos.y = -vTempPos.y; // y' = -y \n" );
  3203. }
  3204. StrcatToALUCode( "vTempPos.xy += vcscreen.xy * vTempPos.w;\n" );
  3205. StrcatToALUCode( "gl_Position = vTempPos;\n" );
  3206. }
  3207. else
  3208. {
  3209. StrcatToParamCode( "OUTPUT oPos = result.position;\n" );
  3210. // TODO: insert clip distance computation something like this:
  3211. //
  3212. // StrcatToALUCode( "DP4 oCLP[0].x, oPos, c[215]; \n" );
  3213. //
  3214. }
  3215. }
  3216. if ( m_bVertexShader )
  3217. {
  3218. if ( m_dwMajorVersion == 3 )
  3219. {
  3220. WriteGLSLOutputVariableAssignments();
  3221. }
  3222. else
  3223. {
  3224. for ( int i=0; i<32; i++ )
  3225. {
  3226. char outTexCoordBuff[64];
  3227. // Don't declare a varying for the output that is mapped to the position output
  3228. if ( i != m_nVSPositionOutput )
  3229. {
  3230. if ( m_dwTexCoordOutMask & ( 0x00000001 << i ) )
  3231. {
  3232. if ( m_nCentroidMask & ( 0x00000001 << i ) )
  3233. {
  3234. V_snprintf( outTexCoordBuff, sizeof( outTexCoordBuff ), "centroid varying vec4 oT%d;\n", i ); // centroid varying
  3235. StrcatToHeaderCode( outTexCoordBuff );
  3236. }
  3237. else
  3238. {
  3239. V_snprintf( outTexCoordBuff, sizeof( outTexCoordBuff ), "varying vec4 oT%d;\n", i );
  3240. StrcatToHeaderCode( outTexCoordBuff );
  3241. }
  3242. }
  3243. }
  3244. }
  3245. }
  3246. }
  3247. else
  3248. {
  3249. if ( m_dwMajorVersion == 3 )
  3250. {
  3251. WriteGLSLInputVariableAssignments();
  3252. }
  3253. }
  3254. // do some annotation at the end of the attrib block
  3255. {
  3256. char temp[1000];
  3257. if ( m_bVertexShader )
  3258. {
  3259. // write attrib map into the text starting at pAttribMapStart - two hex digits per attrib
  3260. for( int i=0; i<16; i++ )
  3261. {
  3262. if ( m_dwAttribMap[i] != 0xFFFFFFFF )
  3263. {
  3264. V_snprintf( temp, sizeof(temp), "%02X", m_dwAttribMap[i] );
  3265. memcpy( pAttribMapStart + (i*3), temp, 2 );
  3266. }
  3267. }
  3268. }
  3269. PrintIndentation( (char*)m_pBufAttribCode->Base(), m_pBufAttribCode->Size() );
  3270. // This used to write out a translation counter into the shader as a comment. However, the order that shaders get in here
  3271. // is non-deterministic between runs, and the change in this comment would cause shaders to appear different to the GL disk cache,
  3272. // significantly increasing app load time.
  3273. // Other code looks for trans#%d, so we can't just remove it. Instead, output it as 0.
  3274. V_snprintf( temp, sizeof(temp), "%s trans#%d label:%s\n", "//", 0, debugLabel ? debugLabel : "none" );
  3275. StrcatToAttribCode( temp );
  3276. }
  3277. // If we actually sample from a shadow depth sampler, we need to declare the shadow option at the top
  3278. if ( m_bDeclareShadowOption )
  3279. {
  3280. StrcatToHeaderCode( "OPTION ARB_fragment_program_shadow;\n" );
  3281. }
  3282. StrcatToHeaderCode( "\nvoid main()\n{\n" );
  3283. if ( m_bUsedAtomicTempVar )
  3284. {
  3285. PrintToBufWithIndents( *m_pBufHeaderCode, "vec4 %s;\n\n", g_pAtomicTempVarName );
  3286. }
  3287. // sRGB Write suffix
  3288. if ( m_bGenerateSRGBWriteSuffix )
  3289. {
  3290. StrcatToALUCode( "vec3 sRGBFragData;\n" );
  3291. StrcatToALUCode( "sRGBFragData.xyz = log( gl_FragData[0].xyz );\n" );
  3292. StrcatToALUCode( "sRGBFragData.xyz = sRGBFragData.xyz * vec3( 0.454545f, 0.454545f, 0.454545f );\n" );
  3293. StrcatToALUCode( "sRGBFragData.xyz = exp( sRGBFragData.xyz );\n" );
  3294. StrcatToALUCode( "gl_FragData[0].xyz = mix( gl_FragData[0].xyz, sRGBFragData, flSRGBWrite );\n" );
  3295. }
  3296. strcat_s( (char*)m_pBufALUCode->Base(), m_pBufALUCode->Size(), "}\n" );
  3297. // Put all of the strings together for final program ( pHeaderCode + pAttribCode + pParamCode + pALUCode )
  3298. StrcatToHeaderCode( (char*)m_pBufAttribCode->Base() );
  3299. StrcatToHeaderCode( (char*)m_pBufParamCode->Base() );
  3300. StrcatToHeaderCode( (char*)m_pBufALUCode->Base() );
  3301. // Cleanup - don't touch m_pBufHeaderCode, as it is managed by the caller
  3302. delete m_pBufAttribCode;
  3303. delete m_pBufParamCode;
  3304. delete m_pBufALUCode;
  3305. m_pBufAttribCode = m_pBufParamCode = m_pBufALUCode = NULL;
  3306. if ( m_bSpew )
  3307. {
  3308. printf("\n************* translation complete\n\n " );
  3309. }
  3310. return DISASM_OK;
  3311. }