CmD3D9HardwarePixelBuffer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __D3D9PIXELBUFFER_H__
  25. #define __D3D9PIXELBUFFER_H__
  26. #include "CmD3D9Prerequisites.h"
  27. #include "CmHardwarePixelBuffer.h"
  28. namespace CamelotEngine {
  29. class D3D9Texture;
  30. class D3D9RenderTexture;
  31. class _OgreD3D9Export D3D9HardwarePixelBuffer: public HardwarePixelBuffer
  32. {
  33. protected:
  34. struct BufferResources
  35. {
  36. /// Surface abstracted by this buffer
  37. IDirect3DSurface9* surface;
  38. /// AA Surface abstracted by this buffer
  39. IDirect3DSurface9* fSAASurface;
  40. /// Volume abstracted by this buffer
  41. IDirect3DVolume9* volume;
  42. /// Temporary surface in main memory if direct locking of mSurface is not possible
  43. IDirect3DSurface9* tempSurface;
  44. /// Temporary volume in main memory if direct locking of mVolume is not possible
  45. IDirect3DVolume9* tempVolume;
  46. /// Mip map texture.
  47. IDirect3DBaseTexture9 *mipTex;
  48. };
  49. typedef map<IDirect3DDevice9*, BufferResources*>::type DeviceToBufferResourcesMap;
  50. typedef DeviceToBufferResourcesMap::iterator DeviceToBufferResourcesIterator;
  51. /// Map between device to buffer resources.
  52. DeviceToBufferResourcesMap mMapDeviceToBufferResources;
  53. /// Mipmapping
  54. bool mDoMipmapGen;
  55. bool mHWMipmaps;
  56. /// Render target
  57. D3D9RenderTexture* mRenderTexture;
  58. // The owner texture if exists.
  59. D3D9Texture* mOwnerTexture;
  60. // The current lock flags of this surface.
  61. DWORD mLockFlags;
  62. // Device access mutex.
  63. CM_STATIC_MUTEX(msDeviceAccessMutex)
  64. protected:
  65. /// Lock a box
  66. PixelData lockImpl(const Box lockBox, LockOptions options);
  67. PixelData lockBuffer(BufferResources* bufferResources, const Box &lockBox, DWORD flags);
  68. /// Unlock a box
  69. void unlockImpl(void);
  70. void unlockBuffer(BufferResources* bufferResources);
  71. BufferResources* getBufferResources(IDirect3DDevice9* d3d9Device);
  72. BufferResources* createBufferResources();
  73. /// updates render texture.
  74. void updateRenderTexture(bool writeGamma, UINT32 fsaa, const String& srcName);
  75. /// destroy render texture.
  76. void destroyRenderTexture();
  77. void blit(IDirect3DDevice9* d3d9Device, const HardwarePixelBufferPtr &src,
  78. const Box &srcBox, const Box &dstBox,
  79. BufferResources* srcBufferResources,
  80. BufferResources* dstBufferResources);
  81. void blitFromMemory(const PixelData &src, const Box &dstBox, BufferResources* dstBufferResources);
  82. void blitToMemory(const Box &srcBox, const PixelData &dst, BufferResources* srcBufferResources, IDirect3DDevice9* d3d9Device);
  83. public:
  84. D3D9HardwarePixelBuffer(HardwareBuffer::Usage usage,
  85. D3D9Texture* ownerTexture);
  86. ~D3D9HardwarePixelBuffer();
  87. /// Call this to associate a D3D surface or volume with this pixel buffer
  88. void bind(IDirect3DDevice9 *dev, IDirect3DSurface9 *mSurface, IDirect3DSurface9* fsaaSurface,
  89. bool writeGamma, UINT32 fsaa, const String& srcName, IDirect3DBaseTexture9 *mipTex);
  90. void bind(IDirect3DDevice9 *dev, IDirect3DVolume9 *mVolume, IDirect3DBaseTexture9 *mipTex);
  91. /// @copydoc HardwarePixelBuffer::blit
  92. void blit(const HardwarePixelBufferPtr &src, const Box &srcBox, const Box &dstBox);
  93. /// @copydoc HardwarePixelBuffer::blitFromMemory
  94. void blitFromMemory(const PixelData &src, const Box &dstBox);
  95. /// @copydoc HardwarePixelBuffer::blitToMemory
  96. void blitToMemory(const Box &srcBox, const PixelData &dst);
  97. /// Internal function to update mipmaps on update of level 0
  98. void _genMipmaps(IDirect3DBaseTexture9* mipTex);
  99. /// Function to set mipmap generation
  100. void _setMipmapping(bool doMipmapGen, bool HWMipmaps);
  101. /// Get rendertarget for z slice
  102. RenderTexture *getRenderTarget(size_t zoffset);
  103. /// Accessor for surface
  104. IDirect3DSurface9 *getSurface(IDirect3DDevice9* d3d9Device);
  105. /// Accessor for AA surface
  106. IDirect3DSurface9 *getFSAASurface(IDirect3DDevice9* d3d9Device);
  107. /// Notify TextureBuffer of destruction of render target
  108. virtual void _clearSliceRTT(size_t zoffset);
  109. /// Release surfaces held by this pixel buffer.
  110. void releaseSurfaces(IDirect3DDevice9* d3d9Device);
  111. /// Destroy resources associated with the given device.
  112. void destroyBufferResources(IDirect3DDevice9* d3d9Device);
  113. // Called when device state is changing. Access to any device should be locked.
  114. // Relevant for multi thread application.
  115. static void lockDeviceAccess();
  116. // Called when device state change completed. Access to any device is allowed.
  117. // Relevant for multi thread application.
  118. static void unlockDeviceAccess();
  119. };
  120. };
  121. #endif