CmWin32RenderTexture.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "CmRenderSystem.h"
  25. #include "CmException.h"
  26. #include "CmWin32RenderTexture.h"
  27. #include "CmWin32GLSupport.h"
  28. #include "CmWin32Context.h"
  29. namespace CamelotEngine {
  30. Win32PBuffer::Win32PBuffer(PixelComponentType format, size_t width, size_t height):
  31. GLPBuffer(format, width, height),
  32. mContext(0)
  33. {
  34. createPBuffer();
  35. // Create context
  36. mContext = new Win32Context(mHDC, mGlrc);
  37. #if 0
  38. if(mUseBind)
  39. {
  40. // Bind texture
  41. glBindTextureEXT(GL_TEXTURE_2D, static_cast<GLTexture*>(mTexture.get())->getGLID());
  42. wglBindTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
  43. }
  44. #endif
  45. }
  46. Win32PBuffer::~Win32PBuffer()
  47. {
  48. #if 0
  49. if(mUseBind)
  50. {
  51. // Unbind texture
  52. glBindTextureEXT(GL_TEXTURE_2D,
  53. static_cast<GLTexture*>(mTexture.get())->getGLID());
  54. glBindTextureEXT(GL_TEXTURE_2D,
  55. static_cast<GLTexture*>(mTexture.get())->getGLID());
  56. wglReleaseTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
  57. }
  58. #endif
  59. // Unregister and destroy mContext
  60. delete mContext;
  61. // Destroy pbuffer
  62. destroyPBuffer();
  63. }
  64. void Win32PBuffer::createPBuffer()
  65. {
  66. // Process format
  67. int bits=0;
  68. bool isFloat=false;
  69. #if 0
  70. bool hasAlpha=true;
  71. #endif
  72. switch(mFormat)
  73. {
  74. case PCT_BYTE:
  75. bits=8; isFloat=false;
  76. break;
  77. case PCT_SHORT:
  78. bits=16; isFloat=false;
  79. break;
  80. case PCT_FLOAT16:
  81. bits=16; isFloat=true;
  82. break;
  83. case PCT_FLOAT32:
  84. bits=32; isFloat=true;
  85. break;
  86. default: break;
  87. };
  88. HDC old_hdc = wglGetCurrentDC();
  89. HGLRC old_context = wglGetCurrentContext();
  90. // Bind to RGB or RGBA texture
  91. int bttype = 0;
  92. #if 0
  93. if(mUseBind)
  94. {
  95. // Only provide bind type when actually binding
  96. bttype = PixelUtil::hasAlpha(mInternalFormat)?
  97. WGL_BIND_TO_TEXTURE_RGBA_ARB : WGL_BIND_TO_TEXTURE_RGB_ARB;
  98. }
  99. int texformat = hasAlpha?
  100. WGL_TEXTURE_RGBA_ARB : WGL_TEXTURE_RGB_ARB;
  101. #endif
  102. // Make a float buffer?
  103. int pixeltype = isFloat?
  104. WGL_TYPE_RGBA_FLOAT_ARB: WGL_TYPE_RGBA_ARB;
  105. int attrib[] = {
  106. WGL_RED_BITS_ARB,bits,
  107. WGL_GREEN_BITS_ARB,bits,
  108. WGL_BLUE_BITS_ARB,bits,
  109. WGL_ALPHA_BITS_ARB,bits,
  110. WGL_STENCIL_BITS_ARB,1,
  111. WGL_DEPTH_BITS_ARB,15,
  112. WGL_DRAW_TO_PBUFFER_ARB,true,
  113. WGL_SUPPORT_OPENGL_ARB,true,
  114. WGL_PIXEL_TYPE_ARB,pixeltype,
  115. //WGL_DOUBLE_BUFFER_ARB,true,
  116. //WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB, // Make sure it is accelerated
  117. bttype,true, // must be last, as bttype can be zero
  118. 0
  119. };
  120. int pattrib_default[] = {
  121. 0
  122. };
  123. #if 0
  124. int pattrib_bind[] = {
  125. WGL_TEXTURE_FORMAT_ARB, texformat,
  126. WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB,
  127. WGL_PBUFFER_LARGEST_ARB, true,
  128. 0
  129. };
  130. #endif
  131. int format;
  132. unsigned int count;
  133. // Choose suitable pixel format
  134. wglChoosePixelFormatARB(old_hdc,attrib,NULL,1,&format,&count);
  135. if(count == 0)
  136. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglChoosePixelFormatARB() failed", " Win32PBuffer::createPBuffer");
  137. // Analyse pixel format
  138. const int piAttributes[]={
  139. WGL_RED_BITS_ARB,WGL_GREEN_BITS_ARB,WGL_BLUE_BITS_ARB,WGL_ALPHA_BITS_ARB,
  140. WGL_DEPTH_BITS_ARB,WGL_STENCIL_BITS_ARB
  141. };
  142. int piValues[sizeof(piAttributes)/sizeof(const int)];
  143. wglGetPixelFormatAttribivARB(old_hdc,format,0,sizeof(piAttributes)/sizeof(const int),piAttributes,piValues);
  144. mPBuffer = wglCreatePbufferARB(old_hdc,format,mWidth,mHeight,pattrib_default);
  145. if(!mPBuffer)
  146. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglCreatePbufferARB() failed", " Win32PBuffer::createPBuffer");
  147. mHDC = wglGetPbufferDCARB(mPBuffer);
  148. if(!mHDC) {
  149. wglDestroyPbufferARB(mPBuffer);
  150. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglGetPbufferDCARB() failed", " Win32PBuffer::createPBuffer");
  151. }
  152. mGlrc = wglCreateContext(mHDC);
  153. if(!mGlrc) {
  154. wglReleasePbufferDCARB(mPBuffer,mHDC);
  155. wglDestroyPbufferARB(mPBuffer);
  156. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglCreateContext() failed", " Win32PBuffer::createPBuffer");
  157. }
  158. if(!wglShareLists(old_context,mGlrc)) {
  159. wglDeleteContext(mGlrc);
  160. wglReleasePbufferDCARB(mPBuffer,mHDC);
  161. wglDestroyPbufferARB(mPBuffer);
  162. OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "wglShareLists() failed", " Win32PBuffer::createPBuffer");
  163. }
  164. // Query real width and height
  165. int iWidth, iHeight;
  166. wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_WIDTH_ARB, &iWidth);
  167. wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_HEIGHT_ARB, &iHeight);
  168. mWidth = iWidth;
  169. mHeight = iHeight;
  170. }
  171. void Win32PBuffer::destroyPBuffer()
  172. {
  173. wglDeleteContext(mGlrc);
  174. wglReleasePbufferDCARB(mPBuffer,mHDC);
  175. wglDestroyPbufferARB(mPBuffer);
  176. }
  177. }