1 /**************************************************************************
2 * Copyright 2015 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 #define WGL_WGLEXT_PROTOTYPES
32 #include <GL/wglext.h>
34 #include "state_tracker/st_copytex.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_state.h"
41 #include "stw_context.h"
42 #include "stw_device.h"
43 #include "stw_pixelformat.h"
44 #include "stw_framebuffer.h"
48 /** Translate a WGL buffer name to a GLenum */
50 translate_ibuffer(int iBuffer
)
53 case WGL_FRONT_LEFT_ARB
:
55 case WGL_BACK_LEFT_ARB
:
57 case WGL_FRONT_RIGHT_ARB
:
58 return GL_FRONT_RIGHT
;
59 case WGL_BACK_RIGHT_ARB
:
69 /** Translate a WGL texture target type to a GLenum */
71 translate_target(unsigned textureTarget
)
73 switch (textureTarget
) {
74 case WGL_TEXTURE_1D_ARB
:
76 case WGL_TEXTURE_2D_ARB
:
78 case WGL_TEXTURE_CUBE_MAP_ARB
:
79 return GL_TEXTURE_CUBE_MAP
;
80 case WGL_NO_TEXTURE_ARB
:
87 /** Translate a WGL texture format to a GLenum */
89 translate_texture_format(unsigned wgl_format
)
92 case WGL_TEXTURE_RGB_ARB
:
94 case WGL_TEXTURE_RGBA_ARB
:
96 case WGL_NO_TEXTURE_ARB
:
104 wglBindTexImageARB(HPBUFFERARB hPbuffer
, int iBuffer
)
106 HDC prevDrawable
= stw_get_current_dc();
107 HDC prevReadable
= stw_get_current_read_dc();
109 struct stw_context
*curctx
= stw_current_context();
110 struct stw_framebuffer
*fb
;
111 GLenum texFormat
, srcBuffer
, target
;
116 * Implementation notes:
117 * Ideally, we'd implement this function with the
118 * st_context_iface::teximage() function which replaces a specific
119 * texture image with a different resource (the pbuffer).
120 * The main problem however, is the pbuffer image is upside down relative
121 * to the texture image.
122 * Window system drawing surfaces (windows & pbuffers) are "top to bottom"
123 * while OpenGL texture images are "bottom to top". One possible solution
124 * to this is to invert rendering to pbuffers (as we do for renderbuffers)
125 * but that could lead to other issues (and would require extensive
128 * The simple alternative is to use a copy-based approach which copies the
129 * pbuffer image into the texture via glCopyTex[Sub]Image. That's what
134 debug_printf("No rendering context in wglBindTexImageARB()\n");
135 SetLastError(ERROR_INVALID_OPERATION
);
139 fb
= stw_framebuffer_from_HPBUFFERARB(hPbuffer
);
141 debug_printf("Invalid pbuffer handle in wglBindTexImageARB()\n");
142 SetLastError(ERROR_INVALID_HANDLE
);
146 srcBuffer
= translate_ibuffer(iBuffer
);
147 if (srcBuffer
== GL_NONE
) {
148 debug_printf("Invalid buffer 0x%x in wglBindTexImageARB()\n", iBuffer
);
149 SetLastError(ERROR_INVALID_DATA
);
153 target
= translate_target(fb
->textureTarget
);
154 if (target
== GL_NONE
) {
155 debug_printf("no texture target in wglBindTexImageARB()\n");
159 texFormat
= translate_texture_format(fb
->textureFormat
);
160 if (texFormat
== GL_NONE
) {
161 debug_printf("no texture format in wglBindTexImageARB()\n");
166 * Bind the pbuffer surface so we can read/copy from it.
168 * Before we can call stw_make_current() we have to temporarily
169 * change the pbuffer's pixel format to match the context to avoid
170 * an error condition. After the stw_make_current() we restore the
171 * buffer's pixel format.
173 pixelFormatSave
= fb
->iPixelFormat
;
174 fb
->iPixelFormat
= curctx
->iPixelFormat
;
175 dc
= wglGetPbufferDCARB(hPbuffer
);
176 retVal
= stw_make_current(dc
, dc
, curctx
->dhglrc
);
177 fb
->iPixelFormat
= pixelFormatSave
;
179 debug_printf("stw_make_current(#1) failed in wglBindTexImageARB()\n");
180 wglReleasePbufferDCARB(hPbuffer
, dc
);
184 st_copy_framebuffer_to_texture(srcBuffer
, fb
->width
, fb
->height
,
185 target
, fb
->textureLevel
,
186 fb
->textureFace
, texFormat
);
188 /* rebind previous drawing surface */
189 retVal
= stw_make_current(prevDrawable
, prevReadable
, curctx
->dhglrc
);
191 debug_printf("stw_make_current(#2) failed in wglBindTexImageARB()\n");
194 wglReleasePbufferDCARB(hPbuffer
, dc
);
201 wglReleaseTexImageARB(HPBUFFERARB hPbuffer
, int iBuffer
)
203 struct stw_framebuffer
*fb
= stw_framebuffer_from_HPBUFFERARB(hPbuffer
);
206 /* nothing to do here, but we do error checking anyway */
209 debug_printf("Invalid pbuffer handle in wglReleaseTexImageARB()\n");
210 SetLastError(ERROR_INVALID_HANDLE
);
214 srcBuffer
= translate_ibuffer(iBuffer
);
215 if (srcBuffer
== GL_NONE
) {
216 debug_printf("Invalid buffer 0x%x in wglReleaseTexImageARB()\n", iBuffer
);
217 SetLastError(ERROR_INVALID_DATA
);
226 wglSetPbufferAttribARB(HPBUFFERARB hPbuffer
, const int *piAttribList
)
228 struct stw_framebuffer
*fb
= stw_framebuffer_from_HPBUFFERARB(hPbuffer
);
232 SetLastError(ERROR_INVALID_HANDLE
);
236 for (i
= 0; piAttribList
[i
]; i
+= 2) {
237 switch (piAttribList
[i
]) {
238 case WGL_MIPMAP_LEVEL_ARB
:
239 fb
->textureLevel
= piAttribList
[i
+1];
241 case WGL_CUBE_MAP_FACE_ARB
:
242 face
= piAttribList
[i
+1];
243 if (face
>= WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
&&
244 face
<= WGL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
) {
245 fb
->textureFace
= face
- WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
;
248 debug_printf("Invalid cube face 0x%x in "
249 "wglSetPbufferAttribARB()\n",
251 SetLastError(ERROR_INVALID_DATA
);
256 debug_printf("Invalid attribute 0x%x in wglSetPbufferAttribARB()\n",
258 SetLastError(ERROR_INVALID_DATA
);