BsD3D11Texture.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "Image/BsTexture.h"
  6. namespace bs { namespace ct
  7. {
  8. /** @addtogroup D3D11
  9. * @{
  10. */
  11. /** DirectX 11 implementation of a texture. */
  12. class D3D11Texture : public Texture
  13. {
  14. public:
  15. ~D3D11Texture();
  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 D3D11TextureManager;
  28. D3D11Texture(const TEXTURE_DESC& desc, const SPtr<PixelData>& initialData, GpuDeviceFlags deviceMask);
  29. /** @copydoc CoreObject::initialize() */
  30. void initialize() override;
  31. /** @copydoc Texture::lockImpl */
  32. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  33. UINT32 queueIdx = 0) override;
  34. /** @copydoc Texture::unlockImpl */
  35. void unlockImpl() override;
  36. /** @copydoc Texture::copyImpl */
  37. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 dstFace, UINT32 dstMipLevel,
  38. const SPtr<Texture>& target, const SPtr<CommandBuffer>& commandBuffer) override;
  39. /** @copydoc Texture::readData */
  40. void readDataImpl(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0, UINT32 deviceIdx = 0,
  41. UINT32 queueIdx = 0) override;
  42. /** @copydoc Texture::writeData */
  43. void writeDataImpl(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false,
  44. UINT32 queueIdx = 0) override;
  45. /** Creates a blank DX11 1D texture object. */
  46. void create1DTex();
  47. /** Creates a blank DX11 2D texture object. */
  48. void create2DTex();
  49. /** Creates a blank DX11 3D texture object. */
  50. void create3DTex();
  51. /**
  52. * Creates a staging buffer that is used as a temporary buffer for read operations on textures that do not support
  53. * direct reading.
  54. */
  55. void createStagingBuffer();
  56. /**
  57. * Maps the specified texture surface for reading/writing.
  58. *
  59. * @param[in] res Texture resource to map.
  60. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  61. * @param[in] mipLevel Mip level to map (0 being the base level).
  62. * @param[in] face Texture face to map, in case texture has more than one.
  63. * @param[out] rowPitch Output size of a single row in bytes.
  64. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  65. * @return Pointer to the mapped area of memory.
  66. *
  67. * @note
  68. * Non-staging textures must be dynamic in order to be mapped directly and only for writing. No restrictions are
  69. * made on staging textures.
  70. */
  71. void* map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  72. /** Unmaps a previously mapped texture. */
  73. void unmap(ID3D11Resource* res);
  74. /**
  75. * Copies texture data into a staging buffer and maps the staging buffer. Will create a staging buffer if one
  76. * doesn't already exist (potentially wasting a lot of memory).
  77. *
  78. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  79. * @param[in] mipLevel Mip level to map (0 being the base level).
  80. * @param[in] face Texture face to map, in case texture has more than one.
  81. * @param[out] rowPitch Output size of a single row in bytes.
  82. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  83. * @return Pointer to the mapped area of memory.
  84. */
  85. void* mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  86. /** Unmaps a previously mapped staging buffer. */
  87. void unmapstagingbuffer();
  88. /**
  89. * Maps a static buffer, for writing only. Returned pointer points to temporary CPU memory that will be copied to
  90. * the mapped resource on "unmap" call.
  91. *
  92. * @param[in] lock Area of the texture to lock.
  93. * @param[in] mipLevel Mip level to map (0 being the base level).
  94. * @param[in] face Texture face to map, in case texture has more than one.
  95. */
  96. void* mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 face);
  97. /** Unmaps a previously mapped static buffer and flushes its data to the actual GPU buffer. */
  98. void unmapstaticbuffer();
  99. /** Creates an empty and uninitialized texture view object. */
  100. SPtr<TextureView> createView(const TEXTURE_VIEW_DESC& desc) override;
  101. protected:
  102. ID3D11Texture1D* m1DTex;
  103. ID3D11Texture2D* m2DTex;
  104. ID3D11Texture3D* m3DTex;
  105. ID3D11Resource* mTex;
  106. SPtr<D3D11TextureView> mShaderResourceView;
  107. PixelFormat mInternalFormat;
  108. DXGI_FORMAT mDXGIFormat;
  109. DXGI_FORMAT mDXGIColorFormat;
  110. DXGI_FORMAT mDXGIDepthStencilFormat;
  111. ID3D11Resource* mStagingBuffer;
  112. PixelData* mStaticBuffer;
  113. UINT32 mLockedSubresourceIdx;
  114. bool mLockedForReading;
  115. };
  116. /** @} */
  117. }}