Texture2D.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2008-2023 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 Image;
  10. class XMLFile;
  11. /// 2D texture resource.
  12. class URHO3D_API Texture2D : public Texture
  13. {
  14. URHO3D_OBJECT(Texture2D, Texture);
  15. public:
  16. /// Construct.
  17. explicit Texture2D(Context* context);
  18. /// Destruct.
  19. ~Texture2D() 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 size, format, usage and multisampling parameters for rendertargets. Zero size will follow application window size. Return true if successful.
  34. /** Autoresolve true means the multisampled texture will be automatically resolved to 1-sample after being rendered to and before being sampled as a texture.
  35. Autoresolve false means the multisampled texture will be read as individual samples in the shader and is not supported on Direct3D9.
  36. */
  37. bool SetSize(int width, int height, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1, bool autoResolve = true);
  38. /// Set data either partially or fully on a mip level. Return true if successful.
  39. bool SetData(unsigned level, int x, int y, int width, int height, const void* data);
  40. /// Set data from an image. Return true if successful. Optionally make a single channel image alpha-only.
  41. bool SetData(Image* image, bool useAlpha = false);
  42. /// Get data from a mip level. The destination buffer must be big enough. Return true if successful.
  43. bool GetData(unsigned level, void* dest) const;
  44. /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
  45. bool GetImage(Image& image) const;
  46. /// Get image data from zero mip level. Only RGB and RGBA textures are supported.
  47. SharedPtr<Image> GetImage() const;
  48. /// Return render surface.
  49. /// @property
  50. RenderSurface* GetRenderSurface() const { return renderSurface_; }
  51. protected:
  52. /// Create the GPU texture.
  53. bool Create() override;
  54. private:
  55. #ifdef URHO3D_OPENGL
  56. void OnDeviceLost_OGL();
  57. void OnDeviceReset_OGL();
  58. void Release_OGL();
  59. bool SetData_OGL(unsigned level, int x, int y, int width, int height, const void* data);
  60. bool SetData_OGL(Image* image, bool useAlpha);
  61. bool GetData_OGL(unsigned level, void* dest) const;
  62. bool Create_OGL();
  63. #endif // def URHO3D_OPENGL
  64. #ifdef URHO3D_D3D11
  65. void OnDeviceLost_D3D11();
  66. void OnDeviceReset_D3D11();
  67. void Release_D3D11();
  68. bool SetData_D3D11(unsigned level, int x, int y, int width, int height, const void* data);
  69. bool SetData_D3D11(Image* image, bool useAlpha);
  70. bool GetData_D3D11(unsigned level, void* dest) const;
  71. bool Create_D3D11();
  72. #endif // def URHO3D_D3D11
  73. /// Handle render surface update event.
  74. void HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData);
  75. /// Render surface.
  76. SharedPtr<RenderSurface> renderSurface_;
  77. /// Image file acquired during BeginLoad.
  78. SharedPtr<Image> loadImage_;
  79. /// Parameter file acquired during BeginLoad.
  80. SharedPtr<XMLFile> loadParameters_;
  81. };
  82. }