OGLRenderSurface.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright (c) 2008-2015 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. #include "../../Precompiled.h"
  23. #include "../../Graphics/Camera.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../Graphics/GraphicsImpl.h"
  26. #include "../../Graphics/Renderer.h"
  27. #include "../../Graphics/RenderSurface.h"
  28. #include "../../Graphics/Texture.h"
  29. #include "../../DebugNew.h"
  30. #ifdef GL_ES_VERSION_2_0
  31. #define GL_RENDERBUFFER_EXT GL_RENDERBUFFER
  32. #define glGenRenderbuffersEXT glGenRenderbuffers
  33. #define glBindRenderbufferEXT glBindRenderbuffer
  34. #define glRenderbufferStorageEXT glRenderbufferStorage
  35. #define glDeleteRenderbuffersEXT glDeleteRenderbuffers
  36. #endif
  37. namespace Atomic
  38. {
  39. RenderSurface::RenderSurface(Texture* parentTexture) :
  40. parentTexture_(parentTexture),
  41. target_(GL_TEXTURE_2D),
  42. renderBuffer_(0),
  43. updateMode_(SURFACE_UPDATEVISIBLE),
  44. updateQueued_(false)
  45. {
  46. }
  47. RenderSurface::~RenderSurface()
  48. {
  49. Release();
  50. }
  51. void RenderSurface::SetNumViewports(unsigned num)
  52. {
  53. viewports_.Resize(num);
  54. }
  55. void RenderSurface::SetViewport(unsigned index, Viewport* viewport)
  56. {
  57. if (index >= viewports_.Size())
  58. viewports_.Resize(index + 1);
  59. viewports_[index] = viewport;
  60. }
  61. void RenderSurface::SetUpdateMode(RenderSurfaceUpdateMode mode)
  62. {
  63. updateMode_ = mode;
  64. }
  65. void RenderSurface::SetLinkedRenderTarget(RenderSurface* renderTarget)
  66. {
  67. if (renderTarget != this)
  68. linkedRenderTarget_ = renderTarget;
  69. }
  70. void RenderSurface::SetLinkedDepthStencil(RenderSurface* depthStencil)
  71. {
  72. if (depthStencil != this)
  73. linkedDepthStencil_ = depthStencil;
  74. }
  75. void RenderSurface::QueueUpdate()
  76. {
  77. if (!updateQueued_)
  78. {
  79. bool hasValidView = false;
  80. // Verify that there is at least 1 non-null viewport, as otherwise Renderer will not accept the surface and the update flag
  81. // will be left on
  82. for (unsigned i = 0; i < viewports_.Size(); ++i)
  83. {
  84. if (viewports_[i])
  85. {
  86. hasValidView = true;
  87. break;
  88. }
  89. }
  90. if (hasValidView)
  91. {
  92. Renderer* renderer = parentTexture_->GetSubsystem<Renderer>();
  93. if (renderer)
  94. renderer->QueueRenderSurface(this);
  95. updateQueued_ = true;
  96. }
  97. }
  98. }
  99. bool RenderSurface::CreateRenderBuffer(unsigned width, unsigned height, unsigned format)
  100. {
  101. Graphics* graphics = parentTexture_->GetGraphics();
  102. if (!graphics)
  103. return false;
  104. Release();
  105. glGenRenderbuffersEXT(1, &renderBuffer_);
  106. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderBuffer_);
  107. glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, format, width, height);
  108. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
  109. return true;
  110. }
  111. void RenderSurface::OnDeviceLost()
  112. {
  113. Graphics* graphics = parentTexture_->GetGraphics();
  114. if (!graphics)
  115. return;
  116. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  117. {
  118. if (graphics->GetRenderTarget(i) == this)
  119. graphics->ResetRenderTarget(i);
  120. }
  121. if (graphics->GetDepthStencil() == this)
  122. graphics->ResetDepthStencil();
  123. // Clean up also from non-active FBOs
  124. graphics->CleanupRenderSurface(this);
  125. renderBuffer_ = 0;
  126. }
  127. void RenderSurface::Release()
  128. {
  129. Graphics* graphics = parentTexture_->GetGraphics();
  130. if (!graphics)
  131. return;
  132. if (!graphics->IsDeviceLost())
  133. {
  134. for (unsigned i = 0; i < MAX_RENDERTARGETS; ++i)
  135. {
  136. if (graphics->GetRenderTarget(i) == this)
  137. graphics->ResetRenderTarget(i);
  138. }
  139. if (graphics->GetDepthStencil() == this)
  140. graphics->ResetDepthStencil();
  141. // Clean up also from non-active FBOs
  142. graphics->CleanupRenderSurface(this);
  143. if (renderBuffer_)
  144. glDeleteRenderbuffersEXT(1, &renderBuffer_);
  145. }
  146. renderBuffer_ = 0;
  147. }
  148. int RenderSurface::GetWidth() const
  149. {
  150. return parentTexture_->GetWidth();
  151. }
  152. int RenderSurface::GetHeight() const
  153. {
  154. return parentTexture_->GetHeight();
  155. }
  156. TextureUsage RenderSurface::GetUsage() const
  157. {
  158. return parentTexture_->GetUsage();
  159. }
  160. Viewport* RenderSurface::GetViewport(unsigned index) const
  161. {
  162. return index < viewports_.Size() ? viewports_[index] : (Viewport*)0;
  163. }
  164. void RenderSurface::SetTarget(unsigned target)
  165. {
  166. target_ = target;
  167. }
  168. void RenderSurface::WasUpdated()
  169. {
  170. updateQueued_ = false;
  171. }
  172. }