BsD3D11Texture.h 5.2 KB

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