2 * IWineD3DSurface Implementation
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11 * Copyright 2007 Henri Verbeet
12 * Copyright 2006-2008 Roderick Colenbrander
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
36 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
37 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey);
38 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This);
39 static void surface_remove_pbo(IWineD3DSurfaceImpl *This);
41 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This) {
44 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
45 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
46 * gl states. The current texture unit should always be a valid one.
48 * TODO: Track the current active texture per GL context instead of using glGet
50 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
52 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
54 active_texture -= GL_TEXTURE0_ARB;
58 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_texture));
59 IWineD3DSurface_BindTexture((IWineD3DSurface *)This);
62 /* This function checks if the primary render target uses the 8bit paletted format. */
63 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
65 if (device->render_targets && device->render_targets[0]) {
66 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
67 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
73 /* This call just downloads data, the caller is responsible for activating the
74 * right context and binding the correct texture. */
75 static void surface_download_data(IWineD3DSurfaceImpl *This) {
76 if (0 == This->glDescription.textureName) {
77 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
81 /* Only support read back of converted P8 surfaces */
82 if(This->Flags & SFLAG_CONVERTED && (This->resource.format != WINED3DFMT_P8)) {
83 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
89 if (This->resource.format == WINED3DFMT_DXT1 ||
90 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
91 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
92 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
93 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
95 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
96 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
98 if(This->Flags & SFLAG_PBO) {
99 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
100 checkGLcall("glBindBufferARB");
101 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
102 checkGLcall("glGetCompressedTexImageARB()");
103 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
104 checkGLcall("glBindBufferARB");
106 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
107 checkGLcall("glGetCompressedTexImageARB()");
113 GLenum format = This->glDescription.glFormat;
114 GLenum type = This->glDescription.glType;
118 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
119 if(This->resource.format == WINED3DFMT_P8) {
121 type = GL_UNSIGNED_BYTE;
124 if (This->Flags & SFLAG_NONPOW2) {
125 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
126 src_pitch = This->bytesPerPixel * This->pow2Width;
127 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
128 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
129 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
131 mem = This->resource.allocatedMemory;
134 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
137 if(This->Flags & SFLAG_PBO) {
138 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
139 checkGLcall("glBindBufferARB");
141 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
143 checkGLcall("glGetTexImage()");
145 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
146 checkGLcall("glBindBufferARB");
148 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
150 checkGLcall("glGetTexImage()");
154 if (This->Flags & SFLAG_NONPOW2) {
155 LPBYTE src_data, dst_data;
158 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
159 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
160 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
162 * We're doing this...
164 * instead of boxing the texture :
165 * |<-texture width ->| -->pow2width| /\
166 * |111111111111111111| | |
167 * |222 Texture 222222| boxed empty | texture height
168 * |3333 Data 33333333| | |
169 * |444444444444444444| | \/
170 * ----------------------------------- |
171 * | boxed empty | boxed empty | pow2height
173 * -----------------------------------
176 * we're repacking the data to the expected texture width
178 * |<-texture width ->| -->pow2width| /\
179 * |111111111111111111222222222222222| |
180 * |222333333333333333333444444444444| texture height
184 * | empty | pow2height
186 * -----------------------------------
190 * |<-texture width ->| /\
191 * |111111111111111111|
192 * |222222222222222222|texture height
193 * |333333333333333333|
194 * |444444444444444444| \/
195 * --------------------
197 * this also means that any references to allocatedMemory should work with the data as if were a
198 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
200 * internally the texture is still stored in a boxed format so any references to textureName will
201 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
203 * Performance should not be an issue, because applications normally do not lock the surfaces when
204 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
205 * and doesn't have to be re-read.
208 dst_data = This->resource.allocatedMemory;
209 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
210 for (y = 1 ; y < This->currentDesc.Height; y++) {
211 /* skip the first row */
212 src_data += src_pitch;
213 dst_data += dst_pitch;
214 memcpy(dst_data, src_data, dst_pitch);
217 HeapFree(GetProcessHeap(), 0, mem);
221 /* Surface has now been downloaded */
222 This->Flags |= SFLAG_INSYSMEM;
225 /* This call just uploads data, the caller is responsible for activating the
226 * right context and binding the correct texture. */
227 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
228 if (This->resource.format == WINED3DFMT_DXT1 ||
229 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
230 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
231 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
232 FIXME("Using DXT1/3/5 without advertized support\n");
234 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
235 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
236 * function uses glCompressedTexImage2D instead of the SubImage call
238 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
241 if(This->Flags & SFLAG_PBO) {
242 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
243 checkGLcall("glBindBufferARB");
244 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
246 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
247 width, height, 0 /* border */, This->resource.size, NULL));
248 checkGLcall("glCompressedTexSubImage2D");
250 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
251 checkGLcall("glBindBufferARB");
253 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
254 width, height, 0 /* border */, This->resource.size, data));
255 checkGLcall("glCompressedTexSubImage2D");
260 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
263 if(This->Flags & SFLAG_PBO) {
264 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
265 checkGLcall("glBindBufferARB");
266 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
268 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
269 checkGLcall("glTexSubImage2D");
271 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
272 checkGLcall("glBindBufferARB");
275 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
276 checkGLcall("glTexSubImage2D");
283 /* This call just allocates the texture, the caller is responsible for
284 * activating the right context and binding the correct texture. */
285 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
286 BOOL enable_client_storage = FALSE;
289 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,
290 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
292 if (This->resource.format == WINED3DFMT_DXT1 ||
293 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
294 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
295 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
296 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
298 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
299 * once, unfortunately
301 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
302 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
303 This->Flags |= SFLAG_CLIENT;
304 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
306 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
307 width, height, 0 /* border */, This->resource.size, mem));
316 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
317 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
318 /* In some cases we want to disable client storage.
319 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
320 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
321 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
322 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
323 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
325 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
326 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
327 This->Flags &= ~SFLAG_CLIENT;
328 enable_client_storage = TRUE;
330 This->Flags |= SFLAG_CLIENT;
332 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
333 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
335 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
338 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
339 checkGLcall("glTexImage2D");
341 if(enable_client_storage) {
342 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
343 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
347 This->Flags |= SFLAG_ALLOCATED;
350 /* In D3D the depth stencil dimensions have to be greater than or equal to the
351 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
352 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
353 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
354 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
355 renderbuffer_entry_t *entry;
356 GLuint renderbuffer = 0;
357 unsigned int src_width, src_height;
359 src_width = This->pow2Width;
360 src_height = This->pow2Height;
362 /* A depth stencil smaller than the render target is not valid */
363 if (width > src_width || height > src_height) return;
365 /* Remove any renderbuffer set if the sizes match */
366 if (width == src_width && height == src_height) {
367 This->current_renderbuffer = NULL;
371 /* Look if we've already got a renderbuffer of the correct dimensions */
372 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
373 if (entry->width == width && entry->height == height) {
374 renderbuffer = entry->id;
375 This->current_renderbuffer = entry;
381 const GlPixelFormatDesc *glDesc;
382 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
384 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
385 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
386 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
388 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
389 entry->width = width;
390 entry->height = height;
391 entry->id = renderbuffer;
392 list_add_head(&This->renderbuffers, &entry->entry);
394 This->current_renderbuffer = entry;
397 checkGLcall("set_compatible_renderbuffer");
400 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
401 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
402 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
404 TRACE("(%p) : swapchain %p\n", This, swapchain);
406 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
407 TRACE("Returning GL_BACK\n");
409 } else if (swapchain_impl->frontBuffer == iface) {
410 TRACE("Returning GL_FRONT\n");
414 FIXME("Higher back buffer, returning GL_BACK\n");
418 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
419 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
420 ULONG ref = InterlockedDecrement(&This->resource.ref);
421 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
423 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
424 renderbuffer_entry_t *entry, *entry2;
425 TRACE("(%p) : cleaning up\n", This);
427 /* Need a context to destroy the texture. Use the currently active render target, but only if
428 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
429 * When destroying the primary rt, Uninit3D will activate a context before doing anything
431 if(device->render_targets && device->render_targets[0]) {
432 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
436 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
437 TRACE("Deleting texture %d\n", This->glDescription.textureName);
438 glDeleteTextures(1, &This->glDescription.textureName);
441 if(This->Flags & SFLAG_PBO) {
443 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
446 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
447 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
448 HeapFree(GetProcessHeap(), 0, entry);
452 if(This->Flags & SFLAG_DIBSECTION) {
454 SelectObject(This->hDC, This->dib.holdbitmap);
456 /* Release the DIB section */
457 DeleteObject(This->dib.DIBsection);
458 This->dib.bitmap_data = NULL;
459 This->resource.allocatedMemory = NULL;
461 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
463 HeapFree(GetProcessHeap(), 0, This->palette9);
465 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
466 if(iface == device->ddraw_primary)
467 device->ddraw_primary = NULL;
469 TRACE("(%p) Released\n", This);
470 HeapFree(GetProcessHeap(), 0, This);
476 /* ****************************************************
477 IWineD3DSurface IWineD3DResource parts follow
478 **************************************************** */
480 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
481 /* TODO: check for locks */
482 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
483 IWineD3DBaseTexture *baseTexture = NULL;
484 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
486 TRACE("(%p)Checking to see if the container is a base texture\n", This);
487 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
488 TRACE("Passing to container\n");
489 IWineD3DBaseTexture_PreLoad(baseTexture);
490 IWineD3DBaseTexture_Release(baseTexture);
492 TRACE("(%p) : About to load surface\n", This);
494 if(!device->isInDraw) {
495 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
498 if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
499 if(palette9_changed(This)) {
500 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
501 /* TODO: This is not necessarily needed with hw palettized texture support */
502 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
503 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
504 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
508 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
509 if (!This->glDescription.level) {
510 if (!This->glDescription.textureName) {
511 glGenTextures(1, &This->glDescription.textureName);
512 checkGLcall("glGenTextures");
513 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
515 glBindTexture(This->glDescription.target, This->glDescription.textureName);
516 checkGLcall("glBindTexture");
518 IWineD3DSurface_LoadTexture(iface, FALSE);
519 /* This is where we should be reducing the amount of GLMemoryUsed */
520 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
521 /* assume this is a coding error not a real error for now */
522 FIXME("Mipmap surface has a glTexture bound to it!\n");
525 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
526 /* Tell opengl to try and keep this texture in video ram (well mostly) */
530 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
537 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
538 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
539 This->resource.allocatedMemory =
540 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
543 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
544 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
545 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
546 checkGLcall("glGetBufferSubData");
547 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
548 checkGLcall("glDeleteBuffers");
552 This->Flags &= ~SFLAG_PBO;
555 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
556 IWineD3DBaseTexture *texture = NULL;
557 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
558 renderbuffer_entry_t *entry, *entry2;
559 TRACE("(%p)\n", iface);
561 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
562 /* Default pool resources are supposed to be destroyed before Reset is called.
563 * Implicit resources stay however. So this means we have an implicit render target
564 * or depth stencil. The content may be destroyed, but we still have to tear down
565 * opengl resources, so we cannot leave early.
567 * Put the most up to date surface location into the drawable. D3D-wise this content
568 * is undefined, so it would be nowhere, but that would make the location management
569 * more complicated. The drawable is a sane location, because if we mark sysmem or
570 * texture up to date, drawPrim will copy the uninitialized texture or sysmem to the
571 * uninitialized drawable. That's pointless and we'd have to allocate the texture /
574 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, TRUE);
576 /* Load the surface into system memory */
577 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
578 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
580 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
581 This->Flags &= ~SFLAG_ALLOCATED;
583 /* Destroy PBOs, but load them into real sysmem before */
584 if(This->Flags & SFLAG_PBO) {
585 surface_remove_pbo(This);
588 /* Destroy fbo render buffers. This is needed for implicit render targets, for
589 * all application-created targets the application has to release the surface
590 * before calling _Reset
592 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
594 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
596 list_remove(&entry->entry);
597 HeapFree(GetProcessHeap(), 0, entry);
599 list_init(&This->renderbuffers);
600 This->current_renderbuffer = NULL;
602 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
605 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
608 glDeleteTextures(1, &This->glDescription.textureName);
609 This->glDescription.textureName = 0;
612 IWineD3DBaseTexture_Release(texture);
617 /* ******************************************************
618 IWineD3DSurface IWineD3DSurface parts follow
619 ****************************************************** */
621 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
622 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
623 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
624 if (This->glDescription.textureName == 0 && textureName != 0) {
625 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
626 IWineD3DSurface_AddDirtyRect(iface, NULL);
628 This->glDescription.textureName = textureName;
629 This->glDescription.target = target;
630 This->Flags &= ~SFLAG_ALLOCATED;
633 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
634 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
635 TRACE("(%p) : returning %p\n", This, &This->glDescription);
636 *glDescription = &This->glDescription;
639 /* TODO: think about moving this down to resource? */
640 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
641 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
642 /* This should only be called for sysmem textures, it may be a good idea to extend this to all pools at some point in the future */
643 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
644 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
646 return (CONST void*)(This->resource.allocatedMemory);
649 /* Read the framebuffer back into the surface */
650 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
651 IWineD3DSwapChainImpl *swapchain;
652 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
656 BYTE *row, *top, *bottom;
660 BOOL srcIsUpsideDown;
662 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
663 static BOOL warned = FALSE;
665 ERR("The application tries to lock the render target, but render target locking is disabled\n");
671 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
672 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
673 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
674 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
675 * context->last_was_blit set on the unlock.
677 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
680 /* Select the correct read buffer, and give some debug output.
681 * There is no need to keep track of the current read buffer or reset it, every part of the code
682 * that reads sets the read buffer as desired.
685 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
686 * Read from the back buffer
688 TRACE("Locking offscreen render target\n");
689 glReadBuffer(myDevice->offscreenBuffer);
690 srcIsUpsideDown = TRUE;
692 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
693 TRACE("Locking %#x buffer\n", buffer);
694 glReadBuffer(buffer);
695 checkGLcall("glReadBuffer");
697 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
698 srcIsUpsideDown = FALSE;
701 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
705 local_rect.right = This->currentDesc.Width;
706 local_rect.bottom = This->currentDesc.Height;
710 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
712 switch(This->resource.format)
716 if(primary_render_target_is_p8(myDevice)) {
717 /* In case of P8 render targets the index is stored in the alpha component */
719 type = GL_UNSIGNED_BYTE;
721 bpp = This->bytesPerPixel;
723 /* GL can't return palettized data, so read ARGB pixels into a
724 * separate block of memory and convert them into palettized format
725 * in software. Slow, but if the app means to use palettized render
726 * targets and locks it...
728 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
729 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
730 * for the color channels when palettizing the colors.
733 type = GL_UNSIGNED_BYTE;
735 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
737 ERR("Out of memory\n");
741 bpp = This->bytesPerPixel * 3;
748 fmt = This->glDescription.glFormat;
749 type = This->glDescription.glType;
750 bpp = This->bytesPerPixel;
753 if(This->Flags & SFLAG_PBO) {
754 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
755 checkGLcall("glBindBufferARB");
758 glReadPixels(local_rect.left, local_rect.top,
759 local_rect.right - local_rect.left,
760 local_rect.bottom - local_rect.top,
762 vcheckGLcall("glReadPixels");
764 if(This->Flags & SFLAG_PBO) {
765 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
766 checkGLcall("glBindBufferARB");
768 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
769 * to get a pointer to it and perform the flipping in software. This is a lot
770 * faster than calling glReadPixels for each line. In case we want more speed
771 * we should rerender it flipped in a FBO and read the data back from the FBO. */
772 if(!srcIsUpsideDown) {
773 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
774 checkGLcall("glBindBufferARB");
776 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
777 checkGLcall("glMapBufferARB");
781 /* TODO: Merge this with the palettization loop below for P8 targets */
782 if(!srcIsUpsideDown) {
784 /* glReadPixels returns the image upside down, and there is no way to prevent this.
785 Flip the lines in software */
786 len = (local_rect.right - local_rect.left) * bpp;
787 off = local_rect.left * bpp;
789 row = HeapAlloc(GetProcessHeap(), 0, len);
791 ERR("Out of memory\n");
792 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
797 top = mem + pitch * local_rect.top;
798 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
799 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
800 memcpy(row, top + off, len);
801 memcpy(top + off, bottom + off, len);
802 memcpy(bottom + off, row, len);
806 HeapFree(GetProcessHeap(), 0, row);
808 /* Unmap the temp PBO buffer */
809 if(This->Flags & SFLAG_PBO) {
810 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
811 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
817 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
818 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
819 * the same color but we have no choice.
820 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
822 if((This->resource.format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice)) {
823 PALETTEENTRY *pal = NULL;
824 DWORD width = pitch / 3;
828 pal = This->palette->palents;
830 ERR("Palette is missing, cannot perform inverse palette lookup\n");
831 HeapFree(GetProcessHeap(), 0, mem);
835 for(y = local_rect.top; y < local_rect.bottom; y++) {
836 for(x = local_rect.left; x < local_rect.right; x++) {
837 /* start lines pixels */
838 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
839 BYTE *green = blue + 1;
840 BYTE *red = green + 1;
842 for(c = 0; c < 256; c++) {
843 if(*red == pal[c].peRed &&
844 *green == pal[c].peGreen &&
845 *blue == pal[c].peBlue)
847 *((BYTE *) dest + y * width + x) = c;
853 HeapFree(GetProcessHeap(), 0, mem);
857 /* Read the framebuffer contents into a texture */
858 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
860 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
861 IWineD3DSwapChainImpl *swapchain;
863 GLenum format, internal, type;
864 CONVERT_TYPES convert;
865 BOOL srcIsUpsideDown;
868 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
870 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
871 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
872 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
873 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
874 * context->last_was_blit set on the unlock.
876 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
877 surface_bind_and_dirtify(This);
880 glGetIntegerv(GL_READ_BUFFER, &prevRead);
882 /* Select the correct read buffer, and give some debug output.
883 * There is no need to keep track of the current read buffer or reset it, every part of the code
884 * that reads sets the read buffer as desired.
887 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
888 * Read from the back buffer
890 TRACE("Locking offscreen render target\n");
891 glReadBuffer(device->offscreenBuffer);
892 srcIsUpsideDown = TRUE;
894 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
895 TRACE("Locking %#x buffer\n", buffer);
896 glReadBuffer(buffer);
897 checkGLcall("glReadBuffer");
899 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
900 srcIsUpsideDown = FALSE;
903 if(!(This->Flags & SFLAG_ALLOCATED)) {
904 surface_allocate_surface(This, internal, This->pow2Width,
905 This->pow2Height, format, type);
908 clear_unused_channels(This);
910 /* If !SrcIsUpsideDown we should flip the surface.
911 * This can be done using glCopyTexSubImage2D but this
912 * is VERY slow, so don't do that. We should prevent
913 * this code from getting called in such cases or perhaps
916 glCopyTexSubImage2D(This->glDescription.target,
917 This->glDescription.level,
919 This->currentDesc.Width,
920 This->currentDesc.Height);
921 checkGLcall("glCopyTexSubImage2D");
923 glReadBuffer(prevRead);
924 vcheckGLcall("glReadBuffer");
927 TRACE("Updated target %d\n", This->glDescription.target);
930 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
931 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
932 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
935 if(!(This->Flags & SFLAG_DYNLOCK)) {
937 /* MAXLOCKCOUNT is defined in wined3d_private.h */
938 if(This->lockCount > MAXLOCKCOUNT) {
939 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
940 This->Flags |= SFLAG_DYNLOCK;
944 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
945 * Also don't create a PBO for systemmem surfaces.
947 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
951 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
952 error = glGetError();
953 if(This->pbo == 0 || error != GL_NO_ERROR) {
954 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
957 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
959 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
960 checkGLcall("glBindBufferARB");
962 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
963 checkGLcall("glBufferDataARB");
965 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
966 checkGLcall("glBindBufferARB");
968 /* We don't need the system memory anymore and we can't even use it for PBOs */
969 if(!(This->Flags & SFLAG_CLIENT)) {
970 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
971 This->resource.heapMemory = NULL;
973 This->resource.allocatedMemory = NULL;
974 This->Flags |= SFLAG_PBO;
976 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
977 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
980 if(!This->resource.heapMemory) {
981 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
983 This->resource.allocatedMemory =
984 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
985 if(This->Flags & SFLAG_INSYSMEM) {
986 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
991 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
992 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
993 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
994 IWineD3DSwapChain *swapchain = NULL;
996 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
998 /* This is also done in the base class, but we have to verify this before loading any data from
999 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1000 * may interfere, and all other bad things may happen
1002 if (This->Flags & SFLAG_LOCKED) {
1003 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1004 return WINED3DERR_INVALIDCALL;
1006 This->Flags |= SFLAG_LOCKED;
1008 if (!(This->Flags & SFLAG_LOCKABLE))
1010 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1013 if (Flags & WINED3DLOCK_DISCARD) {
1014 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1015 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1016 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1017 This->Flags |= SFLAG_INSYSMEM;
1021 if (This->Flags & SFLAG_INSYSMEM) {
1022 TRACE("Local copy is up to date, not downloading data\n");
1023 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1027 /* Now download the surface content from opengl
1028 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1029 * Offscreen targets which are not active at the moment or are higher targets(fbos) can be locked with the texture path
1031 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1032 if(swapchain || iface == myDevice->render_targets[0]) {
1033 const RECT *pass_rect = pRect;
1035 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1036 * because most caller functions do not need that. So do that here
1041 pRect->right == This->currentDesc.Width &&
1042 pRect->bottom == This->currentDesc.Height) {
1046 switch(wined3d_settings.rendertargetlock_mode) {
1049 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1051 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1052 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1053 * This may be faster on some cards
1055 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1062 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1068 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1070 } else if(iface == myDevice->stencilBufferTarget) {
1071 /** the depth stencil in openGL has a format of GL_FLOAT
1072 * which should be good for WINED3DFMT_D16_LOCKABLE
1073 * and WINED3DFMT_D16
1074 * it is unclear what format the stencil buffer is in except.
1075 * 'Each index is converted to fixed point...
1076 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1077 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1078 * glReadPixels(This->lockedRect.left,
1079 * This->lockedRect.bottom - j - 1,
1080 * This->lockedRect.right - This->lockedRect.left,
1082 * GL_DEPTH_COMPONENT,
1084 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1086 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1087 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1088 * none of that is the case the problem is not in this function :-)
1089 ********************************************/
1090 FIXME("Depth stencil locking not supported yet\n");
1092 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1093 TRACE("locking an ordinary surface\n");
1094 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1098 if(This->Flags & SFLAG_PBO) {
1099 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1101 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1102 checkGLcall("glBindBufferARB");
1104 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1105 if(This->resource.allocatedMemory) {
1106 ERR("The surface already has PBO memory allocated!\n");
1109 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1110 checkGLcall("glMapBufferARB");
1112 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1113 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1114 checkGLcall("glBindBufferARB");
1119 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1122 IWineD3DBaseTexture *pBaseTexture;
1125 * as seen in msdn docs
1127 IWineD3DSurface_AddDirtyRect(iface, pRect);
1129 /** Dirtify Container if needed */
1130 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1131 TRACE("Making container dirty\n");
1132 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1133 IWineD3DBaseTexture_Release(pBaseTexture);
1135 TRACE("Surface is standalone, no need to dirty the container\n");
1139 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1142 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1144 GLint prev_rasterpos[4];
1145 GLint skipBytes = 0;
1146 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1147 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1148 IWineD3DSwapChainImpl *swapchain;
1150 /* Activate the correct context for the render target */
1151 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1154 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1156 /* Primary offscreen render target */
1157 TRACE("Offscreen render target\n");
1158 glDrawBuffer(myDevice->offscreenBuffer);
1159 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1161 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1162 TRACE("Unlocking %#x buffer\n", buffer);
1163 glDrawBuffer(buffer);
1164 checkGLcall("glDrawBuffer");
1166 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1170 vcheckGLcall("glFlush");
1171 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1172 vcheckGLcall("glIntegerv");
1173 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1174 vcheckGLcall("glIntegerv");
1175 glPixelZoom(1.0, -1.0);
1176 vcheckGLcall("glPixelZoom");
1178 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1179 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1180 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1182 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1183 vcheckGLcall("glRasterPos2f");
1185 /* Some drivers(radeon dri, others?) don't like exceptions during
1186 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1187 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1188 * catch to put the dib section in InSync mode, which leads to a crash
1189 * and a blocked x server on my radeon card.
1191 * The following lines read the dib section so it is put in inSync mode
1192 * before glDrawPixels is called and the crash is prevented. There won't
1193 * be any interfering gdi accesses, because UnlockRect is called from
1194 * ReleaseDC, and the app won't use the dc any more afterwards.
1196 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1198 read = This->resource.allocatedMemory[0];
1201 if(This->Flags & SFLAG_PBO) {
1202 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1203 checkGLcall("glBindBufferARB");
1206 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1207 if(This->Flags & SFLAG_LOCKED) {
1208 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1209 (This->lockedRect.bottom - This->lockedRect.top)-1,
1211 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1212 checkGLcall("glDrawPixels");
1214 glDrawPixels(This->currentDesc.Width,
1215 This->currentDesc.Height,
1217 checkGLcall("glDrawPixels");
1220 if(This->Flags & SFLAG_PBO) {
1221 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1222 checkGLcall("glBindBufferARB");
1225 glPixelZoom(1.0,1.0);
1226 vcheckGLcall("glPixelZoom");
1228 glRasterPos3iv(&prev_rasterpos[0]);
1229 vcheckGLcall("glRasterPos3iv");
1231 /* Reset to previous pack row length */
1232 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1233 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1236 glDrawBuffer(myDevice->offscreenBuffer);
1237 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1238 } else if(swapchain->backBuffer) {
1239 glDrawBuffer(GL_BACK);
1240 checkGLcall("glDrawBuffer(GL_BACK)");
1242 glDrawBuffer(GL_FRONT);
1243 checkGLcall("glDrawBuffer(GL_FRONT)");
1250 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1251 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1252 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1253 IWineD3DSwapChainImpl *swapchain = NULL;
1256 if (!(This->Flags & SFLAG_LOCKED)) {
1257 WARN("trying to Unlock an unlocked surf@%p\n", This);
1258 return WINED3DERR_INVALIDCALL;
1261 if (This->Flags & SFLAG_PBO) {
1262 TRACE("Freeing PBO memory\n");
1263 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1265 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1266 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1267 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1268 checkGLcall("glUnmapBufferARB");
1270 This->resource.allocatedMemory = NULL;
1273 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1275 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1276 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1280 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1281 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1282 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1284 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1285 static BOOL warned = FALSE;
1287 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1293 if(This->dirtyRect.left == 0 &&
1294 This->dirtyRect.top == 0 &&
1295 This->dirtyRect.right == This->currentDesc.Width &&
1296 This->dirtyRect.bottom == This->currentDesc.Height) {
1299 /* TODO: Proper partial rectangle tracking */
1300 fullsurface = FALSE;
1301 This->Flags |= SFLAG_INSYSMEM;
1304 switch(wined3d_settings.rendertargetlock_mode) {
1307 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1309 if (This->glDescription.textureName == 0) {
1310 glGenTextures(1, &This->glDescription.textureName);
1311 checkGLcall("glGenTextures");
1313 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1314 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1316 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1322 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1327 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1328 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1329 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1330 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1331 * not fully up to date because only a subrectangle was read in LockRect.
1333 This->Flags &= ~SFLAG_INSYSMEM;
1334 This->Flags |= SFLAG_INDRAWABLE;
1337 This->dirtyRect.left = This->currentDesc.Width;
1338 This->dirtyRect.top = This->currentDesc.Height;
1339 This->dirtyRect.right = 0;
1340 This->dirtyRect.bottom = 0;
1341 } else if(iface == myDevice->stencilBufferTarget) {
1342 FIXME("Depth Stencil buffer locking is not implemented\n");
1344 /* The rest should be a normal texture */
1345 IWineD3DBaseTextureImpl *impl;
1346 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1347 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1348 * states need resetting
1350 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1351 if(impl->baseTexture.bindCount) {
1352 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1354 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1359 This->Flags &= ~SFLAG_LOCKED;
1360 memset(&This->lockedRect, 0, sizeof(RECT));
1364 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1365 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1366 WINED3DLOCKED_RECT lock;
1370 TRACE("(%p)->(%p)\n",This,pHDC);
1372 if(This->Flags & SFLAG_USERPTR) {
1373 ERR("Not supported on surfaces with an application-provided surfaces\n");
1374 return WINEDDERR_NODC;
1377 /* Give more detailed info for ddraw */
1378 if (This->Flags & SFLAG_DCINUSE)
1379 return WINEDDERR_DCALREADYCREATED;
1381 /* Can't GetDC if the surface is locked */
1382 if (This->Flags & SFLAG_LOCKED)
1383 return WINED3DERR_INVALIDCALL;
1385 /* According to Direct3D9 docs, only these formats are supported */
1386 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1387 if (This->resource.format != WINED3DFMT_R5G6B5 &&
1388 This->resource.format != WINED3DFMT_X1R5G5B5 &&
1389 This->resource.format != WINED3DFMT_R8G8B8 &&
1390 This->resource.format != WINED3DFMT_X8R8G8B8) return WINED3DERR_INVALIDCALL;
1393 memset(&lock, 0, sizeof(lock)); /* To be sure */
1395 /* Create a DIB section if there isn't a hdc yet */
1397 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1398 if(This->Flags & SFLAG_CLIENT) {
1399 IWineD3DSurface_PreLoad(iface);
1402 /* Use the dib section from now on if we are not using a PBO */
1403 if(!(This->Flags & SFLAG_PBO))
1404 This->resource.allocatedMemory = This->dib.bitmap_data;
1407 /* Lock the surface */
1408 hr = IWineD3DSurface_LockRect(iface,
1413 if(This->Flags & SFLAG_PBO) {
1414 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1415 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1419 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1420 /* keep the dib section */
1424 if(This->resource.format == WINED3DFMT_P8 ||
1425 This->resource.format == WINED3DFMT_A8P8) {
1426 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1427 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1429 PALETTEENTRY *pal = NULL;
1432 pal = This->palette->palents;
1434 IWineD3DSurfaceImpl *dds_primary = (IWineD3DSurfaceImpl *)This->resource.wineD3DDevice->ddraw_primary;
1435 if (dds_primary && dds_primary->palette)
1436 pal = dds_primary->palette->palents;
1440 for (n=0; n<256; n++) {
1441 col[n].rgbRed = pal[n].peRed;
1442 col[n].rgbGreen = pal[n].peGreen;
1443 col[n].rgbBlue = pal[n].peBlue;
1444 col[n].rgbReserved = 0;
1446 SetDIBColorTable(This->hDC, 0, 256, col);
1451 TRACE("returning %p\n",*pHDC);
1452 This->Flags |= SFLAG_DCINUSE;
1457 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1458 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1460 TRACE("(%p)->(%p)\n",This,hDC);
1462 if (!(This->Flags & SFLAG_DCINUSE))
1463 return WINED3DERR_INVALIDCALL;
1465 if (This->hDC !=hDC) {
1466 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1467 return WINED3DERR_INVALIDCALL;
1470 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1471 /* Copy the contents of the DIB over to the PBO */
1472 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1475 /* we locked first, so unlock now */
1476 IWineD3DSurface_UnlockRect(iface);
1478 This->Flags &= ~SFLAG_DCINUSE;
1483 /* ******************************************************
1484 IWineD3DSurface Internal (No mapping to directx api) parts follow
1485 ****************************************************** */
1487 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck, BOOL use_texturing, GLenum *format, GLenum *internal, GLenum *type, CONVERT_TYPES *convert, int *target_bpp, BOOL srgb_mode) {
1488 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1489 const GlPixelFormatDesc *glDesc;
1490 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1491 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1493 /* Default values: From the surface */
1494 *format = glDesc->glFormat;
1495 *type = glDesc->glType;
1496 *convert = NO_CONVERSION;
1497 *target_bpp = This->bytesPerPixel;
1500 *internal = glDesc->glGammaInternal;
1501 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1502 *internal = glDesc->rtInternal;
1504 *internal = glDesc->glInternal;
1507 /* Ok, now look if we have to do any conversion */
1508 switch(This->resource.format) {
1514 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1515 * of the two is available make sure texturing is requested as neither of the two works in
1516 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1517 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1518 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1519 * conflicts with this.
1521 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && primary_render_target_is_p8(device))) || colorkey_active || !use_texturing ) {
1523 *internal = GL_RGBA;
1524 *type = GL_UNSIGNED_BYTE;
1526 if(colorkey_active) {
1527 *convert = CONVERT_PALETTED_CK;
1529 *convert = CONVERT_PALETTED;
1532 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1534 *internal = GL_RGBA;
1535 *type = GL_UNSIGNED_BYTE;
1541 case WINED3DFMT_R3G3B2:
1542 /* **********************
1543 GL_UNSIGNED_BYTE_3_3_2
1544 ********************** */
1545 if (colorkey_active) {
1546 /* This texture format will never be used.. So do not care about color keying
1547 up until the point in time it will be needed :-) */
1548 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1552 case WINED3DFMT_R5G6B5:
1553 if (colorkey_active) {
1554 *convert = CONVERT_CK_565;
1556 *internal = GL_RGBA;
1557 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1561 case WINED3DFMT_X1R5G5B5:
1562 if (colorkey_active) {
1563 *convert = CONVERT_CK_5551;
1565 *internal = GL_RGBA;
1566 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1570 case WINED3DFMT_R8G8B8:
1571 if (colorkey_active) {
1572 *convert = CONVERT_CK_RGB24;
1574 *internal = GL_RGBA;
1575 *type = GL_UNSIGNED_INT_8_8_8_8;
1580 case WINED3DFMT_X8R8G8B8:
1581 if (colorkey_active) {
1582 *convert = CONVERT_RGB32_888;
1584 *internal = GL_RGBA;
1585 *type = GL_UNSIGNED_INT_8_8_8_8;
1589 case WINED3DFMT_V8U8:
1590 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1591 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1592 *format = GL_DUDV_ATI;
1593 *internal = GL_DU8DV8_ATI;
1595 /* No conversion - Just change the gl type */
1598 *convert = CONVERT_V8U8;
1600 *internal = GL_RGB8;
1601 *type = GL_UNSIGNED_BYTE;
1605 case WINED3DFMT_L6V5U5:
1606 *convert = CONVERT_L6V5U5;
1607 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1609 /* Use format and types from table */
1611 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1614 *internal = GL_RGB5;
1615 *type = GL_UNSIGNED_SHORT_5_6_5;
1619 case WINED3DFMT_X8L8V8U8:
1620 *convert = CONVERT_X8L8V8U8;
1622 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1623 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1624 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1625 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1626 * the needed type and format parameter, so the internal format contains a
1627 * 4th component, which is returned as alpha
1630 /* Not supported by GL_ATI_envmap_bumpmap */
1632 *internal = GL_RGB8;
1633 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1637 case WINED3DFMT_Q8W8V8U8:
1638 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1639 *convert = CONVERT_Q8W8V8U8;
1641 *internal = GL_RGBA8;
1642 *type = GL_UNSIGNED_BYTE;
1644 /* Not supported by GL_ATI_envmap_bumpmap */
1647 case WINED3DFMT_V16U16:
1648 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1649 *convert = CONVERT_V16U16;
1651 *internal = GL_RGB16_EXT;
1652 *type = GL_UNSIGNED_SHORT;
1654 /* What should I do here about GL_ATI_envmap_bumpmap?
1655 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1659 case WINED3DFMT_A4L4:
1660 /* A4L4 exists as an internal gl format, but for some reason there is not
1661 * format+type combination to load it. Thus convert it to A8L8, then load it
1662 * with A4L4 internal, but A8L8 format+type
1664 *convert = CONVERT_A4L4;
1665 *format = GL_LUMINANCE_ALPHA;
1666 *internal = GL_LUMINANCE4_ALPHA4;
1667 *type = GL_UNSIGNED_BYTE;
1671 case WINED3DFMT_R32F:
1672 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1673 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1674 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1677 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1679 *convert = CONVERT_R32F;
1681 *internal = GL_RGB32F_ARB;
1686 case WINED3DFMT_R16F:
1687 /* Similar to R32F */
1688 *convert = CONVERT_R16F;
1690 *internal = GL_RGB16F_ARB;
1691 *type = GL_HALF_FLOAT_ARB;
1695 case WINED3DFMT_G16R16:
1696 *convert = CONVERT_G16R16;
1698 *internal = GL_RGB16_EXT;
1699 *type = GL_UNSIGNED_SHORT;
1710 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1711 BYTE *source, *dest;
1712 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1717 memcpy(dst, src, pitch * height);
1720 case CONVERT_PALETTED:
1721 case CONVERT_PALETTED_CK:
1723 IWineD3DPaletteImpl* pal = This->palette;
1728 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1731 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1733 for (y = 0; y < height; y++)
1735 source = src + pitch * y;
1736 dest = dst + outpitch * y;
1737 /* This is an 1 bpp format, using the width here is fine */
1738 for (x = 0; x < width; x++) {
1739 BYTE color = *source++;
1740 *dest++ = table[color][0];
1741 *dest++ = table[color][1];
1742 *dest++ = table[color][2];
1743 *dest++ = table[color][3];
1749 case CONVERT_CK_565:
1751 /* Converting the 565 format in 5551 packed to emulate color-keying.
1753 Note : in all these conversion, it would be best to average the averaging
1754 pixels to get the color of the pixel that will be color-keyed to
1755 prevent 'color bleeding'. This will be done later on if ever it is
1758 Note2: Nvidia documents say that their driver does not support alpha + color keying
1759 on the same surface and disables color keying in such a case
1765 TRACE("Color keyed 565\n");
1767 for (y = 0; y < height; y++) {
1768 Source = (WORD *) (src + y * pitch);
1769 Dest = (WORD *) (dst + y * outpitch);
1770 for (x = 0; x < width; x++ ) {
1771 WORD color = *Source++;
1772 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1773 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1774 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1783 case CONVERT_CK_5551:
1785 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1789 TRACE("Color keyed 5551\n");
1790 for (y = 0; y < height; y++) {
1791 Source = (WORD *) (src + y * pitch);
1792 Dest = (WORD *) (dst + y * outpitch);
1793 for (x = 0; x < width; x++ ) {
1794 WORD color = *Source++;
1796 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1797 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1801 *Dest &= ~(1 << 15);
1809 case CONVERT_RGB32_888:
1811 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
1813 for (y = 0; y < height; y++)
1815 source = src + pitch * y;
1816 dest = dst + outpitch * y;
1817 for (x = 0; x < width; x++) {
1818 DWORD color = 0xffffff & *(DWORD*)source;
1819 DWORD dstcolor = color << 8;
1820 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1821 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1824 *(DWORD*)dest = dstcolor;
1836 unsigned char *Dest;
1837 for(y = 0; y < height; y++) {
1838 Source = (short *) (src + y * pitch);
1839 Dest = dst + y * outpitch;
1840 for (x = 0; x < width; x++ ) {
1841 long color = (*Source++);
1842 /* B */ Dest[0] = 0xff;
1843 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1844 /* R */ Dest[2] = (color) + 128; /* U */
1851 case CONVERT_V16U16:
1855 unsigned short *Dest;
1856 for(y = 0; y < height; y++) {
1857 Source = (DWORD *) (src + y * pitch);
1858 Dest = (unsigned short *) (dst + y * outpitch);
1859 for (x = 0; x < width; x++ ) {
1860 DWORD color = (*Source++);
1861 /* B */ Dest[0] = 0xffff;
1862 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1863 /* R */ Dest[2] = (color ) + 32768; /* U */
1870 case CONVERT_Q8W8V8U8:
1874 unsigned char *Dest;
1875 for(y = 0; y < height; y++) {
1876 Source = (DWORD *) (src + y * pitch);
1877 Dest = dst + y * outpitch;
1878 for (x = 0; x < width; x++ ) {
1879 long color = (*Source++);
1880 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1881 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1882 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1883 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1890 case CONVERT_L6V5U5:
1894 unsigned char *Dest;
1896 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1897 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1898 * fixed function and shaders without further conversion once the surface is
1901 for(y = 0; y < height; y++) {
1902 Source = (WORD *) (src + y * pitch);
1903 Dest = dst + y * outpitch;
1904 for (x = 0; x < width; x++ ) {
1905 short color = (*Source++);
1906 unsigned char l = ((color >> 10) & 0xfc);
1907 char v = ((color >> 5) & 0x3e);
1908 char u = ((color ) & 0x1f);
1910 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1911 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1912 * shift. GL reads a signed value and converts it into an unsigned value.
1914 /* M */ Dest[2] = l << 1;
1916 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1917 * from 5 bit values to 8 bit values.
1919 /* V */ Dest[1] = v << 3;
1920 /* U */ Dest[0] = u << 3;
1925 for(y = 0; y < height; y++) {
1926 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1927 Source = (WORD *) (src + y * pitch);
1928 for (x = 0; x < width; x++ ) {
1929 short color = (*Source++);
1930 unsigned char l = ((color >> 10) & 0xfc);
1931 short v = ((color >> 5) & 0x3e);
1932 short u = ((color ) & 0x1f);
1933 short v_conv = v + 16;
1934 short u_conv = u + 16;
1936 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1944 case CONVERT_X8L8V8U8:
1948 unsigned char *Dest;
1950 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1951 /* This implementation works with the fixed function pipeline and shaders
1952 * without further modification after converting the surface.
1954 for(y = 0; y < height; y++) {
1955 Source = (DWORD *) (src + y * pitch);
1956 Dest = dst + y * outpitch;
1957 for (x = 0; x < width; x++ ) {
1958 long color = (*Source++);
1959 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1960 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1961 /* U */ Dest[0] = (color & 0xff); /* U */
1962 /* I */ Dest[3] = 255; /* X */
1967 /* Doesn't work correctly with the fixed function pipeline, but can work in
1968 * shaders if the shader is adjusted. (There's no use for this format in gl's
1969 * standard fixed function pipeline anyway).
1971 for(y = 0; y < height; y++) {
1972 Source = (DWORD *) (src + y * pitch);
1973 Dest = dst + y * outpitch;
1974 for (x = 0; x < width; x++ ) {
1975 long color = (*Source++);
1976 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1977 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1978 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1989 unsigned char *Source;
1990 unsigned char *Dest;
1991 for(y = 0; y < height; y++) {
1992 Source = src + y * pitch;
1993 Dest = dst + y * outpitch;
1994 for (x = 0; x < width; x++ ) {
1995 unsigned char color = (*Source++);
1996 /* A */ Dest[1] = (color & 0xf0) << 0;
1997 /* L */ Dest[0] = (color & 0x0f) << 4;
2009 for(y = 0; y < height; y++) {
2010 Source = (float *) (src + y * pitch);
2011 Dest = (float *) (dst + y * outpitch);
2012 for (x = 0; x < width; x++ ) {
2013 float color = (*Source++);
2029 for(y = 0; y < height; y++) {
2030 Source = (WORD *) (src + y * pitch);
2031 Dest = (WORD *) (dst + y * outpitch);
2032 for (x = 0; x < width; x++ ) {
2033 WORD color = (*Source++);
2043 case CONVERT_G16R16:
2049 for(y = 0; y < height; y++) {
2050 Source = (WORD *) (src + y * pitch);
2051 Dest = (WORD *) (dst + y * outpitch);
2052 for (x = 0; x < width; x++ ) {
2053 WORD green = (*Source++);
2054 WORD red = (*Source++);
2065 ERR("Unsupported conversation type %d\n", convert);
2070 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2071 IWineD3DPaletteImpl* pal = This->palette;
2072 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2073 BOOL index_in_alpha = FALSE;
2074 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2077 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2078 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2079 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2080 * duplicate entries. Store the color key in the unused alpha component to speed the
2081 * download up and to make conversion unneeded. */
2082 index_in_alpha = primary_render_target_is_p8(device);
2085 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2086 if(dxVersion <= 7) {
2087 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2091 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2092 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2093 capability flag is present (wine does advertise this capability) */
2094 for (i = 0; i < 256; i++) {
2095 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2096 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2097 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2098 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2101 TRACE("Using surface palette %p\n", pal);
2102 /* Get the surface's palette */
2103 for (i = 0; i < 256; i++) {
2104 table[i][0] = pal->palents[i].peRed;
2105 table[i][1] = pal->palents[i].peGreen;
2106 table[i][2] = pal->palents[i].peBlue;
2108 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2109 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2110 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2111 of pixels that should be masked away is set to 0. */
2112 if(index_in_alpha) {
2114 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2116 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2117 table[i][3] = pal->palents[i].peFlags;
2125 const char *fragment_palette_conversion =
2128 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2129 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2130 "MAD index.a, index.a, constants.x, constants.y;\n" /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
2131 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2134 /* This function is used in case of 8bit paletted textures to upload the palette.
2135 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2136 extensions like ATI_fragment_shaders is possible.
2138 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2139 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2141 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2143 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2145 /* Try to use the paletted texture extension */
2146 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2148 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2149 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2153 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2154 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2155 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2157 /* Create the fragment program if we don't have it */
2158 if(!device->paletteConversionShader)
2160 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2161 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2162 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2163 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2164 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2167 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2168 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2170 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2171 glEnable(GL_TEXTURE_1D);
2172 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2174 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2175 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2176 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2177 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2179 /* Switch back to unit 0 in which the 2D texture will be stored. */
2180 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2182 /* Rebind the texture because it isn't bound anymore */
2183 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2187 BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2188 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2190 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2191 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2192 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2197 if(This->palette9) {
2198 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2202 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2204 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2208 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2209 GLboolean oldwrite[4];
2211 /* Some formats have only some color channels, and the others are 1.0.
2212 * since our rendering renders to all channels, and those pixel formats
2213 * are emulated by using a full texture with the other channels set to 1.0
2214 * manually, clear the unused channels.
2216 * This could be done with hacking colorwriteenable to mask the colors,
2217 * but before drawing the buffer would have to be cleared too, so there's
2220 switch(This->resource.format) {
2221 case WINED3DFMT_R16F:
2222 case WINED3DFMT_R32F:
2223 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2224 /* Do not activate a context, the correct drawable is active already
2225 * though just the read buffer is set, make sure to have the correct draw
2228 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2229 glDisable(GL_SCISSOR_TEST);
2230 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2231 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2232 glClearColor(0.0, 1.0, 1.0, 1.0);
2233 glClear(GL_COLOR_BUFFER_BIT);
2234 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2235 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2236 checkGLcall("Unused channel clear\n");
2243 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2244 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2246 if (!(This->Flags & SFLAG_INTEXTURE)) {
2247 TRACE("Reloading because surface is dirty\n");
2248 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2249 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2250 /* Reload: vice versa OR */
2251 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2252 /* Also reload: Color key is active AND the color key has changed */
2253 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2254 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2255 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2256 TRACE("Reloading because of color keying\n");
2257 /* To perform the color key conversion we need a sysmem copy of
2258 * the surface. Make sure we have it
2261 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2262 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2263 /* TODO: This is not necessarily needed with hw palettized texture support */
2264 This->Flags &= ~SFLAG_INTEXTURE;
2266 TRACE("surface is already in texture\n");
2270 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2271 * These resources are not bound by device size or format restrictions. Because of this,
2272 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2273 * However, these resources can always be created, locked, and copied.
2275 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2277 FIXME("(%p) Operation not supported for scratch textures\n",This);
2278 return WINED3DERR_INVALIDCALL;
2281 This->srgb = srgb_mode;
2282 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2286 static unsigned int gen = 0;
2289 if ((gen % 10) == 0) {
2290 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2291 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2294 * debugging crash code
2303 if (!(This->Flags & SFLAG_DONOTFREE)) {
2304 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2305 This->resource.allocatedMemory = NULL;
2306 This->resource.heapMemory = NULL;
2307 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2313 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2314 /* TODO: check for locks */
2315 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2316 IWineD3DBaseTexture *baseTexture = NULL;
2317 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2319 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2320 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2321 TRACE("Passing to container\n");
2322 IWineD3DBaseTexture_BindTexture(baseTexture);
2323 IWineD3DBaseTexture_Release(baseTexture);
2325 TRACE("(%p) : Binding surface\n", This);
2327 if(!device->isInDraw) {
2328 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2331 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2339 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2342 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2343 char *allocatedMemory;
2345 IWineD3DSwapChain *swapChain = NULL;
2347 GLuint tmpTexture = 0;
2350 Textures may not be stored in ->allocatedgMemory and a GlTexture
2351 so we should lock the surface before saving a snapshot, or at least check that
2353 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2354 by calling GetTexImage and in compressed form by calling
2355 GetCompressedTexImageARB. Queried compressed images can be saved and
2356 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2357 texture images do not need to be processed by the GL and should
2358 significantly improve texture loading performance relative to uncompressed
2361 /* Setup the width and height to be the internal texture width and height. */
2362 width = This->pow2Width;
2363 height = This->pow2Height;
2364 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2365 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2367 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2368 /* if were not a real texture then read the back buffer into a real texture */
2369 /* we don't want to interfere with the back buffer so read the data into a temporary
2370 * texture and then save the data out of the temporary texture
2374 TRACE("(%p) Reading render target into texture\n", This);
2375 glEnable(GL_TEXTURE_2D);
2377 glGenTextures(1, &tmpTexture);
2378 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2380 glTexImage2D(GL_TEXTURE_2D,
2387 GL_UNSIGNED_INT_8_8_8_8_REV,
2390 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2391 vcheckGLcall("glGetIntegerv");
2392 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2393 vcheckGLcall("glReadBuffer");
2394 glCopyTexImage2D(GL_TEXTURE_2D,
2403 checkGLcall("glCopyTexImage2D");
2404 glReadBuffer(prevRead);
2407 } else { /* bind the real texture, and make sure it up to date */
2408 IWineD3DSurface_PreLoad(iface);
2410 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2412 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2413 glGetTexImage(GL_TEXTURE_2D,
2414 This->glDescription.level,
2416 GL_UNSIGNED_INT_8_8_8_8_REV,
2418 checkGLcall("glTexImage2D");
2420 glBindTexture(GL_TEXTURE_2D, 0);
2421 glDeleteTextures(1, &tmpTexture);
2425 f = fopen(filename, "w+");
2427 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2428 return WINED3DERR_INVALIDCALL;
2430 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2431 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2446 fwrite(&width,2,1,f);
2448 fwrite(&height,2,1,f);
2453 /* 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 */
2455 textureRow = allocatedMemory + (width * (height - 1) *4);
2457 textureRow = allocatedMemory;
2458 for (y = 0 ; y < height; y++) {
2459 for (i = 0; i < width; i++) {
2460 color = *((DWORD*)textureRow);
2461 fputc((color >> 16) & 0xFF, f); /* B */
2462 fputc((color >> 8) & 0xFF, f); /* G */
2463 fputc((color >> 0) & 0xFF, f); /* R */
2464 fputc((color >> 24) & 0xFF, f); /* A */
2467 /* take two rows of the pointer to the texture memory */
2469 (textureRow-= width << 3);
2472 TRACE("Closing file\n");
2476 IWineD3DSwapChain_Release(swapChain);
2478 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2483 * Slightly inefficient way to handle multiple dirty rects but it works :)
2485 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2486 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2487 IWineD3DBaseTexture *baseTexture = NULL;
2489 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2490 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2492 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2493 if (NULL != pDirtyRect) {
2494 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2495 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2496 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2497 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2499 This->dirtyRect.left = 0;
2500 This->dirtyRect.top = 0;
2501 This->dirtyRect.right = This->currentDesc.Width;
2502 This->dirtyRect.bottom = This->currentDesc.Height;
2504 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2505 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2506 /* if the container is a basetexture then mark it dirty. */
2507 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2508 TRACE("Passing to container\n");
2509 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2510 IWineD3DBaseTexture_Release(baseTexture);
2515 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2516 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2518 const GlPixelFormatDesc *glDesc;
2519 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2521 TRACE("(%p) : Calling base function first\n", This);
2522 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2524 /* Setup some glformat defaults */
2525 This->glDescription.glFormat = glDesc->glFormat;
2526 This->glDescription.glFormatInternal = glDesc->glInternal;
2527 This->glDescription.glType = glDesc->glType;
2529 This->Flags &= ~SFLAG_ALLOCATED;
2530 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2531 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2536 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2537 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2539 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2540 WARN("Surface is locked or the HDC is in use\n");
2541 return WINED3DERR_INVALIDCALL;
2544 if(Mem && Mem != This->resource.allocatedMemory) {
2545 void *release = NULL;
2547 /* Do I have to copy the old surface content? */
2548 if(This->Flags & SFLAG_DIBSECTION) {
2549 /* Release the DC. No need to hold the critical section for the update
2550 * Thread because this thread runs only on front buffers, but this method
2551 * fails for render targets in the check above.
2553 SelectObject(This->hDC, This->dib.holdbitmap);
2554 DeleteDC(This->hDC);
2555 /* Release the DIB section */
2556 DeleteObject(This->dib.DIBsection);
2557 This->dib.bitmap_data = NULL;
2558 This->resource.allocatedMemory = NULL;
2560 This->Flags &= ~SFLAG_DIBSECTION;
2561 } else if(!(This->Flags & SFLAG_USERPTR)) {
2562 release = This->resource.heapMemory;
2563 This->resource.heapMemory = NULL;
2565 This->resource.allocatedMemory = Mem;
2566 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2568 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2569 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2571 /* For client textures opengl has to be notified */
2572 if(This->Flags & SFLAG_CLIENT) {
2573 This->Flags &= ~SFLAG_ALLOCATED;
2574 IWineD3DSurface_PreLoad(iface);
2575 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2578 /* Now free the old memory if any */
2579 HeapFree(GetProcessHeap(), 0, release);
2580 } else if(This->Flags & SFLAG_USERPTR) {
2581 /* Lockrect and GetDC will re-create the dib section and allocated memory */
2582 This->resource.allocatedMemory = NULL;
2583 /* HeapMemory should be NULL already */
2584 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2585 This->Flags &= ~SFLAG_USERPTR;
2587 if(This->Flags & SFLAG_CLIENT) {
2588 This->Flags &= ~SFLAG_ALLOCATED;
2589 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2590 IWineD3DSurface_PreLoad(iface);
2596 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2597 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2598 IWineD3DSwapChainImpl *swapchain = NULL;
2600 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2602 /* Flipping is only supported on RenderTargets */
2603 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2606 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2607 * FIXME("(%p) Target override is not supported by now\n", This);
2608 * Additionally, it isn't really possible to support triple-buffering
2609 * properly on opengl at all
2613 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2615 ERR("Flipped surface is not on a swapchain\n");
2616 return WINEDDERR_NOTFLIPPABLE;
2619 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2620 * and only d3d8 and d3d9 apps specify the presentation interval
2622 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2623 /* Most common case first to avoid wasting time on all the other cases */
2624 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2625 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2626 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2627 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2628 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2629 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2630 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2632 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2635 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2636 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2637 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2641 /* Does a direct frame buffer -> texture copy. Stretching is done
2642 * with single pixel copy calls
2644 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2645 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2648 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2651 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2652 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2655 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2656 glEnable(This->glDescription.target);
2657 checkGLcall("glEnable(This->glDescription.target)");
2659 /* Bind the target texture */
2660 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2661 checkGLcall("glBindTexture");
2663 glReadBuffer(myDevice->offscreenBuffer);
2665 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2666 glReadBuffer(buffer);
2668 checkGLcall("glReadBuffer");
2670 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2671 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2673 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2674 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2676 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2677 ERR("Texture filtering not supported in direct blit\n");
2679 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2680 ERR("Texture filtering not supported in direct blit\n");
2684 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2685 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2686 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2688 glCopyTexSubImage2D(This->glDescription.target,
2689 This->glDescription.level,
2690 drect->x1, drect->y1, /* xoffset, yoffset */
2691 srect->x1, Src->currentDesc.Height - srect->y2,
2692 drect->x2 - drect->x1, drect->y2 - drect->y1);
2694 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2695 /* I have to process this row by row to swap the image,
2696 * otherwise it would be upside down, so stretching in y direction
2697 * doesn't cost extra time
2699 * However, stretching in x direction can be avoided if not necessary
2701 for(row = drect->y1; row < drect->y2; row++) {
2702 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2703 /* Well, that stuff works, but it's very slow.
2704 * find a better way instead
2708 for(col = drect->x1; col < drect->x2; col++) {
2709 glCopyTexSubImage2D(This->glDescription.target,
2710 This->glDescription.level,
2711 drect->x1 + col, row, /* xoffset, yoffset */
2712 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2716 glCopyTexSubImage2D(This->glDescription.target,
2717 This->glDescription.level,
2718 drect->x1, row, /* xoffset, yoffset */
2719 srect->x1, yoffset - (int) (row * yrel),
2720 drect->x2-drect->x1, 1);
2724 vcheckGLcall("glCopyTexSubImage2D");
2726 /* Leave the opengl state valid for blitting */
2727 glDisable(This->glDescription.target);
2728 checkGLcall("glDisable(This->glDescription.target)");
2733 /* Uses the hardware to stretch and flip the image */
2734 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2735 GLuint src, backup = 0;
2736 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2737 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2738 float left, right, top, bottom; /* Texture coordinates */
2739 UINT fbwidth = Src->currentDesc.Width;
2740 UINT fbheight = Src->currentDesc.Height;
2741 GLenum drawBuffer = GL_BACK;
2742 GLenum texture_target;
2743 BOOL noBackBufferBackup;
2745 TRACE("Using hwstretch blit\n");
2746 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2747 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2748 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2750 noBackBufferBackup = !swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2751 if(!noBackBufferBackup && Src->glDescription.textureName == 0) {
2752 /* Get it a description */
2753 IWineD3DSurface_PreLoad(SrcSurface);
2757 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2758 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2760 if(GL_LIMITS(aux_buffers) >= 2) {
2761 /* Got more than one aux buffer? Use the 2nd aux buffer */
2762 drawBuffer = GL_AUX1;
2763 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && GL_LIMITS(aux_buffers) >= 1) {
2764 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2765 drawBuffer = GL_AUX0;
2768 if(noBackBufferBackup) {
2769 glGenTextures(1, &backup);
2770 checkGLcall("glGenTextures\n");
2771 glBindTexture(GL_TEXTURE_2D, backup);
2772 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2773 texture_target = GL_TEXTURE_2D;
2775 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2776 * we are reading from the back buffer, the backup can be used as source texture
2778 texture_target = Src->glDescription.target;
2779 glBindTexture(texture_target, Src->glDescription.textureName);
2780 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2781 glEnable(texture_target);
2782 checkGLcall("glEnable(texture_target)");
2784 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2785 Src->Flags &= ~SFLAG_INTEXTURE;
2788 glReadBuffer(GL_BACK);
2789 checkGLcall("glReadBuffer(GL_BACK)");
2791 /* TODO: Only back up the part that will be overwritten */
2792 glCopyTexSubImage2D(texture_target, 0,
2793 0, 0 /* read offsets */,
2798 checkGLcall("glCopyTexSubImage2D");
2800 /* No issue with overriding these - the sampler is dirty due to blit usage */
2801 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2802 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
2803 checkGLcall("glTexParameteri");
2804 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2805 minMipLookup[Filter][WINED3DTEXF_NONE]);
2806 checkGLcall("glTexParameteri");
2808 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2809 src = backup ? backup : Src->glDescription.textureName;
2811 glReadBuffer(GL_FRONT);
2812 checkGLcall("glReadBuffer(GL_FRONT)");
2814 glGenTextures(1, &src);
2815 checkGLcall("glGenTextures(1, &src)");
2816 glBindTexture(GL_TEXTURE_2D, src);
2817 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2819 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2820 * out for power of 2 sizes
2822 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2823 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2824 checkGLcall("glTexImage2D");
2825 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2826 0, 0 /* read offsets */,
2831 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2832 checkGLcall("glTexParameteri");
2833 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2834 checkGLcall("glTexParameteri");
2836 glReadBuffer(GL_BACK);
2837 checkGLcall("glReadBuffer(GL_BACK)");
2839 if(texture_target != GL_TEXTURE_2D) {
2840 glDisable(texture_target);
2841 glEnable(GL_TEXTURE_2D);
2842 texture_target = GL_TEXTURE_2D;
2845 checkGLcall("glEnd and previous");
2847 left = (float) srect->x1 / (float) Src->pow2Width;
2848 right = (float) srect->x2 / (float) Src->pow2Width;
2851 top = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2852 bottom = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2854 top = (float) (Src->currentDesc.Height - srect->y2) / (float) Src->pow2Height;
2855 bottom = (float) (Src->currentDesc.Height - srect->y1) / (float) Src->pow2Height;
2858 /* draw the source texture stretched and upside down. The correct surface is bound already */
2859 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2860 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2862 glDrawBuffer(drawBuffer);
2863 glReadBuffer(drawBuffer);
2867 glTexCoord2f(left, bottom);
2868 glVertex2i(0, fbheight);
2871 glTexCoord2f(left, top);
2872 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2875 glTexCoord2f(right, top);
2876 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2879 glTexCoord2f(right, bottom);
2880 glVertex2i(drect->x2 - drect->x1, fbheight);
2882 checkGLcall("glEnd and previous");
2884 if(texture_target != This->glDescription.target) {
2885 glDisable(texture_target);
2886 glEnable(This->glDescription.target);
2887 texture_target = This->glDescription.target;
2890 /* Now read the stretched and upside down image into the destination texture */
2891 glBindTexture(texture_target, This->glDescription.textureName);
2892 checkGLcall("glBindTexture");
2893 glCopyTexSubImage2D(texture_target,
2895 drect->x1, drect->y1, /* xoffset, yoffset */
2896 0, 0, /* We blitted the image to the origin */
2897 drect->x2 - drect->x1, drect->y2 - drect->y1);
2898 checkGLcall("glCopyTexSubImage2D");
2900 if(drawBuffer == GL_BACK) {
2901 /* Write the back buffer backup back */
2903 if(texture_target != GL_TEXTURE_2D) {
2904 glDisable(texture_target);
2905 glEnable(GL_TEXTURE_2D);
2906 texture_target = GL_TEXTURE_2D;
2908 glBindTexture(GL_TEXTURE_2D, backup);
2909 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2911 if(texture_target != Src->glDescription.target) {
2912 glDisable(texture_target);
2913 glEnable(Src->glDescription.target);
2914 texture_target = Src->glDescription.target;
2916 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2917 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2922 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2926 glTexCoord2f(0.0, 0.0);
2927 glVertex2i(0, fbheight);
2930 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2931 glVertex2i(fbwidth, Src->currentDesc.Height);
2934 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2935 glVertex2i(fbwidth, 0);
2938 /* Restore the old draw buffer */
2939 glDrawBuffer(GL_BACK);
2941 glDisable(texture_target);
2942 checkGLcall("glDisable(texture_target)");
2945 if(src != Src->glDescription.textureName && src != backup) {
2946 glDeleteTextures(1, &src);
2947 checkGLcall("glDeleteTextures(1, &src)");
2950 glDeleteTextures(1, &backup);
2951 checkGLcall("glDeleteTextures(1, &backup)");
2957 /* Not called from the VTable */
2958 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2960 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2961 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2962 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2964 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2966 /* Get the swapchain. One of the surfaces has to be a primary surface */
2967 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2968 WARN("Destination is in sysmem, rejecting gl blt\n");
2969 return WINED3DERR_INVALIDCALL;
2971 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2972 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2974 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2975 WARN("Src is in sysmem, rejecting gl blt\n");
2976 return WINED3DERR_INVALIDCALL;
2978 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
2979 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
2982 /* Early sort out of cases where no render target is used */
2983 if(!dstSwapchain && !srcSwapchain &&
2984 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
2985 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
2986 return WINED3DERR_INVALIDCALL;
2989 /* No destination color keying supported */
2990 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
2991 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
2992 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
2993 return WINED3DERR_INVALIDCALL;
2997 rect.x1 = DestRect->left;
2998 rect.y1 = DestRect->top;
2999 rect.x2 = DestRect->right;
3000 rect.y2 = DestRect->bottom;
3004 rect.x2 = This->currentDesc.Width;
3005 rect.y2 = This->currentDesc.Height;
3008 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3009 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3010 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3011 /* Half-life does a Blt from the back buffer to the front buffer,
3012 * Full surface size, no flags... Use present instead
3014 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3017 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3021 TRACE("Looking if a Present can be done...\n");
3022 /* Source Rectangle must be full surface */
3024 if(SrcRect->left != 0 || SrcRect->top != 0 ||
3025 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
3026 TRACE("No, Source rectangle doesn't match\n");
3032 mySrcRect.right = Src->currentDesc.Width;
3033 mySrcRect.bottom = Src->currentDesc.Height;
3035 /* No stretching may occur */
3036 if(mySrcRect.right != rect.x2 - rect.x1 ||
3037 mySrcRect.bottom != rect.y2 - rect.y1) {
3038 TRACE("No, stretching is done\n");
3042 /* Destination must be full surface or match the clipping rectangle */
3043 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3047 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3052 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3055 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3056 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3058 TRACE("No, dest rectangle doesn't match(clipper)\n");
3059 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3060 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3066 if(rect.x1 != 0 || rect.y1 != 0 ||
3067 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3068 TRACE("No, dest rectangle doesn't match(surface size)\n");
3075 /* These flags are unimportant for the flag check, remove them */
3076 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3077 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3079 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3080 * take very long, while a flip is fast.
3081 * This applies to Half-Life, which does such Blts every time it finished
3082 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3083 * menu. This is also used by all apps when they do windowed rendering
3085 * The problem is that flipping is not really the same as copying. After a
3086 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3087 * untouched. Therefore it's necessary to override the swap effect
3088 * and to set it back after the flip.
3090 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3094 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3095 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3097 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3098 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3100 dstSwapchain->presentParms.SwapEffect = orig_swap;
3107 TRACE("Unsupported blit between buffers on the same swapchain\n");
3108 return WINED3DERR_INVALIDCALL;
3109 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3110 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3111 return WINED3DERR_INVALIDCALL;
3112 } else if(dstSwapchain && srcSwapchain) {
3113 FIXME("Implement hardware blit between two different swapchains\n");
3114 return WINED3DERR_INVALIDCALL;
3115 } else if(dstSwapchain) {
3116 if(SrcSurface == myDevice->render_targets[0]) {
3117 TRACE("Blit from active render target to a swapchain\n");
3118 /* Handled with regular texture -> swapchain blit */
3120 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3121 FIXME("Implement blit from a swapchain to the active render target\n");
3122 return WINED3DERR_INVALIDCALL;
3125 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3126 /* Blit from render target to texture */
3128 BOOL upsideDown, stretchx;
3129 BOOL paletteOverride = FALSE;
3131 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3132 TRACE("Color keying not supported by frame buffer to texture blit\n");
3133 return WINED3DERR_INVALIDCALL;
3134 /* Destination color key is checked above */
3137 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3138 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3141 if(SrcRect->top < SrcRect->bottom) {
3142 srect.y1 = SrcRect->top;
3143 srect.y2 = SrcRect->bottom;
3146 srect.y1 = SrcRect->bottom;
3147 srect.y2 = SrcRect->top;
3150 srect.x1 = SrcRect->left;
3151 srect.x2 = SrcRect->right;
3155 srect.x2 = Src->currentDesc.Width;
3156 srect.y2 = Src->currentDesc.Height;
3159 if(rect.x1 > rect.x2) {
3163 upsideDown = !upsideDown;
3166 TRACE("Reading from an offscreen target\n");
3167 upsideDown = !upsideDown;
3170 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3176 /* When blitting from a render target a texture, the texture isn't required to have a palette.
3177 * In this case grab the palette from the render target. */
3178 if((This->resource.format == WINED3DFMT_P8) && (This->palette == NULL)) {
3179 paletteOverride = TRUE;
3180 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3181 This->palette = Src->palette;
3184 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3185 * flip the image nor scale it.
3187 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3188 * -> If the app wants a image width an unscaled width, copy it line per line
3189 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3190 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3191 * back buffer. This is slower than reading line per line, thus not used for flipping
3192 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3195 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3196 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3199 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3200 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3201 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3202 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3203 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3204 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3205 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3207 TRACE("Using hardware stretching to flip / stretch the texture\n");
3208 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3211 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3213 This->palette = NULL;
3215 if(!(This->Flags & SFLAG_DONOTFREE)) {
3216 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3217 This->resource.allocatedMemory = NULL;
3218 This->resource.heapMemory = NULL;
3220 This->Flags &= ~SFLAG_INSYSMEM;
3222 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3223 * path is never entered
3225 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3229 /* Blit from offscreen surface to render target */
3230 float glTexCoord[4];
3231 DWORD oldCKeyFlags = Src->CKeyFlags;
3232 WINEDDCOLORKEY oldBltCKey = This->SrcBltCKey;
3233 RECT SourceRectangle;
3234 BOOL paletteOverride = FALSE;
3236 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3239 SourceRectangle.left = SrcRect->left;
3240 SourceRectangle.right = SrcRect->right;
3241 SourceRectangle.top = SrcRect->top;
3242 SourceRectangle.bottom = SrcRect->bottom;
3244 SourceRectangle.left = 0;
3245 SourceRectangle.right = Src->currentDesc.Width;
3246 SourceRectangle.top = 0;
3247 SourceRectangle.bottom = Src->currentDesc.Height;
3249 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3250 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3251 TRACE("Using stretch_rect_fbo\n");
3252 /* The source is always a texture, but never the currently active render target, and the texture
3253 * contents are never upside down
3255 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3256 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3260 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3261 /* Fall back to software */
3262 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3263 SourceRectangle.left, SourceRectangle.top,
3264 SourceRectangle.right, SourceRectangle.bottom);
3265 return WINED3DERR_INVALIDCALL;
3268 /* Color keying: Check if we have to do a color keyed blt,
3269 * and if not check if a color key is activated.
3271 * Just modify the color keying parameters in the surface and restore them afterwards
3272 * The surface keeps track of the color key last used to load the opengl surface.
3273 * PreLoad will catch the change to the flags and color key and reload if necessary.
3275 if(Flags & WINEDDBLT_KEYSRC) {
3276 /* Use color key from surface */
3277 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3278 /* Use color key from DDBltFx */
3279 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3280 This->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3282 /* Do not use color key */
3283 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3286 /* When blitting from an offscreen surface to a rendertarget, the source
3287 * surface is not required to have a palette. Our rendering / conversion
3288 * code further down the road retrieves the palette from the surface, so
3289 * it must have a palette set. */
3290 if((Src->resource.format == WINED3DFMT_P8) && (Src->palette == NULL)) {
3291 paletteOverride = TRUE;
3292 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3293 Src->palette = This->palette;
3296 /* Now load the surface */
3297 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3300 /* Activate the destination context, set it up for blitting */
3301 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3304 glEnable(Src->glDescription.target);
3305 checkGLcall("glEnable(Src->glDescription.target)");
3308 TRACE("Drawing to offscreen buffer\n");
3309 glDrawBuffer(myDevice->offscreenBuffer);
3310 checkGLcall("glDrawBuffer");
3312 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3313 TRACE("Drawing to %#x buffer\n", buffer);
3314 glDrawBuffer(buffer);
3315 checkGLcall("glDrawBuffer");
3318 /* Bind the texture */
3319 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3320 checkGLcall("glBindTexture");
3322 /* Filtering for StretchRect */
3323 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3324 stateLookup[WINELOOKUP_MAGFILTER][Filter - minLookup[WINELOOKUP_MAGFILTER]]);
3325 checkGLcall("glTexParameteri");
3326 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3327 minMipLookup[Filter][WINED3DTEXF_NONE]);
3328 checkGLcall("glTexParameteri");
3329 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3330 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3331 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3332 checkGLcall("glTexEnvi");
3334 /* This is for color keying */
3335 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3336 glEnable(GL_ALPHA_TEST);
3337 checkGLcall("glEnable GL_ALPHA_TEST");
3339 /* When the primary render target uses P8, the alpha component contains the palette index.
3340 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3341 * should be masked away have alpha set to 0. */
3342 if(primary_render_target_is_p8(myDevice))
3343 glAlphaFunc(GL_NOTEQUAL, (float)This->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3345 glAlphaFunc(GL_NOTEQUAL, 0.0);
3346 checkGLcall("glAlphaFunc\n");
3348 glDisable(GL_ALPHA_TEST);
3349 checkGLcall("glDisable GL_ALPHA_TEST");
3352 /* Draw a textured quad
3356 glColor3d(1.0f, 1.0f, 1.0f);
3357 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3362 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3363 glVertex3f(rect.x1, rect.y2, 0.0);
3365 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3370 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3375 checkGLcall("glEnd");
3377 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3378 glDisable(GL_ALPHA_TEST);
3379 checkGLcall("glDisable(GL_ALPHA_TEST)");
3382 /* Flush in case the drawable is used by multiple GL contexts */
3383 if(dstSwapchain && (dstSwapchain->num_contexts >= 2))
3386 glBindTexture(Src->glDescription.target, 0);
3387 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3388 /* Leave the opengl state valid for blitting */
3389 glDisable(Src->glDescription.target);
3390 checkGLcall("glDisable(Src->glDescription.target)");
3392 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3393 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3395 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3396 glDrawBuffer(GL_BACK);
3397 checkGLcall("glDrawBuffer");
3399 /* Restore the color key parameters */
3400 Src->CKeyFlags = oldCKeyFlags;
3401 This->SrcBltCKey = oldBltCKey;
3403 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3405 Src->palette = NULL;
3409 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3410 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3413 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3414 /* TODO: This should be moved to ModifyLocation() */
3415 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3416 This->Flags |= SFLAG_INTEXTURE;
3421 /* Source-Less Blit to render target */
3422 if (Flags & WINEDDBLT_COLORFILL) {
3423 /* This is easy to handle for the D3D Device... */
3426 TRACE("Colorfill\n");
3428 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3429 must be true if we are here */
3430 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3431 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3432 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3433 TRACE("Surface is higher back buffer, falling back to software\n");
3434 return WINED3DERR_INVALIDCALL;
3437 /* The color as given in the Blt function is in the format of the frame-buffer...
3438 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3440 if (This->resource.format == WINED3DFMT_P8) {
3443 if (primary_render_target_is_p8(myDevice)) alpha = DDBltFx->u5.dwFillColor << 24;
3444 else alpha = 0xFF000000;
3446 if (This->palette) {
3448 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3449 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3450 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3455 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3456 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3459 color = ((0xFF000000) |
3460 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3461 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3462 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3465 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3466 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3467 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3469 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3470 color = DDBltFx->u5.dwFillColor;
3473 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3474 return WINED3DERR_INVALIDCALL;
3477 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3478 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3479 1, /* Number of rectangles */
3480 &rect, WINED3DCLEAR_TARGET, color,
3487 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3488 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3489 return WINED3DERR_INVALIDCALL;
3492 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3494 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3497 if (Flags & WINEDDBLT_DEPTHFILL) {
3498 switch(This->resource.format) {
3499 case WINED3DFMT_D16:
3500 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3502 case WINED3DFMT_D15S1:
3503 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3505 case WINED3DFMT_D24S8:
3506 case WINED3DFMT_D24X8:
3507 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3509 case WINED3DFMT_D32:
3510 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3514 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3517 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3518 DestRect == NULL ? 0 : 1,
3519 (WINED3DRECT *) DestRect,
3520 WINED3DCLEAR_ZBUFFER,
3526 FIXME("(%p): Unsupp depthstencil blit\n", This);
3527 return WINED3DERR_INVALIDCALL;
3530 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3531 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3532 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3533 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3534 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3535 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3537 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3539 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3540 return WINEDDERR_SURFACEBUSY;
3543 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3544 * except depth blits, which seem to work
3546 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3547 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3548 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3549 return WINED3DERR_INVALIDCALL;
3550 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3551 TRACE("Z Blit override handled the blit\n");
3556 /* Special cases for RenderTargets */
3557 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3558 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3559 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3562 /* For the rest call the X11 surface implementation.
3563 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3564 * other Blts are rather rare
3566 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3569 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3570 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3571 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3572 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3573 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3575 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3577 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3578 return WINEDDERR_SURFACEBUSY;
3581 if(myDevice->inScene &&
3582 (iface == myDevice->stencilBufferTarget ||
3583 (Source && Source == myDevice->stencilBufferTarget))) {
3584 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3585 return WINED3DERR_INVALIDCALL;
3588 /* Special cases for RenderTargets */
3589 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3590 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3592 RECT SrcRect, DstRect;
3596 SrcRect.left = rsrc->left;
3597 SrcRect.top= rsrc->top;
3598 SrcRect.bottom = rsrc->bottom;
3599 SrcRect.right = rsrc->right;
3603 SrcRect.right = srcImpl->currentDesc.Width;
3604 SrcRect.bottom = srcImpl->currentDesc.Height;
3607 DstRect.left = dstx;
3609 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3610 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3612 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3613 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3614 Flags |= WINEDDBLT_KEYSRC;
3615 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3616 Flags |= WINEDDBLT_KEYDEST;
3617 if(trans & WINEDDBLTFAST_WAIT)
3618 Flags |= WINEDDBLT_WAIT;
3619 if(trans & WINEDDBLTFAST_DONOTWAIT)
3620 Flags |= WINEDDBLT_DONOTWAIT;
3622 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3626 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3629 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3630 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3632 IWineD3DPaletteImpl *pal = This->palette;
3634 TRACE("(%p)\n", This);
3636 if (!pal) return WINED3D_OK;
3638 if(This->resource.format == WINED3DFMT_P8 ||
3639 This->resource.format == WINED3DFMT_A8P8)
3641 if(!(This->Flags & SFLAG_INSYSMEM)) {
3642 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3643 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3645 TRACE("Dirtifying surface\n");
3646 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3649 if(This->Flags & SFLAG_DIBSECTION) {
3650 TRACE("(%p): Updating the hdc's palette\n", This);
3651 for (n=0; n<256; n++) {
3652 col[n].rgbRed = pal->palents[n].peRed;
3653 col[n].rgbGreen = pal->palents[n].peGreen;
3654 col[n].rgbBlue = pal->palents[n].peBlue;
3655 col[n].rgbReserved = 0;
3657 SetDIBColorTable(This->hDC, 0, 256, col);
3660 /* Propagate the changes to the drawable when we have a palette.
3661 * TODO: in case of hardware p8 palettes we should only upload the palette. */
3662 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3663 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3668 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3669 /** Check against the maximum texture sizes supported by the video card **/
3670 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3671 unsigned int pow2Width, pow2Height;
3672 const GlPixelFormatDesc *glDesc;
3674 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3675 /* Setup some glformat defaults */
3676 This->glDescription.glFormat = glDesc->glFormat;
3677 This->glDescription.glFormatInternal = glDesc->glInternal;
3678 This->glDescription.glType = glDesc->glType;
3680 This->glDescription.textureName = 0;
3681 This->glDescription.target = GL_TEXTURE_2D;
3683 /* Non-power2 support */
3684 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3685 pow2Width = This->currentDesc.Width;
3686 pow2Height = This->currentDesc.Height;
3688 /* Find the nearest pow2 match */
3689 pow2Width = pow2Height = 1;
3690 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3691 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3693 This->pow2Width = pow2Width;
3694 This->pow2Height = pow2Height;
3696 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3697 WINED3DFORMAT Format = This->resource.format;
3698 /** TODO: add support for non power two compressed textures **/
3699 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3700 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3701 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3702 This, This->currentDesc.Width, This->currentDesc.Height);
3703 return WINED3DERR_NOTAVAILABLE;
3707 if(pow2Width != This->currentDesc.Width ||
3708 pow2Height != This->currentDesc.Height) {
3709 This->Flags |= SFLAG_NONPOW2;
3712 TRACE("%p\n", This);
3713 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3714 /* one of three options
3715 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)
3716 2: Set the texture to the maximum size (bad idea)
3717 3: WARN and return WINED3DERR_NOTAVAILABLE;
3718 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.
3720 WARN("(%p) Creating an oversized surface\n", This);
3721 This->Flags |= SFLAG_OVERSIZE;
3723 /* This will be initialized on the first blt */
3724 This->glRect.left = 0;
3725 This->glRect.top = 0;
3726 This->glRect.right = 0;
3727 This->glRect.bottom = 0;
3729 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3730 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3731 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3732 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3734 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3735 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3737 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3738 This->pow2Width = This->currentDesc.Width;
3739 This->pow2Height = This->currentDesc.Height;
3740 This->Flags &= ~SFLAG_NONPOW2;
3743 /* No oversize, gl rect is the full texture size */
3744 This->Flags &= ~SFLAG_OVERSIZE;
3745 This->glRect.left = 0;
3746 This->glRect.top = 0;
3747 This->glRect.right = This->pow2Width;
3748 This->glRect.bottom = This->pow2Height;
3751 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3752 switch(wined3d_settings.offscreen_rendering_mode) {
3753 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3754 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3755 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3759 This->Flags |= SFLAG_INSYSMEM;
3764 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3765 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3766 IWineD3DBaseTexture *texture;
3768 TRACE("(%p)->(%s, %s)\n", iface,
3769 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3770 persistent ? "TRUE" : "FALSE");
3772 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3773 IWineD3DSwapChain *swapchain = NULL;
3775 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3776 TRACE("Surface %p is an onscreen surface\n", iface);
3778 IWineD3DSwapChain_Release(swapchain);
3780 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
3781 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3786 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3787 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3788 TRACE("Passing to container\n");
3789 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3790 IWineD3DBaseTexture_Release(texture);
3793 This->Flags &= ~SFLAG_LOCATIONS;
3794 This->Flags |= flag;
3796 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3797 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3798 TRACE("Passing to container\n");
3799 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3800 IWineD3DBaseTexture_Release(texture);
3803 This->Flags &= ~flag;
3811 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3812 struct coords coords[4];
3814 IWineD3DSwapChain *swapchain = NULL;
3815 IWineD3DBaseTexture *texture = NULL;
3817 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3824 rect.right = This->currentDesc.Width;
3825 rect.bottom = This->currentDesc.Height;
3828 ActivateContext(device, device->render_targets[0], CTXUSAGE_BLIT);
3831 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3832 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3833 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3834 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3835 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3836 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3837 checkGLcall("glTexParameteri");
3838 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3839 checkGLcall("glTexParameteri");
3841 coords[0].x = rect.left;
3844 coords[1].x = rect.left;
3847 coords[2].x = rect.right;
3850 coords[3].x = rect.right;
3853 coords[0].y = rect.top;
3854 coords[1].y = rect.bottom;
3855 coords[2].y = rect.bottom;
3856 coords[3].y = rect.top;
3857 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3858 glEnable(GL_TEXTURE_2D);
3859 checkGLcall("glEnable(GL_TEXTURE_2D)");
3860 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3861 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3862 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3863 checkGLcall("glTexParameteri");
3864 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3865 checkGLcall("glTexParameteri");
3867 coords[0].x = (float)rect.left / This->pow2Width;
3870 coords[1].x = (float)rect.left / This->pow2Width;
3873 coords[2].x = (float)rect.right / This->pow2Width;
3876 coords[3].x = (float)rect.right / This->pow2Width;
3879 coords[0].y = (float)rect.top / This->pow2Height;
3880 coords[1].y = (float)rect.bottom / This->pow2Height;
3881 coords[2].y = (float)rect.bottom / This->pow2Height;
3882 coords[3].y = (float)rect.top / This->pow2Height;
3884 /* Must be a cube map */
3885 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3886 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3887 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3888 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3889 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3890 checkGLcall("glTexParameteri");
3891 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3892 checkGLcall("glTexParameteri");
3894 switch(This->glDescription.target) {
3895 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3896 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3897 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3898 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3899 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3902 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3903 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3904 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3905 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3906 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3909 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3910 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3911 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3912 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3913 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3916 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3917 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3918 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3919 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3920 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3923 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3924 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3925 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3926 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3927 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3930 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3931 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3932 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3933 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3934 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3938 ERR("Unexpected texture target\n");
3945 glTexCoord3fv(&coords[0].x);
3946 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
3948 glTexCoord3fv(&coords[1].x);
3949 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
3951 glTexCoord3fv(&coords[2].x);
3952 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
3954 glTexCoord3fv(&coords[3].x);
3955 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
3957 checkGLcall("glEnd");
3959 if(This->glDescription.target != GL_TEXTURE_2D) {
3960 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
3961 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
3963 glDisable(GL_TEXTURE_2D);
3964 checkGLcall("glDisable(GL_TEXTURE_2D)");
3967 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
3968 if(hr == WINED3D_OK && swapchain) {
3969 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
3970 if(((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
3973 IWineD3DSwapChain_Release(swapchain);
3975 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
3976 * reset properly next draw
3978 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
3979 if(hr == WINED3D_OK && texture) {
3980 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
3981 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
3982 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
3983 IWineD3DBaseTexture_Release(texture);
3989 /*****************************************************************************
3990 * IWineD3DSurface::LoadLocation
3992 * Copies the current surface data from wherever it is to the requested
3993 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
3994 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
3995 * multiple locations, the gl texture is preferred over the drawable, which is
3996 * preferred over system memory. The PBO counts as system memory. If rect is
3997 * not NULL, only the specified rectangle is copied (only supported for
3998 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
3999 * location is marked up to date after the copy.
4002 * flag: Surface location flag to be updated
4003 * rect: rectangle to be copied
4006 * WINED3D_OK on success
4007 * WINED3DERR_DEVICELOST on an internal error
4009 *****************************************************************************/
4010 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4011 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4012 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4013 IWineD3DSwapChain *swapchain = NULL;
4014 GLenum format, internal, type;
4015 CONVERT_TYPES convert;
4017 int width, pitch, outpitch;
4020 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4021 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
4022 TRACE("Surface %p is an onscreen surface\n", iface);
4024 IWineD3DSwapChain_Release(swapchain);
4026 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4027 * Prefer SFLAG_INTEXTURE. */
4028 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4032 TRACE("(%p)->(%s, %p)\n", iface,
4033 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
4036 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4039 if(This->Flags & flag) {
4040 TRACE("Location already up to date\n");
4044 if(!(This->Flags & SFLAG_LOCATIONS)) {
4045 ERR("Surface does not have any up to date location\n");
4046 This->Flags |= SFLAG_LOST;
4047 return WINED3DERR_DEVICELOST;
4050 if(flag == SFLAG_INSYSMEM) {
4051 surface_prepare_system_memory(This);
4053 /* Download the surface to system memory */
4054 if(This->Flags & SFLAG_INTEXTURE) {
4055 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4056 surface_bind_and_dirtify(This);
4058 surface_download_data(This);
4060 read_from_framebuffer(This, rect,
4061 This->resource.allocatedMemory,
4062 IWineD3DSurface_GetPitch(iface));
4064 } else if(flag == SFLAG_INDRAWABLE) {
4065 if(This->Flags & SFLAG_INTEXTURE) {
4066 surface_blt_to_drawable(This, rect);
4068 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4070 /* The width is in 'length' not in bytes */
4071 width = This->currentDesc.Width;
4072 pitch = IWineD3DSurface_GetPitch(iface);
4074 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4075 * but it isn't set (yet) in all cases it is getting called. */
4076 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4077 TRACE("Removing the pbo attached to surface %p\n", This);
4078 surface_remove_pbo(This);
4081 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4082 int height = This->currentDesc.Height;
4084 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4085 outpitch = width * bpp;
4086 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4088 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4090 ERR("Out of memory %d, %d!\n", outpitch, height);
4091 return WINED3DERR_OUTOFVIDEOMEMORY;
4093 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4095 This->Flags |= SFLAG_CONVERTED;
4097 This->Flags &= ~SFLAG_CONVERTED;
4098 mem = This->resource.allocatedMemory;
4101 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4103 /* Don't delete PBO memory */
4104 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4105 HeapFree(GetProcessHeap(), 0, mem);
4107 } else /* if(flag == SFLAG_INTEXTURE) */ {
4108 if (This->Flags & SFLAG_INDRAWABLE) {
4109 read_from_framebuffer_texture(This);
4110 } else { /* Upload from system memory */
4111 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4113 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4114 surface_bind_and_dirtify(This);
4117 /* The only place where LoadTexture() might get called when isInDraw=1
4118 * is ActivateContext where lastActiveRenderTarget is preloaded.
4120 if(iface == device->lastActiveRenderTarget && device->isInDraw)
4121 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
4123 /* Otherwise: System memory copy must be most up to date */
4125 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4126 This->Flags |= SFLAG_GLCKEY;
4127 This->glCKey = This->SrcBltCKey;
4129 else This->Flags &= ~SFLAG_GLCKEY;
4131 /* The width is in 'length' not in bytes */
4132 width = This->currentDesc.Width;
4133 pitch = IWineD3DSurface_GetPitch(iface);
4135 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4136 * but it isn't set (yet) in all cases it is getting called. */
4137 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4138 TRACE("Removing the pbo attached to surface %p\n", This);
4139 surface_remove_pbo(This);
4142 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4143 int height = This->currentDesc.Height;
4145 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4146 outpitch = width * bpp;
4147 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4149 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4151 ERR("Out of memory %d, %d!\n", outpitch, height);
4152 return WINED3DERR_OUTOFVIDEOMEMORY;
4154 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4156 This->Flags |= SFLAG_CONVERTED;
4157 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
4158 d3dfmt_p8_upload_palette(iface, convert);
4159 This->Flags &= ~SFLAG_CONVERTED;
4160 mem = This->resource.allocatedMemory;
4162 This->Flags &= ~SFLAG_CONVERTED;
4163 mem = This->resource.allocatedMemory;
4166 /* Make sure the correct pitch is used */
4167 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4169 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4170 TRACE("non power of two support\n");
4171 if(!(This->Flags & SFLAG_ALLOCATED)) {
4172 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4174 if (mem || (This->Flags & SFLAG_PBO)) {
4175 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4178 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4179 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4181 if(!(This->Flags & SFLAG_ALLOCATED)) {
4182 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4184 if (mem || (This->Flags & SFLAG_PBO)) {
4185 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4189 /* Restore the default pitch */
4190 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4193 /* Don't delete PBO memory */
4194 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4195 HeapFree(GetProcessHeap(), 0, mem);
4200 This->Flags |= flag;
4203 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4204 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4205 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4206 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4212 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4213 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4214 IWineD3DSwapChain *swapchain = NULL;
4216 /* Update the drawable size method */
4218 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4221 This->get_drawable_size = get_drawable_size_swapchain;
4222 IWineD3DSwapChain_Release(swapchain);
4223 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4224 switch(wined3d_settings.offscreen_rendering_mode) {
4225 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4226 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4227 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4231 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4234 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4235 return SURFACE_OPENGL;
4238 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4241 IWineD3DBaseSurfaceImpl_QueryInterface,
4242 IWineD3DBaseSurfaceImpl_AddRef,
4243 IWineD3DSurfaceImpl_Release,
4244 /* IWineD3DResource */
4245 IWineD3DBaseSurfaceImpl_GetParent,
4246 IWineD3DBaseSurfaceImpl_GetDevice,
4247 IWineD3DBaseSurfaceImpl_SetPrivateData,
4248 IWineD3DBaseSurfaceImpl_GetPrivateData,
4249 IWineD3DBaseSurfaceImpl_FreePrivateData,
4250 IWineD3DBaseSurfaceImpl_SetPriority,
4251 IWineD3DBaseSurfaceImpl_GetPriority,
4252 IWineD3DSurfaceImpl_PreLoad,
4253 IWineD3DSurfaceImpl_UnLoad,
4254 IWineD3DBaseSurfaceImpl_GetType,
4255 /* IWineD3DSurface */
4256 IWineD3DBaseSurfaceImpl_GetContainer,
4257 IWineD3DBaseSurfaceImpl_GetDesc,
4258 IWineD3DSurfaceImpl_LockRect,
4259 IWineD3DSurfaceImpl_UnlockRect,
4260 IWineD3DSurfaceImpl_GetDC,
4261 IWineD3DSurfaceImpl_ReleaseDC,
4262 IWineD3DSurfaceImpl_Flip,
4263 IWineD3DSurfaceImpl_Blt,
4264 IWineD3DBaseSurfaceImpl_GetBltStatus,
4265 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4266 IWineD3DBaseSurfaceImpl_IsLost,
4267 IWineD3DBaseSurfaceImpl_Restore,
4268 IWineD3DSurfaceImpl_BltFast,
4269 IWineD3DBaseSurfaceImpl_GetPalette,
4270 IWineD3DBaseSurfaceImpl_SetPalette,
4271 IWineD3DSurfaceImpl_RealizePalette,
4272 IWineD3DBaseSurfaceImpl_SetColorKey,
4273 IWineD3DBaseSurfaceImpl_GetPitch,
4274 IWineD3DSurfaceImpl_SetMem,
4275 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4276 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4277 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4278 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4279 IWineD3DBaseSurfaceImpl_SetClipper,
4280 IWineD3DBaseSurfaceImpl_GetClipper,
4282 IWineD3DSurfaceImpl_AddDirtyRect,
4283 IWineD3DSurfaceImpl_LoadTexture,
4284 IWineD3DSurfaceImpl_BindTexture,
4285 IWineD3DSurfaceImpl_SaveSnapshot,
4286 IWineD3DSurfaceImpl_SetContainer,
4287 IWineD3DSurfaceImpl_SetGlTextureDesc,
4288 IWineD3DSurfaceImpl_GetGlDesc,
4289 IWineD3DSurfaceImpl_GetData,
4290 IWineD3DSurfaceImpl_SetFormat,
4291 IWineD3DSurfaceImpl_PrivateSetup,
4292 IWineD3DSurfaceImpl_ModifyLocation,
4293 IWineD3DSurfaceImpl_LoadLocation,
4294 IWineD3DSurfaceImpl_GetImplType