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.

282 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #ifndef MESHDX10_H
  9. #define MESHDX10_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "meshbase.h"
  14. #include "shaderapi/ishaderdevice.h"
  15. //-----------------------------------------------------------------------------
  16. // Forward declaration
  17. //-----------------------------------------------------------------------------
  18. struct ID3D10Buffer;
  19. //-----------------------------------------------------------------------------
  20. // Dx10 implementation of a vertex buffer
  21. //-----------------------------------------------------------------------------
  22. class CVertexBufferDx10 : public CVertexBufferBase
  23. {
  24. typedef CVertexBufferBase BaseClass;
  25. // Methods of IVertexBuffer
  26. public:
  27. virtual int VertexCount() const;
  28. virtual VertexFormat_t GetVertexFormat() const;
  29. virtual bool Lock( int nMaxVertexCount, bool bAppend, VertexDesc_t &desc );
  30. virtual void Unlock( int nWrittenVertexCount, VertexDesc_t &desc );
  31. virtual bool IsDynamic() const;
  32. virtual void BeginCastBuffer( VertexFormat_t format );
  33. virtual void EndCastBuffer( );
  34. virtual int GetRoomRemaining() const;
  35. // Other public methods
  36. public:
  37. // constructor, destructor
  38. CVertexBufferDx10( ShaderBufferType_t type, VertexFormat_t fmt, int nVertexCount, const char *pBudgetGroupName );
  39. virtual ~CVertexBufferDx10();
  40. ID3D10Buffer* GetDx10Buffer() const;
  41. int VertexSize() const;
  42. // Only used by dynamic buffers, indicates the next lock should perform a discard.
  43. void Flush();
  44. protected:
  45. // Creates, destroys the index buffer
  46. bool Allocate( );
  47. void Free();
  48. ID3D10Buffer *m_pVertexBuffer;
  49. VertexFormat_t m_VertexFormat;
  50. int m_nVertexCount;
  51. int m_nBufferSize;
  52. int m_nFirstUnwrittenOffset;
  53. bool m_bIsLocked : 1;
  54. bool m_bIsDynamic : 1;
  55. bool m_bFlush : 1; // Used only for dynamic buffers, indicates to discard the next time
  56. #ifdef _DEBUG
  57. static int s_nBufferCount;
  58. #endif
  59. };
  60. //-----------------------------------------------------------------------------
  61. // inline methods for CVertexBufferDx10
  62. //-----------------------------------------------------------------------------
  63. inline ID3D10Buffer* CVertexBufferDx10::GetDx10Buffer() const
  64. {
  65. return m_pVertexBuffer;
  66. }
  67. inline int CVertexBufferDx10::VertexSize() const
  68. {
  69. return VertexFormatSize( m_VertexFormat );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Dx10 implementation of an index buffer
  73. //-----------------------------------------------------------------------------
  74. class CIndexBufferDx10 : public CIndexBufferBase
  75. {
  76. typedef CIndexBufferBase BaseClass;
  77. // Methods of IIndexBuffer
  78. public:
  79. virtual int IndexCount() const;
  80. virtual MaterialIndexFormat_t IndexFormat() const;
  81. virtual int GetRoomRemaining() const;
  82. virtual bool Lock( int nMaxIndexCount, bool bAppend, IndexDesc_t &desc );
  83. virtual void Unlock( int nWrittenIndexCount, IndexDesc_t &desc );
  84. virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc );
  85. virtual void ModifyEnd( IndexDesc_t& desc );
  86. virtual bool IsDynamic() const;
  87. virtual void BeginCastBuffer( MaterialIndexFormat_t format );
  88. virtual void EndCastBuffer( );
  89. // Other public methods
  90. public:
  91. // constructor, destructor
  92. CIndexBufferDx10( ShaderBufferType_t type, MaterialIndexFormat_t fmt, int nIndexCount, const char *pBudgetGroupName );
  93. virtual ~CIndexBufferDx10();
  94. ID3D10Buffer* GetDx10Buffer() const;
  95. MaterialIndexFormat_t GetIndexFormat() const;
  96. // Only used by dynamic buffers, indicates the next lock should perform a discard.
  97. void Flush();
  98. protected:
  99. // Creates, destroys the index buffer
  100. bool Allocate( );
  101. void Free();
  102. // Returns the size of the index in bytes
  103. int IndexSize() const;
  104. ID3D10Buffer *m_pIndexBuffer;
  105. MaterialIndexFormat_t m_IndexFormat;
  106. int m_nIndexCount;
  107. int m_nBufferSize;
  108. int m_nFirstUnwrittenOffset; // Used only for dynamic buffers, indicates where it's safe to write (nooverwrite)
  109. bool m_bIsLocked : 1;
  110. bool m_bIsDynamic : 1;
  111. bool m_bFlush : 1; // Used only for dynamic buffers, indicates to discard the next time
  112. #ifdef _DEBUG
  113. static int s_nBufferCount;
  114. #endif
  115. };
  116. //-----------------------------------------------------------------------------
  117. // Returns the size of the index in bytes
  118. //-----------------------------------------------------------------------------
  119. inline int CIndexBufferDx10::IndexSize() const
  120. {
  121. switch( m_IndexFormat )
  122. {
  123. default:
  124. case MATERIAL_INDEX_FORMAT_UNKNOWN:
  125. return 0;
  126. case MATERIAL_INDEX_FORMAT_16BIT:
  127. return 2;
  128. case MATERIAL_INDEX_FORMAT_32BIT:
  129. return 4;
  130. }
  131. }
  132. inline ID3D10Buffer* CIndexBufferDx10::GetDx10Buffer() const
  133. {
  134. return m_pIndexBuffer;
  135. }
  136. inline MaterialIndexFormat_t CIndexBufferDx10::GetIndexFormat() const
  137. {
  138. return m_IndexFormat;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Dx10 implementation of a mesh
  142. //-----------------------------------------------------------------------------
  143. class CMeshDx10 : public CMeshBase
  144. {
  145. public:
  146. CMeshDx10();
  147. virtual ~CMeshDx10();
  148. // FIXME: Make this work! Unsupported methods of IIndexBuffer
  149. virtual bool Lock( int nMaxIndexCount, bool bAppend, IndexDesc_t& desc ) { Assert(0); return false; }
  150. virtual void Unlock( int nWrittenIndexCount, IndexDesc_t& desc ) { Assert(0); }
  151. virtual int GetRoomRemaining() const { Assert(0); return 0; }
  152. virtual void ModifyBegin( bool bReadOnly, int nFirstIndex, int nIndexCount, IndexDesc_t& desc ) { Assert(0); }
  153. virtual void ModifyEnd( IndexDesc_t& desc ) { Assert(0); }
  154. virtual void Spew( int nIndexCount, const IndexDesc_t & desc ) { Assert(0); }
  155. virtual void ValidateData( int nIndexCount, const IndexDesc_t &desc ) { Assert(0); }
  156. virtual bool Lock( int nVertexCount, bool bAppend, VertexDesc_t &desc ) { Assert(0); return false; }
  157. virtual void Unlock( int nVertexCount, VertexDesc_t &desc ) { Assert(0); }
  158. virtual void Spew( int nVertexCount, const VertexDesc_t &desc ) { Assert(0); }
  159. virtual void ValidateData( int nVertexCount, const VertexDesc_t & desc ) { Assert(0); }
  160. virtual bool IsDynamic() const { Assert(0); return false; }
  161. virtual void BeginCastBuffer( MaterialIndexFormat_t format ) { Assert(0); }
  162. virtual void BeginCastBuffer( VertexFormat_t format ) { Assert(0); }
  163. virtual void EndCastBuffer( ) { Assert(0); }
  164. void LockMesh( int numVerts, int numIndices, MeshDesc_t& desc );
  165. void UnlockMesh( int numVerts, int numIndices, MeshDesc_t& desc );
  166. void ModifyBeginEx( bool bReadOnly, int firstVertex, int numVerts, int firstIndex, int numIndices, MeshDesc_t& desc );
  167. void ModifyBegin( int firstVertex, int numVerts, int firstIndex, int numIndices, MeshDesc_t& desc );
  168. void ModifyEnd( MeshDesc_t& desc );
  169. // returns the # of vertices (static meshes only)
  170. int VertexCount() const;
  171. virtual void BeginPass( ) {}
  172. virtual void RenderPass() {}
  173. virtual bool HasColorMesh() const { return false; }
  174. virtual bool IsUsingMorphData() const { return false; }
  175. virtual bool HasFlexMesh() const { return false; }
  176. // Sets the primitive type
  177. void SetPrimitiveType( MaterialPrimitiveType_t type );
  178. // Draws the entire mesh
  179. void Draw(int firstIndex, int numIndices);
  180. void Draw(CPrimList *pPrims, int nPrims);
  181. // Copy verts and/or indices to a mesh builder. This only works for temp meshes!
  182. virtual void CopyToMeshBuilder(
  183. int iStartVert, // Which vertices to copy.
  184. int nVerts,
  185. int iStartIndex, // Which indices to copy.
  186. int nIndices,
  187. int indexOffset, // This is added to each index.
  188. CMeshBuilder &builder );
  189. // Spews the mesh data
  190. void Spew( int numVerts, int numIndices, const MeshDesc_t & desc );
  191. void ValidateData( int numVerts, int numIndices, const MeshDesc_t & desc );
  192. // gets the associated material
  193. IMaterial* GetMaterial();
  194. void SetColorMesh( IMesh *pColorMesh, int nVertexOffset )
  195. {
  196. }
  197. virtual int IndexCount() const
  198. {
  199. return 0;
  200. }
  201. virtual MaterialIndexFormat_t IndexFormat() const
  202. {
  203. Assert( 0 );
  204. return MATERIAL_INDEX_FORMAT_UNKNOWN;
  205. }
  206. virtual void SetFlexMesh( IMesh *pMesh, int nVertexOffset ) {}
  207. virtual void DisableFlexMesh() {}
  208. virtual void MarkAsDrawn() {}
  209. virtual unsigned ComputeMemoryUsed() { return 0; }
  210. virtual VertexFormat_t GetVertexFormat() const { return VERTEX_POSITION; }
  211. virtual IMesh *GetMesh()
  212. {
  213. return this;
  214. }
  215. private:
  216. enum
  217. {
  218. VERTEX_BUFFER_SIZE = 1024 * 1024
  219. };
  220. unsigned char* m_pVertexMemory;
  221. };
  222. #endif // MESHDX10_H