wined3d: Add ENTER_GL/LEAVE_GL in surface_allocate_surface.
[wine.git] / dlls / wined3d / surface.c
blobd164c2f591aed54637552d8e24ae570832e9e941
1 /*
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-2008 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
29 #include "config.h"
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 GLint active_texture;
43 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
44 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
45 * gl states. The current texture unit should always be a valid one.
47 * TODO: Track the current active texture per GL context instead of using glGet
49 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
50 ENTER_GL();
51 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
52 LEAVE_GL();
53 active_texture -= GL_TEXTURE0_ARB;
54 } else {
55 active_texture = 0;
57 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_texture));
58 IWineD3DSurface_BindTexture((IWineD3DSurface *)This);
61 /* This function checks if the primary render target uses the 8bit paletted format. */
62 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
64 if (device->render_targets && device->render_targets[0]) {
65 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
66 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
67 return TRUE;
69 return FALSE;
72 /* This call just downloads data, the caller is responsible for activating the
73 * right context and binding the correct texture. */
74 static void surface_download_data(IWineD3DSurfaceImpl *This) {
75 if (0 == This->glDescription.textureName) {
76 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
77 return;
80 /* Only support read back of converted P8 surfaces */
81 if(This->Flags & SFLAG_CONVERTED && (This->resource.format != WINED3DFMT_P8)) {
82 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
83 return;
86 ENTER_GL();
88 if (This->resource.format == WINED3DFMT_DXT1 ||
89 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
90 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
91 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
92 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
93 } else {
94 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
95 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
97 if(This->Flags & SFLAG_PBO) {
98 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
99 checkGLcall("glBindBufferARB");
100 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
101 checkGLcall("glGetCompressedTexImageARB()");
102 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
103 checkGLcall("glBindBufferARB");
104 } else {
105 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
106 checkGLcall("glGetCompressedTexImageARB()");
109 LEAVE_GL();
110 } else {
111 void *mem;
112 GLenum format = This->glDescription.glFormat;
113 GLenum type = This->glDescription.glType;
114 int src_pitch = 0;
115 int dst_pitch = 0;
117 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
118 if(This->resource.format == WINED3DFMT_P8) {
119 format = GL_ALPHA;
120 type = GL_UNSIGNED_BYTE;
123 if (This->Flags & SFLAG_NONPOW2) {
124 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
125 src_pitch = This->bytesPerPixel * This->pow2Width;
126 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
127 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
128 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
129 } else {
130 mem = This->resource.allocatedMemory;
133 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
134 format, type, mem);
136 if(This->Flags & SFLAG_PBO) {
137 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
138 checkGLcall("glBindBufferARB");
140 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
141 type, NULL);
142 checkGLcall("glGetTexImage()");
144 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
145 checkGLcall("glBindBufferARB");
146 } else {
147 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
148 type, mem);
149 checkGLcall("glGetTexImage()");
151 LEAVE_GL();
153 if (This->Flags & SFLAG_NONPOW2) {
154 LPBYTE src_data, dst_data;
155 int y;
157 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
158 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
159 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
161 * We're doing this...
163 * instead of boxing the texture :
164 * |<-texture width ->| -->pow2width| /\
165 * |111111111111111111| | |
166 * |222 Texture 222222| boxed empty | texture height
167 * |3333 Data 33333333| | |
168 * |444444444444444444| | \/
169 * ----------------------------------- |
170 * | boxed empty | boxed empty | pow2height
171 * | | | \/
172 * -----------------------------------
175 * we're repacking the data to the expected texture width
177 * |<-texture width ->| -->pow2width| /\
178 * |111111111111111111222222222222222| |
179 * |222333333333333333333444444444444| texture height
180 * |444444 | |
181 * | | \/
182 * | | |
183 * | empty | pow2height
184 * | | \/
185 * -----------------------------------
187 * == is the same as
189 * |<-texture width ->| /\
190 * |111111111111111111|
191 * |222222222222222222|texture height
192 * |333333333333333333|
193 * |444444444444444444| \/
194 * --------------------
196 * this also means that any references to allocatedMemory should work with the data as if were a
197 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
199 * internally the texture is still stored in a boxed format so any references to textureName will
200 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
202 * Performance should not be an issue, because applications normally do not lock the surfaces when
203 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
204 * and doesn't have to be re-read.
206 src_data = mem;
207 dst_data = This->resource.allocatedMemory;
208 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
209 for (y = 1 ; y < This->currentDesc.Height; y++) {
210 /* skip the first row */
211 src_data += src_pitch;
212 dst_data += dst_pitch;
213 memcpy(dst_data, src_data, dst_pitch);
216 HeapFree(GetProcessHeap(), 0, mem);
220 /* Surface has now been downloaded */
221 This->Flags |= SFLAG_INSYSMEM;
224 /* This call just uploads data, the caller is responsible for activating the
225 * right context and binding the correct texture. */
226 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
227 if (This->resource.format == WINED3DFMT_DXT1 ||
228 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
229 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
230 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
231 FIXME("Using DXT1/3/5 without advertized support\n");
232 } else {
233 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
234 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
235 * function uses glCompressedTexImage2D instead of the SubImage call
237 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
238 ENTER_GL();
240 if(This->Flags & SFLAG_PBO) {
241 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
242 checkGLcall("glBindBufferARB");
243 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
245 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
246 width, height, 0 /* border */, This->resource.size, NULL));
247 checkGLcall("glCompressedTexSubImage2D");
249 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
250 checkGLcall("glBindBufferARB");
251 } else {
252 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
253 width, height, 0 /* border */, This->resource.size, data));
254 checkGLcall("glCompressedTexSubImage2D");
256 LEAVE_GL();
258 } else {
259 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
260 ENTER_GL();
262 if(This->Flags & SFLAG_PBO) {
263 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
264 checkGLcall("glBindBufferARB");
265 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
267 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
268 checkGLcall("glTexSubImage2D");
270 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
271 checkGLcall("glBindBufferARB");
273 else {
274 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
275 checkGLcall("glTexSubImage2D");
278 LEAVE_GL();
282 /* This call just allocates the texture, the caller is responsible for
283 * activating the right context and binding the correct texture. */
284 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
285 BOOL enable_client_storage = FALSE;
286 BYTE *mem = NULL;
288 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,
289 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
291 if (This->resource.format == WINED3DFMT_DXT1 ||
292 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
293 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
294 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
295 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
297 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
298 * once, unfortunately
300 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
301 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
302 This->Flags |= SFLAG_CLIENT;
303 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
304 ENTER_GL();
305 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
306 width, height, 0 /* border */, This->resource.size, mem));
307 LEAVE_GL();
310 return;
313 ENTER_GL();
315 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
316 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
317 /* In some cases we want to disable client storage.
318 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
319 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
320 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
321 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
322 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
324 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
325 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
326 This->Flags &= ~SFLAG_CLIENT;
327 enable_client_storage = TRUE;
328 } else {
329 This->Flags |= SFLAG_CLIENT;
331 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
332 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
334 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
337 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
338 checkGLcall("glTexImage2D");
340 if(enable_client_storage) {
341 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
342 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
344 LEAVE_GL();
346 This->Flags |= SFLAG_ALLOCATED;
349 /* In D3D the depth stencil dimensions have to be greater than or equal to the
350 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
351 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
352 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
353 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
354 renderbuffer_entry_t *entry;
355 GLuint renderbuffer = 0;
356 unsigned int src_width, src_height;
358 src_width = This->pow2Width;
359 src_height = This->pow2Height;
361 /* A depth stencil smaller than the render target is not valid */
362 if (width > src_width || height > src_height) return;
364 /* Remove any renderbuffer set if the sizes match */
365 if (width == src_width && height == src_height) {
366 This->current_renderbuffer = NULL;
367 return;
370 /* Look if we've already got a renderbuffer of the correct dimensions */
371 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
372 if (entry->width == width && entry->height == height) {
373 renderbuffer = entry->id;
374 This->current_renderbuffer = entry;
375 break;
379 if (!renderbuffer) {
380 const GlPixelFormatDesc *glDesc;
381 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
383 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
384 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
385 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
387 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
388 entry->width = width;
389 entry->height = height;
390 entry->id = renderbuffer;
391 list_add_head(&This->renderbuffers, &entry->entry);
393 This->current_renderbuffer = entry;
396 checkGLcall("set_compatible_renderbuffer");
399 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
400 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
401 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
403 TRACE("(%p) : swapchain %p\n", This, swapchain);
405 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
406 TRACE("Returning GL_BACK\n");
407 return GL_BACK;
408 } else if (swapchain_impl->frontBuffer == iface) {
409 TRACE("Returning GL_FRONT\n");
410 return GL_FRONT;
413 FIXME("Higher back buffer, returning GL_BACK\n");
414 return GL_BACK;
417 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
418 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
419 ULONG ref = InterlockedDecrement(&This->resource.ref);
420 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
421 if (ref == 0) {
422 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
423 renderbuffer_entry_t *entry, *entry2;
424 TRACE("(%p) : cleaning up\n", This);
426 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
428 /* Need a context to destroy the texture. Use the currently active render target, but only if
429 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
430 * When destroying the primary rt, Uninit3D will activate a context before doing anything
432 if(device->render_targets && device->render_targets[0]) {
433 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
436 TRACE("Deleting texture %d\n", This->glDescription.textureName);
437 ENTER_GL();
438 glDeleteTextures(1, &This->glDescription.textureName);
439 LEAVE_GL();
442 if(This->Flags & SFLAG_PBO) {
443 /* Delete the PBO */
444 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
447 if(This->Flags & SFLAG_DIBSECTION) {
448 /* Release the DC */
449 SelectObject(This->hDC, This->dib.holdbitmap);
450 DeleteDC(This->hDC);
451 /* Release the DIB section */
452 DeleteObject(This->dib.DIBsection);
453 This->dib.bitmap_data = NULL;
454 This->resource.allocatedMemory = NULL;
456 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
458 HeapFree(GetProcessHeap(), 0, This->palette9);
460 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
461 if(iface == device->ddraw_primary)
462 device->ddraw_primary = NULL;
464 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
465 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
466 HeapFree(GetProcessHeap(), 0, entry);
469 TRACE("(%p) Released\n", This);
470 HeapFree(GetProcessHeap(), 0, This);
473 return ref;
476 /* ****************************************************
477 IWineD3DSurface IWineD3DResource parts follow
478 **************************************************** */
480 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
481 /* TODO: check for locks */
482 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
483 IWineD3DBaseTexture *baseTexture = NULL;
484 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
486 TRACE("(%p)Checking to see if the container is a base texture\n", This);
487 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
488 TRACE("Passing to container\n");
489 IWineD3DBaseTexture_PreLoad(baseTexture);
490 IWineD3DBaseTexture_Release(baseTexture);
491 } else {
492 TRACE("(%p) : About to load surface\n", This);
494 if(!device->isInDraw) {
495 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
498 ENTER_GL();
499 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
500 if (!This->glDescription.level) {
501 if (!This->glDescription.textureName) {
502 glGenTextures(1, &This->glDescription.textureName);
503 checkGLcall("glGenTextures");
504 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
506 glBindTexture(This->glDescription.target, This->glDescription.textureName);
507 checkGLcall("glBindTexture");
508 IWineD3DSurface_LoadTexture(iface, FALSE);
509 /* This is where we should be reducing the amount of GLMemoryUsed */
510 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
511 /* assume this is a coding error not a real error for now */
512 FIXME("Mipmap surface has a glTexture bound to it!\n");
514 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
515 /* Tell opengl to try and keep this texture in video ram (well mostly) */
516 GLclampf tmp;
517 tmp = 0.9f;
518 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
520 LEAVE_GL();
522 return;
525 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
526 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
527 This->resource.allocatedMemory =
528 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
530 ENTER_GL();
531 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
532 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
533 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
534 checkGLcall("glGetBufferSubData");
535 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
536 checkGLcall("glDeleteBuffers");
537 LEAVE_GL();
539 This->pbo = 0;
540 This->Flags &= ~SFLAG_PBO;
543 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
544 IWineD3DBaseTexture *texture = NULL;
545 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
546 renderbuffer_entry_t *entry, *entry2;
547 TRACE("(%p)\n", iface);
549 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
550 /* Default pool resources are supposed to be destroyed before Reset is called.
551 * Implicit resources stay however. So this means we have an implicit render target
552 * or depth stencil. The content may be destroyed, but we still have to tear down
553 * opengl resources, so we cannot leave early.
555 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
556 } else {
557 /* Load the surface into system memory */
558 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
560 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
561 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
562 This->Flags &= ~SFLAG_ALLOCATED;
564 /* Destroy PBOs, but load them into real sysmem before */
565 if(This->Flags & SFLAG_PBO) {
566 surface_remove_pbo(This);
569 /* Destroy fbo render buffers. This is needed for implicit render targets, for
570 * all application-created targets the application has to release the surface
571 * before calling _Reset
573 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
574 ENTER_GL();
575 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
576 LEAVE_GL();
577 list_remove(&entry->entry);
578 HeapFree(GetProcessHeap(), 0, entry);
580 list_init(&This->renderbuffers);
581 This->current_renderbuffer = NULL;
583 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
584 * destroy it
586 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
587 if(!texture) {
588 ENTER_GL();
589 glDeleteTextures(1, &This->glDescription.textureName);
590 This->glDescription.textureName = 0;
591 LEAVE_GL();
592 } else {
593 IWineD3DBaseTexture_Release(texture);
595 return;
598 /* ******************************************************
599 IWineD3DSurface IWineD3DSurface parts follow
600 ****************************************************** */
602 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
603 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
604 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
605 if (This->glDescription.textureName == 0 && textureName != 0) {
606 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
607 IWineD3DSurface_AddDirtyRect(iface, NULL);
609 This->glDescription.textureName = textureName;
610 This->glDescription.target = target;
611 This->Flags &= ~SFLAG_ALLOCATED;
614 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
615 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
616 TRACE("(%p) : returning %p\n", This, &This->glDescription);
617 *glDescription = &This->glDescription;
620 /* TODO: think about moving this down to resource? */
621 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
622 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
623 /* 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 */
624 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
625 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
627 return (CONST void*)(This->resource.allocatedMemory);
630 /* Read the framebuffer back into the surface */
631 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
632 IWineD3DSwapChainImpl *swapchain;
633 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
634 BYTE *mem;
635 GLint fmt;
636 GLint type;
637 BYTE *row, *top, *bottom;
638 int i;
639 BOOL bpp;
640 RECT local_rect;
641 BOOL srcIsUpsideDown;
643 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
644 static BOOL warned = FALSE;
645 if(!warned) {
646 ERR("The application tries to lock the render target, but render target locking is disabled\n");
647 warned = TRUE;
649 return;
652 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
653 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
654 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
655 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
656 * context->last_was_blit set on the unlock.
658 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
659 ENTER_GL();
661 /* Select the correct read buffer, and give some debug output.
662 * There is no need to keep track of the current read buffer or reset it, every part of the code
663 * that reads sets the read buffer as desired.
665 if(!swapchain) {
666 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
667 * Read from the back buffer
669 TRACE("Locking offscreen render target\n");
670 glReadBuffer(myDevice->offscreenBuffer);
671 srcIsUpsideDown = TRUE;
672 } else {
673 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
674 TRACE("Locking %#x buffer\n", buffer);
675 glReadBuffer(buffer);
676 checkGLcall("glReadBuffer");
678 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
679 srcIsUpsideDown = FALSE;
682 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
683 if(!rect) {
684 local_rect.left = 0;
685 local_rect.top = 0;
686 local_rect.right = This->currentDesc.Width;
687 local_rect.bottom = This->currentDesc.Height;
688 } else {
689 local_rect = *rect;
691 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
693 switch(This->resource.format)
695 case WINED3DFMT_P8:
697 if(primary_render_target_is_p8(myDevice)) {
698 /* In case of P8 render targets the index is stored in the alpha component */
699 fmt = GL_ALPHA;
700 type = GL_UNSIGNED_BYTE;
701 mem = dest;
702 bpp = This->bytesPerPixel;
703 } else {
704 /* GL can't return palettized data, so read ARGB pixels into a
705 * separate block of memory and convert them into palettized format
706 * in software. Slow, but if the app means to use palettized render
707 * targets and locks it...
709 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
710 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
711 * for the color channels when palettizing the colors.
713 fmt = GL_RGB;
714 type = GL_UNSIGNED_BYTE;
715 pitch *= 3;
716 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
717 if(!mem) {
718 ERR("Out of memory\n");
719 LEAVE_GL();
720 return;
722 bpp = This->bytesPerPixel * 3;
725 break;
727 default:
728 mem = dest;
729 fmt = This->glDescription.glFormat;
730 type = This->glDescription.glType;
731 bpp = This->bytesPerPixel;
734 if(This->Flags & SFLAG_PBO) {
735 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
736 checkGLcall("glBindBufferARB");
739 glReadPixels(local_rect.left, local_rect.top,
740 local_rect.right - local_rect.left,
741 local_rect.bottom - local_rect.top,
742 fmt, type, mem);
743 vcheckGLcall("glReadPixels");
745 if(This->Flags & SFLAG_PBO) {
746 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
747 checkGLcall("glBindBufferARB");
749 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
750 * to get a pointer to it and perform the flipping in software. This is a lot
751 * faster than calling glReadPixels for each line. In case we want more speed
752 * we should rerender it flipped in a FBO and read the data back from the FBO. */
753 if(!srcIsUpsideDown) {
754 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
755 checkGLcall("glBindBufferARB");
757 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
758 checkGLcall("glMapBufferARB");
762 /* TODO: Merge this with the palettization loop below for P8 targets */
763 if(!srcIsUpsideDown) {
764 UINT len, off;
765 /* glReadPixels returns the image upside down, and there is no way to prevent this.
766 Flip the lines in software */
767 len = (local_rect.right - local_rect.left) * bpp;
768 off = local_rect.left * bpp;
770 row = HeapAlloc(GetProcessHeap(), 0, len);
771 if(!row) {
772 ERR("Out of memory\n");
773 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
774 LEAVE_GL();
775 return;
778 top = mem + pitch * local_rect.top;
779 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
780 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
781 memcpy(row, top + off, len);
782 memcpy(top + off, bottom + off, len);
783 memcpy(bottom + off, row, len);
784 top += pitch;
785 bottom -= pitch;
787 HeapFree(GetProcessHeap(), 0, row);
789 /* Unmap the temp PBO buffer */
790 if(This->Flags & SFLAG_PBO) {
791 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
792 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
796 LEAVE_GL();
798 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
799 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
800 * the same color but we have no choice.
801 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
803 if((This->resource.format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice)) {
804 PALETTEENTRY *pal = NULL;
805 DWORD width = pitch / 3;
806 int x, y, c;
808 if(This->palette) {
809 pal = This->palette->palents;
810 } else {
811 ERR("Palette is missing, cannot perform inverse palette lookup\n");
812 HeapFree(GetProcessHeap(), 0, mem);
813 return ;
816 for(y = local_rect.top; y < local_rect.bottom; y++) {
817 for(x = local_rect.left; x < local_rect.right; x++) {
818 /* start lines pixels */
819 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
820 BYTE *green = blue + 1;
821 BYTE *red = green + 1;
823 for(c = 0; c < 256; c++) {
824 if(*red == pal[c].peRed &&
825 *green == pal[c].peGreen &&
826 *blue == pal[c].peBlue)
828 *((BYTE *) dest + y * width + x) = c;
829 break;
834 HeapFree(GetProcessHeap(), 0, mem);
838 /* Read the framebuffer contents into a texture */
839 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
841 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
842 IWineD3DSwapChainImpl *swapchain;
843 int bpp;
844 GLenum format, internal, type;
845 CONVERT_TYPES convert;
846 BOOL srcIsUpsideDown;
847 GLint prevRead;
849 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
851 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
852 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
853 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
854 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
855 * context->last_was_blit set on the unlock.
857 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
858 surface_bind_and_dirtify(This);
859 ENTER_GL();
861 glGetIntegerv(GL_READ_BUFFER, &prevRead);
863 /* Select the correct read buffer, and give some debug output.
864 * There is no need to keep track of the current read buffer or reset it, every part of the code
865 * that reads sets the read buffer as desired.
867 if(!swapchain) {
868 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
869 * Read from the back buffer
871 TRACE("Locking offscreen render target\n");
872 glReadBuffer(device->offscreenBuffer);
873 srcIsUpsideDown = TRUE;
874 } else {
875 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
876 TRACE("Locking %#x buffer\n", buffer);
877 glReadBuffer(buffer);
878 checkGLcall("glReadBuffer");
880 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
881 srcIsUpsideDown = FALSE;
884 if(!(This->Flags & SFLAG_ALLOCATED)) {
885 surface_allocate_surface(This, internal, This->pow2Width,
886 This->pow2Height, format, type);
889 clear_unused_channels(This);
891 /* If !SrcIsUpsideDown we should flip the surface.
892 * This can be done using glCopyTexSubImage2D but this
893 * is VERY slow, so don't do that. We should prevent
894 * this code from getting called in such cases or perhaps
895 * we can use FBOs */
897 glCopyTexSubImage2D(This->glDescription.target,
898 This->glDescription.level,
899 0, 0, 0, 0,
900 This->currentDesc.Width,
901 This->currentDesc.Height);
902 checkGLcall("glCopyTexSubImage2D");
904 glReadBuffer(prevRead);
905 vcheckGLcall("glReadBuffer");
907 LEAVE_GL();
908 TRACE("Updated target %d\n", This->glDescription.target);
911 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
912 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
913 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
914 * changed
916 if(!(This->Flags & SFLAG_DYNLOCK)) {
917 This->lockCount++;
918 /* MAXLOCKCOUNT is defined in wined3d_private.h */
919 if(This->lockCount > MAXLOCKCOUNT) {
920 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
921 This->Flags |= SFLAG_DYNLOCK;
925 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
926 * Also don't create a PBO for systemmem surfaces.
928 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
929 GLenum error;
930 ENTER_GL();
932 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
933 error = glGetError();
934 if(This->pbo == 0 || error != GL_NO_ERROR) {
935 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
938 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
940 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
941 checkGLcall("glBindBufferARB");
943 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
944 checkGLcall("glBufferDataARB");
946 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
947 checkGLcall("glBindBufferARB");
949 /* We don't need the system memory anymore and we can't even use it for PBOs */
950 if(!(This->Flags & SFLAG_CLIENT)) {
951 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
952 This->resource.heapMemory = NULL;
954 This->resource.allocatedMemory = NULL;
955 This->Flags |= SFLAG_PBO;
956 LEAVE_GL();
957 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
958 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
959 * or a pbo to map
961 if(!This->resource.heapMemory) {
962 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
964 This->resource.allocatedMemory =
965 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
966 if(This->Flags & SFLAG_INSYSMEM) {
967 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
972 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
973 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
974 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
975 IWineD3DSwapChain *swapchain = NULL;
977 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
979 /* This is also done in the base class, but we have to verify this before loading any data from
980 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
981 * may interfere, and all other bad things may happen
983 if (This->Flags & SFLAG_LOCKED) {
984 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
985 return WINED3DERR_INVALIDCALL;
987 This->Flags |= SFLAG_LOCKED;
989 if (!(This->Flags & SFLAG_LOCKABLE))
991 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
994 if (Flags & WINED3DLOCK_DISCARD) {
995 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
996 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
997 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
998 This->Flags |= SFLAG_INSYSMEM;
999 goto lock_end;
1002 if (This->Flags & SFLAG_INSYSMEM) {
1003 TRACE("Local copy is up to date, not downloading data\n");
1004 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1005 goto lock_end;
1008 /* Now download the surface content from opengl
1009 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1010 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
1012 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1013 if(swapchain || iface == myDevice->render_targets[0]) {
1014 const RECT *pass_rect = pRect;
1016 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1017 * because most caller functions do not need that. So do that here
1019 if(pRect &&
1020 pRect->top == 0 &&
1021 pRect->left == 0 &&
1022 pRect->right == This->currentDesc.Width &&
1023 pRect->bottom == This->currentDesc.Height) {
1024 pass_rect = NULL;
1027 switch(wined3d_settings.rendertargetlock_mode) {
1028 case RTL_TEXDRAW:
1029 case RTL_TEXTEX:
1030 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1031 #if 0
1032 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1033 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1034 * This may be faster on some cards
1036 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1037 #endif
1038 /* drop through */
1040 case RTL_AUTO:
1041 case RTL_READDRAW:
1042 case RTL_READTEX:
1043 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1044 break;
1046 case RTL_DISABLE:
1047 break;
1049 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1051 } else if(iface == myDevice->stencilBufferTarget) {
1052 /** the depth stencil in openGL has a format of GL_FLOAT
1053 * which should be good for WINED3DFMT_D16_LOCKABLE
1054 * and WINED3DFMT_D16
1055 * it is unclear what format the stencil buffer is in except.
1056 * 'Each index is converted to fixed point...
1057 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1058 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1059 * glReadPixels(This->lockedRect.left,
1060 * This->lockedRect.bottom - j - 1,
1061 * This->lockedRect.right - This->lockedRect.left,
1062 * 1,
1063 * GL_DEPTH_COMPONENT,
1064 * type,
1065 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1067 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1068 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1069 * none of that is the case the problem is not in this function :-)
1070 ********************************************/
1071 FIXME("Depth stencil locking not supported yet\n");
1072 } else {
1073 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1074 TRACE("locking an ordinary surface\n");
1075 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1078 lock_end:
1079 if(This->Flags & SFLAG_PBO) {
1080 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1081 ENTER_GL();
1082 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1083 checkGLcall("glBindBufferARB");
1085 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1086 if(This->resource.allocatedMemory) {
1087 ERR("The surface already has PBO memory allocated!\n");
1090 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1091 checkGLcall("glMapBufferARB");
1093 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1094 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1095 checkGLcall("glBindBufferARB");
1097 LEAVE_GL();
1100 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1101 /* Don't dirtify */
1102 } else {
1103 IWineD3DBaseTexture *pBaseTexture;
1105 * Dirtify on lock
1106 * as seen in msdn docs
1108 IWineD3DSurface_AddDirtyRect(iface, pRect);
1110 /** Dirtify Container if needed */
1111 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1112 TRACE("Making container dirty\n");
1113 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1114 IWineD3DBaseTexture_Release(pBaseTexture);
1115 } else {
1116 TRACE("Surface is standalone, no need to dirty the container\n");
1120 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1123 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1124 GLint prev_store;
1125 GLint prev_rasterpos[4];
1126 GLint skipBytes = 0;
1127 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1128 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1129 IWineD3DSwapChainImpl *swapchain;
1131 /* Activate the correct context for the render target */
1132 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1133 ENTER_GL();
1135 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1136 if(!swapchain) {
1137 /* Primary offscreen render target */
1138 TRACE("Offscreen render target\n");
1139 glDrawBuffer(myDevice->offscreenBuffer);
1140 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1141 } else {
1142 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1143 TRACE("Unlocking %#x buffer\n", buffer);
1144 glDrawBuffer(buffer);
1145 checkGLcall("glDrawBuffer");
1147 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1150 glFlush();
1151 vcheckGLcall("glFlush");
1152 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1153 vcheckGLcall("glIntegerv");
1154 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1155 vcheckGLcall("glIntegerv");
1156 glPixelZoom(1.0, -1.0);
1157 vcheckGLcall("glPixelZoom");
1159 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1160 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1161 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1163 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1164 vcheckGLcall("glRasterPos2f");
1166 /* Some drivers(radeon dri, others?) don't like exceptions during
1167 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1168 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1169 * catch to put the dib section in InSync mode, which leads to a crash
1170 * and a blocked x server on my radeon card.
1172 * The following lines read the dib section so it is put in inSync mode
1173 * before glDrawPixels is called and the crash is prevented. There won't
1174 * be any interfering gdi accesses, because UnlockRect is called from
1175 * ReleaseDC, and the app won't use the dc any more afterwards.
1177 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1178 volatile BYTE read;
1179 read = This->resource.allocatedMemory[0];
1182 if(This->Flags & SFLAG_PBO) {
1183 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1184 checkGLcall("glBindBufferARB");
1187 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1188 if(This->Flags & SFLAG_LOCKED) {
1189 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1190 (This->lockedRect.bottom - This->lockedRect.top)-1,
1191 fmt, type,
1192 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1193 checkGLcall("glDrawPixels");
1194 } else {
1195 glDrawPixels(This->currentDesc.Width,
1196 This->currentDesc.Height,
1197 fmt, type, mem);
1198 checkGLcall("glDrawPixels");
1201 if(This->Flags & SFLAG_PBO) {
1202 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1203 checkGLcall("glBindBufferARB");
1206 glPixelZoom(1.0,1.0);
1207 vcheckGLcall("glPixelZoom");
1209 glRasterPos3iv(&prev_rasterpos[0]);
1210 vcheckGLcall("glRasterPos3iv");
1212 /* Reset to previous pack row length */
1213 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1214 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1216 if(!swapchain) {
1217 glDrawBuffer(myDevice->offscreenBuffer);
1218 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1219 } else if(swapchain->backBuffer) {
1220 glDrawBuffer(GL_BACK);
1221 checkGLcall("glDrawBuffer(GL_BACK)");
1222 } else {
1223 glDrawBuffer(GL_FRONT);
1224 checkGLcall("glDrawBuffer(GL_FRONT)");
1226 LEAVE_GL();
1228 return;
1231 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1232 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1233 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1234 IWineD3DSwapChainImpl *swapchain = NULL;
1235 BOOL fullsurface;
1237 if (!(This->Flags & SFLAG_LOCKED)) {
1238 WARN("trying to Unlock an unlocked surf@%p\n", This);
1239 return WINED3DERR_INVALIDCALL;
1242 if (This->Flags & SFLAG_PBO) {
1243 TRACE("Freeing PBO memory\n");
1244 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1245 ENTER_GL();
1246 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1247 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1248 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1249 checkGLcall("glUnmapBufferARB");
1250 LEAVE_GL();
1251 This->resource.allocatedMemory = NULL;
1254 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1256 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1257 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1258 goto unlock_end;
1261 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1262 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1263 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1265 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1266 static BOOL warned = FALSE;
1267 if(!warned) {
1268 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1269 warned = TRUE;
1271 goto unlock_end;
1274 if(This->dirtyRect.left == 0 &&
1275 This->dirtyRect.top == 0 &&
1276 This->dirtyRect.right == This->currentDesc.Width &&
1277 This->dirtyRect.bottom == This->currentDesc.Height) {
1278 fullsurface = TRUE;
1279 } else {
1280 /* TODO: Proper partial rectangle tracking */
1281 fullsurface = FALSE;
1282 This->Flags |= SFLAG_INSYSMEM;
1285 switch(wined3d_settings.rendertargetlock_mode) {
1286 case RTL_READTEX:
1287 case RTL_TEXTEX:
1288 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1289 ENTER_GL();
1290 if (This->glDescription.textureName == 0) {
1291 glGenTextures(1, &This->glDescription.textureName);
1292 checkGLcall("glGenTextures");
1294 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1295 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1296 LEAVE_GL();
1297 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1298 /* drop through */
1300 case RTL_AUTO:
1301 case RTL_READDRAW:
1302 case RTL_TEXDRAW:
1303 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1304 break;
1307 if(!fullsurface) {
1308 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1309 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1310 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1311 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1312 * not fully up to date because only a subrectangle was read in LockRect.
1314 This->Flags &= ~SFLAG_INSYSMEM;
1315 This->Flags |= SFLAG_INDRAWABLE;
1318 This->dirtyRect.left = This->currentDesc.Width;
1319 This->dirtyRect.top = This->currentDesc.Height;
1320 This->dirtyRect.right = 0;
1321 This->dirtyRect.bottom = 0;
1322 } else if(iface == myDevice->stencilBufferTarget) {
1323 FIXME("Depth Stencil buffer locking is not implemented\n");
1324 } else {
1325 /* The rest should be a normal texture */
1326 IWineD3DBaseTextureImpl *impl;
1327 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1328 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1329 * states need resetting
1331 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1332 if(impl->baseTexture.bindCount) {
1333 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1335 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1339 unlock_end:
1340 This->Flags &= ~SFLAG_LOCKED;
1341 memset(&This->lockedRect, 0, sizeof(RECT));
1342 return WINED3D_OK;
1345 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1346 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1347 WINED3DLOCKED_RECT lock;
1348 HRESULT hr;
1349 RGBQUAD col[256];
1351 TRACE("(%p)->(%p)\n",This,pHDC);
1353 if(This->Flags & SFLAG_USERPTR) {
1354 ERR("Not supported on surfaces with an application-provided surfaces\n");
1355 return WINEDDERR_NODC;
1358 /* Give more detailed info for ddraw */
1359 if (This->Flags & SFLAG_DCINUSE)
1360 return WINEDDERR_DCALREADYCREATED;
1362 /* Can't GetDC if the surface is locked */
1363 if (This->Flags & SFLAG_LOCKED)
1364 return WINED3DERR_INVALIDCALL;
1366 /* According to Direct3D9 docs, only these formats are supported */
1367 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1368 if (This->resource.format != WINED3DFMT_R5G6B5 &&
1369 This->resource.format != WINED3DFMT_X1R5G5B5 &&
1370 This->resource.format != WINED3DFMT_R8G8B8 &&
1371 This->resource.format != WINED3DFMT_X8R8G8B8) return WINED3DERR_INVALIDCALL;
1374 memset(&lock, 0, sizeof(lock)); /* To be sure */
1376 /* Create a DIB section if there isn't a hdc yet */
1377 if(!This->hDC) {
1378 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1379 if(This->Flags & SFLAG_CLIENT) {
1380 IWineD3DSurface_PreLoad(iface);
1383 /* Use the dib section from now on if we are not using a PBO */
1384 if(!(This->Flags & SFLAG_PBO))
1385 This->resource.allocatedMemory = This->dib.bitmap_data;
1388 /* Lock the surface */
1389 hr = IWineD3DSurface_LockRect(iface,
1390 &lock,
1391 NULL,
1394 if(This->Flags & SFLAG_PBO) {
1395 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1396 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1399 if(FAILED(hr)) {
1400 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1401 /* keep the dib section */
1402 return hr;
1405 if(This->resource.format == WINED3DFMT_P8 ||
1406 This->resource.format == WINED3DFMT_A8P8) {
1407 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1408 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1409 unsigned int n;
1410 PALETTEENTRY *pal = NULL;
1412 if(This->palette) {
1413 pal = This->palette->palents;
1414 } else {
1415 IWineD3DSurfaceImpl *dds_primary = (IWineD3DSurfaceImpl *)This->resource.wineD3DDevice->ddraw_primary;
1416 if (dds_primary && dds_primary->palette)
1417 pal = dds_primary->palette->palents;
1420 if (pal) {
1421 for (n=0; n<256; n++) {
1422 col[n].rgbRed = pal[n].peRed;
1423 col[n].rgbGreen = pal[n].peGreen;
1424 col[n].rgbBlue = pal[n].peBlue;
1425 col[n].rgbReserved = 0;
1427 SetDIBColorTable(This->hDC, 0, 256, col);
1431 *pHDC = This->hDC;
1432 TRACE("returning %p\n",*pHDC);
1433 This->Flags |= SFLAG_DCINUSE;
1435 return WINED3D_OK;
1438 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1439 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1441 TRACE("(%p)->(%p)\n",This,hDC);
1443 if (!(This->Flags & SFLAG_DCINUSE))
1444 return WINED3DERR_INVALIDCALL;
1446 if (This->hDC !=hDC) {
1447 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1448 return WINED3DERR_INVALIDCALL;
1451 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1452 /* Copy the contents of the DIB over to the PBO */
1453 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1456 /* we locked first, so unlock now */
1457 IWineD3DSurface_UnlockRect(iface);
1459 This->Flags &= ~SFLAG_DCINUSE;
1461 return WINED3D_OK;
1464 /* ******************************************************
1465 IWineD3DSurface Internal (No mapping to directx api) parts follow
1466 ****************************************************** */
1468 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) {
1469 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1470 const GlPixelFormatDesc *glDesc;
1471 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1472 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1474 /* Default values: From the surface */
1475 *format = glDesc->glFormat;
1476 *type = glDesc->glType;
1477 *convert = NO_CONVERSION;
1478 *target_bpp = This->bytesPerPixel;
1480 if(srgb_mode) {
1481 *internal = glDesc->glGammaInternal;
1482 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1483 *internal = glDesc->rtInternal;
1484 } else {
1485 *internal = glDesc->glInternal;
1488 /* Ok, now look if we have to do any conversion */
1489 switch(This->resource.format) {
1490 case WINED3DFMT_P8:
1491 /* ****************
1492 Paletted Texture
1493 **************** */
1495 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1496 * of the two is available make sure texturing is requested as neither of the two works in
1497 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1498 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1499 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1500 * conflicts with this.
1502 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && primary_render_target_is_p8(device))) || colorkey_active || !use_texturing ) {
1503 *format = GL_RGBA;
1504 *internal = GL_RGBA;
1505 *type = GL_UNSIGNED_BYTE;
1506 *target_bpp = 4;
1507 if(colorkey_active) {
1508 *convert = CONVERT_PALETTED_CK;
1509 } else {
1510 *convert = CONVERT_PALETTED;
1513 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1514 *format = GL_ALPHA;
1515 *internal = GL_RGBA;
1516 *type = GL_UNSIGNED_BYTE;
1517 *target_bpp = 1;
1520 break;
1522 case WINED3DFMT_R3G3B2:
1523 /* **********************
1524 GL_UNSIGNED_BYTE_3_3_2
1525 ********************** */
1526 if (colorkey_active) {
1527 /* This texture format will never be used.. So do not care about color keying
1528 up until the point in time it will be needed :-) */
1529 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1531 break;
1533 case WINED3DFMT_R5G6B5:
1534 if (colorkey_active) {
1535 *convert = CONVERT_CK_565;
1536 *format = GL_RGBA;
1537 *internal = GL_RGBA;
1538 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1540 break;
1542 case WINED3DFMT_X1R5G5B5:
1543 if (colorkey_active) {
1544 *convert = CONVERT_CK_5551;
1545 *format = GL_BGRA;
1546 *internal = GL_RGBA;
1547 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1549 break;
1551 case WINED3DFMT_R8G8B8:
1552 if (colorkey_active) {
1553 *convert = CONVERT_CK_RGB24;
1554 *format = GL_RGBA;
1555 *internal = GL_RGBA;
1556 *type = GL_UNSIGNED_INT_8_8_8_8;
1557 *target_bpp = 4;
1559 break;
1561 case WINED3DFMT_X8R8G8B8:
1562 if (colorkey_active) {
1563 *convert = CONVERT_RGB32_888;
1564 *format = GL_RGBA;
1565 *internal = GL_RGBA;
1566 *type = GL_UNSIGNED_INT_8_8_8_8;
1568 break;
1570 case WINED3DFMT_V8U8:
1571 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1572 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1573 *format = GL_DUDV_ATI;
1574 *internal = GL_DU8DV8_ATI;
1575 *type = GL_BYTE;
1576 /* No conversion - Just change the gl type */
1577 break;
1579 *convert = CONVERT_V8U8;
1580 *format = GL_BGR;
1581 *internal = GL_RGB8;
1582 *type = GL_UNSIGNED_BYTE;
1583 *target_bpp = 3;
1584 break;
1586 case WINED3DFMT_L6V5U5:
1587 *convert = CONVERT_L6V5U5;
1588 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1589 *target_bpp = 3;
1590 /* Use format and types from table */
1591 } else {
1592 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1593 *target_bpp = 2;
1594 *format = GL_RGB;
1595 *internal = GL_RGB5;
1596 *type = GL_UNSIGNED_SHORT_5_6_5;
1598 break;
1600 case WINED3DFMT_X8L8V8U8:
1601 *convert = CONVERT_X8L8V8U8;
1602 *target_bpp = 4;
1603 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1604 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1605 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1606 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1607 * the needed type and format parameter, so the internal format contains a
1608 * 4th component, which is returned as alpha
1610 } else {
1611 /* Not supported by GL_ATI_envmap_bumpmap */
1612 *format = GL_BGRA;
1613 *internal = GL_RGB8;
1614 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1616 break;
1618 case WINED3DFMT_Q8W8V8U8:
1619 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1620 *convert = CONVERT_Q8W8V8U8;
1621 *format = GL_BGRA;
1622 *internal = GL_RGBA8;
1623 *type = GL_UNSIGNED_BYTE;
1624 *target_bpp = 4;
1625 /* Not supported by GL_ATI_envmap_bumpmap */
1626 break;
1628 case WINED3DFMT_V16U16:
1629 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1630 *convert = CONVERT_V16U16;
1631 *format = GL_BGR;
1632 *internal = GL_RGB16_EXT;
1633 *type = GL_UNSIGNED_SHORT;
1634 *target_bpp = 6;
1635 /* What should I do here about GL_ATI_envmap_bumpmap?
1636 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1638 break;
1640 case WINED3DFMT_A4L4:
1641 /* A4L4 exists as an internal gl format, but for some reason there is not
1642 * format+type combination to load it. Thus convert it to A8L8, then load it
1643 * with A4L4 internal, but A8L8 format+type
1645 *convert = CONVERT_A4L4;
1646 *format = GL_LUMINANCE_ALPHA;
1647 *internal = GL_LUMINANCE4_ALPHA4;
1648 *type = GL_UNSIGNED_BYTE;
1649 *target_bpp = 2;
1650 break;
1652 case WINED3DFMT_R32F:
1653 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1654 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1655 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1656 * 1.0 instead.
1658 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1660 *convert = CONVERT_R32F;
1661 *format = GL_RGB;
1662 *internal = GL_RGB32F_ARB;
1663 *type = GL_FLOAT;
1664 *target_bpp = 12;
1665 break;
1667 case WINED3DFMT_R16F:
1668 /* Similar to R32F */
1669 *convert = CONVERT_R16F;
1670 *format = GL_RGB;
1671 *internal = GL_RGB16F_ARB;
1672 *type = GL_HALF_FLOAT_ARB;
1673 *target_bpp = 6;
1674 break;
1676 case WINED3DFMT_G16R16:
1677 *convert = CONVERT_G16R16;
1678 *format = GL_RGB;
1679 *internal = GL_RGB16_EXT;
1680 *type = GL_UNSIGNED_SHORT;
1681 *target_bpp = 6;
1682 break;
1684 default:
1685 break;
1688 return WINED3D_OK;
1691 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1692 BYTE *source, *dest;
1693 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1695 switch (convert) {
1696 case NO_CONVERSION:
1698 memcpy(dst, src, pitch * height);
1699 break;
1701 case CONVERT_PALETTED:
1702 case CONVERT_PALETTED_CK:
1704 IWineD3DPaletteImpl* pal = This->palette;
1705 BYTE table[256][4];
1706 unsigned int x, y;
1708 if( pal == NULL) {
1709 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1712 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1714 for (y = 0; y < height; y++)
1716 source = src + pitch * y;
1717 dest = dst + outpitch * y;
1718 /* This is an 1 bpp format, using the width here is fine */
1719 for (x = 0; x < width; x++) {
1720 BYTE color = *source++;
1721 *dest++ = table[color][0];
1722 *dest++ = table[color][1];
1723 *dest++ = table[color][2];
1724 *dest++ = table[color][3];
1728 break;
1730 case CONVERT_CK_565:
1732 /* Converting the 565 format in 5551 packed to emulate color-keying.
1734 Note : in all these conversion, it would be best to average the averaging
1735 pixels to get the color of the pixel that will be color-keyed to
1736 prevent 'color bleeding'. This will be done later on if ever it is
1737 too visible.
1739 Note2: Nvidia documents say that their driver does not support alpha + color keying
1740 on the same surface and disables color keying in such a case
1742 unsigned int x, y;
1743 WORD *Source;
1744 WORD *Dest;
1746 TRACE("Color keyed 565\n");
1748 for (y = 0; y < height; y++) {
1749 Source = (WORD *) (src + y * pitch);
1750 Dest = (WORD *) (dst + y * outpitch);
1751 for (x = 0; x < width; x++ ) {
1752 WORD color = *Source++;
1753 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1754 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1755 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1756 *Dest |= 0x0001;
1758 Dest++;
1762 break;
1764 case CONVERT_CK_5551:
1766 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1767 unsigned int x, y;
1768 WORD *Source;
1769 WORD *Dest;
1770 TRACE("Color keyed 5551\n");
1771 for (y = 0; y < height; y++) {
1772 Source = (WORD *) (src + y * pitch);
1773 Dest = (WORD *) (dst + y * outpitch);
1774 for (x = 0; x < width; x++ ) {
1775 WORD color = *Source++;
1776 *Dest = color;
1777 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1778 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1779 *Dest |= (1 << 15);
1781 else {
1782 *Dest &= ~(1 << 15);
1784 Dest++;
1788 break;
1790 case CONVERT_V8U8:
1792 unsigned int x, y;
1793 short *Source;
1794 unsigned char *Dest;
1795 for(y = 0; y < height; y++) {
1796 Source = (short *) (src + y * pitch);
1797 Dest = dst + y * outpitch;
1798 for (x = 0; x < width; x++ ) {
1799 long color = (*Source++);
1800 /* B */ Dest[0] = 0xff;
1801 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1802 /* R */ Dest[2] = (color) + 128; /* U */
1803 Dest += 3;
1806 break;
1809 case CONVERT_V16U16:
1811 unsigned int x, y;
1812 DWORD *Source;
1813 unsigned short *Dest;
1814 for(y = 0; y < height; y++) {
1815 Source = (DWORD *) (src + y * pitch);
1816 Dest = (unsigned short *) (dst + y * outpitch);
1817 for (x = 0; x < width; x++ ) {
1818 DWORD color = (*Source++);
1819 /* B */ Dest[0] = 0xffff;
1820 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1821 /* R */ Dest[2] = (color ) + 32768; /* U */
1822 Dest += 3;
1825 break;
1828 case CONVERT_Q8W8V8U8:
1830 unsigned int x, y;
1831 DWORD *Source;
1832 unsigned char *Dest;
1833 for(y = 0; y < height; y++) {
1834 Source = (DWORD *) (src + y * pitch);
1835 Dest = dst + y * outpitch;
1836 for (x = 0; x < width; x++ ) {
1837 long color = (*Source++);
1838 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1839 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1840 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1841 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1842 Dest += 4;
1845 break;
1848 case CONVERT_L6V5U5:
1850 unsigned int x, y;
1851 WORD *Source;
1852 unsigned char *Dest;
1854 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1855 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1856 * fixed function and shaders without further conversion once the surface is
1857 * loaded
1859 for(y = 0; y < height; y++) {
1860 Source = (WORD *) (src + y * pitch);
1861 Dest = dst + y * outpitch;
1862 for (x = 0; x < width; x++ ) {
1863 short color = (*Source++);
1864 unsigned char l = ((color >> 10) & 0xfc);
1865 char v = ((color >> 5) & 0x3e);
1866 char u = ((color ) & 0x1f);
1868 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1869 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1870 * shift. GL reads a signed value and converts it into an unsigned value.
1872 /* M */ Dest[2] = l << 1;
1874 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1875 * from 5 bit values to 8 bit values.
1877 /* V */ Dest[1] = v << 3;
1878 /* U */ Dest[0] = u << 3;
1879 Dest += 3;
1882 } else {
1883 for(y = 0; y < height; y++) {
1884 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1885 Source = (WORD *) (src + y * pitch);
1886 for (x = 0; x < width; x++ ) {
1887 short color = (*Source++);
1888 unsigned char l = ((color >> 10) & 0xfc);
1889 short v = ((color >> 5) & 0x3e);
1890 short u = ((color ) & 0x1f);
1891 short v_conv = v + 16;
1892 short u_conv = u + 16;
1894 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1895 Dest_s += 1;
1899 break;
1902 case CONVERT_X8L8V8U8:
1904 unsigned int x, y;
1905 DWORD *Source;
1906 unsigned char *Dest;
1908 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1909 /* This implementation works with the fixed function pipeline and shaders
1910 * without further modification after converting the surface.
1912 for(y = 0; y < height; y++) {
1913 Source = (DWORD *) (src + y * pitch);
1914 Dest = dst + y * outpitch;
1915 for (x = 0; x < width; x++ ) {
1916 long color = (*Source++);
1917 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1918 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1919 /* U */ Dest[0] = (color & 0xff); /* U */
1920 /* I */ Dest[3] = 255; /* X */
1921 Dest += 4;
1924 } else {
1925 /* Doesn't work correctly with the fixed function pipeline, but can work in
1926 * shaders if the shader is adjusted. (There's no use for this format in gl's
1927 * standard fixed function pipeline anyway).
1929 for(y = 0; y < height; y++) {
1930 Source = (DWORD *) (src + y * pitch);
1931 Dest = dst + y * outpitch;
1932 for (x = 0; x < width; x++ ) {
1933 long color = (*Source++);
1934 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1935 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1936 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1937 Dest += 4;
1941 break;
1944 case CONVERT_A4L4:
1946 unsigned int x, y;
1947 unsigned char *Source;
1948 unsigned char *Dest;
1949 for(y = 0; y < height; y++) {
1950 Source = src + y * pitch;
1951 Dest = dst + y * outpitch;
1952 for (x = 0; x < width; x++ ) {
1953 unsigned char color = (*Source++);
1954 /* A */ Dest[1] = (color & 0xf0) << 0;
1955 /* L */ Dest[0] = (color & 0x0f) << 4;
1956 Dest += 2;
1959 break;
1962 case CONVERT_R32F:
1964 unsigned int x, y;
1965 float *Source;
1966 float *Dest;
1967 for(y = 0; y < height; y++) {
1968 Source = (float *) (src + y * pitch);
1969 Dest = (float *) (dst + y * outpitch);
1970 for (x = 0; x < width; x++ ) {
1971 float color = (*Source++);
1972 Dest[0] = color;
1973 Dest[1] = 1.0;
1974 Dest[2] = 1.0;
1975 Dest += 3;
1978 break;
1981 case CONVERT_R16F:
1983 unsigned int x, y;
1984 WORD *Source;
1985 WORD *Dest;
1986 WORD one = 0x3c00;
1987 for(y = 0; y < height; y++) {
1988 Source = (WORD *) (src + y * pitch);
1989 Dest = (WORD *) (dst + y * outpitch);
1990 for (x = 0; x < width; x++ ) {
1991 WORD color = (*Source++);
1992 Dest[0] = color;
1993 Dest[1] = one;
1994 Dest[2] = one;
1995 Dest += 3;
1998 break;
2001 case CONVERT_G16R16:
2003 unsigned int x, y;
2004 WORD *Source;
2005 WORD *Dest;
2007 for(y = 0; y < height; y++) {
2008 Source = (WORD *) (src + y * pitch);
2009 Dest = (WORD *) (dst + y * outpitch);
2010 for (x = 0; x < width; x++ ) {
2011 WORD green = (*Source++);
2012 WORD red = (*Source++);
2013 Dest[0] = green;
2014 Dest[1] = red;
2015 Dest[2] = 0xffff;
2016 Dest += 3;
2019 break;
2022 default:
2023 ERR("Unsupported conversation type %d\n", convert);
2025 return WINED3D_OK;
2028 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2029 IWineD3DPaletteImpl* pal = This->palette;
2030 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2031 BOOL index_in_alpha = FALSE;
2032 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2033 int i;
2035 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2036 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2037 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2038 * duplicate entries. Store the color key in the unused alpha component to speed the
2039 * download up and to make conversion unneeded. */
2040 index_in_alpha = primary_render_target_is_p8(device);
2042 if (pal == NULL) {
2043 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2044 if(dxVersion <= 7) {
2045 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2046 return;
2049 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2050 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2051 capability flag is present (wine does advertise this capability) */
2052 for (i = 0; i < 256; i++) {
2053 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2054 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2055 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2056 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2058 } else {
2059 TRACE("Using surface palette %p\n", pal);
2060 /* Get the surface's palette */
2061 for (i = 0; i < 256; i++) {
2062 table[i][0] = pal->palents[i].peRed;
2063 table[i][1] = pal->palents[i].peGreen;
2064 table[i][2] = pal->palents[i].peBlue;
2066 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2067 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2068 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2069 of pixels that should be masked away is set to 0. */
2070 if(index_in_alpha) {
2071 table[i][3] = i;
2072 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2073 table[i][3] = 0x00;
2074 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2075 table[i][3] = pal->palents[i].peFlags;
2076 } else {
2077 table[i][3] = 0xFF;
2083 const char *fragment_palette_conversion =
2084 "!!ARBfp1.0\n"
2085 "TEMP index;\n"
2086 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2087 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2088 "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 */
2089 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2090 "END";
2092 /* This function is used in case of 8bit paletted textures to upload the palette.
2093 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2094 extensions like ATI_fragment_shaders is possible.
2096 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2097 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2098 BYTE table[256][4];
2099 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2101 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2103 /* Try to use the paletted texture extension */
2104 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2106 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2107 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2109 else
2111 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2112 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2113 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2115 /* Create the fragment program if we don't have it */
2116 if(!device->paletteConversionShader)
2118 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2119 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2120 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2121 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2122 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2125 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2126 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2128 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2129 glEnable(GL_TEXTURE_1D);
2130 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2132 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2133 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2134 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2135 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2137 /* Switch back to unit 0 in which the 2D texture will be stored. */
2138 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2140 /* Rebind the texture because it isn't bound anymore */
2141 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2145 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2146 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2148 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2149 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2150 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2152 return FALSE;
2155 if(This->palette9) {
2156 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2157 return FALSE;
2159 } else {
2160 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2162 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2163 return TRUE;
2166 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2167 GLboolean oldwrite[4];
2169 /* Some formats have only some color channels, and the others are 1.0.
2170 * since our rendering renders to all channels, and those pixel formats
2171 * are emulated by using a full texture with the other channels set to 1.0
2172 * manually, clear the unused channels.
2174 * This could be done with hacking colorwriteenable to mask the colors,
2175 * but before drawing the buffer would have to be cleared too, so there's
2176 * no gain in that
2178 switch(This->resource.format) {
2179 case WINED3DFMT_R16F:
2180 case WINED3DFMT_R32F:
2181 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2182 /* Do not activate a context, the correct drawable is active already
2183 * though just the read buffer is set, make sure to have the correct draw
2184 * buffer too
2186 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2187 glDisable(GL_SCISSOR_TEST);
2188 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2189 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2190 glClearColor(0.0, 1.0, 1.0, 1.0);
2191 glClear(GL_COLOR_BUFFER_BIT);
2192 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2193 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2194 checkGLcall("Unused channel clear\n");
2195 break;
2197 default: break;
2201 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2202 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2204 if (!(This->Flags & SFLAG_INTEXTURE)) {
2205 TRACE("Reloading because surface is dirty\n");
2206 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2207 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2208 /* Reload: vice versa OR */
2209 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2210 /* Also reload: Color key is active AND the color key has changed */
2211 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2212 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2213 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2214 TRACE("Reloading because of color keying\n");
2215 /* To perform the color key conversion we need a sysmem copy of
2216 * the surface. Make sure we have it
2219 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2220 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2221 /* TODO: This is not necessarily needed with hw palettized texture support */
2222 This->Flags &= ~SFLAG_INTEXTURE;
2223 } else if(palette9_changed(This)) {
2224 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
2225 /* TODO: This is not necessarily needed with hw palettized texture support */
2226 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2228 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2229 This->Flags &= ~SFLAG_INTEXTURE;
2230 } else {
2231 TRACE("surface is already in texture\n");
2232 return WINED3D_OK;
2235 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2236 * These resources are not bound by device size or format restrictions. Because of this,
2237 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2238 * However, these resources can always be created, locked, and copied.
2240 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2242 FIXME("(%p) Operation not supported for scratch textures\n",This);
2243 return WINED3DERR_INVALIDCALL;
2246 This->srgb = srgb_mode;
2247 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2249 #if 0
2251 static unsigned int gen = 0;
2252 char buffer[4096];
2253 ++gen;
2254 if ((gen % 10) == 0) {
2255 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2256 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2259 * debugging crash code
2260 if (gen == 250) {
2261 void** test = NULL;
2262 *test = 0;
2266 #endif
2268 if (!(This->Flags & SFLAG_DONOTFREE)) {
2269 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2270 This->resource.allocatedMemory = NULL;
2271 This->resource.heapMemory = NULL;
2272 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2275 return WINED3D_OK;
2278 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2279 /* TODO: check for locks */
2280 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2281 IWineD3DBaseTexture *baseTexture = NULL;
2282 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2284 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2285 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2286 TRACE("Passing to container\n");
2287 IWineD3DBaseTexture_BindTexture(baseTexture);
2288 IWineD3DBaseTexture_Release(baseTexture);
2289 } else {
2290 TRACE("(%p) : Binding surface\n", This);
2292 if(!device->isInDraw) {
2293 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2295 ENTER_GL();
2296 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2297 LEAVE_GL();
2299 return;
2302 #include <errno.h>
2303 #include <stdio.h>
2304 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2305 FILE* f = NULL;
2306 UINT i, y;
2307 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2308 char *allocatedMemory;
2309 char *textureRow;
2310 IWineD3DSwapChain *swapChain = NULL;
2311 int width, height;
2312 GLuint tmpTexture = 0;
2313 DWORD color;
2314 /*FIXME:
2315 Textures may not be stored in ->allocatedgMemory and a GlTexture
2316 so we should lock the surface before saving a snapshot, or at least check that
2318 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2319 by calling GetTexImage and in compressed form by calling
2320 GetCompressedTexImageARB. Queried compressed images can be saved and
2321 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2322 texture images do not need to be processed by the GL and should
2323 significantly improve texture loading performance relative to uncompressed
2324 images. */
2326 /* Setup the width and height to be the internal texture width and height. */
2327 width = This->pow2Width;
2328 height = This->pow2Height;
2329 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2330 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2332 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2333 /* if were not a real texture then read the back buffer into a real texture */
2334 /* we don't want to interfere with the back buffer so read the data into a temporary
2335 * texture and then save the data out of the temporary texture
2337 GLint prevRead;
2338 ENTER_GL();
2339 TRACE("(%p) Reading render target into texture\n", This);
2340 glEnable(GL_TEXTURE_2D);
2342 glGenTextures(1, &tmpTexture);
2343 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2345 glTexImage2D(GL_TEXTURE_2D,
2347 GL_RGBA,
2348 width,
2349 height,
2350 0/*border*/,
2351 GL_RGBA,
2352 GL_UNSIGNED_INT_8_8_8_8_REV,
2353 NULL);
2355 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2356 vcheckGLcall("glGetIntegerv");
2357 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2358 vcheckGLcall("glReadBuffer");
2359 glCopyTexImage2D(GL_TEXTURE_2D,
2361 GL_RGBA,
2364 width,
2365 height,
2368 checkGLcall("glCopyTexImage2D");
2369 glReadBuffer(prevRead);
2370 LEAVE_GL();
2372 } else { /* bind the real texture, and make sure it up to date */
2373 IWineD3DSurface_PreLoad(iface);
2375 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2376 ENTER_GL();
2377 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2378 glGetTexImage(GL_TEXTURE_2D,
2379 This->glDescription.level,
2380 GL_RGBA,
2381 GL_UNSIGNED_INT_8_8_8_8_REV,
2382 allocatedMemory);
2383 checkGLcall("glTexImage2D");
2384 if (tmpTexture) {
2385 glBindTexture(GL_TEXTURE_2D, 0);
2386 glDeleteTextures(1, &tmpTexture);
2388 LEAVE_GL();
2390 f = fopen(filename, "w+");
2391 if (NULL == f) {
2392 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2393 return WINED3DERR_INVALIDCALL;
2395 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2396 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2397 /* TGA header */
2398 fputc(0,f);
2399 fputc(0,f);
2400 fputc(2,f);
2401 fputc(0,f);
2402 fputc(0,f);
2403 fputc(0,f);
2404 fputc(0,f);
2405 fputc(0,f);
2406 fputc(0,f);
2407 fputc(0,f);
2408 fputc(0,f);
2409 fputc(0,f);
2410 /* short width*/
2411 fwrite(&width,2,1,f);
2412 /* short height */
2413 fwrite(&height,2,1,f);
2414 /* format rgba */
2415 fputc(0x20,f);
2416 fputc(0x28,f);
2417 /* raw data */
2418 /* 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 */
2419 if(swapChain)
2420 textureRow = allocatedMemory + (width * (height - 1) *4);
2421 else
2422 textureRow = allocatedMemory;
2423 for (y = 0 ; y < height; y++) {
2424 for (i = 0; i < width; i++) {
2425 color = *((DWORD*)textureRow);
2426 fputc((color >> 16) & 0xFF, f); /* B */
2427 fputc((color >> 8) & 0xFF, f); /* G */
2428 fputc((color >> 0) & 0xFF, f); /* R */
2429 fputc((color >> 24) & 0xFF, f); /* A */
2430 textureRow += 4;
2432 /* take two rows of the pointer to the texture memory */
2433 if(swapChain)
2434 (textureRow-= width << 3);
2437 TRACE("Closing file\n");
2438 fclose(f);
2440 if(swapChain) {
2441 IWineD3DSwapChain_Release(swapChain);
2443 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2444 return WINED3D_OK;
2448 * Slightly inefficient way to handle multiple dirty rects but it works :)
2450 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2451 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2452 IWineD3DBaseTexture *baseTexture = NULL;
2454 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2455 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2457 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2458 if (NULL != pDirtyRect) {
2459 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2460 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2461 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2462 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2463 } else {
2464 This->dirtyRect.left = 0;
2465 This->dirtyRect.top = 0;
2466 This->dirtyRect.right = This->currentDesc.Width;
2467 This->dirtyRect.bottom = This->currentDesc.Height;
2469 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2470 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2471 /* if the container is a basetexture then mark it dirty. */
2472 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2473 TRACE("Passing to container\n");
2474 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2475 IWineD3DBaseTexture_Release(baseTexture);
2477 return WINED3D_OK;
2480 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2481 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2482 HRESULT hr;
2483 const GlPixelFormatDesc *glDesc;
2484 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2486 TRACE("(%p) : Calling base function first\n", This);
2487 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2488 if(SUCCEEDED(hr)) {
2489 /* Setup some glformat defaults */
2490 This->glDescription.glFormat = glDesc->glFormat;
2491 This->glDescription.glFormatInternal = glDesc->glInternal;
2492 This->glDescription.glType = glDesc->glType;
2494 This->Flags &= ~SFLAG_ALLOCATED;
2495 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2496 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2498 return hr;
2501 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2502 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2504 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2505 WARN("Surface is locked or the HDC is in use\n");
2506 return WINED3DERR_INVALIDCALL;
2509 if(Mem && Mem != This->resource.allocatedMemory) {
2510 void *release = NULL;
2512 /* Do I have to copy the old surface content? */
2513 if(This->Flags & SFLAG_DIBSECTION) {
2514 /* Release the DC. No need to hold the critical section for the update
2515 * Thread because this thread runs only on front buffers, but this method
2516 * fails for render targets in the check above.
2518 SelectObject(This->hDC, This->dib.holdbitmap);
2519 DeleteDC(This->hDC);
2520 /* Release the DIB section */
2521 DeleteObject(This->dib.DIBsection);
2522 This->dib.bitmap_data = NULL;
2523 This->resource.allocatedMemory = NULL;
2524 This->hDC = NULL;
2525 This->Flags &= ~SFLAG_DIBSECTION;
2526 } else if(!(This->Flags & SFLAG_USERPTR)) {
2527 release = This->resource.heapMemory;
2528 This->resource.heapMemory = NULL;
2530 This->resource.allocatedMemory = Mem;
2531 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2533 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2534 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2536 /* For client textures opengl has to be notified */
2537 if(This->Flags & SFLAG_CLIENT) {
2538 This->Flags &= ~SFLAG_ALLOCATED;
2539 IWineD3DSurface_PreLoad(iface);
2540 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2543 /* Now free the old memory if any */
2544 HeapFree(GetProcessHeap(), 0, release);
2545 } else if(This->Flags & SFLAG_USERPTR) {
2546 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2547 This->resource.allocatedMemory = NULL;
2548 /* HeapMemory should be NULL already */
2549 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2550 This->Flags &= ~SFLAG_USERPTR;
2552 if(This->Flags & SFLAG_CLIENT) {
2553 This->Flags &= ~SFLAG_ALLOCATED;
2554 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2555 IWineD3DSurface_PreLoad(iface);
2558 return WINED3D_OK;
2561 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2562 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2563 IWineD3DSwapChainImpl *swapchain = NULL;
2564 HRESULT hr;
2565 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2567 /* Flipping is only supported on RenderTargets */
2568 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2570 if(override) {
2571 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2572 * FIXME("(%p) Target override is not supported by now\n", This);
2573 * Additionally, it isn't really possible to support triple-buffering
2574 * properly on opengl at all
2578 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2579 if(!swapchain) {
2580 ERR("Flipped surface is not on a swapchain\n");
2581 return WINEDDERR_NOTFLIPPABLE;
2584 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2585 * and only d3d8 and d3d9 apps specify the presentation interval
2587 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2588 /* Most common case first to avoid wasting time on all the other cases */
2589 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2590 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2591 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2592 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2593 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2594 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2595 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2596 } else {
2597 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2600 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2601 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2602 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2603 return hr;
2606 /* Does a direct frame buffer -> texture copy. Stretching is done
2607 * with single pixel copy calls
2609 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2610 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2611 float xrel, yrel;
2612 UINT row;
2613 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2616 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2617 ENTER_GL();
2618 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2620 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2621 glEnable(This->glDescription.target);
2622 checkGLcall("glEnable(This->glDescription.target)");
2624 /* Bind the target texture */
2625 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2626 checkGLcall("glBindTexture");
2627 if(!swapchain) {
2628 glReadBuffer(myDevice->offscreenBuffer);
2629 } else {
2630 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2631 glReadBuffer(buffer);
2633 checkGLcall("glReadBuffer");
2635 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2636 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2638 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2639 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2641 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2642 ERR("Texture filtering not supported in direct blit\n");
2644 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2645 ERR("Texture filtering not supported in direct blit\n");
2648 if(upsidedown &&
2649 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2650 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2651 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2653 glCopyTexSubImage2D(This->glDescription.target,
2654 This->glDescription.level,
2655 drect->x1, drect->y1, /* xoffset, yoffset */
2656 srect->x1, Src->currentDesc.Height - srect->y2,
2657 drect->x2 - drect->x1, drect->y2 - drect->y1);
2658 } else {
2659 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2660 /* I have to process this row by row to swap the image,
2661 * otherwise it would be upside down, so stretching in y direction
2662 * doesn't cost extra time
2664 * However, stretching in x direction can be avoided if not necessary
2666 for(row = drect->y1; row < drect->y2; row++) {
2667 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2668 /* Well, that stuff works, but it's very slow.
2669 * find a better way instead
2671 UINT col;
2673 for(col = drect->x1; col < drect->x2; col++) {
2674 glCopyTexSubImage2D(This->glDescription.target,
2675 This->glDescription.level,
2676 drect->x1 + col, row, /* xoffset, yoffset */
2677 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2678 1, 1);
2680 } else {
2681 glCopyTexSubImage2D(This->glDescription.target,
2682 This->glDescription.level,
2683 drect->x1, row, /* xoffset, yoffset */
2684 srect->x1, yoffset - (int) (row * yrel),
2685 drect->x2-drect->x1, 1);
2689 vcheckGLcall("glCopyTexSubImage2D");
2691 /* Leave the opengl state valid for blitting */
2692 glDisable(This->glDescription.target);
2693 checkGLcall("glDisable(This->glDescription.target)");
2695 LEAVE_GL();
2698 /* Uses the hardware to stretch and flip the image */
2699 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2700 GLuint src, backup = 0;
2701 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2702 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2703 float left, right, top, bottom; /* Texture coordinates */
2704 UINT fbwidth = Src->currentDesc.Width;
2705 UINT fbheight = Src->currentDesc.Height;
2706 GLenum drawBuffer = GL_BACK;
2707 GLenum texture_target;
2709 TRACE("Using hwstretch blit\n");
2710 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2711 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2712 ENTER_GL();
2714 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2716 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2717 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2719 if(GL_LIMITS(aux_buffers) >= 2) {
2720 /* Got more than one aux buffer? Use the 2nd aux buffer */
2721 drawBuffer = GL_AUX1;
2722 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2723 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2724 drawBuffer = GL_AUX0;
2727 if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2728 glGenTextures(1, &backup);
2729 checkGLcall("glGenTextures\n");
2730 glBindTexture(GL_TEXTURE_2D, backup);
2731 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2732 texture_target = GL_TEXTURE_2D;
2733 } else {
2734 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2735 * we are reading from the back buffer, the backup can be used as source texture
2737 if(Src->glDescription.textureName == 0) {
2738 /* Get it a description */
2739 IWineD3DSurface_PreLoad(SrcSurface);
2741 texture_target = Src->glDescription.target;
2742 glBindTexture(texture_target, Src->glDescription.textureName);
2743 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2744 glEnable(texture_target);
2745 checkGLcall("glEnable(texture_target)");
2747 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2748 Src->Flags &= ~SFLAG_INTEXTURE;
2751 glReadBuffer(GL_BACK);
2752 checkGLcall("glReadBuffer(GL_BACK)");
2754 /* TODO: Only back up the part that will be overwritten */
2755 glCopyTexSubImage2D(texture_target, 0,
2756 0, 0 /* read offsets */,
2757 0, 0,
2758 fbwidth,
2759 fbheight);
2761 checkGLcall("glCopyTexSubImage2D");
2763 /* No issue with overriding these - the sampler is dirty due to blit usage */
2764 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2765 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2766 checkGLcall("glTexParameteri");
2767 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2768 minMipLookup[Filter][WINED3DTEXF_NONE]);
2769 checkGLcall("glTexParameteri");
2771 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2772 src = backup ? backup : Src->glDescription.textureName;
2773 } else {
2774 glReadBuffer(GL_FRONT);
2775 checkGLcall("glReadBuffer(GL_FRONT)");
2777 glGenTextures(1, &src);
2778 checkGLcall("glGenTextures(1, &src)");
2779 glBindTexture(GL_TEXTURE_2D, src);
2780 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2782 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2783 * out for power of 2 sizes
2785 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2786 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2787 checkGLcall("glTexImage2D");
2788 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2789 0, 0 /* read offsets */,
2790 0, 0,
2791 fbwidth,
2792 fbheight);
2794 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2795 checkGLcall("glTexParameteri");
2796 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2797 checkGLcall("glTexParameteri");
2799 glReadBuffer(GL_BACK);
2800 checkGLcall("glReadBuffer(GL_BACK)");
2802 if(texture_target != GL_TEXTURE_2D) {
2803 glDisable(texture_target);
2804 glEnable(GL_TEXTURE_2D);
2805 texture_target = GL_TEXTURE_2D;
2808 checkGLcall("glEnd and previous");
2810 left = (float) srect->x1 / (float) Src->pow2Width;
2811 right = (float) srect->x2 / (float) Src->pow2Width;
2813 if(upsidedown) {
2814 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2815 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2816 } else {
2817 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2818 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2821 /* draw the source texture stretched and upside down. The correct surface is bound already */
2822 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2823 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2825 glDrawBuffer(drawBuffer);
2826 glReadBuffer(drawBuffer);
2828 glBegin(GL_QUADS);
2829 /* bottom left */
2830 glTexCoord2f(left, bottom);
2831 glVertex2i(0, fbheight);
2833 /* top left */
2834 glTexCoord2f(left, top);
2835 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2837 /* top right */
2838 glTexCoord2f(right, top);
2839 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2841 /* bottom right */
2842 glTexCoord2f(right, bottom);
2843 glVertex2i(drect->x2 - drect->x1, fbheight);
2844 glEnd();
2845 checkGLcall("glEnd and previous");
2847 if(texture_target != This->glDescription.target) {
2848 glDisable(texture_target);
2849 glEnable(This->glDescription.target);
2850 texture_target = This->glDescription.target;
2853 /* Now read the stretched and upside down image into the destination texture */
2854 glBindTexture(texture_target, This->glDescription.textureName);
2855 checkGLcall("glBindTexture");
2856 glCopyTexSubImage2D(texture_target,
2858 drect->x1, drect->y1, /* xoffset, yoffset */
2859 0, 0, /* We blitted the image to the origin */
2860 drect->x2 - drect->x1, drect->y2 - drect->y1);
2861 checkGLcall("glCopyTexSubImage2D");
2863 if(drawBuffer == GL_BACK) {
2864 /* Write the back buffer backup back */
2865 if(backup) {
2866 if(texture_target != GL_TEXTURE_2D) {
2867 glDisable(texture_target);
2868 glEnable(GL_TEXTURE_2D);
2869 texture_target = GL_TEXTURE_2D;
2871 glBindTexture(GL_TEXTURE_2D, backup);
2872 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2873 } else {
2874 if(texture_target != Src->glDescription.target) {
2875 glDisable(texture_target);
2876 glEnable(Src->glDescription.target);
2877 texture_target = Src->glDescription.target;
2879 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2880 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2883 glBegin(GL_QUADS);
2884 /* top left */
2885 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2886 glVertex2i(0, 0);
2888 /* bottom left */
2889 glTexCoord2f(0.0, 0.0);
2890 glVertex2i(0, fbheight);
2892 /* bottom right */
2893 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2894 glVertex2i(fbwidth, Src->currentDesc.Height);
2896 /* top right */
2897 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2898 glVertex2i(fbwidth, 0);
2899 glEnd();
2900 } else {
2901 /* Restore the old draw buffer */
2902 glDrawBuffer(GL_BACK);
2904 glDisable(texture_target);
2905 checkGLcall("glDisable(texture_target)");
2907 /* Cleanup */
2908 if(src != Src->glDescription.textureName && src != backup) {
2909 glDeleteTextures(1, &src);
2910 checkGLcall("glDeleteTextures(1, &src)");
2912 if(backup) {
2913 glDeleteTextures(1, &backup);
2914 checkGLcall("glDeleteTextures(1, &backup)");
2917 LEAVE_GL();
2920 /* Not called from the VTable */
2921 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2922 WINED3DRECT rect;
2923 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2924 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2925 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2927 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2929 /* Get the swapchain. One of the surfaces has to be a primary surface */
2930 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2931 WARN("Destination is in sysmem, rejecting gl blt\n");
2932 return WINED3DERR_INVALIDCALL;
2934 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2935 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2936 if(Src) {
2937 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2938 WARN("Src is in sysmem, rejecting gl blt\n");
2939 return WINED3DERR_INVALIDCALL;
2941 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2942 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2945 /* Early sort out of cases where no render target is used */
2946 if(!dstSwapchain && !srcSwapchain &&
2947 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2948 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2949 return WINED3DERR_INVALIDCALL;
2952 /* No destination color keying supported */
2953 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2954 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2955 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2956 return WINED3DERR_INVALIDCALL;
2959 if (DestRect) {
2960 rect.x1 = DestRect->left;
2961 rect.y1 = DestRect->top;
2962 rect.x2 = DestRect->right;
2963 rect.y2 = DestRect->bottom;
2964 } else {
2965 rect.x1 = 0;
2966 rect.y1 = 0;
2967 rect.x2 = This->currentDesc.Width;
2968 rect.y2 = This->currentDesc.Height;
2971 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2972 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
2973 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
2974 /* Half-life does a Blt from the back buffer to the front buffer,
2975 * Full surface size, no flags... Use present instead
2977 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
2980 /* Check rects - IWineD3DDevice_Present doesn't handle them */
2981 while(1)
2983 RECT mySrcRect;
2984 TRACE("Looking if a Present can be done...\n");
2985 /* Source Rectangle must be full surface */
2986 if( SrcRect ) {
2987 if(SrcRect->left != 0 || SrcRect->top != 0 ||
2988 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
2989 TRACE("No, Source rectangle doesn't match\n");
2990 break;
2993 mySrcRect.left = 0;
2994 mySrcRect.top = 0;
2995 mySrcRect.right = Src->currentDesc.Width;
2996 mySrcRect.bottom = Src->currentDesc.Height;
2998 /* No stretching may occur */
2999 if(mySrcRect.right != rect.x2 - rect.x1 ||
3000 mySrcRect.bottom != rect.y2 - rect.y1) {
3001 TRACE("No, stretching is done\n");
3002 break;
3005 /* Destination must be full surface or match the clipping rectangle */
3006 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3008 RECT cliprect;
3009 POINT pos[2];
3010 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3011 pos[0].x = rect.x1;
3012 pos[0].y = rect.y1;
3013 pos[1].x = rect.x2;
3014 pos[1].y = rect.y2;
3015 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3016 pos, 2);
3018 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3019 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3021 TRACE("No, dest rectangle doesn't match(clipper)\n");
3022 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3023 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3024 break;
3027 else
3029 if(rect.x1 != 0 || rect.y1 != 0 ||
3030 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3031 TRACE("No, dest rectangle doesn't match(surface size)\n");
3032 break;
3036 TRACE("Yes\n");
3038 /* These flags are unimportant for the flag check, remove them */
3039 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3040 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3042 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3043 * take very long, while a flip is fast.
3044 * This applies to Half-Life, which does such Blts every time it finished
3045 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3046 * menu. This is also used by all apps when they do windowed rendering
3048 * The problem is that flipping is not really the same as copying. After a
3049 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3050 * untouched. Therefore it's necessary to override the swap effect
3051 * and to set it back after the flip.
3053 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3054 * testcases.
3057 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3058 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3060 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3061 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3063 dstSwapchain->presentParms.SwapEffect = orig_swap;
3065 return WINED3D_OK;
3067 break;
3070 TRACE("Unsupported blit between buffers on the same swapchain\n");
3071 return WINED3DERR_INVALIDCALL;
3072 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3073 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3074 return WINED3DERR_INVALIDCALL;
3075 } else if(dstSwapchain && srcSwapchain) {
3076 FIXME("Implement hardware blit between two different swapchains\n");
3077 return WINED3DERR_INVALIDCALL;
3078 } else if(dstSwapchain) {
3079 if(SrcSurface == myDevice->render_targets[0]) {
3080 TRACE("Blit from active render target to a swapchain\n");
3081 /* Handled with regular texture -> swapchain blit */
3083 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3084 FIXME("Implement blit from a swapchain to the active render target\n");
3085 return WINED3DERR_INVALIDCALL;
3088 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3089 /* Blit from render target to texture */
3090 WINED3DRECT srect;
3091 BOOL upsideDown, stretchx;
3093 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3094 TRACE("Color keying not supported by frame buffer to texture blit\n");
3095 return WINED3DERR_INVALIDCALL;
3096 /* Destination color key is checked above */
3099 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3100 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3102 if(SrcRect) {
3103 if(SrcRect->top < SrcRect->bottom) {
3104 srect.y1 = SrcRect->top;
3105 srect.y2 = SrcRect->bottom;
3106 upsideDown = FALSE;
3107 } else {
3108 srect.y1 = SrcRect->bottom;
3109 srect.y2 = SrcRect->top;
3110 upsideDown = TRUE;
3112 srect.x1 = SrcRect->left;
3113 srect.x2 = SrcRect->right;
3114 } else {
3115 srect.x1 = 0;
3116 srect.y1 = 0;
3117 srect.x2 = Src->currentDesc.Width;
3118 srect.y2 = Src->currentDesc.Height;
3119 upsideDown = FALSE;
3121 if(rect.x1 > rect.x2) {
3122 UINT tmp = rect.x2;
3123 rect.x2 = rect.x1;
3124 rect.x1 = tmp;
3125 upsideDown = !upsideDown;
3127 if(!srcSwapchain) {
3128 TRACE("Reading from an offscreen target\n");
3129 upsideDown = !upsideDown;
3132 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3133 stretchx = TRUE;
3134 } else {
3135 stretchx = FALSE;
3138 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3139 * flip the image nor scale it.
3141 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3142 * -> If the app wants a image width an unscaled width, copy it line per line
3143 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3144 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3145 * back buffer. This is slower than reading line per line, thus not used for flipping
3146 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3147 * pixel by pixel
3149 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3150 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3151 * backends.
3153 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3154 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3155 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3156 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3157 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3158 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3159 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3160 } else {
3161 TRACE("Using hardware stretching to flip / stretch the texture\n");
3162 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3165 if(!(This->Flags & SFLAG_DONOTFREE)) {
3166 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3167 This->resource.allocatedMemory = NULL;
3168 This->resource.heapMemory = NULL;
3169 } else {
3170 This->Flags &= ~SFLAG_INSYSMEM;
3172 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3173 * path is never entered
3175 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3177 return WINED3D_OK;
3178 } else if(Src) {
3179 /* Blit from offscreen surface to render target */
3180 float glTexCoord[4];
3181 DWORD oldCKeyFlags = Src->CKeyFlags;
3182 WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3183 RECT SourceRectangle;
3184 BOOL paletteOverride = FALSE;
3186 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3188 if(SrcRect) {
3189 SourceRectangle.left = SrcRect->left;
3190 SourceRectangle.right = SrcRect->right;
3191 SourceRectangle.top = SrcRect->top;
3192 SourceRectangle.bottom = SrcRect->bottom;
3193 } else {
3194 SourceRectangle.left = 0;
3195 SourceRectangle.right = Src->currentDesc.Width;
3196 SourceRectangle.top = 0;
3197 SourceRectangle.bottom = Src->currentDesc.Height;
3199 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3200 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3201 TRACE("Using stretch_rect_fbo\n");
3202 /* The source is always a texture, but never the currently active render target, and the texture
3203 * contents are never upside down
3205 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3206 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3207 return WINED3D_OK;
3210 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3211 /* Fall back to software */
3212 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3213 SourceRectangle.left, SourceRectangle.top,
3214 SourceRectangle.right, SourceRectangle.bottom);
3215 return WINED3DERR_INVALIDCALL;
3218 /* Color keying: Check if we have to do a color keyed blt,
3219 * and if not check if a color key is activated.
3221 * Just modify the color keying parameters in the surface and restore them afterwards
3222 * The surface keeps track of the color key last used to load the opengl surface.
3223 * PreLoad will catch the change to the flags and color key and reload if necessary.
3225 if(Flags & WINEDDBLT_KEYSRC) {
3226 /* Use color key from surface */
3227 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3228 /* Use color key from DDBltFx */
3229 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3230 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3231 } else {
3232 /* Do not use color key */
3233 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3236 /* When blitting from an offscreen surface to a rendertarget, the source
3237 * surface is not required to have a palette. Our rendering / conversion
3238 * code further down the road retrieves the palette from the surface, so
3239 * it must have a palette set. */
3240 if((Src->resource.format == WINED3DFMT_P8) && (Src->palette == NULL)) {
3241 paletteOverride = TRUE;
3242 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3243 Src->palette = This->palette;
3246 /* Now load the surface */
3247 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3250 /* Activate the destination context, set it up for blitting */
3251 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3252 ENTER_GL();
3254 glEnable(Src->glDescription.target);
3255 checkGLcall("glEnable(Src->glDescription.target)");
3257 if(!dstSwapchain) {
3258 TRACE("Drawing to offscreen buffer\n");
3259 glDrawBuffer(myDevice->offscreenBuffer);
3260 checkGLcall("glDrawBuffer");
3261 } else {
3262 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3263 TRACE("Drawing to %#x buffer\n", buffer);
3264 glDrawBuffer(buffer);
3265 checkGLcall("glDrawBuffer");
3268 /* Bind the texture */
3269 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3270 checkGLcall("glBindTexture");
3272 /* Filtering for StretchRect */
3273 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3274 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3275 checkGLcall("glTexParameteri");
3276 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3277 minMipLookup[Filter][WINED3DTEXF_NONE]);
3278 checkGLcall("glTexParameteri");
3279 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3280 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3281 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3282 checkGLcall("glTexEnvi");
3284 /* This is for color keying */
3285 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3286 glEnable(GL_ALPHA_TEST);
3287 checkGLcall("glEnable GL_ALPHA_TEST");
3289 /* When the primary render target uses P8, the alpha component contains the palette index.
3290 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3291 * should be masked away have alpha set to 0. */
3292 if(primary_render_target_is_p8(myDevice))
3293 glAlphaFunc(GL_NOTEQUAL, (float)This->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3294 else
3295 glAlphaFunc(GL_NOTEQUAL, 0.0);
3296 checkGLcall("glAlphaFunc\n");
3297 } else {
3298 glDisable(GL_ALPHA_TEST);
3299 checkGLcall("glDisable GL_ALPHA_TEST");
3302 /* Draw a textured quad
3304 glBegin(GL_QUADS);
3306 glColor3d(1.0f, 1.0f, 1.0f);
3307 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3308 glVertex3f(rect.x1,
3309 rect.y1,
3310 0.0);
3312 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3313 glVertex3f(rect.x1, rect.y2, 0.0);
3315 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3316 glVertex3f(rect.x2,
3317 rect.y2,
3318 0.0);
3320 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3321 glVertex3f(rect.x2,
3322 rect.y1,
3323 0.0);
3324 glEnd();
3325 checkGLcall("glEnd");
3327 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3328 glDisable(GL_ALPHA_TEST);
3329 checkGLcall("glDisable(GL_ALPHA_TEST)");
3332 /* Flush in case the drawable is used by multiple GL contexts */
3333 if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3334 glFlush();
3336 glBindTexture(Src->glDescription.target, 0);
3337 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3338 /* Leave the opengl state valid for blitting */
3339 glDisable(Src->glDescription.target);
3340 checkGLcall("glDisable(Src->glDescription.target)");
3342 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3343 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3345 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3346 glDrawBuffer(GL_BACK);
3347 checkGLcall("glDrawBuffer");
3349 /* Restore the color key parameters */
3350 Src->CKeyFlags = oldCKeyFlags;
3351 This->SrcBltCKey = oldBltCKey;
3353 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3354 if(paletteOverride)
3355 Src->palette = NULL;
3357 LEAVE_GL();
3359 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3360 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3361 * is outdated now
3363 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3364 /* TODO: This should be moved to ModifyLocation() */
3365 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3366 This->Flags |= SFLAG_INTEXTURE;
3369 return WINED3D_OK;
3370 } else {
3371 /* Source-Less Blit to render target */
3372 if (Flags & WINEDDBLT_COLORFILL) {
3373 /* This is easy to handle for the D3D Device... */
3374 DWORD color;
3376 TRACE("Colorfill\n");
3378 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3379 must be true if we are here */
3380 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3381 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3382 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3383 TRACE("Surface is higher back buffer, falling back to software\n");
3384 return WINED3DERR_INVALIDCALL;
3387 /* The color as given in the Blt function is in the format of the frame-buffer...
3388 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3390 if (This->resource.format == WINED3DFMT_P8) {
3391 if (This->palette) {
3392 color = ((0xFF000000) |
3393 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3394 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3395 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3396 } else {
3397 color = 0xFF000000;
3400 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3401 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3402 color = 0xFFFFFFFF;
3403 } else {
3404 color = ((0xFF000000) |
3405 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3406 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3407 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3410 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3411 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3412 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3414 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3415 color = DDBltFx->u5.dwFillColor;
3417 else {
3418 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3419 return WINED3DERR_INVALIDCALL;
3422 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3423 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3424 1, /* Number of rectangles */
3425 &rect, WINED3DCLEAR_TARGET, color,
3426 0.0 /* Z */,
3427 0 /* Stencil */);
3428 return WINED3D_OK;
3432 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3433 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3434 return WINED3DERR_INVALIDCALL;
3437 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3439 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3440 float depth;
3442 if (Flags & WINEDDBLT_DEPTHFILL) {
3443 switch(This->resource.format) {
3444 case WINED3DFMT_D16:
3445 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3446 break;
3447 case WINED3DFMT_D15S1:
3448 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3449 break;
3450 case WINED3DFMT_D24S8:
3451 case WINED3DFMT_D24X8:
3452 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3453 break;
3454 case WINED3DFMT_D32:
3455 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3456 break;
3457 default:
3458 depth = 0.0;
3459 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3462 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3463 DestRect == NULL ? 0 : 1,
3464 (WINED3DRECT *) DestRect,
3465 WINED3DCLEAR_ZBUFFER,
3466 0x00000000,
3467 depth,
3468 0x00000000);
3471 FIXME("(%p): Unsupp depthstencil blit\n", This);
3472 return WINED3DERR_INVALIDCALL;
3475 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3476 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3477 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3478 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3479 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3480 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3482 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3484 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3485 return WINEDDERR_SURFACEBUSY;
3488 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3489 * except depth blits, which seem to work
3491 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3492 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3493 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3494 return WINED3DERR_INVALIDCALL;
3495 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3496 TRACE("Z Blit override handled the blit\n");
3497 return WINED3D_OK;
3501 /* Special cases for RenderTargets */
3502 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3503 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3504 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3507 /* For the rest call the X11 surface implementation.
3508 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3509 * other Blts are rather rare
3511 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3514 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3515 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3516 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3517 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3518 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3520 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3522 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3523 return WINEDDERR_SURFACEBUSY;
3526 if(myDevice->inScene &&
3527 (iface == myDevice->stencilBufferTarget ||
3528 (Source && Source == myDevice->stencilBufferTarget))) {
3529 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3530 return WINED3DERR_INVALIDCALL;
3533 /* Special cases for RenderTargets */
3534 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3535 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3537 RECT SrcRect, DstRect;
3538 DWORD Flags=0;
3540 if(rsrc) {
3541 SrcRect.left = rsrc->left;
3542 SrcRect.top= rsrc->top;
3543 SrcRect.bottom = rsrc->bottom;
3544 SrcRect.right = rsrc->right;
3545 } else {
3546 SrcRect.left = 0;
3547 SrcRect.top = 0;
3548 SrcRect.right = srcImpl->currentDesc.Width;
3549 SrcRect.bottom = srcImpl->currentDesc.Height;
3552 DstRect.left = dstx;
3553 DstRect.top=dsty;
3554 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3555 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3557 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3558 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3559 Flags |= WINEDDBLT_KEYSRC;
3560 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3561 Flags |= WINEDDBLT_KEYDEST;
3562 if(trans & WINEDDBLTFAST_WAIT)
3563 Flags |= WINEDDBLT_WAIT;
3564 if(trans & WINEDDBLTFAST_DONOTWAIT)
3565 Flags |= WINEDDBLT_DONOTWAIT;
3567 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3571 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3574 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3575 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3576 RGBQUAD col[256];
3577 IWineD3DPaletteImpl *pal = This->palette;
3578 unsigned int n;
3579 TRACE("(%p)\n", This);
3581 if (!pal) return WINED3D_OK;
3583 if(This->resource.format == WINED3DFMT_P8 ||
3584 This->resource.format == WINED3DFMT_A8P8)
3586 if(!(This->Flags & SFLAG_INSYSMEM)) {
3587 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3588 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3590 TRACE("Dirtifying surface\n");
3591 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3594 if(This->Flags & SFLAG_DIBSECTION) {
3595 TRACE("(%p): Updating the hdc's palette\n", This);
3596 for (n=0; n<256; n++) {
3597 col[n].rgbRed = pal->palents[n].peRed;
3598 col[n].rgbGreen = pal->palents[n].peGreen;
3599 col[n].rgbBlue = pal->palents[n].peBlue;
3600 col[n].rgbReserved = 0;
3602 SetDIBColorTable(This->hDC, 0, 256, col);
3605 /* Propagate the changes to the drawable when we have a palette.
3606 * TODO: in case of hardware p8 palettes we should only upload the palette. */
3607 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3608 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3610 return WINED3D_OK;
3613 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3614 /** Check against the maximum texture sizes supported by the video card **/
3615 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3616 unsigned int pow2Width, pow2Height;
3617 const GlPixelFormatDesc *glDesc;
3619 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3620 /* Setup some glformat defaults */
3621 This->glDescription.glFormat = glDesc->glFormat;
3622 This->glDescription.glFormatInternal = glDesc->glInternal;
3623 This->glDescription.glType = glDesc->glType;
3625 This->glDescription.textureName = 0;
3626 This->glDescription.target = GL_TEXTURE_2D;
3628 /* Non-power2 support */
3629 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3630 pow2Width = This->currentDesc.Width;
3631 pow2Height = This->currentDesc.Height;
3632 } else {
3633 /* Find the nearest pow2 match */
3634 pow2Width = pow2Height = 1;
3635 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3636 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3638 This->pow2Width = pow2Width;
3639 This->pow2Height = pow2Height;
3641 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3642 WINED3DFORMAT Format = This->resource.format;
3643 /** TODO: add support for non power two compressed textures **/
3644 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3645 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3646 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3647 This, This->currentDesc.Width, This->currentDesc.Height);
3648 return WINED3DERR_NOTAVAILABLE;
3652 if(pow2Width != This->currentDesc.Width ||
3653 pow2Height != This->currentDesc.Height) {
3654 This->Flags |= SFLAG_NONPOW2;
3657 TRACE("%p\n", This);
3658 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3659 /* one of three options
3660 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)
3661 2: Set the texture to the maximum size (bad idea)
3662 3: WARN and return WINED3DERR_NOTAVAILABLE;
3663 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.
3665 WARN("(%p) Creating an oversized surface\n", This);
3666 This->Flags |= SFLAG_OVERSIZE;
3668 /* This will be initialized on the first blt */
3669 This->glRect.left = 0;
3670 This->glRect.top = 0;
3671 This->glRect.right = 0;
3672 This->glRect.bottom = 0;
3673 } else {
3674 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3675 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3676 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3677 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3679 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3680 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3682 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3683 This->pow2Width = This->currentDesc.Width;
3684 This->pow2Height = This->currentDesc.Height;
3685 This->Flags &= ~SFLAG_NONPOW2;
3688 /* No oversize, gl rect is the full texture size */
3689 This->Flags &= ~SFLAG_OVERSIZE;
3690 This->glRect.left = 0;
3691 This->glRect.top = 0;
3692 This->glRect.right = This->pow2Width;
3693 This->glRect.bottom = This->pow2Height;
3696 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3697 switch(wined3d_settings.offscreen_rendering_mode) {
3698 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3699 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3700 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3704 This->Flags |= SFLAG_INSYSMEM;
3706 return WINED3D_OK;
3709 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3710 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3711 IWineD3DBaseTexture *texture;
3713 TRACE("(%p)->(%s, %s)\n", iface,
3714 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3715 persistent ? "TRUE" : "FALSE");
3717 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3718 IWineD3DSwapChain *swapchain = NULL;
3720 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3721 TRACE("Surface %p is an onscreen surface\n", iface);
3723 IWineD3DSwapChain_Release(swapchain);
3724 } else {
3725 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
3726 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3730 if(persistent) {
3731 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3732 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3733 TRACE("Passing to container\n");
3734 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3735 IWineD3DBaseTexture_Release(texture);
3738 This->Flags &= ~SFLAG_LOCATIONS;
3739 This->Flags |= flag;
3740 } else {
3741 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3742 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3743 TRACE("Passing to container\n");
3744 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3745 IWineD3DBaseTexture_Release(texture);
3748 This->Flags &= ~flag;
3752 struct coords {
3753 GLfloat x, y, z;
3756 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3757 struct coords coords[4];
3758 RECT rect;
3759 IWineD3DSwapChain *swapchain = NULL;
3760 IWineD3DBaseTexture *texture = NULL;
3761 HRESULT hr;
3762 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3764 if(rect_in) {
3765 rect = *rect_in;
3766 } else {
3767 rect.left = 0;
3768 rect.top = 0;
3769 rect.right = This->currentDesc.Width;
3770 rect.bottom = This->currentDesc.Height;
3773 ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3774 ENTER_GL();
3776 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3777 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3778 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3779 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3780 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3781 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3782 checkGLcall("glTexParameteri");
3783 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3784 checkGLcall("glTexParameteri");
3786 coords[0].x = rect.left;
3787 coords[0].z = 0;
3789 coords[1].x = rect.left;
3790 coords[1].z = 0;
3792 coords[2].x = rect.right;
3793 coords[2].z = 0;
3795 coords[3].x = rect.right;
3796 coords[3].z = 0;
3798 coords[0].y = rect.top;
3799 coords[1].y = rect.bottom;
3800 coords[2].y = rect.bottom;
3801 coords[3].y = rect.top;
3802 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3803 glEnable(GL_TEXTURE_2D);
3804 checkGLcall("glEnable(GL_TEXTURE_2D)");
3805 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3806 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3807 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3808 checkGLcall("glTexParameteri");
3809 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3810 checkGLcall("glTexParameteri");
3812 coords[0].x = (float)rect.left / This->pow2Width;
3813 coords[0].z = 0;
3815 coords[1].x = (float)rect.left / This->pow2Width;
3816 coords[1].z = 0;
3818 coords[2].x = (float)rect.right / This->pow2Width;
3819 coords[2].z = 0;
3821 coords[3].x = (float)rect.right / This->pow2Width;
3822 coords[3].z = 0;
3824 coords[0].y = (float)rect.top / This->pow2Height;
3825 coords[1].y = (float)rect.bottom / This->pow2Height;
3826 coords[2].y = (float)rect.bottom / This->pow2Height;
3827 coords[3].y = (float)rect.top / This->pow2Height;
3828 } else {
3829 /* Must be a cube map */
3830 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3831 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3832 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3833 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3834 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3835 checkGLcall("glTexParameteri");
3836 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3837 checkGLcall("glTexParameteri");
3839 switch(This->glDescription.target) {
3840 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3841 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3842 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3843 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3844 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3845 break;
3847 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3848 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3849 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3850 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3851 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3852 break;
3854 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3855 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3856 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3857 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3858 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3859 break;
3861 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3862 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3863 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3864 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3865 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3866 break;
3868 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3869 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3870 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3871 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3872 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3873 break;
3875 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3876 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3877 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3878 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3879 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3880 break;
3882 default:
3883 ERR("Unexpected texture target\n");
3884 LEAVE_GL();
3885 return;
3889 glBegin(GL_QUADS);
3890 glTexCoord3fv(&coords[0].x);
3891 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3893 glTexCoord3fv(&coords[1].x);
3894 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
3896 glTexCoord3fv(&coords[2].x);
3897 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3899 glTexCoord3fv(&coords[3].x);
3900 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3901 glEnd();
3902 checkGLcall("glEnd");
3904 if(This->glDescription.target != GL_TEXTURE_2D) {
3905 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3906 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3907 } else {
3908 glDisable(GL_TEXTURE_2D);
3909 checkGLcall("glDisable(GL_TEXTURE_2D)");
3912 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3913 if(hr == WINED3D_OK && swapchain) {
3914 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3915 if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3916 glFlush();
3918 IWineD3DSwapChain_Release(swapchain);
3919 } else {
3920 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3921 * reset properly next draw
3923 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3924 if(hr == WINED3D_OK && texture) {
3925 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3926 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3927 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3928 IWineD3DBaseTexture_Release(texture);
3931 LEAVE_GL();
3934 /*****************************************************************************
3935 * IWineD3DSurface::LoadLocation
3937 * Copies the current surface data from wherever it is to the requested
3938 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3939 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3940 * multiple locations, the gl texture is preferred over the drawable, which is
3941 * preferred over system memory. The PBO counts as system memory. If rect is
3942 * not NULL, only the specified rectangle is copied (only supported for
3943 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3944 * location is marked up to date after the copy.
3946 * Parameters:
3947 * flag: Surface location flag to be updated
3948 * rect: rectangle to be copied
3950 * Returns:
3951 * WINED3D_OK on success
3952 * WINED3DERR_DEVICELOST on an internal error
3954 *****************************************************************************/
3955 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
3956 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3957 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3958 IWineD3DSwapChain *swapchain = NULL;
3959 GLenum format, internal, type;
3960 CONVERT_TYPES convert;
3961 int bpp;
3962 int width, pitch, outpitch;
3963 BYTE *mem;
3965 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3966 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3967 TRACE("Surface %p is an onscreen surface\n", iface);
3969 IWineD3DSwapChain_Release(swapchain);
3970 } else {
3971 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
3972 * Prefer SFLAG_INTEXTURE. */
3973 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
3977 TRACE("(%p)->(%s, %p)\n", iface,
3978 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3979 rect);
3980 if(rect) {
3981 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
3984 if(This->Flags & flag) {
3985 TRACE("Location already up to date\n");
3986 return WINED3D_OK;
3989 if(!(This->Flags & SFLAG_LOCATIONS)) {
3990 ERR("Surface does not have any up to date location\n");
3991 This->Flags |= SFLAG_LOST;
3992 return WINED3DERR_DEVICELOST;
3995 if(flag == SFLAG_INSYSMEM) {
3996 surface_prepare_system_memory(This);
3998 /* Download the surface to system memory */
3999 if(This->Flags & SFLAG_INTEXTURE) {
4000 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4001 surface_bind_and_dirtify(This);
4003 surface_download_data(This);
4004 } else {
4005 read_from_framebuffer(This, rect,
4006 This->resource.allocatedMemory,
4007 IWineD3DSurface_GetPitch(iface));
4009 } else if(flag == SFLAG_INDRAWABLE) {
4010 if(This->Flags & SFLAG_INTEXTURE) {
4011 surface_blt_to_drawable(This, rect);
4012 } else {
4013 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4015 /* The width is in 'length' not in bytes */
4016 width = This->currentDesc.Width;
4017 pitch = IWineD3DSurface_GetPitch(iface);
4019 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4020 int height = This->currentDesc.Height;
4022 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4023 outpitch = width * bpp;
4024 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4026 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4027 if(!mem) {
4028 ERR("Out of memory %d, %d!\n", outpitch, height);
4029 return WINED3DERR_OUTOFVIDEOMEMORY;
4031 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4033 This->Flags |= SFLAG_CONVERTED;
4034 } else {
4035 This->Flags &= ~SFLAG_CONVERTED;
4036 mem = This->resource.allocatedMemory;
4039 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4041 /* Don't delete PBO memory */
4042 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4043 HeapFree(GetProcessHeap(), 0, mem);
4045 } else /* if(flag == SFLAG_INTEXTURE) */ {
4046 if (This->Flags & SFLAG_INDRAWABLE) {
4047 read_from_framebuffer_texture(This);
4048 } else { /* Upload from system memory */
4049 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4051 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4052 surface_bind_and_dirtify(This);
4053 ENTER_GL();
4055 /* The only place where LoadTexture() might get called when isInDraw=1
4056 * is ActivateContext where lastActiveRenderTarget is preloaded.
4058 if(iface == device->lastActiveRenderTarget && device->isInDraw)
4059 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
4061 /* Otherwise: System memory copy must be most up to date */
4063 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4064 This->Flags |= SFLAG_GLCKEY;
4065 This->glCKey = This->SrcBltCKey;
4067 else This->Flags &= ~SFLAG_GLCKEY;
4069 /* The width is in 'length' not in bytes */
4070 width = This->currentDesc.Width;
4071 pitch = IWineD3DSurface_GetPitch(iface);
4073 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4074 int height = This->currentDesc.Height;
4076 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4077 outpitch = width * bpp;
4078 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4080 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4081 if(!mem) {
4082 ERR("Out of memory %d, %d!\n", outpitch, height);
4083 return WINED3DERR_OUTOFVIDEOMEMORY;
4085 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4087 This->Flags |= SFLAG_CONVERTED;
4088 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
4089 d3dfmt_p8_upload_palette(iface, convert);
4090 This->Flags &= ~SFLAG_CONVERTED;
4091 mem = This->resource.allocatedMemory;
4092 } else {
4093 This->Flags &= ~SFLAG_CONVERTED;
4094 mem = This->resource.allocatedMemory;
4097 /* Make sure the correct pitch is used */
4098 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4100 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4101 TRACE("non power of two support\n");
4102 if(!(This->Flags & SFLAG_ALLOCATED)) {
4103 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4105 if (mem || (This->Flags & SFLAG_PBO)) {
4106 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4108 } else {
4109 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4110 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4112 if(!(This->Flags & SFLAG_ALLOCATED)) {
4113 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4115 if (mem || (This->Flags & SFLAG_PBO)) {
4116 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4120 /* Restore the default pitch */
4121 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4122 LEAVE_GL();
4124 /* Don't delete PBO memory */
4125 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4126 HeapFree(GetProcessHeap(), 0, mem);
4130 if(rect == NULL) {
4131 This->Flags |= flag;
4134 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4135 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4136 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4137 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4140 return WINED3D_OK;
4143 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4144 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4145 IWineD3DSwapChain *swapchain = NULL;
4147 /* Update the drawable size method */
4148 if(container) {
4149 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4151 if(swapchain) {
4152 This->get_drawable_size = get_drawable_size_swapchain;
4153 IWineD3DSwapChain_Release(swapchain);
4154 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4155 switch(wined3d_settings.offscreen_rendering_mode) {
4156 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4157 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4158 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4162 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4165 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4166 return SURFACE_OPENGL;
4169 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4171 /* IUnknown */
4172 IWineD3DBaseSurfaceImpl_QueryInterface,
4173 IWineD3DBaseSurfaceImpl_AddRef,
4174 IWineD3DSurfaceImpl_Release,
4175 /* IWineD3DResource */
4176 IWineD3DBaseSurfaceImpl_GetParent,
4177 IWineD3DBaseSurfaceImpl_GetDevice,
4178 IWineD3DBaseSurfaceImpl_SetPrivateData,
4179 IWineD3DBaseSurfaceImpl_GetPrivateData,
4180 IWineD3DBaseSurfaceImpl_FreePrivateData,
4181 IWineD3DBaseSurfaceImpl_SetPriority,
4182 IWineD3DBaseSurfaceImpl_GetPriority,
4183 IWineD3DSurfaceImpl_PreLoad,
4184 IWineD3DSurfaceImpl_UnLoad,
4185 IWineD3DBaseSurfaceImpl_GetType,
4186 /* IWineD3DSurface */
4187 IWineD3DBaseSurfaceImpl_GetContainer,
4188 IWineD3DBaseSurfaceImpl_GetDesc,
4189 IWineD3DSurfaceImpl_LockRect,
4190 IWineD3DSurfaceImpl_UnlockRect,
4191 IWineD3DSurfaceImpl_GetDC,
4192 IWineD3DSurfaceImpl_ReleaseDC,
4193 IWineD3DSurfaceImpl_Flip,
4194 IWineD3DSurfaceImpl_Blt,
4195 IWineD3DBaseSurfaceImpl_GetBltStatus,
4196 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4197 IWineD3DBaseSurfaceImpl_IsLost,
4198 IWineD3DBaseSurfaceImpl_Restore,
4199 IWineD3DSurfaceImpl_BltFast,
4200 IWineD3DBaseSurfaceImpl_GetPalette,
4201 IWineD3DBaseSurfaceImpl_SetPalette,
4202 IWineD3DSurfaceImpl_RealizePalette,
4203 IWineD3DBaseSurfaceImpl_SetColorKey,
4204 IWineD3DBaseSurfaceImpl_GetPitch,
4205 IWineD3DSurfaceImpl_SetMem,
4206 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4207 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4208 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4209 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4210 IWineD3DBaseSurfaceImpl_SetClipper,
4211 IWineD3DBaseSurfaceImpl_GetClipper,
4212 /* Internal use: */
4213 IWineD3DSurfaceImpl_AddDirtyRect,
4214 IWineD3DSurfaceImpl_LoadTexture,
4215 IWineD3DSurfaceImpl_BindTexture,
4216 IWineD3DSurfaceImpl_SaveSnapshot,
4217 IWineD3DSurfaceImpl_SetContainer,
4218 IWineD3DSurfaceImpl_SetGlTextureDesc,
4219 IWineD3DSurfaceImpl_GetGlDesc,
4220 IWineD3DSurfaceImpl_GetData,
4221 IWineD3DSurfaceImpl_SetFormat,
4222 IWineD3DSurfaceImpl_PrivateSetup,
4223 IWineD3DSurfaceImpl_ModifyLocation,
4224 IWineD3DSurfaceImpl_LoadLocation,
4225 IWineD3DSurfaceImpl_GetImplType