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.

276 lines
9.5 KiB

  1. //============ Copyright (c) Valve Corporation, All rights reserved. ============
  2. //
  3. // cglmtex.h
  4. // GLMgr textures
  5. //
  6. //===============================================================================
  7. #ifndef CGLMTEX_H
  8. #define CGLMTEX_H
  9. #pragma once
  10. #ifdef OSX
  11. #include "glmgr/glmgrbasics.h"
  12. #endif
  13. #include "tier1/utlhash.h"
  14. #include "tier1/utlmap.h"
  15. //===============================================================================
  16. // forward declarations
  17. class GLMContext;
  18. class GLMTester;
  19. class CGLMTexLayoutTable;
  20. class CGLMTex;
  21. class CGLMFBO;
  22. class IDirect3DSurface9;
  23. //===============================================================================
  24. struct GLMTexFormatDesc
  25. {
  26. const char *m_formatSummary; // for debug visibility
  27. D3DFORMAT m_d3dFormat; // what D3D knows it as; see public/bitmap/imageformat.h
  28. GLenum m_glIntFormat; // GL internal format
  29. GLenum m_glIntFormatSRGB; // internal format if SRGB flavor
  30. GLenum m_glDataFormat; // GL data format
  31. GLenum m_glDataType; // GL data type
  32. int m_chunkSize; // 1 or 4 - 4 is used for compressed textures
  33. int m_bytesPerSquareChunk; // how many bytes for the smallest quantum (m_chunkSize x m_chunkSize)
  34. // this description lets us calculate size cleanly without conditional logic for compression
  35. };
  36. const GLMTexFormatDesc *GetFormatDesc( D3DFORMAT format );
  37. //===============================================================================
  38. // utility function for generating slabs of texels. mostly for test.
  39. typedef struct
  40. {
  41. // in
  42. D3DFORMAT m_format;
  43. void *m_dest; // dest address
  44. int m_chunkCount; // square chunk count (single texels or compressed blocks)
  45. int m_byteCountLimit; // caller expectation of max number of bytes to write out
  46. float r,g,b,a; // color desired
  47. // out
  48. int m_bytesWritten;
  49. } GLMGenTexelParams;
  50. // return true if successful
  51. bool GLMGenTexels( GLMGenTexelParams *params );
  52. //===============================================================================
  53. struct GLMTexLayoutSlice
  54. {
  55. int m_xSize,m_ySize,m_zSize; //texel dimensions of this slice
  56. int m_storageOffset; //where in the storage slab does this slice live
  57. int m_storageSize; //how much storage does this slice occupy
  58. };
  59. enum EGLMTexFlags
  60. {
  61. kGLMTexMipped = 0x01,
  62. kGLMTexMippedAuto = 0x02,
  63. kGLMTexRenderable = 0x04,
  64. kGLMTexIsStencil = 0x08,
  65. kGLMTexIsDepth = 0x10,
  66. kGLMTexSRGB = 0x20,
  67. kGLMTexUnused = 0x40, // UNUSED
  68. kGLMTexMultisampled = 0x80, // has an RBO backing it. Cannot combine with Mipped, MippedAuto. One slice maximum, only targeting GL_TEXTURE_2D.
  69. // actually not 100% positive on the mipmapping, the RBO itself can't be mipped, but the resulting texture could
  70. // have mipmaps generated.
  71. };
  72. //===============================================================================
  73. struct GLMTexLayoutKey
  74. {
  75. // input values: held const, these are the hash key for the form map
  76. GLenum m_texGLTarget; // flavor of texture: GL_TEXTURE_2D, GL_TEXTURE_3D, GLTEXTURE_CUBE_MAP
  77. D3DFORMAT m_texFormat; // D3D texel format
  78. unsigned long m_texFlags; // mipped, autogen mips, render target, ... ?
  79. unsigned long m_texSamples; // zero for a plain tex, 2/4/6/8 for "MSAA tex" (RBO backed)
  80. int m_xSize,m_ySize,m_zSize; // size of base mip
  81. };
  82. bool LessFunc_GLMTexLayoutKey( const GLMTexLayoutKey &a, const GLMTexLayoutKey &b );
  83. #define GLM_TEX_MAX_MIPS 14
  84. #define GLM_TEX_MAX_FACES 6
  85. #define GLM_TEX_MAX_SLICES (GLM_TEX_MAX_MIPS * GLM_TEX_MAX_FACES)
  86. struct GLMTexLayout
  87. {
  88. char *m_layoutSummary; // for debug visibility
  89. // const inputs used for hashing
  90. GLMTexLayoutKey m_key;
  91. // refcount
  92. int m_refCount;
  93. // derived values:
  94. GLMTexFormatDesc *m_format; // format specific info
  95. int m_mipCount; // derived by starying at base size and working down towards 1x1
  96. int m_faceCount; // 1 for 2d/3d, 6 for cubemap
  97. int m_sliceCount; // product of faces and mips
  98. int m_storageTotalSize; // size of storage slab required
  99. // slice array
  100. GLMTexLayoutSlice m_slices[0]; // dynamically allocated 2-d array [faces][mips]
  101. };
  102. class CGLMTexLayoutTable
  103. {
  104. public:
  105. CGLMTexLayoutTable();
  106. GLMTexLayout *NewLayoutRef( GLMTexLayoutKey *key ); // pass in a pointer to layout key - receive ptr to completed layout
  107. void DelLayoutRef( GLMTexLayout *layout ); // pass in pointer to completed layout. refcount is dropped.
  108. void DumpStats( void );
  109. protected:
  110. CUtlMap< GLMTexLayoutKey, GLMTexLayout* > m_layoutMap;
  111. };
  112. //===============================================================================
  113. // a sampler specifies desired state for drawing on a given sampler index
  114. // this is the combination of a texture choice and a set of sampler parameters
  115. // see http://msdn.microsoft.com/en-us/library/bb172602(VS.85).aspx
  116. struct GLMTexSamplingParams
  117. {
  118. GLenum m_addressModes[3]; // S, T, R
  119. GLfloat m_borderColor[4]; // R,G,B,A
  120. GLenum m_magFilter;
  121. GLenum m_minFilter;
  122. GLfloat m_mipmapBias;
  123. GLint m_minMipLevel;
  124. GLint m_maxMipLevel;
  125. GLint m_maxAniso;
  126. GLenum m_compareMode; // only used for depth and stencil type textures
  127. bool m_srgb; // srgb texture read...
  128. };
  129. struct GLMTexLockParams
  130. {
  131. // input params which identify the slice of interest
  132. CGLMTex *m_tex;
  133. int m_face;
  134. int m_mip;
  135. // identifies the region of the slice
  136. GLMRegion m_region;
  137. // tells GLM to force re-read of the texels back from GL
  138. // i.e. "I know I stepped on those texels with a draw or blit - the GLM copy is stale"
  139. bool m_readback;
  140. };
  141. struct GLMTexLockDesc
  142. {
  143. GLMTexLockParams m_req; // form of the lock request
  144. bool m_active; // set true at lock time. cleared at unlock time.
  145. int m_sliceIndex; // which slice in the layout
  146. int m_sliceBaseOffset; // where is that in the texture data
  147. int m_sliceRegionOffset; // offset to the start (lowest address corner) of the region requested
  148. };
  149. //===============================================================================
  150. #define GLM_SAMPLER_COUNT 16
  151. typedef CBitVec<GLM_SAMPLER_COUNT> CTexBindMask;
  152. enum EGLMTexSliceFlag
  153. {
  154. kSliceValid = 0x01, // slice has been teximage'd in whole at least once - set to 0 initially
  155. kSliceStorageValid = 0x02, // if backing store is available, this slice's data is a valid copy - set to 0 initially
  156. kSliceLocked = 0x04, // are one or more locks outstanding on this slice
  157. kSliceFullyDirty = 0x08, // does the slice need to be fully downloaded at unlock time (disregard dirty rects)
  158. };
  159. class CGLMTex
  160. {
  161. public:
  162. void Lock( GLMTexLockParams *params, char** addressOut, int* yStrideOut, int *zStrideOut );
  163. void Unlock( GLMTexLockParams *params );
  164. protected:
  165. friend class GLMContext; // only GLMContext can make CGLMTex objects
  166. friend class GLMTester;
  167. friend class CGLMFBO;
  168. friend class IDirect3DDevice9;
  169. friend class IDirect3DBaseTexture9;
  170. friend class IDirect3DTexture9;
  171. friend class IDirect3DSurface9;
  172. friend class IDirect3DCubeTexture9;
  173. friend class IDirect3DVolumeTexture9;
  174. CGLMTex( GLMContext *ctx, GLMTexLayout *layout, GLMTexSamplingParams *sampling, const char *debugLabel = NULL );
  175. ~CGLMTex( );
  176. int CalcSliceIndex( int face, int mip );
  177. void CalcTexelDataOffsetAndStrides( int sliceIndex, int x, int y, int z, int *offsetOut, int *yStrideOut, int *zStrideOut );
  178. void ApplySamplingParams( GLMTexSamplingParams *params, bool noCheck=FALSE );
  179. void ReadTexels( GLMTexLockDesc *desc, bool readWholeSlice=true );
  180. void WriteTexels( GLMTexLockDesc *desc, bool writeWholeSlice=true, bool noDataWrite=false );
  181. // last param lets us send NULL data ptr (only legal with uncompressed formats, beware)
  182. // this helps out ResetSRGB.
  183. void ResetSRGB( bool srgb, bool noDataWrite );
  184. // re-specify texture format to match desired sRGB form
  185. // noWrite means send NULL for texel source addresses instead of actual data - ideal for RT's
  186. GLMTexLayout *m_layout; // layout of texture (shared across all tex with same layout)
  187. int m_minActiveMip;//index of lowest mip that has been written. used to drive setting of GL_TEXTURE_MAX_LEVEL.
  188. int m_maxActiveMip;//index of highest mip that has been written. used to drive setting of GL_TEXTURE_MAX_LEVEL.
  189. GLMTexSamplingParams m_sampling; // mirror of sampling params currently embodied in the texture
  190. // (consult this at draw time, in order to know if changes need to be made)
  191. GLMContext *m_ctx; // link back to parent context
  192. GLuint m_texName; // name of this texture in the context
  193. bool m_texClientStorage; // was CS selecetd for texture
  194. bool m_texPreloaded; // has it been kicked into VRAM with GLMContext::PreloadTex yet
  195. GLuint m_rboName; // name of MSAA RBO backing the tex if MSAA enabled (or zero)
  196. bool m_rboDirty; // has RBO been drawn on - i.e. needs to be blitted back to texture if texture is going to be sampled from
  197. CTexBindMask m_bindPoints; // true for each place in the parent ctx where currently
  198. // bound (indexed via EGLMTexCtxBindingIndex)
  199. int m_rtAttachCount; // how many RT's have this texture attached somewhere
  200. char *m_backing; // backing storage if available
  201. int m_lockCount; // lock reqs are stored in the GLMContext for tracking
  202. CUtlVector<unsigned char> m_sliceFlags;
  203. char *m_debugLabel; // strdup() of debugLabel passed in, or NULL
  204. };
  205. #endif