OGLRenderSurface.cpp 5.6 KB

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