BsD3D11Texture.h 5.8 KB

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