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