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 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
25 | * a choice of LGPL license versions is made available with the language indicating
|
---|
26 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the LGPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
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 | #ifdef VBOXWDDM
|
---|
334 | IWineD3DDeviceImpl *device = context->device;
|
---|
335 | #else
|
---|
336 | IWineD3DDeviceImpl *device = context->swapchain->device;
|
---|
337 | #endif
|
---|
338 | struct fbo_entry *entry;
|
---|
339 |
|
---|
340 | entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
|
---|
341 | entry->render_targets = HeapAlloc(GetProcessHeap(), 0, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
342 | memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
343 | entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
|
---|
344 | entry->attached = FALSE;
|
---|
345 | entry->id = 0;
|
---|
346 |
|
---|
347 | return entry;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* GL locking is done by the caller */
|
---|
351 | static void context_reuse_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
352 | {
|
---|
353 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
354 | #ifdef VBOXWDDM
|
---|
355 | IWineD3DDeviceImpl *device = context->device;
|
---|
356 | #else
|
---|
357 | IWineD3DDeviceImpl *device = context->swapchain->device;
|
---|
358 | #endif
|
---|
359 |
|
---|
360 | context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
|
---|
361 | context_clean_fbo_attachments(gl_info);
|
---|
362 |
|
---|
363 | memcpy(entry->render_targets, device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets));
|
---|
364 | entry->depth_stencil = (IWineD3DSurfaceImpl *)device->stencilBufferTarget;
|
---|
365 | entry->attached = FALSE;
|
---|
366 | }
|
---|
367 |
|
---|
368 | /* GL locking is done by the caller */
|
---|
369 | static void context_destroy_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
370 | {
|
---|
371 | if (entry->id)
|
---|
372 | {
|
---|
373 | TRACE("Destroy FBO %d\n", entry->id);
|
---|
374 | context_destroy_fbo(context, &entry->id);
|
---|
375 | }
|
---|
376 | --context->fbo_entry_count;
|
---|
377 | list_remove(&entry->entry);
|
---|
378 | HeapFree(GetProcessHeap(), 0, entry->render_targets);
|
---|
379 | HeapFree(GetProcessHeap(), 0, entry);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | /* GL locking is done by the caller */
|
---|
384 | static struct fbo_entry *context_find_fbo_entry(struct wined3d_context *context)
|
---|
385 | {
|
---|
386 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
387 | #ifdef VBOXWDDM
|
---|
388 | IWineD3DDeviceImpl *device = context->device;
|
---|
389 | #else
|
---|
390 | IWineD3DDeviceImpl *device = context->swapchain->device;
|
---|
391 | #endif
|
---|
392 | struct fbo_entry *entry;
|
---|
393 |
|
---|
394 | LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
|
---|
395 | {
|
---|
396 | if (!memcmp(entry->render_targets,
|
---|
397 | device->render_targets, gl_info->limits.buffers * sizeof(*entry->render_targets))
|
---|
398 | && entry->depth_stencil == (IWineD3DSurfaceImpl *)device->stencilBufferTarget)
|
---|
399 | {
|
---|
400 | list_remove(&entry->entry);
|
---|
401 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
402 | return entry;
|
---|
403 | }
|
---|
404 | }
|
---|
405 |
|
---|
406 | if (context->fbo_entry_count < WINED3D_MAX_FBO_ENTRIES)
|
---|
407 | {
|
---|
408 | entry = context_create_fbo_entry(context);
|
---|
409 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
410 | ++context->fbo_entry_count;
|
---|
411 | }
|
---|
412 | else
|
---|
413 | {
|
---|
414 | entry = LIST_ENTRY(list_tail(&context->fbo_list), struct fbo_entry, entry);
|
---|
415 | context_reuse_fbo_entry(context, entry);
|
---|
416 | list_remove(&entry->entry);
|
---|
417 | list_add_head(&context->fbo_list, &entry->entry);
|
---|
418 | }
|
---|
419 |
|
---|
420 | return entry;
|
---|
421 | }
|
---|
422 |
|
---|
423 | /* GL locking is done by the caller */
|
---|
424 | static void context_apply_fbo_entry(struct wined3d_context *context, struct fbo_entry *entry)
|
---|
425 | {
|
---|
426 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
427 | #ifdef VBOXWDDM
|
---|
428 | IWineD3DDeviceImpl *device = context->device;
|
---|
429 | #else
|
---|
430 | IWineD3DDeviceImpl *device = context->swapchain->device;
|
---|
431 | #endif
|
---|
432 | unsigned int i;
|
---|
433 |
|
---|
434 | context_bind_fbo(context, GL_FRAMEBUFFER, &entry->id);
|
---|
435 |
|
---|
436 | if (!entry->attached)
|
---|
437 | {
|
---|
438 | /* Apply render targets */
|
---|
439 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
440 | {
|
---|
441 | IWineD3DSurfaceImpl *render_target = (IWineD3DSurfaceImpl *)device->render_targets[i];
|
---|
442 | context_attach_surface_fbo(context, GL_FRAMEBUFFER, i, render_target);
|
---|
443 | }
|
---|
444 |
|
---|
445 | /* Apply depth targets */
|
---|
446 | if (device->stencilBufferTarget)
|
---|
447 | {
|
---|
448 | unsigned int w = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Width;
|
---|
449 | unsigned int h = ((IWineD3DSurfaceImpl *)device->render_targets[0])->pow2Height;
|
---|
450 |
|
---|
451 | surface_set_compatible_renderbuffer(device->stencilBufferTarget, w, h);
|
---|
452 | }
|
---|
453 | context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, (IWineD3DSurfaceImpl *)device->stencilBufferTarget, TRUE);
|
---|
454 |
|
---|
455 | entry->attached = TRUE;
|
---|
456 | }
|
---|
457 | else
|
---|
458 | {
|
---|
459 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
460 | {
|
---|
461 | if (device->render_targets[i])
|
---|
462 | context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->render_targets[i]);
|
---|
463 | }
|
---|
464 | if (device->stencilBufferTarget)
|
---|
465 | context_apply_attachment_filter_states((IWineD3DSurfaceImpl *)device->stencilBufferTarget);
|
---|
466 | }
|
---|
467 |
|
---|
468 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
469 | {
|
---|
470 | if (device->render_targets[i])
|
---|
471 | device->draw_buffers[i] = GL_COLOR_ATTACHMENT0 + i;
|
---|
472 | else
|
---|
473 | device->draw_buffers[i] = GL_NONE;
|
---|
474 | }
|
---|
475 | }
|
---|
476 |
|
---|
477 | /* GL locking is done by the caller */
|
---|
478 | static void context_apply_fbo_state(struct wined3d_context *context)
|
---|
479 | {
|
---|
480 | struct fbo_entry *entry, *entry2;
|
---|
481 |
|
---|
482 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
|
---|
483 | {
|
---|
484 | context_destroy_fbo_entry(context, entry);
|
---|
485 | }
|
---|
486 |
|
---|
487 | if (context->rebind_fbo)
|
---|
488 | {
|
---|
489 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
490 | context->rebind_fbo = FALSE;
|
---|
491 | }
|
---|
492 |
|
---|
493 | if (context->render_offscreen)
|
---|
494 | {
|
---|
495 | context->current_fbo = context_find_fbo_entry(context);
|
---|
496 | context_apply_fbo_entry(context, context->current_fbo);
|
---|
497 | } else {
|
---|
498 | context->current_fbo = NULL;
|
---|
499 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
500 | }
|
---|
501 |
|
---|
502 | #if defined(DEBUG) && !defined(DEBUG_misha)
|
---|
503 | context_check_fbo_status(context);
|
---|
504 | #endif
|
---|
505 | }
|
---|
506 |
|
---|
507 | /* Context activation is done by the caller. */
|
---|
508 | void context_alloc_occlusion_query(struct wined3d_context *context, struct wined3d_occlusion_query *query)
|
---|
509 | {
|
---|
510 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
511 |
|
---|
512 | if (context->free_occlusion_query_count)
|
---|
513 | {
|
---|
514 | query->id = context->free_occlusion_queries[--context->free_occlusion_query_count];
|
---|
515 | }
|
---|
516 | else
|
---|
517 | {
|
---|
518 | if (gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
519 | {
|
---|
520 | ENTER_GL();
|
---|
521 | GL_EXTCALL(glGenQueriesARB(1, &query->id));
|
---|
522 | checkGLcall("glGenQueriesARB");
|
---|
523 | LEAVE_GL();
|
---|
524 |
|
---|
525 | TRACE("Allocated occlusion query %u in context %p.\n", query->id, context);
|
---|
526 | }
|
---|
527 | else
|
---|
528 | {
|
---|
529 | WARN("Occlusion queries not supported, not allocating query id.\n");
|
---|
530 | query->id = 0;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | query->context = context;
|
---|
535 | list_add_head(&context->occlusion_queries, &query->entry);
|
---|
536 | }
|
---|
537 |
|
---|
538 | void context_free_occlusion_query(struct wined3d_occlusion_query *query)
|
---|
539 | {
|
---|
540 | struct wined3d_context *context = query->context;
|
---|
541 |
|
---|
542 | list_remove(&query->entry);
|
---|
543 | query->context = NULL;
|
---|
544 |
|
---|
545 | if (context->free_occlusion_query_count >= context->free_occlusion_query_size - 1)
|
---|
546 | {
|
---|
547 | UINT new_size = context->free_occlusion_query_size << 1;
|
---|
548 | GLuint *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_occlusion_queries,
|
---|
549 | new_size * sizeof(*context->free_occlusion_queries));
|
---|
550 |
|
---|
551 | if (!new_data)
|
---|
552 | {
|
---|
553 | ERR("Failed to grow free list, leaking query %u in context %p.\n", query->id, context);
|
---|
554 | return;
|
---|
555 | }
|
---|
556 |
|
---|
557 | context->free_occlusion_query_size = new_size;
|
---|
558 | context->free_occlusion_queries = new_data;
|
---|
559 | }
|
---|
560 |
|
---|
561 | context->free_occlusion_queries[context->free_occlusion_query_count++] = query->id;
|
---|
562 | }
|
---|
563 |
|
---|
564 | /* Context activation is done by the caller. */
|
---|
565 | void context_alloc_event_query(struct wined3d_context *context, struct wined3d_event_query *query)
|
---|
566 | {
|
---|
567 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
568 |
|
---|
569 | if (context->free_event_query_count)
|
---|
570 | {
|
---|
571 | query->object = context->free_event_queries[--context->free_event_query_count];
|
---|
572 | }
|
---|
573 | else
|
---|
574 | {
|
---|
575 | if (gl_info->supported[ARB_SYNC])
|
---|
576 | {
|
---|
577 | /* Using ARB_sync, not much to do here. */
|
---|
578 | query->object.sync = NULL;
|
---|
579 | TRACE("Allocated event query %p in context %p.\n", query->object.sync, context);
|
---|
580 | }
|
---|
581 | else if (gl_info->supported[APPLE_FENCE])
|
---|
582 | {
|
---|
583 | ENTER_GL();
|
---|
584 | GL_EXTCALL(glGenFencesAPPLE(1, &query->object.id));
|
---|
585 | checkGLcall("glGenFencesAPPLE");
|
---|
586 | LEAVE_GL();
|
---|
587 |
|
---|
588 | TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
|
---|
589 | }
|
---|
590 | else if(gl_info->supported[NV_FENCE])
|
---|
591 | {
|
---|
592 | ENTER_GL();
|
---|
593 | GL_EXTCALL(glGenFencesNV(1, &query->object.id));
|
---|
594 | checkGLcall("glGenFencesNV");
|
---|
595 | LEAVE_GL();
|
---|
596 |
|
---|
597 | TRACE("Allocated event query %u in context %p.\n", query->object.id, context);
|
---|
598 | }
|
---|
599 | else
|
---|
600 | {
|
---|
601 | WARN("Event queries not supported, not allocating query id.\n");
|
---|
602 | query->object.id = 0;
|
---|
603 | }
|
---|
604 | }
|
---|
605 |
|
---|
606 | query->context = context;
|
---|
607 | list_add_head(&context->event_queries, &query->entry);
|
---|
608 | }
|
---|
609 |
|
---|
610 | void context_free_event_query(struct wined3d_event_query *query)
|
---|
611 | {
|
---|
612 | struct wined3d_context *context = query->context;
|
---|
613 |
|
---|
614 | list_remove(&query->entry);
|
---|
615 | query->context = NULL;
|
---|
616 |
|
---|
617 | if (context->free_event_query_count >= context->free_event_query_size - 1)
|
---|
618 | {
|
---|
619 | UINT new_size = context->free_event_query_size << 1;
|
---|
620 | union wined3d_gl_query_object *new_data = HeapReAlloc(GetProcessHeap(), 0, context->free_event_queries,
|
---|
621 | new_size * sizeof(*context->free_event_queries));
|
---|
622 |
|
---|
623 | if (!new_data)
|
---|
624 | {
|
---|
625 | ERR("Failed to grow free list, leaking query %u in context %p.\n", query->object.id, context);
|
---|
626 | return;
|
---|
627 | }
|
---|
628 |
|
---|
629 | context->free_event_query_size = new_size;
|
---|
630 | context->free_event_queries = new_data;
|
---|
631 | }
|
---|
632 |
|
---|
633 | context->free_event_queries[context->free_event_query_count++] = query->object;
|
---|
634 | }
|
---|
635 |
|
---|
636 | void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
|
---|
637 | {
|
---|
638 | IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
|
---|
639 | UINT i;
|
---|
640 |
|
---|
641 | if (!This->d3d_initialized) return;
|
---|
642 |
|
---|
643 | switch(type)
|
---|
644 | {
|
---|
645 | case WINED3DRTYPE_SURFACE:
|
---|
646 | {
|
---|
647 | for (i = 0; i < This->numContexts; ++i)
|
---|
648 | {
|
---|
649 | struct wined3d_context *context = This->contexts[i];
|
---|
650 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
651 | struct fbo_entry *entry, *entry2;
|
---|
652 |
|
---|
653 | if (context->current_rt == (IWineD3DSurface *)resource) context->current_rt = NULL;
|
---|
654 |
|
---|
655 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
|
---|
656 | {
|
---|
657 | UINT j;
|
---|
658 |
|
---|
659 | if (entry->depth_stencil == (IWineD3DSurfaceImpl *)resource)
|
---|
660 | {
|
---|
661 | list_remove(&entry->entry);
|
---|
662 | list_add_head(&context->fbo_destroy_list, &entry->entry);
|
---|
663 | continue;
|
---|
664 | }
|
---|
665 |
|
---|
666 | for (j = 0; j < gl_info->limits.buffers; ++j)
|
---|
667 | {
|
---|
668 | if (entry->render_targets[j] == (IWineD3DSurfaceImpl *)resource)
|
---|
669 | {
|
---|
670 | list_remove(&entry->entry);
|
---|
671 | list_add_head(&context->fbo_destroy_list, &entry->entry);
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | }
|
---|
675 | }
|
---|
676 | }
|
---|
677 |
|
---|
678 | break;
|
---|
679 | }
|
---|
680 |
|
---|
681 | default:
|
---|
682 | break;
|
---|
683 | }
|
---|
684 | }
|
---|
685 |
|
---|
686 | void context_surface_update(struct wined3d_context *context, IWineD3DSurfaceImpl *surface)
|
---|
687 | {
|
---|
688 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
689 | struct fbo_entry *entry = context->current_fbo;
|
---|
690 | unsigned int i;
|
---|
691 |
|
---|
692 | if (!entry || context->rebind_fbo) return;
|
---|
693 |
|
---|
694 | for (i = 0; i < gl_info->limits.buffers; ++i)
|
---|
695 | {
|
---|
696 | if (surface == entry->render_targets[i])
|
---|
697 | {
|
---|
698 | TRACE("Updated surface %p is bound as color attachment %u to the current FBO.\n", surface, i);
|
---|
699 | context->rebind_fbo = TRUE;
|
---|
700 | return;
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 | if (surface == entry->depth_stencil)
|
---|
705 | {
|
---|
706 | TRACE("Updated surface %p is bound as depth attachment to the current FBO.\n", surface);
|
---|
707 | context->rebind_fbo = TRUE;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | static BOOL context_set_pixel_format(const struct wined3d_gl_info *gl_info, HDC dc, int format)
|
---|
712 | {
|
---|
713 | int current = GetPixelFormat(dc);
|
---|
714 |
|
---|
715 | if (current == format) return TRUE;
|
---|
716 |
|
---|
717 | if (!current)
|
---|
718 | {
|
---|
719 | if (!SetPixelFormat(dc, format, NULL))
|
---|
720 | {
|
---|
721 | ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
|
---|
722 | format, dc, GetLastError());
|
---|
723 | return FALSE;
|
---|
724 | }
|
---|
725 | return TRUE;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /* By default WGL doesn't allow pixel format adjustments but we need it
|
---|
729 | * here. For this reason there's a Wine specific wglSetPixelFormat()
|
---|
730 | * which allows us to set the pixel format multiple times. Only use it
|
---|
731 | * when really needed. */
|
---|
732 | if (gl_info->supported[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH])
|
---|
733 | {
|
---|
734 | if (!GL_EXTCALL(wglSetPixelFormatWINE(dc, format, NULL)))
|
---|
735 | {
|
---|
736 | ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
|
---|
737 | format, dc);
|
---|
738 | return FALSE;
|
---|
739 | }
|
---|
740 | return TRUE;
|
---|
741 | }
|
---|
742 |
|
---|
743 | /* OpenGL doesn't allow pixel format adjustments. Print an error and
|
---|
744 | * continue using the old format. There's a big chance that the old
|
---|
745 | * format works although with a performance hit and perhaps rendering
|
---|
746 | * errors. */
|
---|
747 | ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
|
---|
748 | format, dc, current);
|
---|
749 | return TRUE;
|
---|
750 | }
|
---|
751 |
|
---|
752 | static void context_update_window(struct wined3d_context *context
|
---|
753 | #ifdef VBOXWDDM
|
---|
754 | , IWineD3DSwapChainImpl *swapchain
|
---|
755 | #endif
|
---|
756 | )
|
---|
757 | {
|
---|
758 | #ifdef VBOXWDDM
|
---|
759 | # ifdef DEBUG_misha
|
---|
760 | Assert(0);
|
---|
761 | # endif
|
---|
762 | TRACE("Updating context %p window from %p to %p.\n",
|
---|
763 | context, context->win_handle, swapchain->win_handle);
|
---|
764 | #else
|
---|
765 | TRACE("Updating context %p window from %p to %p.\n",
|
---|
766 | context, context->win_handle, context->swapchain->win_handle);
|
---|
767 | #endif
|
---|
768 |
|
---|
769 | if (context->valid)
|
---|
770 | {
|
---|
771 | if (!ReleaseDC(context->win_handle, context->hdc))
|
---|
772 | {
|
---|
773 | ERR("Failed to release device context %p, last error %#x.\n",
|
---|
774 | context->hdc, GetLastError());
|
---|
775 | }
|
---|
776 | }
|
---|
777 | else context->valid = 1;
|
---|
778 |
|
---|
779 | #ifdef VBOXWDDM
|
---|
780 | context->win_handle = swapchain->win_handle;
|
---|
781 | context->currentSwapchain = swapchain;
|
---|
782 | #else
|
---|
783 | context->win_handle = context->swapchain->win_handle;
|
---|
784 | #endif
|
---|
785 | if (!(context->hdc = GetDC(context->win_handle)))
|
---|
786 | {
|
---|
787 | ERR("Failed to get a device context for window %p.\n", context->win_handle);
|
---|
788 | goto err;
|
---|
789 | }
|
---|
790 |
|
---|
791 | if (!context_set_pixel_format(context->gl_info, context->hdc, context->pixel_format))
|
---|
792 | {
|
---|
793 | ERR("Failed to set pixel format %d on device context %p.\n",
|
---|
794 | context->pixel_format, context->hdc);
|
---|
795 | goto err;
|
---|
796 | }
|
---|
797 |
|
---|
798 | if (!pwglMakeCurrent(context->hdc, context->glCtx))
|
---|
799 | {
|
---|
800 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
801 | context->glCtx, context->hdc, GetLastError());
|
---|
802 | goto err;
|
---|
803 | }
|
---|
804 |
|
---|
805 | return;
|
---|
806 |
|
---|
807 | err:
|
---|
808 | context->valid = 0;
|
---|
809 | }
|
---|
810 |
|
---|
811 | static void context_validate(struct wined3d_context *context
|
---|
812 | #ifdef VBOXWDDM
|
---|
813 | , IWineD3DSwapChainImpl *swapchain
|
---|
814 | #endif
|
---|
815 | )
|
---|
816 | {
|
---|
817 | HWND wnd = WindowFromDC(context->hdc);
|
---|
818 |
|
---|
819 | if (wnd != context->win_handle)
|
---|
820 | {
|
---|
821 | WARN("DC %p belongs to window %p instead of %p.\n",
|
---|
822 | context->hdc, wnd, context->win_handle);
|
---|
823 | context->valid = 0;
|
---|
824 | }
|
---|
825 |
|
---|
826 | if (
|
---|
827 | #ifdef VBOXWDDM
|
---|
828 | swapchain && context->win_handle != swapchain->win_handle
|
---|
829 | #else
|
---|
830 | context->win_handle != context->swapchain->win_handle
|
---|
831 | #endif
|
---|
832 | )
|
---|
833 | {
|
---|
834 | context_update_window(context
|
---|
835 | #ifdef VBOXWDDM
|
---|
836 | , swapchain
|
---|
837 | #endif
|
---|
838 | );
|
---|
839 | }
|
---|
840 | }
|
---|
841 |
|
---|
842 | static void context_destroy_gl_resources(struct wined3d_context *context)
|
---|
843 | {
|
---|
844 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
845 | struct wined3d_occlusion_query *occlusion_query;
|
---|
846 | struct wined3d_event_query *event_query;
|
---|
847 | struct fbo_entry *entry, *entry2;
|
---|
848 | HGLRC restore_ctx;
|
---|
849 | HDC restore_dc;
|
---|
850 | unsigned int i;
|
---|
851 |
|
---|
852 | restore_ctx = pwglGetCurrentContext();
|
---|
853 | restore_dc = pwglGetCurrentDC();
|
---|
854 |
|
---|
855 | context_validate(context
|
---|
856 | #ifdef VBOXWDDM/* tmp work-around */
|
---|
857 | , context->device->swapchains[context->device->NumberOfSwapChains-1]
|
---|
858 | #endif
|
---|
859 | );
|
---|
860 | if (context->valid && restore_ctx != context->glCtx) pwglMakeCurrent(context->hdc, context->glCtx);
|
---|
861 | else restore_ctx = NULL;
|
---|
862 |
|
---|
863 | ENTER_GL();
|
---|
864 |
|
---|
865 | LIST_FOR_EACH_ENTRY(occlusion_query, &context->occlusion_queries, struct wined3d_occlusion_query, entry)
|
---|
866 | {
|
---|
867 | if (context->valid && gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
868 | GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query->id));
|
---|
869 | occlusion_query->context = NULL;
|
---|
870 | }
|
---|
871 |
|
---|
872 | LIST_FOR_EACH_ENTRY(event_query, &context->event_queries, struct wined3d_event_query, entry)
|
---|
873 | {
|
---|
874 | if (context->valid)
|
---|
875 | {
|
---|
876 | if (gl_info->supported[ARB_SYNC])
|
---|
877 | {
|
---|
878 | if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
|
---|
879 | }
|
---|
880 | else if (gl_info->supported[APPLE_FENCE]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query->object.id));
|
---|
881 | else if (gl_info->supported[NV_FENCE]) GL_EXTCALL(glDeleteFencesNV(1, &event_query->object.id));
|
---|
882 | }
|
---|
883 | event_query->context = NULL;
|
---|
884 | }
|
---|
885 |
|
---|
886 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_destroy_list, struct fbo_entry, entry)
|
---|
887 | {
|
---|
888 | if (!context->valid) entry->id = 0;
|
---|
889 | context_destroy_fbo_entry(context, entry);
|
---|
890 | }
|
---|
891 |
|
---|
892 | LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry)
|
---|
893 | {
|
---|
894 | if (!context->valid) entry->id = 0;
|
---|
895 | context_destroy_fbo_entry(context, entry);
|
---|
896 | }
|
---|
897 |
|
---|
898 | if (context->valid)
|
---|
899 | {
|
---|
900 | if (context->src_fbo)
|
---|
901 | {
|
---|
902 | TRACE("Destroy src FBO %d\n", context->src_fbo);
|
---|
903 | context_destroy_fbo(context, &context->src_fbo);
|
---|
904 | }
|
---|
905 | if (context->dst_fbo)
|
---|
906 | {
|
---|
907 | TRACE("Destroy dst FBO %d\n", context->dst_fbo);
|
---|
908 | context_destroy_fbo(context, &context->dst_fbo);
|
---|
909 | }
|
---|
910 | if (context->dummy_arbfp_prog)
|
---|
911 | {
|
---|
912 | GL_EXTCALL(glDeleteProgramsARB(1, &context->dummy_arbfp_prog));
|
---|
913 | }
|
---|
914 |
|
---|
915 | if (gl_info->supported[ARB_OCCLUSION_QUERY])
|
---|
916 | GL_EXTCALL(glDeleteQueriesARB(context->free_occlusion_query_count, context->free_occlusion_queries));
|
---|
917 |
|
---|
918 | if (gl_info->supported[ARB_SYNC])
|
---|
919 | {
|
---|
920 | if (event_query->object.sync) GL_EXTCALL(glDeleteSync(event_query->object.sync));
|
---|
921 | }
|
---|
922 | else if (gl_info->supported[APPLE_FENCE])
|
---|
923 | {
|
---|
924 | for (i = 0; i < context->free_event_query_count; ++i)
|
---|
925 | {
|
---|
926 | GL_EXTCALL(glDeleteFencesAPPLE(1, &context->free_event_queries[i].id));
|
---|
927 | }
|
---|
928 | }
|
---|
929 | else if (gl_info->supported[NV_FENCE])
|
---|
930 | {
|
---|
931 | for (i = 0; i < context->free_event_query_count; ++i)
|
---|
932 | {
|
---|
933 | GL_EXTCALL(glDeleteFencesNV(1, &context->free_event_queries[i].id));
|
---|
934 | }
|
---|
935 | }
|
---|
936 |
|
---|
937 | checkGLcall("context cleanup");
|
---|
938 | }
|
---|
939 |
|
---|
940 | LEAVE_GL();
|
---|
941 |
|
---|
942 | HeapFree(GetProcessHeap(), 0, context->free_occlusion_queries);
|
---|
943 | HeapFree(GetProcessHeap(), 0, context->free_event_queries);
|
---|
944 |
|
---|
945 | if (restore_ctx)
|
---|
946 | {
|
---|
947 | if (!pwglMakeCurrent(restore_dc, restore_ctx))
|
---|
948 | {
|
---|
949 | DWORD err = GetLastError();
|
---|
950 | ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
|
---|
951 | restore_ctx, restore_dc, err);
|
---|
952 | }
|
---|
953 | }
|
---|
954 | else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL, NULL))
|
---|
955 | {
|
---|
956 | ERR("Failed to disable GL context.\n");
|
---|
957 | }
|
---|
958 |
|
---|
959 | ReleaseDC(context->win_handle, context->hdc);
|
---|
960 |
|
---|
961 | if (!pwglDeleteContext(context->glCtx))
|
---|
962 | {
|
---|
963 | DWORD err = GetLastError();
|
---|
964 | ERR("wglDeleteContext(%p) failed, last error %#x.\n", context->glCtx, err);
|
---|
965 | }
|
---|
966 | }
|
---|
967 |
|
---|
968 | DWORD context_get_tls_idx(void)
|
---|
969 | {
|
---|
970 | return wined3d_context_tls_idx;
|
---|
971 | }
|
---|
972 |
|
---|
973 | void context_set_tls_idx(DWORD idx)
|
---|
974 | {
|
---|
975 | wined3d_context_tls_idx = idx;
|
---|
976 | }
|
---|
977 |
|
---|
978 | struct wined3d_context *context_get_current(void)
|
---|
979 | {
|
---|
980 | return TlsGetValue(wined3d_context_tls_idx);
|
---|
981 | }
|
---|
982 |
|
---|
983 | BOOL context_set_current(struct wined3d_context *ctx)
|
---|
984 | {
|
---|
985 | struct wined3d_context *old = context_get_current();
|
---|
986 |
|
---|
987 | if (old == ctx)
|
---|
988 | {
|
---|
989 | TRACE("Already using D3D context %p.\n", ctx);
|
---|
990 | return TRUE;
|
---|
991 | }
|
---|
992 |
|
---|
993 | if (old)
|
---|
994 | {
|
---|
995 | if (old->destroyed)
|
---|
996 | {
|
---|
997 | TRACE("Switching away from destroyed context %p.\n", old);
|
---|
998 | context_destroy_gl_resources(old);
|
---|
999 | HeapFree(GetProcessHeap(), 0, old);
|
---|
1000 | }
|
---|
1001 | else
|
---|
1002 | {
|
---|
1003 | old->current = 0;
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | if (ctx)
|
---|
1008 | {
|
---|
1009 | TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx, ctx->glCtx, ctx->hdc);
|
---|
1010 | if (!pwglMakeCurrent(ctx->hdc, ctx->glCtx))
|
---|
1011 | {
|
---|
1012 | DWORD err = GetLastError();
|
---|
1013 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
1014 | ctx->glCtx, ctx->hdc, err);
|
---|
1015 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1016 | return FALSE;
|
---|
1017 | }
|
---|
1018 | ctx->current = 1;
|
---|
1019 | }
|
---|
1020 | else if(pwglGetCurrentContext())
|
---|
1021 | {
|
---|
1022 | TRACE("Clearing current D3D context.\n");
|
---|
1023 | if (!pwglMakeCurrent(NULL, NULL))
|
---|
1024 | {
|
---|
1025 | DWORD err = GetLastError();
|
---|
1026 | ERR("Failed to clear current GL context, last error %#x.\n", err);
|
---|
1027 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1028 | return FALSE;
|
---|
1029 | }
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | return TlsSetValue(wined3d_context_tls_idx, ctx);
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | void context_release(struct wined3d_context *context)
|
---|
1036 | {
|
---|
1037 | TRACE("Releasing context %p, level %u.\n", context, context->level);
|
---|
1038 |
|
---|
1039 | if (WARN_ON(d3d))
|
---|
1040 | {
|
---|
1041 | if (!context->level)
|
---|
1042 | WARN("Context %p is not active.\n", context);
|
---|
1043 | else if (context != context_get_current())
|
---|
1044 | WARN("Context %p is not the current context.\n", context);
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | if (!--context->level && context->restore_ctx)
|
---|
1048 | {
|
---|
1049 | TRACE("Restoring GL context %p on device context %p.\n", context->restore_ctx, context->restore_dc);
|
---|
1050 | if (!pwglMakeCurrent(context->restore_dc, context->restore_ctx))
|
---|
1051 | {
|
---|
1052 | DWORD err = GetLastError();
|
---|
1053 | ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
|
---|
1054 | context->restore_ctx, context->restore_dc, err);
|
---|
1055 | }
|
---|
1056 | context->restore_ctx = NULL;
|
---|
1057 | context->restore_dc = NULL;
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | static void context_enter(struct wined3d_context *context)
|
---|
1062 | {
|
---|
1063 | TRACE("Entering context %p, level %u.\n", context, context->level + 1);
|
---|
1064 |
|
---|
1065 | if (!context->level++)
|
---|
1066 | {
|
---|
1067 | const struct wined3d_context *current_context = context_get_current();
|
---|
1068 | HGLRC current_gl = pwglGetCurrentContext();
|
---|
1069 |
|
---|
1070 | if (current_gl && (!current_context || current_context->glCtx != current_gl))
|
---|
1071 | {
|
---|
1072 | TRACE("Another GL context (%p on device context %p) is already current.\n",
|
---|
1073 | current_gl, pwglGetCurrentDC());
|
---|
1074 | context->restore_ctx = current_gl;
|
---|
1075 | context->restore_dc = pwglGetCurrentDC();
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | /*****************************************************************************
|
---|
1081 | * Context_MarkStateDirty
|
---|
1082 | *
|
---|
1083 | * Marks a state in a context dirty. Only one context, opposed to
|
---|
1084 | * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
|
---|
1085 | * contexts
|
---|
1086 | *
|
---|
1087 | * Params:
|
---|
1088 | * context: Context to mark the state dirty in
|
---|
1089 | * state: State to mark dirty
|
---|
1090 | * StateTable: Pointer to the state table in use(for state grouping)
|
---|
1091 | *
|
---|
1092 | *****************************************************************************/
|
---|
1093 | static void Context_MarkStateDirty(struct wined3d_context *context, DWORD state, const struct StateEntry *StateTable)
|
---|
1094 | {
|
---|
1095 | DWORD rep = StateTable[state].representative;
|
---|
1096 | DWORD idx;
|
---|
1097 | BYTE shift;
|
---|
1098 |
|
---|
1099 | if (isStateDirty(context, rep)) return;
|
---|
1100 |
|
---|
1101 | context->dirtyArray[context->numDirtyEntries++] = rep;
|
---|
1102 | idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
|
---|
1103 | shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
|
---|
1104 | context->isStateDirty[idx] |= (1 << shift);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /* This function takes care of WineD3D pixel format selection. */
|
---|
1108 | static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc,
|
---|
1109 | const struct wined3d_format_desc *color_format_desc, const struct wined3d_format_desc *ds_format_desc,
|
---|
1110 | BOOL auxBuffers, int numSamples, BOOL findCompatible)
|
---|
1111 | {
|
---|
1112 | int iPixelFormat=0;
|
---|
1113 | unsigned int matchtry;
|
---|
1114 | short redBits, greenBits, blueBits, alphaBits, colorBits;
|
---|
1115 | short depthBits=0, stencilBits=0;
|
---|
1116 |
|
---|
1117 | struct match_type {
|
---|
1118 | BOOL require_aux;
|
---|
1119 | BOOL exact_alpha;
|
---|
1120 | BOOL exact_color;
|
---|
1121 | } matches[] = {
|
---|
1122 | /* First, try without alpha match buffers. MacOS supports aux buffers only
|
---|
1123 | * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
|
---|
1124 | * Then try without aux buffers - this is the most common cause for not
|
---|
1125 | * finding a pixel format. Also some drivers(the open source ones)
|
---|
1126 | * only offer 32 bit ARB pixel formats. First try without an exact alpha
|
---|
1127 | * match, then try without an exact alpha and color match.
|
---|
1128 | */
|
---|
1129 | { TRUE, TRUE, TRUE },
|
---|
1130 | { TRUE, FALSE, TRUE },
|
---|
1131 | { FALSE, TRUE, TRUE },
|
---|
1132 | { FALSE, FALSE, TRUE },
|
---|
1133 | { TRUE, FALSE, FALSE },
|
---|
1134 | { FALSE, FALSE, FALSE },
|
---|
1135 | };
|
---|
1136 |
|
---|
1137 | int i = 0;
|
---|
1138 | int nCfgs = This->adapter->nCfgs;
|
---|
1139 |
|
---|
1140 | TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
|
---|
1141 | debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format),
|
---|
1142 | auxBuffers, numSamples, findCompatible);
|
---|
1143 |
|
---|
1144 | if (!getColorBits(color_format_desc, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
|
---|
1145 | {
|
---|
1146 | ERR("Unable to get color bits for format %s (%#x)!\n",
|
---|
1147 | debug_d3dformat(color_format_desc->format), color_format_desc->format);
|
---|
1148 | return 0;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | getDepthStencilBits(ds_format_desc, &depthBits, &stencilBits);
|
---|
1152 |
|
---|
1153 | for(matchtry = 0; matchtry < (sizeof(matches) / sizeof(matches[0])) && !iPixelFormat; matchtry++) {
|
---|
1154 | for(i=0; i<nCfgs; i++) {
|
---|
1155 | BOOL exactDepthMatch = TRUE;
|
---|
1156 | WineD3D_PixelFormat *cfg = &This->adapter->cfgs[i];
|
---|
1157 |
|
---|
1158 | /* For now only accept RGBA formats. Perhaps some day we will
|
---|
1159 | * allow floating point formats for pbuffers. */
|
---|
1160 | if(cfg->iPixelType != WGL_TYPE_RGBA_ARB)
|
---|
1161 | continue;
|
---|
1162 |
|
---|
1163 | /* In window mode we need a window drawable format and double buffering. */
|
---|
1164 | if(!(cfg->windowDrawable && cfg->doubleBuffer))
|
---|
1165 | continue;
|
---|
1166 |
|
---|
1167 | /* We like to have aux buffers in backbuffer mode */
|
---|
1168 | if(auxBuffers && !cfg->auxBuffers && matches[matchtry].require_aux)
|
---|
1169 | continue;
|
---|
1170 |
|
---|
1171 | if(matches[matchtry].exact_color) {
|
---|
1172 | if(cfg->redSize != redBits)
|
---|
1173 | continue;
|
---|
1174 | if(cfg->greenSize != greenBits)
|
---|
1175 | continue;
|
---|
1176 | if(cfg->blueSize != blueBits)
|
---|
1177 | continue;
|
---|
1178 | } else {
|
---|
1179 | if(cfg->redSize < redBits)
|
---|
1180 | continue;
|
---|
1181 | if(cfg->greenSize < greenBits)
|
---|
1182 | continue;
|
---|
1183 | if(cfg->blueSize < blueBits)
|
---|
1184 | continue;
|
---|
1185 | }
|
---|
1186 | if(matches[matchtry].exact_alpha) {
|
---|
1187 | if(cfg->alphaSize != alphaBits)
|
---|
1188 | continue;
|
---|
1189 | } else {
|
---|
1190 | if(cfg->alphaSize < alphaBits)
|
---|
1191 | continue;
|
---|
1192 | }
|
---|
1193 |
|
---|
1194 | /* We try to locate a format which matches our requirements exactly. In case of
|
---|
1195 | * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
|
---|
1196 | if(cfg->depthSize < depthBits)
|
---|
1197 | continue;
|
---|
1198 | else if(cfg->depthSize > depthBits)
|
---|
1199 | exactDepthMatch = FALSE;
|
---|
1200 |
|
---|
1201 | /* In all cases make sure the number of stencil bits matches our requirements
|
---|
1202 | * even when we don't need stencil because it could affect performance EXCEPT
|
---|
1203 | * on cards which don't offer depth formats without stencil like the i915 drivers
|
---|
1204 | * on Linux. */
|
---|
1205 | if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
|
---|
1206 | continue;
|
---|
1207 |
|
---|
1208 | /* Check multisampling support */
|
---|
1209 | if(cfg->numSamples != numSamples)
|
---|
1210 | continue;
|
---|
1211 |
|
---|
1212 | /* When we have passed all the checks then we have found a format which matches our
|
---|
1213 | * requirements. Note that we only check for a limit number of capabilities right now,
|
---|
1214 | * so there can easily be a dozen of pixel formats which appear to be the 'same' but
|
---|
1215 | * can still differ in things like multisampling, stereo, SRGB and other flags.
|
---|
1216 | */
|
---|
1217 |
|
---|
1218 | /* Exit the loop as we have found a format :) */
|
---|
1219 | if(exactDepthMatch) {
|
---|
1220 | iPixelFormat = cfg->iPixelFormat;
|
---|
1221 | break;
|
---|
1222 | } else if(!iPixelFormat) {
|
---|
1223 | /* In the end we might end up with a format which doesn't exactly match our depth
|
---|
1224 | * requirements. Accept the first format we found because formats with higher iPixelFormat
|
---|
1225 | * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
|
---|
1226 | iPixelFormat = cfg->iPixelFormat;
|
---|
1227 | }
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
|
---|
1232 | if(!iPixelFormat && !findCompatible) {
|
---|
1233 | ERR("Can't find a suitable iPixelFormat\n");
|
---|
1234 | return FALSE;
|
---|
1235 | } else if(!iPixelFormat) {
|
---|
1236 | PIXELFORMATDESCRIPTOR pfd;
|
---|
1237 |
|
---|
1238 | TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
|
---|
1239 | /* PixelFormat selection */
|
---|
1240 | ZeroMemory(&pfd, sizeof(pfd));
|
---|
1241 | pfd.nSize = sizeof(pfd);
|
---|
1242 | pfd.nVersion = 1;
|
---|
1243 | pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
|
---|
1244 | pfd.iPixelType = PFD_TYPE_RGBA;
|
---|
1245 | pfd.cAlphaBits = alphaBits;
|
---|
1246 | pfd.cColorBits = colorBits;
|
---|
1247 | pfd.cDepthBits = depthBits;
|
---|
1248 | pfd.cStencilBits = stencilBits;
|
---|
1249 | pfd.iLayerType = PFD_MAIN_PLANE;
|
---|
1250 |
|
---|
1251 | iPixelFormat = ChoosePixelFormat(hdc, &pfd);
|
---|
1252 | if(!iPixelFormat) {
|
---|
1253 | /* If this happens something is very wrong as ChoosePixelFormat barely fails */
|
---|
1254 | ERR("Can't find a suitable iPixelFormat\n");
|
---|
1255 | return FALSE;
|
---|
1256 | }
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 | TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
|
---|
1260 | iPixelFormat, debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format));
|
---|
1261 | return iPixelFormat;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 |
|
---|
1265 |
|
---|
1266 |
|
---|
1267 | /*****************************************************************************
|
---|
1268 | * context_create
|
---|
1269 | *
|
---|
1270 | * Creates a new context.
|
---|
1271 | *
|
---|
1272 | * * Params:
|
---|
1273 | * This: Device to activate the context for
|
---|
1274 | * target: Surface this context will render to
|
---|
1275 | * win_handle: handle to the window which we are drawing to
|
---|
1276 | * pPresentParameters: contains the pixelformats to use for onscreen rendering
|
---|
1277 | *
|
---|
1278 | *****************************************************************************/
|
---|
1279 | struct wined3d_context *context_create(IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
|
---|
1280 | const struct wined3d_format_desc *ds_format_desc)
|
---|
1281 | {
|
---|
1282 | IWineD3DDeviceImpl *device = swapchain->device;
|
---|
1283 | const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
|
---|
1284 | const struct wined3d_format_desc *color_format_desc;
|
---|
1285 | struct wined3d_context *ret;
|
---|
1286 | PIXELFORMATDESCRIPTOR pfd;
|
---|
1287 | BOOL auxBuffers = FALSE;
|
---|
1288 | int numSamples = 0;
|
---|
1289 | int pixel_format;
|
---|
1290 | unsigned int s;
|
---|
1291 | DWORD state;
|
---|
1292 | HGLRC ctx;
|
---|
1293 | HDC hdc;
|
---|
1294 |
|
---|
1295 | TRACE("swapchain %p, target %p, window %p.\n", swapchain, target, swapchain->win_handle);
|
---|
1296 |
|
---|
1297 | ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ret));
|
---|
1298 | if (!ret)
|
---|
1299 | {
|
---|
1300 | ERR("Failed to allocate context memory.\n");
|
---|
1301 | return NULL;
|
---|
1302 | }
|
---|
1303 |
|
---|
1304 | if (!(hdc = GetDC(swapchain->win_handle)))
|
---|
1305 | {
|
---|
1306 | ERR("Failed to retrieve a device context.\n");
|
---|
1307 | goto out;
|
---|
1308 | }
|
---|
1309 |
|
---|
1310 | color_format_desc = target->resource.format_desc;
|
---|
1311 |
|
---|
1312 | /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
|
---|
1313 | * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
|
---|
1314 | if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
|
---|
1315 | {
|
---|
1316 | auxBuffers = TRUE;
|
---|
1317 |
|
---|
1318 | if (color_format_desc->format == WINED3DFMT_B4G4R4X4_UNORM)
|
---|
1319 | color_format_desc = getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM, gl_info);
|
---|
1320 | else if (color_format_desc->format == WINED3DFMT_B8G8R8X8_UNORM)
|
---|
1321 | color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | /* DirectDraw supports 8bit paletted render targets and these are used by
|
---|
1325 | * old games like Starcraft and C&C. Most modern hardware doesn't support
|
---|
1326 | * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
|
---|
1327 | * conversion (ab)uses the alpha component for storing the palette index.
|
---|
1328 | * For this reason we require a format with 8bit alpha, so request
|
---|
1329 | * A8R8G8B8. */
|
---|
1330 | if (color_format_desc->format == WINED3DFMT_P8_UINT)
|
---|
1331 | color_format_desc = getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM, gl_info);
|
---|
1332 |
|
---|
1333 | /* Retrieve the depth stencil format from the present parameters.
|
---|
1334 | * The choice of the proper format can give a nice performance boost
|
---|
1335 | * in case of GPU limited programs. */
|
---|
1336 | if (swapchain->presentParms.EnableAutoDepthStencil)
|
---|
1337 | {
|
---|
1338 | TRACE("Auto depth stencil enabled, using format %s.\n",
|
---|
1339 | debug_d3dformat(swapchain->presentParms.AutoDepthStencilFormat));
|
---|
1340 | ds_format_desc = getFormatDescEntry(swapchain->presentParms.AutoDepthStencilFormat, gl_info);
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
|
---|
1344 | if (swapchain->presentParms.MultiSampleType && (swapchain->presentParms.SwapEffect == WINED3DSWAPEFFECT_DISCARD))
|
---|
1345 | {
|
---|
1346 | if (!gl_info->supported[ARB_MULTISAMPLE])
|
---|
1347 | WARN("The application is requesting multisampling without support.\n");
|
---|
1348 | else
|
---|
1349 | {
|
---|
1350 | TRACE("Requesting multisample type %#x.\n", swapchain->presentParms.MultiSampleType);
|
---|
1351 | numSamples = swapchain->presentParms.MultiSampleType;
|
---|
1352 | }
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | /* Try to find a pixel format which matches our requirements. */
|
---|
1356 | pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
|
---|
1357 | auxBuffers, numSamples, FALSE /* findCompatible */);
|
---|
1358 |
|
---|
1359 | /* Try to locate a compatible format if we weren't able to find anything. */
|
---|
1360 | if (!pixel_format)
|
---|
1361 | {
|
---|
1362 | TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
|
---|
1363 | pixel_format = WineD3D_ChoosePixelFormat(device, hdc, color_format_desc, ds_format_desc,
|
---|
1364 | auxBuffers, 0 /* numSamples */, TRUE /* findCompatible */);
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
|
---|
1368 | if (!pixel_format)
|
---|
1369 | {
|
---|
1370 | ERR("Can't find a suitable pixel format.\n");
|
---|
1371 | goto out;
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd);
|
---|
1375 | if (!context_set_pixel_format(gl_info, hdc, pixel_format))
|
---|
1376 | {
|
---|
1377 | ERR("Failed to set pixel format %d on device context %p.\n", pixel_format, hdc);
|
---|
1378 | goto out;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | ctx = pwglCreateContext(hdc);
|
---|
1382 | if (device->numContexts)
|
---|
1383 | {
|
---|
1384 | if (!pwglShareLists(device->contexts[0]->glCtx, ctx))
|
---|
1385 | {
|
---|
1386 | DWORD err = GetLastError();
|
---|
1387 | ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
|
---|
1388 | device->contexts[0]->glCtx, ctx, err);
|
---|
1389 | }
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | if(!ctx) {
|
---|
1393 | ERR("Failed to create a WGL context\n");
|
---|
1394 | goto out;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 | if (!device_context_add(device, ret))
|
---|
1398 | {
|
---|
1399 | ERR("Failed to add the newly created context to the context list\n");
|
---|
1400 | if (!pwglDeleteContext(ctx))
|
---|
1401 | {
|
---|
1402 | DWORD err = GetLastError();
|
---|
1403 | ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx, err);
|
---|
1404 | }
|
---|
1405 | goto out;
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | ret->gl_info = gl_info;
|
---|
1409 |
|
---|
1410 | /* Mark all states dirty to force a proper initialization of the states
|
---|
1411 | * on the first use of the context. */
|
---|
1412 | for (state = 0; state <= STATE_HIGHEST; ++state)
|
---|
1413 | {
|
---|
1414 | if (device->StateTable[state].representative)
|
---|
1415 | Context_MarkStateDirty(ret, state, device->StateTable);
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | #ifdef VBOXWDDM
|
---|
1419 | ret->device = device;
|
---|
1420 | ret->currentSwapchain = swapchain;
|
---|
1421 | #else
|
---|
1422 | ret->swapchain = swapchain;
|
---|
1423 | #endif
|
---|
1424 | ret->current_rt = (IWineD3DSurface *)target;
|
---|
1425 | ret->tid = GetCurrentThreadId();
|
---|
1426 |
|
---|
1427 | ret->render_offscreen = surface_is_offscreen((IWineD3DSurface *) target);
|
---|
1428 | ret->draw_buffer_dirty = TRUE;
|
---|
1429 | ret->valid = 1;
|
---|
1430 |
|
---|
1431 | ret->glCtx = ctx;
|
---|
1432 | ret->win_handle = swapchain->win_handle;
|
---|
1433 | ret->hdc = hdc;
|
---|
1434 | ret->pixel_format = pixel_format;
|
---|
1435 |
|
---|
1436 | if (device->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *)device))
|
---|
1437 | {
|
---|
1438 | /* Create the dirty constants array and initialize them to dirty */
|
---|
1439 | ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
|
---|
1440 | sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
1441 | ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
|
---|
1442 | sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
1443 | memset(ret->vshader_const_dirty, 1,
|
---|
1444 | sizeof(*ret->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
1445 | memset(ret->pshader_const_dirty, 1,
|
---|
1446 | sizeof(*ret->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | ret->free_occlusion_query_size = 4;
|
---|
1450 | ret->free_occlusion_queries = HeapAlloc(GetProcessHeap(), 0,
|
---|
1451 | ret->free_occlusion_query_size * sizeof(*ret->free_occlusion_queries));
|
---|
1452 | if (!ret->free_occlusion_queries) goto out;
|
---|
1453 |
|
---|
1454 | list_init(&ret->occlusion_queries);
|
---|
1455 |
|
---|
1456 | ret->free_event_query_size = 4;
|
---|
1457 | ret->free_event_queries = HeapAlloc(GetProcessHeap(), 0,
|
---|
1458 | ret->free_event_query_size * sizeof(*ret->free_event_queries));
|
---|
1459 | if (!ret->free_event_queries) goto out;
|
---|
1460 |
|
---|
1461 | list_init(&ret->event_queries);
|
---|
1462 |
|
---|
1463 | TRACE("Successfully created new context %p\n", ret);
|
---|
1464 |
|
---|
1465 | list_init(&ret->fbo_list);
|
---|
1466 | list_init(&ret->fbo_destroy_list);
|
---|
1467 |
|
---|
1468 | context_enter(ret);
|
---|
1469 |
|
---|
1470 | /* Set up the context defaults */
|
---|
1471 | if (!context_set_current(ret))
|
---|
1472 | {
|
---|
1473 | ERR("Cannot activate context to set up defaults\n");
|
---|
1474 | context_release(ret);
|
---|
1475 | goto out;
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | ENTER_GL();
|
---|
1479 |
|
---|
1480 | glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
|
---|
1481 |
|
---|
1482 | TRACE("Setting up the screen\n");
|
---|
1483 | /* Clear the screen */
|
---|
1484 | glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
|
---|
1485 | checkGLcall("glClearColor");
|
---|
1486 | glClearIndex(0);
|
---|
1487 | glClearDepth(1);
|
---|
1488 | glClearStencil(0xffff);
|
---|
1489 |
|
---|
1490 | checkGLcall("glClear");
|
---|
1491 |
|
---|
1492 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
1493 | checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
|
---|
1494 |
|
---|
1495 | glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
|
---|
1496 | checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
|
---|
1497 |
|
---|
1498 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
|
---|
1499 | checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
|
---|
1500 |
|
---|
1501 | glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);
|
---|
1502 | checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
|
---|
1503 | glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);
|
---|
1504 | checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
|
---|
1505 |
|
---|
1506 | if (gl_info->supported[APPLE_CLIENT_STORAGE])
|
---|
1507 | {
|
---|
1508 | /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
|
---|
1509 | * and textures in DIB sections(due to the memory protection).
|
---|
1510 | */
|
---|
1511 | glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
|
---|
1512 | checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
|
---|
1513 | }
|
---|
1514 | if (gl_info->supported[ARB_VERTEX_BLEND])
|
---|
1515 | {
|
---|
1516 | /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
|
---|
1517 | * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
|
---|
1518 | * GL_VERTEX_BLEND_ARB isn't enabled too
|
---|
1519 | */
|
---|
1520 | glEnable(GL_WEIGHT_SUM_UNITY_ARB);
|
---|
1521 | checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
|
---|
1522 | }
|
---|
1523 | if (gl_info->supported[NV_TEXTURE_SHADER2])
|
---|
1524 | {
|
---|
1525 | /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
|
---|
1526 | * the previous texture where to source the offset from is always unit - 1.
|
---|
1527 | */
|
---|
1528 | for (s = 1; s < gl_info->limits.textures; ++s)
|
---|
1529 | {
|
---|
1530 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
|
---|
1531 | glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
|
---|
1532 | checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
|
---|
1533 | }
|
---|
1534 | }
|
---|
1535 | if (gl_info->supported[ARB_FRAGMENT_PROGRAM])
|
---|
1536 | {
|
---|
1537 | /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
|
---|
1538 | * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
|
---|
1539 | * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
|
---|
1540 | * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
|
---|
1541 | * is ever assigned.
|
---|
1542 | *
|
---|
1543 | * So make sure a program is assigned to each context. The first real ARBFP use will set a different
|
---|
1544 | * program and the dummy program is destroyed when the context is destroyed.
|
---|
1545 | */
|
---|
1546 | const char *dummy_program =
|
---|
1547 | "!!ARBfp1.0\n"
|
---|
1548 | "MOV result.color, fragment.color.primary;\n"
|
---|
1549 | "END\n";
|
---|
1550 | GL_EXTCALL(glGenProgramsARB(1, &ret->dummy_arbfp_prog));
|
---|
1551 | GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, ret->dummy_arbfp_prog));
|
---|
1552 | GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(dummy_program), dummy_program));
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | for (s = 0; s < gl_info->limits.point_sprite_units; ++s)
|
---|
1556 | {
|
---|
1557 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
|
---|
1558 | glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
|
---|
1559 | checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | if (gl_info->supported[ARB_PROVOKING_VERTEX])
|
---|
1563 | {
|
---|
1564 | GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION));
|
---|
1565 | }
|
---|
1566 | else if (gl_info->supported[EXT_PROVOKING_VERTEX])
|
---|
1567 | {
|
---|
1568 | GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT));
|
---|
1569 | }
|
---|
1570 |
|
---|
1571 | #ifdef VBOXWDDM
|
---|
1572 | GL_EXTCALL(glChromiumParameteriCR(GL_SHARE_CONTEXT_RESOURCES_CR, GL_TRUE));
|
---|
1573 | #endif
|
---|
1574 |
|
---|
1575 | LEAVE_GL();
|
---|
1576 |
|
---|
1577 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
1578 |
|
---|
1579 | TRACE("Created context %p.\n", ret);
|
---|
1580 |
|
---|
1581 | return ret;
|
---|
1582 |
|
---|
1583 | out:
|
---|
1584 | HeapFree(GetProcessHeap(), 0, ret->free_event_queries);
|
---|
1585 | HeapFree(GetProcessHeap(), 0, ret->free_occlusion_queries);
|
---|
1586 | HeapFree(GetProcessHeap(), 0, ret->pshader_const_dirty);
|
---|
1587 | HeapFree(GetProcessHeap(), 0, ret->vshader_const_dirty);
|
---|
1588 | HeapFree(GetProcessHeap(), 0, ret);
|
---|
1589 | return NULL;
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | #ifdef VBOXWDDM
|
---|
1593 | static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target);
|
---|
1594 |
|
---|
1595 | struct wined3d_context *context_find_create(IWineD3DDeviceImpl *device, IWineD3DSwapChainImpl *swapchain, IWineD3DSurfaceImpl *target,
|
---|
1596 | const struct wined3d_format_desc *ds_format_desc)
|
---|
1597 | {
|
---|
1598 | int i;
|
---|
1599 | DWORD tid = GetCurrentThreadId();
|
---|
1600 | struct wined3d_context *context = NULL;
|
---|
1601 |
|
---|
1602 | Assert(0);
|
---|
1603 |
|
---|
1604 | for(i = 0 ; i < device->numContexts ; i ++)
|
---|
1605 | {
|
---|
1606 | if(device->contexts[i]->tid == tid) {
|
---|
1607 | context = device->contexts[i];
|
---|
1608 | break;
|
---|
1609 | }
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | if (!context)
|
---|
1613 | {
|
---|
1614 | Assert(!device->NumberOfSwapChains);
|
---|
1615 | context = context_create(swapchain, target, ds_format_desc);
|
---|
1616 | }
|
---|
1617 | else
|
---|
1618 | {
|
---|
1619 | context_validate(context, swapchain);
|
---|
1620 | context_setup_target(device, context, target);
|
---|
1621 | context_enter(context);
|
---|
1622 | Assert(context->valid);
|
---|
1623 | }
|
---|
1624 |
|
---|
1625 | return context;
|
---|
1626 | }
|
---|
1627 | #endif
|
---|
1628 |
|
---|
1629 | /*****************************************************************************
|
---|
1630 | * context_destroy
|
---|
1631 | *
|
---|
1632 | * Destroys a wined3d context
|
---|
1633 | *
|
---|
1634 | * Params:
|
---|
1635 | * This: Device to activate the context for
|
---|
1636 | * context: Context to destroy
|
---|
1637 | *
|
---|
1638 | *****************************************************************************/
|
---|
1639 | void context_destroy(IWineD3DDeviceImpl *This, struct wined3d_context *context)
|
---|
1640 | {
|
---|
1641 | BOOL destroy;
|
---|
1642 |
|
---|
1643 | TRACE("Destroying ctx %p\n", context);
|
---|
1644 |
|
---|
1645 | if (context->tid == GetCurrentThreadId() || !context->current)
|
---|
1646 | {
|
---|
1647 | context_destroy_gl_resources(context);
|
---|
1648 | TlsSetValue(wined3d_context_tls_idx, NULL);
|
---|
1649 | destroy = TRUE;
|
---|
1650 | }
|
---|
1651 | else
|
---|
1652 | {
|
---|
1653 | context->destroyed = 1;
|
---|
1654 | destroy = FALSE;
|
---|
1655 | }
|
---|
1656 |
|
---|
1657 | HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
|
---|
1658 | HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
|
---|
1659 | device_context_remove(This, context);
|
---|
1660 | if (destroy) HeapFree(GetProcessHeap(), 0, context);
|
---|
1661 | }
|
---|
1662 |
|
---|
1663 | /* GL locking is done by the caller */
|
---|
1664 | static inline void set_blit_dimension(UINT width, UINT height) {
|
---|
1665 | glMatrixMode(GL_PROJECTION);
|
---|
1666 | checkGLcall("glMatrixMode(GL_PROJECTION)");
|
---|
1667 | glLoadIdentity();
|
---|
1668 | checkGLcall("glLoadIdentity()");
|
---|
1669 | glOrtho(0, width, height, 0, 0.0, -1.0);
|
---|
1670 | checkGLcall("glOrtho");
|
---|
1671 | glViewport(0, 0, width, height);
|
---|
1672 | checkGLcall("glViewport");
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | /*****************************************************************************
|
---|
1676 | * SetupForBlit
|
---|
1677 | *
|
---|
1678 | * Sets up a context for DirectDraw blitting.
|
---|
1679 | * All texture units are disabled, texture unit 0 is set as current unit
|
---|
1680 | * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
|
---|
1681 | * color writing enabled for all channels
|
---|
1682 | * register combiners disabled, shaders disabled
|
---|
1683 | * world matrix is set to identity, texture matrix 0 too
|
---|
1684 | * projection matrix is setup for drawing screen coordinates
|
---|
1685 | *
|
---|
1686 | * Params:
|
---|
1687 | * This: Device to activate the context for
|
---|
1688 | * context: Context to setup
|
---|
1689 | *
|
---|
1690 | *****************************************************************************/
|
---|
1691 | /* Context activation is done by the caller. */
|
---|
1692 | static void SetupForBlit(IWineD3DDeviceImpl *This, struct wined3d_context *context)
|
---|
1693 | {
|
---|
1694 | int i;
|
---|
1695 | const struct StateEntry *StateTable = This->StateTable;
|
---|
1696 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
1697 | UINT width = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Width;
|
---|
1698 | UINT height = ((IWineD3DSurfaceImpl *)context->current_rt)->currentDesc.Height;
|
---|
1699 | DWORD sampler;
|
---|
1700 |
|
---|
1701 | TRACE("Setting up context %p for blitting\n", context);
|
---|
1702 | if(context->last_was_blit) {
|
---|
1703 | if(context->blit_w != width || context->blit_h != height) {
|
---|
1704 | ENTER_GL();
|
---|
1705 | set_blit_dimension(width, height);
|
---|
1706 | LEAVE_GL();
|
---|
1707 | context->blit_w = width; context->blit_h = height;
|
---|
1708 | /* No need to dirtify here, the states are still dirtified because they weren't
|
---|
1709 | * applied since the last SetupForBlit call. Otherwise last_was_blit would not
|
---|
1710 | * be set
|
---|
1711 | */
|
---|
1712 | }
|
---|
1713 | TRACE("Context is already set up for blitting, nothing to do\n");
|
---|
1714 | return;
|
---|
1715 | }
|
---|
1716 | context->last_was_blit = TRUE;
|
---|
1717 |
|
---|
1718 | /* TODO: Use a display list */
|
---|
1719 |
|
---|
1720 | /* Disable shaders */
|
---|
1721 | ENTER_GL();
|
---|
1722 | This->shader_backend->shader_select(context, FALSE, FALSE);
|
---|
1723 | LEAVE_GL();
|
---|
1724 |
|
---|
1725 | Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
|
---|
1726 | Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
|
---|
1727 |
|
---|
1728 | /* Call ENTER_GL() once for all gl calls below. In theory we should not call
|
---|
1729 | * helper functions in between gl calls. This function is full of Context_MarkStateDirty
|
---|
1730 | * which can safely be called from here, we only lock once instead locking/unlocking
|
---|
1731 | * after each GL call.
|
---|
1732 | */
|
---|
1733 | ENTER_GL();
|
---|
1734 |
|
---|
1735 | /* Disable all textures. The caller can then bind a texture it wants to blit
|
---|
1736 | * from
|
---|
1737 | *
|
---|
1738 | * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
|
---|
1739 | * function texture unit. No need to care for higher samplers
|
---|
1740 | */
|
---|
1741 | for (i = gl_info->limits.textures - 1; i > 0 ; --i)
|
---|
1742 | {
|
---|
1743 | sampler = This->rev_tex_unit_map[i];
|
---|
1744 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
|
---|
1745 | checkGLcall("glActiveTextureARB");
|
---|
1746 |
|
---|
1747 | if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
|
---|
1748 | {
|
---|
1749 | glDisable(GL_TEXTURE_CUBE_MAP_ARB);
|
---|
1750 | checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
|
---|
1751 | }
|
---|
1752 | glDisable(GL_TEXTURE_3D);
|
---|
1753 | checkGLcall("glDisable GL_TEXTURE_3D");
|
---|
1754 | if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
|
---|
1755 | {
|
---|
1756 | glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
---|
1757 | checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
|
---|
1758 | }
|
---|
1759 | glDisable(GL_TEXTURE_2D);
|
---|
1760 | checkGLcall("glDisable GL_TEXTURE_2D");
|
---|
1761 |
|
---|
1762 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
1763 | checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
|
---|
1764 |
|
---|
1765 | if (sampler != WINED3D_UNMAPPED_STAGE)
|
---|
1766 | {
|
---|
1767 | if (sampler < MAX_TEXTURES) {
|
---|
1768 | Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
|
---|
1769 | }
|
---|
1770 | Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
|
---|
1771 | }
|
---|
1772 | }
|
---|
1773 | GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
|
---|
1774 | checkGLcall("glActiveTextureARB");
|
---|
1775 |
|
---|
1776 | sampler = This->rev_tex_unit_map[0];
|
---|
1777 |
|
---|
1778 | if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
|
---|
1779 | {
|
---|
1780 | glDisable(GL_TEXTURE_CUBE_MAP_ARB);
|
---|
1781 | checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
|
---|
1782 | }
|
---|
1783 | glDisable(GL_TEXTURE_3D);
|
---|
1784 | checkGLcall("glDisable GL_TEXTURE_3D");
|
---|
1785 | if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
|
---|
1786 | {
|
---|
1787 | glDisable(GL_TEXTURE_RECTANGLE_ARB);
|
---|
1788 | checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
|
---|
1789 | }
|
---|
1790 | glDisable(GL_TEXTURE_2D);
|
---|
1791 | checkGLcall("glDisable GL_TEXTURE_2D");
|
---|
1792 |
|
---|
1793 | glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
|
---|
1794 |
|
---|
1795 | glMatrixMode(GL_TEXTURE);
|
---|
1796 | checkGLcall("glMatrixMode(GL_TEXTURE)");
|
---|
1797 | glLoadIdentity();
|
---|
1798 | checkGLcall("glLoadIdentity()");
|
---|
1799 |
|
---|
1800 | if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
|
---|
1801 | {
|
---|
1802 | glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
|
---|
1803 | GL_TEXTURE_LOD_BIAS_EXT,
|
---|
1804 | 0.0f);
|
---|
1805 | checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
|
---|
1806 | }
|
---|
1807 |
|
---|
1808 | if (sampler != WINED3D_UNMAPPED_STAGE)
|
---|
1809 | {
|
---|
1810 | if (sampler < MAX_TEXTURES) {
|
---|
1811 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
|
---|
1812 | Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
|
---|
1813 | }
|
---|
1814 | Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
|
---|
1815 | }
|
---|
1816 |
|
---|
1817 | /* Other misc states */
|
---|
1818 | glDisable(GL_ALPHA_TEST);
|
---|
1819 | checkGLcall("glDisable(GL_ALPHA_TEST)");
|
---|
1820 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
|
---|
1821 | glDisable(GL_LIGHTING);
|
---|
1822 | checkGLcall("glDisable GL_LIGHTING");
|
---|
1823 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
|
---|
1824 | glDisable(GL_DEPTH_TEST);
|
---|
1825 | checkGLcall("glDisable GL_DEPTH_TEST");
|
---|
1826 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
|
---|
1827 | glDisableWINE(GL_FOG);
|
---|
1828 | checkGLcall("glDisable GL_FOG");
|
---|
1829 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
|
---|
1830 | glDisable(GL_BLEND);
|
---|
1831 | checkGLcall("glDisable GL_BLEND");
|
---|
1832 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
1833 | glDisable(GL_CULL_FACE);
|
---|
1834 | checkGLcall("glDisable GL_CULL_FACE");
|
---|
1835 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
|
---|
1836 | glDisable(GL_STENCIL_TEST);
|
---|
1837 | checkGLcall("glDisable GL_STENCIL_TEST");
|
---|
1838 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
|
---|
1839 | glDisable(GL_SCISSOR_TEST);
|
---|
1840 | checkGLcall("glDisable GL_SCISSOR_TEST");
|
---|
1841 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
|
---|
1842 | if (gl_info->supported[ARB_POINT_SPRITE])
|
---|
1843 | {
|
---|
1844 | glDisable(GL_POINT_SPRITE_ARB);
|
---|
1845 | checkGLcall("glDisable GL_POINT_SPRITE_ARB");
|
---|
1846 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
|
---|
1847 | }
|
---|
1848 | glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
|
---|
1849 | checkGLcall("glColorMask");
|
---|
1850 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE), StateTable);
|
---|
1851 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1), StateTable);
|
---|
1852 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2), StateTable);
|
---|
1853 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3), StateTable);
|
---|
1854 | if (gl_info->supported[EXT_SECONDARY_COLOR])
|
---|
1855 | {
|
---|
1856 | glDisable(GL_COLOR_SUM_EXT);
|
---|
1857 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
|
---|
1858 | checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
|
---|
1859 | }
|
---|
1860 |
|
---|
1861 | /* Setup transforms */
|
---|
1862 | glMatrixMode(GL_MODELVIEW);
|
---|
1863 | checkGLcall("glMatrixMode(GL_MODELVIEW)");
|
---|
1864 | glLoadIdentity();
|
---|
1865 | checkGLcall("glLoadIdentity()");
|
---|
1866 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
|
---|
1867 |
|
---|
1868 | context->last_was_rhw = TRUE;
|
---|
1869 | Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
|
---|
1870 |
|
---|
1871 | glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
|
---|
1872 | glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
|
---|
1873 | glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
|
---|
1874 | glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
|
---|
1875 | glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
|
---|
1876 | glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
|
---|
1877 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
|
---|
1878 |
|
---|
1879 | set_blit_dimension(width, height);
|
---|
1880 |
|
---|
1881 | LEAVE_GL();
|
---|
1882 |
|
---|
1883 | context->blit_w = width; context->blit_h = height;
|
---|
1884 | Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
|
---|
1885 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
|
---|
1886 |
|
---|
1887 |
|
---|
1888 | This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
|
---|
1889 | }
|
---|
1890 |
|
---|
1891 | /*****************************************************************************
|
---|
1892 | * findThreadContextForSwapChain
|
---|
1893 | *
|
---|
1894 | * Searches a swapchain for all contexts and picks one for the thread tid.
|
---|
1895 | * If none can be found the swapchain is requested to create a new context
|
---|
1896 | *
|
---|
1897 | *****************************************************************************/
|
---|
1898 | static struct wined3d_context *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid)
|
---|
1899 | {
|
---|
1900 | unsigned int i;
|
---|
1901 | #ifdef VBOXWDDM
|
---|
1902 | IWineD3DDeviceImpl *device = ((IWineD3DSwapChainImpl*)swapchain)->device;
|
---|
1903 | for (i = 0; i < device->numContexts; ++i)
|
---|
1904 | {
|
---|
1905 | if (device->contexts[i]->tid == tid)
|
---|
1906 | return device->contexts[i];
|
---|
1907 | }
|
---|
1908 | #else
|
---|
1909 | for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
|
---|
1910 | if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
|
---|
1911 | return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
|
---|
1912 | }
|
---|
1913 | }
|
---|
1914 | #endif
|
---|
1915 |
|
---|
1916 | /* Create a new context for the thread */
|
---|
1917 | return swapchain_create_context_for_thread(swapchain);
|
---|
1918 | }
|
---|
1919 |
|
---|
1920 | /*****************************************************************************
|
---|
1921 | * FindContext
|
---|
1922 | *
|
---|
1923 | * Finds a context for the current render target and thread
|
---|
1924 | *
|
---|
1925 | * Parameters:
|
---|
1926 | * target: Render target to find the context for
|
---|
1927 | * tid: Thread to activate the context for
|
---|
1928 | *
|
---|
1929 | * Returns: The needed context
|
---|
1930 | *
|
---|
1931 | *****************************************************************************/
|
---|
1932 | static struct wined3d_context *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target)
|
---|
1933 | {
|
---|
1934 | IWineD3DSwapChain *swapchain = NULL;
|
---|
1935 | struct wined3d_context *current_context = context_get_current();
|
---|
1936 | DWORD tid = GetCurrentThreadId();
|
---|
1937 | struct wined3d_context *context;
|
---|
1938 |
|
---|
1939 | if (current_context && current_context->destroyed) current_context = NULL;
|
---|
1940 |
|
---|
1941 | if (!target)
|
---|
1942 | {
|
---|
1943 | if (current_context
|
---|
1944 | && current_context->current_rt
|
---|
1945 | #ifdef VBOXWDDM
|
---|
1946 | && current_context->device == This
|
---|
1947 | #else
|
---|
1948 | && current_context->swapchain->device == This
|
---|
1949 | #endif
|
---|
1950 | )
|
---|
1951 | {
|
---|
1952 | target = current_context->current_rt;
|
---|
1953 | }
|
---|
1954 | else
|
---|
1955 | {
|
---|
1956 | #ifdef VBOXWDDM
|
---|
1957 | /* tmp work-around */
|
---|
1958 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1];
|
---|
1959 | #else
|
---|
1960 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *)This->swapchains[0];
|
---|
1961 | #endif
|
---|
1962 | if (swapchain->backBuffer) target = swapchain->backBuffer[0];
|
---|
1963 | else target = swapchain->frontBuffer;
|
---|
1964 | }
|
---|
1965 | }
|
---|
1966 |
|
---|
1967 | if (current_context && current_context->current_rt == target)
|
---|
1968 | {
|
---|
1969 | #ifdef VBOXWDDM
|
---|
1970 | IWineD3DSwapChainImpl *swapchain = NULL;
|
---|
1971 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
1972 | context_validate(current_context, swapchain);
|
---|
1973 | IWineD3DSwapChain_Release(swapchain);
|
---|
1974 | }
|
---|
1975 | else {
|
---|
1976 | /* tmp work-around */
|
---|
1977 | context_validate(current_context, current_context->device->swapchains[current_context->device->NumberOfSwapChains-1]);
|
---|
1978 | }
|
---|
1979 | #else
|
---|
1980 | context_validate(current_context);
|
---|
1981 | #endif
|
---|
1982 | return current_context;
|
---|
1983 | }
|
---|
1984 |
|
---|
1985 | if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
|
---|
1986 | TRACE("Rendering onscreen\n");
|
---|
1987 |
|
---|
1988 | context = findThreadContextForSwapChain(swapchain, tid);
|
---|
1989 | #ifdef VBOXWDDM
|
---|
1990 | context_validate(context, swapchain);
|
---|
1991 | #endif
|
---|
1992 | IWineD3DSwapChain_Release(swapchain);
|
---|
1993 | }
|
---|
1994 | else
|
---|
1995 | {
|
---|
1996 | TRACE("Rendering offscreen\n");
|
---|
1997 |
|
---|
1998 | /* Stay with the currently active context. */
|
---|
1999 | if (current_context
|
---|
2000 | #ifdef VBOXWDDM
|
---|
2001 | && current_context->device == This
|
---|
2002 | #else
|
---|
2003 | && current_context->swapchain->device == This
|
---|
2004 | #endif
|
---|
2005 | )
|
---|
2006 | {
|
---|
2007 | context = current_context;
|
---|
2008 | }
|
---|
2009 | else
|
---|
2010 | {
|
---|
2011 | /* This may happen if the app jumps straight into offscreen rendering
|
---|
2012 | * Start using the context of the primary swapchain. tid == 0 is no problem
|
---|
2013 | * for findThreadContextForSwapChain.
|
---|
2014 | *
|
---|
2015 | * Can also happen on thread switches - in that case findThreadContextForSwapChain
|
---|
2016 | * is perfect to call. */
|
---|
2017 | #ifdef VBOXWDDM /* tmp work-around */
|
---|
2018 | context = findThreadContextForSwapChain(This->swapchains[This->NumberOfSwapChains-1], tid);
|
---|
2019 | #else
|
---|
2020 | context = findThreadContextForSwapChain(This->swapchains[0], tid);
|
---|
2021 | #endif
|
---|
2022 | }
|
---|
2023 | #ifdef VBOXWDDM
|
---|
2024 | context_validate(context,
|
---|
2025 | This->swapchains[This->NumberOfSwapChains-1] /* tmp work-around */
|
---|
2026 | );
|
---|
2027 | #endif
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | #ifndef VBOXWDDM
|
---|
2031 | context_validate(context);
|
---|
2032 | #endif
|
---|
2033 |
|
---|
2034 | return context;
|
---|
2035 | }
|
---|
2036 |
|
---|
2037 | /* Context activation is done by the caller. */
|
---|
2038 | static void context_apply_draw_buffer(struct wined3d_context *context, BOOL blit)
|
---|
2039 | {
|
---|
2040 | const struct wined3d_gl_info *gl_info = context->gl_info;
|
---|
2041 | IWineD3DSurface *rt = context->current_rt;
|
---|
2042 | IWineD3DDeviceImpl *device;
|
---|
2043 |
|
---|
2044 | device = ((IWineD3DSurfaceImpl *)rt)->resource.device;
|
---|
2045 | if (!surface_is_offscreen(rt))
|
---|
2046 | {
|
---|
2047 | ENTER_GL();
|
---|
2048 | glDrawBuffer(surface_get_gl_buffer(rt));
|
---|
2049 | checkGLcall("glDrawBuffers()");
|
---|
2050 | LEAVE_GL();
|
---|
2051 | }
|
---|
2052 | else
|
---|
2053 | {
|
---|
2054 | ENTER_GL();
|
---|
2055 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
|
---|
2056 | {
|
---|
2057 | if (!blit)
|
---|
2058 | {
|
---|
2059 | if (gl_info->supported[ARB_DRAW_BUFFERS])
|
---|
2060 | {
|
---|
2061 | GL_EXTCALL(glDrawBuffersARB(gl_info->limits.buffers, device->draw_buffers));
|
---|
2062 | checkGLcall("glDrawBuffers()");
|
---|
2063 | }
|
---|
2064 | else
|
---|
2065 | {
|
---|
2066 | glDrawBuffer(device->draw_buffers[0]);
|
---|
2067 | checkGLcall("glDrawBuffer()");
|
---|
2068 | }
|
---|
2069 | } else {
|
---|
2070 | glDrawBuffer(GL_COLOR_ATTACHMENT0);
|
---|
2071 | checkGLcall("glDrawBuffer()");
|
---|
2072 | }
|
---|
2073 | }
|
---|
2074 | else
|
---|
2075 | {
|
---|
2076 | glDrawBuffer(device->offscreenBuffer);
|
---|
2077 | checkGLcall("glDrawBuffer()");
|
---|
2078 | }
|
---|
2079 | LEAVE_GL();
|
---|
2080 | }
|
---|
2081 | }
|
---|
2082 |
|
---|
2083 | /* GL locking is done by the caller. */
|
---|
2084 | void context_set_draw_buffer(struct wined3d_context *context, GLenum buffer)
|
---|
2085 | {
|
---|
2086 | glDrawBuffer(buffer);
|
---|
2087 | checkGLcall("glDrawBuffer()");
|
---|
2088 | context->draw_buffer_dirty = TRUE;
|
---|
2089 | }
|
---|
2090 |
|
---|
2091 | static inline void context_set_render_offscreen(struct wined3d_context *context, const struct StateEntry *StateTable,
|
---|
2092 | BOOL offscreen)
|
---|
2093 | {
|
---|
2094 | if (context->render_offscreen == offscreen) return;
|
---|
2095 |
|
---|
2096 | Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
|
---|
2097 | Context_MarkStateDirty(context, STATE_VDECL, StateTable);
|
---|
2098 | Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
|
---|
2099 | Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
|
---|
2100 | Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
|
---|
2101 | context->render_offscreen = offscreen;
|
---|
2102 | }
|
---|
2103 |
|
---|
2104 | static BOOL match_depth_stencil_format(const struct wined3d_format_desc *existing,
|
---|
2105 | const struct wined3d_format_desc *required)
|
---|
2106 | {
|
---|
2107 | short existing_depth, existing_stencil, required_depth, required_stencil;
|
---|
2108 |
|
---|
2109 | if(existing == required) return TRUE;
|
---|
2110 | if((existing->Flags & WINED3DFMT_FLAG_FLOAT) != (required->Flags & WINED3DFMT_FLAG_FLOAT)) return FALSE;
|
---|
2111 |
|
---|
2112 | getDepthStencilBits(existing, &existing_depth, &existing_stencil);
|
---|
2113 | getDepthStencilBits(required, &required_depth, &required_stencil);
|
---|
2114 |
|
---|
2115 | if(existing_depth < required_depth) return FALSE;
|
---|
2116 | /* If stencil bits are used the exact amount is required - otherwise wrapping
|
---|
2117 | * won't work correctly */
|
---|
2118 | if(required_stencil && required_stencil != existing_stencil) return FALSE;
|
---|
2119 | return TRUE;
|
---|
2120 | }
|
---|
2121 | /* The caller provides a context */
|
---|
2122 | static void context_validate_onscreen_formats(IWineD3DDeviceImpl *device, struct wined3d_context *context)
|
---|
2123 | {
|
---|
2124 | /* Onscreen surfaces are always in a swapchain */
|
---|
2125 | IWineD3DSurfaceImpl *depth_stencil = (IWineD3DSurfaceImpl *) device->stencilBufferTarget;
|
---|
2126 | IWineD3DSwapChainImpl *swapchain = (IWineD3DSwapChainImpl *) ((IWineD3DSurfaceImpl *)context->current_rt)->container;
|
---|
2127 |
|
---|
2128 | if (!depth_stencil) return;
|
---|
2129 | if (match_depth_stencil_format(swapchain->ds_format, depth_stencil->resource.format_desc)) return;
|
---|
2130 |
|
---|
2131 | /* TODO: If the requested format would satisfy the needs of the existing one(reverse match),
|
---|
2132 | * or no onscreen depth buffer was created, the OpenGL drawable could be changed to the new
|
---|
2133 | * format. */
|
---|
2134 | WARN("Depth stencil format is not supported by WGL, rendering the backbuffer in an FBO\n");
|
---|
2135 |
|
---|
2136 | /* The currently active context is the necessary context to access the swapchain's onscreen buffers */
|
---|
2137 | IWineD3DSurface_LoadLocation(context->current_rt, SFLAG_INTEXTURE, NULL);
|
---|
2138 | swapchain->render_to_fbo = TRUE;
|
---|
2139 | context_set_render_offscreen(context, device->StateTable, TRUE);
|
---|
2140 | }
|
---|
2141 |
|
---|
2142 | /* Context activation is done by the caller. */
|
---|
2143 | static void context_apply_state(struct wined3d_context *context, IWineD3DDeviceImpl *device, enum ContextUsage usage)
|
---|
2144 | {
|
---|
2145 | const struct StateEntry *state_table = device->StateTable;
|
---|
2146 | unsigned int i;
|
---|
2147 |
|
---|
2148 | switch (usage) {
|
---|
2149 | case CTXUSAGE_CLEAR:
|
---|
2150 | case CTXUSAGE_DRAWPRIM:
|
---|
2151 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
|
---|
2152 | if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
|
---|
2153 | ENTER_GL();
|
---|
2154 | context_apply_fbo_state(context);
|
---|
2155 | LEAVE_GL();
|
---|
2156 | }
|
---|
2157 | if (context->draw_buffer_dirty) {
|
---|
2158 | context_apply_draw_buffer(context, FALSE);
|
---|
2159 | context->draw_buffer_dirty = FALSE;
|
---|
2160 | }
|
---|
2161 | break;
|
---|
2162 |
|
---|
2163 | case CTXUSAGE_BLIT:
|
---|
2164 | if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
|
---|
2165 | if (!context->render_offscreen) context_validate_onscreen_formats(device, context);
|
---|
2166 | if (context->render_offscreen)
|
---|
2167 | {
|
---|
2168 | FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
|
---|
2169 | surface_internal_preload(context->current_rt, SRGB_RGB);
|
---|
2170 |
|
---|
2171 | ENTER_GL();
|
---|
2172 | context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
|
---|
2173 | context_attach_surface_fbo(context, GL_FRAMEBUFFER, 0, (IWineD3DSurfaceImpl *)context->current_rt);
|
---|
2174 | context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, NULL, FALSE);
|
---|
2175 | LEAVE_GL();
|
---|
2176 | } else {
|
---|
2177 | ENTER_GL();
|
---|
2178 | context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
|
---|
2179 | LEAVE_GL();
|
---|
2180 | }
|
---|
2181 | context->draw_buffer_dirty = TRUE;
|
---|
2182 | }
|
---|
2183 | if (context->draw_buffer_dirty) {
|
---|
2184 | context_apply_draw_buffer(context, TRUE);
|
---|
2185 | if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
|
---|
2186 | context->draw_buffer_dirty = FALSE;
|
---|
2187 | }
|
---|
2188 | }
|
---|
2189 | break;
|
---|
2190 |
|
---|
2191 | default:
|
---|
2192 | break;
|
---|
2193 | }
|
---|
2194 |
|
---|
2195 | switch(usage) {
|
---|
2196 | case CTXUSAGE_RESOURCELOAD:
|
---|
2197 | /* This does not require any special states to be set up */
|
---|
2198 | break;
|
---|
2199 |
|
---|
2200 | case CTXUSAGE_CLEAR:
|
---|
2201 | if(context->last_was_blit) {
|
---|
2202 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
2203 | }
|
---|
2204 |
|
---|
2205 | /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
|
---|
2206 | * blending when clearing improves the clearing performance incredibly.
|
---|
2207 | */
|
---|
2208 | ENTER_GL();
|
---|
2209 | glDisable(GL_BLEND);
|
---|
2210 | LEAVE_GL();
|
---|
2211 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), state_table);
|
---|
2212 |
|
---|
2213 | ENTER_GL();
|
---|
2214 | glEnable(GL_SCISSOR_TEST);
|
---|
2215 | checkGLcall("glEnable GL_SCISSOR_TEST");
|
---|
2216 | LEAVE_GL();
|
---|
2217 | context->last_was_blit = FALSE;
|
---|
2218 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), state_table);
|
---|
2219 | Context_MarkStateDirty(context, STATE_SCISSORRECT, state_table);
|
---|
2220 | break;
|
---|
2221 |
|
---|
2222 | case CTXUSAGE_DRAWPRIM:
|
---|
2223 | /* This needs all dirty states applied */
|
---|
2224 | if(context->last_was_blit) {
|
---|
2225 | device->frag_pipe->enable_extension((IWineD3DDevice *)device, TRUE);
|
---|
2226 | }
|
---|
2227 |
|
---|
2228 | IWineD3DDeviceImpl_FindTexUnitMap(device);
|
---|
2229 | device_preload_textures(device);
|
---|
2230 | if (isStateDirty(context, STATE_VDECL))
|
---|
2231 | device_update_stream_info(device, context->gl_info);
|
---|
2232 |
|
---|
2233 | ENTER_GL();
|
---|
2234 | for (i = 0; i < context->numDirtyEntries; ++i)
|
---|
2235 | {
|
---|
2236 | DWORD rep = context->dirtyArray[i];
|
---|
2237 | DWORD idx = rep / (sizeof(*context->isStateDirty) * CHAR_BIT);
|
---|
2238 | BYTE shift = rep & ((sizeof(*context->isStateDirty) * CHAR_BIT) - 1);
|
---|
2239 | context->isStateDirty[idx] &= ~(1 << shift);
|
---|
2240 | state_table[rep].apply(rep, device->stateBlock, context);
|
---|
2241 | }
|
---|
2242 | LEAVE_GL();
|
---|
2243 | context->numDirtyEntries = 0; /* This makes the whole list clean */
|
---|
2244 | context->last_was_blit = FALSE;
|
---|
2245 | break;
|
---|
2246 |
|
---|
2247 | case CTXUSAGE_BLIT:
|
---|
2248 | SetupForBlit(device, context);
|
---|
2249 | break;
|
---|
2250 |
|
---|
2251 | default:
|
---|
2252 | FIXME("Unexpected context usage requested\n");
|
---|
2253 | }
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | static void context_setup_target(IWineD3DDeviceImpl *device, struct wined3d_context *context, IWineD3DSurface *target)
|
---|
2257 | {
|
---|
2258 | BOOL old_render_offscreen = context->render_offscreen, render_offscreen;
|
---|
2259 | const struct StateEntry *StateTable = device->StateTable;
|
---|
2260 |
|
---|
2261 | if (!target) return;
|
---|
2262 | else if (context->current_rt == target) return;
|
---|
2263 | render_offscreen = surface_is_offscreen(target);
|
---|
2264 |
|
---|
2265 | context_set_render_offscreen(context, StateTable, render_offscreen);
|
---|
2266 |
|
---|
2267 | /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
|
---|
2268 | * the alpha blend state changes with different render target formats. */
|
---|
2269 | if (!context->current_rt)
|
---|
2270 | {
|
---|
2271 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
2272 | }
|
---|
2273 | else
|
---|
2274 | {
|
---|
2275 | const struct wined3d_format_desc *old = ((IWineD3DSurfaceImpl *)context->current_rt)->resource.format_desc;
|
---|
2276 | const struct wined3d_format_desc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
|
---|
2277 |
|
---|
2278 | if (old->format != new->format)
|
---|
2279 | {
|
---|
2280 | /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
|
---|
2281 | if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
|
---|
2282 | || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
|
---|
2283 | {
|
---|
2284 | Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
|
---|
2285 | }
|
---|
2286 | }
|
---|
2287 |
|
---|
2288 | /* When switching away from an offscreen render target, and we're not
|
---|
2289 | * using FBOs, we have to read the drawable into the texture. This is
|
---|
2290 | * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
|
---|
2291 | * are some things that need care though. PreLoad needs a GL context,
|
---|
2292 | * and FindContext is called before the context is activated. It also
|
---|
2293 | * has to be called with the old rendertarget active, otherwise a
|
---|
2294 | * wrong drawable is read. */
|
---|
2295 | if (wined3d_settings.offscreen_rendering_mode != ORM_FBO
|
---|
2296 | && old_render_offscreen && context->current_rt != target)
|
---|
2297 | {
|
---|
2298 | BOOL oldInDraw = device->isInDraw;
|
---|
2299 |
|
---|
2300 | /* surface_internal_preload() requires a context to load the
|
---|
2301 | * texture, so it will call context_acquire(). Set isInDraw to true
|
---|
2302 | * to signal surface_internal_preload() that it has a context. */
|
---|
2303 |
|
---|
2304 | /* FIXME: This is just broken. There's no guarantee whatsoever
|
---|
2305 | * that the currently active context, if any, is appropriate for
|
---|
2306 | * reading back the render target. We should probably call
|
---|
2307 | * context_set_current(context) here and then rely on
|
---|
2308 | * context_acquire() doing the right thing. */
|
---|
2309 | device->isInDraw = TRUE;
|
---|
2310 |
|
---|
2311 | /* Read the back buffer of the old drawable into the destination texture. */
|
---|
2312 | if (((IWineD3DSurfaceImpl *)context->current_rt)->texture_name_srgb)
|
---|
2313 | {
|
---|
2314 | surface_internal_preload(context->current_rt, SRGB_BOTH);
|
---|
2315 | }
|
---|
2316 | else
|
---|
2317 | {
|
---|
2318 | surface_internal_preload(context->current_rt, SRGB_RGB);
|
---|
2319 | }
|
---|
2320 |
|
---|
2321 | IWineD3DSurface_ModifyLocation(context->current_rt, SFLAG_INDRAWABLE, FALSE);
|
---|
2322 |
|
---|
2323 | device->isInDraw = oldInDraw;
|
---|
2324 | }
|
---|
2325 | }
|
---|
2326 |
|
---|
2327 | context->draw_buffer_dirty = TRUE;
|
---|
2328 | context->current_rt = target;
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 | /*****************************************************************************
|
---|
2332 | * context_acquire
|
---|
2333 | *
|
---|
2334 | * Finds a rendering context and drawable matching the device and render
|
---|
2335 | * target for the current thread, activates them and puts them into the
|
---|
2336 | * requested state.
|
---|
2337 | *
|
---|
2338 | * Params:
|
---|
2339 | * This: Device to activate the context for
|
---|
2340 | * target: Requested render target
|
---|
2341 | * usage: Prepares the context for blitting, drawing or other actions
|
---|
2342 | *
|
---|
2343 | *****************************************************************************/
|
---|
2344 | struct wined3d_context *context_acquire(IWineD3DDeviceImpl *device, IWineD3DSurface *target, enum ContextUsage usage)
|
---|
2345 | {
|
---|
2346 | struct wined3d_context *current_context = context_get_current();
|
---|
2347 | struct wined3d_context *context;
|
---|
2348 |
|
---|
2349 | TRACE("device %p, target %p, usage %#x.\n", device, target, usage);
|
---|
2350 |
|
---|
2351 | context = FindContext(device, target);
|
---|
2352 | context_setup_target(device, context, target);
|
---|
2353 | context_enter(context);
|
---|
2354 | if (!context->valid) return context;
|
---|
2355 |
|
---|
2356 | if (context != current_context)
|
---|
2357 | {
|
---|
2358 | if (!context_set_current(context)) ERR("Failed to activate the new context.\n");
|
---|
2359 | else device->frag_pipe->enable_extension((IWineD3DDevice *)device, !context->last_was_blit);
|
---|
2360 |
|
---|
2361 | if (context->vshader_const_dirty)
|
---|
2362 | {
|
---|
2363 | memset(context->vshader_const_dirty, 1,
|
---|
2364 | sizeof(*context->vshader_const_dirty) * device->d3d_vshader_constantF);
|
---|
2365 | device->highest_dirty_vs_const = device->d3d_vshader_constantF;
|
---|
2366 | }
|
---|
2367 | if (context->pshader_const_dirty)
|
---|
2368 | {
|
---|
2369 | memset(context->pshader_const_dirty, 1,
|
---|
2370 | sizeof(*context->pshader_const_dirty) * device->d3d_pshader_constantF);
|
---|
2371 | device->highest_dirty_ps_const = device->d3d_pshader_constantF;
|
---|
2372 | }
|
---|
2373 | }
|
---|
2374 | else if (context->restore_ctx)
|
---|
2375 | {
|
---|
2376 | if (!pwglMakeCurrent(context->hdc, context->glCtx))
|
---|
2377 | {
|
---|
2378 | DWORD err = GetLastError();
|
---|
2379 | ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
|
---|
2380 | context->hdc, context->glCtx, err);
|
---|
2381 | }
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | context_apply_state(context, device, usage);
|
---|
2385 |
|
---|
2386 | return context;
|
---|
2387 | }
|
---|