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.

384 lines
11 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // glmgrbasics.h
  4. // types, common headers, forward declarations, utilities
  5. //
  6. //===============================================================================
  7. #ifndef GLMBASICS_H
  8. #define GLMBASICS_H
  9. #pragma once
  10. #include <OpenGL/OpenGL.h>
  11. #include <OpenGL/gl.h>
  12. #include <OpenGL/glext.h>
  13. #include <OpenGL/CGLTypes.h>
  14. #include <OpenGL/CGLRenderers.h>
  15. #include <OpenGL/CGLCurrent.h>
  16. #include <OpenGL/CGLProfiler.h>
  17. #include <ApplicationServices/ApplicationServices.h>
  18. #include "bitmap/imageformat.h"
  19. #include "bitvec.h"
  20. #include "tier1/checksum_md5.h"
  21. #include "tier1/utlvector.h"
  22. #include "tier1/convar.h"
  23. #include <sys/stat.h>
  24. #include "glmgr/glmdebug.h"
  25. // types
  26. // 3-d integer box (used for texture lock/unlock etc)
  27. struct GLMRegion
  28. {
  29. int xmin,xmax;
  30. int ymin,ymax;
  31. int zmin,zmax;
  32. };
  33. struct GLMRect // follows GL convention - if coming from the D3D rect you will need to fiddle the Y's
  34. {
  35. int xmin; // left
  36. int ymin; // bottom
  37. int xmax; // right
  38. int ymax; // top
  39. };
  40. // macros
  41. //#define GLMassert(x) assert(x)
  42. // forward decls
  43. class GLMgr; // singleton
  44. class GLMContext; // GL context
  45. class CGLMContextTester; // testing class
  46. class CGLMTex;
  47. class CGLMFBO;
  48. class CGLMProgram;
  49. class CGLMBuffer;
  50. // utilities
  51. typedef enum
  52. {
  53. // D3D codes
  54. eD3D_DEVTYPE,
  55. eD3D_FORMAT,
  56. eD3D_RTYPE,
  57. eD3D_USAGE,
  58. eD3D_RSTATE, // render state
  59. eD3D_SIO, // D3D shader bytecode
  60. eD3D_VTXDECLUSAGE,
  61. // CGL codes
  62. eCGL_RENDID,
  63. // OpenGL error codes
  64. eGL_ERROR,
  65. // OpenGL enums
  66. eGL_ENUM,
  67. eGL_RENDERER
  68. } GLMThing_t;
  69. const char* GLMDecode( GLMThing_t type, unsigned long value ); // decode a numeric const
  70. const char* GLMDecodeMask( GLMThing_t type, unsigned long value ); // decode a bitmask
  71. void GLMStop( void ); // aka Debugger()
  72. void GLMCheckError( bool noStop = false, bool noLog= false );
  73. void GLMEnableTrace( bool on );
  74. //===============================================================================
  75. // debug channels
  76. enum EGLMDebugChannel
  77. {
  78. ePrintf,
  79. eDebugger,
  80. eGLProfiler
  81. };
  82. #if GLMDEBUG
  83. // make all these prototypes disappear in non GLMDEBUG
  84. void GLMDebugInitialize( bool forceReinit=false );
  85. bool GLMDetectOGLP( void );
  86. bool GLMDetectGDB( void );
  87. uint GLMDetectAvailableChannels( void );
  88. uint GLMDebugChannelMask( uint *newValue = NULL );
  89. // note that GDB and OGLP can both come and go during run - forceCheck will allow that to be detected.
  90. // mask returned is in form of 1<<n, n from EGLMDebugChannel
  91. #endif
  92. //===============================================================================
  93. // debug message flavors
  94. enum EGLMDebugFlavor
  95. {
  96. eAllFlavors, // 0
  97. eDebugDump, // 1 debug dump flavor -D-
  98. eTenure, // 2 code tenures > <
  99. eComment, // 3 one off messages ---
  100. eMatrixData, // 4 matrix data -M-
  101. eShaderData, // 5 shader data (params) -S-
  102. eFrameBufData, // 6 FBO data (attachments) -F-
  103. eDXStuff, // 7 dxabstract spew -X-
  104. eAllocations, // 8 tracking allocs and frees -A-
  105. eSlowness, // 9 slow things happening (srgb flips..) -Z-
  106. eDefaultFlavor, // not specified (no marker)
  107. eFlavorCount
  108. };
  109. uint GLMDebugFlavorMask( uint *newValue = NULL );
  110. //===============================================================================
  111. // output functions
  112. // make all these prototypes disappear in non GLMDEBUG
  113. #if GLMDEBUG
  114. // these are unconditional outputs, they don't interrogate the string
  115. void GLMStringOut( const char *string );
  116. void GLMStringOutIndented( const char *string, int indentColumns );
  117. // these will look at the string to guess its flavor: <, >, ---, -M-, -S-
  118. void GLMPrintfVA( const char *fmt, va_list vargs );
  119. void GLMPrintf( const char *fmt, ... );
  120. // these take an explicit flavor with a default value
  121. void GLMPrintStr( const char *str, EGLMDebugFlavor flavor = eDefaultFlavor );
  122. #define GLMPRINTTEXT_NUMBEREDLINES 0x80000000
  123. void GLMPrintText( const char *str, EGLMDebugFlavor flavor = eDefaultFlavor, uint options=0 ); // indent each newline
  124. int GLMIncIndent( int indentDelta );
  125. int GLMGetIndent( void );
  126. void GLMSetIndent( int indent );
  127. #endif
  128. // expose these in release now
  129. // Mimic PIX events so we can decorate debug spew
  130. void GLMBeginPIXEvent( const char *str );
  131. void GLMEndPIXEvent( void );
  132. //===============================================================================
  133. // knob twiddling
  134. float GLMKnob( char *knobname, float *setvalue ); // Pass NULL to not-set the knob value
  135. float GLMKnobToggle( char *knobname );
  136. //===============================================================================
  137. // other stuff
  138. #if GLMDEBUG
  139. inline void GLMDebugger( void )
  140. {
  141. if (GLMDebugChannelMask() & (1<<eDebugger))
  142. {
  143. asm ( "int $3" );
  144. }
  145. if (GLMDebugChannelMask() & (1<<eGLProfiler))
  146. {
  147. // we call an obscure GL function which we know has been breakpointed in the OGLP function list
  148. static short nada[] = {0xFFFF,0xFFFF,0xFFFF,0xFFFF};
  149. glColor4sv( nada );
  150. }
  151. }
  152. #else
  153. #define GLMDebugger()
  154. #endif
  155. // helpers for CGLSetOption - no op if no profiler
  156. void GLMProfilerClearTrace( void );
  157. void GLMProfilerEnableTrace( bool enable );
  158. // helpers for CGLSetParameter - no op if no profiler
  159. void GLMProfilerDumpState( void );
  160. //===============================================================================
  161. // classes
  162. // helper class making function tracking easier to wire up
  163. #if GLMDEBUG || GLMDEBUG_PS3
  164. //extern bool g_bEnableGLMDebug;
  165. class GLMFuncLogger
  166. {
  167. public:
  168. // simple function log
  169. GLMFuncLogger( const char *funcName )
  170. {
  171. m_funcName = funcName;
  172. m_earlyOut = false;
  173. // if ( g_bEnableGLMDebug )
  174. // {
  175. // GLMPrintf( ">%s\n", m_funcName );
  176. // }
  177. };
  178. // more advanced version lets you pass args (i.e. called parameters or anything else of interest)
  179. // no macro for this one, since no easy way to pass through the args as well as the funcname
  180. GLMFuncLogger( const char *funcName, char *fmt, ... )
  181. {
  182. m_funcName = funcName;
  183. m_earlyOut = false;
  184. // this acts like GLMPrintf here
  185. // all the indent policy is down in GLMPrintfVA
  186. // which means we need to inject a ">" at the front of the format string to make this work... sigh.
  187. char modifiedFmt[2000];
  188. modifiedFmt[0] = '>';
  189. strcpy( modifiedFmt+1, fmt );
  190. va_list vargs;
  191. va_start(vargs, fmt);
  192. GLMPrintfVA( modifiedFmt, vargs );
  193. va_end( vargs );
  194. }
  195. ~GLMFuncLogger( )
  196. {
  197. return;
  198. // if ( !g_bEnableGLMDebug )
  199. // return;
  200. if (m_earlyOut)
  201. {
  202. GLMPrintf( "<%s (early out)", m_funcName );
  203. }
  204. else
  205. {
  206. GLMPrintf( "<%s", m_funcName );
  207. }
  208. };
  209. void EarlyOut( void )
  210. {
  211. m_earlyOut = true;
  212. };
  213. const char *m_funcName; // set at construction time
  214. bool m_earlyOut;
  215. };
  216. // handy macro to go with the function tracking class
  217. #define GLM_FUNC GLMFuncLogger _logger_ ( __FUNCTION__ )
  218. #else
  219. #define GLM_FUNC
  220. #endif
  221. // class to keep an in-memory mirror of a file which may be getting edited during run
  222. class CGLMFileMirror
  223. {
  224. public:
  225. CGLMFileMirror( char *fullpath ); // just associates mirror with file. if file exists it will be read.
  226. //if non existent it will be created with size zero
  227. ~CGLMFileMirror( );
  228. bool HasData( void ); // see if data avail
  229. void GetData( char **dataPtr, uint *dataSizePtr ); // read it out
  230. void SetData( char *data, uint dataSize ); // put data in (and write it to disk)
  231. bool PollForChanges( void ); // check disk copy. If different, read it back in and return true.
  232. void UpdateStatInfo( void ); // make sure stat info is current for our file
  233. void ReadFile( void );
  234. void WriteFile( void );
  235. void OpenInEditor( bool foreground=false ); // pass TRUE if you would like the editor to pop to foreground
  236. /// how about a "wait for change" method..
  237. char *m_path; // fullpath to file
  238. bool m_exists;
  239. struct stat m_stat; // stat results for the file (last time checked)
  240. char *m_data; // content of file
  241. uint m_size; // length of content
  242. };
  243. // class based on the file mirror, that makes it easy to edit them outside the app.
  244. // it receives an initial block of text from the engine, and hashes it. ("orig")
  245. // it munges it by duplicating all the text after the "!!" line, and appending it in commented form. ("munged")
  246. // a mirror file is activated, using a filename based on the hash from the orig text.
  247. // if there is already content on disk matching that filename, use that content *unless* the 'blitz' parameter is set.
  248. // (i.e. engine is instructing this subsystem to wipe out any old/modified variants of the text)
  249. class CGLMEditableTextItem
  250. {
  251. public:
  252. CGLMEditableTextItem( char *text, uint size, bool forceOverwrite, char *prefix, char *suffix = NULL ); // create a text blob from text source, optional filename suffix
  253. ~CGLMEditableTextItem( );
  254. bool HasData( void );
  255. bool PollForChanges( void ); // return true if stale i.e. you need to get a new edition
  256. void GetCurrentText( char **textOut, uint *sizeOut ); // query for read access to the active blob (could be the original, could be external edited copy)
  257. void OpenInEditor( bool foreground=false ); // call user attention to this text
  258. // internal methods
  259. void GenHashOfOrigText( void );
  260. void GenBaseNameAndFullPath( char *prefix, char *suffix );
  261. void GenMungedText( bool fromMirror );
  262. // members
  263. // orig
  264. uint m_origSize;
  265. char *m_origText; // what was submitted
  266. unsigned char m_origDigest[MD5_DIGEST_LENGTH]; // digest of what was submitted
  267. // munged
  268. uint m_mungedSize;
  269. char *m_mungedText; // re-processed edition, initial content submission to the file mirror
  270. // mirror
  271. char *m_mirrorBaseName; // generated from the hash of the orig text, plus the label / prefix
  272. char *m_mirrorFullPath; // base name
  273. CGLMFileMirror *m_mirror; // file mirror itself. holds "official" copy for GetCurrentText to return.
  274. };
  275. // debug font
  276. extern unsigned char g_glmDebugFontMap[16384];
  277. // class for cracking multi-part text blobs
  278. // sections are demarcated by beginning-of-line markers submitted in a table by the caller
  279. struct GLMTextSection
  280. {
  281. int m_markerIndex; // based on table of markers passed in to constructor
  282. uint m_textOffset; // where is the text - offset
  283. int m_textLength; // how big is the section
  284. };
  285. class CGLMTextSectioner
  286. {
  287. public:
  288. CGLMTextSectioner( char *text, int textSize, const char **markers ); // constructor finds all the sections
  289. ~CGLMTextSectioner( );
  290. int Count( void ); // how many sections found
  291. void GetSection( int index, uint *offsetOut, uint *lengthOut, int *markerIndexOut );
  292. // find section, size, what marker
  293. // note that more than one section can be marked similarly.
  294. // so policy isn't made here, you walk the sections and decide what to do if there are dupes.
  295. //members
  296. //section table
  297. CUtlVector< GLMTextSection > m_sectionTable;
  298. };
  299. #endif