CmGLPBRenderTexture.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "CmGLHardwarePixelBuffer.h"
  28. namespace CamelotEngine {
  29. //-----------------------------------------------------------------------------
  30. GLPBuffer::GLPBuffer(PixelComponentType format, UINT32 width, UINT32 height):
  31. mFormat(format),
  32. mWidth(width),
  33. mHeight(height)
  34. {
  35. }
  36. GLPBuffer::~GLPBuffer()
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. GLPBRenderTexture::GLPBRenderTexture(GLPBRTTManager *manager, const String &name,
  41. const GLSurfaceDesc &target, bool writeGamma, UINT32 fsaa):
  42. GLRenderTexture(name, target, writeGamma, fsaa),
  43. mManager(manager)
  44. {
  45. mPBFormat = PixelUtil::getComponentType(target.buffer->getFormat());
  46. mManager->requestPBuffer(mPBFormat, mWidth, mHeight);
  47. }
  48. GLPBRenderTexture::~GLPBRenderTexture()
  49. {
  50. // Release PBuffer
  51. mManager->releasePBuffer(mPBFormat);
  52. }
  53. void GLPBRenderTexture::getCustomAttribute_internal(const String& name, void* pData)
  54. {
  55. if(name=="TARGET")
  56. {
  57. GLSurfaceDesc &target = *static_cast<GLSurfaceDesc*>(pData);
  58. target.buffer = static_cast<GLHardwarePixelBuffer*>(mBuffer);
  59. target.zoffset = mZOffset;
  60. }
  61. else if(name=="GLCONTEXT")
  62. {
  63. // Get PBuffer for our internal format
  64. *static_cast<GLContext**>(pData) = mManager->getContextFor(mPBFormat, mWidth, mHeight);
  65. }
  66. }
  67. //-----------------------------------------------------------------------------
  68. GLPBRTTManager::GLPBRTTManager(GLSupport *support, RenderTarget *mainwindow):
  69. mSupport(support),
  70. mMainWindow(mainwindow),
  71. mMainContext(0)
  72. {
  73. mMainWindow->getCustomAttribute_internal("GLCONTEXT", &mMainContext);
  74. }
  75. GLPBRTTManager::~GLPBRTTManager()
  76. {
  77. // Delete remaining PBuffers
  78. for(size_t x=0; x<PCT_COUNT; ++x)
  79. {
  80. delete mPBuffers[x].pb;
  81. }
  82. }
  83. RenderTexture *GLPBRTTManager::createRenderTexture(const String &name,
  84. const GLSurfaceDesc &target, bool writeGamma, UINT32 fsaa)
  85. {
  86. return new GLPBRenderTexture(this, name, target, writeGamma, fsaa);
  87. }
  88. bool GLPBRTTManager::checkFormat(PixelFormat format)
  89. {
  90. return true;
  91. }
  92. void GLPBRTTManager::bind(RenderTarget *target)
  93. {
  94. // Nothing to do here
  95. // Binding of context is done by GL subsystem, as contexts are also used for RenderWindows
  96. }
  97. void GLPBRTTManager::unbind(RenderTarget *target)
  98. {
  99. // Copy on unbind
  100. GLSurfaceDesc surface;
  101. surface.buffer = 0;
  102. target->getCustomAttribute_internal("TARGET", &surface);
  103. if(surface.buffer)
  104. static_cast<GLTextureBuffer*>(surface.buffer)->copyFromFramebuffer(surface.zoffset);
  105. }
  106. void GLPBRTTManager::requestPBuffer(PixelComponentType ctype, UINT32 width, UINT32 height)
  107. {
  108. //Check size
  109. if(mPBuffers[ctype].pb)
  110. {
  111. if(mPBuffers[ctype].pb->getWidth()<width || mPBuffers[ctype].pb->getHeight()<height)
  112. {
  113. // If the current PBuffer is too small, destroy it and create a new one
  114. delete mPBuffers[ctype].pb;
  115. mPBuffers[ctype].pb = 0;
  116. }
  117. }
  118. if(!mPBuffers[ctype].pb)
  119. {
  120. // Create pbuffer via rendersystem
  121. mPBuffers[ctype].pb = mSupport->createPBuffer(ctype, width, height);
  122. }
  123. ++mPBuffers[ctype].refcount;
  124. }
  125. void GLPBRTTManager::releasePBuffer(PixelComponentType ctype)
  126. {
  127. --mPBuffers[ctype].refcount;
  128. if(mPBuffers[ctype].refcount == 0)
  129. {
  130. delete mPBuffers[ctype].pb;
  131. mPBuffers[ctype].pb = 0;
  132. }
  133. }
  134. GLContext *GLPBRTTManager::getContextFor(PixelComponentType ctype, UINT32 width, UINT32 height)
  135. {
  136. // Faster to return main context if the RTT is smaller than the window size
  137. // and ctype is PCT_BYTE. This must be checked every time because the window might have been resized
  138. if(ctype == PCT_BYTE)
  139. {
  140. if(width <= mMainWindow->getWidth() && height <= mMainWindow->getHeight())
  141. return mMainContext;
  142. }
  143. assert(mPBuffers[ctype].pb);
  144. return mPBuffers[ctype].pb->getContext();
  145. }
  146. //---------------------------------------------------------------------------------------------
  147. }