wined3d: Implement D3DFMT_G16R16.
[wine/wine-kai.git] / dlls / wined3d / surface.c
blobbedb3065cddc0e37e9804bfc1959bb17f169f3cd
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-2007 Stefan Dösinger for CodeWeavers
11 * Copyright 2007 Henri Verbeet
12 * Copyright 2006-2007 Roderick Colenbrander
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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);
39 static void surface_download_data(IWineD3DSurfaceImpl *This) {
40 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
42 if (0 == This->glDescription.textureName) {
43 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
44 return;
47 if(myDevice->createParms.BehaviorFlags & WINED3DCREATE_MULTITHREADED) {
48 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
51 ENTER_GL();
52 /* Make sure that a proper texture unit is selected, bind the texture
53 * and dirtify the sampler to restore the texture on the next draw
55 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
56 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
57 checkGLcall("glActiveTextureARB");
59 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(0));
60 IWineD3DSurface_BindTexture((IWineD3DSurface *) This);
62 if (This->resource.format == WINED3DFMT_DXT1 ||
63 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
64 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
65 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
66 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
67 } else {
68 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
69 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
71 if(This->Flags & SFLAG_PBO) {
72 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
73 checkGLcall("glBindBufferARB");
74 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
75 checkGLcall("glGetCompressedTexImageARB()");
76 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
77 checkGLcall("glBindBufferARB");
78 } else {
79 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
80 checkGLcall("glGetCompressedTexImageARB()");
83 LEAVE_GL();
84 } else {
85 void *mem;
86 int src_pitch = 0;
87 int dst_pitch = 0;
89 if(This->Flags & SFLAG_CONVERTED) {
90 FIXME("Read back converted textures unsupported\n");
91 LEAVE_GL();
92 return;
95 if (This->Flags & SFLAG_NONPOW2) {
96 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
97 src_pitch = This->bytesPerPixel * This->pow2Width;
98 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
99 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
100 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
101 } else {
102 mem = This->resource.allocatedMemory;
105 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
106 This->glDescription.glFormat, This->glDescription.glType, mem);
108 if(This->Flags & SFLAG_PBO) {
109 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
110 checkGLcall("glBindBufferARB");
112 glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
113 This->glDescription.glType, NULL);
114 checkGLcall("glGetTexImage()");
116 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
117 checkGLcall("glBindBufferARB");
118 } else {
119 glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
120 This->glDescription.glType, mem);
121 checkGLcall("glGetTexImage()");
123 LEAVE_GL();
125 if (This->Flags & SFLAG_NONPOW2) {
126 LPBYTE src_data, dst_data;
127 int y;
129 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
130 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
131 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
133 * We're doing this...
135 * instead of boxing the texture :
136 * |<-texture width ->| -->pow2width| /\
137 * |111111111111111111| | |
138 * |222 Texture 222222| boxed empty | texture height
139 * |3333 Data 33333333| | |
140 * |444444444444444444| | \/
141 * ----------------------------------- |
142 * | boxed empty | boxed empty | pow2height
143 * | | | \/
144 * -----------------------------------
147 * we're repacking the data to the expected texture width
149 * |<-texture width ->| -->pow2width| /\
150 * |111111111111111111222222222222222| |
151 * |222333333333333333333444444444444| texture height
152 * |444444 | |
153 * | | \/
154 * | | |
155 * | empty | pow2height
156 * | | \/
157 * -----------------------------------
159 * == is the same as
161 * |<-texture width ->| /\
162 * |111111111111111111|
163 * |222222222222222222|texture height
164 * |333333333333333333|
165 * |444444444444444444| \/
166 * --------------------
168 * this also means that any references to allocatedMemory should work with the data as if were a
169 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
171 * internally the texture is still stored in a boxed format so any references to textureName will
172 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
174 * Performance should not be an issue, because applications normally do not lock the surfaces when
175 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
176 * and doesn't have to be re-read.
178 src_data = mem;
179 dst_data = This->resource.allocatedMemory;
180 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
181 for (y = 1 ; y < This->currentDesc.Height; y++) {
182 /* skip the first row */
183 src_data += src_pitch;
184 dst_data += dst_pitch;
185 memcpy(dst_data, src_data, dst_pitch);
188 HeapFree(GetProcessHeap(), 0, mem);
192 /* Surface has now been downloaded */
193 This->Flags |= SFLAG_INSYSMEM;
196 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
197 if (This->resource.format == WINED3DFMT_DXT1 ||
198 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
199 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
200 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
201 FIXME("Using DXT1/3/5 without advertized support\n");
202 } else {
203 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
204 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
205 * function uses glCompressedTexImage2D instead of the SubImage call
207 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
208 ENTER_GL();
210 if(This->Flags & SFLAG_PBO) {
211 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
212 checkGLcall("glBindBufferARB");
213 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
215 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
216 width, height, 0 /* border */, This->resource.size, NULL));
217 checkGLcall("glCompressedTexSubImage2D");
219 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
220 checkGLcall("glBindBufferARB");
221 } else {
222 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
223 width, height, 0 /* border */, This->resource.size, data));
224 checkGLcall("glCompressedTexSubImage2D");
226 LEAVE_GL();
228 } else {
229 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
230 ENTER_GL();
232 if(This->Flags & SFLAG_PBO) {
233 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
234 checkGLcall("glBindBufferARB");
235 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
237 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
238 checkGLcall("glTexSubImage2D");
240 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
241 checkGLcall("glBindBufferARB");
243 else {
244 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
245 checkGLcall("glTexSubImage2D");
248 LEAVE_GL();
252 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
253 BOOL enable_client_storage = FALSE;
254 BYTE *mem = NULL;
256 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,
257 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
259 if (This->resource.format == WINED3DFMT_DXT1 ||
260 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
261 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
262 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
263 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
265 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
266 * once, unfortunately
268 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
269 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
270 This->Flags |= SFLAG_CLIENT;
271 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
272 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
273 width, height, 0 /* border */, This->resource.size, mem));
276 return;
279 ENTER_GL();
281 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
282 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
283 /* In some cases we want to disable client storage.
284 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
285 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
286 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
287 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
288 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
290 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
291 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
292 This->Flags &= ~SFLAG_CLIENT;
293 enable_client_storage = TRUE;
294 } else {
295 This->Flags |= SFLAG_CLIENT;
297 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
298 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
300 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
303 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
304 checkGLcall("glTexImage2D");
306 if(enable_client_storage) {
307 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
308 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
310 LEAVE_GL();
312 This->Flags |= SFLAG_ALLOCATED;
315 /* In D3D the depth stencil dimensions have to be greater than or equal to the
316 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
317 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
318 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
319 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
320 renderbuffer_entry_t *entry;
321 GLuint renderbuffer = 0;
322 unsigned int src_width, src_height;
324 src_width = This->pow2Width;
325 src_height = This->pow2Height;
327 /* A depth stencil smaller than the render target is not valid */
328 if (width > src_width || height > src_height) return;
330 /* Remove any renderbuffer set if the sizes match */
331 if (width == src_width && height == src_height) {
332 This->current_renderbuffer = NULL;
333 return;
336 /* Look if we've already got a renderbuffer of the correct dimensions */
337 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
338 if (entry->width == width && entry->height == height) {
339 renderbuffer = entry->id;
340 This->current_renderbuffer = entry;
341 break;
345 if (!renderbuffer) {
346 const GlPixelFormatDesc *glDesc;
347 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
349 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
350 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
351 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
353 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
354 entry->width = width;
355 entry->height = height;
356 entry->id = renderbuffer;
357 list_add_head(&This->renderbuffers, &entry->entry);
359 This->current_renderbuffer = entry;
362 checkGLcall("set_compatible_renderbuffer");
365 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
366 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
367 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
369 TRACE("(%p) : swapchain %p\n", This, swapchain);
371 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
372 TRACE("Returning GL_BACK\n");
373 return GL_BACK;
374 } else if (swapchain_impl->frontBuffer == iface) {
375 TRACE("Returning GL_FRONT\n");
376 return GL_FRONT;
379 FIXME("Higher back buffer, returning GL_BACK\n");
380 return GL_BACK;
383 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
384 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
385 ULONG ref = InterlockedDecrement(&This->resource.ref);
386 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
387 if (ref == 0) {
388 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
389 renderbuffer_entry_t *entry, *entry2;
390 TRACE("(%p) : cleaning up\n", This);
392 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
394 /* Need a context to destroy the texture. Use the currently active render target, but only if
395 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
396 * When destroying the primary rt, Uninit3D will activate a context before doing anything
398 if(device->render_targets && device->render_targets[0]) {
399 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
402 TRACE("Deleting texture %d\n", This->glDescription.textureName);
403 ENTER_GL();
404 glDeleteTextures(1, &This->glDescription.textureName);
405 LEAVE_GL();
408 if(This->Flags & SFLAG_PBO) {
409 /* Delete the PBO */
410 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
413 if(This->Flags & SFLAG_DIBSECTION) {
414 /* Release the DC */
415 SelectObject(This->hDC, This->dib.holdbitmap);
416 DeleteDC(This->hDC);
417 /* Release the DIB section */
418 DeleteObject(This->dib.DIBsection);
419 This->dib.bitmap_data = NULL;
420 This->resource.allocatedMemory = NULL;
422 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
424 HeapFree(GetProcessHeap(), 0, This->palette9);
426 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
427 if(iface == device->ddraw_primary)
428 device->ddraw_primary = NULL;
430 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
431 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
432 HeapFree(GetProcessHeap(), 0, entry);
435 TRACE("(%p) Released\n", This);
436 HeapFree(GetProcessHeap(), 0, This);
439 return ref;
442 /* ****************************************************
443 IWineD3DSurface IWineD3DResource parts follow
444 **************************************************** */
446 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
447 /* TODO: check for locks */
448 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
449 IWineD3DBaseTexture *baseTexture = NULL;
450 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
452 TRACE("(%p)Checking to see if the container is a base texture\n", This);
453 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
454 TRACE("Passing to container\n");
455 IWineD3DBaseTexture_PreLoad(baseTexture);
456 IWineD3DBaseTexture_Release(baseTexture);
457 } else {
458 TRACE("(%p) : About to load surface\n", This);
460 if(!device->isInDraw) {
461 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
464 ENTER_GL();
465 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
466 if (!This->glDescription.level) {
467 if (!This->glDescription.textureName) {
468 glGenTextures(1, &This->glDescription.textureName);
469 checkGLcall("glGenTextures");
470 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
472 glBindTexture(This->glDescription.target, This->glDescription.textureName);
473 checkGLcall("glBindTexture");
474 IWineD3DSurface_LoadTexture(iface, FALSE);
475 /* This is where we should be reducing the amount of GLMemoryUsed */
476 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
477 /* assume this is a coding error not a real error for now */
478 FIXME("Mipmap surface has a glTexture bound to it!\n");
480 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
481 /* Tell opengl to try and keep this texture in video ram (well mostly) */
482 GLclampf tmp;
483 tmp = 0.9f;
484 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
486 LEAVE_GL();
488 return;
491 /* ******************************************************
492 IWineD3DSurface IWineD3DSurface parts follow
493 ****************************************************** */
495 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
496 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
497 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
498 if (This->glDescription.textureName == 0 && textureName != 0) {
499 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
500 IWineD3DSurface_AddDirtyRect(iface, NULL);
502 This->glDescription.textureName = textureName;
503 This->glDescription.target = target;
504 This->Flags &= ~SFLAG_ALLOCATED;
507 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
508 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
509 TRACE("(%p) : returning %p\n", This, &This->glDescription);
510 *glDescription = &This->glDescription;
513 /* TODO: think about moving this down to resource? */
514 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
515 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
516 /* 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 */
517 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
518 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
520 return (CONST void*)(This->resource.allocatedMemory);
523 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
524 IWineD3DSwapChainImpl *swapchain;
525 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
526 BYTE *mem;
527 GLint fmt;
528 GLint type;
529 BYTE *row, *top, *bottom;
530 int i;
531 BOOL bpp;
532 RECT local_rect;
533 BOOL srcIsUpsideDown;
535 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
536 static BOOL warned = FALSE;
537 if(!warned) {
538 ERR("The application tries to lock the render target, but render target locking is disabled\n");
539 warned = TRUE;
541 return;
544 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
545 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
546 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
547 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
548 * context->last_was_blit set on the unlock.
550 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
551 ENTER_GL();
553 /* Select the correct read buffer, and give some debug output.
554 * There is no need to keep track of the current read buffer or reset it, every part of the code
555 * that reads sets the read buffer as desired.
557 if(!swapchain) {
558 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
559 * Read from the back buffer
561 TRACE("Locking offscreen render target\n");
562 glReadBuffer(myDevice->offscreenBuffer);
563 srcIsUpsideDown = TRUE;
564 } else {
565 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
566 TRACE("Locking %#x buffer\n", buffer);
567 glReadBuffer(buffer);
568 checkGLcall("glReadBuffer");
570 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
571 srcIsUpsideDown = FALSE;
574 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
575 if(!rect) {
576 local_rect.left = 0;
577 local_rect.top = 0;
578 local_rect.right = This->currentDesc.Width;
579 local_rect.bottom = This->currentDesc.Height;
580 } else {
581 local_rect = *rect;
583 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
585 switch(This->resource.format)
587 case WINED3DFMT_P8:
589 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
590 /* In case of P8 render targets the index is stored in the alpha component */
591 fmt = GL_ALPHA;
592 type = GL_UNSIGNED_BYTE;
593 mem = dest;
594 bpp = This->bytesPerPixel;
595 } else {
596 /* GL can't return palettized data, so read ARGB pixels into a
597 * separate block of memory and convert them into palettized format
598 * in software. Slow, but if the app means to use palettized render
599 * targets and locks it...
601 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
602 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
603 * for the color channels when palettizing the colors.
605 fmt = GL_RGB;
606 type = GL_UNSIGNED_BYTE;
607 pitch *= 3;
608 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
609 if(!mem) {
610 ERR("Out of memory\n");
611 LEAVE_GL();
612 return;
614 bpp = This->bytesPerPixel * 3;
617 break;
619 default:
620 mem = dest;
621 fmt = This->glDescription.glFormat;
622 type = This->glDescription.glType;
623 bpp = This->bytesPerPixel;
626 if(This->Flags & SFLAG_PBO) {
627 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
628 checkGLcall("glBindBufferARB");
631 glReadPixels(local_rect.left, local_rect.top,
632 local_rect.right - local_rect.left,
633 local_rect.bottom - local_rect.top,
634 fmt, type, mem);
635 vcheckGLcall("glReadPixels");
637 if(This->Flags & SFLAG_PBO) {
638 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
639 checkGLcall("glBindBufferARB");
641 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
642 * to get a pointer to it and perform the flipping in software. This is a lot
643 * faster than calling glReadPixels for each line. In case we want more speed
644 * we should rerender it flipped in a FBO and read the data back from the FBO. */
645 if(!srcIsUpsideDown) {
646 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
647 checkGLcall("glBindBufferARB");
649 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
650 checkGLcall("glMapBufferARB");
654 /* TODO: Merge this with the palettization loop below for P8 targets */
655 if(!srcIsUpsideDown) {
656 UINT len, off;
657 /* glReadPixels returns the image upside down, and there is no way to prevent this.
658 Flip the lines in software */
659 len = (local_rect.right - local_rect.left) * bpp;
660 off = local_rect.left * bpp;
662 row = HeapAlloc(GetProcessHeap(), 0, len);
663 if(!row) {
664 ERR("Out of memory\n");
665 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
666 LEAVE_GL();
667 return;
670 top = mem + pitch * local_rect.top;
671 bottom = ((BYTE *) mem) + pitch * ( local_rect.bottom - local_rect.top - 1);
672 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
673 memcpy(row, top + off, len);
674 memcpy(top + off, bottom + off, len);
675 memcpy(bottom + off, row, len);
676 top += pitch;
677 bottom -= pitch;
679 HeapFree(GetProcessHeap(), 0, row);
681 /* Unmap the temp PBO buffer */
682 if(This->Flags & SFLAG_PBO) {
683 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
684 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
688 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
689 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
690 * the same color but we have no choice.
691 * In case of render targets, the index is stored in the alpha component so no conversion is needed.
693 if((This->resource.format == WINED3DFMT_P8) && !(This->resource.usage & WINED3DUSAGE_RENDERTARGET)) {
694 PALETTEENTRY *pal;
695 DWORD width = pitch / 3;
696 int x, y, c;
697 if(This->palette) {
698 pal = This->palette->palents;
699 } else {
700 pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
703 for(y = local_rect.top; y < local_rect.bottom; y++) {
704 for(x = local_rect.left; x < local_rect.right; x++) {
705 /* start lines pixels */
706 BYTE *blue = (BYTE *) ((BYTE *) mem) + y * pitch + x * (sizeof(BYTE) * 3);
707 BYTE *green = blue + 1;
708 BYTE *red = green + 1;
710 for(c = 0; c < 256; c++) {
711 if(*red == pal[c].peRed &&
712 *green == pal[c].peGreen &&
713 *blue == pal[c].peBlue)
715 *((BYTE *) dest + y * width + x) = c;
716 break;
721 HeapFree(GetProcessHeap(), 0, mem);
723 LEAVE_GL();
726 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
727 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
728 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
729 * changed
731 if(!(This->Flags & SFLAG_DYNLOCK)) {
732 This->lockCount++;
733 /* MAXLOCKCOUNT is defined in wined3d_private.h */
734 if(This->lockCount > MAXLOCKCOUNT) {
735 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
736 This->Flags |= SFLAG_DYNLOCK;
740 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
741 * Also don't create a PBO for systemmem surfaces.
743 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
744 GLenum error;
745 ENTER_GL();
747 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
748 error = glGetError();
749 if(This->pbo == 0 || error != GL_NO_ERROR) {
750 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
753 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
755 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
756 checkGLcall("glBindBufferARB");
758 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
759 checkGLcall("glBufferDataARB");
761 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
762 checkGLcall("glBindBufferARB");
764 /* We don't need the system memory anymore and we can't even use it for PBOs */
765 if(!(This->Flags & SFLAG_CLIENT)) {
766 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
767 This->resource.heapMemory = NULL;
769 This->resource.allocatedMemory = NULL;
770 This->Flags |= SFLAG_PBO;
771 LEAVE_GL();
772 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
773 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
774 * or a pbo to map
776 if(!This->resource.heapMemory) {
777 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
779 This->resource.allocatedMemory =
780 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
781 if(This->Flags & SFLAG_INSYSMEM) {
782 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
787 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
788 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
789 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
790 IWineD3DSwapChain *swapchain = NULL;
792 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
794 /* This is also done in the base class, but we have to verify this before loading any data from
795 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
796 * may interfere, and all other bad things may happen
798 if (This->Flags & SFLAG_LOCKED) {
799 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
800 return WINED3DERR_INVALIDCALL;
802 This->Flags |= SFLAG_LOCKED;
804 if (!(This->Flags & SFLAG_LOCKABLE))
806 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
809 if (Flags & WINED3DLOCK_DISCARD) {
810 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
811 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
812 This->Flags |= SFLAG_INSYSMEM;
815 if (This->Flags & SFLAG_INSYSMEM) {
816 TRACE("Local copy is up to date, not downloading data\n");
817 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
818 goto lock_end;
821 /* Now download the surface content from opengl
822 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
823 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
825 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
826 if(swapchain || iface == myDevice->render_targets[0]) {
827 const RECT *pass_rect = pRect;
829 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
830 * because most caller functions do not need that. So do that here
832 if(pRect &&
833 pRect->top == 0 &&
834 pRect->left == 0 &&
835 pRect->right == This->currentDesc.Width &&
836 pRect->bottom == This->currentDesc.Height) {
837 pass_rect = NULL;
840 switch(wined3d_settings.rendertargetlock_mode) {
841 case RTL_TEXDRAW:
842 case RTL_TEXTEX:
843 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
844 #if 0
845 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
846 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
847 * This may be faster on some cards
849 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
850 #endif
851 /* drop through */
853 case RTL_AUTO:
854 case RTL_READDRAW:
855 case RTL_READTEX:
856 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
857 break;
859 case RTL_DISABLE:
860 break;
862 if(swapchain) IWineD3DSwapChain_Release(swapchain);
864 } else if(iface == myDevice->stencilBufferTarget) {
865 /** the depth stencil in openGL has a format of GL_FLOAT
866 * which should be good for WINED3DFMT_D16_LOCKABLE
867 * and WINED3DFMT_D16
868 * it is unclear what format the stencil buffer is in except.
869 * 'Each index is converted to fixed point...
870 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
871 * mappings in the table GL_PIXEL_MAP_S_TO_S.
872 * glReadPixels(This->lockedRect.left,
873 * This->lockedRect.bottom - j - 1,
874 * This->lockedRect.right - This->lockedRect.left,
875 * 1,
876 * GL_DEPTH_COMPONENT,
877 * type,
878 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
880 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
881 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
882 * none of that is the case the problem is not in this function :-)
883 ********************************************/
884 FIXME("Depth stencil locking not supported yet\n");
885 } else {
886 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
887 TRACE("locking an ordinary surface\n");
888 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
891 lock_end:
892 if(This->Flags & SFLAG_PBO) {
893 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
894 ENTER_GL();
895 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
896 checkGLcall("glBindBufferARB");
898 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
899 if(This->resource.allocatedMemory) {
900 ERR("The surface already has PBO memory allocated!\n");
903 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
904 checkGLcall("glMapBufferARB");
906 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
907 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
908 checkGLcall("glBindBufferARB");
910 LEAVE_GL();
913 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
914 /* Don't dirtify */
915 } else {
916 IWineD3DBaseTexture *pBaseTexture;
918 * Dirtify on lock
919 * as seen in msdn docs
921 IWineD3DSurface_AddDirtyRect(iface, pRect);
923 /** Dirtify Container if needed */
924 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
925 TRACE("Making container dirty\n");
926 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
927 IWineD3DBaseTexture_Release(pBaseTexture);
928 } else {
929 TRACE("Surface is standalone, no need to dirty the container\n");
933 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
936 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This) {
937 GLint prev_store;
938 GLint prev_rasterpos[4];
939 GLint skipBytes = 0;
940 BOOL storechanged = FALSE, memory_allocated = FALSE;
941 GLint fmt, type;
942 BYTE *mem;
943 UINT bpp;
944 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
945 IWineD3DDeviceImpl *myDevice = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
946 IWineD3DSwapChainImpl *swapchain;
948 /* Activate the correct context for the render target */
949 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
950 ENTER_GL();
952 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
953 if(!swapchain) {
954 /* Primary offscreen render target */
955 TRACE("Offscreen render target\n");
956 glDrawBuffer(myDevice->offscreenBuffer);
957 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
958 } else {
959 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
960 TRACE("Unlocking %#x buffer\n", buffer);
961 glDrawBuffer(buffer);
962 checkGLcall("glDrawBuffer");
964 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
967 glFlush();
968 vcheckGLcall("glFlush");
969 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
970 vcheckGLcall("glIntegerv");
971 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
972 vcheckGLcall("glIntegerv");
973 glPixelZoom(1.0, -1.0);
974 vcheckGLcall("glPixelZoom");
976 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
977 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
978 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
980 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
981 vcheckGLcall("glRasterPos2f");
983 /* Some drivers(radeon dri, others?) don't like exceptions during
984 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
985 * after ReleaseDC. Reading it will cause an exception, which x11drv will
986 * catch to put the dib section in InSync mode, which leads to a crash
987 * and a blocked x server on my radeon card.
989 * The following lines read the dib section so it is put in inSync mode
990 * before glDrawPixels is called and the crash is prevented. There won't
991 * be any interfering gdi accesses, because UnlockRect is called from
992 * ReleaseDC, and the app won't use the dc any more afterwards.
994 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
995 volatile BYTE read;
996 read = This->resource.allocatedMemory[0];
999 switch (This->resource.format) {
1000 /* No special care needed */
1001 case WINED3DFMT_A4R4G4B4:
1002 case WINED3DFMT_R5G6B5:
1003 case WINED3DFMT_A1R5G5B5:
1004 case WINED3DFMT_R8G8B8:
1005 case WINED3DFMT_X4R4G4B4:
1006 case WINED3DFMT_X1R5G5B5:
1007 type = This->glDescription.glType;
1008 fmt = This->glDescription.glFormat;
1009 mem = This->resource.allocatedMemory;
1010 bpp = This->bytesPerPixel;
1011 break;
1013 /* In the past times we used to set the X channel of X8R8G8B8 and the above formats to
1014 * 1.0 because it happened to fix the intro movie in Pirates. However, this seems wrong.
1015 * If the game uses an X8R8G8B8 back buffer the GL alpha channel should not make any differences,
1016 * and the bug must be somewhere else. If we really have to set the alpha channel to 1.0 in
1017 * this case do it by clearing it after the draw instead of fixing it up in the CPU. Blending
1018 * is disabled via CTXUSAGE_BLIT context setup, so in the glDrawPixels call it does not
1019 * have any effects
1021 case WINED3DFMT_X8R8G8B8:
1022 case WINED3DFMT_A8R8G8B8:
1024 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1025 vcheckGLcall("glPixelStorei");
1026 storechanged = TRUE;
1027 type = This->glDescription.glType;
1028 fmt = This->glDescription.glFormat;
1029 mem = This->resource.allocatedMemory;
1030 bpp = This->bytesPerPixel;
1032 break;
1034 case WINED3DFMT_A2R10G10B10:
1036 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
1037 vcheckGLcall("glPixelStorei");
1038 storechanged = TRUE;
1039 type = This->glDescription.glType;
1040 fmt = This->glDescription.glFormat;
1041 mem = This->resource.allocatedMemory;
1042 bpp = This->bytesPerPixel;
1044 break;
1046 case WINED3DFMT_P8:
1048 int height = This->glRect.bottom - This->glRect.top;
1049 type = GL_UNSIGNED_BYTE;
1050 fmt = GL_RGBA;
1052 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * sizeof(DWORD));
1053 if(!mem) {
1054 ERR("Out of memory\n");
1055 goto cleanup;
1057 memory_allocated = TRUE;
1058 d3dfmt_convert_surface(This->resource.allocatedMemory,
1059 mem,
1060 pitch,
1061 pitch,
1062 height,
1063 pitch * 4,
1064 CONVERT_PALETTED,
1065 This);
1066 bpp = This->bytesPerPixel * 4;
1067 pitch *= 4;
1069 break;
1071 default:
1072 FIXME("Unsupported Format %u in locking func\n", This->resource.format);
1074 /* Give it a try */
1075 type = This->glDescription.glType;
1076 fmt = This->glDescription.glFormat;
1077 mem = This->resource.allocatedMemory;
1078 bpp = This->bytesPerPixel;
1081 if(This->Flags & SFLAG_PBO) {
1082 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1083 checkGLcall("glBindBufferARB");
1086 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1087 (This->lockedRect.bottom - This->lockedRect.top)-1,
1088 fmt, type,
1089 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1090 checkGLcall("glDrawPixels");
1092 cleanup:
1093 if(This->Flags & SFLAG_PBO) {
1094 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1095 checkGLcall("glBindBufferARB");
1098 glPixelZoom(1.0,1.0);
1099 vcheckGLcall("glPixelZoom");
1101 glRasterPos3iv(&prev_rasterpos[0]);
1102 vcheckGLcall("glRasterPos3iv");
1104 /* Reset to previous pack row length */
1105 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1106 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1107 if(storechanged) {
1108 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
1109 vcheckGLcall("glPixelStorei GL_PACK_SWAP_BYTES");
1112 if(memory_allocated) HeapFree(GetProcessHeap(), 0, mem);
1114 if(!swapchain) {
1115 glDrawBuffer(myDevice->offscreenBuffer);
1116 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1117 } else if(swapchain->backBuffer) {
1118 glDrawBuffer(GL_BACK);
1119 checkGLcall("glDrawBuffer(GL_BACK)");
1120 } else {
1121 glDrawBuffer(GL_FRONT);
1122 checkGLcall("glDrawBuffer(GL_FRONT)");
1124 LEAVE_GL();
1126 return;
1129 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1130 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1131 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1132 IWineD3DSwapChainImpl *swapchain = NULL;
1133 BOOL fullsurface;
1135 if (!(This->Flags & SFLAG_LOCKED)) {
1136 WARN("trying to Unlock an unlocked surf@%p\n", This);
1137 return WINED3DERR_INVALIDCALL;
1140 if (This->Flags & SFLAG_PBO) {
1141 TRACE("Freeing PBO memory\n");
1142 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1143 ENTER_GL();
1144 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1145 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1146 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1147 checkGLcall("glUnmapBufferARB");
1148 LEAVE_GL();
1149 This->resource.allocatedMemory = NULL;
1152 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1154 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1155 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1156 goto unlock_end;
1159 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1160 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1161 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1162 static BOOL warned = FALSE;
1163 if(!warned) {
1164 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1165 warned = TRUE;
1167 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1168 goto unlock_end;
1171 if(This->dirtyRect.left == 0 &&
1172 This->dirtyRect.top == 0 &&
1173 This->dirtyRect.right == This->currentDesc.Width &&
1174 This->dirtyRect.bottom == This->currentDesc.Height) {
1175 fullsurface = TRUE;
1176 } else {
1177 /* TODO: Proper partial rectangle tracking */
1178 fullsurface = FALSE;
1179 This->Flags |= SFLAG_INSYSMEM;
1182 switch(wined3d_settings.rendertargetlock_mode) {
1183 case RTL_READTEX:
1184 case RTL_TEXTEX:
1185 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1186 ENTER_GL();
1187 if (This->glDescription.textureName == 0) {
1188 glGenTextures(1, &This->glDescription.textureName);
1189 checkGLcall("glGenTextures");
1191 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1192 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1193 LEAVE_GL();
1194 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1195 /* drop through */
1197 case RTL_AUTO:
1198 case RTL_READDRAW:
1199 case RTL_TEXDRAW:
1200 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1201 break;
1204 if(!fullsurface) {
1205 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1206 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1207 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1208 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1209 * not fully up to date because only a subrectangle was read in LockRect.
1211 This->Flags &= ~SFLAG_INSYSMEM;
1212 This->Flags |= SFLAG_INDRAWABLE;
1215 This->dirtyRect.left = This->currentDesc.Width;
1216 This->dirtyRect.top = This->currentDesc.Height;
1217 This->dirtyRect.right = 0;
1218 This->dirtyRect.bottom = 0;
1219 } else if(iface == myDevice->stencilBufferTarget) {
1220 FIXME("Depth Stencil buffer locking is not implemented\n");
1221 } else {
1222 /* The rest should be a normal texture */
1223 IWineD3DBaseTextureImpl *impl;
1224 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1225 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1226 * states need resetting
1228 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1229 if(impl->baseTexture.bindCount) {
1230 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1232 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1236 unlock_end:
1237 This->Flags &= ~SFLAG_LOCKED;
1238 memset(&This->lockedRect, 0, sizeof(RECT));
1239 return WINED3D_OK;
1242 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1243 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1244 WINED3DLOCKED_RECT lock;
1245 HRESULT hr;
1246 RGBQUAD col[256];
1248 TRACE("(%p)->(%p)\n",This,pHDC);
1250 if(This->Flags & SFLAG_USERPTR) {
1251 ERR("Not supported on surfaces with an application-provided surfaces\n");
1252 return WINEDDERR_NODC;
1255 /* Give more detailed info for ddraw */
1256 if (This->Flags & SFLAG_DCINUSE)
1257 return WINEDDERR_DCALREADYCREATED;
1259 /* Can't GetDC if the surface is locked */
1260 if (This->Flags & SFLAG_LOCKED)
1261 return WINED3DERR_INVALIDCALL;
1263 memset(&lock, 0, sizeof(lock)); /* To be sure */
1265 /* Create a DIB section if there isn't a hdc yet */
1266 if(!This->hDC) {
1267 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1268 if(This->Flags & SFLAG_CLIENT) {
1269 IWineD3DSurface_PreLoad(iface);
1272 /* Use the dib section from now on if we are not using a PBO */
1273 if(!(This->Flags & SFLAG_PBO))
1274 This->resource.allocatedMemory = This->dib.bitmap_data;
1277 /* Lock the surface */
1278 hr = IWineD3DSurface_LockRect(iface,
1279 &lock,
1280 NULL,
1283 if(This->Flags & SFLAG_PBO) {
1284 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1285 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1288 if(FAILED(hr)) {
1289 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1290 /* keep the dib section */
1291 return hr;
1294 if(This->resource.format == WINED3DFMT_P8 ||
1295 This->resource.format == WINED3DFMT_A8P8) {
1296 unsigned int n;
1297 if(This->palette) {
1298 PALETTEENTRY ent[256];
1300 GetPaletteEntries(This->palette->hpal, 0, 256, ent);
1301 for (n=0; n<256; n++) {
1302 col[n].rgbRed = ent[n].peRed;
1303 col[n].rgbGreen = ent[n].peGreen;
1304 col[n].rgbBlue = ent[n].peBlue;
1305 col[n].rgbReserved = 0;
1307 } else {
1308 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1310 for (n=0; n<256; n++) {
1311 col[n].rgbRed = device->palettes[device->currentPalette][n].peRed;
1312 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
1313 col[n].rgbBlue = device->palettes[device->currentPalette][n].peBlue;
1314 col[n].rgbReserved = 0;
1318 SetDIBColorTable(This->hDC, 0, 256, col);
1321 *pHDC = This->hDC;
1322 TRACE("returning %p\n",*pHDC);
1323 This->Flags |= SFLAG_DCINUSE;
1325 return WINED3D_OK;
1328 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1329 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1331 TRACE("(%p)->(%p)\n",This,hDC);
1333 if (!(This->Flags & SFLAG_DCINUSE))
1334 return WINED3DERR_INVALIDCALL;
1336 if (This->hDC !=hDC) {
1337 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1338 return WINED3DERR_INVALIDCALL;
1341 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1342 /* Copy the contents of the DIB over to the PBO */
1343 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1346 /* we locked first, so unlock now */
1347 IWineD3DSurface_UnlockRect(iface);
1349 This->Flags &= ~SFLAG_DCINUSE;
1351 return WINED3D_OK;
1354 /* ******************************************************
1355 IWineD3DSurface Internal (No mapping to directx api) parts follow
1356 ****************************************************** */
1358 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) {
1359 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1360 const GlPixelFormatDesc *glDesc;
1361 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1362 BOOL p8_render_target = FALSE;
1363 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1365 /* Default values: From the surface */
1366 *format = glDesc->glFormat;
1367 *type = glDesc->glType;
1368 *convert = NO_CONVERSION;
1369 *target_bpp = This->bytesPerPixel;
1371 if(srgb_mode) {
1372 *internal = glDesc->glGammaInternal;
1373 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1374 *internal = glDesc->rtInternal;
1375 } else {
1376 *internal = glDesc->glInternal;
1379 /* Ok, now look if we have to do any conversion */
1380 switch(This->resource.format) {
1381 case WINED3DFMT_P8:
1382 /* ****************
1383 Paletted Texture
1384 **************** */
1386 if (device->render_targets && device->render_targets[0]) {
1387 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1388 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
1389 p8_render_target = TRUE;
1392 /* Use conversion when the paletted texture extension is not available, or when it is available make sure it is used
1393 * for texturing as it won't work for calls like glDraw-/glReadPixels and further also use conversion in case of color keying.
1394 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which conflicts with this.
1396 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && p8_render_target)) || colorkey_active || (!use_texturing && GL_SUPPORT(EXT_PALETTED_TEXTURE)) ) {
1397 *format = GL_RGBA;
1398 *internal = GL_RGBA;
1399 *type = GL_UNSIGNED_BYTE;
1400 *target_bpp = 4;
1401 if(colorkey_active) {
1402 *convert = CONVERT_PALETTED_CK;
1403 } else {
1404 *convert = CONVERT_PALETTED;
1407 else if(GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1408 *format = GL_RED;
1409 *internal = GL_RGBA;
1410 *type = GL_UNSIGNED_BYTE;
1411 *target_bpp = 1;
1414 break;
1416 case WINED3DFMT_R3G3B2:
1417 /* **********************
1418 GL_UNSIGNED_BYTE_3_3_2
1419 ********************** */
1420 if (colorkey_active) {
1421 /* This texture format will never be used.. So do not care about color keying
1422 up until the point in time it will be needed :-) */
1423 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1425 break;
1427 case WINED3DFMT_R5G6B5:
1428 if (colorkey_active) {
1429 *convert = CONVERT_CK_565;
1430 *format = GL_RGBA;
1431 *internal = GL_RGBA;
1432 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1434 break;
1436 case WINED3DFMT_X1R5G5B5:
1437 if (colorkey_active) {
1438 *convert = CONVERT_CK_5551;
1439 *format = GL_BGRA;
1440 *internal = GL_RGBA;
1441 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1443 break;
1445 case WINED3DFMT_R8G8B8:
1446 if (colorkey_active) {
1447 *convert = CONVERT_CK_RGB24;
1448 *format = GL_RGBA;
1449 *internal = GL_RGBA;
1450 *type = GL_UNSIGNED_INT_8_8_8_8;
1451 *target_bpp = 4;
1453 break;
1455 case WINED3DFMT_X8R8G8B8:
1456 if (colorkey_active) {
1457 *convert = CONVERT_RGB32_888;
1458 *format = GL_RGBA;
1459 *internal = GL_RGBA;
1460 *type = GL_UNSIGNED_INT_8_8_8_8;
1462 break;
1464 case WINED3DFMT_V8U8:
1465 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1466 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1467 *format = GL_DUDV_ATI;
1468 *internal = GL_DU8DV8_ATI;
1469 *type = GL_BYTE;
1470 /* No conversion - Just change the gl type */
1471 break;
1473 *convert = CONVERT_V8U8;
1474 *format = GL_BGR;
1475 *internal = GL_RGB8;
1476 *type = GL_UNSIGNED_BYTE;
1477 *target_bpp = 3;
1478 break;
1480 case WINED3DFMT_L6V5U5:
1481 *convert = CONVERT_L6V5U5;
1482 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1483 *target_bpp = 3;
1484 /* Use format and types from table */
1485 } else {
1486 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1487 *target_bpp = 2;
1488 *format = GL_RGB;
1489 *internal = GL_RGB5;
1490 *type = GL_UNSIGNED_SHORT_5_6_5;
1492 break;
1494 case WINED3DFMT_X8L8V8U8:
1495 *convert = CONVERT_X8L8V8U8;
1496 *target_bpp = 4;
1497 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1498 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1499 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1500 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1501 * the needed type and format parameter, so the internal format contains a
1502 * 4th component, which is returned as alpha
1504 } else {
1505 /* Not supported by GL_ATI_envmap_bumpmap */
1506 *format = GL_BGRA;
1507 *internal = GL_RGB8;
1508 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1510 break;
1512 case WINED3DFMT_Q8W8V8U8:
1513 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1514 *convert = CONVERT_Q8W8V8U8;
1515 *format = GL_BGRA;
1516 *internal = GL_RGBA8;
1517 *type = GL_UNSIGNED_BYTE;
1518 *target_bpp = 4;
1519 /* Not supported by GL_ATI_envmap_bumpmap */
1520 break;
1522 case WINED3DFMT_V16U16:
1523 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1524 *convert = CONVERT_V16U16;
1525 *format = GL_BGR;
1526 *internal = GL_RGB16_EXT;
1527 *type = GL_UNSIGNED_SHORT;
1528 *target_bpp = 6;
1529 /* What should I do here about GL_ATI_envmap_bumpmap?
1530 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1532 break;
1534 case WINED3DFMT_A4L4:
1535 /* A4L4 exists as an internal gl format, but for some reason there is not
1536 * format+type combination to load it. Thus convert it to A8L8, then load it
1537 * with A4L4 internal, but A8L8 format+type
1539 *convert = CONVERT_A4L4;
1540 *format = GL_LUMINANCE_ALPHA;
1541 *internal = GL_LUMINANCE4_ALPHA4;
1542 *type = GL_UNSIGNED_BYTE;
1543 *target_bpp = 2;
1544 break;
1546 case WINED3DFMT_R32F:
1547 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1548 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1549 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1550 * 1.0 instead.
1552 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1554 *convert = CONVERT_R32F;
1555 *format = GL_RGB;
1556 *internal = GL_RGB32F_ARB;
1557 *type = GL_FLOAT;
1558 *target_bpp = 12;
1559 break;
1561 case WINED3DFMT_R16F:
1562 /* Similar to R32F */
1563 *convert = CONVERT_R16F;
1564 *format = GL_RGB;
1565 *internal = GL_RGB16F_ARB;
1566 *type = GL_HALF_FLOAT_ARB;
1567 *target_bpp = 6;
1568 break;
1570 case WINED3DFMT_G16R16:
1571 *convert = CONVERT_G16R16;
1572 *format = GL_RGB;
1573 *internal = GL_RGB16_EXT;
1574 *type = GL_UNSIGNED_SHORT;
1575 *target_bpp = 6;
1576 break;
1578 default:
1579 break;
1582 return WINED3D_OK;
1585 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1586 BYTE *source, *dest;
1587 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1589 switch (convert) {
1590 case NO_CONVERSION:
1592 memcpy(dst, src, pitch * height);
1593 break;
1595 case CONVERT_PALETTED:
1596 case CONVERT_PALETTED_CK:
1598 IWineD3DPaletteImpl* pal = This->palette;
1599 BYTE table[256][4];
1600 unsigned int x, y;
1602 if( pal == NULL) {
1603 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1606 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1608 for (y = 0; y < height; y++)
1610 source = src + pitch * y;
1611 dest = dst + outpitch * y;
1612 /* This is an 1 bpp format, using the width here is fine */
1613 for (x = 0; x < width; x++) {
1614 BYTE color = *source++;
1615 *dest++ = table[color][0];
1616 *dest++ = table[color][1];
1617 *dest++ = table[color][2];
1618 *dest++ = table[color][3];
1622 break;
1624 case CONVERT_CK_565:
1626 /* Converting the 565 format in 5551 packed to emulate color-keying.
1628 Note : in all these conversion, it would be best to average the averaging
1629 pixels to get the color of the pixel that will be color-keyed to
1630 prevent 'color bleeding'. This will be done later on if ever it is
1631 too visible.
1633 Note2: Nvidia documents say that their driver does not support alpha + color keying
1634 on the same surface and disables color keying in such a case
1636 unsigned int x, y;
1637 WORD *Source;
1638 WORD *Dest;
1640 TRACE("Color keyed 565\n");
1642 for (y = 0; y < height; y++) {
1643 Source = (WORD *) (src + y * pitch);
1644 Dest = (WORD *) (dst + y * outpitch);
1645 for (x = 0; x < width; x++ ) {
1646 WORD color = *Source++;
1647 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1648 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1649 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1650 *Dest |= 0x0001;
1652 Dest++;
1656 break;
1658 case CONVERT_CK_5551:
1660 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1661 unsigned int x, y;
1662 WORD *Source;
1663 WORD *Dest;
1664 TRACE("Color keyed 5551\n");
1665 for (y = 0; y < height; y++) {
1666 Source = (WORD *) (src + y * pitch);
1667 Dest = (WORD *) (dst + y * outpitch);
1668 for (x = 0; x < width; x++ ) {
1669 WORD color = *Source++;
1670 *Dest = color;
1671 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1672 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1673 *Dest |= (1 << 15);
1675 else {
1676 *Dest &= ~(1 << 15);
1678 Dest++;
1682 break;
1684 case CONVERT_V8U8:
1686 unsigned int x, y;
1687 short *Source;
1688 unsigned char *Dest;
1689 for(y = 0; y < height; y++) {
1690 Source = (short *) (src + y * pitch);
1691 Dest = (unsigned char *) (dst + y * outpitch);
1692 for (x = 0; x < width; x++ ) {
1693 long color = (*Source++);
1694 /* B */ Dest[0] = 0xff;
1695 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1696 /* R */ Dest[2] = (color) + 128; /* U */
1697 Dest += 3;
1700 break;
1703 case CONVERT_V16U16:
1705 unsigned int x, y;
1706 DWORD *Source;
1707 unsigned short *Dest;
1708 for(y = 0; y < height; y++) {
1709 Source = (DWORD *) (src + y * pitch);
1710 Dest = (unsigned short *) (dst + y * outpitch);
1711 for (x = 0; x < width; x++ ) {
1712 DWORD color = (*Source++);
1713 /* B */ Dest[0] = 0xffff;
1714 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1715 /* R */ Dest[2] = (color ) + 32768; /* U */
1716 Dest += 3;
1719 break;
1722 case CONVERT_Q8W8V8U8:
1724 unsigned int x, y;
1725 DWORD *Source;
1726 unsigned char *Dest;
1727 for(y = 0; y < height; y++) {
1728 Source = (DWORD *) (src + y * pitch);
1729 Dest = (unsigned char *) (dst + y * outpitch);
1730 for (x = 0; x < width; x++ ) {
1731 long color = (*Source++);
1732 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1733 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1734 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1735 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1736 Dest += 4;
1739 break;
1742 case CONVERT_L6V5U5:
1744 unsigned int x, y;
1745 WORD *Source;
1746 unsigned char *Dest;
1748 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1749 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1750 * fixed function and shaders without further conversion once the surface is
1751 * loaded
1753 for(y = 0; y < height; y++) {
1754 Source = (WORD *) (src + y * pitch);
1755 Dest = (unsigned char *) (dst + y * outpitch);
1756 for (x = 0; x < width; x++ ) {
1757 short color = (*Source++);
1758 unsigned char l = ((color >> 10) & 0xfc);
1759 char v = ((color >> 5) & 0x3e);
1760 char u = ((color ) & 0x1f);
1762 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1763 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1764 * shift. GL reads a signed value and converts it into an unsigned value.
1766 /* M */ Dest[2] = l << 1;
1768 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1769 * from 5 bit values to 8 bit values.
1771 /* V */ Dest[1] = v << 3;
1772 /* U */ Dest[0] = u << 3;
1773 Dest += 3;
1776 } else {
1777 for(y = 0; y < height; y++) {
1778 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1779 Source = (WORD *) (src + y * pitch);
1780 for (x = 0; x < width; x++ ) {
1781 short color = (*Source++);
1782 unsigned char l = ((color >> 10) & 0xfc);
1783 short v = ((color >> 5) & 0x3e);
1784 short u = ((color ) & 0x1f);
1785 short v_conv = v + 16;
1786 short u_conv = u + 16;
1788 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1789 Dest_s += 1;
1793 break;
1796 case CONVERT_X8L8V8U8:
1798 unsigned int x, y;
1799 DWORD *Source;
1800 unsigned char *Dest;
1802 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1803 /* This implementation works with the fixed function pipeline and shaders
1804 * without further modification after converting the surface.
1806 for(y = 0; y < height; y++) {
1807 Source = (DWORD *) (src + y * pitch);
1808 Dest = (unsigned char *) (dst + y * outpitch);
1809 for (x = 0; x < width; x++ ) {
1810 long color = (*Source++);
1811 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1812 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1813 /* U */ Dest[0] = (color & 0xff); /* U */
1814 /* I */ Dest[3] = 255; /* X */
1815 Dest += 4;
1818 } else {
1819 /* Doesn't work correctly with the fixed function pipeline, but can work in
1820 * shaders if the shader is adjusted. (There's no use for this format in gl's
1821 * standard fixed function pipeline anyway).
1823 for(y = 0; y < height; y++) {
1824 Source = (DWORD *) (src + y * pitch);
1825 Dest = (unsigned char *) (dst + y * outpitch);
1826 for (x = 0; x < width; x++ ) {
1827 long color = (*Source++);
1828 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1829 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1830 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1831 Dest += 4;
1835 break;
1838 case CONVERT_A4L4:
1840 unsigned int x, y;
1841 unsigned char *Source;
1842 unsigned char *Dest;
1843 for(y = 0; y < height; y++) {
1844 Source = (unsigned char *) (src + y * pitch);
1845 Dest = (unsigned char *) (dst + y * outpitch);
1846 for (x = 0; x < width; x++ ) {
1847 unsigned char color = (*Source++);
1848 /* A */ Dest[1] = (color & 0xf0) << 0;
1849 /* L */ Dest[0] = (color & 0x0f) << 4;
1850 Dest += 2;
1853 break;
1856 case CONVERT_R32F:
1858 unsigned int x, y;
1859 float *Source;
1860 float *Dest;
1861 for(y = 0; y < height; y++) {
1862 Source = (float *) (src + y * pitch);
1863 Dest = (float *) (dst + y * outpitch);
1864 for (x = 0; x < width; x++ ) {
1865 float color = (*Source++);
1866 Dest[0] = color;
1867 Dest[1] = 1.0;
1868 Dest[2] = 1.0;
1869 Dest += 3;
1872 break;
1875 case CONVERT_R16F:
1877 unsigned int x, y;
1878 WORD *Source;
1879 WORD *Dest;
1880 WORD one = 0x3c00;
1881 for(y = 0; y < height; y++) {
1882 Source = (WORD *) (src + y * pitch);
1883 Dest = (WORD *) (dst + y * outpitch);
1884 for (x = 0; x < width; x++ ) {
1885 WORD color = (*Source++);
1886 Dest[0] = color;
1887 Dest[1] = one;
1888 Dest[2] = one;
1889 Dest += 3;
1892 break;
1895 case CONVERT_G16R16:
1897 unsigned int x, y;
1898 WORD *Source;
1899 WORD *Dest;
1901 for(y = 0; y < height; y++) {
1902 Source = (WORD *) (src + y * pitch);
1903 Dest = (WORD *) (dst + y * outpitch);
1904 for (x = 0; x < width; x++ ) {
1905 WORD green = (*Source++);
1906 WORD red = (*Source++);
1907 Dest[0] = green;
1908 Dest[1] = red;
1909 Dest[2] = 0xffff;
1910 Dest += 3;
1913 break;
1916 default:
1917 ERR("Unsupported conversation type %d\n", convert);
1919 return WINED3D_OK;
1922 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
1923 IWineD3DPaletteImpl* pal = This->palette;
1924 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1925 BOOL index_in_alpha = FALSE;
1926 int i;
1928 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
1929 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
1930 * is slow. Further RGB->P8 conversion is not possible because palettes can have
1931 * duplicate entries. Store the color key in the unused alpha component to speed the
1932 * download up and to make conversion unneeded. */
1933 if (device->render_targets && device->render_targets[0]) {
1934 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
1936 if(render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
1937 index_in_alpha = TRUE;
1940 if (pal == NULL) {
1941 /* Still no palette? Use the device's palette */
1942 /* Get the surface's palette */
1943 for (i = 0; i < 256; i++) {
1944 table[i][0] = device->palettes[device->currentPalette][i].peRed;
1945 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
1946 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
1948 if(index_in_alpha) {
1949 table[i][3] = i;
1950 } else if (colorkey &&
1951 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1952 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1953 /* We should maybe here put a more 'neutral' color than the standard bright purple
1954 one often used by application to prevent the nice purple borders when bi-linear
1955 filtering is on */
1956 table[i][3] = 0x00;
1957 } else {
1958 table[i][3] = 0xFF;
1961 } else {
1962 TRACE("Using surface palette %p\n", pal);
1963 /* Get the surface's palette */
1964 for (i = 0; i < 256; i++) {
1965 table[i][0] = pal->palents[i].peRed;
1966 table[i][1] = pal->palents[i].peGreen;
1967 table[i][2] = pal->palents[i].peBlue;
1969 if(index_in_alpha) {
1970 table[i][3] = i;
1972 else if (colorkey &&
1973 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1974 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1975 /* We should maybe here put a more 'neutral' color than the standard bright purple
1976 one often used by application to prevent the nice purple borders when bi-linear
1977 filtering is on */
1978 table[i][3] = 0x00;
1979 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
1980 table[i][3] = pal->palents[i].peFlags;
1981 } else {
1982 table[i][3] = 0xFF;
1988 const char *fragment_palette_conversion =
1989 "!!ARBfp1.0\n"
1990 "TEMP index;\n"
1991 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
1992 "TEX index.x, fragment.texcoord[0], texture[0], 2D;\n" /* store the red-component of the current pixel */
1993 "MAD index.x, index.x, constants.x, constants.y;\n" /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
1994 "TEX result.color, index, texture[1], 1D;\n" /* use the red-component as a index in the palette to get the final color */
1995 "END";
1997 /* This function is used in case of 8bit paletted textures to upload the palette.
1998 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
1999 extensions like ATI_fragment_shaders is possible.
2001 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2002 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2003 BYTE table[256][4];
2004 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2006 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2008 /* Try to use the paletted texture extension */
2009 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2011 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2012 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2014 else
2016 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2017 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2018 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2020 /* Create the fragment program if we don't have it */
2021 if(!device->paletteConversionShader)
2023 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2024 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2025 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2026 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2027 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2030 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2031 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2033 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2034 glEnable(GL_TEXTURE_1D);
2035 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2037 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2038 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2039 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2040 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2042 /* Switch back to unit 0 in which the 2D texture will be stored. */
2043 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2045 /* Rebind the texture because it isn't bound anymore */
2046 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2050 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2051 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2053 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2054 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2055 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2057 return FALSE;
2060 if(This->palette9) {
2061 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2062 return FALSE;
2064 } else {
2065 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2067 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2068 return TRUE;
2071 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2072 GLboolean oldwrite[4];
2074 /* Some formats have only some color channels, and the others are 1.0.
2075 * since our rendering renders to all channels, and those pixel formats
2076 * are emulated by using a full texture with the other channels set to 1.0
2077 * manually, clear the unused channels.
2079 * This could be done with hacking colorwriteenable to mask the colors,
2080 * but before drawing the buffer would have to be cleared too, so there's
2081 * no gain in that
2083 switch(This->resource.format) {
2084 case WINED3DFMT_R16F:
2085 case WINED3DFMT_R32F:
2086 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2087 /* Do not activate a context, the correct drawable is active already
2088 * though just the read buffer is set, make sure to have the correct draw
2089 * buffer too
2091 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2092 glDisable(GL_SCISSOR_TEST);
2093 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2094 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2095 glClearColor(0.0, 1.0, 1.0, 1.0);
2096 glClear(GL_COLOR_BUFFER_BIT);
2097 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2098 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2099 checkGLcall("Unused channel clear\n");
2100 break;
2102 default: break;
2106 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2107 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2109 if (!(This->Flags & SFLAG_INTEXTURE)) {
2110 TRACE("Reloading because surface is dirty\n");
2111 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2112 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2113 /* Reload: vice versa OR */
2114 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2115 /* Also reload: Color key is active AND the color key has changed */
2116 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2117 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2118 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2119 TRACE("Reloading because of color keying\n");
2120 /* To perform the color key conversion we need a sysmem copy of
2121 * the surface. Make sure we have it
2123 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2124 } else if(palette9_changed(This)) {
2125 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
2126 /* TODO: This is not necessarily needed with hw palettized texture support */
2127 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2128 } else {
2129 TRACE("surface is already in texture\n");
2130 return WINED3D_OK;
2133 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2134 * These resources are not bound by device size or format restrictions. Because of this,
2135 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2136 * However, these resources can always be created, locked, and copied.
2138 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2140 FIXME("(%p) Operation not supported for scratch textures\n",This);
2141 return WINED3DERR_INVALIDCALL;
2144 This->srgb = srgb_mode;
2145 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2147 #if 0
2149 static unsigned int gen = 0;
2150 char buffer[4096];
2151 ++gen;
2152 if ((gen % 10) == 0) {
2153 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2154 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2157 * debugging crash code
2158 if (gen == 250) {
2159 void** test = NULL;
2160 *test = 0;
2164 #endif
2166 if (!(This->Flags & SFLAG_DONOTFREE)) {
2167 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2168 This->resource.allocatedMemory = NULL;
2169 This->resource.heapMemory = NULL;
2170 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2173 return WINED3D_OK;
2176 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2177 /* TODO: check for locks */
2178 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2179 IWineD3DBaseTexture *baseTexture = NULL;
2180 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2182 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2183 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2184 TRACE("Passing to container\n");
2185 IWineD3DBaseTexture_BindTexture(baseTexture);
2186 IWineD3DBaseTexture_Release(baseTexture);
2187 } else {
2188 TRACE("(%p) : Binding surface\n", This);
2190 if(!device->isInDraw) {
2191 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2193 ENTER_GL();
2194 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2195 LEAVE_GL();
2197 return;
2200 #include <errno.h>
2201 #include <stdio.h>
2202 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2203 FILE* f = NULL;
2204 UINT i, y;
2205 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2206 char *allocatedMemory;
2207 char *textureRow;
2208 IWineD3DSwapChain *swapChain = NULL;
2209 int width, height;
2210 GLuint tmpTexture = 0;
2211 DWORD color;
2212 /*FIXME:
2213 Textures may not be stored in ->allocatedgMemory and a GlTexture
2214 so we should lock the surface before saving a snapshot, or at least check that
2216 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2217 by calling GetTexImage and in compressed form by calling
2218 GetCompressedTexImageARB. Queried compressed images can be saved and
2219 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2220 texture images do not need to be processed by the GL and should
2221 significantly improve texture loading performance relative to uncompressed
2222 images. */
2224 /* Setup the width and height to be the internal texture width and height. */
2225 width = This->pow2Width;
2226 height = This->pow2Height;
2227 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2228 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2230 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2231 /* if were not a real texture then read the back buffer into a real texture */
2232 /* we don't want to interfere with the back buffer so read the data into a temporary
2233 * texture and then save the data out of the temporary texture
2235 GLint prevRead;
2236 ENTER_GL();
2237 TRACE("(%p) Reading render target into texture\n", This);
2238 glEnable(GL_TEXTURE_2D);
2240 glGenTextures(1, &tmpTexture);
2241 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2243 glTexImage2D(GL_TEXTURE_2D,
2245 GL_RGBA,
2246 width,
2247 height,
2248 0/*border*/,
2249 GL_RGBA,
2250 GL_UNSIGNED_INT_8_8_8_8_REV,
2251 NULL);
2253 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2254 vcheckGLcall("glGetIntegerv");
2255 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2256 vcheckGLcall("glReadBuffer");
2257 glCopyTexImage2D(GL_TEXTURE_2D,
2259 GL_RGBA,
2262 width,
2263 height,
2266 checkGLcall("glCopyTexImage2D");
2267 glReadBuffer(prevRead);
2268 LEAVE_GL();
2270 } else { /* bind the real texture, and make sure it up to date */
2271 IWineD3DSurface_PreLoad(iface);
2273 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2274 ENTER_GL();
2275 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2276 glGetTexImage(GL_TEXTURE_2D,
2277 This->glDescription.level,
2278 GL_RGBA,
2279 GL_UNSIGNED_INT_8_8_8_8_REV,
2280 allocatedMemory);
2281 checkGLcall("glTexImage2D");
2282 if (tmpTexture) {
2283 glBindTexture(GL_TEXTURE_2D, 0);
2284 glDeleteTextures(1, &tmpTexture);
2286 LEAVE_GL();
2288 f = fopen(filename, "w+");
2289 if (NULL == f) {
2290 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2291 return WINED3DERR_INVALIDCALL;
2293 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2294 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2295 /* TGA header */
2296 fputc(0,f);
2297 fputc(0,f);
2298 fputc(2,f);
2299 fputc(0,f);
2300 fputc(0,f);
2301 fputc(0,f);
2302 fputc(0,f);
2303 fputc(0,f);
2304 fputc(0,f);
2305 fputc(0,f);
2306 fputc(0,f);
2307 fputc(0,f);
2308 /* short width*/
2309 fwrite(&width,2,1,f);
2310 /* short height */
2311 fwrite(&height,2,1,f);
2312 /* format rgba */
2313 fputc(0x20,f);
2314 fputc(0x28,f);
2315 /* raw data */
2316 /* 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 */
2317 if(swapChain)
2318 textureRow = allocatedMemory + (width * (height - 1) *4);
2319 else
2320 textureRow = allocatedMemory;
2321 for (y = 0 ; y < height; y++) {
2322 for (i = 0; i < width; i++) {
2323 color = *((DWORD*)textureRow);
2324 fputc((color >> 16) & 0xFF, f); /* B */
2325 fputc((color >> 8) & 0xFF, f); /* G */
2326 fputc((color >> 0) & 0xFF, f); /* R */
2327 fputc((color >> 24) & 0xFF, f); /* A */
2328 textureRow += 4;
2330 /* take two rows of the pointer to the texture memory */
2331 if(swapChain)
2332 (textureRow-= width << 3);
2335 TRACE("Closing file\n");
2336 fclose(f);
2338 if(swapChain) {
2339 IWineD3DSwapChain_Release(swapChain);
2341 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2342 return WINED3D_OK;
2346 * Slightly inefficient way to handle multiple dirty rects but it works :)
2348 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2349 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2350 IWineD3DBaseTexture *baseTexture = NULL;
2352 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2353 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2355 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2356 if (NULL != pDirtyRect) {
2357 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2358 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2359 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2360 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2361 } else {
2362 This->dirtyRect.left = 0;
2363 This->dirtyRect.top = 0;
2364 This->dirtyRect.right = This->currentDesc.Width;
2365 This->dirtyRect.bottom = This->currentDesc.Height;
2367 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2368 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2369 /* if the container is a basetexture then mark it dirty. */
2370 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2371 TRACE("Passing to container\n");
2372 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2373 IWineD3DBaseTexture_Release(baseTexture);
2375 return WINED3D_OK;
2378 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2379 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2380 HRESULT hr;
2381 const GlPixelFormatDesc *glDesc;
2382 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2384 TRACE("(%p) : Calling base function first\n", This);
2385 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2386 if(SUCCEEDED(hr)) {
2387 /* Setup some glformat defaults */
2388 This->glDescription.glFormat = glDesc->glFormat;
2389 This->glDescription.glFormatInternal = glDesc->glInternal;
2390 This->glDescription.glType = glDesc->glType;
2392 This->Flags &= ~SFLAG_ALLOCATED;
2393 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2394 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2396 return hr;
2399 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2400 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2402 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2403 WARN("Surface is locked or the HDC is in use\n");
2404 return WINED3DERR_INVALIDCALL;
2407 if(Mem && Mem != This->resource.allocatedMemory) {
2408 void *release = NULL;
2410 /* Do I have to copy the old surface content? */
2411 if(This->Flags & SFLAG_DIBSECTION) {
2412 /* Release the DC. No need to hold the critical section for the update
2413 * Thread because this thread runs only on front buffers, but this method
2414 * fails for render targets in the check above.
2416 SelectObject(This->hDC, This->dib.holdbitmap);
2417 DeleteDC(This->hDC);
2418 /* Release the DIB section */
2419 DeleteObject(This->dib.DIBsection);
2420 This->dib.bitmap_data = NULL;
2421 This->resource.allocatedMemory = NULL;
2422 This->hDC = NULL;
2423 This->Flags &= ~SFLAG_DIBSECTION;
2424 } else if(!(This->Flags & SFLAG_USERPTR)) {
2425 release = This->resource.heapMemory;
2426 This->resource.heapMemory = NULL;
2428 This->resource.allocatedMemory = Mem;
2429 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2431 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2432 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2434 /* For client textures opengl has to be notified */
2435 if(This->Flags & SFLAG_CLIENT) {
2436 This->Flags &= ~SFLAG_ALLOCATED;
2437 IWineD3DSurface_PreLoad(iface);
2438 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2441 /* Now free the old memory if any */
2442 HeapFree(GetProcessHeap(), 0, release);
2443 } else if(This->Flags & SFLAG_USERPTR) {
2444 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2445 This->resource.allocatedMemory = NULL;
2446 /* HeapMemory should be NULL already */
2447 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2448 This->Flags &= ~SFLAG_USERPTR;
2450 if(This->Flags & SFLAG_CLIENT) {
2451 This->Flags &= ~SFLAG_ALLOCATED;
2452 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2453 IWineD3DSurface_PreLoad(iface);
2456 return WINED3D_OK;
2459 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2460 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2461 IWineD3DSwapChainImpl *swapchain = NULL;
2462 HRESULT hr;
2463 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2465 /* Flipping is only supported on RenderTargets */
2466 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2468 if(override) {
2469 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2470 * FIXME("(%p) Target override is not supported by now\n", This);
2471 * Additionally, it isn't really possible to support triple-buffering
2472 * properly on opengl at all
2476 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2477 if(!swapchain) {
2478 ERR("Flipped surface is not on a swapchain\n");
2479 return WINEDDERR_NOTFLIPPABLE;
2482 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2483 * and only d3d8 and d3d9 apps specify the presentation interval
2485 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2486 /* Most common case first to avoid wasting time on all the other cases */
2487 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2488 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2489 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2490 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2491 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2492 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2493 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2494 } else {
2495 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2498 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2499 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2500 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2501 return hr;
2504 /* Does a direct frame buffer -> texture copy. Stretching is done
2505 * with single pixel copy calls
2507 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2508 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2509 float xrel, yrel;
2510 UINT row;
2511 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2514 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2515 ENTER_GL();
2516 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2518 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2519 glEnable(This->glDescription.target);
2520 checkGLcall("glEnable(This->glDescription.target)");
2522 /* Bind the target texture */
2523 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2524 checkGLcall("glBindTexture");
2525 if(!swapchain) {
2526 glReadBuffer(myDevice->offscreenBuffer);
2527 } else {
2528 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2529 glReadBuffer(buffer);
2531 checkGLcall("glReadBuffer");
2533 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2534 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2536 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2537 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2539 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2540 ERR("Texture filtering not supported in direct blit\n");
2542 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2543 ERR("Texture filtering not supported in direct blit\n");
2546 if(upsidedown &&
2547 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2548 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2549 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2551 glCopyTexSubImage2D(This->glDescription.target,
2552 This->glDescription.level,
2553 drect->x1, drect->y1, /* xoffset, yoffset */
2554 srect->x1, Src->currentDesc.Height - srect->y2,
2555 drect->x2 - drect->x1, drect->y2 - drect->y1);
2556 } else {
2557 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2558 /* I have to process this row by row to swap the image,
2559 * otherwise it would be upside down, so stretching in y direction
2560 * doesn't cost extra time
2562 * However, stretching in x direction can be avoided if not necessary
2564 for(row = drect->y1; row < drect->y2; row++) {
2565 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2566 /* Well, that stuff works, but it's very slow.
2567 * find a better way instead
2569 UINT col;
2571 for(col = drect->x1; col < drect->x2; col++) {
2572 glCopyTexSubImage2D(This->glDescription.target,
2573 This->glDescription.level,
2574 drect->x1 + col, row, /* xoffset, yoffset */
2575 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2576 1, 1);
2578 } else {
2579 glCopyTexSubImage2D(This->glDescription.target,
2580 This->glDescription.level,
2581 drect->x1, row, /* xoffset, yoffset */
2582 srect->x1, yoffset - (int) (row * yrel),
2583 drect->x2-drect->x1, 1);
2587 vcheckGLcall("glCopyTexSubImage2D");
2589 /* Leave the opengl state valid for blitting */
2590 glDisable(This->glDescription.target);
2591 checkGLcall("glDisable(This->glDescription.target)");
2593 LEAVE_GL();
2596 /* Uses the hardware to stretch and flip the image */
2597 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2598 GLuint src, backup = 0;
2599 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2600 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2601 float left, right, top, bottom; /* Texture coordinates */
2602 UINT fbwidth = Src->currentDesc.Width;
2603 UINT fbheight = Src->currentDesc.Height;
2604 GLenum drawBuffer = GL_BACK;
2605 GLenum texture_target;
2607 TRACE("Using hwstretch blit\n");
2608 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2609 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2610 ENTER_GL();
2612 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2614 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2615 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2617 if(GL_LIMITS(aux_buffers) >= 2) {
2618 /* Got more than one aux buffer? Use the 2nd aux buffer */
2619 drawBuffer = GL_AUX1;
2620 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2621 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2622 drawBuffer = GL_AUX0;
2625 if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2626 glGenTextures(1, &backup);
2627 checkGLcall("glGenTextures\n");
2628 glBindTexture(GL_TEXTURE_2D, backup);
2629 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2630 texture_target = GL_TEXTURE_2D;
2631 } else {
2632 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2633 * we are reading from the back buffer, the backup can be used as source texture
2635 if(Src->glDescription.textureName == 0) {
2636 /* Get it a description */
2637 IWineD3DSurface_PreLoad(SrcSurface);
2639 texture_target = Src->glDescription.target;
2640 glBindTexture(texture_target, Src->glDescription.textureName);
2641 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2642 glEnable(texture_target);
2643 checkGLcall("glEnable(texture_target)");
2645 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2646 Src->Flags &= ~SFLAG_INTEXTURE;
2649 glReadBuffer(GL_BACK);
2650 checkGLcall("glReadBuffer(GL_BACK)");
2652 /* TODO: Only back up the part that will be overwritten */
2653 glCopyTexSubImage2D(texture_target, 0,
2654 0, 0 /* read offsets */,
2655 0, 0,
2656 fbwidth,
2657 fbheight);
2659 checkGLcall("glCopyTexSubImage2D");
2661 /* No issue with overriding these - the sampler is dirty due to blit usage */
2662 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2663 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2664 checkGLcall("glTexParameteri");
2665 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2666 minMipLookup[Filter][WINED3DTEXF_NONE]);
2667 checkGLcall("glTexParameteri");
2669 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2670 src = backup ? backup : Src->glDescription.textureName;
2671 } else {
2672 glReadBuffer(GL_FRONT);
2673 checkGLcall("glReadBuffer(GL_FRONT)");
2675 glGenTextures(1, &src);
2676 checkGLcall("glGenTextures(1, &src)");
2677 glBindTexture(GL_TEXTURE_2D, src);
2678 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2680 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2681 * out for power of 2 sizes
2683 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2684 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2685 checkGLcall("glTexImage2D");
2686 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2687 0, 0 /* read offsets */,
2688 0, 0,
2689 fbwidth,
2690 fbheight);
2692 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2693 checkGLcall("glTexParameteri");
2694 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2695 checkGLcall("glTexParameteri");
2697 glReadBuffer(GL_BACK);
2698 checkGLcall("glReadBuffer(GL_BACK)");
2700 if(texture_target != GL_TEXTURE_2D) {
2701 glDisable(texture_target);
2702 glEnable(GL_TEXTURE_2D);
2703 texture_target = GL_TEXTURE_2D;
2706 checkGLcall("glEnd and previous");
2708 left = (float) srect->x1 / (float) Src->pow2Width;
2709 right = (float) srect->x2 / (float) Src->pow2Width;
2711 if(upsidedown) {
2712 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2713 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2714 } else {
2715 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2716 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2719 /* draw the source texture stretched and upside down. The correct surface is bound already */
2720 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2721 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2723 glDrawBuffer(drawBuffer);
2724 glReadBuffer(drawBuffer);
2726 glBegin(GL_QUADS);
2727 /* bottom left */
2728 glTexCoord2f(left, bottom);
2729 glVertex2i(0, fbheight);
2731 /* top left */
2732 glTexCoord2f(left, top);
2733 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2735 /* top right */
2736 glTexCoord2f(right, top);
2737 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2739 /* bottom right */
2740 glTexCoord2f(right, bottom);
2741 glVertex2i(drect->x2 - drect->x1, fbheight);
2742 glEnd();
2743 checkGLcall("glEnd and previous");
2745 if(texture_target != This->glDescription.target) {
2746 glDisable(texture_target);
2747 glEnable(This->glDescription.target);
2748 texture_target = This->glDescription.target;
2751 /* Now read the stretched and upside down image into the destination texture */
2752 glBindTexture(texture_target, This->glDescription.textureName);
2753 checkGLcall("glBindTexture");
2754 glCopyTexSubImage2D(texture_target,
2756 drect->x1, drect->y1, /* xoffset, yoffset */
2757 0, 0, /* We blitted the image to the origin */
2758 drect->x2 - drect->x1, drect->y2 - drect->y1);
2759 checkGLcall("glCopyTexSubImage2D");
2761 if(drawBuffer == GL_BACK) {
2762 /* Write the back buffer backup back */
2763 if(backup) {
2764 if(texture_target != GL_TEXTURE_2D) {
2765 glDisable(texture_target);
2766 glEnable(GL_TEXTURE_2D);
2767 texture_target = GL_TEXTURE_2D;
2769 glBindTexture(GL_TEXTURE_2D, backup);
2770 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2771 } else {
2772 if(texture_target != Src->glDescription.target) {
2773 glDisable(texture_target);
2774 glEnable(Src->glDescription.target);
2775 texture_target = Src->glDescription.target;
2777 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2778 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2781 glBegin(GL_QUADS);
2782 /* top left */
2783 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2784 glVertex2i(0, 0);
2786 /* bottom left */
2787 glTexCoord2f(0.0, 0.0);
2788 glVertex2i(0, fbheight);
2790 /* bottom right */
2791 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2792 glVertex2i(fbwidth, Src->currentDesc.Height);
2794 /* top right */
2795 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2796 glVertex2i(fbwidth, 0);
2797 glEnd();
2798 } else {
2799 /* Restore the old draw buffer */
2800 glDrawBuffer(GL_BACK);
2802 glDisable(texture_target);
2803 checkGLcall("glDisable(texture_target)");
2805 /* Cleanup */
2806 if(src != Src->glDescription.textureName && src != backup) {
2807 glDeleteTextures(1, &src);
2808 checkGLcall("glDeleteTextures(1, &src)");
2810 if(backup) {
2811 glDeleteTextures(1, &backup);
2812 checkGLcall("glDeleteTextures(1, &backup)");
2815 LEAVE_GL();
2818 /* Not called from the VTable */
2819 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2820 WINED3DRECT rect;
2821 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2822 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2823 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2825 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2827 /* Get the swapchain. One of the surfaces has to be a primary surface */
2828 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2829 WARN("Destination is in sysmem, rejecting gl blt\n");
2830 return WINED3DERR_INVALIDCALL;
2832 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2833 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2834 if(Src) {
2835 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2836 WARN("Src is in sysmem, rejecting gl blt\n");
2837 return WINED3DERR_INVALIDCALL;
2839 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2840 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2843 /* Early sort out of cases where no render target is used */
2844 if(!dstSwapchain && !srcSwapchain &&
2845 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2846 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2847 return WINED3DERR_INVALIDCALL;
2850 /* No destination color keying supported */
2851 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2852 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2853 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2854 return WINED3DERR_INVALIDCALL;
2857 if (DestRect) {
2858 rect.x1 = DestRect->left;
2859 rect.y1 = DestRect->top;
2860 rect.x2 = DestRect->right;
2861 rect.y2 = DestRect->bottom;
2862 } else {
2863 rect.x1 = 0;
2864 rect.y1 = 0;
2865 rect.x2 = This->currentDesc.Width;
2866 rect.y2 = This->currentDesc.Height;
2869 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2870 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
2871 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
2872 /* Half-life does a Blt from the back buffer to the front buffer,
2873 * Full surface size, no flags... Use present instead
2875 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
2878 /* Check rects - IWineD3DDevice_Present doesn't handle them */
2879 while(1)
2881 RECT mySrcRect;
2882 TRACE("Looking if a Present can be done...\n");
2883 /* Source Rectangle must be full surface */
2884 if( SrcRect ) {
2885 if(SrcRect->left != 0 || SrcRect->top != 0 ||
2886 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
2887 TRACE("No, Source rectangle doesn't match\n");
2888 break;
2891 mySrcRect.left = 0;
2892 mySrcRect.top = 0;
2893 mySrcRect.right = Src->currentDesc.Width;
2894 mySrcRect.bottom = Src->currentDesc.Height;
2896 /* No stretching may occur */
2897 if(mySrcRect.right != rect.x2 - rect.x1 ||
2898 mySrcRect.bottom != rect.y2 - rect.y1) {
2899 TRACE("No, stretching is done\n");
2900 break;
2903 /* Destination must be full surface or match the clipping rectangle */
2904 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
2906 RECT cliprect;
2907 POINT pos[2];
2908 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
2909 pos[0].x = rect.x1;
2910 pos[0].y = rect.y1;
2911 pos[1].x = rect.x2;
2912 pos[1].y = rect.y2;
2913 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
2914 pos, 2);
2916 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
2917 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
2919 TRACE("No, dest rectangle doesn't match(clipper)\n");
2920 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
2921 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
2922 break;
2925 else
2927 if(rect.x1 != 0 || rect.y1 != 0 ||
2928 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
2929 TRACE("No, dest rectangle doesn't match(surface size)\n");
2930 break;
2934 TRACE("Yes\n");
2936 /* These flags are unimportant for the flag check, remove them */
2937 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
2938 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
2940 /* The idea behind this is that a glReadPixels and a glDrawPixels call
2941 * take very long, while a flip is fast.
2942 * This applies to Half-Life, which does such Blts every time it finished
2943 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
2944 * menu. This is also used by all apps when they do windowed rendering
2946 * The problem is that flipping is not really the same as copying. After a
2947 * Blt the front buffer is a copy of the back buffer, and the back buffer is
2948 * untouched. Therefore it's necessary to override the swap effect
2949 * and to set it back after the flip.
2951 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
2952 * testcases.
2955 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
2956 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2958 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
2959 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
2961 dstSwapchain->presentParms.SwapEffect = orig_swap;
2963 return WINED3D_OK;
2965 break;
2968 TRACE("Unsupported blit between buffers on the same swapchain\n");
2969 return WINED3DERR_INVALIDCALL;
2970 } else if((dstSwapchain || This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) &&
2971 (srcSwapchain || SrcSurface == myDevice->render_targets[0]) ) {
2972 ERR("Can't perform hardware blit between 2 different swapchains, falling back to software\n");
2973 return WINED3DERR_INVALIDCALL;
2976 if(srcSwapchain || SrcSurface == myDevice->render_targets[0]) {
2977 /* Blit from render target to texture */
2978 WINED3DRECT srect;
2979 BOOL upsideDown, stretchx;
2981 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
2982 TRACE("Color keying not supported by frame buffer to texture blit\n");
2983 return WINED3DERR_INVALIDCALL;
2984 /* Destination color key is checked above */
2987 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2988 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2990 if(SrcRect) {
2991 if(SrcRect->top < SrcRect->bottom) {
2992 srect.y1 = SrcRect->top;
2993 srect.y2 = SrcRect->bottom;
2994 upsideDown = FALSE;
2995 } else {
2996 srect.y1 = SrcRect->bottom;
2997 srect.y2 = SrcRect->top;
2998 upsideDown = TRUE;
3000 srect.x1 = SrcRect->left;
3001 srect.x2 = SrcRect->right;
3002 } else {
3003 srect.x1 = 0;
3004 srect.y1 = 0;
3005 srect.x2 = Src->currentDesc.Width;
3006 srect.y2 = Src->currentDesc.Height;
3007 upsideDown = FALSE;
3009 if(rect.x1 > rect.x2) {
3010 UINT tmp = rect.x2;
3011 rect.x2 = rect.x1;
3012 rect.x1 = tmp;
3013 upsideDown = !upsideDown;
3015 if(!srcSwapchain) {
3016 TRACE("Reading from an offscreen target\n");
3017 upsideDown = !upsideDown;
3020 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3021 stretchx = TRUE;
3022 } else {
3023 stretchx = FALSE;
3026 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3027 * flip the image nor scale it.
3029 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3030 * -> If the app wants a image width an unscaled width, copy it line per line
3031 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3032 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3033 * back buffer. This is slower than reading line per line, thus not used for flipping
3034 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3035 * pixel by pixel
3037 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3038 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3039 * backends.
3041 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3042 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3043 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3044 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3045 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3046 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3047 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3048 } else {
3049 TRACE("Using hardware stretching to flip / stretch the texture\n");
3050 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3053 if(!(This->Flags & SFLAG_DONOTFREE)) {
3054 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3055 This->resource.allocatedMemory = NULL;
3056 This->resource.heapMemory = NULL;
3057 } else {
3058 This->Flags &= ~SFLAG_INSYSMEM;
3060 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3061 * path is never entered
3063 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3065 return WINED3D_OK;
3066 } else if(Src) {
3067 /* Blit from offscreen surface to render target */
3068 float glTexCoord[4];
3069 DWORD oldCKeyFlags = Src->CKeyFlags;
3070 WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3071 RECT SourceRectangle;
3073 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3075 if(SrcRect) {
3076 SourceRectangle.left = SrcRect->left;
3077 SourceRectangle.right = SrcRect->right;
3078 SourceRectangle.top = SrcRect->top;
3079 SourceRectangle.bottom = SrcRect->bottom;
3080 } else {
3081 SourceRectangle.left = 0;
3082 SourceRectangle.right = Src->currentDesc.Width;
3083 SourceRectangle.top = 0;
3084 SourceRectangle.bottom = Src->currentDesc.Height;
3087 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3088 /* Fall back to software */
3089 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3090 SourceRectangle.left, SourceRectangle.top,
3091 SourceRectangle.right, SourceRectangle.bottom);
3092 return WINED3DERR_INVALIDCALL;
3095 /* Color keying: Check if we have to do a color keyed blt,
3096 * and if not check if a color key is activated.
3098 * Just modify the color keying parameters in the surface and restore them afterwards
3099 * The surface keeps track of the color key last used to load the opengl surface.
3100 * PreLoad will catch the change to the flags and color key and reload if necessary.
3102 if(Flags & WINEDDBLT_KEYSRC) {
3103 /* Use color key from surface */
3104 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3105 /* Use color key from DDBltFx */
3106 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3107 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3108 } else {
3109 /* Do not use color key */
3110 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3113 /* Now load the surface */
3114 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3117 /* Activate the destination context, set it up for blitting */
3118 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3119 ENTER_GL();
3121 glEnable(Src->glDescription.target);
3122 checkGLcall("glEnable(Src->glDescription.target)");
3124 if(!dstSwapchain) {
3125 TRACE("Drawing to offscreen buffer\n");
3126 glDrawBuffer(myDevice->offscreenBuffer);
3127 checkGLcall("glDrawBuffer");
3128 } else {
3129 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3130 TRACE("Drawing to %#x buffer\n", buffer);
3131 glDrawBuffer(buffer);
3132 checkGLcall("glDrawBuffer");
3135 /* Bind the texture */
3136 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3137 checkGLcall("glBindTexture");
3139 /* Filtering for StretchRect */
3140 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3141 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3142 checkGLcall("glTexParameteri");
3143 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3144 minMipLookup[Filter][WINED3DTEXF_NONE]);
3145 checkGLcall("glTexParameteri");
3146 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3147 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3148 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3149 checkGLcall("glTexEnvi");
3151 /* This is for color keying */
3152 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3153 glEnable(GL_ALPHA_TEST);
3154 checkGLcall("glEnable GL_ALPHA_TEST");
3155 glAlphaFunc(GL_NOTEQUAL, 0.0);
3156 checkGLcall("glAlphaFunc\n");
3157 } else {
3158 glDisable(GL_ALPHA_TEST);
3159 checkGLcall("glDisable GL_ALPHA_TEST");
3162 /* Draw a textured quad
3164 glBegin(GL_QUADS);
3166 glColor3d(1.0f, 1.0f, 1.0f);
3167 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3168 glVertex3f(rect.x1,
3169 rect.y1,
3170 0.0);
3172 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3173 glVertex3f(rect.x1, rect.y2, 0.0);
3175 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3176 glVertex3f(rect.x2,
3177 rect.y2,
3178 0.0);
3180 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3181 glVertex3f(rect.x2,
3182 rect.y1,
3183 0.0);
3184 glEnd();
3185 checkGLcall("glEnd");
3187 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3188 glDisable(GL_ALPHA_TEST);
3189 checkGLcall("glDisable(GL_ALPHA_TEST)");
3192 /* Flush in case the drawable is used by multiple GL contexts */
3193 if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3194 glFlush();
3196 glBindTexture(Src->glDescription.target, 0);
3197 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3198 /* Leave the opengl state valid for blitting */
3199 glDisable(Src->glDescription.target);
3200 checkGLcall("glDisable(Src->glDescription.target)");
3202 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3203 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3205 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3206 glDrawBuffer(GL_BACK);
3207 checkGLcall("glDrawBuffer");
3209 /* Restore the color key parameters */
3210 Src->CKeyFlags = oldCKeyFlags;
3211 This->SrcBltCKey = oldBltCKey;
3213 LEAVE_GL();
3215 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3216 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3217 * is outdated now
3219 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3220 /* TODO: This should be moved to ModifyLocation() */
3221 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3222 This->Flags |= SFLAG_INTEXTURE;
3225 return WINED3D_OK;
3226 } else {
3227 /* Source-Less Blit to render target */
3228 if (Flags & WINEDDBLT_COLORFILL) {
3229 /* This is easy to handle for the D3D Device... */
3230 DWORD color;
3232 TRACE("Colorfill\n");
3234 /* The color as given in the Blt function is in the format of the frame-buffer...
3235 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3237 if (This->resource.format == WINED3DFMT_P8) {
3238 if (This->palette) {
3239 color = ((0xFF000000) |
3240 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3241 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3242 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3243 } else {
3244 color = 0xFF000000;
3247 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3248 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3249 color = 0xFFFFFFFF;
3250 } else {
3251 color = ((0xFF000000) |
3252 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3253 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3254 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3257 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3258 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3259 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3261 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3262 color = DDBltFx->u5.dwFillColor;
3264 else {
3265 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3266 return WINED3DERR_INVALIDCALL;
3269 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_RESOURCELOAD);
3270 ENTER_GL();
3272 TRACE("Calling GetSwapChain with mydevice = %p\n", myDevice);
3273 if(dstSwapchain && dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]) {
3274 glDrawBuffer(GL_BACK);
3275 checkGLcall("glDrawBuffer(GL_BACK)");
3276 } else if (dstSwapchain && This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer) {
3277 glDrawBuffer(GL_FRONT);
3278 checkGLcall("glDrawBuffer(GL_FRONT)");
3279 } else if(This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3280 glDrawBuffer(myDevice->offscreenBuffer);
3281 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer3)");
3282 } else {
3283 LEAVE_GL();
3284 TRACE("Surface is higher back buffer, falling back to software\n");
3285 return WINED3DERR_INVALIDCALL;
3288 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3290 IWineD3DDevice_Clear( (IWineD3DDevice *) myDevice,
3291 1 /* Number of rectangles */,
3292 &rect,
3293 WINED3DCLEAR_TARGET,
3294 color,
3295 0.0 /* Z */,
3296 0 /* Stencil */);
3298 /* Restore the original draw buffer */
3299 if(!dstSwapchain) {
3300 glDrawBuffer(myDevice->offscreenBuffer);
3301 } else if(dstSwapchain->backBuffer && dstSwapchain->backBuffer[0]) {
3302 glDrawBuffer(GL_BACK);
3304 vcheckGLcall("glDrawBuffer");
3305 LEAVE_GL();
3307 return WINED3D_OK;
3311 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3312 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3313 return WINED3DERR_INVALIDCALL;
3316 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3318 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3319 float depth;
3321 if (Flags & WINEDDBLT_DEPTHFILL) {
3322 switch(This->resource.format) {
3323 case WINED3DFMT_D16:
3324 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3325 break;
3326 case WINED3DFMT_D15S1:
3327 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3328 break;
3329 case WINED3DFMT_D24S8:
3330 case WINED3DFMT_D24X8:
3331 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3332 break;
3333 case WINED3DFMT_D32:
3334 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3335 break;
3336 default:
3337 depth = 0.0;
3338 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3341 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3342 DestRect == NULL ? 0 : 1,
3343 (WINED3DRECT *) DestRect,
3344 WINED3DCLEAR_ZBUFFER,
3345 0x00000000,
3346 depth,
3347 0x00000000);
3350 FIXME("(%p): Unsupp depthstencil blit\n", This);
3351 return WINED3DERR_INVALIDCALL;
3354 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3355 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3356 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3357 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3358 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3359 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3361 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3362 * except depth blits, which seem to work
3364 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3365 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3366 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3367 return WINED3DERR_INVALIDCALL;
3368 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3369 TRACE("Z Blit override handled the blit\n");
3370 return WINED3D_OK;
3374 /* Special cases for RenderTargets */
3375 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3376 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3377 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3380 /* For the rest call the X11 surface implementation.
3381 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3382 * other Blts are rather rare
3384 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3387 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3388 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3389 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3390 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3391 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3393 if(myDevice->inScene &&
3394 (iface == myDevice->stencilBufferTarget ||
3395 (Source && Source == myDevice->stencilBufferTarget))) {
3396 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3397 return WINED3DERR_INVALIDCALL;
3400 /* Special cases for RenderTargets */
3401 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3402 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3404 RECT SrcRect, DstRect;
3405 DWORD Flags=0;
3407 if(rsrc) {
3408 SrcRect.left = rsrc->left;
3409 SrcRect.top= rsrc->top;
3410 SrcRect.bottom = rsrc->bottom;
3411 SrcRect.right = rsrc->right;
3412 } else {
3413 SrcRect.left = 0;
3414 SrcRect.top = 0;
3415 SrcRect.right = srcImpl->currentDesc.Width;
3416 SrcRect.bottom = srcImpl->currentDesc.Height;
3419 DstRect.left = dstx;
3420 DstRect.top=dsty;
3421 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3422 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3424 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3425 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3426 Flags |= WINEDDBLT_KEYSRC;
3427 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3428 Flags |= WINEDDBLT_KEYDEST;
3429 if(trans & WINEDDBLTFAST_WAIT)
3430 Flags |= WINEDDBLT_WAIT;
3431 if(trans & WINEDDBLTFAST_DONOTWAIT)
3432 Flags |= WINEDDBLT_DONOTWAIT;
3434 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3438 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3441 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3442 /** Check against the maximum texture sizes supported by the video card **/
3443 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3444 unsigned int pow2Width, pow2Height;
3445 const GlPixelFormatDesc *glDesc;
3447 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3448 /* Setup some glformat defaults */
3449 This->glDescription.glFormat = glDesc->glFormat;
3450 This->glDescription.glFormatInternal = glDesc->glInternal;
3451 This->glDescription.glType = glDesc->glType;
3453 This->glDescription.textureName = 0;
3454 This->glDescription.target = GL_TEXTURE_2D;
3456 /* Non-power2 support */
3457 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3458 pow2Width = This->currentDesc.Width;
3459 pow2Height = This->currentDesc.Height;
3460 } else {
3461 /* Find the nearest pow2 match */
3462 pow2Width = pow2Height = 1;
3463 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3464 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3466 This->pow2Width = pow2Width;
3467 This->pow2Height = pow2Height;
3469 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3470 WINED3DFORMAT Format = This->resource.format;
3471 /** TODO: add support for non power two compressed textures **/
3472 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3473 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3474 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3475 This, This->currentDesc.Width, This->currentDesc.Height);
3476 return WINED3DERR_NOTAVAILABLE;
3480 if(pow2Width != This->currentDesc.Width ||
3481 pow2Height != This->currentDesc.Height) {
3482 This->Flags |= SFLAG_NONPOW2;
3485 TRACE("%p\n", This);
3486 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3487 /* one of three options
3488 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)
3489 2: Set the texture to the maximum size (bad idea)
3490 3: WARN and return WINED3DERR_NOTAVAILABLE;
3491 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.
3493 WARN("(%p) Creating an oversized surface\n", This);
3494 This->Flags |= SFLAG_OVERSIZE;
3496 /* This will be initialized on the first blt */
3497 This->glRect.left = 0;
3498 This->glRect.top = 0;
3499 This->glRect.right = 0;
3500 This->glRect.bottom = 0;
3501 } else {
3502 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one */
3503 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
3504 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3505 This->pow2Width = This->currentDesc.Width;
3506 This->pow2Height = This->currentDesc.Height;
3507 This->Flags &= ~SFLAG_NONPOW2;
3510 /* No oversize, gl rect is the full texture size */
3511 This->Flags &= ~SFLAG_OVERSIZE;
3512 This->glRect.left = 0;
3513 This->glRect.top = 0;
3514 This->glRect.right = This->pow2Width;
3515 This->glRect.bottom = This->pow2Height;
3518 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3519 switch(wined3d_settings.offscreen_rendering_mode) {
3520 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3521 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3522 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3526 This->Flags |= SFLAG_INSYSMEM;
3528 return WINED3D_OK;
3531 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3532 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3533 IWineD3DBaseTexture *texture;
3535 TRACE("(%p)->(%s, %s)\n", iface,
3536 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3537 persistent ? "TRUE" : "FALSE");
3539 /* TODO: For offscreen textures with fbo offscreen rendering the drawable is the same as the texture.*/
3540 if(persistent) {
3541 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3542 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3543 TRACE("Passing to container\n");
3544 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3545 IWineD3DBaseTexture_Release(texture);
3548 This->Flags &= ~SFLAG_LOCATIONS;
3549 This->Flags |= flag;
3550 } else {
3551 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3552 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3553 TRACE("Passing to container\n");
3554 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3555 IWineD3DBaseTexture_Release(texture);
3558 This->Flags &= ~flag;
3562 struct coords {
3563 int x, y, z;
3566 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3567 struct coords coords[4];
3568 RECT rect;
3569 IWineD3DSwapChain *swapchain = NULL;
3570 IWineD3DBaseTexture *texture = NULL;
3571 HRESULT hr;
3572 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3574 if(rect_in) {
3575 rect = *rect_in;
3576 } else {
3577 rect.left = 0;
3578 rect.top = 0;
3579 rect.right = This->currentDesc.Width;
3580 rect.bottom = This->currentDesc.Height;
3583 ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3584 ENTER_GL();
3586 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3587 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3588 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3589 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3590 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3591 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3592 checkGLcall("glTexParameteri");
3593 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3594 checkGLcall("glTexParameteri");
3596 coords[0].x = rect.left;
3597 coords[0].z = 0;
3599 coords[1].x = rect.left;
3600 coords[1].z = 0;
3602 coords[2].x = rect.right;
3603 coords[2].z = 0;
3605 coords[3].x = rect.right;
3606 coords[3].z = 0;
3608 coords[0].y = rect.top;
3609 coords[1].y = rect.bottom;
3610 coords[2].y = rect.bottom;
3611 coords[3].y = rect.top;
3612 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3613 glEnable(GL_TEXTURE_2D);
3614 checkGLcall("glEnable(GL_TEXTURE_2D)");
3615 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3616 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3617 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3618 checkGLcall("glTexParameteri");
3619 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3620 checkGLcall("glTexParameteri");
3622 coords[0].x = rect.left / This->pow2Width;
3623 coords[0].z = 0;
3625 coords[1].x = rect.left / This->pow2Width;
3626 coords[1].z = 0;
3628 coords[2].x = rect.right / This->pow2Width;
3629 coords[2].z = 0;
3631 coords[3].x = rect.right / This->pow2Width;
3632 coords[3].z = 0;
3634 coords[0].y = rect.top / This->pow2Height;
3635 coords[1].y = rect.bottom / This->pow2Height;
3636 coords[2].y = rect.bottom / This->pow2Height;
3637 coords[3].y = rect.top / This->pow2Height;
3638 } else {
3639 /* Must be a cube map */
3640 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3641 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3642 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3643 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3644 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3645 checkGLcall("glTexParameteri");
3646 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3647 checkGLcall("glTexParameteri");
3649 switch(This->glDescription.target) {
3650 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3651 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3652 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3653 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3654 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3655 break;
3657 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3658 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3659 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3660 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3661 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3662 break;
3664 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3665 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3666 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3667 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3668 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3669 break;
3671 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3672 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3673 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3674 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3675 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3676 break;
3678 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3679 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3680 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3681 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3682 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3683 break;
3685 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3686 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3687 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3688 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3689 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3691 default:
3692 ERR("Unexpected texture target\n");
3693 LEAVE_GL();
3694 return;
3698 glBegin(GL_QUADS);
3699 glTexCoord3iv((GLint *) &coords[0]);
3700 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3702 glTexCoord3iv((GLint *) &coords[1]);
3703 glVertex2i(0, device->render_offscreen ? rect.top : rect.bottom);
3705 glTexCoord3iv((GLint *) &coords[2]);
3706 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3708 glTexCoord3iv((GLint *) &coords[3]);
3709 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3710 glEnd();
3711 checkGLcall("glEnd");
3713 if(This->glDescription.target != GL_TEXTURE_2D) {
3714 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3715 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3716 } else {
3717 glDisable(GL_TEXTURE_2D);
3718 checkGLcall("glDisable(GL_TEXTURE_2D)");
3721 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3722 if(hr == WINED3D_OK && swapchain) {
3723 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3724 if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3725 glFlush();
3727 IWineD3DSwapChain_Release(swapchain);
3728 } else {
3729 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3730 * reset properly next draw
3732 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3733 if(hr == WINED3D_OK && texture) {
3734 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3735 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3736 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3737 IWineD3DBaseTexture_Release(texture);
3740 LEAVE_GL();
3743 /*****************************************************************************
3744 * IWineD3DSurface::LoadLocation
3746 * Copies the current surface data from wherever it is to the requested
3747 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3748 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3749 * multiple locations, the gl texture is preferred over the drawable, which is
3750 * preferred over system memory. The PBO counts as system memory. If rect is
3751 * not NULL, only the specified rectangle is copied (only supported for
3752 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3753 * location is marked up to date after the copy.
3755 * Parameters:
3756 * flag: Surface location flag to be updated
3757 * rect: rectangle to be copied
3759 * Returns:
3760 * WINED3D_OK on success
3761 * WINED3DERR_DEVICELOST on an internal error
3763 *****************************************************************************/
3764 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
3765 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3766 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3767 GLenum format, internal, type;
3768 CONVERT_TYPES convert;
3769 int bpp;
3770 int width, pitch, outpitch;
3771 BYTE *mem;
3773 TRACE("(%p)->(%s, %p)\n", iface,
3774 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3775 rect);
3776 if(rect) {
3777 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
3780 /* TODO: For fbo targets, texture == drawable */
3781 if(This->Flags & flag) {
3782 TRACE("Location already up to date\n");
3783 return WINED3D_OK;
3786 if(!(This->Flags & SFLAG_LOCATIONS)) {
3787 ERR("Surface does not have any up to date location\n");
3788 This->Flags |= SFLAG_LOST;
3789 return WINED3DERR_DEVICELOST;
3792 if(flag == SFLAG_INSYSMEM) {
3793 surface_prepare_system_memory(This);
3795 /* Download the surface to system memory */
3796 if(This->Flags & SFLAG_INTEXTURE) {
3797 surface_download_data(This);
3798 } else {
3799 read_from_framebuffer(This, rect,
3800 This->resource.allocatedMemory,
3801 IWineD3DSurface_GetPitch(iface));
3803 } else if(flag == SFLAG_INDRAWABLE) {
3804 if(This->Flags & SFLAG_INTEXTURE) {
3805 surface_blt_to_drawable(This, rect);
3806 } else {
3807 flush_to_framebuffer_drawpixels(This);
3809 } else /* if(flag == SFLAG_INTEXTURE) */ {
3810 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
3812 if (This->Flags & SFLAG_INDRAWABLE) {
3813 GLint prevRead;
3815 ENTER_GL();
3816 glGetIntegerv(GL_READ_BUFFER, &prevRead);
3817 vcheckGLcall("glGetIntegerv");
3818 glReadBuffer(This->resource.wineD3DDevice->offscreenBuffer);
3819 vcheckGLcall("glReadBuffer");
3821 if(!(This->Flags & SFLAG_ALLOCATED)) {
3822 surface_allocate_surface(This, internal, This->pow2Width,
3823 This->pow2Height, format, type);
3826 clear_unused_channels(This);
3828 glCopyTexSubImage2D(This->glDescription.target,
3829 This->glDescription.level,
3830 0, 0, 0, 0,
3831 This->currentDesc.Width,
3832 This->currentDesc.Height);
3833 checkGLcall("glCopyTexSubImage2D");
3835 glReadBuffer(prevRead);
3836 vcheckGLcall("glReadBuffer");
3838 LEAVE_GL();
3840 TRACE("Updated target %d\n", This->glDescription.target);
3841 } else {
3842 /* The only place where LoadTexture() might get called when isInDraw=1
3843 * is ActivateContext where lastActiveRenderTarget is preloaded.
3845 if(iface == device->lastActiveRenderTarget && device->isInDraw)
3846 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
3848 /* Otherwise: System memory copy must be most up to date */
3850 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
3851 This->Flags |= SFLAG_GLCKEY;
3852 This->glCKey = This->SrcBltCKey;
3854 else This->Flags &= ~SFLAG_GLCKEY;
3856 /* The width is in 'length' not in bytes */
3857 width = This->currentDesc.Width;
3858 pitch = IWineD3DSurface_GetPitch(iface);
3860 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
3861 int height = This->currentDesc.Height;
3863 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
3864 outpitch = width * bpp;
3865 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
3867 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
3868 if(!mem) {
3869 ERR("Out of memory %d, %d!\n", outpitch, height);
3870 return WINED3DERR_OUTOFVIDEOMEMORY;
3872 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
3874 This->Flags |= SFLAG_CONVERTED;
3875 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
3876 d3dfmt_p8_upload_palette(iface, convert);
3877 This->Flags &= ~SFLAG_CONVERTED;
3878 mem = This->resource.allocatedMemory;
3879 } else {
3880 This->Flags &= ~SFLAG_CONVERTED;
3881 mem = This->resource.allocatedMemory;
3884 /* Make sure the correct pitch is used */
3885 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
3887 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
3888 TRACE("non power of two support\n");
3889 if(!(This->Flags & SFLAG_ALLOCATED)) {
3890 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
3892 if (mem || (This->Flags & SFLAG_PBO)) {
3893 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
3895 } else {
3896 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
3897 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
3899 if(!(This->Flags & SFLAG_ALLOCATED)) {
3900 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
3902 if (mem || (This->Flags & SFLAG_PBO)) {
3903 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
3907 /* Restore the default pitch */
3908 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
3910 /* Don't delete PBO memory */
3911 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
3912 HeapFree(GetProcessHeap(), 0, mem);
3916 if(rect == NULL) {
3917 This->Flags |= flag;
3920 return WINED3D_OK;
3923 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
3924 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3925 IWineD3DSwapChain *swapchain = NULL;
3927 /* Update the drawable size method */
3928 if(container) {
3929 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
3931 if(swapchain) {
3932 This->get_drawable_size = get_drawable_size_swapchain;
3933 IWineD3DSwapChain_Release(swapchain);
3934 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3935 switch(wined3d_settings.offscreen_rendering_mode) {
3936 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3937 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3938 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3942 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
3945 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
3947 /* IUnknown */
3948 IWineD3DBaseSurfaceImpl_QueryInterface,
3949 IWineD3DBaseSurfaceImpl_AddRef,
3950 IWineD3DSurfaceImpl_Release,
3951 /* IWineD3DResource */
3952 IWineD3DBaseSurfaceImpl_GetParent,
3953 IWineD3DBaseSurfaceImpl_GetDevice,
3954 IWineD3DBaseSurfaceImpl_SetPrivateData,
3955 IWineD3DBaseSurfaceImpl_GetPrivateData,
3956 IWineD3DBaseSurfaceImpl_FreePrivateData,
3957 IWineD3DBaseSurfaceImpl_SetPriority,
3958 IWineD3DBaseSurfaceImpl_GetPriority,
3959 IWineD3DSurfaceImpl_PreLoad,
3960 IWineD3DBaseSurfaceImpl_GetType,
3961 /* IWineD3DSurface */
3962 IWineD3DBaseSurfaceImpl_GetContainer,
3963 IWineD3DBaseSurfaceImpl_GetDesc,
3964 IWineD3DSurfaceImpl_LockRect,
3965 IWineD3DSurfaceImpl_UnlockRect,
3966 IWineD3DSurfaceImpl_GetDC,
3967 IWineD3DSurfaceImpl_ReleaseDC,
3968 IWineD3DSurfaceImpl_Flip,
3969 IWineD3DSurfaceImpl_Blt,
3970 IWineD3DBaseSurfaceImpl_GetBltStatus,
3971 IWineD3DBaseSurfaceImpl_GetFlipStatus,
3972 IWineD3DBaseSurfaceImpl_IsLost,
3973 IWineD3DBaseSurfaceImpl_Restore,
3974 IWineD3DSurfaceImpl_BltFast,
3975 IWineD3DBaseSurfaceImpl_GetPalette,
3976 IWineD3DBaseSurfaceImpl_SetPalette,
3977 IWineD3DBaseSurfaceImpl_RealizePalette,
3978 IWineD3DBaseSurfaceImpl_SetColorKey,
3979 IWineD3DBaseSurfaceImpl_GetPitch,
3980 IWineD3DSurfaceImpl_SetMem,
3981 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
3982 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
3983 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
3984 IWineD3DBaseSurfaceImpl_UpdateOverlay,
3985 IWineD3DBaseSurfaceImpl_SetClipper,
3986 IWineD3DBaseSurfaceImpl_GetClipper,
3987 /* Internal use: */
3988 IWineD3DSurfaceImpl_AddDirtyRect,
3989 IWineD3DSurfaceImpl_LoadTexture,
3990 IWineD3DSurfaceImpl_BindTexture,
3991 IWineD3DSurfaceImpl_SaveSnapshot,
3992 IWineD3DSurfaceImpl_SetContainer,
3993 IWineD3DSurfaceImpl_SetGlTextureDesc,
3994 IWineD3DSurfaceImpl_GetGlDesc,
3995 IWineD3DSurfaceImpl_GetData,
3996 IWineD3DSurfaceImpl_SetFormat,
3997 IWineD3DSurfaceImpl_PrivateSetup,
3998 IWineD3DSurfaceImpl_ModifyLocation,
3999 IWineD3DSurfaceImpl_LoadLocation