GPUObject.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Ptr.h"
  24. namespace Atomic
  25. {
  26. class Graphics;
  27. /// API-specific GPU object representation.
  28. union GPUObjectHandle
  29. {
  30. /// Object pointer (Direct3D.)
  31. void* ptr_;
  32. /// Object name (OpenGL.)
  33. unsigned name_;
  34. };
  35. /// Base class for GPU resources.
  36. class ATOMIC_API GPUObject
  37. {
  38. public:
  39. /// Construct with graphics subsystem pointer.
  40. GPUObject(Graphics* graphics);
  41. /// Destruct. Remove from the Graphics.
  42. virtual ~GPUObject();
  43. /// Mark the GPU resource destroyed on graphics context destruction.
  44. virtual void OnDeviceLost();
  45. /// Recreate the GPU resource and restore data if applicable.
  46. virtual void OnDeviceReset();
  47. /// Unconditionally release the GPU resource.
  48. virtual void Release();
  49. /// Clear the data lost flag.
  50. void ClearDataLost();
  51. /// Return the graphics subsystem associated with this GPU object.
  52. Graphics* GetGraphics() const;
  53. /// Return the object pointer. Applicable only on Direct3D.
  54. void* GetGPUObject() const { return object_.ptr_; }
  55. /// Return the object name. Applicable only on OpenGL.
  56. unsigned GetGPUObjectName() const { return object_.name_; }
  57. /// Return whether data is lost due to context loss.
  58. bool IsDataLost() const { return dataLost_; }
  59. /// Return whether has pending data assigned while graphics context was lost.
  60. bool HasPendingData() const { return dataPending_; }
  61. protected:
  62. /// Graphics subsystem.
  63. WeakPtr<Graphics> graphics_;
  64. /// Object pointer or name.
  65. GPUObjectHandle object_;
  66. /// Data lost flag.
  67. bool dataLost_;
  68. /// Data pending flag.
  69. bool dataPending_;
  70. };
  71. }