CmGLPBRenderTexture.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLPBRenderTexture.h"
  25. #include "CmGLContext.h"
  26. #include "CmGLPixelFormat.h"
  27. #include "OgreStringConverter.h"
  28. #include "CmGLHardwarePixelBuffer.h"
  29. namespace CamelotEngine {
  30. //-----------------------------------------------------------------------------
  31. GLPBuffer::GLPBuffer(PixelComponentType format, size_t width, size_t height):
  32. mFormat(format),
  33. mWidth(width),
  34. mHeight(height)
  35. {
  36. }
  37. GLPBuffer::~GLPBuffer()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. GLPBRenderTexture::GLPBRenderTexture(GLPBRTTManager *manager, const String &name,
  42. const GLSurfaceDesc &target, bool writeGamma, UINT32 fsaa):
  43. GLRenderTexture(name, target, writeGamma, fsaa),
  44. mManager(manager)
  45. {
  46. mPBFormat = PixelUtil::getComponentType(target.buffer->getFormat());
  47. mManager->requestPBuffer(mPBFormat, mWidth, mHeight);
  48. }
  49. GLPBRenderTexture::~GLPBRenderTexture()
  50. {
  51. // Release PBuffer
  52. mManager->releasePBuffer(mPBFormat);
  53. }
  54. void GLPBRenderTexture::getCustomAttribute(const String& name, void* pData)
  55. {
  56. if(name=="TARGET")
  57. {
  58. GLSurfaceDesc &target = *static_cast<GLSurfaceDesc*>(pData);
  59. target.buffer = static_cast<GLHardwarePixelBuffer*>(mBuffer);
  60. target.zoffset = mZOffset;
  61. }
  62. else if(name=="GLCONTEXT")
  63. {
  64. // Get PBuffer for our internal format
  65. *static_cast<GLContext**>(pData) = mManager->getContextFor(mPBFormat, mWidth, mHeight);
  66. }
  67. }
  68. //-----------------------------------------------------------------------------
  69. GLPBRTTManager::GLPBRTTManager(GLSupport *support, RenderTarget *mainwindow):
  70. mSupport(support),
  71. mMainWindow(mainwindow),
  72. mMainContext(0)
  73. {
  74. mMainWindow->getCustomAttribute("GLCONTEXT", &mMainContext);
  75. }
  76. GLPBRTTManager::~GLPBRTTManager()
  77. {
  78. // Delete remaining PBuffers
  79. for(size_t x=0; x<PCT_COUNT; ++x)
  80. {
  81. delete mPBuffers[x].pb;
  82. }
  83. }
  84. RenderTexture *GLPBRTTManager::createRenderTexture(const String &name,
  85. const GLSurfaceDesc &target, bool writeGamma, UINT32 fsaa)
  86. {
  87. return new GLPBRenderTexture(this, name, target, writeGamma, fsaa);
  88. }
  89. bool GLPBRTTManager::checkFormat(PixelFormat format)
  90. {
  91. return true;
  92. }
  93. void GLPBRTTManager::bind(RenderTarget *target)
  94. {
  95. // Nothing to do here
  96. // Binding of context is done by GL subsystem, as contexts are also used for RenderWindows
  97. }
  98. void GLPBRTTManager::unbind(RenderTarget *target)
  99. {
  100. // Copy on unbind
  101. GLSurfaceDesc surface;
  102. surface.buffer = 0;
  103. target->getCustomAttribute("TARGET", &surface);
  104. if(surface.buffer)
  105. static_cast<GLTextureBuffer*>(surface.buffer)->copyFromFramebuffer(surface.zoffset);
  106. }
  107. void GLPBRTTManager::requestPBuffer(PixelComponentType ctype, size_t width, size_t height)
  108. {
  109. //Check size
  110. if(mPBuffers[ctype].pb)
  111. {
  112. if(mPBuffers[ctype].pb->getWidth()<width || mPBuffers[ctype].pb->getHeight()<height)
  113. {
  114. // If the current PBuffer is too small, destroy it and create a new one
  115. delete mPBuffers[ctype].pb;
  116. mPBuffers[ctype].pb = 0;
  117. }
  118. }
  119. if(!mPBuffers[ctype].pb)
  120. {
  121. // Create pbuffer via rendersystem
  122. mPBuffers[ctype].pb = mSupport->createPBuffer(ctype, width, height);
  123. }
  124. ++mPBuffers[ctype].refcount;
  125. }
  126. void GLPBRTTManager::releasePBuffer(PixelComponentType ctype)
  127. {
  128. --mPBuffers[ctype].refcount;
  129. if(mPBuffers[ctype].refcount == 0)
  130. {
  131. delete mPBuffers[ctype].pb;
  132. mPBuffers[ctype].pb = 0;
  133. }
  134. }
  135. GLContext *GLPBRTTManager::getContextFor(PixelComponentType ctype, size_t width, size_t height)
  136. {
  137. // Faster to return main context if the RTT is smaller than the window size
  138. // and ctype is PCT_BYTE. This must be checked every time because the window might have been resized
  139. if(ctype == PCT_BYTE)
  140. {
  141. if(width <= mMainWindow->getWidth() && height <= mMainWindow->getHeight())
  142. return mMainContext;
  143. }
  144. assert(mPBuffers[ctype].pb);
  145. return mPBuffers[ctype].pb->getContext();
  146. }
  147. //---------------------------------------------------------------------------------------------
  148. }