Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / wined3d / surface.c
blobbc63496a444c84a95a862b09b244d951fe68ecf4
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 Stefan Dösinger for CodeWeavers
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "config.h"
28 #include "wine/port.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
32 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
34 typedef enum {
35 NO_CONVERSION,
36 CONVERT_PALETTED,
37 CONVERT_PALETTED_CK,
38 CONVERT_CK_565,
39 CONVERT_CK_5551,
40 CONVERT_CK_4444,
41 CONVERT_CK_4444_ARGB,
42 CONVERT_CK_1555,
43 CONVERT_555,
44 CONVERT_CK_RGB24,
45 CONVERT_CK_8888,
46 CONVERT_CK_8888_ARGB,
47 CONVERT_RGB32_888,
48 CONVERT_V8U8,
49 CONVERT_X8L8V8U8,
50 CONVERT_Q8W8V8U8,
51 CONVERT_V16U16
52 } CONVERT_TYPES;
54 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
56 static void surface_download_data(IWineD3DSurfaceImpl *This) {
57 if (This->resource.format == WINED3DFMT_DXT1 ||
58 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
59 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
60 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
61 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
62 } else {
63 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
64 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
66 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
67 checkGLcall("glGetCompressedTexImageARB()");
69 } else {
70 void *mem;
71 int src_pitch = 0;
72 int dst_pitch = 0;
74 if(This->Flags & SFLAG_CONVERTED) {
75 FIXME("Read back converted textures unsupported\n");
76 return;
79 if (This->Flags & SFLAG_NONPOW2) {
80 src_pitch = This->bytesPerPixel * This->pow2Width;
81 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
82 src_pitch = (src_pitch + SURFACE_ALIGNMENT - 1) & ~(SURFACE_ALIGNMENT - 1);
83 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
84 } else {
85 mem = This->resource.allocatedMemory;
88 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
89 This->glDescription.glFormat, This->glDescription.glType, mem);
91 glGetTexImage(This->glDescription.target, This->glDescription.level, This->glDescription.glFormat,
92 This->glDescription.glType, mem);
93 checkGLcall("glGetTexImage()");
95 if (This->Flags & SFLAG_NONPOW2) {
96 LPBYTE src_data, dst_data;
97 int y;
99 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
100 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
101 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
103 * We're doing this...
105 * instead of boxing the texture :
106 * |<-texture width ->| -->pow2width| /\
107 * |111111111111111111| | |
108 * |222 Texture 222222| boxed empty | texture height
109 * |3333 Data 33333333| | |
110 * |444444444444444444| | \/
111 * ----------------------------------- |
112 * | boxed empty | boxed empty | pow2height
113 * | | | \/
114 * -----------------------------------
117 * we're repacking the data to the expected texture width
119 * |<-texture width ->| -->pow2width| /\
120 * |111111111111111111222222222222222| |
121 * |222333333333333333333444444444444| texture height
122 * |444444 | |
123 * | | \/
124 * | | |
125 * | empty | pow2height
126 * | | \/
127 * -----------------------------------
129 * == is the same as
131 * |<-texture width ->| /\
132 * |111111111111111111|
133 * |222222222222222222|texture height
134 * |333333333333333333|
135 * |444444444444444444| \/
136 * --------------------
138 * this also means that any references to allocatedMemory should work with the data as if were a
139 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
141 * internally the texture is still stored in a boxed format so any references to textureName will
142 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
144 * Performance should not be an issue, because applications normally do not lock the surfaces when
145 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
146 * and doesn't have to be re-read.
148 src_data = mem;
149 dst_data = This->resource.allocatedMemory;
150 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
151 for (y = 1 ; y < This->currentDesc.Height; y++) {
152 /* skip the first row */
153 src_data += src_pitch;
154 dst_data += dst_pitch;
155 memcpy(dst_data, src_data, dst_pitch);
158 HeapFree(GetProcessHeap(), 0, mem);
163 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
164 if (This->resource.format == WINED3DFMT_DXT1 ||
165 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
166 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
167 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
168 FIXME("Using DXT1/3/5 without advertized support\n");
169 } else {
170 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
171 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
172 This->Flags |= SFLAG_CLIENT;
175 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
176 ENTER_GL();
177 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
178 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
179 * function uses glCompressedTexImage2D instead of the SubImage call
181 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, This->glDescription.glFormatInternal,
182 width, height, 0 /* border */, This->resource.size, data));
183 checkGLcall("glCompressedTexSubImage2D");
184 LEAVE_GL();
186 } else {
187 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
188 ENTER_GL();
189 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
190 checkGLcall("glTexSubImage2D");
191 LEAVE_GL();
195 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
196 BOOL enable_client_storage = FALSE;
198 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,
199 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
201 if (This->resource.format == WINED3DFMT_DXT1 ||
202 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
203 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
204 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
205 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
206 return;
209 ENTER_GL();
211 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
212 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
213 /* In some cases we want to disable client storage.
214 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
215 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
216 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
217 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
218 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
220 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
221 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
222 This->Flags &= SFLAG_CLIENT;
223 enable_client_storage = TRUE;
224 } else {
225 This->Flags |= SFLAG_CLIENT;
226 /* Below point opengl to our allocated texture memory */
229 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type,
230 This->Flags & SFLAG_CLIENT ? This->resource.allocatedMemory : NULL);
231 checkGLcall("glTexImage2D");
233 if(enable_client_storage) {
234 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
235 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
237 LEAVE_GL();
240 /* *******************************************
241 IWineD3DSurface IUnknown parts follow
242 ******************************************* */
243 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
245 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
246 /* Warn ,but be nice about things */
247 TRACE("(%p)->(%s,%p)\n", This,debugstr_guid(riid),ppobj);
249 if (IsEqualGUID(riid, &IID_IUnknown)
250 || IsEqualGUID(riid, &IID_IWineD3DBase)
251 || IsEqualGUID(riid, &IID_IWineD3DResource)
252 || IsEqualGUID(riid, &IID_IWineD3DSurface)) {
253 IUnknown_AddRef((IUnknown*)iface);
254 *ppobj = This;
255 return S_OK;
257 *ppobj = NULL;
258 return E_NOINTERFACE;
261 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface) {
262 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
263 ULONG ref = InterlockedIncrement(&This->resource.ref);
264 TRACE("(%p) : AddRef increasing from %d\n", This,ref - 1);
265 return ref;
268 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
269 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
270 ULONG ref = InterlockedDecrement(&This->resource.ref);
271 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
272 if (ref == 0) {
273 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) This->resource.wineD3DDevice;
274 TRACE("(%p) : cleaning up\n", This);
276 if(iface == device->lastActiveRenderTarget) {
277 IWineD3DSwapChainImpl *swapchain = device->swapchains ? (IWineD3DSwapChainImpl *) device->swapchains[0] : NULL;
279 TRACE("Last active render target destroyed\n");
280 /* Find a replacement surface for the currently active back buffer. The context manager does not do NULL
281 * checks, so switch to a valid target as long as the currently set surface is still valid. Use the
282 * surface of the implicit swpchain. If that is the same as the destroyed surface the device is destroyed
283 * and the lastActiveRenderTarget member shouldn't matter
285 if(swapchain) {
286 ENTER_GL(); /* For ActivateContext */
287 if(swapchain->backBuffer && swapchain->backBuffer[0] != iface) {
288 TRACE("Activating primary back buffer\n");
289 ActivateContext(device, swapchain->backBuffer[0], CTXUSAGE_RESOURCELOAD);
290 } else if(!swapchain->backBuffer && swapchain->frontBuffer != iface) {
291 /* Single buffering environment */
292 TRACE("Activating primary front buffer\n");
293 ActivateContext(device, swapchain->frontBuffer, CTXUSAGE_RESOURCELOAD);
294 } else {
295 TRACE("Device is being destroyed, setting lastActiveRenderTarget = 0xdeadbabe\n");
296 /* Implicit render target destroyed, that means the device is being destroyed
297 * whatever we set here, it shouldn't matter
299 device->lastActiveRenderTarget = (IWineD3DSurface *) 0xdeadbabe;
301 LEAVE_GL();
302 } else {
303 /* May happen during ddraw uninitialization */
304 TRACE("Render target set, but swapchain does not exist!\n");
305 device->lastActiveRenderTarget = (IWineD3DSurface *) 0xdeadcafe;
309 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
310 ENTER_GL();
312 /* Need a context to destroy the texture. Use the currently active render target, but only if
313 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
314 * When destroying the primary rt, Uninit3D will activate a context before doing anything
316 if(device->render_targets[0]) {
317 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
320 TRACE("Deleting texture %d\n", This->glDescription.textureName);
321 glDeleteTextures(1, &This->glDescription.textureName);
322 LEAVE_GL();
325 if(This->Flags & SFLAG_DIBSECTION) {
326 /* Release the DC */
327 SelectObject(This->hDC, This->dib.holdbitmap);
328 DeleteDC(This->hDC);
329 /* Release the DIB section */
330 DeleteObject(This->dib.DIBsection);
331 This->dib.bitmap_data = NULL;
332 This->resource.allocatedMemory = NULL;
334 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
336 HeapFree(GetProcessHeap(), 0, This->palette9);
338 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
339 if(iface == device->ddraw_primary)
340 device->ddraw_primary = NULL;
342 TRACE("(%p) Released\n", This);
343 HeapFree(GetProcessHeap(), 0, This);
346 return ref;
349 /* ****************************************************
350 IWineD3DSurface IWineD3DResource parts follow
351 **************************************************** */
352 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
353 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
356 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
357 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
360 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
361 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
364 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
365 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
368 DWORD WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
369 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
372 DWORD WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
373 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
376 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
377 /* TODO: check for locks */
378 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
379 IWineD3DBaseTexture *baseTexture = NULL;
380 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
382 TRACE("(%p)Checking to see if the container is a base texture\n", This);
383 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
384 TRACE("Passing to conatiner\n");
385 IWineD3DBaseTexture_PreLoad(baseTexture);
386 IWineD3DBaseTexture_Release(baseTexture);
387 } else {
388 TRACE("(%p) : About to load surface\n", This);
390 ENTER_GL();
391 if(!device->isInDraw) {
392 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
395 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
396 if (!This->glDescription.level) {
397 if (!This->glDescription.textureName) {
398 glGenTextures(1, &This->glDescription.textureName);
399 checkGLcall("glGenTextures");
400 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
402 glBindTexture(This->glDescription.target, This->glDescription.textureName);
403 checkGLcall("glBindTexture");
404 IWineD3DSurface_LoadTexture(iface);
405 /* This is where we should be reducing the amount of GLMemoryUsed */
406 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
407 /* assume this is a coding error not a real error for now */
408 FIXME("Mipmap surface has a glTexture bound to it!\n");
410 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
411 /* Tell opengl to try and keep this texture in video ram (well mostly) */
412 GLclampf tmp;
413 tmp = 0.9f;
414 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
416 LEAVE_GL();
418 return;
421 WINED3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface) {
422 TRACE("(%p) : calling resourceimpl_GetType\n", iface);
423 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
426 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
427 TRACE("(%p) : calling resourceimpl_GetParent\n", iface);
428 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
431 /* ******************************************************
432 IWineD3DSurface IWineD3DSurface parts follow
433 ****************************************************** */
435 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface* iface, REFIID riid, void** ppContainer) {
436 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
437 IWineD3DBase *container = 0;
439 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
441 if (!ppContainer) {
442 ERR("Called without a valid ppContainer.\n");
445 /** From MSDN:
446 * If the surface is created using CreateImageSurface/CreateOffscreenPlainSurface, CreateRenderTarget,
447 * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
448 * GetContainer will return the Direct3D device used to create the surface.
450 if (This->container) {
451 container = This->container;
452 } else {
453 container = (IWineD3DBase *)This->resource.wineD3DDevice;
456 TRACE("Relaying to QueryInterface\n");
457 return IUnknown_QueryInterface(container, riid, ppContainer);
460 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
461 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
463 TRACE("(%p) : copying into %p\n", This, pDesc);
464 if(pDesc->Format != NULL) *(pDesc->Format) = This->resource.format;
465 if(pDesc->Type != NULL) *(pDesc->Type) = This->resource.resourceType;
466 if(pDesc->Usage != NULL) *(pDesc->Usage) = This->resource.usage;
467 if(pDesc->Pool != NULL) *(pDesc->Pool) = This->resource.pool;
468 if(pDesc->Size != NULL) *(pDesc->Size) = This->resource.size; /* dx8 only */
469 if(pDesc->MultiSampleType != NULL) *(pDesc->MultiSampleType) = This->currentDesc.MultiSampleType;
470 if(pDesc->MultiSampleQuality != NULL) *(pDesc->MultiSampleQuality) = This->currentDesc.MultiSampleQuality;
471 if(pDesc->Width != NULL) *(pDesc->Width) = This->currentDesc.Width;
472 if(pDesc->Height != NULL) *(pDesc->Height) = This->currentDesc.Height;
473 return WINED3D_OK;
476 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
477 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
478 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
479 if (This->glDescription.textureName == 0 && textureName != 0) {
480 This->Flags &= ~SFLAG_INTEXTURE;
481 IWineD3DSurface_AddDirtyRect(iface, NULL);
483 This->glDescription.textureName = textureName;
484 This->glDescription.target = target;
487 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
488 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
489 TRACE("(%p) : returning %p\n", This, &This->glDescription);
490 *glDescription = &This->glDescription;
493 /* TODO: think about moving this down to resource? */
494 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
495 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
496 /* 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 futture */
497 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
498 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
500 return (CONST void*)(This->resource.allocatedMemory);
503 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch, BOOL srcUpsideDown) {
504 BYTE *mem;
505 GLint fmt;
506 GLint type;
507 BYTE *row, *top, *bottom;
508 int i;
509 BOOL bpp;
511 switch(This->resource.format)
513 case WINED3DFMT_P8:
515 /* GL can't return palettized data, so read ARGB pixels into a
516 * separate block of memory and convert them into palettized format
517 * in software. Slow, but if the app means to use palettized render
518 * targets and locks it...
520 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
521 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
522 * for the color channels when palettizing the colors.
524 fmt = GL_RGB;
525 type = GL_UNSIGNED_BYTE;
526 pitch *= 3;
527 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
528 if(!mem) {
529 ERR("Out of memory\n");
530 return;
532 bpp = This->bytesPerPixel * 3;
534 break;
536 default:
537 mem = dest;
538 fmt = This->glDescription.glFormat;
539 type = This->glDescription.glType;
540 bpp = This->bytesPerPixel;
543 glReadPixels(rect->left, rect->top,
544 rect->right - rect->left,
545 rect->bottom - rect->top,
546 fmt, type, mem);
547 vcheckGLcall("glReadPixels");
549 /* TODO: Merge this with the palettization loop below for P8 targets */
551 if(!srcUpsideDown) {
552 UINT len, off;
553 /* glReadPixels returns the image upside down, and there is no way to prevent this.
554 Flip the lines in software */
555 len = (rect->right - rect->left) * bpp;
556 off = rect->left * bpp;
558 row = HeapAlloc(GetProcessHeap(), 0, len);
559 if(!row) {
560 ERR("Out of memory\n");
561 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
562 return;
565 top = mem + pitch * rect->top;
566 bottom = ((BYTE *) mem) + pitch * ( rect->bottom - rect->top - 1);
567 for(i = 0; i < (rect->bottom - rect->top) / 2; i++) {
568 memcpy(row, top + off, len);
569 memcpy(top + off, bottom + off, len);
570 memcpy(bottom + off, row, len);
571 top += pitch;
572 bottom -= pitch;
574 HeapFree(GetProcessHeap(), 0, row);
577 if(This->resource.format == WINED3DFMT_P8) {
578 PALETTEENTRY *pal;
579 DWORD width = pitch / 3;
580 int x, y, c;
581 if(This->palette) {
582 pal = This->palette->palents;
583 } else {
584 pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
587 for(y = rect->top; y < rect->bottom; y++) {
588 for(x = rect->left; x < rect->right; x++) {
589 /* start lines pixels */
590 BYTE *blue = (BYTE *) ((BYTE *) mem) + y * pitch + x * (sizeof(BYTE) * 3);
591 BYTE *green = blue + 1;
592 BYTE *red = green + 1;
594 for(c = 0; c < 256; c++) {
595 if(*red == pal[c].peRed &&
596 *green == pal[c].peGreen &&
597 *blue == pal[c].peBlue)
599 *((BYTE *) dest + y * width + x) = c;
600 break;
605 HeapFree(GetProcessHeap(), 0, mem);
609 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
610 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
611 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
612 IWineD3DSwapChainImpl *swapchain = NULL;
614 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
616 if (!(This->Flags & SFLAG_LOCKABLE)) {
617 /* Note: UpdateTextures calls CopyRects which calls this routine to populate the
618 texture regions, and since the destination is an unlockable region we need
619 to tolerate this */
620 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
621 /*return WINED3DERR_INVALIDCALL; */
624 pLockedRect->Pitch = IWineD3DSurface_GetPitch(iface);
626 /* Mark the surface locked */
627 This->Flags |= SFLAG_LOCKED;
629 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy */
630 if(!This->resource.allocatedMemory) {
631 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + 4);
632 This->Flags &= ~SFLAG_INSYSMEM; /* This is the marker that surface data has to be downloaded */
635 /* Calculate the correct start address to report */
636 if (NULL == pRect) {
637 pLockedRect->pBits = This->resource.allocatedMemory;
638 This->lockedRect.left = 0;
639 This->lockedRect.top = 0;
640 This->lockedRect.right = This->currentDesc.Width;
641 This->lockedRect.bottom = This->currentDesc.Height;
642 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n", &This->lockedRect, This->lockedRect.left, This->lockedRect.top, This->lockedRect.right, This->lockedRect.bottom);
643 } else {
644 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
646 if ((pRect->top < 0) ||
647 (pRect->left < 0) ||
648 (pRect->left >= pRect->right) ||
649 (pRect->top >= pRect->bottom) ||
650 (pRect->right > This->currentDesc.Width) ||
651 (pRect->bottom > This->currentDesc.Height))
653 WARN(" Invalid values in pRect !!!\n");
654 return WINED3DERR_INVALIDCALL;
657 /* DXTn textures are based on compressed blocks of 4x4 pixels, each
658 * 16 bytes large (8 bytes in case of DXT1). Because of that Pitch has
659 * slightly different meaning compared to regular textures. For DXTn
660 * textures Pitch is the size of a row of blocks, 4 high and "width"
661 * long. The x offset is calculated differently as well, since moving 4
662 * pixels to the right actually moves an entire 4x4 block to right, ie
663 * 16 bytes (8 in case of DXT1). */
664 if (This->resource.format == WINED3DFMT_DXT1) {
665 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 2);
666 } else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3
667 || This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
668 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top / 4) + (pRect->left * 4);
669 } else {
670 pLockedRect->pBits = This->resource.allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
672 This->lockedRect.left = pRect->left;
673 This->lockedRect.top = pRect->top;
674 This->lockedRect.right = pRect->right;
675 This->lockedRect.bottom = pRect->bottom;
678 if (This->Flags & SFLAG_NONPOW2) {
679 TRACE("Locking non-power 2 texture\n");
682 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
683 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
684 * changed
686 if(!(This->Flags & SFLAG_DYNLOCK)) {
687 This->lockCount++;
688 /* MAXLOCKCOUNT is defined in wined3d_private.h */
689 if(This->lockCount > MAXLOCKCOUNT) {
690 TRACE("Surface is locked regularily, not freeing the system memory copy any more\n");
691 This->Flags |= SFLAG_DYNLOCK;
695 if((Flags & WINED3DLOCK_DISCARD) || (This->Flags & SFLAG_INSYSMEM)) {
696 TRACE("WINED3DLOCK_DISCARD flag passed, or local copy is up to date, not downloading data\n");
697 goto lock_end;
700 /* Now download the surface content from opengl
701 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
702 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
704 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
705 if(swapchain || iface == myDevice->render_targets[0]) {
706 BOOL srcIsUpsideDown;
708 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
709 static BOOL warned = FALSE;
710 if(!warned) {
711 ERR("The application tries to lock the render target, but render target locking is disabled\n");
712 warned = TRUE;
714 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
715 return WINED3D_OK;
718 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
719 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
720 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
721 * context->last_was_blit set on the unlock.
723 ENTER_GL();
724 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
726 /* Select the correct read buffer, and give some debug output.
727 * There is no need to keep track of the current read buffer or reset it, every part of the code
728 * that reads sets the read buffer as desired.
730 if(!swapchain) {
731 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
732 * Read from the back buffer
734 TRACE("Locking offscreen render target\n");
735 glReadBuffer(myDevice->offscreenBuffer);
736 srcIsUpsideDown = TRUE;
737 } else {
738 if(iface == swapchain->frontBuffer) {
739 TRACE("Locking the front buffer\n");
740 glReadBuffer(GL_FRONT);
741 } else if(swapchain->backBuffer && iface == swapchain->backBuffer[0]) {
742 TRACE("Locking the back buffer\n");
743 glReadBuffer(GL_BACK);
744 } else {
745 /* Ok, there is an issue: OpenGL does not guarant any back buffer number, so all we can do is to read GL_BACK
746 * and hope it gives what the app wants
748 FIXME("Application is locking a 2nd or higher back buffer\n");
749 glReadBuffer(GL_BACK);
751 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
752 srcIsUpsideDown = FALSE;
755 switch(wined3d_settings.rendertargetlock_mode) {
756 case RTL_AUTO:
757 case RTL_READDRAW:
758 case RTL_READTEX:
759 read_from_framebuffer(This, &This->lockedRect, This->resource.allocatedMemory, pLockedRect->Pitch, srcIsUpsideDown);
760 break;
762 case RTL_TEXDRAW:
763 case RTL_TEXTEX:
764 read_from_framebuffer(This, &This->lockedRect, This->resource.allocatedMemory, pLockedRect->Pitch, srcIsUpsideDown);
765 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
766 break;
768 LEAVE_GL();
770 /* Mark the local copy up to date if a full download was done */
771 if(This->lockedRect.left == 0 &&
772 This->lockedRect.top == 0 &&
773 This->lockedRect.right == This->currentDesc.Width &&
774 This->lockedRect.bottom == This->currentDesc.Height) {
775 This->Flags |= SFLAG_INSYSMEM;
777 } else if(iface == myDevice->stencilBufferTarget) {
778 /** the depth stencil in openGL has a format of GL_FLOAT
779 * which should be good for WINED3DFMT_D16_LOCKABLE
780 * and WINED3DFMT_D16
781 * it is unclear what format the stencil buffer is in except.
782 * 'Each index is converted to fixed point...
783 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
784 * mappings in the table GL_PIXEL_MAP_S_TO_S.
785 * glReadPixels(This->lockedRect.left,
786 * This->lockedRect.bottom - j - 1,
787 * This->lockedRect.right - This->lockedRect.left,
788 * 1,
789 * GL_DEPTH_COMPONENT,
790 * type,
791 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
793 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
794 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
795 * none of that is the case the problem is not in this function :-)
796 ********************************************/
797 FIXME("Depth stencil locking not supported yet\n");
798 } else {
799 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
800 TRACE("locking an ordinarary surface\n");
802 if (0 != This->glDescription.textureName) {
803 /* Now I have to copy thing bits back */
805 ENTER_GL();
807 if(myDevice->createParms.BehaviorFlags & WINED3DCREATE_MULTITHREADED) {
808 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
811 /* Make sure that a proper texture unit is selected, bind the texture and dirtify the sampler to restore the texture on the next draw */
812 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
813 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
814 checkGLcall("glActiveTextureARB");
816 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(0));
817 IWineD3DSurface_PreLoad(iface);
819 surface_download_data(This);
820 LEAVE_GL();
823 /* The local copy is now up to date to the opengl one because a full download was done */
824 This->Flags |= SFLAG_INSYSMEM;
827 lock_end:
828 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
829 /* Don't dirtify */
830 } else {
831 IWineD3DBaseTexture *pBaseTexture;
833 * Dirtify on lock
834 * as seen in msdn docs
836 IWineD3DSurface_AddDirtyRect(iface, &This->lockedRect);
838 /** Dirtify Container if needed */
839 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
840 TRACE("Making container dirty\n");
841 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
842 IWineD3DBaseTexture_Release(pBaseTexture);
843 } else {
844 TRACE("Surface is standalone, no need to dirty the container\n");
848 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch,
849 This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
850 return WINED3D_OK;
853 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This) {
854 GLint prev_store;
855 GLint prev_rasterpos[4];
856 GLint skipBytes = 0;
857 BOOL storechanged = FALSE, memory_allocated = FALSE;
858 GLint fmt, type;
859 BYTE *mem;
860 UINT bpp;
861 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
863 glDisable(GL_TEXTURE_2D);
864 vcheckGLcall("glDisable(GL_TEXTURE_2D)");
866 glFlush();
867 vcheckGLcall("glFlush");
868 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
869 vcheckGLcall("glIntegerv");
870 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
871 vcheckGLcall("glIntegerv");
872 glPixelZoom(1.0, -1.0);
873 vcheckGLcall("glPixelZoom");
875 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
876 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
877 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
879 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
880 vcheckGLcall("glRasterPos2f");
882 /* Some drivers(radeon dri, others?) don't like exceptions during
883 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
884 * after ReleaseDC. Reading it will cause an exception, which x11drv will
885 * catch to put the dib section in InSync mode, which leads to a crash
886 * and a blocked x server on my radeon card.
888 * The following lines read the dib section so it is put in inSync mode
889 * before glDrawPixels is called and the crash is prevented. There won't
890 * be any interfering gdi accesses, because UnlockRect is called from
891 * ReleaseDC, and the app won't use the dc any more afterwards.
893 if(This->Flags & SFLAG_DIBSECTION) {
894 volatile BYTE read;
895 read = This->resource.allocatedMemory[0];
898 switch (This->resource.format) {
899 /* No special care needed */
900 case WINED3DFMT_A4R4G4B4:
901 case WINED3DFMT_R5G6B5:
902 case WINED3DFMT_A1R5G5B5:
903 case WINED3DFMT_R8G8B8:
904 type = This->glDescription.glType;
905 fmt = This->glDescription.glFormat;
906 mem = This->resource.allocatedMemory;
907 bpp = This->bytesPerPixel;
908 break;
910 case WINED3DFMT_X4R4G4B4:
912 int size;
913 unsigned short *data;
914 data = (unsigned short *)This->resource.allocatedMemory;
915 size = (This->lockedRect.bottom - This->lockedRect.top) * (This->lockedRect.right - This->lockedRect.left);
916 while(size > 0) {
917 *data |= 0xF000;
918 data++;
919 size--;
921 type = This->glDescription.glType;
922 fmt = This->glDescription.glFormat;
923 mem = This->resource.allocatedMemory;
924 bpp = This->bytesPerPixel;
926 break;
928 case WINED3DFMT_X1R5G5B5:
930 int size;
931 unsigned short *data;
932 data = (unsigned short *)This->resource.allocatedMemory;
933 size = (This->lockedRect.bottom - This->lockedRect.top) * (This->lockedRect.right - This->lockedRect.left);
934 while(size > 0) {
935 *data |= 0x8000;
936 data++;
937 size--;
939 type = This->glDescription.glType;
940 fmt = This->glDescription.glFormat;
941 mem = This->resource.allocatedMemory;
942 bpp = This->bytesPerPixel;
944 break;
946 case WINED3DFMT_X8R8G8B8:
948 /* make sure the X byte is set to alpha on, since it
949 could be any random value. This fixes the intro movie in Pirates! */
950 int size;
951 unsigned int *data;
952 data = (unsigned int *)This->resource.allocatedMemory;
953 size = (This->lockedRect.bottom - This->lockedRect.top) * (This->lockedRect.right - This->lockedRect.left);
954 while(size > 0) {
955 *data |= 0xFF000000;
956 data++;
957 size--;
960 /* Fall through */
962 case WINED3DFMT_A8R8G8B8:
964 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
965 vcheckGLcall("glPixelStorei");
966 storechanged = TRUE;
967 type = This->glDescription.glType;
968 fmt = This->glDescription.glFormat;
969 mem = This->resource.allocatedMemory;
970 bpp = This->bytesPerPixel;
972 break;
974 case WINED3DFMT_A2R10G10B10:
976 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
977 vcheckGLcall("glPixelStorei");
978 storechanged = TRUE;
979 type = This->glDescription.glType;
980 fmt = This->glDescription.glFormat;
981 mem = This->resource.allocatedMemory;
982 bpp = This->bytesPerPixel;
984 break;
986 case WINED3DFMT_P8:
988 int height = This->glRect.bottom - This->glRect.top;
989 type = GL_UNSIGNED_BYTE;
990 fmt = GL_RGBA;
992 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * sizeof(DWORD));
993 if(!mem) {
994 ERR("Out of memory\n");
995 return;
997 memory_allocated = TRUE;
998 d3dfmt_convert_surface(This->resource.allocatedMemory,
999 mem,
1000 pitch,
1001 pitch,
1002 height,
1003 pitch * 4,
1004 CONVERT_PALETTED,
1005 This);
1006 bpp = This->bytesPerPixel * 4;
1007 pitch *= 4;
1009 break;
1011 default:
1012 FIXME("Unsupported Format %u in locking func\n", This->resource.format);
1014 /* Give it a try */
1015 type = This->glDescription.glType;
1016 fmt = This->glDescription.glFormat;
1017 mem = This->resource.allocatedMemory;
1018 bpp = This->bytesPerPixel;
1021 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1022 (This->lockedRect.bottom - This->lockedRect.top)-1,
1023 fmt, type,
1024 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1025 checkGLcall("glDrawPixels");
1026 glPixelZoom(1.0,1.0);
1027 vcheckGLcall("glPixelZoom");
1029 glRasterPos3iv(&prev_rasterpos[0]);
1030 vcheckGLcall("glRasterPos3iv");
1032 /* Reset to previous pack row length */
1033 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1034 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1035 if(storechanged) {
1036 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
1037 vcheckGLcall("glPixelStorei GL_PACK_SWAP_BYTES");
1040 /* Blitting environment requires that 2D texturing is enabled. It was turned off before,
1041 * turn it on again
1043 glEnable(GL_TEXTURE_2D);
1044 checkGLcall("glEnable(GL_TEXTURE_2D)");
1046 if(memory_allocated) HeapFree(GetProcessHeap(), 0, mem);
1047 return;
1050 static void flush_to_framebuffer_texture(IWineD3DSurfaceImpl *This) {
1051 float glTexCoord[4];
1053 glTexCoord[0] = (float) This->lockedRect.left / (float) This->pow2Width; /* left */
1054 glTexCoord[1] = (float) This->lockedRect.right / (float) This->pow2Width; /* right */
1055 glTexCoord[2] = (float) This->lockedRect.top / (float) This->pow2Height; /* top */
1056 glTexCoord[3] = (float) This->lockedRect.bottom / (float) This->pow2Height; /* bottom */
1058 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
1060 ENTER_GL();
1062 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
1063 checkGLcall("glEnable glBindTexture");
1065 /* No filtering for blts */
1066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1067 checkGLcall("glTexParameteri");
1068 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1069 checkGLcall("glTexParameteri");
1071 /* Start drawing a quad */
1072 glBegin(GL_QUADS);
1074 glColor3d(1.0f, 1.0f, 1.0f);
1075 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
1076 glVertex3f(This->lockedRect.left, This->lockedRect.top, 0.0);
1078 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
1079 glVertex3f(This->lockedRect.left, This->lockedRect.bottom, 0.0);
1081 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
1082 glVertex3d(This->lockedRect.right, This->lockedRect.bottom, 0.0);
1084 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
1085 glVertex3f(This->lockedRect.right, This->lockedRect.top, 0.0);
1087 glEnd();
1088 checkGLcall("glEnd");
1090 /* Unbind the texture */
1091 glBindTexture(GL_TEXTURE_2D, 0);
1092 checkGLcall("glEnable glBindTexture");
1094 LEAVE_GL();
1097 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1098 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1099 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1100 IWineD3DSwapChainImpl *swapchain = NULL;
1102 if (!(This->Flags & SFLAG_LOCKED)) {
1103 WARN("trying to Unlock an unlocked surf@%p\n", This);
1104 return WINED3DERR_INVALIDCALL;
1107 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1109 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1110 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1111 goto unlock_end;
1114 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1115 if(swapchain || iface == myDevice->render_targets[0]) {
1116 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1117 static BOOL warned = FALSE;
1118 if(!warned) {
1119 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1120 warned = TRUE;
1122 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1123 goto unlock_end;
1126 /* Activate the correct context for the render target */
1127 ENTER_GL();
1128 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1130 if(!swapchain) {
1131 /* Primary offscreen render target */
1132 TRACE("Offscreen render target\n");
1133 glDrawBuffer(myDevice->offscreenBuffer);
1134 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1135 } else {
1136 if(iface == swapchain->frontBuffer) {
1137 TRACE("Onscreen front buffer\n");
1138 glDrawBuffer(GL_FRONT);
1139 checkGLcall("glDrawBuffer(GL_FRONT)");
1140 } else if(iface == swapchain->backBuffer[0]) {
1141 TRACE("Onscreen back buffer\n");
1142 glDrawBuffer(GL_BACK);
1143 checkGLcall("glDrawBuffer(GL_BACK)");
1144 } else {
1145 FIXME("Unlocking a higher back buffer\n");
1146 glDrawBuffer(GL_BACK);
1147 checkGLcall("glDrawBuffer(GL_BACK)");
1149 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1152 switch(wined3d_settings.rendertargetlock_mode) {
1153 case RTL_AUTO:
1154 case RTL_READDRAW:
1155 case RTL_TEXDRAW:
1156 flush_to_framebuffer_drawpixels(This);
1157 break;
1159 case RTL_READTEX:
1160 case RTL_TEXTEX:
1161 flush_to_framebuffer_texture(This);
1162 break;
1164 if(!swapchain) {
1165 glDrawBuffer(myDevice->offscreenBuffer);
1166 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1167 } else if(swapchain->backBuffer) {
1168 glDrawBuffer(GL_BACK);
1169 checkGLcall("glDrawBuffer(GL_BACK)");
1170 } else {
1171 glDrawBuffer(GL_FRONT);
1172 checkGLcall("glDrawBuffer(GL_FRONT)");
1174 LEAVE_GL();
1176 This->dirtyRect.left = This->currentDesc.Width;
1177 This->dirtyRect.top = This->currentDesc.Height;
1178 This->dirtyRect.right = 0;
1179 This->dirtyRect.bottom = 0;
1180 This->Flags |= SFLAG_INDRAWABLE;
1181 } else if(iface == myDevice->stencilBufferTarget) {
1182 FIXME("Depth Stencil buffer locking is not implemented\n");
1183 } else {
1184 /* The rest should be a normal texture */
1185 IWineD3DBaseTextureImpl *impl;
1186 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1187 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1188 * states need resetting
1190 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1191 if(impl->baseTexture.bindCount) {
1192 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1194 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1198 unlock_end:
1199 This->Flags &= ~SFLAG_LOCKED;
1200 memset(&This->lockedRect, 0, sizeof(RECT));
1201 return WINED3D_OK;
1204 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1205 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1206 WINED3DLOCKED_RECT lock;
1207 UINT usage;
1208 BITMAPINFO* b_info;
1209 HDC ddc;
1210 DWORD *masks;
1211 HRESULT hr;
1212 RGBQUAD col[256];
1213 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1215 TRACE("(%p)->(%p)\n",This,pHDC);
1217 if(This->Flags & SFLAG_USERPTR) {
1218 ERR("Not supported on surfaces with an application-provided surfaces\n");
1219 return DDERR_NODC;
1222 /* Give more detailed info for ddraw */
1223 if (This->Flags & SFLAG_DCINUSE)
1224 return DDERR_DCALREADYCREATED;
1226 /* Can't GetDC if the surface is locked */
1227 if (This->Flags & SFLAG_LOCKED)
1228 return WINED3DERR_INVALIDCALL;
1230 memset(&lock, 0, sizeof(lock)); /* To be sure */
1232 /* Create a DIB section if there isn't a hdc yet */
1233 if(!This->hDC) {
1234 int extraline = 0;
1235 SYSTEM_INFO sysInfo;
1236 void *oldmem = This->resource.allocatedMemory;
1238 switch (This->bytesPerPixel) {
1239 case 2:
1240 case 4:
1241 /* Allocate extra space to store the RGB bit masks. */
1242 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD));
1243 break;
1245 case 3:
1246 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
1247 break;
1249 default:
1250 /* Allocate extra space for a palette. */
1251 b_info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1252 sizeof(BITMAPINFOHEADER)
1253 + sizeof(RGBQUAD)
1254 * (1 << (This->bytesPerPixel * 8)));
1255 break;
1258 if (!b_info)
1259 return E_OUTOFMEMORY;
1261 /* Some apps access the surface in via DWORDs, and do not take the necessary care at the end of the
1262 * surface. So we need at least extra 4 bytes at the end of the surface. Check against the page size,
1263 * if the last page used for the surface has at least 4 spare bytes we're safe, otherwise
1264 * add an extra line to the dib section
1266 GetSystemInfo(&sysInfo);
1267 if( ((This->resource.size + 3) % sysInfo.dwPageSize) < 4) {
1268 extraline = 1;
1269 TRACE("Adding an extra line to the dib section\n");
1272 b_info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1273 b_info->bmiHeader.biWidth = This->currentDesc.Width;
1274 b_info->bmiHeader.biHeight = -This->currentDesc.Height -extraline;
1275 b_info->bmiHeader.biSizeImage = ( This->currentDesc.Height + extraline) * IWineD3DSurface_GetPitch(iface);
1276 b_info->bmiHeader.biPlanes = 1;
1277 b_info->bmiHeader.biBitCount = This->bytesPerPixel * 8;
1279 b_info->bmiHeader.biXPelsPerMeter = 0;
1280 b_info->bmiHeader.biYPelsPerMeter = 0;
1281 b_info->bmiHeader.biClrUsed = 0;
1282 b_info->bmiHeader.biClrImportant = 0;
1284 /* Get the bit masks */
1285 masks = (DWORD *) &(b_info->bmiColors);
1286 switch (This->resource.format) {
1287 case WINED3DFMT_R8G8B8:
1288 usage = DIB_RGB_COLORS;
1289 b_info->bmiHeader.biCompression = BI_RGB;
1290 break;
1292 case WINED3DFMT_X1R5G5B5:
1293 case WINED3DFMT_A1R5G5B5:
1294 case WINED3DFMT_A4R4G4B4:
1295 case WINED3DFMT_X4R4G4B4:
1296 case WINED3DFMT_R3G3B2:
1297 case WINED3DFMT_A8R3G3B2:
1298 case WINED3DFMT_A2B10G10R10:
1299 case WINED3DFMT_A8B8G8R8:
1300 case WINED3DFMT_X8B8G8R8:
1301 case WINED3DFMT_A2R10G10B10:
1302 case WINED3DFMT_R5G6B5:
1303 case WINED3DFMT_A16B16G16R16:
1304 usage = 0;
1305 b_info->bmiHeader.biCompression = BI_BITFIELDS;
1306 masks[0] = formatEntry->redMask;
1307 masks[1] = formatEntry->greenMask;
1308 masks[2] = formatEntry->blueMask;
1309 break;
1311 default:
1312 /* Don't know palette */
1313 b_info->bmiHeader.biCompression = BI_RGB;
1314 usage = 0;
1315 break;
1318 ddc = GetDC(0);
1319 if (ddc == 0) {
1320 HeapFree(GetProcessHeap(), 0, b_info);
1321 return HRESULT_FROM_WIN32(GetLastError());
1324 TRACE("Creating a DIB section with size %dx%dx%d, size=%d\n", b_info->bmiHeader.biWidth, b_info->bmiHeader.biHeight, b_info->bmiHeader.biBitCount, b_info->bmiHeader.biSizeImage);
1325 This->dib.DIBsection = CreateDIBSection(ddc, b_info, usage, &This->dib.bitmap_data, 0 /* Handle */, 0 /* Offset */);
1326 ReleaseDC(0, ddc);
1328 if (!This->dib.DIBsection) {
1329 ERR("CreateDIBSection failed!\n");
1330 HeapFree(GetProcessHeap(), 0, b_info);
1331 return HRESULT_FROM_WIN32(GetLastError());
1334 TRACE("DIBSection at : %p\n", This->dib.bitmap_data);
1336 /* copy the existing surface to the dib section */
1337 if(This->resource.allocatedMemory) {
1338 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, b_info->bmiHeader.biSizeImage);
1339 /* We won't need that any more */
1340 } else {
1341 /* This is to make LockRect read the gl Texture although memory is allocated */
1342 This->Flags &= ~SFLAG_INSYSMEM;
1345 HeapFree(GetProcessHeap(), 0, b_info);
1347 /* Use the dib section from now on */
1348 This->resource.allocatedMemory = This->dib.bitmap_data;
1350 /* Now allocate a HDC */
1351 This->hDC = CreateCompatibleDC(0);
1352 This->dib.holdbitmap = SelectObject(This->hDC, This->dib.DIBsection);
1353 TRACE("using wined3d palette %p\n", This->palette);
1354 SelectPalette(This->hDC,
1355 This->palette ? This->palette->hpal : 0,
1356 FALSE);
1358 This->Flags |= SFLAG_DIBSECTION;
1360 if(This->Flags & SFLAG_CLIENT) {
1361 IWineD3DSurface_PreLoad(iface);
1363 HeapFree(GetProcessHeap(), 0, oldmem);
1366 /* Lock the surface */
1367 hr = IWineD3DSurface_LockRect(iface,
1368 &lock,
1369 NULL,
1371 if(FAILED(hr)) {
1372 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1373 /* keep the dib section */
1374 return hr;
1377 if(This->resource.format == WINED3DFMT_P8 ||
1378 This->resource.format == WINED3DFMT_A8P8) {
1379 unsigned int n;
1380 if(This->palette) {
1381 PALETTEENTRY ent[256];
1383 GetPaletteEntries(This->palette->hpal, 0, 256, ent);
1384 for (n=0; n<256; n++) {
1385 col[n].rgbRed = ent[n].peRed;
1386 col[n].rgbGreen = ent[n].peGreen;
1387 col[n].rgbBlue = ent[n].peBlue;
1388 col[n].rgbReserved = 0;
1390 } else {
1391 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1393 for (n=0; n<256; n++) {
1394 col[n].rgbRed = device->palettes[device->currentPalette][n].peRed;
1395 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
1396 col[n].rgbBlue = device->palettes[device->currentPalette][n].peBlue;
1397 col[n].rgbReserved = 0;
1401 SetDIBColorTable(This->hDC, 0, 256, col);
1404 *pHDC = This->hDC;
1405 TRACE("returning %p\n",*pHDC);
1406 This->Flags |= SFLAG_DCINUSE;
1408 return WINED3D_OK;
1411 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1412 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1414 TRACE("(%p)->(%p)\n",This,hDC);
1416 if (!(This->Flags & SFLAG_DCINUSE))
1417 return WINED3DERR_INVALIDCALL;
1419 /* we locked first, so unlock now */
1420 IWineD3DSurface_UnlockRect(iface);
1422 This->Flags &= ~SFLAG_DCINUSE;
1424 return WINED3D_OK;
1427 /* ******************************************************
1428 IWineD3DSurface Internal (No mapping to directx api) parts follow
1429 ****************************************************** */
1431 static 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) {
1432 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & DDSD_CKSRCBLT);
1433 const PixelFormatDesc *formatEntry = getFormatDescEntry(This->resource.format);
1435 /* Default values: From the surface */
1436 *format = formatEntry->glFormat;
1437 *internal = formatEntry->glInternal;
1438 *type = formatEntry->glType;
1439 *convert = NO_CONVERSION;
1440 *target_bpp = This->bytesPerPixel;
1442 /* Ok, now look if we have to do any conversion */
1443 switch(This->resource.format) {
1444 case WINED3DFMT_P8:
1445 /* ****************
1446 Paletted Texture
1447 **************** */
1448 /* Use conversion when the paletted texture extension is not available, or when it is available make sure it is used
1449 * for texturing as it won't work for calls like glDraw-/glReadPixels and further also use conversion in case of color keying.
1451 if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) || colorkey_active || (!use_texturing && GL_SUPPORT(EXT_PALETTED_TEXTURE)) ) {
1452 *format = GL_RGBA;
1453 *internal = GL_RGBA;
1454 *type = GL_UNSIGNED_BYTE;
1455 *target_bpp = 4;
1456 if(colorkey_active) {
1457 *convert = CONVERT_PALETTED_CK;
1458 } else {
1459 *convert = CONVERT_PALETTED;
1463 break;
1465 case WINED3DFMT_R3G3B2:
1466 /* **********************
1467 GL_UNSIGNED_BYTE_3_3_2
1468 ********************** */
1469 if (colorkey_active) {
1470 /* This texture format will never be used.. So do not care about color keying
1471 up until the point in time it will be needed :-) */
1472 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1474 break;
1476 case WINED3DFMT_R5G6B5:
1477 if (colorkey_active) {
1478 *convert = CONVERT_CK_565;
1479 *format = GL_RGBA;
1480 *internal = GL_RGBA;
1481 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1483 break;
1485 case WINED3DFMT_R8G8B8:
1486 if (colorkey_active) {
1487 *convert = CONVERT_CK_RGB24;
1488 *format = GL_RGBA;
1489 *internal = GL_RGBA;
1490 *type = GL_UNSIGNED_INT_8_8_8_8;
1491 *target_bpp = 4;
1493 break;
1495 case WINED3DFMT_X8R8G8B8:
1496 if (colorkey_active) {
1497 *convert = CONVERT_RGB32_888;
1498 *format = GL_RGBA;
1499 *internal = GL_RGBA;
1500 *type = GL_UNSIGNED_INT_8_8_8_8;
1502 break;
1504 case WINED3DFMT_V8U8:
1505 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1506 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1507 *format = GL_DUDV_ATI;
1508 *internal = GL_DU8DV8_ATI;
1509 *type = GL_BYTE;
1510 /* No conversion - Just change the gl type */
1511 break;
1513 *convert = CONVERT_V8U8;
1514 *format = GL_BGR;
1515 *internal = GL_RGB8;
1516 *type = GL_UNSIGNED_BYTE;
1517 *target_bpp = 3;
1518 break;
1520 case WINED3DFMT_X8L8V8U8:
1521 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1522 *convert = CONVERT_X8L8V8U8;
1523 *format = GL_BGRA;
1524 *internal = GL_RGBA8;
1525 *type = GL_UNSIGNED_BYTE;
1526 *target_bpp = 4;
1527 /* Not supported by GL_ATI_envmap_bumpmap */
1528 break;
1530 case WINED3DFMT_Q8W8V8U8:
1531 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1532 *convert = CONVERT_Q8W8V8U8;
1533 *format = GL_BGRA;
1534 *internal = GL_RGBA8;
1535 *type = GL_UNSIGNED_BYTE;
1536 *target_bpp = 4;
1537 /* Not supported by GL_ATI_envmap_bumpmap */
1538 break;
1540 case WINED3DFMT_V16U16:
1541 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1542 *convert = CONVERT_V16U16;
1543 *format = GL_BGR;
1544 *internal = GL_RGB16;
1545 *type = GL_SHORT;
1546 *target_bpp = 6;
1547 /* What should I do here about GL_ATI_envmap_bumpmap?
1548 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1550 break;
1552 default:
1553 break;
1556 return WINED3D_OK;
1559 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf) {
1560 BYTE *source, *dest;
1561 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert, surf);
1563 switch (convert) {
1564 case NO_CONVERSION:
1566 memcpy(dst, src, pitch * height);
1567 break;
1569 case CONVERT_PALETTED:
1570 case CONVERT_PALETTED_CK:
1572 IWineD3DPaletteImpl* pal = surf->palette;
1573 BYTE table[256][4];
1574 unsigned int i;
1575 unsigned int x, y;
1577 if( pal == NULL) {
1578 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1581 if (pal == NULL) {
1582 /* Still no palette? Use the device's palette */
1583 /* Get the surface's palette */
1584 for (i = 0; i < 256; i++) {
1585 IWineD3DDeviceImpl *device = surf->resource.wineD3DDevice;
1587 table[i][0] = device->palettes[device->currentPalette][i].peRed;
1588 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
1589 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
1590 if ((convert == CONVERT_PALETTED_CK) &&
1591 (i >= surf->SrcBltCKey.dwColorSpaceLowValue) &&
1592 (i <= surf->SrcBltCKey.dwColorSpaceHighValue)) {
1593 /* We should maybe here put a more 'neutral' color than the standard bright purple
1594 one often used by application to prevent the nice purple borders when bi-linear
1595 filtering is on */
1596 table[i][3] = 0x00;
1597 } else {
1598 table[i][3] = 0xFF;
1601 } else {
1602 TRACE("Using surface palette %p\n", pal);
1603 /* Get the surface's palette */
1604 for (i = 0; i < 256; i++) {
1605 table[i][0] = pal->palents[i].peRed;
1606 table[i][1] = pal->palents[i].peGreen;
1607 table[i][2] = pal->palents[i].peBlue;
1608 if ((convert == CONVERT_PALETTED_CK) &&
1609 (i >= surf->SrcBltCKey.dwColorSpaceLowValue) &&
1610 (i <= surf->SrcBltCKey.dwColorSpaceHighValue)) {
1611 /* We should maybe here put a more 'neutral' color than the standard bright purple
1612 one often used by application to prevent the nice purple borders when bi-linear
1613 filtering is on */
1614 table[i][3] = 0x00;
1615 } else {
1616 table[i][3] = 0xFF;
1621 for (y = 0; y < height; y++)
1623 source = src + pitch * y;
1624 dest = dst + outpitch * y;
1625 /* This is an 1 bpp format, using the width here is fine */
1626 for (x = 0; x < width; x++) {
1627 BYTE color = *source++;
1628 *dest++ = table[color][0];
1629 *dest++ = table[color][1];
1630 *dest++ = table[color][2];
1631 *dest++ = table[color][3];
1635 break;
1637 case CONVERT_CK_565:
1639 /* Converting the 565 format in 5551 packed to emulate color-keying.
1641 Note : in all these conversion, it would be best to average the averaging
1642 pixels to get the color of the pixel that will be color-keyed to
1643 prevent 'color bleeding'. This will be done later on if ever it is
1644 too visible.
1646 Note2: Nvidia documents say that their driver does not support alpha + color keying
1647 on the same surface and disables color keying in such a case
1649 unsigned int x, y;
1650 WORD *Source;
1651 WORD *Dest;
1653 TRACE("Color keyed 565\n");
1655 for (y = 0; y < height; y++) {
1656 Source = (WORD *) (src + y * pitch);
1657 Dest = (WORD *) (dst + y * outpitch);
1658 for (x = 0; x < width; x++ ) {
1659 WORD color = *Source++;
1660 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1661 if ((color < surf->SrcBltCKey.dwColorSpaceLowValue) ||
1662 (color > surf->SrcBltCKey.dwColorSpaceHighValue)) {
1663 *Dest |= 0x0001;
1665 Dest++;
1669 break;
1671 case CONVERT_V8U8:
1673 unsigned int x, y;
1674 short *Source;
1675 unsigned char *Dest;
1676 for(y = 0; y < height; y++) {
1677 Source = (short *) (src + y * pitch);
1678 Dest = (unsigned char *) (dst + y * outpitch);
1679 for (x = 0; x < width; x++ ) {
1680 long color = (*Source++);
1681 /* B */ Dest[0] = 0xff;
1682 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1683 /* R */ Dest[2] = (color) + 128; /* U */
1684 Dest += 3;
1687 break;
1690 case CONVERT_Q8W8V8U8:
1692 unsigned int x, y;
1693 DWORD *Source;
1694 unsigned char *Dest;
1695 for(y = 0; y < height; y++) {
1696 Source = (DWORD *) (src + y * pitch);
1697 Dest = (unsigned char *) (dst + y * outpitch);
1698 for (x = 0; x < width; x++ ) {
1699 long color = (*Source++);
1700 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1701 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1702 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1703 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1704 Dest += 4;
1707 break;
1710 default:
1711 ERR("Unsupported conversation type %d\n", convert);
1713 return WINED3D_OK;
1716 /* This function is used in case of 8bit paletted textures to upload the palette.
1717 For now it only supports GL_EXT_paletted_texture extension but support for other
1718 extensions like ARB_fragment_program and ATI_fragment_shaders will be added as well.
1720 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
1721 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1722 IWineD3DPaletteImpl* pal = This->palette;
1723 BYTE table[256][4];
1724 int i;
1726 if (pal == NULL) {
1727 /* Still no palette? Use the device's palette */
1728 /* Get the surface's palette */
1729 for (i = 0; i < 256; i++) {
1730 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1732 table[i][0] = device->palettes[device->currentPalette][i].peRed;
1733 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
1734 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
1735 if ((convert == CONVERT_PALETTED_CK) &&
1736 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1737 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1738 /* We should maybe here put a more 'neutral' color than the standard bright purple
1739 one often used by application to prevent the nice purple borders when bi-linear
1740 filtering is on */
1741 table[i][3] = 0x00;
1742 } else {
1743 table[i][3] = 0xFF;
1746 } else {
1747 TRACE("Using surface palette %p\n", pal);
1748 /* Get the surface's palette */
1749 for (i = 0; i < 256; i++) {
1750 table[i][0] = pal->palents[i].peRed;
1751 table[i][1] = pal->palents[i].peGreen;
1752 table[i][2] = pal->palents[i].peBlue;
1753 if ((convert == CONVERT_PALETTED_CK) &&
1754 (i >= This->SrcBltCKey.dwColorSpaceLowValue) &&
1755 (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
1756 /* We should maybe here put a more 'neutral' color than the standard bright purple
1757 one often used by application to prevent the nice purple borders when bi-linear
1758 filtering is on */
1759 table[i][3] = 0x00;
1760 } else {
1761 table[i][3] = 0xFF;
1765 GL_EXTCALL(glColorTableEXT(GL_TEXTURE_2D,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
1768 static BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
1769 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1771 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
1772 /* If a ddraw-style palette is attached assume no d3d9 palette change.
1773 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
1775 return FALSE;
1778 if(This->palette9) {
1779 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
1780 return FALSE;
1782 } else {
1783 This->palette9 = (PALETTEENTRY *) HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
1785 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
1786 return TRUE;
1789 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface) {
1790 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1791 GLenum format, internal, type;
1792 CONVERT_TYPES convert;
1793 int bpp;
1794 int width, pitch, outpitch;
1795 BYTE *mem;
1797 if (!(This->Flags & SFLAG_INTEXTURE)) {
1798 TRACE("Reloading because surface is dirty\n");
1799 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
1800 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & DDSD_CKSRCBLT))) ||
1801 /* Reload: vice versa OR */
1802 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & DDSD_CKSRCBLT)) ||
1803 /* Also reload: Color key is active AND the color key has changed */
1804 ((This->CKeyFlags & DDSD_CKSRCBLT) && (
1805 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
1806 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
1807 TRACE("Reloading because of color keying\n");
1808 } else if(palette9_changed(This)) {
1809 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1810 } else {
1811 TRACE("surface is already in texture\n");
1812 return WINED3D_OK;
1815 This->Flags |= SFLAG_INTEXTURE;
1817 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
1818 * These resources are not bound by device size or format restrictions. Because of this,
1819 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
1820 * However, these resources can always be created, locked, and copied.
1822 if (This->resource.pool == WINED3DPOOL_SCRATCH )
1824 FIXME("(%p) Operation not supported for scratch textures\n",This);
1825 return WINED3DERR_INVALIDCALL;
1828 if (This->Flags & SFLAG_INDRAWABLE) {
1829 if (This->glDescription.level != 0)
1830 FIXME("Surface in texture is only supported for level 0\n");
1831 else if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8 ||
1832 This->resource.format == WINED3DFMT_DXT1 || This->resource.format == WINED3DFMT_DXT2 ||
1833 This->resource.format == WINED3DFMT_DXT3 || This->resource.format == WINED3DFMT_DXT4 ||
1834 This->resource.format == WINED3DFMT_DXT5)
1835 FIXME("Format %d not supported\n", This->resource.format);
1836 else {
1837 GLint prevRead;
1839 ENTER_GL();
1840 glGetIntegerv(GL_READ_BUFFER, &prevRead);
1841 vcheckGLcall("glGetIntegerv");
1842 glReadBuffer(This->resource.wineD3DDevice->offscreenBuffer);
1843 vcheckGLcall("glReadBuffer");
1845 #ifdef __APPLE__
1846 /* Crossover HACK for nvidia macs
1847 * The nvidia macs don't synchronize auxiliary buffers properly, force them to their luck :-/
1849 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
1850 glFinish();
1852 #endif
1853 glCopyTexImage2D(This->glDescription.target,
1854 This->glDescription.level,
1855 This->glDescription.glFormatInternal,
1858 This->currentDesc.Width,
1859 This->currentDesc.Height,
1862 checkGLcall("glCopyTexImage2D");
1863 glReadBuffer(prevRead);
1864 vcheckGLcall("glReadBuffer");
1866 LEAVE_GL();
1868 TRACE("Updated target %d\n", This->glDescription.target);
1870 return WINED3D_OK;
1872 /* Otherwise: System memory copy must be most up to date */
1874 if(This->CKeyFlags & DDSD_CKSRCBLT) {
1875 This->Flags |= SFLAG_GLCKEY;
1876 This->glCKey = This->SrcBltCKey;
1878 else This->Flags &= ~SFLAG_GLCKEY;
1880 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp);
1882 /* The width is in 'length' not in bytes */
1883 width = This->currentDesc.Width;
1884 pitch = IWineD3DSurface_GetPitch(iface);
1886 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
1887 int height = This->currentDesc.Height;
1889 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
1890 outpitch = width * bpp;
1891 outpitch = (outpitch + SURFACE_ALIGNMENT - 1) & ~(SURFACE_ALIGNMENT - 1);
1893 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
1894 if(!mem) {
1895 ERR("Out of memory %d, %d!\n", outpitch, height);
1896 return WINED3DERR_OUTOFVIDEOMEMORY;
1898 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
1900 This->Flags |= SFLAG_CONVERTED;
1901 } else if (This->resource.format == WINED3DFMT_P8 && GL_SUPPORT(EXT_PALETTED_TEXTURE)) {
1902 d3dfmt_p8_upload_palette(iface, convert);
1903 This->Flags &= ~SFLAG_CONVERTED;
1904 mem = This->resource.allocatedMemory;
1905 } else {
1906 This->Flags &= ~SFLAG_CONVERTED;
1907 mem = This->resource.allocatedMemory;
1910 /* Make sure the correct pitch is used */
1911 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
1913 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
1914 TRACE("non power of two support\n");
1915 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
1916 if (mem) {
1917 surface_upload_data(This, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
1919 } else {
1920 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
1921 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
1923 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
1924 if (mem) {
1925 surface_upload_data(This, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
1929 /* Restore the default pitch */
1930 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
1932 if (mem != This->resource.allocatedMemory)
1933 HeapFree(GetProcessHeap(), 0, mem);
1935 #if 0
1937 static unsigned int gen = 0;
1938 char buffer[4096];
1939 ++gen;
1940 if ((gen % 10) == 0) {
1941 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
1942 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
1945 * debugging crash code
1946 if (gen == 250) {
1947 void** test = NULL;
1948 *test = 0;
1952 #endif
1954 if (!(This->Flags & SFLAG_DONOTFREE)) {
1955 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
1956 This->resource.allocatedMemory = NULL;
1957 This->Flags &= ~SFLAG_INSYSMEM;
1960 return WINED3D_OK;
1963 #include <errno.h>
1964 #include <stdio.h>
1965 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
1966 FILE* f = NULL;
1967 UINT i, y;
1968 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1969 char *allocatedMemory;
1970 char *textureRow;
1971 IWineD3DSwapChain *swapChain = NULL;
1972 int width, height;
1973 GLuint tmpTexture = 0;
1974 DWORD color;
1975 /*FIXME:
1976 Textures my not be stored in ->allocatedgMemory and a GlTexture
1977 so we should lock the surface before saving a snapshot, or at least check that
1979 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
1980 by calling GetTexImage and in compressed form by calling
1981 GetCompressedTexImageARB. Queried compressed images can be saved and
1982 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
1983 texture images do not need to be processed by the GL and should
1984 significantly improve texture loading performance relative to uncompressed
1985 images. */
1987 /* Setup the width and height to be the internal texture width and height. */
1988 width = This->pow2Width;
1989 height = This->pow2Height;
1990 /* check to see if were a 'virtual' texture e.g. were not a pbuffer of texture were a back buffer*/
1991 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
1993 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
1994 /* if were not a real texture then read the back buffer into a real texture */
1995 /* we don't want to interfere with the back buffer so read the data into a temporary
1996 * texture and then save the data out of the temporary texture
1998 GLint prevRead;
1999 ENTER_GL();
2000 TRACE("(%p) Reading render target into texture\n", This);
2001 glEnable(GL_TEXTURE_2D);
2003 glGenTextures(1, &tmpTexture);
2004 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2006 glTexImage2D(GL_TEXTURE_2D,
2008 GL_RGBA,
2009 width,
2010 height,
2011 0/*border*/,
2012 GL_RGBA,
2013 GL_UNSIGNED_INT_8_8_8_8_REV,
2014 NULL);
2016 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2017 vcheckGLcall("glGetIntegerv");
2018 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2019 vcheckGLcall("glReadBuffer");
2020 glCopyTexImage2D(GL_TEXTURE_2D,
2022 GL_RGBA,
2025 width,
2026 height,
2029 checkGLcall("glCopyTexImage2D");
2030 glReadBuffer(prevRead);
2031 LEAVE_GL();
2033 } else { /* bind the real texture, and make sure it up to date */
2034 IWineD3DSurface_PreLoad(iface);
2036 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2037 ENTER_GL();
2038 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2039 glGetTexImage(GL_TEXTURE_2D,
2040 This->glDescription.level,
2041 GL_RGBA,
2042 GL_UNSIGNED_INT_8_8_8_8_REV,
2043 allocatedMemory);
2044 checkGLcall("glTexImage2D");
2045 if (tmpTexture) {
2046 glBindTexture(GL_TEXTURE_2D, 0);
2047 glDeleteTextures(1, &tmpTexture);
2049 LEAVE_GL();
2051 f = fopen(filename, "w+");
2052 if (NULL == f) {
2053 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2054 return WINED3DERR_INVALIDCALL;
2056 /* Save the dat out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha chanel*/
2057 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2058 /* TGA header */
2059 fputc(0,f);
2060 fputc(0,f);
2061 fputc(2,f);
2062 fputc(0,f);
2063 fputc(0,f);
2064 fputc(0,f);
2065 fputc(0,f);
2066 fputc(0,f);
2067 fputc(0,f);
2068 fputc(0,f);
2069 fputc(0,f);
2070 fputc(0,f);
2071 /* short width*/
2072 fwrite(&width,2,1,f);
2073 /* short height */
2074 fwrite(&height,2,1,f);
2075 /* format rgba */
2076 fputc(0x20,f);
2077 fputc(0x28,f);
2078 /* raw data */
2079 /* 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*/
2080 if(swapChain)
2081 textureRow = allocatedMemory + (width * (height - 1) *4);
2082 else
2083 textureRow = allocatedMemory;
2084 for (y = 0 ; y < height; y++) {
2085 for (i = 0; i < width; i++) {
2086 color = *((DWORD*)textureRow);
2087 fputc((color >> 16) & 0xFF, f); /* B */
2088 fputc((color >> 8) & 0xFF, f); /* G */
2089 fputc((color >> 0) & 0xFF, f); /* R */
2090 fputc((color >> 24) & 0xFF, f); /* A */
2091 textureRow += 4;
2093 /* take two rows of the pointer to the texture memory */
2094 if(swapChain)
2095 (textureRow-= width << 3);
2098 TRACE("Closing file\n");
2099 fclose(f);
2101 if(swapChain) {
2102 IWineD3DSwapChain_Release(swapChain);
2104 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2105 return WINED3D_OK;
2109 * Slightly inefficient way to handle multiple dirty rects but it works :)
2111 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2112 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2113 IWineD3DBaseTexture *baseTexture = NULL;
2114 This->Flags &= ~(SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
2115 if (NULL != pDirtyRect) {
2116 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2117 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2118 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2119 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2120 } else {
2121 This->dirtyRect.left = 0;
2122 This->dirtyRect.top = 0;
2123 This->dirtyRect.right = This->currentDesc.Width;
2124 This->dirtyRect.bottom = This->currentDesc.Height;
2126 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2127 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2128 /* if the container is a basetexture then mark it dirty. */
2129 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2130 TRACE("Passing to conatiner\n");
2131 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2132 IWineD3DBaseTexture_Release(baseTexture);
2134 return WINED3D_OK;
2137 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
2138 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2140 TRACE("This %p, container %p\n", This, container);
2142 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
2144 TRACE("Setting container to %p from %p\n", container, This->container);
2145 This->container = container;
2147 return WINED3D_OK;
2150 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2151 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2152 const PixelFormatDesc *formatEntry = getFormatDescEntry(format);
2154 if (This->resource.format != WINED3DFMT_UNKNOWN) {
2155 FIXME("(%p) : The foramt of the surface must be WINED3DFORMAT_UNKNOWN\n", This);
2156 return WINED3DERR_INVALIDCALL;
2159 TRACE("(%p) : Setting texture foramt to (%d,%s)\n", This, format, debug_d3dformat(format));
2160 if (format == WINED3DFMT_UNKNOWN) {
2161 This->resource.size = 0;
2162 } else if (format == WINED3DFMT_DXT1) {
2163 /* DXT1 is half byte per pixel */
2164 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4)) >> 1;
2166 } else if (format == WINED3DFMT_DXT2 || format == WINED3DFMT_DXT3 ||
2167 format == WINED3DFMT_DXT4 || format == WINED3DFMT_DXT5) {
2168 This->resource.size = ((max(This->pow2Width, 4) * formatEntry->bpp) * max(This->pow2Height, 4));
2169 } else {
2170 This->resource.size = ((This->pow2Width * formatEntry->bpp) + SURFACE_ALIGNMENT - 1) & ~(SURFACE_ALIGNMENT - 1);
2171 This->resource.size *= This->pow2Height;
2175 /* Setup some glformat defaults */
2176 This->glDescription.glFormat = formatEntry->glFormat;
2177 This->glDescription.glFormatInternal = formatEntry->glInternal;
2178 This->glDescription.glType = formatEntry->glType;
2180 if (format != WINED3DFMT_UNKNOWN) {
2181 This->bytesPerPixel = formatEntry->bpp;
2182 } else {
2183 This->bytesPerPixel = 0;
2186 This->Flags |= (WINED3DFMT_D16_LOCKABLE == format) ? SFLAG_LOCKABLE : 0;
2188 This->resource.format = format;
2190 TRACE("(%p) : Size %d, bytesPerPixel %d, glFormat %d, glFotmatInternal %d, glType %d\n", This, This->resource.size, This->bytesPerPixel, This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2192 return WINED3D_OK;
2195 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2196 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2198 /* Render targets depend on their hdc, and we can't create a hdc on a user pointer */
2199 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
2200 ERR("Not supported on render targets\n");
2201 return WINED3DERR_INVALIDCALL;
2204 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2205 WARN("Surface is locked or the HDC is in use\n");
2206 return WINED3DERR_INVALIDCALL;
2209 if(Mem && Mem != This->resource.allocatedMemory) {
2210 void *release = NULL;
2212 /* Do I have to copy the old surface content? */
2213 if(This->Flags & SFLAG_DIBSECTION) {
2214 /* Release the DC. No need to hold the critical section for the update
2215 * Thread because this thread runs only on front buffers, but this method
2216 * fails for render targets in the check above.
2218 SelectObject(This->hDC, This->dib.holdbitmap);
2219 DeleteDC(This->hDC);
2220 /* Release the DIB section */
2221 DeleteObject(This->dib.DIBsection);
2222 This->dib.bitmap_data = NULL;
2223 This->resource.allocatedMemory = NULL;
2224 This->hDC = NULL;
2225 This->Flags &= ~SFLAG_DIBSECTION;
2226 } else if(!(This->Flags & SFLAG_USERPTR)) {
2227 release = This->resource.allocatedMemory;
2229 This->resource.allocatedMemory = Mem;
2230 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2232 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2233 This->Flags &= ~(SFLAG_INDRAWABLE | SFLAG_INTEXTURE);
2235 /* For client textures opengl has to be notified */
2236 if(This->Flags & SFLAG_CLIENT) {
2237 IWineD3DSurface_PreLoad(iface);
2238 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2241 /* Now free the old memory if any */
2242 HeapFree(GetProcessHeap(), 0, release);
2243 } else if(This->Flags & SFLAG_USERPTR) {
2244 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2245 This->resource.allocatedMemory = NULL;
2246 This->Flags &= ~SFLAG_USERPTR;
2248 if(This->Flags & SFLAG_CLIENT) {
2249 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2250 IWineD3DSurface_PreLoad(iface);
2253 return WINED3D_OK;
2256 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2257 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2258 IWineD3DDevice *D3D = (IWineD3DDevice *) This->resource.wineD3DDevice;
2259 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2261 /* Flipping is only supported on RenderTargets */
2262 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return DDERR_NOTFLIPPABLE;
2264 if(override) {
2265 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2266 * FIXME("(%p) Target override is not supported by now\n", This);
2267 * Additionally, it isn't really possible to support triple-buffering
2268 * properly on opengl at all
2272 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2273 return IWineD3DDevice_Present(D3D, NULL, NULL, 0, NULL);
2276 /* Does a direct frame buffer -> texture copy. Stretching is done
2277 * with single pixel copy calls
2279 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown) {
2280 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2281 float xrel, yrel;
2282 UINT row;
2283 BOOL warned = FALSE; /* deliberately not static */
2284 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2286 ENTER_GL();
2288 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2290 /* Bind the target texture */
2291 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
2292 checkGLcall("glBindTexture");
2293 if(!swapchain) {
2294 glReadBuffer(myDevice->offscreenBuffer);
2295 } else if(swapchain->backBuffer && SrcSurface == swapchain->backBuffer[0]) {
2296 glReadBuffer(GL_BACK);
2297 } else {
2298 glReadBuffer(GL_FRONT);
2300 checkGLcall("glReadBuffer");
2302 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2303 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2305 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2306 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2309 #ifdef __APPLE__
2310 /* Crossover HACK for nvidia macs
2311 * The nvidia macs don't synchronize auxiliary buffers properly, force them to their luck :-/
2313 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
2314 glFinish();
2316 #endif
2318 if(upsidedown &&
2319 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2320 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2321 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2323 glCopyTexSubImage2D(This->glDescription.target,
2324 This->glDescription.level,
2325 drect->x1, drect->y1, /* xoffset, yoffset */
2326 srect->x1, Src->currentDesc.Height - srect->y2,
2327 drect->x2 - drect->x1, drect->y2 - drect->y1);
2328 } else {
2329 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2330 /* I have to process this row by row to swap the image,
2331 * otherwise it would be upside down, so streching in y direction
2332 * doesn't cost extra time
2334 * However, streching in x direction can be avoided if not necessary
2336 for(row = drect->y1; row < drect->y2; row++) {
2337 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2338 /* Well, that stuff works, but it's very slow.
2339 * find a better way instead
2341 UINT col;
2343 if(!warned) {
2344 warned = TRUE;
2345 FIXME("Doing a pixel by pixel render target -> texture copy, expect performance issues\n");
2347 /* warned is not static, so this works */
2348 if(!enableSlowBlit) {
2349 ERR("Slow pixel per pixel copy disabled, aborting\n");
2350 break;
2354 for(col = drect->x1; col < drect->x2; col++) {
2355 glCopyTexSubImage2D(This->glDescription.target,
2356 This->glDescription.level,
2357 drect->x1 + col, row, /* xoffset, yoffset */
2358 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2359 1, 1);
2361 } else {
2362 glCopyTexSubImage2D(This->glDescription.target,
2363 This->glDescription.level,
2364 drect->x1, row, /* xoffset, yoffset */
2365 srect->x1, yoffset - (int) (row * yrel),
2366 drect->x2-drect->x1, 1);
2371 vcheckGLcall("glCopyTexSubImage2D");
2372 LEAVE_GL();
2375 /* Uses the hardware to stretch and flip the image */
2376 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown) {
2377 GLuint src, backup = 0;
2378 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2379 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2380 float left, right, top, bottom; /* Texture coordinates */
2381 UINT fbwidth = Src->currentDesc.Width;
2382 UINT fbheight = Src->currentDesc.Height;
2383 GLenum drawBuffer = GL_BACK;
2385 TRACE("Using hwstretch blit\n");
2386 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2387 ENTER_GL();
2388 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2390 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2391 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2393 if(GL_LIMITS(aux_buffers) >= 2) {
2394 /* Got more than one aux buffer? Use the 2nd aux buffer */
2395 drawBuffer = GL_AUX1;
2396 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2397 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2398 drawBuffer = GL_AUX0;
2401 if(!swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
2402 glGenTextures(1, &backup);
2403 checkGLcall("glGenTextures\n");
2404 glBindTexture(GL_TEXTURE_2D, backup);
2405 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2406 } else {
2407 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2408 * we are reading from the back buffer, the backup can be used as source texture
2410 if(Src->glDescription.textureName == 0) {
2411 /* Get it a description */
2412 IWineD3DSurface_PreLoad(SrcSurface);
2414 glBindTexture(GL_TEXTURE_2D, Src->glDescription.textureName);
2415 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2417 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2418 Src->Flags &= ~SFLAG_INTEXTURE;
2421 glReadBuffer(GL_BACK);
2422 checkGLcall("glReadBuffer(GL_BACK)");
2424 /* TODO: Only back up the part that will be overwritten */
2425 #ifdef __APPLE__
2426 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
2427 glFinish();
2429 #endif
2431 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2432 0, 0 /* read offsets */,
2433 0, 0,
2434 fbwidth,
2435 fbheight);
2436 #ifdef __APPLE__
2437 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
2438 glFinish();
2440 #endif
2442 checkGLcall("glCopyTexSubImage2D");
2444 /* No issue with overriding these - the sampler is dirty due to blit usage */
2445 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2446 checkGLcall("glTexParameteri");
2447 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2448 checkGLcall("glTexParameteri");
2450 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2451 src = backup ? backup : Src->glDescription.textureName;
2452 } else {
2453 glReadBuffer(GL_FRONT);
2454 checkGLcall("glReadBuffer(GL_FRONT)");
2456 glGenTextures(1, &src);
2457 checkGLcall("glGenTextures(1, &src)");
2458 glBindTexture(GL_TEXTURE_2D, src);
2459 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2461 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2462 * out for power of 2 sizes
2464 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2465 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2466 checkGLcall("glTexImage2D");
2467 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2468 0, 0 /* read offsets */,
2469 0, 0,
2470 fbwidth,
2471 fbheight);
2473 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2474 checkGLcall("glTexParameteri");
2475 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2476 checkGLcall("glTexParameteri");
2478 glReadBuffer(GL_BACK);
2479 checkGLcall("glReadBuffer(GL_BACK)");
2481 checkGLcall("glEnd and previous");
2483 left = (float) srect->x1 / (float) Src->pow2Width;
2484 right = (float) srect->x2 / (float) Src->pow2Width;
2486 if(upsidedown) {
2487 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2488 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2489 } else {
2490 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2491 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2494 /* draw the source texture stretched and upside down. The correct surface is bound already */
2495 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2496 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2498 glDrawBuffer(drawBuffer);
2499 glReadBuffer(drawBuffer);
2501 glBegin(GL_QUADS);
2502 /* bottom left */
2503 glTexCoord2f(left, bottom);
2504 glVertex2i(0, fbheight);
2506 /* top left */
2507 glTexCoord2f(left, top);
2508 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2510 /* top right */
2511 glTexCoord2f(right, top);
2512 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2514 /* bottom right */
2515 glTexCoord2f(right, bottom);
2516 glVertex2i(drect->x2 - drect->x1, fbheight);
2517 glEnd();
2518 checkGLcall("glEnd and previous");
2520 /* Now read the stretched and upside down image into the destination texture */
2521 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2522 checkGLcall("glBindTexture");
2524 #ifdef __APPLE__
2525 /* Crossover HACK for nvidia macs
2526 * The nvidia macs don't synchronize auxiliary buffers properly, force them to their luck :-/
2528 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
2529 glFinish();
2531 #endif
2533 glCopyTexSubImage2D(This->glDescription.target,
2535 drect->x1, drect->y1, /* xoffset, yoffset */
2536 0, 0, /* We blitted the image to the origin */
2537 drect->x2 - drect->x1, drect->y2 - drect->y1);
2538 checkGLcall("glCopyTexSubImage2D");
2539 #ifdef __APPLE__
2540 if(GL_SUPPORT(NV_REGISTER_COMBINERS)) {
2541 glFinish();
2543 #endif
2545 /* Write the back buffer backup back */
2546 glBindTexture(GL_TEXTURE_2D, backup ? backup : Src->glDescription.textureName);
2547 checkGLcall("glBindTexture(GL_TEXTURE_2D, Src->glDescription.textureName)");
2549 if(drawBuffer == GL_BACK) {
2550 glBegin(GL_QUADS);
2551 /* top left */
2552 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2553 glVertex2i(0, 0);
2555 /* bottom left */
2556 glTexCoord2f(0.0, 0.0);
2557 glVertex2i(0, fbheight);
2559 /* bottom right */
2560 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2561 glVertex2i(fbwidth, Src->currentDesc.Height);
2563 /* top right */
2564 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2565 glVertex2i(fbwidth, 0);
2566 glEnd();
2567 } else {
2568 /* Restore the old draw buffer */
2569 glDrawBuffer(GL_BACK);
2572 /* Cleanup */
2573 if(src != Src->glDescription.textureName && src != backup) {
2574 glDeleteTextures(1, &src);
2575 checkGLcall("glDeleteTextures(1, &src)");
2577 if(backup) {
2578 glDeleteTextures(1, &backup);
2579 checkGLcall("glDeleteTextures(1, &backup)");
2581 LEAVE_GL();
2584 /* Not called from the VTable */
2585 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx) {
2586 WINED3DRECT rect;
2587 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2588 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2589 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2590 BOOL SrcOK = TRUE;
2592 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2594 /* Get the swapchain. One of the surfaces has to be a primary surface */
2595 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2596 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2597 if(Src) {
2598 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2599 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2602 /* Early sort out of cases where no render target is used */
2603 if(!dstSwapchain && !srcSwapchain &&
2604 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2605 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2606 return WINED3DERR_INVALIDCALL;
2609 /* No destination color keying supported */
2610 if(Flags & (DDBLT_KEYDEST | DDBLT_KEYDESTOVERRIDE)) {
2611 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2612 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2613 return WINED3DERR_INVALIDCALL;
2616 if (DestRect) {
2617 rect.x1 = DestRect->left;
2618 rect.y1 = DestRect->top;
2619 rect.x2 = DestRect->right;
2620 rect.y2 = DestRect->bottom;
2621 } else {
2622 rect.x1 = 0;
2623 rect.y1 = 0;
2624 rect.x2 = This->currentDesc.Width;
2625 rect.y2 = This->currentDesc.Height;
2628 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
2629 if(dstSwapchain && dstSwapchain == srcSwapchain) {
2630 /* Half-life does a Blt from the back buffer to the front buffer,
2631 * Full surface size, no flags... Use present instead
2634 /* Check rects - IWineD3DDevice_Present doesn't handle them */
2635 if( SrcRect ) {
2636 if( (SrcRect->left == 0) && (SrcRect->top == 0) &&
2637 (SrcRect->right == Src->currentDesc.Width) && (SrcRect->bottom == Src->currentDesc.Height) ) {
2638 SrcOK = TRUE;
2640 } else {
2641 SrcOK = TRUE;
2644 /* Check the Destination rect and the surface sizes */
2645 if(hl1hack ||
2646 (SrcOK &&
2647 (rect.x1 == 0) && (rect.y1 == 0) &&
2648 (rect.x2 == This->currentDesc.Width) && (rect.y2 == This->currentDesc.Height) &&
2649 (This->currentDesc.Width == Src->currentDesc.Width) &&
2650 (This->currentDesc.Height == Src->currentDesc.Height))) {
2651 /* These flags are unimportant for the flag check, remove them */
2653 if((Flags & ~(DDBLT_DONOTWAIT | DDBLT_WAIT)) == 0) {
2654 if( dstSwapchain->backBuffer && ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) &&
2655 SrcSurface == dstSwapchain->backBuffer[0] ) {
2657 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
2659 /* The idea behind this is that a glReadPixels and a glDrawPixels call
2660 * take very long, while a flip is fast.
2661 * This applies to Half-Life, which does such Blts every time it finished
2662 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
2663 * menu. This is also used by all apps when they do windowed rendering
2665 * The problem is that flipping is not really the same as copying. After a
2666 * Blt the front buffer is a copy of the back buffer, and the back buffer is
2667 * untouched. Therefore it's necessary to override the swap effect
2668 * and to set it back after the flip.
2671 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
2673 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
2674 IWineD3DDevice_Present((IWineD3DDevice *) This->resource.wineD3DDevice,
2675 NULL, NULL, 0, NULL);
2677 dstSwapchain->presentParms.SwapEffect = orig_swap;
2679 return WINED3D_OK;
2684 TRACE("Unsupported blit between buffers on the same swapchain\n");
2685 return WINED3DERR_INVALIDCALL;
2686 } else if((dstSwapchain || This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) &&
2687 (srcSwapchain || SrcSurface == myDevice->render_targets[0]) ) {
2688 ERR("Can't perform hardware blit between 2 different swapchains, falling back to software\n");
2689 return WINED3DERR_INVALIDCALL;
2692 if(srcSwapchain || SrcSurface == myDevice->render_targets[0]) {
2693 /* Blit from render target to texture */
2694 WINED3DRECT srect;
2695 BOOL upsideDown, stretchx;
2697 if(Flags & (DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE)) {
2698 TRACE("Color keying not supported by frame buffer to texture blit\n");
2699 return WINED3DERR_INVALIDCALL;
2700 /* Destination color key is checked above */
2703 /* Call preload for the surface to make sure it isn't dirty */
2704 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2706 /* Make sure that the top pixel is always above the bottom pixel, and keep a seperate upside down flag
2707 * glCopyTexSubImage is a bit picky about the parameters we pass to it
2709 if(SrcRect) {
2710 if(SrcRect->top < SrcRect->bottom) {
2711 srect.y1 = SrcRect->top;
2712 srect.y2 = SrcRect->bottom;
2713 upsideDown = FALSE;
2714 } else {
2715 srect.y1 = SrcRect->bottom;
2716 srect.y2 = SrcRect->top;
2717 upsideDown = TRUE;
2719 srect.x1 = SrcRect->left;
2720 srect.x2 = SrcRect->right;
2721 } else {
2722 srect.x1 = 0;
2723 srect.y1 = 0;
2724 srect.x2 = Src->currentDesc.Width;
2725 srect.y2 = Src->currentDesc.Height;
2726 upsideDown = FALSE;
2728 if(rect.x1 > rect.x2) {
2729 UINT tmp = rect.x2;
2730 rect.x2 = rect.x1;
2731 rect.x1 = tmp;
2732 upsideDown = !upsideDown;
2734 if(!srcSwapchain) {
2735 TRACE("Reading from an offscreen target\n");
2736 upsideDown = !upsideDown;
2739 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
2740 stretchx = TRUE;
2741 } else {
2742 stretchx = FALSE;
2745 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
2746 * flip the image nor scale it. If GL_EXT_framebuffer_blit is available it can be used(hopefully,
2747 * not implemented by now). Otherwise:
2749 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
2750 * -> If the app wants a image width an unscaled width, copy it line per line
2751 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
2752 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
2753 * back buffer. This is slower than reading line per line, thus not used for flipping
2754 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
2755 * pixel by pixel
2757 if(FALSE /* GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) */) {
2758 TRACE("Using GL_EXT_framebuffer_blit for copying\n");
2759 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
2760 rect.y2 - rect.y1 > Src->currentDesc.Height) {
2761 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
2762 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown);
2763 } else {
2764 TRACE("Using hardware stretching to flip / stretch the texture\n");
2765 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown);
2768 if(!(This->Flags & SFLAG_DONOTFREE)) {
2769 HeapFree(GetProcessHeap(), 0, This->resource.allocatedMemory);
2770 This->resource.allocatedMemory = NULL;
2771 } else {
2772 This->Flags &= ~SFLAG_INSYSMEM;
2774 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2775 * path is never entered
2777 This->Flags |= SFLAG_INTEXTURE;
2779 return WINED3D_OK;
2780 } else if(Src) {
2781 /* Blit from offscreen surface to render target */
2782 float glTexCoord[4];
2783 DWORD oldCKeyFlags = Src->CKeyFlags;
2784 DDCOLORKEY oldBltCKey = This->SrcBltCKey;
2785 RECT SourceRectangle;
2787 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
2789 if(SrcRect) {
2790 SourceRectangle.left = SrcRect->left;
2791 SourceRectangle.right = SrcRect->right;
2792 SourceRectangle.top = SrcRect->top;
2793 SourceRectangle.bottom = SrcRect->bottom;
2794 } else {
2795 SourceRectangle.left = 0;
2796 SourceRectangle.right = Src->currentDesc.Width;
2797 SourceRectangle.top = 0;
2798 SourceRectangle.bottom = Src->currentDesc.Height;
2801 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
2802 /* Fall back to software */
2803 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
2804 SourceRectangle.left, SourceRectangle.top,
2805 SourceRectangle.right, SourceRectangle.bottom);
2806 return WINED3DERR_INVALIDCALL;
2809 /* Color keying: Check if we have to do a color keyed blt,
2810 * and if not check if a color key is activated.
2812 * Just modify the color keying parameters in the surface and restore them afterwards
2813 * The surface keeps track of the color key last used to load the opengl surface.
2814 * PreLoad will catch the change to the flags and color key and reload if neccessary.
2816 if(Flags & DDBLT_KEYSRC) {
2817 /* Use color key from surface */
2818 } else if(Flags & DDBLT_KEYSRCOVERRIDE) {
2819 /* Use color key from DDBltFx */
2820 Src->CKeyFlags |= DDSD_CKSRCBLT;
2821 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
2822 } else {
2823 /* Do not use color key */
2824 Src->CKeyFlags &= ~DDSD_CKSRCBLT;
2827 /* Now load the surface */
2828 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
2830 ENTER_GL();
2832 /* Activate the destination context, set it up for blitting */
2833 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
2835 if(!dstSwapchain) {
2836 TRACE("Drawing to offscreen buffer\n");
2837 glDrawBuffer(myDevice->offscreenBuffer);
2838 } else if(This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer) {
2839 TRACE("Drawing to front buffer\n");
2840 glDrawBuffer(GL_FRONT);
2841 checkGLcall("glDrawBuffer GL_FRONT");
2842 } else {
2843 TRACE("Drawing to back buffer\n");
2844 glDrawBuffer(GL_BACK);
2845 checkGLcall("glDrawBuffer GL_BACK");
2848 /* Bind the texture */
2849 glBindTexture(GL_TEXTURE_2D, Src->glDescription.textureName);
2850 checkGLcall("glBindTexture");
2852 /* No filtering for blts */
2853 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
2854 GL_NEAREST);
2855 checkGLcall("glTexParameteri");
2856 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
2857 GL_NEAREST);
2858 checkGLcall("glTexParameteri");
2859 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
2860 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
2861 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2862 checkGLcall("glTexEnvi");
2864 /* This is for color keying */
2865 if(Flags & (DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE)) {
2866 glEnable(GL_ALPHA_TEST);
2867 checkGLcall("glEnable GL_ALPHA_TEST");
2868 glAlphaFunc(GL_NOTEQUAL, 0.0);
2869 checkGLcall("glAlphaFunc\n");
2870 } else {
2871 glDisable(GL_ALPHA_TEST);
2872 checkGLcall("glDisable GL_ALPHA_TEST");
2875 /* Draw a textured quad
2877 glBegin(GL_QUADS);
2879 glColor3d(1.0f, 1.0f, 1.0f);
2880 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
2881 glVertex3f(rect.x1,
2882 rect.y1,
2883 0.0);
2885 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
2886 glVertex3f(rect.x1, rect.y2, 0.0);
2888 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
2889 glVertex3f(rect.x2,
2890 rect.y2,
2891 0.0);
2893 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
2894 glVertex3f(rect.x2,
2895 rect.y1,
2896 0.0);
2897 glEnd();
2898 checkGLcall("glEnd");
2900 if(Flags & (DDBLT_KEYSRC | DDBLT_KEYSRCOVERRIDE)) {
2901 glDisable(GL_ALPHA_TEST);
2902 checkGLcall("glDisable(GL_ALPHA_TEST)");
2905 /* Unbind the texture */
2906 glBindTexture(GL_TEXTURE_2D, 0);
2907 checkGLcall("glEnable glBindTexture");
2909 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
2910 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
2912 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
2913 glDrawBuffer(GL_BACK);
2915 /* Restore the color key parameters */
2916 Src->CKeyFlags = oldCKeyFlags;
2917 This->SrcBltCKey = oldBltCKey;
2919 LEAVE_GL();
2921 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
2922 This->Flags &= ~SFLAG_INSYSMEM;
2923 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
2924 * is outdated now
2926 if(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO) {
2927 This->Flags |= SFLAG_INDRAWABLE;
2928 This->Flags &= ~SFLAG_INTEXTURE;
2929 } else {
2930 This->Flags |= SFLAG_INTEXTURE;
2933 return WINED3D_OK;
2934 } else {
2935 /* Source-Less Blit to render target */
2936 if (Flags & DDBLT_COLORFILL) {
2937 /* This is easy to handle for the D3D Device... */
2938 DWORD color;
2940 TRACE("Colorfill\n");
2942 /* The color as given in the Blt function is in the format of the frame-buffer...
2943 * 'clear' expect it in ARGB format => we need to do some conversion :-)
2945 if (This->resource.format == WINED3DFMT_P8) {
2946 if (This->palette) {
2947 color = ((0xFF000000) |
2948 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
2949 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
2950 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
2951 } else {
2952 color = 0xFF000000;
2955 else if (This->resource.format == WINED3DFMT_R5G6B5) {
2956 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
2957 color = 0xFFFFFFFF;
2958 } else {
2959 color = ((0xFF000000) |
2960 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
2961 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
2962 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
2965 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
2966 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
2967 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
2969 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
2970 color = DDBltFx->u5.dwFillColor;
2972 else {
2973 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
2974 return WINED3DERR_INVALIDCALL;
2977 TRACE("Calling GetSwapChain with mydevice = %p\n", myDevice);
2978 if(dstSwapchain && dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]) {
2979 glDrawBuffer(GL_BACK);
2980 checkGLcall("glDrawBuffer(GL_BACK)");
2981 } else if (dstSwapchain && This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer) {
2982 glDrawBuffer(GL_FRONT);
2983 checkGLcall("glDrawBuffer(GL_FRONT)");
2984 } else if(This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2985 glDrawBuffer(myDevice->offscreenBuffer);
2986 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer3)");
2987 } else {
2988 TRACE("Surface is higher back buffer, falling back to software\n");
2989 return WINED3DERR_INVALIDCALL;
2992 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
2994 IWineD3DDevice_Clear( (IWineD3DDevice *) myDevice,
2995 1 /* Number of rectangles */,
2996 &rect,
2997 WINED3DCLEAR_TARGET,
2998 color,
2999 0.0 /* Z */,
3000 0 /* Stencil */);
3002 /* Restore the original draw buffer */
3003 if(!dstSwapchain) {
3004 glDrawBuffer(myDevice->offscreenBuffer);
3005 } else if(dstSwapchain->backBuffer && dstSwapchain->backBuffer[0]) {
3006 glDrawBuffer(GL_BACK);
3008 vcheckGLcall("glDrawBuffer");
3010 return WINED3D_OK;
3014 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3015 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3016 return WINED3DERR_INVALIDCALL;
3019 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, DDBLTFX *DDBltFx) {
3020 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3021 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3022 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3023 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3024 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3026 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair */
3027 if(myDevice->inScene &&
3028 (iface == myDevice->stencilBufferTarget ||
3029 (SrcSurface && SrcSurface == myDevice->stencilBufferTarget))) {
3030 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3031 return WINED3DERR_INVALIDCALL;
3034 /* Special cases for RenderTargets */
3035 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3036 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3037 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) return WINED3D_OK;
3040 /* For the rest call the X11 surface implementation.
3041 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3042 * other Blts are rather rare
3044 return IWineGDISurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3047 HRESULT WINAPI IWineD3DSurfaceImpl_GetBltStatus(IWineD3DSurface *iface, DWORD Flags) {
3048 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3049 TRACE("(%p)->(%x)\n", This, Flags);
3051 switch (Flags)
3053 case DDGBS_CANBLT:
3054 case DDGBS_ISBLTDONE:
3055 return DD_OK;
3057 default:
3058 return DDERR_INVALIDPARAMS;
3062 HRESULT WINAPI IWineD3DSurfaceImpl_GetFlipStatus(IWineD3DSurface *iface, DWORD Flags) {
3063 /* XXX: DDERR_INVALIDSURFACETYPE */
3065 TRACE("(%p)->(%08x)\n",iface,Flags);
3066 switch (Flags) {
3067 case DDGFS_CANFLIP:
3068 case DDGFS_ISFLIPDONE:
3069 return DD_OK;
3071 default:
3072 return DDERR_INVALIDPARAMS;
3076 HRESULT WINAPI IWineD3DSurfaceImpl_IsLost(IWineD3DSurface *iface) {
3077 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3078 TRACE("(%p)\n", This);
3080 return This->Flags & SFLAG_LOST ? DDERR_SURFACELOST : WINED3D_OK;
3083 HRESULT WINAPI IWineD3DSurfaceImpl_Restore(IWineD3DSurface *iface) {
3084 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3085 TRACE("(%p)\n", This);
3087 /* So far we don't lose anything :) */
3088 This->Flags &= ~SFLAG_LOST;
3089 return WINED3D_OK;
3092 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3093 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3094 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3095 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3096 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3098 if(myDevice->inScene &&
3099 (iface == myDevice->stencilBufferTarget ||
3100 (Source && Source == myDevice->stencilBufferTarget))) {
3101 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3102 return WINED3DERR_INVALIDCALL;
3105 /* Special cases for RenderTargets */
3106 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3107 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3109 RECT SrcRect, DstRect;
3110 DWORD Flags=0;
3112 if(rsrc) {
3113 SrcRect.left = rsrc->left;
3114 SrcRect.top= rsrc->top;
3115 SrcRect.bottom = rsrc->bottom;
3116 SrcRect.right = rsrc->right;
3117 } else {
3118 SrcRect.left = 0;
3119 SrcRect.top = 0;
3120 SrcRect.right = srcImpl->currentDesc.Width;
3121 SrcRect.bottom = srcImpl->currentDesc.Height;
3124 DstRect.left = dstx;
3125 DstRect.top=dsty;
3126 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3127 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3129 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3130 if(trans & DDBLTFAST_SRCCOLORKEY)
3131 Flags |= DDBLT_KEYSRC;
3132 if(trans & DDBLTFAST_DESTCOLORKEY)
3133 Flags |= DDBLT_KEYDEST;
3134 if(trans & DDBLTFAST_WAIT)
3135 Flags |= DDBLT_WAIT;
3136 if(trans & DDBLTFAST_DONOTWAIT)
3137 Flags |= DDBLT_DONOTWAIT;
3139 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL) == WINED3D_OK) return WINED3D_OK;
3143 return IWineGDISurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3146 HRESULT WINAPI IWineD3DSurfaceImpl_GetPalette(IWineD3DSurface *iface, IWineD3DPalette **Pal) {
3147 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3148 TRACE("(%p)->(%p)\n", This, Pal);
3150 *Pal = (IWineD3DPalette *) This->palette;
3151 return DD_OK;
3154 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3155 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3156 RGBQUAD col[256];
3157 IWineD3DPaletteImpl *pal = This->palette;
3158 unsigned int n;
3159 TRACE("(%p)\n", This);
3161 if(This->resource.format == WINED3DFMT_P8 ||
3162 This->resource.format == WINED3DFMT_A8P8)
3164 if(!This->Flags & SFLAG_INSYSMEM) {
3165 FIXME("Palette changed with surface that does not have an up to date system memory copy\n");
3167 TRACE("Dirtifying surface\n");
3168 This->Flags &= ~(SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3171 if(This->Flags & SFLAG_DIBSECTION) {
3172 TRACE("(%p): Updating the hdc's palette\n", This);
3173 for (n=0; n<256; n++) {
3174 if(pal) {
3175 col[n].rgbRed = pal->palents[n].peRed;
3176 col[n].rgbGreen = pal->palents[n].peGreen;
3177 col[n].rgbBlue = pal->palents[n].peBlue;
3178 } else {
3179 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3180 /* Use the default device palette */
3181 col[n].rgbRed = device->palettes[device->currentPalette][n].peRed;
3182 col[n].rgbGreen = device->palettes[device->currentPalette][n].peGreen;
3183 col[n].rgbBlue = device->palettes[device->currentPalette][n].peBlue;
3185 col[n].rgbReserved = 0;
3187 SetDIBColorTable(This->hDC, 0, 256, col);
3190 return WINED3D_OK;
3193 HRESULT WINAPI IWineD3DSurfaceImpl_SetPalette(IWineD3DSurface *iface, IWineD3DPalette *Pal) {
3194 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3195 IWineD3DPaletteImpl *PalImpl = (IWineD3DPaletteImpl *) Pal;
3196 TRACE("(%p)->(%p)\n", This, Pal);
3198 if(This->palette != NULL)
3199 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3200 This->palette->Flags &= ~DDPCAPS_PRIMARYSURFACE;
3202 if(PalImpl != NULL) {
3203 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3204 /* Set the device's main palette if the palette
3205 * wasn't a primary palette before
3207 if(!(PalImpl->Flags & DDPCAPS_PRIMARYSURFACE)) {
3208 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3209 unsigned int i;
3211 for(i=0; i < 256; i++) {
3212 device->palettes[device->currentPalette][i] = PalImpl->palents[i];
3216 (PalImpl)->Flags |= DDPCAPS_PRIMARYSURFACE;
3219 This->palette = PalImpl;
3221 return IWineD3DSurface_RealizePalette(iface);
3224 HRESULT WINAPI IWineD3DSurfaceImpl_SetColorKey(IWineD3DSurface *iface, DWORD Flags, DDCOLORKEY *CKey) {
3225 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3226 TRACE("(%p)->(%08x,%p)\n", This, Flags, CKey);
3228 if ((Flags & DDCKEY_COLORSPACE) != 0) {
3229 FIXME(" colorkey value not supported (%08x) !\n", Flags);
3230 return DDERR_INVALIDPARAMS;
3233 /* Dirtify the surface, but only if a key was changed */
3234 if(CKey) {
3235 switch (Flags & ~DDCKEY_COLORSPACE) {
3236 case DDCKEY_DESTBLT:
3237 This->DestBltCKey = *CKey;
3238 This->CKeyFlags |= DDSD_CKDESTBLT;
3239 break;
3241 case DDCKEY_DESTOVERLAY:
3242 This->DestOverlayCKey = *CKey;
3243 This->CKeyFlags |= DDSD_CKDESTOVERLAY;
3244 break;
3246 case DDCKEY_SRCOVERLAY:
3247 This->SrcOverlayCKey = *CKey;
3248 This->CKeyFlags |= DDSD_CKSRCOVERLAY;
3249 break;
3251 case DDCKEY_SRCBLT:
3252 This->SrcBltCKey = *CKey;
3253 This->CKeyFlags |= DDSD_CKSRCBLT;
3254 break;
3257 else {
3258 switch (Flags & ~DDCKEY_COLORSPACE) {
3259 case DDCKEY_DESTBLT:
3260 This->CKeyFlags &= ~DDSD_CKDESTBLT;
3261 break;
3263 case DDCKEY_DESTOVERLAY:
3264 This->CKeyFlags &= ~DDSD_CKDESTOVERLAY;
3265 break;
3267 case DDCKEY_SRCOVERLAY:
3268 This->CKeyFlags &= ~DDSD_CKSRCOVERLAY;
3269 break;
3271 case DDCKEY_SRCBLT:
3272 This->CKeyFlags &= ~DDSD_CKSRCBLT;
3273 break;
3277 return WINED3D_OK;
3280 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3281 /** Check against the maximum texture sizes supported by the video card **/
3282 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3284 TRACE("%p\n", This);
3285 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3286 /* one of three options
3287 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)
3288 2: Set the texture to the maxium size (bad idea)
3289 3: WARN and return WINED3DERR_NOTAVAILABLE;
3290 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.
3292 WARN("(%p) Creating an oversized surface\n", This);
3293 This->Flags |= SFLAG_OVERSIZE;
3295 /* This will be initialized on the first blt */
3296 This->glRect.left = 0;
3297 This->glRect.top = 0;
3298 This->glRect.right = 0;
3299 This->glRect.bottom = 0;
3300 } else {
3301 /* No oversize, gl rect is the full texture size */
3302 This->Flags &= ~SFLAG_OVERSIZE;
3303 This->glRect.left = 0;
3304 This->glRect.top = 0;
3305 This->glRect.right = This->pow2Width;
3306 This->glRect.bottom = This->pow2Height;
3309 if(GL_SUPPORT(APPLE_CLIENT_STORAGE) && This->resource.allocatedMemory == NULL) {
3310 /* Make sure that memory is allocated from the start if we are going to use GL_APPLE_client_storage.
3311 * Otherwise a glTexImage2D with a NULL pointer may be done, e.g. when blitting or with offscreen render
3312 * targets, thus the client storage wouldn't be used for that texture
3314 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size + 4);
3316 return WINED3D_OK;
3319 DWORD WINAPI IWineD3DSurfaceImpl_GetPitch(IWineD3DSurface *iface) {
3320 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3321 DWORD ret;
3322 TRACE("(%p)\n", This);
3324 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
3325 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt2/3/4/5)
3326 ie pitch = (width/4) * bytes per block */
3327 if (This->resource.format == WINED3DFMT_DXT1) /* DXT1 is 8 bytes per block */
3328 ret = ((This->currentDesc.Width + 3) >> 2) << 3;
3329 else if (This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
3330 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) /* DXT2/3/4/5 is 16 bytes per block */
3331 ret = ((This->currentDesc.Width + 3) >> 2) << 4;
3332 else {
3333 ret = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
3334 /* Surfaces are 32 bit aligned */
3335 ret = (ret + SURFACE_ALIGNMENT - 1) & ~(SURFACE_ALIGNMENT - 1);
3337 TRACE("(%p) Returning %d\n", This, ret);
3338 return ret;
3341 HRESULT WINAPI IWineD3DSurfaceImpl_SetOverlayPosition(IWineD3DSurface *iface, LONG X, LONG Y) {
3342 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3344 FIXME("(%p)->(%d,%d) Stub!\n", This, X, Y);
3346 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
3348 TRACE("(%p): Not an overlay surface\n", This);
3349 return DDERR_NOTAOVERLAYSURFACE;
3352 return WINED3D_OK;
3355 HRESULT WINAPI IWineD3DSurfaceImpl_GetOverlayPosition(IWineD3DSurface *iface, LONG *X, LONG *Y) {
3356 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3358 FIXME("(%p)->(%p,%p) Stub!\n", This, X, Y);
3360 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
3362 TRACE("(%p): Not an overlay surface\n", This);
3363 return DDERR_NOTAOVERLAYSURFACE;
3366 return WINED3D_OK;
3369 HRESULT WINAPI IWineD3DSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface *iface, DWORD Flags, IWineD3DSurface *Ref) {
3370 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3371 IWineD3DSurfaceImpl *RefImpl = (IWineD3DSurfaceImpl *) Ref;
3373 FIXME("(%p)->(%08x,%p) Stub!\n", This, Flags, RefImpl);
3375 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
3377 TRACE("(%p): Not an overlay surface\n", This);
3378 return DDERR_NOTAOVERLAYSURFACE;
3381 return WINED3D_OK;
3384 HRESULT WINAPI IWineD3DSurfaceImpl_UpdateOverlay(IWineD3DSurface *iface, RECT *SrcRect, IWineD3DSurface *DstSurface, RECT *DstRect, DWORD Flags, WINEDDOVERLAYFX *FX) {
3385 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3386 IWineD3DSurfaceImpl *Dst = (IWineD3DSurfaceImpl *) DstSurface;
3387 FIXME("(%p)->(%p, %p, %p, %08x, %p)\n", This, SrcRect, Dst, DstRect, Flags, FX);
3389 if(!(This->resource.usage & WINED3DUSAGE_OVERLAY))
3391 TRACE("(%p): Not an overlay surface\n", This);
3392 return DDERR_NOTAOVERLAYSURFACE;
3395 return WINED3D_OK;
3398 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
3400 /* IUnknown */
3401 IWineD3DSurfaceImpl_QueryInterface,
3402 IWineD3DSurfaceImpl_AddRef,
3403 IWineD3DSurfaceImpl_Release,
3404 /* IWineD3DResource */
3405 IWineD3DSurfaceImpl_GetParent,
3406 IWineD3DSurfaceImpl_GetDevice,
3407 IWineD3DSurfaceImpl_SetPrivateData,
3408 IWineD3DSurfaceImpl_GetPrivateData,
3409 IWineD3DSurfaceImpl_FreePrivateData,
3410 IWineD3DSurfaceImpl_SetPriority,
3411 IWineD3DSurfaceImpl_GetPriority,
3412 IWineD3DSurfaceImpl_PreLoad,
3413 IWineD3DSurfaceImpl_GetType,
3414 /* IWineD3DSurface */
3415 IWineD3DSurfaceImpl_GetContainer,
3416 IWineD3DSurfaceImpl_GetDesc,
3417 IWineD3DSurfaceImpl_LockRect,
3418 IWineD3DSurfaceImpl_UnlockRect,
3419 IWineD3DSurfaceImpl_GetDC,
3420 IWineD3DSurfaceImpl_ReleaseDC,
3421 IWineD3DSurfaceImpl_Flip,
3422 IWineD3DSurfaceImpl_Blt,
3423 IWineD3DSurfaceImpl_GetBltStatus,
3424 IWineD3DSurfaceImpl_GetFlipStatus,
3425 IWineD3DSurfaceImpl_IsLost,
3426 IWineD3DSurfaceImpl_Restore,
3427 IWineD3DSurfaceImpl_BltFast,
3428 IWineD3DSurfaceImpl_GetPalette,
3429 IWineD3DSurfaceImpl_SetPalette,
3430 IWineD3DSurfaceImpl_RealizePalette,
3431 IWineD3DSurfaceImpl_SetColorKey,
3432 IWineD3DSurfaceImpl_GetPitch,
3433 IWineD3DSurfaceImpl_SetMem,
3434 IWineD3DSurfaceImpl_SetOverlayPosition,
3435 IWineD3DSurfaceImpl_GetOverlayPosition,
3436 IWineD3DSurfaceImpl_UpdateOverlayZOrder,
3437 IWineD3DSurfaceImpl_UpdateOverlay,
3438 /* Internal use: */
3439 IWineD3DSurfaceImpl_AddDirtyRect,
3440 IWineD3DSurfaceImpl_LoadTexture,
3441 IWineD3DSurfaceImpl_SaveSnapshot,
3442 IWineD3DSurfaceImpl_SetContainer,
3443 IWineD3DSurfaceImpl_SetGlTextureDesc,
3444 IWineD3DSurfaceImpl_GetGlDesc,
3445 IWineD3DSurfaceImpl_GetData,
3446 IWineD3DSurfaceImpl_SetFormat,
3447 IWineD3DSurfaceImpl_PrivateSetup