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.

424 lines
16 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef WORLDSTRUCTURES_H
  9. #define WORLDSTRUCTURES_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #define WORLD_FILE_VERSION 101
  14. #include "tier0/platform.h"
  15. #include "rendersystem/irenderdevice.h"
  16. #include "rendersystem/irendercontext.h"
  17. #include "bvhsupport.h"
  18. //--------------------------------------------------------------------------------------
  19. // Chunk types
  20. //--------------------------------------------------------------------------------------
  21. enum BVHChunkType_t
  22. {
  23. // Client-only resources
  24. // GPU resources
  25. CHUNK_TYPE_RESOURCE_DICTIONARY = 0, // Resource dictionary
  26. CHUNK_TYPE_ENTITIES, // Entity lump
  27. CHUNK_TYPE_HEIRARCHY, // Heirarchy
  28. CHUNK_TYPE_VISIBILITY, // Visibility vectors
  29. MAX_CHUNK_TYPES
  30. };
  31. //--------------------------------------------------------------------------------------
  32. // Generic chunk descriptor
  33. //--------------------------------------------------------------------------------------
  34. struct BVHChunkDescriptor_t
  35. {
  36. DECLARE_BYTESWAP_DATADESC();
  37. BVHChunkType_t m_nChunkType; // type for the chunk
  38. uint64 m_nOffset; // Offset from the beginning of the file to this chunk
  39. uint64 m_nSize; // Number of bytes taken up
  40. };
  41. //--------------------------------------------------------------------------------------
  42. // Fake-material related
  43. //--------------------------------------------------------------------------------------
  44. enum ShaderComboVariation_t
  45. {
  46. VARIATION_DEFAULT = 0, // business as usual
  47. VARIATION_DEPTH, // depth only
  48. VARIATION_NORM_DEPTH_SPEC, // normal and specular power
  49. VARIATION_SAMPLE_LIGHTING, // sampling lighting
  50. VARIATION_SAMPLE_LIGHTING_LOW_TEXTURE, // sampling lighting and only add a little texture in
  51. // Tools modes
  52. VARIATION_BAKE_ALL, // bake albedo, normal, and specular
  53. VARIATION_RENDER_UV, // Render position and normal to UV space
  54. MAX_VARIATIONS
  55. };
  56. #define MAX_RUNTIME_VARIATIONS VARIATION_BAKE_ALL
  57. //--------------------------------------------------------------------------------------
  58. // Resource related
  59. //--------------------------------------------------------------------------------------
  60. enum BVHResourceType_t
  61. {
  62. // Client-only resources
  63. // GPU resources
  64. BVH_VERTEX_BUFFER = 0,
  65. BVH_CONSTANT_BUFFER,
  66. BVH_INDEX_BUFFER,
  67. BVH_TEXTURE,
  68. BVH_PRECOMP_CMD_BUFFER, // 360-only for now
  69. // Cutoff for client-only resources
  70. BVH_MAX_CLIENT_ONLY_RESOURCES = 16, // Make this a hard line in the sand?
  71. // Non-client-only resources
  72. BVH_MATERIAL, // Fake material class
  73. BVH_KDTREE, // KD-tree for raycasts (normally stored in node0)
  74. BVH_POINTLIGHT_DATA, // pointlight data
  75. BVH_HEMILIGHT_DATA, // hemilight data
  76. BVH_SPOTLIGHT_DATA, // spotlight data
  77. BVH_PVS,
  78. BVH_SOMETHING_ELSE,
  79. };
  80. //--------------------------------------------------------------------------------------
  81. // use this buffer desc instead of BufferDesc_t because BufferDesc_t has pointers
  82. // that won't serialized consistently between 32 and 64bits
  83. //--------------------------------------------------------------------------------------
  84. struct BVHBufferDesc_t
  85. {
  86. DECLARE_BYTESWAP_DATADESC();
  87. RenderBufferType_t m_nBufferType;
  88. int32 m_nElementCount; // Number of vertices/indices
  89. int32 m_nElementSizeInBytes; // Size of a single vertex/index
  90. };
  91. //--------------------------------------------------------------------------------------
  92. // Dictionary related
  93. //--------------------------------------------------------------------------------------
  94. enum BVHDictionaryEntryFlags_t
  95. {
  96. NON_PAGEABLE = 0x01, // Resource can never be evicted once loaded
  97. USE_SYS_MEMORY = 0x02, // Use system memory to fulfill this request
  98. USE_INSTANCE_DATA = 0x04, // Instance data is in this resource
  99. //USE_GPU_MEMORY = 0x04, // Use GPU visible memory to fulfill this request
  100. USE_TRANSIENT_MEMORY = 0x08, // Use transient memory ( reclaimed system memory )
  101. };
  102. #define MAX_RESOURCE_NAME 64
  103. class CBVHDictionaryEntry
  104. {
  105. public:
  106. DECLARE_BYTESWAP_DATADESC();
  107. CBVHDictionaryEntry() :
  108. m_nRefCount( 0 )
  109. {
  110. }
  111. ~CBVHDictionaryEntry()
  112. {
  113. }
  114. int AddRef()
  115. {
  116. return ++m_nRefCount;
  117. }
  118. int Release()
  119. {
  120. m_nRefCount--;
  121. if ( m_nRefCount == 0 )
  122. {
  123. // TODO: do we need to do anything here?
  124. }
  125. return m_nRefCount;
  126. }
  127. int GetRefCount()
  128. {
  129. return m_nRefCount;
  130. }
  131. uint8 GetFlags() { return m_Flags; }
  132. uint64 GetOffset() const { return m_ChunkDesc.m_nOffset; }
  133. uint64 GetSize() { return m_ChunkDesc.m_nSize; }
  134. int GetResourceType() { return m_nResourceType; }
  135. char *GetName() { return m_pName; }
  136. bool GetInstanced() { return m_bInstanceData; }
  137. void SetFlags( unsigned char Flags ) { m_Flags = Flags; }
  138. void SetOffset( uint64 offset ) { m_ChunkDesc.m_nOffset = offset; }
  139. void SetSize( uint64 size ) { m_ChunkDesc.m_nSize = size; }
  140. void SetResourceType( int type ) { m_nResourceType = type; }
  141. void SetName( char *pName ) { Q_strncpy( m_pName, pName, MAX_RESOURCE_NAME ); }
  142. void SetInstanced( bool bInstanced ) { m_bInstanceData = bInstanced; }
  143. private:
  144. BVHChunkDescriptor_t m_ChunkDesc; // Chunk descriptor for the mapping in file
  145. int32 m_nRefCount; // Reference count
  146. uint32 m_nLastFrameUsed; // The last frame in which this resource was used
  147. int32 m_nResourceType; // of type BVHResourceType_t
  148. char m_pName[MAX_RESOURCE_NAME]; // unique name to help identify this resource or its origin
  149. uint8 m_Flags; // of type DictionaryEntryFlags_t
  150. bool m_bInstanceData; // is this data instanced?
  151. uint8 m_padding[2]; // pad the struct out to a multiple of 4 bytes
  152. };
  153. #define MAX_PAGE_FILE_NAME 64
  154. struct BVHResourceDictionaryHeader_t
  155. {
  156. DECLARE_BYTESWAP_DATADESC();
  157. int32 m_nInputLayouts; // Number of pre-defined input layouts for this map
  158. int32 m_nResources; // Number of instanced resources
  159. char m_pPageFile[MAX_PAGE_FILE_NAME]; // name of our page file
  160. };
  161. //--------------------------------------------------------------------------------------
  162. // Input layout
  163. //--------------------------------------------------------------------------------------
  164. struct BVHInputLayoutDesc_t
  165. {
  166. DECLARE_BYTESWAP_DATADESC();
  167. char m_pName[RENDER_INPUT_LAYOUT_FIELD_SEMANTIC_NAME_SIZE];
  168. int32 m_nFields;
  169. union
  170. {
  171. RenderInputLayoutField_t *m_pFields;
  172. uint64 m_64Bits; // force to 64bits
  173. };
  174. };
  175. //--------------------------------------------------------------------------------------
  176. // Draw-call related
  177. //--------------------------------------------------------------------------------------
  178. struct BVHResourceBinding_t
  179. {
  180. DECLARE_BYTESWAP_DATADESC();
  181. int32 m_nResourceIndex; // Which resource in the resource dictionary to bind
  182. int32 m_nBindOffset; // Byte offset for binding (only applicable for buffers)
  183. int32 m_nElementStride; // Element stride (only applicable for buffers) (byte?, short?)
  184. uint8 m_cBindStage; // Which stage to bind to (IA,SHADER)
  185. uint8 m_cBindSlot;
  186. uint8 m_padding[2]; // pad this structure out to a multiple of 4 bytes
  187. };
  188. enum BVHDrawCallFlags_t
  189. {
  190. DRAW_WHEN_NODE_LOADED = 0x00000001, // Wait for the entire node to load before drawing. Mutex with ASAP.
  191. DRAW_ASAP = 0x00000002, // Draw as soon as we're loaded. Don't wait for anything.
  192. DRAW_IF_PREVIOUS_DREW = 0x00000004, // Only draw if our previous draw happened
  193. DRAW_TRANSPARENT = 0x00000008, // We have transparency
  194. DRAW_WHEN_CAMERA_INSIDE = 0x00000010, // Only draw when the camera is in this node
  195. DRAW_ALWAYS = 0x00000020, // Draw if any of the traversal hits this node
  196. DRAW_CULL = 0x00000040, // Cull the draw individually using the draw's bounding box
  197. DRAW_LIGHT = 0x00000080, // The draw call is bounding light geometry
  198. };
  199. enum BVHBindStage_t
  200. {
  201. BIND_STAGE_MATERIAL = 0x0,
  202. BIND_STAGE_SHADER,
  203. BIND_STAGE_IA,
  204. };
  205. class CBVHDrawCall
  206. {
  207. public:
  208. bool Issue(); // issue the draw call
  209. int32 GetFlags() { return m_Flags; }
  210. int32 GetInputLayout() { return m_nInputLayout; }
  211. int32 GetNumResourceBindings() { return m_nResourceBindings; }
  212. public:
  213. DECLARE_BYTESWAP_DATADESC();
  214. int32 m_Flags; // One of BVHDrawCallFlags_t
  215. AABB_t m_Bounds; // Bounding box for culling
  216. int32 m_nInputLayout; // Index for the pre-defined input layout for the vertices
  217. int32 m_nResourceBindings; // Number of resource bindings
  218. // Draw call data
  219. RenderPrimitiveType_t m_nPrimitiveType; // Type of primitive to draw
  220. int32 m_nBaseVertex; // Base vertex to use when rendering
  221. int32 m_nVertexCount; // Number of vertices
  222. int32 m_nStartIndex; // First index to use
  223. int32 m_nIndexCount; // Number of indices to draw ( or index count per instance if instancing )
  224. int32 m_nStartInstance; // Location of the first instance
  225. int32 m_nInstanceCount; // Number of instances ( if 0, instancing is disabled )
  226. // Binding pointer stored in-file
  227. union // TODO: are nameless unions ok in gcc?
  228. {
  229. BVHResourceBinding_t *m_pResourceBindings; // Resource bindings
  230. uint64 m_64Bits; // pad to 64bits
  231. };
  232. };
  233. //--------------------------------------------------------------------------------------
  234. // BVHNode related
  235. //--------------------------------------------------------------------------------------
  236. enum BVHNodeFlags_t
  237. {
  238. NODE_SIMPLIFIED = 0x0001, // The geometry is simplified
  239. NODE_UNIQUE_UV = 0x0002, // The geometry is uniquely mapped (likely, we're a higher LOD)
  240. NODE_ATLASED = 0x0004, // This node was atlased but not uniquely mapped
  241. NODE_KDTREE = 0x0008, // Node contains a kd-tree for raycasts
  242. NODE_NODRAW = 0x0010, // Node has no actual draw calls... it's just a container for stuff and other nodes
  243. NODE_START_TRAVERSAL = 0x0020, // Start a traversal at this node (add a check to ensure that the KDTREE flag also exists with this one)
  244. NODE_CAN_SEE_SKY = 0x0040, // Can this node see the sky?
  245. NODE_MOST_DETAILED = 0x0080, // Node is the most detailed node containing the original geometry and textures
  246. };
  247. struct BVHNodeHeader_t
  248. {
  249. DECLARE_BYTESWAP_DATADESC();
  250. int32 m_nID; // Node ID
  251. int32 m_Flags; // One of BVHNodeFlags_t
  252. // hierarchy
  253. int32 m_nParent; // Parent node
  254. TiledPosition_t m_Origin; // Tiled-position origin placing us in the world
  255. AABB_t m_Bounds; // Axis-aligned bounding-box (pull out and vectorize?)
  256. float m_flMinimumDistance; // Minimum camera distance at which this node renders (pull out and vectorize?)
  257. int32 m_nChildren; // Number of child nodes
  258. // resources
  259. int32 m_nResources; // Number of resources needed by this node (int16?)
  260. int32 m_nDrawCalls; // Number of draw calls (int16?)
  261. };
  262. //--------------------------------------------------------------------------------------
  263. // World related
  264. //--------------------------------------------------------------------------------------
  265. struct BVHBuilderParams_t
  266. {
  267. DECLARE_BYTESWAP_DATADESC();
  268. int32 m_nSizeBytesPerVoxel; // target size per-voxel
  269. float m_flMinDrawVolumeSize; // minimum size of any draw call
  270. float m_flMinDistToCamera; // minimum distance to camera for near objects
  271. float m_flMinAtlasDist; // minimum distance at which any atlased node can be visible
  272. float m_flMinSimplifiedDist; // minimum distance at which any simplified node can be visible
  273. float m_flHorzFOV; // horizontal fov used for texel to screenspace calcs
  274. float m_flHalfScreenWidth; // half target screen res used for texel to screenspace calcs
  275. int32 m_nAtlasTextureSizeX; // X res of atlas textures
  276. int32 m_nAtlasTextureSizeY; // Y res of atlas textures
  277. int32 m_nUniqueTextureSizeX; // X res of uniquely atlased textures
  278. int32 m_nUniqueTextureSizeY; // Y res of uniquely atlased textures
  279. int32 m_nCompressedAtlasSize; // approx size of a compressed atlas texture
  280. float m_flGutterSize; // gutter size (in texels)
  281. float m_flUVMapThreshold; // cos( angle ) threshold between faces when creating a unique uv parameterization
  282. Vector m_vWorldUnitsPerTile; // world units per tile for tiled coordinates
  283. int32 m_nMaxTexScaleSlots; // maximum number of gpu registers we can take up with texture scaling
  284. bool m_bWrapInAtlas; // true == handle wrapping texcoords by tiling the texture in the atlas
  285. // false == handle wrapping by a frac in the pixel shader
  286. uint8 m_padding[3]; // pad this structure out to a mutiple of 4 bytes
  287. };
  288. //--------------------------------------------------------------------------------------
  289. // File header
  290. //--------------------------------------------------------------------------------------
  291. struct WorldFileHeader_t
  292. {
  293. DECLARE_BYTESWAP_DATADESC();
  294. uint32 m_nFileVersion; // Versioning
  295. Vector m_vWorldUnitsPerTile; // World units per-tile that this map was built against
  296. int32 m_nChunks; // Number of chunks
  297. BVHBuilderParams_t m_BuilderParams; // Original build parameters ( so we can potentially remake this file )
  298. };
  299. //--------------------------------------------------------------------------------------
  300. // Known chunk headers
  301. //--------------------------------------------------------------------------------------
  302. struct HierarchyChunkHeader_t
  303. {
  304. DECLARE_BYTESWAP_DATADESC();
  305. int32 m_nNodes; // Number of nodes in this world
  306. int32 m_nMaxNodeSizeBytes; // Maximum size of a node in bytes (Average instead?)
  307. int32 m_nAvgNodeSizeBytes; // Average size of a node in bytes (Average instead?)
  308. };
  309. struct EntityChunkHeader_t
  310. {
  311. DECLARE_BYTESWAP_DATADESC();
  312. int32 m_nEntities; // Entity count
  313. };
  314. struct VisibilityChunkHeader_t
  315. {
  316. DECLARE_BYTESWAP_DATADESC();
  317. int32 m_nNodes; // Num nodes accounted for
  318. int32 m_nDWORDS; // Num dwords per visibility chunk
  319. int32 m_nX; // Num grid points in X
  320. int32 m_nY; // Num grid points in Y
  321. int32 m_nZ; // Num grid points in Z
  322. Vector m_vCellSize; // Cell size
  323. Vector m_vStart; // Cell start
  324. };
  325. struct RenderInputLayoutFieldProxy_t
  326. {
  327. DECLARE_BYTESWAP_DATADESC();
  328. char m_pSemanticName[RENDER_INPUT_LAYOUT_FIELD_SEMANTIC_NAME_SIZE];
  329. int m_nSemanticIndex;
  330. ColorFormat_t m_Format;
  331. int m_nOffset;
  332. int m_nSlot;
  333. RenderSlotType_t m_nSlotType;
  334. int m_nInstanceStepRate;
  335. };
  336. //--------------------------------------------------------------------------------------
  337. // Light data
  338. //--------------------------------------------------------------------------------------
  339. struct PointLightData_t
  340. {
  341. Vector m_vOrigin;
  342. Vector4D m_vColorNRadius;
  343. Vector m_vAttenuation;
  344. };
  345. struct HemiLightData_t
  346. {
  347. Vector4D m_vTransform0; // Direction is z column
  348. Vector4D m_vTransform1; // Direction is z column
  349. Vector4D m_vTransform2; // Direction is z column
  350. Vector4D m_vColorNRadius;
  351. Vector m_vAttenuation;
  352. };
  353. struct SpotLightData_t
  354. {
  355. Vector4D m_vTransform0; // Direction is z column
  356. Vector4D m_vTransform1; // Direction is z column
  357. Vector4D m_vTransform2; // Direction is z column
  358. Vector4D m_vColorNRadius;
  359. Vector4D m_vAttenuationNCosSpot;
  360. };
  361. #endif