1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #include "OgreD3D9HardwareVertexBuffer.h" |
---|
30 | #include "OgreD3D9Mappings.h" |
---|
31 | #include "OgreException.h" |
---|
32 | #include "OgreD3D9HardwareBufferManager.h" |
---|
33 | |
---|
34 | namespace Ogre { |
---|
35 | |
---|
36 | //--------------------------------------------------------------------- |
---|
37 | D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer(size_t vertexSize, |
---|
38 | size_t numVertices, HardwareBuffer::Usage usage, LPDIRECT3DDEVICE9 pDev, |
---|
39 | bool useSystemMemory, bool useShadowBuffer) |
---|
40 | : HardwareVertexBuffer(vertexSize, numVertices, usage, useSystemMemory, useShadowBuffer) |
---|
41 | { |
---|
42 | // Create the vertex buffer |
---|
43 | #if OGRE_D3D_MANAGE_BUFFERS |
---|
44 | mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : |
---|
45 | // If not system mem, use managed pool UNLESS buffer is discardable |
---|
46 | // if discardable, keeping the software backing is expensive |
---|
47 | (usage & HardwareBuffer::HBU_DISCARDABLE)? D3DPOOL_DEFAULT : D3DPOOL_MANAGED; |
---|
48 | #else |
---|
49 | mD3DPool = useSystemMemory? D3DPOOL_SYSTEMMEM : D3DPOOL_DEFAULT; |
---|
50 | #endif |
---|
51 | HRESULT hr = pDev->CreateVertexBuffer( |
---|
52 | static_cast<UINT>(mSizeInBytes), |
---|
53 | D3D9Mappings::get(usage), |
---|
54 | 0, // No FVF here, thankyou |
---|
55 | mD3DPool, |
---|
56 | &mlpD3DBuffer, |
---|
57 | NULL); |
---|
58 | if (FAILED(hr)) |
---|
59 | { |
---|
60 | String msg = DXGetErrorDescription9(hr); |
---|
61 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, |
---|
62 | "Cannot create D3D9 vertex buffer: " + msg, |
---|
63 | "D3D9HardwareVertexBuffer::D3D9HardwareVertexBuffer"); |
---|
64 | } |
---|
65 | |
---|
66 | } |
---|
67 | //--------------------------------------------------------------------- |
---|
68 | D3D9HardwareVertexBuffer::~D3D9HardwareVertexBuffer() |
---|
69 | { |
---|
70 | SAFE_RELEASE(mlpD3DBuffer); |
---|
71 | } |
---|
72 | //--------------------------------------------------------------------- |
---|
73 | void* D3D9HardwareVertexBuffer::lockImpl(size_t offset, |
---|
74 | size_t length, LockOptions options) |
---|
75 | { |
---|
76 | void* pBuf; |
---|
77 | HRESULT hr = mlpD3DBuffer->Lock( |
---|
78 | static_cast<UINT>(offset), |
---|
79 | static_cast<UINT>(length), |
---|
80 | &pBuf, |
---|
81 | D3D9Mappings::get(options, mUsage)); |
---|
82 | |
---|
83 | if (FAILED(hr)) |
---|
84 | { |
---|
85 | String msg = DXGetErrorDescription9(hr); |
---|
86 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, |
---|
87 | "Cannot lock D3D9 vertex buffer: " + msg, |
---|
88 | "D3D9HardwareVertexBuffer::lock"); |
---|
89 | } |
---|
90 | |
---|
91 | return pBuf; |
---|
92 | } |
---|
93 | //--------------------------------------------------------------------- |
---|
94 | void D3D9HardwareVertexBuffer::unlockImpl(void) |
---|
95 | { |
---|
96 | HRESULT hr = mlpD3DBuffer->Unlock(); |
---|
97 | } |
---|
98 | //--------------------------------------------------------------------- |
---|
99 | void D3D9HardwareVertexBuffer::readData(size_t offset, size_t length, |
---|
100 | void* pDest) |
---|
101 | { |
---|
102 | // There is no functional interface in D3D, just do via manual |
---|
103 | // lock, copy & unlock |
---|
104 | void* pSrc = this->lock(offset, length, HardwareBuffer::HBL_READ_ONLY); |
---|
105 | memcpy(pDest, pSrc, length); |
---|
106 | this->unlock(); |
---|
107 | |
---|
108 | } |
---|
109 | //--------------------------------------------------------------------- |
---|
110 | void D3D9HardwareVertexBuffer::writeData(size_t offset, size_t length, |
---|
111 | const void* pSource, |
---|
112 | bool discardWholeBuffer) |
---|
113 | { |
---|
114 | // There is no functional interface in D3D, just do via manual |
---|
115 | // lock, copy & unlock |
---|
116 | void* pDst = this->lock(offset, length, |
---|
117 | discardWholeBuffer ? HardwareBuffer::HBL_DISCARD : HardwareBuffer::HBL_NORMAL); |
---|
118 | memcpy(pDst, pSource, length); |
---|
119 | this->unlock(); |
---|
120 | } |
---|
121 | //--------------------------------------------------------------------- |
---|
122 | bool D3D9HardwareVertexBuffer::releaseIfDefaultPool(void) |
---|
123 | { |
---|
124 | if (mD3DPool == D3DPOOL_DEFAULT) |
---|
125 | { |
---|
126 | SAFE_RELEASE(mlpD3DBuffer); |
---|
127 | return true; |
---|
128 | } |
---|
129 | return false; |
---|
130 | } |
---|
131 | //--------------------------------------------------------------------- |
---|
132 | bool D3D9HardwareVertexBuffer::recreateIfDefaultPool(LPDIRECT3DDEVICE9 pDev) |
---|
133 | { |
---|
134 | if (mD3DPool == D3DPOOL_DEFAULT) |
---|
135 | { |
---|
136 | HRESULT hr = pDev->CreateVertexBuffer( |
---|
137 | static_cast<UINT>(mSizeInBytes), |
---|
138 | D3D9Mappings::get(mUsage), |
---|
139 | 0, // No FVF here, thankyou |
---|
140 | mD3DPool, |
---|
141 | &mlpD3DBuffer, |
---|
142 | NULL); |
---|
143 | if (FAILED(hr)) |
---|
144 | { |
---|
145 | String msg = DXGetErrorDescription9(hr); |
---|
146 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, |
---|
147 | "Cannot restore D3D9 vertex buffer: " + msg, |
---|
148 | "D3D9HardwareVertexBuffer::recreateIfDefaultPool"); |
---|
149 | } |
---|
150 | return true; |
---|
151 | } |
---|
152 | return false; |
---|
153 | } |
---|
154 | //--------------------------------------------------------------------- |
---|
155 | } |
---|