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 "OgreD3D9HardwarePixelBuffer.h" |
---|
30 | #include "OgreD3D9Texture.h" |
---|
31 | #include "OgreD3D9Mappings.h" |
---|
32 | #include "OgreException.h" |
---|
33 | #include "OgreLogManager.h" |
---|
34 | #include "OgreStringConverter.h" |
---|
35 | #include "OgreBitwise.h" |
---|
36 | |
---|
37 | #include "OgreRoot.h" |
---|
38 | #include "OgreRenderSystem.h" |
---|
39 | |
---|
40 | #include "OgreNoMemoryMacros.h" |
---|
41 | #include <d3dx9.h> |
---|
42 | #include <dxerr9.h> |
---|
43 | #include "OgreMemoryMacros.h" |
---|
44 | |
---|
45 | namespace Ogre { |
---|
46 | |
---|
47 | //----------------------------------------------------------------------------- |
---|
48 | |
---|
49 | D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer(HardwareBuffer::Usage usage): |
---|
50 | HardwarePixelBuffer(0, 0, 0, PF_UNKNOWN, usage, false, false), |
---|
51 | mpDev(0), |
---|
52 | mSurface(0), mVolume(0), mTempSurface(0), mTempVolume(0), |
---|
53 | mDoMipmapGen(0), mHWMipmaps(0), mMipTex(0) |
---|
54 | { |
---|
55 | } |
---|
56 | D3D9HardwarePixelBuffer::~D3D9HardwarePixelBuffer() |
---|
57 | { |
---|
58 | destroyRenderTextures(); |
---|
59 | } |
---|
60 | //----------------------------------------------------------------------------- |
---|
61 | void D3D9HardwarePixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *surface, bool update) |
---|
62 | { |
---|
63 | mpDev = dev; |
---|
64 | mSurface = surface; |
---|
65 | |
---|
66 | D3DSURFACE_DESC desc; |
---|
67 | if(mSurface->GetDesc(&desc) != D3D_OK) |
---|
68 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information", |
---|
69 | "D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer"); |
---|
70 | mWidth = desc.Width; |
---|
71 | mHeight = desc.Height; |
---|
72 | mDepth = 1; |
---|
73 | mFormat = D3D9Mappings::_getPF(desc.Format); |
---|
74 | // Default |
---|
75 | mRowPitch = mWidth; |
---|
76 | mSlicePitch = mHeight*mWidth; |
---|
77 | mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat); |
---|
78 | |
---|
79 | if(mUsage & TU_RENDERTARGET) |
---|
80 | createRenderTextures(update); |
---|
81 | } |
---|
82 | //----------------------------------------------------------------------------- |
---|
83 | void D3D9HardwarePixelBuffer::bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *volume, bool update) |
---|
84 | { |
---|
85 | mpDev = dev; |
---|
86 | mVolume = volume; |
---|
87 | |
---|
88 | D3DVOLUME_DESC desc; |
---|
89 | if(mVolume->GetDesc(&desc) != D3D_OK) |
---|
90 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get volume information", |
---|
91 | "D3D9HardwarePixelBuffer::D3D9HardwarePixelBuffer"); |
---|
92 | mWidth = desc.Width; |
---|
93 | mHeight = desc.Height; |
---|
94 | mDepth = desc.Depth; |
---|
95 | mFormat = D3D9Mappings::_getPF(desc.Format); |
---|
96 | // Default |
---|
97 | mRowPitch = mWidth; |
---|
98 | mSlicePitch = mHeight*mWidth; |
---|
99 | mSizeInBytes = PixelUtil::getMemorySize(mWidth, mHeight, mDepth, mFormat); |
---|
100 | |
---|
101 | if(mUsage & TU_RENDERTARGET) |
---|
102 | createRenderTextures(update); |
---|
103 | } |
---|
104 | //----------------------------------------------------------------------------- |
---|
105 | // Util functions to convert a D3D locked box to a pixel box |
---|
106 | void fromD3DLock(PixelBox &rval, const D3DLOCKED_RECT &lrect) |
---|
107 | { |
---|
108 | size_t bpp = PixelUtil::getNumElemBytes(rval.format); |
---|
109 | if (bpp != 0) |
---|
110 | { |
---|
111 | rval.rowPitch = lrect.Pitch / bpp; |
---|
112 | rval.slicePitch = rval.rowPitch * rval.getHeight(); |
---|
113 | assert((lrect.Pitch % bpp)==0); |
---|
114 | } |
---|
115 | else if (PixelUtil::isCompressed(rval.format)) |
---|
116 | { |
---|
117 | rval.rowPitch = rval.getWidth(); |
---|
118 | rval.slicePitch = rval.getWidth() * rval.getHeight(); |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
---|
123 | "Invalid pixel format", "fromD3DLock"); |
---|
124 | } |
---|
125 | |
---|
126 | rval.data = lrect.pBits; |
---|
127 | } |
---|
128 | void fromD3DLock(PixelBox &rval, const D3DLOCKED_BOX &lbox) |
---|
129 | { |
---|
130 | size_t bpp = PixelUtil::getNumElemBytes(rval.format); |
---|
131 | if (bpp != 0) |
---|
132 | { |
---|
133 | rval.rowPitch = lbox.RowPitch / bpp; |
---|
134 | rval.slicePitch = lbox.SlicePitch / bpp; |
---|
135 | assert((lbox.RowPitch % bpp)==0); |
---|
136 | assert((lbox.SlicePitch % bpp)==0); |
---|
137 | } |
---|
138 | else if (PixelUtil::isCompressed(rval.format)) |
---|
139 | { |
---|
140 | rval.rowPitch = rval.getWidth(); |
---|
141 | rval.slicePitch = rval.getWidth() * rval.getHeight(); |
---|
142 | } |
---|
143 | else |
---|
144 | { |
---|
145 | OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, |
---|
146 | "Invalid pixel format", "fromD3DLock"); |
---|
147 | } |
---|
148 | rval.data = lbox.pBits; |
---|
149 | } |
---|
150 | // Convert Ogre integer Box to D3D rectangle |
---|
151 | RECT toD3DRECT(const Box &lockBox) |
---|
152 | { |
---|
153 | RECT prect; |
---|
154 | assert(lockBox.getDepth() == 1); |
---|
155 | prect.left = lockBox.left; |
---|
156 | prect.right = lockBox.right; |
---|
157 | prect.top = lockBox.top; |
---|
158 | prect.bottom = lockBox.bottom; |
---|
159 | return prect; |
---|
160 | } |
---|
161 | // Convert Ogre integer Box to D3D box |
---|
162 | D3DBOX toD3DBOX(const Box &lockBox) |
---|
163 | { |
---|
164 | D3DBOX pbox; |
---|
165 | pbox.Left = lockBox.left; |
---|
166 | pbox.Right = lockBox.right; |
---|
167 | pbox.Top = lockBox.top; |
---|
168 | pbox.Bottom = lockBox.bottom; |
---|
169 | pbox.Front = lockBox.front; |
---|
170 | pbox.Back = lockBox.back; |
---|
171 | return pbox; |
---|
172 | } |
---|
173 | // Convert Ogre pixelbox extent to D3D rectangle |
---|
174 | RECT toD3DRECTExtent(const PixelBox &lockBox) |
---|
175 | { |
---|
176 | RECT prect; |
---|
177 | assert(lockBox.getDepth() == 1); |
---|
178 | prect.left = 0; |
---|
179 | prect.right = lockBox.getWidth(); |
---|
180 | prect.top = 0; |
---|
181 | prect.bottom = lockBox.getHeight(); |
---|
182 | return prect; |
---|
183 | } |
---|
184 | // Convert Ogre pixelbox extent to D3D box |
---|
185 | D3DBOX toD3DBOXExtent(const PixelBox &lockBox) |
---|
186 | { |
---|
187 | D3DBOX pbox; |
---|
188 | pbox.Left = 0; |
---|
189 | pbox.Right = lockBox.getWidth(); |
---|
190 | pbox.Top = 0; |
---|
191 | pbox.Bottom = lockBox.getHeight(); |
---|
192 | pbox.Front = 0; |
---|
193 | pbox.Back = lockBox.getDepth(); |
---|
194 | return pbox; |
---|
195 | } |
---|
196 | //----------------------------------------------------------------------------- |
---|
197 | PixelBox D3D9HardwarePixelBuffer::lockImpl(const Image::Box lockBox, LockOptions options) |
---|
198 | { |
---|
199 | // Check for misuse |
---|
200 | if(mUsage & TU_RENDERTARGET) |
---|
201 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "DirectX does not allow locking of or directly writing to RenderTargets. Use blitFromMemory if you need the contents.", |
---|
202 | "D3D9HardwarePixelBuffer::lockImpl"); |
---|
203 | // Set extents and format |
---|
204 | PixelBox rval(lockBox, mFormat); |
---|
205 | // Set locking flags according to options |
---|
206 | DWORD flags = 0; |
---|
207 | switch(options) |
---|
208 | { |
---|
209 | case HBL_DISCARD: |
---|
210 | // D3D only likes D3DLOCK_DISCARD if you created the texture with D3DUSAGE_DYNAMIC |
---|
211 | // debug runtime flags this up, could cause problems on some drivers |
---|
212 | if (mUsage & HBU_DYNAMIC) |
---|
213 | flags |= D3DLOCK_DISCARD; |
---|
214 | break; |
---|
215 | case HBL_READ_ONLY: |
---|
216 | flags |= D3DLOCK_READONLY; |
---|
217 | break; |
---|
218 | default: |
---|
219 | break; |
---|
220 | }; |
---|
221 | |
---|
222 | if(mSurface) |
---|
223 | { |
---|
224 | // Surface |
---|
225 | D3DLOCKED_RECT lrect; // Filled in by D3D |
---|
226 | HRESULT hr; |
---|
227 | |
---|
228 | if (lockBox.left == 0 && lockBox.top == 0 |
---|
229 | && lockBox.right == mWidth && lockBox.bottom == mHeight) |
---|
230 | { |
---|
231 | // Lock whole surface |
---|
232 | hr = mSurface->LockRect(&lrect, NULL, flags); |
---|
233 | } |
---|
234 | else |
---|
235 | { |
---|
236 | RECT prect = toD3DRECT(lockBox); // specify range to lock |
---|
237 | hr = mSurface->LockRect(&lrect, &prect, flags); |
---|
238 | } |
---|
239 | if (FAILED(hr)) |
---|
240 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Surface locking failed", |
---|
241 | "D3D9HardwarePixelBuffer::lockImpl"); |
---|
242 | fromD3DLock(rval, lrect); |
---|
243 | } |
---|
244 | else |
---|
245 | { |
---|
246 | // Volume |
---|
247 | D3DBOX pbox = toD3DBOX(lockBox); // specify range to lock |
---|
248 | D3DLOCKED_BOX lbox; // Filled in by D3D |
---|
249 | |
---|
250 | if(mVolume->LockBox(&lbox, &pbox, flags) != D3D_OK) |
---|
251 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Volume locking failed", |
---|
252 | "D3D9HardwarePixelBuffer::lockImpl"); |
---|
253 | fromD3DLock(rval, lbox); |
---|
254 | } |
---|
255 | |
---|
256 | return rval; |
---|
257 | } |
---|
258 | //----------------------------------------------------------------------------- |
---|
259 | void D3D9HardwarePixelBuffer::unlockImpl(void) |
---|
260 | { |
---|
261 | if(mSurface) |
---|
262 | { |
---|
263 | // Surface |
---|
264 | mSurface->UnlockRect(); |
---|
265 | } else { |
---|
266 | // Volume |
---|
267 | mVolume->UnlockBox(); |
---|
268 | } |
---|
269 | if(mDoMipmapGen) |
---|
270 | _genMipmaps(); |
---|
271 | } |
---|
272 | //----------------------------------------------------------------------------- |
---|
273 | void D3D9HardwarePixelBuffer::blit(const HardwarePixelBufferSharedPtr &rsrc, const Image::Box &srcBox, const Image::Box &dstBox) |
---|
274 | { |
---|
275 | D3D9HardwarePixelBuffer *src = static_cast<D3D9HardwarePixelBuffer*>(rsrc.getPointer()); |
---|
276 | if(mSurface && src->mSurface) |
---|
277 | { |
---|
278 | // Surface-to-surface |
---|
279 | RECT dsrcRect = toD3DRECT(srcBox); |
---|
280 | RECT ddestRect = toD3DRECT(dstBox); |
---|
281 | // D3DXLoadSurfaceFromSurface |
---|
282 | if(D3DXLoadSurfaceFromSurface( |
---|
283 | mSurface, NULL, &ddestRect, |
---|
284 | src->mSurface, NULL, &dsrcRect, |
---|
285 | D3DX_DEFAULT, 0) != D3D_OK) |
---|
286 | { |
---|
287 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromSurface failed", |
---|
288 | "D3D9HardwarePixelBuffer::blit"); |
---|
289 | } |
---|
290 | } |
---|
291 | else if(mVolume && src->mVolume) |
---|
292 | { |
---|
293 | // Volume-to-volume |
---|
294 | D3DBOX dsrcBox = toD3DBOX(srcBox); |
---|
295 | D3DBOX ddestBox = toD3DBOX(dstBox); |
---|
296 | |
---|
297 | // D3DXLoadVolumeFromVolume |
---|
298 | if(D3DXLoadVolumeFromVolume( |
---|
299 | mVolume, NULL, &ddestBox, |
---|
300 | src->mVolume, NULL, &dsrcBox, |
---|
301 | D3DX_DEFAULT, 0) != D3D_OK) |
---|
302 | { |
---|
303 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadVolumeFromVolume failed", |
---|
304 | "D3D9HardwarePixelBuffer::blit"); |
---|
305 | } |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | // Software fallback |
---|
310 | HardwarePixelBuffer::blit(rsrc, srcBox, dstBox); |
---|
311 | } |
---|
312 | } |
---|
313 | //----------------------------------------------------------------------------- |
---|
314 | void D3D9HardwarePixelBuffer::blitFromMemory(const PixelBox &src, const Image::Box &dstBox) |
---|
315 | { |
---|
316 | // for scoped deletion of conversion buffer |
---|
317 | MemoryDataStreamPtr buf; |
---|
318 | PixelBox converted = src; |
---|
319 | |
---|
320 | // convert to pixelbuffer's native format if necessary |
---|
321 | if (D3D9Mappings::_getPF(src.format) == D3DFMT_UNKNOWN) |
---|
322 | { |
---|
323 | buf.bind(new MemoryDataStream( |
---|
324 | PixelUtil::getMemorySize(src.getWidth(), src.getHeight(), src.getDepth(), |
---|
325 | mFormat))); |
---|
326 | converted = PixelBox(src.getWidth(), src.getHeight(), src.getDepth(), mFormat, buf->getPtr()); |
---|
327 | PixelUtil::bulkPixelConversion(src, converted); |
---|
328 | } |
---|
329 | |
---|
330 | size_t rowWidth; |
---|
331 | if (PixelUtil::isCompressed(converted.format)) |
---|
332 | { |
---|
333 | // D3D wants the width of one row of cells in bytes |
---|
334 | if (converted.format == PF_DXT1) |
---|
335 | { |
---|
336 | // 64 bits (8 bytes) per 4x4 block |
---|
337 | rowWidth = (converted.rowPitch / 4) * 8; |
---|
338 | } |
---|
339 | else |
---|
340 | { |
---|
341 | // 128 bits (16 bytes) per 4x4 block |
---|
342 | rowWidth = (converted.rowPitch / 4) * 16; |
---|
343 | } |
---|
344 | |
---|
345 | } |
---|
346 | else |
---|
347 | { |
---|
348 | rowWidth = converted.rowPitch * PixelUtil::getNumElemBytes(converted.format); |
---|
349 | } |
---|
350 | if(mSurface) |
---|
351 | { |
---|
352 | RECT destRect, srcRect; |
---|
353 | srcRect = toD3DRECTExtent(converted); |
---|
354 | destRect = toD3DRECT(dstBox); |
---|
355 | |
---|
356 | if(D3DXLoadSurfaceFromMemory(mSurface, NULL, &destRect, |
---|
357 | converted.data, D3D9Mappings::_getPF(converted.format), |
---|
358 | rowWidth, |
---|
359 | NULL, &srcRect, D3DX_DEFAULT, 0) != D3D_OK) |
---|
360 | { |
---|
361 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromMemory failed", |
---|
362 | "D3D9HardwarePixelBuffer::blitFromMemory"); |
---|
363 | } |
---|
364 | } |
---|
365 | else |
---|
366 | { |
---|
367 | D3DBOX destBox, srcBox; |
---|
368 | srcBox = toD3DBOXExtent(converted); |
---|
369 | destBox = toD3DBOX(dstBox); |
---|
370 | size_t sliceWidth; |
---|
371 | if (PixelUtil::isCompressed(converted.format)) |
---|
372 | { |
---|
373 | // D3D wants the width of one slice of cells in bytes |
---|
374 | if (converted.format == PF_DXT1) |
---|
375 | { |
---|
376 | // 64 bits (8 bytes) per 4x4 block |
---|
377 | sliceWidth = (converted.slicePitch / 16) * 8; |
---|
378 | } |
---|
379 | else |
---|
380 | { |
---|
381 | // 128 bits (16 bytes) per 4x4 block |
---|
382 | sliceWidth = (converted.slicePitch / 16) * 16; |
---|
383 | } |
---|
384 | |
---|
385 | } |
---|
386 | else |
---|
387 | { |
---|
388 | sliceWidth = converted.slicePitch * PixelUtil::getNumElemBytes(converted.format); |
---|
389 | } |
---|
390 | |
---|
391 | if(D3DXLoadVolumeFromMemory(mVolume, NULL, &destBox, |
---|
392 | converted.data, D3D9Mappings::_getPF(converted.format), |
---|
393 | rowWidth, sliceWidth, |
---|
394 | NULL, &srcBox, D3DX_DEFAULT, 0) != D3D_OK) |
---|
395 | { |
---|
396 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromMemory failed", |
---|
397 | "D3D9HardwarePixelBuffer::blitFromMemory"); |
---|
398 | } |
---|
399 | } |
---|
400 | if(mDoMipmapGen) |
---|
401 | _genMipmaps(); |
---|
402 | } |
---|
403 | //----------------------------------------------------------------------------- |
---|
404 | void D3D9HardwarePixelBuffer::blitToMemory(const Image::Box &srcBox, const PixelBox &dst) |
---|
405 | { |
---|
406 | // Decide on pixel format of temp surface |
---|
407 | PixelFormat tmpFormat = mFormat; |
---|
408 | if(D3D9Mappings::_getPF(dst.format) != D3DFMT_UNKNOWN) |
---|
409 | { |
---|
410 | tmpFormat = dst.format; |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | if(mSurface) |
---|
415 | { |
---|
416 | assert(srcBox.getDepth() == 1 && dst.getDepth() == 1); |
---|
417 | // Create temp texture |
---|
418 | IDirect3DTexture9 *tmp; |
---|
419 | IDirect3DSurface9 *surface; |
---|
420 | |
---|
421 | D3DSURFACE_DESC srcDesc; |
---|
422 | if(mSurface->GetDesc(&srcDesc) != D3D_OK) |
---|
423 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information", |
---|
424 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
425 | |
---|
426 | D3DPOOL temppool = D3DPOOL_SCRATCH; |
---|
427 | // if we're going to try to use GetRenderTargetData, need to use system mem pool |
---|
428 | bool tryGetRenderTargetData = false; |
---|
429 | if (((srcDesc.Usage & D3DUSAGE_RENDERTARGET) != 0) && |
---|
430 | (srcBox.getWidth() == dst.getWidth()) && (srcBox.getHeight() == dst.getHeight()) && |
---|
431 | (srcBox.getWidth() == getWidth()) && (srcBox.getHeight() == getHeight()) && |
---|
432 | (mFormat == tmpFormat)) |
---|
433 | { |
---|
434 | tryGetRenderTargetData = true; |
---|
435 | temppool = D3DPOOL_SYSTEMMEM; |
---|
436 | } |
---|
437 | |
---|
438 | if(D3DXCreateTexture( |
---|
439 | mpDev, |
---|
440 | dst.getWidth(), dst.getHeight(), |
---|
441 | 1, // 1 mip level ie topmost, generate no mipmaps |
---|
442 | 0, D3D9Mappings::_getPF(tmpFormat), temppool, |
---|
443 | &tmp |
---|
444 | ) != D3D_OK) |
---|
445 | { |
---|
446 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Create temporary texture failed", |
---|
447 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
448 | } |
---|
449 | if(tmp->GetSurfaceLevel(0, &surface) != D3D_OK) |
---|
450 | { |
---|
451 | tmp->Release(); |
---|
452 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get surface level failed", |
---|
453 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
454 | } |
---|
455 | // Copy texture to this temp surface |
---|
456 | RECT destRect, srcRect; |
---|
457 | srcRect = toD3DRECT(srcBox); |
---|
458 | destRect = toD3DRECTExtent(dst); |
---|
459 | |
---|
460 | // Get the real temp surface format |
---|
461 | D3DSURFACE_DESC dstDesc; |
---|
462 | if(surface->GetDesc(&dstDesc) != D3D_OK) |
---|
463 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Could not get surface information", |
---|
464 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
465 | tmpFormat = D3D9Mappings::_getPF(dstDesc.Format); |
---|
466 | |
---|
467 | // Use fast GetRenderTargetData if we are in its usage conditions |
---|
468 | bool fastLoadSuccess = false; |
---|
469 | if (tryGetRenderTargetData) |
---|
470 | { |
---|
471 | if(mpDev->GetRenderTargetData(mSurface, surface) == D3D_OK) |
---|
472 | { |
---|
473 | fastLoadSuccess = true; |
---|
474 | } |
---|
475 | } |
---|
476 | if (!fastLoadSuccess) |
---|
477 | { |
---|
478 | if(D3DXLoadSurfaceFromSurface( |
---|
479 | surface, NULL, &destRect, |
---|
480 | mSurface, NULL, &srcRect, |
---|
481 | D3DX_DEFAULT, 0) != D3D_OK) |
---|
482 | { |
---|
483 | surface->Release(); |
---|
484 | tmp->Release(); |
---|
485 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadSurfaceFromSurface failed", |
---|
486 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
487 | } |
---|
488 | } |
---|
489 | |
---|
490 | // Lock temp surface and copy it to memory |
---|
491 | D3DLOCKED_RECT lrect; // Filled in by D3D |
---|
492 | if(surface->LockRect(&lrect, NULL, D3DLOCK_READONLY) != D3D_OK) |
---|
493 | { |
---|
494 | surface->Release(); |
---|
495 | tmp->Release(); |
---|
496 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "surface->LockRect", |
---|
497 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
498 | } |
---|
499 | // Copy it |
---|
500 | PixelBox locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat); |
---|
501 | fromD3DLock(locked, lrect); |
---|
502 | PixelUtil::bulkPixelConversion(locked, dst); |
---|
503 | surface->UnlockRect(); |
---|
504 | // Release temporary surface and texture |
---|
505 | surface->Release(); |
---|
506 | tmp->Release(); |
---|
507 | } |
---|
508 | else |
---|
509 | { |
---|
510 | // Create temp texture |
---|
511 | IDirect3DVolumeTexture9 *tmp; |
---|
512 | IDirect3DVolume9 *surface; |
---|
513 | |
---|
514 | if(D3DXCreateVolumeTexture( |
---|
515 | mpDev, |
---|
516 | dst.getWidth(), dst.getHeight(), dst.getDepth(), 0, |
---|
517 | 0, D3D9Mappings::_getPF(tmpFormat), D3DPOOL_SCRATCH, |
---|
518 | &tmp |
---|
519 | ) != D3D_OK) |
---|
520 | { |
---|
521 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Create temporary texture failed", |
---|
522 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
523 | } |
---|
524 | if(tmp->GetVolumeLevel(0, &surface) != D3D_OK) |
---|
525 | { |
---|
526 | tmp->Release(); |
---|
527 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Get volume level failed", |
---|
528 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
529 | } |
---|
530 | // Volume |
---|
531 | D3DBOX ddestBox, dsrcBox; |
---|
532 | ddestBox = toD3DBOXExtent(dst); |
---|
533 | dsrcBox = toD3DBOX(srcBox); |
---|
534 | |
---|
535 | if(D3DXLoadVolumeFromVolume( |
---|
536 | surface, NULL, &ddestBox, |
---|
537 | mVolume, NULL, &dsrcBox, |
---|
538 | D3DX_DEFAULT, 0) != D3D_OK) |
---|
539 | { |
---|
540 | surface->Release(); |
---|
541 | tmp->Release(); |
---|
542 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "D3DXLoadVolumeFromVolume failed", |
---|
543 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
544 | } |
---|
545 | // Lock temp surface and copy it to memory |
---|
546 | D3DLOCKED_BOX lbox; // Filled in by D3D |
---|
547 | if(surface->LockBox(&lbox, NULL, D3DLOCK_READONLY) != D3D_OK) |
---|
548 | { |
---|
549 | surface->Release(); |
---|
550 | tmp->Release(); |
---|
551 | OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "surface->LockBox", |
---|
552 | "D3D9HardwarePixelBuffer::blitToMemory"); |
---|
553 | } |
---|
554 | // Copy it |
---|
555 | PixelBox locked(dst.getWidth(), dst.getHeight(), dst.getDepth(), tmpFormat); |
---|
556 | fromD3DLock(locked, lbox); |
---|
557 | PixelUtil::bulkPixelConversion(locked, dst); |
---|
558 | surface->UnlockBox(); |
---|
559 | // Release temporary surface and texture |
---|
560 | surface->Release(); |
---|
561 | tmp->Release(); |
---|
562 | } |
---|
563 | } |
---|
564 | //----------------------------------------------------------------------------- |
---|
565 | void D3D9HardwarePixelBuffer::_genMipmaps() |
---|
566 | { |
---|
567 | assert(mMipTex); |
---|
568 | // Mipmapping |
---|
569 | if (mHWMipmaps) |
---|
570 | { |
---|
571 | // Hardware mipmaps |
---|
572 | mMipTex->GenerateMipSubLevels(); |
---|
573 | } |
---|
574 | else |
---|
575 | { |
---|
576 | // Software mipmaps |
---|
577 | if( D3DXFilterTexture( mMipTex, NULL, D3DX_DEFAULT, D3DX_DEFAULT ) != D3D_OK ) |
---|
578 | { |
---|
579 | OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, |
---|
580 | "Failed to filter texture (generate mipmaps)", |
---|
581 | "D3D9HardwarePixelBuffer::_genMipmaps" ); |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | } |
---|
586 | //----------------------------------------------------------------------------- |
---|
587 | void D3D9HardwarePixelBuffer::_setMipmapping(bool doMipmapGen, bool HWMipmaps, IDirect3DBaseTexture9 *mipTex) |
---|
588 | { |
---|
589 | mDoMipmapGen = doMipmapGen; |
---|
590 | mHWMipmaps = HWMipmaps; |
---|
591 | mMipTex = mipTex; |
---|
592 | } |
---|
593 | //----------------------------------------------------------------------------- |
---|
594 | RenderTexture *D3D9HardwarePixelBuffer::getRenderTarget(size_t zoffset) |
---|
595 | { |
---|
596 | assert(mUsage & TU_RENDERTARGET); |
---|
597 | assert(zoffset < mDepth); |
---|
598 | return mSliceTRT[zoffset]; |
---|
599 | } |
---|
600 | //----------------------------------------------------------------------------- |
---|
601 | void D3D9HardwarePixelBuffer::createRenderTextures(bool update) |
---|
602 | { |
---|
603 | if (update) |
---|
604 | { |
---|
605 | assert(mSliceTRT.size() == mDepth); |
---|
606 | for (SliceTRT::const_iterator it = mSliceTRT.begin(); it != mSliceTRT.end(); ++it) |
---|
607 | { |
---|
608 | D3D9RenderTexture *trt = static_cast<D3D9RenderTexture*>(*it); |
---|
609 | trt->rebind(this); |
---|
610 | } |
---|
611 | return; |
---|
612 | } |
---|
613 | |
---|
614 | destroyRenderTextures(); |
---|
615 | if(!mSurface) |
---|
616 | { |
---|
617 | OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, |
---|
618 | "Rendering to 3D slices not supported yet for Direct3D", |
---|
619 | "D3D9HardwarePixelBuffer::createRenderTexture"); |
---|
620 | } |
---|
621 | // Create render target for each slice |
---|
622 | mSliceTRT.reserve(mDepth); |
---|
623 | assert(mDepth==1); |
---|
624 | for(size_t zoffset=0; zoffset<mDepth; ++zoffset) |
---|
625 | { |
---|
626 | String name; |
---|
627 | name = "rtt/"+Ogre::StringConverter::toString((size_t)mSurface); |
---|
628 | |
---|
629 | RenderTexture *trt = new D3D9RenderTexture(name, this); |
---|
630 | mSliceTRT.push_back(trt); |
---|
631 | Root::getSingleton().getRenderSystem()->attachRenderTarget(*trt); |
---|
632 | } |
---|
633 | } |
---|
634 | //----------------------------------------------------------------------------- |
---|
635 | void D3D9HardwarePixelBuffer::destroyRenderTextures() |
---|
636 | { |
---|
637 | if(mSliceTRT.empty()) |
---|
638 | return; |
---|
639 | // Delete all render targets that are not yet deleted via _clearSliceRTT |
---|
640 | for(size_t zoffset=0; zoffset<mDepth; ++zoffset) |
---|
641 | { |
---|
642 | if(mSliceTRT[zoffset]) |
---|
643 | Root::getSingleton().getRenderSystem()->destroyRenderTarget(mSliceTRT[zoffset]->getName()); |
---|
644 | } |
---|
645 | } |
---|
646 | |
---|
647 | }; |
---|