VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c@ 19678

Last change on this file since 19678 was 19678, checked in by vboxsync, 16 years ago

opengl: update wine to 1.1.21, add d3d9.dll to build list

  • Property svn:eol-style set to native
File size: 67.8 KB
Line 
1/*
2 * Context and render target management in wined3d
3 *
4 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
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#include "config.h"
31#include <stdio.h>
32#ifdef HAVE_FLOAT_H
33# include <float.h>
34#endif
35#include "wined3d_private.h"
36
37WINE_DEFAULT_DEBUG_CHANNEL(d3d);
38
39#define GLINFO_LOCATION This->adapter->gl_info
40
41/* The last used device.
42 *
43 * If the application creates multiple devices and switches between them, ActivateContext has to
44 * change the opengl context. This flag allows to keep track which device is active
45 */
46static IWineD3DDeviceImpl *last_device;
47
48/* FBO helper functions */
49
50void context_bind_fbo(IWineD3DDevice *iface, GLenum target, GLuint *fbo)
51{
52 const IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
53
54 if (!*fbo)
55 {
56 GL_EXTCALL(glGenFramebuffersEXT(1, fbo));
57 checkGLcall("glGenFramebuffersEXT()");
58 TRACE("Created FBO %d\n", *fbo);
59 }
60
61 GL_EXTCALL(glBindFramebufferEXT(target, *fbo));
62 checkGLcall("glBindFramebuffer()");
63}
64
65static void context_destroy_fbo(IWineD3DDeviceImpl *This, const GLuint *fbo)
66{
67 unsigned int i;
68
69 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo));
70 checkGLcall("glBindFramebuffer()");
71 for (i = 0; i < GL_LIMITS(buffers); ++i)
72 {
73 GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, 0, 0));
74 checkGLcall("glFramebufferTexture2D()");
75 }
76 GL_EXTCALL(glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
77 checkGLcall("glFramebufferTexture2D()");
78 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
79 checkGLcall("glBindFramebuffer()");
80 GL_EXTCALL(glDeleteFramebuffersEXT(1, fbo));
81 checkGLcall("glDeleteFramebuffers()");
82}
83
84static void context_apply_attachment_filter_states(IWineD3DDevice *iface, IWineD3DSurface *surface, BOOL force_preload)
85{
86 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
87 const IWineD3DSurfaceImpl *surface_impl = (IWineD3DSurfaceImpl *)surface;
88 IWineD3DBaseTextureImpl *texture_impl;
89 BOOL update_minfilter = FALSE;
90 BOOL update_magfilter = FALSE;
91
92 /* Update base texture states array */
93 if (SUCCEEDED(IWineD3DSurface_GetContainer(surface, &IID_IWineD3DBaseTexture, (void **)&texture_impl)))
94 {
95 if (texture_impl->baseTexture.states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_POINT
96 || texture_impl->baseTexture.states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_NONE)
97 {
98 texture_impl->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
99 texture_impl->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
100 update_minfilter = TRUE;
101 }
102
103 if (texture_impl->baseTexture.states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_POINT)
104 {
105 texture_impl->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
106 update_magfilter = TRUE;
107 }
108
109 if (texture_impl->baseTexture.bindCount)
110 {
111 WARN("Render targets should not be bound to a sampler\n");
112 IWineD3DDeviceImpl_MarkStateDirty(This, STATE_SAMPLER(texture_impl->baseTexture.sampler));
113 }
114
115 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *)texture_impl);
116 }
117
118 if (update_minfilter || update_magfilter || force_preload)
119 {
120 GLenum target, bind_target;
121 GLint old_binding;
122
123 target = surface_impl->glDescription.target;
124 if (target == GL_TEXTURE_2D)
125 {
126 bind_target = GL_TEXTURE_2D;
127 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
128 } else if (target == GL_TEXTURE_RECTANGLE_ARB) {
129 bind_target = GL_TEXTURE_RECTANGLE_ARB;
130 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
131 } else {
132 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
133 glGetIntegerv(GL_TEXTURE_BINDING_CUBE_MAP_ARB, &old_binding);
134 }
135
136 surface_internal_preload(surface, SRGB_RGB);
137
138 glBindTexture(bind_target, surface_impl->glDescription.textureName);
139 if (update_minfilter) glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
140 if (update_magfilter) glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
141 glBindTexture(bind_target, old_binding);
142 }
143
144 checkGLcall("apply_attachment_filter_states()");
145}
146
147/* TODO: Handle stencil attachments */
148void context_attach_depth_stencil_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, IWineD3DSurface *depth_stencil, BOOL use_render_buffer)
149{
150 IWineD3DSurfaceImpl *depth_stencil_impl = (IWineD3DSurfaceImpl *)depth_stencil;
151
152 TRACE("Attach depth stencil %p\n", depth_stencil);
153
154 if (depth_stencil)
155 {
156 if (use_render_buffer && depth_stencil_impl->current_renderbuffer)
157 {
158 GL_EXTCALL(glFramebufferRenderbufferEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depth_stencil_impl->current_renderbuffer->id));
159 checkGLcall("glFramebufferRenderbufferEXT()");
160 } else {
161 context_apply_attachment_filter_states((IWineD3DDevice *)This, depth_stencil, TRUE);
162
163 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, depth_stencil_impl->glDescription.target,
164 depth_stencil_impl->glDescription.textureName, depth_stencil_impl->glDescription.level));
165 checkGLcall("glFramebufferTexture2DEXT()");
166 }
167 } else {
168 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, 0, 0));
169 checkGLcall("glFramebufferTexture2DEXT()");
170 }
171}
172
173void context_attach_surface_fbo(IWineD3DDeviceImpl *This, GLenum fbo_target, DWORD idx, IWineD3DSurface *surface)
174{
175 const IWineD3DSurfaceImpl *surface_impl = (IWineD3DSurfaceImpl *)surface;
176
177 TRACE("Attach surface %p to %u\n", surface, idx);
178
179 if (surface)
180 {
181 context_apply_attachment_filter_states((IWineD3DDevice *)This, surface, TRUE);
182
183 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, surface_impl->glDescription.target,
184 surface_impl->glDescription.textureName, surface_impl->glDescription.level));
185 checkGLcall("glFramebufferTexture2DEXT()");
186 } else {
187 GL_EXTCALL(glFramebufferTexture2DEXT(fbo_target, GL_COLOR_ATTACHMENT0_EXT + idx, GL_TEXTURE_2D, 0, 0));
188 checkGLcall("glFramebufferTexture2DEXT()");
189 }
190}
191
192static void context_check_fbo_status(IWineD3DDevice *iface)
193{
194 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
195 GLenum status;
196
197 status = GL_EXTCALL(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
198 if (status == GL_FRAMEBUFFER_COMPLETE_EXT)
199 {
200 TRACE("FBO complete\n");
201 } else {
202 IWineD3DSurfaceImpl *attachment;
203 unsigned int i;
204 FIXME("FBO status %s (%#x)\n", debug_fbostatus(status), status);
205
206 /* Dump the FBO attachments */
207 for (i = 0; i < GL_LIMITS(buffers); ++i)
208 {
209 attachment = (IWineD3DSurfaceImpl *)This->activeContext->current_fbo->render_targets[i];
210 if (attachment)
211 {
212 FIXME("\tColor attachment %d: (%p) %s %ux%u\n",
213 i, attachment, debug_d3dformat(attachment->resource.format_desc->format),
214 attachment->pow2Width, attachment->pow2Height);
215 }
216 }
217 attachment = (IWineD3DSurfaceImpl *)This->activeContext->current_fbo->depth_stencil;
218 if (attachment)
219 {
220 FIXME("\tDepth attachment: (%p) %s %ux%u\n",
221 attachment, debug_d3dformat(attachment->resource.format_desc->format),
222 attachment->pow2Width, attachment->pow2Height);
223 }
224 }
225}
226
227static struct fbo_entry *context_create_fbo_entry(IWineD3DDevice *iface)
228{
229 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
230 struct fbo_entry *entry;
231
232 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry));
233 entry->render_targets = HeapAlloc(GetProcessHeap(), 0, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
234 memcpy(entry->render_targets, This->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets));
235 entry->depth_stencil = This->stencilBufferTarget;
236 entry->attached = FALSE;
237 entry->id = 0;
238
239 return entry;
240}
241
242static void context_destroy_fbo_entry(IWineD3DDeviceImpl *This, struct fbo_entry *entry)
243{
244 if (entry->id)
245 {
246 TRACE("Destroy FBO %d\n", entry->id);
247 context_destroy_fbo(This, &entry->id);
248 }
249 list_remove(&entry->entry);
250 HeapFree(GetProcessHeap(), 0, entry->render_targets);
251 HeapFree(GetProcessHeap(), 0, entry);
252}
253
254
255static struct fbo_entry *context_find_fbo_entry(IWineD3DDevice *iface, WineD3DContext *context)
256{
257 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
258 struct fbo_entry *entry;
259
260 LIST_FOR_EACH_ENTRY(entry, &context->fbo_list, struct fbo_entry, entry)
261 {
262 if (!memcmp(entry->render_targets, This->render_targets, GL_LIMITS(buffers) * sizeof(*entry->render_targets))
263 && entry->depth_stencil == This->stencilBufferTarget)
264 {
265 return entry;
266 }
267 }
268
269 entry = context_create_fbo_entry(iface);
270 list_add_head(&context->fbo_list, &entry->entry);
271 return entry;
272}
273
274static void context_apply_fbo_entry(IWineD3DDevice *iface, struct fbo_entry *entry)
275{
276 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
277 unsigned int i;
278
279 context_bind_fbo(iface, GL_FRAMEBUFFER_EXT, &entry->id);
280
281 if (!entry->attached)
282 {
283 /* Apply render targets */
284 for (i = 0; i < GL_LIMITS(buffers); ++i)
285 {
286 IWineD3DSurface *render_target = This->render_targets[i];
287 context_attach_surface_fbo(This, GL_FRAMEBUFFER_EXT, i, render_target);
288 }
289
290 /* Apply depth targets */
291 if (This->stencilBufferTarget) {
292 unsigned int w = ((IWineD3DSurfaceImpl *)This->render_targets[0])->pow2Width;
293 unsigned int h = ((IWineD3DSurfaceImpl *)This->render_targets[0])->pow2Height;
294
295 surface_set_compatible_renderbuffer(This->stencilBufferTarget, w, h);
296 }
297 context_attach_depth_stencil_fbo(This, GL_FRAMEBUFFER_EXT, This->stencilBufferTarget, TRUE);
298
299 entry->attached = TRUE;
300 } else {
301 for (i = 0; i < GL_LIMITS(buffers); ++i)
302 {
303 if (This->render_targets[i])
304 context_apply_attachment_filter_states(iface, This->render_targets[i], FALSE);
305 }
306 if (This->stencilBufferTarget)
307 context_apply_attachment_filter_states(iface, This->stencilBufferTarget, FALSE);
308 }
309
310 for (i = 0; i < GL_LIMITS(buffers); ++i)
311 {
312 if (This->render_targets[i])
313 This->draw_buffers[i] = GL_COLOR_ATTACHMENT0_EXT + i;
314 else
315 This->draw_buffers[i] = GL_NONE;
316 }
317}
318
319static void context_apply_fbo_state(IWineD3DDevice *iface)
320{
321 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
322 WineD3DContext *context = This->activeContext;
323
324 if (This->render_offscreen)
325 {
326 context->current_fbo = context_find_fbo_entry(iface, context);
327 context_apply_fbo_entry(iface, context->current_fbo);
328 } else {
329 context->current_fbo = NULL;
330 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
331 }
332
333 context_check_fbo_status(iface);
334}
335
336void context_resource_released(IWineD3DDevice *iface, IWineD3DResource *resource, WINED3DRESOURCETYPE type)
337{
338 IWineD3DDeviceImpl *This = (IWineD3DDeviceImpl *)iface;
339 UINT i;
340
341 switch(type)
342 {
343 case WINED3DRTYPE_SURFACE:
344 {
345 for (i = 0; i < This->numContexts; ++i)
346 {
347 struct fbo_entry *entry, *entry2;
348
349 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->contexts[i]->fbo_list, struct fbo_entry, entry)
350 {
351 BOOL destroyed = FALSE;
352 UINT j;
353
354 for (j = 0; !destroyed && j < GL_LIMITS(buffers); ++j)
355 {
356 if (entry->render_targets[j] == (IWineD3DSurface *)resource)
357 {
358 context_destroy_fbo_entry(This, entry);
359 destroyed = TRUE;
360 }
361 }
362
363 if (!destroyed && entry->depth_stencil == (IWineD3DSurface *)resource)
364 context_destroy_fbo_entry(This, entry);
365 }
366 }
367
368 break;
369 }
370
371 default:
372 break;
373 }
374}
375
376/*****************************************************************************
377 * Context_MarkStateDirty
378 *
379 * Marks a state in a context dirty. Only one context, opposed to
380 * IWineD3DDeviceImpl_MarkStateDirty, which marks the state dirty in all
381 * contexts
382 *
383 * Params:
384 * context: Context to mark the state dirty in
385 * state: State to mark dirty
386 * StateTable: Pointer to the state table in use(for state grouping)
387 *
388 *****************************************************************************/
389static void Context_MarkStateDirty(WineD3DContext *context, DWORD state, const struct StateEntry *StateTable) {
390 DWORD rep = StateTable[state].representative;
391 DWORD idx;
392 BYTE shift;
393
394 if(!rep || isStateDirty(context, rep)) return;
395
396 context->dirtyArray[context->numDirtyEntries++] = rep;
397 idx = rep >> 5;
398 shift = rep & 0x1f;
399 context->isStateDirty[idx] |= (1 << shift);
400}
401
402/*****************************************************************************
403 * AddContextToArray
404 *
405 * Adds a context to the context array. Helper function for CreateContext
406 *
407 * This method is not called in performance-critical code paths, only when a
408 * new render target or swapchain is created. Thus performance is not an issue
409 * here.
410 *
411 * Params:
412 * This: Device to add the context for
413 * hdc: device context
414 * glCtx: WGL context to add
415 * pbuffer: optional pbuffer used with this context
416 *
417 *****************************************************************************/
418static WineD3DContext *AddContextToArray(IWineD3DDeviceImpl *This, HWND win_handle, HDC hdc, HGLRC glCtx, HPBUFFERARB pbuffer) {
419 WineD3DContext **oldArray = This->contexts;
420 DWORD state;
421
422 This->contexts = HeapAlloc(GetProcessHeap(), 0, sizeof(*This->contexts) * (This->numContexts + 1));
423 if(This->contexts == NULL) {
424 ERR("Unable to grow the context array\n");
425 This->contexts = oldArray;
426 return NULL;
427 }
428 if(oldArray) {
429 memcpy(This->contexts, oldArray, sizeof(*This->contexts) * This->numContexts);
430 }
431
432 This->contexts[This->numContexts] = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WineD3DContext));
433 if(This->contexts[This->numContexts] == NULL) {
434 ERR("Unable to allocate a new context\n");
435 HeapFree(GetProcessHeap(), 0, This->contexts);
436 This->contexts = oldArray;
437 return NULL;
438 }
439
440 This->contexts[This->numContexts]->hdc = hdc;
441 This->contexts[This->numContexts]->glCtx = glCtx;
442 This->contexts[This->numContexts]->pbuffer = pbuffer;
443 This->contexts[This->numContexts]->win_handle = win_handle;
444 HeapFree(GetProcessHeap(), 0, oldArray);
445
446 /* Mark all states dirty to force a proper initialization of the states on the first use of the context
447 */
448 for(state = 0; state <= STATE_HIGHEST; state++) {
449 Context_MarkStateDirty(This->contexts[This->numContexts], state, This->StateTable);
450 }
451
452 This->numContexts++;
453 TRACE("Created context %p\n", This->contexts[This->numContexts - 1]);
454 return This->contexts[This->numContexts - 1];
455}
456
457/* This function takes care of WineD3D pixel format selection. */
458static int WineD3D_ChoosePixelFormat(IWineD3DDeviceImpl *This, HDC hdc,
459 const struct GlPixelFormatDesc *color_format_desc, const struct GlPixelFormatDesc *ds_format_desc,
460 BOOL auxBuffers, int numSamples, BOOL pbuffer, BOOL findCompatible)
461{
462 int iPixelFormat=0;
463 unsigned int matchtry;
464 short redBits, greenBits, blueBits, alphaBits, colorBits;
465 short depthBits=0, stencilBits=0;
466
467 struct match_type {
468 BOOL require_aux;
469 BOOL exact_alpha;
470 BOOL exact_color;
471 } matches[] = {
472 /* First, try without alpha match buffers. MacOS supports aux buffers only
473 * on A8R8G8B8, and we prefer better offscreen rendering over an alpha match.
474 * Then try without aux buffers - this is the most common cause for not
475 * finding a pixel format. Also some drivers(the open source ones)
476 * only offer 32 bit ARB pixel formats. First try without an exact alpha
477 * match, then try without an exact alpha and color match.
478 */
479 { TRUE, TRUE, TRUE },
480 { TRUE, FALSE, TRUE },
481 { FALSE, TRUE, TRUE },
482 { FALSE, FALSE, TRUE },
483 { TRUE, FALSE, FALSE },
484 { FALSE, FALSE, FALSE },
485 };
486
487 int i = 0;
488 int nCfgs = This->adapter->nCfgs;
489
490 TRACE("ColorFormat=%s, DepthStencilFormat=%s, auxBuffers=%d, numSamples=%d, pbuffer=%d, findCompatible=%d\n",
491 debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format),
492 auxBuffers, numSamples, pbuffer, findCompatible);
493
494 if (!getColorBits(color_format_desc, &redBits, &greenBits, &blueBits, &alphaBits, &colorBits))
495 {
496 ERR("Unable to get color bits for format %s (%#x)!\n",
497 debug_d3dformat(color_format_desc->format), color_format_desc->format);
498 return 0;
499 }
500
501 /* In WGL both color, depth and stencil are features of a pixel format. In case of D3D they are separate.
502 * You are able to add a depth + stencil surface at a later stage when you need it.
503 * In order to support this properly in WineD3D we need the ability to recreate the opengl context and
504 * drawable when this is required. This is very tricky as we need to reapply ALL opengl states for the new
505 * context, need torecreate shaders, textures and other resources.
506 *
507 * The context manager already takes care of the state problem and for the other tasks code from Reset
508 * can be used. These changes are way to risky during the 1.0 code freeze which is taking place right now.
509 * Likely a lot of other new bugs will be exposed. For that reason request a depth stencil surface all the
510 * time. It can cause a slight performance hit but fixes a lot of regressions. A fixme reminds of that this
511 * issue needs to be fixed. */
512 if (ds_format_desc->format != WINED3DFMT_D24S8)
513 {
514 FIXME("Add OpenGL context recreation support to SetDepthStencilSurface\n");
515 ds_format_desc = getFormatDescEntry(WINED3DFMT_D24S8, &This->adapter->gl_info);
516 }
517
518 getDepthStencilBits(ds_format_desc, &depthBits, &stencilBits);
519
520 for(matchtry = 0; matchtry < (sizeof(matches) / sizeof(matches[0])) && !iPixelFormat; matchtry++) {
521 for(i=0; i<nCfgs; i++) {
522 BOOL exactDepthMatch = TRUE;
523 WineD3D_PixelFormat *cfg = &This->adapter->cfgs[i];
524
525 /* For now only accept RGBA formats. Perhaps some day we will
526 * allow floating point formats for pbuffers. */
527 if(cfg->iPixelType != WGL_TYPE_RGBA_ARB)
528 continue;
529
530 /* In window mode (!pbuffer) we need a window drawable format and double buffering. */
531 if(!pbuffer && !(cfg->windowDrawable && cfg->doubleBuffer))
532 continue;
533
534 /* We like to have aux buffers in backbuffer mode */
535 if(auxBuffers && !cfg->auxBuffers && matches[matchtry].require_aux)
536 continue;
537
538 /* In pbuffer-mode we need a pbuffer-capable format but we don't want double buffering */
539 if(pbuffer && (!cfg->pbufferDrawable || cfg->doubleBuffer))
540 continue;
541
542 if(matches[matchtry].exact_color) {
543 if(cfg->redSize != redBits)
544 continue;
545 if(cfg->greenSize != greenBits)
546 continue;
547 if(cfg->blueSize != blueBits)
548 continue;
549 } else {
550 if(cfg->redSize < redBits)
551 continue;
552 if(cfg->greenSize < greenBits)
553 continue;
554 if(cfg->blueSize < blueBits)
555 continue;
556 }
557 if(matches[matchtry].exact_alpha) {
558 if(cfg->alphaSize != alphaBits)
559 continue;
560 } else {
561 if(cfg->alphaSize < alphaBits)
562 continue;
563 }
564
565 /* We try to locate a format which matches our requirements exactly. In case of
566 * depth it is no problem to emulate 16-bit using e.g. 24-bit, so accept that. */
567 if(cfg->depthSize < depthBits)
568 continue;
569 else if(cfg->depthSize > depthBits)
570 exactDepthMatch = FALSE;
571
572 /* In all cases make sure the number of stencil bits matches our requirements
573 * even when we don't need stencil because it could affect performance EXCEPT
574 * on cards which don't offer depth formats without stencil like the i915 drivers
575 * on Linux. */
576 if(stencilBits != cfg->stencilSize && !(This->adapter->brokenStencil && stencilBits <= cfg->stencilSize))
577 continue;
578
579 /* Check multisampling support */
580 if(cfg->numSamples != numSamples)
581 continue;
582
583 /* When we have passed all the checks then we have found a format which matches our
584 * requirements. Note that we only check for a limit number of capabilities right now,
585 * so there can easily be a dozen of pixel formats which appear to be the 'same' but
586 * can still differ in things like multisampling, stereo, SRGB and other flags.
587 */
588
589 /* Exit the loop as we have found a format :) */
590 if(exactDepthMatch) {
591 iPixelFormat = cfg->iPixelFormat;
592 break;
593 } else if(!iPixelFormat) {
594 /* In the end we might end up with a format which doesn't exactly match our depth
595 * requirements. Accept the first format we found because formats with higher iPixelFormat
596 * values tend to have more extended capabilities (e.g. multisampling) which we don't need. */
597 iPixelFormat = cfg->iPixelFormat;
598 }
599 }
600 }
601
602 /* When findCompatible is set and no suitable format was found, let ChoosePixelFormat choose a pixel format in order not to crash. */
603 if(!iPixelFormat && !findCompatible) {
604 ERR("Can't find a suitable iPixelFormat\n");
605 return FALSE;
606 } else if(!iPixelFormat) {
607 PIXELFORMATDESCRIPTOR pfd;
608
609 TRACE("Falling back to ChoosePixelFormat as we weren't able to find an exactly matching pixel format\n");
610 /* PixelFormat selection */
611 ZeroMemory(&pfd, sizeof(pfd));
612 pfd.nSize = sizeof(pfd);
613 pfd.nVersion = 1;
614 pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;/*PFD_GENERIC_ACCELERATED*/
615 pfd.iPixelType = PFD_TYPE_RGBA;
616 pfd.cAlphaBits = alphaBits;
617 pfd.cColorBits = colorBits;
618 pfd.cDepthBits = depthBits;
619 pfd.cStencilBits = stencilBits;
620 pfd.iLayerType = PFD_MAIN_PLANE;
621
622 iPixelFormat = ChoosePixelFormat(hdc, &pfd);
623 if(!iPixelFormat) {
624 /* If this happens something is very wrong as ChoosePixelFormat barely fails */
625 ERR("Can't find a suitable iPixelFormat\n");
626 return FALSE;
627 }
628 }
629
630 TRACE("Found iPixelFormat=%d for ColorFormat=%s, DepthStencilFormat=%s\n",
631 iPixelFormat, debug_d3dformat(color_format_desc->format), debug_d3dformat(ds_format_desc->format));
632 return iPixelFormat;
633}
634
635/*****************************************************************************
636 * CreateContext
637 *
638 * Creates a new context for a window, or a pbuffer context.
639 *
640 * * Params:
641 * This: Device to activate the context for
642 * target: Surface this context will render to
643 * win_handle: handle to the window which we are drawing to
644 * create_pbuffer: tells whether to create a pbuffer or not
645 * pPresentParameters: contains the pixelformats to use for onscreen rendering
646 *
647 *****************************************************************************/
648WineD3DContext *CreateContext(IWineD3DDeviceImpl *This, IWineD3DSurfaceImpl *target, HWND win_handle, BOOL create_pbuffer, const WINED3DPRESENT_PARAMETERS *pPresentParms) {
649 HDC oldDrawable, hdc;
650 HPBUFFERARB pbuffer = NULL;
651 HGLRC ctx = NULL, oldCtx;
652 WineD3DContext *ret = NULL;
653 unsigned int s;
654
655 TRACE("(%p): Creating a %s context for render target %p\n", This, create_pbuffer ? "offscreen" : "onscreen", target);
656
657 if(create_pbuffer) {
658 HDC hdc_parent = GetDC(win_handle);
659 int iPixelFormat = 0;
660
661 IWineD3DSurface *StencilSurface = This->stencilBufferTarget;
662 const struct GlPixelFormatDesc *ds_format_desc = StencilSurface
663 ? ((IWineD3DSurfaceImpl *)StencilSurface)->resource.format_desc
664 : getFormatDescEntry(WINED3DFMT_UNKNOWN, &This->adapter->gl_info);
665
666 /* Try to find a pixel format with pbuffer support. */
667 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format_desc,
668 ds_format_desc, FALSE /* auxBuffers */, 0 /* numSamples */, TRUE /* PBUFFER */,
669 FALSE /* findCompatible */);
670 if(!iPixelFormat) {
671 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
672
673 /* For some reason we weren't able to find a format, try to find something instead of crashing.
674 * A reason for failure could have been wglChoosePixelFormatARB strictness. */
675 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc_parent, target->resource.format_desc,
676 ds_format_desc, FALSE /* auxBuffer */, 0 /* numSamples */, TRUE /* PBUFFER */,
677 TRUE /* findCompatible */);
678 }
679
680 /* This shouldn't happen as ChoosePixelFormat always returns something */
681 if(!iPixelFormat) {
682 ERR("Unable to locate a pixel format for a pbuffer\n");
683 ReleaseDC(win_handle, hdc_parent);
684 goto out;
685 }
686
687 TRACE("Creating a pBuffer drawable for the new context\n");
688 pbuffer = GL_EXTCALL(wglCreatePbufferARB(hdc_parent, iPixelFormat, target->currentDesc.Width, target->currentDesc.Height, 0));
689 if(!pbuffer) {
690 ERR("Cannot create a pbuffer\n");
691 ReleaseDC(win_handle, hdc_parent);
692 goto out;
693 }
694
695 /* In WGL a pbuffer is 'wrapped' inside a HDC to 'fool' wglMakeCurrent */
696 hdc = GL_EXTCALL(wglGetPbufferDCARB(pbuffer));
697 if(!hdc) {
698 ERR("Cannot get a HDC for pbuffer (%p)\n", pbuffer);
699 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
700 ReleaseDC(win_handle, hdc_parent);
701 goto out;
702 }
703 ReleaseDC(win_handle, hdc_parent);
704 } else {
705 PIXELFORMATDESCRIPTOR pfd;
706 int iPixelFormat;
707 int res;
708 const struct GlPixelFormatDesc *color_format_desc = target->resource.format_desc;
709 const struct GlPixelFormatDesc *ds_format_desc = getFormatDescEntry(WINED3DFMT_UNKNOWN,
710 &This->adapter->gl_info);
711 BOOL auxBuffers = FALSE;
712 int numSamples = 0;
713
714 hdc = GetDC(win_handle);
715 if(hdc == NULL) {
716 ERR("Cannot retrieve a device context!\n");
717 goto out;
718 }
719
720 /* In case of ORM_BACKBUFFER, make sure to request an alpha component for X4R4G4B4/X8R8G8B8 as we might need it for the backbuffer. */
721 if(wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER) {
722 auxBuffers = TRUE;
723
724 if (color_format_desc->format == WINED3DFMT_X4R4G4B4)
725 color_format_desc = getFormatDescEntry(WINED3DFMT_A4R4G4B4, &This->adapter->gl_info);
726 else if (color_format_desc->format == WINED3DFMT_X8R8G8B8)
727 color_format_desc = getFormatDescEntry(WINED3DFMT_A8R8G8B8, &This->adapter->gl_info);
728 }
729
730 /* DirectDraw supports 8bit paletted render targets and these are used by old games like Starcraft and C&C.
731 * Most modern hardware doesn't support 8bit natively so we perform some form of 8bit -> 32bit conversion.
732 * The conversion (ab)uses the alpha component for storing the palette index. For this reason we require
733 * a format with 8bit alpha, so request A8R8G8B8. */
734 if (color_format_desc->format == WINED3DFMT_P8)
735 color_format_desc = getFormatDescEntry(WINED3DFMT_A8R8G8B8, &This->adapter->gl_info);
736
737 /* Retrieve the depth stencil format from the present parameters.
738 * The choice of the proper format can give a nice performance boost
739 * in case of GPU limited programs. */
740 if(pPresentParms->EnableAutoDepthStencil) {
741 TRACE("pPresentParms->EnableAutoDepthStencil=enabled; using AutoDepthStencilFormat=%s\n", debug_d3dformat(pPresentParms->AutoDepthStencilFormat));
742 ds_format_desc = getFormatDescEntry(pPresentParms->AutoDepthStencilFormat, &This->adapter->gl_info);
743 }
744
745 /* D3D only allows multisampling when SwapEffect is set to WINED3DSWAPEFFECT_DISCARD */
746 if(pPresentParms->MultiSampleType && (pPresentParms->SwapEffect == WINED3DSWAPEFFECT_DISCARD)) {
747 if(!GL_SUPPORT(ARB_MULTISAMPLE))
748 ERR("The program is requesting multisampling without support!\n");
749 else {
750 ERR("Requesting MultiSampleType=%d\n", pPresentParms->MultiSampleType);
751 numSamples = pPresentParms->MultiSampleType;
752 }
753 }
754
755 /* Try to find a pixel format which matches our requirements */
756 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, color_format_desc, ds_format_desc,
757 auxBuffers, numSamples, FALSE /* PBUFFER */, FALSE /* findCompatible */);
758
759 /* Try to locate a compatible format if we weren't able to find anything */
760 if(!iPixelFormat) {
761 TRACE("Trying to locate a compatible pixel format because an exact match failed.\n");
762 iPixelFormat = WineD3D_ChoosePixelFormat(This, hdc, color_format_desc, ds_format_desc,
763 auxBuffers, 0 /* numSamples */, FALSE /* PBUFFER */, TRUE /* findCompatible */ );
764 }
765
766 /* If we still don't have a pixel format, something is very wrong as ChoosePixelFormat barely fails */
767 if(!iPixelFormat) {
768 ERR("Can't find a suitable iPixelFormat\n");
769 return FALSE;
770 }
771
772 DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd);
773 res = SetPixelFormat(hdc, iPixelFormat, NULL);
774 if(!res) {
775 int oldPixelFormat = GetPixelFormat(hdc);
776
777 /* By default WGL doesn't allow pixel format adjustments but we need it here.
778 * For this reason there is a WINE-specific wglSetPixelFormat which allows you to
779 * set the pixel format multiple times. Only use it when it is really needed. */
780
781 if(oldPixelFormat == iPixelFormat) {
782 /* We don't have to do anything as the formats are the same :) */
783 } else if(oldPixelFormat && GL_SUPPORT(WGL_WINE_PIXEL_FORMAT_PASSTHROUGH)) {
784 res = GL_EXTCALL(wglSetPixelFormatWINE(hdc, iPixelFormat, NULL));
785
786 if(!res) {
787 ERR("wglSetPixelFormatWINE failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
788 return FALSE;
789 }
790 } else if(oldPixelFormat) {
791 /* OpenGL doesn't allow pixel format adjustments. Print an error and continue using the old format.
792 * There's a big chance that the old format works although with a performance hit and perhaps rendering errors. */
793 ERR("HDC=%p is already set to iPixelFormat=%d and OpenGL doesn't allow changes!\n", hdc, oldPixelFormat);
794 } else {
795 ERR("SetPixelFormat failed on HDC=%p for iPixelFormat=%d\n", hdc, iPixelFormat);
796 return FALSE;
797 }
798 }
799 }
800
801 ctx = pwglCreateContext(hdc);
802 if(This->numContexts) pwglShareLists(This->contexts[0]->glCtx, ctx);
803
804 if(!ctx) {
805 ERR("Failed to create a WGL context\n");
806 if(create_pbuffer) {
807 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
808 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
809 }
810 goto out;
811 }
812 ret = AddContextToArray(This, win_handle, hdc, ctx, pbuffer);
813 if(!ret) {
814 ERR("Failed to add the newly created context to the context list\n");
815 pwglDeleteContext(ctx);
816 if(create_pbuffer) {
817 GL_EXTCALL(wglReleasePbufferDCARB(pbuffer, hdc));
818 GL_EXTCALL(wglDestroyPbufferARB(pbuffer));
819 }
820 goto out;
821 }
822 ret->surface = (IWineD3DSurface *) target;
823 ret->isPBuffer = create_pbuffer;
824 ret->tid = GetCurrentThreadId();
825 if(This->shader_backend->shader_dirtifyable_constants((IWineD3DDevice *) This)) {
826 /* Create the dirty constants array and initialize them to dirty */
827 ret->vshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
828 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
829 ret->pshader_const_dirty = HeapAlloc(GetProcessHeap(), 0,
830 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
831 memset(ret->vshader_const_dirty, 1,
832 sizeof(*ret->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
833 memset(ret->pshader_const_dirty, 1,
834 sizeof(*ret->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
835 }
836
837 TRACE("Successfully created new context %p\n", ret);
838
839 list_init(&ret->fbo_list);
840
841 /* Set up the context defaults */
842 oldCtx = pwglGetCurrentContext();
843 oldDrawable = pwglGetCurrentDC();
844 if(oldCtx && oldDrawable) {
845 /* See comment in ActivateContext context switching */
846 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
847 }
848 if(pwglMakeCurrent(hdc, ctx) == FALSE) {
849 ERR("Cannot activate context to set up defaults\n");
850 goto out;
851 }
852
853 ENTER_GL();
854
855 glGetIntegerv(GL_AUX_BUFFERS, &ret->aux_buffers);
856
857 TRACE("Setting up the screen\n");
858 /* Clear the screen */
859 glClearColor(1.0, 0.0, 0.0, 0.0);
860 checkGLcall("glClearColor");
861 glClearIndex(0);
862 glClearDepth(1);
863 glClearStencil(0xffff);
864
865 checkGLcall("glClear");
866
867 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
868 checkGLcall("glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);");
869
870 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
871 checkGLcall("glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);");
872
873 glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
874 checkGLcall("glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);");
875
876 glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);
877 checkGLcall("glPixelStorei(GL_PACK_ALIGNMENT, This->surface_alignment);");
878 glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);
879 checkGLcall("glPixelStorei(GL_UNPACK_ALIGNMENT, This->surface_alignment);");
880
881 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
882 /* Most textures will use client storage if supported. Exceptions are non-native power of 2 textures
883 * and textures in DIB sections(due to the memory protection).
884 */
885 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
886 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
887 }
888 if(GL_SUPPORT(ARB_VERTEX_BLEND)) {
889 /* Direct3D always uses n-1 weights for n world matrices and uses 1 - sum for the last one
890 * this is equal to GL_WEIGHT_SUM_UNITY_ARB. Enabling it doesn't do anything unless
891 * GL_VERTEX_BLEND_ARB isn't enabled too
892 */
893 glEnable(GL_WEIGHT_SUM_UNITY_ARB);
894 checkGLcall("glEnable(GL_WEIGHT_SUM_UNITY_ARB)");
895 }
896 if(GL_SUPPORT(NV_TEXTURE_SHADER2)) {
897 /* Set up the previous texture input for all shader units. This applies to bump mapping, and in d3d
898 * the previous texture where to source the offset from is always unit - 1.
899 */
900 for(s = 1; s < GL_LIMITS(textures); s++) {
901 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
902 glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB + s - 1);
903 checkGLcall("glTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, ...\n");
904 }
905 }
906
907 for(s = 0; s < GL_LIMITS(point_sprite_units); s++) {
908 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + s));
909 glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
910 checkGLcall("glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE)\n");
911 }
912 LEAVE_GL();
913
914 /* Never keep GL_FRAGMENT_SHADER_ATI enabled on a context that we switch away from,
915 * but enable it for the first context we create, and reenable it on the old context
916 */
917 if(oldDrawable && oldCtx) {
918 pwglMakeCurrent(oldDrawable, oldCtx);
919 } else {
920 last_device = This;
921 }
922 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
923
924 return ret;
925
926out:
927 return NULL;
928}
929
930/*****************************************************************************
931 * RemoveContextFromArray
932 *
933 * Removes a context from the context manager. The opengl context is not
934 * destroyed or unset. context is not a valid pointer after that call.
935 *
936 * Similar to the former call this isn't a performance critical function. A
937 * helper function for DestroyContext.
938 *
939 * Params:
940 * This: Device to activate the context for
941 * context: Context to remove
942 *
943 *****************************************************************************/
944static void RemoveContextFromArray(IWineD3DDeviceImpl *This, WineD3DContext *context) {
945 WineD3DContext **new_array;
946 BOOL found = FALSE;
947 UINT i;
948
949 TRACE("Removing ctx %p\n", context);
950
951 for (i = 0; i < This->numContexts; ++i)
952 {
953 if (This->contexts[i] == context)
954 {
955 HeapFree(GetProcessHeap(), 0, context);
956 found = TRUE;
957 break;
958 }
959 }
960
961 if (!found)
962 {
963 ERR("Context %p doesn't exist in context array\n", context);
964 return;
965 }
966
967 while (i < This->numContexts - 1)
968 {
969 This->contexts[i] = This->contexts[i + 1];
970 ++i;
971 }
972
973 --This->numContexts;
974 if (!This->numContexts)
975 {
976 HeapFree(GetProcessHeap(), 0, This->contexts);
977 This->contexts = NULL;
978 return;
979 }
980
981 new_array = HeapReAlloc(GetProcessHeap(), 0, This->contexts, This->numContexts * sizeof(*This->contexts));
982 if (!new_array)
983 {
984 ERR("Failed to shrink context array. Oh well.\n");
985 return;
986 }
987
988 This->contexts = new_array;
989}
990
991/*****************************************************************************
992 * DestroyContext
993 *
994 * Destroys a wineD3DContext
995 *
996 * Params:
997 * This: Device to activate the context for
998 * context: Context to destroy
999 *
1000 *****************************************************************************/
1001void DestroyContext(IWineD3DDeviceImpl *This, WineD3DContext *context) {
1002 struct fbo_entry *entry, *entry2;
1003
1004 TRACE("Destroying ctx %p\n", context);
1005
1006 /* The correct GL context needs to be active to cleanup the GL resources below */
1007 if(pwglGetCurrentContext() != context->glCtx){
1008 pwglMakeCurrent(context->hdc, context->glCtx);
1009 last_device = NULL;
1010 }
1011
1012 ENTER_GL();
1013
1014 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &context->fbo_list, struct fbo_entry, entry) {
1015 context_destroy_fbo_entry(This, entry);
1016 }
1017 if (context->src_fbo) {
1018 TRACE("Destroy src FBO %d\n", context->src_fbo);
1019 context_destroy_fbo(This, &context->src_fbo);
1020 }
1021 if (context->dst_fbo) {
1022 TRACE("Destroy dst FBO %d\n", context->dst_fbo);
1023 context_destroy_fbo(This, &context->dst_fbo);
1024 }
1025
1026 LEAVE_GL();
1027
1028 if (This->activeContext == context)
1029 {
1030 This->activeContext = NULL;
1031 TRACE("Destroying the active context.\n");
1032 }
1033
1034 /* Cleanup the GL context */
1035 pwglMakeCurrent(NULL, NULL);
1036 if(context->isPBuffer) {
1037 GL_EXTCALL(wglReleasePbufferDCARB(context->pbuffer, context->hdc));
1038 GL_EXTCALL(wglDestroyPbufferARB(context->pbuffer));
1039 } else ReleaseDC(context->win_handle, context->hdc);
1040 pwglDeleteContext(context->glCtx);
1041
1042 HeapFree(GetProcessHeap(), 0, context->vshader_const_dirty);
1043 HeapFree(GetProcessHeap(), 0, context->pshader_const_dirty);
1044 RemoveContextFromArray(This, context);
1045}
1046
1047static inline void set_blit_dimension(UINT width, UINT height) {
1048 glMatrixMode(GL_PROJECTION);
1049 checkGLcall("glMatrixMode(GL_PROJECTION)");
1050 glLoadIdentity();
1051 checkGLcall("glLoadIdentity()");
1052 glOrtho(0, width, height, 0, 0.0, -1.0);
1053 checkGLcall("glOrtho");
1054 glViewport(0, 0, width, height);
1055 checkGLcall("glViewport");
1056}
1057
1058/*****************************************************************************
1059 * SetupForBlit
1060 *
1061 * Sets up a context for DirectDraw blitting.
1062 * All texture units are disabled, texture unit 0 is set as current unit
1063 * fog, lighting, blending, alpha test, z test, scissor test, culling disabled
1064 * color writing enabled for all channels
1065 * register combiners disabled, shaders disabled
1066 * world matrix is set to identity, texture matrix 0 too
1067 * projection matrix is setup for drawing screen coordinates
1068 *
1069 * Params:
1070 * This: Device to activate the context for
1071 * context: Context to setup
1072 * width: render target width
1073 * height: render target height
1074 *
1075 *****************************************************************************/
1076static inline void SetupForBlit(IWineD3DDeviceImpl *This, WineD3DContext *context, UINT width, UINT height) {
1077 int i, sampler;
1078 const struct StateEntry *StateTable = This->StateTable;
1079
1080 TRACE("Setting up context %p for blitting\n", context);
1081 if(context->last_was_blit) {
1082 if(context->blit_w != width || context->blit_h != height) {
1083 set_blit_dimension(width, height);
1084 context->blit_w = width; context->blit_h = height;
1085 /* No need to dirtify here, the states are still dirtified because they weren't
1086 * applied since the last SetupForBlit call. Otherwise last_was_blit would not
1087 * be set
1088 */
1089 }
1090 TRACE("Context is already set up for blitting, nothing to do\n");
1091 return;
1092 }
1093 context->last_was_blit = TRUE;
1094
1095 /* TODO: Use a display list */
1096
1097 /* Disable shaders */
1098 This->shader_backend->shader_select((IWineD3DDevice *)This, FALSE, FALSE);
1099 Context_MarkStateDirty(context, STATE_VSHADER, StateTable);
1100 Context_MarkStateDirty(context, STATE_PIXELSHADER, StateTable);
1101
1102 /* Call ENTER_GL() once for all gl calls below. In theory we should not call
1103 * helper functions in between gl calls. This function is full of Context_MarkStateDirty
1104 * which can safely be called from here, we only lock once instead locking/unlocking
1105 * after each GL call.
1106 */
1107 ENTER_GL();
1108
1109 /* Disable all textures. The caller can then bind a texture it wants to blit
1110 * from
1111 *
1112 * The blitting code uses (for now) the fixed function pipeline, so make sure to reset all fixed
1113 * function texture unit. No need to care for higher samplers
1114 */
1115 for(i = GL_LIMITS(textures) - 1; i > 0 ; i--) {
1116 sampler = This->rev_tex_unit_map[i];
1117 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB + i));
1118 checkGLcall("glActiveTextureARB");
1119
1120 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1121 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1122 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1123 }
1124 glDisable(GL_TEXTURE_3D);
1125 checkGLcall("glDisable GL_TEXTURE_3D");
1126 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
1127 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1128 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1129 }
1130 glDisable(GL_TEXTURE_2D);
1131 checkGLcall("glDisable GL_TEXTURE_2D");
1132
1133 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1134 checkGLcall("glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);");
1135
1136 if (sampler != -1) {
1137 if (sampler < MAX_TEXTURES) {
1138 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1139 }
1140 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1141 }
1142 }
1143 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
1144 checkGLcall("glActiveTextureARB");
1145
1146 sampler = This->rev_tex_unit_map[0];
1147
1148 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
1149 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
1150 checkGLcall("glDisable GL_TEXTURE_CUBE_MAP_ARB");
1151 }
1152 glDisable(GL_TEXTURE_3D);
1153 checkGLcall("glDisable GL_TEXTURE_3D");
1154 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
1155 glDisable(GL_TEXTURE_RECTANGLE_ARB);
1156 checkGLcall("glDisable GL_TEXTURE_RECTANGLE_ARB");
1157 }
1158 glDisable(GL_TEXTURE_2D);
1159 checkGLcall("glDisable GL_TEXTURE_2D");
1160
1161 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1162
1163 glMatrixMode(GL_TEXTURE);
1164 checkGLcall("glMatrixMode(GL_TEXTURE)");
1165 glLoadIdentity();
1166 checkGLcall("glLoadIdentity()");
1167
1168 if (GL_SUPPORT(EXT_TEXTURE_LOD_BIAS)) {
1169 glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1170 GL_TEXTURE_LOD_BIAS_EXT,
1171 0.0);
1172 checkGLcall("glTexEnvi GL_TEXTURE_LOD_BIAS_EXT ...");
1173 }
1174
1175 if (sampler != -1) {
1176 if (sampler < MAX_TEXTURES) {
1177 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_TEXTURE0 + sampler), StateTable);
1178 Context_MarkStateDirty(context, STATE_TEXTURESTAGE(sampler, WINED3DTSS_COLOROP), StateTable);
1179 }
1180 Context_MarkStateDirty(context, STATE_SAMPLER(sampler), StateTable);
1181 }
1182
1183 /* Other misc states */
1184 glDisable(GL_ALPHA_TEST);
1185 checkGLcall("glDisable(GL_ALPHA_TEST)");
1186 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHATESTENABLE), StateTable);
1187 glDisable(GL_LIGHTING);
1188 checkGLcall("glDisable GL_LIGHTING");
1189 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_LIGHTING), StateTable);
1190 glDisable(GL_DEPTH_TEST);
1191 checkGLcall("glDisable GL_DEPTH_TEST");
1192 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ZENABLE), StateTable);
1193 glDisableWINE(GL_FOG);
1194 checkGLcall("glDisable GL_FOG");
1195 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_FOGENABLE), StateTable);
1196 glDisable(GL_BLEND);
1197 checkGLcall("glDisable GL_BLEND");
1198 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1199 glDisable(GL_CULL_FACE);
1200 checkGLcall("glDisable GL_CULL_FACE");
1201 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CULLMODE), StateTable);
1202 glDisable(GL_STENCIL_TEST);
1203 checkGLcall("glDisable GL_STENCIL_TEST");
1204 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_STENCILENABLE), StateTable);
1205 glDisable(GL_SCISSOR_TEST);
1206 checkGLcall("glDisable GL_SCISSOR_TEST");
1207 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1208 if(GL_SUPPORT(ARB_POINT_SPRITE)) {
1209 glDisable(GL_POINT_SPRITE_ARB);
1210 checkGLcall("glDisable GL_POINT_SPRITE_ARB");
1211 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_POINTSPRITEENABLE), StateTable);
1212 }
1213 glColorMask(GL_TRUE, GL_TRUE,GL_TRUE,GL_TRUE);
1214 checkGLcall("glColorMask");
1215 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1216 if (GL_SUPPORT(EXT_SECONDARY_COLOR)) {
1217 glDisable(GL_COLOR_SUM_EXT);
1218 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SPECULARENABLE), StateTable);
1219 checkGLcall("glDisable(GL_COLOR_SUM_EXT)");
1220 }
1221
1222 /* Setup transforms */
1223 glMatrixMode(GL_MODELVIEW);
1224 checkGLcall("glMatrixMode(GL_MODELVIEW)");
1225 glLoadIdentity();
1226 checkGLcall("glLoadIdentity()");
1227 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_WORLDMATRIX(0)), StateTable);
1228
1229 context->last_was_rhw = TRUE;
1230 Context_MarkStateDirty(context, STATE_VDECL, StateTable); /* because of last_was_rhw = TRUE */
1231
1232 glDisable(GL_CLIP_PLANE0); checkGLcall("glDisable(clip plane 0)");
1233 glDisable(GL_CLIP_PLANE1); checkGLcall("glDisable(clip plane 1)");
1234 glDisable(GL_CLIP_PLANE2); checkGLcall("glDisable(clip plane 2)");
1235 glDisable(GL_CLIP_PLANE3); checkGLcall("glDisable(clip plane 3)");
1236 glDisable(GL_CLIP_PLANE4); checkGLcall("glDisable(clip plane 4)");
1237 glDisable(GL_CLIP_PLANE5); checkGLcall("glDisable(clip plane 5)");
1238 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_CLIPPING), StateTable);
1239 LEAVE_GL();
1240
1241 set_blit_dimension(width, height);
1242 context->blit_w = width; context->blit_h = height;
1243 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1244 Context_MarkStateDirty(context, STATE_TRANSFORM(WINED3DTS_PROJECTION), StateTable);
1245
1246
1247 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1248}
1249
1250/*****************************************************************************
1251 * findThreadContextForSwapChain
1252 *
1253 * Searches a swapchain for all contexts and picks one for the thread tid.
1254 * If none can be found the swapchain is requested to create a new context
1255 *
1256 *****************************************************************************/
1257static WineD3DContext *findThreadContextForSwapChain(IWineD3DSwapChain *swapchain, DWORD tid) {
1258 unsigned int i;
1259
1260 for(i = 0; i < ((IWineD3DSwapChainImpl *) swapchain)->num_contexts; i++) {
1261 if(((IWineD3DSwapChainImpl *) swapchain)->context[i]->tid == tid) {
1262 return ((IWineD3DSwapChainImpl *) swapchain)->context[i];
1263 }
1264
1265 }
1266
1267 /* Create a new context for the thread */
1268 return IWineD3DSwapChainImpl_CreateContextForThread(swapchain);
1269}
1270
1271/*****************************************************************************
1272 * FindContext
1273 *
1274 * Finds a context for the current render target and thread
1275 *
1276 * Parameters:
1277 * target: Render target to find the context for
1278 * tid: Thread to activate the context for
1279 *
1280 * Returns: The needed context
1281 *
1282 *****************************************************************************/
1283static inline WineD3DContext *FindContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, DWORD tid) {
1284 IWineD3DSwapChain *swapchain = NULL;
1285 BOOL readTexture = wined3d_settings.offscreen_rendering_mode != ORM_FBO && This->render_offscreen;
1286 WineD3DContext *context = This->activeContext;
1287 BOOL oldRenderOffscreen = This->render_offscreen;
1288 const struct GlPixelFormatDesc *old = ((IWineD3DSurfaceImpl *)This->lastActiveRenderTarget)->resource.format_desc;
1289 const struct GlPixelFormatDesc *new = ((IWineD3DSurfaceImpl *)target)->resource.format_desc;
1290 const struct StateEntry *StateTable = This->StateTable;
1291
1292 /* To compensate the lack of format switching with some offscreen rendering methods and on onscreen buffers
1293 * the alpha blend state changes with different render target formats
1294 */
1295 if (old->format != new->format)
1296 {
1297 /* Disable blending when the alpha mask has changed and when a format doesn't support blending */
1298 if ((old->alpha_mask && !new->alpha_mask) || (!old->alpha_mask && new->alpha_mask)
1299 || !(new->Flags & WINED3DFMT_FLAG_POSTPIXELSHADER_BLENDING))
1300 {
1301 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1302 }
1303 }
1304
1305 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
1306 TRACE("Rendering onscreen\n");
1307
1308 context = findThreadContextForSwapChain(swapchain, tid);
1309
1310 This->render_offscreen = FALSE;
1311 /* The context != This->activeContext will catch a NOP context change. This can occur
1312 * if we are switching back to swapchain rendering in case of FBO or Back Buffer offscreen
1313 * rendering. No context change is needed in that case
1314 */
1315
1316 if(wined3d_settings.offscreen_rendering_mode == ORM_PBUFFER) {
1317 if(This->pbufferContext && tid == This->pbufferContext->tid) {
1318 This->pbufferContext->tid = 0;
1319 }
1320 }
1321 IWineD3DSwapChain_Release(swapchain);
1322
1323 if(oldRenderOffscreen) {
1324 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
1325 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
1326 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1327 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1328 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
1329 }
1330
1331 } else {
1332 TRACE("Rendering offscreen\n");
1333 This->render_offscreen = TRUE;
1334
1335 switch(wined3d_settings.offscreen_rendering_mode) {
1336 case ORM_FBO:
1337 /* FBOs do not need a different context. Stay with whatever context is active at the moment */
1338 if(This->activeContext && tid == This->lastThread) {
1339 context = This->activeContext;
1340 } else {
1341 /* This may happen if the app jumps straight into offscreen rendering
1342 * Start using the context of the primary swapchain. tid == 0 is no problem
1343 * for findThreadContextForSwapChain.
1344 *
1345 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1346 * is perfect to call.
1347 */
1348 context = findThreadContextForSwapChain(This->swapchains[0], tid);
1349 }
1350 break;
1351
1352 case ORM_PBUFFER:
1353 {
1354 IWineD3DSurfaceImpl *targetimpl = (IWineD3DSurfaceImpl *) target;
1355 if(This->pbufferContext == NULL ||
1356 This->pbufferWidth < targetimpl->currentDesc.Width ||
1357 This->pbufferHeight < targetimpl->currentDesc.Height) {
1358 if(This->pbufferContext) {
1359 DestroyContext(This, This->pbufferContext);
1360 }
1361
1362 /* The display is irrelevant here, the window is 0. But CreateContext needs a valid X connection.
1363 * Create the context on the same server as the primary swapchain. The primary swapchain is exists at this point.
1364 */
1365 This->pbufferContext = CreateContext(This, targetimpl,
1366 ((IWineD3DSwapChainImpl *) This->swapchains[0])->context[0]->win_handle,
1367 TRUE /* pbuffer */, &((IWineD3DSwapChainImpl *)This->swapchains[0])->presentParms);
1368 This->pbufferWidth = targetimpl->currentDesc.Width;
1369 This->pbufferHeight = targetimpl->currentDesc.Height;
1370 }
1371
1372 if(This->pbufferContext) {
1373 if(This->pbufferContext->tid != 0 && This->pbufferContext->tid != tid) {
1374 FIXME("The PBuffr context is only supported for one thread for now!\n");
1375 }
1376 This->pbufferContext->tid = tid;
1377 context = This->pbufferContext;
1378 break;
1379 } else {
1380 ERR("Failed to create a buffer context and drawable, falling back to back buffer offscreen rendering\n");
1381 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
1382 }
1383 }
1384
1385 case ORM_BACKBUFFER:
1386 /* Stay with the currently active context for back buffer rendering */
1387 if(This->activeContext && tid == This->lastThread) {
1388 context = This->activeContext;
1389 } else {
1390 /* This may happen if the app jumps straight into offscreen rendering
1391 * Start using the context of the primary swapchain. tid == 0 is no problem
1392 * for findThreadContextForSwapChain.
1393 *
1394 * Can also happen on thread switches - in that case findThreadContextForSwapChain
1395 * is perfect to call.
1396 */
1397 context = findThreadContextForSwapChain(This->swapchains[0], tid);
1398 }
1399 break;
1400 }
1401
1402 if(!oldRenderOffscreen) {
1403 Context_MarkStateDirty(context, WINED3DTS_PROJECTION, StateTable);
1404 Context_MarkStateDirty(context, STATE_VDECL, StateTable);
1405 Context_MarkStateDirty(context, STATE_VIEWPORT, StateTable);
1406 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1407 Context_MarkStateDirty(context, STATE_FRONTFACE, StateTable);
1408 }
1409 }
1410
1411 /* When switching away from an offscreen render target, and we're not using FBOs,
1412 * we have to read the drawable into the texture. This is done via PreLoad(and
1413 * SFLAG_INDRAWABLE set on the surface). There are some things that need care though.
1414 * PreLoad needs a GL context, and FindContext is called before the context is activated.
1415 * It also has to be called with the old rendertarget active, otherwise a wrong drawable
1416 * is read. This leads to these possible situations:
1417 *
1418 * 0) lastActiveRenderTarget == target && oldTid == newTid:
1419 * Nothing to do, we don't even reach this code in this case...
1420 *
1421 * 1) lastActiveRenderTarget != target && oldTid == newTid:
1422 * The currently active context is OK for readback. Call PreLoad, and it
1423 * performs the read
1424 *
1425 * 2) lastActiveRenderTarget == target && oldTid != newTid:
1426 * Nothing to do - the drawable is unchanged
1427 *
1428 * 3) lastActiveRenderTarget != target && oldTid != newTid:
1429 * This is tricky. We have to get a context with the old drawable from somewhere
1430 * before we can switch to the new context. In this case, PreLoad calls
1431 * ActivateContext(lastActiveRenderTarget) from the new(current) thread. This
1432 * is case (2) then. The old drawable is activated for the new thread, and the
1433 * readback can be done. The recursed ActivateContext does *not* call PreLoad again.
1434 * After that, the outer ActivateContext(which calls PreLoad) can activate the new
1435 * target for the new thread
1436 */
1437 if (readTexture && This->lastActiveRenderTarget != target) {
1438 BOOL oldInDraw = This->isInDraw;
1439
1440 /* PreLoad requires a context to load the texture, thus it will call ActivateContext.
1441 * Set the isInDraw to true to signal PreLoad that it has a context. Will be tricky
1442 * when using offscreen rendering with multithreading
1443 */
1444 This->isInDraw = TRUE;
1445
1446 /* Do that before switching the context:
1447 * Read the back buffer of the old drawable into the destination texture
1448 */
1449 if(((IWineD3DSurfaceImpl *)This->lastActiveRenderTarget)->glDescription.srgbTextureName) {
1450 surface_internal_preload(This->lastActiveRenderTarget, SRGB_BOTH);
1451 } else {
1452 surface_internal_preload(This->lastActiveRenderTarget, SRGB_RGB);
1453 }
1454
1455 /* Assume that the drawable will be modified by some other things now */
1456 IWineD3DSurface_ModifyLocation(This->lastActiveRenderTarget, SFLAG_INDRAWABLE, FALSE);
1457
1458 This->isInDraw = oldInDraw;
1459 }
1460
1461 return context;
1462}
1463
1464static void apply_draw_buffer(IWineD3DDeviceImpl *This, IWineD3DSurface *target, BOOL blit)
1465{
1466 IWineD3DSwapChain *swapchain;
1467
1468 if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain)))
1469 {
1470 IWineD3DSwapChain_Release((IUnknown *)swapchain);
1471 ENTER_GL();
1472 glDrawBuffer(surface_get_gl_buffer(target, swapchain));
1473 checkGLcall("glDrawBuffers()");
1474 LEAVE_GL();
1475 }
1476 else
1477 {
1478 ENTER_GL();
1479 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
1480 {
1481 if (!blit)
1482 {
1483 if (GL_SUPPORT(ARB_DRAW_BUFFERS))
1484 {
1485 GL_EXTCALL(glDrawBuffersARB(GL_LIMITS(buffers), This->draw_buffers));
1486 checkGLcall("glDrawBuffers()");
1487 }
1488 else
1489 {
1490 glDrawBuffer(This->draw_buffers[0]);
1491 checkGLcall("glDrawBuffer()");
1492 }
1493 } else {
1494 glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
1495 checkGLcall("glDrawBuffer()");
1496 }
1497 }
1498 else
1499 {
1500 glDrawBuffer(This->offscreenBuffer);
1501 checkGLcall("glDrawBuffer()");
1502 }
1503 LEAVE_GL();
1504 }
1505}
1506
1507/*****************************************************************************
1508 * ActivateContext
1509 *
1510 * Finds a rendering context and drawable matching the device and render
1511 * target for the current thread, activates them and puts them into the
1512 * requested state.
1513 *
1514 * Params:
1515 * This: Device to activate the context for
1516 * target: Requested render target
1517 * usage: Prepares the context for blitting, drawing or other actions
1518 *
1519 *****************************************************************************/
1520void ActivateContext(IWineD3DDeviceImpl *This, IWineD3DSurface *target, ContextUsage usage) {
1521 DWORD tid = GetCurrentThreadId();
1522 DWORD i, dirtyState, idx;
1523 BYTE shift;
1524 WineD3DContext *context;
1525 const struct StateEntry *StateTable = This->StateTable;
1526
1527 TRACE("(%p): Selecting context for render target %p, thread %d\n", This, target, tid);
1528 if(This->lastActiveRenderTarget != target || tid != This->lastThread) {
1529 context = FindContext(This, target, tid);
1530 context->draw_buffer_dirty = TRUE;
1531 This->lastActiveRenderTarget = target;
1532 This->lastThread = tid;
1533 } else {
1534 /* Stick to the old context */
1535 context = This->activeContext;
1536 }
1537
1538 /* Activate the opengl context */
1539 if(last_device != This || context != This->activeContext) {
1540 BOOL ret;
1541
1542 /* Prevent an unneeded context switch as those are expensive */
1543 if(context->glCtx && (context->glCtx == pwglGetCurrentContext())) {
1544 TRACE("Already using gl context %p\n", context->glCtx);
1545 }
1546 else {
1547 TRACE("Switching gl ctx to %p, hdc=%p ctx=%p\n", context, context->hdc, context->glCtx);
1548
1549 ret = pwglMakeCurrent(context->hdc, context->glCtx);
1550 if(ret == FALSE) {
1551 ERR("Failed to activate the new context\n");
1552 } else if(!context->last_was_blit) {
1553 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1554 } else {
1555 This->frag_pipe->enable_extension((IWineD3DDevice *) This, FALSE);
1556 }
1557 }
1558 if(This->activeContext->vshader_const_dirty) {
1559 memset(This->activeContext->vshader_const_dirty, 1,
1560 sizeof(*This->activeContext->vshader_const_dirty) * GL_LIMITS(vshader_constantsF));
1561 }
1562 if(This->activeContext->pshader_const_dirty) {
1563 memset(This->activeContext->pshader_const_dirty, 1,
1564 sizeof(*This->activeContext->pshader_const_dirty) * GL_LIMITS(pshader_constantsF));
1565 }
1566 This->activeContext = context;
1567 last_device = This;
1568 }
1569
1570 switch (usage) {
1571 case CTXUSAGE_CLEAR:
1572 case CTXUSAGE_DRAWPRIM:
1573 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1574 context_apply_fbo_state((IWineD3DDevice *)This);
1575 }
1576 if (context->draw_buffer_dirty) {
1577 apply_draw_buffer(This, target, FALSE);
1578 context->draw_buffer_dirty = FALSE;
1579 }
1580 break;
1581
1582 case CTXUSAGE_BLIT:
1583 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
1584 if (This->render_offscreen) {
1585 FIXME("Activating for CTXUSAGE_BLIT for an offscreen target with ORM_FBO. This should be avoided.\n");
1586 context_bind_fbo((IWineD3DDevice *)This, GL_FRAMEBUFFER_EXT, &context->dst_fbo);
1587 context_attach_surface_fbo(This, GL_FRAMEBUFFER_EXT, 0, target);
1588
1589 ENTER_GL();
1590 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, 0));
1591 checkGLcall("glFramebufferRenderbufferEXT");
1592 LEAVE_GL();
1593 } else {
1594 ENTER_GL();
1595 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
1596 checkGLcall("glFramebufferRenderbufferEXT");
1597 LEAVE_GL();
1598 }
1599 context->draw_buffer_dirty = TRUE;
1600 }
1601 if (context->draw_buffer_dirty) {
1602 apply_draw_buffer(This, target, TRUE);
1603 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
1604 context->draw_buffer_dirty = FALSE;
1605 }
1606 }
1607 break;
1608
1609 default:
1610 break;
1611 }
1612
1613 switch(usage) {
1614 case CTXUSAGE_RESOURCELOAD:
1615 /* This does not require any special states to be set up */
1616 break;
1617
1618 case CTXUSAGE_CLEAR:
1619 if(context->last_was_blit) {
1620 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1621 }
1622
1623 /* Blending and clearing should be orthogonal, but tests on the nvidia driver show that disabling
1624 * blending when clearing improves the clearing performance incredibly.
1625 */
1626 ENTER_GL();
1627 glDisable(GL_BLEND);
1628 LEAVE_GL();
1629 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_ALPHABLENDENABLE), StateTable);
1630
1631 ENTER_GL();
1632 glEnable(GL_SCISSOR_TEST);
1633 checkGLcall("glEnable GL_SCISSOR_TEST");
1634 LEAVE_GL();
1635 context->last_was_blit = FALSE;
1636 Context_MarkStateDirty(context, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE), StateTable);
1637 Context_MarkStateDirty(context, STATE_SCISSORRECT, StateTable);
1638 break;
1639
1640 case CTXUSAGE_DRAWPRIM:
1641 /* This needs all dirty states applied */
1642 if(context->last_was_blit) {
1643 This->frag_pipe->enable_extension((IWineD3DDevice *) This, TRUE);
1644 }
1645
1646 IWineD3DDeviceImpl_FindTexUnitMap(This);
1647
1648 for(i=0; i < context->numDirtyEntries; i++) {
1649 dirtyState = context->dirtyArray[i];
1650 idx = dirtyState >> 5;
1651 shift = dirtyState & 0x1f;
1652 context->isStateDirty[idx] &= ~(1 << shift);
1653 StateTable[dirtyState].apply(dirtyState, This->stateBlock, context);
1654 }
1655 context->numDirtyEntries = 0; /* This makes the whole list clean */
1656 context->last_was_blit = FALSE;
1657 break;
1658
1659 case CTXUSAGE_BLIT:
1660 SetupForBlit(This, context,
1661 ((IWineD3DSurfaceImpl *)target)->currentDesc.Width,
1662 ((IWineD3DSurfaceImpl *)target)->currentDesc.Height);
1663 break;
1664
1665 default:
1666 FIXME("Unexpected context usage requested\n");
1667 }
1668}
1669
1670WineD3DContext *getActiveContext(void) {
1671 return last_device->activeContext;
1672}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette