Texture2DArray.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/Ptr.h"
  5. #include "../GraphicsAPI/RenderSurface.h"
  6. #include "../GraphicsAPI/Texture.h"
  7. namespace Urho3D
  8. {
  9. class Deserializer;
  10. class Image;
  11. /// 2D texture array resource.
  12. class URHO3D_API Texture2DArray : public Texture
  13. {
  14. URHO3D_OBJECT(Texture2DArray, Texture);
  15. public:
  16. /// Construct.
  17. explicit Texture2DArray(Context* context);
  18. /// Destruct.
  19. ~Texture2DArray() override;
  20. /// Register object factory.
  21. /// @nobind
  22. static void RegisterObject(Context* context);
  23. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  24. bool BeginLoad(Deserializer& source) override;
  25. /// Finish resource loading. Always called from the main thread. Return true if successful.
  26. bool EndLoad() override;
  27. /// Mark the GPU resource destroyed on context destruction.
  28. void OnDeviceLost() override;
  29. /// Recreate the GPU resource and restore data if applicable.
  30. void OnDeviceReset() override;
  31. /// Release the texture.
  32. void Release() override;
  33. /// Set the number of layers in the texture. To be used before SetData.
  34. /// @property
  35. void SetLayers(unsigned layers);
  36. /// Set layers, size, format and usage. Set layers to zero to leave them unchanged. Return true if successful.
  37. bool SetSize(unsigned layers, int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC);
  38. /// Set data either partially or fully on a layer's mip level. Return true if successful.
  39. bool SetData(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data);
  40. /// Set data of one layer from a stream. Return true if successful.
  41. bool SetData(unsigned layer, Deserializer& source);
  42. /// Set data of one layer from an image. Return true if successful. Optionally make a single channel image alpha-only.
  43. bool SetData(unsigned layer, Image* image, bool useAlpha = false);
  44. /// Return number of layers in the texture.
  45. /// @property
  46. unsigned GetLayers() const { return layers_; }
  47. /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
  48. bool GetData(unsigned layer, unsigned level, void* dest) const;
  49. /// Return render surface.
  50. /// @property
  51. RenderSurface* GetRenderSurface() const { return renderSurface_; }
  52. protected:
  53. /// Create the GPU texture.
  54. bool Create() override;
  55. private:
  56. #ifdef URHO3D_OPENGL
  57. void OnDeviceLost_OGL();
  58. void OnDeviceReset_OGL();
  59. void Release_OGL();
  60. bool SetData_OGL(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data);
  61. bool SetData_OGL(unsigned layer, Deserializer& source);
  62. bool SetData_OGL(unsigned layer, Image* image, bool useAlpha = false);
  63. bool GetData_OGL(unsigned layer, unsigned level, void* dest) const;
  64. bool Create_OGL();
  65. #endif // def URHO3D_OPENGL
  66. #ifdef URHO3D_D3D9
  67. void OnDeviceLost_D3D9();
  68. void OnDeviceReset_D3D9();
  69. void Release_D3D9();
  70. bool SetData_D3D9(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data);
  71. bool SetData_D3D9(unsigned layer, Deserializer& source);
  72. bool SetData_D3D9(unsigned layer, Image* image, bool useAlpha = false);
  73. bool GetData_D3D9(unsigned layer, unsigned level, void* dest) const;
  74. bool Create_D3D9();
  75. #endif // def URHO3D_D3D9
  76. #ifdef URHO3D_D3D11
  77. void OnDeviceLost_D3D11();
  78. void OnDeviceReset_D3D11();
  79. void Release_D3D11();
  80. bool SetData_D3D11(unsigned layer, unsigned level, int x, int y, int width, int height, const void* data);
  81. bool SetData_D3D11(unsigned layer, Deserializer& source);
  82. bool SetData_D3D11(unsigned layer, Image* image, bool useAlpha = false);
  83. bool GetData_D3D11(unsigned layer, unsigned level, void* dest) const;
  84. bool Create_D3D11();
  85. #endif // def URHO3D_D3D11
  86. /// Handle render surface update event.
  87. void HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData);
  88. /// Texture array layers number.
  89. unsigned layers_{};
  90. /// Render surface.
  91. SharedPtr<RenderSurface> renderSurface_;
  92. /// Memory use per layer.
  93. PODVector<unsigned> layerMemoryUse_;
  94. /// Layer image files acquired during BeginLoad.
  95. Vector<SharedPtr<Image>> loadImages_;
  96. /// Parameter file acquired during BeginLoad.
  97. SharedPtr<XMLFile> loadParameters_;
  98. };
  99. }