BsD3D11Texture.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsD3D11Prerequisites.h"
  5. #include "BsTexture.h"
  6. namespace BansheeEngine
  7. {
  8. /** @addtogroup D3D11
  9. * @{
  10. */
  11. /** DirectX 11 implementation of a texture. */
  12. class D3D11TextureCore : public TextureCore
  13. {
  14. public:
  15. ~D3D11TextureCore();
  16. /** Returns internal DX11 texture resource object. */
  17. ID3D11Resource* getDX11Resource() const { return mTex; }
  18. /** Returns shader resource view associated with the texture. */
  19. ID3D11ShaderResourceView* getSRV() const;
  20. /** Returns DXGI pixel format that was used to create the texture. */
  21. DXGI_FORMAT getDXGIFormat() const { return mDXGIFormat; }
  22. /** Returns DXGI pixel used for reading the texture as a shader resource or writing as a render target. */
  23. DXGI_FORMAT getColorFormat() const { return mDXGIColorFormat; }
  24. /** Returns DXGI pixel used for writing to a depth stencil texture. */
  25. DXGI_FORMAT getDepthStencilFormat() const { return mDXGIDepthStencilFormat; }
  26. protected:
  27. friend class D3D11TextureCoreManager;
  28. D3D11TextureCore(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  29. /** @copydoc CoreObjectCore::initialize() */
  30. void initialize() override;
  31. /** @copydoc TextureCore::lockImpl */
  32. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) override;
  33. /** @copydoc TextureCore::unlockImpl */
  34. void unlockImpl() override;
  35. /** @copydoc TextureCore::copyImpl */
  36. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) override;
  37. /** @copydoc TextureCore::readData */
  38. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) override;
  39. /** @copydoc TextureCore::writeData */
  40. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) override;
  41. /** Creates a blank DX11 1D texture object. */
  42. void create1DTex();
  43. /** Creates a blank DX11 2D texture object. */
  44. void create2DTex();
  45. /** Creates a blank DX11 3D texture object. */
  46. void create3DTex();
  47. /**
  48. * Creates a staging buffer that is used as a temporary buffer for read operations on textures that do not support
  49. * direct reading.
  50. */
  51. void createStagingBuffer();
  52. /**
  53. * Maps the specified texture surface for reading/writing.
  54. *
  55. * @param[in] res Texture resource to map.
  56. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  57. * @param[in] mipLevel Mip level to map (0 being the base level).
  58. * @param[in] face Texture face to map, in case texture has more than one.
  59. * @param[out] rowPitch Output size of a single row in bytes.
  60. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  61. * @return Pointer to the mapped area of memory.
  62. *
  63. * @note
  64. * Non-staging textures must be dynamic in order to be mapped directly and only for writing. No restrictions are
  65. * made on staging textures.
  66. */
  67. void* map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  68. /** Unmaps a previously mapped texture. */
  69. void unmap(ID3D11Resource* res);
  70. /**
  71. * Copies texture data into a staging buffer and maps the staging buffer. Will create a staging buffer if one
  72. * doesn't already exist (potentially wasting a lot of memory).
  73. *
  74. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  75. * @param[in] mipLevel Mip level to map (0 being the base level).
  76. * @param[in] face Texture face to map, in case texture has more than one.
  77. * @param[out] rowPitch Output size of a single row in bytes.
  78. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  79. * @return Pointer to the mapped area of memory.
  80. */
  81. void* mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  82. /** Unmaps a previously mapped staging buffer. */
  83. void unmapstagingbuffer();
  84. /**
  85. * Maps a static buffer, for writing only. Returned pointer points to temporary CPU memory that will be copied to
  86. * the mapped resource on "unmap" call.
  87. *
  88. * @param[in] lock Area of the texture to lock.
  89. * @param[in] mipLevel Mip level to map (0 being the base level).
  90. * @param[in] face Texture face to map, in case texture has more than one.
  91. */
  92. void* mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face);
  93. /** Unmaps a previously mapped static buffer and flushes its data to the actual GPU buffer. */
  94. void unmapstaticbuffer();
  95. /** Creates an empty and uninitialized texture view object. */
  96. SPtr<TextureView> createView(const SPtr<TextureCore>& texture, const TEXTURE_VIEW_DESC& desc) override;
  97. protected:
  98. ID3D11Texture1D* m1DTex;
  99. ID3D11Texture2D* m2DTex;
  100. ID3D11Texture3D* m3DTex;
  101. ID3D11Resource* mTex;
  102. SPtr<D3D11TextureView> mShaderResourceView;
  103. DXGI_FORMAT mDXGIFormat;
  104. DXGI_FORMAT mDXGIColorFormat;
  105. DXGI_FORMAT mDXGIDepthStencilFormat;
  106. ID3D11Resource* mStagingBuffer;
  107. PixelData* mStaticBuffer;
  108. UINT32 mLockedSubresourceIdx;
  109. bool mLockedForReading;
  110. };
  111. /** @} */
  112. }