TextureCube.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Deserializer;
  10. class Image;
  11. /// Cube texture resource.
  12. class URHO3D_API TextureCube : public Texture
  13. {
  14. URHO3D_OBJECT(TextureCube, Texture);
  15. public:
  16. /// Construct.
  17. explicit TextureCube(Context* context);
  18. /// Destruct.
  19. ~TextureCube() 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 parameter for rendertargets. Note that cube textures always use autoresolve when multisampled due to lacking support (on all APIs) to multisample them in a shader. Return true if successful.
  34. bool SetSize(int size, unsigned format, TextureUsage usage = TEXTURE_STATIC, int multiSample = 1);
  35. /// Set data either partially or fully on a face's mip level. Return true if successful.
  36. bool SetData(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data);
  37. /// Set data of one face from a stream. Return true if successful.
  38. bool SetData(CubeMapFace face, Deserializer& source);
  39. /// Set data of one face from an image. Return true if successful. Optionally make a single channel image alpha-only.
  40. bool SetData(CubeMapFace face, Image* image, bool useAlpha = false);
  41. /// Get data from a face's mip level. The destination buffer must be big enough. Return true if successful.
  42. bool GetData(CubeMapFace face, unsigned level, void* dest) const;
  43. /// Get image data from a face's zero mip level. Only RGB and RGBA textures are supported.
  44. SharedPtr<Image> GetImage(CubeMapFace face) const;
  45. /// Return render surface for one face.
  46. /// @property{get_renderSurfaces}
  47. RenderSurface* GetRenderSurface(CubeMapFace face) const { return renderSurfaces_[face]; }
  48. protected:
  49. /// Create the GPU texture.
  50. bool Create() override;
  51. private:
  52. #ifdef URHO3D_OPENGL
  53. void OnDeviceLost_OGL();
  54. void OnDeviceReset_OGL();
  55. void Release_OGL();
  56. bool SetData_OGL(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data);
  57. bool SetData_OGL(CubeMapFace face, Deserializer& source);
  58. bool SetData_OGL(CubeMapFace face, Image* image, bool useAlpha = false);
  59. bool GetData_OGL(CubeMapFace face, unsigned level, void* dest) const;
  60. bool Create_OGL();
  61. #endif // def URHO3D_OPENGL
  62. #ifdef URHO3D_D3D11
  63. void OnDeviceLost_D3D11();
  64. void OnDeviceReset_D3D11();
  65. void Release_D3D11();
  66. bool SetData_D3D11(CubeMapFace face, unsigned level, int x, int y, int width, int height, const void* data);
  67. bool SetData_D3D11(CubeMapFace face, Deserializer& source);
  68. bool SetData_D3D11(CubeMapFace face, Image* image, bool useAlpha = false);
  69. bool GetData_D3D11(CubeMapFace face, unsigned level, void* dest) const;
  70. bool Create_D3D11();
  71. #endif // def URHO3D_D3D11
  72. /// Handle render surface update event.
  73. void HandleRenderSurfaceUpdate(StringHash eventType, VariantMap& eventData);
  74. /// Render surfaces.
  75. SharedPtr<RenderSurface> renderSurfaces_[MAX_CUBEMAP_FACES];
  76. /// Memory use per face.
  77. unsigned faceMemoryUse_[MAX_CUBEMAP_FACES]{};
  78. /// Face image files acquired during BeginLoad.
  79. Vector<SharedPtr<Image>> loadImages_;
  80. /// Parameter file acquired during BeginLoad.
  81. SharedPtr<XMLFile> loadParameters_;
  82. };
  83. }