2 * IWineD3DSurface Implementation
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2007 Stefan Dösinger for CodeWeavers
11 * Copyright 2007 Henri Verbeet
12 * Copyright 2006-2007 Roderick Colenbrander
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
36 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
37 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey);
39 static void surface_download_data(IWineD3DSurfaceImpl *This) {
40 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
42 if (0 == This->glDescription.textureName) {
43 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
47 if(myDevice->createParms.BehaviorFlags & WINED3DCREATE_MULTITHREADED) {
48 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
50 if(This->Flags & SFLAG_CONVERTED) {
51 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
56 /* Make sure that a proper texture unit is selected, bind the texture
57 * and dirtify the sampler to restore the texture on the next draw
59 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
60 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
61 checkGLcall("glActiveTextureARB");
63 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(0));
64 IWineD3DSurface_BindTexture((IWineD3DSurface *) This);
66 if (This->resource.format == WINED3DFMT_DXT1 ||
67 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
68 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
69 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
70 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
72 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
73 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
75 if(This->Flags & SFLAG_PBO) {
76 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
77 checkGLcall("glBindBufferARB");
78 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
79 checkGLcall("glGetCompressedTexImageARB()");
80 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
81 checkGLcall("glBindBufferARB");
83 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
84 checkGLcall("glGetCompressedTexImageARB()");
93 if (This->Flags & SFLAG_NONPOW2) {
94 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
95 src_pitch = This->bytesPerPixel * This->pow2Width;
96 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
97 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
98 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
100 mem = This->resource.allocatedMemory;
103 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
104 This->glDescription.glFormat, This->glDescription.glType, mem);
106 if(This->Flags & SFLAG_PBO) {
107 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
108 checkGLcall("glBindBufferARB");
110 glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
111 This->glDescription.glType, NULL);
112 checkGLcall("glGetTexImage()");
114 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
115 checkGLcall("glBindBufferARB");
117 glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
118 This->glDescription.glType, mem);
119 checkGLcall("glGetTexImage()");
123 if (This->Flags & SFLAG_NONPOW2) {
124 LPBYTE src_data, dst_data;
127 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
128 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
129 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
131 * We're doing this...
133 * instead of boxing the texture :
134 * |<-texture width ->| -->pow2width| /\
135 * |111111111111111111| | |
136 * |222 Texture 222222| boxed empty | texture height
137 * |3333 Data 33333333| | |
138 * |444444444444444444| | \/
139 * ----------------------------------- |
140 * | boxed empty | boxed empty | pow2height
142 * -----------------------------------
145 * we're repacking the data to the expected texture width
147 * |<-texture width ->| -->pow2width| /\
148 * |111111111111111111222222222222222| |
149 * |222333333333333333333444444444444| texture height
153 * | empty | pow2height
155 * -----------------------------------
159 * |<-texture width ->| /\
160 * |111111111111111111|
161 * |222222222222222222|texture height
162 * |333333333333333333|
163 * |444444444444444444| \/
164 * --------------------
166 * this also means that any references to allocatedMemory should work with the data as if were a
167 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
169 * internally the texture is still stored in a boxed format so any references to textureName will
170 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
172 * Performance should not be an issue, because applications normally do not lock the surfaces when
173 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
174 * and doesn't have to be re-read.
177 dst_data = This->resource.allocatedMemory;
178 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
179 for (y = 1 ; y < This->currentDesc.Height; y++) {
180 /* skip the first row */
181 src_data += src_pitch;
182 dst_data += dst_pitch;
183 memcpy(dst_data, src_data, dst_pitch);
186 HeapFree(GetProcessHeap(), 0, mem);
190 /* Surface has now been downloaded */
191 This->Flags |= SFLAG_INSYSMEM;
194 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
195 if (This->resource.format == WINED3DFMT_DXT1 ||
196 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
197 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
198 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
199 FIXME("Using DXT1/3/5 without advertized support\n");
201 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
202 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
203 * function uses glCompressedTexImage2D instead of the SubImage call
205 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
208 if(This->Flags & SFLAG_PBO) {
209 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
210 checkGLcall("glBindBufferARB");
211 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
213 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
214 width, height, 0 /* border */, This->resource.size, NULL));
215 checkGLcall("glCompressedTexSubImage2D");
217 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
218 checkGLcall("glBindBufferARB");
220 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
221 width, height, 0 /* border */, This->resource.size, data));
222 checkGLcall("glCompressedTexSubImage2D");
227 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
230 if(This->Flags & SFLAG_PBO) {
231 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
232 checkGLcall("glBindBufferARB");
233 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
235 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
236 checkGLcall("glTexSubImage2D");
238 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
239 checkGLcall("glBindBufferARB");
242 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
243 checkGLcall("glTexSubImage2D");
250 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
251 BOOL enable_client_storage = FALSE;
254 TRACE("(%p) : Creating surface (target %#x) level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n", This,
255 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
257 if (This->resource.format == WINED3DFMT_DXT1 ||
258 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
259 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
260 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
261 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
263 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
264 * once, unfortunately
266 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
267 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
268 This->Flags |= SFLAG_CLIENT;
269 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
270 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
271 width, height, 0 /* border */, This->resource.size, mem));
279 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
280 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
281 /* In some cases we want to disable client storage.
282 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
283 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
284 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
285 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
286 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
288 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
289 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
290 This->Flags &= ~SFLAG_CLIENT;
291 enable_client_storage = TRUE;
293 This->Flags |= SFLAG_CLIENT;
295 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
296 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
298 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
301 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
302 checkGLcall("glTexImage2D");
304 if(enable_client_storage) {
305 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
306 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
310 This->Flags |= SFLAG_ALLOCATED;
313 /* In D3D the depth stencil dimensions have to be greater than or equal to the
314 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
315 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
316 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
317 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
318 renderbuffer_entry_t *entry;
319 GLuint renderbuffer = 0;
320 unsigned int src_width, src_height;
322 src_width = This->pow2Width;
323 src_height = This->pow2Height;
325 /* A depth stencil smaller than the render target is not valid */
326 if (width > src_width || height > src_height) return;
328 /* Remove any renderbuffer set if the sizes match */
329 if (width == src_width && height == src_height) {
330 This->current_renderbuffer = NULL;
334 /* Look if we've already got a renderbuffer of the correct dimensions */
335 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
336 if (entry->width == width && entry->height == height) {
337 renderbuffer = entry->id;
338 This->current_renderbuffer = entry;
344 const GlPixelFormatDesc *glDesc;
345 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
347 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
348 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
349 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
351 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
352 entry->width = width;
353 entry->height = height;
354 entry->id = renderbuffer;
355 list_add_head(&This->renderbuffers, &entry->entry);
357 This->current_renderbuffer = entry;
360 checkGLcall("set_compatible_renderbuffer");
363 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
364 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
365 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
367 TRACE("(%p) : swapchain %p\n", This, swapchain);
369 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
370 TRACE("Returning GL_BACK\n");
372 } else if (swapchain_impl->frontBuffer == iface) {
373 TRACE("Returning GL_FRONT\n");
377 FIXME("Higher back buffer, returning GL_BACK\n");
381 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
382 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
383 ULONG ref = InterlockedDecrement(&This->resource.ref);
384 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
386 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
387 renderbuffer_entry_t *entry, *entry2;
388 TRACE("(%p) : cleaning up\n", This);
390 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
392 /* Need a context to destroy the texture. Use the currently active render target, but only if
393 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
394 * When destroying the primary rt, Uninit3D will activate a context before doing anything
396 if(device->render_targets && device->render_targets[0]) {
397 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
400 TRACE("Deleting texture %d\n", This->glDescription.textureName);
402 glDeleteTextures(1, &This->glDescription.textureName);
406 if(This->Flags & SFLAG_PBO) {
408 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
411 if(This->Flags & SFLAG_DIBSECTION) {
413 SelectObject(This->hDC, This->dib.holdbitmap);
415 /* Release the DIB section */
416 DeleteObject(This->dib.DIBsection);
417 This->dib.bitmap_data = NULL;
418 This->resource.allocatedMemory = NULL;
420 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
422 HeapFree(GetProcessHeap(), 0, This->palette9);
424 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
425 if(iface == device->ddraw_primary)
426 device->ddraw_primary = NULL;
428 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
429 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
430 HeapFree(GetProcessHeap(), 0, entry);
433 TRACE("(%p) Released\n", This);
434 HeapFree(GetProcessHeap(), 0, This);
440 /* ****************************************************
441 IWineD3DSurface IWineD3DResource parts follow
442 **************************************************** */
444 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
445 /* TODO: check for locks */
446 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
447 IWineD3DBaseTexture *baseTexture = NULL;
448 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
450 TRACE("(%p)Checking to see if the container is a base texture\n", This);
451 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
452 TRACE("Passing to container\n");
453 IWineD3DBaseTexture_PreLoad(baseTexture);
454 IWineD3DBaseTexture_Release(baseTexture);
456 TRACE("(%p) : About to load surface\n", This);
458 if(!device->isInDraw) {
459 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
463 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
464 if (!This->glDescription.level) {
465 if (!This->glDescription.textureName) {
466 glGenTextures(1, &This->glDescription.textureName);
467 checkGLcall("glGenTextures");
468 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
470 glBindTexture(This->glDescription.target, This->glDescription.textureName);
471 checkGLcall("glBindTexture");
472 IWineD3DSurface_LoadTexture(iface, FALSE);
473 /* This is where we should be reducing the amount of GLMemoryUsed */
474 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
475 /* assume this is a coding error not a real error for now */
476 FIXME("Mipmap surface has a glTexture bound to it!\n");
478 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
479 /* Tell opengl to try and keep this texture in video ram (well mostly) */
482 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
489 /* ******************************************************
490 IWineD3DSurface IWineD3DSurface parts follow
491 ****************************************************** */
493 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
494 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
495 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
496 if (This->glDescription.textureName == 0 && textureName != 0) {
497 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
498 IWineD3DSurface_AddDirtyRect(iface, NULL);
500 This->glDescription.textureName = textureName;
501 This->glDescription.target = target;
502 This->Flags &= ~SFLAG_ALLOCATED;
505 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
506 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
507 TRACE("(%p) : returning %p\n", This, &This->glDescription);
508 *glDescription = &This->glDescription;
511 /* TODO: think about moving this down to resource? */
512 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
513 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
514 /* This should only be called for sysmem textures, it may be a good idea to extend this to all pools at some point in the future */
515 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
516 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
518 return (CONST void*)(This->resource.allocatedMemory);
521 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
522 IWineD3DSwapChainImpl *swapchain;
523 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
527 BYTE *row, *top, *bottom;
531 BOOL srcIsUpsideDown;
533 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
534 static BOOL warned = FALSE;
536 ERR("The application tries to lock the render target, but render target locking is disabled\n");
542 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
543 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
544 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
545 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
546 * context->last_was_blit set on the unlock.
548 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
551 /* Select the correct read buffer, and give some debug output.
552 * There is no need to keep track of the current read buffer or reset it, every part of the code
553 * that reads sets the read buffer as desired.
556 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
557 * Read from the back buffer
559 TRACE("Locking offscreen render target\n");
560 glReadBuffer(myDevice->offscreenBuffer);
561 srcIsUpsideDown = TRUE;
563 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
564 TRACE("Locking %#x buffer\n", buffer);
565 glReadBuffer(buffer);
566 checkGLcall("glReadBuffer");
568 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
569 srcIsUpsideDown = FALSE;
572 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
576 local_rect.right = This->currentDesc.Width;
577 local_rect.bottom = This->currentDesc.Height;
581 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
583 switch(This->resource.format)
587 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
588 /* In case of P8 render targets the index is stored in the alpha component */
590 type = GL_UNSIGNED_BYTE;
592 bpp = This->bytesPerPixel;
594 /* GL can't return palettized data, so read ARGB pixels into a
595 * separate block of memory and convert them into palettized format
596 * in software. Slow, but if the app means to use palettized render
597 * targets and locks it...
599 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
600 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
601 * for the color channels when palettizing the colors.
604 type = GL_UNSIGNED_BYTE;
606 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
608 ERR("Out of memory\n");
612 bpp = This->bytesPerPixel * 3;
619 fmt = This->glDescription.glFormat;
620 type = This->glDescription.glType;
621 bpp = This->bytesPerPixel;
624 if(This->Flags & SFLAG_PBO) {
625 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
626 checkGLcall("glBindBufferARB");
629 glReadPixels(local_rect.left, local_rect.top,
630 local_rect.right - local_rect.left,
631 local_rect.bottom - local_rect.top,
633 vcheckGLcall("glReadPixels");
635 if(This->Flags & SFLAG_PBO) {
636 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
637 checkGLcall("glBindBufferARB");
639 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
640 * to get a pointer to it and perform the flipping in software. This is a lot
641 * faster than calling glReadPixels for each line. In case we want more speed
642 * we should rerender it flipped in a FBO and read the data back from the FBO. */
643 if(!srcIsUpsideDown) {
644 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
645 checkGLcall("glBindBufferARB");
647 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
648 checkGLcall("glMapBufferARB");
652 /* TODO: Merge this with the palettization loop below for P8 targets */
653 if(!srcIsUpsideDown) {
655 /* glReadPixels returns the image upside down, and there is no way to prevent this.
656 Flip the lines in software */
657 len = (local_rect.right - local_rect.left) * bpp;
658 off = local_rect.left * bpp;
660 row = HeapAlloc(GetProcessHeap(), 0, len);
662 ERR("Out of memory\n");
663 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
668 top = mem + pitch * local_rect.top;
669 bottom = ((BYTE *) mem) + pitch * ( local_rect.bottom - local_rect.top - 1);
670 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
671 memcpy(row, top + off, len);
672 memcpy(top + off, bottom + off, len);
673 memcpy(bottom + off, row, len);
677 HeapFree(GetProcessHeap(), 0, row);
679 /* Unmap the temp PBO buffer */
680 if(This->Flags & SFLAG_PBO) {
681 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
682 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
686 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
687 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
688 * the same color but we have no choice.
689 * In case of render targets, the index is stored in the alpha component so no conversion is needed.
691 if((This->resource.format == WINED3DFMT_P8) && !(This->resource.usage & WINED3DUSAGE_RENDERTARGET)) {
693 DWORD width = pitch / 3;
696 pal = This->palette->palents;
698 pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
701 for(y = local_rect.top; y < local_rect.bottom; y++) {
702 for(x = local_rect.left; x < local_rect.right; x++) {
703 /* start lines pixels */
704 BYTE *blue = (BYTE *) ((BYTE *) mem) + y * pitch + x * (sizeof(BYTE) * 3);
705 BYTE *green = blue + 1;
706 BYTE *red = green + 1;
708 for(c = 0; c < 256; c++) {
709 if(*red == pal[c].peRed &&
710 *green == pal[c].peGreen &&
711 *blue == pal[c].peBlue)
713 *((BYTE *) dest + y * width + x) = c;
719 HeapFree(GetProcessHeap(), 0, mem);
724 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
725 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
726 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
729 if(!(This->Flags & SFLAG_DYNLOCK)) {
731 /* MAXLOCKCOUNT is defined in wined3d_private.h */
732 if(This->lockCount > MAXLOCKCOUNT) {
733 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
734 This->Flags |= SFLAG_DYNLOCK;
738 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
739 * Also don't create a PBO for systemmem surfaces.
741 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
745 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
746 error = glGetError();
747 if(This->pbo == 0 || error != GL_NO_ERROR) {
748 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
751 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
753 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
754 checkGLcall("glBindBufferARB");
756 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
757 checkGLcall("glBufferDataARB");
759 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
760 checkGLcall("glBindBufferARB");
762 /* We don't need the system memory anymore and we can't even use it for PBOs */
763 if(!(This->Flags & SFLAG_CLIENT)) {
764 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
765 This->resource.heapMemory = NULL;
767 This->resource.allocatedMemory = NULL;
768 This->Flags |= SFLAG_PBO;
770 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
771 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
774 if(!This->resource.heapMemory) {
775 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
777 This->resource.allocatedMemory =
778 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
779 if(This->Flags & SFLAG_INSYSMEM) {
780 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
785 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
786 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
787 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
788 IWineD3DSwapChain *swapchain = NULL;
790 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
792 /* This is also done in the base class, but we have to verify this before loading any data from
793 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
794 * may interfere, and all other bad things may happen
796 if (This->Flags & SFLAG_LOCKED) {
797 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
798 return WINED3DERR_INVALIDCALL;
800 This->Flags |= SFLAG_LOCKED;
802 if (!(This->Flags & SFLAG_LOCKABLE))
804 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
807 if (Flags & WINED3DLOCK_DISCARD) {
808 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
809 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
810 This->Flags |= SFLAG_INSYSMEM;
813 if (This->Flags & SFLAG_INSYSMEM) {
814 TRACE("Local copy is up to date, not downloading data\n");
815 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
819 /* Now download the surface content from opengl
820 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
821 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
823 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
824 if(swapchain || iface == myDevice->render_targets[0]) {
825 const RECT *pass_rect = pRect;
827 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
828 * because most caller functions do not need that. So do that here
833 pRect->right == This->currentDesc.Width &&
834 pRect->bottom == This->currentDesc.Height) {
838 switch(wined3d_settings.rendertargetlock_mode) {
841 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
843 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
844 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
845 * This may be faster on some cards
847 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
854 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
860 if(swapchain) IWineD3DSwapChain_Release(swapchain);
862 } else if(iface == myDevice->stencilBufferTarget) {
863 /** the depth stencil in openGL has a format of GL_FLOAT
864 * which should be good for WINED3DFMT_D16_LOCKABLE
866 * it is unclear what format the stencil buffer is in except.
867 * 'Each index is converted to fixed point...
868 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
869 * mappings in the table GL_PIXEL_MAP_S_TO_S.
870 * glReadPixels(This->lockedRect.left,
871 * This->lockedRect.bottom - j - 1,
872 * This->lockedRect.right - This->lockedRect.left,
874 * GL_DEPTH_COMPONENT,
876 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
878 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
879 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
880 * none of that is the case the problem is not in this function :-)
881 ********************************************/
882 FIXME("Depth stencil locking not supported yet\n");
884 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
885 TRACE("locking an ordinary surface\n");
886 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
890 if(This->Flags & SFLAG_PBO) {
891 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
893 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
894 checkGLcall("glBindBufferARB");
896 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
897 if(This->resource.allocatedMemory) {
898 ERR("The surface already has PBO memory allocated!\n");
901 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
902 checkGLcall("glMapBufferARB");
904 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
905 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
906 checkGLcall("glBindBufferARB");
911 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
914 IWineD3DBaseTexture *pBaseTexture;
917 * as seen in msdn docs
919 IWineD3DSurface_AddDirtyRect(iface, pRect);
921 /** Dirtify Container if needed */
922 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
923 TRACE("Making container dirty\n");
924 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
925 IWineD3DBaseTexture_Release(pBaseTexture);
927 TRACE("Surface is standalone, no need to dirty the container\n");
931 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
934 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This) {
936 GLint prev_rasterpos[4];
938 BOOL storechanged = FALSE, memory_allocated = FALSE;
942 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
943 IWineD3DDeviceImpl *myDevice = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
944 IWineD3DSwapChainImpl *swapchain;
946 /* Activate the correct context for the render target */
947 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
950 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
952 /* Primary offscreen render target */
953 TRACE("Offscreen render target\n");
954 glDrawBuffer(myDevice->offscreenBuffer);
955 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
957 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
958 TRACE("Unlocking %#x buffer\n", buffer);
959 glDrawBuffer(buffer);
960 checkGLcall("glDrawBuffer");
962 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
966 vcheckGLcall("glFlush");
967 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
968 vcheckGLcall("glIntegerv");
969 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
970 vcheckGLcall("glIntegerv");
971 glPixelZoom(1.0, -1.0);
972 vcheckGLcall("glPixelZoom");
974 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
975 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
976 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
978 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
979 vcheckGLcall("glRasterPos2f");
981 /* Some drivers(radeon dri, others?) don't like exceptions during
982 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
983 * after ReleaseDC. Reading it will cause an exception, which x11drv will
984 * catch to put the dib section in InSync mode, which leads to a crash
985 * and a blocked x server on my radeon card.
987 * The following lines read the dib section so it is put in inSync mode
988 * before glDrawPixels is called and the crash is prevented. There won't
989 * be any interfering gdi accesses, because UnlockRect is called from
990 * ReleaseDC, and the app won't use the dc any more afterwards.
992 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
994 read = This->resource.allocatedMemory[0];
997 switch (This->resource.format) {
998 /* No special care needed */
999 case WINED3DFMT_A4R4G4B4:
1000 case WINED3DFMT_R5G6B5:
1001 case WINED3DFMT_A1R5G5B5:
1002 case WINED3DFMT_R8G8B8:
1003 case WINED3DFMT_X4R4G4B4:
1004 case WINED3DFMT_X1R5G5B5:
1005 type = This->glDescription.glType;
1006 fmt = This->glDescription.glFormat;
1007 mem = This->resource.allocatedMemory;
1008 bpp = This->bytesPerPixel;
1011 /* In the past times we used to set the X channel of X8R8G8B8 and the above formats to
1012 * 1.0 because it happened to fix the intro movie in Pirates. However, this seems wrong.
1013 * If the game uses an X8R8G8B8 back buffer the GL alpha channel should not make any differences,
1014 * and the bug must be somewhere else. If we really have to set the alpha channel to 1.0 in
1015 * this case do it by clearing it after the draw instead of fixing it up in the CPU. Blending
1016 * is disabled via CTXUSAGE_BLIT context setup, so in the glDrawPixels call it does not
1019 case WINED3DFMT_X8R8G8B8:
1020 case WINED3DFMT_A8R8G8B8:
1022 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1023 vcheckGLcall("glPixelStorei");
1024 storechanged = TRUE;
1025 type = This->glDescription.glType;
1026 fmt = This->glDescription.glFormat;
1027 mem = This->resource.allocatedMemory;
1028 bpp = This->bytesPerPixel;
1032 case WINED3DFMT_A2R10G10B10:
1034 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1035 vcheckGLcall("glPixelStorei");
1036 storechanged = TRUE;
1037 type = This->glDescription.glType;
1038 fmt = This->glDescription.glFormat;
1039 mem = This->resource.allocatedMemory;
1040 bpp = This->bytesPerPixel;
1046 int height = This->glRect.bottom - This->glRect.top;
1047 type = GL_UNSIGNED_BYTE;
1050 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * sizeof(DWORD));
1052 ERR("Out of memory\n");
1055 memory_allocated = TRUE;
1056 d3dfmt_convert_surface(This->resource.allocatedMemory,
1064 bpp = This->bytesPerPixel * 4;
1070 FIXME("Unsupported Format %u in locking func\n", This->resource.format);
1073 type = This->glDescription.glType;
1074 fmt = This->glDescription.glFormat;
1075 mem = This->resource.allocatedMemory;
1076 bpp = This->bytesPerPixel;
1079 if(This->Flags & SFLAG_PBO) {
1080 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1081 checkGLcall("glBindBufferARB");
1084 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1085 (This->lockedRect.bottom - This->lockedRect.top)-1,
1087 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1088 checkGLcall("glDrawPixels");
1091 if(This->Flags & SFLAG_PBO) {
1092 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1093 checkGLcall("glBindBufferARB");
1096 glPixelZoom(1.0,1.0);
1097 vcheckGLcall("glPixelZoom");
1099 glRasterPos3iv(&prev_rasterpos[0]);
1100 vcheckGLcall("glRasterPos3iv");
1102 /* Reset to previous pack row length */
1103 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1104 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1106 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
1107 vcheckGLcall("glPixelStorei GL_PACK_SWAP_BYTES");
1110 if(memory_allocated) HeapFree(GetProcessHeap(), 0, mem);
1113 glDrawBuffer(myDevice->offscreenBuffer);
1114 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1115 } else if(swapchain->backBuffer) {
1116 glDrawBuffer(GL_BACK);
1117 checkGLcall("glDrawBuffer(GL_BACK)");
1119 glDrawBuffer(GL_FRONT);
1120 checkGLcall("glDrawBuffer(GL_FRONT)");
1127 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1128 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1129 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1130 IWineD3DSwapChainImpl *swapchain = NULL;
1133 if (!(This->Flags & SFLAG_LOCKED)) {
1134 WARN("trying to Unlock an unlocked surf@%p\n", This);
1135 return WINED3DERR_INVALIDCALL;
1138 if (This->Flags & SFLAG_PBO) {
1139 TRACE("Freeing PBO memory\n");
1140 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1142 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1143 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1144 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1145 checkGLcall("glUnmapBufferARB");
1147 This->resource.allocatedMemory = NULL;
1150 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1152 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1153 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1157 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1158 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1159 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1160 static BOOL warned = FALSE;
1162 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1165 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1169 if(This->dirtyRect.left == 0 &&
1170 This->dirtyRect.top == 0 &&
1171 This->dirtyRect.right == This->currentDesc.Width &&
1172 This->dirtyRect.bottom == This->currentDesc.Height) {
1175 /* TODO: Proper partial rectangle tracking */
1176 fullsurface = FALSE;
1177 This->Flags |= SFLAG_INSYSMEM;
1180 switch(wined3d_settings.rendertargetlock_mode) {
1183 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1185 if (This->glDescription.textureName == 0) {
1186 glGenTextures(1, &This->glDescription.textureName);
1187 checkGLcall("glGenTextures");
1189 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1190 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1192 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1198 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1203 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1204 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1205 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1206 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1207 * not fully up to date because only a subrectangle was read in LockRect.
1209 This->Flags &= ~SFLAG_INSYSMEM;
1210 This->Flags |= SFLAG_INDRAWABLE;
1213 This->dirtyRect.left = This->currentDesc.Width;
1214 This->dirtyRect.top = This->currentDesc.Height;
1215 This->dirtyRect.right = 0;
1216 This->dirtyRect.bottom = 0;
1217 } else if(iface == myDevice->stencilBufferTarget) {
1218 FIXME("Depth Stencil buffer locking is not implemented\n");
1220 /* The rest should be a normal texture */
1221 IWineD3DBaseTextureImpl *impl;
1222 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1223 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1224 * states need resetting
1226 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1227 if(impl->baseTexture.bindCount) {
1228 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1230 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1235 This->Flags &= ~SFLAG_LOCKED;
1236 memset(&This->lockedRect, 0, sizeof(RECT));
1240 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1241 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1242 WINED3DLOCKED_RECT lock;
1246 TRACE("(%p)->(%p)\n",This,pHDC);
1248 if(This->Flags & SFLAG_USERPTR) {
1249 ERR("Not supported on surfaces with an application-provided surfaces\n");
1250 return WINEDDERR_NODC;
1253 /* Give more detailed info for ddraw */
1254 if (This->Flags & SFLAG_DCINUSE)
1255 return WINEDDERR_DCALREADYCREATED;
1257 /* Can't GetDC if the surface is locked */
1258 if (This->Flags & SFLAG_LOCKED)
1259 return WINED3DERR_INVALIDCALL;
1261 memset(&lock, 0, sizeof(lock)); /* To be sure */
1263 /* Create a DIB section if there isn't a hdc yet */
1265 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1266 if(This->Flags & SFLAG_CLIENT) {
1267 IWineD3DSurface_PreLoad(iface);
1270 /* Use the dib section from now on if we are not using a PBO */
1271 if(!(This->Flags & SFLAG_PBO))
1272 This->resource.allocatedMemory = This->dib.bitmap_data;
1275 /* Lock the surface */
1276 hr = IWineD3DSurface_LockRect(iface,
1281 if(This->Flags & SFLAG_PBO) {
1282 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1283 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1287 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1288 /* keep the dib section */
1292 if(This->resource.format == WINED3DFMT_P8 ||
1293 This->resource.format == WINED3DFMT_A8P8) {
1296 PALETTEENTRY ent[256];
1298 GetPaletteEntries(This->palette->hpal, 0, 256, ent);
1299 for (n=0; n<256; n++) {
1300 col[n].rgbRed = ent[n].peRed;
1301 col[n].rgbGreen = ent[n].peGreen;
1302 col[n].rgbBlue = ent[n].peBlue;
1303 col[n].rgbReserved = 0;
1306 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1308 for (n=0; n<256; n++) {
1309 col[n].rgbRed = device->palettes[device->currentPalette][n].peRed;
1310 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
1311 col[n].rgbBlue = device->palettes[device->currentPalette][n].peBlue;
1312 col[n].rgbReserved = 0;
1316 SetDIBColorTable(This->hDC, 0, 256, col);
1320 TRACE("returning %p\n",*pHDC);
1321 This->Flags |= SFLAG_DCINUSE;
1326 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1327 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1329 TRACE("(%p)->(%p)\n",This,hDC);
1331 if (!(This->Flags & SFLAG_DCINUSE))
1332 return WINED3DERR_INVALIDCALL;
1334 if (This->hDC !=hDC) {
1335 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1336 return WINED3DERR_INVALIDCALL;
1339 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1340 /* Copy the contents of the DIB over to the PBO */
1341 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1344 /* we locked first, so unlock now */
1345 IWineD3DSurface_UnlockRect(iface);
1347 This->Flags &= ~SFLAG_DCINUSE;
1352 /* ******************************************************
1353 IWineD3DSurface Internal (No mapping to directx api) parts follow
1354 ****************************************************** */
1356 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, GLenum *format, GLenum *internal, GLenum *type, CONVERT_TYPES *convert, int *target_bpp, BOOL srgb_mode) {
1357 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1358 const GlPixelFormatDesc *glDesc;
1359 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1360 BOOL p8_render_target = FALSE;
1361 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1363 /* Default values: From the surface */
1364 *format = glDesc->glFormat;
1365 *type = glDesc->glType;
1366 *convert = NO_CONVERSION;
1367 *target_bpp = This->bytesPerPixel;
1370 *internal = glDesc->glGammaInternal;
1371 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1372 *internal = glDesc->rtInternal;
1374 *internal = glDesc->glInternal;
1377 /* Ok, now look if we have to do any conversion */
1378 switch(This->resource.format) {
1384 if (device->render_targets && device->render_targets[0]) {
1385 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1386 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
1387 p8_render_target = TRUE;
1390 /* Use conversion when the paletted texture extension is not available, or when it is available make sure it is used
1391 * for texturing as it won't work for calls like glDraw-/glReadPixels and further also use conversion in case of color keying.
1392 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which conflicts with this.
1394 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && p8_render_target)) || colorkey_active || (!use_texturing && GL_SUPPORT(EXT_PALETTED_TEXTURE)) ) {
1396 *internal = GL_RGBA;
1397 *type = GL_UNSIGNED_BYTE;
1399 if(colorkey_active) {
1400 *convert = CONVERT_PALETTED_CK;
1402 *convert = CONVERT_PALETTED;
1405 else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1407 *internal = GL_RGBA;
1408 *type = GL_UNSIGNED_BYTE;
1414 case WINED3DFMT_R3G3B2:
1415 /* **********************
1416 GL_UNSIGNED_BYTE_3_3_2
1417 ********************** */
1418 if (colorkey_active) {
1419 /* This texture format will never be used.. So do not care about color keying
1420 up until the point in time it will be needed :-) */
1421 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1425 case WINED3DFMT_R5G6B5:
1426 if (colorkey_active) {
1427 *convert = CONVERT_CK_565;
1429 *internal = GL_RGBA;
1430 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1434 case WINED3DFMT_X1R5G5B5:
1435 if (colorkey_active) {
1436 *convert = CONVERT_CK_5551;
1438 *internal = GL_RGBA;
1439 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1443 case WINED3DFMT_R8G8B8:
1444 if (colorkey_active) {
1445 *convert = CONVERT_CK_RGB24;
1447 *internal = GL_RGBA;
1448 *type = GL_UNSIGNED_INT_8_8_8_8;
1453 case WINED3DFMT_X8R8G8B8:
1454 if (colorkey_active) {
1455 *convert = CONVERT_RGB32_888;
1457 *internal = GL_RGBA;
1458 *type = GL_UNSIGNED_INT_8_8_8_8;
1462 case WINED3DFMT_V8U8:
1463 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1464 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1465 *format = GL_DUDV_ATI;
1466 *internal = GL_DU8DV8_ATI;
1468 /* No conversion - Just change the gl type */
1471 *convert = CONVERT_V8U8;
1473 *internal = GL_RGB8;
1474 *type = GL_UNSIGNED_BYTE;
1478 case WINED3DFMT_L6V5U5:
1479 *convert = CONVERT_L6V5U5;
1480 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1482 /* Use format and types from table */
1484 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1487 *internal = GL_RGB5;
1488 *type = GL_UNSIGNED_SHORT_5_6_5;
1492 case WINED3DFMT_X8L8V8U8:
1493 *convert = CONVERT_X8L8V8U8;
1495 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1496 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1497 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1498 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1499 * the needed type and format parameter, so the internal format contains a
1500 * 4th component, which is returned as alpha
1503 /* Not supported by GL_ATI_envmap_bumpmap */
1505 *internal = GL_RGB8;
1506 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1510 case WINED3DFMT_Q8W8V8U8:
1511 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1512 *convert = CONVERT_Q8W8V8U8;
1514 *internal = GL_RGBA8;
1515 *type = GL_UNSIGNED_BYTE;
1517 /* Not supported by GL_ATI_envmap_bumpmap */
1520 case WINED3DFMT_V16U16:
1521 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1522 *convert = CONVERT_V16U16;
1524 *internal = GL_RGB16_EXT;
1525 *type = GL_UNSIGNED_SHORT;
1527 /* What should I do here about GL_ATI_envmap_bumpmap?
1528 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1532 case WINED3DFMT_A4L4:
1533 /* A4L4 exists as an internal gl format, but for some reason there is not
1534 * format+type combination to load it. Thus convert it to A8L8, then load it
1535 * with A4L4 internal, but A8L8 format+type
1537 *convert = CONVERT_A4L4;
1538 *format = GL_LUMINANCE_ALPHA;
1539 *internal = GL_LUMINANCE4_ALPHA4;
1540 *type = GL_UNSIGNED_BYTE;
1544 case WINED3DFMT_R32F:
1545 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1546 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1547 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1550 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1552 *convert = CONVERT_R32F;
1554 *internal = GL_RGB32F_ARB;
1559 case WINED3DFMT_R16F:
1560 /* Similar to R32F */
1561 *convert = CONVERT_R16F;
1563 *internal = GL_RGB16F_ARB;
1564 *type = GL_HALF_FLOAT_ARB;
1568 case WINED3DFMT_G16R16:
1569 *convert = CONVERT_G16R16;
1571 *internal = GL_RGB16_EXT;
1572 *type = GL_UNSIGNED_SHORT;
1583 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1584 BYTE *source, *dest;
1585 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1590 memcpy(dst, src, pitch * height);
1593 case CONVERT_PALETTED:
1594 case CONVERT_PALETTED_CK:
1596 IWineD3DPaletteImpl* pal = This->palette;
1601 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1604 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1606 for (y = 0; y < height; y++)
1608 source = src + pitch * y;
1609 dest = dst + outpitch * y;
1610 /* This is an 1 bpp format, using the width here is fine */
1611 for (x = 0; x < width; x++) {
1612 BYTE color = *source++;
1613 *dest++ = table[color][0];
1614 *dest++ = table[color][1];
1615 *dest++ = table[color][2];
1616 *dest++ = table[color][3];
1622 case CONVERT_CK_565:
1624 /* Converting the 565 format in 5551 packed to emulate color-keying.
1626 Note : in all these conversion, it would be best to average the averaging
1627 pixels to get the color of the pixel that will be color-keyed to
1628 prevent 'color bleeding'. This will be done later on if ever it is
1631 Note2: Nvidia documents say that their driver does not support alpha + color keying
1632 on the same surface and disables color keying in such a case
1638 TRACE("Color keyed 565\n");
1640 for (y = 0; y < height; y++) {
1641 Source = (WORD *) (src + y * pitch);
1642 Dest = (WORD *) (dst + y * outpitch);
1643 for (x = 0; x < width; x++ ) {
1644 WORD color = *Source++;
1645 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1646 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1647 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1656 case CONVERT_CK_5551:
1658 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1662 TRACE("Color keyed 5551\n");
1663 for (y = 0; y < height; y++) {
1664 Source = (WORD *) (src + y * pitch);
1665 Dest = (WORD *) (dst + y * outpitch);
1666 for (x = 0; x < width; x++ ) {
1667 WORD color = *Source++;
1669 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1670 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1674 *Dest &= ~(1 << 15);
1686 unsigned char *Dest;
1687 for(y = 0; y < height; y++) {
1688 Source = (short *) (src + y * pitch);
1689 Dest = (unsigned char *) (dst + y * outpitch);
1690 for (x = 0; x < width; x++ ) {
1691 long color = (*Source++);
1692 /* B */ Dest[0] = 0xff;
1693 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1694 /* R */ Dest[2] = (color) + 128; /* U */
1701 case CONVERT_V16U16:
1705 unsigned short *Dest;
1706 for(y = 0; y < height; y++) {
1707 Source = (DWORD *) (src + y * pitch);
1708 Dest = (unsigned short *) (dst + y * outpitch);
1709 for (x = 0; x < width; x++ ) {
1710 DWORD color = (*Source++);
1711 /* B */ Dest[0] = 0xffff;
1712 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1713 /* R */ Dest[2] = (color ) + 32768; /* U */
1720 case CONVERT_Q8W8V8U8:
1724 unsigned char *Dest;
1725 for(y = 0; y < height; y++) {
1726 Source = (DWORD *) (src + y * pitch);
1727 Dest = (unsigned char *) (dst + y * outpitch);
1728 for (x = 0; x < width; x++ ) {
1729 long color = (*Source++);
1730 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1731 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1732 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1733 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1740 case CONVERT_L6V5U5:
1744 unsigned char *Dest;
1746 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1747 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1748 * fixed function and shaders without further conversion once the surface is
1751 for(y = 0; y < height; y++) {
1752 Source = (WORD *) (src + y * pitch);
1753 Dest = (unsigned char *) (dst + y * outpitch);
1754 for (x = 0; x < width; x++ ) {
1755 short color = (*Source++);
1756 unsigned char l = ((color >> 10) & 0xfc);
1757 char v = ((color >> 5) & 0x3e);
1758 char u = ((color ) & 0x1f);
1760 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1761 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1762 * shift. GL reads a signed value and converts it into an unsigned value.
1764 /* M */ Dest[2] = l << 1;
1766 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1767 * from 5 bit values to 8 bit values.
1769 /* V */ Dest[1] = v << 3;
1770 /* U */ Dest[0] = u << 3;
1775 for(y = 0; y < height; y++) {
1776 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1777 Source = (WORD *) (src + y * pitch);
1778 for (x = 0; x < width; x++ ) {
1779 short color = (*Source++);
1780 unsigned char l = ((color >> 10) & 0xfc);
1781 short v = ((color >> 5) & 0x3e);
1782 short u = ((color ) & 0x1f);
1783 short v_conv = v + 16;
1784 short u_conv = u + 16;
1786 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1794 case CONVERT_X8L8V8U8:
1798 unsigned char *Dest;
1800 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1801 /* This implementation works with the fixed function pipeline and shaders
1802 * without further modification after converting the surface.
1804 for(y = 0; y < height; y++) {
1805 Source = (DWORD *) (src + y * pitch);
1806 Dest = (unsigned char *) (dst + y * outpitch);
1807 for (x = 0; x < width; x++ ) {
1808 long color = (*Source++);
1809 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1810 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1811 /* U */ Dest[0] = (color & 0xff); /* U */
1812 /* I */ Dest[3] = 255; /* X */
1817 /* Doesn't work correctly with the fixed function pipeline, but can work in
1818 * shaders if the shader is adjusted. (There's no use for this format in gl's
1819 * standard fixed function pipeline anyway).
1821 for(y = 0; y < height; y++) {
1822 Source = (DWORD *) (src + y * pitch);
1823 Dest = (unsigned char *) (dst + y * outpitch);
1824 for (x = 0; x < width; x++ ) {
1825 long color = (*Source++);
1826 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1827 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1828 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1839 unsigned char *Source;
1840 unsigned char *Dest;
1841 for(y = 0; y < height; y++) {
1842 Source = (unsigned char *) (src + y * pitch);
1843 Dest = (unsigned char *) (dst + y * outpitch);
1844 for (x = 0; x < width; x++ ) {
1845 unsigned char color = (*Source++);
1846 /* A */ Dest[1] = (color & 0xf0) << 0;
1847 /* L */ Dest[0] = (color & 0x0f) << 4;
1859 for(y = 0; y < height; y++) {
1860 Source = (float *) (src + y * pitch);
1861 Dest = (float *) (dst + y * outpitch);
1862 for (x = 0; x < width; x++ ) {
1863 float color = (*Source++);
1879 for(y = 0; y < height; y++) {
1880 Source = (WORD *) (src + y * pitch);
1881 Dest = (WORD *) (dst + y * outpitch);
1882 for (x = 0; x < width; x++ ) {
1883 WORD color = (*Source++);
1893 case CONVERT_G16R16:
1899 for(y = 0; y < height; y++) {
1900 Source = (WORD *) (src + y * pitch);
1901 Dest = (WORD *) (dst + y * outpitch);
1902 for (x = 0; x < width; x++ ) {
1903 WORD green = (*Source++);
1904 WORD red = (*Source++);
1915 ERR("Unsupported conversation type %d\n", convert);
1920 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
1921 IWineD3DPaletteImpl* pal = This->palette;
1922 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1923 BOOL index_in_alpha = FALSE;
1926 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
1927 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
1928 * is slow. Further RGB->P8 conversion is not possible because palettes can have
1929 * duplicate entries. Store the color key in the unused alpha component to speed the
1930 * download up and to make conversion unneeded. */
1931 if (device->render_targets && device->render_targets[0]) {
1932 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1934 if(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
1935 index_in_alpha = TRUE;
1939 /* Still no palette? Use the device's palette */
1940 /* Get the surface's palette */
1941 for (i = 0; i < 256; i++) {
1942 table[i][0] = device->palettes[device->currentPalette][i].peRed;
1943 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
1944 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
1946 if(index_in_alpha) {
1948 } else if (colorkey &&
1949 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1950 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1951 /* We should maybe here put a more 'neutral' color than the standard bright purple
1952 one often used by application to prevent the nice purple borders when bi-linear
1960 TRACE("Using surface palette %p\n", pal);
1961 /* Get the surface's palette */
1962 for (i = 0; i < 256; i++) {
1963 table[i][0] = pal->palents[i].peRed;
1964 table[i][1] = pal->palents[i].peGreen;
1965 table[i][2] = pal->palents[i].peBlue;
1967 if(index_in_alpha) {
1970 else if (colorkey &&
1971 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1972 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1973 /* We should maybe here put a more 'neutral' color than the standard bright purple
1974 one often used by application to prevent the nice purple borders when bi-linear
1977 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
1978 table[i][3] = pal->palents[i].peFlags;
1986 const char *fragment_palette_conversion =
1989 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
1990 "TEX index.x, fragment.texcoord[0], texture[0], 2D;\n" /* store the red-component of the current pixel */
1991 "MAD index.x, index.x, constants.x, constants.y;\n" /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
1992 "TEX result.color, index, texture[1], 1D;\n" /* use the red-component as a index in the palette to get the final color */
1995 /* This function is used in case of 8bit paletted textures to upload the palette.
1996 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
1997 extensions like ATI_fragment_shaders is possible.
1999 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2000 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2002 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2004 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2006 /* Try to use the paletted texture extension */
2007 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2009 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2010 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2014 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2015 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2016 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2018 /* Create the fragment program if we don't have it */
2019 if(!device->paletteConversionShader)
2021 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2022 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2023 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2024 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2025 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2028 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2029 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2031 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2032 glEnable(GL_TEXTURE_1D);
2033 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2035 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2036 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2037 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2038 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2040 /* Switch back to unit 0 in which the 2D texture will be stored. */
2041 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2043 /* Rebind the texture because it isn't bound anymore */
2044 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2048 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2049 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2051 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2052 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2053 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2058 if(This->palette9) {
2059 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2063 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2065 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2069 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2070 GLboolean oldwrite[4];
2072 /* Some formats have only some color channels, and the others are 1.0.
2073 * since our rendering renders to all channels, and those pixel formats
2074 * are emulated by using a full texture with the other channels set to 1.0
2075 * manually, clear the unused channels.
2077 * This could be done with hacking colorwriteenable to mask the colors,
2078 * but before drawing the buffer would have to be cleared too, so there's
2081 switch(This->resource.format) {
2082 case WINED3DFMT_R16F:
2083 case WINED3DFMT_R32F:
2084 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2085 /* Do not activate a context, the correct drawable is active already
2086 * though just the read buffer is set, make sure to have the correct draw
2089 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2090 glDisable(GL_SCISSOR_TEST);
2091 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2092 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2093 glClearColor(0.0, 1.0, 1.0, 1.0);
2094 glClear(GL_COLOR_BUFFER_BIT);
2095 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2096 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2097 checkGLcall("Unused channel clear\n");
2104 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2105 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2107 if (!(This->Flags & SFLAG_INTEXTURE)) {
2108 TRACE("Reloading because surface is dirty\n");
2109 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2110 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2111 /* Reload: vice versa OR */
2112 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2113 /* Also reload: Color key is active AND the color key has changed */
2114 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2115 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2116 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2117 TRACE("Reloading because of color keying\n");
2118 /* To perform the color key conversion we need a sysmem copy of
2119 * the surface. Make sure we have it
2121 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2122 } else if(palette9_changed(This)) {
2123 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
2124 /* TODO: This is not necessarily needed with hw palettized texture support */
2125 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2127 TRACE("surface is already in texture\n");
2131 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2132 * These resources are not bound by device size or format restrictions. Because of this,
2133 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2134 * However, these resources can always be created, locked, and copied.
2136 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2138 FIXME("(%p) Operation not supported for scratch textures\n",This);
2139 return WINED3DERR_INVALIDCALL;
2142 This->srgb = srgb_mode;
2143 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2147 static unsigned int gen = 0;
2150 if ((gen % 10) == 0) {
2151 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2152 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2155 * debugging crash code
2164 if (!(This->Flags & SFLAG_DONOTFREE)) {
2165 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2166 This->resource.allocatedMemory = NULL;
2167 This->resource.heapMemory = NULL;
2168 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2174 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2175 /* TODO: check for locks */
2176 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2177 IWineD3DBaseTexture *baseTexture = NULL;
2178 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2180 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2181 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2182 TRACE("Passing to container\n");
2183 IWineD3DBaseTexture_BindTexture(baseTexture);
2184 IWineD3DBaseTexture_Release(baseTexture);
2186 TRACE("(%p) : Binding surface\n", This);
2188 if(!device->isInDraw) {
2189 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2192 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2200 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2203 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2204 char *allocatedMemory;
2206 IWineD3DSwapChain *swapChain = NULL;
2208 GLuint tmpTexture = 0;
2211 Textures may not be stored in ->allocatedgMemory and a GlTexture
2212 so we should lock the surface before saving a snapshot, or at least check that
2214 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2215 by calling GetTexImage and in compressed form by calling
2216 GetCompressedTexImageARB. Queried compressed images can be saved and
2217 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2218 texture images do not need to be processed by the GL and should
2219 significantly improve texture loading performance relative to uncompressed
2222 /* Setup the width and height to be the internal texture width and height. */
2223 width = This->pow2Width;
2224 height = This->pow2Height;
2225 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2226 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2228 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2229 /* if were not a real texture then read the back buffer into a real texture */
2230 /* we don't want to interfere with the back buffer so read the data into a temporary
2231 * texture and then save the data out of the temporary texture
2235 TRACE("(%p) Reading render target into texture\n", This);
2236 glEnable(GL_TEXTURE_2D);
2238 glGenTextures(1, &tmpTexture);
2239 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2241 glTexImage2D(GL_TEXTURE_2D,
2248 GL_UNSIGNED_INT_8_8_8_8_REV,
2251 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2252 vcheckGLcall("glGetIntegerv");
2253 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2254 vcheckGLcall("glReadBuffer");
2255 glCopyTexImage2D(GL_TEXTURE_2D,
2264 checkGLcall("glCopyTexImage2D");
2265 glReadBuffer(prevRead);
2268 } else { /* bind the real texture, and make sure it up to date */
2269 IWineD3DSurface_PreLoad(iface);
2271 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2273 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2274 glGetTexImage(GL_TEXTURE_2D,
2275 This->glDescription.level,
2277 GL_UNSIGNED_INT_8_8_8_8_REV,
2279 checkGLcall("glTexImage2D");
2281 glBindTexture(GL_TEXTURE_2D, 0);
2282 glDeleteTextures(1, &tmpTexture);
2286 f = fopen(filename, "w+");
2288 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2289 return WINED3DERR_INVALIDCALL;
2291 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2292 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2307 fwrite(&width,2,1,f);
2309 fwrite(&height,2,1,f);
2314 /* if the data is upside down if we've fetched it from a back buffer, so it needs flipping again to make it the correct way up */
2316 textureRow = allocatedMemory + (width * (height - 1) *4);
2318 textureRow = allocatedMemory;
2319 for (y = 0 ; y < height; y++) {
2320 for (i = 0; i < width; i++) {
2321 color = *((DWORD*)textureRow);
2322 fputc((color >> 16) & 0xFF, f); /* B */
2323 fputc((color >> 8) & 0xFF, f); /* G */
2324 fputc((color >> 0) & 0xFF, f); /* R */
2325 fputc((color >> 24) & 0xFF, f); /* A */
2328 /* take two rows of the pointer to the texture memory */
2330 (textureRow-= width << 3);
2333 TRACE("Closing file\n");
2337 IWineD3DSwapChain_Release(swapChain);
2339 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2344 * Slightly inefficient way to handle multiple dirty rects but it works :)
2346 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2347 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2348 IWineD3DBaseTexture *baseTexture = NULL;
2350 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2351 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2353 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2354 if (NULL != pDirtyRect) {
2355 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2356 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2357 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2358 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2360 This->dirtyRect.left = 0;
2361 This->dirtyRect.top = 0;
2362 This->dirtyRect.right = This->currentDesc.Width;
2363 This->dirtyRect.bottom = This->currentDesc.Height;
2365 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2366 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2367 /* if the container is a basetexture then mark it dirty. */
2368 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2369 TRACE("Passing to container\n");
2370 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2371 IWineD3DBaseTexture_Release(baseTexture);
2376 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2377 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2379 const GlPixelFormatDesc *glDesc;
2380 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2382 TRACE("(%p) : Calling base function first\n", This);
2383 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2385 /* Setup some glformat defaults */
2386 This->glDescription.glFormat = glDesc->glFormat;
2387 This->glDescription.glFormatInternal = glDesc->glInternal;
2388 This->glDescription.glType = glDesc->glType;
2390 This->Flags &= ~SFLAG_ALLOCATED;
2391 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2392 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2397 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2398 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2400 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2401 WARN("Surface is locked or the HDC is in use\n");
2402 return WINED3DERR_INVALIDCALL;
2405 if(Mem && Mem != This->resource.allocatedMemory) {
2406 void *release = NULL;
2408 /* Do I have to copy the old surface content? */
2409 if(This->Flags & SFLAG_DIBSECTION) {
2410 /* Release the DC. No need to hold the critical section for the update
2411 * Thread because this thread runs only on front buffers, but this method
2412 * fails for render targets in the check above.
2414 SelectObject(This->hDC, This->dib.holdbitmap);
2415 DeleteDC(This->hDC);
2416 /* Release the DIB section */
2417 DeleteObject(This->dib.DIBsection);
2418 This->dib.bitmap_data = NULL;
2419 This->resource.allocatedMemory = NULL;
2421 This->Flags &= ~SFLAG_DIBSECTION;
2422 } else if(!(This->Flags & SFLAG_USERPTR)) {
2423 release = This->resource.heapMemory;
2424 This->resource.heapMemory = NULL;
2426 This->resource.allocatedMemory = Mem;
2427 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2429 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2430 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2432 /* For client textures opengl has to be notified */
2433 if(This->Flags & SFLAG_CLIENT) {
2434 This->Flags &= ~SFLAG_ALLOCATED;
2435 IWineD3DSurface_PreLoad(iface);
2436 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2439 /* Now free the old memory if any */
2440 HeapFree(GetProcessHeap(), 0, release);
2441 } else if(This->Flags & SFLAG_USERPTR) {
2442 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2443 This->resource.allocatedMemory = NULL;
2444 /* HeapMemory should be NULL already */
2445 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2446 This->Flags &= ~SFLAG_USERPTR;
2448 if(This->Flags & SFLAG_CLIENT) {
2449 This->Flags &= ~SFLAG_ALLOCATED;
2450 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2451 IWineD3DSurface_PreLoad(iface);
2457 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2458 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2459 IWineD3DSwapChainImpl *swapchain = NULL;
2461 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2463 /* Flipping is only supported on RenderTargets */
2464 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2467 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2468 * FIXME("(%p) Target override is not supported by now\n", This);
2469 * Additionally, it isn't really possible to support triple-buffering
2470 * properly on opengl at all
2474 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2476 ERR("Flipped surface is not on a swapchain\n");
2477 return WINEDDERR_NOTFLIPPABLE;
2480 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2481 * and only d3d8 and d3d9 apps specify the presentation interval
2483 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2484 /* Most common case first to avoid wasting time on all the other cases */
2485 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2486 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2487 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2488 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2489 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2490 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2491 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2493 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2496 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2497 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2498 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2502 /* Does a direct frame buffer -> texture copy. Stretching is done
2503 * with single pixel copy calls
2505 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2506 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2509 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2512 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2514 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2516 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2517 glEnable(This->glDescription.target);
2518 checkGLcall("glEnable(This->glDescription.target)");
2520 /* Bind the target texture */
2521 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2522 checkGLcall("glBindTexture");
2524 glReadBuffer(myDevice->offscreenBuffer);
2526 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2527 glReadBuffer(buffer);
2529 checkGLcall("glReadBuffer");
2531 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2532 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2534 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2535 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2537 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2538 ERR("Texture filtering not supported in direct blit\n");
2540 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2541 ERR("Texture filtering not supported in direct blit\n");
2545 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2546 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2547 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2549 glCopyTexSubImage2D(This->glDescription.target,
2550 This->glDescription.level,
2551 drect->x1, drect->y1, /* xoffset, yoffset */
2552 srect->x1, Src->currentDesc.Height - srect->y2,
2553 drect->x2 - drect->x1, drect->y2 - drect->y1);
2555 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2556 /* I have to process this row by row to swap the image,
2557 * otherwise it would be upside down, so stretching in y direction
2558 * doesn't cost extra time
2560 * However, stretching in x direction can be avoided if not necessary
2562 for(row = drect->y1; row < drect->y2; row++) {
2563 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2564 /* Well, that stuff works, but it's very slow.
2565 * find a better way instead
2569 for(col = drect->x1; col < drect->x2; col++) {
2570 glCopyTexSubImage2D(This->glDescription.target,
2571 This->glDescription.level,
2572 drect->x1 + col, row, /* xoffset, yoffset */
2573 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2577 glCopyTexSubImage2D(This->glDescription.target,
2578 This->glDescription.level,
2579 drect->x1, row, /* xoffset, yoffset */
2580 srect->x1, yoffset - (int) (row * yrel),
2581 drect->x2-drect->x1, 1);
2585 vcheckGLcall("glCopyTexSubImage2D");
2587 /* Leave the opengl state valid for blitting */
2588 glDisable(This->glDescription.target);
2589 checkGLcall("glDisable(This->glDescription.target)");
2594 /* Uses the hardware to stretch and flip the image */
2595 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2596 GLuint src, backup = 0;
2597 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2598 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2599 float left, right, top, bottom; /* Texture coordinates */
2600 UINT fbwidth = Src->currentDesc.Width;
2601 UINT fbheight = Src->currentDesc.Height;
2602 GLenum drawBuffer = GL_BACK;
2603 GLenum texture_target;
2605 TRACE("Using hwstretch blit\n");
2606 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2607 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2610 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2612 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2613 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2615 if(GL_LIMITS(aux_buffers) >= 2) {
2616 /* Got more than one aux buffer? Use the 2nd aux buffer */
2617 drawBuffer = GL_AUX1;
2618 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2619 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2620 drawBuffer = GL_AUX0;
2623 if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2624 glGenTextures(1, &backup);
2625 checkGLcall("glGenTextures\n");
2626 glBindTexture(GL_TEXTURE_2D, backup);
2627 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2628 texture_target = GL_TEXTURE_2D;
2630 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2631 * we are reading from the back buffer, the backup can be used as source texture
2633 if(Src->glDescription.textureName == 0) {
2634 /* Get it a description */
2635 IWineD3DSurface_PreLoad(SrcSurface);
2637 texture_target = Src->glDescription.target;
2638 glBindTexture(texture_target, Src->glDescription.textureName);
2639 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2640 glEnable(texture_target);
2641 checkGLcall("glEnable(texture_target)");
2643 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2644 Src->Flags &= ~SFLAG_INTEXTURE;
2647 glReadBuffer(GL_BACK);
2648 checkGLcall("glReadBuffer(GL_BACK)");
2650 /* TODO: Only back up the part that will be overwritten */
2651 glCopyTexSubImage2D(texture_target, 0,
2652 0, 0 /* read offsets */,
2657 checkGLcall("glCopyTexSubImage2D");
2659 /* No issue with overriding these - the sampler is dirty due to blit usage */
2660 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2661 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2662 checkGLcall("glTexParameteri");
2663 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2664 minMipLookup[Filter][WINED3DTEXF_NONE]);
2665 checkGLcall("glTexParameteri");
2667 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2668 src = backup ? backup : Src->glDescription.textureName;
2670 glReadBuffer(GL_FRONT);
2671 checkGLcall("glReadBuffer(GL_FRONT)");
2673 glGenTextures(1, &src);
2674 checkGLcall("glGenTextures(1, &src)");
2675 glBindTexture(GL_TEXTURE_2D, src);
2676 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2678 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2679 * out for power of 2 sizes
2681 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2682 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2683 checkGLcall("glTexImage2D");
2684 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2685 0, 0 /* read offsets */,
2690 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2691 checkGLcall("glTexParameteri");
2692 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2693 checkGLcall("glTexParameteri");
2695 glReadBuffer(GL_BACK);
2696 checkGLcall("glReadBuffer(GL_BACK)");
2698 if(texture_target != GL_TEXTURE_2D) {
2699 glDisable(texture_target);
2700 glEnable(GL_TEXTURE_2D);
2701 texture_target = GL_TEXTURE_2D;
2704 checkGLcall("glEnd and previous");
2706 left = (float) srect->x1 / (float) Src->pow2Width;
2707 right = (float) srect->x2 / (float) Src->pow2Width;
2710 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2711 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2713 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2714 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2717 /* draw the source texture stretched and upside down. The correct surface is bound already */
2718 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2719 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2721 glDrawBuffer(drawBuffer);
2722 glReadBuffer(drawBuffer);
2726 glTexCoord2f(left, bottom);
2727 glVertex2i(0, fbheight);
2730 glTexCoord2f(left, top);
2731 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2734 glTexCoord2f(right, top);
2735 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2738 glTexCoord2f(right, bottom);
2739 glVertex2i(drect->x2 - drect->x1, fbheight);
2741 checkGLcall("glEnd and previous");
2743 if(texture_target != This->glDescription.target) {
2744 glDisable(texture_target);
2745 glEnable(This->glDescription.target);
2746 texture_target = This->glDescription.target;
2749 /* Now read the stretched and upside down image into the destination texture */
2750 glBindTexture(texture_target, This->glDescription.textureName);
2751 checkGLcall("glBindTexture");
2752 glCopyTexSubImage2D(texture_target,
2754 drect->x1, drect->y1, /* xoffset, yoffset */
2755 0, 0, /* We blitted the image to the origin */
2756 drect->x2 - drect->x1, drect->y2 - drect->y1);
2757 checkGLcall("glCopyTexSubImage2D");
2759 if(drawBuffer == GL_BACK) {
2760 /* Write the back buffer backup back */
2762 if(texture_target != GL_TEXTURE_2D) {
2763 glDisable(texture_target);
2764 glEnable(GL_TEXTURE_2D);
2765 texture_target = GL_TEXTURE_2D;
2767 glBindTexture(GL_TEXTURE_2D, backup);
2768 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2770 if(texture_target != Src->glDescription.target) {
2771 glDisable(texture_target);
2772 glEnable(Src->glDescription.target);
2773 texture_target = Src->glDescription.target;
2775 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2776 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2781 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2785 glTexCoord2f(0.0, 0.0);
2786 glVertex2i(0, fbheight);
2789 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2790 glVertex2i(fbwidth, Src->currentDesc.Height);
2793 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2794 glVertex2i(fbwidth, 0);
2797 /* Restore the old draw buffer */
2798 glDrawBuffer(GL_BACK);
2800 glDisable(texture_target);
2801 checkGLcall("glDisable(texture_target)");
2804 if(src != Src->glDescription.textureName && src != backup) {
2805 glDeleteTextures(1, &src);
2806 checkGLcall("glDeleteTextures(1, &src)");
2809 glDeleteTextures(1, &backup);
2810 checkGLcall("glDeleteTextures(1, &backup)");
2816 /* Not called from the VTable */
2817 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2819 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2820 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2821 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2823 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2825 /* Get the swapchain. One of the surfaces has to be a primary surface */
2826 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2827 WARN("Destination is in sysmem, rejecting gl blt\n");
2828 return WINED3DERR_INVALIDCALL;
2830 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2831 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2833 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2834 WARN("Src is in sysmem, rejecting gl blt\n");
2835 return WINED3DERR_INVALIDCALL;
2837 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2838 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2841 /* Early sort out of cases where no render target is used */
2842 if(!dstSwapchain && !srcSwapchain &&
2843 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2844 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2845 return WINED3DERR_INVALIDCALL;
2848 /* No destination color keying supported */
2849 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2850 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2851 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2852 return WINED3DERR_INVALIDCALL;
2856 rect.x1 = DestRect->left;
2857 rect.y1 = DestRect->top;
2858 rect.x2 = DestRect->right;
2859 rect.y2 = DestRect->bottom;
2863 rect.x2 = This->currentDesc.Width;
2864 rect.y2 = This->currentDesc.Height;
2867 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2868 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
2869 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
2870 /* Half-life does a Blt from the back buffer to the front buffer,
2871 * Full surface size, no flags... Use present instead
2873 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
2876 /* Check rects - IWineD3DDevice_Present doesn't handle them */
2880 TRACE("Looking if a Present can be done...\n");
2881 /* Source Rectangle must be full surface */
2883 if(SrcRect->left != 0 || SrcRect->top != 0 ||
2884 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
2885 TRACE("No, Source rectangle doesn't match\n");
2891 mySrcRect.right = Src->currentDesc.Width;
2892 mySrcRect.bottom = Src->currentDesc.Height;
2894 /* No stretching may occur */
2895 if(mySrcRect.right != rect.x2 - rect.x1 ||
2896 mySrcRect.bottom != rect.y2 - rect.y1) {
2897 TRACE("No, stretching is done\n");
2901 /* Destination must be full surface or match the clipping rectangle */
2902 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
2906 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
2911 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
2914 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
2915 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
2917 TRACE("No, dest rectangle doesn't match(clipper)\n");
2918 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
2919 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
2925 if(rect.x1 != 0 || rect.y1 != 0 ||
2926 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
2927 TRACE("No, dest rectangle doesn't match(surface size)\n");
2934 /* These flags are unimportant for the flag check, remove them */
2935 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
2936 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
2938 /* The idea behind this is that a glReadPixels and a glDrawPixels call
2939 * take very long, while a flip is fast.
2940 * This applies to Half-Life, which does such Blts every time it finished
2941 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
2942 * menu. This is also used by all apps when they do windowed rendering
2944 * The problem is that flipping is not really the same as copying. After a
2945 * Blt the front buffer is a copy of the back buffer, and the back buffer is
2946 * untouched. Therefore it's necessary to override the swap effect
2947 * and to set it back after the flip.
2949 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
2953 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
2954 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2956 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
2957 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
2959 dstSwapchain->presentParms.SwapEffect = orig_swap;
2966 TRACE("Unsupported blit between buffers on the same swapchain\n");
2967 return WINED3DERR_INVALIDCALL;
2968 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
2969 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
2970 return WINED3DERR_INVALIDCALL;
2971 } else if(dstSwapchain && srcSwapchain) {
2972 FIXME("Implement hardware blit between two different swapchains\n");
2973 return WINED3DERR_INVALIDCALL;
2974 } else if(dstSwapchain) {
2975 if(SrcSurface != myDevice->render_targets[0]) {
2976 ERR("Unexpected render target -> render target blit\n");
2978 TRACE("Blit from active render target to a swapchain\n");
2979 /* Handled with regular texture -> swapchain blit */
2980 } else if(srcSwapchain) {
2981 if(This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2982 ERR("Unexpected render target -> render target blit\n");
2984 FIXME("Implement blit from a swapchain to the active render target\n");
2985 return WINED3DERR_INVALIDCALL;
2988 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
2989 /* Blit from render target to texture */
2991 BOOL upsideDown, stretchx;
2993 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
2994 TRACE("Color keying not supported by frame buffer to texture blit\n");
2995 return WINED3DERR_INVALIDCALL;
2996 /* Destination color key is checked above */
2999 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3000 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3003 if(SrcRect->top < SrcRect->bottom) {
3004 srect.y1 = SrcRect->top;
3005 srect.y2 = SrcRect->bottom;
3008 srect.y1 = SrcRect->bottom;
3009 srect.y2 = SrcRect->top;
3012 srect.x1 = SrcRect->left;
3013 srect.x2 = SrcRect->right;
3017 srect.x2 = Src->currentDesc.Width;
3018 srect.y2 = Src->currentDesc.Height;
3021 if(rect.x1 > rect.x2) {
3025 upsideDown = !upsideDown;
3028 TRACE("Reading from an offscreen target\n");
3029 upsideDown = !upsideDown;
3032 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3038 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3039 * flip the image nor scale it.
3041 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3042 * -> If the app wants a image width an unscaled width, copy it line per line
3043 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3044 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3045 * back buffer. This is slower than reading line per line, thus not used for flipping
3046 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3049 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3050 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3053 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3054 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3055 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3056 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3057 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3058 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3059 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3061 TRACE("Using hardware stretching to flip / stretch the texture\n");
3062 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3065 if(!(This->Flags & SFLAG_DONOTFREE)) {
3066 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3067 This->resource.allocatedMemory = NULL;
3068 This->resource.heapMemory = NULL;
3070 This->Flags &= ~SFLAG_INSYSMEM;
3072 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3073 * path is never entered
3075 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3079 /* Blit from offscreen surface to render target */
3080 float glTexCoord[4];
3081 DWORD oldCKeyFlags = Src->CKeyFlags;
3082 WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3083 RECT SourceRectangle;
3085 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3088 SourceRectangle.left = SrcRect->left;
3089 SourceRectangle.right = SrcRect->right;
3090 SourceRectangle.top = SrcRect->top;
3091 SourceRectangle.bottom = SrcRect->bottom;
3093 SourceRectangle.left = 0;
3094 SourceRectangle.right = Src->currentDesc.Width;
3095 SourceRectangle.top = 0;
3096 SourceRectangle.bottom = Src->currentDesc.Height;
3099 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3100 /* Fall back to software */
3101 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3102 SourceRectangle.left, SourceRectangle.top,
3103 SourceRectangle.right, SourceRectangle.bottom);
3104 return WINED3DERR_INVALIDCALL;
3107 /* Color keying: Check if we have to do a color keyed blt,
3108 * and if not check if a color key is activated.
3110 * Just modify the color keying parameters in the surface and restore them afterwards
3111 * The surface keeps track of the color key last used to load the opengl surface.
3112 * PreLoad will catch the change to the flags and color key and reload if necessary.
3114 if(Flags & WINEDDBLT_KEYSRC) {
3115 /* Use color key from surface */
3116 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3117 /* Use color key from DDBltFx */
3118 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3119 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3121 /* Do not use color key */
3122 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3125 /* Now load the surface */
3126 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3129 /* Activate the destination context, set it up for blitting */
3130 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3133 glEnable(Src->glDescription.target);
3134 checkGLcall("glEnable(Src->glDescription.target)");
3137 TRACE("Drawing to offscreen buffer\n");
3138 glDrawBuffer(myDevice->offscreenBuffer);
3139 checkGLcall("glDrawBuffer");
3141 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3142 TRACE("Drawing to %#x buffer\n", buffer);
3143 glDrawBuffer(buffer);
3144 checkGLcall("glDrawBuffer");
3147 /* Bind the texture */
3148 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3149 checkGLcall("glBindTexture");
3151 /* Filtering for StretchRect */
3152 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3153 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3154 checkGLcall("glTexParameteri");
3155 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3156 minMipLookup[Filter][WINED3DTEXF_NONE]);
3157 checkGLcall("glTexParameteri");
3158 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3159 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3160 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3161 checkGLcall("glTexEnvi");
3163 /* This is for color keying */
3164 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3165 glEnable(GL_ALPHA_TEST);
3166 checkGLcall("glEnable GL_ALPHA_TEST");
3167 glAlphaFunc(GL_NOTEQUAL, 0.0);
3168 checkGLcall("glAlphaFunc\n");
3170 glDisable(GL_ALPHA_TEST);
3171 checkGLcall("glDisable GL_ALPHA_TEST");
3174 /* Draw a textured quad
3178 glColor3d(1.0f, 1.0f, 1.0f);
3179 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3184 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3185 glVertex3f(rect.x1, rect.y2, 0.0);
3187 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3192 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3197 checkGLcall("glEnd");
3199 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3200 glDisable(GL_ALPHA_TEST);
3201 checkGLcall("glDisable(GL_ALPHA_TEST)");
3204 /* Flush in case the drawable is used by multiple GL contexts */
3205 if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3208 glBindTexture(Src->glDescription.target, 0);
3209 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3210 /* Leave the opengl state valid for blitting */
3211 glDisable(Src->glDescription.target);
3212 checkGLcall("glDisable(Src->glDescription.target)");
3214 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3215 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3217 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3218 glDrawBuffer(GL_BACK);
3219 checkGLcall("glDrawBuffer");
3221 /* Restore the color key parameters */
3222 Src->CKeyFlags = oldCKeyFlags;
3223 This->SrcBltCKey = oldBltCKey;
3227 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3228 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3231 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3232 /* TODO: This should be moved to ModifyLocation() */
3233 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3234 This->Flags |= SFLAG_INTEXTURE;
3239 /* Source-Less Blit to render target */
3240 if (Flags & WINEDDBLT_COLORFILL) {
3241 /* This is easy to handle for the D3D Device... */
3244 TRACE("Colorfill\n");
3246 /* The color as given in the Blt function is in the format of the frame-buffer...
3247 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3249 if (This->resource.format == WINED3DFMT_P8) {
3250 if (This->palette) {
3251 color = ((0xFF000000) |
3252 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3253 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3254 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3259 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3260 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3263 color = ((0xFF000000) |
3264 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3265 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3266 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3269 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3270 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3271 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3273 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3274 color = DDBltFx->u5.dwFillColor;
3277 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3278 return WINED3DERR_INVALIDCALL;
3281 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_RESOURCELOAD);
3284 TRACE("Calling GetSwapChain with mydevice = %p\n", myDevice);
3285 if(dstSwapchain && dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]) {
3286 glDrawBuffer(GL_BACK);
3287 checkGLcall("glDrawBuffer(GL_BACK)");
3288 } else if (dstSwapchain && This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer) {
3289 glDrawBuffer(GL_FRONT);
3290 checkGLcall("glDrawBuffer(GL_FRONT)");
3291 } else if(This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3292 glDrawBuffer(myDevice->offscreenBuffer);
3293 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer3)");
3296 TRACE("Surface is higher back buffer, falling back to software\n");
3297 return WINED3DERR_INVALIDCALL;
3300 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3302 IWineD3DDevice_Clear( (IWineD3DDevice *) myDevice,
3303 1 /* Number of rectangles */,
3305 WINED3DCLEAR_TARGET,
3310 /* Restore the original draw buffer */
3312 glDrawBuffer(myDevice->offscreenBuffer);
3313 } else if(dstSwapchain->backBuffer && dstSwapchain->backBuffer[0]) {
3314 glDrawBuffer(GL_BACK);
3316 vcheckGLcall("glDrawBuffer");
3323 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3324 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3325 return WINED3DERR_INVALIDCALL;
3328 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3330 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3333 if (Flags & WINEDDBLT_DEPTHFILL) {
3334 switch(This->resource.format) {
3335 case WINED3DFMT_D16:
3336 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3338 case WINED3DFMT_D15S1:
3339 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3341 case WINED3DFMT_D24S8:
3342 case WINED3DFMT_D24X8:
3343 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3345 case WINED3DFMT_D32:
3346 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3350 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3353 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3354 DestRect == NULL ? 0 : 1,
3355 (WINED3DRECT *) DestRect,
3356 WINED3DCLEAR_ZBUFFER,
3362 FIXME("(%p): Unsupp depthstencil blit\n", This);
3363 return WINED3DERR_INVALIDCALL;
3366 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3367 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3368 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3369 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3370 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3371 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3373 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3374 * except depth blits, which seem to work
3376 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3377 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3378 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3379 return WINED3DERR_INVALIDCALL;
3380 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3381 TRACE("Z Blit override handled the blit\n");
3386 /* Special cases for RenderTargets */
3387 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3388 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3389 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3392 /* For the rest call the X11 surface implementation.
3393 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3394 * other Blts are rather rare
3396 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3399 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3400 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3401 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3402 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3403 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3405 if(myDevice->inScene &&
3406 (iface == myDevice->stencilBufferTarget ||
3407 (Source && Source == myDevice->stencilBufferTarget))) {
3408 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3409 return WINED3DERR_INVALIDCALL;
3412 /* Special cases for RenderTargets */
3413 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3414 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3416 RECT SrcRect, DstRect;
3420 SrcRect.left = rsrc->left;
3421 SrcRect.top= rsrc->top;
3422 SrcRect.bottom = rsrc->bottom;
3423 SrcRect.right = rsrc->right;
3427 SrcRect.right = srcImpl->currentDesc.Width;
3428 SrcRect.bottom = srcImpl->currentDesc.Height;
3431 DstRect.left = dstx;
3433 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3434 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3436 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3437 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3438 Flags |= WINEDDBLT_KEYSRC;
3439 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3440 Flags |= WINEDDBLT_KEYDEST;
3441 if(trans & WINEDDBLTFAST_WAIT)
3442 Flags |= WINEDDBLT_WAIT;
3443 if(trans & WINEDDBLTFAST_DONOTWAIT)
3444 Flags |= WINEDDBLT_DONOTWAIT;
3446 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3450 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3453 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3454 /** Check against the maximum texture sizes supported by the video card **/
3455 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3456 unsigned int pow2Width, pow2Height;
3457 const GlPixelFormatDesc *glDesc;
3459 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3460 /* Setup some glformat defaults */
3461 This->glDescription.glFormat = glDesc->glFormat;
3462 This->glDescription.glFormatInternal = glDesc->glInternal;
3463 This->glDescription.glType = glDesc->glType;
3465 This->glDescription.textureName = 0;
3466 This->glDescription.target = GL_TEXTURE_2D;
3468 /* Non-power2 support */
3469 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3470 pow2Width = This->currentDesc.Width;
3471 pow2Height = This->currentDesc.Height;
3473 /* Find the nearest pow2 match */
3474 pow2Width = pow2Height = 1;
3475 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3476 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3478 This->pow2Width = pow2Width;
3479 This->pow2Height = pow2Height;
3481 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3482 WINED3DFORMAT Format = This->resource.format;
3483 /** TODO: add support for non power two compressed textures **/
3484 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3485 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3486 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3487 This, This->currentDesc.Width, This->currentDesc.Height);
3488 return WINED3DERR_NOTAVAILABLE;
3492 if(pow2Width != This->currentDesc.Width ||
3493 pow2Height != This->currentDesc.Height) {
3494 This->Flags |= SFLAG_NONPOW2;
3497 TRACE("%p\n", This);
3498 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3499 /* one of three options
3500 1: Do the same as we do with nonpow 2 and scale the texture, (any texture ops would require the texture to be scaled which is potentially slow)
3501 2: Set the texture to the maximum size (bad idea)
3502 3: WARN and return WINED3DERR_NOTAVAILABLE;
3503 4: Create the surface, but allow it to be used only for DirectDraw Blts. Some apps(e.g. Swat 3) create textures with a Height of 16 and a Width > 3000 and blt 16x16 letter areas from them to the render target.
3505 WARN("(%p) Creating an oversized surface\n", This);
3506 This->Flags |= SFLAG_OVERSIZE;
3508 /* This will be initialized on the first blt */
3509 This->glRect.left = 0;
3510 This->glRect.top = 0;
3511 This->glRect.right = 0;
3512 This->glRect.bottom = 0;
3514 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one */
3515 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3516 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3517 This->pow2Width = This->currentDesc.Width;
3518 This->pow2Height = This->currentDesc.Height;
3519 This->Flags &= ~SFLAG_NONPOW2;
3522 /* No oversize, gl rect is the full texture size */
3523 This->Flags &= ~SFLAG_OVERSIZE;
3524 This->glRect.left = 0;
3525 This->glRect.top = 0;
3526 This->glRect.right = This->pow2Width;
3527 This->glRect.bottom = This->pow2Height;
3530 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3531 switch(wined3d_settings.offscreen_rendering_mode) {
3532 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3533 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3534 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3538 This->Flags |= SFLAG_INSYSMEM;
3543 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3544 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3545 IWineD3DBaseTexture *texture;
3547 TRACE("(%p)->(%s, %s)\n", iface,
3548 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3549 persistent ? "TRUE" : "FALSE");
3551 /* TODO: For offscreen textures with fbo offscreen rendering the drawable is the same as the texture.*/
3553 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3554 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3555 TRACE("Passing to container\n");
3556 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3557 IWineD3DBaseTexture_Release(texture);
3560 This->Flags &= ~SFLAG_LOCATIONS;
3561 This->Flags |= flag;
3563 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3564 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3565 TRACE("Passing to container\n");
3566 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3567 IWineD3DBaseTexture_Release(texture);
3570 This->Flags &= ~flag;
3578 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3579 struct coords coords[4];
3581 IWineD3DSwapChain *swapchain = NULL;
3582 IWineD3DBaseTexture *texture = NULL;
3584 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3591 rect.right = This->currentDesc.Width;
3592 rect.bottom = This->currentDesc.Height;
3595 ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3598 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3599 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3600 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3601 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3602 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3603 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3604 checkGLcall("glTexParameteri");
3605 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3606 checkGLcall("glTexParameteri");
3608 coords[0].x = rect.left;
3611 coords[1].x = rect.left;
3614 coords[2].x = rect.right;
3617 coords[3].x = rect.right;
3620 coords[0].y = rect.top;
3621 coords[1].y = rect.bottom;
3622 coords[2].y = rect.bottom;
3623 coords[3].y = rect.top;
3624 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3625 glEnable(GL_TEXTURE_2D);
3626 checkGLcall("glEnable(GL_TEXTURE_2D)");
3627 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3628 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3629 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3630 checkGLcall("glTexParameteri");
3631 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3632 checkGLcall("glTexParameteri");
3634 coords[0].x = rect.left / This->pow2Width;
3637 coords[1].x = rect.left / This->pow2Width;
3640 coords[2].x = rect.right / This->pow2Width;
3643 coords[3].x = rect.right / This->pow2Width;
3646 coords[0].y = rect.top / This->pow2Height;
3647 coords[1].y = rect.bottom / This->pow2Height;
3648 coords[2].y = rect.bottom / This->pow2Height;
3649 coords[3].y = rect.top / This->pow2Height;
3651 /* Must be a cube map */
3652 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3653 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3654 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3655 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3656 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3657 checkGLcall("glTexParameteri");
3658 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3659 checkGLcall("glTexParameteri");
3661 switch(This->glDescription.target) {
3662 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3663 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3664 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3665 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3666 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3669 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3670 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3671 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3672 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3673 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3676 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3677 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3678 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3679 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3680 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3683 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3684 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3685 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3686 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3687 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3690 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3691 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3692 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3693 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3694 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3697 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3698 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3699 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3700 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3701 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3704 ERR("Unexpected texture target\n");
3711 glTexCoord3iv((GLint *) &coords[0]);
3712 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3714 glTexCoord3iv((GLint *) &coords[1]);
3715 glVertex2i(0, device->render_offscreen ? rect.top : rect.bottom);
3717 glTexCoord3iv((GLint *) &coords[2]);
3718 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3720 glTexCoord3iv((GLint *) &coords[3]);
3721 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3723 checkGLcall("glEnd");
3725 if(This->glDescription.target != GL_TEXTURE_2D) {
3726 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3727 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3729 glDisable(GL_TEXTURE_2D);
3730 checkGLcall("glDisable(GL_TEXTURE_2D)");
3733 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3734 if(hr == WINED3D_OK && swapchain) {
3735 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3736 if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3739 IWineD3DSwapChain_Release(swapchain);
3741 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3742 * reset properly next draw
3744 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3745 if(hr == WINED3D_OK && texture) {
3746 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3747 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3748 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3749 IWineD3DBaseTexture_Release(texture);
3755 /*****************************************************************************
3756 * IWineD3DSurface::LoadLocation
3758 * Copies the current surface data from wherever it is to the requested
3759 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3760 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3761 * multiple locations, the gl texture is preferred over the drawable, which is
3762 * preferred over system memory. The PBO counts as system memory. If rect is
3763 * not NULL, only the specified rectangle is copied (only supported for
3764 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3765 * location is marked up to date after the copy.
3768 * flag: Surface location flag to be updated
3769 * rect: rectangle to be copied
3772 * WINED3D_OK on success
3773 * WINED3DERR_DEVICELOST on an internal error
3775 *****************************************************************************/
3776 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
3777 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3778 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3779 GLenum format, internal, type;
3780 CONVERT_TYPES convert;
3782 int width, pitch, outpitch;
3785 TRACE("(%p)->(%s, %p)\n", iface,
3786 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3789 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
3792 /* TODO: For fbo targets, texture == drawable */
3793 if(This->Flags & flag) {
3794 TRACE("Location already up to date\n");
3798 if(!(This->Flags & SFLAG_LOCATIONS)) {
3799 ERR("Surface does not have any up to date location\n");
3800 This->Flags |= SFLAG_LOST;
3801 return WINED3DERR_DEVICELOST;
3804 if(flag == SFLAG_INSYSMEM) {
3805 surface_prepare_system_memory(This);
3807 /* Download the surface to system memory */
3808 if(This->Flags & SFLAG_INTEXTURE) {
3809 surface_download_data(This);
3811 read_from_framebuffer(This, rect,
3812 This->resource.allocatedMemory,
3813 IWineD3DSurface_GetPitch(iface));
3815 } else if(flag == SFLAG_INDRAWABLE) {
3816 if(This->Flags & SFLAG_INTEXTURE) {
3817 surface_blt_to_drawable(This, rect);
3819 flush_to_framebuffer_drawpixels(This);
3821 } else /* if(flag == SFLAG_INTEXTURE) */ {
3822 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
3824 if (This->Flags & SFLAG_INDRAWABLE) {
3828 glGetIntegerv(GL_READ_BUFFER, &prevRead);
3829 vcheckGLcall("glGetIntegerv");
3830 glReadBuffer(This->resource.wineD3DDevice->offscreenBuffer);
3831 vcheckGLcall("glReadBuffer");
3833 if(!(This->Flags & SFLAG_ALLOCATED)) {
3834 surface_allocate_surface(This, internal, This->pow2Width,
3835 This->pow2Height, format, type);
3838 clear_unused_channels(This);
3840 glCopyTexSubImage2D(This->glDescription.target,
3841 This->glDescription.level,
3843 This->currentDesc.Width,
3844 This->currentDesc.Height);
3845 checkGLcall("glCopyTexSubImage2D");
3847 glReadBuffer(prevRead);
3848 vcheckGLcall("glReadBuffer");
3852 TRACE("Updated target %d\n", This->glDescription.target);
3854 /* The only place where LoadTexture() might get called when isInDraw=1
3855 * is ActivateContext where lastActiveRenderTarget is preloaded.
3857 if(iface == device->lastActiveRenderTarget && device->isInDraw)
3858 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
3860 /* Otherwise: System memory copy must be most up to date */
3862 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
3863 This->Flags |= SFLAG_GLCKEY;
3864 This->glCKey = This->SrcBltCKey;
3866 else This->Flags &= ~SFLAG_GLCKEY;
3868 /* The width is in 'length' not in bytes */
3869 width = This->currentDesc.Width;
3870 pitch = IWineD3DSurface_GetPitch(iface);
3872 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
3873 int height = This->currentDesc.Height;
3875 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
3876 outpitch = width * bpp;
3877 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
3879 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
3881 ERR("Out of memory %d, %d!\n", outpitch, height);
3882 return WINED3DERR_OUTOFVIDEOMEMORY;
3884 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
3886 This->Flags |= SFLAG_CONVERTED;
3887 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
3888 d3dfmt_p8_upload_palette(iface, convert);
3889 This->Flags &= ~SFLAG_CONVERTED;
3890 mem = This->resource.allocatedMemory;
3892 This->Flags &= ~SFLAG_CONVERTED;
3893 mem = This->resource.allocatedMemory;
3896 /* Make sure the correct pitch is used */
3897 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
3899 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
3900 TRACE("non power of two support\n");
3901 if(!(This->Flags & SFLAG_ALLOCATED)) {
3902 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
3904 if (mem || (This->Flags & SFLAG_PBO)) {
3905 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
3908 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
3909 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
3911 if(!(This->Flags & SFLAG_ALLOCATED)) {
3912 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
3914 if (mem || (This->Flags & SFLAG_PBO)) {
3915 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
3919 /* Restore the default pitch */
3920 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3922 /* Don't delete PBO memory */
3923 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
3924 HeapFree(GetProcessHeap(), 0, mem);
3929 This->Flags |= flag;
3935 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
3936 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3937 IWineD3DSwapChain *swapchain = NULL;
3939 /* Update the drawable size method */
3941 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
3944 This->get_drawable_size = get_drawable_size_swapchain;
3945 IWineD3DSwapChain_Release(swapchain);
3946 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3947 switch(wined3d_settings.offscreen_rendering_mode) {
3948 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3949 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3950 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3954 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
3957 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
3960 IWineD3DBaseSurfaceImpl_QueryInterface,
3961 IWineD3DBaseSurfaceImpl_AddRef,
3962 IWineD3DSurfaceImpl_Release,
3963 /* IWineD3DResource */
3964 IWineD3DBaseSurfaceImpl_GetParent,
3965 IWineD3DBaseSurfaceImpl_GetDevice,
3966 IWineD3DBaseSurfaceImpl_SetPrivateData,
3967 IWineD3DBaseSurfaceImpl_GetPrivateData,
3968 IWineD3DBaseSurfaceImpl_FreePrivateData,
3969 IWineD3DBaseSurfaceImpl_SetPriority,
3970 IWineD3DBaseSurfaceImpl_GetPriority,
3971 IWineD3DSurfaceImpl_PreLoad,
3972 IWineD3DBaseSurfaceImpl_GetType,
3973 /* IWineD3DSurface */
3974 IWineD3DBaseSurfaceImpl_GetContainer,
3975 IWineD3DBaseSurfaceImpl_GetDesc,
3976 IWineD3DSurfaceImpl_LockRect,
3977 IWineD3DSurfaceImpl_UnlockRect,
3978 IWineD3DSurfaceImpl_GetDC,
3979 IWineD3DSurfaceImpl_ReleaseDC,
3980 IWineD3DSurfaceImpl_Flip,
3981 IWineD3DSurfaceImpl_Blt,
3982 IWineD3DBaseSurfaceImpl_GetBltStatus,
3983 IWineD3DBaseSurfaceImpl_GetFlipStatus,
3984 IWineD3DBaseSurfaceImpl_IsLost,
3985 IWineD3DBaseSurfaceImpl_Restore,
3986 IWineD3DSurfaceImpl_BltFast,
3987 IWineD3DBaseSurfaceImpl_GetPalette,
3988 IWineD3DBaseSurfaceImpl_SetPalette,
3989 IWineD3DBaseSurfaceImpl_RealizePalette,
3990 IWineD3DBaseSurfaceImpl_SetColorKey,
3991 IWineD3DBaseSurfaceImpl_GetPitch,
3992 IWineD3DSurfaceImpl_SetMem,
3993 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
3994 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
3995 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
3996 IWineD3DBaseSurfaceImpl_UpdateOverlay,
3997 IWineD3DBaseSurfaceImpl_SetClipper,
3998 IWineD3DBaseSurfaceImpl_GetClipper,
4000 IWineD3DSurfaceImpl_AddDirtyRect,
4001 IWineD3DSurfaceImpl_LoadTexture,
4002 IWineD3DSurfaceImpl_BindTexture,
4003 IWineD3DSurfaceImpl_SaveSnapshot,
4004 IWineD3DSurfaceImpl_SetContainer,
4005 IWineD3DSurfaceImpl_SetGlTextureDesc,
4006 IWineD3DSurfaceImpl_GetGlDesc,
4007 IWineD3DSurfaceImpl_GetData,
4008 IWineD3DSurfaceImpl_SetFormat,
4009 IWineD3DSurfaceImpl_PrivateSetup,
4010 IWineD3DSurfaceImpl_ModifyLocation,
4011 IWineD3DSurfaceImpl_LoadLocation