2 * Context and render target management in wined3d
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 * Copyright 2009 Henri Verbeet for CodeWeavers
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.
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.
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
27 #include "wined3d_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 #define GLINFO_LOCATION (*gl_info)
33 static DWORD wined3d_context_tls_idx
;
35 /* FBO helper functions */
37 /* GL locking is done by the caller */
38 void context_bind_fbo(struct wined3d_context
*context
, GLenum target
, GLuint
*fbo
)
40 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
51 gl_info
->fbo_ops
.glGenFramebuffers(1, fbo
);
52 checkGLcall("glGenFramebuffers()");
53 TRACE("Created FBO %u.\n", *fbo
);
60 case GL_READ_FRAMEBUFFER
:
61 if (context
->fbo_read_binding
== f
) return;
62 context
->fbo_read_binding
= f
;
65 case GL_DRAW_FRAMEBUFFER
:
66 if (context
->fbo_draw_binding
== f
) return;
67 context
->fbo_draw_binding
= f
;
71 if (context
->fbo_read_binding
== f
72 && context
->fbo_draw_binding
== f
) return;
73 context
->fbo_read_binding
= f
;
74 context
->fbo_draw_binding
= f
;
78 FIXME("Unhandled target %#x.\n", target
);
82 gl_info
->fbo_ops
.glBindFramebuffer(target
, f
);
83 checkGLcall("glBindFramebuffer()");
86 /* GL locking is done by the caller */
87 static void context_clean_fbo_attachments(const struct wined3d_gl_info
*gl_info
)
91 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
93 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
+ i
, GL_TEXTURE_2D
, 0, 0);
94 checkGLcall("glFramebufferTexture2D()");
96 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
97 checkGLcall("glFramebufferTexture2D()");
99 gl_info
->fbo_ops
.glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
100 checkGLcall("glFramebufferTexture2D()");
103 /* GL locking is done by the caller */
104 static void context_destroy_fbo(struct wined3d_context
*context
, GLuint
*fbo
)
106 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
108 context_bind_fbo(context
, GL_FRAMEBUFFER
, fbo
);
109 context_clean_fbo_attachments(gl_info
);
110 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
112 gl_info
->fbo_ops
.glDeleteFramebuffers(1, fbo
);
113 checkGLcall("glDeleteFramebuffers()");
116 /* GL locking is done by the caller */
117 static void context_apply_attachment_filter_states(IWineD3DSurface
*surface
)
119 IWineD3DBaseTextureImpl
*texture_impl
;
121 /* Update base texture states array */
122 if (SUCCEEDED(IWineD3DSurface_GetContainer(surface
, &IID_IWineD3DBaseTexture
, (void **)&texture_impl
)))
124 IWineD3DSurfaceImpl
*surface_impl
= (IWineD3DSurfaceImpl
*)surface
;
125 IWineD3DDeviceImpl
*device
= surface_impl
->resource
.device
;
126 BOOL update_minfilter
= FALSE
;
127 BOOL update_magfilter
= FALSE
;
129 if (texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] != WINED3DTEXF_POINT
130 || texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] != WINED3DTEXF_NONE
)
132 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MINFILTER
] = WINED3DTEXF_POINT
;
133 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MIPFILTER
] = WINED3DTEXF_NONE
;
134 update_minfilter
= TRUE
;
137 if (texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] != WINED3DTEXF_POINT
)
139 texture_impl
->baseTexture
.texture_rgb
.states
[WINED3DTEXSTA_MAGFILTER
] = WINED3DTEXF_POINT
;
140 update_magfilter
= TRUE
;
143 if (texture_impl
->baseTexture
.bindCount
)
145 WARN("Render targets should not be bound to a sampler\n");
146 IWineD3DDeviceImpl_MarkStateDirty(device
, STATE_SAMPLER(texture_impl
->baseTexture
.sampler
));
149 IWineD3DBaseTexture_Release((IWineD3DBaseTexture
*)texture_impl
);
151 if (update_minfilter
|| update_magfilter
)
153 GLenum target
, bind_target
;
156 target
= surface_impl
->texture_target
;
157 if (target
== GL_TEXTURE_2D
)
159 bind_target
= GL_TEXTURE_2D
;
160 glGetIntegerv(GL_TEXTURE_BINDING_2D
, &old_binding
);
162 else if (target
== GL_TEXTURE_RECTANGLE_ARB
)
164 bind_target
= GL_TEXTURE_RECTANGLE_ARB
;
165 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB
, &old_binding
);
169 bind_target
= GL_TEXTURE_CUBE_MAP_ARB
;
170 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB
, &old_binding
);
173 glBindTexture(bind_target
, surface_impl
->texture_name
);
174 if (update_minfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
175 if (update_magfilter
) glTexParameteri(bind_target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
176 glBindTexture(bind_target
, old_binding
);
179 checkGLcall("apply_attachment_filter_states()");
183 /* GL locking is done by the caller */
184 void context_attach_depth_stencil_fbo(struct wined3d_context
*context
,
185 GLenum fbo_target
, IWineD3DSurface
*depth_stencil
, BOOL use_render_buffer
)
187 IWineD3DSurfaceImpl
*depth_stencil_impl
= (IWineD3DSurfaceImpl
*)depth_stencil
;
188 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
190 TRACE("Attach depth stencil %p\n", depth_stencil
);
194 DWORD format_flags
= depth_stencil_impl
->resource
.format_desc
->Flags
;
196 if (use_render_buffer
&& depth_stencil_impl
->current_renderbuffer
)
198 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
200 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_DEPTH_ATTACHMENT
,
201 GL_RENDERBUFFER
, depth_stencil_impl
->current_renderbuffer
->id
);
202 checkGLcall("glFramebufferRenderbuffer()");
205 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
207 gl_info
->fbo_ops
.glFramebufferRenderbuffer(fbo_target
, GL_STENCIL_ATTACHMENT
,
208 GL_RENDERBUFFER
, depth_stencil_impl
->current_renderbuffer
->id
);
209 checkGLcall("glFramebufferRenderbuffer()");
214 surface_prepare_texture(depth_stencil_impl
, gl_info
, FALSE
);
215 context_apply_attachment_filter_states(depth_stencil
);
217 if (format_flags
& WINED3DFMT_FLAG_DEPTH
)
219 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
,
220 depth_stencil_impl
->texture_target
, depth_stencil_impl
->texture_name
,
221 depth_stencil_impl
->texture_level
);
222 checkGLcall("glFramebufferTexture2D()");
225 if (format_flags
& WINED3DFMT_FLAG_STENCIL
)
227 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
,
228 depth_stencil_impl
->texture_target
, depth_stencil_impl
->texture_name
,
229 depth_stencil_impl
->texture_level
);
230 checkGLcall("glFramebufferTexture2D()");
234 if (!(format_flags
& WINED3DFMT_FLAG_DEPTH
))
236 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
237 checkGLcall("glFramebufferTexture2D()");
240 if (!(format_flags
& WINED3DFMT_FLAG_STENCIL
))
242 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
243 checkGLcall("glFramebufferTexture2D()");
248 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_DEPTH_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
249 checkGLcall("glFramebufferTexture2D()");
251 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_STENCIL_ATTACHMENT
, GL_TEXTURE_2D
, 0, 0);
252 checkGLcall("glFramebufferTexture2D()");
256 /* GL locking is done by the caller */
257 void context_attach_surface_fbo(const struct wined3d_context
*context
,
258 GLenum fbo_target
, DWORD idx
, IWineD3DSurface
*surface
)
260 IWineD3DSurfaceImpl
*surface_impl
= (IWineD3DSurfaceImpl
*)surface
;
261 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
263 TRACE("Attach surface %p to %u\n", surface
, idx
);
267 surface_prepare_texture(surface_impl
, gl_info
, FALSE
);
268 context_apply_attachment_filter_states(surface
);
270 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, surface_impl
->texture_target
,
271 surface_impl
->texture_name
, surface_impl
->texture_level
);
272 checkGLcall("glFramebufferTexture2D()");
276 gl_info
->fbo_ops
.glFramebufferTexture2D(fbo_target
, GL_COLOR_ATTACHMENT0
+ idx
, GL_TEXTURE_2D
, 0, 0);
277 checkGLcall("glFramebufferTexture2D()");
281 /* GL locking is done by the caller */
282 static void context_check_fbo_status(struct wined3d_context
*context
)
284 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
287 status
= gl_info
->fbo_ops
.glCheckFramebufferStatus(GL_FRAMEBUFFER
);
288 if (status
== GL_FRAMEBUFFER_COMPLETE
)
290 TRACE("FBO complete\n");
292 IWineD3DSurfaceImpl
*attachment
;
294 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status
), status
);
296 if (!context
->current_fbo
)
298 ERR("FBO 0 is incomplete, driver bug?\n");
302 /* Dump the FBO attachments */
303 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
305 attachment
= (IWineD3DSurfaceImpl
*)context
->current_fbo
->render_targets
[i
];
308 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
309 i
, attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
310 attachment
->pow2Width
, attachment
->pow2Height
);
313 attachment
= (IWineD3DSurfaceImpl
*)context
->current_fbo
->depth_stencil
;
316 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
317 attachment
, debug_d3dformat(attachment
->resource
.format_desc
->format
),
318 attachment
->pow2Width
, attachment
->pow2Height
);
323 static struct fbo_entry
*context_create_fbo_entry(struct wined3d_context
*context
)
325 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
326 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
327 struct fbo_entry
*entry
;
329 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
330 entry
->render_targets
= HeapAlloc(GetProcessHeap(), 0, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
331 memcpy(entry
->render_targets
, device
->render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
332 entry
->depth_stencil
= device
->stencilBufferTarget
;
333 entry
->attached
= FALSE
;
339 /* GL locking is done by the caller */
340 static void context_reuse_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
342 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
343 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
345 context_bind_fbo(context
, GL_FRAMEBUFFER
, &entry
->id
);
346 context_clean_fbo_attachments(gl_info
);
348 memcpy(entry
->render_targets
, device
->render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
));
349 entry
->depth_stencil
= device
->stencilBufferTarget
;
350 entry
->attached
= FALSE
;
353 /* GL locking is done by the caller */
354 static void context_destroy_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
358 TRACE("Destroy FBO %d\n", entry
->id
);
359 context_destroy_fbo(context
, &entry
->id
);
361 --context
->fbo_entry_count
;
362 list_remove(&entry
->entry
);
363 HeapFree(GetProcessHeap(), 0, entry
->render_targets
);
364 HeapFree(GetProcessHeap(), 0, entry
);
368 /* GL locking is done by the caller */
369 static struct fbo_entry
*context_find_fbo_entry(struct wined3d_context
*context
)
371 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
372 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
373 struct fbo_entry
*entry
;
375 LIST_FOR_EACH_ENTRY(entry
, &context
->fbo_list
, struct fbo_entry
, entry
)
377 if (!memcmp(entry
->render_targets
,
378 device
->render_targets
, gl_info
->limits
.buffers
* sizeof(*entry
->render_targets
))
379 && entry
->depth_stencil
== device
->stencilBufferTarget
)
381 list_remove(&entry
->entry
);
382 list_add_head(&context
->fbo_list
, &entry
->entry
);
387 if (context
->fbo_entry_count
< WINED3D_MAX_FBO_ENTRIES
)
389 entry
= context_create_fbo_entry(context
);
390 list_add_head(&context
->fbo_list
, &entry
->entry
);
391 ++context
->fbo_entry_count
;
395 entry
= LIST_ENTRY(list_tail(&context
->fbo_list
), struct fbo_entry
, entry
);
396 context_reuse_fbo_entry(context
, entry
);
397 list_remove(&entry
->entry
);
398 list_add_head(&context
->fbo_list
, &entry
->entry
);
404 /* GL locking is done by the caller */
405 static void context_apply_fbo_entry(struct wined3d_context
*context
, struct fbo_entry
*entry
)
407 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
408 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
411 context_bind_fbo(context
, GL_FRAMEBUFFER
, &entry
->id
);
413 if (!entry
->attached
)
415 /* Apply render targets */
416 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
418 IWineD3DSurface
*render_target
= device
->render_targets
[i
];
419 context_attach_surface_fbo(context
, GL_FRAMEBUFFER
, i
, render_target
);
422 /* Apply depth targets */
423 if (device
->stencilBufferTarget
)
425 unsigned int w
= ((IWineD3DSurfaceImpl
*)device
->render_targets
[0])->pow2Width
;
426 unsigned int h
= ((IWineD3DSurfaceImpl
*)device
->render_targets
[0])->pow2Height
;
428 surface_set_compatible_renderbuffer(device
->stencilBufferTarget
, w
, h
);
430 context_attach_depth_stencil_fbo(context
, GL_FRAMEBUFFER
, device
->stencilBufferTarget
, TRUE
);
432 entry
->attached
= TRUE
;
436 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
438 if (device
->render_targets
[i
])
439 context_apply_attachment_filter_states(device
->render_targets
[i
]);
441 if (device
->stencilBufferTarget
)
442 context_apply_attachment_filter_states(device
->stencilBufferTarget
);
445 for (i
= 0; i
< gl_info
->limits
.buffers
; ++i
)
447 if (device
->render_targets
[i
])
448 device
->draw_buffers
[i
] = GL_COLOR_ATTACHMENT0
+ i
;
450 device
->draw_buffers
[i
] = GL_NONE
;
454 /* GL locking is done by the caller */
455 static void context_apply_fbo_state(struct wined3d_context
*context
)
457 struct fbo_entry
*entry
, *entry2
;
459 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
461 context_destroy_fbo_entry(context
, entry
);
464 if (context
->render_offscreen
)
466 context
->current_fbo
= context_find_fbo_entry(context
);
467 context_apply_fbo_entry(context
, context
->current_fbo
);
469 context
->current_fbo
= NULL
;
470 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
473 context_check_fbo_status(context
);
476 /* Context activation is done by the caller. */
477 void context_alloc_occlusion_query(struct wined3d_context
*context
, struct wined3d_occlusion_query
*query
)
479 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
481 if (context
->free_occlusion_query_count
)
483 query
->id
= context
->free_occlusion_queries
[--context
->free_occlusion_query_count
];
487 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
490 GL_EXTCALL(glGenQueriesARB(1, &query
->id
));
491 checkGLcall("glGenQueriesARB");
494 TRACE("Allocated occlusion query %u in context %p.\n", query
->id
, context
);
498 WARN("Occlusion queries not supported, not allocating query id.\n");
503 query
->context
= context
;
504 list_add_head(&context
->occlusion_queries
, &query
->entry
);
507 void context_free_occlusion_query(struct wined3d_occlusion_query
*query
)
509 struct wined3d_context
*context
= query
->context
;
511 list_remove(&query
->entry
);
512 query
->context
= NULL
;
514 if (context
->free_occlusion_query_count
>= context
->free_occlusion_query_size
- 1)
516 UINT new_size
= context
->free_occlusion_query_size
<< 1;
517 GLuint
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_occlusion_queries
,
518 new_size
* sizeof(*context
->free_occlusion_queries
));
522 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->id
, context
);
526 context
->free_occlusion_query_size
= new_size
;
527 context
->free_occlusion_queries
= new_data
;
530 context
->free_occlusion_queries
[context
->free_occlusion_query_count
++] = query
->id
;
533 /* Context activation is done by the caller. */
534 void context_alloc_event_query(struct wined3d_context
*context
, struct wined3d_event_query
*query
)
536 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
538 if (context
->free_event_query_count
)
540 query
->object
= context
->free_event_queries
[--context
->free_event_query_count
];
544 if (gl_info
->supported
[ARB_SYNC
])
546 /* Using ARB_sync, not much to do here. */
547 query
->object
.sync
= NULL
;
548 TRACE("Allocated event query %p in context %p.\n", query
->object
.sync
, context
);
550 else if (gl_info
->supported
[APPLE_FENCE
])
553 GL_EXTCALL(glGenFencesAPPLE(1, &query
->object
.id
));
554 checkGLcall("glGenFencesAPPLE");
557 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
559 else if(gl_info
->supported
[NV_FENCE
])
562 GL_EXTCALL(glGenFencesNV(1, &query
->object
.id
));
563 checkGLcall("glGenFencesNV");
566 TRACE("Allocated event query %u in context %p.\n", query
->object
.id
, context
);
570 WARN("Event queries not supported, not allocating query id.\n");
571 query
->object
.id
= 0;
575 query
->context
= context
;
576 list_add_head(&context
->event_queries
, &query
->entry
);
579 void context_free_event_query(struct wined3d_event_query
*query
)
581 struct wined3d_context
*context
= query
->context
;
583 list_remove(&query
->entry
);
584 query
->context
= NULL
;
586 if (context
->free_event_query_count
>= context
->free_event_query_size
- 1)
588 UINT new_size
= context
->free_event_query_size
<< 1;
589 union wined3d_gl_query_object
*new_data
= HeapReAlloc(GetProcessHeap(), 0, context
->free_event_queries
,
590 new_size
* sizeof(*context
->free_event_queries
));
594 ERR("Failed to grow free list, leaking query %u in context %p.\n", query
->object
.id
, context
);
598 context
->free_event_query_size
= new_size
;
599 context
->free_event_queries
= new_data
;
602 context
->free_event_queries
[context
->free_event_query_count
++] = query
->object
;
605 void context_resource_released(IWineD3DDevice
*iface
, IWineD3DResource
*resource
, WINED3DRESOURCETYPE type
)
607 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
610 if (!This
->d3d_initialized
) return;
614 case WINED3DRTYPE_SURFACE
:
616 for (i
= 0; i
< This
->numContexts
; ++i
)
618 struct wined3d_context
*context
= This
->contexts
[i
];
619 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
620 struct fbo_entry
*entry
, *entry2
;
622 if (context
->current_rt
== (IWineD3DSurface
*)resource
) context
->current_rt
= NULL
;
624 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
628 if (entry
->depth_stencil
== (IWineD3DSurface
*)resource
)
630 list_remove(&entry
->entry
);
631 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
635 for (j
= 0; j
< gl_info
->limits
.buffers
; ++j
)
637 if (entry
->render_targets
[j
] == (IWineD3DSurface
*)resource
)
639 list_remove(&entry
->entry
);
640 list_add_head(&context
->fbo_destroy_list
, &entry
->entry
);
655 static BOOL
context_set_pixel_format(const struct wined3d_gl_info
*gl_info
, HDC dc
, int format
)
657 int current
= GetPixelFormat(dc
);
659 if (current
== format
) return TRUE
;
663 if (!SetPixelFormat(dc
, format
, NULL
))
665 ERR("Failed to set pixel format %d on device context %p, last error %#x.\n",
666 format
, dc
, GetLastError());
672 /* By default WGL doesn't allow pixel format adjustments but we need it
673 * here. For this reason there's a Wine specific wglSetPixelFormat()
674 * which allows us to set the pixel format multiple times. Only use it
675 * when really needed. */
676 if (gl_info
->supported
[WGL_WINE_PIXEL_FORMAT_PASSTHROUGH
])
678 if (!GL_EXTCALL(wglSetPixelFormatWINE(dc
, format
, NULL
)))
680 ERR("wglSetPixelFormatWINE failed to set pixel format %d on device context %p.\n",
687 /* OpenGL doesn't allow pixel format adjustments. Print an error and
688 * continue using the old format. There's a big chance that the old
689 * format works although with a performance hit and perhaps rendering
691 ERR("Unable to set pixel format %d on device context %p. Already using format %d.\n",
692 format
, dc
, current
);
696 static void context_update_window(struct wined3d_context
*context
)
698 TRACE("Updating context %p window from %p to %p.\n",
699 context
, context
->win_handle
, context
->swapchain
->win_handle
);
703 if (!ReleaseDC(context
->win_handle
, context
->hdc
))
705 ERR("Failed to release device context %p, last error %#x.\n",
706 context
->hdc
, GetLastError());
709 else context
->valid
= 1;
711 context
->win_handle
= context
->swapchain
->win_handle
;
713 if (!(context
->hdc
= GetDC(context
->win_handle
)))
715 ERR("Failed to get a device context for window %p.\n", context
->win_handle
);
719 if (!context_set_pixel_format(context
->gl_info
, context
->hdc
, context
->pixel_format
))
721 ERR("Failed to set pixel format %d on device context %p.\n",
722 context
->pixel_format
, context
->hdc
);
726 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
728 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
729 context
->glCtx
, context
->hdc
, GetLastError());
739 static void context_validate(struct wined3d_context
*context
)
741 HWND wnd
= WindowFromDC(context
->hdc
);
743 if (wnd
!= context
->win_handle
)
745 WARN("DC %p belongs to window %p instead of %p.\n",
746 context
->hdc
, wnd
, context
->win_handle
);
750 if (context
->win_handle
!= context
->swapchain
->win_handle
)
751 context_update_window(context
);
754 static void context_destroy_gl_resources(struct wined3d_context
*context
)
756 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
757 struct wined3d_occlusion_query
*occlusion_query
;
758 struct wined3d_event_query
*event_query
;
759 struct fbo_entry
*entry
, *entry2
;
764 restore_ctx
= pwglGetCurrentContext();
765 restore_dc
= pwglGetCurrentDC();
767 context_validate(context
);
768 if (context
->valid
&& restore_ctx
!= context
->glCtx
) pwglMakeCurrent(context
->hdc
, context
->glCtx
);
769 else restore_ctx
= NULL
;
773 LIST_FOR_EACH_ENTRY(occlusion_query
, &context
->occlusion_queries
, struct wined3d_occlusion_query
, entry
)
775 if (context
->valid
&& gl_info
->supported
[ARB_OCCLUSION_QUERY
])
776 GL_EXTCALL(glDeleteQueriesARB(1, &occlusion_query
->id
));
777 occlusion_query
->context
= NULL
;
780 LIST_FOR_EACH_ENTRY(event_query
, &context
->event_queries
, struct wined3d_event_query
, entry
)
784 if (gl_info
->supported
[ARB_SYNC
])
786 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
788 else if (gl_info
->supported
[APPLE_FENCE
]) GL_EXTCALL(glDeleteFencesAPPLE(1, &event_query
->object
.id
));
789 else if (gl_info
->supported
[NV_FENCE
]) GL_EXTCALL(glDeleteFencesNV(1, &event_query
->object
.id
));
791 event_query
->context
= NULL
;
794 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_destroy_list
, struct fbo_entry
, entry
)
796 if (!context
->valid
) entry
->id
= 0;
797 context_destroy_fbo_entry(context
, entry
);
800 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &context
->fbo_list
, struct fbo_entry
, entry
)
802 if (!context
->valid
) entry
->id
= 0;
803 context_destroy_fbo_entry(context
, entry
);
808 if (context
->src_fbo
)
810 TRACE("Destroy src FBO %d\n", context
->src_fbo
);
811 context_destroy_fbo(context
, &context
->src_fbo
);
813 if (context
->dst_fbo
)
815 TRACE("Destroy dst FBO %d\n", context
->dst_fbo
);
816 context_destroy_fbo(context
, &context
->dst_fbo
);
818 if (context
->dummy_arbfp_prog
)
820 GL_EXTCALL(glDeleteProgramsARB(1, &context
->dummy_arbfp_prog
));
823 if (gl_info
->supported
[ARB_OCCLUSION_QUERY
])
824 GL_EXTCALL(glDeleteQueriesARB(context
->free_occlusion_query_count
, context
->free_occlusion_queries
));
826 if (gl_info
->supported
[ARB_SYNC
])
828 if (event_query
->object
.sync
) GL_EXTCALL(glDeleteSync(event_query
->object
.sync
));
830 else if (gl_info
->supported
[APPLE_FENCE
])
832 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
834 GL_EXTCALL(glDeleteFencesAPPLE(1, &context
->free_event_queries
[i
].id
));
837 else if (gl_info
->supported
[NV_FENCE
])
839 for (i
= 0; i
< context
->free_event_query_count
; ++i
)
841 GL_EXTCALL(glDeleteFencesNV(1, &context
->free_event_queries
[i
].id
));
845 checkGLcall("context cleanup");
850 HeapFree(GetProcessHeap(), 0, context
->free_occlusion_queries
);
851 HeapFree(GetProcessHeap(), 0, context
->free_event_queries
);
855 if (!pwglMakeCurrent(restore_dc
, restore_ctx
))
857 DWORD err
= GetLastError();
858 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
859 restore_ctx
, restore_dc
, err
);
862 else if (pwglGetCurrentContext() && !pwglMakeCurrent(NULL
, NULL
))
864 ERR("Failed to disable GL context.\n");
867 ReleaseDC(context
->win_handle
, context
->hdc
);
869 if (!pwglDeleteContext(context
->glCtx
))
871 DWORD err
= GetLastError();
872 ERR("wglDeleteContext(%p) failed, last error %#x.\n", context
->glCtx
, err
);
876 DWORD
context_get_tls_idx(void)
878 return wined3d_context_tls_idx
;
881 void context_set_tls_idx(DWORD idx
)
883 wined3d_context_tls_idx
= idx
;
886 struct wined3d_context
*context_get_current(void)
888 return TlsGetValue(wined3d_context_tls_idx
);
891 BOOL
context_set_current(struct wined3d_context
*ctx
)
893 struct wined3d_context
*old
= context_get_current();
897 TRACE("Already using D3D context %p.\n", ctx
);
905 TRACE("Switching away from destroyed context %p.\n", old
);
906 context_destroy_gl_resources(old
);
907 HeapFree(GetProcessHeap(), 0, old
);
917 TRACE("Switching to D3D context %p, GL context %p, device context %p.\n", ctx
, ctx
->glCtx
, ctx
->hdc
);
918 if (!pwglMakeCurrent(ctx
->hdc
, ctx
->glCtx
))
920 DWORD err
= GetLastError();
921 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
922 ctx
->glCtx
, ctx
->hdc
, err
);
923 TlsSetValue(wined3d_context_tls_idx
, NULL
);
928 else if(pwglGetCurrentContext())
930 TRACE("Clearing current D3D context.\n");
931 if (!pwglMakeCurrent(NULL
, NULL
))
933 DWORD err
= GetLastError();
934 ERR("Failed to clear current GL context, last error %#x.\n", err
);
935 TlsSetValue(wined3d_context_tls_idx
, NULL
);
940 return TlsSetValue(wined3d_context_tls_idx
, ctx
);
943 void context_release(struct wined3d_context
*context
)
945 TRACE("Releasing context %p, level %u.\n", context
, context
->level
);
950 WARN("Context %p is not active.\n", context
);
951 else if (context
!= context_get_current())
952 WARN("Context %p is not the current context.\n", context
);
955 if (!--context
->level
&& context
->restore_ctx
)
957 TRACE("Restoring GL context %p on device context %p.\n", context
->restore_ctx
, context
->restore_dc
);
958 if (!pwglMakeCurrent(context
->restore_dc
, context
->restore_ctx
))
960 DWORD err
= GetLastError();
961 ERR("Failed to restore GL context %p on device context %p, last error %#x.\n",
962 context
->restore_ctx
, context
->restore_dc
, err
);
964 context
->restore_ctx
= NULL
;
965 context
->restore_dc
= NULL
;
969 static void context_enter(struct wined3d_context
*context
)
971 TRACE("Entering context %p, level %u.\n", context
, context
->level
+ 1);
973 if (!context
->level
++)
975 const struct wined3d_context
*current_context
= context_get_current();
976 HGLRC current_gl
= pwglGetCurrentContext();
978 if (current_gl
&& (!current_context
|| current_context
->glCtx
!= current_gl
))
980 TRACE("Another GL context (%p on device context %p) is already current.\n",
981 current_gl
, pwglGetCurrentDC());
982 context
->restore_ctx
= current_gl
;
983 context
->restore_dc
= pwglGetCurrentDC();
988 /*****************************************************************************
989 * Context_MarkStateDirty
991 * Marks a state in a context dirty. Only one context, opposed to
992 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
996 * context: Context to mark the state dirty in
997 * state: State to mark dirty
998 * StateTable: Pointer to the state table in use(for state grouping)
1000 *****************************************************************************/
1001 static void Context_MarkStateDirty(struct wined3d_context
*context
, DWORD state
, const struct StateEntry
*StateTable
)
1003 DWORD rep
= StateTable
[state
].representative
;
1007 if (isStateDirty(context
, rep
)) return;
1009 context
->dirtyArray
[context
->numDirtyEntries
++] = rep
;
1010 idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
1011 shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
1012 context
->isStateDirty
[idx
] |= (1 << shift
);
1015 /* This function takes care of WineD3D pixel format selection. */
1016 static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl
*This
, HDC hdc
,
1017 const struct GlPixelFormatDesc
*color_format_desc
, const struct GlPixelFormatDesc
*ds_format_desc
,
1018 BOOL auxBuffers
, int numSamples
, BOOL findCompatible
)
1021 unsigned int matchtry
;
1022 short redBits
, greenBits
, blueBits
, alphaBits
, colorBits
;
1023 short depthBits
=0, stencilBits
=0;
1030 /* First, try without alpha match buffers. MacOS supports aux buffers only
1031 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
1032 * Then try without aux buffers - this is the most common cause for not
1033 * finding a pixel format. Also some drivers(the open source ones)
1034 * only offer 32 bit ARB pixel formats. First try without an exact alpha
1035 * match, then try without an exact alpha and color match.
1037 { TRUE
, TRUE
, TRUE
},
1038 { TRUE
, FALSE
, TRUE
},
1039 { FALSE
, TRUE
, TRUE
},
1040 { FALSE
, FALSE
, TRUE
},
1041 { TRUE
, FALSE
, FALSE
},
1042 { FALSE
, FALSE
, FALSE
},
1046 int nCfgs
= This
->adapter
->nCfgs
;
1048 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, findCompatible=%d\n",
1049 debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
),
1050 auxBuffers
, numSamples
, findCompatible
);
1052 if (!getColorBits(color_format_desc
, &redBits
, &greenBits
, &blueBits
, &alphaBits
, &colorBits
))
1054 ERR("Unable to get color bits for format %s (%#x)!\n",
1055 debug_d3dformat(color_format_desc
->format
), color_format_desc
->format
);
1059 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
1060 * You are able to add a depth + stencil surface at a later stage when you need it.
1061 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
1062 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
1063 * context, need torecreate shaders, textures and other resources.
1065 * The context manager already takes care of the state problem and for the other tasks code from Reset
1066 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
1067 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
1068 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
1069 * issue needs to be fixed. */
1070 if (ds_format_desc
->format
!= WINED3DFMT_D24_UNORM_S8_UINT
)
1072 FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
1073 ds_format_desc
= getFormatDescEntry(WINED3DFMT_D24_UNORM_S8_UINT
, &This
->adapter
->gl_info
);
1076 getDepthStencilBits(ds_format_desc
, &depthBits
, &stencilBits
);
1078 for(matchtry
= 0; matchtry
< (sizeof(matches
) / sizeof(matches
[0])) && !iPixelFormat
; matchtry
++) {
1079 for(i
=0; i
<nCfgs
; i
++) {
1080 BOOL exactDepthMatch
= TRUE
;
1081 WineD3D_PixelFormat
*cfg
= &This
->adapter
->cfgs
[i
];
1083 /* For now only accept RGBA formats. Perhaps some day we will
1084 * allow floating point formats for pbuffers. */
1085 if(cfg
->iPixelType
!= WGL_TYPE_RGBA_ARB
)
1088 /* In window mode we need a window drawable format and double buffering. */
1089 if(!(cfg
->windowDrawable
&& cfg
->doubleBuffer
))
1092 /* We like to have aux buffers in backbuffer mode */
1093 if(auxBuffers
&& !cfg
->auxBuffers
&& matches
[matchtry
].require_aux
)
1096 if(matches
[matchtry
].exact_color
) {
1097 if(cfg
->redSize
!= redBits
)
1099 if(cfg
->greenSize
!= greenBits
)
1101 if(cfg
->blueSize
!= blueBits
)
1104 if(cfg
->redSize
< redBits
)
1106 if(cfg
->greenSize
< greenBits
)
1108 if(cfg
->blueSize
< blueBits
)
1111 if(matches
[matchtry
].exact_alpha
) {
1112 if(cfg
->alphaSize
!= alphaBits
)
1115 if(cfg
->alphaSize
< alphaBits
)
1119 /* We try to locate a format which matches our requirements exactly. In case of
1120 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
1121 if(cfg
->depthSize
< depthBits
)
1123 else if(cfg
->depthSize
> depthBits
)
1124 exactDepthMatch
= FALSE
;
1126 /* In all cases make sure the number of stencil bits matches our requirements
1127 * even when we don't need stencil because it could affect performance EXCEPT
1128 * on cards which don't offer depth formats without stencil like the i915 drivers
1130 if(stencilBits
!= cfg
->stencilSize
&& !(This
->adapter
->brokenStencil
&& stencilBits
<= cfg
->stencilSize
))
1133 /* Check multisampling support */
1134 if(cfg
->numSamples
!= numSamples
)
1137 /* When we have passed all the checks then we have found a format which matches our
1138 * requirements. Note that we only check for a limit number of capabilities right now,
1139 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
1140 * can still differ in things like multisampling, stereo, SRGB and other flags.
1143 /* Exit the loop as we have found a format :) */
1144 if(exactDepthMatch
) {
1145 iPixelFormat
= cfg
->iPixelFormat
;
1147 } else if(!iPixelFormat
) {
1148 /* In the end we might end up with a format which doesn't exactly match our depth
1149 * requirements. Accept the first format we found because formats with higher iPixelFormat
1150 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
1151 iPixelFormat
= cfg
->iPixelFormat
;
1156 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
1157 if(!iPixelFormat
&& !findCompatible
) {
1158 ERR("Can't find a suitable iPixelFormat\n");
1160 } else if(!iPixelFormat
) {
1161 PIXELFORMATDESCRIPTOR pfd
;
1163 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
1164 /* PixelFormat selection */
1165 ZeroMemory(&pfd
, sizeof(pfd
));
1166 pfd
.nSize
= sizeof(pfd
);
1168 pfd
.dwFlags
= PFD_SUPPORT_OPENGL
| PFD_DOUBLEBUFFER
| PFD_DRAW_TO_WINDOW
;/*PFD_GENERIC_ACCELERATED*/
1169 pfd
.iPixelType
= PFD_TYPE_RGBA
;
1170 pfd
.cAlphaBits
= alphaBits
;
1171 pfd
.cColorBits
= colorBits
;
1172 pfd
.cDepthBits
= depthBits
;
1173 pfd
.cStencilBits
= stencilBits
;
1174 pfd
.iLayerType
= PFD_MAIN_PLANE
;
1176 iPixelFormat
= ChoosePixelFormat(hdc
, &pfd
);
1178 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
1179 ERR("Can't find a suitable iPixelFormat\n");
1184 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
1185 iPixelFormat
, debug_d3dformat(color_format_desc
->format
), debug_d3dformat(ds_format_desc
->format
));
1186 return iPixelFormat
;
1189 /*****************************************************************************
1192 * Creates a new context.
1195 * This: Device to activate the context for
1196 * target: Surface this context will render to
1197 * win_handle: handle to the window which we are drawing to
1198 * pPresentParameters: contains the pixelformats to use for onscreen rendering
1200 *****************************************************************************/
1201 struct wined3d_context
*context_create(IWineD3DSwapChainImpl
*swapchain
, IWineD3DSurfaceImpl
*target
)
1203 IWineD3DDeviceImpl
*device
= swapchain
->device
;
1204 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1205 const struct GlPixelFormatDesc
*color_format_desc
;
1206 const struct GlPixelFormatDesc
*ds_format_desc
;
1207 struct wined3d_context
*ret
;
1208 PIXELFORMATDESCRIPTOR pfd
;
1209 BOOL auxBuffers
= FALSE
;
1217 TRACE("swapchain %p, target %p, window %p.\n", swapchain
, target
, swapchain
->win_handle
);
1219 ret
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*ret
));
1222 ERR("Failed to allocate context memory.\n");
1226 if (!(hdc
= GetDC(swapchain
->win_handle
)))
1228 ERR("Failed to retrieve a device context.\n");
1232 ds_format_desc
= getFormatDescEntry(WINED3DFMT_UNKNOWN
, gl_info
);
1233 color_format_desc
= target
->resource
.format_desc
;
1235 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for
1236 * X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
1237 if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
1241 if (color_format_desc
->format
== WINED3DFMT_B4G4R4X4_UNORM
)
1242 color_format_desc
= getFormatDescEntry(WINED3DFMT_B4G4R4A4_UNORM
, gl_info
);
1243 else if (color_format_desc
->format
== WINED3DFMT_B8G8R8X8_UNORM
)
1244 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1247 /* DirectDraw supports 8bit paletted render targets and these are used by
1248 * old games like Starcraft and C&C. Most modern hardware doesn't support
1249 * 8bit natively so we perform some form of 8bit -> 32bit conversion. The
1250 * conversion (ab)uses the alpha component for storing the palette index.
1251 * For this reason we require a format with 8bit alpha, so request
1253 if (color_format_desc
->format
== WINED3DFMT_P8_UINT
)
1254 color_format_desc
= getFormatDescEntry(WINED3DFMT_B8G8R8A8_UNORM
, gl_info
);
1256 /* Retrieve the depth stencil format from the present parameters.
1257 * The choice of the proper format can give a nice performance boost
1258 * in case of GPU limited programs. */
1259 if (swapchain
->presentParms
.EnableAutoDepthStencil
)
1261 TRACE("Auto depth stencil enabled, using format %s.\n",
1262 debug_d3dformat(swapchain
->presentParms
.AutoDepthStencilFormat
));
1263 ds_format_desc
= getFormatDescEntry(swapchain
->presentParms
.AutoDepthStencilFormat
, gl_info
);
1266 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD. */
1267 if (swapchain
->presentParms
.MultiSampleType
&& (swapchain
->presentParms
.SwapEffect
== WINED3DSWAPEFFECT_DISCARD
))
1269 if (!gl_info
->supported
[ARB_MULTISAMPLE
])
1270 WARN("The application is requesting multisampling without support.\n");
1273 TRACE("Requesting multisample type %#x.\n", swapchain
->presentParms
.MultiSampleType
);
1274 numSamples
= swapchain
->presentParms
.MultiSampleType
;
1278 /* Try to find a pixel format which matches our requirements. */
1279 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1280 auxBuffers
, numSamples
, FALSE
/* findCompatible */);
1282 /* Try to locate a compatible format if we weren't able to find anything. */
1285 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
1286 pixel_format
= WineD3D_ChoosePixelFormat(device
, hdc
, color_format_desc
, ds_format_desc
,
1287 auxBuffers
, 0 /* numSamples */, TRUE
/* findCompatible */);
1290 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
1293 ERR("Can't find a suitable pixel format.\n");
1297 DescribePixelFormat(hdc
, pixel_format
, sizeof(pfd
), &pfd
);
1298 if (!context_set_pixel_format(gl_info
, hdc
, pixel_format
))
1300 ERR("Failed to set pixel format %d on device context %p.\n", pixel_format
, hdc
);
1304 ctx
= pwglCreateContext(hdc
);
1305 if (device
->numContexts
)
1307 if (!pwglShareLists(device
->contexts
[0]->glCtx
, ctx
))
1309 DWORD err
= GetLastError();
1310 ERR("wglShareLists(%p, %p) failed, last error %#x.\n",
1311 device
->contexts
[0]->glCtx
, ctx
, err
);
1316 ERR("Failed to create a WGL context\n");
1320 if (!device_context_add(device
, ret
))
1322 ERR("Failed to add the newly created context to the context list\n");
1323 if (!pwglDeleteContext(ctx
))
1325 DWORD err
= GetLastError();
1326 ERR("wglDeleteContext(%p) failed, last error %#x.\n", ctx
, err
);
1331 ret
->gl_info
= gl_info
;
1333 /* Mark all states dirty to force a proper initialization of the states
1334 * on the first use of the context. */
1335 for (state
= 0; state
<= STATE_HIGHEST
; ++state
)
1337 if (device
->StateTable
[state
].representative
)
1338 Context_MarkStateDirty(ret
, state
, device
->StateTable
);
1341 ret
->swapchain
= swapchain
;
1342 ret
->current_rt
= (IWineD3DSurface
*)target
;
1343 ret
->tid
= GetCurrentThreadId();
1345 ret
->render_offscreen
= surface_is_offscreen((IWineD3DSurface
*) target
);
1346 ret
->draw_buffer_dirty
= TRUE
;
1350 ret
->win_handle
= swapchain
->win_handle
;
1352 ret
->pixel_format
= pixel_format
;
1354 if (device
->shader_backend
->shader_dirtifyable_constants((IWineD3DDevice
*)device
))
1356 /* Create the dirty constants array and initialize them to dirty */
1357 ret
->vshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1358 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1359 ret
->pshader_const_dirty
= HeapAlloc(GetProcessHeap(), 0,
1360 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1361 memset(ret
->vshader_const_dirty
, 1,
1362 sizeof(*ret
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
1363 memset(ret
->pshader_const_dirty
, 1,
1364 sizeof(*ret
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
1367 ret
->free_occlusion_query_size
= 4;
1368 ret
->free_occlusion_queries
= HeapAlloc(GetProcessHeap(), 0,
1369 ret
->free_occlusion_query_size
* sizeof(*ret
->free_occlusion_queries
));
1370 if (!ret
->free_occlusion_queries
) goto out
;
1372 list_init(&ret
->occlusion_queries
);
1374 ret
->free_event_query_size
= 4;
1375 ret
->free_event_queries
= HeapAlloc(GetProcessHeap(), 0,
1376 ret
->free_event_query_size
* sizeof(*ret
->free_event_queries
));
1377 if (!ret
->free_event_queries
) goto out
;
1379 list_init(&ret
->event_queries
);
1381 TRACE("Successfully created new context %p\n", ret
);
1383 list_init(&ret
->fbo_list
);
1384 list_init(&ret
->fbo_destroy_list
);
1388 /* Set up the context defaults */
1389 if (!context_set_current(ret
))
1391 ERR("Cannot activate context to set up defaults\n");
1392 context_release(ret
);
1398 glGetIntegerv(GL_AUX_BUFFERS
, &ret
->aux_buffers
);
1400 TRACE("Setting up the screen\n");
1401 /* Clear the screen */
1402 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
1403 checkGLcall("glClearColor");
1406 glClearStencil(0xffff);
1408 checkGLcall("glClear");
1410 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER
, GL_TRUE
);
1411 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
1413 glTexEnvf(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_COMBINE_EXT
);
1414 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
1416 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL
, GL_SEPARATE_SPECULAR_COLOR
);
1417 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
1419 glPixelStorei(GL_PACK_ALIGNMENT
, device
->surface_alignment
);
1420 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, device->surface_alignment);");
1421 glPixelStorei(GL_UNPACK_ALIGNMENT
, device
->surface_alignment
);
1422 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, device->surface_alignment);");
1424 if (gl_info
->supported
[APPLE_CLIENT_STORAGE
])
1426 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
1427 * and textures in DIB sections(due to the memory protection).
1429 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE
, GL_TRUE
);
1430 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1432 if (gl_info
->supported
[ARB_VERTEX_BLEND
])
1434 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
1435 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
1436 * GL_VERTEX_BLEND_ARB isn't enabled too
1438 glEnable(GL_WEIGHT_SUM_UNITY_ARB
);
1439 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
1441 if (gl_info
->supported
[NV_TEXTURE_SHADER2
])
1443 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
1444 * the previous texture where to source the offset from is always unit - 1.
1446 for (s
= 1; s
< gl_info
->limits
.textures
; ++s
)
1448 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1449 glTexEnvi(GL_TEXTURE_SHADER_NV
, GL_PREVIOUS_TEXTURE_INPUT_NV
, GL_TEXTURE0_ARB
+ s
- 1);
1450 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...");
1453 if (gl_info
->supported
[ARB_FRAGMENT_PROGRAM
])
1455 /* MacOS(radeon X1600 at least, but most likely others too) refuses to draw if GLSL and ARBFP are
1456 * enabled, but the currently bound arbfp program is 0. Enabling ARBFP with prog 0 is invalid, but
1457 * GLSL should bypass this. This causes problems in programs that never use the fixed function pipeline,
1458 * because the ARBFP extension is enabled by the ARBFP pipeline at context creation, but no program
1461 * So make sure a program is assigned to each context. The first real ARBFP use will set a different
1462 * program and the dummy program is destroyed when the context is destroyed.
1464 const char *dummy_program
=
1466 "MOV result.color, fragment.color.primary;\n"
1468 GL_EXTCALL(glGenProgramsARB(1, &ret
->dummy_arbfp_prog
));
1469 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB
, ret
->dummy_arbfp_prog
));
1470 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
, strlen(dummy_program
), dummy_program
));
1473 for (s
= 0; s
< gl_info
->limits
.point_sprite_units
; ++s
)
1475 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ s
));
1476 glTexEnvi(GL_POINT_SPRITE_ARB
, GL_COORD_REPLACE_ARB
, GL_TRUE
);
1477 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)");
1480 if (gl_info
->supported
[ARB_PROVOKING_VERTEX
])
1482 GL_EXTCALL(glProvokingVertex(GL_FIRST_VERTEX_CONVENTION
));
1484 else if (gl_info
->supported
[EXT_PROVOKING_VERTEX
])
1486 GL_EXTCALL(glProvokingVertexEXT(GL_FIRST_VERTEX_CONVENTION_EXT
));
1491 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
1493 TRACE("Created context %p.\n", ret
);
1498 HeapFree(GetProcessHeap(), 0, ret
->free_event_queries
);
1499 HeapFree(GetProcessHeap(), 0, ret
->free_occlusion_queries
);
1500 HeapFree(GetProcessHeap(), 0, ret
->pshader_const_dirty
);
1501 HeapFree(GetProcessHeap(), 0, ret
->vshader_const_dirty
);
1502 HeapFree(GetProcessHeap(), 0, ret
);
1506 /*****************************************************************************
1509 * Destroys a wined3d context
1512 * This: Device to activate the context for
1513 * context: Context to destroy
1515 *****************************************************************************/
1516 void context_destroy(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1520 TRACE("Destroying ctx %p\n", context
);
1522 if (context
->tid
== GetCurrentThreadId() || !context
->current
)
1524 context_destroy_gl_resources(context
);
1525 TlsSetValue(wined3d_context_tls_idx
, NULL
);
1530 context
->destroyed
= 1;
1534 HeapFree(GetProcessHeap(), 0, context
->vshader_const_dirty
);
1535 HeapFree(GetProcessHeap(), 0, context
->pshader_const_dirty
);
1536 device_context_remove(This
, context
);
1537 if (destroy
) HeapFree(GetProcessHeap(), 0, context
);
1540 /* GL locking is done by the caller */
1541 static inline void set_blit_dimension(UINT width
, UINT height
) {
1542 glMatrixMode(GL_PROJECTION
);
1543 checkGLcall("glMatrixMode(GL_PROJECTION)");
1545 checkGLcall("glLoadIdentity()");
1546 glOrtho(0, width
, height
, 0, 0.0, -1.0);
1547 checkGLcall("glOrtho");
1548 glViewport(0, 0, width
, height
);
1549 checkGLcall("glViewport");
1552 /*****************************************************************************
1555 * Sets up a context for DirectDraw blitting.
1556 * All texture units are disabled, texture unit 0 is set as current unit
1557 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1558 * color writing enabled for all channels
1559 * register combiners disabled, shaders disabled
1560 * world matrix is set to identity, texture matrix 0 too
1561 * projection matrix is setup for drawing screen coordinates
1564 * This: Device to activate the context for
1565 * context: Context to setup
1567 *****************************************************************************/
1568 /* Context activation is done by the caller. */
1569 static void SetupForBlit(IWineD3DDeviceImpl
*This
, struct wined3d_context
*context
)
1572 const struct StateEntry
*StateTable
= This
->StateTable
;
1573 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1574 UINT width
= ((IWineD3DSurfaceImpl
*)context
->current_rt
)->currentDesc
.Width
;
1575 UINT height
= ((IWineD3DSurfaceImpl
*)context
->current_rt
)->currentDesc
.Height
;
1578 TRACE("Setting up context %p for blitting\n", context
);
1579 if(context
->last_was_blit
) {
1580 if(context
->blit_w
!= width
|| context
->blit_h
!= height
) {
1582 set_blit_dimension(width
, height
);
1584 context
->blit_w
= width
; context
->blit_h
= height
;
1585 /* No need to dirtify here, the states are still dirtified because they weren't
1586 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1590 TRACE("Context is already set up for blitting, nothing to do\n");
1593 context
->last_was_blit
= TRUE
;
1595 /* TODO: Use a display list */
1597 /* Disable shaders */
1599 This
->shader_backend
->shader_select(context
, FALSE
, FALSE
);
1602 Context_MarkStateDirty(context
, STATE_VSHADER
, StateTable
);
1603 Context_MarkStateDirty(context
, STATE_PIXELSHADER
, StateTable
);
1605 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1606 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1607 * which can safely be called from here, we only lock once instead locking/unlocking
1608 * after each GL call.
1612 /* Disable all textures. The caller can then bind a texture it wants to blit
1615 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1616 * function texture unit. No need to care for higher samplers
1618 for (i
= gl_info
->limits
.textures
- 1; i
> 0 ; --i
)
1620 sampler
= This
->rev_tex_unit_map
[i
];
1621 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
+ i
));
1622 checkGLcall("glActiveTextureARB");
1624 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1626 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1627 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1629 glDisable(GL_TEXTURE_3D
);
1630 checkGLcall("glDisable GL_TEXTURE_3D");
1631 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1633 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1634 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1636 glDisable(GL_TEXTURE_2D
);
1637 checkGLcall("glDisable GL_TEXTURE_2D");
1639 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1640 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1642 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1644 if (sampler
< MAX_TEXTURES
) {
1645 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1647 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1650 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB
));
1651 checkGLcall("glActiveTextureARB");
1653 sampler
= This
->rev_tex_unit_map
[0];
1655 if (gl_info
->supported
[ARB_TEXTURE_CUBE_MAP
])
1657 glDisable(GL_TEXTURE_CUBE_MAP_ARB
);
1658 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1660 glDisable(GL_TEXTURE_3D
);
1661 checkGLcall("glDisable GL_TEXTURE_3D");
1662 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
1664 glDisable(GL_TEXTURE_RECTANGLE_ARB
);
1665 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1667 glDisable(GL_TEXTURE_2D
);
1668 checkGLcall("glDisable GL_TEXTURE_2D");
1670 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
1672 glMatrixMode(GL_TEXTURE
);
1673 checkGLcall("glMatrixMode(GL_TEXTURE)");
1675 checkGLcall("glLoadIdentity()");
1677 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1679 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1680 GL_TEXTURE_LOD_BIAS_EXT
,
1682 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1685 if (sampler
!= WINED3D_UNMAPPED_STAGE
)
1687 if (sampler
< MAX_TEXTURES
) {
1688 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_TEXTURE0
+ sampler
), StateTable
);
1689 Context_MarkStateDirty(context
, STATE_TEXTURESTAGE(sampler
, WINED3DTSS_COLOROP
), StateTable
);
1691 Context_MarkStateDirty(context
, STATE_SAMPLER(sampler
), StateTable
);
1694 /* Other misc states */
1695 glDisable(GL_ALPHA_TEST
);
1696 checkGLcall("glDisable(GL_ALPHA_TEST)");
1697 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHATESTENABLE
), StateTable
);
1698 glDisable(GL_LIGHTING
);
1699 checkGLcall("glDisable GL_LIGHTING");
1700 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_LIGHTING
), StateTable
);
1701 glDisable(GL_DEPTH_TEST
);
1702 checkGLcall("glDisable GL_DEPTH_TEST");
1703 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ZENABLE
), StateTable
);
1704 glDisableWINE(GL_FOG
);
1705 checkGLcall("glDisable GL_FOG");
1706 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_FOGENABLE
), StateTable
);
1707 glDisable(GL_BLEND
);
1708 checkGLcall("glDisable GL_BLEND");
1709 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1710 glDisable(GL_CULL_FACE
);
1711 checkGLcall("glDisable GL_CULL_FACE");
1712 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CULLMODE
), StateTable
);
1713 glDisable(GL_STENCIL_TEST
);
1714 checkGLcall("glDisable GL_STENCIL_TEST");
1715 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_STENCILENABLE
), StateTable
);
1716 glDisable(GL_SCISSOR_TEST
);
1717 checkGLcall("glDisable GL_SCISSOR_TEST");
1718 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), StateTable
);
1719 if (gl_info
->supported
[ARB_POINT_SPRITE
])
1721 glDisable(GL_POINT_SPRITE_ARB
);
1722 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1723 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE
), StateTable
);
1725 glColorMask(GL_TRUE
, GL_TRUE
,GL_TRUE
,GL_TRUE
);
1726 checkGLcall("glColorMask");
1727 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_COLORWRITEENABLE
), StateTable
);
1728 if (gl_info
->supported
[EXT_SECONDARY_COLOR
])
1730 glDisable(GL_COLOR_SUM_EXT
);
1731 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SPECULARENABLE
), StateTable
);
1732 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1735 /* Setup transforms */
1736 glMatrixMode(GL_MODELVIEW
);
1737 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1739 checkGLcall("glLoadIdentity()");
1740 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable
);
1742 context
->last_was_rhw
= TRUE
;
1743 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
); /* because of last_was_rhw = TRUE */
1745 glDisable(GL_CLIP_PLANE0
); checkGLcall("glDisable(clip plane 0)");
1746 glDisable(GL_CLIP_PLANE1
); checkGLcall("glDisable(clip plane 1)");
1747 glDisable(GL_CLIP_PLANE2
); checkGLcall("glDisable(clip plane 2)");
1748 glDisable(GL_CLIP_PLANE3
); checkGLcall("glDisable(clip plane 3)");
1749 glDisable(GL_CLIP_PLANE4
); checkGLcall("glDisable(clip plane 4)");
1750 glDisable(GL_CLIP_PLANE5
); checkGLcall("glDisable(clip plane 5)");
1751 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_CLIPPING
), StateTable
);
1753 set_blit_dimension(width
, height
);
1757 context
->blit_w
= width
; context
->blit_h
= height
;
1758 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1759 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1762 This
->frag_pipe
->enable_extension((IWineD3DDevice
*) This
, FALSE
);
1765 /*****************************************************************************
1766 * findThreadContextForSwapChain
1768 * Searches a swapchain for all contexts and picks one for the thread tid.
1769 * If none can be found the swapchain is requested to create a new context
1771 *****************************************************************************/
1772 static struct wined3d_context
*findThreadContextForSwapChain(IWineD3DSwapChain
*swapchain
, DWORD tid
)
1776 for(i
= 0; i
< ((IWineD3DSwapChainImpl
*) swapchain
)->num_contexts
; i
++) {
1777 if(((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
]->tid
== tid
) {
1778 return ((IWineD3DSwapChainImpl
*) swapchain
)->context
[i
];
1783 /* Create a new context for the thread */
1784 return swapchain_create_context_for_thread(swapchain
);
1787 /*****************************************************************************
1790 * Finds a context for the current render target and thread
1793 * target: Render target to find the context for
1794 * tid: Thread to activate the context for
1796 * Returns: The needed context
1798 *****************************************************************************/
1799 static struct wined3d_context
*FindContext(IWineD3DDeviceImpl
*This
, IWineD3DSurface
*target
)
1801 IWineD3DSwapChain
*swapchain
= NULL
;
1802 struct wined3d_context
*current_context
= context_get_current();
1803 const struct StateEntry
*StateTable
= This
->StateTable
;
1804 DWORD tid
= GetCurrentThreadId();
1805 struct wined3d_context
*context
;
1806 BOOL old_render_offscreen
;
1808 if (current_context
&& current_context
->destroyed
) current_context
= NULL
;
1813 && current_context
->current_rt
1814 && current_context
->swapchain
->device
== This
)
1816 target
= current_context
->current_rt
;
1820 IWineD3DSwapChainImpl
*swapchain
= (IWineD3DSwapChainImpl
*)This
->swapchains
[0];
1821 if (swapchain
->backBuffer
) target
= swapchain
->backBuffer
[0];
1822 else target
= swapchain
->frontBuffer
;
1826 if (current_context
&& current_context
->current_rt
== target
)
1828 context_validate(current_context
);
1829 return current_context
;
1832 if (SUCCEEDED(IWineD3DSurface_GetContainer(target
, &IID_IWineD3DSwapChain
, (void **)&swapchain
))) {
1833 TRACE("Rendering onscreen\n");
1835 context
= findThreadContextForSwapChain(swapchain
, tid
);
1837 old_render_offscreen
= context
->render_offscreen
;
1838 context
->render_offscreen
= surface_is_offscreen(target
);
1839 IWineD3DSwapChain_Release(swapchain
);
1843 TRACE("Rendering offscreen\n");
1845 /* Stay with the currently active context. */
1846 if (current_context
&& current_context
->swapchain
->device
== This
)
1848 context
= current_context
;
1852 /* This may happen if the app jumps straight into offscreen rendering
1853 * Start using the context of the primary swapchain. tid == 0 is no problem
1854 * for findThreadContextForSwapChain.
1856 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1857 * is perfect to call. */
1858 context
= findThreadContextForSwapChain(This
->swapchains
[0], tid
);
1861 old_render_offscreen
= context
->render_offscreen
;
1862 context
->render_offscreen
= TRUE
;
1865 context_validate(context
);
1867 if (context
->render_offscreen
!= old_render_offscreen
)
1869 Context_MarkStateDirty(context
, STATE_TRANSFORM(WINED3DTS_PROJECTION
), StateTable
);
1870 Context_MarkStateDirty(context
, STATE_VDECL
, StateTable
);
1871 Context_MarkStateDirty(context
, STATE_VIEWPORT
, StateTable
);
1872 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, StateTable
);
1873 Context_MarkStateDirty(context
, STATE_FRONTFACE
, StateTable
);
1876 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
1877 * the alpha blend state changes with different render target formats. */
1878 if (!context
->current_rt
)
1880 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1884 const struct GlPixelFormatDesc
*old
= ((IWineD3DSurfaceImpl
*)context
->current_rt
)->resource
.format_desc
;
1885 const struct GlPixelFormatDesc
*new = ((IWineD3DSurfaceImpl
*)target
)->resource
.format_desc
;
1887 if (old
->format
!= new->format
)
1889 /* Disable blending when the alpha mask has changed and when a format doesn't support blending. */
1890 if ((old
->alpha_mask
&& !new->alpha_mask
) || (!old
->alpha_mask
&& new->alpha_mask
)
1891 || !(new->Flags
& WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING
))
1893 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), StateTable
);
1897 /* When switching away from an offscreen render target, and we're not
1898 * using FBOs, we have to read the drawable into the texture. This is
1899 * done via PreLoad (and SFLAG_INDRAWABLE set on the surface). There
1900 * are some things that need care though. PreLoad needs a GL context,
1901 * and FindContext is called before the context is activated. It also
1902 * has to be called with the old rendertarget active, otherwise a
1903 * wrong drawable is read. */
1904 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
1905 && old_render_offscreen
&& context
->current_rt
!= target
)
1907 BOOL oldInDraw
= This
->isInDraw
;
1909 /* surface_internal_preload() requires a context to load the
1910 * texture, so it will call context_acquire(). Set isInDraw to true
1911 * to signal surface_internal_preload() that it has a context. */
1913 /* FIXME: This is just broken. There's no guarantee whatsoever
1914 * that the currently active context, if any, is appropriate for
1915 * reading back the render target. We should probably call
1916 * context_set_current(context) here and then rely on
1917 * context_acquire() doing the right thing. */
1918 This
->isInDraw
= TRUE
;
1920 /* Read the back buffer of the old drawable into the destination texture. */
1921 if (((IWineD3DSurfaceImpl
*)context
->current_rt
)->texture_name_srgb
)
1923 surface_internal_preload(context
->current_rt
, SRGB_BOTH
);
1927 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
1930 IWineD3DSurface_ModifyLocation(context
->current_rt
, SFLAG_INDRAWABLE
, FALSE
);
1932 This
->isInDraw
= oldInDraw
;
1936 context
->draw_buffer_dirty
= TRUE
;
1937 context
->current_rt
= target
;
1942 /* Context activation is done by the caller. */
1943 static void context_apply_draw_buffer(struct wined3d_context
*context
, BOOL blit
)
1945 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1946 IWineD3DSurface
*rt
= context
->current_rt
;
1947 IWineD3DDeviceImpl
*device
;
1949 device
= ((IWineD3DSurfaceImpl
*)rt
)->resource
.device
;
1950 if (!surface_is_offscreen(rt
))
1953 glDrawBuffer(surface_get_gl_buffer(rt
));
1954 checkGLcall("glDrawBuffers()");
1960 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
)
1964 if (gl_info
->supported
[ARB_DRAW_BUFFERS
])
1966 GL_EXTCALL(glDrawBuffersARB(gl_info
->limits
.buffers
, device
->draw_buffers
));
1967 checkGLcall("glDrawBuffers()");
1971 glDrawBuffer(device
->draw_buffers
[0]);
1972 checkGLcall("glDrawBuffer()");
1975 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
1976 checkGLcall("glDrawBuffer()");
1981 glDrawBuffer(device
->offscreenBuffer
);
1982 checkGLcall("glDrawBuffer()");
1988 /* GL locking is done by the caller. */
1989 void context_set_draw_buffer(struct wined3d_context
*context
, GLenum buffer
)
1991 glDrawBuffer(buffer
);
1992 checkGLcall("glDrawBuffer()");
1993 context
->draw_buffer_dirty
= TRUE
;
1996 /* Context activation is done by the caller. */
1997 static void context_apply_state(struct wined3d_context
*context
, IWineD3DDeviceImpl
*device
, enum ContextUsage usage
)
1999 const struct StateEntry
*state_table
= device
->StateTable
;
2003 case CTXUSAGE_CLEAR
:
2004 case CTXUSAGE_DRAWPRIM
:
2005 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
2007 context_apply_fbo_state(context
);
2010 if (context
->draw_buffer_dirty
) {
2011 context_apply_draw_buffer(context
, FALSE
);
2012 context
->draw_buffer_dirty
= FALSE
;
2017 if (wined3d_settings
.offscreen_rendering_mode
== ORM_FBO
) {
2018 if (context
->render_offscreen
)
2020 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
2021 surface_internal_preload(context
->current_rt
, SRGB_RGB
);
2024 context_bind_fbo(context
, GL_FRAMEBUFFER
, &context
->dst_fbo
);
2025 context_attach_surface_fbo(context
, GL_FRAMEBUFFER
, 0, context
->current_rt
);
2026 context_attach_depth_stencil_fbo(context
, GL_FRAMEBUFFER
, NULL
, FALSE
);
2030 context_bind_fbo(context
, GL_FRAMEBUFFER
, NULL
);
2033 context
->draw_buffer_dirty
= TRUE
;
2035 if (context
->draw_buffer_dirty
) {
2036 context_apply_draw_buffer(context
, TRUE
);
2037 if (wined3d_settings
.offscreen_rendering_mode
!= ORM_FBO
) {
2038 context
->draw_buffer_dirty
= FALSE
;
2048 case CTXUSAGE_RESOURCELOAD
:
2049 /* This does not require any special states to be set up */
2052 case CTXUSAGE_CLEAR
:
2053 if(context
->last_was_blit
) {
2054 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2057 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
2058 * blending when clearing improves the clearing performance incredibly.
2061 glDisable(GL_BLEND
);
2063 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE
), state_table
);
2066 glEnable(GL_SCISSOR_TEST
);
2067 checkGLcall("glEnable GL_SCISSOR_TEST");
2069 context
->last_was_blit
= FALSE
;
2070 Context_MarkStateDirty(context
, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE
), state_table
);
2071 Context_MarkStateDirty(context
, STATE_SCISSORRECT
, state_table
);
2074 case CTXUSAGE_DRAWPRIM
:
2075 /* This needs all dirty states applied */
2076 if(context
->last_was_blit
) {
2077 device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, TRUE
);
2080 IWineD3DDeviceImpl_FindTexUnitMap(device
);
2081 device_preload_textures(device
);
2082 if (isStateDirty(context
, STATE_VDECL
))
2083 device_update_stream_info(device
, context
->gl_info
);
2086 for (i
= 0; i
< context
->numDirtyEntries
; ++i
)
2088 DWORD rep
= context
->dirtyArray
[i
];
2089 DWORD idx
= rep
/ (sizeof(*context
->isStateDirty
) * CHAR_BIT
);
2090 BYTE shift
= rep
& ((sizeof(*context
->isStateDirty
) * CHAR_BIT
) - 1);
2091 context
->isStateDirty
[idx
] &= ~(1 << shift
);
2092 state_table
[rep
].apply(rep
, device
->stateBlock
, context
);
2095 context
->numDirtyEntries
= 0; /* This makes the whole list clean */
2096 context
->last_was_blit
= FALSE
;
2100 SetupForBlit(device
, context
);
2104 FIXME("Unexpected context usage requested\n");
2108 /*****************************************************************************
2111 * Finds a rendering context and drawable matching the device and render
2112 * target for the current thread, activates them and puts them into the
2116 * This: Device to activate the context for
2117 * target: Requested render target
2118 * usage: Prepares the context for blitting, drawing or other actions
2120 *****************************************************************************/
2121 struct wined3d_context
*context_acquire(IWineD3DDeviceImpl
*device
, IWineD3DSurface
*target
, enum ContextUsage usage
)
2123 struct wined3d_context
*current_context
= context_get_current();
2124 struct wined3d_context
*context
;
2126 TRACE("device %p, target %p, usage %#x.\n", device
, target
, usage
);
2128 context
= FindContext(device
, target
);
2129 context_enter(context
);
2130 if (!context
->valid
) return context
;
2132 if (context
!= current_context
)
2134 if (!context_set_current(context
)) ERR("Failed to activate the new context.\n");
2135 else device
->frag_pipe
->enable_extension((IWineD3DDevice
*)device
, !context
->last_was_blit
);
2137 if (context
->vshader_const_dirty
)
2139 memset(context
->vshader_const_dirty
, 1,
2140 sizeof(*context
->vshader_const_dirty
) * device
->d3d_vshader_constantF
);
2141 device
->highest_dirty_vs_const
= device
->d3d_vshader_constantF
;
2143 if (context
->pshader_const_dirty
)
2145 memset(context
->pshader_const_dirty
, 1,
2146 sizeof(*context
->pshader_const_dirty
) * device
->d3d_pshader_constantF
);
2147 device
->highest_dirty_ps_const
= device
->d3d_pshader_constantF
;
2150 else if (context
->restore_ctx
)
2152 if (!pwglMakeCurrent(context
->hdc
, context
->glCtx
))
2154 DWORD err
= GetLastError();
2155 ERR("Failed to make GL context %p current on device context %p, last error %#x.\n",
2156 context
->hdc
, context
->glCtx
, err
);
2160 context_apply_state(context
, device
, usage
);