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.

254 lines
7.2 KiB

  1. /*==========================================================================;
  2. *
  3. * Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: buffer.cpp
  6. * Content: Implementation of the CBuffer class.
  7. *
  8. *
  9. ***************************************************************************/
  10. #include "ddrawpr.h"
  11. #include "buffer.hpp"
  12. #undef DPF_MODNAME
  13. #define DPF_MODNAME "CBuffer::CBuffer"
  14. // Constructor returns an error code
  15. // if the object could not be fully
  16. // constructed
  17. CBuffer::CBuffer(CBaseDevice *pDevice,
  18. DWORD cbLength,
  19. DWORD dwFVF,
  20. D3DFORMAT Format,
  21. D3DRESOURCETYPE Type,
  22. DWORD dwUsage,
  23. DWORD dwActualUsage,
  24. D3DPOOL Pool,
  25. D3DPOOL ActualPool,
  26. REF_TYPE refType,
  27. HRESULT *phr
  28. ) :
  29. CResource(pDevice, Pool, refType),
  30. m_pbBuffer(NULL),
  31. #if DBG
  32. m_isLockable((dwActualUsage & (D3DUSAGE_LOCK | D3DUSAGE_LOADONCE)) != 0),
  33. m_SceneStamp(0xFFFFFFFF),
  34. m_TimesLocked(0),
  35. #endif // DBG
  36. m_LockCount(0)
  37. {
  38. // Determine if we need to allocate
  39. // any memory
  40. if (ActualPool == D3DPOOL_SYSTEMMEM ||
  41. IsTypeD3DManaged(pDevice, Type, ActualPool))
  42. {
  43. // cbLength must be a DWORD multiple
  44. cbLength = (cbLength + 3) & (DWORD) ~3;
  45. m_pbBuffer = new BYTE[cbLength];
  46. if (m_pbBuffer == NULL)
  47. {
  48. DPF_ERR("Out Of Memory allocating vertex or index buffer");
  49. *phr = E_OUTOFMEMORY;
  50. return;
  51. }
  52. DXGASSERT((cbLength & 3) == 0);
  53. }
  54. // We need to call the driver
  55. // to get a handle for all cases
  56. // Create a DDSURFACEINFO and CreateSurfaceData object
  57. DDSURFACEINFO SurfInfo;
  58. ZeroMemory(&SurfInfo, sizeof(SurfInfo));
  59. D3D8_CREATESURFACEDATA CreateSurfaceData;
  60. ZeroMemory(&CreateSurfaceData, sizeof(CreateSurfaceData));
  61. // Set up the basic information
  62. CreateSurfaceData.hDD = pDevice->GetHandle();
  63. CreateSurfaceData.pSList = &SurfInfo;
  64. CreateSurfaceData.dwSCnt = 1;
  65. CreateSurfaceData.Type = Type;
  66. CreateSurfaceData.dwUsage = dwActualUsage;
  67. CreateSurfaceData.Pool = DetermineCreationPool(Device(), Type, dwActualUsage, ActualPool);
  68. CreateSurfaceData.Format = Format;
  69. CreateSurfaceData.MultiSampleType = D3DMULTISAMPLE_NONE;
  70. CreateSurfaceData.dwFVF = dwFVF;
  71. if (Pool == D3DPOOL_DEFAULT &&
  72. CreateSurfaceData.Pool == D3DPOOL_SYSTEMMEM)
  73. {
  74. // If we are using sys-mem in cases where the
  75. // user asked for POOL_DEFAULT, we need to let
  76. // the thunk layer know so that Reset will
  77. // fail if this buffer hasn't been released
  78. CreateSurfaceData.bTreatAsVidMem = TRUE;
  79. }
  80. // Specify the surface data
  81. SurfInfo.cpWidth = cbLength;
  82. SurfInfo.cpHeight = 1;
  83. SurfInfo.pbPixels = m_pbBuffer;
  84. SurfInfo.iPitch = cbLength;
  85. // Call thunk to get our handles
  86. *phr = pDevice->GetHalCallbacks()->CreateSurface(&CreateSurfaceData);
  87. if (FAILED(*phr))
  88. return;
  89. // Cache away our handle
  90. SetKernelHandle(SurfInfo.hKernelHandle);
  91. return;
  92. } // CBuffer::CBuffer
  93. #undef DPF_MODNAME
  94. #define DPF_MODNAME "CBuffer::~CBuffer"
  95. // Destructor
  96. CBuffer::~CBuffer()
  97. {
  98. // Tell the thunk layer that we need to
  99. // be freed.
  100. if (CBaseObject::BaseKernelHandle())
  101. {
  102. D3D8_DESTROYSURFACEDATA DestroySurfData;
  103. DestroySurfData.hDD = Device()->GetHandle();
  104. DestroySurfData.hSurface = CBaseObject::BaseKernelHandle();
  105. Device()->GetHalCallbacks()->DestroySurface(&DestroySurfData);
  106. }
  107. delete [] m_pbBuffer;
  108. } // CBuffer::~CBuffer
  109. #undef DPF_MODNAME
  110. #define DPF_MODNAME "CBuffer::OnBufferChangeImpl"
  111. void CBuffer::OnBufferChangeImpl(UINT cbOffsetToLock, UINT cbSizeToLock)
  112. {
  113. // 0 for cbSizeToLock; means the rest of the buffer
  114. // We use this as a special value.
  115. DWORD cbOffsetMax;
  116. if (cbSizeToLock == 0)
  117. cbOffsetMax = 0;
  118. else
  119. cbOffsetMax = cbOffsetToLock + cbSizeToLock;
  120. if (!IsDirty())
  121. {
  122. m_cbDirtyMin = cbOffsetToLock;
  123. m_cbDirtyMax = cbOffsetMax;
  124. OnResourceDirty();
  125. }
  126. else
  127. {
  128. if (m_cbDirtyMin > cbOffsetToLock)
  129. m_cbDirtyMin = cbOffsetToLock;
  130. // An cbOffsetMax of zero means all the way to the
  131. // end of the buffer
  132. if (m_cbDirtyMax < cbOffsetMax || cbOffsetMax == 0)
  133. m_cbDirtyMax = cbOffsetMax;
  134. // We should already be marked as dirty
  135. DXGASSERT(IsDirty());
  136. }
  137. return;
  138. } // OnBufferChangeImpl
  139. #undef DPF_MODNAME
  140. #define DPF_MODNAME "CBuffer::MarkAllDirty"
  141. void CBuffer::MarkAllDirty()
  142. {
  143. // Mark our dirty bounds as being the whole
  144. // thing.
  145. m_cbDirtyMin = 0;
  146. // Zero for max is a special value meaning
  147. // all they way to the end
  148. m_cbDirtyMax = 0;
  149. // Mark ourselves as dirty
  150. OnResourceDirty();
  151. } // CBuffer::MarkAllDirty
  152. // Methods for CCommandBuffer
  153. #undef DPF_MODNAME
  154. #define DPF_MODNAME "CCommandBuffer::Create"
  155. // Static class function for creating a command buffer object.
  156. // (Because it is static; it doesn't have a this pointer.)
  157. // Creation function for Command Buffers
  158. HRESULT CCommandBuffer::Create(CBaseDevice *pDevice,
  159. DWORD cbLength,
  160. D3DPOOL Pool,
  161. CCommandBuffer **ppCmdBuffer)
  162. {
  163. HRESULT hr;
  164. // Zero-out return parameter
  165. *ppCmdBuffer = NULL;
  166. // Allocate new buffer
  167. CCommandBuffer *pCmdBuffer;
  168. DXGASSERT(Pool == D3DPOOL_SYSTEMMEM);
  169. pCmdBuffer = new CCommandBuffer(pDevice,
  170. cbLength,
  171. Pool,
  172. &hr);
  173. if (pCmdBuffer == NULL)
  174. {
  175. DPF_ERR("Out of Memory creating command buffer");
  176. return E_OUTOFMEMORY;
  177. }
  178. if (FAILED(hr))
  179. {
  180. // Command buffers are always internal and hence
  181. // need to be released through DecrementUseCount
  182. DPF_ERR("Error during initialization of command buffer");
  183. pCmdBuffer->DecrementUseCount();
  184. return hr;
  185. }
  186. // We're done; just return the object
  187. *ppCmdBuffer = pCmdBuffer;
  188. return hr;
  189. } // static CCommandBuffer::Create
  190. #undef DPF_MODNAME
  191. #define DPF_MODNAME "CCommandBuffer::Clone"
  192. HRESULT CCommandBuffer::Clone(D3DPOOL Pool,
  193. CResource **ppResource) const
  194. {
  195. HRESULT hr;
  196. *ppResource = new CCommandBuffer(Device(), m_cbLength, Pool, &hr);
  197. if (*ppResource == NULL)
  198. {
  199. DPF_ERR("Failed to allocate command buffer");
  200. return E_OUTOFMEMORY;
  201. }
  202. if (FAILED(hr))
  203. {
  204. DPF_ERR("Failure creating command buffer");
  205. }
  206. return hr;
  207. } // CCommandBuffer::Clone
  208. // End of file : buffer.cpp