1 /**************************************************************************
2 * Copyright 2015 VMware, Inc.
3 * All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 **************************************************************************/
26
27 #include <windows.h>
28
29 #define WGL_WGLEXT_PROTOTYPES
30
31 #include <GL/gl.h>
32 #include <GL/wglext.h>
33
34 #include "state_tracker/st_copytex.h"
35
36 #include "pipe/p_defines.h"
37 #include "pipe/p_screen.h"
38 #include "pipe/p_state.h"
39
40 #include "stw_icd.h"
41 #include "stw_context.h"
42 #include "stw_device.h"
43 #include "stw_pixelformat.h"
44 #include "stw_framebuffer.h"
45 #include "stw_st.h"
46
47
48 /** Translate a WGL buffer name to a GLenum */
49 static GLenum
translate_ibuffer(int iBuffer)50 translate_ibuffer(int iBuffer)
51 {
52 switch (iBuffer) {
53 case WGL_FRONT_LEFT_ARB:
54 return GL_FRONT_LEFT;
55 case WGL_BACK_LEFT_ARB:
56 return GL_BACK_LEFT;
57 case WGL_FRONT_RIGHT_ARB:
58 return GL_FRONT_RIGHT;
59 case WGL_BACK_RIGHT_ARB:
60 return GL_BACK_RIGHT;
61 case WGL_AUX0_ARB:
62 return GL_AUX0;
63 default:
64 return GL_NONE;
65 }
66 }
67
68
69 /** Translate a WGL texture target type to a GLenum */
70 static GLenum
translate_target(unsigned textureTarget)71 translate_target(unsigned textureTarget)
72 {
73 switch (textureTarget) {
74 case WGL_TEXTURE_1D_ARB:
75 return GL_TEXTURE_1D;
76 case WGL_TEXTURE_2D_ARB:
77 return GL_TEXTURE_2D;
78 case WGL_TEXTURE_CUBE_MAP_ARB:
79 return GL_TEXTURE_CUBE_MAP;
80 case WGL_NO_TEXTURE_ARB:
81 default:
82 return GL_NONE;
83 }
84 }
85
86
87 /** Translate a WGL texture format to a GLenum */
88 static GLenum
translate_texture_format(unsigned wgl_format)89 translate_texture_format(unsigned wgl_format)
90 {
91 switch (wgl_format) {
92 case WGL_TEXTURE_RGB_ARB:
93 return GL_RGB;
94 case WGL_TEXTURE_RGBA_ARB:
95 return GL_RGBA;
96 case WGL_NO_TEXTURE_ARB:
97 default:
98 return GL_NONE;
99 }
100 }
101
102
103 BOOL WINAPI
wglBindTexImageARB(HPBUFFERARB hPbuffer,int iBuffer)104 wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
105 {
106 HDC prevDrawable = stw_get_current_dc();
107 HDC prevReadable = stw_get_current_read_dc();
108 HDC dc;
109 struct stw_context *curctx = stw_current_context();
110 struct stw_framebuffer *fb;
111 GLenum texFormat, srcBuffer, target;
112 boolean retVal;
113 int pixelFormatSave;
114
115 /*
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
126 * testing).
127 *
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
130 * we do here.
131 */
132
133 if (!curctx) {
134 debug_printf("No rendering context in wglBindTexImageARB()\n");
135 SetLastError(ERROR_INVALID_OPERATION);
136 return FALSE;
137 }
138
139 fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
140 if (!fb) {
141 debug_printf("Invalid pbuffer handle in wglBindTexImageARB()\n");
142 SetLastError(ERROR_INVALID_HANDLE);
143 return FALSE;
144 }
145
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);
150 return FALSE;
151 }
152
153 target = translate_target(fb->textureTarget);
154 if (target == GL_NONE) {
155 debug_printf("no texture target in wglBindTexImageARB()\n");
156 return FALSE;
157 }
158
159 texFormat = translate_texture_format(fb->textureFormat);
160 if (texFormat == GL_NONE) {
161 debug_printf("no texture format in wglBindTexImageARB()\n");
162 return FALSE;
163 }
164
165 /*
166 * Bind the pbuffer surface so we can read/copy from it.
167 *
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.
172 */
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;
178 if (!retVal) {
179 debug_printf("stw_make_current(#1) failed in wglBindTexImageARB()\n");
180 wglReleasePbufferDCARB(hPbuffer, dc);
181 return FALSE;
182 }
183
184 st_copy_framebuffer_to_texture(srcBuffer, fb->width, fb->height,
185 target, fb->textureLevel,
186 fb->textureFace, texFormat);
187
188 /* rebind previous drawing surface */
189 retVal = stw_make_current(prevDrawable, prevReadable, curctx->dhglrc);
190 if (!retVal) {
191 debug_printf("stw_make_current(#2) failed in wglBindTexImageARB()\n");
192 }
193
194 wglReleasePbufferDCARB(hPbuffer, dc);
195
196 return retVal;
197 }
198
199
200 BOOL WINAPI
wglReleaseTexImageARB(HPBUFFERARB hPbuffer,int iBuffer)201 wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
202 {
203 struct stw_framebuffer *fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
204 GLenum srcBuffer;
205
206 /* nothing to do here, but we do error checking anyway */
207
208 if (!fb) {
209 debug_printf("Invalid pbuffer handle in wglReleaseTexImageARB()\n");
210 SetLastError(ERROR_INVALID_HANDLE);
211 return FALSE;
212 }
213
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);
218 return FALSE;
219 }
220
221 return TRUE;
222 }
223
224
225 BOOL WINAPI
wglSetPbufferAttribARB(HPBUFFERARB hPbuffer,const int * piAttribList)226 wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
227 {
228 struct stw_framebuffer *fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
229 int face, i;
230
231 if (!fb) {
232 SetLastError(ERROR_INVALID_HANDLE);
233 return FALSE;
234 }
235
236 for (i = 0; piAttribList[i]; i += 2) {
237 switch (piAttribList[i]) {
238 case WGL_MIPMAP_LEVEL_ARB:
239 fb->textureLevel = piAttribList[i+1];
240 break;
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;
246 }
247 else {
248 debug_printf("Invalid cube face 0x%x in "
249 "wglSetPbufferAttribARB()\n",
250 piAttribList[i]);
251 SetLastError(ERROR_INVALID_DATA);
252 return FALSE;
253 }
254 break;
255 default:
256 debug_printf("Invalid attribute 0x%x in wglSetPbufferAttribARB()\n",
257 piAttribList[i]);
258 SetLastError(ERROR_INVALID_DATA);
259 return FALSE;
260 }
261 }
262
263 return TRUE;
264 }
265