1 | /*
|
---|
2 | * Context and render target management in wined3d
|
---|
3 | *
|
---|
4 | * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
|
---|
5 | * Copyright 2009 Henri Verbeet for CodeWeavers
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2.1 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Lesser General Public
|
---|
18 | * License along with this library; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*
|
---|
23 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
24 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
25 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
26 | * a choice of LGPL license versions is made available with the language indicating
|
---|
27 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
28 | * of the LGPL is applied is otherwise unspecified.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #include "config.h"
|
---|
32 | #include <stdio.h>
|
---|
33 | #ifdef HAVE_FLOAT_H
|
---|
34 | # include <float.h>
|
---|
35 | #endif
|
---|
36 | #include "wined3d_private.h"
|
---|
37 |
|
---|
38 | WINE_DEFAULT_DEBUG_CHANNEL(d3d);
|
---|
39 |
|
---|
40 | #define GLINFO_LOCATION (*gl_info)
|
---|
41 |
|
---|
42 | static DWORD wined3d_context_tls_idx;
|
---|
43 |
|
---|
44 | /* FBO helper functions */
|
---|
45 |
|
---|
46 | /* GL locking is done by the caller */
|
---|
47 | void context_bind_fbo(struct wined3d_context *context, GLenum target, GLuint *fbo)
|
---|
48 | {
|
---|
49 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
50 | GLuint f;
|
---|
51 |
|
---|
52 | if (!fbo)
|
---|
53 | {
|
---|
54 | f = 0;
|
---|
55 | }
|
---|
56 | else
|
---|
57 | {
|
---|
58 | if (!*fbo)
|
---|
59 | {
|
---|
60 | gl_info->fbo_ops.glGenFramebuffers(1, fbo);
|
---|
61 | checkGLcall("glGenFramebuffers()");
|
---|
62 | TRACE("Created FBO %u.\n", *fbo);
|
---|
63 | }
|
---|
64 | f = *fbo;
|
---|
65 | }
|
---|
66 |
|
---|
67 | switch (target)
|
---|
68 | {
|
---|
69 | case GL_READ_FRAMEBUFFER:
|
---|
70 | if (context->fbo_read_binding == f) return;
|
---|
71 | context->fbo_read_binding = f;
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case GL_DRAW_FRAMEBUFFER:
|
---|
75 | if (context->fbo_draw_binding == f) return;
|
---|
76 | context->fbo_draw_binding = f;
|
---|
77 | break;
|
---|
78 |
|
---|
79 | case GL_FRAMEBUFFER:
|
---|
80 | if (context->fbo_read_binding == f
|
---|
81 | && context->fbo_draw_binding == f) return;
|
---|
82 | context->fbo_read_binding = f;
|
---|
83 | context->fbo_draw_binding = f;
|
---|
84 | break;
|
---|
85 |
|
---|
86 | default:
|
---|
87 | FIXME("Unhandled target %#x.\n", target);
|
---|
88 | break;
|
---|
89 | }
|
---|
90 |
|
---|
91 | gl_info->fbo_ops.glBindFramebuffer(target, f);
|
---|
92 | checkGLcall("glBindFramebuffer()");
|
---|
93 | }
|
---|
94 |
|
---|
95 | /* GL locking is done by the caller */
|
---|
96 | static void context_clean_fbo_attachments(const struct wined3d_gl_info *gl_info)
|
---|
97 | {
|
---|
98 | unsigned int i;
|
---|
99 |
|
---|
100 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
101 | {
|
---|
102 | gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 0, 0);
|
---|
103 | checkGLcall("glFramebufferTexture2D()");
|
---|
104 | }
|
---|
105 | gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
106 | checkGLcall("glFramebufferTexture2D()");
|
---|
107 |
|
---|
108 | gl_info->fbo_ops.glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
109 | checkGLcall("glFramebufferTexture2D()");
|
---|
110 | }
|
---|
111 |
|
---|
112 | /* GL locking is done by the caller */
|
---|
113 | static void context_destroy_fbo(struct wined3d_context *context, GLuint *fbo)
|
---|
114 | {
|
---|
115 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
116 |
|
---|
117 | context_bind_fbo(context, GL_FRAMEBUFFER, fbo);
|
---|
118 | context_clean_fbo_attachments(gl_info);
|
---|
119 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
120 |
|
---|
121 | gl_info->fbo_ops.glDeleteFramebuffers(1, fbo);
|
---|
122 | checkGLcall("glDeleteFramebuffers()");
|
---|
123 | }
|
---|
124 |
|
---|
125 | /* GL locking is done by the caller */
|
---|
126 | static void context_apply_attachment_filter_states(IWineD3DSurfaceImpl *surface)
|
---|
127 | {
|
---|
128 | IWineD3DBaseTextureImpl *texture_impl;
|
---|
129 |
|
---|
130 | /* Update base texture states array */
|
---|
131 | if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)surface,
|
---|
132 | &IID_IWineD3DBaseTexture, (void **)&texture_impl)))
|
---|
133 | {
|
---|
134 | IWineD3DDeviceImpl *device = surface->resource.device;
|
---|
135 | BOOL update_minfilter = FALSE;
|
---|
136 | BOOL update_magfilter = FALSE;
|
---|
137 |
|
---|
138 | if (texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_POINT
|
---|
139 | || texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_NONE)
|
---|
140 | {
|
---|
141 | texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
|
---|
142 | texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
|
---|
143 | update_minfilter = TRUE;
|
---|
144 | }
|
---|
145 |
|
---|
146 | if (texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_POINT)
|
---|
147 | {
|
---|
148 | texture_impl->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
|
---|
149 | update_magfilter = TRUE;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (texture_impl->baseTexture.bindCount)
|
---|
153 | {
|
---|
154 | WARN("Render targets should not be bound to a sampler\n");
|
---|
155 | IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(texture_impl->baseTexture.sampler));
|
---|
156 | }
|
---|
157 |
|
---|
158 | IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture_impl);
|
---|
159 |
|
---|
160 | if (update_minfilter || update_magfilter)
|
---|
161 | {
|
---|
162 | GLenum target, bind_target;
|
---|
163 | GLint old_binding;
|
---|
164 |
|
---|
165 | target = surface->texture_target;
|
---|
166 | if (target == GL_TEXTURE_2D)
|
---|
167 | {
|
---|
168 | bind_target = GL_TEXTURE_2D;
|
---|
169 | glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
|
---|
170 | }
|
---|
171 | else if (target == GL_TEXTURE_RECTANGLE_ARB)
|
---|
172 | {
|
---|
173 | bind_target = GL_TEXTURE_RECTANGLE_ARB;
|
---|
174 | glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
|
---|
175 | }
|
---|
176 | else
|
---|
177 | {
|
---|
178 | bind_target = GL_TEXTURE_CUBE_MAP_ARB;
|
---|
179 | glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB, &old_binding);
|
---|
180 | }
|
---|
181 |
|
---|
182 | glBindTexture(bind_target, surface->texture_name);
|
---|
183 | if (update_minfilter) glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
184 | if (update_magfilter) glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
185 | glBindTexture(bind_target, old_binding);
|
---|
186 | }
|
---|
187 |
|
---|
188 | checkGLcall("apply_attachment_filter_states()");
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* GL locking is done by the caller */
|
---|
193 | void context_attach_depth_stencil_fbo(struct wined3d_context *context,
|
---|
194 | GLenum fbo_target, IWineD3DSurfaceImpl *depth_stencil, BOOL use_render_buffer)
|
---|
195 | {
|
---|
196 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
197 |
|
---|
198 | TRACE("Attach depth stencil %p\n", depth_stencil);
|
---|
199 |
|
---|
200 | if (depth_stencil)
|
---|
201 | {
|
---|
202 | DWORD format_flags = depth_stencil->resource.format_desc->Flags;
|
---|
203 |
|
---|
204 | if (use_render_buffer && depth_stencil->current_renderbuffer)
|
---|
205 | {
|
---|
206 | if (format_flags & WINED3DFMT_FLAG_DEPTH)
|
---|
207 | {
|
---|
208 | gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_DEPTH_ATTACHMENT,
|
---|
209 | GL_RENDERBUFFER, depth_stencil->current_renderbuffer->id);
|
---|
210 | checkGLcall("glFramebufferRenderbuffer()");
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (format_flags & WINED3DFMT_FLAG_STENCIL)
|
---|
214 | {
|
---|
215 | gl_info->fbo_ops.glFramebufferRenderbuffer(fbo_target, GL_STENCIL_ATTACHMENT,
|
---|
216 | GL_RENDERBUFFER, depth_stencil->current_renderbuffer->id);
|
---|
217 | checkGLcall("glFramebufferRenderbuffer()");
|
---|
218 | }
|
---|
219 | }
|
---|
220 | else
|
---|
221 | {
|
---|
222 | surface_prepare_texture(depth_stencil, gl_info, FALSE);
|
---|
223 | context_apply_attachment_filter_states(depth_stencil);
|
---|
224 |
|
---|
225 | if (format_flags & WINED3DFMT_FLAG_DEPTH)
|
---|
226 | {
|
---|
227 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT,
|
---|
228 | depth_stencil->texture_target, depth_stencil->texture_name,
|
---|
229 | depth_stencil->texture_level);
|
---|
230 | checkGLcall("glFramebufferTexture2D()");
|
---|
231 | }
|
---|
232 |
|
---|
233 | if (format_flags & WINED3DFMT_FLAG_STENCIL)
|
---|
234 | {
|
---|
235 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT,
|
---|
236 | depth_stencil->texture_target, depth_stencil->texture_name,
|
---|
237 | depth_stencil->texture_level);
|
---|
238 | checkGLcall("glFramebufferTexture2D()");
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | if (!(format_flags & WINED3DFMT_FLAG_DEPTH))
|
---|
243 | {
|
---|
244 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
245 | checkGLcall("glFramebufferTexture2D()");
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (!(format_flags & WINED3DFMT_FLAG_STENCIL))
|
---|
249 | {
|
---|
250 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
251 | checkGLcall("glFramebufferTexture2D()");
|
---|
252 | }
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
257 | checkGLcall("glFramebufferTexture2D()");
|
---|
258 |
|
---|
259 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
|
---|
260 | checkGLcall("glFramebufferTexture2D()");
|
---|
261 | }
|
---|
262 | }
|
---|
263 |
|
---|
264 | /* GL locking is done by the caller */
|
---|
265 | void context_attach_surface_fbo(const struct wined3d_context *context,
|
---|
266 | GLenum fbo_target, DWORD idx, IWineD3DSurfaceImpl *surface)
|
---|
267 | {
|
---|
268 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
269 |
|
---|
270 | TRACE("Attach surface %p to %u\n", surface, idx);
|
---|
271 |
|
---|
272 | if (surface)
|
---|
273 | {
|
---|
274 | surface_prepare_texture(surface, gl_info, FALSE);
|
---|
275 | context_apply_attachment_filter_states(surface);
|
---|
276 |
|
---|
277 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, surface->texture_target,
|
---|
278 | surface->texture_name, surface->texture_level);
|
---|
279 | checkGLcall("glFramebufferTexture2D()");
|
---|
280 | }
|
---|
281 | else
|
---|
282 | {
|
---|
283 | gl_info->fbo_ops.glFramebufferTexture2D(fbo_target, GL_COLOR_ATTACHMENT0 + idx, GL_TEXTURE_2D, 0, 0);
|
---|
284 | checkGLcall("glFramebufferTexture2D()");
|
---|
285 | }
|
---|
286 | }
|
---|
287 |
|
---|
288 | /* GL locking is done by the caller */
|
---|
289 | static void context_check_fbo_status(struct wined3d_context *context)
|
---|
290 | {
|
---|
291 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
292 | GLenum status;
|
---|
293 |
|
---|
294 | status = gl_info->fbo_ops.glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
---|
295 | if (status == GL_FRAMEBUFFER_COMPLETE)
|
---|
296 | {
|
---|
297 | TRACE("FBO complete\n");
|
---|
298 | } else {
|
---|
299 | IWineD3DSurfaceImpl *attachment;
|
---|
300 | unsigned int i;
|
---|
301 | FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
|
---|
302 |
|
---|
303 | if (!context->current_fbo)
|
---|
304 | {
|
---|
305 | ERR("FBO 0 is incomplete, driver bug?\n");
|
---|
306 | return;
|
---|
307 | }
|
---|
308 |
|
---|
309 | /* Dump the FBO attachments */
|
---|
310 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
311 | {
|
---|
312 | attachment = context->current_fbo->render_targets[i];
|
---|
313 | if (attachment)
|
---|
314 | {
|
---|
315 | FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
|
---|
316 | i, attachment, debug_d3dformat(attachment->resource.format_desc->format),
|
---|
317 | attachment->pow2Width, attachment->pow2Height);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | attachment = context->current_fbo->depth_stencil;
|
---|
321 | if (attachment)
|
---|
322 | {
|
---|
323 | FIXME("\tDepth attachment: (%p) %s %ux%u\n",
|
---|
324 | attachment, debug_d3dformat(attachment->resource.format_desc->format),
|
---|
325 | attachment->pow2Width, attachment->pow2Height);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | static struct fbo_entry *context_create_fbo_entry(struct wined3d_context *context)
|
---|
331 | {
|
---|
332 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
333 | IWineD3DDeviceImpl *device = context_get_device(context);
|
---|
334 | struct fbo_entry *entry;
|
---|
335 |
|
---|
336 | entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
|
---|
337 | entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
338 | memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
339 | entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
|
---|
340 | entry->attached = FALSE;
|
---|
341 | entry->id = 0;
|
---|
342 |
|
---|
343 | return entry;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /* GL locking is done by the caller */
|
---|
347 | static void context_reuse_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
348 | {
|
---|
349 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
350 | IWineD3DDeviceImpl *device = context_get_device(context);
|
---|
351 |
|
---|
352 | context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
|
---|
353 | context_clean_fbo_attachments(gl_info);
|
---|
354 |
|
---|
355 | memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
356 | entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
|
---|
357 | entry->attached = FALSE;
|
---|
358 | }
|
---|
359 |
|
---|
360 | /* GL locking is done by the caller */
|
---|
361 | static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
362 | {
|
---|
363 | if (entry->id)
|
---|
364 | {
|
---|
365 | TRACE("Destroy FBO %d\n", entry->id);
|
---|
366 | context_destroy_fbo(context, &entry->id);
|
---|
367 | }
|
---|
368 | --context->fbo_entry_count;
|
---|
369 | list_remove(&entry->entry);
|
---|
370 | HeapFree(GetProcessHeap(), 0, entry->render_targets);
|
---|
371 | HeapFree(GetProcessHeap(), 0, entry);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /* GL locking is done by the caller */
|
---|
376 | static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context)
|
---|
377 | {
|
---|
378 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
379 | IWineD3DDeviceImpl *device = context_get_device(context);
|
---|
380 | struct fbo_entry *entry;
|
---|
381 |
|
---|
382 | LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
|
---|
383 | {
|
---|
384 | if (!memcmp(entry->render_targets,
|
---|
385 | device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
|
---|
386 | && entry->depth_stencil == (IWineD3DSurfaceImpl *)device->stencilBufferTarget)
|
---|
387 | {
|
---|
388 | list_remove(&entry->entry);
|
---|
389 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
390 | return entry;
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
|
---|
395 | {
|
---|
396 | entry = context_create_fbo_entry(context);
|
---|
397 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
398 | ++context->fbo_entry_count;
|
---|
399 | }
|
---|
400 | else
|
---|
401 | {
|
---|
402 | entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
|
---|
403 | context_reuse_fbo_entry(context, entry);
|
---|
404 | list_remove(&entry->entry);
|
---|
405 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
406 | }
|
---|
407 |
|
---|
408 | return entry;
|
---|
409 | }
|
---|
410 |
|
---|
411 | /* GL locking is done by the caller */
|
---|
412 | static void context_apply_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
413 | {
|
---|
414 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
415 | IWineD3DDeviceImpl *device = context_get_device(context);
|
---|
416 | unsigned int i;
|
---|
417 |
|
---|
418 | context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
|
---|
419 |
|
---|
420 | if (!entry->attached)
|
---|
421 | {
|
---|
422 | /* Apply render targets */
|
---|
423 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
424 | {
|
---|
425 | IWineD3DSurfaceImpl *render_target = (IWineD3DSurfaceImpl *)device->render_targets[i];
|
---|
426 | context_attach_surface_fbo(context, GL_FRAMEBUFFER, i, render_target);
|
---|
427 | }
|
---|
428 |
|
---|
429 | /* Apply depth targets */
|
---|
430 | if (device->stencilBufferTarget)
|
---|
431 | {
|
---|
432 | unsigned int w = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Width;
|
---|
433 | unsigned int h = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Height;
|
---|
434 |
|
---|
435 | surface_set_compatible_renderbuffer(device->stencilBufferTarget, w, h);
|
---|
436 | }
|
---|
437 | context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, (IWineD3DSurfaceImpl *)device->stencilBufferTarget, TRUE);
|
---|
438 |
|
---|
439 | entry->attached = TRUE;
|
---|
440 | }
|
---|
441 | else
|
---|
442 | {
|
---|
443 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
444 | {
|
---|
445 | if (device->render_targets[i])
|
---|
446 | context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->render_targets[i]);
|
---|
447 | }
|
---|
448 | if (device->stencilBufferTarget)
|
---|
449 | context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->stencilBufferTarget);
|
---|
450 | }
|
---|
451 |
|
---|
452 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
453 | {
|
---|
454 | if (device->render_targets[i])
|
---|
455 | device->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
|
---|
456 | else
|
---|
457 | device->draw_buffers[i] = GL_NONE;
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | /* GL locking is done by the caller */
|
---|
462 | static void context_apply_fbo_state(struct wined3d_context *context)
|
---|
463 | {
|
---|
464 | struct fbo_entry *entry, *entry2;
|
---|
465 |
|
---|
466 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
|
---|
467 | {
|
---|
468 | context_destroy_fbo_entry(context, entry);
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (context->rebind_fbo)
|
---|
472 | {
|
---|
473 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
474 | context->rebind_fbo = FALSE;
|
---|
475 | }
|
---|
476 |
|
---|
477 | if (context->render_offscreen)
|
---|
478 | {
|
---|
479 | context->current_fbo = context_find_fbo_entry(context);
|
---|
480 | context_apply_fbo_entry(context, context->current_fbo);
|
---|
481 | } else {
|
---|
482 | context->current_fbo = NULL;
|
---|
483 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
484 | }
|
---|
485 |
|
---|
486 | #if defined(DEBUG) && !defined(DEBUG_misha)
|
---|
487 | context_check_fbo_status(context);
|
---|
488 | #endif
|
---|
489 | }
|
---|
490 |
|
---|
491 | /* Context activation is done by the caller. */
|
---|
492 | void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
|
---|
493 | {
|
---|
494 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
495 |
|
---|
496 | if (context->free_occlusion_query_count)
|
---|
497 | {
|
---|
498 | query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
|
---|
499 | }
|
---|
500 | else
|
---|
501 | {
|
---|
502 | if (gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
503 | {
|
---|
504 | ENTER_GL();
|
---|
505 | GL_EXTCALL(glGenQueriesARB(1, &query->id));
|
---|
506 | checkGLcall("glGenQueriesARB");
|
---|
507 | LEAVE_GL();
|
---|
508 |
|
---|
509 | TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | {
|
---|
513 | WARN("Occlusion queries not supported, not allocating query id.\n");
|
---|
514 | query->id = 0;
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | query->context = context;
|
---|
519 | list_add_head(&context->occlusion_queries, &query->entry);
|
---|
520 | }
|
---|
521 |
|
---|
522 | void context_free_occlusion_query(struct wined3d_occlusion_query *query)
|
---|
523 | {
|
---|
524 | struct wined3d_context *context = query->context;
|
---|
525 |
|
---|
526 | list_remove(&query->entry);
|
---|
527 | query->context = NULL;
|
---|
528 |
|
---|
529 | if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
|
---|
530 | {
|
---|
531 | UINT new_size = context->free_occlusion_query_size << 1;
|
---|
532 | GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
|
---|
533 | new_size * sizeof(*context->free_occlusion_queries));
|
---|
534 |
|
---|
535 | if (!new_data)
|
---|
536 | {
|
---|
537 | ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
|
---|
538 | return;
|
---|
539 | }
|
---|
540 |
|
---|
541 | context->free_occlusion_query_size = new_size;
|
---|
542 | context->free_occlusion_queries = new_data;
|
---|
543 | }
|
---|
544 |
|
---|
545 | context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
|
---|
546 | }
|
---|
547 |
|
---|
548 | /* Context activation is done by the caller. */
|
---|
549 | void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
|
---|
550 | {
|
---|
551 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
552 |
|
---|
553 | if (context->free_event_query_count)
|
---|
554 | {
|
---|
555 | query->object = context->free_event_queries[--context->free_event_query_count];
|
---|
556 | }
|
---|
557 | else
|
---|
558 | {
|
---|
559 | if (gl_info->supported[ARB_SYNC])
|
---|
560 | {
|
---|
561 | /* Using ARB_sync, not much to do here. */
|
---|
562 | query->object.sync = NULL;
|
---|
563 | TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
|
---|
564 | }
|
---|
565 | else if (gl_info->supported[APPLE_FENCE])
|
---|
566 | {
|
---|
567 | ENTER_GL();
|
---|
568 | GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
|
---|
569 | checkGLcall("glGenFencesAPPLE");
|
---|
570 | LEAVE_GL();
|
---|
571 |
|
---|
572 | TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
|
---|
573 | }
|
---|
574 | else if(gl_info->supported[NV_FENCE])
|
---|
575 | {
|
---|
576 | ENTER_GL();
|
---|
577 | GL_EXTCALL(glGenFencesNV(1, &query->object.id));
|
---|
578 | checkGLcall("glGenFencesNV");
|
---|
579 | LEAVE_GL();
|
---|
580 |
|
---|
581 | TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
|
---|
582 | }
|
---|
583 | else
|
---|
584 | {
|
---|
585 | WARN("Event queries not supported, not allocating query id.\n");
|
---|
586 | query->object.id = 0;
|
---|
587 | }
|
---|
588 | }
|
---|
589 |
|
---|
590 | query->context = context;
|
---|
591 | list_add_head(&context->event_queries, &query->entry);
|
---|
592 | }
|
---|
593 |
|
---|
594 | void context_free_event_query(struct wined3d_event_query *query)
|
---|
595 | {
|
---|
596 | struct wined3d_context *context = query->context;
|
---|
597 |
|
---|
598 | list_remove(&query->entry);
|
---|
599 | query->context = NULL;
|
---|
600 |
|
---|
601 | if (context->free_event_query_count >= context->free_event_query_size - 1)
|
---|
602 | {
|
---|
603 | UINT new_size = context->free_event_query_size << 1;
|
---|
604 | union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
|
---|
605 | new_size * sizeof(*context->free_event_queries));
|
---|
606 |
|
---|
607 | if (!new_data)
|
---|
608 | {
|
---|
609 | ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
|
---|
610 | return;
|
---|
611 | }
|
---|
612 |
|
---|
613 | context->free_event_query_size = new_size;
|
---|
614 | context->free_event_queries = new_data;
|
---|
615 | }
|
---|
616 |
|
---|
617 | context->free_event_queries[context->free_event_query_count++] = query->object;
|
---|
618 | }
|
---|
619 |
|
---|
620 | void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
|
---|
621 | {
|
---|
622 | IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
|
---|
623 | UINT i;
|
---|
624 |
|
---|
625 | if (!This->d3d_initialized) return;
|
---|
626 |
|
---|
627 | switch(type)
|
---|
628 | {
|
---|
629 | case WINED3DRTYPE_SURFACE:
|
---|
630 | {
|
---|
631 | for (i = 0; i < This->numContexts; ++i)
|
---|
632 | {
|
---|
633 | struct wined3d_context *context = This->contexts[i];
|
---|
634 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
635 | struct fbo_entry *entry, *entry2;
|
---|
636 |
|
---|
637 | if (context->current_rt == (IWineD3DSurface *)resource) context->current_rt = NULL;
|
---|
638 |
|
---|
639 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
|
---|
640 | {
|
---|
641 | UINT j;
|
---|
642 |
|
---|
643 | if (entry->depth_stencil == (IWineD3DSurfaceImpl *)resource)
|
---|
644 | {
|
---|
645 | list_remove(&entry->entry);
|
---|
646 | list_add_head(&context->fbo_destroy_list, &entry->entry);
|
---|
647 | continue;
|
---|
648 | }
|
---|
649 |
|
---|
650 | for (j = 0; j < gl_info->limits.buffers; ++j)
|
---|
651 | {
|
---|
652 | if (entry->render_targets[j] == (IWineD3DSurfaceImpl *)resource)
|
---|
653 | {
|
---|
654 | list_remove(&entry->entry);
|
---|
655 | list_add_head(&context->fbo_destroy_list, &entry->entry);
|
---|
656 | break;
|
---|
657 | }
|
---|
658 | }
|
---|
659 | }
|
---|
660 | }
|
---|
661 |
|
---|
662 | break;
|
---|
663 | }
|
---|
664 |
|
---|
665 | default:
|
---|
666 | break;
|
---|
667 | }
|
---|
668 | }
|
---|
669 |
|
---|
670 | void context_surface_update(struct wined3d_context *context, IWineD3DSurfaceImpl *surface)
|
---|
671 | {
|
---|
672 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
673 | struct fbo_entry *entry = context->current_fbo;
|
---|
674 | unsigned int i;
|
---|
675 |
|
---|
676 | if (!entry || context->rebind_fbo) return;
|
---|
677 |
|
---|
678 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
679 | {
|
---|
680 | if (surface == entry->render_targets[i])
|
---|
681 | {
|
---|
682 | TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
|
---|
683 | context->rebind_fbo = TRUE;
|
---|
684 | return;
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | if (surface == entry->depth_stencil)
|
---|
689 | {
|
---|
690 | TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
|
---|
691 | context->rebind_fbo = TRUE;
|
---|
692 | }
|
---|
693 | }
|
---|
694 |
|
---|
695 | static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
|
---|
696 | {
|
---|
697 | int current = GetPixelFormat(dc);
|
---|
698 |
|
---|
699 | if (current == format) return TRUE;
|
---|
700 |
|
---|
701 | if (!current)
|
---|
702 | {
|
---|
703 | if (!SetPixelFormat(dc, format, NULL))
|
---|
704 | {
|
---|
705 | ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
|
---|
706 | format, dc, GetLastError());
|
---|
707 | return FALSE;
|
---|
708 | }
|
---|
709 | return TRUE;
|
---|
710 | }
|
---|
711 |
|
---|
712 | /* By default WGL doesn't allow pixel format adjustments but we need it
|
---|
713 | * here. For this reason there's a Wine specific wglSetPixelFormat()
|
---|
714 | * which allows us to set the pixel format multiple times. Only use it
|
---|
715 | * when really needed. */
|
---|
716 | if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
|
---|
717 | {
|
---|
718 | if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format, NULL)))
|
---|
719 | {
|
---|
720 | ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
|
---|
721 | format, dc);
|
---|
722 | return FALSE;
|
---|
723 | }
|
---|
724 | return TRUE;
|
---|
725 | }
|
---|
726 |
|
---|
727 | /* OpenGL doesn't allow pixel format adjustments. Print an error and
|
---|
728 | * continue using the old format. There's a big chance that the old
|
---|
729 | * format works although with a performance hit and perhaps rendering
|
---|
730 | * errors. */
|
---|
731 | ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
|
---|
732 | format, dc, current);
|
---|
733 | return TRUE;
|
---|
734 | }
|
---|
735 |
|
---|
736 | #ifdef VBOX_WITH_WDDM
|
---|
737 | static BOOL swapchain_validate(IWineD3DSwapChainImpl *swapchain)
|
---|
738 | {
|
---|
739 | if (!swapchain->hDC)
|
---|
740 | {
|
---|
741 | ERR("NULL hDC");
|
---|
742 | return FALSE;
|
---|
743 | }
|
---|
744 |
|
---|
745 | #ifdef DEBUG
|
---|
746 | {
|
---|
747 | HWND hWnd = WindowFromDC(swapchain->hDC);
|
---|
748 | if (hWnd != swapchain->win_handle)
|
---|
749 | {
|
---|
750 | ERR("Unexpected swapchain for dc %p window expected %p, but was %p.\n", swapchain->hDC, swapchain->win_handle, hWnd);
|
---|
751 | }
|
---|
752 | }
|
---|
753 | #endif
|
---|
754 | return TRUE;
|
---|
755 | }
|
---|
756 |
|
---|
757 | static IWineD3DSwapChainImpl * swapchain_find_valid(IWineD3DDeviceImpl *device)
|
---|
758 | {
|
---|
759 | int i;
|
---|
760 | for (i = device->NumberOfSwapChains - 1; i >= 0 ; --i)
|
---|
761 | {
|
---|
762 | if (swapchain_validate((IWineD3DSwapChainImpl*)device->swapchains[i]))
|
---|
763 | {
|
---|
764 | return (IWineD3DSwapChainImpl*)device->swapchains[i];
|
---|
765 | }
|
---|
766 | }
|
---|
767 |
|
---|
768 | return NULL;
|
---|
769 | }
|
---|
770 | #endif
|
---|
771 |
|
---|
772 | static void context_update_window(struct wined3d_context *context
|
---|
773 | #ifdef VBOX_WITH_WDDM
|
---|
774 | , IWineD3DSwapChainImpl *swapchain
|
---|
775 | #endif
|
---|
776 | )
|
---|
777 | {
|
---|
778 | #ifdef VBOX_WITH_WDDM
|
---|
779 | TRACE("Updating context %p swapchain from %p to %p.\n",
|
---|
780 | context, context->currentSwapchain, swapchain);
|
---|
781 |
|
---|
782 | context->valid = 1;
|
---|
783 |
|
---|
784 | if (!swapchain_validate(swapchain))
|
---|
785 | {
|
---|
786 | ERR("invalid swapchain %p\n", swapchain);
|
---|
787 | goto err;
|
---|
788 | }
|
---|
789 | context->currentSwapchain = swapchain;
|
---|
790 |
|
---|
791 | if (!context_set_pixel_format(context->gl_info, swapchain->hDC, context->pixel_format))
|
---|
792 | {
|
---|
793 | ERR("Failed to set pixel format %d on device context %p.\n",
|
---|
794 | context->pixel_format, swapchain->hDC);
|
---|
795 | goto err;
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (!pwglMakeCurrent(swapchain->hDC, context->glCtx))
|
---|
799 | {
|
---|
800 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
801 | context->glCtx, swapchain->hDC, GetLastError());
|
---|
802 | goto err;
|
---|
803 | }
|
---|
804 | #else
|
---|
805 | TRACE("Updating context %p window from %p to %p.\n",
|
---|
806 | context, context->win_handle, context->swapchain->win_handle);
|
---|
807 |
|
---|
808 | if (context->valid)
|
---|
809 | {
|
---|
810 | if (!ReleaseDC(context->win_handle, context->hdc))
|
---|
811 | {
|
---|
812 | ERR("Failed to release device context %p, last error %#x.\n",
|
---|
813 | context->hdc, GetLastError());
|
---|
814 | }
|
---|
815 | }
|
---|
816 | else context->valid = 1;
|
---|
817 |
|
---|
818 | context->win_handle = context->swapchain->win_handle;
|
---|
819 | if (!(context->hdc = GetDC(context->win_handle)))
|
---|
820 | {
|
---|
821 | ERR("Failed to get a device context for window %p.\n", context->win_handle);
|
---|
822 | goto err;
|
---|
823 | }
|
---|
824 |
|
---|
825 | if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
|
---|
826 | {
|
---|
827 | ERR("Failed to set pixel format %d on device context %p.\n",
|
---|
828 | context->pixel_format, context->hdc);
|
---|
829 | goto err;
|
---|
830 | }
|
---|
831 |
|
---|
832 | if (!pwglMakeCurrent(context->hdc, context->glCtx))
|
---|
833 | {
|
---|
834 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
835 | context->glCtx, context->hdc, GetLastError());
|
---|
836 | goto err;
|
---|
837 | }
|
---|
838 | #endif
|
---|
839 |
|
---|
840 | return;
|
---|
841 | err:
|
---|
842 | context->valid = 0;
|
---|
843 | }
|
---|
844 |
|
---|
845 | static void context_validate(struct wined3d_context *context
|
---|
846 | #ifdef VBOX_WITH_WDDM
|
---|
847 | , IWineD3DSwapChainImpl *swapchain
|
---|
848 | # ifdef DEBUG_misha
|
---|
849 | , BOOL fExpectedValid
|
---|
850 | # endif
|
---|
851 | #endif
|
---|
852 |
|
---|
853 | )
|
---|
854 | {
|
---|
855 | #ifdef VBOX_WITH_WDDM
|
---|
856 | if (!swapchain)
|
---|
857 | {
|
---|
858 | swapchain = context->currentSwapchain;
|
---|
859 | }
|
---|
860 |
|
---|
861 | if (!swapchain)
|
---|
862 | {
|
---|
863 | context->valid = FALSE;
|
---|
864 | # ifdef DEBUG_misha
|
---|
865 | if (fExpectedValid)
|
---|
866 | {
|
---|
867 | ERR("no current swapchain!\n");
|
---|
868 | }
|
---|
869 | # endif
|
---|
870 | return;
|
---|
871 | }
|
---|
872 |
|
---|
873 | if (swapchain == context->currentSwapchain)
|
---|
874 | {
|
---|
875 | context->valid = swapchain_validate(context->currentSwapchain);
|
---|
876 | }
|
---|
877 | else
|
---|
878 | #else
|
---|
879 | HWND wnd = WindowFromDC(context->hdc);
|
---|
880 |
|
---|
881 | if (wnd != context->win_handle)
|
---|
882 | {
|
---|
883 | DWORD winEr = GetLastError();
|
---|
884 | WARN("DC %p belongs to window %p instead of %p., winEr(%d)\n",
|
---|
885 | context->hdc, wnd, context->win_handle, winEr);
|
---|
886 | context->valid = 0;
|
---|
887 | }
|
---|
888 |
|
---|
889 | if (context->win_handle != context->swapchain->win_handle)
|
---|
890 | #endif
|
---|
891 | {
|
---|
892 | context_update_window(context
|
---|
893 | #ifdef VBOX_WITH_WDDM
|
---|
894 | , swapchain
|
---|
895 | #endif
|
---|
896 | );
|
---|
897 | }
|
---|
898 | }
|
---|
899 |
|
---|
900 | #ifdef VBOX_WITH_WDDM
|
---|
901 | static void context_validate_adjust_wnd(struct wined3d_context *context)
|
---|
902 | {
|
---|
903 | IWineD3DSwapChainImpl *swapchain = NULL;
|
---|
904 |
|
---|
905 | context_validate(context, NULL
|
---|
906 | # ifdef DEBUG_misha
|
---|
907 | , FALSE
|
---|
908 | # endif
|
---|
909 | );
|
---|
910 | if (context->valid)
|
---|
911 | return;
|
---|
912 |
|
---|
913 | swapchain = swapchain_find_valid(context->device);
|
---|
914 | if (swapchain)
|
---|
915 | {
|
---|
916 | context_validate(context, swapchain
|
---|
917 | # ifdef DEBUG_misha
|
---|
918 | , TRUE
|
---|
919 | # endif
|
---|
920 | );
|
---|
921 | if (!context->valid)
|
---|
922 | {
|
---|
923 | ERR("unexpected\n");
|
---|
924 | }
|
---|
925 | }
|
---|
926 | else
|
---|
927 | {
|
---|
928 | ERR("novalid swapchain found\n");
|
---|
929 | }
|
---|
930 | }
|
---|
931 | #endif
|
---|
932 |
|
---|
933 | static void context_destroy_gl_resources(struct wined3d_context *context)
|
---|
934 | {
|
---|
935 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
936 | struct wined3d_occlusion_query *occlusion_query;
|
---|
937 | struct wined3d_event_query *event_query;
|
---|
938 | struct fbo_entry *entry, *entry2;
|
---|
939 | HGLRC restore_ctx;
|
---|
940 | HDC restore_dc;
|
---|
941 | unsigned int i;
|
---|
942 |
|
---|
943 | restore_ctx = pwglGetCurrentContext();
|
---|
944 | restore_dc = pwglGetCurrentDC();
|
---|
945 |
|
---|
946 | #ifdef VBOX_WITH_WDDM
|
---|
947 | context_validate_adjust_wnd(context);
|
---|
948 | if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->currentSwapchain->hDC, context->glCtx);
|
---|
949 | else restore_ctx = NULL;
|
---|
950 | #else
|
---|
951 | context_validate(context);
|
---|
952 | if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->hdc, context->glCtx);
|
---|
953 | else restore_ctx = NULL;
|
---|
954 | #endif
|
---|
955 |
|
---|
956 | ENTER_GL();
|
---|
957 |
|
---|
958 | LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
|
---|
959 | {
|
---|
960 | if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
961 | GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
|
---|
962 | occlusion_query->context = NULL;
|
---|
963 | }
|
---|
964 |
|
---|
965 | LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
|
---|
966 | {
|
---|
967 | if (context->valid)
|
---|
968 | {
|
---|
969 | if (gl_info->supported[ARB_SYNC])
|
---|
970 | {
|
---|
971 | if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
|
---|
972 | }
|
---|
973 | else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
|
---|
974 | else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
|
---|
975 | }
|
---|
976 | event_query->context = NULL;
|
---|
977 | }
|
---|
978 |
|
---|
979 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
|
---|
980 | {
|
---|
981 | if (!context->valid) entry->id = 0;
|
---|
982 | context_destroy_fbo_entry(context, entry);
|
---|
983 | }
|
---|
984 |
|
---|
985 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
|
---|
986 | {
|
---|
987 | if (!context->valid) entry->id = 0;
|
---|
988 | context_destroy_fbo_entry(context, entry);
|
---|
989 | }
|
---|
990 |
|
---|
991 | if (context->valid)
|
---|
992 | {
|
---|
993 | if (context->src_fbo)
|
---|
994 | {
|
---|
995 | TRACE("Destroy src FBO %d\n", context->src_fbo);
|
---|
996 | context_destroy_fbo(context, &context->src_fbo);
|
---|
997 | }
|
---|
998 | if (context->dst_fbo)
|
---|
999 | {
|
---|
1000 | TRACE("Destroy dst FBO %d\n", context->dst_fbo);
|
---|
1001 | context_destroy_fbo(context, &context->dst_fbo);
|
---|
1002 | }
|
---|
1003 | if (context->dummy_arbfp_prog)
|
---|
1004 | {
|
---|
1005 | GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
|
---|
1006 | }
|
---|
1007 |
|
---|
1008 | if (gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
1009 | GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
|
---|
1010 |
|
---|
1011 | if (gl_info->supported[ARB_SYNC])
|
---|
1012 | {
|
---|
1013 | if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
|
---|
1014 | }
|
---|
1015 | else if (gl_info->supported[APPLE_FENCE])
|
---|
1016 | {
|
---|
1017 | for (i = 0; i < context->free_event_query_count; ++i)
|
---|
1018 | {
|
---|
1019 | GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
|
---|
1020 | }
|
---|
1021 | }
|
---|
1022 | else if (gl_info->supported[NV_FENCE])
|
---|
1023 | {
|
---|
1024 | for (i = 0; i < context->free_event_query_count; ++i)
|
---|
1025 | {
|
---|
1026 | GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | checkGLcall("context cleanup");
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | LEAVE_GL();
|
---|
1034 |
|
---|
1035 | HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
|
---|
1036 | HeapFree(GetProcessHeap(), 0, context->free_event_queries);
|
---|
1037 |
|
---|
1038 | if (restore_ctx)
|
---|
1039 | {
|
---|
1040 | if (!pwglMakeCurrent(restore_dc, restore_ctx))
|
---|
1041 | {
|
---|
1042 | DWORD err = GetLastError();
|
---|
1043 | ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
|
---|
1044 | restore_ctx, restore_dc, err);
|
---|
1045 | }
|
---|
1046 | }
|
---|
1047 | else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL, NULL))
|
---|
1048 | {
|
---|
1049 | ERR("Failed to disable GL context.\n");
|
---|
1050 | }
|
---|
1051 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1052 | # ifndef VBOX_WITH_WDDM
|
---|
1053 | ReleaseDC(context->win_handle, context->hdc);
|
---|
1054 | # else
|
---|
1055 | VBoxExtReleaseDC(context->win_handle, context->hdc);
|
---|
1056 | # endif
|
---|
1057 | #endif
|
---|
1058 | if (!pwglDeleteContext(context->glCtx))
|
---|
1059 | {
|
---|
1060 | DWORD err = GetLastError();
|
---|
1061 | ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
|
---|
1062 | }
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | DWORD context_get_tls_idx(void)
|
---|
1066 | {
|
---|
1067 | return wined3d_context_tls_idx;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | void context_set_tls_idx(DWORD idx)
|
---|
1071 | {
|
---|
1072 | wined3d_context_tls_idx = idx;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1076 | static struct wined3d_context *context_get_current_ex(DWORD adjustTid)
|
---|
1077 | {
|
---|
1078 | struct wined3d_context *ctx = TlsGetValue(wined3d_context_tls_idx);
|
---|
1079 | if (!adjustTid)
|
---|
1080 | return ctx;
|
---|
1081 | if (!ctx || ctx->tid == adjustTid)
|
---|
1082 | return ctx;
|
---|
1083 | if (context_set_current(ctx))
|
---|
1084 | {
|
---|
1085 | Assert(ctx->tid == adjustTid);
|
---|
1086 | return ctx;
|
---|
1087 | }
|
---|
1088 | ERR("context_set_current failed\n");
|
---|
1089 | return NULL;
|
---|
1090 | }
|
---|
1091 | #endif
|
---|
1092 |
|
---|
1093 | struct wined3d_context *context_get_current(void)
|
---|
1094 | {
|
---|
1095 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1096 | return TlsGetValue(wined3d_context_tls_idx);
|
---|
1097 | #else
|
---|
1098 | DWORD tid = GetCurrentThreadId();
|
---|
1099 | return context_get_current_ex(tid);
|
---|
1100 | #endif
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 |
|
---|
1104 | BOOL context_set_current(struct wined3d_context *ctx)
|
---|
1105 | {
|
---|
1106 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1107 | struct wined3d_context *old = context_get_current_ex(0);
|
---|
1108 | DWORD tid = GetCurrentThreadId();
|
---|
1109 | #else
|
---|
1110 | struct wined3d_context *old = context_get_current();
|
---|
1111 | #endif
|
---|
1112 | if (old == ctx)
|
---|
1113 | {
|
---|
1114 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1115 | if (ctx && ctx->tid != tid)
|
---|
1116 | {
|
---|
1117 | old = NULL;
|
---|
1118 | }
|
---|
1119 | else
|
---|
1120 | #endif
|
---|
1121 | {
|
---|
1122 | TRACE("Already using D3D context %p.\n", ctx);
|
---|
1123 | return TRUE;
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | if (old)
|
---|
1128 | {
|
---|
1129 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1130 | old->tid = 0;
|
---|
1131 | #endif
|
---|
1132 | if (old->destroyed)
|
---|
1133 | {
|
---|
1134 | TRACE("Switching away from destroyed context %p.\n", old);
|
---|
1135 | context_destroy_gl_resources(old);
|
---|
1136 | HeapFree(GetProcessHeap(), 0, old);
|
---|
1137 | }
|
---|
1138 | else
|
---|
1139 | {
|
---|
1140 | old->current = 0;
|
---|
1141 | }
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | if (ctx)
|
---|
1145 | {
|
---|
1146 | #ifdef VBOX_WITH_WDDM
|
---|
1147 | TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->currentSwapchain->hDC);
|
---|
1148 | if (!pwglMakeCurrent(ctx->currentSwapchain->hDC, ctx->glCtx))
|
---|
1149 | {
|
---|
1150 | DWORD err = GetLastError();
|
---|
1151 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
1152 | ctx->glCtx, ctx->currentSwapchain->hDC, err);
|
---|
1153 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1154 | return FALSE;
|
---|
1155 | }
|
---|
1156 | #else
|
---|
1157 | TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
|
---|
1158 | if (!pwglMakeCurrent(ctx->hdc, ctx->glCtx))
|
---|
1159 | {
|
---|
1160 | DWORD err = GetLastError();
|
---|
1161 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
1162 | ctx->glCtx, ctx->hdc, err);
|
---|
1163 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1164 | return FALSE;
|
---|
1165 | }
|
---|
1166 | #endif
|
---|
1167 | ctx->current = 1;
|
---|
1168 | }
|
---|
1169 | else if(pwglGetCurrentContext())
|
---|
1170 | {
|
---|
1171 | TRACE("Clearing current D3D context.\n");
|
---|
1172 | if (!pwglMakeCurrent(NULL, NULL))
|
---|
1173 | {
|
---|
1174 | DWORD err = GetLastError();
|
---|
1175 | ERR("Failed to clear current GL context, last error %#x.\n", err);
|
---|
1176 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1177 | return FALSE;
|
---|
1178 | }
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1182 | return TlsSetValue(wined3d_context_tls_idx, ctx);
|
---|
1183 | #else
|
---|
1184 | if (TlsSetValue(wined3d_context_tls_idx, ctx))
|
---|
1185 | {
|
---|
1186 | if (ctx)
|
---|
1187 | {
|
---|
1188 | ctx->tid = tid;
|
---|
1189 | }
|
---|
1190 | return TRUE;
|
---|
1191 | }
|
---|
1192 | else
|
---|
1193 | {
|
---|
1194 | DWORD err = GetLastError();
|
---|
1195 | ERR("Failed to set tls value, last error %#x.\n", err);
|
---|
1196 | }
|
---|
1197 | return FALSE;
|
---|
1198 | #endif
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | void context_release(struct wined3d_context *context)
|
---|
1202 | {
|
---|
1203 | TRACE("Releasing context %p, level %u.\n", context, context->level);
|
---|
1204 |
|
---|
1205 | if (WARN_ON(d3d))
|
---|
1206 | {
|
---|
1207 | if (!context->level)
|
---|
1208 | WARN("Context %p is not active.\n", context);
|
---|
1209 | else if (context != context_get_current())
|
---|
1210 | WARN("Context %p is not the current context.\n", context);
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | if (!--context->level && context->restore_ctx)
|
---|
1214 | {
|
---|
1215 | TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
|
---|
1216 | if (!pwglMakeCurrent(context->restore_dc, context->restore_ctx))
|
---|
1217 | {
|
---|
1218 | DWORD err = GetLastError();
|
---|
1219 | ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
|
---|
1220 | context->restore_ctx, context->restore_dc, err);
|
---|
1221 | }
|
---|
1222 | #ifdef VBOX_WITH_WDDM
|
---|
1223 | else
|
---|
1224 | {
|
---|
1225 | /* success branch */
|
---|
1226 | /* sync back our tls with gl settings */
|
---|
1227 | const struct wined3d_context *current_context = context_get_current();
|
---|
1228 | if (current_context && current_context->glCtx != context->restore_ctx)
|
---|
1229 | {
|
---|
1230 | IWineD3DDeviceImpl *device = context->device;
|
---|
1231 | UINT i = 0;
|
---|
1232 | for (; i < device->numContexts; ++i)
|
---|
1233 | {
|
---|
1234 | struct wined3d_context *ctx = device->contexts[i];
|
---|
1235 | if (ctx->glCtx == context->restore_ctx)
|
---|
1236 | {
|
---|
1237 | context_set_current(ctx);
|
---|
1238 | break;
|
---|
1239 | }
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | if (i == device->numContexts)
|
---|
1243 | {
|
---|
1244 | context_set_current(NULL);
|
---|
1245 | }
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 | #endif
|
---|
1249 |
|
---|
1250 | context->restore_ctx = NULL;
|
---|
1251 | context->restore_dc = NULL;
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | static void context_enter(struct wined3d_context *context)
|
---|
1256 | {
|
---|
1257 | TRACE("Entering context %p, level %u.\n", context, context->level + 1);
|
---|
1258 |
|
---|
1259 | if (!context->level++)
|
---|
1260 | {
|
---|
1261 | const struct wined3d_context *current_context = context_get_current();
|
---|
1262 | HGLRC current_gl = pwglGetCurrentContext();
|
---|
1263 |
|
---|
1264 | if (current_gl && (!current_context || current_context->glCtx != current_gl))
|
---|
1265 | {
|
---|
1266 | TRACE("Another GL context (%p on device context %p) is already current.\n",
|
---|
1267 | current_gl, pwglGetCurrentDC());
|
---|
1268 | context->restore_ctx = current_gl;
|
---|
1269 | context->restore_dc = pwglGetCurrentDC();
|
---|
1270 | }
|
---|
1271 | }
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | /*****************************************************************************
|
---|
1275 | * Context_MarkStateDirty
|
---|
1276 | *
|
---|
1277 | * Marks a state in a context dirty. Only one context, opposed to
|
---|
1278 | * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
|
---|
1279 | * contexts
|
---|
1280 | *
|
---|
1281 | * Params:
|
---|
1282 | * context: Context to mark the state dirty in
|
---|
1283 | * state: State to mark dirty
|
---|
1284 | * StateTable: Pointer to the state table in use(for state grouping)
|
---|
1285 | *
|
---|
1286 | *****************************************************************************/
|
---|
1287 | static void Context_MarkStateDirty(struct wined3d_context *context, DWORD state, const struct StateEntry *StateTable)
|
---|
1288 | {
|
---|
1289 | DWORD rep = StateTable[state].representative;
|
---|
1290 | DWORD idx;
|
---|
1291 | BYTE shift;
|
---|
1292 |
|
---|
1293 | if (isStateDirty(context, rep)) return;
|
---|
1294 |
|
---|
1295 | context->dirtyArray[context->numDirtyEntries++] = rep;
|
---|
1296 | idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
|
---|
1297 | shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
|
---|
1298 | context->isStateDirty[idx] |= (1 << shift);
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /* This function takes care of WineD3D pixel format selection. */
|
---|
1302 | static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc,
|
---|
1303 | const struct wined3d_format_desc *color_format_desc, const struct wined3d_format_desc *ds_format_desc,
|
---|
1304 | BOOL auxBuffers, int numSamples, BOOL findCompatible)
|
---|
1305 | {
|
---|
1306 | int iPixelFormat=0;
|
---|
1307 | unsigned int matchtry;
|
---|
1308 | short redBits, greenBits, blueBits, alphaBits, colorBits;
|
---|
1309 | short depthBits=0, stencilBits=0;
|
---|
1310 |
|
---|
1311 | struct match_type {
|
---|
1312 | BOOL require_aux;
|
---|
1313 | BOOL exact_alpha;
|
---|
1314 | BOOL exact_color;
|
---|
1315 | } matches[] = {
|
---|
1316 | /* First, try without alpha match buffers. MacOS supports aux buffers only
|
---|
1317 | * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
|
---|
1318 | * Then try without aux buffers - this is the most common cause for not
|
---|
1319 | * finding a pixel format. Also some drivers(the open source ones)
|
---|
1320 | * only offer 32 bit ARB pixel formats. First try without an exact alpha
|
---|
1321 | * match, then try without an exact alpha and color match.
|
---|
1322 | */
|
---|
1323 | { TRUE, TRUE, TRUE },
|
---|
1324 | { TRUE, FALSE, TRUE },
|
---|
1325 | { FALSE, TRUE, TRUE },
|
---|
1326 | { FALSE, FALSE, TRUE },
|
---|
1327 | { TRUE, FALSE, FALSE },
|
---|
1328 | { FALSE, FALSE, FALSE },
|
---|
1329 | };
|
---|
1330 |
|
---|
1331 | int i = 0;
|
---|
1332 | int nCfgs = This->adapter->nCfgs;
|
---|
1333 |
|
---|
1334 | TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
|
---|
1335 | debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format),
|
---|
1336 | auxBuffers, numSamples, findCompatible);
|
---|
1337 |
|
---|
1338 | if (!getColorBits(color_format_desc, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
|
---|
1339 | {
|
---|
1340 | ERR("Unable to get color bits for format %s (%#x)!\n",
|
---|
1341 | debug_d3dformat(color_format_desc->format), color_format_desc->format);
|
---|
1342 | return 0;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | getDepthStencilBits(ds_format_desc, &depthBits, &stencilBits);
|
---|
1346 |
|
---|
1347 | for(matchtry = 0; matchtry < (sizeof(matches) / sizeof(matches[0])) && !iPixelFormat; matchtry++) {
|
---|
1348 | for(i=0; i<nCfgs; i++) {
|
---|
1349 | BOOL exactDepthMatch = TRUE;
|
---|
1350 | WineD3D_PixelFormat *cfg = &This->adapter->cfgs[i];
|
---|
1351 |
|
---|
1352 | /* For now only accept RGBA formats. Perhaps some day we will
|
---|
1353 | * allow floating point formats for pbuffers. */
|
---|
1354 | if(cfg->iPixelType != WGL_TYPE_RGBA_ARB)
|
---|
1355 | continue;
|
---|
1356 |
|
---|
1357 | /* In window mode we need a window drawable format and double buffering. */
|
---|
1358 | if(!(cfg->windowDrawable && cfg->doubleBuffer))
|
---|
1359 | continue;
|
---|
1360 |
|
---|
1361 | /* We like to have aux buffers in backbuffer mode */
|
---|
1362 | if(auxBuffers && !cfg->auxBuffers && matches[matchtry].require_aux)
|
---|
1363 | continue;
|
---|
1364 |
|
---|
1365 | if(matches[matchtry].exact_color) {
|
---|
1366 | if(cfg->redSize != redBits)
|
---|
1367 | continue;
|
---|
1368 | if(cfg->greenSize != greenBits)
|
---|
1369 | continue;
|
---|
1370 | if(cfg->blueSize != blueBits)
|
---|
1371 | continue;
|
---|
1372 | } else {
|
---|
1373 | if(cfg->redSize < redBits)
|
---|
1374 | continue;
|
---|
1375 | if(cfg->greenSize < greenBits)
|
---|
1376 | continue;
|
---|
1377 | if(cfg->blueSize < blueBits)
|
---|
1378 | continue;
|
---|
1379 | }
|
---|
1380 | if(matches[matchtry].exact_alpha) {
|
---|
1381 | if(cfg->alphaSize != alphaBits)
|
---|
1382 | continue;
|
---|
1383 | } else {
|
---|
1384 | if(cfg->alphaSize < alphaBits)
|
---|
1385 | continue;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | /* We try to locate a format which matches our requirements exactly. In case of
|
---|
1389 | * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
|
---|
1390 | if(cfg->depthSize < depthBits)
|
---|
1391 | continue;
|
---|
1392 | else if(cfg->depthSize > depthBits)
|
---|
1393 | exactDepthMatch = FALSE;
|
---|
1394 |
|
---|
1395 | /* In all cases make sure the number of stencil bits matches our requirements
|
---|
1396 | * even when we don't need stencil because it could affect performance EXCEPT
|
---|
1397 | * on cards which don't offer depth formats without stencil like the i915 drivers
|
---|
1398 | * on Linux. */
|
---|
1399 | if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
|
---|
1400 | continue;
|
---|
1401 |
|
---|
1402 | /* Check multisampling support */
|
---|
1403 | if(cfg->numSamples != numSamples)
|
---|
1404 | continue;
|
---|
1405 |
|
---|
1406 | /* When we have passed all the checks then we have found a format which matches our
|
---|
1407 | * requirements. Note that we only check for a limit number of capabilities right now,
|
---|
1408 | * so there can easily be a dozen of pixel formats which appear to be the 'same' but
|
---|
1409 | * can still differ in things like multisampling, stereo, SRGB and other flags.
|
---|
1410 | */
|
---|
1411 |
|
---|
1412 | /* Exit the loop as we have found a format :) */
|
---|
1413 | if(exactDepthMatch) {
|
---|
1414 | iPixelFormat = cfg->iPixelFormat;
|
---|
1415 | break;
|
---|
1416 | } else if(!iPixelFormat) {
|
---|
1417 | /* In the end we might end up with a format which doesn't exactly match our depth
|
---|
1418 | * requirements. Accept the first format we found because formats with higher iPixelFormat
|
---|
1419 | * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
|
---|
1420 | iPixelFormat = cfg->iPixelFormat;
|
---|
1421 | }
|
---|
1422 | }
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 | /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
|
---|
1426 | if(!iPixelFormat && !findCompatible) {
|
---|
1427 | #ifdef DEBUG_misha
|
---|
1428 | WARN("Can't find a suitable iPixelFormat\n");
|
---|
1429 | #elif !defined(VBOX_WITH_WDDM)
|
---|
1430 | ERR("Can't find a suitable iPixelFormat\n");
|
---|
1431 | #endif
|
---|
1432 | return FALSE;
|
---|
1433 | } else if(!iPixelFormat) {
|
---|
1434 | PIXELFORMATDESCRIPTOR pfd;
|
---|
1435 |
|
---|
1436 | TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
|
---|
1437 | /* PixelFormat selection */
|
---|
1438 | ZeroMemory(&pfd, sizeof(pfd));
|
---|
1439 | pfd.nSize = sizeof(pfd);
|
---|
1440 | pfd.nVersion = 1;
|
---|
1441 | pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
|
---|
1442 | pfd.iPixelType = PFD_TYPE_RGBA;
|
---|
1443 | pfd.cAlphaBits = alphaBits;
|
---|
1444 | pfd.cColorBits = colorBits;
|
---|
1445 | pfd.cDepthBits = depthBits;
|
---|
1446 | pfd.cStencilBits = stencilBits;
|
---|
1447 | pfd.iLayerType = PFD_MAIN_PLANE;
|
---|
1448 |
|
---|
1449 | iPixelFormat = ChoosePixelFormat(hdc, &pfd);
|
---|
1450 | if(!iPixelFormat) {
|
---|
1451 | /* If this happens something is very wrong as ChoosePixelFormat barely fails */
|
---|
1452 | ERR("Can't find a suitable iPixelFormat\n");
|
---|
1453 | return FALSE;
|
---|
1454 | }
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
|
---|
1458 | iPixelFormat, debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format));
|
---|
1459 | return iPixelFormat;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | struct IWineD3DDeviceImpl *context_get_device(const struct wined3d_context *context)
|
---|
1463 | {
|
---|
1464 | #ifdef VBOX_WITH_WDDM
|
---|
1465 | return context->device;
|
---|
1466 | #else
|
---|
1467 | return context->swapchain->device;
|
---|
1468 | #endif
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 |
|
---|
1472 | /*****************************************************************************
|
---|
1473 | * context_create
|
---|
1474 | *
|
---|
1475 | * Creates a new context.
|
---|
1476 | *
|
---|
1477 | * * Params:
|
---|
1478 | * This: Device to activate the context for
|
---|
1479 | * target: Surface this context will render to
|
---|
1480 | * win_handle: handle to the window which we are drawing to
|
---|
1481 | * pPresentParameters: contains the pixelformats to use for onscreen rendering
|
---|
1482 | *
|
---|
1483 | *****************************************************************************/
|
---|
1484 | struct wined3d_context *context_create(IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
|
---|
1485 | const struct wined3d_format_desc *ds_format_desc)
|
---|
1486 | {
|
---|
1487 | IWineD3DDeviceImpl *device = swapchain->device;
|
---|
1488 | const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
|
---|
1489 | const struct wined3d_format_desc *color_format_desc;
|
---|
1490 | struct wined3d_context *ret;
|
---|
1491 | PIXELFORMATDESCRIPTOR pfd;
|
---|
1492 | BOOL auxBuffers = FALSE;
|
---|
1493 | int numSamples = 0;
|
---|
1494 | int pixel_format;
|
---|
1495 | unsigned int s;
|
---|
1496 | DWORD state;
|
---|
1497 | HGLRC ctx;
|
---|
1498 | HDC hdc;
|
---|
1499 |
|
---|
1500 | TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
|
---|
1501 |
|
---|
1502 | ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
|
---|
1503 | if (!ret)
|
---|
1504 | {
|
---|
1505 | ERR("Failed to allocate context memory.\n");
|
---|
1506 | return NULL;
|
---|
1507 | }
|
---|
1508 |
|
---|
1509 | if (!(hdc =
|
---|
1510 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1511 | swapchain->hDC
|
---|
1512 | #else
|
---|
1513 | GetDC(swapchain->win_handle)
|
---|
1514 | #endif
|
---|
1515 | )
|
---|
1516 | )
|
---|
1517 | {
|
---|
1518 | ERR("Failed to retrieve a device context.\n");
|
---|
1519 | goto out;
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | color_format_desc = target->resource.format_desc;
|
---|
1523 |
|
---|
1524 | /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
|
---|
1525 | * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
|
---|
1526 | if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
|
---|
1527 | {
|
---|
1528 | auxBuffers = TRUE;
|
---|
1529 |
|
---|
1530 | if (color_format_desc->format == WINED3DFMT_B4G4R4X4_UNORM)
|
---|
1531 | color_format_desc = getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM, gl_info);
|
---|
1532 | else if (color_format_desc->format == WINED3DFMT_B8G8R8X8_UNORM)
|
---|
1533 | color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | /* DirectDraw supports 8bit paletted render targets and these are used by
|
---|
1537 | * old games like Starcraft and C&C. Most modern hardware doesn't support
|
---|
1538 | * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
|
---|
1539 | * conversion (ab)uses the alpha component for storing the palette index.
|
---|
1540 | * For this reason we require a format with 8bit alpha, so request
|
---|
1541 | * A8R8G8B8. */
|
---|
1542 | if (color_format_desc->format == WINED3DFMT_P8_UINT)
|
---|
1543 | color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
|
---|
1544 |
|
---|
1545 | /* Retrieve the depth stencil format from the present parameters.
|
---|
1546 | * The choice of the proper format can give a nice performance boost
|
---|
1547 | * in case of GPU limited programs. */
|
---|
1548 | if (swapchain->presentParms.EnableAutoDepthStencil)
|
---|
1549 | {
|
---|
1550 | TRACE("Auto depth stencil enabled, using format %s.\n",
|
---|
1551 | debug_d3dformat(swapchain->presentParms.AutoDepthStencilFormat));
|
---|
1552 | ds_format_desc = getFormatDescEntry(swapchain->presentParms.AutoDepthStencilFormat, gl_info);
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
|
---|
1556 | if (swapchain->presentParms.MultiSampleType && (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD))
|
---|
1557 | {
|
---|
1558 | if (!gl_info->supported[ARB_MULTISAMPLE])
|
---|
1559 | WARN("The application is requesting multisampling without support.\n");
|
---|
1560 | else
|
---|
1561 | {
|
---|
1562 | TRACE("Requesting multisample type %#x.\n", swapchain->presentParms.MultiSampleType);
|
---|
1563 | numSamples = swapchain->presentParms.MultiSampleType;
|
---|
1564 | }
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | /* Try to find a pixel format which matches our requirements. */
|
---|
1568 | pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
|
---|
1569 | auxBuffers, numSamples, FALSE /* findCompatible */);
|
---|
1570 |
|
---|
1571 | /* Try to locate a compatible format if we weren't able to find anything. */
|
---|
1572 | if (!pixel_format)
|
---|
1573 | {
|
---|
1574 | TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
|
---|
1575 | pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
|
---|
1576 | auxBuffers, 0 /* numSamples */, TRUE /* findCompatible */);
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
|
---|
1580 | if (!pixel_format)
|
---|
1581 | {
|
---|
1582 | ERR("Can't find a suitable pixel format.\n");
|
---|
1583 | goto out;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd);
|
---|
1587 | if (!context_set_pixel_format(gl_info, hdc, pixel_format))
|
---|
1588 | {
|
---|
1589 | ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
|
---|
1590 | goto out;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 | ctx = pwglCreateContext(hdc);
|
---|
1594 | if (device->numContexts)
|
---|
1595 | {
|
---|
1596 | if (!pwglShareLists(device->contexts[0]->glCtx, ctx))
|
---|
1597 | {
|
---|
1598 | DWORD err = GetLastError();
|
---|
1599 | ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
|
---|
1600 | device->contexts[0]->glCtx, ctx, err);
|
---|
1601 | }
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | if(!ctx) {
|
---|
1605 | ERR("Failed to create a WGL context\n");
|
---|
1606 | goto out;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | if (!device_context_add(device, ret))
|
---|
1610 | {
|
---|
1611 | ERR("Failed to add the newly created context to the context list\n");
|
---|
1612 | if (!pwglDeleteContext(ctx))
|
---|
1613 | {
|
---|
1614 | DWORD err = GetLastError();
|
---|
1615 | ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, err);
|
---|
1616 | }
|
---|
1617 | goto out;
|
---|
1618 | }
|
---|
1619 |
|
---|
1620 | ret->gl_info = gl_info;
|
---|
1621 |
|
---|
1622 | /* Mark all states dirty to force a proper initialization of the states
|
---|
1623 | * on the first use of the context. */
|
---|
1624 | for (state = 0; state <= STATE_HIGHEST; ++state)
|
---|
1625 | {
|
---|
1626 | if (device->StateTable[state].representative)
|
---|
1627 | Context_MarkStateDirty(ret, state, device->StateTable);
|
---|
1628 | }
|
---|
1629 |
|
---|
1630 | #ifdef VBOX_WITH_WDDM
|
---|
1631 | ret->device = device;
|
---|
1632 | ret->currentSwapchain = swapchain;
|
---|
1633 | #else
|
---|
1634 | ret->swapchain = swapchain;
|
---|
1635 | #endif
|
---|
1636 | ret->current_rt = (IWineD3DSurface *)target;
|
---|
1637 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1638 | ret->tid = GetCurrentThreadId();
|
---|
1639 | #endif
|
---|
1640 |
|
---|
1641 | ret->render_offscreen = surface_is_offscreen((IWineD3DSurface *) target);
|
---|
1642 | ret->draw_buffer_dirty = TRUE;
|
---|
1643 | ret->valid = 1;
|
---|
1644 |
|
---|
1645 | ret->glCtx = ctx;
|
---|
1646 | #ifndef VBOX_WITH_WDDM
|
---|
1647 | ret->win_handle = swapchain->win_handle;
|
---|
1648 | ret->hdc = hdc;
|
---|
1649 | #endif
|
---|
1650 | ret->pixel_format = pixel_format;
|
---|
1651 |
|
---|
1652 | if (device->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *)device))
|
---|
1653 | {
|
---|
1654 | /* Create the dirty constants array and initialize them to dirty */
|
---|
1655 | ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
|
---|
1656 | sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
1657 | ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
|
---|
1658 | sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
1659 | memset(ret->vshader_const_dirty, 1,
|
---|
1660 | sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
1661 | memset(ret->pshader_const_dirty, 1,
|
---|
1662 | sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | ret->free_occlusion_query_size = 4;
|
---|
1666 | ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
|
---|
1667 | ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
|
---|
1668 | if (!ret->free_occlusion_queries) goto out;
|
---|
1669 |
|
---|
1670 | list_init(&ret->occlusion_queries);
|
---|
1671 |
|
---|
1672 | ret->free_event_query_size = 4;
|
---|
1673 | ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
|
---|
1674 | ret->free_event_query_size * sizeof(*ret->free_event_queries));
|
---|
1675 | if (!ret->free_event_queries) goto out;
|
---|
1676 |
|
---|
1677 | list_init(&ret->event_queries);
|
---|
1678 |
|
---|
1679 | TRACE("Successfully created new context %p\n", ret);
|
---|
1680 |
|
---|
1681 | list_init(&ret->fbo_list);
|
---|
1682 | list_init(&ret->fbo_destroy_list);
|
---|
1683 |
|
---|
1684 | context_enter(ret);
|
---|
1685 |
|
---|
1686 | /* Set up the context defaults */
|
---|
1687 | if (!context_set_current(ret))
|
---|
1688 | {
|
---|
1689 | ERR("Cannot activate context to set up defaults\n");
|
---|
1690 | context_release(ret);
|
---|
1691 | goto out;
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | ENTER_GL();
|
---|
1695 |
|
---|
1696 | glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
|
---|
1697 |
|
---|
1698 | TRACE("Setting up the screen\n");
|
---|
1699 | /* Clear the screen */
|
---|
1700 | glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
---|
1701 | checkGLcall("glClearColor");
|
---|
1702 | glClearIndex(0);
|
---|
1703 | glClearDepth(1);
|
---|
1704 | glClearStencil(0xffff);
|
---|
1705 |
|
---|
1706 | checkGLcall("glClear");
|
---|
1707 |
|
---|
1708 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
1709 | checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
|
---|
1710 |
|
---|
1711 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
|
---|
1712 | checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
|
---|
1713 |
|
---|
1714 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
|
---|
1715 | checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
|
---|
1716 |
|
---|
1717 | glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
|
---|
1718 | checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
|
---|
1719 | glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
|
---|
1720 | checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
|
---|
1721 |
|
---|
1722 | if (gl_info->supported[APPLE_CLIENT_STORAGE])
|
---|
1723 | {
|
---|
1724 | /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
|
---|
1725 | * and textures in DIB sections(due to the memory protection).
|
---|
1726 | */
|
---|
1727 | glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
|
---|
1728 | checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
|
---|
1729 | }
|
---|
1730 | if (gl_info->supported[ARB_VERTEX_BLEND])
|
---|
1731 | {
|
---|
1732 | /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
|
---|
1733 | * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
|
---|
1734 | * GL_VERTEX_BLEND_ARB isn't enabled too
|
---|
1735 | */
|
---|
1736 | glEnable(GL_WEIGHT_SUM_UNITY_ARB);
|
---|
1737 | checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
|
---|
1738 | }
|
---|
1739 | if (gl_info->supported[NV_TEXTURE_SHADER2])
|
---|
1740 | {
|
---|
1741 | /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
|
---|
1742 | * the previous texture where to source the offset from is always unit - 1.
|
---|
1743 | */
|
---|
1744 | for (s = 1; s < gl_info->limits.textures; ++s)
|
---|
1745 | {
|
---|
1746 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
|
---|
1747 | glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
|
---|
1748 | checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
|
---|
1749 | }
|
---|
1750 | }
|
---|
1751 | if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
|
---|
1752 | {
|
---|
1753 | /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
|
---|
1754 | * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
|
---|
1755 | * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
|
---|
1756 | * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
|
---|
1757 | * is ever assigned.
|
---|
1758 | *
|
---|
1759 | * So make sure a program is assigned to each context. The first real ARBFP use will set a different
|
---|
1760 | * program and the dummy program is destroyed when the context is destroyed.
|
---|
1761 | */
|
---|
1762 | const char *dummy_program =
|
---|
1763 | "!!ARBfp1.0\n"
|
---|
1764 | "MOV result.color, fragment.color.primary;\n"
|
---|
1765 | "END\n";
|
---|
1766 | GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
|
---|
1767 | GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
|
---|
1768 | GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | for (s = 0; s < gl_info->limits.point_sprite_units; ++s)
|
---|
1772 | {
|
---|
1773 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
|
---|
1774 | glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
|
---|
1775 | checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | if (gl_info->supported[ARB_PROVOKING_VERTEX])
|
---|
1779 | {
|
---|
1780 | GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
|
---|
1781 | }
|
---|
1782 | else if (gl_info->supported[EXT_PROVOKING_VERTEX])
|
---|
1783 | {
|
---|
1784 | GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | #ifdef VBOX_WITH_WDDM
|
---|
1788 | GL_EXTCALL(glChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE));
|
---|
1789 | GL_EXTCALL(glChromiumParameteriCR(GL_FLUSH_ON_THREAD_SWITCH_CR, GL_TRUE));
|
---|
1790 | #endif
|
---|
1791 |
|
---|
1792 | LEAVE_GL();
|
---|
1793 |
|
---|
1794 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
1795 |
|
---|
1796 | TRACE("Created context %p.\n", ret);
|
---|
1797 |
|
---|
1798 | return ret;
|
---|
1799 |
|
---|
1800 | out:
|
---|
1801 | HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
|
---|
1802 | HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
|
---|
1803 | HeapFree(GetProcessHeap(), 0, ret->pshader_const_dirty);
|
---|
1804 | HeapFree(GetProcessHeap(), 0, ret->vshader_const_dirty);
|
---|
1805 | HeapFree(GetProcessHeap(), 0, ret);
|
---|
1806 | return NULL;
|
---|
1807 | }
|
---|
1808 |
|
---|
1809 | #ifdef VBOX_WITH_WDDM
|
---|
1810 | static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
|
---|
1811 | static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage);
|
---|
1812 |
|
---|
1813 | BOOL context_acquire_context(struct wined3d_context * context, IWineD3DSurface *target, enum ContextUsage usage, BOOL bReValidate)
|
---|
1814 | {
|
---|
1815 | IWineD3DDeviceImpl *device = context->device;
|
---|
1816 | struct wined3d_context *current_context = context_get_current();
|
---|
1817 | if (bReValidate)
|
---|
1818 | {
|
---|
1819 | IWineD3DSwapChain *swapchain = NULL;
|
---|
1820 | if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
1821 | context_validate(context, (IWineD3DSwapChainImpl*)swapchain
|
---|
1822 | # ifdef DEBUG_misha
|
---|
1823 | , TRUE
|
---|
1824 | # endif
|
---|
1825 | );
|
---|
1826 | IWineD3DSwapChain_Release(swapchain);
|
---|
1827 | }
|
---|
1828 | else {
|
---|
1829 | context_validate_adjust_wnd(context);
|
---|
1830 | }
|
---|
1831 | }
|
---|
1832 | context_setup_target(device, context, target);
|
---|
1833 | context_enter(context);
|
---|
1834 | // Assert(context->valid);
|
---|
1835 | if (!context->valid) return FALSE;
|
---|
1836 |
|
---|
1837 | if (context != current_context)
|
---|
1838 | {
|
---|
1839 | if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
|
---|
1840 | else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
|
---|
1841 |
|
---|
1842 | if (context->vshader_const_dirty)
|
---|
1843 | {
|
---|
1844 | memset(context->vshader_const_dirty, 1,
|
---|
1845 | sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
1846 | device->highest_dirty_vs_const = device->d3d_vshader_constantF;
|
---|
1847 | }
|
---|
1848 | if (context->pshader_const_dirty)
|
---|
1849 | {
|
---|
1850 | memset(context->pshader_const_dirty, 1,
|
---|
1851 | sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
1852 | device->highest_dirty_ps_const = device->d3d_pshader_constantF;
|
---|
1853 | }
|
---|
1854 | }
|
---|
1855 | else if (context->restore_ctx)
|
---|
1856 | {
|
---|
1857 | if (!pwglMakeCurrent(context->currentSwapchain->hDC, context->glCtx))
|
---|
1858 | {
|
---|
1859 | DWORD err = GetLastError();
|
---|
1860 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
1861 | context->currentSwapchain->hDC, context->glCtx, err);
|
---|
1862 | }
|
---|
1863 | }
|
---|
1864 |
|
---|
1865 | context_apply_state(context, device, usage);
|
---|
1866 |
|
---|
1867 | return TRUE;
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
|
---|
1871 | const struct wined3d_format_desc *ds_format_desc)
|
---|
1872 | {
|
---|
1873 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1874 | UINT i;
|
---|
1875 | DWORD tid = GetCurrentThreadId();
|
---|
1876 | #endif
|
---|
1877 | struct wined3d_context *context = NULL;
|
---|
1878 |
|
---|
1879 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
1880 | for(i = 0 ; i < device->numContexts ; i ++)
|
---|
1881 | {
|
---|
1882 | if(device->contexts[i]->tid == tid) {
|
---|
1883 | context = device->contexts[i];
|
---|
1884 | break;
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 | #else
|
---|
1888 | context = device->numContexts ? device->contexts[0] : NULL;
|
---|
1889 | #endif
|
---|
1890 |
|
---|
1891 | if (!context)
|
---|
1892 | {
|
---|
1893 | Assert(!device->NumberOfSwapChains);
|
---|
1894 | context = context_create(swapchain, target, ds_format_desc);
|
---|
1895 | }
|
---|
1896 | else
|
---|
1897 | {
|
---|
1898 | if(!context_acquire_context(context, (IWineD3DSurface*)target, CTXUSAGE_RESOURCELOAD, TRUE))
|
---|
1899 | {
|
---|
1900 | ERR("Failed to acquire the context.\n");
|
---|
1901 | Assert(0);
|
---|
1902 | Assert(!context->valid);
|
---|
1903 | context = NULL;
|
---|
1904 | }
|
---|
1905 | else
|
---|
1906 | {
|
---|
1907 | Assert(context->valid);
|
---|
1908 | }
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | return context;
|
---|
1912 | }
|
---|
1913 | #endif
|
---|
1914 |
|
---|
1915 | /*****************************************************************************
|
---|
1916 | * context_destroy
|
---|
1917 | *
|
---|
1918 | * Destroys a wined3d context
|
---|
1919 | *
|
---|
1920 | * Params:
|
---|
1921 | * This: Device to activate the context for
|
---|
1922 | * context: Context to destroy
|
---|
1923 | *
|
---|
1924 | *****************************************************************************/
|
---|
1925 | void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context)
|
---|
1926 | {
|
---|
1927 | BOOL destroy;
|
---|
1928 |
|
---|
1929 | TRACE("Destroying ctx %p\n", context);
|
---|
1930 |
|
---|
1931 | if (context->tid == GetCurrentThreadId() || !context->current)
|
---|
1932 | {
|
---|
1933 | context_destroy_gl_resources(context);
|
---|
1934 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1935 | destroy = TRUE;
|
---|
1936 | }
|
---|
1937 | else
|
---|
1938 | {
|
---|
1939 | context->destroyed = 1;
|
---|
1940 | destroy = FALSE;
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
|
---|
1944 | HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
|
---|
1945 | device_context_remove(This, context);
|
---|
1946 | if (destroy) HeapFree(GetProcessHeap(), 0, context);
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | /* GL locking is done by the caller */
|
---|
1950 | static inline void set_blit_dimension(UINT width, UINT height) {
|
---|
1951 | glMatrixMode(GL_PROJECTION);
|
---|
1952 | checkGLcall("glMatrixMode(GL_PROJECTION)");
|
---|
1953 | glLoadIdentity();
|
---|
1954 | checkGLcall("glLoadIdentity()");
|
---|
1955 | glOrtho(0, width, height, 0, 0.0, -1.0);
|
---|
1956 | checkGLcall("glOrtho");
|
---|
1957 | glViewport(0, 0, width, height);
|
---|
1958 | checkGLcall("glViewport");
|
---|
1959 | }
|
---|
1960 |
|
---|
1961 | /*****************************************************************************
|
---|
1962 | * SetupForBlit
|
---|
1963 | *
|
---|
1964 | * Sets up a context for DirectDraw blitting.
|
---|
1965 | * All texture units are disabled, texture unit 0 is set as current unit
|
---|
1966 | * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
|
---|
1967 | * color writing enabled for all channels
|
---|
1968 | * register combiners disabled, shaders disabled
|
---|
1969 | * world matrix is set to identity, texture matrix 0 too
|
---|
1970 | * projection matrix is setup for drawing screen coordinates
|
---|
1971 | *
|
---|
1972 | * Params:
|
---|
1973 | * This: Device to activate the context for
|
---|
1974 | * context: Context to setup
|
---|
1975 | *
|
---|
1976 | *****************************************************************************/
|
---|
1977 | /* Context activation is done by the caller. */
|
---|
1978 | static void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context)
|
---|
1979 | {
|
---|
1980 | int i;
|
---|
1981 | const struct StateEntry *StateTable = This->StateTable;
|
---|
1982 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
1983 | UINT width = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Width;
|
---|
1984 | UINT height = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
|
---|
1985 | DWORD sampler;
|
---|
1986 |
|
---|
1987 | TRACE("Setting up context %p for blitting\n", context);
|
---|
1988 | if(context->last_was_blit) {
|
---|
1989 | if(context->blit_w != width || context->blit_h != height) {
|
---|
1990 | ENTER_GL();
|
---|
1991 | set_blit_dimension(width, height);
|
---|
1992 | LEAVE_GL();
|
---|
1993 | context->blit_w = width; context->blit_h = height;
|
---|
1994 | /* No need to dirtify here, the states are still dirtified because they weren't
|
---|
1995 | * applied since the last SetupForBlit call. Otherwise last_was_blit would not
|
---|
1996 | * be set
|
---|
1997 | */
|
---|
1998 | }
|
---|
1999 | TRACE("Context is already set up for blitting, nothing to do\n");
|
---|
2000 | return;
|
---|
2001 | }
|
---|
2002 | context->last_was_blit = TRUE;
|
---|
2003 |
|
---|
2004 | /* TODO: Use a display list */
|
---|
2005 |
|
---|
2006 | /* Disable shaders */
|
---|
2007 | ENTER_GL();
|
---|
2008 | This->shader_backend->shader_select(context, FALSE, FALSE);
|
---|
2009 | LEAVE_GL();
|
---|
2010 |
|
---|
2011 | Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
|
---|
2012 | Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
|
---|
2013 |
|
---|
2014 | /* Call ENTER_GL() once for all gl calls below. In theory we should not call
|
---|
2015 | * helper functions in between gl calls. This function is full of Context_MarkStateDirty
|
---|
2016 | * which can safely be called from here, we only lock once instead locking/unlocking
|
---|
2017 | * after each GL call.
|
---|
2018 | */
|
---|
2019 | ENTER_GL();
|
---|
2020 |
|
---|
2021 | /* Disable all textures. The caller can then bind a texture it wants to blit
|
---|
2022 | * from
|
---|
2023 | *
|
---|
2024 | * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
|
---|
2025 | * function texture unit. No need to care for higher samplers
|
---|
2026 | */
|
---|
2027 | for (i = gl_info->limits.textures - 1; i > 0 ; --i)
|
---|
2028 | {
|
---|
2029 | sampler = This->rev_tex_unit_map[i];
|
---|
2030 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
|
---|
2031 | checkGLcall("glActiveTextureARB");
|
---|
2032 |
|
---|
2033 | if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
|
---|
2034 | {
|
---|
2035 | glDisable(GL_TEXTURE_CUBE_MAP_ARB);
|
---|
2036 | checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
|
---|
2037 | }
|
---|
2038 | glDisable(GL_TEXTURE_3D);
|
---|
2039 | checkGLcall("glDisable GL_TEXTURE_3D");
|
---|
2040 | if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
|
---|
2041 | {
|
---|
2042 | glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
---|
2043 | checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
|
---|
2044 | }
|
---|
2045 | glDisable(GL_TEXTURE_2D);
|
---|
2046 | checkGLcall("glDisable GL_TEXTURE_2D");
|
---|
2047 |
|
---|
2048 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
2049 | checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
|
---|
2050 |
|
---|
2051 | if (sampler != WINED3D_UNMAPPED_STAGE)
|
---|
2052 | {
|
---|
2053 | if (sampler < MAX_TEXTURES) {
|
---|
2054 | Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
|
---|
2055 | }
|
---|
2056 | Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
|
---|
2057 | }
|
---|
2058 | }
|
---|
2059 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
|
---|
2060 | checkGLcall("glActiveTextureARB");
|
---|
2061 |
|
---|
2062 | sampler = This->rev_tex_unit_map[0];
|
---|
2063 |
|
---|
2064 | if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
|
---|
2065 | {
|
---|
2066 | glDisable(GL_TEXTURE_CUBE_MAP_ARB);
|
---|
2067 | checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
|
---|
2068 | }
|
---|
2069 | glDisable(GL_TEXTURE_3D);
|
---|
2070 | checkGLcall("glDisable GL_TEXTURE_3D");
|
---|
2071 | if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
|
---|
2072 | {
|
---|
2073 | glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
---|
2074 | checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
|
---|
2075 | }
|
---|
2076 | glDisable(GL_TEXTURE_2D);
|
---|
2077 | checkGLcall("glDisable GL_TEXTURE_2D");
|
---|
2078 |
|
---|
2079 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
2080 |
|
---|
2081 | glMatrixMode(GL_TEXTURE);
|
---|
2082 | checkGLcall("glMatrixMode(GL_TEXTURE)");
|
---|
2083 | glLoadIdentity();
|
---|
2084 | checkGLcall("glLoadIdentity()");
|
---|
2085 |
|
---|
2086 | if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
|
---|
2087 | {
|
---|
2088 | glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
|
---|
2089 | GL_TEXTURE_LOD_BIAS_EXT,
|
---|
2090 | 0.0f);
|
---|
2091 | checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
|
---|
2092 | }
|
---|
2093 |
|
---|
2094 | if (sampler != WINED3D_UNMAPPED_STAGE)
|
---|
2095 | {
|
---|
2096 | if (sampler < MAX_TEXTURES) {
|
---|
2097 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
|
---|
2098 | Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
|
---|
2099 | }
|
---|
2100 | Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
|
---|
2101 | }
|
---|
2102 |
|
---|
2103 | /* Other misc states */
|
---|
2104 | glDisable(GL_ALPHA_TEST);
|
---|
2105 | checkGLcall("glDisable(GL_ALPHA_TEST)");
|
---|
2106 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
|
---|
2107 | glDisable(GL_LIGHTING);
|
---|
2108 | checkGLcall("glDisable GL_LIGHTING");
|
---|
2109 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
|
---|
2110 | glDisable(GL_DEPTH_TEST);
|
---|
2111 | checkGLcall("glDisable GL_DEPTH_TEST");
|
---|
2112 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
|
---|
2113 | glDisableWINE(GL_FOG);
|
---|
2114 | checkGLcall("glDisable GL_FOG");
|
---|
2115 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
|
---|
2116 | glDisable(GL_BLEND);
|
---|
2117 | checkGLcall("glDisable GL_BLEND");
|
---|
2118 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
2119 | glDisable(GL_CULL_FACE);
|
---|
2120 | checkGLcall("glDisable GL_CULL_FACE");
|
---|
2121 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
|
---|
2122 | glDisable(GL_STENCIL_TEST);
|
---|
2123 | checkGLcall("glDisable GL_STENCIL_TEST");
|
---|
2124 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
|
---|
2125 | glDisable(GL_SCISSOR_TEST);
|
---|
2126 | checkGLcall("glDisable GL_SCISSOR_TEST");
|
---|
2127 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
|
---|
2128 | if (gl_info->supported[ARB_POINT_SPRITE])
|
---|
2129 | {
|
---|
2130 | glDisable(GL_POINT_SPRITE_ARB);
|
---|
2131 | checkGLcall("glDisable GL_POINT_SPRITE_ARB");
|
---|
2132 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
|
---|
2133 | }
|
---|
2134 | glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
|
---|
2135 | checkGLcall("glColorMask");
|
---|
2136 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE), StateTable);
|
---|
2137 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1), StateTable);
|
---|
2138 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2), StateTable);
|
---|
2139 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3), StateTable);
|
---|
2140 | if (gl_info->supported[EXT_SECONDARY_COLOR])
|
---|
2141 | {
|
---|
2142 | glDisable(GL_COLOR_SUM_EXT);
|
---|
2143 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
|
---|
2144 | checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
|
---|
2145 | }
|
---|
2146 |
|
---|
2147 | /* Setup transforms */
|
---|
2148 | glMatrixMode(GL_MODELVIEW);
|
---|
2149 | checkGLcall("glMatrixMode(GL_MODELVIEW)");
|
---|
2150 | glLoadIdentity();
|
---|
2151 | checkGLcall("glLoadIdentity()");
|
---|
2152 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
|
---|
2153 |
|
---|
2154 | context->last_was_rhw = TRUE;
|
---|
2155 | Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
|
---|
2156 |
|
---|
2157 | glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
|
---|
2158 | glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
|
---|
2159 | glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
|
---|
2160 | glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
|
---|
2161 | glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
|
---|
2162 | glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
|
---|
2163 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
|
---|
2164 |
|
---|
2165 | set_blit_dimension(width, height);
|
---|
2166 |
|
---|
2167 | LEAVE_GL();
|
---|
2168 |
|
---|
2169 | context->blit_w = width; context->blit_h = height;
|
---|
2170 | Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
|
---|
2171 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
|
---|
2172 |
|
---|
2173 |
|
---|
2174 | This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
|
---|
2175 | }
|
---|
2176 |
|
---|
2177 | /*****************************************************************************
|
---|
2178 | * findThreadContextForSwapChain
|
---|
2179 | *
|
---|
2180 | * Searches a swapchain for all contexts and picks one for the thread tid.
|
---|
2181 | * If none can be found the swapchain is requested to create a new context
|
---|
2182 | *
|
---|
2183 | *****************************************************************************/
|
---|
2184 | static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain
|
---|
2185 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2186 | , DWORD tid
|
---|
2187 | #endif
|
---|
2188 | )
|
---|
2189 | {
|
---|
2190 | #ifdef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2191 | IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
|
---|
2192 | if (device->numContexts)
|
---|
2193 | return device->contexts[0];
|
---|
2194 | #else
|
---|
2195 | unsigned int i;
|
---|
2196 | # ifdef VBOX_WITH_WDDM
|
---|
2197 | IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
|
---|
2198 | for (i = 0; i < device->numContexts; ++i)
|
---|
2199 | {
|
---|
2200 | if (device->contexts[i]->tid == tid)
|
---|
2201 | return device->contexts[i];
|
---|
2202 | }
|
---|
2203 | # else
|
---|
2204 | for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
|
---|
2205 | if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
|
---|
2206 | return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
|
---|
2207 | }
|
---|
2208 | }
|
---|
2209 | # endif
|
---|
2210 | #endif
|
---|
2211 |
|
---|
2212 | /* Create a new context for the thread */
|
---|
2213 | return swapchain_create_context_for_thread(swapchain);
|
---|
2214 | }
|
---|
2215 |
|
---|
2216 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2217 | /*****************************************************************************
|
---|
2218 | * FindContext
|
---|
2219 | *
|
---|
2220 | * Finds a context for the current render target and thread
|
---|
2221 | *
|
---|
2222 | * Parameters:
|
---|
2223 | * target: Render target to find the context for
|
---|
2224 | * tid: Thread to activate the context for
|
---|
2225 | *
|
---|
2226 | * Returns: The needed context
|
---|
2227 | *
|
---|
2228 | *****************************************************************************/
|
---|
2229 | static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
|
---|
2230 | {
|
---|
2231 | IWineD3DSwapChain *swapchain = NULL;
|
---|
2232 | struct wined3d_context *current_context = context_get_current();
|
---|
2233 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2234 | DWORD tid = GetCurrentThreadId();
|
---|
2235 | #endif
|
---|
2236 | struct wined3d_context *context;
|
---|
2237 |
|
---|
2238 | if (current_context && current_context->destroyed) current_context = NULL;
|
---|
2239 |
|
---|
2240 | if (!target)
|
---|
2241 | {
|
---|
2242 | if (current_context
|
---|
2243 | && current_context->current_rt
|
---|
2244 | && context_get_device(current_context) == This
|
---|
2245 | )
|
---|
2246 | {
|
---|
2247 | target = current_context->current_rt;
|
---|
2248 | }
|
---|
2249 | else
|
---|
2250 | {
|
---|
2251 | #ifdef VBOX_WITH_WDDM
|
---|
2252 | /* tmp work-around */
|
---|
2253 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
|
---|
2254 | #else
|
---|
2255 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
|
---|
2256 | #endif
|
---|
2257 | if (swapchain->backBuffer) target = swapchain->backBuffer[0];
|
---|
2258 | else target = swapchain->frontBuffer;
|
---|
2259 | }
|
---|
2260 | }
|
---|
2261 |
|
---|
2262 | if (current_context && current_context->current_rt == target)
|
---|
2263 | {
|
---|
2264 | #ifdef VBOX_WITH_WDDM
|
---|
2265 | IWineD3DSwapChain *swapchain = NULL;
|
---|
2266 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
2267 | context_validate(current_context, (IWineD3DSwapChainImpl*)swapchain);
|
---|
2268 | IWineD3DSwapChain_Release(swapchain);
|
---|
2269 | }
|
---|
2270 | else {
|
---|
2271 | /* tmp work-around */
|
---|
2272 | context_validate(current_context,
|
---|
2273 | NULL //(IWineD3DSwapChainImpl*)current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]
|
---|
2274 | );
|
---|
2275 | }
|
---|
2276 | #else
|
---|
2277 | context_validate(current_context);
|
---|
2278 | #endif
|
---|
2279 | return current_context;
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
2283 | TRACE("Rendering onscreen\n");
|
---|
2284 |
|
---|
2285 | context = findThreadContextForSwapChain(swapchain
|
---|
2286 | #ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2287 | , tid
|
---|
2288 | #endif
|
---|
2289 | );
|
---|
2290 | #ifdef VBOX_WITH_WDDM
|
---|
2291 | context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
|
---|
2292 | #endif
|
---|
2293 | IWineD3DSwapChain_Release(swapchain);
|
---|
2294 | }
|
---|
2295 | else
|
---|
2296 | {
|
---|
2297 | TRACE("Rendering offscreen\n");
|
---|
2298 |
|
---|
2299 | /* Stay with the currently active context. */
|
---|
2300 | if (current_context && context_get_device(current_context) == This)
|
---|
2301 | {
|
---|
2302 | context = current_context;
|
---|
2303 | }
|
---|
2304 | else
|
---|
2305 | {
|
---|
2306 | /* This may happen if the app jumps straight into offscreen rendering
|
---|
2307 | * Start using the context of the primary swapchain. tid == 0 is no problem
|
---|
2308 | * for findThreadContextForSwapChain.
|
---|
2309 | *
|
---|
2310 | * Can also happen on thread switches - in that case findThreadContextForSwapChain
|
---|
2311 | * is perfect to call. */
|
---|
2312 | #ifdef VBOX_WITH_WDDM /* tmp work-around */
|
---|
2313 | context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1]
|
---|
2314 | # ifndef VBOX_WINE_WITH_SINGLE_CONTEXT
|
---|
2315 | , tid
|
---|
2316 | # endif
|
---|
2317 | );
|
---|
2318 | #else
|
---|
2319 | context = findThreadContextForSwapChain(This->swapchains[0], tid);
|
---|
2320 | #endif
|
---|
2321 | }
|
---|
2322 | #ifdef VBOX_WITH_WDDM
|
---|
2323 | context_validate(context,
|
---|
2324 | NULL //(IWineD3DSwapChainImpl*)This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
|
---|
2325 | );
|
---|
2326 | #endif
|
---|
2327 | }
|
---|
2328 |
|
---|
2329 | #ifndef VBOX_WITH_WDDM
|
---|
2330 | context_validate(context);
|
---|
2331 | #endif
|
---|
2332 |
|
---|
2333 | return context;
|
---|
2334 | }
|
---|
2335 | #else
|
---|
2336 | /*****************************************************************************
|
---|
2337 | * FindContext
|
---|
2338 | *
|
---|
2339 | * Finds a context for the current render target and thread
|
---|
2340 | *
|
---|
2341 | * Parameters:
|
---|
2342 | * target: Render target to find the context for
|
---|
2343 | * tid: Thread to activate the context for
|
---|
2344 | *
|
---|
2345 | * Returns: The needed context
|
---|
2346 | *
|
---|
2347 | *****************************************************************************/
|
---|
2348 | static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
|
---|
2349 | {
|
---|
2350 | IWineD3DSwapChain *swapchain = NULL;
|
---|
2351 | struct wined3d_context *context = This->numContexts ? This->contexts[0] : NULL;
|
---|
2352 |
|
---|
2353 |
|
---|
2354 | if (context && context->destroyed)
|
---|
2355 | {
|
---|
2356 | ERR("context is destroyed");
|
---|
2357 | }
|
---|
2358 |
|
---|
2359 | if (!target)
|
---|
2360 | {
|
---|
2361 | if (context
|
---|
2362 | && context->current_rt
|
---|
2363 | && context_get_device(context) == This
|
---|
2364 | )
|
---|
2365 | {
|
---|
2366 | target = context->current_rt;
|
---|
2367 | }
|
---|
2368 | else
|
---|
2369 | {
|
---|
2370 | IWineD3DSwapChainImpl *swapchain = swapchain_find_valid(This);
|
---|
2371 | if (!swapchain)
|
---|
2372 | swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1]; /* just fallback to anything to avoid NPE */
|
---|
2373 | if (swapchain->backBuffer) target = swapchain->backBuffer[0];
|
---|
2374 | else if (swapchain->frontBuffer) target = swapchain->frontBuffer;
|
---|
2375 | else target = swapchain->presentRt;
|
---|
2376 | }
|
---|
2377 | }
|
---|
2378 |
|
---|
2379 | if (context && context->current_rt == target)
|
---|
2380 | {
|
---|
2381 | IWineD3DSwapChain *swapchain = NULL;
|
---|
2382 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
2383 | context_validate(context, (IWineD3DSwapChainImpl*)swapchain
|
---|
2384 | # ifdef DEBUG_misha
|
---|
2385 | , TRUE
|
---|
2386 | # endif
|
---|
2387 | );
|
---|
2388 | IWineD3DSwapChain_Release(swapchain);
|
---|
2389 | }
|
---|
2390 | else {
|
---|
2391 | context_validate_adjust_wnd(context);
|
---|
2392 | }
|
---|
2393 | return context;
|
---|
2394 | }
|
---|
2395 |
|
---|
2396 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
2397 | TRACE("Rendering onscreen\n");
|
---|
2398 |
|
---|
2399 | context = findThreadContextForSwapChain(swapchain);
|
---|
2400 | context_validate(context, (IWineD3DSwapChainImpl*)swapchain
|
---|
2401 | # ifdef DEBUG_misha
|
---|
2402 | , TRUE
|
---|
2403 | # endif
|
---|
2404 | );
|
---|
2405 | IWineD3DSwapChain_Release(swapchain);
|
---|
2406 | }
|
---|
2407 | else
|
---|
2408 | {
|
---|
2409 | if (context)
|
---|
2410 | {
|
---|
2411 | context_validate_adjust_wnd(context);
|
---|
2412 | }
|
---|
2413 | else
|
---|
2414 | {
|
---|
2415 | IWineD3DSwapChainImpl *swapchain = swapchain_find_valid(This);
|
---|
2416 | if (!swapchain)
|
---|
2417 | swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1]; /* just fallback to anything to avoid NPE */
|
---|
2418 | context = findThreadContextForSwapChain((IWineD3DSwapChain*)swapchain);
|
---|
2419 | context_validate(context, swapchain
|
---|
2420 | # ifdef DEBUG_misha
|
---|
2421 | , TRUE
|
---|
2422 | # endif
|
---|
2423 | );
|
---|
2424 | }
|
---|
2425 |
|
---|
2426 | TRACE("Rendering offscreen\n");
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 | return context;
|
---|
2430 | }
|
---|
2431 | #endif
|
---|
2432 |
|
---|
2433 | /* Context activation is done by the caller. */
|
---|
2434 | static void context_apply_draw_buffer(struct wined3d_context *context, BOOL blit)
|
---|
2435 | {
|
---|
2436 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
2437 | IWineD3DSurface *rt = context->current_rt;
|
---|
2438 | IWineD3DDeviceImpl *device;
|
---|
2439 |
|
---|
2440 | device = ((IWineD3DSurfaceImpl *)rt)->resource.device;
|
---|
2441 | if (!surface_is_offscreen(rt))
|
---|
2442 | {
|
---|
2443 | ENTER_GL();
|
---|
2444 | glDrawBuffer(surface_get_gl_buffer(rt));
|
---|
2445 | checkGLcall("glDrawBuffers()");
|
---|
2446 | LEAVE_GL();
|
---|
2447 | }
|
---|
2448 | else
|
---|
2449 | {
|
---|
2450 | ENTER_GL();
|
---|
2451 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
|
---|
2452 | {
|
---|
2453 | if (!blit)
|
---|
2454 | {
|
---|
2455 | if (gl_info->supported[ARB_DRAW_BUFFERS])
|
---|
2456 | {
|
---|
2457 | GL_EXTCALL(glDrawBuffersARB(gl_info->limits.buffers, device->draw_buffers));
|
---|
2458 | checkGLcall("glDrawBuffers()");
|
---|
2459 | }
|
---|
2460 | else
|
---|
2461 | {
|
---|
2462 | glDrawBuffer(device->draw_buffers[0]);
|
---|
2463 | checkGLcall("glDrawBuffer()");
|
---|
2464 | }
|
---|
2465 | } else {
|
---|
2466 | glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
---|
2467 | checkGLcall("glDrawBuffer()");
|
---|
2468 | }
|
---|
2469 | }
|
---|
2470 | else
|
---|
2471 | {
|
---|
2472 | glDrawBuffer(device->offscreenBuffer);
|
---|
2473 | checkGLcall("glDrawBuffer()");
|
---|
2474 | }
|
---|
2475 | LEAVE_GL();
|
---|
2476 | }
|
---|
2477 | }
|
---|
2478 |
|
---|
2479 | /* GL locking is done by the caller. */
|
---|
2480 | void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
|
---|
2481 | {
|
---|
2482 | glDrawBuffer(buffer);
|
---|
2483 | checkGLcall("glDrawBuffer()");
|
---|
2484 | context->draw_buffer_dirty = TRUE;
|
---|
2485 | }
|
---|
2486 |
|
---|
2487 | static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
|
---|
2488 | BOOL offscreen)
|
---|
2489 | {
|
---|
2490 | if (context->render_offscreen == offscreen) return;
|
---|
2491 |
|
---|
2492 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
|
---|
2493 | Context_MarkStateDirty(context, STATE_VDECL, StateTable);
|
---|
2494 | Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
|
---|
2495 | Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
|
---|
2496 | Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
|
---|
2497 | context->render_offscreen = offscreen;
|
---|
2498 | }
|
---|
2499 |
|
---|
2500 | static BOOL match_depth_stencil_format(const struct wined3d_format_desc *existing,
|
---|
2501 | const struct wined3d_format_desc *required)
|
---|
2502 | {
|
---|
2503 | short existing_depth, existing_stencil, required_depth, required_stencil;
|
---|
2504 |
|
---|
2505 | if(existing == required) return TRUE;
|
---|
2506 | if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
|
---|
2507 |
|
---|
2508 | getDepthStencilBits(existing, &existing_depth, &existing_stencil);
|
---|
2509 | getDepthStencilBits(required, &required_depth, &required_stencil);
|
---|
2510 |
|
---|
2511 | if(existing_depth < required_depth) return FALSE;
|
---|
2512 | /* If stencil bits are used the exact amount is required - otherwise wrapping
|
---|
2513 | * won't work correctly */
|
---|
2514 | if(required_stencil && required_stencil != existing_stencil) return FALSE;
|
---|
2515 | return TRUE;
|
---|
2516 | }
|
---|
2517 | /* The caller provides a context */
|
---|
2518 | static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
|
---|
2519 | {
|
---|
2520 | /* Onscreen surfaces are always in a swapchain */
|
---|
2521 | IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *) device->stencilBufferTarget;
|
---|
2522 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
|
---|
2523 |
|
---|
2524 | if (!depth_stencil) return;
|
---|
2525 | if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format_desc)) return;
|
---|
2526 |
|
---|
2527 | /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
|
---|
2528 | * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
|
---|
2529 | * format. */
|
---|
2530 | WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
|
---|
2531 |
|
---|
2532 | /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
|
---|
2533 | IWineD3DSurface_LoadLocation(context->current_rt, SFLAG_INTEXTURE, NULL);
|
---|
2534 | swapchain->render_to_fbo = TRUE;
|
---|
2535 | context_set_render_offscreen(context, device->StateTable, TRUE);
|
---|
2536 | }
|
---|
2537 |
|
---|
2538 | /* Context activation is done by the caller. */
|
---|
2539 | static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
|
---|
2540 | {
|
---|
2541 | const struct StateEntry *state_table = device->StateTable;
|
---|
2542 | unsigned int i;
|
---|
2543 |
|
---|
2544 | switch (usage) {
|
---|
2545 | case CTXUSAGE_CLEAR:
|
---|
2546 | case CTXUSAGE_DRAWPRIM:
|
---|
2547 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
|
---|
2548 | if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
|
---|
2549 | ENTER_GL();
|
---|
2550 | context_apply_fbo_state(context);
|
---|
2551 | LEAVE_GL();
|
---|
2552 | }
|
---|
2553 | if (context->draw_buffer_dirty) {
|
---|
2554 | context_apply_draw_buffer(context, FALSE);
|
---|
2555 | context->draw_buffer_dirty = FALSE;
|
---|
2556 | }
|
---|
2557 | break;
|
---|
2558 |
|
---|
2559 | case CTXUSAGE_BLIT:
|
---|
2560 | case CTXUSAGE_BLIT_LIGHT:
|
---|
2561 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
|
---|
2562 | if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
|
---|
2563 | if (context->render_offscreen)
|
---|
2564 | {
|
---|
2565 | FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
|
---|
2566 | surface_internal_preload(context->current_rt, SRGB_RGB);
|
---|
2567 |
|
---|
2568 | ENTER_GL();
|
---|
2569 | context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
|
---|
2570 | context_attach_surface_fbo(context, GL_FRAMEBUFFER, 0, (IWineD3DSurfaceImpl *)context->current_rt);
|
---|
2571 | context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, NULL, FALSE);
|
---|
2572 | LEAVE_GL();
|
---|
2573 | } else {
|
---|
2574 | ENTER_GL();
|
---|
2575 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
2576 | LEAVE_GL();
|
---|
2577 | }
|
---|
2578 | context->draw_buffer_dirty = TRUE;
|
---|
2579 | }
|
---|
2580 | if (context->draw_buffer_dirty) {
|
---|
2581 | context_apply_draw_buffer(context, TRUE);
|
---|
2582 | if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
|
---|
2583 | context->draw_buffer_dirty = FALSE;
|
---|
2584 | }
|
---|
2585 | }
|
---|
2586 | break;
|
---|
2587 |
|
---|
2588 | default:
|
---|
2589 | break;
|
---|
2590 | }
|
---|
2591 |
|
---|
2592 | switch(usage) {
|
---|
2593 | case CTXUSAGE_RESOURCELOAD:
|
---|
2594 | case CTXUSAGE_BLIT_LIGHT:
|
---|
2595 | /* This does not require any special states to be set up */
|
---|
2596 | break;
|
---|
2597 |
|
---|
2598 | case CTXUSAGE_CLEAR:
|
---|
2599 | if(context->last_was_blit) {
|
---|
2600 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
2601 | }
|
---|
2602 |
|
---|
2603 | /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
|
---|
2604 | * blending when clearing improves the clearing performance incredibly.
|
---|
2605 | */
|
---|
2606 | ENTER_GL();
|
---|
2607 | glDisable(GL_BLEND);
|
---|
2608 | LEAVE_GL();
|
---|
2609 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), state_table);
|
---|
2610 |
|
---|
2611 | ENTER_GL();
|
---|
2612 | glEnable(GL_SCISSOR_TEST);
|
---|
2613 | checkGLcall("glEnable GL_SCISSOR_TEST");
|
---|
2614 | LEAVE_GL();
|
---|
2615 | context->last_was_blit = FALSE;
|
---|
2616 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), state_table);
|
---|
2617 | Context_MarkStateDirty(context, STATE_SCISSORRECT, state_table);
|
---|
2618 | break;
|
---|
2619 |
|
---|
2620 | case CTXUSAGE_DRAWPRIM:
|
---|
2621 | /* This needs all dirty states applied */
|
---|
2622 | if(context->last_was_blit) {
|
---|
2623 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | IWineD3DDeviceImpl_FindTexUnitMap(device);
|
---|
2627 | device_preload_textures(device);
|
---|
2628 | if (isStateDirty(context, STATE_VDECL))
|
---|
2629 | device_update_stream_info(device, context->gl_info);
|
---|
2630 |
|
---|
2631 | ENTER_GL();
|
---|
2632 | for (i = 0; i < context->numDirtyEntries; ++i)
|
---|
2633 | {
|
---|
2634 | DWORD rep = context->dirtyArray[i];
|
---|
2635 | DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
|
---|
2636 | BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
|
---|
2637 | context->isStateDirty[idx] &= ~(1 << shift);
|
---|
2638 | state_table[rep].apply(rep, device->stateBlock, context);
|
---|
2639 | }
|
---|
2640 | LEAVE_GL();
|
---|
2641 | context->numDirtyEntries = 0; /* This makes the whole list clean */
|
---|
2642 | context->last_was_blit = FALSE;
|
---|
2643 | break;
|
---|
2644 |
|
---|
2645 | case CTXUSAGE_BLIT:
|
---|
2646 | SetupForBlit(device, context);
|
---|
2647 | break;
|
---|
2648 |
|
---|
2649 | default:
|
---|
2650 | FIXME("Unexpected context usage requested\n");
|
---|
2651 | }
|
---|
2652 | }
|
---|
2653 |
|
---|
2654 | static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
|
---|
2655 | {
|
---|
2656 | BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
|
---|
2657 | const struct StateEntry *StateTable = device->StateTable;
|
---|
2658 |
|
---|
2659 | if (!target) return;
|
---|
2660 | else if (context->current_rt == target) return;
|
---|
2661 | render_offscreen = surface_is_offscreen(target);
|
---|
2662 |
|
---|
2663 | context_set_render_offscreen(context, StateTable, render_offscreen);
|
---|
2664 |
|
---|
2665 | /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
|
---|
2666 | * the alpha blend state changes with different render target formats. */
|
---|
2667 | if (!context->current_rt)
|
---|
2668 | {
|
---|
2669 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
2670 | }
|
---|
2671 | else
|
---|
2672 | {
|
---|
2673 | const struct wined3d_format_desc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
|
---|
2674 | const struct wined3d_format_desc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
|
---|
2675 |
|
---|
2676 | if (old->format != new->format)
|
---|
2677 | {
|
---|
2678 | /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
|
---|
2679 | if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
|
---|
2680 | || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
|
---|
2681 | {
|
---|
2682 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
2683 | }
|
---|
2684 | }
|
---|
2685 |
|
---|
2686 | /* When switching away from an offscreen render target, and we're not
|
---|
2687 | * using FBOs, we have to read the drawable into the texture. This is
|
---|
2688 | * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
|
---|
2689 | * are some things that need care though. PreLoad needs a GL context,
|
---|
2690 | * and FindContext is called before the context is activated. It also
|
---|
2691 | * has to be called with the old rendertarget active, otherwise a
|
---|
2692 | * wrong drawable is read. */
|
---|
2693 | if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
|
---|
2694 | && old_render_offscreen && context->current_rt != target)
|
---|
2695 | {
|
---|
2696 | BOOL oldInDraw = device->isInDraw;
|
---|
2697 |
|
---|
2698 | /* surface_internal_preload() requires a context to load the
|
---|
2699 | * texture, so it will call context_acquire(). Set isInDraw to true
|
---|
2700 | * to signal surface_internal_preload() that it has a context. */
|
---|
2701 |
|
---|
2702 | /* FIXME: This is just broken. There's no guarantee whatsoever
|
---|
2703 | * that the currently active context, if any, is appropriate for
|
---|
2704 | * reading back the render target. We should probably call
|
---|
2705 | * context_set_current(context) here and then rely on
|
---|
2706 | * context_acquire() doing the right thing. */
|
---|
2707 | device->isInDraw = TRUE;
|
---|
2708 |
|
---|
2709 | /* Read the back buffer of the old drawable into the destination texture. */
|
---|
2710 | if (((IWineD3DSurfaceImpl *)context->current_rt)->texture_name_srgb)
|
---|
2711 | {
|
---|
2712 | surface_internal_preload(context->current_rt, SRGB_BOTH);
|
---|
2713 | }
|
---|
2714 | else
|
---|
2715 | {
|
---|
2716 | surface_internal_preload(context->current_rt, SRGB_RGB);
|
---|
2717 | }
|
---|
2718 |
|
---|
2719 | IWineD3DSurface_ModifyLocation(context->current_rt, SFLAG_INDRAWABLE, FALSE);
|
---|
2720 |
|
---|
2721 | device->isInDraw = oldInDraw;
|
---|
2722 | }
|
---|
2723 | }
|
---|
2724 |
|
---|
2725 | context->draw_buffer_dirty = TRUE;
|
---|
2726 | context->current_rt = target;
|
---|
2727 | }
|
---|
2728 |
|
---|
2729 | /*****************************************************************************
|
---|
2730 | * context_acquire
|
---|
2731 | *
|
---|
2732 | * Finds a rendering context and drawable matching the device and render
|
---|
2733 | * target for the current thread, activates them and puts them into the
|
---|
2734 | * requested state.
|
---|
2735 | *
|
---|
2736 | * Params:
|
---|
2737 | * This: Device to activate the context for
|
---|
2738 | * target: Requested render target
|
---|
2739 | * usage: Prepares the context for blitting, drawing or other actions
|
---|
2740 | *
|
---|
2741 | *****************************************************************************/
|
---|
2742 | struct wined3d_context *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
|
---|
2743 | {
|
---|
2744 | struct wined3d_context *current_context = context_get_current();
|
---|
2745 | struct wined3d_context *context;
|
---|
2746 |
|
---|
2747 | TRACE("device %p, target %p, usage %#x.\n", device, target, usage);
|
---|
2748 |
|
---|
2749 | context = FindContext(device, target);
|
---|
2750 | context_setup_target(device, context, target);
|
---|
2751 | context_enter(context);
|
---|
2752 | if (!context->valid) return context;
|
---|
2753 |
|
---|
2754 | if (context != current_context)
|
---|
2755 | {
|
---|
2756 | if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
|
---|
2757 | else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
|
---|
2758 |
|
---|
2759 | if (context->vshader_const_dirty)
|
---|
2760 | {
|
---|
2761 | memset(context->vshader_const_dirty, 1,
|
---|
2762 | sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
2763 | device->highest_dirty_vs_const = device->d3d_vshader_constantF;
|
---|
2764 | }
|
---|
2765 | if (context->pshader_const_dirty)
|
---|
2766 | {
|
---|
2767 | memset(context->pshader_const_dirty, 1,
|
---|
2768 | sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
2769 | device->highest_dirty_ps_const = device->d3d_pshader_constantF;
|
---|
2770 | }
|
---|
2771 | }
|
---|
2772 | else if (context->restore_ctx)
|
---|
2773 | {
|
---|
2774 | #ifdef VBOX_WITH_WDDM
|
---|
2775 | if (!pwglMakeCurrent(context->currentSwapchain->hDC, context->glCtx))
|
---|
2776 | {
|
---|
2777 | DWORD err = GetLastError();
|
---|
2778 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
2779 | context->currentSwapchain->hDC, context->glCtx, err);
|
---|
2780 | }
|
---|
2781 | #else
|
---|
2782 | if (!pwglMakeCurrent(context->hdc, context->glCtx))
|
---|
2783 | {
|
---|
2784 | DWORD err = GetLastError();
|
---|
2785 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
2786 | context->hdc, context->glCtx, err);
|
---|
2787 | }
|
---|
2788 | #endif
|
---|
2789 | }
|
---|
2790 |
|
---|
2791 | context_apply_state(context, device, usage);
|
---|
2792 |
|
---|
2793 | return context;
|
---|
2794 | }
|
---|