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.

157 lines
3.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "togl/rendermechanism.h"
  9. #include "recording.h"
  10. #include "shaderapi/IShaderUtil.h"
  11. #include "materialsystem/IMaterialSystem.h"
  12. #include "ShaderAPIDX8_Global.h"
  13. #include "UtlVector.h"
  14. #include <stdio.h>
  15. // memdbgon must be the last include file in a .cpp file!!!
  16. #include "tier0/memdbgon.h"
  17. #ifdef RECORDING
  18. //-----------------------------------------------------------------------------
  19. // Globals
  20. //-----------------------------------------------------------------------------
  21. static CUtlVector<unsigned char> g_pRecordingBuffer;
  22. static int g_ArgsRemaining = 0;
  23. static int g_CommandStartIdx = 0;
  24. //-----------------------------------------------------------------------------
  25. // Opens the recording file
  26. //-----------------------------------------------------------------------------
  27. static FILE* OpenRecordingFile()
  28. {
  29. #ifdef CRASH_RECORDING
  30. static FILE *fp = 0;
  31. #else
  32. FILE* fp = 0;
  33. #endif
  34. static bool g_CantOpenFile = false;
  35. static bool g_NeverOpened = true;
  36. if (!g_CantOpenFile)
  37. {
  38. #ifdef CRASH_RECORDING
  39. if( g_NeverOpened )
  40. {
  41. fp = fopen( "shaderdx8.rec", "wbc" );
  42. }
  43. #else
  44. fp = fopen( "shaderdx8.rec", g_NeverOpened ? "wb" : "ab" );
  45. #endif
  46. if (!fp)
  47. {
  48. Warning("Unable to open recording file shaderdx8.rec!\n");
  49. g_CantOpenFile = true;
  50. }
  51. g_NeverOpened = false;
  52. }
  53. return fp;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Writes to the recording file
  57. //-----------------------------------------------------------------------------
  58. #define COMMAND_BUFFER_SIZE 32768
  59. static void WriteRecordingFile()
  60. {
  61. // Store the command size
  62. *(int*)&g_pRecordingBuffer[g_CommandStartIdx] =
  63. g_pRecordingBuffer.Size() - g_CommandStartIdx;
  64. #ifndef CRASH_RECORDING
  65. // When not crash recording, flush when buffer gets too big,
  66. // or when Present() is called
  67. if ((g_pRecordingBuffer.Size() < COMMAND_BUFFER_SIZE) &&
  68. (g_pRecordingBuffer[g_CommandStartIdx+4] != DX8_PRESENT))
  69. return;
  70. #endif
  71. FILE* fp = OpenRecordingFile();
  72. if (fp)
  73. {
  74. // store the command size
  75. fwrite( g_pRecordingBuffer.Base(), 1, g_pRecordingBuffer.Size(), fp );
  76. fflush( fp );
  77. #ifndef CRASH_RECORDING
  78. fclose( fp );
  79. #endif
  80. }
  81. g_pRecordingBuffer.RemoveAll();
  82. }
  83. // Write the buffered crap out on shutdown.
  84. void FinishRecording()
  85. {
  86. #ifndef CRASH_RECORDING
  87. FILE* fp = OpenRecordingFile();
  88. if (fp)
  89. {
  90. // store the command size
  91. fwrite( g_pRecordingBuffer.Base(), 1, g_pRecordingBuffer.Size(), fp );
  92. fflush( fp );
  93. }
  94. g_pRecordingBuffer.RemoveAll();
  95. #endif
  96. }
  97. // set this to true in the debugger to actually record commands.
  98. static bool g_bDoRecord = true;
  99. //-----------------------------------------------------------------------------
  100. // Records a command
  101. //-----------------------------------------------------------------------------
  102. void RecordCommand( RecordingCommands_t cmd, int numargs )
  103. {
  104. if( !g_bDoRecord )
  105. {
  106. return;
  107. }
  108. Assert( g_ArgsRemaining == 0 );
  109. g_CommandStartIdx = g_pRecordingBuffer.AddMultipleToTail( 6 );
  110. // save space for the total command size
  111. g_pRecordingBuffer[g_CommandStartIdx+4] = cmd;
  112. g_pRecordingBuffer[g_CommandStartIdx+5] = numargs;
  113. g_ArgsRemaining = numargs;
  114. if (g_ArgsRemaining == 0)
  115. WriteRecordingFile();
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Records an argument for a command, flushes when the command is done
  119. //-----------------------------------------------------------------------------
  120. void RecordArgument( void const* pMemory, int size )
  121. {
  122. if( !g_bDoRecord )
  123. {
  124. return;
  125. }
  126. Assert( g_ArgsRemaining > 0 );
  127. int tail = g_pRecordingBuffer.Size();
  128. g_pRecordingBuffer.AddMultipleToTail( size );
  129. memcpy( &g_pRecordingBuffer[tail], pMemory, size );
  130. if (--g_ArgsRemaining == 0)
  131. WriteRecordingFile();
  132. }
  133. #endif // RECORDING