2
0

BsD3D11Texture.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #pragma once
  2. #include "BsD3D11Prerequisites.h"
  3. #include "BsTexture.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief DirectX 11 implementation of a texture.
  8. */
  9. class D3D11Texture : public Texture
  10. {
  11. public:
  12. ~D3D11Texture();
  13. /**
  14. * @brief Returns internal DX11 texture resource object.
  15. */
  16. ID3D11Resource* getDX11Resource() const { return mTex; }
  17. /**
  18. * @brief Returns shader resource view associated with the texture.
  19. */
  20. ID3D11ShaderResourceView* getSRV() const { return mShaderResourceView; }
  21. /**
  22. * @brief Returns DXGI pixel format used by the texture.
  23. */
  24. DXGI_FORMAT getDXGIFormat() const { return mDXGIFormat; }
  25. protected:
  26. friend class D3D11TextureManager;
  27. D3D11Texture();
  28. /**
  29. * @copydoc Texture::initialize_internal()
  30. */
  31. void initialize_internal();
  32. /**
  33. * @copydoc Texture::destroy_internal()
  34. */
  35. void destroy_internal();
  36. /**
  37. * @copydoc Texture::lockImpl
  38. */
  39. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0);
  40. /**
  41. * @copydoc Texture::unlockImpl
  42. */
  43. void unlockImpl();
  44. /**
  45. * @copydoc Texture::copyImpl
  46. */
  47. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, TexturePtr& target);
  48. /**
  49. * @copydoc Texture::readData
  50. */
  51. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0);
  52. /**
  53. * @copydoc Texture::writeData
  54. */
  55. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false);
  56. /**
  57. * @brief Creates a blank DX11 1D texture object.
  58. */
  59. void create1DTex();
  60. /**
  61. * @brief Creates a blank DX11 2D texture object.
  62. */
  63. void create2DTex();
  64. /**
  65. * @brief Creates a blank DX11 3D texture object.
  66. */
  67. void create3DTex();
  68. /**
  69. * @brief Creates a staging buffer that is used as a temporary buffer for read operations on textures
  70. * that do not support direct reading.
  71. */
  72. void createStagingBuffer();
  73. /**
  74. * @brief Maps the specified texture surface for reading/writing.
  75. *
  76. * @param res Texture resource to map.
  77. * @param flags Mapping flags that let the API know what are we planning to do with mapped memory.
  78. * @param mipLevel Mip level to map (0 being the base level).
  79. * @param face Texture face to map, in case texture has more than one.
  80. * @param rowPitch Output size of a single row in bytes.
  81. * @param slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  82. *
  83. * @returns Pointer to the mapped area of memory.
  84. *
  85. * @note Non-staging textures must be dynamic in order to be mapped directly and only for writing.
  86. * No restrictions are made on staging textures.
  87. */
  88. void* map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  89. /**
  90. * @brief Unmaps a previously mapped texture.
  91. */
  92. void unmap(ID3D11Resource* res);
  93. /**
  94. * @brief Copies texture data into a staging buffer and maps the staging buffer. Will create a staging
  95. * buffer if one doesn't already exist (potentially wasting a lot of memory).
  96. *
  97. * @param flags Mapping flags that let the API know what are we planning to do with mapped memory.
  98. * @param mipLevel Mip level to map (0 being the base level).
  99. * @param face Texture face to map, in case texture has more than one.
  100. * @param rowPitch Output size of a single row in bytes.
  101. * @param slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  102. *
  103. * @returns Pointer to the mapped area of memory.
  104. */
  105. void* mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  106. /**
  107. * @brief Unmaps a previously mapped staging buffer.
  108. */
  109. void unmapstagingbuffer();
  110. /**
  111. * @brief Maps a static buffer, for writing only. Returned pointer points to temporary CPU memory
  112. * that will be copied to the mapped resource on "unmap" call.
  113. *
  114. * @param flags Mapping flags that let the API know what are we planning to do with mapped memory.
  115. * @param mipLevel Mip level to map (0 being the base level).
  116. * @param face Texture face to map, in case texture has more than one.
  117. * @param rowPitch Output size of a single row in bytes.
  118. * @param slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  119. *
  120. * @returns Pointer to the mapped area of memory.
  121. */
  122. void* mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 slice);
  123. /**
  124. * @brief Unmaps a previously mapped static buffer and flushes its data to the actual GPU buffer.
  125. */
  126. void unmapstaticbuffer();
  127. /**
  128. * @brief Creates an empty and uninitialized texture view object.
  129. */
  130. TextureViewPtr createView();
  131. protected:
  132. ID3D11Texture1D* m1DTex;
  133. ID3D11Texture2D* m2DTex;
  134. ID3D11Texture3D* m3DTex;
  135. ID3D11Resource* mTex;
  136. ID3D11ShaderResourceView* mShaderResourceView;
  137. D3D11_SHADER_RESOURCE_VIEW_DESC mSRVDesc;
  138. D3D11_SRV_DIMENSION mDimension;
  139. DXGI_FORMAT mDXGIFormat;
  140. ID3D11Resource* mStagingBuffer;
  141. PixelData* mStaticBuffer;
  142. UINT32 mLockedSubresourceIdx;
  143. bool mLockedForReading;
  144. };
  145. }