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_gdishim.h"
41 #include "gldrv.h"
42 #include "stw_context.h"
43 #include "stw_device.h"
44 #include "stw_pixelformat.h"
45 #include "stw_framebuffer.h"
46 #include "stw_st.h"
47
48
49 /** Translate a WGL buffer name to a GLenum */
50 static GLenum
translate_ibuffer(int iBuffer)51 translate_ibuffer(int iBuffer)
52 {
53 switch (iBuffer) {
54 case WGL_FRONT_LEFT_ARB:
55 return GL_FRONT_LEFT;
56 case WGL_BACK_LEFT_ARB:
57 return GL_BACK_LEFT;
58 case WGL_FRONT_RIGHT_ARB:
59 return GL_FRONT_RIGHT;
60 case WGL_BACK_RIGHT_ARB:
61 return GL_BACK_RIGHT;
62 case WGL_AUX0_ARB:
63 return GL_AUX0;
64 default:
65 return GL_NONE;
66 }
67 }
68
69
70 /** Translate a WGL texture target type to a GLenum */
71 static GLenum
translate_target(unsigned textureTarget)72 translate_target(unsigned textureTarget)
73 {
74 switch (textureTarget) {
75 case WGL_TEXTURE_1D_ARB:
76 return GL_TEXTURE_1D;
77 case WGL_TEXTURE_2D_ARB:
78 return GL_TEXTURE_2D;
79 case WGL_TEXTURE_CUBE_MAP_ARB:
80 return GL_TEXTURE_CUBE_MAP;
81 case WGL_NO_TEXTURE_ARB:
82 default:
83 return GL_NONE;
84 }
85 }
86
87
88 /** Translate a WGL texture format to a GLenum */
89 static GLenum
translate_texture_format(unsigned wgl_format)90 translate_texture_format(unsigned wgl_format)
91 {
92 switch (wgl_format) {
93 case WGL_TEXTURE_RGB_ARB:
94 return GL_RGB;
95 case WGL_TEXTURE_RGBA_ARB:
96 return GL_RGBA;
97 case WGL_NO_TEXTURE_ARB:
98 default:
99 return GL_NONE;
100 }
101 }
102
103
104 BOOL WINAPI
wglBindTexImageARB(HPBUFFERARB hPbuffer,int iBuffer)105 wglBindTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
106 {
107 struct stw_context *curctx = stw_current_context();
108 struct stw_framebuffer *fb, *old_fb, *old_fbRead;
109 GLenum texFormat, srcBuffer, target;
110 bool retVal;
111 const struct stw_pixelformat_info *pfiSave;
112
113 /*
114 * Implementation notes:
115 * Ideally, we'd implement this function with the
116 * st_context_teximage() function which replaces a specific
117 * texture image with a different resource (the pbuffer).
118 * The main problem however, is the pbuffer image is upside down relative
119 * to the texture image.
120 * Window system drawing surfaces (windows & pbuffers) are "top to bottom"
121 * while OpenGL texture images are "bottom to top". One possible solution
122 * to this is to invert rendering to pbuffers (as we do for renderbuffers)
123 * but that could lead to other issues (and would require extensive
124 * testing).
125 *
126 * The simple alternative is to use a copy-based approach which copies the
127 * pbuffer image into the texture via glCopyTex[Sub]Image. That's what
128 * we do here.
129 */
130
131 if (!curctx) {
132 debug_printf("No rendering context in wglBindTexImageARB()\n");
133 SetLastError(ERROR_INVALID_OPERATION);
134 return false;
135 }
136
137 fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
138 if (!fb) {
139 debug_printf("Invalid pbuffer handle in wglBindTexImageARB()\n");
140 SetLastError(ERROR_INVALID_HANDLE);
141 return false;
142 }
143
144 srcBuffer = translate_ibuffer(iBuffer);
145 if (srcBuffer == GL_NONE) {
146 debug_printf("Invalid buffer 0x%x in wglBindTexImageARB()\n", iBuffer);
147 SetLastError(ERROR_INVALID_DATA);
148 return false;
149 }
150
151 target = translate_target(fb->textureTarget);
152 if (target == GL_NONE) {
153 debug_printf("no texture target in wglBindTexImageARB()\n");
154 return false;
155 }
156
157 texFormat = translate_texture_format(fb->textureFormat);
158 if (texFormat == GL_NONE) {
159 debug_printf("no texture format in wglBindTexImageARB()\n");
160 return false;
161 }
162
163 old_fb = curctx->current_framebuffer;
164 old_fbRead = curctx->current_read_framebuffer;
165
166 /*
167 * Bind the pbuffer surface so we can read/copy from it.
168 *
169 * Before we can call stw_make_current() we have to temporarily
170 * change the pbuffer's pixel format to match the context to avoid
171 * an error condition. After the stw_make_current() we restore the
172 * buffer's pixel format.
173 */
174 pfiSave = fb->pfi;
175 fb->pfi = curctx->pfi;
176 retVal = stw_make_current(fb, fb, curctx);
177 fb->pfi = pfiSave;
178 if (!retVal) {
179 debug_printf("stw_make_current(#1) failed in wglBindTexImageARB()\n");
180 return false;
181 }
182
183 st_copy_framebuffer_to_texture(srcBuffer, fb->width, fb->height,
184 target, fb->textureLevel,
185 fb->textureFace, texFormat);
186
187 /* rebind previous drawing surface */
188 retVal = stw_make_current(old_fb, old_fbRead, curctx);
189 if (!retVal) {
190 debug_printf("stw_make_current(#2) failed in wglBindTexImageARB()\n");
191 }
192
193 return retVal;
194 }
195
196
197 BOOL WINAPI
wglReleaseTexImageARB(HPBUFFERARB hPbuffer,int iBuffer)198 wglReleaseTexImageARB(HPBUFFERARB hPbuffer, int iBuffer)
199 {
200 struct stw_framebuffer *fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
201 GLenum srcBuffer;
202
203 /* nothing to do here, but we do error checking anyway */
204
205 if (!fb) {
206 debug_printf("Invalid pbuffer handle in wglReleaseTexImageARB()\n");
207 SetLastError(ERROR_INVALID_HANDLE);
208 return false;
209 }
210
211 srcBuffer = translate_ibuffer(iBuffer);
212 if (srcBuffer == GL_NONE) {
213 debug_printf("Invalid buffer 0x%x in wglReleaseTexImageARB()\n", iBuffer);
214 SetLastError(ERROR_INVALID_DATA);
215 return false;
216 }
217
218 return true;
219 }
220
221
222 BOOL WINAPI
wglSetPbufferAttribARB(HPBUFFERARB hPbuffer,const int * piAttribList)223 wglSetPbufferAttribARB(HPBUFFERARB hPbuffer, const int *piAttribList)
224 {
225 struct stw_framebuffer *fb = stw_framebuffer_from_HPBUFFERARB(hPbuffer);
226 int face, i;
227
228 if (!fb) {
229 SetLastError(ERROR_INVALID_HANDLE);
230 return false;
231 }
232
233 for (i = 0; piAttribList[i]; i += 2) {
234 switch (piAttribList[i]) {
235 case WGL_MIPMAP_LEVEL_ARB:
236 fb->textureLevel = piAttribList[i+1];
237 break;
238 case WGL_CUBE_MAP_FACE_ARB:
239 face = piAttribList[i+1];
240 if (face >= WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB &&
241 face <= WGL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB) {
242 fb->textureFace = face - WGL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB;
243 }
244 else {
245 debug_printf("Invalid cube face 0x%x in "
246 "wglSetPbufferAttribARB()\n",
247 piAttribList[i]);
248 SetLastError(ERROR_INVALID_DATA);
249 return false;
250 }
251 break;
252 default:
253 debug_printf("Invalid attribute 0x%x in wglSetPbufferAttribARB()\n",
254 piAttribList[i]);
255 SetLastError(ERROR_INVALID_DATA);
256 return false;
257 }
258 }
259
260 return true;
261 }
262