wined3d: Add a more convenient way to check if a surface is on a swapchain.
[wine/multimedia.git] / dlls / wined3d / surface.c
blobb0c6511b6f0bcbd9d4d9eb5c1320e7c400408a20
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-2008 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 WINE_DECLARE_DEBUG_CHANNEL(d3d);
35 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
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 void surface_remove_pbo(IWineD3DSurfaceImpl *This);
41 void surface_force_reload(IWineD3DSurface *iface)
43 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
45 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
48 void surface_set_texture_name(IWineD3DSurface *iface, GLuint new_name, BOOL srgb)
50 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
51 GLuint *name;
52 DWORD flag;
54 if(srgb)
56 name = &This->glDescription.srgbTextureName;
57 flag = SFLAG_INSRGBTEX;
59 else
61 name = &This->glDescription.textureName;
62 flag = SFLAG_INTEXTURE;
65 TRACE("(%p) : setting texture name %u\n", This, new_name);
67 if (!*name && new_name)
69 /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
70 * surface has no texture name yet. See if we can get rid of this. */
71 if (This->Flags & flag)
72 ERR("Surface has SFLAG_INTEXTURE set, but no texture name\n");
73 IWineD3DSurface_ModifyLocation(iface, flag, FALSE);
76 *name = new_name;
77 surface_force_reload(iface);
80 void surface_set_texture_target(IWineD3DSurface *iface, GLenum target)
82 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
84 TRACE("(%p) : setting target %#x\n", This, target);
86 if (This->glDescription.target != target)
88 if (target == GL_TEXTURE_RECTANGLE_ARB)
90 This->Flags &= ~SFLAG_NORMCOORD;
92 else if (This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB)
94 This->Flags |= SFLAG_NORMCOORD;
97 This->glDescription.target = target;
98 surface_force_reload(iface);
101 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This, BOOL srgb) {
102 int active_sampler;
104 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
105 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
106 * gl states. The current texture unit should always be a valid one.
108 * To be more specific, this is tricky because we can implicitly be called
109 * from sampler() in state.c. This means we can't touch anything other than
110 * whatever happens to be the currently active texture, or we would risk
111 * marking already applied sampler states dirty again.
113 * TODO: Track the current active texture per GL context instead of using glGet
115 GLint active_texture;
116 ENTER_GL();
117 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
118 LEAVE_GL();
119 active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
121 if (active_sampler != -1) {
122 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
124 IWineD3DSurface_BindTexture((IWineD3DSurface *)This, srgb);
127 /* This function checks if the primary render target uses the 8bit paletted format. */
128 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
130 if (device->render_targets && device->render_targets[0]) {
131 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
132 if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
133 && (render_target->resource.format_desc->format == WINED3DFMT_P8))
134 return TRUE;
136 return FALSE;
139 /* This call just downloads data, the caller is responsible for activating the
140 * right context and binding the correct texture. */
141 static void surface_download_data(IWineD3DSurfaceImpl *This) {
142 const struct GlPixelFormatDesc *format_desc = This->resource.format_desc;
144 /* Only support read back of converted P8 surfaces */
145 if (This->Flags & SFLAG_CONVERTED && format_desc->format != WINED3DFMT_P8)
147 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(format_desc->format));
148 return;
151 ENTER_GL();
153 if (format_desc->format == WINED3DFMT_DXT1 || format_desc->format == WINED3DFMT_DXT2
154 || format_desc->format == WINED3DFMT_DXT3 || format_desc->format == WINED3DFMT_DXT4
155 || format_desc->format == WINED3DFMT_DXT5 || format_desc->format == WINED3DFMT_ATI2N)
157 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
158 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
159 } else {
160 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n",
161 This, This->glDescription.level, format_desc->glFormat, format_desc->glType,
162 This->resource.allocatedMemory);
164 if(This->Flags & SFLAG_PBO) {
165 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
166 checkGLcall("glBindBufferARB");
167 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
168 checkGLcall("glGetCompressedTexImageARB()");
169 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
170 checkGLcall("glBindBufferARB");
171 } else {
172 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
173 checkGLcall("glGetCompressedTexImageARB()");
176 LEAVE_GL();
177 } else {
178 void *mem;
179 GLenum format = format_desc->glFormat;
180 GLenum type = format_desc->glType;
181 int src_pitch = 0;
182 int dst_pitch = 0;
184 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
185 if (format_desc->format == WINED3DFMT_P8 && primary_render_target_is_p8(This->resource.wineD3DDevice))
187 format = GL_ALPHA;
188 type = GL_UNSIGNED_BYTE;
191 if (This->Flags & SFLAG_NONPOW2) {
192 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
193 src_pitch = format_desc->byte_count * This->pow2Width;
194 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
195 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
196 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
197 } else {
198 mem = This->resource.allocatedMemory;
201 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
202 format, type, mem);
204 if(This->Flags & SFLAG_PBO) {
205 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
206 checkGLcall("glBindBufferARB");
208 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
209 type, NULL);
210 checkGLcall("glGetTexImage()");
212 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
213 checkGLcall("glBindBufferARB");
214 } else {
215 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
216 type, mem);
217 checkGLcall("glGetTexImage()");
219 LEAVE_GL();
221 if (This->Flags & SFLAG_NONPOW2) {
222 const BYTE *src_data;
223 BYTE *dst_data;
224 UINT y;
226 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
227 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
228 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
230 * We're doing this...
232 * instead of boxing the texture :
233 * |<-texture width ->| -->pow2width| /\
234 * |111111111111111111| | |
235 * |222 Texture 222222| boxed empty | texture height
236 * |3333 Data 33333333| | |
237 * |444444444444444444| | \/
238 * ----------------------------------- |
239 * | boxed empty | boxed empty | pow2height
240 * | | | \/
241 * -----------------------------------
244 * we're repacking the data to the expected texture width
246 * |<-texture width ->| -->pow2width| /\
247 * |111111111111111111222222222222222| |
248 * |222333333333333333333444444444444| texture height
249 * |444444 | |
250 * | | \/
251 * | | |
252 * | empty | pow2height
253 * | | \/
254 * -----------------------------------
256 * == is the same as
258 * |<-texture width ->| /\
259 * |111111111111111111|
260 * |222222222222222222|texture height
261 * |333333333333333333|
262 * |444444444444444444| \/
263 * --------------------
265 * this also means that any references to allocatedMemory should work with the data as if were a
266 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
268 * internally the texture is still stored in a boxed format so any references to textureName will
269 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
271 * Performance should not be an issue, because applications normally do not lock the surfaces when
272 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
273 * and doesn't have to be re-read.
275 src_data = mem;
276 dst_data = This->resource.allocatedMemory;
277 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
278 for (y = 1 ; y < This->currentDesc.Height; y++) {
279 /* skip the first row */
280 src_data += src_pitch;
281 dst_data += dst_pitch;
282 memcpy(dst_data, src_data, dst_pitch);
285 HeapFree(GetProcessHeap(), 0, mem);
289 /* Surface has now been downloaded */
290 This->Flags |= SFLAG_INSYSMEM;
293 /* This call just uploads data, the caller is responsible for activating the
294 * right context and binding the correct texture. */
295 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
296 const struct GlPixelFormatDesc *format_desc = This->resource.format_desc;
298 if (format_desc->heightscale != 1.0 && format_desc->heightscale != 0.0) height *= format_desc->heightscale;
300 if (format_desc->format == WINED3DFMT_DXT1 || format_desc->format == WINED3DFMT_DXT2
301 || format_desc->format == WINED3DFMT_DXT3 || format_desc->format == WINED3DFMT_DXT4
302 || format_desc->format == WINED3DFMT_DXT5 || format_desc->format == WINED3DFMT_ATI2N)
304 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
305 FIXME("Using DXT1/3/5 without advertized support\n");
306 } else {
307 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
308 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
309 * function uses glCompressedTexImage2D instead of the SubImage call
311 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
312 ENTER_GL();
314 if(This->Flags & SFLAG_PBO) {
315 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
316 checkGLcall("glBindBufferARB");
317 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
319 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
320 width, height, 0 /* border */, This->resource.size, NULL));
321 checkGLcall("glCompressedTexSubImage2D");
323 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
324 checkGLcall("glBindBufferARB");
325 } else {
326 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
327 width, height, 0 /* border */, This->resource.size, data));
328 checkGLcall("glCompressedTexSubImage2D");
330 LEAVE_GL();
332 } else {
333 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
334 ENTER_GL();
336 if(This->Flags & SFLAG_PBO) {
337 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
338 checkGLcall("glBindBufferARB");
339 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
341 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
342 checkGLcall("glTexSubImage2D");
344 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
345 checkGLcall("glBindBufferARB");
347 else {
348 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
349 checkGLcall("glTexSubImage2D");
352 LEAVE_GL();
356 /* This call just allocates the texture, the caller is responsible for
357 * activating the right context and binding the correct texture. */
358 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
359 const struct GlPixelFormatDesc *format_desc = This->resource.format_desc;
360 BOOL enable_client_storage = FALSE;
361 const BYTE *mem = NULL;
363 if (format_desc->heightscale != 1.0 && format_desc->heightscale != 0.0) height *= format_desc->heightscale;
365 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",
366 This, This->glDescription.target, This->glDescription.level, debug_d3dformat(format_desc->format),
367 internal, width, height, format, type);
369 if (format_desc->format == WINED3DFMT_DXT1 || format_desc->format == WINED3DFMT_DXT2
370 || format_desc->format == WINED3DFMT_DXT3 || format_desc->format == WINED3DFMT_DXT4
371 || format_desc->format == WINED3DFMT_DXT5 || format_desc->format == WINED3DFMT_ATI2N)
373 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
374 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
376 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
377 * once, unfortunately
379 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
380 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
381 This->Flags |= SFLAG_CLIENT;
382 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
383 ENTER_GL();
384 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
385 width, height, 0 /* border */, This->resource.size, mem));
386 LEAVE_GL();
389 return;
392 ENTER_GL();
394 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
395 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
396 /* In some cases we want to disable client storage.
397 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
398 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
399 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
400 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
401 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
403 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
404 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
405 This->Flags &= ~SFLAG_CLIENT;
406 enable_client_storage = TRUE;
407 } else {
408 This->Flags |= SFLAG_CLIENT;
410 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
411 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
413 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
416 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
417 checkGLcall("glTexImage2D");
419 if(enable_client_storage) {
420 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
421 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
423 LEAVE_GL();
426 /* In D3D the depth stencil dimensions have to be greater than or equal to the
427 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
428 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
429 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
430 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
431 renderbuffer_entry_t *entry;
432 GLuint renderbuffer = 0;
433 unsigned int src_width, src_height;
435 src_width = This->pow2Width;
436 src_height = This->pow2Height;
438 /* A depth stencil smaller than the render target is not valid */
439 if (width > src_width || height > src_height) return;
441 /* Remove any renderbuffer set if the sizes match */
442 if (width == src_width && height == src_height) {
443 This->current_renderbuffer = NULL;
444 return;
447 /* Look if we've already got a renderbuffer of the correct dimensions */
448 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
449 if (entry->width == width && entry->height == height) {
450 renderbuffer = entry->id;
451 This->current_renderbuffer = entry;
452 break;
456 if (!renderbuffer) {
457 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
458 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
459 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT,
460 This->resource.format_desc->glInternal, width, height));
462 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
463 entry->width = width;
464 entry->height = height;
465 entry->id = renderbuffer;
466 list_add_head(&This->renderbuffers, &entry->entry);
468 This->current_renderbuffer = entry;
471 checkGLcall("set_compatible_renderbuffer");
474 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
475 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
476 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
478 TRACE("(%p) : swapchain %p\n", This, swapchain);
480 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
481 TRACE("Returning GL_BACK\n");
482 return GL_BACK;
483 } else if (swapchain_impl->frontBuffer == iface) {
484 TRACE("Returning GL_FRONT\n");
485 return GL_FRONT;
488 FIXME("Higher back buffer, returning GL_BACK\n");
489 return GL_BACK;
492 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
493 void surface_add_dirty_rect(IWineD3DSurface *iface, const RECT *dirty_rect)
495 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
496 IWineD3DBaseTexture *baseTexture = NULL;
498 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
499 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
501 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
502 if (dirty_rect)
504 This->dirtyRect.left = min(This->dirtyRect.left, dirty_rect->left);
505 This->dirtyRect.top = min(This->dirtyRect.top, dirty_rect->top);
506 This->dirtyRect.right = max(This->dirtyRect.right, dirty_rect->right);
507 This->dirtyRect.bottom = max(This->dirtyRect.bottom, dirty_rect->bottom);
509 else
511 This->dirtyRect.left = 0;
512 This->dirtyRect.top = 0;
513 This->dirtyRect.right = This->currentDesc.Width;
514 This->dirtyRect.bottom = This->currentDesc.Height;
517 TRACE("(%p) : Dirty: yes, Rect:(%d, %d, %d, %d)\n", This, This->dirtyRect.left,
518 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
520 /* if the container is a basetexture then mark it dirty. */
521 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture)))
523 TRACE("Passing to container\n");
524 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
525 IWineD3DBaseTexture_Release(baseTexture);
529 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
531 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
532 ULONG ref = InterlockedDecrement(&This->resource.ref);
533 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
534 if (ref == 0) {
535 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
536 renderbuffer_entry_t *entry, *entry2;
537 TRACE("(%p) : cleaning up\n", This);
539 /* Need a context to destroy the texture. Use the currently active render target, but only if
540 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
541 * When destroying the primary rt, Uninit3D will activate a context before doing anything
543 if(device->render_targets && device->render_targets[0]) {
544 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
547 ENTER_GL();
548 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
549 TRACE("Deleting texture %d\n", This->glDescription.textureName);
550 glDeleteTextures(1, &This->glDescription.textureName);
553 if(This->Flags & SFLAG_PBO) {
554 /* Delete the PBO */
555 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
558 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
559 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
560 HeapFree(GetProcessHeap(), 0, entry);
562 LEAVE_GL();
564 if(This->Flags & SFLAG_DIBSECTION) {
565 /* Release the DC */
566 SelectObject(This->hDC, This->dib.holdbitmap);
567 DeleteDC(This->hDC);
568 /* Release the DIB section */
569 DeleteObject(This->dib.DIBsection);
570 This->dib.bitmap_data = NULL;
571 This->resource.allocatedMemory = NULL;
573 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
575 HeapFree(GetProcessHeap(), 0, This->palette9);
577 resource_cleanup((IWineD3DResource *)iface);
579 if(This->overlay_dest) {
580 list_remove(&This->overlay_entry);
583 TRACE("(%p) Released\n", This);
584 HeapFree(GetProcessHeap(), 0, This);
587 return ref;
590 /* ****************************************************
591 IWineD3DSurface IWineD3DResource parts follow
592 **************************************************** */
594 void surface_internal_preload(IWineD3DSurface *iface, enum WINED3DSRGB srgb)
596 /* TODO: check for locks */
597 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
598 IWineD3DBaseTexture *baseTexture = NULL;
599 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
601 TRACE("(%p)Checking to see if the container is a base texture\n", This);
602 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
603 IWineD3DBaseTextureImpl *tex_impl = (IWineD3DBaseTextureImpl *) baseTexture;
604 TRACE("Passing to container\n");
605 tex_impl->baseTexture.internal_preload(baseTexture, SRGB_RGB);
606 IWineD3DBaseTexture_Release(baseTexture);
607 } else {
608 TRACE("(%p) : About to load surface\n", This);
610 if(!device->isInDraw) {
611 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
614 if (This->resource.format_desc->format == WINED3DFMT_P8
615 || This->resource.format_desc->format == WINED3DFMT_A8P8)
617 if(palette9_changed(This)) {
618 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
619 /* TODO: This is not necessarily needed with hw palettized texture support */
620 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
621 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
622 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
626 IWineD3DSurface_LoadTexture(iface, srgb == SRGB_SRGB ? TRUE : FALSE);
628 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
629 /* Tell opengl to try and keep this texture in video ram (well mostly) */
630 GLclampf tmp;
631 tmp = 0.9f;
632 ENTER_GL();
633 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
634 LEAVE_GL();
637 return;
640 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
641 surface_internal_preload(iface, SRGB_ANY);
644 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
645 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
646 This->resource.allocatedMemory =
647 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
649 ENTER_GL();
650 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
651 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
652 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
653 checkGLcall("glGetBufferSubData");
654 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
655 checkGLcall("glDeleteBuffers");
656 LEAVE_GL();
658 This->pbo = 0;
659 This->Flags &= ~SFLAG_PBO;
662 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
663 IWineD3DBaseTexture *texture = NULL;
664 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
665 renderbuffer_entry_t *entry, *entry2;
666 TRACE("(%p)\n", iface);
668 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
669 /* Default pool resources are supposed to be destroyed before Reset is called.
670 * Implicit resources stay however. So this means we have an implicit render target
671 * or depth stencil. The content may be destroyed, but we still have to tear down
672 * opengl resources, so we cannot leave early.
674 * Put the most up to date surface location into the drawable. D3D-wise this content
675 * is undefined, so it would be nowhere, but that would make the location management
676 * more complicated. The drawable is a sane location, because if we mark sysmem or
677 * texture up to date, drawPrim will copy the uninitialized texture or sysmem to the
678 * uninitialized drawable. That's pointless and we'd have to allocate the texture /
679 * sysmem copy here.
681 if (This->resource.usage & WINED3DUSAGE_DEPTHSTENCIL) {
682 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
683 } else {
684 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, TRUE);
686 } else {
687 /* Load the surface into system memory */
688 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
689 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
691 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
692 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
694 /* Destroy PBOs, but load them into real sysmem before */
695 if(This->Flags & SFLAG_PBO) {
696 surface_remove_pbo(This);
699 /* Destroy fbo render buffers. This is needed for implicit render targets, for
700 * all application-created targets the application has to release the surface
701 * before calling _Reset
703 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
704 ENTER_GL();
705 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
706 LEAVE_GL();
707 list_remove(&entry->entry);
708 HeapFree(GetProcessHeap(), 0, entry);
710 list_init(&This->renderbuffers);
711 This->current_renderbuffer = NULL;
713 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
714 * destroy it
716 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
717 if(!texture) {
718 ENTER_GL();
719 glDeleteTextures(1, &This->glDescription.textureName);
720 This->glDescription.textureName = 0;
721 LEAVE_GL();
722 } else {
723 IWineD3DBaseTexture_Release(texture);
725 return;
728 /* ******************************************************
729 IWineD3DSurface IWineD3DSurface parts follow
730 ****************************************************** */
732 static void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription)
734 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
735 TRACE("(%p) : returning %p\n", This, &This->glDescription);
736 *glDescription = &This->glDescription;
739 /* Read the framebuffer back into the surface */
740 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
741 IWineD3DSwapChainImpl *swapchain;
742 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
743 BYTE *mem;
744 GLint fmt;
745 GLint type;
746 BYTE *row, *top, *bottom;
747 int i;
748 BOOL bpp;
749 RECT local_rect;
750 BOOL srcIsUpsideDown;
751 GLint rowLen = 0;
752 GLint skipPix = 0;
753 GLint skipRow = 0;
755 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
756 static BOOL warned = FALSE;
757 if(!warned) {
758 ERR("The application tries to lock the render target, but render target locking is disabled\n");
759 warned = TRUE;
761 return;
764 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
765 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
766 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
767 * context->last_was_blit set on the unlock.
769 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
770 ENTER_GL();
772 /* Select the correct read buffer, and give some debug output.
773 * There is no need to keep track of the current read buffer or reset it, every part of the code
774 * that reads sets the read buffer as desired.
776 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain)))
778 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
779 TRACE("Locking %#x buffer\n", buffer);
780 glReadBuffer(buffer);
781 checkGLcall("glReadBuffer");
783 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
784 srcIsUpsideDown = FALSE;
785 } else {
786 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
787 * Read from the back buffer
789 TRACE("Locking offscreen render target\n");
790 glReadBuffer(myDevice->offscreenBuffer);
791 srcIsUpsideDown = TRUE;
794 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
795 if(!rect) {
796 local_rect.left = 0;
797 local_rect.top = 0;
798 local_rect.right = This->currentDesc.Width;
799 local_rect.bottom = This->currentDesc.Height;
800 } else {
801 local_rect = *rect;
803 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
805 switch(This->resource.format_desc->format)
807 case WINED3DFMT_P8:
809 if(primary_render_target_is_p8(myDevice)) {
810 /* In case of P8 render targets the index is stored in the alpha component */
811 fmt = GL_ALPHA;
812 type = GL_UNSIGNED_BYTE;
813 mem = dest;
814 bpp = This->resource.format_desc->byte_count;
815 } else {
816 /* GL can't return palettized data, so read ARGB pixels into a
817 * separate block of memory and convert them into palettized format
818 * in software. Slow, but if the app means to use palettized render
819 * targets and locks it...
821 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
822 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
823 * for the color channels when palettizing the colors.
825 fmt = GL_RGB;
826 type = GL_UNSIGNED_BYTE;
827 pitch *= 3;
828 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
829 if(!mem) {
830 ERR("Out of memory\n");
831 LEAVE_GL();
832 return;
834 bpp = This->resource.format_desc->byte_count * 3;
837 break;
839 default:
840 mem = dest;
841 fmt = This->resource.format_desc->glFormat;
842 type = This->resource.format_desc->glType;
843 bpp = This->resource.format_desc->byte_count;
846 if(This->Flags & SFLAG_PBO) {
847 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
848 checkGLcall("glBindBufferARB");
849 if(mem != NULL) {
850 ERR("mem not null for pbo -- unexpected\n");
851 mem = NULL;
855 /* Save old pixel store pack state */
856 glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
857 checkGLcall("glIntegerv");
858 glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
859 checkGLcall("glIntegerv");
860 glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
861 checkGLcall("glIntegerv");
863 /* Setup pixel store pack state -- to glReadPixels into the correct place */
864 glPixelStorei(GL_PACK_ROW_LENGTH, This->currentDesc.Width);
865 checkGLcall("glPixelStorei");
866 glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
867 checkGLcall("glPixelStorei");
868 glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
869 checkGLcall("glPixelStorei");
871 glReadPixels(local_rect.left, (!srcIsUpsideDown) ? (This->currentDesc.Height - local_rect.bottom) : local_rect.top ,
872 local_rect.right - local_rect.left,
873 local_rect.bottom - local_rect.top,
874 fmt, type, mem);
875 checkGLcall("glReadPixels");
877 /* Reset previous pixel store pack state */
878 glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
879 checkGLcall("glPixelStorei");
880 glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
881 checkGLcall("glPixelStorei");
882 glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
883 checkGLcall("glPixelStorei");
885 if(This->Flags & SFLAG_PBO) {
886 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
887 checkGLcall("glBindBufferARB");
889 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
890 * to get a pointer to it and perform the flipping in software. This is a lot
891 * faster than calling glReadPixels for each line. In case we want more speed
892 * we should rerender it flipped in a FBO and read the data back from the FBO. */
893 if(!srcIsUpsideDown) {
894 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
895 checkGLcall("glBindBufferARB");
897 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
898 checkGLcall("glMapBufferARB");
902 /* TODO: Merge this with the palettization loop below for P8 targets */
903 if(!srcIsUpsideDown) {
904 UINT len, off;
905 /* glReadPixels returns the image upside down, and there is no way to prevent this.
906 Flip the lines in software */
907 len = (local_rect.right - local_rect.left) * bpp;
908 off = local_rect.left * bpp;
910 row = HeapAlloc(GetProcessHeap(), 0, len);
911 if(!row) {
912 ERR("Out of memory\n");
913 if (This->resource.format_desc->format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
914 LEAVE_GL();
915 return;
918 top = mem + pitch * local_rect.top;
919 bottom = mem + pitch * (local_rect.bottom - 1);
920 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
921 memcpy(row, top + off, len);
922 memcpy(top + off, bottom + off, len);
923 memcpy(bottom + off, row, len);
924 top += pitch;
925 bottom -= pitch;
927 HeapFree(GetProcessHeap(), 0, row);
929 /* Unmap the temp PBO buffer */
930 if(This->Flags & SFLAG_PBO) {
931 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
932 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
936 LEAVE_GL();
938 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
939 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
940 * the same color but we have no choice.
941 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
943 if ((This->resource.format_desc->format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice))
945 const PALETTEENTRY *pal = NULL;
946 DWORD width = pitch / 3;
947 int x, y, c;
949 if(This->palette) {
950 pal = This->palette->palents;
951 } else {
952 ERR("Palette is missing, cannot perform inverse palette lookup\n");
953 HeapFree(GetProcessHeap(), 0, mem);
954 return ;
957 for(y = local_rect.top; y < local_rect.bottom; y++) {
958 for(x = local_rect.left; x < local_rect.right; x++) {
959 /* start lines pixels */
960 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
961 const BYTE *green = blue + 1;
962 const BYTE *red = green + 1;
964 for(c = 0; c < 256; c++) {
965 if(*red == pal[c].peRed &&
966 *green == pal[c].peGreen &&
967 *blue == pal[c].peBlue)
969 *((BYTE *) dest + y * width + x) = c;
970 break;
975 HeapFree(GetProcessHeap(), 0, mem);
979 /* Read the framebuffer contents into a texture */
980 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
982 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
983 IWineD3DSwapChainImpl *swapchain;
984 int bpp;
985 GLenum format, internal, type;
986 CONVERT_TYPES convert;
987 GLint prevRead;
988 BOOL alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
990 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, srgb);
992 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
993 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
994 * states in the stateblock, and no driver was found yet that had bugs in that regard.
996 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_RESOURCELOAD);
997 surface_bind_and_dirtify(This, srgb);
999 ENTER_GL();
1000 glGetIntegerv(GL_READ_BUFFER, &prevRead);
1001 LEAVE_GL();
1003 /* Select the correct read buffer, and give some debug output.
1004 * There is no need to keep track of the current read buffer or reset it, every part of the code
1005 * that reads sets the read buffer as desired.
1007 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)This, &IID_IWineD3DSwapChain, (void **)&swapchain)))
1009 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1010 TRACE("Locking %#x buffer\n", buffer);
1012 ENTER_GL();
1013 glReadBuffer(buffer);
1014 checkGLcall("glReadBuffer");
1015 LEAVE_GL();
1017 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1018 } else {
1019 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
1020 * Read from the back buffer
1022 TRACE("Locking offscreen render target\n");
1023 ENTER_GL();
1024 glReadBuffer(device->offscreenBuffer);
1025 checkGLcall("glReadBuffer");
1026 LEAVE_GL();
1029 if(!(This->Flags & alloc_flag)) {
1030 surface_allocate_surface(This, internal, This->pow2Width,
1031 This->pow2Height, format, type);
1032 This->Flags |= alloc_flag;
1035 ENTER_GL();
1036 /* If !SrcIsUpsideDown we should flip the surface.
1037 * This can be done using glCopyTexSubImage2D but this
1038 * is VERY slow, so don't do that. We should prevent
1039 * this code from getting called in such cases or perhaps
1040 * we can use FBOs */
1042 glCopyTexSubImage2D(This->glDescription.target,
1043 This->glDescription.level,
1044 0, 0, 0, 0,
1045 This->currentDesc.Width,
1046 This->currentDesc.Height);
1047 checkGLcall("glCopyTexSubImage2D");
1049 glReadBuffer(prevRead);
1050 checkGLcall("glReadBuffer");
1052 LEAVE_GL();
1053 TRACE("Updated target %d\n", This->glDescription.target);
1056 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
1057 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1058 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1059 * changed
1061 if(!(This->Flags & SFLAG_DYNLOCK)) {
1062 This->lockCount++;
1063 /* MAXLOCKCOUNT is defined in wined3d_private.h */
1064 if(This->lockCount > MAXLOCKCOUNT) {
1065 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1066 This->Flags |= SFLAG_DYNLOCK;
1070 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1071 * Also don't create a PBO for systemmem surfaces.
1073 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
1074 GLenum error;
1075 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1077 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1078 ENTER_GL();
1080 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1081 error = glGetError();
1082 if(This->pbo == 0 || error != GL_NO_ERROR) {
1083 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1086 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1088 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1089 checkGLcall("glBindBufferARB");
1091 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1092 checkGLcall("glBufferDataARB");
1094 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1095 checkGLcall("glBindBufferARB");
1097 /* We don't need the system memory anymore and we can't even use it for PBOs */
1098 if(!(This->Flags & SFLAG_CLIENT)) {
1099 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1100 This->resource.heapMemory = NULL;
1102 This->resource.allocatedMemory = NULL;
1103 This->Flags |= SFLAG_PBO;
1104 LEAVE_GL();
1105 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
1106 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1107 * or a pbo to map
1109 if(!This->resource.heapMemory) {
1110 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1112 This->resource.allocatedMemory =
1113 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1114 if(This->Flags & SFLAG_INSYSMEM) {
1115 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1120 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1121 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1122 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1124 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1126 /* This is also done in the base class, but we have to verify this before loading any data from
1127 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1128 * may interfere, and all other bad things may happen
1130 if (This->Flags & SFLAG_LOCKED) {
1131 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1132 return WINED3DERR_INVALIDCALL;
1134 This->Flags |= SFLAG_LOCKED;
1136 if (!(This->Flags & SFLAG_LOCKABLE))
1138 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1141 if (Flags & WINED3DLOCK_DISCARD) {
1142 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1143 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1144 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1145 This->Flags |= SFLAG_INSYSMEM;
1146 goto lock_end;
1149 if (This->Flags & SFLAG_INSYSMEM) {
1150 TRACE("Local copy is up to date, not downloading data\n");
1151 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1152 goto lock_end;
1155 /* Now download the surface content from opengl
1156 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1157 * Offscreen targets which are not active at the moment or are higher targets(FBOs) can be locked with the texture path
1159 if ((This->Flags & SFLAG_SWAPCHAIN) || iface == myDevice->render_targets[0])
1161 const RECT *pass_rect = pRect;
1163 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1164 * because most caller functions do not need that. So do that here
1166 if(pRect &&
1167 pRect->top == 0 &&
1168 pRect->left == 0 &&
1169 pRect->right == This->currentDesc.Width &&
1170 pRect->bottom == This->currentDesc.Height) {
1171 pass_rect = NULL;
1174 switch(wined3d_settings.rendertargetlock_mode) {
1175 case RTL_TEXDRAW:
1176 case RTL_TEXTEX:
1177 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1178 #if 0
1179 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1180 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1181 * This may be faster on some cards
1183 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1184 #endif
1185 /* drop through */
1187 case RTL_AUTO:
1188 case RTL_READDRAW:
1189 case RTL_READTEX:
1190 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pass_rect);
1191 break;
1193 case RTL_DISABLE:
1194 break;
1196 } else if(iface == myDevice->stencilBufferTarget) {
1197 /** the depth stencil in openGL has a format of GL_FLOAT
1198 * which should be good for WINED3DFMT_D16_LOCKABLE
1199 * and WINED3DFMT_D16
1200 * it is unclear what format the stencil buffer is in except.
1201 * 'Each index is converted to fixed point...
1202 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1203 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1204 * glReadPixels(This->lockedRect.left,
1205 * This->lockedRect.bottom - j - 1,
1206 * This->lockedRect.right - This->lockedRect.left,
1207 * 1,
1208 * GL_DEPTH_COMPONENT,
1209 * type,
1210 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1212 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1213 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1214 * none of that is the case the problem is not in this function :-)
1215 ********************************************/
1216 FIXME("Depth stencil locking not supported yet\n");
1217 } else {
1218 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1219 TRACE("locking an ordinary surface\n");
1220 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1223 lock_end:
1224 if(This->Flags & SFLAG_PBO) {
1225 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1226 ENTER_GL();
1227 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1228 checkGLcall("glBindBufferARB");
1230 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1231 if(This->resource.allocatedMemory) {
1232 ERR("The surface already has PBO memory allocated!\n");
1235 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1236 checkGLcall("glMapBufferARB");
1238 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1239 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1240 checkGLcall("glBindBufferARB");
1242 LEAVE_GL();
1245 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1246 /* Don't dirtify */
1247 } else {
1248 IWineD3DBaseTexture *pBaseTexture;
1250 * Dirtify on lock
1251 * as seen in msdn docs
1253 surface_add_dirty_rect(iface, pRect);
1255 /** Dirtify Container if needed */
1256 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture))) {
1257 TRACE("Making container dirty\n");
1258 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1259 IWineD3DBaseTexture_Release(pBaseTexture);
1260 } else {
1261 TRACE("Surface is standalone, no need to dirty the container\n");
1265 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1268 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1269 GLint prev_store;
1270 GLint prev_rasterpos[4];
1271 GLint skipBytes = 0;
1272 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1273 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1274 IWineD3DSwapChainImpl *swapchain;
1276 /* Activate the correct context for the render target */
1277 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1278 ENTER_GL();
1280 if (SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface *)This, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
1281 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1282 TRACE("Unlocking %#x buffer\n", buffer);
1283 glDrawBuffer(buffer);
1284 checkGLcall("glDrawBuffer");
1286 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1287 } else {
1288 /* Primary offscreen render target */
1289 TRACE("Offscreen render target\n");
1290 glDrawBuffer(myDevice->offscreenBuffer);
1291 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1294 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1295 checkGLcall("glIntegerv");
1296 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1297 checkGLcall("glIntegerv");
1298 glPixelZoom(1.0, -1.0);
1299 checkGLcall("glPixelZoom");
1301 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1302 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1303 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1305 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1306 checkGLcall("glRasterPos2f");
1308 /* Some drivers(radeon dri, others?) don't like exceptions during
1309 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1310 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1311 * catch to put the dib section in InSync mode, which leads to a crash
1312 * and a blocked x server on my radeon card.
1314 * The following lines read the dib section so it is put in InSync mode
1315 * before glDrawPixels is called and the crash is prevented. There won't
1316 * be any interfering gdi accesses, because UnlockRect is called from
1317 * ReleaseDC, and the app won't use the dc any more afterwards.
1319 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1320 volatile BYTE read;
1321 read = This->resource.allocatedMemory[0];
1324 if(This->Flags & SFLAG_PBO) {
1325 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1326 checkGLcall("glBindBufferARB");
1329 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1330 if(This->Flags & SFLAG_LOCKED) {
1331 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1332 (This->lockedRect.bottom - This->lockedRect.top)-1,
1333 fmt, type,
1334 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1335 checkGLcall("glDrawPixels");
1336 } else {
1337 glDrawPixels(This->currentDesc.Width,
1338 This->currentDesc.Height,
1339 fmt, type, mem);
1340 checkGLcall("glDrawPixels");
1343 if(This->Flags & SFLAG_PBO) {
1344 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1345 checkGLcall("glBindBufferARB");
1348 glPixelZoom(1.0,1.0);
1349 checkGLcall("glPixelZoom");
1351 glRasterPos3iv(&prev_rasterpos[0]);
1352 checkGLcall("glRasterPos3iv");
1354 /* Reset to previous pack row length */
1355 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1356 checkGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1358 if(!swapchain) {
1359 glDrawBuffer(myDevice->offscreenBuffer);
1360 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1361 } else if(swapchain->backBuffer) {
1362 glDrawBuffer(GL_BACK);
1363 checkGLcall("glDrawBuffer(GL_BACK)");
1364 } else {
1365 glDrawBuffer(GL_FRONT);
1366 checkGLcall("glDrawBuffer(GL_FRONT)");
1368 LEAVE_GL();
1370 return;
1373 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1374 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1375 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1376 BOOL fullsurface;
1378 if (!(This->Flags & SFLAG_LOCKED)) {
1379 WARN("trying to Unlock an unlocked surf@%p\n", This);
1380 return WINEDDERR_NOTLOCKED;
1383 if (This->Flags & SFLAG_PBO) {
1384 TRACE("Freeing PBO memory\n");
1385 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1386 ENTER_GL();
1387 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1388 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1389 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1390 checkGLcall("glUnmapBufferARB");
1391 LEAVE_GL();
1392 This->resource.allocatedMemory = NULL;
1395 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1397 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1398 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1399 goto unlock_end;
1402 if ((This->Flags & SFLAG_SWAPCHAIN) || (myDevice->render_targets && iface == myDevice->render_targets[0]))
1404 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1405 static BOOL warned = FALSE;
1406 if(!warned) {
1407 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1408 warned = TRUE;
1410 goto unlock_end;
1413 if(This->dirtyRect.left == 0 &&
1414 This->dirtyRect.top == 0 &&
1415 This->dirtyRect.right == This->currentDesc.Width &&
1416 This->dirtyRect.bottom == This->currentDesc.Height) {
1417 fullsurface = TRUE;
1418 } else {
1419 /* TODO: Proper partial rectangle tracking */
1420 fullsurface = FALSE;
1421 This->Flags |= SFLAG_INSYSMEM;
1424 switch(wined3d_settings.rendertargetlock_mode) {
1425 case RTL_READTEX:
1426 case RTL_TEXTEX:
1427 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1428 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1429 /* drop through */
1431 case RTL_AUTO:
1432 case RTL_READDRAW:
1433 case RTL_TEXDRAW:
1434 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1435 break;
1438 if(!fullsurface) {
1439 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1440 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1441 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1442 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1443 * not fully up to date because only a subrectangle was read in LockRect.
1445 This->Flags &= ~SFLAG_INSYSMEM;
1446 This->Flags |= SFLAG_INDRAWABLE;
1449 This->dirtyRect.left = This->currentDesc.Width;
1450 This->dirtyRect.top = This->currentDesc.Height;
1451 This->dirtyRect.right = 0;
1452 This->dirtyRect.bottom = 0;
1453 } else if(iface == myDevice->stencilBufferTarget) {
1454 FIXME("Depth Stencil buffer locking is not implemented\n");
1455 } else {
1456 /* The rest should be a normal texture */
1457 IWineD3DBaseTextureImpl *impl;
1458 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1459 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1460 * states need resetting
1462 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1463 if(impl->baseTexture.bindCount) {
1464 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1466 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1470 unlock_end:
1471 This->Flags &= ~SFLAG_LOCKED;
1472 memset(&This->lockedRect, 0, sizeof(RECT));
1474 /* Overlays have to be redrawn manually after changes with the GL implementation */
1475 if(This->overlay_dest) {
1476 IWineD3DSurface_DrawOverlay(iface);
1478 return WINED3D_OK;
1481 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
1483 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1484 WINED3DLOCKED_RECT lock;
1485 HRESULT hr;
1486 RGBQUAD col[256];
1488 TRACE("(%p)->(%p)\n",This,pHDC);
1490 if(This->Flags & SFLAG_USERPTR) {
1491 ERR("Not supported on surfaces with an application-provided surfaces\n");
1492 return WINEDDERR_NODC;
1495 /* Give more detailed info for ddraw */
1496 if (This->Flags & SFLAG_DCINUSE)
1497 return WINEDDERR_DCALREADYCREATED;
1499 /* Can't GetDC if the surface is locked */
1500 if (This->Flags & SFLAG_LOCKED)
1501 return WINED3DERR_INVALIDCALL;
1503 /* According to Direct3D9 docs, only these formats are supported */
1504 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1505 if (This->resource.format_desc->format != WINED3DFMT_R5G6B5
1506 && This->resource.format_desc->format != WINED3DFMT_X1R5G5B5
1507 && This->resource.format_desc->format != WINED3DFMT_R8G8B8
1508 && This->resource.format_desc->format != WINED3DFMT_X8R8G8B8)
1509 return WINED3DERR_INVALIDCALL;
1512 memset(&lock, 0, sizeof(lock)); /* To be sure */
1514 /* Create a DIB section if there isn't a hdc yet */
1515 if(!This->hDC) {
1516 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1517 if(This->Flags & SFLAG_CLIENT) {
1518 surface_internal_preload(iface, SRGB_RGB);
1521 /* Use the dib section from now on if we are not using a PBO */
1522 if(!(This->Flags & SFLAG_PBO))
1523 This->resource.allocatedMemory = This->dib.bitmap_data;
1526 /* Lock the surface */
1527 hr = IWineD3DSurface_LockRect(iface,
1528 &lock,
1529 NULL,
1532 if(This->Flags & SFLAG_PBO) {
1533 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1534 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1537 if(FAILED(hr)) {
1538 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1539 /* keep the dib section */
1540 return hr;
1543 if (This->resource.format_desc->format == WINED3DFMT_P8
1544 || This->resource.format_desc->format == WINED3DFMT_A8P8)
1546 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1547 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1548 unsigned int n;
1549 const PALETTEENTRY *pal = NULL;
1551 if(This->palette) {
1552 pal = This->palette->palents;
1553 } else {
1554 IWineD3DSurfaceImpl *dds_primary;
1555 IWineD3DSwapChainImpl *swapchain;
1556 swapchain = (IWineD3DSwapChainImpl *)This->resource.wineD3DDevice->swapchains[0];
1557 dds_primary = (IWineD3DSurfaceImpl *)swapchain->frontBuffer;
1558 if (dds_primary && dds_primary->palette)
1559 pal = dds_primary->palette->palents;
1562 if (pal) {
1563 for (n=0; n<256; n++) {
1564 col[n].rgbRed = pal[n].peRed;
1565 col[n].rgbGreen = pal[n].peGreen;
1566 col[n].rgbBlue = pal[n].peBlue;
1567 col[n].rgbReserved = 0;
1569 SetDIBColorTable(This->hDC, 0, 256, col);
1573 *pHDC = This->hDC;
1574 TRACE("returning %p\n",*pHDC);
1575 This->Flags |= SFLAG_DCINUSE;
1577 return WINED3D_OK;
1580 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
1582 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1584 TRACE("(%p)->(%p)\n",This,hDC);
1586 if (!(This->Flags & SFLAG_DCINUSE))
1587 return WINEDDERR_NODC;
1589 if (This->hDC !=hDC) {
1590 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1591 return WINEDDERR_NODC;
1594 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1595 /* Copy the contents of the DIB over to the PBO */
1596 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1599 /* we locked first, so unlock now */
1600 IWineD3DSurface_UnlockRect(iface);
1602 This->Flags &= ~SFLAG_DCINUSE;
1604 return WINED3D_OK;
1607 /* ******************************************************
1608 IWineD3DSurface Internal (No mapping to directx api) parts follow
1609 ****************************************************** */
1611 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) {
1612 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1613 const struct GlPixelFormatDesc *glDesc = This->resource.format_desc;
1614 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1616 /* Default values: From the surface */
1617 *format = glDesc->glFormat;
1618 *type = glDesc->glType;
1619 *convert = NO_CONVERSION;
1620 *target_bpp = glDesc->byte_count;
1622 if(srgb_mode) {
1623 *internal = glDesc->glGammaInternal;
1624 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1625 *internal = glDesc->rtInternal;
1626 } else {
1627 *internal = glDesc->glInternal;
1630 /* Ok, now look if we have to do any conversion */
1631 switch(This->resource.format_desc->format)
1633 case WINED3DFMT_P8:
1634 /* ****************
1635 Paletted Texture
1636 **************** */
1638 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1639 * of the two is available make sure texturing is requested as neither of the two works in
1640 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1641 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1642 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1643 * conflicts with this.
1645 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) ||
1646 (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) &&
1647 device->render_targets &&
1648 This == (IWineD3DSurfaceImpl*)device->render_targets[0])) ||
1649 colorkey_active || !use_texturing ) {
1650 *format = GL_RGBA;
1651 *internal = GL_RGBA;
1652 *type = GL_UNSIGNED_BYTE;
1653 *target_bpp = 4;
1654 if(colorkey_active) {
1655 *convert = CONVERT_PALETTED_CK;
1656 } else {
1657 *convert = CONVERT_PALETTED;
1660 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1661 *format = GL_ALPHA;
1662 *internal = GL_RGBA;
1663 *type = GL_UNSIGNED_BYTE;
1664 *target_bpp = 1;
1667 break;
1669 case WINED3DFMT_R3G3B2:
1670 /* **********************
1671 GL_UNSIGNED_BYTE_3_3_2
1672 ********************** */
1673 if (colorkey_active) {
1674 /* This texture format will never be used.. So do not care about color keying
1675 up until the point in time it will be needed :-) */
1676 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1678 break;
1680 case WINED3DFMT_R5G6B5:
1681 if (colorkey_active) {
1682 *convert = CONVERT_CK_565;
1683 *format = GL_RGBA;
1684 *internal = GL_RGBA;
1685 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1687 break;
1689 case WINED3DFMT_X1R5G5B5:
1690 if (colorkey_active) {
1691 *convert = CONVERT_CK_5551;
1692 *format = GL_BGRA;
1693 *internal = GL_RGBA;
1694 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1696 break;
1698 case WINED3DFMT_R8G8B8:
1699 if (colorkey_active) {
1700 *convert = CONVERT_CK_RGB24;
1701 *format = GL_RGBA;
1702 *internal = GL_RGBA;
1703 *type = GL_UNSIGNED_INT_8_8_8_8;
1704 *target_bpp = 4;
1706 break;
1708 case WINED3DFMT_X8R8G8B8:
1709 if (colorkey_active) {
1710 *convert = CONVERT_RGB32_888;
1711 *format = GL_RGBA;
1712 *internal = GL_RGBA;
1713 *type = GL_UNSIGNED_INT_8_8_8_8;
1715 break;
1717 case WINED3DFMT_R8G8_SNORM:
1718 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1719 *convert = CONVERT_V8U8;
1720 *format = GL_BGR;
1721 *internal = GL_RGB8;
1722 *type = GL_UNSIGNED_BYTE;
1723 *target_bpp = 3;
1724 break;
1726 case WINED3DFMT_L6V5U5:
1727 *convert = CONVERT_L6V5U5;
1728 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1729 *target_bpp = 3;
1730 /* Use format and types from table */
1731 } else {
1732 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1733 *target_bpp = 2;
1734 *format = GL_RGB;
1735 *internal = GL_RGB5;
1736 *type = GL_UNSIGNED_SHORT_5_6_5;
1738 break;
1740 case WINED3DFMT_X8L8V8U8:
1741 *convert = CONVERT_X8L8V8U8;
1742 *target_bpp = 4;
1743 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1744 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1745 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1746 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1747 * the needed type and format parameter, so the internal format contains a
1748 * 4th component, which is returned as alpha
1750 } else {
1751 *format = GL_BGRA;
1752 *internal = GL_RGB8;
1753 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1755 break;
1757 case WINED3DFMT_R8G8B8A8_SNORM:
1758 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1759 *convert = CONVERT_Q8W8V8U8;
1760 *format = GL_BGRA;
1761 *internal = GL_RGBA8;
1762 *type = GL_UNSIGNED_BYTE;
1763 *target_bpp = 4;
1764 break;
1766 case WINED3DFMT_R16G16_SNORM:
1767 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1768 *convert = CONVERT_V16U16;
1769 *format = GL_BGR;
1770 *internal = GL_RGB16_EXT;
1771 *type = GL_UNSIGNED_SHORT;
1772 *target_bpp = 6;
1773 break;
1775 case WINED3DFMT_A4L4:
1776 /* A4L4 exists as an internal gl format, but for some reason there is not
1777 * format+type combination to load it. Thus convert it to A8L8, then load it
1778 * with A4L4 internal, but A8L8 format+type
1780 *convert = CONVERT_A4L4;
1781 *format = GL_LUMINANCE_ALPHA;
1782 *internal = GL_LUMINANCE4_ALPHA4;
1783 *type = GL_UNSIGNED_BYTE;
1784 *target_bpp = 2;
1785 break;
1787 case WINED3DFMT_R16G16_UNORM:
1788 *convert = CONVERT_G16R16;
1789 *format = GL_RGB;
1790 *internal = GL_RGB16_EXT;
1791 *type = GL_UNSIGNED_SHORT;
1792 *target_bpp = 6;
1793 break;
1795 default:
1796 break;
1799 return WINED3D_OK;
1802 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
1803 UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
1805 const BYTE *source;
1806 BYTE *dest;
1807 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1809 switch (convert) {
1810 case NO_CONVERSION:
1812 memcpy(dst, src, pitch * height);
1813 break;
1815 case CONVERT_PALETTED:
1816 case CONVERT_PALETTED_CK:
1818 IWineD3DPaletteImpl* pal = This->palette;
1819 BYTE table[256][4];
1820 unsigned int x, y;
1822 if( pal == NULL) {
1823 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1826 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1828 for (y = 0; y < height; y++)
1830 source = src + pitch * y;
1831 dest = dst + outpitch * y;
1832 /* This is an 1 bpp format, using the width here is fine */
1833 for (x = 0; x < width; x++) {
1834 BYTE color = *source++;
1835 *dest++ = table[color][0];
1836 *dest++ = table[color][1];
1837 *dest++ = table[color][2];
1838 *dest++ = table[color][3];
1842 break;
1844 case CONVERT_CK_565:
1846 /* Converting the 565 format in 5551 packed to emulate color-keying.
1848 Note : in all these conversion, it would be best to average the averaging
1849 pixels to get the color of the pixel that will be color-keyed to
1850 prevent 'color bleeding'. This will be done later on if ever it is
1851 too visible.
1853 Note2: Nvidia documents say that their driver does not support alpha + color keying
1854 on the same surface and disables color keying in such a case
1856 unsigned int x, y;
1857 const WORD *Source;
1858 WORD *Dest;
1860 TRACE("Color keyed 565\n");
1862 for (y = 0; y < height; y++) {
1863 Source = (const WORD *)(src + y * pitch);
1864 Dest = (WORD *) (dst + y * outpitch);
1865 for (x = 0; x < width; x++ ) {
1866 WORD color = *Source++;
1867 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1868 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1869 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1870 *Dest |= 0x0001;
1872 Dest++;
1876 break;
1878 case CONVERT_CK_5551:
1880 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1881 unsigned int x, y;
1882 const WORD *Source;
1883 WORD *Dest;
1884 TRACE("Color keyed 5551\n");
1885 for (y = 0; y < height; y++) {
1886 Source = (const WORD *)(src + y * pitch);
1887 Dest = (WORD *) (dst + y * outpitch);
1888 for (x = 0; x < width; x++ ) {
1889 WORD color = *Source++;
1890 *Dest = color;
1891 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1892 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1893 *Dest |= (1 << 15);
1895 else {
1896 *Dest &= ~(1 << 15);
1898 Dest++;
1902 break;
1904 case CONVERT_CK_RGB24:
1906 /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
1907 unsigned int x, y;
1908 for (y = 0; y < height; y++)
1910 source = src + pitch * y;
1911 dest = dst + outpitch * y;
1912 for (x = 0; x < width; x++) {
1913 DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
1914 DWORD dstcolor = color << 8;
1915 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1916 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1917 dstcolor |= 0xff;
1919 *(DWORD*)dest = dstcolor;
1920 source += 3;
1921 dest += 4;
1925 break;
1927 case CONVERT_RGB32_888:
1929 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
1930 unsigned int x, y;
1931 for (y = 0; y < height; y++)
1933 source = src + pitch * y;
1934 dest = dst + outpitch * y;
1935 for (x = 0; x < width; x++) {
1936 DWORD color = 0xffffff & *(const DWORD*)source;
1937 DWORD dstcolor = color << 8;
1938 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1939 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1940 dstcolor |= 0xff;
1942 *(DWORD*)dest = dstcolor;
1943 source += 4;
1944 dest += 4;
1948 break;
1950 case CONVERT_V8U8:
1952 unsigned int x, y;
1953 const short *Source;
1954 unsigned char *Dest;
1955 for(y = 0; y < height; y++) {
1956 Source = (const short *)(src + y * pitch);
1957 Dest = dst + y * outpitch;
1958 for (x = 0; x < width; x++ ) {
1959 long color = (*Source++);
1960 /* B */ Dest[0] = 0xff;
1961 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1962 /* R */ Dest[2] = (color) + 128; /* U */
1963 Dest += 3;
1966 break;
1969 case CONVERT_V16U16:
1971 unsigned int x, y;
1972 const DWORD *Source;
1973 unsigned short *Dest;
1974 for(y = 0; y < height; y++) {
1975 Source = (const DWORD *)(src + y * pitch);
1976 Dest = (unsigned short *) (dst + y * outpitch);
1977 for (x = 0; x < width; x++ ) {
1978 DWORD color = (*Source++);
1979 /* B */ Dest[0] = 0xffff;
1980 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1981 /* R */ Dest[2] = (color ) + 32768; /* U */
1982 Dest += 3;
1985 break;
1988 case CONVERT_Q8W8V8U8:
1990 unsigned int x, y;
1991 const DWORD *Source;
1992 unsigned char *Dest;
1993 for(y = 0; y < height; y++) {
1994 Source = (const DWORD *)(src + y * pitch);
1995 Dest = dst + y * outpitch;
1996 for (x = 0; x < width; x++ ) {
1997 long color = (*Source++);
1998 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1999 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
2000 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
2001 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
2002 Dest += 4;
2005 break;
2008 case CONVERT_L6V5U5:
2010 unsigned int x, y;
2011 const WORD *Source;
2012 unsigned char *Dest;
2014 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
2015 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
2016 * fixed function and shaders without further conversion once the surface is
2017 * loaded
2019 for(y = 0; y < height; y++) {
2020 Source = (const WORD *)(src + y * pitch);
2021 Dest = dst + y * outpitch;
2022 for (x = 0; x < width; x++ ) {
2023 short color = (*Source++);
2024 unsigned char l = ((color >> 10) & 0xfc);
2025 char v = ((color >> 5) & 0x3e);
2026 char u = ((color ) & 0x1f);
2028 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
2029 * and doubles the positive range. Thus shift left only once, gl does the 2nd
2030 * shift. GL reads a signed value and converts it into an unsigned value.
2032 /* M */ Dest[2] = l << 1;
2034 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
2035 * from 5 bit values to 8 bit values.
2037 /* V */ Dest[1] = v << 3;
2038 /* U */ Dest[0] = u << 3;
2039 Dest += 3;
2042 } else {
2043 for(y = 0; y < height; y++) {
2044 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
2045 Source = (const WORD *)(src + y * pitch);
2046 for (x = 0; x < width; x++ ) {
2047 short color = (*Source++);
2048 unsigned char l = ((color >> 10) & 0xfc);
2049 short v = ((color >> 5) & 0x3e);
2050 short u = ((color ) & 0x1f);
2051 short v_conv = v + 16;
2052 short u_conv = u + 16;
2054 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
2055 Dest_s += 1;
2059 break;
2062 case CONVERT_X8L8V8U8:
2064 unsigned int x, y;
2065 const DWORD *Source;
2066 unsigned char *Dest;
2068 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
2069 /* This implementation works with the fixed function pipeline and shaders
2070 * without further modification after converting the surface.
2072 for(y = 0; y < height; y++) {
2073 Source = (const DWORD *)(src + y * pitch);
2074 Dest = dst + y * outpitch;
2075 for (x = 0; x < width; x++ ) {
2076 long color = (*Source++);
2077 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
2078 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
2079 /* U */ Dest[0] = (color & 0xff); /* U */
2080 /* I */ Dest[3] = 255; /* X */
2081 Dest += 4;
2084 } else {
2085 /* Doesn't work correctly with the fixed function pipeline, but can work in
2086 * shaders if the shader is adjusted. (There's no use for this format in gl's
2087 * standard fixed function pipeline anyway).
2089 for(y = 0; y < height; y++) {
2090 Source = (const DWORD *)(src + y * pitch);
2091 Dest = dst + y * outpitch;
2092 for (x = 0; x < width; x++ ) {
2093 long color = (*Source++);
2094 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
2095 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
2096 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
2097 Dest += 4;
2101 break;
2104 case CONVERT_A4L4:
2106 unsigned int x, y;
2107 const unsigned char *Source;
2108 unsigned char *Dest;
2109 for(y = 0; y < height; y++) {
2110 Source = src + y * pitch;
2111 Dest = dst + y * outpitch;
2112 for (x = 0; x < width; x++ ) {
2113 unsigned char color = (*Source++);
2114 /* A */ Dest[1] = (color & 0xf0) << 0;
2115 /* L */ Dest[0] = (color & 0x0f) << 4;
2116 Dest += 2;
2119 break;
2122 case CONVERT_G16R16:
2124 unsigned int x, y;
2125 const WORD *Source;
2126 WORD *Dest;
2128 for(y = 0; y < height; y++) {
2129 Source = (const WORD *)(src + y * pitch);
2130 Dest = (WORD *) (dst + y * outpitch);
2131 for (x = 0; x < width; x++ ) {
2132 WORD green = (*Source++);
2133 WORD red = (*Source++);
2134 Dest[0] = green;
2135 Dest[1] = red;
2136 Dest[2] = 0xffff;
2137 Dest += 3;
2140 break;
2143 default:
2144 ERR("Unsupported conversation type %d\n", convert);
2146 return WINED3D_OK;
2149 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2150 IWineD3DPaletteImpl* pal = This->palette;
2151 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2152 BOOL index_in_alpha = FALSE;
2153 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2154 unsigned int i;
2156 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2157 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2158 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2159 * duplicate entries. Store the color key in the unused alpha component to speed the
2160 * download up and to make conversion unneeded. */
2161 index_in_alpha = primary_render_target_is_p8(device);
2163 if (pal == NULL) {
2164 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2165 if(dxVersion <= 7) {
2166 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2167 if(index_in_alpha) {
2168 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2169 there's no palette at this time. */
2170 for (i = 0; i < 256; i++) table[i][3] = i;
2172 } else {
2173 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2174 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2175 capability flag is present (wine does advertise this capability) */
2176 for (i = 0; i < 256; i++) {
2177 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2178 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2179 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2180 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2183 } else {
2184 TRACE("Using surface palette %p\n", pal);
2185 /* Get the surface's palette */
2186 for (i = 0; i < 256; i++) {
2187 table[i][0] = pal->palents[i].peRed;
2188 table[i][1] = pal->palents[i].peGreen;
2189 table[i][2] = pal->palents[i].peBlue;
2191 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2192 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2193 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2194 of pixels that should be masked away is set to 0. */
2195 if(index_in_alpha) {
2196 table[i][3] = i;
2197 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2198 table[i][3] = 0x00;
2199 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2200 table[i][3] = pal->palents[i].peFlags;
2201 } else {
2202 table[i][3] = 0xFF;
2208 /* This function is used in case of 8bit paletted textures to upload the palette.
2209 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2210 extensions like ATI_fragment_shaders is possible.
2212 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2213 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2214 BYTE table[256][4];
2215 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2217 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2219 /* Try to use the paletted texture extension */
2220 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2222 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2223 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2225 else
2227 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2228 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2229 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2231 /* Create the fragment program if we don't have it */
2232 if(!device->paletteConversionShader)
2234 const char *fragment_palette_conversion =
2235 "!!ARBfp1.0\n"
2236 "TEMP index;\n"
2237 /* { 255/256, 0.5/255*255/256, 0, 0 } */
2238 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n"
2239 /* The alpha-component contains the palette index */
2240 "TEX index, fragment.texcoord[0], texture[0], 2D;\n"
2241 /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
2242 "MAD index.a, index.a, constants.x, constants.y;\n"
2243 /* Use the alpha-component as an index in the palette to get the final color */
2244 "TEX result.color, index.a, texture[1], 1D;\n"
2245 "END";
2247 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2248 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2249 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2250 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), fragment_palette_conversion));
2251 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2254 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2255 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2257 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2258 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2260 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2261 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2262 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2263 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2265 /* Switch back to unit 0 in which the 2D texture will be stored. */
2266 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2268 /* Rebind the texture because it isn't bound anymore */
2269 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2273 BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2274 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2276 if (This->palette || (This->resource.format_desc->format != WINED3DFMT_P8
2277 && This->resource.format_desc->format != WINED3DFMT_A8P8))
2279 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2280 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2282 return FALSE;
2285 if(This->palette9) {
2286 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2287 return FALSE;
2289 } else {
2290 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2292 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2293 return TRUE;
2296 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2297 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2298 DWORD flag = srgb_mode ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
2300 if (!(This->Flags & flag)) {
2301 TRACE("Reloading because surface is dirty\n");
2302 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2303 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2304 /* Reload: vice versa OR */
2305 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2306 /* Also reload: Color key is active AND the color key has changed */
2307 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2308 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2309 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2310 TRACE("Reloading because of color keying\n");
2311 /* To perform the color key conversion we need a sysmem copy of
2312 * the surface. Make sure we have it
2315 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2316 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2317 /* TODO: This is not necessarily needed with hw palettized texture support */
2318 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2319 } else {
2320 TRACE("surface is already in texture\n");
2321 return WINED3D_OK;
2324 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2325 * These resources are not bound by device size or format restrictions. Because of this,
2326 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2327 * However, these resources can always be created, locked, and copied.
2329 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2331 FIXME("(%p) Operation not supported for scratch textures\n",This);
2332 return WINED3DERR_INVALIDCALL;
2335 IWineD3DSurface_LoadLocation(iface, flag, NULL /* no partial locking for textures yet */);
2337 #if 0
2339 static unsigned int gen = 0;
2340 char buffer[4096];
2341 ++gen;
2342 if ((gen % 10) == 0) {
2343 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2344 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2347 * debugging crash code
2348 if (gen == 250) {
2349 void** test = NULL;
2350 *test = 0;
2354 #endif
2356 if (!(This->Flags & SFLAG_DONOTFREE)) {
2357 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2358 This->resource.allocatedMemory = NULL;
2359 This->resource.heapMemory = NULL;
2360 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2363 return WINED3D_OK;
2366 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface, BOOL srgb) {
2367 /* TODO: check for locks */
2368 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2369 IWineD3DBaseTexture *baseTexture = NULL;
2370 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2372 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2373 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2374 TRACE("Passing to container\n");
2375 IWineD3DBaseTexture_BindTexture(baseTexture, srgb);
2376 IWineD3DBaseTexture_Release(baseTexture);
2377 } else {
2378 GLuint *name;
2379 TRACE("(%p) : Binding surface\n", This);
2381 name = srgb ? &This->glDescription.srgbTextureName : &This->glDescription.textureName;
2382 if(!device->isInDraw) {
2383 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2386 ENTER_GL();
2388 if (!This->glDescription.level) {
2389 if (!*name) {
2390 glGenTextures(1, name);
2391 checkGLcall("glGenTextures");
2392 TRACE("Surface %p given name %d\n", This, *name);
2394 glBindTexture(This->glDescription.target, *name);
2395 checkGLcall("glBindTexture");
2396 glTexParameteri(This->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2397 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
2398 glTexParameteri(This->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
2399 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
2400 glTexParameteri(This->glDescription.target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
2401 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE)");
2402 glTexParameteri(This->glDescription.target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2403 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
2404 glTexParameteri(This->glDescription.target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2405 checkGLcall("glTexParameteri(dimension, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
2407 /* This is where we should be reducing the amount of GLMemoryUsed */
2408 } else if (*name) {
2409 /* Mipmap surfaces should have a base texture container */
2410 ERR("Mipmap surface has a glTexture bound to it!\n");
2413 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2414 checkGLcall("glBindTexture");
2416 LEAVE_GL();
2418 return;
2421 #include <errno.h>
2422 #include <stdio.h>
2423 static HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename)
2425 FILE* f = NULL;
2426 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2427 char *allocatedMemory;
2428 const char *textureRow;
2429 IWineD3DSwapChain *swapChain = NULL;
2430 int width, height, i, y;
2431 GLuint tmpTexture = 0;
2432 DWORD color;
2433 /*FIXME:
2434 Textures may not be stored in ->allocatedgMemory and a GlTexture
2435 so we should lock the surface before saving a snapshot, or at least check that
2437 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2438 by calling GetTexImage and in compressed form by calling
2439 GetCompressedTexImageARB. Queried compressed images can be saved and
2440 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2441 texture images do not need to be processed by the GL and should
2442 significantly improve texture loading performance relative to uncompressed
2443 images. */
2445 /* Setup the width and height to be the internal texture width and height. */
2446 width = This->pow2Width;
2447 height = This->pow2Height;
2448 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2449 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2451 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2452 /* if were not a real texture then read the back buffer into a real texture */
2453 /* we don't want to interfere with the back buffer so read the data into a temporary
2454 * texture and then save the data out of the temporary texture
2456 GLint prevRead;
2457 ENTER_GL();
2458 TRACE("(%p) Reading render target into texture\n", This);
2460 glGenTextures(1, &tmpTexture);
2461 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2463 glTexImage2D(GL_TEXTURE_2D,
2465 GL_RGBA,
2466 width,
2467 height,
2468 0/*border*/,
2469 GL_RGBA,
2470 GL_UNSIGNED_INT_8_8_8_8_REV,
2471 NULL);
2473 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2474 checkGLcall("glGetIntegerv");
2475 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2476 checkGLcall("glReadBuffer");
2477 glCopyTexImage2D(GL_TEXTURE_2D,
2479 GL_RGBA,
2482 width,
2483 height,
2486 checkGLcall("glCopyTexImage2D");
2487 glReadBuffer(prevRead);
2488 LEAVE_GL();
2490 } else { /* bind the real texture, and make sure it up to date */
2491 surface_internal_preload(iface, SRGB_RGB);
2492 surface_bind_and_dirtify(This, FALSE);
2494 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2495 ENTER_GL();
2496 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2497 glGetTexImage(GL_TEXTURE_2D,
2498 This->glDescription.level,
2499 GL_RGBA,
2500 GL_UNSIGNED_INT_8_8_8_8_REV,
2501 allocatedMemory);
2502 checkGLcall("glTexImage2D");
2503 if (tmpTexture) {
2504 glBindTexture(GL_TEXTURE_2D, 0);
2505 glDeleteTextures(1, &tmpTexture);
2507 LEAVE_GL();
2509 f = fopen(filename, "w+");
2510 if (NULL == f) {
2511 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2512 return WINED3DERR_INVALIDCALL;
2514 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2515 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format_desc->format));
2516 /* TGA header */
2517 fputc(0,f);
2518 fputc(0,f);
2519 fputc(2,f);
2520 fputc(0,f);
2521 fputc(0,f);
2522 fputc(0,f);
2523 fputc(0,f);
2524 fputc(0,f);
2525 fputc(0,f);
2526 fputc(0,f);
2527 fputc(0,f);
2528 fputc(0,f);
2529 /* short width*/
2530 fwrite(&width,2,1,f);
2531 /* short height */
2532 fwrite(&height,2,1,f);
2533 /* format rgba */
2534 fputc(0x20,f);
2535 fputc(0x28,f);
2536 /* raw data */
2537 /* 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 */
2538 if(swapChain)
2539 textureRow = allocatedMemory + (width * (height - 1) *4);
2540 else
2541 textureRow = allocatedMemory;
2542 for (y = 0 ; y < height; y++) {
2543 for (i = 0; i < width; i++) {
2544 color = *((const DWORD*)textureRow);
2545 fputc((color >> 16) & 0xFF, f); /* B */
2546 fputc((color >> 8) & 0xFF, f); /* G */
2547 fputc((color >> 0) & 0xFF, f); /* R */
2548 fputc((color >> 24) & 0xFF, f); /* A */
2549 textureRow += 4;
2551 /* take two rows of the pointer to the texture memory */
2552 if(swapChain)
2553 (textureRow-= width << 3);
2556 TRACE("Closing file\n");
2557 fclose(f);
2559 if(swapChain) {
2560 IWineD3DSwapChain_Release(swapChain);
2562 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2563 return WINED3D_OK;
2566 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2567 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2568 HRESULT hr;
2570 TRACE("(%p) : Calling base function first\n", This);
2571 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2572 if(SUCCEEDED(hr)) {
2573 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2574 TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format_desc->glFormat,
2575 This->resource.format_desc->glInternal, This->resource.format_desc->glType);
2577 return hr;
2580 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2581 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2583 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2584 WARN("Surface is locked or the HDC is in use\n");
2585 return WINED3DERR_INVALIDCALL;
2588 if(Mem && Mem != This->resource.allocatedMemory) {
2589 void *release = NULL;
2591 /* Do I have to copy the old surface content? */
2592 if(This->Flags & SFLAG_DIBSECTION) {
2593 /* Release the DC. No need to hold the critical section for the update
2594 * Thread because this thread runs only on front buffers, but this method
2595 * fails for render targets in the check above.
2597 SelectObject(This->hDC, This->dib.holdbitmap);
2598 DeleteDC(This->hDC);
2599 /* Release the DIB section */
2600 DeleteObject(This->dib.DIBsection);
2601 This->dib.bitmap_data = NULL;
2602 This->resource.allocatedMemory = NULL;
2603 This->hDC = NULL;
2604 This->Flags &= ~SFLAG_DIBSECTION;
2605 } else if(!(This->Flags & SFLAG_USERPTR)) {
2606 release = This->resource.heapMemory;
2607 This->resource.heapMemory = NULL;
2609 This->resource.allocatedMemory = Mem;
2610 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2612 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2613 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2615 /* For client textures opengl has to be notified */
2616 if(This->Flags & SFLAG_CLIENT) {
2617 DWORD oldFlags = This->Flags;
2618 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2619 if(oldFlags & SFLAG_ALLOCATED) surface_internal_preload(iface, SRGB_RGB);
2620 if(oldFlags & SFLAG_SRGBALLOCATED) surface_internal_preload(iface, SRGB_SRGB);
2621 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2624 /* Now free the old memory if any */
2625 HeapFree(GetProcessHeap(), 0, release);
2626 } else if(This->Flags & SFLAG_USERPTR) {
2627 /* LockRect and GetDC will re-create the dib section and allocated memory */
2628 This->resource.allocatedMemory = NULL;
2629 /* HeapMemory should be NULL already */
2630 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2631 This->Flags &= ~SFLAG_USERPTR;
2633 if(This->Flags & SFLAG_CLIENT) {
2634 DWORD oldFlags = This->Flags;
2635 This->Flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2636 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2637 if(oldFlags & SFLAG_ALLOCATED) surface_internal_preload(iface, SRGB_RGB);
2638 if(oldFlags & SFLAG_SRGBALLOCATED) surface_internal_preload(iface, SRGB_SRGB);
2641 return WINED3D_OK;
2644 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2646 /* Flip the surface contents */
2647 /* Flip the DC */
2649 HDC tmp;
2650 tmp = front->hDC;
2651 front->hDC = back->hDC;
2652 back->hDC = tmp;
2655 /* Flip the DIBsection */
2657 HBITMAP tmp;
2658 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
2659 tmp = front->dib.DIBsection;
2660 front->dib.DIBsection = back->dib.DIBsection;
2661 back->dib.DIBsection = tmp;
2663 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
2664 else front->Flags &= ~SFLAG_DIBSECTION;
2665 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
2666 else back->Flags &= ~SFLAG_DIBSECTION;
2669 /* Flip the surface data */
2671 void* tmp;
2673 tmp = front->dib.bitmap_data;
2674 front->dib.bitmap_data = back->dib.bitmap_data;
2675 back->dib.bitmap_data = tmp;
2677 tmp = front->resource.allocatedMemory;
2678 front->resource.allocatedMemory = back->resource.allocatedMemory;
2679 back->resource.allocatedMemory = tmp;
2681 tmp = front->resource.heapMemory;
2682 front->resource.heapMemory = back->resource.heapMemory;
2683 back->resource.heapMemory = tmp;
2686 /* Flip the PBO */
2688 GLuint tmp_pbo = front->pbo;
2689 front->pbo = back->pbo;
2690 back->pbo = tmp_pbo;
2693 /* client_memory should not be different, but just in case */
2695 BOOL tmp;
2696 tmp = front->dib.client_memory;
2697 front->dib.client_memory = back->dib.client_memory;
2698 back->dib.client_memory = tmp;
2701 /* Flip the opengl texture */
2703 glDescriptor tmp_desc = back->glDescription;
2704 back->glDescription = front->glDescription;
2705 front->glDescription = tmp_desc;
2709 DWORD tmp_flags = back->Flags;
2710 back->Flags = front->Flags;
2711 front->Flags = tmp_flags;
2715 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2716 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2717 IWineD3DSwapChainImpl *swapchain = NULL;
2718 HRESULT hr;
2719 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2721 /* Flipping is only supported on RenderTargets and overlays*/
2722 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2723 WARN("Tried to flip a non-render target, non-overlay surface\n");
2724 return WINEDDERR_NOTFLIPPABLE;
2727 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2728 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2730 /* Update the overlay if it is visible */
2731 if(This->overlay_dest) {
2732 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
2733 } else {
2734 return WINED3D_OK;
2738 if(override) {
2739 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2740 * FIXME("(%p) Target override is not supported by now\n", This);
2741 * Additionally, it isn't really possible to support triple-buffering
2742 * properly on opengl at all
2746 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2747 if(!swapchain) {
2748 ERR("Flipped surface is not on a swapchain\n");
2749 return WINEDDERR_NOTFLIPPABLE;
2752 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2753 * and only d3d8 and d3d9 apps specify the presentation interval
2755 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2756 /* Most common case first to avoid wasting time on all the other cases */
2757 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2758 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2759 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2760 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2761 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2762 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2763 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2764 } else {
2765 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2768 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2769 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2770 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2771 return hr;
2774 /* Does a direct frame buffer -> texture copy. Stretching is done
2775 * with single pixel copy calls
2777 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface,
2778 IWineD3DSwapChainImpl *swapchain, const WINED3DRECT *srect, const WINED3DRECT *drect,
2779 BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter)
2781 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2782 float xrel, yrel;
2783 UINT row;
2784 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2787 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2788 surface_internal_preload((IWineD3DSurface *) This, SRGB_RGB);
2789 ENTER_GL();
2791 /* Bind the target texture */
2792 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2793 checkGLcall("glBindTexture");
2794 if(!swapchain) {
2795 TRACE("Reading from an offscreen target\n");
2796 upsidedown = !upsidedown;
2797 glReadBuffer(myDevice->offscreenBuffer);
2798 } else {
2799 GLenum buffer = surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain);
2800 glReadBuffer(buffer);
2802 checkGLcall("glReadBuffer");
2804 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2805 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2807 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2808 FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2810 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2811 ERR("Texture filtering not supported in direct blit\n");
2813 } else if((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) && ((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2814 ERR("Texture filtering not supported in direct blit\n");
2817 if(upsidedown &&
2818 !((xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) &&
2819 !((yrel - 1.0 < -eps) || (yrel - 1.0 > eps))) {
2820 /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2822 glCopyTexSubImage2D(This->glDescription.target,
2823 This->glDescription.level,
2824 drect->x1, drect->y1, /* xoffset, yoffset */
2825 srect->x1, Src->currentDesc.Height - srect->y2,
2826 drect->x2 - drect->x1, drect->y2 - drect->y1);
2827 } else {
2828 UINT yoffset = Src->currentDesc.Height - srect->y1 + drect->y1 - 1;
2829 /* I have to process this row by row to swap the image,
2830 * otherwise it would be upside down, so stretching in y direction
2831 * doesn't cost extra time
2833 * However, stretching in x direction can be avoided if not necessary
2835 for(row = drect->y1; row < drect->y2; row++) {
2836 if( (xrel - 1.0 < -eps) || (xrel - 1.0 > eps)) {
2837 /* Well, that stuff works, but it's very slow.
2838 * find a better way instead
2840 UINT col;
2842 for(col = drect->x1; col < drect->x2; col++) {
2843 glCopyTexSubImage2D(This->glDescription.target,
2844 This->glDescription.level,
2845 drect->x1 + col, row, /* xoffset, yoffset */
2846 srect->x1 + col * xrel, yoffset - (int) (row * yrel),
2847 1, 1);
2849 } else {
2850 glCopyTexSubImage2D(This->glDescription.target,
2851 This->glDescription.level,
2852 drect->x1, row, /* xoffset, yoffset */
2853 srect->x1, yoffset - (int) (row * yrel),
2854 drect->x2-drect->x1, 1);
2858 checkGLcall("glCopyTexSubImage2D");
2860 LEAVE_GL();
2863 /* Uses the hardware to stretch and flip the image */
2864 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface,
2865 IWineD3DSwapChainImpl *swapchain, const WINED3DRECT *srect, const WINED3DRECT *drect,
2866 BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter)
2868 GLuint src, backup = 0;
2869 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2870 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2871 float left, right, top, bottom; /* Texture coordinates */
2872 UINT fbwidth = Src->currentDesc.Width;
2873 UINT fbheight = Src->currentDesc.Height;
2874 GLenum drawBuffer = GL_BACK;
2875 GLenum texture_target;
2876 BOOL noBackBufferBackup;
2878 TRACE("Using hwstretch blit\n");
2879 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2880 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2881 surface_internal_preload((IWineD3DSurface *) This, SRGB_RGB);
2883 noBackBufferBackup = !swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2884 if(!noBackBufferBackup && Src->glDescription.textureName == 0) {
2885 /* Get it a description */
2886 surface_internal_preload(SrcSurface, SRGB_RGB);
2888 ENTER_GL();
2890 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2891 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2893 if(myDevice->activeContext->aux_buffers >= 2) {
2894 /* Got more than one aux buffer? Use the 2nd aux buffer */
2895 drawBuffer = GL_AUX1;
2896 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && myDevice->activeContext->aux_buffers >= 1) {
2897 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2898 drawBuffer = GL_AUX0;
2901 if(noBackBufferBackup) {
2902 glGenTextures(1, &backup);
2903 checkGLcall("glGenTextures\n");
2904 glBindTexture(GL_TEXTURE_2D, backup);
2905 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2906 texture_target = GL_TEXTURE_2D;
2907 } else {
2908 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2909 * we are reading from the back buffer, the backup can be used as source texture
2911 texture_target = Src->glDescription.target;
2912 glBindTexture(texture_target, Src->glDescription.textureName);
2913 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2914 glEnable(texture_target);
2915 checkGLcall("glEnable(texture_target)");
2917 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2918 Src->Flags &= ~SFLAG_INTEXTURE;
2921 if(swapchain) {
2922 glReadBuffer(surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain));
2923 } else {
2924 TRACE("Reading from an offscreen target\n");
2925 upsidedown = !upsidedown;
2926 glReadBuffer(myDevice->offscreenBuffer);
2929 /* TODO: Only back up the part that will be overwritten */
2930 glCopyTexSubImage2D(texture_target, 0,
2931 0, 0 /* read offsets */,
2932 0, 0,
2933 fbwidth,
2934 fbheight);
2936 checkGLcall("glCopyTexSubImage2D");
2938 /* No issue with overriding these - the sampler is dirty due to blit usage */
2939 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2940 magLookup[Filter - WINED3DTEXF_NONE]);
2941 checkGLcall("glTexParameteri");
2942 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2943 minMipLookup[Filter].mip[WINED3DTEXF_NONE]);
2944 checkGLcall("glTexParameteri");
2946 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2947 src = backup ? backup : Src->glDescription.textureName;
2948 } else {
2949 glReadBuffer(GL_FRONT);
2950 checkGLcall("glReadBuffer(GL_FRONT)");
2952 glGenTextures(1, &src);
2953 checkGLcall("glGenTextures(1, &src)");
2954 glBindTexture(GL_TEXTURE_2D, src);
2955 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2957 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2958 * out for power of 2 sizes
2960 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2961 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2962 checkGLcall("glTexImage2D");
2963 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2964 0, 0 /* read offsets */,
2965 0, 0,
2966 fbwidth,
2967 fbheight);
2969 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2970 checkGLcall("glTexParameteri");
2971 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2972 checkGLcall("glTexParameteri");
2974 glReadBuffer(GL_BACK);
2975 checkGLcall("glReadBuffer(GL_BACK)");
2977 if(texture_target != GL_TEXTURE_2D) {
2978 glDisable(texture_target);
2979 glEnable(GL_TEXTURE_2D);
2980 texture_target = GL_TEXTURE_2D;
2983 checkGLcall("glEnd and previous");
2985 left = srect->x1;
2986 right = srect->x2;
2988 if(upsidedown) {
2989 top = Src->currentDesc.Height - srect->y1;
2990 bottom = Src->currentDesc.Height - srect->y2;
2991 } else {
2992 top = Src->currentDesc.Height - srect->y2;
2993 bottom = Src->currentDesc.Height - srect->y1;
2996 if(Src->Flags & SFLAG_NORMCOORD) {
2997 left /= Src->pow2Width;
2998 right /= Src->pow2Width;
2999 top /= Src->pow2Height;
3000 bottom /= Src->pow2Height;
3003 /* draw the source texture stretched and upside down. The correct surface is bound already */
3004 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3005 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3007 glDrawBuffer(drawBuffer);
3008 glReadBuffer(drawBuffer);
3010 glBegin(GL_QUADS);
3011 /* bottom left */
3012 glTexCoord2f(left, bottom);
3013 glVertex2i(0, fbheight);
3015 /* top left */
3016 glTexCoord2f(left, top);
3017 glVertex2i(0, fbheight - drect->y2 - drect->y1);
3019 /* top right */
3020 glTexCoord2f(right, top);
3021 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
3023 /* bottom right */
3024 glTexCoord2f(right, bottom);
3025 glVertex2i(drect->x2 - drect->x1, fbheight);
3026 glEnd();
3027 checkGLcall("glEnd and previous");
3029 if(texture_target != This->glDescription.target) {
3030 glDisable(texture_target);
3031 glEnable(This->glDescription.target);
3032 texture_target = This->glDescription.target;
3035 /* Now read the stretched and upside down image into the destination texture */
3036 glBindTexture(texture_target, This->glDescription.textureName);
3037 checkGLcall("glBindTexture");
3038 glCopyTexSubImage2D(texture_target,
3040 drect->x1, drect->y1, /* xoffset, yoffset */
3041 0, 0, /* We blitted the image to the origin */
3042 drect->x2 - drect->x1, drect->y2 - drect->y1);
3043 checkGLcall("glCopyTexSubImage2D");
3045 if(drawBuffer == GL_BACK) {
3046 /* Write the back buffer backup back */
3047 if(backup) {
3048 if(texture_target != GL_TEXTURE_2D) {
3049 glDisable(texture_target);
3050 glEnable(GL_TEXTURE_2D);
3051 texture_target = GL_TEXTURE_2D;
3053 glBindTexture(GL_TEXTURE_2D, backup);
3054 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3055 } else {
3056 if(texture_target != Src->glDescription.target) {
3057 glDisable(texture_target);
3058 glEnable(Src->glDescription.target);
3059 texture_target = Src->glDescription.target;
3061 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3062 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
3065 glBegin(GL_QUADS);
3066 /* top left */
3067 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
3068 glVertex2i(0, 0);
3070 /* bottom left */
3071 glTexCoord2f(0.0, 0.0);
3072 glVertex2i(0, fbheight);
3074 /* bottom right */
3075 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
3076 glVertex2i(fbwidth, Src->currentDesc.Height);
3078 /* top right */
3079 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
3080 glVertex2i(fbwidth, 0);
3081 glEnd();
3082 } else {
3083 /* Restore the old draw buffer */
3084 glDrawBuffer(GL_BACK);
3086 glDisable(texture_target);
3087 checkGLcall("glDisable(texture_target)");
3089 /* Cleanup */
3090 if(src != Src->glDescription.textureName && src != backup) {
3091 glDeleteTextures(1, &src);
3092 checkGLcall("glDeleteTextures(1, &src)");
3094 if(backup) {
3095 glDeleteTextures(1, &backup);
3096 checkGLcall("glDeleteTextures(1, &backup)");
3099 LEAVE_GL();
3102 /* Not called from the VTable */
3103 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3104 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx,
3105 WINED3DTEXTUREFILTERTYPE Filter)
3107 WINED3DRECT rect;
3108 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3109 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3110 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3112 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3114 /* Get the swapchain. One of the surfaces has to be a primary surface */
3115 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3116 WARN("Destination is in sysmem, rejecting gl blt\n");
3117 return WINED3DERR_INVALIDCALL;
3119 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3120 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
3121 if(Src) {
3122 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3123 WARN("Src is in sysmem, rejecting gl blt\n");
3124 return WINED3DERR_INVALIDCALL;
3126 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3127 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3130 /* Early sort out of cases where no render target is used */
3131 if(!dstSwapchain && !srcSwapchain &&
3132 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3133 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3134 return WINED3DERR_INVALIDCALL;
3137 /* No destination color keying supported */
3138 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3139 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3140 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3141 return WINED3DERR_INVALIDCALL;
3144 if (DestRect) {
3145 rect.x1 = DestRect->left;
3146 rect.y1 = DestRect->top;
3147 rect.x2 = DestRect->right;
3148 rect.y2 = DestRect->bottom;
3149 } else {
3150 rect.x1 = 0;
3151 rect.y1 = 0;
3152 rect.x2 = This->currentDesc.Width;
3153 rect.y2 = This->currentDesc.Height;
3156 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3157 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3158 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3159 /* Half-life does a Blt from the back buffer to the front buffer,
3160 * Full surface size, no flags... Use present instead
3162 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3165 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3166 while(1)
3168 RECT mySrcRect;
3169 TRACE("Looking if a Present can be done...\n");
3170 /* Source Rectangle must be full surface */
3171 if( SrcRect ) {
3172 if(SrcRect->left != 0 || SrcRect->top != 0 ||
3173 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
3174 TRACE("No, Source rectangle doesn't match\n");
3175 break;
3178 mySrcRect.left = 0;
3179 mySrcRect.top = 0;
3180 mySrcRect.right = Src->currentDesc.Width;
3181 mySrcRect.bottom = Src->currentDesc.Height;
3183 /* No stretching may occur */
3184 if(mySrcRect.right != rect.x2 - rect.x1 ||
3185 mySrcRect.bottom != rect.y2 - rect.y1) {
3186 TRACE("No, stretching is done\n");
3187 break;
3190 /* Destination must be full surface or match the clipping rectangle */
3191 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3193 RECT cliprect;
3194 POINT pos[2];
3195 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3196 pos[0].x = rect.x1;
3197 pos[0].y = rect.y1;
3198 pos[1].x = rect.x2;
3199 pos[1].y = rect.y2;
3200 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3201 pos, 2);
3203 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3204 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3206 TRACE("No, dest rectangle doesn't match(clipper)\n");
3207 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3208 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3209 break;
3212 else
3214 if(rect.x1 != 0 || rect.y1 != 0 ||
3215 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3216 TRACE("No, dest rectangle doesn't match(surface size)\n");
3217 break;
3221 TRACE("Yes\n");
3223 /* These flags are unimportant for the flag check, remove them */
3224 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3225 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3227 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3228 * take very long, while a flip is fast.
3229 * This applies to Half-Life, which does such Blts every time it finished
3230 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3231 * menu. This is also used by all apps when they do windowed rendering
3233 * The problem is that flipping is not really the same as copying. After a
3234 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3235 * untouched. Therefore it's necessary to override the swap effect
3236 * and to set it back after the flip.
3238 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3239 * testcases.
3242 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3243 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3245 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3246 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3248 dstSwapchain->presentParms.SwapEffect = orig_swap;
3250 return WINED3D_OK;
3252 break;
3255 TRACE("Unsupported blit between buffers on the same swapchain\n");
3256 return WINED3DERR_INVALIDCALL;
3257 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3258 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3259 return WINED3DERR_INVALIDCALL;
3260 } else if(dstSwapchain && srcSwapchain) {
3261 FIXME("Implement hardware blit between two different swapchains\n");
3262 return WINED3DERR_INVALIDCALL;
3263 } else if(dstSwapchain) {
3264 if(SrcSurface == myDevice->render_targets[0]) {
3265 TRACE("Blit from active render target to a swapchain\n");
3266 /* Handled with regular texture -> swapchain blit */
3268 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3269 FIXME("Implement blit from a swapchain to the active render target\n");
3270 return WINED3DERR_INVALIDCALL;
3273 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3274 /* Blit from render target to texture */
3275 WINED3DRECT srect;
3276 BOOL upsideDown, stretchx;
3277 BOOL paletteOverride = FALSE;
3279 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3280 TRACE("Color keying not supported by frame buffer to texture blit\n");
3281 return WINED3DERR_INVALIDCALL;
3282 /* Destination color key is checked above */
3285 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3286 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3288 if(SrcRect) {
3289 if(SrcRect->top < SrcRect->bottom) {
3290 srect.y1 = SrcRect->top;
3291 srect.y2 = SrcRect->bottom;
3292 upsideDown = FALSE;
3293 } else {
3294 srect.y1 = SrcRect->bottom;
3295 srect.y2 = SrcRect->top;
3296 upsideDown = TRUE;
3298 srect.x1 = SrcRect->left;
3299 srect.x2 = SrcRect->right;
3300 } else {
3301 srect.x1 = 0;
3302 srect.y1 = 0;
3303 srect.x2 = Src->currentDesc.Width;
3304 srect.y2 = Src->currentDesc.Height;
3305 upsideDown = FALSE;
3307 if(rect.x1 > rect.x2) {
3308 UINT tmp = rect.x2;
3309 rect.x2 = rect.x1;
3310 rect.x1 = tmp;
3311 upsideDown = !upsideDown;
3314 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3315 stretchx = TRUE;
3316 } else {
3317 stretchx = FALSE;
3320 /* When blitting from a render target a texture, the texture isn't required to have a palette.
3321 * In this case grab the palette from the render target. */
3322 if ((This->resource.format_desc->format == WINED3DFMT_P8) && (This->palette == NULL))
3324 paletteOverride = TRUE;
3325 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3326 This->palette = Src->palette;
3329 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3330 * flip the image nor scale it.
3332 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3333 * -> If the app wants a image width an unscaled width, copy it line per line
3334 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3335 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3336 * back buffer. This is slower than reading line per line, thus not used for flipping
3337 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3338 * pixel by pixel
3340 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3341 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3342 * backends.
3344 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3345 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3346 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3347 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3348 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3349 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3350 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3351 } else {
3352 TRACE("Using hardware stretching to flip / stretch the texture\n");
3353 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3356 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3357 if(paletteOverride)
3358 This->palette = NULL;
3360 if(!(This->Flags & SFLAG_DONOTFREE)) {
3361 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3362 This->resource.allocatedMemory = NULL;
3363 This->resource.heapMemory = NULL;
3364 } else {
3365 This->Flags &= ~SFLAG_INSYSMEM;
3367 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3368 * path is never entered
3370 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3372 return WINED3D_OK;
3373 } else if(Src) {
3374 /* Blit from offscreen surface to render target */
3375 float glTexCoord[4];
3376 DWORD oldCKeyFlags = Src->CKeyFlags;
3377 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3378 RECT SourceRectangle;
3379 BOOL paletteOverride = FALSE;
3381 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3383 if(SrcRect) {
3384 SourceRectangle.left = SrcRect->left;
3385 SourceRectangle.right = SrcRect->right;
3386 SourceRectangle.top = SrcRect->top;
3387 SourceRectangle.bottom = SrcRect->bottom;
3388 } else {
3389 SourceRectangle.left = 0;
3390 SourceRectangle.right = Src->currentDesc.Width;
3391 SourceRectangle.top = 0;
3392 SourceRectangle.bottom = Src->currentDesc.Height;
3395 /* When blitting from an offscreen surface to a rendertarget, the source
3396 * surface is not required to have a palette. Our rendering / conversion
3397 * code further down the road retrieves the palette from the surface, so
3398 * it must have a palette set. */
3399 if ((Src->resource.format_desc->format == WINED3DFMT_P8) && (Src->palette == NULL))
3401 paletteOverride = TRUE;
3402 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3403 Src->palette = This->palette;
3406 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3407 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3408 TRACE("Using stretch_rect_fbo\n");
3409 /* The source is always a texture, but never the currently active render target, and the texture
3410 * contents are never upside down
3412 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3413 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3415 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3416 if(paletteOverride)
3417 Src->palette = NULL;
3418 return WINED3D_OK;
3421 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3422 /* Fall back to software */
3423 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3424 SourceRectangle.left, SourceRectangle.top,
3425 SourceRectangle.right, SourceRectangle.bottom);
3426 return WINED3DERR_INVALIDCALL;
3429 /* Color keying: Check if we have to do a color keyed blt,
3430 * and if not check if a color key is activated.
3432 * Just modify the color keying parameters in the surface and restore them afterwards
3433 * The surface keeps track of the color key last used to load the opengl surface.
3434 * PreLoad will catch the change to the flags and color key and reload if necessary.
3436 if(Flags & WINEDDBLT_KEYSRC) {
3437 /* Use color key from surface */
3438 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3439 /* Use color key from DDBltFx */
3440 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3441 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3442 } else {
3443 /* Do not use color key */
3444 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3447 /* Now load the surface */
3448 surface_internal_preload((IWineD3DSurface *) Src, SRGB_RGB);
3450 /* Activate the destination context, set it up for blitting */
3451 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3453 /* The coordinates of the ddraw front buffer are always fullscreen ('screen coordinates',
3454 * while OpenGL coordinates are window relative.
3455 * Also beware of the origin difference(top left vs bottom left).
3456 * Also beware that the front buffer's surface size is screen width x screen height,
3457 * whereas the real gl drawable size is the size of the window.
3459 if (dstSwapchain && (IWineD3DSurface *)This == dstSwapchain->frontBuffer) {
3460 RECT windowsize;
3461 POINT offset = {0,0};
3462 UINT h;
3463 ClientToScreen(dstSwapchain->win_handle, &offset);
3464 GetClientRect(dstSwapchain->win_handle, &windowsize);
3465 h = windowsize.bottom - windowsize.top;
3466 rect.x1 -= offset.x; rect.x2 -=offset.x;
3467 rect.y1 -= offset.y; rect.y2 -=offset.y;
3468 rect.y1 += This->currentDesc.Height - h; rect.y2 += This->currentDesc.Height - h;
3471 myDevice->blitter->set_shader((IWineD3DDevice *) myDevice, Src->resource.format_desc,
3472 Src->glDescription.target, Src->pow2Width, Src->pow2Height);
3474 ENTER_GL();
3476 /* Bind the texture */
3477 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3478 checkGLcall("glBindTexture");
3480 /* Filtering for StretchRect */
3481 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3482 magLookup[Filter - WINED3DTEXF_NONE]);
3483 checkGLcall("glTexParameteri");
3484 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3485 minMipLookup[Filter].mip[WINED3DTEXF_NONE]);
3486 checkGLcall("glTexParameteri");
3487 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3488 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3489 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3490 checkGLcall("glTexEnvi");
3492 /* This is for color keying */
3493 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3494 glEnable(GL_ALPHA_TEST);
3495 checkGLcall("glEnable GL_ALPHA_TEST");
3497 /* When the primary render target uses P8, the alpha component contains the palette index.
3498 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3499 * should be masked away have alpha set to 0. */
3500 if(primary_render_target_is_p8(myDevice))
3501 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3502 else
3503 glAlphaFunc(GL_NOTEQUAL, 0.0);
3504 checkGLcall("glAlphaFunc\n");
3505 } else {
3506 glDisable(GL_ALPHA_TEST);
3507 checkGLcall("glDisable GL_ALPHA_TEST");
3510 /* Draw a textured quad
3512 glBegin(GL_QUADS);
3514 glColor3d(1.0f, 1.0f, 1.0f);
3515 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3516 glVertex3f(rect.x1,
3517 rect.y1,
3518 0.0);
3520 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3521 glVertex3f(rect.x1, rect.y2, 0.0);
3523 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3524 glVertex3f(rect.x2,
3525 rect.y2,
3526 0.0);
3528 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3529 glVertex3f(rect.x2,
3530 rect.y1,
3531 0.0);
3532 glEnd();
3533 checkGLcall("glEnd");
3535 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3536 glDisable(GL_ALPHA_TEST);
3537 checkGLcall("glDisable(GL_ALPHA_TEST)");
3540 glBindTexture(Src->glDescription.target, 0);
3541 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3543 /* Restore the color key parameters */
3544 Src->CKeyFlags = oldCKeyFlags;
3545 Src->SrcBltCKey = oldBltCKey;
3547 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3548 if(paletteOverride)
3549 Src->palette = NULL;
3551 LEAVE_GL();
3553 /* Leave the opengl state valid for blitting */
3554 myDevice->blitter->unset_shader((IWineD3DDevice *) myDevice);
3556 /* Flush in case the drawable is used by multiple GL contexts */
3557 if(dstSwapchain && (This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer || dstSwapchain->num_contexts >= 2))
3558 glFlush();
3560 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3561 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3562 * is outdated now
3564 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3566 return WINED3D_OK;
3567 } else {
3568 /* Source-Less Blit to render target */
3569 if (Flags & WINEDDBLT_COLORFILL) {
3570 /* This is easy to handle for the D3D Device... */
3571 DWORD color;
3573 TRACE("Colorfill\n");
3575 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3576 must be true if we are here */
3577 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3578 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3579 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3580 TRACE("Surface is higher back buffer, falling back to software\n");
3581 return WINED3DERR_INVALIDCALL;
3584 /* The color as given in the Blt function is in the format of the frame-buffer...
3585 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3587 if (This->resource.format_desc->format == WINED3DFMT_P8)
3589 DWORD alpha;
3591 if (primary_render_target_is_p8(myDevice)) alpha = DDBltFx->u5.dwFillColor << 24;
3592 else alpha = 0xFF000000;
3594 if (This->palette) {
3595 color = (alpha |
3596 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3597 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3598 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3599 } else {
3600 color = alpha;
3603 else if (This->resource.format_desc->format == WINED3DFMT_R5G6B5)
3605 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3606 color = 0xFFFFFFFF;
3607 } else {
3608 color = ((0xFF000000) |
3609 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3610 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3611 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3614 else if ((This->resource.format_desc->format == WINED3DFMT_R8G8B8)
3615 || (This->resource.format_desc->format == WINED3DFMT_X8R8G8B8))
3617 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3619 else if (This->resource.format_desc->format == WINED3DFMT_A8R8G8B8)
3621 color = DDBltFx->u5.dwFillColor;
3623 else {
3624 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3625 return WINED3DERR_INVALIDCALL;
3628 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3629 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3630 1, /* Number of rectangles */
3631 &rect, WINED3DCLEAR_TARGET, color,
3632 0.0 /* Z */,
3633 0 /* Stencil */);
3634 return WINED3D_OK;
3638 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3639 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3640 return WINED3DERR_INVALIDCALL;
3643 static HRESULT IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, const RECT *DestRect,
3644 IWineD3DSurface *SrcSurface, const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx)
3646 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3647 float depth;
3649 if (Flags & WINEDDBLT_DEPTHFILL) {
3650 switch(This->resource.format_desc->format)
3652 case WINED3DFMT_D16_UNORM:
3653 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3654 break;
3655 case WINED3DFMT_D15S1:
3656 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3657 break;
3658 case WINED3DFMT_D24S8:
3659 case WINED3DFMT_D24X8:
3660 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3661 break;
3662 case WINED3DFMT_D32:
3663 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3664 break;
3665 default:
3666 depth = 0.0;
3667 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format_desc->format));
3670 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3671 DestRect == NULL ? 0 : 1,
3672 (const WINED3DRECT *)DestRect,
3673 WINED3DCLEAR_ZBUFFER,
3674 0x00000000,
3675 depth,
3676 0x00000000);
3679 FIXME("(%p): Unsupp depthstencil blit\n", This);
3680 return WINED3DERR_INVALIDCALL;
3683 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect, IWineD3DSurface *SrcSurface,
3684 const RECT *SrcRect, DWORD Flags, const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3685 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3686 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3687 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3688 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3689 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3691 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3693 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3694 return WINEDDERR_SURFACEBUSY;
3697 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3698 * except depth blits, which seem to work
3700 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3701 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3702 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3703 return WINED3DERR_INVALIDCALL;
3704 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3705 TRACE("Z Blit override handled the blit\n");
3706 return WINED3D_OK;
3710 /* Special cases for RenderTargets */
3711 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3712 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3713 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3716 /* For the rest call the X11 surface implementation.
3717 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3718 * other Blts are rather rare
3720 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3723 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
3724 IWineD3DSurface *Source, const RECT *rsrc, DWORD trans)
3726 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3727 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3728 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3729 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3731 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3733 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3734 return WINEDDERR_SURFACEBUSY;
3737 if(myDevice->inScene &&
3738 (iface == myDevice->stencilBufferTarget ||
3739 (Source && Source == myDevice->stencilBufferTarget))) {
3740 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3741 return WINED3DERR_INVALIDCALL;
3744 /* Special cases for RenderTargets */
3745 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3746 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3748 RECT SrcRect, DstRect;
3749 DWORD Flags=0;
3751 if(rsrc) {
3752 SrcRect.left = rsrc->left;
3753 SrcRect.top= rsrc->top;
3754 SrcRect.bottom = rsrc->bottom;
3755 SrcRect.right = rsrc->right;
3756 } else {
3757 SrcRect.left = 0;
3758 SrcRect.top = 0;
3759 SrcRect.right = srcImpl->currentDesc.Width;
3760 SrcRect.bottom = srcImpl->currentDesc.Height;
3763 DstRect.left = dstx;
3764 DstRect.top=dsty;
3765 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3766 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3768 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3769 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3770 Flags |= WINEDDBLT_KEYSRC;
3771 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3772 Flags |= WINEDDBLT_KEYDEST;
3773 if(trans & WINEDDBLTFAST_WAIT)
3774 Flags |= WINEDDBLT_WAIT;
3775 if(trans & WINEDDBLTFAST_DONOTWAIT)
3776 Flags |= WINEDDBLT_DONOTWAIT;
3778 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3782 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3785 static HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface)
3787 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3788 RGBQUAD col[256];
3789 IWineD3DPaletteImpl *pal = This->palette;
3790 unsigned int n;
3791 TRACE("(%p)\n", This);
3793 if (!pal) return WINED3D_OK;
3795 if (This->resource.format_desc->format == WINED3DFMT_P8
3796 || This->resource.format_desc->format == WINED3DFMT_A8P8)
3798 int bpp;
3799 GLenum format, internal, type;
3800 CONVERT_TYPES convert;
3802 /* Check if we are using a RTL mode which uses texturing for uploads */
3803 BOOL use_texture = (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX);
3805 /* Check if we have hardware palette conversion if we have convert is set to NO_CONVERSION */
3806 d3dfmt_get_conv(This, TRUE, use_texture, &format, &internal, &type, &convert, &bpp, FALSE);
3808 if((This->resource.usage & WINED3DUSAGE_RENDERTARGET) && (convert == NO_CONVERSION))
3810 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3811 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3813 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3814 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
3816 /* Re-upload the palette */
3817 d3dfmt_p8_upload_palette(iface, convert);
3818 } else {
3819 if(!(This->Flags & SFLAG_INSYSMEM)) {
3820 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3821 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3823 TRACE("Dirtifying surface\n");
3824 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3828 if(This->Flags & SFLAG_DIBSECTION) {
3829 TRACE("(%p): Updating the hdc's palette\n", This);
3830 for (n=0; n<256; n++) {
3831 col[n].rgbRed = pal->palents[n].peRed;
3832 col[n].rgbGreen = pal->palents[n].peGreen;
3833 col[n].rgbBlue = pal->palents[n].peBlue;
3834 col[n].rgbReserved = 0;
3836 SetDIBColorTable(This->hDC, 0, 256, col);
3839 /* Propagate the changes to the drawable when we have a palette. */
3840 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3841 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3843 return WINED3D_OK;
3846 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3847 /** Check against the maximum texture sizes supported by the video card **/
3848 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3849 unsigned int pow2Width, pow2Height;
3851 This->glDescription.textureName = 0;
3852 This->glDescription.target = GL_TEXTURE_2D;
3854 /* Non-power2 support */
3855 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO) || GL_SUPPORT(WINE_NORMALIZED_TEXRECT)) {
3856 pow2Width = This->currentDesc.Width;
3857 pow2Height = This->currentDesc.Height;
3858 } else {
3859 /* Find the nearest pow2 match */
3860 pow2Width = pow2Height = 1;
3861 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3862 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3864 This->pow2Width = pow2Width;
3865 This->pow2Height = pow2Height;
3867 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3868 WINED3DFORMAT Format = This->resource.format_desc->format;
3869 /** TODO: add support for non power two compressed textures **/
3870 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3871 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5
3872 || Format == WINED3DFMT_ATI2N)
3874 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3875 This, This->currentDesc.Width, This->currentDesc.Height);
3876 return WINED3DERR_NOTAVAILABLE;
3880 if(pow2Width != This->currentDesc.Width ||
3881 pow2Height != This->currentDesc.Height) {
3882 This->Flags |= SFLAG_NONPOW2;
3885 TRACE("%p\n", This);
3886 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3887 /* one of three options
3888 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)
3889 2: Set the texture to the maximum size (bad idea)
3890 3: WARN and return WINED3DERR_NOTAVAILABLE;
3891 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.
3893 WARN("(%p) Creating an oversized surface: %ux%u (texture is %ux%u)\n",
3894 This, This->pow2Width, This->pow2Height, This->currentDesc.Width, This->currentDesc.Height);
3895 This->Flags |= SFLAG_OVERSIZE;
3897 /* This will be initialized on the first blt */
3898 This->glRect.left = 0;
3899 This->glRect.top = 0;
3900 This->glRect.right = 0;
3901 This->glRect.bottom = 0;
3902 } else {
3903 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3904 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3905 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3906 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3908 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE)
3909 && !((This->resource.format_desc->format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE)
3910 && (wined3d_settings.rendertargetlock_mode == RTL_READTEX
3911 || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3913 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3914 This->pow2Width = This->currentDesc.Width;
3915 This->pow2Height = This->currentDesc.Height;
3916 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
3919 /* No oversize, gl rect is the full texture size */
3920 This->Flags &= ~SFLAG_OVERSIZE;
3921 This->glRect.left = 0;
3922 This->glRect.top = 0;
3923 This->glRect.right = This->pow2Width;
3924 This->glRect.bottom = This->pow2Height;
3927 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3928 switch(wined3d_settings.offscreen_rendering_mode) {
3929 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3930 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3931 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3935 This->Flags |= SFLAG_INSYSMEM;
3937 return WINED3D_OK;
3940 struct depth_blt_info
3942 GLenum binding;
3943 GLenum bind_target;
3944 enum tex_types tex_type;
3945 GLfloat coords[4][3];
3948 static void surface_get_depth_blt_info(GLenum target, GLsizei w, GLsizei h, struct depth_blt_info *info)
3950 GLfloat (*coords)[3] = info->coords;
3952 switch (target)
3954 default:
3955 FIXME("Unsupported texture target %#x\n", target);
3956 /* Fall back to GL_TEXTURE_2D */
3957 case GL_TEXTURE_2D:
3958 info->binding = GL_TEXTURE_BINDING_2D;
3959 info->bind_target = GL_TEXTURE_2D;
3960 info->tex_type = tex_2d;
3961 coords[0][0] = 0.0f; coords[0][1] = 1.0f; coords[0][2] = 0.0f;
3962 coords[1][0] = 1.0f; coords[1][1] = 1.0f; coords[1][2] = 0.0f;
3963 coords[2][0] = 0.0f; coords[2][1] = 0.0f; coords[2][2] = 0.0f;
3964 coords[3][0] = 1.0f; coords[3][1] = 0.0f; coords[3][2] = 0.0f;
3965 break;
3967 case GL_TEXTURE_RECTANGLE_ARB:
3968 info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
3969 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
3970 info->tex_type = tex_rect;
3971 coords[0][0] = 0.0f; coords[0][1] = h; coords[0][2] = 0.0f;
3972 coords[1][0] = w; coords[1][1] = h; coords[1][2] = 0.0f;
3973 coords[2][0] = 0.0f; coords[2][1] = 0.0f; coords[2][2] = 0.0f;
3974 coords[3][0] = w; coords[3][1] = 0.0f; coords[3][2] = 0.0f;
3975 break;
3977 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3978 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
3979 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
3980 info->tex_type = tex_cube;
3981 coords[0][0] = 1.0f; coords[0][1] = -1.0f; coords[0][2] = 1.0f;
3982 coords[1][0] = 1.0f; coords[1][1] = -1.0f; coords[1][2] = -1.0f;
3983 coords[2][0] = 1.0f; coords[2][1] = 1.0f; coords[2][2] = 1.0f;
3984 coords[3][0] = 1.0f; coords[3][1] = 1.0f; coords[3][2] = -1.0f;
3986 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3987 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
3988 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
3989 info->tex_type = tex_cube;
3990 coords[0][0] = -1.0f; coords[0][1] = -1.0f; coords[0][2] = -1.0f;
3991 coords[1][0] = -1.0f; coords[1][1] = -1.0f; coords[1][2] = 1.0f;
3992 coords[2][0] = -1.0f; coords[2][1] = 1.0f; coords[2][2] = -1.0f;
3993 coords[3][0] = -1.0f; coords[3][1] = 1.0f; coords[3][2] = 1.0f;
3995 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3996 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
3997 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
3998 info->tex_type = tex_cube;
3999 coords[0][0] = -1.0f; coords[0][1] = 1.0f; coords[0][2] = 1.0f;
4000 coords[1][0] = 1.0f; coords[1][1] = 1.0f; coords[1][2] = 1.0f;
4001 coords[2][0] = -1.0f; coords[2][1] = 1.0f; coords[2][2] = -1.0f;
4002 coords[3][0] = 1.0f; coords[3][1] = 1.0f; coords[3][2] = -1.0f;
4004 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4005 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
4006 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4007 info->tex_type = tex_cube;
4008 coords[0][0] = -1.0f; coords[0][1] = -1.0f; coords[0][2] = -1.0f;
4009 coords[1][0] = 1.0f; coords[1][1] = -1.0f; coords[1][2] = -1.0f;
4010 coords[2][0] = -1.0f; coords[2][1] = -1.0f; coords[2][2] = 1.0f;
4011 coords[3][0] = 1.0f; coords[3][1] = -1.0f; coords[3][2] = 1.0f;
4013 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4014 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
4015 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4016 info->tex_type = tex_cube;
4017 coords[0][0] = -1.0f; coords[0][1] = -1.0f; coords[0][2] = 1.0f;
4018 coords[1][0] = 1.0f; coords[1][1] = -1.0f; coords[1][2] = 1.0f;
4019 coords[2][0] = -1.0f; coords[2][1] = 1.0f; coords[2][2] = 1.0f;
4020 coords[3][0] = 1.0f; coords[3][1] = 1.0f; coords[3][2] = 1.0f;
4022 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4023 info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
4024 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4025 info->tex_type = tex_cube;
4026 coords[0][0] = 1.0f; coords[0][1] = -1.0f; coords[0][2] = -1.0f;
4027 coords[1][0] = -1.0f; coords[1][1] = -1.0f; coords[1][2] = -1.0f;
4028 coords[2][0] = 1.0f; coords[2][1] = 1.0f; coords[2][2] = -1.0f;
4029 coords[3][0] = -1.0f; coords[3][1] = 1.0f; coords[3][2] = -1.0f;
4033 static void surface_depth_blt(IWineD3DSurfaceImpl *This, GLuint texture, GLsizei w, GLsizei h, GLenum target)
4035 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4036 struct depth_blt_info info;
4037 GLint old_binding = 0;
4039 glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4041 glDisable(GL_CULL_FACE);
4042 glEnable(GL_BLEND);
4043 glDisable(GL_ALPHA_TEST);
4044 glDisable(GL_SCISSOR_TEST);
4045 glDisable(GL_STENCIL_TEST);
4046 glEnable(GL_DEPTH_TEST);
4047 glDepthFunc(GL_ALWAYS);
4048 glDepthMask(GL_TRUE);
4049 glBlendFunc(GL_ZERO, GL_ONE);
4050 glViewport(0, 0, w, h);
4052 surface_get_depth_blt_info(target, w, h, &info);
4053 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4054 glGetIntegerv(info.binding, &old_binding);
4055 glBindTexture(info.bind_target, texture);
4057 device->shader_backend->shader_select_depth_blt((IWineD3DDevice *)device, info.tex_type);
4059 glBegin(GL_TRIANGLE_STRIP);
4060 glTexCoord3fv(info.coords[0]);
4061 glVertex2f(-1.0f, -1.0f);
4062 glTexCoord3fv(info.coords[1]);
4063 glVertex2f(1.0f, -1.0f);
4064 glTexCoord3fv(info.coords[2]);
4065 glVertex2f(-1.0f, 1.0f);
4066 glTexCoord3fv(info.coords[3]);
4067 glVertex2f(1.0f, 1.0f);
4068 glEnd();
4070 glBindTexture(info.bind_target, old_binding);
4072 glPopAttrib();
4074 device->shader_backend->shader_deselect_depth_blt((IWineD3DDevice *)device);
4077 void surface_modify_ds_location(IWineD3DSurface *iface, DWORD location) {
4078 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4080 TRACE("(%p) New location %#x\n", This, location);
4082 if (location & ~SFLAG_DS_LOCATIONS) {
4083 FIXME("(%p) Invalid location (%#x) specified\n", This, location);
4086 This->Flags &= ~SFLAG_DS_LOCATIONS;
4087 This->Flags |= location;
4090 void surface_load_ds_location(IWineD3DSurface *iface, DWORD location) {
4091 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4092 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4094 TRACE("(%p) New location %#x\n", This, location);
4096 /* TODO: Make this work for modes other than FBO */
4097 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4099 if (This->Flags & location) {
4100 TRACE("(%p) Location (%#x) is already up to date\n", This, location);
4101 return;
4104 if (This->current_renderbuffer) {
4105 FIXME("(%p) Not supported with fixed up depth stencil\n", This);
4106 return;
4109 if (location == SFLAG_DS_OFFSCREEN) {
4110 if (This->Flags & SFLAG_DS_ONSCREEN) {
4111 GLint old_binding = 0;
4112 GLenum bind_target;
4114 TRACE("(%p) Copying onscreen depth buffer to depth texture\n", This);
4116 ENTER_GL();
4118 if (!device->depth_blt_texture) {
4119 glGenTextures(1, &device->depth_blt_texture);
4122 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4123 * directly on the FBO texture. That's because we need to flip. */
4124 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
4125 if (This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
4126 glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4127 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4128 } else {
4129 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4130 bind_target = GL_TEXTURE_2D;
4132 glBindTexture(bind_target, device->depth_blt_texture);
4133 glCopyTexImage2D(bind_target,
4134 This->glDescription.level,
4135 This->resource.format_desc->glInternal,
4138 This->currentDesc.Width,
4139 This->currentDesc.Height,
4141 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4142 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4143 glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4144 glBindTexture(bind_target, old_binding);
4146 /* Setup the destination */
4147 if (!device->depth_blt_rb) {
4148 GL_EXTCALL(glGenRenderbuffersEXT(1, &device->depth_blt_rb));
4149 checkGLcall("glGenRenderbuffersEXT");
4151 if (device->depth_blt_rb_w != This->currentDesc.Width
4152 || device->depth_blt_rb_h != This->currentDesc.Height) {
4153 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, device->depth_blt_rb));
4154 checkGLcall("glBindRenderbufferEXT");
4155 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, This->currentDesc.Width, This->currentDesc.Height));
4156 checkGLcall("glRenderbufferStorageEXT");
4157 device->depth_blt_rb_w = This->currentDesc.Width;
4158 device->depth_blt_rb_h = This->currentDesc.Height;
4161 context_bind_fbo((IWineD3DDevice *)device, GL_FRAMEBUFFER_EXT, &device->activeContext->dst_fbo);
4162 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, device->depth_blt_rb));
4163 checkGLcall("glFramebufferRenderbufferEXT");
4164 context_attach_depth_stencil_fbo(device, GL_FRAMEBUFFER_EXT, iface, FALSE);
4166 /* Do the actual blit */
4167 surface_depth_blt(This, device->depth_blt_texture, This->currentDesc.Width, This->currentDesc.Height, bind_target);
4168 checkGLcall("depth_blt");
4170 if (device->activeContext->current_fbo) {
4171 context_bind_fbo((IWineD3DDevice *)device, GL_FRAMEBUFFER_EXT, &device->activeContext->current_fbo->id);
4172 } else {
4173 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
4174 checkGLcall("glBindFramebuffer()");
4177 LEAVE_GL();
4178 } else {
4179 FIXME("No up to date depth stencil location\n");
4181 } else if (location == SFLAG_DS_ONSCREEN) {
4182 if (This->Flags & SFLAG_DS_OFFSCREEN) {
4183 TRACE("(%p) Copying depth texture to onscreen depth buffer\n", This);
4185 ENTER_GL();
4187 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
4188 checkGLcall("glBindFramebuffer()");
4189 surface_depth_blt(This, This->glDescription.textureName, This->currentDesc.Width, This->currentDesc.Height, This->glDescription.target);
4190 checkGLcall("depth_blt");
4192 if (device->activeContext->current_fbo) {
4193 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, device->activeContext->current_fbo->id));
4194 checkGLcall("glBindFramebuffer()");
4197 LEAVE_GL();
4198 } else {
4199 FIXME("No up to date depth stencil location\n");
4201 } else {
4202 ERR("(%p) Invalid location (%#x) specified\n", This, location);
4205 This->Flags |= location;
4208 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
4209 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4210 IWineD3DBaseTexture *texture;
4211 IWineD3DSurfaceImpl *overlay;
4213 TRACE("(%p)->(%s, %s)\n", iface, debug_surflocation(flag),
4214 persistent ? "TRUE" : "FALSE");
4216 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4217 if (This->Flags & SFLAG_SWAPCHAIN)
4219 TRACE("Surface %p is an onscreen surface\n", iface);
4220 } else {
4221 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4222 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4226 if(persistent) {
4227 if(((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) ||
4228 ((This->Flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX))) {
4229 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4230 TRACE("Passing to container\n");
4231 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4232 IWineD3DBaseTexture_Release(texture);
4235 This->Flags &= ~SFLAG_LOCATIONS;
4236 This->Flags |= flag;
4238 /* Redraw emulated overlays, if any */
4239 if(flag & SFLAG_INDRAWABLE && !list_empty(&This->overlays)) {
4240 LIST_FOR_EACH_ENTRY(overlay, &This->overlays, IWineD3DSurfaceImpl, overlay_entry) {
4241 IWineD3DSurface_DrawOverlay((IWineD3DSurface *) overlay);
4244 } else {
4245 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))) {
4246 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4247 TRACE("Passing to container\n");
4248 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4249 IWineD3DBaseTexture_Release(texture);
4252 This->Flags &= ~flag;
4255 if(!(This->Flags & SFLAG_LOCATIONS)) {
4256 ERR("%p: Surface does not have any up to date location\n", This);
4260 struct coords {
4261 GLfloat x, y, z;
4264 struct float_rect
4266 float l;
4267 float t;
4268 float r;
4269 float b;
4272 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
4274 f->l = ((r->left * 2.0f) / w) - 1.0f;
4275 f->t = ((r->top * 2.0f) / h) - 1.0f;
4276 f->r = ((r->right * 2.0f) / w) - 1.0f;
4277 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
4280 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
4281 struct coords coords[4];
4282 RECT rect;
4283 IWineD3DSwapChain *swapchain;
4284 IWineD3DBaseTexture *texture;
4285 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4286 GLenum bind_target;
4287 struct float_rect f;
4289 if(rect_in) {
4290 rect = *rect_in;
4291 } else {
4292 rect.left = 0;
4293 rect.top = 0;
4294 rect.right = This->currentDesc.Width;
4295 rect.bottom = This->currentDesc.Height;
4298 switch(This->glDescription.target)
4300 case GL_TEXTURE_2D:
4301 bind_target = GL_TEXTURE_2D;
4303 coords[0].x = (float)rect.left / This->pow2Width;
4304 coords[0].y = (float)rect.top / This->pow2Height;
4305 coords[0].z = 0;
4307 coords[1].x = (float)rect.left / This->pow2Width;
4308 coords[1].y = (float)rect.bottom / This->pow2Height;
4309 coords[1].z = 0;
4311 coords[2].x = (float)rect.right / This->pow2Width;
4312 coords[2].y = (float)rect.bottom / This->pow2Height;
4313 coords[2].z = 0;
4315 coords[3].x = (float)rect.right / This->pow2Width;
4316 coords[3].y = (float)rect.top / This->pow2Height;
4317 coords[3].z = 0;
4318 break;
4320 case GL_TEXTURE_RECTANGLE_ARB:
4321 bind_target = GL_TEXTURE_RECTANGLE_ARB;
4322 coords[0].x = rect.left; coords[0].y = rect.top; coords[0].z = 0;
4323 coords[1].x = rect.left; coords[1].y = rect.bottom; coords[1].z = 0;
4324 coords[2].x = rect.right; coords[2].y = rect.bottom; coords[2].z = 0;
4325 coords[3].x = rect.right; coords[3].y = rect.top; coords[3].z = 0;
4326 break;
4328 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4329 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4330 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4331 coords[0].x = 1; coords[0].y = -f.t; coords[0].z = -f.l;
4332 coords[1].x = 1; coords[1].y = -f.b; coords[1].z = -f.l;
4333 coords[2].x = 1; coords[2].y = -f.b; coords[2].z = -f.r;
4334 coords[3].x = 1; coords[3].y = -f.t; coords[3].z = -f.r;
4335 break;
4337 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4338 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4339 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4340 coords[0].x = -1; coords[0].y = -f.t; coords[0].z = f.l;
4341 coords[1].x = -1; coords[1].y = -f.b; coords[1].z = f.l;
4342 coords[2].x = -1; coords[2].y = -f.b; coords[2].z = f.r;
4343 coords[3].x = -1; coords[3].y = -f.t; coords[3].z = f.r;
4344 break;
4346 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4347 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4348 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4349 coords[0].x = f.l; coords[0].y = 1; coords[0].z = f.t;
4350 coords[1].x = f.l; coords[1].y = 1; coords[1].z = f.b;
4351 coords[2].x = f.r; coords[2].y = 1; coords[2].z = f.b;
4352 coords[3].x = f.r; coords[3].y = 1; coords[3].z = f.t;
4353 break;
4355 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4356 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4357 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4358 coords[0].x = f.l; coords[0].y = -1; coords[0].z = -f.t;
4359 coords[1].x = f.l; coords[1].y = -1; coords[1].z = -f.b;
4360 coords[2].x = f.r; coords[2].y = -1; coords[2].z = -f.b;
4361 coords[3].x = f.r; coords[3].y = -1; coords[3].z = -f.t;
4362 break;
4364 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4365 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4366 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4367 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1;
4368 coords[1].x = f.l; coords[1].y = -f.b; coords[1].z = 1;
4369 coords[2].x = f.r; coords[2].y = -f.b; coords[2].z = 1;
4370 coords[3].x = f.r; coords[3].y = -f.t; coords[3].z = 1;
4371 break;
4373 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4374 bind_target = GL_TEXTURE_CUBE_MAP_ARB;
4375 cube_coords_float(&rect, This->pow2Width, This->pow2Height, &f);
4376 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1;
4377 coords[1].x = -f.l; coords[1].y = -f.b; coords[1].z = -1;
4378 coords[2].x = -f.r; coords[2].y = -f.b; coords[2].z = -1;
4379 coords[3].x = -f.r; coords[3].y = -f.t; coords[3].z = -1;
4380 break;
4382 default:
4383 ERR("Unexpected texture target %#x\n", This->glDescription.target);
4384 return;
4387 ActivateContext(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
4388 ENTER_GL();
4390 glEnable(bind_target);
4391 checkGLcall("glEnable(bind_target)");
4392 glBindTexture(bind_target, This->glDescription.textureName);
4393 checkGLcall("bind_target, This->glDescription.textureName)");
4394 glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4395 checkGLcall("glTexParameteri");
4396 glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4397 checkGLcall("glTexParameteri");
4399 if (device->render_offscreen)
4401 LONG tmp = rect.top;
4402 rect.top = rect.bottom;
4403 rect.bottom = tmp;
4406 glBegin(GL_QUADS);
4407 glTexCoord3fv(&coords[0].x);
4408 glVertex2i(rect.left, rect.top);
4410 glTexCoord3fv(&coords[1].x);
4411 glVertex2i(rect.left, rect.bottom);
4413 glTexCoord3fv(&coords[2].x);
4414 glVertex2i(rect.right, rect.bottom);
4416 glTexCoord3fv(&coords[3].x);
4417 glVertex2i(rect.right, rect.top);
4418 glEnd();
4419 checkGLcall("glEnd");
4421 glDisable(bind_target);
4422 checkGLcall("glDisable(bind_target)");
4424 LEAVE_GL();
4426 if(SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain)))
4428 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
4429 if(((IWineD3DSwapChainImpl*)swapchain)->frontBuffer == (IWineD3DSurface*)This ||
4430 ((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
4431 glFlush();
4433 IWineD3DSwapChain_Release(swapchain);
4434 } else {
4435 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
4436 * reset properly next draw
4438 if(SUCCEEDED(IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture)))
4440 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
4441 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
4442 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
4443 IWineD3DBaseTexture_Release(texture);
4448 /*****************************************************************************
4449 * IWineD3DSurface::LoadLocation
4451 * Copies the current surface data from wherever it is to the requested
4452 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4453 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4454 * multiple locations, the gl texture is preferred over the drawable, which is
4455 * preferred over system memory. The PBO counts as system memory. If rect is
4456 * not NULL, only the specified rectangle is copied (only supported for
4457 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4458 * location is marked up to date after the copy.
4460 * Parameters:
4461 * flag: Surface location flag to be updated
4462 * rect: rectangle to be copied
4464 * Returns:
4465 * WINED3D_OK on success
4466 * WINED3DERR_DEVICELOST on an internal error
4468 *****************************************************************************/
4469 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4470 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4471 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4472 GLenum format, internal, type;
4473 CONVERT_TYPES convert;
4474 int bpp;
4475 int width, pitch, outpitch;
4476 BYTE *mem;
4477 BOOL drawable_read_ok = TRUE;
4479 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4480 if (This->Flags & SFLAG_SWAPCHAIN)
4482 TRACE("Surface %p is an onscreen surface\n", iface);
4483 } else {
4484 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4485 * Prefer SFLAG_INTEXTURE. */
4486 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4487 drawable_read_ok = FALSE;
4491 TRACE("(%p)->(%s, %p)\n", iface, debug_surflocation(flag), rect);
4492 if(rect) {
4493 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4496 if(This->Flags & flag) {
4497 TRACE("Location already up to date\n");
4498 return WINED3D_OK;
4501 if(!(This->Flags & SFLAG_LOCATIONS)) {
4502 ERR("%p: Surface does not have any up to date location\n", This);
4503 This->Flags |= SFLAG_LOST;
4504 return WINED3DERR_DEVICELOST;
4507 if(flag == SFLAG_INSYSMEM) {
4508 surface_prepare_system_memory(This);
4510 /* Download the surface to system memory */
4511 if(This->Flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) {
4512 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4513 surface_bind_and_dirtify(This, !(This->Flags & SFLAG_INTEXTURE));
4515 surface_download_data(This);
4516 } else {
4517 read_from_framebuffer(This, rect,
4518 This->resource.allocatedMemory,
4519 IWineD3DSurface_GetPitch(iface));
4521 } else if(flag == SFLAG_INDRAWABLE) {
4522 if(This->Flags & SFLAG_INTEXTURE) {
4523 surface_blt_to_drawable(This, rect);
4524 } else {
4525 if((This->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX) {
4526 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4527 * values, otherwise we get incorrect values in the target. For now go the slow way
4528 * via a system memory copy
4530 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4533 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, FALSE);
4535 /* The width is in 'length' not in bytes */
4536 width = This->currentDesc.Width;
4537 pitch = IWineD3DSurface_GetPitch(iface);
4539 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4540 * but it isn't set (yet) in all cases it is getting called. */
4541 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4542 TRACE("Removing the pbo attached to surface %p\n", This);
4543 surface_remove_pbo(This);
4546 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4547 int height = This->currentDesc.Height;
4549 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4550 outpitch = width * bpp;
4551 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4553 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4554 if(!mem) {
4555 ERR("Out of memory %d, %d!\n", outpitch, height);
4556 return WINED3DERR_OUTOFVIDEOMEMORY;
4558 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4560 This->Flags |= SFLAG_CONVERTED;
4561 } else {
4562 This->Flags &= ~SFLAG_CONVERTED;
4563 mem = This->resource.allocatedMemory;
4566 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4568 /* Don't delete PBO memory */
4569 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4570 HeapFree(GetProcessHeap(), 0, mem);
4572 } else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */ {
4573 if (drawable_read_ok && (This->Flags & SFLAG_INDRAWABLE)) {
4574 read_from_framebuffer_texture(This, flag == SFLAG_INSRGBTEX);
4575 } else { /* Upload from system memory */
4576 BOOL srgb = flag == SFLAG_INSRGBTEX;
4577 DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
4578 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, srgb);
4580 if(srgb) {
4581 if((This->Flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE) {
4582 /* Performance warning ... */
4583 FIXME("%p: Downloading rgb texture to reload it as srgb\n", This);
4584 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4586 } else {
4587 if((This->Flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX) {
4588 /* Performance warning ... */
4589 FIXME("%p: Downloading srgb texture to reload it as rgb\n", This);
4590 IWineD3DSurfaceImpl_LoadLocation(iface, SFLAG_INSYSMEM, rect);
4594 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4595 surface_bind_and_dirtify(This, srgb);
4597 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4598 This->Flags |= SFLAG_GLCKEY;
4599 This->glCKey = This->SrcBltCKey;
4601 else This->Flags &= ~SFLAG_GLCKEY;
4603 /* The width is in 'length' not in bytes */
4604 width = This->currentDesc.Width;
4605 pitch = IWineD3DSurface_GetPitch(iface);
4607 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4608 * but it isn't set (yet) in all cases it is getting called. */
4609 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4610 TRACE("Removing the pbo attached to surface %p\n", This);
4611 surface_remove_pbo(This);
4614 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4615 int height = This->currentDesc.Height;
4617 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4618 outpitch = width * bpp;
4619 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4621 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4622 if(!mem) {
4623 ERR("Out of memory %d, %d!\n", outpitch, height);
4624 return WINED3DERR_OUTOFVIDEOMEMORY;
4626 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4628 This->Flags |= SFLAG_CONVERTED;
4630 else if ((This->resource.format_desc->format == WINED3DFMT_P8)
4631 && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)))
4633 d3dfmt_p8_upload_palette(iface, convert);
4634 This->Flags &= ~SFLAG_CONVERTED;
4635 mem = This->resource.allocatedMemory;
4636 } else {
4637 This->Flags &= ~SFLAG_CONVERTED;
4638 mem = This->resource.allocatedMemory;
4641 /* Make sure the correct pitch is used */
4642 ENTER_GL();
4643 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4644 LEAVE_GL();
4646 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4647 TRACE("non power of two support\n");
4648 if(!(This->Flags & alloc_flag)) {
4649 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4650 This->Flags |= alloc_flag;
4652 if (mem || (This->Flags & SFLAG_PBO)) {
4653 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4655 } else {
4656 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4657 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4659 if(!(This->Flags & alloc_flag)) {
4660 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4661 This->Flags |= alloc_flag;
4663 if (mem || (This->Flags & SFLAG_PBO)) {
4664 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4668 /* Restore the default pitch */
4669 ENTER_GL();
4670 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4671 LEAVE_GL();
4673 /* Don't delete PBO memory */
4674 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4675 HeapFree(GetProcessHeap(), 0, mem);
4679 if(rect == NULL) {
4680 This->Flags |= flag;
4683 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !(This->Flags & SFLAG_SWAPCHAIN)
4684 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4685 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4686 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4689 return WINED3D_OK;
4692 static HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container)
4694 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4695 IWineD3DSwapChain *swapchain = NULL;
4697 /* Update the drawable size method */
4698 if(container) {
4699 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4701 if(swapchain) {
4702 This->get_drawable_size = get_drawable_size_swapchain;
4703 IWineD3DSwapChain_Release(swapchain);
4704 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4705 switch(wined3d_settings.offscreen_rendering_mode) {
4706 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4707 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4708 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4712 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4715 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4716 return SURFACE_OPENGL;
4719 static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4720 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4721 HRESULT hr;
4723 /* If there's no destination surface there is nothing to do */
4724 if(!This->overlay_dest) return WINED3D_OK;
4726 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4727 * update the overlay. Prevent an endless recursion
4729 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4730 return WINED3D_OK;
4732 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4733 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4734 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4735 NULL, WINED3DTEXF_LINEAR);
4736 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4738 return hr;
4741 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4743 /* IUnknown */
4744 IWineD3DBaseSurfaceImpl_QueryInterface,
4745 IWineD3DBaseSurfaceImpl_AddRef,
4746 IWineD3DSurfaceImpl_Release,
4747 /* IWineD3DResource */
4748 IWineD3DBaseSurfaceImpl_GetParent,
4749 IWineD3DBaseSurfaceImpl_GetDevice,
4750 IWineD3DBaseSurfaceImpl_SetPrivateData,
4751 IWineD3DBaseSurfaceImpl_GetPrivateData,
4752 IWineD3DBaseSurfaceImpl_FreePrivateData,
4753 IWineD3DBaseSurfaceImpl_SetPriority,
4754 IWineD3DBaseSurfaceImpl_GetPriority,
4755 IWineD3DSurfaceImpl_PreLoad,
4756 IWineD3DSurfaceImpl_UnLoad,
4757 IWineD3DBaseSurfaceImpl_GetType,
4758 /* IWineD3DSurface */
4759 IWineD3DBaseSurfaceImpl_GetContainer,
4760 IWineD3DBaseSurfaceImpl_GetDesc,
4761 IWineD3DSurfaceImpl_LockRect,
4762 IWineD3DSurfaceImpl_UnlockRect,
4763 IWineD3DSurfaceImpl_GetDC,
4764 IWineD3DSurfaceImpl_ReleaseDC,
4765 IWineD3DSurfaceImpl_Flip,
4766 IWineD3DSurfaceImpl_Blt,
4767 IWineD3DBaseSurfaceImpl_GetBltStatus,
4768 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4769 IWineD3DBaseSurfaceImpl_IsLost,
4770 IWineD3DBaseSurfaceImpl_Restore,
4771 IWineD3DSurfaceImpl_BltFast,
4772 IWineD3DBaseSurfaceImpl_GetPalette,
4773 IWineD3DBaseSurfaceImpl_SetPalette,
4774 IWineD3DSurfaceImpl_RealizePalette,
4775 IWineD3DBaseSurfaceImpl_SetColorKey,
4776 IWineD3DBaseSurfaceImpl_GetPitch,
4777 IWineD3DSurfaceImpl_SetMem,
4778 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4779 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4780 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4781 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4782 IWineD3DBaseSurfaceImpl_SetClipper,
4783 IWineD3DBaseSurfaceImpl_GetClipper,
4784 /* Internal use: */
4785 IWineD3DSurfaceImpl_LoadTexture,
4786 IWineD3DSurfaceImpl_BindTexture,
4787 IWineD3DSurfaceImpl_SaveSnapshot,
4788 IWineD3DSurfaceImpl_SetContainer,
4789 IWineD3DSurfaceImpl_GetGlDesc,
4790 IWineD3DBaseSurfaceImpl_GetData,
4791 IWineD3DSurfaceImpl_SetFormat,
4792 IWineD3DSurfaceImpl_PrivateSetup,
4793 IWineD3DSurfaceImpl_ModifyLocation,
4794 IWineD3DSurfaceImpl_LoadLocation,
4795 IWineD3DSurfaceImpl_GetImplType,
4796 IWineD3DSurfaceImpl_DrawOverlay
4798 #undef GLINFO_LOCATION
4800 #define GLINFO_LOCATION device->adapter->gl_info
4801 static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4802 static void ffp_blit_free(IWineD3DDevice *iface) { }
4804 static HRESULT ffp_blit_set(IWineD3DDevice *iface, const struct GlPixelFormatDesc *format_desc,
4805 GLenum textype, UINT width, UINT height)
4807 glEnable(textype);
4808 checkGLcall("glEnable(textype)");
4809 return WINED3D_OK;
4812 static void ffp_blit_unset(IWineD3DDevice *iface) {
4813 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4814 glDisable(GL_TEXTURE_2D);
4815 checkGLcall("glDisable(GL_TEXTURE_2D)");
4816 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
4817 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4818 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4820 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
4821 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4822 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4826 static BOOL ffp_blit_color_fixup_supported(struct color_fixup_desc fixup)
4828 if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
4830 TRACE("Checking support for fixup:\n");
4831 dump_color_fixup_desc(fixup);
4834 /* We only support identity conversions. */
4835 if (is_identity_fixup(fixup))
4837 TRACE("[OK]\n");
4838 return TRUE;
4841 TRACE("[FAILED]\n");
4842 return FALSE;
4845 const struct blit_shader ffp_blit = {
4846 ffp_blit_alloc,
4847 ffp_blit_free,
4848 ffp_blit_set,
4849 ffp_blit_unset,
4850 ffp_blit_color_fixup_supported