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-2008 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);
38 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This);
40 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This) {
41 /* Make sure that a proper texture unit is selected, bind the texture
42 * and dirtify the sampler to restore the texture on the next draw. */
43 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
44 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
45 checkGLcall("glActiveTextureARB");
47 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(0));
48 IWineD3DSurface_BindTexture((IWineD3DSurface *)This);
51 /* This call just downloads data, the caller is responsible for activating the
52 * right context and binding the correct texture. */
53 static void surface_download_data(IWineD3DSurfaceImpl *This) {
54 if (0 == This->glDescription.textureName) {
55 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
59 /* Only support read back of converted P8 surfaces */
60 if(This->Flags & SFLAG_CONVERTED && (This->resource.format != WINED3DFMT_P8)) {
61 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
67 if (This->resource.format == WINED3DFMT_DXT1 ||
68 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
69 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
70 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
71 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
73 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
74 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
76 if(This->Flags & SFLAG_PBO) {
77 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
78 checkGLcall("glBindBufferARB");
79 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
80 checkGLcall("glGetCompressedTexImageARB()");
81 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
82 checkGLcall("glBindBufferARB");
84 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
85 checkGLcall("glGetCompressedTexImageARB()");
91 GLenum format = This->glDescription.glFormat;
92 GLenum type = This->glDescription.glType;
96 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
97 if(This->resource.format == WINED3DFMT_P8) {
99 type = GL_UNSIGNED_BYTE;
102 if (This->Flags & SFLAG_NONPOW2) {
103 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
104 src_pitch = This->bytesPerPixel * This->pow2Width;
105 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
106 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
107 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
109 mem = This->resource.allocatedMemory;
112 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
115 if(This->Flags & SFLAG_PBO) {
116 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
117 checkGLcall("glBindBufferARB");
119 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
121 checkGLcall("glGetTexImage()");
123 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
124 checkGLcall("glBindBufferARB");
126 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
128 checkGLcall("glGetTexImage()");
132 if (This->Flags & SFLAG_NONPOW2) {
133 LPBYTE src_data, dst_data;
136 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
137 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
138 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
140 * We're doing this...
142 * instead of boxing the texture :
143 * |<-texture width ->| -->pow2width| /\
144 * |111111111111111111| | |
145 * |222 Texture 222222| boxed empty | texture height
146 * |3333 Data 33333333| | |
147 * |444444444444444444| | \/
148 * ----------------------------------- |
149 * | boxed empty | boxed empty | pow2height
151 * -----------------------------------
154 * we're repacking the data to the expected texture width
156 * |<-texture width ->| -->pow2width| /\
157 * |111111111111111111222222222222222| |
158 * |222333333333333333333444444444444| texture height
162 * | empty | pow2height
164 * -----------------------------------
168 * |<-texture width ->| /\
169 * |111111111111111111|
170 * |222222222222222222|texture height
171 * |333333333333333333|
172 * |444444444444444444| \/
173 * --------------------
175 * this also means that any references to allocatedMemory should work with the data as if were a
176 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
178 * internally the texture is still stored in a boxed format so any references to textureName will
179 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
181 * Performance should not be an issue, because applications normally do not lock the surfaces when
182 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
183 * and doesn't have to be re-read.
186 dst_data = This->resource.allocatedMemory;
187 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
188 for (y = 1 ; y < This->currentDesc.Height; y++) {
189 /* skip the first row */
190 src_data += src_pitch;
191 dst_data += dst_pitch;
192 memcpy(dst_data, src_data, dst_pitch);
195 HeapFree(GetProcessHeap(), 0, mem);
199 /* Surface has now been downloaded */
200 This->Flags |= SFLAG_INSYSMEM;
203 /* This call just uploads data, the caller is responsible for activating the
204 * right context and binding the correct texture. */
205 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
206 if (This->resource.format == WINED3DFMT_DXT1 ||
207 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
208 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
209 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
210 FIXME("Using DXT1/3/5 without advertized support\n");
212 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
213 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
214 * function uses glCompressedTexImage2D instead of the SubImage call
216 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
219 if(This->Flags & SFLAG_PBO) {
220 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
221 checkGLcall("glBindBufferARB");
222 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
224 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
225 width, height, 0 /* border */, This->resource.size, NULL));
226 checkGLcall("glCompressedTexSubImage2D");
228 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
229 checkGLcall("glBindBufferARB");
231 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
232 width, height, 0 /* border */, This->resource.size, data));
233 checkGLcall("glCompressedTexSubImage2D");
238 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
241 if(This->Flags & SFLAG_PBO) {
242 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
243 checkGLcall("glBindBufferARB");
244 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
246 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
247 checkGLcall("glTexSubImage2D");
249 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
250 checkGLcall("glBindBufferARB");
253 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
254 checkGLcall("glTexSubImage2D");
261 /* This call just allocates the texture, the caller is responsible for
262 * activating the right context and binding the correct texture. */
263 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
264 BOOL enable_client_storage = FALSE;
267 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,
268 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
270 if (This->resource.format == WINED3DFMT_DXT1 ||
271 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
272 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
273 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
274 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
276 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
277 * once, unfortunately
279 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
280 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
281 This->Flags |= SFLAG_CLIENT;
282 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
283 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
284 width, height, 0 /* border */, This->resource.size, mem));
292 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
293 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
294 /* In some cases we want to disable client storage.
295 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
296 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
297 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
298 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
299 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
301 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
302 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
303 This->Flags &= ~SFLAG_CLIENT;
304 enable_client_storage = TRUE;
306 This->Flags |= SFLAG_CLIENT;
308 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
309 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
311 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
314 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
315 checkGLcall("glTexImage2D");
317 if(enable_client_storage) {
318 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
319 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
323 This->Flags |= SFLAG_ALLOCATED;
326 /* In D3D the depth stencil dimensions have to be greater than or equal to the
327 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
328 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
329 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
330 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
331 renderbuffer_entry_t *entry;
332 GLuint renderbuffer = 0;
333 unsigned int src_width, src_height;
335 src_width = This->pow2Width;
336 src_height = This->pow2Height;
338 /* A depth stencil smaller than the render target is not valid */
339 if (width > src_width || height > src_height) return;
341 /* Remove any renderbuffer set if the sizes match */
342 if (width == src_width && height == src_height) {
343 This->current_renderbuffer = NULL;
347 /* Look if we've already got a renderbuffer of the correct dimensions */
348 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
349 if (entry->width == width && entry->height == height) {
350 renderbuffer = entry->id;
351 This->current_renderbuffer = entry;
357 const GlPixelFormatDesc *glDesc;
358 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
360 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
361 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
362 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
364 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
365 entry->width = width;
366 entry->height = height;
367 entry->id = renderbuffer;
368 list_add_head(&This->renderbuffers, &entry->entry);
370 This->current_renderbuffer = entry;
373 checkGLcall("set_compatible_renderbuffer");
376 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
377 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
378 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
380 TRACE("(%p) : swapchain %p\n", This, swapchain);
382 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
383 TRACE("Returning GL_BACK\n");
385 } else if (swapchain_impl->frontBuffer == iface) {
386 TRACE("Returning GL_FRONT\n");
390 FIXME("Higher back buffer, returning GL_BACK\n");
394 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
395 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
396 ULONG ref = InterlockedDecrement(&This->resource.ref);
397 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
399 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
400 renderbuffer_entry_t *entry, *entry2;
401 TRACE("(%p) : cleaning up\n", This);
403 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
405 /* Need a context to destroy the texture. Use the currently active render target, but only if
406 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
407 * When destroying the primary rt, Uninit3D will activate a context before doing anything
409 if(device->render_targets && device->render_targets[0]) {
410 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
413 TRACE("Deleting texture %d\n", This->glDescription.textureName);
415 glDeleteTextures(1, &This->glDescription.textureName);
419 if(This->Flags & SFLAG_PBO) {
421 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
424 if(This->Flags & SFLAG_DIBSECTION) {
426 SelectObject(This->hDC, This->dib.holdbitmap);
428 /* Release the DIB section */
429 DeleteObject(This->dib.DIBsection);
430 This->dib.bitmap_data = NULL;
431 This->resource.allocatedMemory = NULL;
433 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
435 HeapFree(GetProcessHeap(), 0, This->palette9);
437 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
438 if(iface == device->ddraw_primary)
439 device->ddraw_primary = NULL;
441 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
442 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
443 HeapFree(GetProcessHeap(), 0, entry);
446 TRACE("(%p) Released\n", This);
447 HeapFree(GetProcessHeap(), 0, This);
453 /* ****************************************************
454 IWineD3DSurface IWineD3DResource parts follow
455 **************************************************** */
457 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
458 /* TODO: check for locks */
459 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
460 IWineD3DBaseTexture *baseTexture = NULL;
461 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
463 TRACE("(%p)Checking to see if the container is a base texture\n", This);
464 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
465 TRACE("Passing to container\n");
466 IWineD3DBaseTexture_PreLoad(baseTexture);
467 IWineD3DBaseTexture_Release(baseTexture);
469 TRACE("(%p) : About to load surface\n", This);
471 if(!device->isInDraw) {
472 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
476 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
477 if (!This->glDescription.level) {
478 if (!This->glDescription.textureName) {
479 glGenTextures(1, &This->glDescription.textureName);
480 checkGLcall("glGenTextures");
481 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
483 glBindTexture(This->glDescription.target, This->glDescription.textureName);
484 checkGLcall("glBindTexture");
485 IWineD3DSurface_LoadTexture(iface, FALSE);
486 /* This is where we should be reducing the amount of GLMemoryUsed */
487 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
488 /* assume this is a coding error not a real error for now */
489 FIXME("Mipmap surface has a glTexture bound to it!\n");
491 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
492 /* Tell opengl to try and keep this texture in video ram (well mostly) */
495 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
502 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
503 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
504 This->resource.allocatedMemory =
505 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
508 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
509 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
510 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
511 checkGLcall("glGetBufferSubData");
512 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
513 checkGLcall("glDeleteBuffers");
517 This->Flags &= ~SFLAG_PBO;
520 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
521 IWineD3DBaseTexture *texture = NULL;
522 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
523 renderbuffer_entry_t *entry, *entry2;
524 TRACE("(%p)\n", iface);
526 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
527 /* Default pool resources are supposed to be destroyed before Reset is called.
528 * Implicit resources stay however. So this means we have an implicit render target
529 * or depth stencil. The content may be destroyed, but we still have to tear down
530 * opengl resources, so we cannot leave early.
532 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
534 /* Load the surface into system memory */
535 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
537 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
538 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
539 This->Flags &= ~SFLAG_ALLOCATED;
541 /* Destroy PBOs, but load them into real sysmem before */
542 if(This->Flags & SFLAG_PBO) {
543 surface_remove_pbo(This);
546 /* Destroy fbo render buffers. This is needed for implicit render targets, for
547 * all application-created targets the application has to release the surface
548 * before calling _Reset
550 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
552 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
554 list_remove(&entry->entry);
555 HeapFree(GetProcessHeap(), 0, entry);
557 list_init(&This->renderbuffers);
558 This->current_renderbuffer = NULL;
560 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
563 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
566 glDeleteTextures(1, &This->glDescription.textureName);
567 This->glDescription.textureName = 0;
570 IWineD3DBaseTexture_Release(texture);
575 /* ******************************************************
576 IWineD3DSurface IWineD3DSurface parts follow
577 ****************************************************** */
579 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
580 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
581 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
582 if (This->glDescription.textureName == 0 && textureName != 0) {
583 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
584 IWineD3DSurface_AddDirtyRect(iface, NULL);
586 This->glDescription.textureName = textureName;
587 This->glDescription.target = target;
588 This->Flags &= ~SFLAG_ALLOCATED;
591 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
592 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
593 TRACE("(%p) : returning %p\n", This, &This->glDescription);
594 *glDescription = &This->glDescription;
597 /* TODO: think about moving this down to resource? */
598 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
599 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
600 /* 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 */
601 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
602 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
604 return (CONST void*)(This->resource.allocatedMemory);
607 /* Read the framebuffer back into the surface */
608 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
609 IWineD3DSwapChainImpl *swapchain;
610 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
614 BYTE *row, *top, *bottom;
618 BOOL srcIsUpsideDown;
620 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
621 static BOOL warned = FALSE;
623 ERR("The application tries to lock the render target, but render target locking is disabled\n");
629 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
630 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
631 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
632 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
633 * context->last_was_blit set on the unlock.
635 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
638 /* Select the correct read buffer, and give some debug output.
639 * There is no need to keep track of the current read buffer or reset it, every part of the code
640 * that reads sets the read buffer as desired.
643 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
644 * Read from the back buffer
646 TRACE("Locking offscreen render target\n");
647 glReadBuffer(myDevice->offscreenBuffer);
648 srcIsUpsideDown = TRUE;
650 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
651 TRACE("Locking %#x buffer\n", buffer);
652 glReadBuffer(buffer);
653 checkGLcall("glReadBuffer");
655 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
656 srcIsUpsideDown = FALSE;
659 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
663 local_rect.right = This->currentDesc.Width;
664 local_rect.bottom = This->currentDesc.Height;
668 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
670 switch(This->resource.format)
674 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
675 /* In case of P8 render targets the index is stored in the alpha component */
677 type = GL_UNSIGNED_BYTE;
679 bpp = This->bytesPerPixel;
681 /* GL can't return palettized data, so read ARGB pixels into a
682 * separate block of memory and convert them into palettized format
683 * in software. Slow, but if the app means to use palettized render
684 * targets and locks it...
686 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
687 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
688 * for the color channels when palettizing the colors.
691 type = GL_UNSIGNED_BYTE;
693 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
695 ERR("Out of memory\n");
699 bpp = This->bytesPerPixel * 3;
706 fmt = This->glDescription.glFormat;
707 type = This->glDescription.glType;
708 bpp = This->bytesPerPixel;
711 if(This->Flags & SFLAG_PBO) {
712 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
713 checkGLcall("glBindBufferARB");
716 glReadPixels(local_rect.left, local_rect.top,
717 local_rect.right - local_rect.left,
718 local_rect.bottom - local_rect.top,
720 vcheckGLcall("glReadPixels");
722 if(This->Flags & SFLAG_PBO) {
723 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
724 checkGLcall("glBindBufferARB");
726 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
727 * to get a pointer to it and perform the flipping in software. This is a lot
728 * faster than calling glReadPixels for each line. In case we want more speed
729 * we should rerender it flipped in a FBO and read the data back from the FBO. */
730 if(!srcIsUpsideDown) {
731 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
732 checkGLcall("glBindBufferARB");
734 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
735 checkGLcall("glMapBufferARB");
739 /* TODO: Merge this with the palettization loop below for P8 targets */
740 if(!srcIsUpsideDown) {
742 /* glReadPixels returns the image upside down, and there is no way to prevent this.
743 Flip the lines in software */
744 len = (local_rect.right - local_rect.left) * bpp;
745 off = local_rect.left * bpp;
747 row = HeapAlloc(GetProcessHeap(), 0, len);
749 ERR("Out of memory\n");
750 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
755 top = mem + pitch * local_rect.top;
756 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
757 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
758 memcpy(row, top + off, len);
759 memcpy(top + off, bottom + off, len);
760 memcpy(bottom + off, row, len);
764 HeapFree(GetProcessHeap(), 0, row);
766 /* Unmap the temp PBO buffer */
767 if(This->Flags & SFLAG_PBO) {
768 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
769 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
773 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
774 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
775 * the same color but we have no choice.
776 * In case of render targets, the index is stored in the alpha component so no conversion is needed.
778 if((This->resource.format == WINED3DFMT_P8) && !(This->resource.usage & WINED3DUSAGE_RENDERTARGET)) {
780 DWORD width = pitch / 3;
783 pal = This->palette->palents;
785 pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
788 for(y = local_rect.top; y < local_rect.bottom; y++) {
789 for(x = local_rect.left; x < local_rect.right; x++) {
790 /* start lines pixels */
791 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
792 BYTE *green = blue + 1;
793 BYTE *red = green + 1;
795 for(c = 0; c < 256; c++) {
796 if(*red == pal[c].peRed &&
797 *green == pal[c].peGreen &&
798 *blue == pal[c].peBlue)
800 *((BYTE *) dest + y * width + x) = c;
806 HeapFree(GetProcessHeap(), 0, mem);
811 /* Read the framebuffer contents into a texture */
812 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
814 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
815 IWineD3DSwapChainImpl *swapchain;
817 GLenum format, internal, type;
818 CONVERT_TYPES convert;
819 BOOL srcIsUpsideDown;
822 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
824 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
825 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
826 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
827 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
828 * context->last_was_blit set on the unlock.
830 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
831 surface_bind_and_dirtify(This);
834 glGetIntegerv(GL_READ_BUFFER, &prevRead);
836 /* Select the correct read buffer, and give some debug output.
837 * There is no need to keep track of the current read buffer or reset it, every part of the code
838 * that reads sets the read buffer as desired.
841 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
842 * Read from the back buffer
844 TRACE("Locking offscreen render target\n");
845 glReadBuffer(device->offscreenBuffer);
846 srcIsUpsideDown = TRUE;
848 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
849 TRACE("Locking %#x buffer\n", buffer);
850 glReadBuffer(buffer);
851 checkGLcall("glReadBuffer");
853 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
854 srcIsUpsideDown = FALSE;
857 if(!(This->Flags & SFLAG_ALLOCATED)) {
858 surface_allocate_surface(This, internal, This->pow2Width,
859 This->pow2Height, format, type);
862 clear_unused_channels(This);
864 /* If !SrcIsUpsideDown we should flip the surface.
865 * This can be done using glCopyTexSubImage2D but this
866 * is VERY slow, so don't do that. We should prevent
867 * this code from getting called in such cases or perhaps
870 glCopyTexSubImage2D(This->glDescription.target,
871 This->glDescription.level,
873 This->currentDesc.Width,
874 This->currentDesc.Height);
875 checkGLcall("glCopyTexSubImage2D");
877 glReadBuffer(prevRead);
878 vcheckGLcall("glReadBuffer");
881 TRACE("Updated target %d\n", This->glDescription.target);
884 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
885 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
886 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
889 if(!(This->Flags & SFLAG_DYNLOCK)) {
891 /* MAXLOCKCOUNT is defined in wined3d_private.h */
892 if(This->lockCount > MAXLOCKCOUNT) {
893 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
894 This->Flags |= SFLAG_DYNLOCK;
898 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
899 * Also don't create a PBO for systemmem surfaces.
901 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
905 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
906 error = glGetError();
907 if(This->pbo == 0 || error != GL_NO_ERROR) {
908 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
911 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
913 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
914 checkGLcall("glBindBufferARB");
916 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
917 checkGLcall("glBufferDataARB");
919 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
920 checkGLcall("glBindBufferARB");
922 /* We don't need the system memory anymore and we can't even use it for PBOs */
923 if(!(This->Flags & SFLAG_CLIENT)) {
924 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
925 This->resource.heapMemory = NULL;
927 This->resource.allocatedMemory = NULL;
928 This->Flags |= SFLAG_PBO;
930 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
931 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
934 if(!This->resource.heapMemory) {
935 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
937 This->resource.allocatedMemory =
938 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
939 if(This->Flags & SFLAG_INSYSMEM) {
940 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
945 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
946 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
947 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
948 IWineD3DSwapChain *swapchain = NULL;
950 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
952 /* This is also done in the base class, but we have to verify this before loading any data from
953 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
954 * may interfere, and all other bad things may happen
956 if (This->Flags & SFLAG_LOCKED) {
957 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
958 return WINED3DERR_INVALIDCALL;
960 This->Flags |= SFLAG_LOCKED;
962 if (!(This->Flags & SFLAG_LOCKABLE))
964 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
967 if (Flags & WINED3DLOCK_DISCARD) {
968 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
969 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
970 This->Flags |= SFLAG_INSYSMEM;
973 if (This->Flags & SFLAG_INSYSMEM) {
974 TRACE("Local copy is up to date, not downloading data\n");
975 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
979 /* Now download the surface content from opengl
980 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
981 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
983 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
984 if(swapchain || iface == myDevice->render_targets[0]) {
985 const RECT *pass_rect = pRect;
987 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
988 * because most caller functions do not need that. So do that here
993 pRect->right == This->currentDesc.Width &&
994 pRect->bottom == This->currentDesc.Height) {
998 switch(wined3d_settings.rendertargetlock_mode) {
1001 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1003 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1004 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1005 * This may be faster on some cards
1007 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1014 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1020 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1022 } else if(iface == myDevice->stencilBufferTarget) {
1023 /** the depth stencil in openGL has a format of GL_FLOAT
1024 * which should be good for WINED3DFMT_D16_LOCKABLE
1025 * and WINED3DFMT_D16
1026 * it is unclear what format the stencil buffer is in except.
1027 * 'Each index is converted to fixed point...
1028 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1029 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1030 * glReadPixels(This->lockedRect.left,
1031 * This->lockedRect.bottom - j - 1,
1032 * This->lockedRect.right - This->lockedRect.left,
1034 * GL_DEPTH_COMPONENT,
1036 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1038 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1039 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1040 * none of that is the case the problem is not in this function :-)
1041 ********************************************/
1042 FIXME("Depth stencil locking not supported yet\n");
1044 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1045 TRACE("locking an ordinary surface\n");
1046 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1050 if(This->Flags & SFLAG_PBO) {
1051 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1053 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1054 checkGLcall("glBindBufferARB");
1056 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1057 if(This->resource.allocatedMemory) {
1058 ERR("The surface already has PBO memory allocated!\n");
1061 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1062 checkGLcall("glMapBufferARB");
1064 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1065 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1066 checkGLcall("glBindBufferARB");
1071 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1074 IWineD3DBaseTexture *pBaseTexture;
1077 * as seen in msdn docs
1079 IWineD3DSurface_AddDirtyRect(iface, pRect);
1081 /** Dirtify Container if needed */
1082 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1083 TRACE("Making container dirty\n");
1084 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1085 IWineD3DBaseTexture_Release(pBaseTexture);
1087 TRACE("Surface is standalone, no need to dirty the container\n");
1091 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1094 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1096 GLint prev_rasterpos[4];
1097 GLint skipBytes = 0;
1098 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1099 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1100 IWineD3DSwapChainImpl *swapchain;
1102 /* Activate the correct context for the render target */
1103 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1106 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1108 /* Primary offscreen render target */
1109 TRACE("Offscreen render target\n");
1110 glDrawBuffer(myDevice->offscreenBuffer);
1111 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1113 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1114 TRACE("Unlocking %#x buffer\n", buffer);
1115 glDrawBuffer(buffer);
1116 checkGLcall("glDrawBuffer");
1118 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1122 vcheckGLcall("glFlush");
1123 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1124 vcheckGLcall("glIntegerv");
1125 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1126 vcheckGLcall("glIntegerv");
1127 glPixelZoom(1.0, -1.0);
1128 vcheckGLcall("glPixelZoom");
1130 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1131 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1132 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1134 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1135 vcheckGLcall("glRasterPos2f");
1137 /* Some drivers(radeon dri, others?) don't like exceptions during
1138 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1139 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1140 * catch to put the dib section in InSync mode, which leads to a crash
1141 * and a blocked x server on my radeon card.
1143 * The following lines read the dib section so it is put in inSync mode
1144 * before glDrawPixels is called and the crash is prevented. There won't
1145 * be any interfering gdi accesses, because UnlockRect is called from
1146 * ReleaseDC, and the app won't use the dc any more afterwards.
1148 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1150 read = This->resource.allocatedMemory[0];
1153 if(This->Flags & SFLAG_PBO) {
1154 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1155 checkGLcall("glBindBufferARB");
1158 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1159 (This->lockedRect.bottom - This->lockedRect.top)-1,
1161 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1162 checkGLcall("glDrawPixels");
1164 if(This->Flags & SFLAG_PBO) {
1165 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1166 checkGLcall("glBindBufferARB");
1169 glPixelZoom(1.0,1.0);
1170 vcheckGLcall("glPixelZoom");
1172 glRasterPos3iv(&prev_rasterpos[0]);
1173 vcheckGLcall("glRasterPos3iv");
1175 /* Reset to previous pack row length */
1176 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1177 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1180 glDrawBuffer(myDevice->offscreenBuffer);
1181 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1182 } else if(swapchain->backBuffer) {
1183 glDrawBuffer(GL_BACK);
1184 checkGLcall("glDrawBuffer(GL_BACK)");
1186 glDrawBuffer(GL_FRONT);
1187 checkGLcall("glDrawBuffer(GL_FRONT)");
1194 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1195 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1196 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1197 IWineD3DSwapChainImpl *swapchain = NULL;
1200 if (!(This->Flags & SFLAG_LOCKED)) {
1201 WARN("trying to Unlock an unlocked surf@%p\n", This);
1202 return WINED3DERR_INVALIDCALL;
1205 if (This->Flags & SFLAG_PBO) {
1206 TRACE("Freeing PBO memory\n");
1207 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1209 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1210 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1211 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1212 checkGLcall("glUnmapBufferARB");
1214 This->resource.allocatedMemory = NULL;
1217 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1219 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1220 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1224 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1225 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1226 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1228 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1229 static BOOL warned = FALSE;
1231 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1237 if(This->dirtyRect.left == 0 &&
1238 This->dirtyRect.top == 0 &&
1239 This->dirtyRect.right == This->currentDesc.Width &&
1240 This->dirtyRect.bottom == This->currentDesc.Height) {
1243 /* TODO: Proper partial rectangle tracking */
1244 fullsurface = FALSE;
1245 This->Flags |= SFLAG_INSYSMEM;
1248 switch(wined3d_settings.rendertargetlock_mode) {
1251 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1253 if (This->glDescription.textureName == 0) {
1254 glGenTextures(1, &This->glDescription.textureName);
1255 checkGLcall("glGenTextures");
1257 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1258 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1260 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1266 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1271 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1272 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1273 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1274 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1275 * not fully up to date because only a subrectangle was read in LockRect.
1277 This->Flags &= ~SFLAG_INSYSMEM;
1278 This->Flags |= SFLAG_INDRAWABLE;
1281 This->dirtyRect.left = This->currentDesc.Width;
1282 This->dirtyRect.top = This->currentDesc.Height;
1283 This->dirtyRect.right = 0;
1284 This->dirtyRect.bottom = 0;
1285 } else if(iface == myDevice->stencilBufferTarget) {
1286 FIXME("Depth Stencil buffer locking is not implemented\n");
1288 /* The rest should be a normal texture */
1289 IWineD3DBaseTextureImpl *impl;
1290 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1291 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1292 * states need resetting
1294 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1295 if(impl->baseTexture.bindCount) {
1296 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1298 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1303 This->Flags &= ~SFLAG_LOCKED;
1304 memset(&This->lockedRect, 0, sizeof(RECT));
1308 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1309 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1310 WINED3DLOCKED_RECT lock;
1314 TRACE("(%p)->(%p)\n",This,pHDC);
1316 if(This->Flags & SFLAG_USERPTR) {
1317 ERR("Not supported on surfaces with an application-provided surfaces\n");
1318 return WINEDDERR_NODC;
1321 /* Give more detailed info for ddraw */
1322 if (This->Flags & SFLAG_DCINUSE)
1323 return WINEDDERR_DCALREADYCREATED;
1325 /* Can't GetDC if the surface is locked */
1326 if (This->Flags & SFLAG_LOCKED)
1327 return WINED3DERR_INVALIDCALL;
1329 memset(&lock, 0, sizeof(lock)); /* To be sure */
1331 /* Create a DIB section if there isn't a hdc yet */
1333 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1334 if(This->Flags & SFLAG_CLIENT) {
1335 IWineD3DSurface_PreLoad(iface);
1338 /* Use the dib section from now on if we are not using a PBO */
1339 if(!(This->Flags & SFLAG_PBO))
1340 This->resource.allocatedMemory = This->dib.bitmap_data;
1343 /* Lock the surface */
1344 hr = IWineD3DSurface_LockRect(iface,
1349 if(This->Flags & SFLAG_PBO) {
1350 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1351 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1355 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1356 /* keep the dib section */
1360 if(This->resource.format == WINED3DFMT_P8 ||
1361 This->resource.format == WINED3DFMT_A8P8) {
1364 PALETTEENTRY ent[256];
1366 GetPaletteEntries(This->palette->hpal, 0, 256, ent);
1367 for (n=0; n<256; n++) {
1368 col[n].rgbRed = ent[n].peRed;
1369 col[n].rgbGreen = ent[n].peGreen;
1370 col[n].rgbBlue = ent[n].peBlue;
1371 col[n].rgbReserved = 0;
1374 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1376 for (n=0; n<256; n++) {
1377 col[n].rgbRed = device->palettes[device->currentPalette][n].peRed;
1378 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
1379 col[n].rgbBlue = device->palettes[device->currentPalette][n].peBlue;
1380 col[n].rgbReserved = 0;
1384 SetDIBColorTable(This->hDC, 0, 256, col);
1388 TRACE("returning %p\n",*pHDC);
1389 This->Flags |= SFLAG_DCINUSE;
1394 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1395 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1397 TRACE("(%p)->(%p)\n",This,hDC);
1399 if (!(This->Flags & SFLAG_DCINUSE))
1400 return WINED3DERR_INVALIDCALL;
1402 if (This->hDC !=hDC) {
1403 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1404 return WINED3DERR_INVALIDCALL;
1407 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1408 /* Copy the contents of the DIB over to the PBO */
1409 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1412 /* we locked first, so unlock now */
1413 IWineD3DSurface_UnlockRect(iface);
1415 This->Flags &= ~SFLAG_DCINUSE;
1420 /* ******************************************************
1421 IWineD3DSurface Internal (No mapping to directx api) parts follow
1422 ****************************************************** */
1424 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) {
1425 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1426 const GlPixelFormatDesc *glDesc;
1427 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1428 BOOL p8_render_target = FALSE;
1429 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1431 /* Default values: From the surface */
1432 *format = glDesc->glFormat;
1433 *type = glDesc->glType;
1434 *convert = NO_CONVERSION;
1435 *target_bpp = This->bytesPerPixel;
1438 *internal = glDesc->glGammaInternal;
1439 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1440 *internal = glDesc->rtInternal;
1442 *internal = glDesc->glInternal;
1445 /* Ok, now look if we have to do any conversion */
1446 switch(This->resource.format) {
1452 if (device->render_targets && device->render_targets[0]) {
1453 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1454 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
1455 p8_render_target = TRUE;
1458 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1459 * of the two is available make sure texturing is requested as neither of the two works in
1460 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1461 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1462 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1463 * conflicts with this.
1465 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && p8_render_target)) || colorkey_active || !use_texturing ) {
1467 *internal = GL_RGBA;
1468 *type = GL_UNSIGNED_BYTE;
1470 if(colorkey_active) {
1471 *convert = CONVERT_PALETTED_CK;
1473 *convert = CONVERT_PALETTED;
1476 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1478 *internal = GL_RGBA;
1479 *type = GL_UNSIGNED_BYTE;
1485 case WINED3DFMT_R3G3B2:
1486 /* **********************
1487 GL_UNSIGNED_BYTE_3_3_2
1488 ********************** */
1489 if (colorkey_active) {
1490 /* This texture format will never be used.. So do not care about color keying
1491 up until the point in time it will be needed :-) */
1492 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1496 case WINED3DFMT_R5G6B5:
1497 if (colorkey_active) {
1498 *convert = CONVERT_CK_565;
1500 *internal = GL_RGBA;
1501 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1505 case WINED3DFMT_X1R5G5B5:
1506 if (colorkey_active) {
1507 *convert = CONVERT_CK_5551;
1509 *internal = GL_RGBA;
1510 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1514 case WINED3DFMT_R8G8B8:
1515 if (colorkey_active) {
1516 *convert = CONVERT_CK_RGB24;
1518 *internal = GL_RGBA;
1519 *type = GL_UNSIGNED_INT_8_8_8_8;
1524 case WINED3DFMT_X8R8G8B8:
1525 if (colorkey_active) {
1526 *convert = CONVERT_RGB32_888;
1528 *internal = GL_RGBA;
1529 *type = GL_UNSIGNED_INT_8_8_8_8;
1533 case WINED3DFMT_V8U8:
1534 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1535 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1536 *format = GL_DUDV_ATI;
1537 *internal = GL_DU8DV8_ATI;
1539 /* No conversion - Just change the gl type */
1542 *convert = CONVERT_V8U8;
1544 *internal = GL_RGB8;
1545 *type = GL_UNSIGNED_BYTE;
1549 case WINED3DFMT_L6V5U5:
1550 *convert = CONVERT_L6V5U5;
1551 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1553 /* Use format and types from table */
1555 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1558 *internal = GL_RGB5;
1559 *type = GL_UNSIGNED_SHORT_5_6_5;
1563 case WINED3DFMT_X8L8V8U8:
1564 *convert = CONVERT_X8L8V8U8;
1566 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1567 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1568 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1569 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1570 * the needed type and format parameter, so the internal format contains a
1571 * 4th component, which is returned as alpha
1574 /* Not supported by GL_ATI_envmap_bumpmap */
1576 *internal = GL_RGB8;
1577 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1581 case WINED3DFMT_Q8W8V8U8:
1582 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1583 *convert = CONVERT_Q8W8V8U8;
1585 *internal = GL_RGBA8;
1586 *type = GL_UNSIGNED_BYTE;
1588 /* Not supported by GL_ATI_envmap_bumpmap */
1591 case WINED3DFMT_V16U16:
1592 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1593 *convert = CONVERT_V16U16;
1595 *internal = GL_RGB16_EXT;
1596 *type = GL_UNSIGNED_SHORT;
1598 /* What should I do here about GL_ATI_envmap_bumpmap?
1599 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1603 case WINED3DFMT_A4L4:
1604 /* A4L4 exists as an internal gl format, but for some reason there is not
1605 * format+type combination to load it. Thus convert it to A8L8, then load it
1606 * with A4L4 internal, but A8L8 format+type
1608 *convert = CONVERT_A4L4;
1609 *format = GL_LUMINANCE_ALPHA;
1610 *internal = GL_LUMINANCE4_ALPHA4;
1611 *type = GL_UNSIGNED_BYTE;
1615 case WINED3DFMT_R32F:
1616 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1617 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1618 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1621 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1623 *convert = CONVERT_R32F;
1625 *internal = GL_RGB32F_ARB;
1630 case WINED3DFMT_R16F:
1631 /* Similar to R32F */
1632 *convert = CONVERT_R16F;
1634 *internal = GL_RGB16F_ARB;
1635 *type = GL_HALF_FLOAT_ARB;
1639 case WINED3DFMT_G16R16:
1640 *convert = CONVERT_G16R16;
1642 *internal = GL_RGB16_EXT;
1643 *type = GL_UNSIGNED_SHORT;
1654 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1655 BYTE *source, *dest;
1656 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1661 memcpy(dst, src, pitch * height);
1664 case CONVERT_PALETTED:
1665 case CONVERT_PALETTED_CK:
1667 IWineD3DPaletteImpl* pal = This->palette;
1672 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1675 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1677 for (y = 0; y < height; y++)
1679 source = src + pitch * y;
1680 dest = dst + outpitch * y;
1681 /* This is an 1 bpp format, using the width here is fine */
1682 for (x = 0; x < width; x++) {
1683 BYTE color = *source++;
1684 *dest++ = table[color][0];
1685 *dest++ = table[color][1];
1686 *dest++ = table[color][2];
1687 *dest++ = table[color][3];
1693 case CONVERT_CK_565:
1695 /* Converting the 565 format in 5551 packed to emulate color-keying.
1697 Note : in all these conversion, it would be best to average the averaging
1698 pixels to get the color of the pixel that will be color-keyed to
1699 prevent 'color bleeding'. This will be done later on if ever it is
1702 Note2: Nvidia documents say that their driver does not support alpha + color keying
1703 on the same surface and disables color keying in such a case
1709 TRACE("Color keyed 565\n");
1711 for (y = 0; y < height; y++) {
1712 Source = (WORD *) (src + y * pitch);
1713 Dest = (WORD *) (dst + y * outpitch);
1714 for (x = 0; x < width; x++ ) {
1715 WORD color = *Source++;
1716 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1717 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1718 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1727 case CONVERT_CK_5551:
1729 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1733 TRACE("Color keyed 5551\n");
1734 for (y = 0; y < height; y++) {
1735 Source = (WORD *) (src + y * pitch);
1736 Dest = (WORD *) (dst + y * outpitch);
1737 for (x = 0; x < width; x++ ) {
1738 WORD color = *Source++;
1740 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1741 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1745 *Dest &= ~(1 << 15);
1757 unsigned char *Dest;
1758 for(y = 0; y < height; y++) {
1759 Source = (short *) (src + y * pitch);
1760 Dest = dst + y * outpitch;
1761 for (x = 0; x < width; x++ ) {
1762 long color = (*Source++);
1763 /* B */ Dest[0] = 0xff;
1764 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1765 /* R */ Dest[2] = (color) + 128; /* U */
1772 case CONVERT_V16U16:
1776 unsigned short *Dest;
1777 for(y = 0; y < height; y++) {
1778 Source = (DWORD *) (src + y * pitch);
1779 Dest = (unsigned short *) (dst + y * outpitch);
1780 for (x = 0; x < width; x++ ) {
1781 DWORD color = (*Source++);
1782 /* B */ Dest[0] = 0xffff;
1783 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1784 /* R */ Dest[2] = (color ) + 32768; /* U */
1791 case CONVERT_Q8W8V8U8:
1795 unsigned char *Dest;
1796 for(y = 0; y < height; y++) {
1797 Source = (DWORD *) (src + y * pitch);
1798 Dest = dst + y * outpitch;
1799 for (x = 0; x < width; x++ ) {
1800 long color = (*Source++);
1801 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1802 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1803 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1804 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1811 case CONVERT_L6V5U5:
1815 unsigned char *Dest;
1817 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1818 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1819 * fixed function and shaders without further conversion once the surface is
1822 for(y = 0; y < height; y++) {
1823 Source = (WORD *) (src + y * pitch);
1824 Dest = dst + y * outpitch;
1825 for (x = 0; x < width; x++ ) {
1826 short color = (*Source++);
1827 unsigned char l = ((color >> 10) & 0xfc);
1828 char v = ((color >> 5) & 0x3e);
1829 char u = ((color ) & 0x1f);
1831 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1832 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1833 * shift. GL reads a signed value and converts it into an unsigned value.
1835 /* M */ Dest[2] = l << 1;
1837 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1838 * from 5 bit values to 8 bit values.
1840 /* V */ Dest[1] = v << 3;
1841 /* U */ Dest[0] = u << 3;
1846 for(y = 0; y < height; y++) {
1847 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1848 Source = (WORD *) (src + y * pitch);
1849 for (x = 0; x < width; x++ ) {
1850 short color = (*Source++);
1851 unsigned char l = ((color >> 10) & 0xfc);
1852 short v = ((color >> 5) & 0x3e);
1853 short u = ((color ) & 0x1f);
1854 short v_conv = v + 16;
1855 short u_conv = u + 16;
1857 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1865 case CONVERT_X8L8V8U8:
1869 unsigned char *Dest;
1871 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1872 /* This implementation works with the fixed function pipeline and shaders
1873 * without further modification after converting the surface.
1875 for(y = 0; y < height; y++) {
1876 Source = (DWORD *) (src + y * pitch);
1877 Dest = dst + y * outpitch;
1878 for (x = 0; x < width; x++ ) {
1879 long color = (*Source++);
1880 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1881 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1882 /* U */ Dest[0] = (color & 0xff); /* U */
1883 /* I */ Dest[3] = 255; /* X */
1888 /* Doesn't work correctly with the fixed function pipeline, but can work in
1889 * shaders if the shader is adjusted. (There's no use for this format in gl's
1890 * standard fixed function pipeline anyway).
1892 for(y = 0; y < height; y++) {
1893 Source = (DWORD *) (src + y * pitch);
1894 Dest = dst + y * outpitch;
1895 for (x = 0; x < width; x++ ) {
1896 long color = (*Source++);
1897 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1898 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1899 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1910 unsigned char *Source;
1911 unsigned char *Dest;
1912 for(y = 0; y < height; y++) {
1913 Source = src + y * pitch;
1914 Dest = dst + y * outpitch;
1915 for (x = 0; x < width; x++ ) {
1916 unsigned char color = (*Source++);
1917 /* A */ Dest[1] = (color & 0xf0) << 0;
1918 /* L */ Dest[0] = (color & 0x0f) << 4;
1930 for(y = 0; y < height; y++) {
1931 Source = (float *) (src + y * pitch);
1932 Dest = (float *) (dst + y * outpitch);
1933 for (x = 0; x < width; x++ ) {
1934 float color = (*Source++);
1950 for(y = 0; y < height; y++) {
1951 Source = (WORD *) (src + y * pitch);
1952 Dest = (WORD *) (dst + y * outpitch);
1953 for (x = 0; x < width; x++ ) {
1954 WORD color = (*Source++);
1964 case CONVERT_G16R16:
1970 for(y = 0; y < height; y++) {
1971 Source = (WORD *) (src + y * pitch);
1972 Dest = (WORD *) (dst + y * outpitch);
1973 for (x = 0; x < width; x++ ) {
1974 WORD green = (*Source++);
1975 WORD red = (*Source++);
1986 ERR("Unsupported conversation type %d\n", convert);
1991 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
1992 IWineD3DPaletteImpl* pal = This->palette;
1993 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1994 BOOL index_in_alpha = FALSE;
1997 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
1998 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
1999 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2000 * duplicate entries. Store the color key in the unused alpha component to speed the
2001 * download up and to make conversion unneeded. */
2002 if (device->render_targets && device->render_targets[0]) {
2003 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
2005 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
2006 index_in_alpha = TRUE;
2010 /* Still no palette? Use the device's palette */
2011 /* Get the surface's palette */
2012 for (i = 0; i < 256; i++) {
2013 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2014 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2015 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2017 /* BltOverride uses a GL_ALPHA_TEST based on GL_NOT_EQUAL 0, so the alpha component
2018 of pixels that should be masked away should be 0. When inde_in_alpha is set,
2019 we will store the palette index (the glReadPixels code reads GL_ALPHA back)
2020 or else we store 0xff. */
2021 if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2023 } else if(index_in_alpha) {
2030 TRACE("Using surface palette %p\n", pal);
2031 /* Get the surface's palette */
2032 for (i = 0; i < 256; i++) {
2033 table[i][0] = pal->palents[i].peRed;
2034 table[i][1] = pal->palents[i].peGreen;
2035 table[i][2] = pal->palents[i].peBlue;
2037 /* BltOverride uses a GL_ALPHA_TEST based on GL_NOT_EQUAL 0, so the alpha component
2038 of pixels that should be masked away should be 0. When inde_in_alpha is set,
2039 we will store the palette index (the glReadPixels code reads GL_ALPHA back)
2040 or else we store 0xff. */
2041 if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2043 } else if(index_in_alpha) {
2045 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2046 table[i][3] = pal->palents[i].peFlags;
2054 const char *fragment_palette_conversion =
2057 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2058 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2059 "MAD index.a, index.a, 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 */
2060 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2063 /* This function is used in case of 8bit paletted textures to upload the palette.
2064 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2065 extensions like ATI_fragment_shaders is possible.
2067 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2068 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2070 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2072 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2074 /* Try to use the paletted texture extension */
2075 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2077 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2078 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2082 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2083 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2084 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2086 /* Create the fragment program if we don't have it */
2087 if(!device->paletteConversionShader)
2089 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2090 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2091 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2092 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2093 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2096 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2097 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2099 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2100 glEnable(GL_TEXTURE_1D);
2101 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2103 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2104 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2105 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2106 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2108 /* Switch back to unit 0 in which the 2D texture will be stored. */
2109 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2111 /* Rebind the texture because it isn't bound anymore */
2112 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2116 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2117 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2119 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2120 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2121 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2126 if(This->palette9) {
2127 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2131 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2133 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2137 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2138 GLboolean oldwrite[4];
2140 /* Some formats have only some color channels, and the others are 1.0.
2141 * since our rendering renders to all channels, and those pixel formats
2142 * are emulated by using a full texture with the other channels set to 1.0
2143 * manually, clear the unused channels.
2145 * This could be done with hacking colorwriteenable to mask the colors,
2146 * but before drawing the buffer would have to be cleared too, so there's
2149 switch(This->resource.format) {
2150 case WINED3DFMT_R16F:
2151 case WINED3DFMT_R32F:
2152 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2153 /* Do not activate a context, the correct drawable is active already
2154 * though just the read buffer is set, make sure to have the correct draw
2157 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2158 glDisable(GL_SCISSOR_TEST);
2159 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2160 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2161 glClearColor(0.0, 1.0, 1.0, 1.0);
2162 glClear(GL_COLOR_BUFFER_BIT);
2163 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2164 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2165 checkGLcall("Unused channel clear\n");
2172 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2173 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2175 if (!(This->Flags & SFLAG_INTEXTURE)) {
2176 TRACE("Reloading because surface is dirty\n");
2177 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2178 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2179 /* Reload: vice versa OR */
2180 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2181 /* Also reload: Color key is active AND the color key has changed */
2182 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2183 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2184 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2185 TRACE("Reloading because of color keying\n");
2186 /* To perform the color key conversion we need a sysmem copy of
2187 * the surface. Make sure we have it
2190 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2191 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2192 /* TODO: This is not necessarily needed with hw palettized texture support */
2193 This->Flags &= ~SFLAG_INTEXTURE;
2194 } else if(palette9_changed(This)) {
2195 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
2196 /* TODO: This is not necessarily needed with hw palettized texture support */
2197 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2199 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2200 This->Flags &= ~SFLAG_INTEXTURE;
2202 TRACE("surface is already in texture\n");
2206 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2207 * These resources are not bound by device size or format restrictions. Because of this,
2208 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2209 * However, these resources can always be created, locked, and copied.
2211 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2213 FIXME("(%p) Operation not supported for scratch textures\n",This);
2214 return WINED3DERR_INVALIDCALL;
2217 This->srgb = srgb_mode;
2218 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2222 static unsigned int gen = 0;
2225 if ((gen % 10) == 0) {
2226 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2227 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2230 * debugging crash code
2239 if (!(This->Flags & SFLAG_DONOTFREE)) {
2240 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2241 This->resource.allocatedMemory = NULL;
2242 This->resource.heapMemory = NULL;
2243 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2249 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2250 /* TODO: check for locks */
2251 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2252 IWineD3DBaseTexture *baseTexture = NULL;
2253 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2255 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2256 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2257 TRACE("Passing to container\n");
2258 IWineD3DBaseTexture_BindTexture(baseTexture);
2259 IWineD3DBaseTexture_Release(baseTexture);
2261 TRACE("(%p) : Binding surface\n", This);
2263 if(!device->isInDraw) {
2264 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2267 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2275 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2278 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2279 char *allocatedMemory;
2281 IWineD3DSwapChain *swapChain = NULL;
2283 GLuint tmpTexture = 0;
2286 Textures may not be stored in ->allocatedgMemory and a GlTexture
2287 so we should lock the surface before saving a snapshot, or at least check that
2289 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2290 by calling GetTexImage and in compressed form by calling
2291 GetCompressedTexImageARB. Queried compressed images can be saved and
2292 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2293 texture images do not need to be processed by the GL and should
2294 significantly improve texture loading performance relative to uncompressed
2297 /* Setup the width and height to be the internal texture width and height. */
2298 width = This->pow2Width;
2299 height = This->pow2Height;
2300 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2301 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2303 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2304 /* if were not a real texture then read the back buffer into a real texture */
2305 /* we don't want to interfere with the back buffer so read the data into a temporary
2306 * texture and then save the data out of the temporary texture
2310 TRACE("(%p) Reading render target into texture\n", This);
2311 glEnable(GL_TEXTURE_2D);
2313 glGenTextures(1, &tmpTexture);
2314 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2316 glTexImage2D(GL_TEXTURE_2D,
2323 GL_UNSIGNED_INT_8_8_8_8_REV,
2326 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2327 vcheckGLcall("glGetIntegerv");
2328 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2329 vcheckGLcall("glReadBuffer");
2330 glCopyTexImage2D(GL_TEXTURE_2D,
2339 checkGLcall("glCopyTexImage2D");
2340 glReadBuffer(prevRead);
2343 } else { /* bind the real texture, and make sure it up to date */
2344 IWineD3DSurface_PreLoad(iface);
2346 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2348 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2349 glGetTexImage(GL_TEXTURE_2D,
2350 This->glDescription.level,
2352 GL_UNSIGNED_INT_8_8_8_8_REV,
2354 checkGLcall("glTexImage2D");
2356 glBindTexture(GL_TEXTURE_2D, 0);
2357 glDeleteTextures(1, &tmpTexture);
2361 f = fopen(filename, "w+");
2363 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2364 return WINED3DERR_INVALIDCALL;
2366 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2367 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2382 fwrite(&width,2,1,f);
2384 fwrite(&height,2,1,f);
2389 /* 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 */
2391 textureRow = allocatedMemory + (width * (height - 1) *4);
2393 textureRow = allocatedMemory;
2394 for (y = 0 ; y < height; y++) {
2395 for (i = 0; i < width; i++) {
2396 color = *((DWORD*)textureRow);
2397 fputc((color >> 16) & 0xFF, f); /* B */
2398 fputc((color >> 8) & 0xFF, f); /* G */
2399 fputc((color >> 0) & 0xFF, f); /* R */
2400 fputc((color >> 24) & 0xFF, f); /* A */
2403 /* take two rows of the pointer to the texture memory */
2405 (textureRow-= width << 3);
2408 TRACE("Closing file\n");
2412 IWineD3DSwapChain_Release(swapChain);
2414 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2419 * Slightly inefficient way to handle multiple dirty rects but it works :)
2421 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2422 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2423 IWineD3DBaseTexture *baseTexture = NULL;
2425 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2426 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2428 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2429 if (NULL != pDirtyRect) {
2430 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2431 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2432 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2433 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2435 This->dirtyRect.left = 0;
2436 This->dirtyRect.top = 0;
2437 This->dirtyRect.right = This->currentDesc.Width;
2438 This->dirtyRect.bottom = This->currentDesc.Height;
2440 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2441 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2442 /* if the container is a basetexture then mark it dirty. */
2443 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2444 TRACE("Passing to container\n");
2445 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2446 IWineD3DBaseTexture_Release(baseTexture);
2451 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2452 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2454 const GlPixelFormatDesc *glDesc;
2455 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2457 TRACE("(%p) : Calling base function first\n", This);
2458 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2460 /* Setup some glformat defaults */
2461 This->glDescription.glFormat = glDesc->glFormat;
2462 This->glDescription.glFormatInternal = glDesc->glInternal;
2463 This->glDescription.glType = glDesc->glType;
2465 This->Flags &= ~SFLAG_ALLOCATED;
2466 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2467 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2472 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2473 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2475 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2476 WARN("Surface is locked or the HDC is in use\n");
2477 return WINED3DERR_INVALIDCALL;
2480 if(Mem && Mem != This->resource.allocatedMemory) {
2481 void *release = NULL;
2483 /* Do I have to copy the old surface content? */
2484 if(This->Flags & SFLAG_DIBSECTION) {
2485 /* Release the DC. No need to hold the critical section for the update
2486 * Thread because this thread runs only on front buffers, but this method
2487 * fails for render targets in the check above.
2489 SelectObject(This->hDC, This->dib.holdbitmap);
2490 DeleteDC(This->hDC);
2491 /* Release the DIB section */
2492 DeleteObject(This->dib.DIBsection);
2493 This->dib.bitmap_data = NULL;
2494 This->resource.allocatedMemory = NULL;
2496 This->Flags &= ~SFLAG_DIBSECTION;
2497 } else if(!(This->Flags & SFLAG_USERPTR)) {
2498 release = This->resource.heapMemory;
2499 This->resource.heapMemory = NULL;
2501 This->resource.allocatedMemory = Mem;
2502 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2504 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2505 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2507 /* For client textures opengl has to be notified */
2508 if(This->Flags & SFLAG_CLIENT) {
2509 This->Flags &= ~SFLAG_ALLOCATED;
2510 IWineD3DSurface_PreLoad(iface);
2511 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2514 /* Now free the old memory if any */
2515 HeapFree(GetProcessHeap(), 0, release);
2516 } else if(This->Flags & SFLAG_USERPTR) {
2517 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2518 This->resource.allocatedMemory = NULL;
2519 /* HeapMemory should be NULL already */
2520 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2521 This->Flags &= ~SFLAG_USERPTR;
2523 if(This->Flags & SFLAG_CLIENT) {
2524 This->Flags &= ~SFLAG_ALLOCATED;
2525 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2526 IWineD3DSurface_PreLoad(iface);
2532 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2533 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2534 IWineD3DSwapChainImpl *swapchain = NULL;
2536 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2538 /* Flipping is only supported on RenderTargets */
2539 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2542 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2543 * FIXME("(%p) Target override is not supported by now\n", This);
2544 * Additionally, it isn't really possible to support triple-buffering
2545 * properly on opengl at all
2549 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2551 ERR("Flipped surface is not on a swapchain\n");
2552 return WINEDDERR_NOTFLIPPABLE;
2555 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2556 * and only d3d8 and d3d9 apps specify the presentation interval
2558 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2559 /* Most common case first to avoid wasting time on all the other cases */
2560 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2561 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2562 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2563 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2564 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2565 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2566 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2568 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2571 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2572 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2573 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2577 /* Does a direct frame buffer -> texture copy. Stretching is done
2578 * with single pixel copy calls
2580 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2581 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2584 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2587 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2589 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2591 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2592 glEnable(This->glDescription.target);
2593 checkGLcall("glEnable(This->glDescription.target)");
2595 /* Bind the target texture */
2596 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2597 checkGLcall("glBindTexture");
2599 glReadBuffer(myDevice->offscreenBuffer);
2601 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2602 glReadBuffer(buffer);
2604 checkGLcall("glReadBuffer");
2606 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2607 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2609 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2610 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2612 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2613 ERR("Texture filtering not supported in direct blit\n");
2615 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2616 ERR("Texture filtering not supported in direct blit\n");
2620 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2621 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2622 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2624 glCopyTexSubImage2D(This->glDescription.target,
2625 This->glDescription.level,
2626 drect->x1, drect->y1, /* xoffset, yoffset */
2627 srect->x1, Src->currentDesc.Height - srect->y2,
2628 drect->x2 - drect->x1, drect->y2 - drect->y1);
2630 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2631 /* I have to process this row by row to swap the image,
2632 * otherwise it would be upside down, so stretching in y direction
2633 * doesn't cost extra time
2635 * However, stretching in x direction can be avoided if not necessary
2637 for(row = drect->y1; row < drect->y2; row++) {
2638 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2639 /* Well, that stuff works, but it's very slow.
2640 * find a better way instead
2644 for(col = drect->x1; col < drect->x2; col++) {
2645 glCopyTexSubImage2D(This->glDescription.target,
2646 This->glDescription.level,
2647 drect->x1 + col, row, /* xoffset, yoffset */
2648 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2652 glCopyTexSubImage2D(This->glDescription.target,
2653 This->glDescription.level,
2654 drect->x1, row, /* xoffset, yoffset */
2655 srect->x1, yoffset - (int) (row * yrel),
2656 drect->x2-drect->x1, 1);
2660 vcheckGLcall("glCopyTexSubImage2D");
2662 /* Leave the opengl state valid for blitting */
2663 glDisable(This->glDescription.target);
2664 checkGLcall("glDisable(This->glDescription.target)");
2669 /* Uses the hardware to stretch and flip the image */
2670 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2671 GLuint src, backup = 0;
2672 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2673 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2674 float left, right, top, bottom; /* Texture coordinates */
2675 UINT fbwidth = Src->currentDesc.Width;
2676 UINT fbheight = Src->currentDesc.Height;
2677 GLenum drawBuffer = GL_BACK;
2678 GLenum texture_target;
2680 TRACE("Using hwstretch blit\n");
2681 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2682 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2685 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2687 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2688 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2690 if(GL_LIMITS(aux_buffers) >= 2) {
2691 /* Got more than one aux buffer? Use the 2nd aux buffer */
2692 drawBuffer = GL_AUX1;
2693 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2694 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2695 drawBuffer = GL_AUX0;
2698 if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2699 glGenTextures(1, &backup);
2700 checkGLcall("glGenTextures\n");
2701 glBindTexture(GL_TEXTURE_2D, backup);
2702 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2703 texture_target = GL_TEXTURE_2D;
2705 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2706 * we are reading from the back buffer, the backup can be used as source texture
2708 if(Src->glDescription.textureName == 0) {
2709 /* Get it a description */
2710 IWineD3DSurface_PreLoad(SrcSurface);
2712 texture_target = Src->glDescription.target;
2713 glBindTexture(texture_target, Src->glDescription.textureName);
2714 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2715 glEnable(texture_target);
2716 checkGLcall("glEnable(texture_target)");
2718 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2719 Src->Flags &= ~SFLAG_INTEXTURE;
2722 glReadBuffer(GL_BACK);
2723 checkGLcall("glReadBuffer(GL_BACK)");
2725 /* TODO: Only back up the part that will be overwritten */
2726 glCopyTexSubImage2D(texture_target, 0,
2727 0, 0 /* read offsets */,
2732 checkGLcall("glCopyTexSubImage2D");
2734 /* No issue with overriding these - the sampler is dirty due to blit usage */
2735 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2736 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2737 checkGLcall("glTexParameteri");
2738 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2739 minMipLookup[Filter][WINED3DTEXF_NONE]);
2740 checkGLcall("glTexParameteri");
2742 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2743 src = backup ? backup : Src->glDescription.textureName;
2745 glReadBuffer(GL_FRONT);
2746 checkGLcall("glReadBuffer(GL_FRONT)");
2748 glGenTextures(1, &src);
2749 checkGLcall("glGenTextures(1, &src)");
2750 glBindTexture(GL_TEXTURE_2D, src);
2751 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2753 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2754 * out for power of 2 sizes
2756 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2757 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2758 checkGLcall("glTexImage2D");
2759 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2760 0, 0 /* read offsets */,
2765 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2766 checkGLcall("glTexParameteri");
2767 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2768 checkGLcall("glTexParameteri");
2770 glReadBuffer(GL_BACK);
2771 checkGLcall("glReadBuffer(GL_BACK)");
2773 if(texture_target != GL_TEXTURE_2D) {
2774 glDisable(texture_target);
2775 glEnable(GL_TEXTURE_2D);
2776 texture_target = GL_TEXTURE_2D;
2779 checkGLcall("glEnd and previous");
2781 left = (float) srect->x1 / (float) Src->pow2Width;
2782 right = (float) srect->x2 / (float) Src->pow2Width;
2785 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2786 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2788 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2789 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2792 /* draw the source texture stretched and upside down. The correct surface is bound already */
2793 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2794 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2796 glDrawBuffer(drawBuffer);
2797 glReadBuffer(drawBuffer);
2801 glTexCoord2f(left, bottom);
2802 glVertex2i(0, fbheight);
2805 glTexCoord2f(left, top);
2806 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2809 glTexCoord2f(right, top);
2810 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2813 glTexCoord2f(right, bottom);
2814 glVertex2i(drect->x2 - drect->x1, fbheight);
2816 checkGLcall("glEnd and previous");
2818 if(texture_target != This->glDescription.target) {
2819 glDisable(texture_target);
2820 glEnable(This->glDescription.target);
2821 texture_target = This->glDescription.target;
2824 /* Now read the stretched and upside down image into the destination texture */
2825 glBindTexture(texture_target, This->glDescription.textureName);
2826 checkGLcall("glBindTexture");
2827 glCopyTexSubImage2D(texture_target,
2829 drect->x1, drect->y1, /* xoffset, yoffset */
2830 0, 0, /* We blitted the image to the origin */
2831 drect->x2 - drect->x1, drect->y2 - drect->y1);
2832 checkGLcall("glCopyTexSubImage2D");
2834 if(drawBuffer == GL_BACK) {
2835 /* Write the back buffer backup back */
2837 if(texture_target != GL_TEXTURE_2D) {
2838 glDisable(texture_target);
2839 glEnable(GL_TEXTURE_2D);
2840 texture_target = GL_TEXTURE_2D;
2842 glBindTexture(GL_TEXTURE_2D, backup);
2843 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2845 if(texture_target != Src->glDescription.target) {
2846 glDisable(texture_target);
2847 glEnable(Src->glDescription.target);
2848 texture_target = Src->glDescription.target;
2850 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2851 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2856 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2860 glTexCoord2f(0.0, 0.0);
2861 glVertex2i(0, fbheight);
2864 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2865 glVertex2i(fbwidth, Src->currentDesc.Height);
2868 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2869 glVertex2i(fbwidth, 0);
2872 /* Restore the old draw buffer */
2873 glDrawBuffer(GL_BACK);
2875 glDisable(texture_target);
2876 checkGLcall("glDisable(texture_target)");
2879 if(src != Src->glDescription.textureName && src != backup) {
2880 glDeleteTextures(1, &src);
2881 checkGLcall("glDeleteTextures(1, &src)");
2884 glDeleteTextures(1, &backup);
2885 checkGLcall("glDeleteTextures(1, &backup)");
2891 /* Not called from the VTable */
2892 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2894 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2895 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2896 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2898 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2900 /* Get the swapchain. One of the surfaces has to be a primary surface */
2901 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2902 WARN("Destination is in sysmem, rejecting gl blt\n");
2903 return WINED3DERR_INVALIDCALL;
2905 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2906 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2908 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2909 WARN("Src is in sysmem, rejecting gl blt\n");
2910 return WINED3DERR_INVALIDCALL;
2912 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2913 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2916 /* Early sort out of cases where no render target is used */
2917 if(!dstSwapchain && !srcSwapchain &&
2918 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2919 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2920 return WINED3DERR_INVALIDCALL;
2923 /* No destination color keying supported */
2924 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2925 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2926 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2927 return WINED3DERR_INVALIDCALL;
2931 rect.x1 = DestRect->left;
2932 rect.y1 = DestRect->top;
2933 rect.x2 = DestRect->right;
2934 rect.y2 = DestRect->bottom;
2938 rect.x2 = This->currentDesc.Width;
2939 rect.y2 = This->currentDesc.Height;
2942 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2943 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
2944 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
2945 /* Half-life does a Blt from the back buffer to the front buffer,
2946 * Full surface size, no flags... Use present instead
2948 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
2951 /* Check rects - IWineD3DDevice_Present doesn't handle them */
2955 TRACE("Looking if a Present can be done...\n");
2956 /* Source Rectangle must be full surface */
2958 if(SrcRect->left != 0 || SrcRect->top != 0 ||
2959 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
2960 TRACE("No, Source rectangle doesn't match\n");
2966 mySrcRect.right = Src->currentDesc.Width;
2967 mySrcRect.bottom = Src->currentDesc.Height;
2969 /* No stretching may occur */
2970 if(mySrcRect.right != rect.x2 - rect.x1 ||
2971 mySrcRect.bottom != rect.y2 - rect.y1) {
2972 TRACE("No, stretching is done\n");
2976 /* Destination must be full surface or match the clipping rectangle */
2977 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
2981 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
2986 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
2989 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
2990 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
2992 TRACE("No, dest rectangle doesn't match(clipper)\n");
2993 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
2994 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3000 if(rect.x1 != 0 || rect.y1 != 0 ||
3001 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3002 TRACE("No, dest rectangle doesn't match(surface size)\n");
3009 /* These flags are unimportant for the flag check, remove them */
3010 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3011 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3013 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3014 * take very long, while a flip is fast.
3015 * This applies to Half-Life, which does such Blts every time it finished
3016 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3017 * menu. This is also used by all apps when they do windowed rendering
3019 * The problem is that flipping is not really the same as copying. After a
3020 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3021 * untouched. Therefore it's necessary to override the swap effect
3022 * and to set it back after the flip.
3024 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3028 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3029 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3031 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3032 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3034 dstSwapchain->presentParms.SwapEffect = orig_swap;
3041 TRACE("Unsupported blit between buffers on the same swapchain\n");
3042 return WINED3DERR_INVALIDCALL;
3043 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3044 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3045 return WINED3DERR_INVALIDCALL;
3046 } else if(dstSwapchain && srcSwapchain) {
3047 FIXME("Implement hardware blit between two different swapchains\n");
3048 return WINED3DERR_INVALIDCALL;
3049 } else if(dstSwapchain) {
3050 if(SrcSurface == myDevice->render_targets[0]) {
3051 TRACE("Blit from active render target to a swapchain\n");
3052 /* Handled with regular texture -> swapchain blit */
3054 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3055 FIXME("Implement blit from a swapchain to the active render target\n");
3056 return WINED3DERR_INVALIDCALL;
3059 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3060 /* Blit from render target to texture */
3062 BOOL upsideDown, stretchx;
3064 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3065 TRACE("Color keying not supported by frame buffer to texture blit\n");
3066 return WINED3DERR_INVALIDCALL;
3067 /* Destination color key is checked above */
3070 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3071 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3074 if(SrcRect->top < SrcRect->bottom) {
3075 srect.y1 = SrcRect->top;
3076 srect.y2 = SrcRect->bottom;
3079 srect.y1 = SrcRect->bottom;
3080 srect.y2 = SrcRect->top;
3083 srect.x1 = SrcRect->left;
3084 srect.x2 = SrcRect->right;
3088 srect.x2 = Src->currentDesc.Width;
3089 srect.y2 = Src->currentDesc.Height;
3092 if(rect.x1 > rect.x2) {
3096 upsideDown = !upsideDown;
3099 TRACE("Reading from an offscreen target\n");
3100 upsideDown = !upsideDown;
3103 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3109 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3110 * flip the image nor scale it.
3112 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3113 * -> If the app wants a image width an unscaled width, copy it line per line
3114 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3115 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3116 * back buffer. This is slower than reading line per line, thus not used for flipping
3117 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3120 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3121 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3124 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3125 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3126 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3127 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3128 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3129 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3130 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3132 TRACE("Using hardware stretching to flip / stretch the texture\n");
3133 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3136 if(!(This->Flags & SFLAG_DONOTFREE)) {
3137 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3138 This->resource.allocatedMemory = NULL;
3139 This->resource.heapMemory = NULL;
3141 This->Flags &= ~SFLAG_INSYSMEM;
3143 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3144 * path is never entered
3146 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3150 /* Blit from offscreen surface to render target */
3151 float glTexCoord[4];
3152 DWORD oldCKeyFlags = Src->CKeyFlags;
3153 WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3154 RECT SourceRectangle;
3156 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3159 SourceRectangle.left = SrcRect->left;
3160 SourceRectangle.right = SrcRect->right;
3161 SourceRectangle.top = SrcRect->top;
3162 SourceRectangle.bottom = SrcRect->bottom;
3164 SourceRectangle.left = 0;
3165 SourceRectangle.right = Src->currentDesc.Width;
3166 SourceRectangle.top = 0;
3167 SourceRectangle.bottom = Src->currentDesc.Height;
3169 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3170 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3171 TRACE("Using stretch_rect_fbo\n");
3172 /* The source is always a texture, but never the currently active render target, and the texture
3173 * contents are never upside down
3175 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3176 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3180 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3181 /* Fall back to software */
3182 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3183 SourceRectangle.left, SourceRectangle.top,
3184 SourceRectangle.right, SourceRectangle.bottom);
3185 return WINED3DERR_INVALIDCALL;
3188 /* Color keying: Check if we have to do a color keyed blt,
3189 * and if not check if a color key is activated.
3191 * Just modify the color keying parameters in the surface and restore them afterwards
3192 * The surface keeps track of the color key last used to load the opengl surface.
3193 * PreLoad will catch the change to the flags and color key and reload if necessary.
3195 if(Flags & WINEDDBLT_KEYSRC) {
3196 /* Use color key from surface */
3197 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3198 /* Use color key from DDBltFx */
3199 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3200 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3202 /* Do not use color key */
3203 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3206 /* Now load the surface */
3207 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3210 /* Activate the destination context, set it up for blitting */
3211 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3214 glEnable(Src->glDescription.target);
3215 checkGLcall("glEnable(Src->glDescription.target)");
3218 TRACE("Drawing to offscreen buffer\n");
3219 glDrawBuffer(myDevice->offscreenBuffer);
3220 checkGLcall("glDrawBuffer");
3222 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3223 TRACE("Drawing to %#x buffer\n", buffer);
3224 glDrawBuffer(buffer);
3225 checkGLcall("glDrawBuffer");
3228 /* Bind the texture */
3229 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3230 checkGLcall("glBindTexture");
3232 /* Filtering for StretchRect */
3233 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3234 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3235 checkGLcall("glTexParameteri");
3236 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3237 minMipLookup[Filter][WINED3DTEXF_NONE]);
3238 checkGLcall("glTexParameteri");
3239 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3240 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3241 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3242 checkGLcall("glTexEnvi");
3244 /* This is for color keying */
3245 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3246 glEnable(GL_ALPHA_TEST);
3247 checkGLcall("glEnable GL_ALPHA_TEST");
3248 glAlphaFunc(GL_NOTEQUAL, 0.0);
3249 checkGLcall("glAlphaFunc\n");
3251 glDisable(GL_ALPHA_TEST);
3252 checkGLcall("glDisable GL_ALPHA_TEST");
3255 /* Draw a textured quad
3259 glColor3d(1.0f, 1.0f, 1.0f);
3260 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3265 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3266 glVertex3f(rect.x1, rect.y2, 0.0);
3268 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3273 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3278 checkGLcall("glEnd");
3280 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3281 glDisable(GL_ALPHA_TEST);
3282 checkGLcall("glDisable(GL_ALPHA_TEST)");
3285 /* Flush in case the drawable is used by multiple GL contexts */
3286 if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3289 glBindTexture(Src->glDescription.target, 0);
3290 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3291 /* Leave the opengl state valid for blitting */
3292 glDisable(Src->glDescription.target);
3293 checkGLcall("glDisable(Src->glDescription.target)");
3295 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3296 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3298 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3299 glDrawBuffer(GL_BACK);
3300 checkGLcall("glDrawBuffer");
3302 /* Restore the color key parameters */
3303 Src->CKeyFlags = oldCKeyFlags;
3304 This->SrcBltCKey = oldBltCKey;
3308 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3309 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3312 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3313 /* TODO: This should be moved to ModifyLocation() */
3314 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3315 This->Flags |= SFLAG_INTEXTURE;
3320 /* Source-Less Blit to render target */
3321 if (Flags & WINEDDBLT_COLORFILL) {
3322 /* This is easy to handle for the D3D Device... */
3325 TRACE("Colorfill\n");
3327 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3328 must be true if we are here */
3329 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3330 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3331 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3332 TRACE("Surface is higher back buffer, falling back to software\n");
3333 return WINED3DERR_INVALIDCALL;
3336 /* The color as given in the Blt function is in the format of the frame-buffer...
3337 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3339 if (This->resource.format == WINED3DFMT_P8) {
3340 if (This->palette) {
3341 color = ((0xFF000000) |
3342 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3343 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3344 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3349 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3350 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3353 color = ((0xFF000000) |
3354 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3355 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3356 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3359 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3360 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3361 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3363 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3364 color = DDBltFx->u5.dwFillColor;
3367 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3368 return WINED3DERR_INVALIDCALL;
3371 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3372 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3373 1, /* Number of rectangles */
3374 &rect, WINED3DCLEAR_TARGET, color,
3381 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3382 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3383 return WINED3DERR_INVALIDCALL;
3386 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3388 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3391 if (Flags & WINEDDBLT_DEPTHFILL) {
3392 switch(This->resource.format) {
3393 case WINED3DFMT_D16:
3394 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3396 case WINED3DFMT_D15S1:
3397 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3399 case WINED3DFMT_D24S8:
3400 case WINED3DFMT_D24X8:
3401 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3403 case WINED3DFMT_D32:
3404 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3408 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3411 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3412 DestRect == NULL ? 0 : 1,
3413 (WINED3DRECT *) DestRect,
3414 WINED3DCLEAR_ZBUFFER,
3420 FIXME("(%p): Unsupp depthstencil blit\n", This);
3421 return WINED3DERR_INVALIDCALL;
3424 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3425 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3426 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3427 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3428 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3429 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3431 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3432 * except depth blits, which seem to work
3434 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3435 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3436 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3437 return WINED3DERR_INVALIDCALL;
3438 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3439 TRACE("Z Blit override handled the blit\n");
3444 /* Special cases for RenderTargets */
3445 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3446 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3447 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3450 /* For the rest call the X11 surface implementation.
3451 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3452 * other Blts are rather rare
3454 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3457 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3458 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3459 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3460 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3461 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3463 if(myDevice->inScene &&
3464 (iface == myDevice->stencilBufferTarget ||
3465 (Source && Source == myDevice->stencilBufferTarget))) {
3466 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3467 return WINED3DERR_INVALIDCALL;
3470 /* Special cases for RenderTargets */
3471 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3472 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3474 RECT SrcRect, DstRect;
3478 SrcRect.left = rsrc->left;
3479 SrcRect.top= rsrc->top;
3480 SrcRect.bottom = rsrc->bottom;
3481 SrcRect.right = rsrc->right;
3485 SrcRect.right = srcImpl->currentDesc.Width;
3486 SrcRect.bottom = srcImpl->currentDesc.Height;
3489 DstRect.left = dstx;
3491 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3492 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3494 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3495 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3496 Flags |= WINEDDBLT_KEYSRC;
3497 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3498 Flags |= WINEDDBLT_KEYDEST;
3499 if(trans & WINEDDBLTFAST_WAIT)
3500 Flags |= WINEDDBLT_WAIT;
3501 if(trans & WINEDDBLTFAST_DONOTWAIT)
3502 Flags |= WINEDDBLT_DONOTWAIT;
3504 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3508 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3511 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3512 /** Check against the maximum texture sizes supported by the video card **/
3513 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3514 unsigned int pow2Width, pow2Height;
3515 const GlPixelFormatDesc *glDesc;
3517 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3518 /* Setup some glformat defaults */
3519 This->glDescription.glFormat = glDesc->glFormat;
3520 This->glDescription.glFormatInternal = glDesc->glInternal;
3521 This->glDescription.glType = glDesc->glType;
3523 This->glDescription.textureName = 0;
3524 This->glDescription.target = GL_TEXTURE_2D;
3526 /* Non-power2 support */
3527 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3528 pow2Width = This->currentDesc.Width;
3529 pow2Height = This->currentDesc.Height;
3531 /* Find the nearest pow2 match */
3532 pow2Width = pow2Height = 1;
3533 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3534 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3536 This->pow2Width = pow2Width;
3537 This->pow2Height = pow2Height;
3539 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3540 WINED3DFORMAT Format = This->resource.format;
3541 /** TODO: add support for non power two compressed textures **/
3542 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3543 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3544 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3545 This, This->currentDesc.Width, This->currentDesc.Height);
3546 return WINED3DERR_NOTAVAILABLE;
3550 if(pow2Width != This->currentDesc.Width ||
3551 pow2Height != This->currentDesc.Height) {
3552 This->Flags |= SFLAG_NONPOW2;
3555 TRACE("%p\n", This);
3556 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3557 /* one of three options
3558 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)
3559 2: Set the texture to the maximum size (bad idea)
3560 3: WARN and return WINED3DERR_NOTAVAILABLE;
3561 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.
3563 WARN("(%p) Creating an oversized surface\n", This);
3564 This->Flags |= SFLAG_OVERSIZE;
3566 /* This will be initialized on the first blt */
3567 This->glRect.left = 0;
3568 This->glRect.top = 0;
3569 This->glRect.right = 0;
3570 This->glRect.bottom = 0;
3572 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3573 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3574 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3575 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3577 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3578 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3580 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3581 This->pow2Width = This->currentDesc.Width;
3582 This->pow2Height = This->currentDesc.Height;
3583 This->Flags &= ~SFLAG_NONPOW2;
3586 /* No oversize, gl rect is the full texture size */
3587 This->Flags &= ~SFLAG_OVERSIZE;
3588 This->glRect.left = 0;
3589 This->glRect.top = 0;
3590 This->glRect.right = This->pow2Width;
3591 This->glRect.bottom = This->pow2Height;
3594 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3595 switch(wined3d_settings.offscreen_rendering_mode) {
3596 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3597 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3598 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3602 This->Flags |= SFLAG_INSYSMEM;
3607 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3608 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3609 IWineD3DBaseTexture *texture;
3611 TRACE("(%p)->(%s, %s)\n", iface,
3612 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3613 persistent ? "TRUE" : "FALSE");
3615 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3616 IWineD3DSwapChain *swapchain = NULL;
3618 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3619 TRACE("Surface %p is an onscreen surface\n", iface);
3621 IWineD3DSwapChain_Release(swapchain);
3623 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
3624 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3629 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3630 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3631 TRACE("Passing to container\n");
3632 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3633 IWineD3DBaseTexture_Release(texture);
3636 This->Flags &= ~SFLAG_LOCATIONS;
3637 This->Flags |= flag;
3639 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3640 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3641 TRACE("Passing to container\n");
3642 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3643 IWineD3DBaseTexture_Release(texture);
3646 This->Flags &= ~flag;
3654 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3655 struct coords coords[4];
3657 IWineD3DSwapChain *swapchain = NULL;
3658 IWineD3DBaseTexture *texture = NULL;
3660 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3667 rect.right = This->currentDesc.Width;
3668 rect.bottom = This->currentDesc.Height;
3671 ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3674 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3675 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3676 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3677 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3678 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3679 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3680 checkGLcall("glTexParameteri");
3681 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3682 checkGLcall("glTexParameteri");
3684 coords[0].x = rect.left;
3687 coords[1].x = rect.left;
3690 coords[2].x = rect.right;
3693 coords[3].x = rect.right;
3696 coords[0].y = rect.top;
3697 coords[1].y = rect.bottom;
3698 coords[2].y = rect.bottom;
3699 coords[3].y = rect.top;
3700 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3701 glEnable(GL_TEXTURE_2D);
3702 checkGLcall("glEnable(GL_TEXTURE_2D)");
3703 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3704 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3705 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3706 checkGLcall("glTexParameteri");
3707 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3708 checkGLcall("glTexParameteri");
3710 coords[0].x = (float)rect.left / This->pow2Width;
3713 coords[1].x = (float)rect.left / This->pow2Width;
3716 coords[2].x = (float)rect.right / This->pow2Width;
3719 coords[3].x = (float)rect.right / This->pow2Width;
3722 coords[0].y = (float)rect.top / This->pow2Height;
3723 coords[1].y = (float)rect.bottom / This->pow2Height;
3724 coords[2].y = (float)rect.bottom / This->pow2Height;
3725 coords[3].y = (float)rect.top / This->pow2Height;
3727 /* Must be a cube map */
3728 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3729 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3730 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3731 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3732 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3733 checkGLcall("glTexParameteri");
3734 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3735 checkGLcall("glTexParameteri");
3737 switch(This->glDescription.target) {
3738 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3739 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3740 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3741 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3742 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3745 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3746 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3747 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3748 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3749 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3752 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3753 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3754 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3755 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3756 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3759 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3760 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3761 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3762 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3763 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3766 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3767 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3768 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3769 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3770 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3773 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3774 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3775 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3776 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3777 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3780 ERR("Unexpected texture target\n");
3787 glTexCoord3fv(&coords[0].x);
3788 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3790 glTexCoord3fv(&coords[1].x);
3791 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
3793 glTexCoord3fv(&coords[2].x);
3794 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3796 glTexCoord3fv(&coords[3].x);
3797 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3799 checkGLcall("glEnd");
3801 if(This->glDescription.target != GL_TEXTURE_2D) {
3802 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3803 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3805 glDisable(GL_TEXTURE_2D);
3806 checkGLcall("glDisable(GL_TEXTURE_2D)");
3809 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3810 if(hr == WINED3D_OK && swapchain) {
3811 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3812 if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3815 IWineD3DSwapChain_Release(swapchain);
3817 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3818 * reset properly next draw
3820 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3821 if(hr == WINED3D_OK && texture) {
3822 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3823 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3824 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3825 IWineD3DBaseTexture_Release(texture);
3831 /*****************************************************************************
3832 * IWineD3DSurface::LoadLocation
3834 * Copies the current surface data from wherever it is to the requested
3835 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3836 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3837 * multiple locations, the gl texture is preferred over the drawable, which is
3838 * preferred over system memory. The PBO counts as system memory. If rect is
3839 * not NULL, only the specified rectangle is copied (only supported for
3840 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3841 * location is marked up to date after the copy.
3844 * flag: Surface location flag to be updated
3845 * rect: rectangle to be copied
3848 * WINED3D_OK on success
3849 * WINED3DERR_DEVICELOST on an internal error
3851 *****************************************************************************/
3852 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
3853 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3854 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3855 IWineD3DSwapChain *swapchain = NULL;
3856 GLenum format, internal, type;
3857 CONVERT_TYPES convert;
3859 int width, pitch, outpitch;
3862 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3863 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3864 TRACE("Surface %p is an onscreen surface\n", iface);
3866 IWineD3DSwapChain_Release(swapchain);
3868 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
3869 * Prefer SFLAG_INTEXTURE. */
3870 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
3874 TRACE("(%p)->(%s, %p)\n", iface,
3875 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3878 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
3881 if(This->Flags & flag) {
3882 TRACE("Location already up to date\n");
3886 if(!(This->Flags & SFLAG_LOCATIONS)) {
3887 ERR("Surface does not have any up to date location\n");
3888 This->Flags |= SFLAG_LOST;
3889 return WINED3DERR_DEVICELOST;
3892 if(flag == SFLAG_INSYSMEM) {
3893 surface_prepare_system_memory(This);
3895 /* Download the surface to system memory */
3896 if(This->Flags & SFLAG_INTEXTURE) {
3897 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
3898 surface_bind_and_dirtify(This);
3900 surface_download_data(This);
3902 read_from_framebuffer(This, rect,
3903 This->resource.allocatedMemory,
3904 IWineD3DSurface_GetPitch(iface));
3906 } else if(flag == SFLAG_INDRAWABLE) {
3907 if(This->Flags & SFLAG_INTEXTURE) {
3908 surface_blt_to_drawable(This, rect);
3910 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
3912 /* The width is in 'length' not in bytes */
3913 width = This->currentDesc.Width;
3914 pitch = IWineD3DSurface_GetPitch(iface);
3916 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
3917 int height = This->currentDesc.Height;
3919 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
3920 outpitch = width * bpp;
3921 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
3923 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
3925 ERR("Out of memory %d, %d!\n", outpitch, height);
3926 return WINED3DERR_OUTOFVIDEOMEMORY;
3928 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
3930 This->Flags |= SFLAG_CONVERTED;
3932 This->Flags &= ~SFLAG_CONVERTED;
3933 mem = This->resource.allocatedMemory;
3936 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
3938 /* Don't delete PBO memory */
3939 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
3940 HeapFree(GetProcessHeap(), 0, mem);
3942 } else /* if(flag == SFLAG_INTEXTURE) */ {
3943 if (This->Flags & SFLAG_INDRAWABLE) {
3944 read_from_framebuffer_texture(This);
3945 } else { /* Upload from system memory */
3946 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
3948 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
3949 surface_bind_and_dirtify(This);
3952 /* The only place where LoadTexture() might get called when isInDraw=1
3953 * is ActivateContext where lastActiveRenderTarget is preloaded.
3955 if(iface == device->lastActiveRenderTarget && device->isInDraw)
3956 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
3958 /* Otherwise: System memory copy must be most up to date */
3960 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
3961 This->Flags |= SFLAG_GLCKEY;
3962 This->glCKey = This->SrcBltCKey;
3964 else This->Flags &= ~SFLAG_GLCKEY;
3966 /* The width is in 'length' not in bytes */
3967 width = This->currentDesc.Width;
3968 pitch = IWineD3DSurface_GetPitch(iface);
3970 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
3971 int height = This->currentDesc.Height;
3973 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
3974 outpitch = width * bpp;
3975 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
3977 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
3979 ERR("Out of memory %d, %d!\n", outpitch, height);
3980 return WINED3DERR_OUTOFVIDEOMEMORY;
3982 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
3984 This->Flags |= SFLAG_CONVERTED;
3985 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
3986 d3dfmt_p8_upload_palette(iface, convert);
3987 This->Flags &= ~SFLAG_CONVERTED;
3988 mem = This->resource.allocatedMemory;
3990 This->Flags &= ~SFLAG_CONVERTED;
3991 mem = This->resource.allocatedMemory;
3994 /* Make sure the correct pitch is used */
3995 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
3997 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
3998 TRACE("non power of two support\n");
3999 if(!(This->Flags & SFLAG_ALLOCATED)) {
4000 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4002 if (mem || (This->Flags & SFLAG_PBO)) {
4003 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4006 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4007 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4009 if(!(This->Flags & SFLAG_ALLOCATED)) {
4010 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4012 if (mem || (This->Flags & SFLAG_PBO)) {
4013 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4017 /* Restore the default pitch */
4018 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4021 /* Don't delete PBO memory */
4022 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4023 HeapFree(GetProcessHeap(), 0, mem);
4028 This->Flags |= flag;
4031 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4032 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4033 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4034 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4040 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4041 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4042 IWineD3DSwapChain *swapchain = NULL;
4044 /* Update the drawable size method */
4046 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4049 This->get_drawable_size = get_drawable_size_swapchain;
4050 IWineD3DSwapChain_Release(swapchain);
4051 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4052 switch(wined3d_settings.offscreen_rendering_mode) {
4053 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4054 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4055 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4059 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4062 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4065 IWineD3DBaseSurfaceImpl_QueryInterface,
4066 IWineD3DBaseSurfaceImpl_AddRef,
4067 IWineD3DSurfaceImpl_Release,
4068 /* IWineD3DResource */
4069 IWineD3DBaseSurfaceImpl_GetParent,
4070 IWineD3DBaseSurfaceImpl_GetDevice,
4071 IWineD3DBaseSurfaceImpl_SetPrivateData,
4072 IWineD3DBaseSurfaceImpl_GetPrivateData,
4073 IWineD3DBaseSurfaceImpl_FreePrivateData,
4074 IWineD3DBaseSurfaceImpl_SetPriority,
4075 IWineD3DBaseSurfaceImpl_GetPriority,
4076 IWineD3DSurfaceImpl_PreLoad,
4077 IWineD3DSurfaceImpl_UnLoad,
4078 IWineD3DBaseSurfaceImpl_GetType,
4079 /* IWineD3DSurface */
4080 IWineD3DBaseSurfaceImpl_GetContainer,
4081 IWineD3DBaseSurfaceImpl_GetDesc,
4082 IWineD3DSurfaceImpl_LockRect,
4083 IWineD3DSurfaceImpl_UnlockRect,
4084 IWineD3DSurfaceImpl_GetDC,
4085 IWineD3DSurfaceImpl_ReleaseDC,
4086 IWineD3DSurfaceImpl_Flip,
4087 IWineD3DSurfaceImpl_Blt,
4088 IWineD3DBaseSurfaceImpl_GetBltStatus,
4089 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4090 IWineD3DBaseSurfaceImpl_IsLost,
4091 IWineD3DBaseSurfaceImpl_Restore,
4092 IWineD3DSurfaceImpl_BltFast,
4093 IWineD3DBaseSurfaceImpl_GetPalette,
4094 IWineD3DBaseSurfaceImpl_SetPalette,
4095 IWineD3DBaseSurfaceImpl_RealizePalette,
4096 IWineD3DBaseSurfaceImpl_SetColorKey,
4097 IWineD3DBaseSurfaceImpl_GetPitch,
4098 IWineD3DSurfaceImpl_SetMem,
4099 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4100 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4101 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4102 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4103 IWineD3DBaseSurfaceImpl_SetClipper,
4104 IWineD3DBaseSurfaceImpl_GetClipper,
4106 IWineD3DSurfaceImpl_AddDirtyRect,
4107 IWineD3DSurfaceImpl_LoadTexture,
4108 IWineD3DSurfaceImpl_BindTexture,
4109 IWineD3DSurfaceImpl_SaveSnapshot,
4110 IWineD3DSurfaceImpl_SetContainer,
4111 IWineD3DSurfaceImpl_SetGlTextureDesc,
4112 IWineD3DSurfaceImpl_GetGlDesc,
4113 IWineD3DSurfaceImpl_GetData,
4114 IWineD3DSurfaceImpl_SetFormat,
4115 IWineD3DSurfaceImpl_PrivateSetup,
4116 IWineD3DSurfaceImpl_ModifyLocation,
4117 IWineD3DSurfaceImpl_LoadLocation