Team Fortress 2 Source Code as on 22/4/2020
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.

332 lines
9.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. // TOGL CODE LICENSE
  3. //
  4. // Copyright 2011-2014 Valve Corporation
  5. // All Rights Reserved.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. // glmgrbasics.h
  26. // types, common headers, forward declarations, utilities
  27. //
  28. //===============================================================================
  29. #ifndef GLMBASICS_H
  30. #define GLMBASICS_H
  31. #pragma once
  32. #ifdef USE_SDL
  33. #include "SDL_opengl.h"
  34. #endif
  35. #ifdef OSX
  36. #include <OpenGL/CGLTypes.h>
  37. #include <OpenGL/CGLRenderers.h>
  38. #include <OpenGL/CGLCurrent.h>
  39. #include <AvailabilityMacros.h>
  40. #ifndef MAC_OS_X_VERSION_10_9
  41. #include <OpenGL/CGLProfiler.h>
  42. #include <ApplicationServices/ApplicationServices.h>
  43. #endif
  44. #endif
  45. #include "tier0/platform.h"
  46. #include "bitmap/imageformat.h"
  47. #include "bitvec.h"
  48. #include "tier1/checksum_md5.h"
  49. #include "tier1/utlvector.h"
  50. #include "tier1/convar.h"
  51. #include <sys/stat.h>
  52. #include "dxabstract_types.h"
  53. struct GLMRect;
  54. typedef void *PseudoGLContextPtr;
  55. // types
  56. // 3-d integer box (used for texture lock/unlock etc)
  57. struct GLMRegion
  58. {
  59. int xmin,xmax;
  60. int ymin,ymax;
  61. int zmin,zmax;
  62. };
  63. struct GLMRect // follows GL convention - if coming from the D3D rect you will need to fiddle the Y's
  64. {
  65. int xmin; // left
  66. int ymin; // bottom
  67. int xmax; // right
  68. int ymax; // top
  69. };
  70. // macros
  71. //#define GLMassert(x) assert(x)
  72. // forward decls
  73. class GLMgr; // singleton
  74. class GLMContext; // GL context
  75. class CGLMContextTester; // testing class
  76. class CGLMTex;
  77. class CGLMFBO;
  78. class CGLMProgram;
  79. class CGLMBuffer;
  80. // utilities
  81. typedef enum
  82. {
  83. // D3D codes
  84. eD3D_DEVTYPE,
  85. eD3D_FORMAT,
  86. eD3D_RTYPE,
  87. eD3D_USAGE,
  88. eD3D_RSTATE, // render state
  89. eD3D_SIO, // D3D shader bytecode
  90. eD3D_VTXDECLUSAGE,
  91. // CGL codes
  92. eCGL_RENDID,
  93. // OpenGL error codes
  94. eGL_ERROR,
  95. // OpenGL enums
  96. eGL_ENUM,
  97. eGL_RENDERER
  98. } GLMThing_t;
  99. // these will look at the string to guess its flavor: <, >, ---, -M-, -S-
  100. #ifdef TOGL_DLL_EXPORT
  101. DLL_EXPORT const char* GLMDecode( GLMThing_t type, unsigned long value ); // decode a numeric const
  102. #else
  103. DLL_IMPORT const char* GLMDecode( GLMThing_t type, unsigned long value ); // decode a numeric const
  104. #endif
  105. const char* GLMDecodeMask( GLMThing_t type, unsigned long value ); // decode a bitmask
  106. FORCEINLINE void GLMStop( void ) { DXABSTRACT_BREAK_ON_ERROR(); }
  107. void GLMEnableTrace( bool on );
  108. //===============================================================================
  109. // output functions
  110. // expose these in release now
  111. // Mimic PIX events so we can decorate debug spew
  112. DLL_EXPORT void GLMBeginPIXEvent( const char *str );
  113. DLL_EXPORT void GLMEndPIXEvent( void );
  114. class CScopedGLMPIXEvent
  115. {
  116. CScopedGLMPIXEvent( const CScopedGLMPIXEvent & );
  117. CScopedGLMPIXEvent& operator= ( const CScopedGLMPIXEvent & );
  118. public:
  119. inline CScopedGLMPIXEvent( const char *pName ) { GLMBeginPIXEvent( pName ); }
  120. inline ~CScopedGLMPIXEvent() { GLMEndPIXEvent( ); }
  121. };
  122. #if GLMDEBUG
  123. //===============================================================================
  124. // classes
  125. // helper class making function tracking easier to wire up
  126. class GLMFuncLogger
  127. {
  128. public:
  129. // simple function log
  130. GLMFuncLogger( const char *funcName )
  131. {
  132. m_funcName = funcName;
  133. m_earlyOut = false;
  134. GLMPrintf( ">%s", m_funcName );
  135. };
  136. // more advanced version lets you pass args (i.e. called parameters or anything else of interest)
  137. // no macro for this one, since no easy way to pass through the args as well as the funcname
  138. GLMFuncLogger( const char *funcName, char *fmt, ... )
  139. {
  140. m_funcName = funcName;
  141. m_earlyOut = false;
  142. // this acts like GLMPrintf here
  143. // all the indent policy is down in GLMPrintfVA
  144. // which means we need to inject a ">" at the front of the format string to make this work... sigh.
  145. char modifiedFmt[2000];
  146. modifiedFmt[0] = '>';
  147. strcpy( modifiedFmt+1, fmt );
  148. va_list vargs;
  149. va_start(vargs, fmt);
  150. GLMPrintfVA( modifiedFmt, vargs );
  151. va_end( vargs );
  152. }
  153. ~GLMFuncLogger( )
  154. {
  155. if (m_earlyOut)
  156. {
  157. GLMPrintf( "<%s (early out)", m_funcName );
  158. }
  159. else
  160. {
  161. GLMPrintf( "<%s", m_funcName );
  162. }
  163. };
  164. void EarlyOut( void )
  165. {
  166. m_earlyOut = true;
  167. };
  168. const char *m_funcName; // set at construction time
  169. bool m_earlyOut;
  170. };
  171. // handy macro to go with the function tracking class
  172. #define GLM_FUNC GLMFuncLogger _logger_ ( __FUNCTION__ )
  173. #else
  174. #define GLM_FUNC tmZone( TELEMETRY_LEVEL1, TMZF_NONE, "%s", __FUNCTION__ )
  175. #endif
  176. // class to keep an in-memory mirror of a file which may be getting edited during run
  177. class CGLMFileMirror
  178. {
  179. public:
  180. CGLMFileMirror( char *fullpath ); // just associates mirror with file. if file exists it will be read.
  181. //if non existent it will be created with size zero
  182. ~CGLMFileMirror( );
  183. bool HasData( void ); // see if data avail
  184. void GetData( char **dataPtr, uint *dataSizePtr ); // read it out
  185. void SetData( char *data, uint dataSize ); // put data in (and write it to disk)
  186. bool PollForChanges( void ); // check disk copy. If different, read it back in and return true.
  187. void UpdateStatInfo( void ); // make sure stat info is current for our file
  188. void ReadFile( void );
  189. void WriteFile( void );
  190. void OpenInEditor( bool foreground=false ); // pass TRUE if you would like the editor to pop to foreground
  191. /// how about a "wait for change" method..
  192. char *m_path; // fullpath to file
  193. bool m_exists;
  194. struct stat m_stat; // stat results for the file (last time checked)
  195. char *m_data; // content of file
  196. uint m_size; // length of content
  197. };
  198. // class based on the file mirror, that makes it easy to edit them outside the app.
  199. // it receives an initial block of text from the engine, and hashes it. ("orig")
  200. // it munges it by duplicating all the text after the "!!" line, and appending it in commented form. ("munged")
  201. // a mirror file is activated, using a filename based on the hash from the orig text.
  202. // if there is already content on disk matching that filename, use that content *unless* the 'blitz' parameter is set.
  203. // (i.e. engine is instructing this subsystem to wipe out any old/modified variants of the text)
  204. class CGLMEditableTextItem
  205. {
  206. public:
  207. CGLMEditableTextItem( char *text, uint size, bool forceOverwrite, char *prefix, char *suffix = NULL ); // create a text blob from text source, optional filename suffix
  208. ~CGLMEditableTextItem( );
  209. bool HasData( void );
  210. bool PollForChanges( void ); // return true if stale i.e. you need to get a new edition
  211. void GetCurrentText( char **textOut, uint *sizeOut ); // query for read access to the active blob (could be the original, could be external edited copy)
  212. void OpenInEditor( bool foreground=false ); // call user attention to this text
  213. // internal methods
  214. void GenHashOfOrigText( void );
  215. void GenBaseNameAndFullPath( char *prefix, char *suffix );
  216. void GenMungedText( bool fromMirror );
  217. // members
  218. // orig
  219. uint m_origSize;
  220. char *m_origText; // what was submitted
  221. unsigned char m_origDigest[MD5_DIGEST_LENGTH]; // digest of what was submitted
  222. // munged
  223. uint m_mungedSize;
  224. char *m_mungedText; // re-processed edition, initial content submission to the file mirror
  225. // mirror
  226. char *m_mirrorBaseName; // generated from the hash of the orig text, plus the label / prefix
  227. char *m_mirrorFullPath; // base name
  228. CGLMFileMirror *m_mirror; // file mirror itself. holds "official" copy for GetCurrentText to return.
  229. };
  230. // debug font
  231. extern unsigned char g_glmDebugFontMap[16384];
  232. // class for cracking multi-part text blobs
  233. // sections are demarcated by beginning-of-line markers submitted in a table by the caller
  234. struct GLMTextSection
  235. {
  236. int m_markerIndex; // based on table of markers passed in to constructor
  237. uint m_textOffset; // where is the text - offset
  238. int m_textLength; // how big is the section
  239. };
  240. class CGLMTextSectioner
  241. {
  242. public:
  243. CGLMTextSectioner( char *text, int textSize, const char **markers ); // constructor finds all the sections
  244. ~CGLMTextSectioner( );
  245. int Count( void ); // how many sections found
  246. void GetSection( int index, uint *offsetOut, uint *lengthOut, int *markerIndexOut );
  247. // find section, size, what marker
  248. // note that more than one section can be marked similarly.
  249. // so policy isn't made here, you walk the sections and decide what to do if there are dupes.
  250. //members
  251. //section table
  252. CUtlVector< GLMTextSection > m_sectionTable;
  253. };
  254. #ifndef OSX
  255. void GLMGPUTimestampManagerInit();
  256. void GLMGPUTimestampManagerDeinit();
  257. void GLMGPUTimestampManagerTick();
  258. #endif
  259. #endif // GLMBASICS_H