avifil32: The array index is 0-based.
[wine/multimedia.git] / dlls / wined3d / surface.c
blobd89203195bdaf962a023bbb8fc6f722ff45224cc
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 if((This->Flags & SFLAG_LOCATIONS) == 0) {
632 /* In 1.0-rc4 and earlier, AddDirtyRect was called in the place of this if condition.
633 * This had the problem that a correctly set INDRAWABLE flag was removed if the PreLoad
634 * during the offscreen rendering readback triggered the creation of the GL texture.
635 * The change intended to keep the INDRAWABLE intact. To prevent unintended side effects
636 * before release, set the INSYSMEM flag like the old AddDirtyRect did.
638 WARN("Wine 1.0 safety path hit\n");
639 This->Flags |= SFLAG_INSYSMEM;
642 if(target == GL_TEXTURE_RECTANGLE_ARB && This->glDescription.target != target) {
643 This->Flags &= ~SFLAG_NORMCOORD;
644 } else if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB && target != GL_TEXTURE_RECTANGLE_ARB) {
645 This->Flags |= SFLAG_NORMCOORD;
647 This->glDescription.textureName = textureName;
648 This->glDescription.target = target;
649 This->Flags &= ~SFLAG_ALLOCATED;
652 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
653 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
654 TRACE("(%p) : returning %p\n", This, &This->glDescription);
655 *glDescription = &This->glDescription;
658 /* TODO: think about moving this down to resource? */
659 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
660 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
661 /* 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 */
662 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
663 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
665 return (CONST void*)(This->resource.allocatedMemory);
668 /* Read the framebuffer back into the surface */
669 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
670 IWineD3DSwapChainImpl *swapchain;
671 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
672 BYTE *mem;
673 GLint fmt;
674 GLint type;
675 BYTE *row, *top, *bottom;
676 int i;
677 BOOL bpp;
678 RECT local_rect;
679 BOOL srcIsUpsideDown;
681 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
682 static BOOL warned = FALSE;
683 if(!warned) {
684 ERR("The application tries to lock the render target, but render target locking is disabled\n");
685 warned = TRUE;
687 return;
690 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
691 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
692 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
693 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
694 * context->last_was_blit set on the unlock.
696 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
697 ENTER_GL();
699 /* Select the correct read buffer, and give some debug output.
700 * There is no need to keep track of the current read buffer or reset it, every part of the code
701 * that reads sets the read buffer as desired.
703 if(!swapchain) {
704 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
705 * Read from the back buffer
707 TRACE("Locking offscreen render target\n");
708 glReadBuffer(myDevice->offscreenBuffer);
709 srcIsUpsideDown = TRUE;
710 } else {
711 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
712 TRACE("Locking %#x buffer\n", buffer);
713 glReadBuffer(buffer);
714 checkGLcall("glReadBuffer");
716 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
717 srcIsUpsideDown = FALSE;
720 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
721 if(!rect) {
722 local_rect.left = 0;
723 local_rect.top = 0;
724 local_rect.right = This->currentDesc.Width;
725 local_rect.bottom = This->currentDesc.Height;
726 } else {
727 local_rect = *rect;
729 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
731 switch(This->resource.format)
733 case WINED3DFMT_P8:
735 if(primary_render_target_is_p8(myDevice)) {
736 /* In case of P8 render targets the index is stored in the alpha component */
737 fmt = GL_ALPHA;
738 type = GL_UNSIGNED_BYTE;
739 mem = dest;
740 bpp = This->bytesPerPixel;
741 } else {
742 /* GL can't return palettized data, so read ARGB pixels into a
743 * separate block of memory and convert them into palettized format
744 * in software. Slow, but if the app means to use palettized render
745 * targets and locks it...
747 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
748 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
749 * for the color channels when palettizing the colors.
751 fmt = GL_RGB;
752 type = GL_UNSIGNED_BYTE;
753 pitch *= 3;
754 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
755 if(!mem) {
756 ERR("Out of memory\n");
757 LEAVE_GL();
758 return;
760 bpp = This->bytesPerPixel * 3;
763 break;
765 default:
766 mem = dest;
767 fmt = This->glDescription.glFormat;
768 type = This->glDescription.glType;
769 bpp = This->bytesPerPixel;
772 if(This->Flags & SFLAG_PBO) {
773 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
774 checkGLcall("glBindBufferARB");
777 glReadPixels(local_rect.left, local_rect.top,
778 local_rect.right - local_rect.left,
779 local_rect.bottom - local_rect.top,
780 fmt, type, mem);
781 vcheckGLcall("glReadPixels");
783 if(This->Flags & SFLAG_PBO) {
784 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
785 checkGLcall("glBindBufferARB");
787 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
788 * to get a pointer to it and perform the flipping in software. This is a lot
789 * faster than calling glReadPixels for each line. In case we want more speed
790 * we should rerender it flipped in a FBO and read the data back from the FBO. */
791 if(!srcIsUpsideDown) {
792 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
793 checkGLcall("glBindBufferARB");
795 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
796 checkGLcall("glMapBufferARB");
800 /* TODO: Merge this with the palettization loop below for P8 targets */
801 if(!srcIsUpsideDown) {
802 UINT len, off;
803 /* glReadPixels returns the image upside down, and there is no way to prevent this.
804 Flip the lines in software */
805 len = (local_rect.right - local_rect.left) * bpp;
806 off = local_rect.left * bpp;
808 row = HeapAlloc(GetProcessHeap(), 0, len);
809 if(!row) {
810 ERR("Out of memory\n");
811 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
812 LEAVE_GL();
813 return;
816 top = mem + pitch * local_rect.top;
817 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
818 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
819 memcpy(row, top + off, len);
820 memcpy(top + off, bottom + off, len);
821 memcpy(bottom + off, row, len);
822 top += pitch;
823 bottom -= pitch;
825 HeapFree(GetProcessHeap(), 0, row);
827 /* Unmap the temp PBO buffer */
828 if(This->Flags & SFLAG_PBO) {
829 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
830 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
834 LEAVE_GL();
836 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
837 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
838 * the same color but we have no choice.
839 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
841 if((This->resource.format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice)) {
842 PALETTEENTRY *pal = NULL;
843 DWORD width = pitch / 3;
844 int x, y, c;
846 if(This->palette) {
847 pal = This->palette->palents;
848 } else {
849 ERR("Palette is missing, cannot perform inverse palette lookup\n");
850 HeapFree(GetProcessHeap(), 0, mem);
851 return ;
854 for(y = local_rect.top; y < local_rect.bottom; y++) {
855 for(x = local_rect.left; x < local_rect.right; x++) {
856 /* start lines pixels */
857 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
858 BYTE *green = blue + 1;
859 BYTE *red = green + 1;
861 for(c = 0; c < 256; c++) {
862 if(*red == pal[c].peRed &&
863 *green == pal[c].peGreen &&
864 *blue == pal[c].peBlue)
866 *((BYTE *) dest + y * width + x) = c;
867 break;
872 HeapFree(GetProcessHeap(), 0, mem);
876 /* Read the framebuffer contents into a texture */
877 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
879 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
880 IWineD3DSwapChainImpl *swapchain;
881 int bpp;
882 GLenum format, internal, type;
883 CONVERT_TYPES convert;
884 GLint prevRead;
886 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
888 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
889 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
890 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
891 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
892 * context->last_was_blit set on the unlock.
894 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
895 surface_bind_and_dirtify(This);
896 ENTER_GL();
898 glGetIntegerv(GL_READ_BUFFER, &prevRead);
900 /* Select the correct read buffer, and give some debug output.
901 * There is no need to keep track of the current read buffer or reset it, every part of the code
902 * that reads sets the read buffer as desired.
904 if(!swapchain) {
905 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
906 * Read from the back buffer
908 TRACE("Locking offscreen render target\n");
909 glReadBuffer(device->offscreenBuffer);
910 } else {
911 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
912 TRACE("Locking %#x buffer\n", buffer);
913 glReadBuffer(buffer);
914 checkGLcall("glReadBuffer");
916 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
919 if(!(This->Flags & SFLAG_ALLOCATED)) {
920 surface_allocate_surface(This, internal, This->pow2Width,
921 This->pow2Height, format, type);
924 clear_unused_channels(This);
926 /* If !SrcIsUpsideDown we should flip the surface.
927 * This can be done using glCopyTexSubImage2D but this
928 * is VERY slow, so don't do that. We should prevent
929 * this code from getting called in such cases or perhaps
930 * we can use FBOs */
932 glCopyTexSubImage2D(This->glDescription.target,
933 This->glDescription.level,
934 0, 0, 0, 0,
935 This->currentDesc.Width,
936 This->currentDesc.Height);
937 checkGLcall("glCopyTexSubImage2D");
939 glReadBuffer(prevRead);
940 vcheckGLcall("glReadBuffer");
942 LEAVE_GL();
943 TRACE("Updated target %d\n", This->glDescription.target);
946 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
947 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
948 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
949 * changed
951 if(!(This->Flags & SFLAG_DYNLOCK)) {
952 This->lockCount++;
953 /* MAXLOCKCOUNT is defined in wined3d_private.h */
954 if(This->lockCount > MAXLOCKCOUNT) {
955 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
956 This->Flags |= SFLAG_DYNLOCK;
960 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
961 * Also don't create a PBO for systemmem surfaces.
963 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
964 GLenum error;
965 ENTER_GL();
967 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
968 error = glGetError();
969 if(This->pbo == 0 || error != GL_NO_ERROR) {
970 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
973 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
975 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
976 checkGLcall("glBindBufferARB");
978 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
979 checkGLcall("glBufferDataARB");
981 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
982 checkGLcall("glBindBufferARB");
984 /* We don't need the system memory anymore and we can't even use it for PBOs */
985 if(!(This->Flags & SFLAG_CLIENT)) {
986 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
987 This->resource.heapMemory = NULL;
989 This->resource.allocatedMemory = NULL;
990 This->Flags |= SFLAG_PBO;
991 LEAVE_GL();
992 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
993 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
994 * or a pbo to map
996 if(!This->resource.heapMemory) {
997 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
999 This->resource.allocatedMemory =
1000 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1001 if(This->Flags & SFLAG_INSYSMEM) {
1002 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1007 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1008 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1009 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1010 IWineD3DSwapChain *swapchain = NULL;
1012 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1014 /* This is also done in the base class, but we have to verify this before loading any data from
1015 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1016 * may interfere, and all other bad things may happen
1018 if (This->Flags & SFLAG_LOCKED) {
1019 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1020 return WINED3DERR_INVALIDCALL;
1022 This->Flags |= SFLAG_LOCKED;
1024 if (!(This->Flags & SFLAG_LOCKABLE))
1026 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1029 if (Flags & WINED3DLOCK_DISCARD) {
1030 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1031 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1032 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1033 This->Flags |= SFLAG_INSYSMEM;
1034 goto lock_end;
1037 if (This->Flags & SFLAG_INSYSMEM) {
1038 TRACE("Local copy is up to date, not downloading data\n");
1039 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1040 goto lock_end;
1043 /* Now download the surface content from opengl
1044 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1045 * Offscreen targets which are not active at the moment or are higher targets(FBOs) can be locked with the texture path
1047 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1048 if(swapchain || iface == myDevice->render_targets[0]) {
1049 const RECT *pass_rect = pRect;
1051 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1052 * because most caller functions do not need that. So do that here
1054 if(pRect &&
1055 pRect->top == 0 &&
1056 pRect->left == 0 &&
1057 pRect->right == This->currentDesc.Width &&
1058 pRect->bottom == This->currentDesc.Height) {
1059 pass_rect = NULL;
1062 switch(wined3d_settings.rendertargetlock_mode) {
1063 case RTL_TEXDRAW:
1064 case RTL_TEXTEX:
1065 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1066 #if 0
1067 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1068 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1069 * This may be faster on some cards
1071 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1072 #endif
1073 /* drop through */
1075 case RTL_AUTO:
1076 case RTL_READDRAW:
1077 case RTL_READTEX:
1078 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1079 break;
1081 case RTL_DISABLE:
1082 break;
1084 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1086 } else if(iface == myDevice->stencilBufferTarget) {
1087 /** the depth stencil in openGL has a format of GL_FLOAT
1088 * which should be good for WINED3DFMT_D16_LOCKABLE
1089 * and WINED3DFMT_D16
1090 * it is unclear what format the stencil buffer is in except.
1091 * 'Each index is converted to fixed point...
1092 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1093 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1094 * glReadPixels(This->lockedRect.left,
1095 * This->lockedRect.bottom - j - 1,
1096 * This->lockedRect.right - This->lockedRect.left,
1097 * 1,
1098 * GL_DEPTH_COMPONENT,
1099 * type,
1100 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1102 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1103 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1104 * none of that is the case the problem is not in this function :-)
1105 ********************************************/
1106 FIXME("Depth stencil locking not supported yet\n");
1107 } else {
1108 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1109 TRACE("locking an ordinary surface\n");
1110 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1113 lock_end:
1114 if(This->Flags & SFLAG_PBO) {
1115 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1116 ENTER_GL();
1117 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1118 checkGLcall("glBindBufferARB");
1120 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1121 if(This->resource.allocatedMemory) {
1122 ERR("The surface already has PBO memory allocated!\n");
1125 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1126 checkGLcall("glMapBufferARB");
1128 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1129 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1130 checkGLcall("glBindBufferARB");
1132 LEAVE_GL();
1135 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1136 /* Don't dirtify */
1137 } else {
1138 IWineD3DBaseTexture *pBaseTexture;
1140 * Dirtify on lock
1141 * as seen in msdn docs
1143 IWineD3DSurface_AddDirtyRect(iface, pRect);
1145 /** Dirtify Container if needed */
1146 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1147 TRACE("Making container dirty\n");
1148 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1149 IWineD3DBaseTexture_Release(pBaseTexture);
1150 } else {
1151 TRACE("Surface is standalone, no need to dirty the container\n");
1155 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1158 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1159 GLint prev_store;
1160 GLint prev_rasterpos[4];
1161 GLint skipBytes = 0;
1162 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1163 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1164 IWineD3DSwapChainImpl *swapchain;
1166 /* Activate the correct context for the render target */
1167 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1168 ENTER_GL();
1170 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1171 if(!swapchain) {
1172 /* Primary offscreen render target */
1173 TRACE("Offscreen render target\n");
1174 glDrawBuffer(myDevice->offscreenBuffer);
1175 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1176 } else {
1177 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1178 TRACE("Unlocking %#x buffer\n", buffer);
1179 glDrawBuffer(buffer);
1180 checkGLcall("glDrawBuffer");
1182 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1185 glFlush();
1186 vcheckGLcall("glFlush");
1187 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1188 vcheckGLcall("glIntegerv");
1189 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1190 vcheckGLcall("glIntegerv");
1191 glPixelZoom(1.0, -1.0);
1192 vcheckGLcall("glPixelZoom");
1194 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1195 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1196 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1198 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1199 vcheckGLcall("glRasterPos2f");
1201 /* Some drivers(radeon dri, others?) don't like exceptions during
1202 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1203 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1204 * catch to put the dib section in InSync mode, which leads to a crash
1205 * and a blocked x server on my radeon card.
1207 * The following lines read the dib section so it is put in InSync mode
1208 * before glDrawPixels is called and the crash is prevented. There won't
1209 * be any interfering gdi accesses, because UnlockRect is called from
1210 * ReleaseDC, and the app won't use the dc any more afterwards.
1212 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1213 volatile BYTE read;
1214 read = This->resource.allocatedMemory[0];
1217 if(This->Flags & SFLAG_PBO) {
1218 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1219 checkGLcall("glBindBufferARB");
1222 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1223 if(This->Flags & SFLAG_LOCKED) {
1224 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1225 (This->lockedRect.bottom - This->lockedRect.top)-1,
1226 fmt, type,
1227 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1228 checkGLcall("glDrawPixels");
1229 } else {
1230 glDrawPixels(This->currentDesc.Width,
1231 This->currentDesc.Height,
1232 fmt, type, mem);
1233 checkGLcall("glDrawPixels");
1236 if(This->Flags & SFLAG_PBO) {
1237 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1238 checkGLcall("glBindBufferARB");
1241 glPixelZoom(1.0,1.0);
1242 vcheckGLcall("glPixelZoom");
1244 glRasterPos3iv(&prev_rasterpos[0]);
1245 vcheckGLcall("glRasterPos3iv");
1247 /* Reset to previous pack row length */
1248 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1249 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1251 if(!swapchain) {
1252 glDrawBuffer(myDevice->offscreenBuffer);
1253 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1254 } else if(swapchain->backBuffer) {
1255 glDrawBuffer(GL_BACK);
1256 checkGLcall("glDrawBuffer(GL_BACK)");
1257 } else {
1258 glDrawBuffer(GL_FRONT);
1259 checkGLcall("glDrawBuffer(GL_FRONT)");
1261 LEAVE_GL();
1263 return;
1266 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1267 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1268 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1269 IWineD3DSwapChainImpl *swapchain = NULL;
1270 BOOL fullsurface;
1272 if (!(This->Flags & SFLAG_LOCKED)) {
1273 WARN("trying to Unlock an unlocked surf@%p\n", This);
1274 return WINED3DERR_INVALIDCALL;
1277 if (This->Flags & SFLAG_PBO) {
1278 TRACE("Freeing PBO memory\n");
1279 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1280 ENTER_GL();
1281 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1282 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1283 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1284 checkGLcall("glUnmapBufferARB");
1285 LEAVE_GL();
1286 This->resource.allocatedMemory = NULL;
1289 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1291 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1292 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1293 goto unlock_end;
1296 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1297 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1298 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1300 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1301 static BOOL warned = FALSE;
1302 if(!warned) {
1303 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1304 warned = TRUE;
1306 goto unlock_end;
1309 if(This->dirtyRect.left == 0 &&
1310 This->dirtyRect.top == 0 &&
1311 This->dirtyRect.right == This->currentDesc.Width &&
1312 This->dirtyRect.bottom == This->currentDesc.Height) {
1313 fullsurface = TRUE;
1314 } else {
1315 /* TODO: Proper partial rectangle tracking */
1316 fullsurface = FALSE;
1317 This->Flags |= SFLAG_INSYSMEM;
1320 switch(wined3d_settings.rendertargetlock_mode) {
1321 case RTL_READTEX:
1322 case RTL_TEXTEX:
1323 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1324 ENTER_GL();
1325 if (This->glDescription.textureName == 0) {
1326 glGenTextures(1, &This->glDescription.textureName);
1327 checkGLcall("glGenTextures");
1329 glBindTexture(This->glDescription.target, This->glDescription.textureName);
1330 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
1331 LEAVE_GL();
1332 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1333 /* drop through */
1335 case RTL_AUTO:
1336 case RTL_READDRAW:
1337 case RTL_TEXDRAW:
1338 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1339 break;
1342 if(!fullsurface) {
1343 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1344 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1345 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1346 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1347 * not fully up to date because only a subrectangle was read in LockRect.
1349 This->Flags &= ~SFLAG_INSYSMEM;
1350 This->Flags |= SFLAG_INDRAWABLE;
1353 This->dirtyRect.left = This->currentDesc.Width;
1354 This->dirtyRect.top = This->currentDesc.Height;
1355 This->dirtyRect.right = 0;
1356 This->dirtyRect.bottom = 0;
1357 } else if(iface == myDevice->stencilBufferTarget) {
1358 FIXME("Depth Stencil buffer locking is not implemented\n");
1359 } else {
1360 /* The rest should be a normal texture */
1361 IWineD3DBaseTextureImpl *impl;
1362 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1363 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1364 * states need resetting
1366 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1367 if(impl->baseTexture.bindCount) {
1368 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1370 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1374 unlock_end:
1375 This->Flags &= ~SFLAG_LOCKED;
1376 memset(&This->lockedRect, 0, sizeof(RECT));
1377 return WINED3D_OK;
1380 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1381 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1382 WINED3DLOCKED_RECT lock;
1383 HRESULT hr;
1384 RGBQUAD col[256];
1386 TRACE("(%p)->(%p)\n",This,pHDC);
1388 if(This->Flags & SFLAG_USERPTR) {
1389 ERR("Not supported on surfaces with an application-provided surfaces\n");
1390 return WINEDDERR_NODC;
1393 /* Give more detailed info for ddraw */
1394 if (This->Flags & SFLAG_DCINUSE)
1395 return WINEDDERR_DCALREADYCREATED;
1397 /* Can't GetDC if the surface is locked */
1398 if (This->Flags & SFLAG_LOCKED)
1399 return WINED3DERR_INVALIDCALL;
1401 /* According to Direct3D9 docs, only these formats are supported */
1402 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1403 if (This->resource.format != WINED3DFMT_R5G6B5 &&
1404 This->resource.format != WINED3DFMT_X1R5G5B5 &&
1405 This->resource.format != WINED3DFMT_R8G8B8 &&
1406 This->resource.format != WINED3DFMT_X8R8G8B8) return WINED3DERR_INVALIDCALL;
1409 memset(&lock, 0, sizeof(lock)); /* To be sure */
1411 /* Create a DIB section if there isn't a hdc yet */
1412 if(!This->hDC) {
1413 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1414 if(This->Flags & SFLAG_CLIENT) {
1415 IWineD3DSurface_PreLoad(iface);
1418 /* Use the dib section from now on if we are not using a PBO */
1419 if(!(This->Flags & SFLAG_PBO))
1420 This->resource.allocatedMemory = This->dib.bitmap_data;
1423 /* Lock the surface */
1424 hr = IWineD3DSurface_LockRect(iface,
1425 &lock,
1426 NULL,
1429 if(This->Flags & SFLAG_PBO) {
1430 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1431 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1434 if(FAILED(hr)) {
1435 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1436 /* keep the dib section */
1437 return hr;
1440 if(This->resource.format == WINED3DFMT_P8 ||
1441 This->resource.format == WINED3DFMT_A8P8) {
1442 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1443 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1444 unsigned int n;
1445 PALETTEENTRY *pal = NULL;
1447 if(This->palette) {
1448 pal = This->palette->palents;
1449 } else {
1450 IWineD3DSurfaceImpl *dds_primary = (IWineD3DSurfaceImpl *)This->resource.wineD3DDevice->ddraw_primary;
1451 if (dds_primary && dds_primary->palette)
1452 pal = dds_primary->palette->palents;
1455 if (pal) {
1456 for (n=0; n<256; n++) {
1457 col[n].rgbRed = pal[n].peRed;
1458 col[n].rgbGreen = pal[n].peGreen;
1459 col[n].rgbBlue = pal[n].peBlue;
1460 col[n].rgbReserved = 0;
1462 SetDIBColorTable(This->hDC, 0, 256, col);
1466 *pHDC = This->hDC;
1467 TRACE("returning %p\n",*pHDC);
1468 This->Flags |= SFLAG_DCINUSE;
1470 return WINED3D_OK;
1473 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1474 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1476 TRACE("(%p)->(%p)\n",This,hDC);
1478 if (!(This->Flags & SFLAG_DCINUSE))
1479 return WINED3DERR_INVALIDCALL;
1481 if (This->hDC !=hDC) {
1482 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1483 return WINED3DERR_INVALIDCALL;
1486 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1487 /* Copy the contents of the DIB over to the PBO */
1488 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1491 /* we locked first, so unlock now */
1492 IWineD3DSurface_UnlockRect(iface);
1494 This->Flags &= ~SFLAG_DCINUSE;
1496 return WINED3D_OK;
1499 /* ******************************************************
1500 IWineD3DSurface Internal (No mapping to directx api) parts follow
1501 ****************************************************** */
1503 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) {
1504 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1505 const GlPixelFormatDesc *glDesc;
1506 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1507 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1509 /* Default values: From the surface */
1510 *format = glDesc->glFormat;
1511 *type = glDesc->glType;
1512 *convert = NO_CONVERSION;
1513 *target_bpp = This->bytesPerPixel;
1515 if(srgb_mode) {
1516 *internal = glDesc->glGammaInternal;
1517 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1518 *internal = glDesc->rtInternal;
1519 } else {
1520 *internal = glDesc->glInternal;
1523 /* Ok, now look if we have to do any conversion */
1524 switch(This->resource.format) {
1525 case WINED3DFMT_P8:
1526 /* ****************
1527 Paletted Texture
1528 **************** */
1530 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1531 * of the two is available make sure texturing is requested as neither of the two works in
1532 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1533 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1534 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1535 * conflicts with this.
1537 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) || (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) && primary_render_target_is_p8(device))) || colorkey_active || !use_texturing ) {
1538 *format = GL_RGBA;
1539 *internal = GL_RGBA;
1540 *type = GL_UNSIGNED_BYTE;
1541 *target_bpp = 4;
1542 if(colorkey_active) {
1543 *convert = CONVERT_PALETTED_CK;
1544 } else {
1545 *convert = CONVERT_PALETTED;
1548 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1549 *format = GL_ALPHA;
1550 *internal = GL_RGBA;
1551 *type = GL_UNSIGNED_BYTE;
1552 *target_bpp = 1;
1555 break;
1557 case WINED3DFMT_R3G3B2:
1558 /* **********************
1559 GL_UNSIGNED_BYTE_3_3_2
1560 ********************** */
1561 if (colorkey_active) {
1562 /* This texture format will never be used.. So do not care about color keying
1563 up until the point in time it will be needed :-) */
1564 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1566 break;
1568 case WINED3DFMT_R5G6B5:
1569 if (colorkey_active) {
1570 *convert = CONVERT_CK_565;
1571 *format = GL_RGBA;
1572 *internal = GL_RGBA;
1573 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1575 break;
1577 case WINED3DFMT_X1R5G5B5:
1578 if (colorkey_active) {
1579 *convert = CONVERT_CK_5551;
1580 *format = GL_BGRA;
1581 *internal = GL_RGBA;
1582 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1584 break;
1586 case WINED3DFMT_R8G8B8:
1587 if (colorkey_active) {
1588 *convert = CONVERT_CK_RGB24;
1589 *format = GL_RGBA;
1590 *internal = GL_RGBA;
1591 *type = GL_UNSIGNED_INT_8_8_8_8;
1592 *target_bpp = 4;
1594 break;
1596 case WINED3DFMT_X8R8G8B8:
1597 if (colorkey_active) {
1598 *convert = CONVERT_RGB32_888;
1599 *format = GL_RGBA;
1600 *internal = GL_RGBA;
1601 *type = GL_UNSIGNED_INT_8_8_8_8;
1603 break;
1605 case WINED3DFMT_V8U8:
1606 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1607 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1608 *format = GL_DUDV_ATI;
1609 *internal = GL_DU8DV8_ATI;
1610 *type = GL_BYTE;
1611 /* No conversion - Just change the gl type */
1612 break;
1614 *convert = CONVERT_V8U8;
1615 *format = GL_BGR;
1616 *internal = GL_RGB8;
1617 *type = GL_UNSIGNED_BYTE;
1618 *target_bpp = 3;
1619 break;
1621 case WINED3DFMT_L6V5U5:
1622 *convert = CONVERT_L6V5U5;
1623 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1624 *target_bpp = 3;
1625 /* Use format and types from table */
1626 } else {
1627 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1628 *target_bpp = 2;
1629 *format = GL_RGB;
1630 *internal = GL_RGB5;
1631 *type = GL_UNSIGNED_SHORT_5_6_5;
1633 break;
1635 case WINED3DFMT_X8L8V8U8:
1636 *convert = CONVERT_X8L8V8U8;
1637 *target_bpp = 4;
1638 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1639 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1640 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1641 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1642 * the needed type and format parameter, so the internal format contains a
1643 * 4th component, which is returned as alpha
1645 } else {
1646 /* Not supported by GL_ATI_envmap_bumpmap */
1647 *format = GL_BGRA;
1648 *internal = GL_RGB8;
1649 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1651 break;
1653 case WINED3DFMT_Q8W8V8U8:
1654 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1655 *convert = CONVERT_Q8W8V8U8;
1656 *format = GL_BGRA;
1657 *internal = GL_RGBA8;
1658 *type = GL_UNSIGNED_BYTE;
1659 *target_bpp = 4;
1660 /* Not supported by GL_ATI_envmap_bumpmap */
1661 break;
1663 case WINED3DFMT_V16U16:
1664 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1665 *convert = CONVERT_V16U16;
1666 *format = GL_BGR;
1667 *internal = GL_RGB16_EXT;
1668 *type = GL_UNSIGNED_SHORT;
1669 *target_bpp = 6;
1670 /* What should I do here about GL_ATI_envmap_bumpmap?
1671 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1673 break;
1675 case WINED3DFMT_A4L4:
1676 /* A4L4 exists as an internal gl format, but for some reason there is not
1677 * format+type combination to load it. Thus convert it to A8L8, then load it
1678 * with A4L4 internal, but A8L8 format+type
1680 *convert = CONVERT_A4L4;
1681 *format = GL_LUMINANCE_ALPHA;
1682 *internal = GL_LUMINANCE4_ALPHA4;
1683 *type = GL_UNSIGNED_BYTE;
1684 *target_bpp = 2;
1685 break;
1687 case WINED3DFMT_R32F:
1688 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1689 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1690 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1691 * 1.0 instead.
1693 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1695 *convert = CONVERT_R32F;
1696 *format = GL_RGB;
1697 *internal = GL_RGB32F_ARB;
1698 *type = GL_FLOAT;
1699 *target_bpp = 12;
1700 break;
1702 case WINED3DFMT_R16F:
1703 /* Similar to R32F */
1704 *convert = CONVERT_R16F;
1705 *format = GL_RGB;
1706 *internal = GL_RGB16F_ARB;
1707 *type = GL_HALF_FLOAT_ARB;
1708 *target_bpp = 6;
1709 break;
1711 case WINED3DFMT_G16R16:
1712 *convert = CONVERT_G16R16;
1713 *format = GL_RGB;
1714 *internal = GL_RGB16_EXT;
1715 *type = GL_UNSIGNED_SHORT;
1716 *target_bpp = 6;
1717 break;
1719 default:
1720 break;
1723 return WINED3D_OK;
1726 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1727 BYTE *source, *dest;
1728 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1730 switch (convert) {
1731 case NO_CONVERSION:
1733 memcpy(dst, src, pitch * height);
1734 break;
1736 case CONVERT_PALETTED:
1737 case CONVERT_PALETTED_CK:
1739 IWineD3DPaletteImpl* pal = This->palette;
1740 BYTE table[256][4];
1741 unsigned int x, y;
1743 if( pal == NULL) {
1744 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1747 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1749 for (y = 0; y < height; y++)
1751 source = src + pitch * y;
1752 dest = dst + outpitch * y;
1753 /* This is an 1 bpp format, using the width here is fine */
1754 for (x = 0; x < width; x++) {
1755 BYTE color = *source++;
1756 *dest++ = table[color][0];
1757 *dest++ = table[color][1];
1758 *dest++ = table[color][2];
1759 *dest++ = table[color][3];
1763 break;
1765 case CONVERT_CK_565:
1767 /* Converting the 565 format in 5551 packed to emulate color-keying.
1769 Note : in all these conversion, it would be best to average the averaging
1770 pixels to get the color of the pixel that will be color-keyed to
1771 prevent 'color bleeding'. This will be done later on if ever it is
1772 too visible.
1774 Note2: Nvidia documents say that their driver does not support alpha + color keying
1775 on the same surface and disables color keying in such a case
1777 unsigned int x, y;
1778 WORD *Source;
1779 WORD *Dest;
1781 TRACE("Color keyed 565\n");
1783 for (y = 0; y < height; y++) {
1784 Source = (WORD *) (src + y * pitch);
1785 Dest = (WORD *) (dst + y * outpitch);
1786 for (x = 0; x < width; x++ ) {
1787 WORD color = *Source++;
1788 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1789 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1790 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1791 *Dest |= 0x0001;
1793 Dest++;
1797 break;
1799 case CONVERT_CK_5551:
1801 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1802 unsigned int x, y;
1803 WORD *Source;
1804 WORD *Dest;
1805 TRACE("Color keyed 5551\n");
1806 for (y = 0; y < height; y++) {
1807 Source = (WORD *) (src + y * pitch);
1808 Dest = (WORD *) (dst + y * outpitch);
1809 for (x = 0; x < width; x++ ) {
1810 WORD color = *Source++;
1811 *Dest = color;
1812 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1813 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1814 *Dest |= (1 << 15);
1816 else {
1817 *Dest &= ~(1 << 15);
1819 Dest++;
1823 break;
1825 case CONVERT_RGB32_888:
1827 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
1828 unsigned int x, y;
1829 for (y = 0; y < height; y++)
1831 source = src + pitch * y;
1832 dest = dst + outpitch * y;
1833 for (x = 0; x < width; x++) {
1834 DWORD color = 0xffffff & *(DWORD*)source;
1835 DWORD dstcolor = color << 8;
1836 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1837 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1838 dstcolor |= 0xff;
1840 *(DWORD*)dest = dstcolor;
1841 source += 4;
1842 dest += 4;
1846 break;
1848 case CONVERT_V8U8:
1850 unsigned int x, y;
1851 short *Source;
1852 unsigned char *Dest;
1853 for(y = 0; y < height; y++) {
1854 Source = (short *) (src + y * pitch);
1855 Dest = dst + y * outpitch;
1856 for (x = 0; x < width; x++ ) {
1857 long color = (*Source++);
1858 /* B */ Dest[0] = 0xff;
1859 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1860 /* R */ Dest[2] = (color) + 128; /* U */
1861 Dest += 3;
1864 break;
1867 case CONVERT_V16U16:
1869 unsigned int x, y;
1870 DWORD *Source;
1871 unsigned short *Dest;
1872 for(y = 0; y < height; y++) {
1873 Source = (DWORD *) (src + y * pitch);
1874 Dest = (unsigned short *) (dst + y * outpitch);
1875 for (x = 0; x < width; x++ ) {
1876 DWORD color = (*Source++);
1877 /* B */ Dest[0] = 0xffff;
1878 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1879 /* R */ Dest[2] = (color ) + 32768; /* U */
1880 Dest += 3;
1883 break;
1886 case CONVERT_Q8W8V8U8:
1888 unsigned int x, y;
1889 DWORD *Source;
1890 unsigned char *Dest;
1891 for(y = 0; y < height; y++) {
1892 Source = (DWORD *) (src + y * pitch);
1893 Dest = dst + y * outpitch;
1894 for (x = 0; x < width; x++ ) {
1895 long color = (*Source++);
1896 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1897 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1898 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1899 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1900 Dest += 4;
1903 break;
1906 case CONVERT_L6V5U5:
1908 unsigned int x, y;
1909 WORD *Source;
1910 unsigned char *Dest;
1912 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1913 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1914 * fixed function and shaders without further conversion once the surface is
1915 * loaded
1917 for(y = 0; y < height; y++) {
1918 Source = (WORD *) (src + y * pitch);
1919 Dest = dst + y * outpitch;
1920 for (x = 0; x < width; x++ ) {
1921 short color = (*Source++);
1922 unsigned char l = ((color >> 10) & 0xfc);
1923 char v = ((color >> 5) & 0x3e);
1924 char u = ((color ) & 0x1f);
1926 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1927 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1928 * shift. GL reads a signed value and converts it into an unsigned value.
1930 /* M */ Dest[2] = l << 1;
1932 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1933 * from 5 bit values to 8 bit values.
1935 /* V */ Dest[1] = v << 3;
1936 /* U */ Dest[0] = u << 3;
1937 Dest += 3;
1940 } else {
1941 for(y = 0; y < height; y++) {
1942 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1943 Source = (WORD *) (src + y * pitch);
1944 for (x = 0; x < width; x++ ) {
1945 short color = (*Source++);
1946 unsigned char l = ((color >> 10) & 0xfc);
1947 short v = ((color >> 5) & 0x3e);
1948 short u = ((color ) & 0x1f);
1949 short v_conv = v + 16;
1950 short u_conv = u + 16;
1952 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1953 Dest_s += 1;
1957 break;
1960 case CONVERT_X8L8V8U8:
1962 unsigned int x, y;
1963 DWORD *Source;
1964 unsigned char *Dest;
1966 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1967 /* This implementation works with the fixed function pipeline and shaders
1968 * without further modification after converting the surface.
1970 for(y = 0; y < height; y++) {
1971 Source = (DWORD *) (src + y * pitch);
1972 Dest = dst + y * outpitch;
1973 for (x = 0; x < width; x++ ) {
1974 long color = (*Source++);
1975 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1976 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1977 /* U */ Dest[0] = (color & 0xff); /* U */
1978 /* I */ Dest[3] = 255; /* X */
1979 Dest += 4;
1982 } else {
1983 /* Doesn't work correctly with the fixed function pipeline, but can work in
1984 * shaders if the shader is adjusted. (There's no use for this format in gl's
1985 * standard fixed function pipeline anyway).
1987 for(y = 0; y < height; y++) {
1988 Source = (DWORD *) (src + y * pitch);
1989 Dest = dst + y * outpitch;
1990 for (x = 0; x < width; x++ ) {
1991 long color = (*Source++);
1992 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
1993 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1994 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1995 Dest += 4;
1999 break;
2002 case CONVERT_A4L4:
2004 unsigned int x, y;
2005 unsigned char *Source;
2006 unsigned char *Dest;
2007 for(y = 0; y < height; y++) {
2008 Source = src + y * pitch;
2009 Dest = dst + y * outpitch;
2010 for (x = 0; x < width; x++ ) {
2011 unsigned char color = (*Source++);
2012 /* A */ Dest[1] = (color & 0xf0) << 0;
2013 /* L */ Dest[0] = (color & 0x0f) << 4;
2014 Dest += 2;
2017 break;
2020 case CONVERT_R32F:
2022 unsigned int x, y;
2023 float *Source;
2024 float *Dest;
2025 for(y = 0; y < height; y++) {
2026 Source = (float *) (src + y * pitch);
2027 Dest = (float *) (dst + y * outpitch);
2028 for (x = 0; x < width; x++ ) {
2029 float color = (*Source++);
2030 Dest[0] = color;
2031 Dest[1] = 1.0;
2032 Dest[2] = 1.0;
2033 Dest += 3;
2036 break;
2039 case CONVERT_R16F:
2041 unsigned int x, y;
2042 WORD *Source;
2043 WORD *Dest;
2044 WORD one = 0x3c00;
2045 for(y = 0; y < height; y++) {
2046 Source = (WORD *) (src + y * pitch);
2047 Dest = (WORD *) (dst + y * outpitch);
2048 for (x = 0; x < width; x++ ) {
2049 WORD color = (*Source++);
2050 Dest[0] = color;
2051 Dest[1] = one;
2052 Dest[2] = one;
2053 Dest += 3;
2056 break;
2059 case CONVERT_G16R16:
2061 unsigned int x, y;
2062 WORD *Source;
2063 WORD *Dest;
2065 for(y = 0; y < height; y++) {
2066 Source = (WORD *) (src + y * pitch);
2067 Dest = (WORD *) (dst + y * outpitch);
2068 for (x = 0; x < width; x++ ) {
2069 WORD green = (*Source++);
2070 WORD red = (*Source++);
2071 Dest[0] = green;
2072 Dest[1] = red;
2073 Dest[2] = 0xffff;
2074 Dest += 3;
2077 break;
2080 default:
2081 ERR("Unsupported conversation type %d\n", convert);
2083 return WINED3D_OK;
2086 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2087 IWineD3DPaletteImpl* pal = This->palette;
2088 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2089 BOOL index_in_alpha = FALSE;
2090 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2091 int i;
2093 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2094 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2095 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2096 * duplicate entries. Store the color key in the unused alpha component to speed the
2097 * download up and to make conversion unneeded. */
2098 index_in_alpha = primary_render_target_is_p8(device);
2100 if (pal == NULL) {
2101 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2102 if(dxVersion <= 7) {
2103 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2104 if(index_in_alpha) {
2105 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2106 there's no palette at this time. */
2107 for (i = 0; i < 256; i++) table[i][3] = i;
2109 } else {
2110 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2111 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2112 capability flag is present (wine does advertise this capability) */
2113 for (i = 0; i < 256; i++) {
2114 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2115 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2116 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2117 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2120 } else {
2121 TRACE("Using surface palette %p\n", pal);
2122 /* Get the surface's palette */
2123 for (i = 0; i < 256; i++) {
2124 table[i][0] = pal->palents[i].peRed;
2125 table[i][1] = pal->palents[i].peGreen;
2126 table[i][2] = pal->palents[i].peBlue;
2128 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2129 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2130 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2131 of pixels that should be masked away is set to 0. */
2132 if(index_in_alpha) {
2133 table[i][3] = i;
2134 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2135 table[i][3] = 0x00;
2136 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2137 table[i][3] = pal->palents[i].peFlags;
2138 } else {
2139 table[i][3] = 0xFF;
2145 const char *fragment_palette_conversion =
2146 "!!ARBfp1.0\n"
2147 "TEMP index;\n"
2148 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2149 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2150 "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 */
2151 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2152 "END";
2154 /* This function is used in case of 8bit paletted textures to upload the palette.
2155 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2156 extensions like ATI_fragment_shaders is possible.
2158 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2159 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2160 BYTE table[256][4];
2161 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2163 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2165 /* Try to use the paletted texture extension */
2166 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2168 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2169 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2171 else
2173 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2174 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2175 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2177 /* Create the fragment program if we don't have it */
2178 if(!device->paletteConversionShader)
2180 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2181 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2182 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2183 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2184 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2187 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2188 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2190 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2191 glEnable(GL_TEXTURE_1D);
2192 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2194 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2195 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2196 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2197 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2199 /* Switch back to unit 0 in which the 2D texture will be stored. */
2200 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2202 /* Rebind the texture because it isn't bound anymore */
2203 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2207 BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2208 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2210 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2211 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2212 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2214 return FALSE;
2217 if(This->palette9) {
2218 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2219 return FALSE;
2221 } else {
2222 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2224 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2225 return TRUE;
2228 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2229 GLboolean oldwrite[4];
2231 /* Some formats have only some color channels, and the others are 1.0.
2232 * since our rendering renders to all channels, and those pixel formats
2233 * are emulated by using a full texture with the other channels set to 1.0
2234 * manually, clear the unused channels.
2236 * This could be done with hacking colorwriteenable to mask the colors,
2237 * but before drawing the buffer would have to be cleared too, so there's
2238 * no gain in that
2240 switch(This->resource.format) {
2241 case WINED3DFMT_R16F:
2242 case WINED3DFMT_R32F:
2243 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2244 /* Do not activate a context, the correct drawable is active already
2245 * though just the read buffer is set, make sure to have the correct draw
2246 * buffer too
2248 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2249 glDisable(GL_SCISSOR_TEST);
2250 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2251 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2252 glClearColor(0.0, 1.0, 1.0, 1.0);
2253 glClear(GL_COLOR_BUFFER_BIT);
2254 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2255 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2256 checkGLcall("Unused channel clear\n");
2257 break;
2259 default: break;
2263 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2264 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2266 if (!(This->Flags & SFLAG_INTEXTURE)) {
2267 TRACE("Reloading because surface is dirty\n");
2268 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2269 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2270 /* Reload: vice versa OR */
2271 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2272 /* Also reload: Color key is active AND the color key has changed */
2273 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2274 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2275 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2276 TRACE("Reloading because of color keying\n");
2277 /* To perform the color key conversion we need a sysmem copy of
2278 * the surface. Make sure we have it
2281 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2282 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2283 /* TODO: This is not necessarily needed with hw palettized texture support */
2284 This->Flags &= ~SFLAG_INTEXTURE;
2285 } else {
2286 TRACE("surface is already in texture\n");
2287 return WINED3D_OK;
2290 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2291 * These resources are not bound by device size or format restrictions. Because of this,
2292 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2293 * However, these resources can always be created, locked, and copied.
2295 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2297 FIXME("(%p) Operation not supported for scratch textures\n",This);
2298 return WINED3DERR_INVALIDCALL;
2301 This->srgb = srgb_mode;
2302 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2304 #if 0
2306 static unsigned int gen = 0;
2307 char buffer[4096];
2308 ++gen;
2309 if ((gen % 10) == 0) {
2310 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2311 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2314 * debugging crash code
2315 if (gen == 250) {
2316 void** test = NULL;
2317 *test = 0;
2321 #endif
2323 if (!(This->Flags & SFLAG_DONOTFREE)) {
2324 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2325 This->resource.allocatedMemory = NULL;
2326 This->resource.heapMemory = NULL;
2327 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2330 return WINED3D_OK;
2333 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2334 /* TODO: check for locks */
2335 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2336 IWineD3DBaseTexture *baseTexture = NULL;
2337 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2339 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2340 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2341 TRACE("Passing to container\n");
2342 IWineD3DBaseTexture_BindTexture(baseTexture);
2343 IWineD3DBaseTexture_Release(baseTexture);
2344 } else {
2345 TRACE("(%p) : Binding surface\n", This);
2347 if(!device->isInDraw) {
2348 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2350 ENTER_GL();
2351 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2352 LEAVE_GL();
2354 return;
2357 #include <errno.h>
2358 #include <stdio.h>
2359 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2360 FILE* f = NULL;
2361 UINT i, y;
2362 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2363 char *allocatedMemory;
2364 char *textureRow;
2365 IWineD3DSwapChain *swapChain = NULL;
2366 int width, height;
2367 GLuint tmpTexture = 0;
2368 DWORD color;
2369 /*FIXME:
2370 Textures may not be stored in ->allocatedgMemory and a GlTexture
2371 so we should lock the surface before saving a snapshot, or at least check that
2373 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2374 by calling GetTexImage and in compressed form by calling
2375 GetCompressedTexImageARB. Queried compressed images can be saved and
2376 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2377 texture images do not need to be processed by the GL and should
2378 significantly improve texture loading performance relative to uncompressed
2379 images. */
2381 /* Setup the width and height to be the internal texture width and height. */
2382 width = This->pow2Width;
2383 height = This->pow2Height;
2384 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2385 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2387 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2388 /* if were not a real texture then read the back buffer into a real texture */
2389 /* we don't want to interfere with the back buffer so read the data into a temporary
2390 * texture and then save the data out of the temporary texture
2392 GLint prevRead;
2393 ENTER_GL();
2394 TRACE("(%p) Reading render target into texture\n", This);
2395 glEnable(GL_TEXTURE_2D);
2397 glGenTextures(1, &tmpTexture);
2398 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2400 glTexImage2D(GL_TEXTURE_2D,
2402 GL_RGBA,
2403 width,
2404 height,
2405 0/*border*/,
2406 GL_RGBA,
2407 GL_UNSIGNED_INT_8_8_8_8_REV,
2408 NULL);
2410 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2411 vcheckGLcall("glGetIntegerv");
2412 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2413 vcheckGLcall("glReadBuffer");
2414 glCopyTexImage2D(GL_TEXTURE_2D,
2416 GL_RGBA,
2419 width,
2420 height,
2423 checkGLcall("glCopyTexImage2D");
2424 glReadBuffer(prevRead);
2425 LEAVE_GL();
2427 } else { /* bind the real texture, and make sure it up to date */
2428 IWineD3DSurface_PreLoad(iface);
2430 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2431 ENTER_GL();
2432 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2433 glGetTexImage(GL_TEXTURE_2D,
2434 This->glDescription.level,
2435 GL_RGBA,
2436 GL_UNSIGNED_INT_8_8_8_8_REV,
2437 allocatedMemory);
2438 checkGLcall("glTexImage2D");
2439 if (tmpTexture) {
2440 glBindTexture(GL_TEXTURE_2D, 0);
2441 glDeleteTextures(1, &tmpTexture);
2443 LEAVE_GL();
2445 f = fopen(filename, "w+");
2446 if (NULL == f) {
2447 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2448 return WINED3DERR_INVALIDCALL;
2450 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2451 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2452 /* TGA header */
2453 fputc(0,f);
2454 fputc(0,f);
2455 fputc(2,f);
2456 fputc(0,f);
2457 fputc(0,f);
2458 fputc(0,f);
2459 fputc(0,f);
2460 fputc(0,f);
2461 fputc(0,f);
2462 fputc(0,f);
2463 fputc(0,f);
2464 fputc(0,f);
2465 /* short width*/
2466 fwrite(&width,2,1,f);
2467 /* short height */
2468 fwrite(&height,2,1,f);
2469 /* format rgba */
2470 fputc(0x20,f);
2471 fputc(0x28,f);
2472 /* raw data */
2473 /* 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 */
2474 if(swapChain)
2475 textureRow = allocatedMemory + (width * (height - 1) *4);
2476 else
2477 textureRow = allocatedMemory;
2478 for (y = 0 ; y < height; y++) {
2479 for (i = 0; i < width; i++) {
2480 color = *((DWORD*)textureRow);
2481 fputc((color >> 16) & 0xFF, f); /* B */
2482 fputc((color >> 8) & 0xFF, f); /* G */
2483 fputc((color >> 0) & 0xFF, f); /* R */
2484 fputc((color >> 24) & 0xFF, f); /* A */
2485 textureRow += 4;
2487 /* take two rows of the pointer to the texture memory */
2488 if(swapChain)
2489 (textureRow-= width << 3);
2492 TRACE("Closing file\n");
2493 fclose(f);
2495 if(swapChain) {
2496 IWineD3DSwapChain_Release(swapChain);
2498 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2499 return WINED3D_OK;
2503 * Slightly inefficient way to handle multiple dirty rects but it works :)
2505 HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2506 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2507 IWineD3DBaseTexture *baseTexture = NULL;
2509 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2510 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2512 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2513 if (NULL != pDirtyRect) {
2514 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2515 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2516 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2517 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2518 } else {
2519 This->dirtyRect.left = 0;
2520 This->dirtyRect.top = 0;
2521 This->dirtyRect.right = This->currentDesc.Width;
2522 This->dirtyRect.bottom = This->currentDesc.Height;
2524 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2525 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2526 /* if the container is a basetexture then mark it dirty. */
2527 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2528 TRACE("Passing to container\n");
2529 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2530 IWineD3DBaseTexture_Release(baseTexture);
2532 return WINED3D_OK;
2535 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2536 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2537 HRESULT hr;
2538 const GlPixelFormatDesc *glDesc;
2539 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2541 TRACE("(%p) : Calling base function first\n", This);
2542 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2543 if(SUCCEEDED(hr)) {
2544 /* Setup some glformat defaults */
2545 This->glDescription.glFormat = glDesc->glFormat;
2546 This->glDescription.glFormatInternal = glDesc->glInternal;
2547 This->glDescription.glType = glDesc->glType;
2549 This->Flags &= ~SFLAG_ALLOCATED;
2550 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2551 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2553 return hr;
2556 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2557 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2559 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2560 WARN("Surface is locked or the HDC is in use\n");
2561 return WINED3DERR_INVALIDCALL;
2564 if(Mem && Mem != This->resource.allocatedMemory) {
2565 void *release = NULL;
2567 /* Do I have to copy the old surface content? */
2568 if(This->Flags & SFLAG_DIBSECTION) {
2569 /* Release the DC. No need to hold the critical section for the update
2570 * Thread because this thread runs only on front buffers, but this method
2571 * fails for render targets in the check above.
2573 SelectObject(This->hDC, This->dib.holdbitmap);
2574 DeleteDC(This->hDC);
2575 /* Release the DIB section */
2576 DeleteObject(This->dib.DIBsection);
2577 This->dib.bitmap_data = NULL;
2578 This->resource.allocatedMemory = NULL;
2579 This->hDC = NULL;
2580 This->Flags &= ~SFLAG_DIBSECTION;
2581 } else if(!(This->Flags & SFLAG_USERPTR)) {
2582 release = This->resource.heapMemory;
2583 This->resource.heapMemory = NULL;
2585 This->resource.allocatedMemory = Mem;
2586 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2588 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2589 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2591 /* For client textures opengl has to be notified */
2592 if(This->Flags & SFLAG_CLIENT) {
2593 This->Flags &= ~SFLAG_ALLOCATED;
2594 IWineD3DSurface_PreLoad(iface);
2595 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2598 /* Now free the old memory if any */
2599 HeapFree(GetProcessHeap(), 0, release);
2600 } else if(This->Flags & SFLAG_USERPTR) {
2601 /* LockRect and GetDC will re-create the dib section and allocated memory */
2602 This->resource.allocatedMemory = NULL;
2603 /* HeapMemory should be NULL already */
2604 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2605 This->Flags &= ~SFLAG_USERPTR;
2607 if(This->Flags & SFLAG_CLIENT) {
2608 This->Flags &= ~SFLAG_ALLOCATED;
2609 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2610 IWineD3DSurface_PreLoad(iface);
2613 return WINED3D_OK;
2616 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2617 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2618 IWineD3DSwapChainImpl *swapchain = NULL;
2619 HRESULT hr;
2620 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2622 /* Flipping is only supported on RenderTargets */
2623 if( !(This->resource.usage & WINED3DUSAGE_RENDERTARGET) ) return WINEDDERR_NOTFLIPPABLE;
2625 if(override) {
2626 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2627 * FIXME("(%p) Target override is not supported by now\n", This);
2628 * Additionally, it isn't really possible to support triple-buffering
2629 * properly on opengl at all
2633 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2634 if(!swapchain) {
2635 ERR("Flipped surface is not on a swapchain\n");
2636 return WINEDDERR_NOTFLIPPABLE;
2639 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2640 * and only d3d8 and d3d9 apps specify the presentation interval
2642 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2643 /* Most common case first to avoid wasting time on all the other cases */
2644 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2645 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2646 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2647 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2648 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2649 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2650 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2651 } else {
2652 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2655 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2656 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2657 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2658 return hr;
2661 /* Does a direct frame buffer -> texture copy. Stretching is done
2662 * with single pixel copy calls
2664 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2665 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2666 float xrel, yrel;
2667 UINT row;
2668 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2671 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2672 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2673 ENTER_GL();
2675 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2676 glEnable(This->glDescription.target);
2677 checkGLcall("glEnable(This->glDescription.target)");
2679 /* Bind the target texture */
2680 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2681 checkGLcall("glBindTexture");
2682 if(!swapchain) {
2683 glReadBuffer(myDevice->offscreenBuffer);
2684 } else {
2685 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2686 glReadBuffer(buffer);
2688 checkGLcall("glReadBuffer");
2690 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2691 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2693 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2694 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2696 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2697 ERR("Texture filtering not supported in direct blit\n");
2699 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2700 ERR("Texture filtering not supported in direct blit\n");
2703 if(upsidedown &&
2704 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2705 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2706 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2708 glCopyTexSubImage2D(This->glDescription.target,
2709 This->glDescription.level,
2710 drect->x1, drect->y1, /* xoffset, yoffset */
2711 srect->x1, Src->currentDesc.Height - srect->y2,
2712 drect->x2 - drect->x1, drect->y2 - drect->y1);
2713 } else {
2714 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2715 /* I have to process this row by row to swap the image,
2716 * otherwise it would be upside down, so stretching in y direction
2717 * doesn't cost extra time
2719 * However, stretching in x direction can be avoided if not necessary
2721 for(row = drect->y1; row < drect->y2; row++) {
2722 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2723 /* Well, that stuff works, but it's very slow.
2724 * find a better way instead
2726 UINT col;
2728 for(col = drect->x1; col < drect->x2; col++) {
2729 glCopyTexSubImage2D(This->glDescription.target,
2730 This->glDescription.level,
2731 drect->x1 + col, row, /* xoffset, yoffset */
2732 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2733 1, 1);
2735 } else {
2736 glCopyTexSubImage2D(This->glDescription.target,
2737 This->glDescription.level,
2738 drect->x1, row, /* xoffset, yoffset */
2739 srect->x1, yoffset - (int) (row * yrel),
2740 drect->x2-drect->x1, 1);
2744 vcheckGLcall("glCopyTexSubImage2D");
2746 /* Leave the opengl state valid for blitting */
2747 glDisable(This->glDescription.target);
2748 checkGLcall("glDisable(This->glDescription.target)");
2750 LEAVE_GL();
2753 /* Uses the hardware to stretch and flip the image */
2754 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2755 GLuint src, backup = 0;
2756 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2757 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2758 float left, right, top, bottom; /* Texture coordinates */
2759 UINT fbwidth = Src->currentDesc.Width;
2760 UINT fbheight = Src->currentDesc.Height;
2761 GLenum drawBuffer = GL_BACK;
2762 GLenum texture_target;
2763 BOOL noBackBufferBackup;
2765 TRACE("Using hwstretch blit\n");
2766 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2767 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2768 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2770 noBackBufferBackup = !swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2771 if(!noBackBufferBackup && Src->glDescription.textureName == 0) {
2772 /* Get it a description */
2773 IWineD3DSurface_PreLoad(SrcSurface);
2775 ENTER_GL();
2777 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2778 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2780 if(myDevice->activeContext->aux_buffers >= 2) {
2781 /* Got more than one aux buffer? Use the 2nd aux buffer */
2782 drawBuffer = GL_AUX1;
2783 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && myDevice->activeContext->aux_buffers >= 1) {
2784 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2785 drawBuffer = GL_AUX0;
2788 if(noBackBufferBackup) {
2789 glGenTextures(1, &backup);
2790 checkGLcall("glGenTextures\n");
2791 glBindTexture(GL_TEXTURE_2D, backup);
2792 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2793 texture_target = GL_TEXTURE_2D;
2794 } else {
2795 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2796 * we are reading from the back buffer, the backup can be used as source texture
2798 texture_target = Src->glDescription.target;
2799 glBindTexture(texture_target, Src->glDescription.textureName);
2800 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2801 glEnable(texture_target);
2802 checkGLcall("glEnable(texture_target)");
2804 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2805 Src->Flags &= ~SFLAG_INTEXTURE;
2808 if(swapchain) {
2809 glReadBuffer(surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain));
2810 } else {
2811 glReadBuffer(myDevice->offscreenBuffer);
2814 /* TODO: Only back up the part that will be overwritten */
2815 glCopyTexSubImage2D(texture_target, 0,
2816 0, 0 /* read offsets */,
2817 0, 0,
2818 fbwidth,
2819 fbheight);
2821 checkGLcall("glCopyTexSubImage2D");
2823 /* No issue with overriding these - the sampler is dirty due to blit usage */
2824 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2825 magLookup[Filter - WINED3DTEXF_NONE]);
2826 checkGLcall("glTexParameteri");
2827 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2828 minMipLookup[Filter][WINED3DTEXF_NONE]);
2829 checkGLcall("glTexParameteri");
2831 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2832 src = backup ? backup : Src->glDescription.textureName;
2833 } else {
2834 glReadBuffer(GL_FRONT);
2835 checkGLcall("glReadBuffer(GL_FRONT)");
2837 glGenTextures(1, &src);
2838 checkGLcall("glGenTextures(1, &src)");
2839 glBindTexture(GL_TEXTURE_2D, src);
2840 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2842 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2843 * out for power of 2 sizes
2845 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2846 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2847 checkGLcall("glTexImage2D");
2848 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2849 0, 0 /* read offsets */,
2850 0, 0,
2851 fbwidth,
2852 fbheight);
2854 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2855 checkGLcall("glTexParameteri");
2856 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2857 checkGLcall("glTexParameteri");
2859 glReadBuffer(GL_BACK);
2860 checkGLcall("glReadBuffer(GL_BACK)");
2862 if(texture_target != GL_TEXTURE_2D) {
2863 glDisable(texture_target);
2864 glEnable(GL_TEXTURE_2D);
2865 texture_target = GL_TEXTURE_2D;
2868 checkGLcall("glEnd and previous");
2870 left = srect->x1;
2871 right = srect->x2;
2873 if(upsidedown) {
2874 top = Src->currentDesc.Height - srect->y1;
2875 bottom = Src->currentDesc.Height - srect->y2;
2876 } else {
2877 top = Src->currentDesc.Height - srect->y2;
2878 bottom = Src->currentDesc.Height - srect->y1;
2881 if(Src->Flags & SFLAG_NORMCOORD) {
2882 left /= Src->pow2Width;
2883 right /= Src->pow2Width;
2884 top /= Src->pow2Height;
2885 bottom /= Src->pow2Height;
2888 /* draw the source texture stretched and upside down. The correct surface is bound already */
2889 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
2890 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
2892 glDrawBuffer(drawBuffer);
2893 glReadBuffer(drawBuffer);
2895 glBegin(GL_QUADS);
2896 /* bottom left */
2897 glTexCoord2f(left, bottom);
2898 glVertex2i(0, fbheight);
2900 /* top left */
2901 glTexCoord2f(left, top);
2902 glVertex2i(0, fbheight - drect->y2 - drect->y1);
2904 /* top right */
2905 glTexCoord2f(right, top);
2906 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
2908 /* bottom right */
2909 glTexCoord2f(right, bottom);
2910 glVertex2i(drect->x2 - drect->x1, fbheight);
2911 glEnd();
2912 checkGLcall("glEnd and previous");
2914 if(texture_target != This->glDescription.target) {
2915 glDisable(texture_target);
2916 glEnable(This->glDescription.target);
2917 texture_target = This->glDescription.target;
2920 /* Now read the stretched and upside down image into the destination texture */
2921 glBindTexture(texture_target, This->glDescription.textureName);
2922 checkGLcall("glBindTexture");
2923 glCopyTexSubImage2D(texture_target,
2925 drect->x1, drect->y1, /* xoffset, yoffset */
2926 0, 0, /* We blitted the image to the origin */
2927 drect->x2 - drect->x1, drect->y2 - drect->y1);
2928 checkGLcall("glCopyTexSubImage2D");
2930 if(drawBuffer == GL_BACK) {
2931 /* Write the back buffer backup back */
2932 if(backup) {
2933 if(texture_target != GL_TEXTURE_2D) {
2934 glDisable(texture_target);
2935 glEnable(GL_TEXTURE_2D);
2936 texture_target = GL_TEXTURE_2D;
2938 glBindTexture(GL_TEXTURE_2D, backup);
2939 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
2940 } else {
2941 if(texture_target != Src->glDescription.target) {
2942 glDisable(texture_target);
2943 glEnable(Src->glDescription.target);
2944 texture_target = Src->glDescription.target;
2946 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
2947 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2950 glBegin(GL_QUADS);
2951 /* top left */
2952 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
2953 glVertex2i(0, 0);
2955 /* bottom left */
2956 glTexCoord2f(0.0, 0.0);
2957 glVertex2i(0, fbheight);
2959 /* bottom right */
2960 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
2961 glVertex2i(fbwidth, Src->currentDesc.Height);
2963 /* top right */
2964 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
2965 glVertex2i(fbwidth, 0);
2966 glEnd();
2967 } else {
2968 /* Restore the old draw buffer */
2969 glDrawBuffer(GL_BACK);
2971 glDisable(texture_target);
2972 checkGLcall("glDisable(texture_target)");
2974 /* Cleanup */
2975 if(src != Src->glDescription.textureName && src != backup) {
2976 glDeleteTextures(1, &src);
2977 checkGLcall("glDeleteTextures(1, &src)");
2979 if(backup) {
2980 glDeleteTextures(1, &backup);
2981 checkGLcall("glDeleteTextures(1, &backup)");
2984 LEAVE_GL();
2987 /* Not called from the VTable */
2988 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
2989 WINED3DRECT rect;
2990 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2991 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
2992 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2994 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
2996 /* Get the swapchain. One of the surfaces has to be a primary surface */
2997 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
2998 WARN("Destination is in sysmem, rejecting gl blt\n");
2999 return WINED3DERR_INVALIDCALL;
3001 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3002 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
3003 if(Src) {
3004 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3005 WARN("Src is in sysmem, rejecting gl blt\n");
3006 return WINED3DERR_INVALIDCALL;
3008 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3009 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3012 /* Early sort out of cases where no render target is used */
3013 if(!dstSwapchain && !srcSwapchain &&
3014 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3015 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3016 return WINED3DERR_INVALIDCALL;
3019 /* No destination color keying supported */
3020 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3021 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3022 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3023 return WINED3DERR_INVALIDCALL;
3026 if (DestRect) {
3027 rect.x1 = DestRect->left;
3028 rect.y1 = DestRect->top;
3029 rect.x2 = DestRect->right;
3030 rect.y2 = DestRect->bottom;
3031 } else {
3032 rect.x1 = 0;
3033 rect.y1 = 0;
3034 rect.x2 = This->currentDesc.Width;
3035 rect.y2 = This->currentDesc.Height;
3038 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3039 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3040 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3041 /* Half-life does a Blt from the back buffer to the front buffer,
3042 * Full surface size, no flags... Use present instead
3044 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3047 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3048 while(1)
3050 RECT mySrcRect;
3051 TRACE("Looking if a Present can be done...\n");
3052 /* Source Rectangle must be full surface */
3053 if( SrcRect ) {
3054 if(SrcRect->left != 0 || SrcRect->top != 0 ||
3055 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
3056 TRACE("No, Source rectangle doesn't match\n");
3057 break;
3060 mySrcRect.left = 0;
3061 mySrcRect.top = 0;
3062 mySrcRect.right = Src->currentDesc.Width;
3063 mySrcRect.bottom = Src->currentDesc.Height;
3065 /* No stretching may occur */
3066 if(mySrcRect.right != rect.x2 - rect.x1 ||
3067 mySrcRect.bottom != rect.y2 - rect.y1) {
3068 TRACE("No, stretching is done\n");
3069 break;
3072 /* Destination must be full surface or match the clipping rectangle */
3073 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3075 RECT cliprect;
3076 POINT pos[2];
3077 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3078 pos[0].x = rect.x1;
3079 pos[0].y = rect.y1;
3080 pos[1].x = rect.x2;
3081 pos[1].y = rect.y2;
3082 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3083 pos, 2);
3085 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3086 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3088 TRACE("No, dest rectangle doesn't match(clipper)\n");
3089 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3090 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3091 break;
3094 else
3096 if(rect.x1 != 0 || rect.y1 != 0 ||
3097 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3098 TRACE("No, dest rectangle doesn't match(surface size)\n");
3099 break;
3103 TRACE("Yes\n");
3105 /* These flags are unimportant for the flag check, remove them */
3106 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3107 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3109 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3110 * take very long, while a flip is fast.
3111 * This applies to Half-Life, which does such Blts every time it finished
3112 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3113 * menu. This is also used by all apps when they do windowed rendering
3115 * The problem is that flipping is not really the same as copying. After a
3116 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3117 * untouched. Therefore it's necessary to override the swap effect
3118 * and to set it back after the flip.
3120 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3121 * testcases.
3124 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3125 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3127 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3128 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3130 dstSwapchain->presentParms.SwapEffect = orig_swap;
3132 return WINED3D_OK;
3134 break;
3137 TRACE("Unsupported blit between buffers on the same swapchain\n");
3138 return WINED3DERR_INVALIDCALL;
3139 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3140 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3141 return WINED3DERR_INVALIDCALL;
3142 } else if(dstSwapchain && srcSwapchain) {
3143 FIXME("Implement hardware blit between two different swapchains\n");
3144 return WINED3DERR_INVALIDCALL;
3145 } else if(dstSwapchain) {
3146 if(SrcSurface == myDevice->render_targets[0]) {
3147 TRACE("Blit from active render target to a swapchain\n");
3148 /* Handled with regular texture -> swapchain blit */
3150 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3151 FIXME("Implement blit from a swapchain to the active render target\n");
3152 return WINED3DERR_INVALIDCALL;
3155 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3156 /* Blit from render target to texture */
3157 WINED3DRECT srect;
3158 BOOL upsideDown, stretchx;
3159 BOOL paletteOverride = FALSE;
3161 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3162 TRACE("Color keying not supported by frame buffer to texture blit\n");
3163 return WINED3DERR_INVALIDCALL;
3164 /* Destination color key is checked above */
3167 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3168 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3170 if(SrcRect) {
3171 if(SrcRect->top < SrcRect->bottom) {
3172 srect.y1 = SrcRect->top;
3173 srect.y2 = SrcRect->bottom;
3174 upsideDown = FALSE;
3175 } else {
3176 srect.y1 = SrcRect->bottom;
3177 srect.y2 = SrcRect->top;
3178 upsideDown = TRUE;
3180 srect.x1 = SrcRect->left;
3181 srect.x2 = SrcRect->right;
3182 } else {
3183 srect.x1 = 0;
3184 srect.y1 = 0;
3185 srect.x2 = Src->currentDesc.Width;
3186 srect.y2 = Src->currentDesc.Height;
3187 upsideDown = FALSE;
3189 if(rect.x1 > rect.x2) {
3190 UINT tmp = rect.x2;
3191 rect.x2 = rect.x1;
3192 rect.x1 = tmp;
3193 upsideDown = !upsideDown;
3195 if(!srcSwapchain) {
3196 TRACE("Reading from an offscreen target\n");
3197 upsideDown = !upsideDown;
3200 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3201 stretchx = TRUE;
3202 } else {
3203 stretchx = FALSE;
3206 /* When blitting from a render target a texture, the texture isn't required to have a palette.
3207 * In this case grab the palette from the render target. */
3208 if((This->resource.format == WINED3DFMT_P8) && (This->palette == NULL)) {
3209 paletteOverride = TRUE;
3210 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3211 This->palette = Src->palette;
3214 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3215 * flip the image nor scale it.
3217 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3218 * -> If the app wants a image width an unscaled width, copy it line per line
3219 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3220 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3221 * back buffer. This is slower than reading line per line, thus not used for flipping
3222 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3223 * pixel by pixel
3225 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3226 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3227 * backends.
3229 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3230 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3231 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3232 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3233 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3234 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3235 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3236 } else {
3237 TRACE("Using hardware stretching to flip / stretch the texture\n");
3238 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3241 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3242 if(paletteOverride)
3243 This->palette = NULL;
3245 if(!(This->Flags & SFLAG_DONOTFREE)) {
3246 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3247 This->resource.allocatedMemory = NULL;
3248 This->resource.heapMemory = NULL;
3249 } else {
3250 This->Flags &= ~SFLAG_INSYSMEM;
3252 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3253 * path is never entered
3255 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3257 return WINED3D_OK;
3258 } else if(Src) {
3259 /* Blit from offscreen surface to render target */
3260 float glTexCoord[4];
3261 DWORD oldCKeyFlags = Src->CKeyFlags;
3262 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3263 RECT SourceRectangle;
3264 BOOL paletteOverride = FALSE;
3266 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3268 if(SrcRect) {
3269 SourceRectangle.left = SrcRect->left;
3270 SourceRectangle.right = SrcRect->right;
3271 SourceRectangle.top = SrcRect->top;
3272 SourceRectangle.bottom = SrcRect->bottom;
3273 } else {
3274 SourceRectangle.left = 0;
3275 SourceRectangle.right = Src->currentDesc.Width;
3276 SourceRectangle.top = 0;
3277 SourceRectangle.bottom = Src->currentDesc.Height;
3279 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3280 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3281 TRACE("Using stretch_rect_fbo\n");
3282 /* The source is always a texture, but never the currently active render target, and the texture
3283 * contents are never upside down
3285 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3286 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3287 return WINED3D_OK;
3290 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3291 /* Fall back to software */
3292 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3293 SourceRectangle.left, SourceRectangle.top,
3294 SourceRectangle.right, SourceRectangle.bottom);
3295 return WINED3DERR_INVALIDCALL;
3298 /* Color keying: Check if we have to do a color keyed blt,
3299 * and if not check if a color key is activated.
3301 * Just modify the color keying parameters in the surface and restore them afterwards
3302 * The surface keeps track of the color key last used to load the opengl surface.
3303 * PreLoad will catch the change to the flags and color key and reload if necessary.
3305 if(Flags & WINEDDBLT_KEYSRC) {
3306 /* Use color key from surface */
3307 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3308 /* Use color key from DDBltFx */
3309 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3310 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3311 } else {
3312 /* Do not use color key */
3313 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3316 /* When blitting from an offscreen surface to a rendertarget, the source
3317 * surface is not required to have a palette. Our rendering / conversion
3318 * code further down the road retrieves the palette from the surface, so
3319 * it must have a palette set. */
3320 if((Src->resource.format == WINED3DFMT_P8) && (Src->palette == NULL)) {
3321 paletteOverride = TRUE;
3322 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3323 Src->palette = This->palette;
3326 /* Now load the surface */
3327 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3330 /* Activate the destination context, set it up for blitting */
3331 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3332 ENTER_GL();
3334 glEnable(Src->glDescription.target);
3335 checkGLcall("glEnable(Src->glDescription.target)");
3337 if(!dstSwapchain) {
3338 TRACE("Drawing to offscreen buffer\n");
3339 glDrawBuffer(myDevice->offscreenBuffer);
3340 checkGLcall("glDrawBuffer");
3341 } else {
3342 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *)This, (IWineD3DSwapChain *)dstSwapchain);
3343 TRACE("Drawing to %#x buffer\n", buffer);
3344 glDrawBuffer(buffer);
3345 checkGLcall("glDrawBuffer");
3348 /* Bind the texture */
3349 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3350 checkGLcall("glBindTexture");
3352 /* Filtering for StretchRect */
3353 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3354 magLookup[Filter - WINED3DTEXF_NONE]);
3355 checkGLcall("glTexParameteri");
3356 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3357 minMipLookup[Filter][WINED3DTEXF_NONE]);
3358 checkGLcall("glTexParameteri");
3359 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3360 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3361 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3362 checkGLcall("glTexEnvi");
3364 /* This is for color keying */
3365 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3366 glEnable(GL_ALPHA_TEST);
3367 checkGLcall("glEnable GL_ALPHA_TEST");
3369 /* When the primary render target uses P8, the alpha component contains the palette index.
3370 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3371 * should be masked away have alpha set to 0. */
3372 if(primary_render_target_is_p8(myDevice))
3373 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3374 else
3375 glAlphaFunc(GL_NOTEQUAL, 0.0);
3376 checkGLcall("glAlphaFunc\n");
3377 } else {
3378 glDisable(GL_ALPHA_TEST);
3379 checkGLcall("glDisable GL_ALPHA_TEST");
3382 /* Draw a textured quad
3384 glBegin(GL_QUADS);
3386 glColor3d(1.0f, 1.0f, 1.0f);
3387 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3388 glVertex3f(rect.x1,
3389 rect.y1,
3390 0.0);
3392 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3393 glVertex3f(rect.x1, rect.y2, 0.0);
3395 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3396 glVertex3f(rect.x2,
3397 rect.y2,
3398 0.0);
3400 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3401 glVertex3f(rect.x2,
3402 rect.y1,
3403 0.0);
3404 glEnd();
3405 checkGLcall("glEnd");
3407 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3408 glDisable(GL_ALPHA_TEST);
3409 checkGLcall("glDisable(GL_ALPHA_TEST)");
3412 /* Flush in case the drawable is used by multiple GL contexts */
3413 if(dstSwapchain && (This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer || dstSwapchain->num_contexts >= 2))
3414 glFlush();
3416 glBindTexture(Src->glDescription.target, 0);
3417 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3418 /* Leave the opengl state valid for blitting */
3419 glDisable(Src->glDescription.target);
3420 checkGLcall("glDisable(Src->glDescription.target)");
3422 /* The draw buffer should only need to be restored if we were drawing to the front buffer, and there is a back buffer.
3423 * otherwise the context manager should choose between GL_BACK / offscreenDrawBuffer
3425 if(dstSwapchain && This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer && dstSwapchain->backBuffer) {
3426 glDrawBuffer(GL_BACK);
3427 checkGLcall("glDrawBuffer");
3429 /* Restore the color key parameters */
3430 Src->CKeyFlags = oldCKeyFlags;
3431 Src->SrcBltCKey = oldBltCKey;
3433 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3434 if(paletteOverride)
3435 Src->palette = NULL;
3437 LEAVE_GL();
3439 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3440 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3441 * is outdated now
3443 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3444 /* TODO: This should be moved to ModifyLocation() */
3445 if(!(dstSwapchain || wined3d_settings.offscreen_rendering_mode != ORM_FBO)) {
3446 This->Flags |= SFLAG_INTEXTURE;
3449 return WINED3D_OK;
3450 } else {
3451 /* Source-Less Blit to render target */
3452 if (Flags & WINEDDBLT_COLORFILL) {
3453 /* This is easy to handle for the D3D Device... */
3454 DWORD color;
3456 TRACE("Colorfill\n");
3458 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3459 must be true if we are here */
3460 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3461 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3462 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3463 TRACE("Surface is higher back buffer, falling back to software\n");
3464 return WINED3DERR_INVALIDCALL;
3467 /* The color as given in the Blt function is in the format of the frame-buffer...
3468 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3470 if (This->resource.format == WINED3DFMT_P8) {
3471 DWORD alpha;
3473 if (primary_render_target_is_p8(myDevice)) alpha = DDBltFx->u5.dwFillColor << 24;
3474 else alpha = 0xFF000000;
3476 if (This->palette) {
3477 color = (alpha |
3478 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3479 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3480 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3481 } else {
3482 color = alpha;
3485 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3486 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3487 color = 0xFFFFFFFF;
3488 } else {
3489 color = ((0xFF000000) |
3490 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3491 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3492 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3495 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3496 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3497 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3499 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3500 color = DDBltFx->u5.dwFillColor;
3502 else {
3503 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3504 return WINED3DERR_INVALIDCALL;
3507 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3508 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3509 1, /* Number of rectangles */
3510 &rect, WINED3DCLEAR_TARGET, color,
3511 0.0 /* Z */,
3512 0 /* Stencil */);
3513 return WINED3D_OK;
3517 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3518 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3519 return WINED3DERR_INVALIDCALL;
3522 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3524 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3525 float depth;
3527 if (Flags & WINEDDBLT_DEPTHFILL) {
3528 switch(This->resource.format) {
3529 case WINED3DFMT_D16:
3530 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3531 break;
3532 case WINED3DFMT_D15S1:
3533 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3534 break;
3535 case WINED3DFMT_D24S8:
3536 case WINED3DFMT_D24X8:
3537 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3538 break;
3539 case WINED3DFMT_D32:
3540 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3541 break;
3542 default:
3543 depth = 0.0;
3544 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3547 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3548 DestRect == NULL ? 0 : 1,
3549 (WINED3DRECT *) DestRect,
3550 WINED3DCLEAR_ZBUFFER,
3551 0x00000000,
3552 depth,
3553 0x00000000);
3556 FIXME("(%p): Unsupp depthstencil blit\n", This);
3557 return WINED3DERR_INVALIDCALL;
3560 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3561 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3562 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3563 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3564 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3565 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3567 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3569 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3570 return WINEDDERR_SURFACEBUSY;
3573 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3574 * except depth blits, which seem to work
3576 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3577 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3578 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3579 return WINED3DERR_INVALIDCALL;
3580 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3581 TRACE("Z Blit override handled the blit\n");
3582 return WINED3D_OK;
3586 /* Special cases for RenderTargets */
3587 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3588 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3589 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3592 /* For the rest call the X11 surface implementation.
3593 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3594 * other Blts are rather rare
3596 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3599 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3600 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3601 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3602 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3603 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3605 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3607 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3608 return WINEDDERR_SURFACEBUSY;
3611 if(myDevice->inScene &&
3612 (iface == myDevice->stencilBufferTarget ||
3613 (Source && Source == myDevice->stencilBufferTarget))) {
3614 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3615 return WINED3DERR_INVALIDCALL;
3618 /* Special cases for RenderTargets */
3619 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3620 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3622 RECT SrcRect, DstRect;
3623 DWORD Flags=0;
3625 if(rsrc) {
3626 SrcRect.left = rsrc->left;
3627 SrcRect.top= rsrc->top;
3628 SrcRect.bottom = rsrc->bottom;
3629 SrcRect.right = rsrc->right;
3630 } else {
3631 SrcRect.left = 0;
3632 SrcRect.top = 0;
3633 SrcRect.right = srcImpl->currentDesc.Width;
3634 SrcRect.bottom = srcImpl->currentDesc.Height;
3637 DstRect.left = dstx;
3638 DstRect.top=dsty;
3639 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3640 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3642 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3643 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3644 Flags |= WINEDDBLT_KEYSRC;
3645 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3646 Flags |= WINEDDBLT_KEYDEST;
3647 if(trans & WINEDDBLTFAST_WAIT)
3648 Flags |= WINEDDBLT_WAIT;
3649 if(trans & WINEDDBLTFAST_DONOTWAIT)
3650 Flags |= WINEDDBLT_DONOTWAIT;
3652 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3656 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3659 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3660 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3661 RGBQUAD col[256];
3662 IWineD3DPaletteImpl *pal = This->palette;
3663 unsigned int n;
3664 TRACE("(%p)\n", This);
3666 if (!pal) return WINED3D_OK;
3668 if(This->resource.format == WINED3DFMT_P8 ||
3669 This->resource.format == WINED3DFMT_A8P8)
3671 int bpp;
3672 GLenum format, internal, type;
3673 CONVERT_TYPES convert;
3675 /* Check if we are using a RTL mode which uses texturing for uploads */
3676 BOOL use_texture = (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX);
3678 /* Check if we have hardware palette conversion if we have convert is set to NO_CONVERSION */
3679 d3dfmt_get_conv(This, TRUE, use_texture, &format, &internal, &type, &convert, &bpp, This->srgb);
3681 if((This->resource.usage & WINED3DUSAGE_RENDERTARGET) && (convert == NO_CONVERSION))
3683 ENTER_GL();
3684 if (This->glDescription.textureName == 0) {
3685 glGenTextures(1, &This->glDescription.textureName);
3686 checkGLcall("glGenTextures");
3688 glBindTexture(This->glDescription.target, This->glDescription.textureName);
3689 checkGLcall("glBindTexture(This->glDescription.target, This->glDescription.textureName)");
3690 LEAVE_GL();
3692 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3693 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3695 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3696 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
3698 /* Re-upload the palette */
3699 d3dfmt_p8_upload_palette(iface, convert);
3701 /* Without this some palette updates are missed. This at least happens on Nvidia drivers but
3702 * it works fine using Mesa. */
3703 ENTER_GL();
3704 glFlush();
3705 LEAVE_GL();
3706 } else {
3707 if(!(This->Flags & SFLAG_INSYSMEM)) {
3708 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3709 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3711 TRACE("Dirtifying surface\n");
3712 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3716 if(This->Flags & SFLAG_DIBSECTION) {
3717 TRACE("(%p): Updating the hdc's palette\n", This);
3718 for (n=0; n<256; n++) {
3719 col[n].rgbRed = pal->palents[n].peRed;
3720 col[n].rgbGreen = pal->palents[n].peGreen;
3721 col[n].rgbBlue = pal->palents[n].peBlue;
3722 col[n].rgbReserved = 0;
3724 SetDIBColorTable(This->hDC, 0, 256, col);
3727 /* Propagate the changes to the drawable when we have a palette. */
3728 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3729 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3731 return WINED3D_OK;
3734 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3735 /** Check against the maximum texture sizes supported by the video card **/
3736 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3737 unsigned int pow2Width, pow2Height;
3738 const GlPixelFormatDesc *glDesc;
3740 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3741 /* Setup some glformat defaults */
3742 This->glDescription.glFormat = glDesc->glFormat;
3743 This->glDescription.glFormatInternal = glDesc->glInternal;
3744 This->glDescription.glType = glDesc->glType;
3746 This->glDescription.textureName = 0;
3747 This->glDescription.target = GL_TEXTURE_2D;
3749 /* Non-power2 support */
3750 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO)) {
3751 pow2Width = This->currentDesc.Width;
3752 pow2Height = This->currentDesc.Height;
3753 } else {
3754 /* Find the nearest pow2 match */
3755 pow2Width = pow2Height = 1;
3756 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3757 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3759 This->pow2Width = pow2Width;
3760 This->pow2Height = pow2Height;
3762 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3763 WINED3DFORMAT Format = This->resource.format;
3764 /** TODO: add support for non power two compressed textures **/
3765 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3766 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5) {
3767 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3768 This, This->currentDesc.Width, This->currentDesc.Height);
3769 return WINED3DERR_NOTAVAILABLE;
3773 if(pow2Width != This->currentDesc.Width ||
3774 pow2Height != This->currentDesc.Height) {
3775 This->Flags |= SFLAG_NONPOW2;
3778 TRACE("%p\n", This);
3779 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3780 /* one of three options
3781 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)
3782 2: Set the texture to the maximum size (bad idea)
3783 3: WARN and return WINED3DERR_NOTAVAILABLE;
3784 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.
3786 WARN("(%p) Creating an oversized surface\n", This);
3787 This->Flags |= SFLAG_OVERSIZE;
3789 /* This will be initialized on the first blt */
3790 This->glRect.left = 0;
3791 This->glRect.top = 0;
3792 This->glRect.right = 0;
3793 This->glRect.bottom = 0;
3794 } else {
3795 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3796 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3797 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3798 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3800 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3801 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3803 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3804 This->pow2Width = This->currentDesc.Width;
3805 This->pow2Height = This->currentDesc.Height;
3806 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
3809 /* No oversize, gl rect is the full texture size */
3810 This->Flags &= ~SFLAG_OVERSIZE;
3811 This->glRect.left = 0;
3812 This->glRect.top = 0;
3813 This->glRect.right = This->pow2Width;
3814 This->glRect.bottom = This->pow2Height;
3817 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3818 switch(wined3d_settings.offscreen_rendering_mode) {
3819 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3820 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3821 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3825 This->Flags |= SFLAG_INSYSMEM;
3827 return WINED3D_OK;
3830 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
3831 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3832 IWineD3DBaseTexture *texture;
3834 TRACE("(%p)->(%s, %s)\n", iface,
3835 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
3836 persistent ? "TRUE" : "FALSE");
3838 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
3839 IWineD3DSwapChain *swapchain = NULL;
3841 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
3842 TRACE("Surface %p is an onscreen surface\n", iface);
3844 IWineD3DSwapChain_Release(swapchain);
3845 } else {
3846 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
3847 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
3851 if(persistent) {
3852 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
3853 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3854 TRACE("Passing to container\n");
3855 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3856 IWineD3DBaseTexture_Release(texture);
3859 This->Flags &= ~SFLAG_LOCATIONS;
3860 This->Flags |= flag;
3861 } else {
3862 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
3863 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
3864 TRACE("Passing to container\n");
3865 IWineD3DBaseTexture_SetDirty(texture, TRUE);
3866 IWineD3DBaseTexture_Release(texture);
3869 This->Flags &= ~flag;
3873 struct coords {
3874 GLfloat x, y, z;
3877 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
3878 struct coords coords[4];
3879 RECT rect;
3880 IWineD3DSwapChain *swapchain = NULL;
3881 IWineD3DBaseTexture *texture = NULL;
3882 HRESULT hr;
3883 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3885 if(rect_in) {
3886 rect = *rect_in;
3887 } else {
3888 rect.left = 0;
3889 rect.top = 0;
3890 rect.right = This->currentDesc.Width;
3891 rect.bottom = This->currentDesc.Height;
3894 ActivateContext(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
3895 ENTER_GL();
3897 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
3898 glEnable(GL_TEXTURE_RECTANGLE_ARB);
3899 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
3900 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
3901 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
3902 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3903 checkGLcall("glTexParameteri");
3904 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3905 checkGLcall("glTexParameteri");
3907 coords[0].x = rect.left;
3908 coords[0].z = 0;
3910 coords[1].x = rect.left;
3911 coords[1].z = 0;
3913 coords[2].x = rect.right;
3914 coords[2].z = 0;
3916 coords[3].x = rect.right;
3917 coords[3].z = 0;
3919 coords[0].y = rect.top;
3920 coords[1].y = rect.bottom;
3921 coords[2].y = rect.bottom;
3922 coords[3].y = rect.top;
3923 } else if(This->glDescription.target == GL_TEXTURE_2D) {
3924 glEnable(GL_TEXTURE_2D);
3925 checkGLcall("glEnable(GL_TEXTURE_2D)");
3926 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
3927 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
3928 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3929 checkGLcall("glTexParameteri");
3930 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3931 checkGLcall("glTexParameteri");
3933 coords[0].x = (float)rect.left / This->pow2Width;
3934 coords[0].z = 0;
3936 coords[1].x = (float)rect.left / This->pow2Width;
3937 coords[1].z = 0;
3939 coords[2].x = (float)rect.right / This->pow2Width;
3940 coords[2].z = 0;
3942 coords[3].x = (float)rect.right / This->pow2Width;
3943 coords[3].z = 0;
3945 coords[0].y = (float)rect.top / This->pow2Height;
3946 coords[1].y = (float)rect.bottom / This->pow2Height;
3947 coords[2].y = (float)rect.bottom / This->pow2Height;
3948 coords[3].y = (float)rect.top / This->pow2Height;
3949 } else {
3950 /* Must be a cube map */
3951 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
3952 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
3953 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
3954 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
3955 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3956 checkGLcall("glTexParameteri");
3957 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3958 checkGLcall("glTexParameteri");
3960 switch(This->glDescription.target) {
3961 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3962 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
3963 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3964 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3965 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
3966 break;
3968 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3969 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3970 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
3971 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
3972 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3973 break;
3975 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3976 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
3977 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
3978 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
3979 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
3980 break;
3982 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3983 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3984 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3985 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
3986 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
3987 break;
3989 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3990 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
3991 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
3992 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
3993 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
3994 break;
3996 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3997 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
3998 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
3999 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
4000 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
4001 break;
4003 default:
4004 ERR("Unexpected texture target\n");
4005 LEAVE_GL();
4006 return;
4010 glBegin(GL_QUADS);
4011 glTexCoord3fv(&coords[0].x);
4012 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
4014 glTexCoord3fv(&coords[1].x);
4015 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
4017 glTexCoord3fv(&coords[2].x);
4018 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
4020 glTexCoord3fv(&coords[3].x);
4021 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
4022 glEnd();
4023 checkGLcall("glEnd");
4025 if(This->glDescription.target != GL_TEXTURE_2D) {
4026 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4027 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4028 } else {
4029 glDisable(GL_TEXTURE_2D);
4030 checkGLcall("glDisable(GL_TEXTURE_2D)");
4033 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
4034 if(hr == WINED3D_OK && swapchain) {
4035 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
4036 if(((IWineD3DSwapChainImpl*)swapchain)->frontBuffer == (IWineD3DSurface*)This ||
4037 ((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
4038 glFlush();
4040 IWineD3DSwapChain_Release(swapchain);
4041 } else {
4042 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
4043 * reset properly next draw
4045 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
4046 if(hr == WINED3D_OK && texture) {
4047 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
4048 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
4049 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
4050 IWineD3DBaseTexture_Release(texture);
4053 LEAVE_GL();
4056 /*****************************************************************************
4057 * IWineD3DSurface::LoadLocation
4059 * Copies the current surface data from wherever it is to the requested
4060 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4061 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4062 * multiple locations, the gl texture is preferred over the drawable, which is
4063 * preferred over system memory. The PBO counts as system memory. If rect is
4064 * not NULL, only the specified rectangle is copied (only supported for
4065 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4066 * location is marked up to date after the copy.
4068 * Parameters:
4069 * flag: Surface location flag to be updated
4070 * rect: rectangle to be copied
4072 * Returns:
4073 * WINED3D_OK on success
4074 * WINED3DERR_DEVICELOST on an internal error
4076 *****************************************************************************/
4077 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4078 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4079 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4080 IWineD3DSwapChain *swapchain = NULL;
4081 GLenum format, internal, type;
4082 CONVERT_TYPES convert;
4083 int bpp;
4084 int width, pitch, outpitch;
4085 BYTE *mem;
4087 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4088 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
4089 TRACE("Surface %p is an onscreen surface\n", iface);
4091 IWineD3DSwapChain_Release(swapchain);
4092 } else {
4093 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4094 * Prefer SFLAG_INTEXTURE. */
4095 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4099 TRACE("(%p)->(%s, %p)\n", iface,
4100 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
4101 rect);
4102 if(rect) {
4103 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4106 if(This->Flags & flag) {
4107 TRACE("Location already up to date\n");
4108 return WINED3D_OK;
4111 if(!(This->Flags & SFLAG_LOCATIONS)) {
4112 ERR("Surface does not have any up to date location\n");
4113 This->Flags |= SFLAG_LOST;
4114 return WINED3DERR_DEVICELOST;
4117 if(flag == SFLAG_INSYSMEM) {
4118 surface_prepare_system_memory(This);
4120 /* Download the surface to system memory */
4121 if(This->Flags & SFLAG_INTEXTURE) {
4122 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4123 surface_bind_and_dirtify(This);
4125 surface_download_data(This);
4126 } else {
4127 read_from_framebuffer(This, rect,
4128 This->resource.allocatedMemory,
4129 IWineD3DSurface_GetPitch(iface));
4131 } else if(flag == SFLAG_INDRAWABLE) {
4132 if(This->Flags & SFLAG_INTEXTURE) {
4133 surface_blt_to_drawable(This, rect);
4134 } else {
4135 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4137 /* The width is in 'length' not in bytes */
4138 width = This->currentDesc.Width;
4139 pitch = IWineD3DSurface_GetPitch(iface);
4141 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4142 * but it isn't set (yet) in all cases it is getting called. */
4143 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4144 TRACE("Removing the pbo attached to surface %p\n", This);
4145 surface_remove_pbo(This);
4148 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4149 int height = This->currentDesc.Height;
4151 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4152 outpitch = width * bpp;
4153 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4155 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4156 if(!mem) {
4157 ERR("Out of memory %d, %d!\n", outpitch, height);
4158 return WINED3DERR_OUTOFVIDEOMEMORY;
4160 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4162 This->Flags |= SFLAG_CONVERTED;
4163 } else {
4164 This->Flags &= ~SFLAG_CONVERTED;
4165 mem = This->resource.allocatedMemory;
4168 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4170 /* Don't delete PBO memory */
4171 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4172 HeapFree(GetProcessHeap(), 0, mem);
4174 } else /* if(flag == SFLAG_INTEXTURE) */ {
4175 if (This->Flags & SFLAG_INDRAWABLE) {
4176 read_from_framebuffer_texture(This);
4177 } else { /* Upload from system memory */
4178 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4180 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4181 surface_bind_and_dirtify(This);
4182 ENTER_GL();
4184 /* The only place where LoadTexture() might get called when isInDraw=1
4185 * is ActivateContext where lastActiveRenderTarget is preloaded.
4187 if(iface == device->lastActiveRenderTarget && device->isInDraw)
4188 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
4190 /* Otherwise: System memory copy must be most up to date */
4192 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4193 This->Flags |= SFLAG_GLCKEY;
4194 This->glCKey = This->SrcBltCKey;
4196 else This->Flags &= ~SFLAG_GLCKEY;
4198 /* The width is in 'length' not in bytes */
4199 width = This->currentDesc.Width;
4200 pitch = IWineD3DSurface_GetPitch(iface);
4202 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4203 * but it isn't set (yet) in all cases it is getting called. */
4204 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4205 TRACE("Removing the pbo attached to surface %p\n", This);
4206 surface_remove_pbo(This);
4209 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4210 int height = This->currentDesc.Height;
4212 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4213 outpitch = width * bpp;
4214 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4216 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4217 if(!mem) {
4218 ERR("Out of memory %d, %d!\n", outpitch, height);
4219 return WINED3DERR_OUTOFVIDEOMEMORY;
4221 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4223 This->Flags |= SFLAG_CONVERTED;
4224 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
4225 d3dfmt_p8_upload_palette(iface, convert);
4226 This->Flags &= ~SFLAG_CONVERTED;
4227 mem = This->resource.allocatedMemory;
4228 } else {
4229 This->Flags &= ~SFLAG_CONVERTED;
4230 mem = This->resource.allocatedMemory;
4233 /* Make sure the correct pitch is used */
4234 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4236 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4237 TRACE("non power of two support\n");
4238 if(!(This->Flags & SFLAG_ALLOCATED)) {
4239 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4241 if (mem || (This->Flags & SFLAG_PBO)) {
4242 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4244 } else {
4245 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4246 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4248 if(!(This->Flags & SFLAG_ALLOCATED)) {
4249 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4251 if (mem || (This->Flags & SFLAG_PBO)) {
4252 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4256 /* Restore the default pitch */
4257 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4258 LEAVE_GL();
4260 /* Don't delete PBO memory */
4261 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4262 HeapFree(GetProcessHeap(), 0, mem);
4266 if(rect == NULL) {
4267 This->Flags |= flag;
4270 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4271 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4272 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4273 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4276 return WINED3D_OK;
4279 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4280 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4281 IWineD3DSwapChain *swapchain = NULL;
4283 /* Update the drawable size method */
4284 if(container) {
4285 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4287 if(swapchain) {
4288 This->get_drawable_size = get_drawable_size_swapchain;
4289 IWineD3DSwapChain_Release(swapchain);
4290 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4291 switch(wined3d_settings.offscreen_rendering_mode) {
4292 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4293 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4294 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4298 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4301 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4302 return SURFACE_OPENGL;
4305 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4307 /* IUnknown */
4308 IWineD3DBaseSurfaceImpl_QueryInterface,
4309 IWineD3DBaseSurfaceImpl_AddRef,
4310 IWineD3DSurfaceImpl_Release,
4311 /* IWineD3DResource */
4312 IWineD3DBaseSurfaceImpl_GetParent,
4313 IWineD3DBaseSurfaceImpl_GetDevice,
4314 IWineD3DBaseSurfaceImpl_SetPrivateData,
4315 IWineD3DBaseSurfaceImpl_GetPrivateData,
4316 IWineD3DBaseSurfaceImpl_FreePrivateData,
4317 IWineD3DBaseSurfaceImpl_SetPriority,
4318 IWineD3DBaseSurfaceImpl_GetPriority,
4319 IWineD3DSurfaceImpl_PreLoad,
4320 IWineD3DSurfaceImpl_UnLoad,
4321 IWineD3DBaseSurfaceImpl_GetType,
4322 /* IWineD3DSurface */
4323 IWineD3DBaseSurfaceImpl_GetContainer,
4324 IWineD3DBaseSurfaceImpl_GetDesc,
4325 IWineD3DSurfaceImpl_LockRect,
4326 IWineD3DSurfaceImpl_UnlockRect,
4327 IWineD3DSurfaceImpl_GetDC,
4328 IWineD3DSurfaceImpl_ReleaseDC,
4329 IWineD3DSurfaceImpl_Flip,
4330 IWineD3DSurfaceImpl_Blt,
4331 IWineD3DBaseSurfaceImpl_GetBltStatus,
4332 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4333 IWineD3DBaseSurfaceImpl_IsLost,
4334 IWineD3DBaseSurfaceImpl_Restore,
4335 IWineD3DSurfaceImpl_BltFast,
4336 IWineD3DBaseSurfaceImpl_GetPalette,
4337 IWineD3DBaseSurfaceImpl_SetPalette,
4338 IWineD3DSurfaceImpl_RealizePalette,
4339 IWineD3DBaseSurfaceImpl_SetColorKey,
4340 IWineD3DBaseSurfaceImpl_GetPitch,
4341 IWineD3DSurfaceImpl_SetMem,
4342 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4343 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4344 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4345 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4346 IWineD3DBaseSurfaceImpl_SetClipper,
4347 IWineD3DBaseSurfaceImpl_GetClipper,
4348 /* Internal use: */
4349 IWineD3DSurfaceImpl_AddDirtyRect,
4350 IWineD3DSurfaceImpl_LoadTexture,
4351 IWineD3DSurfaceImpl_BindTexture,
4352 IWineD3DSurfaceImpl_SaveSnapshot,
4353 IWineD3DSurfaceImpl_SetContainer,
4354 IWineD3DSurfaceImpl_SetGlTextureDesc,
4355 IWineD3DSurfaceImpl_GetGlDesc,
4356 IWineD3DSurfaceImpl_GetData,
4357 IWineD3DSurfaceImpl_SetFormat,
4358 IWineD3DSurfaceImpl_PrivateSetup,
4359 IWineD3DSurfaceImpl_ModifyLocation,
4360 IWineD3DSurfaceImpl_LoadLocation,
4361 IWineD3DSurfaceImpl_GetImplType