wined3d: Set the surface location to SFLAG_INSYSMEM for depth stencils on Reset.
[wine/hacks.git] / dlls / wined3d / surface.c
blobadddaa940c301cdfde1aa00d5490c21e0d9aea30
1 /*
2 * IWineD3DSurface Implementation
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11 * Copyright 2007 Henri Verbeet
12 * Copyright 2006-2008 Roderick Colenbrander
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
36 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
37 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey);
38 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert);
39 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This);
40 static void surface_remove_pbo(IWineD3DSurfaceImpl *This);
42 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This) {
43 GLint active_texture;
45 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
46 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
47 * gl states. The current texture unit should always be a valid one.
49 * TODO: Track the current active texture per GL context instead of using glGet
51 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
52 ENTER_GL();
53 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
54 LEAVE_GL();
55 active_texture -= GL_TEXTURE0_ARB;
56 } else {
57 active_texture = 0;
59 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_texture));
60 IWineD3DSurface_BindTexture((IWineD3DSurface *)This);
63 /* This function checks if the primary render target uses the 8bit paletted format. */
64 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
66 if (device->render_targets && device->render_targets[0]) {
67 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
68 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
69 return TRUE;
71 return FALSE;
74 /* This call just downloads data, the caller is responsible for activating the
75 * right context and binding the correct texture. */
76 static void surface_download_data(IWineD3DSurfaceImpl *This) {
77 if (0 == This->glDescription.textureName) {
78 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
79 return;
82 /* Only support read back of converted P8 surfaces */
83 if(This->Flags & SFLAG_CONVERTED && (This->resource.format != WINED3DFMT_P8)) {
84 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
85 return;
88 ENTER_GL();
90 if (This->resource.format == WINED3DFMT_DXT1 ||
91 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
92 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
93 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
94 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
95 } else {
96 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
97 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
99 if(This->Flags & SFLAG_PBO) {
100 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
101 checkGLcall("glBindBufferARB");
102 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
103 checkGLcall("glGetCompressedTexImageARB()");
104 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
105 checkGLcall("glBindBufferARB");
106 } else {
107 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
108 checkGLcall("glGetCompressedTexImageARB()");
111 LEAVE_GL();
112 } else {
113 void *mem;
114 GLenum format = This->glDescription.glFormat;
115 GLenum type = This->glDescription.glType;
116 int src_pitch = 0;
117 int dst_pitch = 0;
119 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
120 if(This->resource.format == WINED3DFMT_P8 && primary_render_target_is_p8(This->resource.wineD3DDevice)) {
121 format = GL_ALPHA;
122 type = GL_UNSIGNED_BYTE;
125 if (This->Flags & SFLAG_NONPOW2) {
126 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
127 src_pitch = This->bytesPerPixel * This->pow2Width;
128 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
129 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
130 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
131 } else {
132 mem = This->resource.allocatedMemory;
135 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
136 format, type, mem);
138 if(This->Flags & SFLAG_PBO) {
139 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
140 checkGLcall("glBindBufferARB");
142 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
143 type, NULL);
144 checkGLcall("glGetTexImage()");
146 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
147 checkGLcall("glBindBufferARB");
148 } else {
149 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
150 type, mem);
151 checkGLcall("glGetTexImage()");
153 LEAVE_GL();
155 if (This->Flags & SFLAG_NONPOW2) {
156 LPBYTE src_data, dst_data;
157 int y;
159 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
160 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
161 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
163 * We're doing this...
165 * instead of boxing the texture :
166 * |<-texture width ->| -->pow2width| /\
167 * |111111111111111111| | |
168 * |222 Texture 222222| boxed empty | texture height
169 * |3333 Data 33333333| | |
170 * |444444444444444444| | \/
171 * ----------------------------------- |
172 * | boxed empty | boxed empty | pow2height
173 * | | | \/
174 * -----------------------------------
177 * we're repacking the data to the expected texture width
179 * |<-texture width ->| -->pow2width| /\
180 * |111111111111111111222222222222222| |
181 * |222333333333333333333444444444444| texture height
182 * |444444 | |
183 * | | \/
184 * | | |
185 * | empty | pow2height
186 * | | \/
187 * -----------------------------------
189 * == is the same as
191 * |<-texture width ->| /\
192 * |111111111111111111|
193 * |222222222222222222|texture height
194 * |333333333333333333|
195 * |444444444444444444| \/
196 * --------------------
198 * this also means that any references to allocatedMemory should work with the data as if were a
199 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
201 * internally the texture is still stored in a boxed format so any references to textureName will
202 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
204 * Performance should not be an issue, because applications normally do not lock the surfaces when
205 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
206 * and doesn't have to be re-read.
208 src_data = mem;
209 dst_data = This->resource.allocatedMemory;
210 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
211 for (y = 1 ; y < This->currentDesc.Height; y++) {
212 /* skip the first row */
213 src_data += src_pitch;
214 dst_data += dst_pitch;
215 memcpy(dst_data, src_data, dst_pitch);
218 HeapFree(GetProcessHeap(), 0, mem);
222 /* Surface has now been downloaded */
223 This->Flags |= SFLAG_INSYSMEM;
226 /* This call just uploads data, the caller is responsible for activating the
227 * right context and binding the correct texture. */
228 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
229 if (This->resource.format == WINED3DFMT_DXT1 ||
230 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
231 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
232 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
233 FIXME("Using DXT1/3/5 without advertized support\n");
234 } else {
235 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
236 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
237 * function uses glCompressedTexImage2D instead of the SubImage call
239 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
240 ENTER_GL();
242 if(This->Flags & SFLAG_PBO) {
243 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
244 checkGLcall("glBindBufferARB");
245 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
247 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
248 width, height, 0 /* border */, This->resource.size, NULL));
249 checkGLcall("glCompressedTexSubImage2D");
251 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
252 checkGLcall("glBindBufferARB");
253 } else {
254 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
255 width, height, 0 /* border */, This->resource.size, data));
256 checkGLcall("glCompressedTexSubImage2D");
258 LEAVE_GL();
260 } else {
261 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
262 ENTER_GL();
264 if(This->Flags & SFLAG_PBO) {
265 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
266 checkGLcall("glBindBufferARB");
267 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
269 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
270 checkGLcall("glTexSubImage2D");
272 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
273 checkGLcall("glBindBufferARB");
275 else {
276 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
277 checkGLcall("glTexSubImage2D");
280 LEAVE_GL();
284 /* This call just allocates the texture, the caller is responsible for
285 * activating the right context and binding the correct texture. */
286 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
287 BOOL enable_client_storage = FALSE;
288 BYTE *mem = NULL;
290 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,
291 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
293 if (This->resource.format == WINED3DFMT_DXT1 ||
294 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
295 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5) {
296 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
297 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
299 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
300 * once, unfortunately
302 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
303 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
304 This->Flags |= SFLAG_CLIENT;
305 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
306 ENTER_GL();
307 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
308 width, height, 0 /* border */, This->resource.size, mem));
309 LEAVE_GL();
312 return;
315 ENTER_GL();
317 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
318 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
319 /* In some cases we want to disable client storage.
320 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
321 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
322 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
323 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
324 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
326 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
327 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
328 This->Flags &= ~SFLAG_CLIENT;
329 enable_client_storage = TRUE;
330 } else {
331 This->Flags |= SFLAG_CLIENT;
333 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
334 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
336 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
339 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
340 checkGLcall("glTexImage2D");
342 if(enable_client_storage) {
343 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
344 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
346 LEAVE_GL();
348 This->Flags |= SFLAG_ALLOCATED;
351 /* In D3D the depth stencil dimensions have to be greater than or equal to the
352 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
353 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
354 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
355 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
356 renderbuffer_entry_t *entry;
357 GLuint renderbuffer = 0;
358 unsigned int src_width, src_height;
360 src_width = This->pow2Width;
361 src_height = This->pow2Height;
363 /* A depth stencil smaller than the render target is not valid */
364 if (width > src_width || height > src_height) return;
366 /* Remove any renderbuffer set if the sizes match */
367 if (width == src_width && height == src_height) {
368 This->current_renderbuffer = NULL;
369 return;
372 /* Look if we've already got a renderbuffer of the correct dimensions */
373 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
374 if (entry->width == width && entry->height == height) {
375 renderbuffer = entry->id;
376 This->current_renderbuffer = entry;
377 break;
381 if (!renderbuffer) {
382 const GlPixelFormatDesc *glDesc;
383 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
385 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
386 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
387 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glFormat, width, height));
389 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
390 entry->width = width;
391 entry->height = height;
392 entry->id = renderbuffer;
393 list_add_head(&This->renderbuffers, &entry->entry);
395 This->current_renderbuffer = entry;
398 checkGLcall("set_compatible_renderbuffer");
401 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
402 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
403 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
405 TRACE("(%p) : swapchain %p\n", This, swapchain);
407 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
408 TRACE("Returning GL_BACK\n");
409 return GL_BACK;
410 } else if (swapchain_impl->frontBuffer == iface) {
411 TRACE("Returning GL_FRONT\n");
412 return GL_FRONT;
415 FIXME("Higher back buffer, returning GL_BACK\n");
416 return GL_BACK;
419 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
420 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
421 ULONG ref = InterlockedDecrement(&This->resource.ref);
422 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
423 if (ref == 0) {
424 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
425 renderbuffer_entry_t *entry, *entry2;
426 TRACE("(%p) : cleaning up\n", This);
428 /* Need a context to destroy the texture. Use the currently active render target, but only if
429 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
430 * When destroying the primary rt, Uninit3D will activate a context before doing anything
432 if(device->render_targets && device->render_targets[0]) {
433 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
436 ENTER_GL();
437 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
438 TRACE("Deleting texture %d\n", This->glDescription.textureName);
439 glDeleteTextures(1, &This->glDescription.textureName);
442 if(This->Flags & SFLAG_PBO) {
443 /* Delete the PBO */
444 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
447 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
448 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
449 HeapFree(GetProcessHeap(), 0, entry);
451 LEAVE_GL();
453 if(This->Flags & SFLAG_DIBSECTION) {
454 /* Release the DC */
455 SelectObject(This->hDC, This->dib.holdbitmap);
456 DeleteDC(This->hDC);
457 /* Release the DIB section */
458 DeleteObject(This->dib.DIBsection);
459 This->dib.bitmap_data = NULL;
460 This->resource.allocatedMemory = NULL;
462 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
464 HeapFree(GetProcessHeap(), 0, This->palette9);
466 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
467 if(iface == device->ddraw_primary)
468 device->ddraw_primary = NULL;
470 TRACE("(%p) Released\n", This);
471 HeapFree(GetProcessHeap(), 0, This);
474 return ref;
477 /* ****************************************************
478 IWineD3DSurface IWineD3DResource parts follow
479 **************************************************** */
481 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
482 /* TODO: check for locks */
483 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
484 IWineD3DBaseTexture *baseTexture = NULL;
485 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
487 TRACE("(%p)Checking to see if the container is a base texture\n", This);
488 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
489 TRACE("Passing to container\n");
490 IWineD3DBaseTexture_PreLoad(baseTexture);
491 IWineD3DBaseTexture_Release(baseTexture);
492 } else {
493 TRACE("(%p) : About to load surface\n", This);
495 if(!device->isInDraw) {
496 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
499 if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
500 if(palette9_changed(This)) {
501 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
502 /* TODO: This is not necessarily needed with hw palettized texture support */
503 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
504 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
505 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
508 ENTER_GL();
509 glEnable(This->glDescription.target);/* make sure texture support is enabled in this context */
510 if (!This->glDescription.level) {
511 if (!This->glDescription.textureName) {
512 glGenTextures(1, &This->glDescription.textureName);
513 checkGLcall("glGenTextures");
514 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
516 glBindTexture(This->glDescription.target, This->glDescription.textureName);
517 checkGLcall("glBindTexture");
518 LEAVE_GL();
519 IWineD3DSurface_LoadTexture(iface, FALSE);
520 /* This is where we should be reducing the amount of GLMemoryUsed */
521 } else if (This->glDescription.textureName) { /* NOTE: the level 0 surface of a mpmapped texture must be loaded first! */
522 /* assume this is a coding error not a real error for now */
523 FIXME("Mipmap surface has a glTexture bound to it!\n");
524 LEAVE_GL();
526 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
527 /* Tell opengl to try and keep this texture in video ram (well mostly) */
528 GLclampf tmp;
529 tmp = 0.9f;
530 ENTER_GL();
531 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
532 LEAVE_GL();
535 return;
538 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
539 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
540 This->resource.allocatedMemory =
541 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
543 ENTER_GL();
544 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
545 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
546 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
547 checkGLcall("glGetBufferSubData");
548 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
549 checkGLcall("glDeleteBuffers");
550 LEAVE_GL();
552 This->pbo = 0;
553 This->Flags &= ~SFLAG_PBO;
556 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
557 IWineD3DBaseTexture *texture = NULL;
558 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
559 renderbuffer_entry_t *entry, *entry2;
560 TRACE("(%p)\n", iface);
562 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
563 /* Default pool resources are supposed to be destroyed before Reset is called.
564 * Implicit resources stay however. So this means we have an implicit render target
565 * or depth stencil. The content may be destroyed, but we still have to tear down
566 * opengl resources, so we cannot leave early.
568 * Put the most up to date surface location into the drawable. D3D-wise this content
569 * is undefined, so it would be nowhere, but that would make the location management
570 * more complicated. The drawable is a sane location, because if we mark sysmem or
571 * texture up to date, drawPrim will copy the uninitialized texture or sysmem to the
572 * uninitialized drawable. That's pointless and we'd have to allocate the texture /
573 * sysmem copy here.
575 if (This->resource.usage & WINED3DUSAGE_DEPTHSTENCIL) {
576 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
577 } else {
578 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, TRUE);
580 } else {
581 /* Load the surface into system memory */
582 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
583 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
585 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
586 This->Flags &= ~SFLAG_ALLOCATED;
588 /* Destroy PBOs, but load them into real sysmem before */
589 if(This->Flags & SFLAG_PBO) {
590 surface_remove_pbo(This);
593 /* Destroy fbo render buffers. This is needed for implicit render targets, for
594 * all application-created targets the application has to release the surface
595 * before calling _Reset
597 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
598 ENTER_GL();
599 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
600 LEAVE_GL();
601 list_remove(&entry->entry);
602 HeapFree(GetProcessHeap(), 0, entry);
604 list_init(&This->renderbuffers);
605 This->current_renderbuffer = NULL;
607 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
608 * destroy it
610 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
611 if(!texture) {
612 ENTER_GL();
613 glDeleteTextures(1, &This->glDescription.textureName);
614 This->glDescription.textureName = 0;
615 LEAVE_GL();
616 } else {
617 IWineD3DBaseTexture_Release(texture);
619 return;
622 /* ******************************************************
623 IWineD3DSurface IWineD3DSurface parts follow
624 ****************************************************** */
626 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
627 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
628 TRACE("(%p) : setting textureName %u, target %i\n", This, textureName, target);
629 if (This->glDescription.textureName == 0 && textureName != 0) {
630 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
631 IWineD3DSurface_AddDirtyRect(iface, NULL);
633 if(target == GL_TEXTURE_RECTANGLE_ARB && This->glDescription.target != target) {
634 This->Flags &= ~SFLAG_NORMCOORD;
635 } else if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB && target != GL_TEXTURE_RECTANGLE_ARB) {
636 This->Flags |= SFLAG_NORMCOORD;
638 This->glDescription.textureName = textureName;
639 This->glDescription.target = target;
640 This->Flags &= ~SFLAG_ALLOCATED;
643 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
644 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
645 TRACE("(%p) : returning %p\n", This, &This->glDescription);
646 *glDescription = &This->glDescription;
649 /* TODO: think about moving this down to resource? */
650 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
651 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
652 /* 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 */
653 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
654 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
656 return (CONST void*)(This->resource.allocatedMemory);
659 /* Read the framebuffer back into the surface */
660 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
661 IWineD3DSwapChainImpl *swapchain;
662 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
663 BYTE *mem;
664 GLint fmt;
665 GLint type;
666 BYTE *row, *top, *bottom;
667 int i;
668 BOOL bpp;
669 RECT local_rect;
670 BOOL srcIsUpsideDown;
672 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
673 static BOOL warned = FALSE;
674 if(!warned) {
675 ERR("The application tries to lock the render target, but render target locking is disabled\n");
676 warned = TRUE;
678 return;
681 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
682 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
683 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
684 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
685 * context->last_was_blit set on the unlock.
687 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
688 ENTER_GL();
690 /* Select the correct read buffer, and give some debug output.
691 * There is no need to keep track of the current read buffer or reset it, every part of the code
692 * that reads sets the read buffer as desired.
694 if(!swapchain) {
695 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
696 * Read from the back buffer
698 TRACE("Locking offscreen render target\n");
699 glReadBuffer(myDevice->offscreenBuffer);
700 srcIsUpsideDown = TRUE;
701 } else {
702 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
703 TRACE("Locking %#x buffer\n", buffer);
704 glReadBuffer(buffer);
705 checkGLcall("glReadBuffer");
707 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
708 srcIsUpsideDown = FALSE;
711 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
712 if(!rect) {
713 local_rect.left = 0;
714 local_rect.top = 0;
715 local_rect.right = This->currentDesc.Width;
716 local_rect.bottom = This->currentDesc.Height;
717 } else {
718 local_rect = *rect;
720 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
722 switch(This->resource.format)
724 case WINED3DFMT_P8:
726 if(primary_render_target_is_p8(myDevice)) {
727 /* In case of P8 render targets the index is stored in the alpha component */
728 fmt = GL_ALPHA;
729 type = GL_UNSIGNED_BYTE;
730 mem = dest;
731 bpp = This->bytesPerPixel;
732 } else {
733 /* GL can't return palettized data, so read ARGB pixels into a
734 * separate block of memory and convert them into palettized format
735 * in software. Slow, but if the app means to use palettized render
736 * targets and locks it...
738 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
739 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
740 * for the color channels when palettizing the colors.
742 fmt = GL_RGB;
743 type = GL_UNSIGNED_BYTE;
744 pitch *= 3;
745 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
746 if(!mem) {
747 ERR("Out of memory\n");
748 LEAVE_GL();
749 return;
751 bpp = This->bytesPerPixel * 3;
754 break;
756 default:
757 mem = dest;
758 fmt = This->glDescription.glFormat;
759 type = This->glDescription.glType;
760 bpp = This->bytesPerPixel;
763 if(This->Flags & SFLAG_PBO) {
764 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
765 checkGLcall("glBindBufferARB");
768 glReadPixels(local_rect.left, local_rect.top,
769 local_rect.right - local_rect.left,
770 local_rect.bottom - local_rect.top,
771 fmt, type, mem);
772 vcheckGLcall("glReadPixels");
774 if(This->Flags & SFLAG_PBO) {
775 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
776 checkGLcall("glBindBufferARB");
778 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
779 * to get a pointer to it and perform the flipping in software. This is a lot
780 * faster than calling glReadPixels for each line. In case we want more speed
781 * we should rerender it flipped in a FBO and read the data back from the FBO. */
782 if(!srcIsUpsideDown) {
783 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
784 checkGLcall("glBindBufferARB");
786 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
787 checkGLcall("glMapBufferARB");
791 /* TODO: Merge this with the palettization loop below for P8 targets */
792 if(!srcIsUpsideDown) {
793 UINT len, off;
794 /* glReadPixels returns the image upside down, and there is no way to prevent this.
795 Flip the lines in software */
796 len = (local_rect.right - local_rect.left) * bpp;
797 off = local_rect.left * bpp;
799 row = HeapAlloc(GetProcessHeap(), 0, len);
800 if(!row) {
801 ERR("Out of memory\n");
802 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
803 LEAVE_GL();
804 return;
807 top = mem + pitch * local_rect.top;
808 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
809 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
810 memcpy(row, top + off, len);
811 memcpy(top + off, bottom + off, len);
812 memcpy(bottom + off, row, len);
813 top += pitch;
814 bottom -= pitch;
816 HeapFree(GetProcessHeap(), 0, row);
818 /* Unmap the temp PBO buffer */
819 if(This->Flags & SFLAG_PBO) {
820 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
821 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
825 LEAVE_GL();
827 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
828 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
829 * the same color but we have no choice.
830 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
832 if((This->resource.format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice)) {
833 PALETTEENTRY *pal = NULL;
834 DWORD width = pitch / 3;
835 int x, y, c;
837 if(This->palette) {
838 pal = This->palette->palents;
839 } else {
840 ERR("Palette is missing, cannot perform inverse palette lookup\n");
841 HeapFree(GetProcessHeap(), 0, mem);
842 return ;
845 for(y = local_rect.top; y < local_rect.bottom; y++) {
846 for(x = local_rect.left; x < local_rect.right; x++) {
847 /* start lines pixels */
848 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
849 BYTE *green = blue + 1;
850 BYTE *red = green + 1;
852 for(c = 0; c < 256; c++) {
853 if(*red == pal[c].peRed &&
854 *green == pal[c].peGreen &&
855 *blue == pal[c].peBlue)
857 *((BYTE *) dest + y * width + x) = c;
858 break;
863 HeapFree(GetProcessHeap(), 0, mem);
867 /* Read the framebuffer contents into a texture */
868 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
870 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
871 IWineD3DSwapChainImpl *swapchain;
872 int bpp;
873 GLenum format, internal, type;
874 CONVERT_TYPES convert;
875 GLint prevRead;
877 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
879 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
880 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
881 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
882 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
883 * context->last_was_blit set on the unlock.
885 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
886 surface_bind_and_dirtify(This);
887 ENTER_GL();
889 glGetIntegerv(GL_READ_BUFFER, &prevRead);
891 /* Select the correct read buffer, and give some debug output.
892 * There is no need to keep track of the current read buffer or reset it, every part of the code
893 * that reads sets the read buffer as desired.
895 if(!swapchain) {
896 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
897 * Read from the back buffer
899 TRACE("Locking offscreen render target\n");
900 glReadBuffer(device->offscreenBuffer);
901 } else {
902 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
903 TRACE("Locking %#x buffer\n", buffer);
904 glReadBuffer(buffer);
905 checkGLcall("glReadBuffer");
907 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
910 if(!(This->Flags & SFLAG_ALLOCATED)) {
911 surface_allocate_surface(This, internal, This->pow2Width,
912 This->pow2Height, format, type);
915 clear_unused_channels(This);
917 /* If !SrcIsUpsideDown we should flip the surface.
918 * This can be done using glCopyTexSubImage2D but this
919 * is VERY slow, so don't do that. We should prevent
920 * this code from getting called in such cases or perhaps
921 * we can use FBOs */
923 glCopyTexSubImage2D(This->glDescription.target,
924 This->glDescription.level,
925 0, 0, 0, 0,
926 This->currentDesc.Width,
927 This->currentDesc.Height);
928 checkGLcall("glCopyTexSubImage2D");
930 glReadBuffer(prevRead);
931 vcheckGLcall("glReadBuffer");
933 LEAVE_GL();
934 TRACE("Updated target %d\n", This->glDescription.target);
937 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
938 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
939 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
940 * changed
942 if(!(This->Flags & SFLAG_DYNLOCK)) {
943 This->lockCount++;
944 /* MAXLOCKCOUNT is defined in wined3d_private.h */
945 if(This->lockCount > MAXLOCKCOUNT) {
946 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
947 This->Flags |= SFLAG_DYNLOCK;
951 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
952 * Also don't create a PBO for systemmem surfaces.
954 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
955 GLenum error;
956 ENTER_GL();
958 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
959 error = glGetError();
960 if(This->pbo == 0 || error != GL_NO_ERROR) {
961 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
964 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
966 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
967 checkGLcall("glBindBufferARB");
969 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
970 checkGLcall("glBufferDataARB");
972 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
973 checkGLcall("glBindBufferARB");
975 /* We don't need the system memory anymore and we can't even use it for PBOs */
976 if(!(This->Flags & SFLAG_CLIENT)) {
977 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
978 This->resource.heapMemory = NULL;
980 This->resource.allocatedMemory = NULL;
981 This->Flags |= SFLAG_PBO;
982 LEAVE_GL();
983 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
984 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
985 * or a pbo to map
987 if(!This->resource.heapMemory) {
988 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
990 This->resource.allocatedMemory =
991 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
992 if(This->Flags & SFLAG_INSYSMEM) {
993 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
998 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
999 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1000 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1001 IWineD3DSwapChain *swapchain = NULL;
1003 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1005 /* This is also done in the base class, but we have to verify this before loading any data from
1006 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1007 * may interfere, and all other bad things may happen
1009 if (This->Flags & SFLAG_LOCKED) {
1010 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1011 return WINED3DERR_INVALIDCALL;
1013 This->Flags |= SFLAG_LOCKED;
1015 if (!(This->Flags & SFLAG_LOCKABLE))
1017 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1020 if (Flags & WINED3DLOCK_DISCARD) {
1021 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1022 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1023 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1024 This->Flags |= SFLAG_INSYSMEM;
1025 goto lock_end;
1028 if (This->Flags & SFLAG_INSYSMEM) {
1029 TRACE("Local copy is up to date, not downloading data\n");
1030 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1031 goto lock_end;
1034 /* Now download the surface content from opengl
1035 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1036 * Offscreen targets which are not active at the moment or are higher targets(FBOs) can be locked with the texture path
1038 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1039 if(swapchain || iface == myDevice->render_targets[0]) {
1040 const RECT *pass_rect = pRect;
1042 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1043 * because most caller functions do not need that. So do that here
1045 if(pRect &&
1046 pRect->top == 0 &&
1047 pRect->left == 0 &&
1048 pRect->right == This->currentDesc.Width &&
1049 pRect->bottom == This->currentDesc.Height) {
1050 pass_rect = NULL;
1053 switch(wined3d_settings.rendertargetlock_mode) {
1054 case RTL_TEXDRAW:
1055 case RTL_TEXTEX:
1056 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1057 #if 0
1058 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1059 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1060 * This may be faster on some cards
1062 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1063 #endif
1064 /* drop through */
1066 case RTL_AUTO:
1067 case RTL_READDRAW:
1068 case RTL_READTEX:
1069 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1070 break;
1072 case RTL_DISABLE:
1073 break;
1075 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1077 } else if(iface == myDevice->stencilBufferTarget) {
1078 /** the depth stencil in openGL has a format of GL_FLOAT
1079 * which should be good for WINED3DFMT_D16_LOCKABLE
1080 * and WINED3DFMT_D16
1081 * it is unclear what format the stencil buffer is in except.
1082 * 'Each index is converted to fixed point...
1083 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1084 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1085 * glReadPixels(This->lockedRect.left,
1086 * This->lockedRect.bottom - j - 1,
1087 * This->lockedRect.right - This->lockedRect.left,
1088 * 1,
1089 * GL_DEPTH_COMPONENT,
1090 * type,
1091 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1093 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1094 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1095 * none of that is the case the problem is not in this function :-)
1096 ********************************************/
1097 FIXME("Depth stencil locking not supported yet\n");
1098 } else {
1099 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1100 TRACE("locking an ordinary surface\n");
1101 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1104 lock_end:
1105 if(This->Flags & SFLAG_PBO) {
1106 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1107 ENTER_GL();
1108 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1109 checkGLcall("glBindBufferARB");
1111 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1112 if(This->resource.allocatedMemory) {
1113 ERR("The surface already has PBO memory allocated!\n");
1116 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1117 checkGLcall("glMapBufferARB");
1119 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1120 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1121 checkGLcall("glBindBufferARB");
1123 LEAVE_GL();
1126 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1127 /* Don't dirtify */
1128 } else {
1129 IWineD3DBaseTexture *pBaseTexture;
1131 * Dirtify on lock
1132 * as seen in msdn docs
1134 IWineD3DSurface_AddDirtyRect(iface, pRect);
1136 /** Dirtify Container if needed */
1137 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1138 TRACE("Making container dirty\n");
1139 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1140 IWineD3DBaseTexture_Release(pBaseTexture);
1141 } else {
1142 TRACE("Surface is standalone, no need to dirty the container\n");
1146 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1149 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1150 GLint prev_store;
1151 GLint prev_rasterpos[4];
1152 GLint skipBytes = 0;
1153 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1154 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1155 IWineD3DSwapChainImpl *swapchain;
1157 /* Activate the correct context for the render target */
1158 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1159 ENTER_GL();
1161 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1162 if(!swapchain) {
1163 /* Primary offscreen render target */
1164 TRACE("Offscreen render target\n");
1165 glDrawBuffer(myDevice->offscreenBuffer);
1166 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1167 } else {
1168 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1169 TRACE("Unlocking %#x buffer\n", buffer);
1170 glDrawBuffer(buffer);
1171 checkGLcall("glDrawBuffer");
1173 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1176 glFlush();
1177 vcheckGLcall("glFlush");
1178 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1179 vcheckGLcall("glIntegerv");
1180 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1181 vcheckGLcall("glIntegerv");
1182 glPixelZoom(1.0, -1.0);
1183 vcheckGLcall("glPixelZoom");
1185 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1186 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1187 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1189 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1190 vcheckGLcall("glRasterPos2f");
1192 /* Some drivers(radeon dri, others?) don't like exceptions during
1193 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1194 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1195 * catch to put the dib section in InSync mode, which leads to a crash
1196 * and a blocked x server on my radeon card.
1198 * The following lines read the dib section so it is put in InSync mode
1199 * before glDrawPixels is called and the crash is prevented. There won't
1200 * be any interfering gdi accesses, because UnlockRect is called from
1201 * ReleaseDC, and the app won't use the dc any more afterwards.
1203 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1204 volatile BYTE read;
1205 read = This->resource.allocatedMemory[0];
1208 if(This->Flags & SFLAG_PBO) {
1209 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1210 checkGLcall("glBindBufferARB");
1213 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1214 if(This->Flags & SFLAG_LOCKED) {
1215 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1216 (This->lockedRect.bottom - This->lockedRect.top)-1,
1217 fmt, type,
1218 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1219 checkGLcall("glDrawPixels");
1220 } else {
1221 glDrawPixels(This->currentDesc.Width,
1222 This->currentDesc.Height,
1223 fmt, type, mem);
1224 checkGLcall("glDrawPixels");
1227 if(This->Flags & SFLAG_PBO) {
1228 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1229 checkGLcall("glBindBufferARB");
1232 glPixelZoom(1.0,1.0);
1233 vcheckGLcall("glPixelZoom");
1235 glRasterPos3iv(&prev_rasterpos[0]);
1236 vcheckGLcall("glRasterPos3iv");
1238 /* Reset to previous pack row length */
1239 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1240 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1242 if(!swapchain) {
1243 glDrawBuffer(myDevice->offscreenBuffer);
1244 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1245 } else if(swapchain->backBuffer) {
1246 glDrawBuffer(GL_BACK);
1247 checkGLcall("glDrawBuffer(GL_BACK)");
1248 } else {
1249 glDrawBuffer(GL_FRONT);
1250 checkGLcall("glDrawBuffer(GL_FRONT)");
1252 LEAVE_GL();
1254 return;
1257 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1258 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1259 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1260 IWineD3DSwapChainImpl *swapchain = NULL;
1261 BOOL fullsurface;
1263 if (!(This->Flags & SFLAG_LOCKED)) {
1264 WARN("trying to Unlock an unlocked surf@%p\n", This);
1265 return WINED3DERR_INVALIDCALL;
1268 if (This->Flags & SFLAG_PBO) {
1269 TRACE("Freeing PBO memory\n");
1270 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1271 ENTER_GL();
1272 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1273 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1274 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1275 checkGLcall("glUnmapBufferARB");
1276 LEAVE_GL();
1277 This->resource.allocatedMemory = NULL;
1280 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1282 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1283 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1284 goto unlock_end;
1287 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1288 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1289 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1291 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1292 static BOOL warned = FALSE;
1293 if(!warned) {
1294 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1295 warned = TRUE;
1297 goto unlock_end;
1300 if(This->dirtyRect.left == 0 &&
1301 This->dirtyRect.top == 0 &&
1302 This->dirtyRect.right == This->currentDesc.Width &&
1303 This->dirtyRect.bottom == This->currentDesc.Height) {
1304 fullsurface = TRUE;
1305 } else {
1306 /* TODO: Proper partial rectangle tracking */
1307 fullsurface = FALSE;
1308 This->Flags |= SFLAG_INSYSMEM;
1311 switch(wined3d_settings.rendertargetlock_mode) {
1312 case RTL_READTEX:
1313 case RTL_TEXTEX:
1314 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1315 ENTER_GL();
1316 if (This->glDescription.textureName == 0) {
1317 glGenTextures(1, &This->glDescription.textureName);
1318 checkGLcall("glGenTextures");
1320 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1321 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1322 LEAVE_GL();
1323 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1324 /* drop through */
1326 case RTL_AUTO:
1327 case RTL_READDRAW:
1328 case RTL_TEXDRAW:
1329 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1330 break;
1333 if(!fullsurface) {
1334 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1335 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1336 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1337 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1338 * not fully up to date because only a subrectangle was read in LockRect.
1340 This->Flags &= ~SFLAG_INSYSMEM;
1341 This->Flags |= SFLAG_INDRAWABLE;
1344 This->dirtyRect.left = This->currentDesc.Width;
1345 This->dirtyRect.top = This->currentDesc.Height;
1346 This->dirtyRect.right = 0;
1347 This->dirtyRect.bottom = 0;
1348 } else if(iface == myDevice->stencilBufferTarget) {
1349 FIXME("Depth Stencil buffer locking is not implemented\n");
1350 } else {
1351 /* The rest should be a normal texture */
1352 IWineD3DBaseTextureImpl *impl;
1353 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1354 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1355 * states need resetting
1357 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1358 if(impl->baseTexture.bindCount) {
1359 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1361 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1365 unlock_end:
1366 This->Flags &= ~SFLAG_LOCKED;
1367 memset(&This->lockedRect, 0, sizeof(RECT));
1368 return WINED3D_OK;
1371 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1372 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1373 WINED3DLOCKED_RECT lock;
1374 HRESULT hr;
1375 RGBQUAD col[256];
1377 TRACE("(%p)->(%p)\n",This,pHDC);
1379 if(This->Flags & SFLAG_USERPTR) {
1380 ERR("Not supported on surfaces with an application-provided surfaces\n");
1381 return WINEDDERR_NODC;
1384 /* Give more detailed info for ddraw */
1385 if (This->Flags & SFLAG_DCINUSE)
1386 return WINEDDERR_DCALREADYCREATED;
1388 /* Can't GetDC if the surface is locked */
1389 if (This->Flags & SFLAG_LOCKED)
1390 return WINED3DERR_INVALIDCALL;
1392 /* According to Direct3D9 docs, only these formats are supported */
1393 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1394 if (This->resource.format != WINED3DFMT_R5G6B5 &&
1395 This->resource.format != WINED3DFMT_X1R5G5B5 &&
1396 This->resource.format != WINED3DFMT_R8G8B8 &&
1397 This->resource.format != WINED3DFMT_X8R8G8B8) return WINED3DERR_INVALIDCALL;
1400 memset(&lock, 0, sizeof(lock)); /* To be sure */
1402 /* Create a DIB section if there isn't a hdc yet */
1403 if(!This->hDC) {
1404 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1405 if(This->Flags & SFLAG_CLIENT) {
1406 IWineD3DSurface_PreLoad(iface);
1409 /* Use the dib section from now on if we are not using a PBO */
1410 if(!(This->Flags & SFLAG_PBO))
1411 This->resource.allocatedMemory = This->dib.bitmap_data;
1414 /* Lock the surface */
1415 hr = IWineD3DSurface_LockRect(iface,
1416 &lock,
1417 NULL,
1420 if(This->Flags & SFLAG_PBO) {
1421 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1422 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1425 if(FAILED(hr)) {
1426 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1427 /* keep the dib section */
1428 return hr;
1431 if(This->resource.format == WINED3DFMT_P8 ||
1432 This->resource.format == WINED3DFMT_A8P8) {
1433 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1434 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1435 unsigned int n;
1436 PALETTEENTRY *pal = NULL;
1438 if(This->palette) {
1439 pal = This->palette->palents;
1440 } else {
1441 IWineD3DSurfaceImpl *dds_primary = (IWineD3DSurfaceImpl *)This->resource.wineD3DDevice->ddraw_primary;
1442 if (dds_primary && dds_primary->palette)
1443 pal = dds_primary->palette->palents;
1446 if (pal) {
1447 for (n=0; n<256; n++) {
1448 col[n].rgbRed = pal[n].peRed;
1449 col[n].rgbGreen = pal[n].peGreen;
1450 col[n].rgbBlue = pal[n].peBlue;
1451 col[n].rgbReserved = 0;
1453 SetDIBColorTable(This->hDC, 0, 256, col);
1457 *pHDC = This->hDC;
1458 TRACE("returning %p\n",*pHDC);
1459 This->Flags |= SFLAG_DCINUSE;
1461 return WINED3D_OK;
1464 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1465 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1467 TRACE("(%p)->(%p)\n",This,hDC);
1469 if (!(This->Flags & SFLAG_DCINUSE))
1470 return WINED3DERR_INVALIDCALL;
1472 if (This->hDC !=hDC) {
1473 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1474 return WINED3DERR_INVALIDCALL;
1477 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1478 /* Copy the contents of the DIB over to the PBO */
1479 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1482 /* we locked first, so unlock now */
1483 IWineD3DSurface_UnlockRect(iface);
1485 This->Flags &= ~SFLAG_DCINUSE;
1487 return WINED3D_OK;
1490 /* ******************************************************
1491 IWineD3DSurface Internal (No mapping to directx api) parts follow
1492 ****************************************************** */
1494 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) {
1495 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1496 const GlPixelFormatDesc *glDesc;
1497 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1498 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1500 /* Default values: From the surface */
1501 *format = glDesc->glFormat;
1502 *type = glDesc->glType;
1503 *convert = NO_CONVERSION;
1504 *target_bpp = This->bytesPerPixel;
1506 if(srgb_mode) {
1507 *internal = glDesc->glGammaInternal;
1508 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1509 *internal = glDesc->rtInternal;
1510 } else {
1511 *internal = glDesc->glInternal;
1514 /* Ok, now look if we have to do any conversion */
1515 switch(This->resource.format) {
1516 case WINED3DFMT_P8:
1517 /* ****************
1518 Paletted Texture
1519 **************** */
1521 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1522 * of the two is available make sure texturing is requested as neither of the two works in
1523 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1524 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1525 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1526 * conflicts with this.
1528 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && primary_render_target_is_p8(device))) || colorkey_active || !use_texturing ) {
1529 *format = GL_RGBA;
1530 *internal = GL_RGBA;
1531 *type = GL_UNSIGNED_BYTE;
1532 *target_bpp = 4;
1533 if(colorkey_active) {
1534 *convert = CONVERT_PALETTED_CK;
1535 } else {
1536 *convert = CONVERT_PALETTED;
1539 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1540 *format = GL_ALPHA;
1541 *internal = GL_RGBA;
1542 *type = GL_UNSIGNED_BYTE;
1543 *target_bpp = 1;
1546 break;
1548 case WINED3DFMT_R3G3B2:
1549 /* **********************
1550 GL_UNSIGNED_BYTE_3_3_2
1551 ********************** */
1552 if (colorkey_active) {
1553 /* This texture format will never be used.. So do not care about color keying
1554 up until the point in time it will be needed :-) */
1555 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1557 break;
1559 case WINED3DFMT_R5G6B5:
1560 if (colorkey_active) {
1561 *convert = CONVERT_CK_565;
1562 *format = GL_RGBA;
1563 *internal = GL_RGBA;
1564 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1566 break;
1568 case WINED3DFMT_X1R5G5B5:
1569 if (colorkey_active) {
1570 *convert = CONVERT_CK_5551;
1571 *format = GL_BGRA;
1572 *internal = GL_RGBA;
1573 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1575 break;
1577 case WINED3DFMT_R8G8B8:
1578 if (colorkey_active) {
1579 *convert = CONVERT_CK_RGB24;
1580 *format = GL_RGBA;
1581 *internal = GL_RGBA;
1582 *type = GL_UNSIGNED_INT_8_8_8_8;
1583 *target_bpp = 4;
1585 break;
1587 case WINED3DFMT_X8R8G8B8:
1588 if (colorkey_active) {
1589 *convert = CONVERT_RGB32_888;
1590 *format = GL_RGBA;
1591 *internal = GL_RGBA;
1592 *type = GL_UNSIGNED_INT_8_8_8_8;
1594 break;
1596 case WINED3DFMT_V8U8:
1597 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1598 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1599 *format = GL_DUDV_ATI;
1600 *internal = GL_DU8DV8_ATI;
1601 *type = GL_BYTE;
1602 /* No conversion - Just change the gl type */
1603 break;
1605 *convert = CONVERT_V8U8;
1606 *format = GL_BGR;
1607 *internal = GL_RGB8;
1608 *type = GL_UNSIGNED_BYTE;
1609 *target_bpp = 3;
1610 break;
1612 case WINED3DFMT_L6V5U5:
1613 *convert = CONVERT_L6V5U5;
1614 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1615 *target_bpp = 3;
1616 /* Use format and types from table */
1617 } else {
1618 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1619 *target_bpp = 2;
1620 *format = GL_RGB;
1621 *internal = GL_RGB5;
1622 *type = GL_UNSIGNED_SHORT_5_6_5;
1624 break;
1626 case WINED3DFMT_X8L8V8U8:
1627 *convert = CONVERT_X8L8V8U8;
1628 *target_bpp = 4;
1629 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1630 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1631 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1632 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1633 * the needed type and format parameter, so the internal format contains a
1634 * 4th component, which is returned as alpha
1636 } else {
1637 /* Not supported by GL_ATI_envmap_bumpmap */
1638 *format = GL_BGRA;
1639 *internal = GL_RGB8;
1640 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1642 break;
1644 case WINED3DFMT_Q8W8V8U8:
1645 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1646 *convert = CONVERT_Q8W8V8U8;
1647 *format = GL_BGRA;
1648 *internal = GL_RGBA8;
1649 *type = GL_UNSIGNED_BYTE;
1650 *target_bpp = 4;
1651 /* Not supported by GL_ATI_envmap_bumpmap */
1652 break;
1654 case WINED3DFMT_V16U16:
1655 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1656 *convert = CONVERT_V16U16;
1657 *format = GL_BGR;
1658 *internal = GL_RGB16_EXT;
1659 *type = GL_UNSIGNED_SHORT;
1660 *target_bpp = 6;
1661 /* What should I do here about GL_ATI_envmap_bumpmap?
1662 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1664 break;
1666 case WINED3DFMT_A4L4:
1667 /* A4L4 exists as an internal gl format, but for some reason there is not
1668 * format+type combination to load it. Thus convert it to A8L8, then load it
1669 * with A4L4 internal, but A8L8 format+type
1671 *convert = CONVERT_A4L4;
1672 *format = GL_LUMINANCE_ALPHA;
1673 *internal = GL_LUMINANCE4_ALPHA4;
1674 *type = GL_UNSIGNED_BYTE;
1675 *target_bpp = 2;
1676 break;
1678 case WINED3DFMT_R32F:
1679 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1680 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1681 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1682 * 1.0 instead.
1684 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1686 *convert = CONVERT_R32F;
1687 *format = GL_RGB;
1688 *internal = GL_RGB32F_ARB;
1689 *type = GL_FLOAT;
1690 *target_bpp = 12;
1691 break;
1693 case WINED3DFMT_R16F:
1694 /* Similar to R32F */
1695 *convert = CONVERT_R16F;
1696 *format = GL_RGB;
1697 *internal = GL_RGB16F_ARB;
1698 *type = GL_HALF_FLOAT_ARB;
1699 *target_bpp = 6;
1700 break;
1702 case WINED3DFMT_G16R16:
1703 *convert = CONVERT_G16R16;
1704 *format = GL_RGB;
1705 *internal = GL_RGB16_EXT;
1706 *type = GL_UNSIGNED_SHORT;
1707 *target_bpp = 6;
1708 break;
1710 default:
1711 break;
1714 return WINED3D_OK;
1717 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1718 BYTE *source, *dest;
1719 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1721 switch (convert) {
1722 case NO_CONVERSION:
1724 memcpy(dst, src, pitch * height);
1725 break;
1727 case CONVERT_PALETTED:
1728 case CONVERT_PALETTED_CK:
1730 IWineD3DPaletteImpl* pal = This->palette;
1731 BYTE table[256][4];
1732 unsigned int x, y;
1734 if( pal == NULL) {
1735 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1738 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1740 for (y = 0; y < height; y++)
1742 source = src + pitch * y;
1743 dest = dst + outpitch * y;
1744 /* This is an 1 bpp format, using the width here is fine */
1745 for (x = 0; x < width; x++) {
1746 BYTE color = *source++;
1747 *dest++ = table[color][0];
1748 *dest++ = table[color][1];
1749 *dest++ = table[color][2];
1750 *dest++ = table[color][3];
1754 break;
1756 case CONVERT_CK_565:
1758 /* Converting the 565 format in 5551 packed to emulate color-keying.
1760 Note : in all these conversion, it would be best to average the averaging
1761 pixels to get the color of the pixel that will be color-keyed to
1762 prevent 'color bleeding'. This will be done later on if ever it is
1763 too visible.
1765 Note2: Nvidia documents say that their driver does not support alpha + color keying
1766 on the same surface and disables color keying in such a case
1768 unsigned int x, y;
1769 WORD *Source;
1770 WORD *Dest;
1772 TRACE("Color keyed 565\n");
1774 for (y = 0; y < height; y++) {
1775 Source = (WORD *) (src + y * pitch);
1776 Dest = (WORD *) (dst + y * outpitch);
1777 for (x = 0; x < width; x++ ) {
1778 WORD color = *Source++;
1779 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1780 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1781 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1782 *Dest |= 0x0001;
1784 Dest++;
1788 break;
1790 case CONVERT_CK_5551:
1792 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1793 unsigned int x, y;
1794 WORD *Source;
1795 WORD *Dest;
1796 TRACE("Color keyed 5551\n");
1797 for (y = 0; y < height; y++) {
1798 Source = (WORD *) (src + y * pitch);
1799 Dest = (WORD *) (dst + y * outpitch);
1800 for (x = 0; x < width; x++ ) {
1801 WORD color = *Source++;
1802 *Dest = color;
1803 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1804 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1805 *Dest |= (1 << 15);
1807 else {
1808 *Dest &= ~(1 << 15);
1810 Dest++;
1814 break;
1816 case CONVERT_RGB32_888:
1818 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
1819 unsigned int x, y;
1820 for (y = 0; y < height; y++)
1822 source = src + pitch * y;
1823 dest = dst + outpitch * y;
1824 for (x = 0; x < width; x++) {
1825 DWORD color = 0xffffff & *(DWORD*)source;
1826 DWORD dstcolor = color << 8;
1827 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1828 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1829 dstcolor |= 0xff;
1831 *(DWORD*)dest = dstcolor;
1832 source += 4;
1833 dest += 4;
1837 break;
1839 case CONVERT_V8U8:
1841 unsigned int x, y;
1842 short *Source;
1843 unsigned char *Dest;
1844 for(y = 0; y < height; y++) {
1845 Source = (short *) (src + y * pitch);
1846 Dest = dst + y * outpitch;
1847 for (x = 0; x < width; x++ ) {
1848 long color = (*Source++);
1849 /* B */ Dest[0] = 0xff;
1850 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1851 /* R */ Dest[2] = (color) + 128; /* U */
1852 Dest += 3;
1855 break;
1858 case CONVERT_V16U16:
1860 unsigned int x, y;
1861 DWORD *Source;
1862 unsigned short *Dest;
1863 for(y = 0; y < height; y++) {
1864 Source = (DWORD *) (src + y * pitch);
1865 Dest = (unsigned short *) (dst + y * outpitch);
1866 for (x = 0; x < width; x++ ) {
1867 DWORD color = (*Source++);
1868 /* B */ Dest[0] = 0xffff;
1869 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1870 /* R */ Dest[2] = (color ) + 32768; /* U */
1871 Dest += 3;
1874 break;
1877 case CONVERT_Q8W8V8U8:
1879 unsigned int x, y;
1880 DWORD *Source;
1881 unsigned char *Dest;
1882 for(y = 0; y < height; y++) {
1883 Source = (DWORD *) (src + y * pitch);
1884 Dest = dst + y * outpitch;
1885 for (x = 0; x < width; x++ ) {
1886 long color = (*Source++);
1887 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1888 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1889 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1890 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1891 Dest += 4;
1894 break;
1897 case CONVERT_L6V5U5:
1899 unsigned int x, y;
1900 WORD *Source;
1901 unsigned char *Dest;
1903 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1904 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1905 * fixed function and shaders without further conversion once the surface is
1906 * loaded
1908 for(y = 0; y < height; y++) {
1909 Source = (WORD *) (src + y * pitch);
1910 Dest = dst + y * outpitch;
1911 for (x = 0; x < width; x++ ) {
1912 short color = (*Source++);
1913 unsigned char l = ((color >> 10) & 0xfc);
1914 char v = ((color >> 5) & 0x3e);
1915 char u = ((color ) & 0x1f);
1917 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1918 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1919 * shift. GL reads a signed value and converts it into an unsigned value.
1921 /* M */ Dest[2] = l << 1;
1923 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1924 * from 5 bit values to 8 bit values.
1926 /* V */ Dest[1] = v << 3;
1927 /* U */ Dest[0] = u << 3;
1928 Dest += 3;
1931 } else {
1932 for(y = 0; y < height; y++) {
1933 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1934 Source = (WORD *) (src + y * pitch);
1935 for (x = 0; x < width; x++ ) {
1936 short color = (*Source++);
1937 unsigned char l = ((color >> 10) & 0xfc);
1938 short v = ((color >> 5) & 0x3e);
1939 short u = ((color ) & 0x1f);
1940 short v_conv = v + 16;
1941 short u_conv = u + 16;
1943 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1944 Dest_s += 1;
1948 break;
1951 case CONVERT_X8L8V8U8:
1953 unsigned int x, y;
1954 DWORD *Source;
1955 unsigned char *Dest;
1957 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1958 /* This implementation works with the fixed function pipeline and shaders
1959 * without further modification after converting the surface.
1961 for(y = 0; y < height; y++) {
1962 Source = (DWORD *) (src + y * pitch);
1963 Dest = dst + y * outpitch;
1964 for (x = 0; x < width; x++ ) {
1965 long color = (*Source++);
1966 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1967 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1968 /* U */ Dest[0] = (color & 0xff); /* U */
1969 /* I */ Dest[3] = 255; /* X */
1970 Dest += 4;
1973 } else {
1974 /* Doesn't work correctly with the fixed function pipeline, but can work in
1975 * shaders if the shader is adjusted. (There's no use for this format in gl's
1976 * standard fixed function pipeline anyway).
1978 for(y = 0; y < height; y++) {
1979 Source = (DWORD *) (src + y * pitch);
1980 Dest = dst + y * outpitch;
1981 for (x = 0; x < width; x++ ) {
1982 long color = (*Source++);
1983 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1984 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1985 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1986 Dest += 4;
1990 break;
1993 case CONVERT_A4L4:
1995 unsigned int x, y;
1996 unsigned char *Source;
1997 unsigned char *Dest;
1998 for(y = 0; y < height; y++) {
1999 Source = src + y * pitch;
2000 Dest = dst + y * outpitch;
2001 for (x = 0; x < width; x++ ) {
2002 unsigned char color = (*Source++);
2003 /* A */ Dest[1] = (color & 0xf0) << 0;
2004 /* L */ Dest[0] = (color & 0x0f) << 4;
2005 Dest += 2;
2008 break;
2011 case CONVERT_R32F:
2013 unsigned int x, y;
2014 float *Source;
2015 float *Dest;
2016 for(y = 0; y < height; y++) {
2017 Source = (float *) (src + y * pitch);
2018 Dest = (float *) (dst + y * outpitch);
2019 for (x = 0; x < width; x++ ) {
2020 float color = (*Source++);
2021 Dest[0] = color;
2022 Dest[1] = 1.0;
2023 Dest[2] = 1.0;
2024 Dest += 3;
2027 break;
2030 case CONVERT_R16F:
2032 unsigned int x, y;
2033 WORD *Source;
2034 WORD *Dest;
2035 WORD one = 0x3c00;
2036 for(y = 0; y < height; y++) {
2037 Source = (WORD *) (src + y * pitch);
2038 Dest = (WORD *) (dst + y * outpitch);
2039 for (x = 0; x < width; x++ ) {
2040 WORD color = (*Source++);
2041 Dest[0] = color;
2042 Dest[1] = one;
2043 Dest[2] = one;
2044 Dest += 3;
2047 break;
2050 case CONVERT_G16R16:
2052 unsigned int x, y;
2053 WORD *Source;
2054 WORD *Dest;
2056 for(y = 0; y < height; y++) {
2057 Source = (WORD *) (src + y * pitch);
2058 Dest = (WORD *) (dst + y * outpitch);
2059 for (x = 0; x < width; x++ ) {
2060 WORD green = (*Source++);
2061 WORD red = (*Source++);
2062 Dest[0] = green;
2063 Dest[1] = red;
2064 Dest[2] = 0xffff;
2065 Dest += 3;
2068 break;
2071 default:
2072 ERR("Unsupported conversation type %d\n", convert);
2074 return WINED3D_OK;
2077 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2078 IWineD3DPaletteImpl* pal = This->palette;
2079 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2080 BOOL index_in_alpha = FALSE;
2081 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2082 int i;
2084 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2085 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2086 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2087 * duplicate entries. Store the color key in the unused alpha component to speed the
2088 * download up and to make conversion unneeded. */
2089 index_in_alpha = primary_render_target_is_p8(device);
2091 if (pal == NULL) {
2092 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2093 if(dxVersion <= 7) {
2094 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2095 if(index_in_alpha) {
2096 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2097 there's no palette at this time. */
2098 for (i = 0; i < 256; i++) table[i][3] = i;
2100 } else {
2101 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2102 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2103 capability flag is present (wine does advertise this capability) */
2104 for (i = 0; i < 256; i++) {
2105 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2106 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2107 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2108 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2111 } else {
2112 TRACE("Using surface palette %p\n", pal);
2113 /* Get the surface's palette */
2114 for (i = 0; i < 256; i++) {
2115 table[i][0] = pal->palents[i].peRed;
2116 table[i][1] = pal->palents[i].peGreen;
2117 table[i][2] = pal->palents[i].peBlue;
2119 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2120 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2121 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2122 of pixels that should be masked away is set to 0. */
2123 if(index_in_alpha) {
2124 table[i][3] = i;
2125 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2126 table[i][3] = 0x00;
2127 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2128 table[i][3] = pal->palents[i].peFlags;
2129 } else {
2130 table[i][3] = 0xFF;
2136 const char *fragment_palette_conversion =
2137 "!!ARBfp1.0\n"
2138 "TEMP index;\n"
2139 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2140 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2141 "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 */
2142 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2143 "END";
2145 /* This function is used in case of 8bit paletted textures to upload the palette.
2146 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2147 extensions like ATI_fragment_shaders is possible.
2149 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2150 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2151 BYTE table[256][4];
2152 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2154 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2156 /* Try to use the paletted texture extension */
2157 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2159 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2160 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2162 else
2164 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2165 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2166 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2168 /* Create the fragment program if we don't have it */
2169 if(!device->paletteConversionShader)
2171 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2172 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2173 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2174 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2175 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2178 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2179 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2181 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2182 glEnable(GL_TEXTURE_1D);
2183 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2185 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2186 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2187 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2188 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2190 /* Switch back to unit 0 in which the 2D texture will be stored. */
2191 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2193 /* Rebind the texture because it isn't bound anymore */
2194 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2198 BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2199 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2201 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2202 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2203 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2205 return FALSE;
2208 if(This->palette9) {
2209 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2210 return FALSE;
2212 } else {
2213 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2215 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2216 return TRUE;
2219 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2220 GLboolean oldwrite[4];
2222 /* Some formats have only some color channels, and the others are 1.0.
2223 * since our rendering renders to all channels, and those pixel formats
2224 * are emulated by using a full texture with the other channels set to 1.0
2225 * manually, clear the unused channels.
2227 * This could be done with hacking colorwriteenable to mask the colors,
2228 * but before drawing the buffer would have to be cleared too, so there's
2229 * no gain in that
2231 switch(This->resource.format) {
2232 case WINED3DFMT_R16F:
2233 case WINED3DFMT_R32F:
2234 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2235 /* Do not activate a context, the correct drawable is active already
2236 * though just the read buffer is set, make sure to have the correct draw
2237 * buffer too
2239 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2240 glDisable(GL_SCISSOR_TEST);
2241 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2242 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2243 glClearColor(0.0, 1.0, 1.0, 1.0);
2244 glClear(GL_COLOR_BUFFER_BIT);
2245 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2246 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2247 checkGLcall("Unused channel clear\n");
2248 break;
2250 default: break;
2254 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2255 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2257 if (!(This->Flags & SFLAG_INTEXTURE)) {
2258 TRACE("Reloading because surface is dirty\n");
2259 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2260 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2261 /* Reload: vice versa OR */
2262 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2263 /* Also reload: Color key is active AND the color key has changed */
2264 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2265 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2266 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2267 TRACE("Reloading because of color keying\n");
2268 /* To perform the color key conversion we need a sysmem copy of
2269 * the surface. Make sure we have it
2272 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2273 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2274 /* TODO: This is not necessarily needed with hw palettized texture support */
2275 This->Flags &= ~SFLAG_INTEXTURE;
2276 } else {
2277 TRACE("surface is already in texture\n");
2278 return WINED3D_OK;
2281 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2282 * These resources are not bound by device size or format restrictions. Because of this,
2283 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2284 * However, these resources can always be created, locked, and copied.
2286 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2288 FIXME("(%p) Operation not supported for scratch textures\n",This);
2289 return WINED3DERR_INVALIDCALL;
2292 This->srgb = srgb_mode;
2293 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2295 #if 0
2297 static unsigned int gen = 0;
2298 char buffer[4096];
2299 ++gen;
2300 if ((gen % 10) == 0) {
2301 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2302 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2305 * debugging crash code
2306 if (gen == 250) {
2307 void** test = NULL;
2308 *test = 0;
2312 #endif
2314 if (!(This->Flags & SFLAG_DONOTFREE)) {
2315 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2316 This->resource.allocatedMemory = NULL;
2317 This->resource.heapMemory = NULL;
2318 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2321 return WINED3D_OK;
2324 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2325 /* TODO: check for locks */
2326 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2327 IWineD3DBaseTexture *baseTexture = NULL;
2328 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2330 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2331 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2332 TRACE("Passing to container\n");
2333 IWineD3DBaseTexture_BindTexture(baseTexture);
2334 IWineD3DBaseTexture_Release(baseTexture);
2335 } else {
2336 TRACE("(%p) : Binding surface\n", This);
2338 if(!device->isInDraw) {
2339 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2341 ENTER_GL();
2342 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2343 LEAVE_GL();
2345 return;
2348 #include <errno.h>
2349 #include <stdio.h>
2350 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2351 FILE* f = NULL;
2352 UINT i, y;
2353 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2354 char *allocatedMemory;
2355 char *textureRow;
2356 IWineD3DSwapChain *swapChain = NULL;
2357 int width, height;
2358 GLuint tmpTexture = 0;
2359 DWORD color;
2360 /*FIXME:
2361 Textures may not be stored in ->allocatedgMemory and a GlTexture
2362 so we should lock the surface before saving a snapshot, or at least check that
2364 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2365 by calling GetTexImage and in compressed form by calling
2366 GetCompressedTexImageARB. Queried compressed images can be saved and
2367 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2368 texture images do not need to be processed by the GL and should
2369 significantly improve texture loading performance relative to uncompressed
2370 images. */
2372 /* Setup the width and height to be the internal texture width and height. */
2373 width = This->pow2Width;
2374 height = This->pow2Height;
2375 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2376 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2378 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2379 /* if were not a real texture then read the back buffer into a real texture */
2380 /* we don't want to interfere with the back buffer so read the data into a temporary
2381 * texture and then save the data out of the temporary texture
2383 GLint prevRead;
2384 ENTER_GL();
2385 TRACE("(%p) Reading render target into texture\n", This);
2386 glEnable(GL_TEXTURE_2D);
2388 glGenTextures(1, &tmpTexture);
2389 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2391 glTexImage2D(GL_TEXTURE_2D,
2393 GL_RGBA,
2394 width,
2395 height,
2396 0/*border*/,
2397 GL_RGBA,
2398 GL_UNSIGNED_INT_8_8_8_8_REV,
2399 NULL);
2401 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2402 vcheckGLcall("glGetIntegerv");
2403 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2404 vcheckGLcall("glReadBuffer");
2405 glCopyTexImage2D(GL_TEXTURE_2D,
2407 GL_RGBA,
2410 width,
2411 height,
2414 checkGLcall("glCopyTexImage2D");
2415 glReadBuffer(prevRead);
2416 LEAVE_GL();
2418 } else { /* bind the real texture, and make sure it up to date */
2419 IWineD3DSurface_PreLoad(iface);
2421 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2422 ENTER_GL();
2423 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2424 glGetTexImage(GL_TEXTURE_2D,
2425 This->glDescription.level,
2426 GL_RGBA,
2427 GL_UNSIGNED_INT_8_8_8_8_REV,
2428 allocatedMemory);
2429 checkGLcall("glTexImage2D");
2430 if (tmpTexture) {
2431 glBindTexture(GL_TEXTURE_2D, 0);
2432 glDeleteTextures(1, &tmpTexture);
2434 LEAVE_GL();
2436 f = fopen(filename, "w+");
2437 if (NULL == f) {
2438 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2439 return WINED3DERR_INVALIDCALL;
2441 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2442 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2443 /* TGA header */
2444 fputc(0,f);
2445 fputc(0,f);
2446 fputc(2,f);
2447 fputc(0,f);
2448 fputc(0,f);
2449 fputc(0,f);
2450 fputc(0,f);
2451 fputc(0,f);
2452 fputc(0,f);
2453 fputc(0,f);
2454 fputc(0,f);
2455 fputc(0,f);
2456 /* short width*/
2457 fwrite(&width,2,1,f);
2458 /* short height */
2459 fwrite(&height,2,1,f);
2460 /* format rgba */
2461 fputc(0x20,f);
2462 fputc(0x28,f);
2463 /* raw data */
2464 /* 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 */
2465 if(swapChain)
2466 textureRow = allocatedMemory + (width * (height - 1) *4);
2467 else
2468 textureRow = allocatedMemory;
2469 for (y = 0 ; y < height; y++) {
2470 for (i = 0; i < width; i++) {
2471 color = *((DWORD*)textureRow);
2472 fputc((color >> 16) & 0xFF, f); /* B */
2473 fputc((color >> 8) & 0xFF, f); /* G */
2474 fputc((color >> 0) & 0xFF, f); /* R */
2475 fputc((color >> 24) & 0xFF, f); /* A */
2476 textureRow += 4;
2478 /* take two rows of the pointer to the texture memory */
2479 if(swapChain)
2480 (textureRow-= width << 3);
2483 TRACE("Closing file\n");
2484 fclose(f);
2486 if(swapChain) {
2487 IWineD3DSwapChain_Release(swapChain);
2489 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2490 return WINED3D_OK;
2494 * Slightly inefficient way to handle multiple dirty rects but it works :)
2496 HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2497 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2498 IWineD3DBaseTexture *baseTexture = NULL;
2500 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2501 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2503 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2504 if (NULL != pDirtyRect) {
2505 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2506 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2507 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2508 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2509 } else {
2510 This->dirtyRect.left = 0;
2511 This->dirtyRect.top = 0;
2512 This->dirtyRect.right = This->currentDesc.Width;
2513 This->dirtyRect.bottom = This->currentDesc.Height;
2515 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2516 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2517 /* if the container is a basetexture then mark it dirty. */
2518 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2519 TRACE("Passing to container\n");
2520 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2521 IWineD3DBaseTexture_Release(baseTexture);
2523 return WINED3D_OK;
2526 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2527 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2528 HRESULT hr;
2529 const GlPixelFormatDesc *glDesc;
2530 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2532 TRACE("(%p) : Calling base function first\n", This);
2533 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2534 if(SUCCEEDED(hr)) {
2535 /* Setup some glformat defaults */
2536 This->glDescription.glFormat = glDesc->glFormat;
2537 This->glDescription.glFormatInternal = glDesc->glInternal;
2538 This->glDescription.glType = glDesc->glType;
2540 This->Flags &= ~SFLAG_ALLOCATED;
2541 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2542 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2544 return hr;
2547 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2548 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2550 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2551 WARN("Surface is locked or the HDC is in use\n");
2552 return WINED3DERR_INVALIDCALL;
2555 if(Mem && Mem != This->resource.allocatedMemory) {
2556 void *release = NULL;
2558 /* Do I have to copy the old surface content? */
2559 if(This->Flags & SFLAG_DIBSECTION) {
2560 /* Release the DC. No need to hold the critical section for the update
2561 * Thread because this thread runs only on front buffers, but this method
2562 * fails for render targets in the check above.
2564 SelectObject(This->hDC, This->dib.holdbitmap);
2565 DeleteDC(This->hDC);
2566 /* Release the DIB section */
2567 DeleteObject(This->dib.DIBsection);
2568 This->dib.bitmap_data = NULL;
2569 This->resource.allocatedMemory = NULL;
2570 This->hDC = NULL;
2571 This->Flags &= ~SFLAG_DIBSECTION;
2572 } else if(!(This->Flags & SFLAG_USERPTR)) {
2573 release = This->resource.heapMemory;
2574 This->resource.heapMemory = NULL;
2576 This->resource.allocatedMemory = Mem;
2577 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2579 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2580 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2582 /* For client textures opengl has to be notified */
2583 if(This->Flags & SFLAG_CLIENT) {
2584 This->Flags &= ~SFLAG_ALLOCATED;
2585 IWineD3DSurface_PreLoad(iface);
2586 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2589 /* Now free the old memory if any */
2590 HeapFree(GetProcessHeap(), 0, release);
2591 } else if(This->Flags & SFLAG_USERPTR) {
2592 /* LockRect and GetDC will re-create the dib section and allocated memory */
2593 This->resource.allocatedMemory = NULL;
2594 /* HeapMemory should be NULL already */
2595 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2596 This->Flags &= ~SFLAG_USERPTR;
2598 if(This->Flags & SFLAG_CLIENT) {
2599 This->Flags &= ~SFLAG_ALLOCATED;
2600 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2601 IWineD3DSurface_PreLoad(iface);
2604 return WINED3D_OK;
2607 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2608 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2609 IWineD3DSwapChainImpl *swapchain = NULL;
2610 HRESULT hr;
2611 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2613 /* Flipping is only supported on RenderTargets */
2614 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2616 if(override) {
2617 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2618 * FIXME("(%p) Target override is not supported by now\n", This);
2619 * Additionally, it isn't really possible to support triple-buffering
2620 * properly on opengl at all
2624 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2625 if(!swapchain) {
2626 ERR("Flipped surface is not on a swapchain\n");
2627 return WINEDDERR_NOTFLIPPABLE;
2630 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2631 * and only d3d8 and d3d9 apps specify the presentation interval
2633 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2634 /* Most common case first to avoid wasting time on all the other cases */
2635 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2636 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2637 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2638 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2639 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2640 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2641 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2642 } else {
2643 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2646 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2647 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2648 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2649 return hr;
2652 /* Does a direct frame buffer -> texture copy. Stretching is done
2653 * with single pixel copy calls
2655 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2656 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2657 float xrel, yrel;
2658 UINT row;
2659 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2662 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2663 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2664 ENTER_GL();
2666 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2667 glEnable(This->glDescription.target);
2668 checkGLcall("glEnable(This->glDescription.target)");
2670 /* Bind the target texture */
2671 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2672 checkGLcall("glBindTexture");
2673 if(!swapchain) {
2674 glReadBuffer(myDevice->offscreenBuffer);
2675 } else {
2676 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2677 glReadBuffer(buffer);
2679 checkGLcall("glReadBuffer");
2681 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2682 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2684 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2685 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2687 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2688 ERR("Texture filtering not supported in direct blit\n");
2690 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2691 ERR("Texture filtering not supported in direct blit\n");
2694 if(upsidedown &&
2695 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2696 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2697 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2699 glCopyTexSubImage2D(This->glDescription.target,
2700 This->glDescription.level,
2701 drect->x1, drect->y1, /* xoffset, yoffset */
2702 srect->x1, Src->currentDesc.Height - srect->y2,
2703 drect->x2 - drect->x1, drect->y2 - drect->y1);
2704 } else {
2705 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2706 /* I have to process this row by row to swap the image,
2707 * otherwise it would be upside down, so stretching in y direction
2708 * doesn't cost extra time
2710 * However, stretching in x direction can be avoided if not necessary
2712 for(row = drect->y1; row < drect->y2; row++) {
2713 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2714 /* Well, that stuff works, but it's very slow.
2715 * find a better way instead
2717 UINT col;
2719 for(col = drect->x1; col < drect->x2; col++) {
2720 glCopyTexSubImage2D(This->glDescription.target,
2721 This->glDescription.level,
2722 drect->x1 + col, row, /* xoffset, yoffset */
2723 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2724 1, 1);
2726 } else {
2727 glCopyTexSubImage2D(This->glDescription.target,
2728 This->glDescription.level,
2729 drect->x1, row, /* xoffset, yoffset */
2730 srect->x1, yoffset - (int) (row * yrel),
2731 drect->x2-drect->x1, 1);
2735 vcheckGLcall("glCopyTexSubImage2D");
2737 /* Leave the opengl state valid for blitting */
2738 glDisable(This->glDescription.target);
2739 checkGLcall("glDisable(This->glDescription.target)");
2741 LEAVE_GL();
2744 /* Uses the hardware to stretch and flip the image */
2745 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2746 GLuint src, backup = 0;
2747 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2748 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2749 float left, right, top, bottom; /* Texture coordinates */
2750 UINT fbwidth = Src->currentDesc.Width;
2751 UINT fbheight = Src->currentDesc.Height;
2752 GLenum drawBuffer = GL_BACK;
2753 GLenum texture_target;
2754 BOOL noBackBufferBackup;
2756 TRACE("Using hwstretch blit\n");
2757 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2758 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2759 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2761 noBackBufferBackup = !swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2762 if(!noBackBufferBackup && Src->glDescription.textureName == 0) {
2763 /* Get it a description */
2764 IWineD3DSurface_PreLoad(SrcSurface);
2766 ENTER_GL();
2768 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2769 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2771 if(myDevice->activeContext->aux_buffers >= 2) {
2772 /* Got more than one aux buffer? Use the 2nd aux buffer */
2773 drawBuffer = GL_AUX1;
2774 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && myDevice->activeContext->aux_buffers >= 1) {
2775 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2776 drawBuffer = GL_AUX0;
2779 if(noBackBufferBackup) {
2780 glGenTextures(1, &backup);
2781 checkGLcall("glGenTextures\n");
2782 glBindTexture(GL_TEXTURE_2D, backup);
2783 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2784 texture_target = GL_TEXTURE_2D;
2785 } else {
2786 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2787 * we are reading from the back buffer, the backup can be used as source texture
2789 texture_target = Src->glDescription.target;
2790 glBindTexture(texture_target, Src->glDescription.textureName);
2791 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2792 glEnable(texture_target);
2793 checkGLcall("glEnable(texture_target)");
2795 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2796 Src->Flags &= ~SFLAG_INTEXTURE;
2799 if(swapchain) {
2800 glReadBuffer(surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain));
2801 } else {
2802 glReadBuffer(myDevice->offscreenBuffer);
2805 /* TODO: Only back up the part that will be overwritten */
2806 glCopyTexSubImage2D(texture_target, 0,
2807 0, 0 /* read offsets */,
2808 0, 0,
2809 fbwidth,
2810 fbheight);
2812 checkGLcall("glCopyTexSubImage2D");
2814 /* No issue with overriding these - the sampler is dirty due to blit usage */
2815 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2816 magLookup[Filter - WINED3DTEXF_NONE]);
2817 checkGLcall("glTexParameteri");
2818 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2819 minMipLookup[Filter][WINED3DTEXF_NONE]);
2820 checkGLcall("glTexParameteri");
2822 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2823 src = backup ? backup : Src->glDescription.textureName;
2824 } else {
2825 glReadBuffer(GL_FRONT);
2826 checkGLcall("glReadBuffer(GL_FRONT)");
2828 glGenTextures(1, &src);
2829 checkGLcall("glGenTextures(1, &src)");
2830 glBindTexture(GL_TEXTURE_2D, src);
2831 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2833 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2834 * out for power of 2 sizes
2836 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2837 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2838 checkGLcall("glTexImage2D");
2839 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2840 0, 0 /* read offsets */,
2841 0, 0,
2842 fbwidth,
2843 fbheight);
2845 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2846 checkGLcall("glTexParameteri");
2847 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2848 checkGLcall("glTexParameteri");
2850 glReadBuffer(GL_BACK);
2851 checkGLcall("glReadBuffer(GL_BACK)");
2853 if(texture_target != GL_TEXTURE_2D) {
2854 glDisable(texture_target);
2855 glEnable(GL_TEXTURE_2D);
2856 texture_target = GL_TEXTURE_2D;
2859 checkGLcall("glEnd and previous");
2861 left = srect->x1;
2862 right = srect->x2;
2864 if(upsidedown) {
2865 top = Src->currentDesc.Height - srect->y1;
2866 bottom = Src->currentDesc.Height - srect->y2;
2867 } else {
2868 top = Src->currentDesc.Height - srect->y2;
2869 bottom = Src->currentDesc.Height - srect->y1;
2872 if(Src->Flags & SFLAG_NORMCOORD) {
2873 left /= Src->pow2Width;
2874 right /= Src->pow2Width;
2875 top /= Src->pow2Height;
2876 bottom /= Src->pow2Height;
2879 /* draw the source texture stretched and upside down. The correct surface is bound already */
2880 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2881 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2883 glDrawBuffer(drawBuffer);
2884 glReadBuffer(drawBuffer);
2886 glBegin(GL_QUADS);
2887 /* bottom left */
2888 glTexCoord2f(left, bottom);
2889 glVertex2i(0, fbheight);
2891 /* top left */
2892 glTexCoord2f(left, top);
2893 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2895 /* top right */
2896 glTexCoord2f(right, top);
2897 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2899 /* bottom right */
2900 glTexCoord2f(right, bottom);
2901 glVertex2i(drect->x2 - drect->x1, fbheight);
2902 glEnd();
2903 checkGLcall("glEnd and previous");
2905 if(texture_target != This->glDescription.target) {
2906 glDisable(texture_target);
2907 glEnable(This->glDescription.target);
2908 texture_target = This->glDescription.target;
2911 /* Now read the stretched and upside down image into the destination texture */
2912 glBindTexture(texture_target, This->glDescription.textureName);
2913 checkGLcall("glBindTexture");
2914 glCopyTexSubImage2D(texture_target,
2916 drect->x1, drect->y1, /* xoffset, yoffset */
2917 0, 0, /* We blitted the image to the origin */
2918 drect->x2 - drect->x1, drect->y2 - drect->y1);
2919 checkGLcall("glCopyTexSubImage2D");
2921 if(drawBuffer == GL_BACK) {
2922 /* Write the back buffer backup back */
2923 if(backup) {
2924 if(texture_target != GL_TEXTURE_2D) {
2925 glDisable(texture_target);
2926 glEnable(GL_TEXTURE_2D);
2927 texture_target = GL_TEXTURE_2D;
2929 glBindTexture(GL_TEXTURE_2D, backup);
2930 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2931 } else {
2932 if(texture_target != Src->glDescription.target) {
2933 glDisable(texture_target);
2934 glEnable(Src->glDescription.target);
2935 texture_target = Src->glDescription.target;
2937 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2938 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2941 glBegin(GL_QUADS);
2942 /* top left */
2943 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2944 glVertex2i(0, 0);
2946 /* bottom left */
2947 glTexCoord2f(0.0, 0.0);
2948 glVertex2i(0, fbheight);
2950 /* bottom right */
2951 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2952 glVertex2i(fbwidth, Src->currentDesc.Height);
2954 /* top right */
2955 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2956 glVertex2i(fbwidth, 0);
2957 glEnd();
2958 } else {
2959 /* Restore the old draw buffer */
2960 glDrawBuffer(GL_BACK);
2962 glDisable(texture_target);
2963 checkGLcall("glDisable(texture_target)");
2965 /* Cleanup */
2966 if(src != Src->glDescription.textureName && src != backup) {
2967 glDeleteTextures(1, &src);
2968 checkGLcall("glDeleteTextures(1, &src)");
2970 if(backup) {
2971 glDeleteTextures(1, &backup);
2972 checkGLcall("glDeleteTextures(1, &backup)");
2975 LEAVE_GL();
2978 /* Not called from the VTable */
2979 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2980 WINED3DRECT rect;
2981 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2982 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2983 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2985 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2987 /* Get the swapchain. One of the surfaces has to be a primary surface */
2988 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2989 WARN("Destination is in sysmem, rejecting gl blt\n");
2990 return WINED3DERR_INVALIDCALL;
2992 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
2993 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
2994 if(Src) {
2995 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2996 WARN("Src is in sysmem, rejecting gl blt\n");
2997 return WINED3DERR_INVALIDCALL;
2999 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3000 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3003 /* Early sort out of cases where no render target is used */
3004 if(!dstSwapchain && !srcSwapchain &&
3005 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3006 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3007 return WINED3DERR_INVALIDCALL;
3010 /* No destination color keying supported */
3011 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3012 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3013 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3014 return WINED3DERR_INVALIDCALL;
3017 if (DestRect) {
3018 rect.x1 = DestRect->left;
3019 rect.y1 = DestRect->top;
3020 rect.x2 = DestRect->right;
3021 rect.y2 = DestRect->bottom;
3022 } else {
3023 rect.x1 = 0;
3024 rect.y1 = 0;
3025 rect.x2 = This->currentDesc.Width;
3026 rect.y2 = This->currentDesc.Height;
3029 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3030 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3031 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3032 /* Half-life does a Blt from the back buffer to the front buffer,
3033 * Full surface size, no flags... Use present instead
3035 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3038 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3039 while(1)
3041 RECT mySrcRect;
3042 TRACE("Looking if a Present can be done...\n");
3043 /* Source Rectangle must be full surface */
3044 if( SrcRect ) {
3045 if(SrcRect->left != 0 || SrcRect->top != 0 ||
3046 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
3047 TRACE("No, Source rectangle doesn't match\n");
3048 break;
3051 mySrcRect.left = 0;
3052 mySrcRect.top = 0;
3053 mySrcRect.right = Src->currentDesc.Width;
3054 mySrcRect.bottom = Src->currentDesc.Height;
3056 /* No stretching may occur */
3057 if(mySrcRect.right != rect.x2 - rect.x1 ||
3058 mySrcRect.bottom != rect.y2 - rect.y1) {
3059 TRACE("No, stretching is done\n");
3060 break;
3063 /* Destination must be full surface or match the clipping rectangle */
3064 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3066 RECT cliprect;
3067 POINT pos[2];
3068 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3069 pos[0].x = rect.x1;
3070 pos[0].y = rect.y1;
3071 pos[1].x = rect.x2;
3072 pos[1].y = rect.y2;
3073 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3074 pos, 2);
3076 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3077 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3079 TRACE("No, dest rectangle doesn't match(clipper)\n");
3080 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3081 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3082 break;
3085 else
3087 if(rect.x1 != 0 || rect.y1 != 0 ||
3088 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3089 TRACE("No, dest rectangle doesn't match(surface size)\n");
3090 break;
3094 TRACE("Yes\n");
3096 /* These flags are unimportant for the flag check, remove them */
3097 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3098 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3100 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3101 * take very long, while a flip is fast.
3102 * This applies to Half-Life, which does such Blts every time it finished
3103 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3104 * menu. This is also used by all apps when they do windowed rendering
3106 * The problem is that flipping is not really the same as copying. After a
3107 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3108 * untouched. Therefore it's necessary to override the swap effect
3109 * and to set it back after the flip.
3111 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3112 * testcases.
3115 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3116 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3118 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3119 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3121 dstSwapchain->presentParms.SwapEffect = orig_swap;
3123 return WINED3D_OK;
3125 break;
3128 TRACE("Unsupported blit between buffers on the same swapchain\n");
3129 return WINED3DERR_INVALIDCALL;
3130 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3131 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3132 return WINED3DERR_INVALIDCALL;
3133 } else if(dstSwapchain && srcSwapchain) {
3134 FIXME("Implement hardware blit between two different swapchains\n");
3135 return WINED3DERR_INVALIDCALL;
3136 } else if(dstSwapchain) {
3137 if(SrcSurface == myDevice->render_targets[0]) {
3138 TRACE("Blit from active render target to a swapchain\n");
3139 /* Handled with regular texture -> swapchain blit */
3141 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3142 FIXME("Implement blit from a swapchain to the active render target\n");
3143 return WINED3DERR_INVALIDCALL;
3146 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3147 /* Blit from render target to texture */
3148 WINED3DRECT srect;
3149 BOOL upsideDown, stretchx;
3150 BOOL paletteOverride = FALSE;
3152 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3153 TRACE("Color keying not supported by frame buffer to texture blit\n");
3154 return WINED3DERR_INVALIDCALL;
3155 /* Destination color key is checked above */
3158 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3159 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3161 if(SrcRect) {
3162 if(SrcRect->top < SrcRect->bottom) {
3163 srect.y1 = SrcRect->top;
3164 srect.y2 = SrcRect->bottom;
3165 upsideDown = FALSE;
3166 } else {
3167 srect.y1 = SrcRect->bottom;
3168 srect.y2 = SrcRect->top;
3169 upsideDown = TRUE;
3171 srect.x1 = SrcRect->left;
3172 srect.x2 = SrcRect->right;
3173 } else {
3174 srect.x1 = 0;
3175 srect.y1 = 0;
3176 srect.x2 = Src->currentDesc.Width;
3177 srect.y2 = Src->currentDesc.Height;
3178 upsideDown = FALSE;
3180 if(rect.x1 > rect.x2) {
3181 UINT tmp = rect.x2;
3182 rect.x2 = rect.x1;
3183 rect.x1 = tmp;
3184 upsideDown = !upsideDown;
3186 if(!srcSwapchain) {
3187 TRACE("Reading from an offscreen target\n");
3188 upsideDown = !upsideDown;
3191 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3192 stretchx = TRUE;
3193 } else {
3194 stretchx = FALSE;
3197 /* When blitting from a render target a texture, the texture isn't required to have a palette.
3198 * In this case grab the palette from the render target. */
3199 if((This->resource.format == WINED3DFMT_P8) && (This->palette == NULL)) {
3200 paletteOverride = TRUE;
3201 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3202 This->palette = Src->palette;
3205 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3206 * flip the image nor scale it.
3208 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3209 * -> If the app wants a image width an unscaled width, copy it line per line
3210 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3211 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3212 * back buffer. This is slower than reading line per line, thus not used for flipping
3213 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3214 * pixel by pixel
3216 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3217 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3218 * backends.
3220 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3221 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3222 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3223 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3224 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3225 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3226 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3227 } else {
3228 TRACE("Using hardware stretching to flip / stretch the texture\n");
3229 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3232 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3233 if(paletteOverride)
3234 This->palette = NULL;
3236 if(!(This->Flags & SFLAG_DONOTFREE)) {
3237 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3238 This->resource.allocatedMemory = NULL;
3239 This->resource.heapMemory = NULL;
3240 } else {
3241 This->Flags &= ~SFLAG_INSYSMEM;
3243 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3244 * path is never entered
3246 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3248 return WINED3D_OK;
3249 } else if(Src) {
3250 /* Blit from offscreen surface to render target */
3251 float glTexCoord[4];
3252 DWORD oldCKeyFlags = Src->CKeyFlags;
3253 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3254 RECT SourceRectangle;
3255 BOOL paletteOverride = FALSE;
3257 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3259 if(SrcRect) {
3260 SourceRectangle.left = SrcRect->left;
3261 SourceRectangle.right = SrcRect->right;
3262 SourceRectangle.top = SrcRect->top;
3263 SourceRectangle.bottom = SrcRect->bottom;
3264 } else {
3265 SourceRectangle.left = 0;
3266 SourceRectangle.right = Src->currentDesc.Width;
3267 SourceRectangle.top = 0;
3268 SourceRectangle.bottom = Src->currentDesc.Height;
3270 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3271 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3272 TRACE("Using stretch_rect_fbo\n");
3273 /* The source is always a texture, but never the currently active render target, and the texture
3274 * contents are never upside down
3276 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3277 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3278 return WINED3D_OK;
3281 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3282 /* Fall back to software */
3283 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3284 SourceRectangle.left, SourceRectangle.top,
3285 SourceRectangle.right, SourceRectangle.bottom);
3286 return WINED3DERR_INVALIDCALL;
3289 /* Color keying: Check if we have to do a color keyed blt,
3290 * and if not check if a color key is activated.
3292 * Just modify the color keying parameters in the surface and restore them afterwards
3293 * The surface keeps track of the color key last used to load the opengl surface.
3294 * PreLoad will catch the change to the flags and color key and reload if necessary.
3296 if(Flags & WINEDDBLT_KEYSRC) {
3297 /* Use color key from surface */
3298 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3299 /* Use color key from DDBltFx */
3300 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3301 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3302 } else {
3303 /* Do not use color key */
3304 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3307 /* When blitting from an offscreen surface to a rendertarget, the source
3308 * surface is not required to have a palette. Our rendering / conversion
3309 * code further down the road retrieves the palette from the surface, so
3310 * it must have a palette set. */
3311 if((Src->resource.format == WINED3DFMT_P8) && (Src->palette == NULL)) {
3312 paletteOverride = TRUE;
3313 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3314 Src->palette = This->palette;
3317 /* Now load the surface */
3318 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3321 /* Activate the destination context, set it up for blitting */
3322 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3323 ENTER_GL();
3325 glEnable(Src->glDescription.target);
3326 checkGLcall("glEnable(Src->glDescription.target)");
3328 if(!dstSwapchain) {
3329 TRACE("Drawing to offscreen buffer\n");
3330 glDrawBuffer(myDevice->offscreenBuffer);
3331 checkGLcall("glDrawBuffer");
3332 } else {
3333 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3334 TRACE("Drawing to %#x buffer\n", buffer);
3335 glDrawBuffer(buffer);
3336 checkGLcall("glDrawBuffer");
3339 /* Bind the texture */
3340 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3341 checkGLcall("glBindTexture");
3343 /* Filtering for StretchRect */
3344 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3345 magLookup[Filter - WINED3DTEXF_NONE]);
3346 checkGLcall("glTexParameteri");
3347 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3348 minMipLookup[Filter][WINED3DTEXF_NONE]);
3349 checkGLcall("glTexParameteri");
3350 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3351 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3352 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3353 checkGLcall("glTexEnvi");
3355 /* This is for color keying */
3356 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3357 glEnable(GL_ALPHA_TEST);
3358 checkGLcall("glEnable GL_ALPHA_TEST");
3360 /* When the primary render target uses P8, the alpha component contains the palette index.
3361 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3362 * should be masked away have alpha set to 0. */
3363 if(primary_render_target_is_p8(myDevice))
3364 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3365 else
3366 glAlphaFunc(GL_NOTEQUAL, 0.0);
3367 checkGLcall("glAlphaFunc\n");
3368 } else {
3369 glDisable(GL_ALPHA_TEST);
3370 checkGLcall("glDisable GL_ALPHA_TEST");
3373 /* Draw a textured quad
3375 glBegin(GL_QUADS);
3377 glColor3d(1.0f, 1.0f, 1.0f);
3378 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3379 glVertex3f(rect.x1,
3380 rect.y1,
3381 0.0);
3383 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3384 glVertex3f(rect.x1, rect.y2, 0.0);
3386 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3387 glVertex3f(rect.x2,
3388 rect.y2,
3389 0.0);
3391 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3392 glVertex3f(rect.x2,
3393 rect.y1,
3394 0.0);
3395 glEnd();
3396 checkGLcall("glEnd");
3398 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3399 glDisable(GL_ALPHA_TEST);
3400 checkGLcall("glDisable(GL_ALPHA_TEST)");
3403 /* Flush in case the drawable is used by multiple GL contexts */
3404 if(dstSwapchain && (This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer || dstSwapchain->num_contexts >= 2))
3405 glFlush();
3407 glBindTexture(Src->glDescription.target, 0);
3408 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3409 /* Leave the opengl state valid for blitting */
3410 glDisable(Src->glDescription.target);
3411 checkGLcall("glDisable(Src->glDescription.target)");
3413 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3414 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3416 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3417 glDrawBuffer(GL_BACK);
3418 checkGLcall("glDrawBuffer");
3420 /* Restore the color key parameters */
3421 Src->CKeyFlags = oldCKeyFlags;
3422 Src->SrcBltCKey = oldBltCKey;
3424 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3425 if(paletteOverride)
3426 Src->palette = NULL;
3428 LEAVE_GL();
3430 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3431 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3432 * is outdated now
3434 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3435 /* TODO: This should be moved to ModifyLocation() */
3436 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3437 This->Flags |= SFLAG_INTEXTURE;
3440 return WINED3D_OK;
3441 } else {
3442 /* Source-Less Blit to render target */
3443 if (Flags & WINEDDBLT_COLORFILL) {
3444 /* This is easy to handle for the D3D Device... */
3445 DWORD color;
3447 TRACE("Colorfill\n");
3449 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3450 must be true if we are here */
3451 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3452 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3453 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3454 TRACE("Surface is higher back buffer, falling back to software\n");
3455 return WINED3DERR_INVALIDCALL;
3458 /* The color as given in the Blt function is in the format of the frame-buffer...
3459 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3461 if (This->resource.format == WINED3DFMT_P8) {
3462 DWORD alpha;
3464 if (primary_render_target_is_p8(myDevice)) alpha = DDBltFx->u5.dwFillColor << 24;
3465 else alpha = 0xFF000000;
3467 if (This->palette) {
3468 color = (alpha |
3469 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3470 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3471 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3472 } else {
3473 color = alpha;
3476 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3477 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3478 color = 0xFFFFFFFF;
3479 } else {
3480 color = ((0xFF000000) |
3481 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3482 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3483 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3486 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3487 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3488 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3490 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3491 color = DDBltFx->u5.dwFillColor;
3493 else {
3494 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3495 return WINED3DERR_INVALIDCALL;
3498 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3499 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3500 1, /* Number of rectangles */
3501 &rect, WINED3DCLEAR_TARGET, color,
3502 0.0 /* Z */,
3503 0 /* Stencil */);
3504 return WINED3D_OK;
3508 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3509 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3510 return WINED3DERR_INVALIDCALL;
3513 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3515 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3516 float depth;
3518 if (Flags & WINEDDBLT_DEPTHFILL) {
3519 switch(This->resource.format) {
3520 case WINED3DFMT_D16:
3521 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3522 break;
3523 case WINED3DFMT_D15S1:
3524 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3525 break;
3526 case WINED3DFMT_D24S8:
3527 case WINED3DFMT_D24X8:
3528 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3529 break;
3530 case WINED3DFMT_D32:
3531 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3532 break;
3533 default:
3534 depth = 0.0;
3535 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3538 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3539 DestRect == NULL ? 0 : 1,
3540 (WINED3DRECT *) DestRect,
3541 WINED3DCLEAR_ZBUFFER,
3542 0x00000000,
3543 depth,
3544 0x00000000);
3547 FIXME("(%p): Unsupp depthstencil blit\n", This);
3548 return WINED3DERR_INVALIDCALL;
3551 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3552 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3553 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3554 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3555 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3556 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3558 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3560 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3561 return WINEDDERR_SURFACEBUSY;
3564 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3565 * except depth blits, which seem to work
3567 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3568 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3569 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3570 return WINED3DERR_INVALIDCALL;
3571 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3572 TRACE("Z Blit override handled the blit\n");
3573 return WINED3D_OK;
3577 /* Special cases for RenderTargets */
3578 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3579 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3580 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3583 /* For the rest call the X11 surface implementation.
3584 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3585 * other Blts are rather rare
3587 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3590 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3591 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3592 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3593 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3594 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3596 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3598 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3599 return WINEDDERR_SURFACEBUSY;
3602 if(myDevice->inScene &&
3603 (iface == myDevice->stencilBufferTarget ||
3604 (Source && Source == myDevice->stencilBufferTarget))) {
3605 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3606 return WINED3DERR_INVALIDCALL;
3609 /* Special cases for RenderTargets */
3610 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3611 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3613 RECT SrcRect, DstRect;
3614 DWORD Flags=0;
3616 if(rsrc) {
3617 SrcRect.left = rsrc->left;
3618 SrcRect.top= rsrc->top;
3619 SrcRect.bottom = rsrc->bottom;
3620 SrcRect.right = rsrc->right;
3621 } else {
3622 SrcRect.left = 0;
3623 SrcRect.top = 0;
3624 SrcRect.right = srcImpl->currentDesc.Width;
3625 SrcRect.bottom = srcImpl->currentDesc.Height;
3628 DstRect.left = dstx;
3629 DstRect.top=dsty;
3630 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3631 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3633 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3634 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3635 Flags |= WINEDDBLT_KEYSRC;
3636 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3637 Flags |= WINEDDBLT_KEYDEST;
3638 if(trans & WINEDDBLTFAST_WAIT)
3639 Flags |= WINEDDBLT_WAIT;
3640 if(trans & WINEDDBLTFAST_DONOTWAIT)
3641 Flags |= WINEDDBLT_DONOTWAIT;
3643 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3647 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3650 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3651 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3652 RGBQUAD col[256];
3653 IWineD3DPaletteImpl *pal = This->palette;
3654 unsigned int n;
3655 TRACE("(%p)\n", This);
3657 if (!pal) return WINED3D_OK;
3659 if(This->resource.format == WINED3DFMT_P8 ||
3660 This->resource.format == WINED3DFMT_A8P8)
3662 int bpp;
3663 GLenum format, internal, type;
3664 CONVERT_TYPES convert;
3666 /* Check if we are using a RTL mode which uses texturing for uploads */
3667 BOOL use_texture = (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX);
3669 /* Check if we have hardware palette conversion if we have convert is set to NO_CONVERSION */
3670 d3dfmt_get_conv(This, TRUE, use_texture, &format, &internal, &type, &convert, &bpp, This->srgb);
3672 if((This->resource.usage & WINED3DUSAGE_RENDERTARGET) && (convert == NO_CONVERSION))
3674 ENTER_GL();
3675 if (This->glDescription.textureName == 0) {
3676 glGenTextures(1, &This->glDescription.textureName);
3677 checkGLcall("glGenTextures");
3679 glBindTexture(This->glDescription.target, This->glDescription.textureName);
3680 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
3681 LEAVE_GL();
3683 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3684 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3686 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3687 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
3689 /* Re-upload the palette */
3690 d3dfmt_p8_upload_palette(iface, convert);
3692 /* Without this some palette updates are missed. This at least happens on Nvidia drivers but
3693 * it works fine using Mesa. */
3694 ENTER_GL();
3695 glFlush();
3696 LEAVE_GL();
3697 } else {
3698 if(!(This->Flags & SFLAG_INSYSMEM)) {
3699 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3700 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3702 TRACE("Dirtifying surface\n");
3703 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3707 if(This->Flags & SFLAG_DIBSECTION) {
3708 TRACE("(%p): Updating the hdc's palette\n", This);
3709 for (n=0; n<256; n++) {
3710 col[n].rgbRed = pal->palents[n].peRed;
3711 col[n].rgbGreen = pal->palents[n].peGreen;
3712 col[n].rgbBlue = pal->palents[n].peBlue;
3713 col[n].rgbReserved = 0;
3715 SetDIBColorTable(This->hDC, 0, 256, col);
3718 /* Propagate the changes to the drawable when we have a palette. */
3719 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3720 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3722 return WINED3D_OK;
3725 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3726 /** Check against the maximum texture sizes supported by the video card **/
3727 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3728 unsigned int pow2Width, pow2Height;
3729 const GlPixelFormatDesc *glDesc;
3731 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3732 /* Setup some glformat defaults */
3733 This->glDescription.glFormat = glDesc->glFormat;
3734 This->glDescription.glFormatInternal = glDesc->glInternal;
3735 This->glDescription.glType = glDesc->glType;
3737 This->glDescription.textureName = 0;
3738 This->glDescription.target = GL_TEXTURE_2D;
3740 /* Non-power2 support */
3741 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3742 pow2Width = This->currentDesc.Width;
3743 pow2Height = This->currentDesc.Height;
3744 } else {
3745 /* Find the nearest pow2 match */
3746 pow2Width = pow2Height = 1;
3747 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3748 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3750 This->pow2Width = pow2Width;
3751 This->pow2Height = pow2Height;
3753 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3754 WINED3DFORMAT Format = This->resource.format;
3755 /** TODO: add support for non power two compressed textures **/
3756 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3757 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3758 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3759 This, This->currentDesc.Width, This->currentDesc.Height);
3760 return WINED3DERR_NOTAVAILABLE;
3764 if(pow2Width != This->currentDesc.Width ||
3765 pow2Height != This->currentDesc.Height) {
3766 This->Flags |= SFLAG_NONPOW2;
3769 TRACE("%p\n", This);
3770 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3771 /* one of three options
3772 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)
3773 2: Set the texture to the maximum size (bad idea)
3774 3: WARN and return WINED3DERR_NOTAVAILABLE;
3775 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.
3777 WARN("(%p) Creating an oversized surface\n", This);
3778 This->Flags |= SFLAG_OVERSIZE;
3780 /* This will be initialized on the first blt */
3781 This->glRect.left = 0;
3782 This->glRect.top = 0;
3783 This->glRect.right = 0;
3784 This->glRect.bottom = 0;
3785 } else {
3786 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3787 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3788 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3789 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3791 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3792 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3794 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3795 This->pow2Width = This->currentDesc.Width;
3796 This->pow2Height = This->currentDesc.Height;
3797 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
3800 /* No oversize, gl rect is the full texture size */
3801 This->Flags &= ~SFLAG_OVERSIZE;
3802 This->glRect.left = 0;
3803 This->glRect.top = 0;
3804 This->glRect.right = This->pow2Width;
3805 This->glRect.bottom = This->pow2Height;
3808 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3809 switch(wined3d_settings.offscreen_rendering_mode) {
3810 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3811 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3812 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3816 This->Flags |= SFLAG_INSYSMEM;
3818 return WINED3D_OK;
3821 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3822 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3823 IWineD3DBaseTexture *texture;
3825 TRACE("(%p)->(%s, %s)\n", iface,
3826 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3827 persistent ? "TRUE" : "FALSE");
3829 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3830 IWineD3DSwapChain *swapchain = NULL;
3832 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3833 TRACE("Surface %p is an onscreen surface\n", iface);
3835 IWineD3DSwapChain_Release(swapchain);
3836 } else {
3837 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
3838 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3842 if(persistent) {
3843 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3844 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3845 TRACE("Passing to container\n");
3846 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3847 IWineD3DBaseTexture_Release(texture);
3850 This->Flags &= ~SFLAG_LOCATIONS;
3851 This->Flags |= flag;
3852 } else {
3853 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3854 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3855 TRACE("Passing to container\n");
3856 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3857 IWineD3DBaseTexture_Release(texture);
3860 This->Flags &= ~flag;
3864 struct coords {
3865 GLfloat x, y, z;
3868 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3869 struct coords coords[4];
3870 RECT rect;
3871 IWineD3DSwapChain *swapchain = NULL;
3872 IWineD3DBaseTexture *texture = NULL;
3873 HRESULT hr;
3874 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3876 if(rect_in) {
3877 rect = *rect_in;
3878 } else {
3879 rect.left = 0;
3880 rect.top = 0;
3881 rect.right = This->currentDesc.Width;
3882 rect.bottom = This->currentDesc.Height;
3885 ActivateContext(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
3886 ENTER_GL();
3888 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3889 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3890 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3891 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3892 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3893 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3894 checkGLcall("glTexParameteri");
3895 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3896 checkGLcall("glTexParameteri");
3898 coords[0].x = rect.left;
3899 coords[0].z = 0;
3901 coords[1].x = rect.left;
3902 coords[1].z = 0;
3904 coords[2].x = rect.right;
3905 coords[2].z = 0;
3907 coords[3].x = rect.right;
3908 coords[3].z = 0;
3910 coords[0].y = rect.top;
3911 coords[1].y = rect.bottom;
3912 coords[2].y = rect.bottom;
3913 coords[3].y = rect.top;
3914 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3915 glEnable(GL_TEXTURE_2D);
3916 checkGLcall("glEnable(GL_TEXTURE_2D)");
3917 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3918 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3919 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3920 checkGLcall("glTexParameteri");
3921 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3922 checkGLcall("glTexParameteri");
3924 coords[0].x = (float)rect.left / This->pow2Width;
3925 coords[0].z = 0;
3927 coords[1].x = (float)rect.left / This->pow2Width;
3928 coords[1].z = 0;
3930 coords[2].x = (float)rect.right / This->pow2Width;
3931 coords[2].z = 0;
3933 coords[3].x = (float)rect.right / This->pow2Width;
3934 coords[3].z = 0;
3936 coords[0].y = (float)rect.top / This->pow2Height;
3937 coords[1].y = (float)rect.bottom / This->pow2Height;
3938 coords[2].y = (float)rect.bottom / This->pow2Height;
3939 coords[3].y = (float)rect.top / This->pow2Height;
3940 } else {
3941 /* Must be a cube map */
3942 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3943 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3944 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3945 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3946 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3947 checkGLcall("glTexParameteri");
3948 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3949 checkGLcall("glTexParameteri");
3951 switch(This->glDescription.target) {
3952 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3953 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3954 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3955 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3956 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3957 break;
3959 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3960 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3961 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3962 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3963 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3964 break;
3966 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3967 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3968 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3969 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3970 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3971 break;
3973 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3974 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3975 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3976 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3977 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3978 break;
3980 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3981 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3982 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3983 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3984 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3985 break;
3987 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3988 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3989 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3990 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3991 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3992 break;
3994 default:
3995 ERR("Unexpected texture target\n");
3996 LEAVE_GL();
3997 return;
4001 glBegin(GL_QUADS);
4002 glTexCoord3fv(&coords[0].x);
4003 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
4005 glTexCoord3fv(&coords[1].x);
4006 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
4008 glTexCoord3fv(&coords[2].x);
4009 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
4011 glTexCoord3fv(&coords[3].x);
4012 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
4013 glEnd();
4014 checkGLcall("glEnd");
4016 if(This->glDescription.target != GL_TEXTURE_2D) {
4017 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4018 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4019 } else {
4020 glDisable(GL_TEXTURE_2D);
4021 checkGLcall("glDisable(GL_TEXTURE_2D)");
4024 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
4025 if(hr == WINED3D_OK && swapchain) {
4026 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
4027 if(((IWineD3DSwapChainImpl*)swapchain)->frontBuffer == (IWineD3DSurface*)This ||
4028 ((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
4029 glFlush();
4031 IWineD3DSwapChain_Release(swapchain);
4032 } else {
4033 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
4034 * reset properly next draw
4036 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
4037 if(hr == WINED3D_OK && texture) {
4038 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
4039 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
4040 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
4041 IWineD3DBaseTexture_Release(texture);
4044 LEAVE_GL();
4047 /*****************************************************************************
4048 * IWineD3DSurface::LoadLocation
4050 * Copies the current surface data from wherever it is to the requested
4051 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4052 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4053 * multiple locations, the gl texture is preferred over the drawable, which is
4054 * preferred over system memory. The PBO counts as system memory. If rect is
4055 * not NULL, only the specified rectangle is copied (only supported for
4056 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4057 * location is marked up to date after the copy.
4059 * Parameters:
4060 * flag: Surface location flag to be updated
4061 * rect: rectangle to be copied
4063 * Returns:
4064 * WINED3D_OK on success
4065 * WINED3DERR_DEVICELOST on an internal error
4067 *****************************************************************************/
4068 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4069 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4070 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4071 IWineD3DSwapChain *swapchain = NULL;
4072 GLenum format, internal, type;
4073 CONVERT_TYPES convert;
4074 int bpp;
4075 int width, pitch, outpitch;
4076 BYTE *mem;
4078 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4079 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
4080 TRACE("Surface %p is an onscreen surface\n", iface);
4082 IWineD3DSwapChain_Release(swapchain);
4083 } else {
4084 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4085 * Prefer SFLAG_INTEXTURE. */
4086 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4090 TRACE("(%p)->(%s, %p)\n", iface,
4091 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
4092 rect);
4093 if(rect) {
4094 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4097 if(This->Flags & flag) {
4098 TRACE("Location already up to date\n");
4099 return WINED3D_OK;
4102 if(!(This->Flags & SFLAG_LOCATIONS)) {
4103 ERR("Surface does not have any up to date location\n");
4104 This->Flags |= SFLAG_LOST;
4105 return WINED3DERR_DEVICELOST;
4108 if(flag == SFLAG_INSYSMEM) {
4109 surface_prepare_system_memory(This);
4111 /* Download the surface to system memory */
4112 if(This->Flags & SFLAG_INTEXTURE) {
4113 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4114 surface_bind_and_dirtify(This);
4116 surface_download_data(This);
4117 } else {
4118 read_from_framebuffer(This, rect,
4119 This->resource.allocatedMemory,
4120 IWineD3DSurface_GetPitch(iface));
4122 } else if(flag == SFLAG_INDRAWABLE) {
4123 if(This->Flags & SFLAG_INTEXTURE) {
4124 surface_blt_to_drawable(This, rect);
4125 } else {
4126 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4128 /* The width is in 'length' not in bytes */
4129 width = This->currentDesc.Width;
4130 pitch = IWineD3DSurface_GetPitch(iface);
4132 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4133 * but it isn't set (yet) in all cases it is getting called. */
4134 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4135 TRACE("Removing the pbo attached to surface %p\n", This);
4136 surface_remove_pbo(This);
4139 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4140 int height = This->currentDesc.Height;
4142 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4143 outpitch = width * bpp;
4144 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4146 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4147 if(!mem) {
4148 ERR("Out of memory %d, %d!\n", outpitch, height);
4149 return WINED3DERR_OUTOFVIDEOMEMORY;
4151 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4153 This->Flags |= SFLAG_CONVERTED;
4154 } else {
4155 This->Flags &= ~SFLAG_CONVERTED;
4156 mem = This->resource.allocatedMemory;
4159 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4161 /* Don't delete PBO memory */
4162 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4163 HeapFree(GetProcessHeap(), 0, mem);
4165 } else /* if(flag == SFLAG_INTEXTURE) */ {
4166 if (This->Flags & SFLAG_INDRAWABLE) {
4167 read_from_framebuffer_texture(This);
4168 } else { /* Upload from system memory */
4169 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4171 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4172 surface_bind_and_dirtify(This);
4173 ENTER_GL();
4175 /* The only place where LoadTexture() might get called when isInDraw=1
4176 * is ActivateContext where lastActiveRenderTarget is preloaded.
4178 if(iface == device->lastActiveRenderTarget && device->isInDraw)
4179 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
4181 /* Otherwise: System memory copy must be most up to date */
4183 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4184 This->Flags |= SFLAG_GLCKEY;
4185 This->glCKey = This->SrcBltCKey;
4187 else This->Flags &= ~SFLAG_GLCKEY;
4189 /* The width is in 'length' not in bytes */
4190 width = This->currentDesc.Width;
4191 pitch = IWineD3DSurface_GetPitch(iface);
4193 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4194 * but it isn't set (yet) in all cases it is getting called. */
4195 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4196 TRACE("Removing the pbo attached to surface %p\n", This);
4197 surface_remove_pbo(This);
4200 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4201 int height = This->currentDesc.Height;
4203 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4204 outpitch = width * bpp;
4205 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4207 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4208 if(!mem) {
4209 ERR("Out of memory %d, %d!\n", outpitch, height);
4210 return WINED3DERR_OUTOFVIDEOMEMORY;
4212 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4214 This->Flags |= SFLAG_CONVERTED;
4215 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
4216 d3dfmt_p8_upload_palette(iface, convert);
4217 This->Flags &= ~SFLAG_CONVERTED;
4218 mem = This->resource.allocatedMemory;
4219 } else {
4220 This->Flags &= ~SFLAG_CONVERTED;
4221 mem = This->resource.allocatedMemory;
4224 /* Make sure the correct pitch is used */
4225 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4227 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4228 TRACE("non power of two support\n");
4229 if(!(This->Flags & SFLAG_ALLOCATED)) {
4230 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4232 if (mem || (This->Flags & SFLAG_PBO)) {
4233 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4235 } else {
4236 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4237 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4239 if(!(This->Flags & SFLAG_ALLOCATED)) {
4240 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4242 if (mem || (This->Flags & SFLAG_PBO)) {
4243 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4247 /* Restore the default pitch */
4248 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4249 LEAVE_GL();
4251 /* Don't delete PBO memory */
4252 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4253 HeapFree(GetProcessHeap(), 0, mem);
4257 if(rect == NULL) {
4258 This->Flags |= flag;
4261 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4262 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4263 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4264 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4267 return WINED3D_OK;
4270 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4271 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4272 IWineD3DSwapChain *swapchain = NULL;
4274 /* Update the drawable size method */
4275 if(container) {
4276 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4278 if(swapchain) {
4279 This->get_drawable_size = get_drawable_size_swapchain;
4280 IWineD3DSwapChain_Release(swapchain);
4281 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4282 switch(wined3d_settings.offscreen_rendering_mode) {
4283 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4284 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4285 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4289 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4292 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4293 return SURFACE_OPENGL;
4296 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4298 /* IUnknown */
4299 IWineD3DBaseSurfaceImpl_QueryInterface,
4300 IWineD3DBaseSurfaceImpl_AddRef,
4301 IWineD3DSurfaceImpl_Release,
4302 /* IWineD3DResource */
4303 IWineD3DBaseSurfaceImpl_GetParent,
4304 IWineD3DBaseSurfaceImpl_GetDevice,
4305 IWineD3DBaseSurfaceImpl_SetPrivateData,
4306 IWineD3DBaseSurfaceImpl_GetPrivateData,
4307 IWineD3DBaseSurfaceImpl_FreePrivateData,
4308 IWineD3DBaseSurfaceImpl_SetPriority,
4309 IWineD3DBaseSurfaceImpl_GetPriority,
4310 IWineD3DSurfaceImpl_PreLoad,
4311 IWineD3DSurfaceImpl_UnLoad,
4312 IWineD3DBaseSurfaceImpl_GetType,
4313 /* IWineD3DSurface */
4314 IWineD3DBaseSurfaceImpl_GetContainer,
4315 IWineD3DBaseSurfaceImpl_GetDesc,
4316 IWineD3DSurfaceImpl_LockRect,
4317 IWineD3DSurfaceImpl_UnlockRect,
4318 IWineD3DSurfaceImpl_GetDC,
4319 IWineD3DSurfaceImpl_ReleaseDC,
4320 IWineD3DSurfaceImpl_Flip,
4321 IWineD3DSurfaceImpl_Blt,
4322 IWineD3DBaseSurfaceImpl_GetBltStatus,
4323 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4324 IWineD3DBaseSurfaceImpl_IsLost,
4325 IWineD3DBaseSurfaceImpl_Restore,
4326 IWineD3DSurfaceImpl_BltFast,
4327 IWineD3DBaseSurfaceImpl_GetPalette,
4328 IWineD3DBaseSurfaceImpl_SetPalette,
4329 IWineD3DSurfaceImpl_RealizePalette,
4330 IWineD3DBaseSurfaceImpl_SetColorKey,
4331 IWineD3DBaseSurfaceImpl_GetPitch,
4332 IWineD3DSurfaceImpl_SetMem,
4333 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4334 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4335 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4336 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4337 IWineD3DBaseSurfaceImpl_SetClipper,
4338 IWineD3DBaseSurfaceImpl_GetClipper,
4339 /* Internal use: */
4340 IWineD3DSurfaceImpl_AddDirtyRect,
4341 IWineD3DSurfaceImpl_LoadTexture,
4342 IWineD3DSurfaceImpl_BindTexture,
4343 IWineD3DSurfaceImpl_SaveSnapshot,
4344 IWineD3DSurfaceImpl_SetContainer,
4345 IWineD3DSurfaceImpl_SetGlTextureDesc,
4346 IWineD3DSurfaceImpl_GetGlDesc,
4347 IWineD3DSurfaceImpl_GetData,
4348 IWineD3DSurfaceImpl_SetFormat,
4349 IWineD3DSurfaceImpl_PrivateSetup,
4350 IWineD3DSurfaceImpl_ModifyLocation,
4351 IWineD3DSurfaceImpl_LoadLocation,
4352 IWineD3DSurfaceImpl_GetImplType