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 |
|
---|
7 | Copyright (c) 2000-2005 The OGRE Team
|
---|
8 | Also see acknowledgements in Readme.html
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify it under
|
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software
|
---|
12 | Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT
|
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
|
---|
18 |
|
---|
19 | You should have received a copy of the GNU Lesser General Public License along with
|
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple
|
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to
|
---|
22 | http://www.gnu.org/copyleft/lesser.txt.
|
---|
23 | -----------------------------------------------------------------------------
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include "OgreRoot.h"
|
---|
27 | #include "OgreLogManager.h"
|
---|
28 | #include "OgreRenderSystem.h"
|
---|
29 | #include "OgreImageCodec.h"
|
---|
30 | #include "OgreException.h"
|
---|
31 |
|
---|
32 | #include "OgreWin32RenderTexture.h"
|
---|
33 | #include "OgreWin32GLSupport.h"
|
---|
34 | #include "OgreWin32Context.h"
|
---|
35 |
|
---|
36 | // ATI float extension
|
---|
37 | #ifndef WGL_ATI_pixel_format_float
|
---|
38 | #define WGL_ATI_pixel_format_float 1
|
---|
39 |
|
---|
40 | #define WGL_TYPE_RGBA_FLOAT_ATI 0x21A0
|
---|
41 | #define GL_TYPE_RGBA_FLOAT_ATI 0x8820
|
---|
42 | #define GL_COLOR_CLEAR_UNCLAMPED_VALUE_ATI 0x8835
|
---|
43 | #endif
|
---|
44 |
|
---|
45 |
|
---|
46 | namespace Ogre {
|
---|
47 |
|
---|
48 | static PFNWGLCHOOSEPIXELFORMATARBPROC _wglChoosePixelFormatARB = 0;
|
---|
49 | static PFNWGLCREATEPBUFFERARBPROC _wglCreatePbufferARB = 0;
|
---|
50 | static PFNWGLGETPBUFFERDCARBPROC _wglGetPbufferDCARB = 0;
|
---|
51 | static PFNWGLRELEASEPBUFFERDCARBPROC _wglReleasePbufferDCARB = 0;
|
---|
52 | static PFNWGLDESTROYPBUFFERARBPROC _wglDestroyPbufferARB = 0;
|
---|
53 | static PFNWGLQUERYPBUFFERARBPROC _wglQueryPbufferARB = 0;
|
---|
54 | static PFNWGLBINDTEXIMAGEARBPROC _wglBindTexImageARB = 0;
|
---|
55 | static PFNWGLRELEASETEXIMAGEARBPROC _wglReleaseTexImageARB = 0;
|
---|
56 | static PFNWGLGETPIXELFORMATATTRIBIVARBPROC _wglGetPixelFormatAttribivARB = 0;
|
---|
57 |
|
---|
58 | Win32RenderTexture::Win32RenderTexture(Win32GLSupport &glsupport, const String & name,
|
---|
59 | unsigned int width, unsigned int height,
|
---|
60 | TextureType texType, PixelFormat internalFormat,
|
---|
61 | const NameValuePairList *miscParams, bool useBind ):
|
---|
62 | GLRenderTexture(name, width, height, texType, internalFormat, miscParams),
|
---|
63 | mGLSupport(glsupport),
|
---|
64 | mContext(0), mUseBind(useBind)
|
---|
65 | {
|
---|
66 | if(!_wglChoosePixelFormatARB) _wglChoosePixelFormatARB = (PFNWGLCHOOSEPIXELFORMATARBPROC)wglGetProcAddress("wglChoosePixelFormatARB");
|
---|
67 | if(!_wglCreatePbufferARB) _wglCreatePbufferARB = (PFNWGLCREATEPBUFFERARBPROC)wglGetProcAddress("wglCreatePbufferARB");
|
---|
68 | if(!_wglGetPbufferDCARB) _wglGetPbufferDCARB = (PFNWGLGETPBUFFERDCARBPROC)wglGetProcAddress("wglGetPbufferDCARB");
|
---|
69 | if(!_wglReleasePbufferDCARB) _wglReleasePbufferDCARB = (PFNWGLRELEASEPBUFFERDCARBPROC)wglGetProcAddress("wglReleasePbufferDCARB");
|
---|
70 | if(!_wglDestroyPbufferARB) _wglDestroyPbufferARB = (PFNWGLDESTROYPBUFFERARBPROC)wglGetProcAddress("wglDestroyPbufferARB");
|
---|
71 | if(!_wglQueryPbufferARB) _wglQueryPbufferARB = (PFNWGLQUERYPBUFFERARBPROC)wglGetProcAddress("wglQueryPbufferARB");
|
---|
72 | if(!_wglGetPixelFormatAttribivARB) _wglGetPixelFormatAttribivARB = (PFNWGLGETPIXELFORMATATTRIBIVARBPROC)wglGetProcAddress("wglGetPixelFormatAttribivARB");
|
---|
73 | if(mUseBind)
|
---|
74 | {
|
---|
75 | if(!_wglBindTexImageARB) _wglBindTexImageARB = (PFNWGLBINDTEXIMAGEARBPROC)wglGetProcAddress("wglBindTexImageARB");
|
---|
76 | if(!_wglReleaseTexImageARB) _wglReleaseTexImageARB = (PFNWGLRELEASETEXIMAGEARBPROC)wglGetProcAddress("wglReleaseTexImageARB");
|
---|
77 | }
|
---|
78 |
|
---|
79 | createPBuffer();
|
---|
80 |
|
---|
81 | // Create context
|
---|
82 | mContext = new Win32Context(mHDC, mGlrc);
|
---|
83 | // Register the context with the rendersystem
|
---|
84 | GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());
|
---|
85 | rs->_registerContext(this, mContext);
|
---|
86 |
|
---|
87 | if(mUseBind)
|
---|
88 | {
|
---|
89 | // Bind texture
|
---|
90 | glBindTexture(GL_TEXTURE_2D, static_cast<GLTexture*>(mTexture.get())->getGLID());
|
---|
91 | _wglBindTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | Win32RenderTexture::~Win32RenderTexture()
|
---|
95 | {
|
---|
96 | if(mUseBind)
|
---|
97 | {
|
---|
98 | // Unbind texture
|
---|
99 | glBindTexture(GL_TEXTURE_2D,
|
---|
100 | static_cast<GLTexture*>(mTexture.get())->getGLID());
|
---|
101 | glBindTexture(GL_TEXTURE_2D,
|
---|
102 | static_cast<GLTexture*>(mTexture.get())->getGLID());
|
---|
103 | _wglReleaseTexImageARB(mPBuffer, WGL_FRONT_LEFT_ARB);
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Unregister and destroy mContext
|
---|
107 | GLRenderSystem *rs = static_cast<GLRenderSystem*>(Root::getSingleton().getRenderSystem());
|
---|
108 | rs->_unregisterContext(this);
|
---|
109 | delete mContext;
|
---|
110 |
|
---|
111 | // Destroy pbuffer
|
---|
112 | destroyPBuffer();
|
---|
113 | }
|
---|
114 |
|
---|
115 | void Win32RenderTexture::_copyToTexture()
|
---|
116 | {
|
---|
117 | if(!mUseBind)
|
---|
118 | {
|
---|
119 | // Use old fasioned copying
|
---|
120 | GLRenderTexture::_copyToTexture();
|
---|
121 | }
|
---|
122 | }
|
---|
123 | /*
|
---|
124 | void Win32RenderTexture::firePreUpdate(void)
|
---|
125 | {
|
---|
126 | //SwapBuffers(mHDC);
|
---|
127 | // Enable current context
|
---|
128 | mGLSupport.pushContext(mHDC, mGlrc);
|
---|
129 | // Fire default preupdate
|
---|
130 | GLRenderTexture::firePreUpdate();
|
---|
131 | }
|
---|
132 | void Win32RenderTexture::firePostUpdate(void)
|
---|
133 | {
|
---|
134 | // Fire default postupdate
|
---|
135 | GLRenderTexture::firePostUpdate();
|
---|
136 | // Possibly enable previous context
|
---|
137 | mGLSupport.popContext();
|
---|
138 | }
|
---|
139 | */
|
---|
140 | void Win32RenderTexture::createPBuffer()
|
---|
141 | {
|
---|
142 | LogManager::getSingleton().logMessage(
|
---|
143 | "Win32RenderTexture::Creating PBuffer"
|
---|
144 | );
|
---|
145 |
|
---|
146 | HDC old_hdc = wglGetCurrentDC();
|
---|
147 | HGLRC old_context = wglGetCurrentContext();
|
---|
148 |
|
---|
149 | // Adjust pbuffer size to the texture size for best performance.
|
---|
150 | //
|
---|
151 | // Note: Since most NVIDIA card support rectangle texture (bear
|
---|
152 | // in mind the rectangle texture can't support mipmap at all), but
|
---|
153 | // we doesn't support this feature in texture, so, if the card
|
---|
154 | // doesn't support really NPOT texture, it'll resize to POT always.
|
---|
155 | // And, the NVIDIA card can create NPOT pbuffer since we wasn't
|
---|
156 | // tell it create a pbuffer that support really NPOT texture (In
|
---|
157 | // actually, I can't found a ways to create non-mipmaps support
|
---|
158 | // but must POT if not support really NPOT texture by card). So,
|
---|
159 | // seems NVIDIA card create NPOT pbuffer that intented to support
|
---|
160 | // rectangle texture.
|
---|
161 | //
|
---|
162 | // In my test (FX 5600), NPOT pbuffer bind to POT texture cause huge
|
---|
163 | // performance harm (In RenderToTexture demo, which frame-rate drop
|
---|
164 | // under 1 FPS!), it's work fine if both pbuffer and texture are POT,
|
---|
165 | // no matter size match exactly or not.
|
---|
166 | //
|
---|
167 | // Just set to same size here to avoid harm performance in case mentioned
|
---|
168 | // above, and doesn't lost of really NPOT texture support. Also notes set
|
---|
169 | // to difference size is useful in some situation (AA effect, etc).
|
---|
170 | //
|
---|
171 | mWidth = mTexture->getWidth();
|
---|
172 | mHeight = mTexture->getHeight();
|
---|
173 |
|
---|
174 | // Bind to RGB or RGBA texture
|
---|
175 | int bttype = 0;
|
---|
176 | if(mUseBind)
|
---|
177 | {
|
---|
178 | // Only provide bind type when actually binding
|
---|
179 | bttype = PixelUtil::hasAlpha(mInternalFormat)?
|
---|
180 | WGL_BIND_TO_TEXTURE_RGBA_ARB : WGL_BIND_TO_TEXTURE_RGB_ARB;
|
---|
181 | }
|
---|
182 | int texformat = PixelUtil::hasAlpha(mInternalFormat)?
|
---|
183 | WGL_TEXTURE_RGBA_ARB : WGL_TEXTURE_RGB_ARB;
|
---|
184 | // Make a float buffer?
|
---|
185 | int pixeltype = PixelUtil::isFloatingPoint(mInternalFormat)?
|
---|
186 | WGL_TYPE_RGBA_FLOAT_ATI : WGL_TYPE_RGBA_ARB;
|
---|
187 | // Get R,G,B,A depths
|
---|
188 | int depths[4];
|
---|
189 | PixelUtil::getBitDepths(mInternalFormat, depths);
|
---|
190 |
|
---|
191 | int attrib[] = {
|
---|
192 | WGL_RED_BITS_ARB,depths[0],
|
---|
193 | WGL_GREEN_BITS_ARB,depths[1],
|
---|
194 | WGL_BLUE_BITS_ARB,depths[2],
|
---|
195 | WGL_ALPHA_BITS_ARB,depths[3],
|
---|
196 | WGL_STENCIL_BITS_ARB,1,
|
---|
197 | WGL_DEPTH_BITS_ARB,15,
|
---|
198 | WGL_DRAW_TO_PBUFFER_ARB,true,
|
---|
199 | WGL_SUPPORT_OPENGL_ARB,true,
|
---|
200 | WGL_PIXEL_TYPE_ARB,pixeltype,
|
---|
201 | //WGL_DOUBLE_BUFFER_ARB,true,
|
---|
202 | //WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB, // Make sure it is accelerated
|
---|
203 | bttype,true, // must be last, as bttype can be zero
|
---|
204 | 0
|
---|
205 | };
|
---|
206 | int pattrib[] = {
|
---|
207 | WGL_TEXTURE_FORMAT_ARB, texformat,
|
---|
208 | WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB,
|
---|
209 | WGL_PBUFFER_LARGEST_ARB, true,
|
---|
210 | 0
|
---|
211 | };
|
---|
212 |
|
---|
213 | int format;
|
---|
214 | unsigned int count;
|
---|
215 |
|
---|
216 | // Choose suitable pixel format
|
---|
217 | _wglChoosePixelFormatARB(old_hdc,attrib,NULL,1,&format,&count);
|
---|
218 | if(count == 0)
|
---|
219 | OGRE_EXCEPT(0, "wglChoosePixelFormatARB() failed", "Win32RenderTexture::createPBuffer");
|
---|
220 |
|
---|
221 | // Analyse pixel format
|
---|
222 | const int piAttributes[]={
|
---|
223 | WGL_RED_BITS_ARB,WGL_GREEN_BITS_ARB,WGL_BLUE_BITS_ARB,WGL_ALPHA_BITS_ARB,
|
---|
224 | WGL_DEPTH_BITS_ARB,WGL_STENCIL_BITS_ARB
|
---|
225 | };
|
---|
226 | int piValues[sizeof(piAttributes)/sizeof(const int)];
|
---|
227 | wglGetPixelFormatAttribivARB(old_hdc,format,0,sizeof(piAttributes)/sizeof(const int),piAttributes,piValues);
|
---|
228 |
|
---|
229 | StringUtil::StrStreamType str;
|
---|
230 | str << "Win32RenderTexture::PBuffer -- Chosen pixel format rgba="
|
---|
231 | << piValues[0] << ","
|
---|
232 | << piValues[1] << ","
|
---|
233 | << piValues[2] << ","
|
---|
234 | << piValues[3]
|
---|
235 | << " depth=" << piValues[4]
|
---|
236 | << " stencil=" << piValues[5];
|
---|
237 | LogManager::getSingleton().logMessage(
|
---|
238 | LML_NORMAL, str.str());
|
---|
239 |
|
---|
240 | mPBuffer = _wglCreatePbufferARB(old_hdc,format,mWidth,mHeight,pattrib);
|
---|
241 | if(!mPBuffer)
|
---|
242 | OGRE_EXCEPT(0, "wglCreatePbufferARB() failed", "Win32RenderTexture::createPBuffer");
|
---|
243 |
|
---|
244 | mHDC = _wglGetPbufferDCARB(mPBuffer);
|
---|
245 | if(!mHDC) {
|
---|
246 | _wglDestroyPbufferARB(mPBuffer);
|
---|
247 | OGRE_EXCEPT(0, "wglGetPbufferDCARB() failed", "Win32RenderTexture::createPBuffer");
|
---|
248 | }
|
---|
249 |
|
---|
250 | mGlrc = wglCreateContext(mHDC);
|
---|
251 | if(!mGlrc) {
|
---|
252 | _wglReleasePbufferDCARB(mPBuffer,mHDC);
|
---|
253 | _wglDestroyPbufferARB(mPBuffer);
|
---|
254 | OGRE_EXCEPT(0, "wglCreateContext() failed", "Win32RenderTexture::createPBuffer");
|
---|
255 | }
|
---|
256 |
|
---|
257 | if(!wglShareLists(old_context,mGlrc)) {
|
---|
258 | wglDeleteContext(mGlrc);
|
---|
259 | _wglReleasePbufferDCARB(mPBuffer,mHDC);
|
---|
260 | _wglDestroyPbufferARB(mPBuffer);
|
---|
261 | OGRE_EXCEPT(0, "wglShareLists() failed", "Win32RenderTexture::createPBuffer");
|
---|
262 | }
|
---|
263 |
|
---|
264 | // Query real width and height
|
---|
265 | int iWidth, iHeight;
|
---|
266 | _wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_WIDTH_ARB, &iWidth);
|
---|
267 | _wglQueryPbufferARB(mPBuffer, WGL_PBUFFER_HEIGHT_ARB, &iHeight);
|
---|
268 | str.str(StringUtil::BLANK);
|
---|
269 | str << "Win32RenderTexture::PBuffer created -- Real dimensions "
|
---|
270 | << mWidth << "x" << mHeight;
|
---|
271 | LogManager::getSingleton().logMessage(LML_NORMAL, str.str());
|
---|
272 | mWidth = iWidth;
|
---|
273 | mHeight = iHeight;
|
---|
274 | }
|
---|
275 | void Win32RenderTexture::destroyPBuffer()
|
---|
276 | {
|
---|
277 | wglDeleteContext(mGlrc);
|
---|
278 | _wglReleasePbufferDCARB(mPBuffer,mHDC);
|
---|
279 | _wglDestroyPbufferARB(mPBuffer);
|
---|
280 | }
|
---|
281 |
|
---|
282 |
|
---|
283 | }
|
---|