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.

123 lines
4.3 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ====//
  2. //
  3. // Fragment Program Constant Patcher: an SPU implementation, V0
  4. //
  5. #ifndef PS3_SHADER_CONSTANT_PATCH_SPU_HDR
  6. #define PS3_SHADER_CONSTANT_PATCH_SPU_HDR
  7. #ifdef _PS3
  8. #include "vjobs/fpcpatch_shared.h"
  9. #include <cg/cg.h>
  10. #include <cg/cgBinary.h>
  11. #ifdef _DEBUG
  12. //#define DEBUG_FPC_PATCHER
  13. #endif
  14. class FpcPatchState
  15. {
  16. public:
  17. job_fpcpatch::FpcPatchState_t * m_pSharedState;
  18. uint32 m_nEndOfJournalIdx; // this is PPU-side variable only, written by PPU only
  19. fltx4 * GetBufferStart(){ return m_pSharedState->GetBufferStart() ; } // the buffer start address
  20. uint32 m_nBufferMask; // the number of Qwords in the buffer
  21. //#ifdef _DEBUG
  22. //int m_nRangesAdded;
  23. //#endif
  24. public:
  25. FpcPatchState(){m_pSharedState = NULL;}
  26. void Init( job_fpcpatch::FpcPatchState_t * pSharedState, uint32 nBufferQwords );
  27. void Reset();
  28. uint AddRange( uint32 nStart, uint32 nCount, const float * pData );
  29. void GetSyncState( fltx4 * pRegisters );
  30. protected:
  31. fltx4 * AddInternalPtr()
  32. {
  33. fltx4 * pOut = GetBufferStart() + ( m_nEndOfJournalIdx & m_nBufferMask );
  34. m_nEndOfJournalIdx++;
  35. return pOut;
  36. }
  37. void AddInternal( const fltx4 f4 )
  38. {
  39. *AddInternalPtr() = f4;
  40. }
  41. inline void AddInternalBlock( const void *pBlock, const uint32 numFltx4s )
  42. {
  43. // Fit the first portion until the end of the buffer, second portion at start
  44. uint32 const nCurrentIdx = ( m_nEndOfJournalIdx & m_nBufferMask ); // the start index to copy to
  45. uint32 const numFltx4sUntilEnd = ( -nCurrentIdx ) & m_nBufferMask; // number of fltx4's from the nCurrentIdx to the end of the current buffer ring
  46. uint32 const numFirstCopy = MIN( numFltx4sUntilEnd, numFltx4s ); // number of fltx4's to copy first
  47. memcpy( GetBufferStart() + nCurrentIdx, pBlock, numFirstCopy * sizeof( fltx4 ) );
  48. memcpy( GetBufferStart(), ( ( fltx4* ) pBlock ) + numFirstCopy, ( numFltx4s - numFirstCopy ) * sizeof( fltx4 ) );
  49. m_nEndOfJournalIdx += numFltx4s;
  50. }
  51. };
  52. struct IDirect3DPixelShader9 ;
  53. class CFragmentProgramConstantPatcher_SPU
  54. {
  55. public:
  56. CFragmentProgramConstantPatcher_SPU();
  57. ~CFragmentProgramConstantPatcher_SPU();
  58. void InitLocal( void *pBuffer, uint nSize );
  59. void Shutdown();
  60. // semantics should match cgGLSetFragmentRegisterBlock()
  61. void SetFragmentRegisterBlock( uint StartRegister, uint Vector4fCount, const float* pConstantData );
  62. // semantics of cgGLBindProgram( pPixelShader->m_pixProgram->m_CGprogram )
  63. void BindProgram( const CgBinaryProgram *prog );
  64. void BindProgram( const struct IDirect3DPixelShader9 * prog );
  65. void BeginScene();
  66. void EndScene();
  67. //job_fpcpatch::FpcPatchState_t * GetSharedState(){return m_state.m_pSharedState; }
  68. uint GetStateEndOfJournalIdx() { return m_state.m_nEndOfJournalIdx; }
  69. uint GetJournalCapacity() const { return m_state.m_nBufferMask + 1; }
  70. int GetJournalSpaceUsedSince( uint nMarker )const{ return int( m_state.m_nEndOfJournalIdx - nMarker ); }
  71. int GetJournalSpaceLeftSince( uint nMarker )const{ return int( ( m_state.m_nBufferMask + 1 ) - ( m_state.m_nEndOfJournalIdx - nMarker ) ); }
  72. protected:
  73. void ResetPut();
  74. void * FpcPatch( const struct CgBinaryProgram * prog, void * pFragmentProgramDestination, uint32 * pJts );
  75. void FpcPatch2( const job_fpcpatch2::FpHeader_t * psh, uint nFpDmaSize, void *pPatchedProgram, uint32 * pJts );
  76. protected:
  77. friend class CSpuGcm;
  78. FpcPatchState m_state;
  79. uint32* m_pBuffer, *m_pBufferEnd;
  80. int m_nIoOffsetDelta; // m_pBuffer + m_nIoOffsetDelta == IO offset usable by RSX
  81. uint32 * m_pPutFragmentProgram;
  82. uint m_nFpcPatchCounterAtBeginScene; // used for timing
  83. uint m_nFpcPatchCounterOfLastSyncJob;
  84. uint m_nBufferLocation;// CELL_GCM_LOCATION_MAIN
  85. uint m_nFpcPatchCounter, m_nFpcPatchSyncMask;
  86. //uint m_nStartRangesAfterLastSync; // this is the index used to upload only the useful constants to SPU
  87. bool m_isBufferPassedIn;
  88. bool m_bFpcPatchOnPpu, m_bEnableSPU;
  89. #ifdef DEBUG_FPC_PATCHER
  90. void ValidatePatchedProgram( const CgBinaryProgram *prog, void * pPatchedUcode );
  91. fltx4 *m_pSyncState;
  92. bool m_bTestAlwaysStateSync;
  93. bool m_bSync; // don't use JTS, but just patch synchronously (may be more stable with GPAD)
  94. #endif
  95. };
  96. extern CFragmentProgramConstantPatcher_SPU g_pixelShaderPatcher; // Patches pixel shader constants
  97. #endif
  98. #endif