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