BsD3D11Texture.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /** DirectX 11 implementation of a texture. */
  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. /** @copydoc CoreObjectCore::initialize() */
  28. void initialize() override;
  29. /** @copydoc TextureCore::lockImpl */
  30. PixelData lockImpl(GpuLockOptions options, UINT32 mipLevel = 0, UINT32 face = 0) override;
  31. /** @copydoc TextureCore::unlockImpl */
  32. void unlockImpl() override;
  33. /** @copydoc TextureCore::copyImpl */
  34. void copyImpl(UINT32 srcFace, UINT32 srcMipLevel, UINT32 destFace, UINT32 destMipLevel, const SPtr<TextureCore>& target) override;
  35. /** @copydoc TextureCore::readData */
  36. void readData(PixelData& dest, UINT32 mipLevel = 0, UINT32 face = 0) override;
  37. /** @copydoc TextureCore::writeData */
  38. void writeData(const PixelData& src, UINT32 mipLevel = 0, UINT32 face = 0, bool discardWholeBuffer = false) override;
  39. /** Creates a blank DX11 1D texture object. */
  40. void create1DTex();
  41. /** Creates a blank DX11 2D texture object. */
  42. void create2DTex();
  43. /** Creates a blank DX11 3D texture object. */
  44. void create3DTex();
  45. /**
  46. * Creates a staging buffer that is used as a temporary buffer for read operations on textures that do not support
  47. * direct reading.
  48. */
  49. void createStagingBuffer();
  50. /**
  51. * Maps the specified texture surface for reading/writing.
  52. *
  53. * @param[in] res Texture resource to map.
  54. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  55. * @param[in] mipLevel Mip level to map (0 being the base level).
  56. * @param[in] face Texture face to map, in case texture has more than one.
  57. * @param[out] rowPitch Output size of a single row in bytes.
  58. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  59. * @return Pointer to the mapped area of memory.
  60. *
  61. * @note
  62. * Non-staging textures must be dynamic in order to be mapped directly and only for writing. No restrictions are
  63. * made on staging textures.
  64. */
  65. void* map(ID3D11Resource* res, D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  66. /** Unmaps a previously mapped texture. */
  67. void unmap(ID3D11Resource* res);
  68. /**
  69. * Copies texture data into a staging buffer and maps the staging buffer. Will create a staging buffer if one
  70. * doesn't already exist (potentially wasting a lot of memory).
  71. *
  72. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  73. * @param[in] mipLevel Mip level to map (0 being the base level).
  74. * @param[in] face Texture face to map, in case texture has more than one.
  75. * @param[out] rowPitch Output size of a single row in bytes.
  76. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  77. * @return Pointer to the mapped area of memory.
  78. */
  79. void* mapstagingbuffer(D3D11_MAP flags, UINT32 mipLevel, UINT32 face, UINT32& rowPitch, UINT32& slicePitch);
  80. /** Unmaps a previously mapped staging buffer. */
  81. void unmapstagingbuffer();
  82. /**
  83. * Maps a static buffer, for writing only. Returned pointer points to temporary CPU memory that will be copied to
  84. * the mapped resource on "unmap" call.
  85. *
  86. * @param[in] flags Mapping flags that let the API know what are we planning to do with mapped memory.
  87. * @param[in] mipLevel Mip level to map (0 being the base level).
  88. * @param[in] face Texture face to map, in case texture has more than one.
  89. * @param[out] rowPitch Output size of a single row in bytes.
  90. * @param[out] slicePitch Output size of a single slice in bytes (relevant only for 3D textures).
  91. * @return Pointer to the mapped area of memory.
  92. */
  93. void* mapstaticbuffer(PixelData lock, UINT32 mipLevel, UINT32 slice);
  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. TextureViewPtr 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. }