BsD3D9PixelBuffer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D9Prerequisites.h"
  5. #include "BsPixelBuffer.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief DirectX9 implementation of a pixel buffer. Represents a hardware buffer
  10. * containing a surface of pixels.
  11. */
  12. class BS_D3D9_EXPORT D3D9PixelBuffer : public PixelBuffer
  13. {
  14. protected:
  15. /**
  16. * @brief Internal DX9 buffer resources container.
  17. */
  18. struct BufferResources
  19. {
  20. IDirect3DSurface9* surface;
  21. IDirect3DVolume9* volume;
  22. IDirect3DSurface9* tempSurface;
  23. IDirect3DVolume9* tempVolume;
  24. IDirect3DBaseTexture9* mipTex;
  25. };
  26. public:
  27. D3D9PixelBuffer(GpuBufferUsage usage, D3D9TextureCore* ownerTexture);
  28. ~D3D9PixelBuffer();
  29. /**
  30. * @brief Binds the specified surface object to this buffer. This needs to be called in order
  31. * to initialize the pixel buffer.
  32. *
  33. * @param dev Device the surface was created on.
  34. * @param surface DirectX 9 object representing the surface.
  35. * @param mipTex Base texture that will be used for generating mipmaps. Usually
  36. * the parent texture of the surface.
  37. */
  38. void bind(IDirect3DDevice9* dev, IDirect3DSurface9* surface, IDirect3DBaseTexture9* mipTex);
  39. /**
  40. * @brief Binds the specified volume object to this buffer. This needs to be called in order
  41. * to initialize the pixel buffer.
  42. *
  43. * @param dev Device the volume was created on.
  44. * @param surface DirectX 9 object representing the volume.
  45. * @param mipTex Base texture that will be used for generating mipmaps. Usually
  46. * the parent texture of the volume.
  47. */
  48. void bind(IDirect3DDevice9* dev, IDirect3DVolume9* volume, IDirect3DBaseTexture9* mipTex);
  49. /**
  50. * @brief Enables/disabled automatic mipmap generation on updates.
  51. *
  52. * @param doMipmapGen If true, mipmaps will be regenerated whenever data is written to the buffer.
  53. * @param HWMipmaps If true the mipmaps will be generated by the hardware, otherwise software (slower).
  54. */
  55. void setMipmapping(bool doMipmapGen, bool HWMipmaps);
  56. /**
  57. * @brief Returns internal DirectX 9 surface object for this buffer.
  58. */
  59. IDirect3DSurface9* getSurface(IDirect3DDevice9* d3d9Device);
  60. /**
  61. * @brief Release all surface objects held by this buffer for the specified device.
  62. */
  63. void releaseSurfaces(IDirect3DDevice9* d3d9Device);
  64. /**
  65. * @brief Destroy all resources associated with the specified device.
  66. */
  67. void destroyBufferResources(IDirect3DDevice9* d3d9Device);
  68. /**
  69. * @brief Called when device state is changing. Access to any device should be locked.
  70. */
  71. static void lockDeviceAccess();
  72. /**
  73. * @brief Called when device state change completed. Access to any device is allowed.
  74. */
  75. static void unlockDeviceAccess();
  76. /**
  77. * @brief Initializes the provided pixel data buffer with information provided in the D3D9 locked rectangle.
  78. */
  79. static void initPixelDataFromD3DLock(PixelData& pixelData, const D3DLOCKED_RECT& lrect);
  80. /**
  81. * @brief Initializes the provided pixel data buffer with information provided in the D3D9 locked box.
  82. */
  83. static void initPixelDataFromD3DLock(PixelData& pixelData, const D3DLOCKED_BOX& lrect);
  84. protected:
  85. /**
  86. * @copydoc PixelBuffer::lockImpl
  87. */
  88. PixelData lockImpl(PixelVolume lockBox, GpuLockOptions options);
  89. /**
  90. * @copydoc PixelBuffer::unlockImpl
  91. */
  92. void unlockImpl();
  93. /**
  94. * @brief Locks the specified volume of the provided buffer objects and
  95. * returns a data buffer that you may use to access it.
  96. */
  97. PixelData lockBuffer(BufferResources* bufferResources, const PixelVolume& lockBox, DWORD flags);
  98. /**
  99. * @brief Unlocks the specified buffer objects.
  100. */
  101. void unlockBuffer(BufferResources* bufferResources);
  102. /**
  103. * @brief Generates mip-map chain for the specified texture.
  104. */
  105. void genMipmaps(IDirect3DBaseTexture9* mipTex);
  106. /**
  107. * @brief Retrieves buffer resources for the specified device, or null if they
  108. * do not exist.
  109. */
  110. BufferResources* getBufferResources(IDirect3DDevice9* d3d9Device);
  111. /**
  112. * @brief Creates a new empty set of buffer resources.
  113. */
  114. BufferResources* createBufferResources();
  115. protected:
  116. Map<IDirect3DDevice9*, BufferResources*> mMapDeviceToBufferResources;
  117. bool mDoMipmapGen;
  118. bool mHWMipmaps;
  119. D3D9TextureCore* mOwnerTexture;
  120. DWORD mLockFlags;
  121. };
  122. };