Leaked source code of windows server 2003
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.

185 lines
5.2 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1997 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: d3dmem.h
  6. * Content: Direct3D memory access include file
  7. *
  8. ***************************************************************************/
  9. #ifndef _D3DMEM_H_
  10. #define _D3DMEM_H_
  11. class DIRECT3DDEVICEI;
  12. /*
  13. * Register a set of functions to be used in place of malloc, realloc
  14. * and free for memory allocation. The functions D3DMalloc, D3DRealloc
  15. * and D3DFree will use these functions. The default is to use the
  16. * ANSI C library routines malloc, realloc and free.
  17. */
  18. typedef LPVOID (*D3DMALLOCFUNCTION)(size_t);
  19. typedef LPVOID (*D3DREALLOCFUNCTION)(LPVOID, size_t);
  20. typedef VOID (*D3DFREEFUNCTION)(LPVOID);
  21. /*
  22. * Allocate size bytes of memory and return a pointer to it in *p_return.
  23. * Returns D3DERR_BADALLOC with *p_return unchanged if the allocation fails.
  24. */
  25. HRESULT D3DAPI D3DMalloc(LPVOID* p_return, size_t size);
  26. /*
  27. * Change the size of an allocated block of memory. A pointer to the
  28. * block is passed in in *p_inout. If *p_inout is NULL then a new
  29. * block is allocated. If the reallocation is successful, *p_inout is
  30. * changed to point to the new block. If the allocation fails,
  31. * *p_inout is unchanged and D3DERR_BADALLOC is returned.
  32. */
  33. HRESULT D3DAPI D3DRealloc(LPVOID* p_inout, size_t size);
  34. /*
  35. * Free a block of memory previously allocated with D3DMalloc or
  36. * D3DRealloc.
  37. */
  38. VOID D3DAPI D3DFree(LPVOID p);
  39. HRESULT MallocAligned(void** p_return, size_t size);
  40. void FreeAligned(void* p);
  41. HRESULT ReallocAligned(void** p_inout, size_t size);
  42. /* Base class for all D3D classes to use our special allocation functions everywhere */
  43. class CD3DAlloc
  44. {
  45. public:
  46. void* operator new(size_t s)
  47. {
  48. void *p;
  49. MallocAligned(&p,s);
  50. return p;
  51. };
  52. void operator delete(void* p)
  53. {
  54. FreeAligned(p);
  55. };
  56. };
  57. //---------------------------------------------------------------------
  58. // This class manages growing buffer, aligned to 32 byte boundary
  59. // Number if bytes should be power of 2.
  60. // D3DMalloc is used to allocate memory
  61. //
  62. class CAlignedBuffer32
  63. {
  64. public:
  65. CAlignedBuffer32() {size = 0; allocatedBuf = 0; alignedBuf = 0;}
  66. ~CAlignedBuffer32() {if (allocatedBuf) D3DFree(allocatedBuf);}
  67. // Returns aligned buffer address
  68. LPVOID GetAddress() {return alignedBuf;}
  69. // Returns aligned buffer size
  70. DWORD GetSize() {return size;}
  71. HRESULT Grow(DWORD dwSize);
  72. HRESULT CheckAndGrow(DWORD dwSize)
  73. {
  74. if (dwSize > size)
  75. return Grow(dwSize + 1024);
  76. else
  77. return D3D_OK;
  78. }
  79. protected:
  80. LPVOID allocatedBuf;
  81. LPVOID alignedBuf;
  82. DWORD size;
  83. };
  84. // Forward declarations
  85. class DIRECT3DDEVICEI;
  86. class CDirect3DVertexBuffer;
  87. class CDirect3DDeviceIDP2;
  88. //----------------------------------------------------------------------
  89. // This class manages a growing buffer using DDraw Surfaces.
  90. class CBufferDDS
  91. {
  92. protected:
  93. LPDIRECTDRAWSURFACE allocatedBuf;
  94. LPVOID alignedBuf;
  95. DWORD size;
  96. public:
  97. CBufferDDS()
  98. {
  99. size = 0;
  100. allocatedBuf = 0;
  101. alignedBuf = 0;
  102. }
  103. ~CBufferDDS()
  104. {
  105. if (allocatedBuf)
  106. allocatedBuf->Release();
  107. }
  108. // Returns aligned buffer address
  109. LPVOID GetAddress()
  110. {
  111. return (LPBYTE)alignedBuf;
  112. }
  113. // Returns aligned buffer size
  114. DWORD GetSize()
  115. {
  116. return size;
  117. }
  118. LPDIRECTDRAWSURFACE GetDDS()
  119. {
  120. return allocatedBuf;
  121. }
  122. HRESULT CheckAndGrow(DIRECT3DDEVICEI *lpDevI, DWORD dwSize)
  123. {
  124. if (dwSize > size)
  125. return Grow(lpDevI, dwSize + 1024);
  126. else
  127. return D3D_OK;
  128. }
  129. HRESULT Grow(DIRECT3DDEVICEI *lpDevI, DWORD dwSize);
  130. // define these later on in this file after CDirect3DVertexBuffer is defined
  131. };
  132. //----------------------------------------------------------------------
  133. // This class manages a growing vertex buffer.
  134. // Allocate it in driver friendly memory.
  135. // Do not use except for DP2 DDI
  136. class CBufferVB
  137. {
  138. protected:
  139. LPDIRECT3DVERTEXBUFFER allocatedBuf;
  140. LPVOID alignedBuf;
  141. DWORD size, base;
  142. public:
  143. CBufferVB()
  144. {
  145. size = 0;
  146. allocatedBuf = 0;
  147. alignedBuf = 0;
  148. base = 0;
  149. }
  150. ~CBufferVB()
  151. {
  152. if (allocatedBuf)
  153. allocatedBuf->Release();
  154. }
  155. // Returns aligned buffer address
  156. LPVOID GetAddress()
  157. {
  158. return (LPBYTE)alignedBuf + base;
  159. }
  160. // Returns aligned buffer size
  161. DWORD GetSize() { return size - base; }
  162. HRESULT Grow(DIRECT3DDEVICEI *lpDevI, DWORD dwSize);
  163. DWORD& Base() { return base; }
  164. // define these later on in this file after CDirect3DVertexBuffer is defined
  165. inline CDirect3DVertexBuffer* GetVBI();
  166. inline LPDIRECTDRAWSURFACE GetDDS();
  167. HRESULT CheckAndGrow(DIRECT3DDEVICEI *lpDevI, DWORD dwSize)
  168. {
  169. if (dwSize > size)
  170. return Grow(lpDevI, dwSize + 1024);
  171. else
  172. return D3D_OK;
  173. }
  174. friend CDirect3DDeviceIDP2;
  175. };
  176. #endif