BsD3D11Texture.h 5.6 KB

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