be6ffb33c85a66c8e4ac6daf7dff5331a7455fd9
[reactos.git] / reactos / dll / directx / wine / wined3d / surface.c
1 /*
2 * IWineD3DSurface Implementation
3 *
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
13 *
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.
18 *
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.
23 *
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
27 */
28
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wined3d_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
35
36 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *surf);
37 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey);
38 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert);
39 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This);
40 static void surface_remove_pbo(IWineD3DSurfaceImpl *This);
41
42 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *This) {
43 int active_sampler;
44
45 /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
46 * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
47 * gl states. The current texture unit should always be a valid one.
48 *
49 * To be more specific, this is tricky because we can implicitly be called
50 * from sampler() in state.c. This means we can't touch anything other than
51 * whatever happens to be the currently active texture, or we would risk
52 * marking already applied sampler states dirty again.
53 *
54 * TODO: Track the current active texture per GL context instead of using glGet
55 */
56 if (GL_SUPPORT(ARB_MULTITEXTURE)) {
57 GLint active_texture;
58 ENTER_GL();
59 glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
60 LEAVE_GL();
61 active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
62 } else {
63 active_sampler = 0;
64 }
65
66 if (active_sampler != -1) {
67 IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
68 }
69 IWineD3DSurface_BindTexture((IWineD3DSurface *)This);
70 }
71
72 /* This function checks if the primary render target uses the 8bit paletted format. */
73 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
74 {
75 if (device->render_targets && device->render_targets[0]) {
76 IWineD3DSurfaceImpl* render_target = (IWineD3DSurfaceImpl*)device->render_targets[0];
77 if((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET) && (render_target->resource.format == WINED3DFMT_P8))
78 return TRUE;
79 }
80 return FALSE;
81 }
82
83 /* This call just downloads data, the caller is responsible for activating the
84 * right context and binding the correct texture. */
85 static void surface_download_data(IWineD3DSurfaceImpl *This) {
86 if (0 == This->glDescription.textureName) {
87 ERR("Surface does not have a texture, but SFLAG_INTEXTURE is set\n");
88 return;
89 }
90
91 /* Only support read back of converted P8 surfaces */
92 if(This->Flags & SFLAG_CONVERTED && (This->resource.format != WINED3DFMT_P8)) {
93 FIXME("Read back converted textures unsupported, format=%s\n", debug_d3dformat(This->resource.format));
94 return;
95 }
96
97 ENTER_GL();
98
99 if (This->resource.format == WINED3DFMT_DXT1 ||
100 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
101 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5 ||
102 This->resource.format == WINED3DFMT_ATI2N) {
103 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) { /* We can assume this as the texture would not have been created otherwise */
104 FIXME("(%p) : Attempting to lock a compressed texture when texture compression isn't supported by opengl\n", This);
105 } else {
106 TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
107 This->glDescription.glFormat, This->glDescription.glType, This->resource.allocatedMemory);
108
109 if(This->Flags & SFLAG_PBO) {
110 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
111 checkGLcall("glBindBufferARB");
112 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, NULL));
113 checkGLcall("glGetCompressedTexImageARB()");
114 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
115 checkGLcall("glBindBufferARB");
116 } else {
117 GL_EXTCALL(glGetCompressedTexImageARB(This->glDescription.target, This->glDescription.level, This->resource.allocatedMemory));
118 checkGLcall("glGetCompressedTexImageARB()");
119 }
120 }
121 LEAVE_GL();
122 } else {
123 void *mem;
124 GLenum format = This->glDescription.glFormat;
125 GLenum type = This->glDescription.glType;
126 int src_pitch = 0;
127 int dst_pitch = 0;
128
129 /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
130 if(This->resource.format == WINED3DFMT_P8 && primary_render_target_is_p8(This->resource.wineD3DDevice)) {
131 format = GL_ALPHA;
132 type = GL_UNSIGNED_BYTE;
133 }
134
135 if (This->Flags & SFLAG_NONPOW2) {
136 unsigned char alignment = This->resource.wineD3DDevice->surface_alignment;
137 src_pitch = This->bytesPerPixel * This->pow2Width;
138 dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
139 src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
140 mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
141 } else {
142 mem = This->resource.allocatedMemory;
143 }
144
145 TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n", This, This->glDescription.level,
146 format, type, mem);
147
148 if(This->Flags & SFLAG_PBO) {
149 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
150 checkGLcall("glBindBufferARB");
151
152 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
153 type, NULL);
154 checkGLcall("glGetTexImage()");
155
156 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
157 checkGLcall("glBindBufferARB");
158 } else {
159 glGetTexImage(This->glDescription.target, This->glDescription.level, format,
160 type, mem);
161 checkGLcall("glGetTexImage()");
162 }
163 LEAVE_GL();
164
165 if (This->Flags & SFLAG_NONPOW2) {
166 LPBYTE src_data, dst_data;
167 int y;
168 /*
169 * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
170 * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
171 * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
172 *
173 * We're doing this...
174 *
175 * instead of boxing the texture :
176 * |<-texture width ->| -->pow2width| /\
177 * |111111111111111111| | |
178 * |222 Texture 222222| boxed empty | texture height
179 * |3333 Data 33333333| | |
180 * |444444444444444444| | \/
181 * ----------------------------------- |
182 * | boxed empty | boxed empty | pow2height
183 * | | | \/
184 * -----------------------------------
185 *
186 *
187 * we're repacking the data to the expected texture width
188 *
189 * |<-texture width ->| -->pow2width| /\
190 * |111111111111111111222222222222222| |
191 * |222333333333333333333444444444444| texture height
192 * |444444 | |
193 * | | \/
194 * | | |
195 * | empty | pow2height
196 * | | \/
197 * -----------------------------------
198 *
199 * == is the same as
200 *
201 * |<-texture width ->| /\
202 * |111111111111111111|
203 * |222222222222222222|texture height
204 * |333333333333333333|
205 * |444444444444444444| \/
206 * --------------------
207 *
208 * this also means that any references to allocatedMemory should work with the data as if were a
209 * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
210 *
211 * internally the texture is still stored in a boxed format so any references to textureName will
212 * get a boxed texture with width pow2width and not a texture of width currentDesc.Width.
213 *
214 * Performance should not be an issue, because applications normally do not lock the surfaces when
215 * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
216 * and doesn't have to be re-read.
217 */
218 src_data = mem;
219 dst_data = This->resource.allocatedMemory;
220 TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
221 for (y = 1 ; y < This->currentDesc.Height; y++) {
222 /* skip the first row */
223 src_data += src_pitch;
224 dst_data += dst_pitch;
225 memcpy(dst_data, src_data, dst_pitch);
226 }
227
228 HeapFree(GetProcessHeap(), 0, mem);
229 }
230 }
231
232 /* Surface has now been downloaded */
233 This->Flags |= SFLAG_INSYSMEM;
234 }
235
236 /* This call just uploads data, the caller is responsible for activating the
237 * right context and binding the correct texture. */
238 static void surface_upload_data(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *data) {
239
240 if(This->heightscale != 1.0 && This->heightscale != 0.0) height *= This->heightscale;
241
242 if (This->resource.format == WINED3DFMT_DXT1 ||
243 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
244 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5 ||
245 This->resource.format == WINED3DFMT_ATI2N) {
246 if (!GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
247 FIXME("Using DXT1/3/5 without advertized support\n");
248 } else {
249 /* glCompressedTexSubImage2D for uploading and glTexImage2D for allocating does not work well on some drivers(r200 dri, MacOS ATI driver)
250 * glCompressedTexImage2D does not accept NULL pointers. So for compressed textures surface_allocate_surface does nothing, and this
251 * function uses glCompressedTexImage2D instead of the SubImage call
252 */
253 TRACE("(%p) : Calling glCompressedTexSubImage2D w %d, h %d, data %p\n", This, width, height, data);
254 ENTER_GL();
255
256 if(This->Flags & SFLAG_PBO) {
257 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
258 checkGLcall("glBindBufferARB");
259 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
260
261 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
262 width, height, 0 /* border */, This->resource.size, NULL));
263 checkGLcall("glCompressedTexSubImage2D");
264
265 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
266 checkGLcall("glBindBufferARB");
267 } else {
268 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
269 width, height, 0 /* border */, This->resource.size, data));
270 checkGLcall("glCompressedTexSubImage2D");
271 }
272 LEAVE_GL();
273 }
274 } else {
275 TRACE("(%p) : Calling glTexSubImage2D w %d, h %d, data, %p\n", This, width, height, data);
276 ENTER_GL();
277
278 if(This->Flags & SFLAG_PBO) {
279 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
280 checkGLcall("glBindBufferARB");
281 TRACE("(%p) pbo: %#x, data: %p\n", This, This->pbo, data);
282
283 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, NULL);
284 checkGLcall("glTexSubImage2D");
285
286 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
287 checkGLcall("glBindBufferARB");
288 }
289 else {
290 glTexSubImage2D(This->glDescription.target, This->glDescription.level, 0, 0, width, height, format, type, data);
291 checkGLcall("glTexSubImage2D");
292 }
293
294 LEAVE_GL();
295 }
296 }
297
298 /* This call just allocates the texture, the caller is responsible for
299 * activating the right context and binding the correct texture. */
300 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, GLenum internal, GLsizei width, GLsizei height, GLenum format, GLenum type) {
301 BOOL enable_client_storage = FALSE;
302 BYTE *mem = NULL;
303
304 if(This->heightscale != 1.0 && This->heightscale != 0.0) height *= This->heightscale;
305
306 TRACE("(%p) : Creating surface (target %#x) level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n", This,
307 This->glDescription.target, This->glDescription.level, debug_d3dformat(This->resource.format), internal, width, height, format, type);
308
309 if (This->resource.format == WINED3DFMT_DXT1 ||
310 This->resource.format == WINED3DFMT_DXT2 || This->resource.format == WINED3DFMT_DXT3 ||
311 This->resource.format == WINED3DFMT_DXT4 || This->resource.format == WINED3DFMT_DXT5 ||
312 This->resource.format == WINED3DFMT_ATI2N) {
313 /* glCompressedTexImage2D does not accept NULL pointers, so we cannot allocate a compressed texture without uploading data */
314 TRACE("Not allocating compressed surfaces, surface_upload_data will specify them\n");
315
316 /* We have to point GL to the client storage memory here, because upload_data might use a PBO. This means a double upload
317 * once, unfortunately
318 */
319 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
320 /* Neither NONPOW2, DIBSECTION nor OVERSIZE flags can be set on compressed textures */
321 This->Flags |= SFLAG_CLIENT;
322 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
323 ENTER_GL();
324 GL_EXTCALL(glCompressedTexImage2DARB(This->glDescription.target, This->glDescription.level, internal,
325 width, height, 0 /* border */, This->resource.size, mem));
326 LEAVE_GL();
327 }
328
329 return;
330 }
331
332 ENTER_GL();
333
334 if(GL_SUPPORT(APPLE_CLIENT_STORAGE)) {
335 if(This->Flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_OVERSIZE | SFLAG_CONVERTED) || This->resource.allocatedMemory == NULL) {
336 /* In some cases we want to disable client storage.
337 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
338 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
339 * SFLAG_OVERSIZE: The gl texture is smaller than the allocated memory
340 * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
341 * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
342 */
343 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
344 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
345 This->Flags &= ~SFLAG_CLIENT;
346 enable_client_storage = TRUE;
347 } else {
348 This->Flags |= SFLAG_CLIENT;
349
350 /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
351 * it might point into a pbo. Instead use heapMemory, but get the alignment right.
352 */
353 mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
354 }
355 }
356 glTexImage2D(This->glDescription.target, This->glDescription.level, internal, width, height, 0, format, type, mem);
357 checkGLcall("glTexImage2D");
358
359 if(enable_client_storage) {
360 glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
361 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
362 }
363 LEAVE_GL();
364
365 This->Flags |= SFLAG_ALLOCATED;
366 }
367
368 /* In D3D the depth stencil dimensions have to be greater than or equal to the
369 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
370 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
371 void surface_set_compatible_renderbuffer(IWineD3DSurface *iface, unsigned int width, unsigned int height) {
372 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
373 renderbuffer_entry_t *entry;
374 GLuint renderbuffer = 0;
375 unsigned int src_width, src_height;
376
377 src_width = This->pow2Width;
378 src_height = This->pow2Height;
379
380 /* A depth stencil smaller than the render target is not valid */
381 if (width > src_width || height > src_height) return;
382
383 /* Remove any renderbuffer set if the sizes match */
384 if (width == src_width && height == src_height) {
385 This->current_renderbuffer = NULL;
386 return;
387 }
388
389 /* Look if we've already got a renderbuffer of the correct dimensions */
390 LIST_FOR_EACH_ENTRY(entry, &This->renderbuffers, renderbuffer_entry_t, entry) {
391 if (entry->width == width && entry->height == height) {
392 renderbuffer = entry->id;
393 This->current_renderbuffer = entry;
394 break;
395 }
396 }
397
398 if (!renderbuffer) {
399 const GlPixelFormatDesc *glDesc;
400 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
401
402 GL_EXTCALL(glGenRenderbuffersEXT(1, &renderbuffer));
403 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffer));
404 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, glDesc->glInternal, width, height));
405
406 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
407 entry->width = width;
408 entry->height = height;
409 entry->id = renderbuffer;
410 list_add_head(&This->renderbuffers, &entry->entry);
411
412 This->current_renderbuffer = entry;
413 }
414
415 checkGLcall("set_compatible_renderbuffer");
416 }
417
418 GLenum surface_get_gl_buffer(IWineD3DSurface *iface, IWineD3DSwapChain *swapchain) {
419 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
420 IWineD3DSwapChainImpl *swapchain_impl = (IWineD3DSwapChainImpl *)swapchain;
421
422 TRACE("(%p) : swapchain %p\n", This, swapchain);
423
424 if (swapchain_impl->backBuffer && swapchain_impl->backBuffer[0] == iface) {
425 TRACE("Returning GL_BACK\n");
426 return GL_BACK;
427 } else if (swapchain_impl->frontBuffer == iface) {
428 TRACE("Returning GL_FRONT\n");
429 return GL_FRONT;
430 }
431
432 FIXME("Higher back buffer, returning GL_BACK\n");
433 return GL_BACK;
434 }
435
436 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
437 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
438 ULONG ref = InterlockedDecrement(&This->resource.ref);
439 TRACE("(%p) : Releasing from %d\n", This, ref + 1);
440 if (ref == 0) {
441 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
442 renderbuffer_entry_t *entry, *entry2;
443 TRACE("(%p) : cleaning up\n", This);
444
445 /* Need a context to destroy the texture. Use the currently active render target, but only if
446 * the primary render target exists. Otherwise lastActiveRenderTarget is garbage, see above.
447 * When destroying the primary rt, Uninit3D will activate a context before doing anything
448 */
449 if(device->render_targets && device->render_targets[0]) {
450 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
451 }
452
453 ENTER_GL();
454 if (This->glDescription.textureName != 0) { /* release the openGL texture.. */
455 TRACE("Deleting texture %d\n", This->glDescription.textureName);
456 glDeleteTextures(1, &This->glDescription.textureName);
457 }
458
459 if(This->Flags & SFLAG_PBO) {
460 /* Delete the PBO */
461 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
462 }
463
464 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
465 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
466 HeapFree(GetProcessHeap(), 0, entry);
467 }
468 LEAVE_GL();
469
470 if(This->Flags & SFLAG_DIBSECTION) {
471 /* Release the DC */
472 SelectObject(This->hDC, This->dib.holdbitmap);
473 DeleteDC(This->hDC);
474 /* Release the DIB section */
475 DeleteObject(This->dib.DIBsection);
476 This->dib.bitmap_data = NULL;
477 This->resource.allocatedMemory = NULL;
478 }
479 if(This->Flags & SFLAG_USERPTR) IWineD3DSurface_SetMem(iface, NULL);
480
481 HeapFree(GetProcessHeap(), 0, This->palette9);
482
483 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
484
485 if(This->overlay_dest) {
486 list_remove(&This->overlay_entry);
487 }
488
489 TRACE("(%p) Released\n", This);
490 HeapFree(GetProcessHeap(), 0, This);
491
492 }
493 return ref;
494 }
495
496 /* ****************************************************
497 IWineD3DSurface IWineD3DResource parts follow
498 **************************************************** */
499
500 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
501 /* TODO: check for locks */
502 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
503 IWineD3DBaseTexture *baseTexture = NULL;
504 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
505
506 TRACE("(%p)Checking to see if the container is a base texture\n", This);
507 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
508 TRACE("Passing to container\n");
509 IWineD3DBaseTexture_PreLoad(baseTexture);
510 IWineD3DBaseTexture_Release(baseTexture);
511 } else {
512 TRACE("(%p) : About to load surface\n", This);
513
514 if(!device->isInDraw) {
515 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
516 }
517
518 if (This->resource.format == WINED3DFMT_P8 || This->resource.format == WINED3DFMT_A8P8) {
519 if(palette9_changed(This)) {
520 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
521 /* TODO: This is not necessarily needed with hw palettized texture support */
522 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
523 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
524 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
525 }
526 }
527
528 IWineD3DSurface_LoadTexture(iface, FALSE);
529
530 if (This->resource.pool == WINED3DPOOL_DEFAULT) {
531 /* Tell opengl to try and keep this texture in video ram (well mostly) */
532 GLclampf tmp;
533 tmp = 0.9f;
534 ENTER_GL();
535 glPrioritizeTextures(1, &This->glDescription.textureName, &tmp);
536 LEAVE_GL();
537 }
538 }
539 return;
540 }
541
542 static void surface_remove_pbo(IWineD3DSurfaceImpl *This) {
543 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
544 This->resource.allocatedMemory =
545 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
546
547 ENTER_GL();
548 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
549 checkGLcall("glBindBuffer(GL_PIXEL_UNPACK_BUFFER, This->pbo)");
550 GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0, This->resource.size, This->resource.allocatedMemory));
551 checkGLcall("glGetBufferSubData");
552 GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
553 checkGLcall("glDeleteBuffers");
554 LEAVE_GL();
555
556 This->pbo = 0;
557 This->Flags &= ~SFLAG_PBO;
558 }
559
560 static void WINAPI IWineD3DSurfaceImpl_UnLoad(IWineD3DSurface *iface) {
561 IWineD3DBaseTexture *texture = NULL;
562 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
563 renderbuffer_entry_t *entry, *entry2;
564 TRACE("(%p)\n", iface);
565
566 if(This->resource.pool == WINED3DPOOL_DEFAULT) {
567 /* Default pool resources are supposed to be destroyed before Reset is called.
568 * Implicit resources stay however. So this means we have an implicit render target
569 * or depth stencil. The content may be destroyed, but we still have to tear down
570 * opengl resources, so we cannot leave early.
571 *
572 * Put the most up to date surface location into the drawable. D3D-wise this content
573 * is undefined, so it would be nowhere, but that would make the location management
574 * more complicated. The drawable is a sane location, because if we mark sysmem or
575 * texture up to date, drawPrim will copy the uninitialized texture or sysmem to the
576 * uninitialized drawable. That's pointless and we'd have to allocate the texture /
577 * sysmem copy here.
578 */
579 if (This->resource.usage & WINED3DUSAGE_DEPTHSTENCIL) {
580 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
581 } else {
582 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, TRUE);
583 }
584 } else {
585 /* Load the surface into system memory */
586 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
587 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
588 }
589 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
590 This->Flags &= ~SFLAG_ALLOCATED;
591
592 /* Destroy PBOs, but load them into real sysmem before */
593 if(This->Flags & SFLAG_PBO) {
594 surface_remove_pbo(This);
595 }
596
597 /* Destroy fbo render buffers. This is needed for implicit render targets, for
598 * all application-created targets the application has to release the surface
599 * before calling _Reset
600 */
601 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry) {
602 ENTER_GL();
603 GL_EXTCALL(glDeleteRenderbuffersEXT(1, &entry->id));
604 LEAVE_GL();
605 list_remove(&entry->entry);
606 HeapFree(GetProcessHeap(), 0, entry);
607 }
608 list_init(&This->renderbuffers);
609 This->current_renderbuffer = NULL;
610
611 /* If we're in a texture, the texture name belongs to the texture. Otherwise,
612 * destroy it
613 */
614 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **) &texture);
615 if(!texture) {
616 ENTER_GL();
617 glDeleteTextures(1, &This->glDescription.textureName);
618 This->glDescription.textureName = 0;
619 LEAVE_GL();
620 } else {
621 IWineD3DBaseTexture_Release(texture);
622 }
623 return;
624 }
625
626 /* ******************************************************
627 IWineD3DSurface IWineD3DSurface parts follow
628 ****************************************************** */
629
630 void WINAPI IWineD3DSurfaceImpl_SetGlTextureDesc(IWineD3DSurface *iface, UINT textureName, int target) {
631 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
632 TRACE("(%p) : setting textureName %u, target %#x\n", This, textureName, target);
633 if (This->glDescription.textureName == 0 && textureName != 0) {
634 IWineD3DSurface_ModifyLocation(iface, SFLAG_INTEXTURE, FALSE);
635 if((This->Flags & SFLAG_LOCATIONS) == 0) {
636 /* In 1.0-rc4 and earlier, AddDirtyRect was called in the place of this if condition.
637 * This had the problem that a correctly set INDRAWABLE flag was removed if the PreLoad
638 * during the offscreen rendering readback triggered the creation of the GL texture.
639 * The change intended to keep the INDRAWABLE intact. To prevent unintended side effects
640 * before release, set the INSYSMEM flag like the old AddDirtyRect did.
641 */
642 WARN("Wine 1.0 safety path hit\n");
643 This->Flags |= SFLAG_INSYSMEM;
644 }
645 }
646 if(target == GL_TEXTURE_RECTANGLE_ARB && This->glDescription.target != target) {
647 This->Flags &= ~SFLAG_NORMCOORD;
648 } else if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB && target != GL_TEXTURE_RECTANGLE_ARB) {
649 This->Flags |= SFLAG_NORMCOORD;
650 }
651 This->glDescription.textureName = textureName;
652 This->glDescription.target = target;
653 This->Flags &= ~SFLAG_ALLOCATED;
654 }
655
656 void WINAPI IWineD3DSurfaceImpl_GetGlDesc(IWineD3DSurface *iface, glDescriptor **glDescription) {
657 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
658 TRACE("(%p) : returning %p\n", This, &This->glDescription);
659 *glDescription = &This->glDescription;
660 }
661
662 /* TODO: think about moving this down to resource? */
663 const void *WINAPI IWineD3DSurfaceImpl_GetData(IWineD3DSurface *iface) {
664 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
665 /* This should only be called for sysmem textures, it may be a good idea to extend this to all pools at some point in the future */
666 if (This->resource.pool != WINED3DPOOL_SYSTEMMEM) {
667 FIXME(" (%p)Attempting to get system memory for a non-system memory texture\n", iface);
668 }
669 return (CONST void*)(This->resource.allocatedMemory);
670 }
671
672 /* Read the framebuffer back into the surface */
673 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, CONST RECT *rect, void *dest, UINT pitch) {
674 IWineD3DSwapChainImpl *swapchain;
675 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
676 BYTE *mem;
677 GLint fmt;
678 GLint type;
679 BYTE *row, *top, *bottom;
680 int i;
681 BOOL bpp;
682 RECT local_rect;
683 BOOL srcIsUpsideDown;
684
685 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
686 static BOOL warned = FALSE;
687 if(!warned) {
688 ERR("The application tries to lock the render target, but render target locking is disabled\n");
689 warned = TRUE;
690 }
691 return;
692 }
693
694 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
695 /* Activate the surface. Set it up for blitting now, although not necessarily needed for LockRect.
696 * Certain graphics drivers seem to dislike some enabled states when reading from opengl, the blitting usage
697 * should help here. Furthermore unlockrect will need the context set up for blitting. The context manager will find
698 * context->last_was_blit set on the unlock.
699 */
700 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
701 ENTER_GL();
702
703 /* Select the correct read buffer, and give some debug output.
704 * There is no need to keep track of the current read buffer or reset it, every part of the code
705 * that reads sets the read buffer as desired.
706 */
707 if(!swapchain) {
708 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
709 * Read from the back buffer
710 */
711 TRACE("Locking offscreen render target\n");
712 glReadBuffer(myDevice->offscreenBuffer);
713 srcIsUpsideDown = TRUE;
714 } else {
715 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
716 TRACE("Locking %#x buffer\n", buffer);
717 glReadBuffer(buffer);
718 checkGLcall("glReadBuffer");
719
720 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
721 srcIsUpsideDown = FALSE;
722 }
723
724 /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
725 if(!rect) {
726 local_rect.left = 0;
727 local_rect.top = 0;
728 local_rect.right = This->currentDesc.Width;
729 local_rect.bottom = This->currentDesc.Height;
730 } else {
731 local_rect = *rect;
732 }
733 /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
734
735 switch(This->resource.format)
736 {
737 case WINED3DFMT_P8:
738 {
739 if(primary_render_target_is_p8(myDevice)) {
740 /* In case of P8 render targets the index is stored in the alpha component */
741 fmt = GL_ALPHA;
742 type = GL_UNSIGNED_BYTE;
743 mem = dest;
744 bpp = This->bytesPerPixel;
745 } else {
746 /* GL can't return palettized data, so read ARGB pixels into a
747 * separate block of memory and convert them into palettized format
748 * in software. Slow, but if the app means to use palettized render
749 * targets and locks it...
750 *
751 * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
752 * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
753 * for the color channels when palettizing the colors.
754 */
755 fmt = GL_RGB;
756 type = GL_UNSIGNED_BYTE;
757 pitch *= 3;
758 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
759 if(!mem) {
760 ERR("Out of memory\n");
761 LEAVE_GL();
762 return;
763 }
764 bpp = This->bytesPerPixel * 3;
765 }
766 }
767 break;
768
769 default:
770 mem = dest;
771 fmt = This->glDescription.glFormat;
772 type = This->glDescription.glType;
773 bpp = This->bytesPerPixel;
774 }
775
776 if(This->Flags & SFLAG_PBO) {
777 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
778 checkGLcall("glBindBufferARB");
779 }
780
781 glReadPixels(local_rect.left, local_rect.top,
782 local_rect.right - local_rect.left,
783 local_rect.bottom - local_rect.top,
784 fmt, type, mem);
785 vcheckGLcall("glReadPixels");
786
787 if(This->Flags & SFLAG_PBO) {
788 GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
789 checkGLcall("glBindBufferARB");
790
791 /* Check if we need to flip the image. If we need to flip use glMapBufferARB
792 * to get a pointer to it and perform the flipping in software. This is a lot
793 * faster than calling glReadPixels for each line. In case we want more speed
794 * we should rerender it flipped in a FBO and read the data back from the FBO. */
795 if(!srcIsUpsideDown) {
796 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
797 checkGLcall("glBindBufferARB");
798
799 mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
800 checkGLcall("glMapBufferARB");
801 }
802 }
803
804 /* TODO: Merge this with the palettization loop below for P8 targets */
805 if(!srcIsUpsideDown) {
806 UINT len, off;
807 /* glReadPixels returns the image upside down, and there is no way to prevent this.
808 Flip the lines in software */
809 len = (local_rect.right - local_rect.left) * bpp;
810 off = local_rect.left * bpp;
811
812 row = HeapAlloc(GetProcessHeap(), 0, len);
813 if(!row) {
814 ERR("Out of memory\n");
815 if(This->resource.format == WINED3DFMT_P8) HeapFree(GetProcessHeap(), 0, mem);
816 LEAVE_GL();
817 return;
818 }
819
820 top = mem + pitch * local_rect.top;
821 bottom = mem + pitch * ( local_rect.bottom - local_rect.top - 1);
822 for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
823 memcpy(row, top + off, len);
824 memcpy(top + off, bottom + off, len);
825 memcpy(bottom + off, row, len);
826 top += pitch;
827 bottom -= pitch;
828 }
829 HeapFree(GetProcessHeap(), 0, row);
830
831 /* Unmap the temp PBO buffer */
832 if(This->Flags & SFLAG_PBO) {
833 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
834 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
835 }
836 }
837
838 LEAVE_GL();
839
840 /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
841 * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
842 * the same color but we have no choice.
843 * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
844 */
845 if((This->resource.format == WINED3DFMT_P8) && !primary_render_target_is_p8(myDevice)) {
846 PALETTEENTRY *pal = NULL;
847 DWORD width = pitch / 3;
848 int x, y, c;
849
850 if(This->palette) {
851 pal = This->palette->palents;
852 } else {
853 ERR("Palette is missing, cannot perform inverse palette lookup\n");
854 HeapFree(GetProcessHeap(), 0, mem);
855 return ;
856 }
857
858 for(y = local_rect.top; y < local_rect.bottom; y++) {
859 for(x = local_rect.left; x < local_rect.right; x++) {
860 /* start lines pixels */
861 BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
862 BYTE *green = blue + 1;
863 BYTE *red = green + 1;
864
865 for(c = 0; c < 256; c++) {
866 if(*red == pal[c].peRed &&
867 *green == pal[c].peGreen &&
868 *blue == pal[c].peBlue)
869 {
870 *((BYTE *) dest + y * width + x) = c;
871 break;
872 }
873 }
874 }
875 }
876 HeapFree(GetProcessHeap(), 0, mem);
877 }
878 }
879
880 /* Read the framebuffer contents into a texture */
881 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This)
882 {
883 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
884 IWineD3DSwapChainImpl *swapchain;
885 int bpp;
886 GLenum format, internal, type;
887 CONVERT_TYPES convert;
888 GLint prevRead;
889
890 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
891
892 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
893 /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
894 * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
895 * states in the stateblock, and no driver was found yet that had bugs in that regard.
896 */
897 ActivateContext(device, (IWineD3DSurface *) This, CTXUSAGE_RESOURCELOAD);
898 surface_bind_and_dirtify(This);
899 ENTER_GL();
900
901 glGetIntegerv(GL_READ_BUFFER, &prevRead);
902
903 /* Select the correct read buffer, and give some debug output.
904 * There is no need to keep track of the current read buffer or reset it, every part of the code
905 * that reads sets the read buffer as desired.
906 */
907 if(!swapchain) {
908 /* Locking the primary render target which is not on a swapchain(=offscreen render target).
909 * Read from the back buffer
910 */
911 TRACE("Locking offscreen render target\n");
912 glReadBuffer(device->offscreenBuffer);
913 } else {
914 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
915 TRACE("Locking %#x buffer\n", buffer);
916 glReadBuffer(buffer);
917 checkGLcall("glReadBuffer");
918
919 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
920 }
921
922 if(!(This->Flags & SFLAG_ALLOCATED)) {
923 surface_allocate_surface(This, internal, This->pow2Width,
924 This->pow2Height, format, type);
925 }
926
927 clear_unused_channels(This);
928
929 /* If !SrcIsUpsideDown we should flip the surface.
930 * This can be done using glCopyTexSubImage2D but this
931 * is VERY slow, so don't do that. We should prevent
932 * this code from getting called in such cases or perhaps
933 * we can use FBOs */
934
935 glCopyTexSubImage2D(This->glDescription.target,
936 This->glDescription.level,
937 0, 0, 0, 0,
938 This->currentDesc.Width,
939 This->currentDesc.Height);
940 checkGLcall("glCopyTexSubImage2D");
941
942 glReadBuffer(prevRead);
943 vcheckGLcall("glReadBuffer");
944
945 LEAVE_GL();
946 TRACE("Updated target %d\n", This->glDescription.target);
947 }
948
949 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This) {
950 /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
951 * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
952 * changed
953 */
954 if(!(This->Flags & SFLAG_DYNLOCK)) {
955 This->lockCount++;
956 /* MAXLOCKCOUNT is defined in wined3d_private.h */
957 if(This->lockCount > MAXLOCKCOUNT) {
958 TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
959 This->Flags |= SFLAG_DYNLOCK;
960 }
961 }
962
963 /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
964 * Also don't create a PBO for systemmem surfaces.
965 */
966 if(GL_SUPPORT(ARB_PIXEL_BUFFER_OBJECT) && (This->Flags & SFLAG_DYNLOCK) && !(This->Flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2)) && (This->resource.pool != WINED3DPOOL_SYSTEMMEM)) {
967 GLenum error;
968 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
969
970 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
971 ENTER_GL();
972
973 GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
974 error = glGetError();
975 if(This->pbo == 0 || error != GL_NO_ERROR) {
976 ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
977 }
978
979 TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
980
981 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
982 checkGLcall("glBindBufferARB");
983
984 GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
985 checkGLcall("glBufferDataARB");
986
987 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
988 checkGLcall("glBindBufferARB");
989
990 /* We don't need the system memory anymore and we can't even use it for PBOs */
991 if(!(This->Flags & SFLAG_CLIENT)) {
992 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
993 This->resource.heapMemory = NULL;
994 }
995 This->resource.allocatedMemory = NULL;
996 This->Flags |= SFLAG_PBO;
997 LEAVE_GL();
998 } else if(!(This->resource.allocatedMemory || This->Flags & SFLAG_PBO)) {
999 /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1000 * or a pbo to map
1001 */
1002 if(!This->resource.heapMemory) {
1003 This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1004 }
1005 This->resource.allocatedMemory =
1006 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1007 if(This->Flags & SFLAG_INSYSMEM) {
1008 ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1009 }
1010 }
1011 }
1012
1013 static HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, WINED3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
1014 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1015 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1016 IWineD3DSwapChain *swapchain = NULL;
1017
1018 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->resource.allocatedMemory);
1019
1020 /* This is also done in the base class, but we have to verify this before loading any data from
1021 * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1022 * may interfere, and all other bad things may happen
1023 */
1024 if (This->Flags & SFLAG_LOCKED) {
1025 WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1026 return WINED3DERR_INVALIDCALL;
1027 }
1028 This->Flags |= SFLAG_LOCKED;
1029
1030 if (!(This->Flags & SFLAG_LOCKABLE))
1031 {
1032 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1033 }
1034
1035 if (Flags & WINED3DLOCK_DISCARD) {
1036 /* Set SFLAG_INSYSMEM, so we'll never try to download the data from the texture. */
1037 TRACE("WINED3DLOCK_DISCARD flag passed, marking local copy as up to date\n");
1038 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1039 This->Flags |= SFLAG_INSYSMEM;
1040 goto lock_end;
1041 }
1042
1043 if (This->Flags & SFLAG_INSYSMEM) {
1044 TRACE("Local copy is up to date, not downloading data\n");
1045 surface_prepare_system_memory(This); /* Makes sure memory is allocated */
1046 goto lock_end;
1047 }
1048
1049 /* Now download the surface content from opengl
1050 * Use the render target readback if the surface is on a swapchain(=onscreen render target) or the current primary target
1051 * Offscreen targets which are not active at the moment or are higher targets(FBOs) can be locked with the texture path
1052 */
1053 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1054 if(swapchain || iface == myDevice->render_targets[0]) {
1055 const RECT *pass_rect = pRect;
1056
1057 /* IWineD3DSurface_LoadLocation does not check if the rectangle specifies the full surfaces
1058 * because most caller functions do not need that. So do that here
1059 */
1060 if(pRect &&
1061 pRect->top == 0 &&
1062 pRect->left == 0 &&
1063 pRect->right == This->currentDesc.Width &&
1064 pRect->bottom == This->currentDesc.Height) {
1065 pass_rect = NULL;
1066 }
1067
1068 switch(wined3d_settings.rendertargetlock_mode) {
1069 case RTL_TEXDRAW:
1070 case RTL_TEXTEX:
1071 FIXME("Reading from render target with a texture isn't implemented yet, falling back to framebuffer reading\n");
1072 #if 0
1073 /* Disabled for now. LoadLocation prefers the texture over the drawable as the source. So if we copy to the
1074 * texture first, then to sysmem, we'll avoid glReadPixels and use glCopyTexImage and glGetTexImage2D instead.
1075 * This may be faster on some cards
1076 */
1077 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* No partial texture copy yet */);
1078 #endif
1079 /* drop through */
1080
1081 case RTL_AUTO:
1082 case RTL_READDRAW:
1083 case RTL_READTEX:
1084 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, pRect);
1085 break;
1086
1087 case RTL_DISABLE:
1088 break;
1089 }
1090 if(swapchain) IWineD3DSwapChain_Release(swapchain);
1091
1092 } else if(iface == myDevice->stencilBufferTarget) {
1093 /** the depth stencil in openGL has a format of GL_FLOAT
1094 * which should be good for WINED3DFMT_D16_LOCKABLE
1095 * and WINED3DFMT_D16
1096 * it is unclear what format the stencil buffer is in except.
1097 * 'Each index is converted to fixed point...
1098 * If GL_MAP_STENCIL is GL_TRUE, indices are replaced by their
1099 * mappings in the table GL_PIXEL_MAP_S_TO_S.
1100 * glReadPixels(This->lockedRect.left,
1101 * This->lockedRect.bottom - j - 1,
1102 * This->lockedRect.right - This->lockedRect.left,
1103 * 1,
1104 * GL_DEPTH_COMPONENT,
1105 * type,
1106 * (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
1107 *
1108 * Depth Stencil surfaces which are not the current depth stencil target should have their data in a
1109 * gl texture(next path), or in local memory(early return because of set SFLAG_INSYSMEM above). If
1110 * none of that is the case the problem is not in this function :-)
1111 ********************************************/
1112 FIXME("Depth stencil locking not supported yet\n");
1113 } else {
1114 /* This path is for normal surfaces, offscreen render targets and everything else that is in a gl texture */
1115 TRACE("locking an ordinary surface\n");
1116 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
1117 }
1118
1119 lock_end:
1120 if(This->Flags & SFLAG_PBO) {
1121 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1122 ENTER_GL();
1123 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1124 checkGLcall("glBindBufferARB");
1125
1126 /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1127 if(This->resource.allocatedMemory) {
1128 ERR("The surface already has PBO memory allocated!\n");
1129 }
1130
1131 This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1132 checkGLcall("glMapBufferARB");
1133
1134 /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1135 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1136 checkGLcall("glBindBufferARB");
1137
1138 LEAVE_GL();
1139 }
1140
1141 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
1142 /* Don't dirtify */
1143 } else {
1144 IWineD3DBaseTexture *pBaseTexture;
1145 /**
1146 * Dirtify on lock
1147 * as seen in msdn docs
1148 */
1149 IWineD3DSurface_AddDirtyRect(iface, pRect);
1150
1151 /** Dirtify Container if needed */
1152 if (WINED3D_OK == IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&pBaseTexture) && pBaseTexture != NULL) {
1153 TRACE("Making container dirty\n");
1154 IWineD3DBaseTexture_SetDirty(pBaseTexture, TRUE);
1155 IWineD3DBaseTexture_Release(pBaseTexture);
1156 } else {
1157 TRACE("Surface is standalone, no need to dirty the container\n");
1158 }
1159 }
1160
1161 return IWineD3DBaseSurfaceImpl_LockRect(iface, pLockedRect, pRect, Flags);
1162 }
1163
1164 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *This, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem) {
1165 GLint prev_store;
1166 GLint prev_rasterpos[4];
1167 GLint skipBytes = 0;
1168 UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This); /* target is argb, 4 byte */
1169 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1170 IWineD3DSwapChainImpl *swapchain;
1171
1172 /* Activate the correct context for the render target */
1173 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
1174 ENTER_GL();
1175
1176 IWineD3DSurface_GetContainer((IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&swapchain);
1177 if(!swapchain) {
1178 /* Primary offscreen render target */
1179 TRACE("Offscreen render target\n");
1180 glDrawBuffer(myDevice->offscreenBuffer);
1181 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1182 } else {
1183 GLenum buffer = surface_get_gl_buffer((IWineD3DSurface *) This, (IWineD3DSwapChain *)swapchain);
1184 TRACE("Unlocking %#x buffer\n", buffer);
1185 glDrawBuffer(buffer);
1186 checkGLcall("glDrawBuffer");
1187
1188 IWineD3DSwapChain_Release((IWineD3DSwapChain *)swapchain);
1189 }
1190
1191 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
1192 vcheckGLcall("glIntegerv");
1193 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
1194 vcheckGLcall("glIntegerv");
1195 glPixelZoom(1.0, -1.0);
1196 vcheckGLcall("glPixelZoom");
1197
1198 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
1199 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
1200 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
1201
1202 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
1203 vcheckGLcall("glRasterPos2f");
1204
1205 /* Some drivers(radeon dri, others?) don't like exceptions during
1206 * glDrawPixels. If the surface is a DIB section, it might be in GDIMode
1207 * after ReleaseDC. Reading it will cause an exception, which x11drv will
1208 * catch to put the dib section in InSync mode, which leads to a crash
1209 * and a blocked x server on my radeon card.
1210 *
1211 * The following lines read the dib section so it is put in InSync mode
1212 * before glDrawPixels is called and the crash is prevented. There won't
1213 * be any interfering gdi accesses, because UnlockRect is called from
1214 * ReleaseDC, and the app won't use the dc any more afterwards.
1215 */
1216 if((This->Flags & SFLAG_DIBSECTION) && !(This->Flags & SFLAG_PBO)) {
1217 volatile BYTE read;
1218 read = This->resource.allocatedMemory[0];
1219 }
1220
1221 if(This->Flags & SFLAG_PBO) {
1222 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1223 checkGLcall("glBindBufferARB");
1224 }
1225
1226 /* When the surface is locked we only have to refresh the locked part else we need to update the whole image */
1227 if(This->Flags & SFLAG_LOCKED) {
1228 glDrawPixels(This->lockedRect.right - This->lockedRect.left,
1229 (This->lockedRect.bottom - This->lockedRect.top)-1,
1230 fmt, type,
1231 mem + bpp * This->lockedRect.left + pitch * This->lockedRect.top);
1232 checkGLcall("glDrawPixels");
1233 } else {
1234 glDrawPixels(This->currentDesc.Width,
1235 This->currentDesc.Height,
1236 fmt, type, mem);
1237 checkGLcall("glDrawPixels");
1238 }
1239
1240 if(This->Flags & SFLAG_PBO) {
1241 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1242 checkGLcall("glBindBufferARB");
1243 }
1244
1245 glPixelZoom(1.0,1.0);
1246 vcheckGLcall("glPixelZoom");
1247
1248 glRasterPos3iv(&prev_rasterpos[0]);
1249 vcheckGLcall("glRasterPos3iv");
1250
1251 /* Reset to previous pack row length */
1252 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
1253 vcheckGLcall("glPixelStorei GL_UNPACK_ROW_LENGTH");
1254
1255 if(!swapchain) {
1256 glDrawBuffer(myDevice->offscreenBuffer);
1257 checkGLcall("glDrawBuffer(myDevice->offscreenBuffer)");
1258 } else if(swapchain->backBuffer) {
1259 glDrawBuffer(GL_BACK);
1260 checkGLcall("glDrawBuffer(GL_BACK)");
1261 } else {
1262 glDrawBuffer(GL_FRONT);
1263 checkGLcall("glDrawBuffer(GL_FRONT)");
1264 }
1265 LEAVE_GL();
1266
1267 return;
1268 }
1269
1270 static HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
1271 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1272 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
1273 IWineD3DSwapChainImpl *swapchain = NULL;
1274 BOOL fullsurface;
1275
1276 if (!(This->Flags & SFLAG_LOCKED)) {
1277 WARN("trying to Unlock an unlocked surf@%p\n", This);
1278 return WINED3DERR_INVALIDCALL;
1279 }
1280
1281 if (This->Flags & SFLAG_PBO) {
1282 TRACE("Freeing PBO memory\n");
1283 ActivateContext(myDevice, myDevice->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
1284 ENTER_GL();
1285 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1286 GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1287 GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1288 checkGLcall("glUnmapBufferARB");
1289 LEAVE_GL();
1290 This->resource.allocatedMemory = NULL;
1291 }
1292
1293 TRACE("(%p) : dirtyfied(%d)\n", This, This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
1294
1295 if (This->Flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE)) {
1296 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
1297 goto unlock_end;
1298 }
1299
1300 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain);
1301 if(swapchain || (myDevice->render_targets && iface == myDevice->render_targets[0])) {
1302 if(swapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
1303
1304 if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1305 static BOOL warned = FALSE;
1306 if(!warned) {
1307 ERR("The application tries to write to the render target, but render target locking is disabled\n");
1308 warned = TRUE;
1309 }
1310 goto unlock_end;
1311 }
1312
1313 if(This->dirtyRect.left == 0 &&
1314 This->dirtyRect.top == 0 &&
1315 This->dirtyRect.right == This->currentDesc.Width &&
1316 This->dirtyRect.bottom == This->currentDesc.Height) {
1317 fullsurface = TRUE;
1318 } else {
1319 /* TODO: Proper partial rectangle tracking */
1320 fullsurface = FALSE;
1321 This->Flags |= SFLAG_INSYSMEM;
1322 }
1323
1324 switch(wined3d_settings.rendertargetlock_mode) {
1325 case RTL_READTEX:
1326 case RTL_TEXTEX:
1327 ActivateContext(myDevice, iface, CTXUSAGE_BLIT);
1328 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* partial texture loading not supported yet */);
1329 /* drop through */
1330
1331 case RTL_AUTO:
1332 case RTL_READDRAW:
1333 case RTL_TEXDRAW:
1334 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
1335 break;
1336 }
1337
1338 if(!fullsurface) {
1339 /* Partial rectangle tracking is not commonly implemented, it is only done for render targets. Overwrite
1340 * the flags to bring them back into a sane state. INSYSMEM was set before to tell LoadLocation where
1341 * to read the rectangle from. Indrawable is set because all modifications from the partial sysmem copy
1342 * are written back to the drawable, thus the surface is merged again in the drawable. The sysmem copy is
1343 * not fully up to date because only a subrectangle was read in LockRect.
1344 */
1345 This->Flags &= ~SFLAG_INSYSMEM;
1346 This->Flags |= SFLAG_INDRAWABLE;
1347 }
1348
1349 This->dirtyRect.left = This->currentDesc.Width;
1350 This->dirtyRect.top = This->currentDesc.Height;
1351 This->dirtyRect.right = 0;
1352 This->dirtyRect.bottom = 0;
1353 } else if(iface == myDevice->stencilBufferTarget) {
1354 FIXME("Depth Stencil buffer locking is not implemented\n");
1355 } else {
1356 /* The rest should be a normal texture */
1357 IWineD3DBaseTextureImpl *impl;
1358 /* Check if the texture is bound, if yes dirtify the sampler to force a re-upload of the texture
1359 * Can't load the texture here because PreLoad may destroy and recreate the gl texture, so sampler
1360 * states need resetting
1361 */
1362 if(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&impl) == WINED3D_OK) {
1363 if(impl->baseTexture.bindCount) {
1364 IWineD3DDeviceImpl_MarkStateDirty(myDevice, STATE_SAMPLER(impl->baseTexture.sampler));
1365 }
1366 IWineD3DBaseTexture_Release((IWineD3DBaseTexture *) impl);
1367 }
1368 }
1369
1370 unlock_end:
1371 This->Flags &= ~SFLAG_LOCKED;
1372 memset(&This->lockedRect, 0, sizeof(RECT));
1373
1374 /* Overlays have to be redrawn manually after changes with the GL implementation */
1375 if(This->overlay_dest) {
1376 IWineD3DSurface_DrawOverlay(iface);
1377 }
1378 return WINED3D_OK;
1379 }
1380
1381 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
1382 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1383 WINED3DLOCKED_RECT lock;
1384 HRESULT hr;
1385 RGBQUAD col[256];
1386
1387 TRACE("(%p)->(%p)\n",This,pHDC);
1388
1389 if(This->Flags & SFLAG_USERPTR) {
1390 ERR("Not supported on surfaces with an application-provided surfaces\n");
1391 return WINEDDERR_NODC;
1392 }
1393
1394 /* Give more detailed info for ddraw */
1395 if (This->Flags & SFLAG_DCINUSE)
1396 return WINEDDERR_DCALREADYCREATED;
1397
1398 /* Can't GetDC if the surface is locked */
1399 if (This->Flags & SFLAG_LOCKED)
1400 return WINED3DERR_INVALIDCALL;
1401
1402 /* According to Direct3D9 docs, only these formats are supported */
1403 if (((IWineD3DImpl *)This->resource.wineD3DDevice->wineD3D)->dxVersion > 7) {
1404 if (This->resource.format != WINED3DFMT_R5G6B5 &&
1405 This->resource.format != WINED3DFMT_X1R5G5B5 &&
1406 This->resource.format != WINED3DFMT_R8G8B8 &&
1407 This->resource.format != WINED3DFMT_X8R8G8B8) return WINED3DERR_INVALIDCALL;
1408 }
1409
1410 memset(&lock, 0, sizeof(lock)); /* To be sure */
1411
1412 /* Create a DIB section if there isn't a hdc yet */
1413 if(!This->hDC) {
1414 IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
1415 if(This->Flags & SFLAG_CLIENT) {
1416 IWineD3DSurface_PreLoad(iface);
1417 }
1418
1419 /* Use the dib section from now on if we are not using a PBO */
1420 if(!(This->Flags & SFLAG_PBO))
1421 This->resource.allocatedMemory = This->dib.bitmap_data;
1422 }
1423
1424 /* Lock the surface */
1425 hr = IWineD3DSurface_LockRect(iface,
1426 &lock,
1427 NULL,
1428 0);
1429
1430 if(This->Flags & SFLAG_PBO) {
1431 /* Sync the DIB with the PBO. This can't be done earlier because LockRect activates the allocatedMemory */
1432 memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
1433 }
1434
1435 if(FAILED(hr)) {
1436 ERR("IWineD3DSurface_LockRect failed with hr = %08x\n", hr);
1437 /* keep the dib section */
1438 return hr;
1439 }
1440
1441 if(This->resource.format == WINED3DFMT_P8 ||
1442 This->resource.format == WINED3DFMT_A8P8) {
1443 /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
1444 D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
1445 unsigned int n;
1446 PALETTEENTRY *pal = NULL;
1447
1448 if(This->palette) {
1449 pal = This->palette->palents;
1450 } else {
1451 IWineD3DSurfaceImpl *dds_primary;
1452 IWineD3DSwapChainImpl *swapchain;
1453 swapchain = (IWineD3DSwapChainImpl *)This->resource.wineD3DDevice->swapchains[0];
1454 dds_primary = (IWineD3DSurfaceImpl *)swapchain->frontBuffer;
1455 if (dds_primary && dds_primary->palette)
1456 pal = dds_primary->palette->palents;
1457 }
1458
1459 if (pal) {
1460 for (n=0; n<256; n++) {
1461 col[n].rgbRed = pal[n].peRed;
1462 col[n].rgbGreen = pal[n].peGreen;
1463 col[n].rgbBlue = pal[n].peBlue;
1464 col[n].rgbReserved = 0;
1465 }
1466 SetDIBColorTable(This->hDC, 0, 256, col);
1467 }
1468 }
1469
1470 *pHDC = This->hDC;
1471 TRACE("returning %p\n",*pHDC);
1472 This->Flags |= SFLAG_DCINUSE;
1473
1474 return WINED3D_OK;
1475 }
1476
1477 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
1478 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1479
1480 TRACE("(%p)->(%p)\n",This,hDC);
1481
1482 if (!(This->Flags & SFLAG_DCINUSE))
1483 return WINED3DERR_INVALIDCALL;
1484
1485 if (This->hDC !=hDC) {
1486 WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
1487 return WINED3DERR_INVALIDCALL;
1488 }
1489
1490 if((This->Flags & SFLAG_PBO) && This->resource.allocatedMemory) {
1491 /* Copy the contents of the DIB over to the PBO */
1492 memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
1493 }
1494
1495 /* we locked first, so unlock now */
1496 IWineD3DSurface_UnlockRect(iface);
1497
1498 This->Flags &= ~SFLAG_DCINUSE;
1499
1500 return WINED3D_OK;
1501 }
1502
1503 /* ******************************************************
1504 IWineD3DSurface Internal (No mapping to directx api) parts follow
1505 ****************************************************** */
1506
1507 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) {
1508 BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
1509 const GlPixelFormatDesc *glDesc;
1510 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
1511 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
1512
1513 /* Default values: From the surface */
1514 *format = glDesc->glFormat;
1515 *type = glDesc->glType;
1516 *convert = NO_CONVERSION;
1517 *target_bpp = This->bytesPerPixel;
1518
1519 if(srgb_mode) {
1520 *internal = glDesc->glGammaInternal;
1521 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
1522 *internal = glDesc->rtInternal;
1523 } else {
1524 *internal = glDesc->glInternal;
1525 }
1526
1527 /* Ok, now look if we have to do any conversion */
1528 switch(This->resource.format) {
1529 case WINED3DFMT_P8:
1530 /* ****************
1531 Paletted Texture
1532 **************** */
1533
1534 /* Use conversion when the paletted texture extension OR fragment shaders are available. When either
1535 * of the two is available make sure texturing is requested as neither of the two works in
1536 * conjunction with calls like glDraw-/glReadPixels. Further also use conversion in case of color keying.
1537 * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
1538 * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
1539 * conflicts with this.
1540 */
1541 if( !(GL_SUPPORT(EXT_PALETTED_TEXTURE) ||
1542 (GL_SUPPORT(ARB_FRAGMENT_PROGRAM) &&
1543 device->render_targets &&
1544 This == (IWineD3DSurfaceImpl*)device->render_targets[0])) ||
1545 colorkey_active || !use_texturing ) {
1546 *format = GL_RGBA;
1547 *internal = GL_RGBA;
1548 *type = GL_UNSIGNED_BYTE;
1549 *target_bpp = 4;
1550 if(colorkey_active) {
1551 *convert = CONVERT_PALETTED_CK;
1552 } else {
1553 *convert = CONVERT_PALETTED;
1554 }
1555 }
1556 else if(!GL_SUPPORT(EXT_PALETTED_TEXTURE) && GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) {
1557 *format = GL_ALPHA;
1558 *internal = GL_RGBA;
1559 *type = GL_UNSIGNED_BYTE;
1560 *target_bpp = 1;
1561 }
1562
1563 break;
1564
1565 case WINED3DFMT_R3G3B2:
1566 /* **********************
1567 GL_UNSIGNED_BYTE_3_3_2
1568 ********************** */
1569 if (colorkey_active) {
1570 /* This texture format will never be used.. So do not care about color keying
1571 up until the point in time it will be needed :-) */
1572 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
1573 }
1574 break;
1575
1576 case WINED3DFMT_R5G6B5:
1577 if (colorkey_active) {
1578 *convert = CONVERT_CK_565;
1579 *format = GL_RGBA;
1580 *internal = GL_RGBA;
1581 *type = GL_UNSIGNED_SHORT_5_5_5_1;
1582 }
1583 break;
1584
1585 case WINED3DFMT_X1R5G5B5:
1586 if (colorkey_active) {
1587 *convert = CONVERT_CK_5551;
1588 *format = GL_BGRA;
1589 *internal = GL_RGBA;
1590 *type = GL_UNSIGNED_SHORT_1_5_5_5_REV;
1591 }
1592 break;
1593
1594 case WINED3DFMT_R8G8B8:
1595 if (colorkey_active) {
1596 *convert = CONVERT_CK_RGB24;
1597 *format = GL_RGBA;
1598 *internal = GL_RGBA;
1599 *type = GL_UNSIGNED_INT_8_8_8_8;
1600 *target_bpp = 4;
1601 }
1602 break;
1603
1604 case WINED3DFMT_X8R8G8B8:
1605 if (colorkey_active) {
1606 *convert = CONVERT_RGB32_888;
1607 *format = GL_RGBA;
1608 *internal = GL_RGBA;
1609 *type = GL_UNSIGNED_INT_8_8_8_8;
1610 }
1611 break;
1612
1613 case WINED3DFMT_V8U8:
1614 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1615 else if(GL_SUPPORT(ATI_ENVMAP_BUMPMAP)) {
1616 *format = GL_DUDV_ATI;
1617 *internal = GL_DU8DV8_ATI;
1618 *type = GL_BYTE;
1619 /* No conversion - Just change the gl type */
1620 break;
1621 }
1622 *convert = CONVERT_V8U8;
1623 *format = GL_BGR;
1624 *internal = GL_RGB8;
1625 *type = GL_UNSIGNED_BYTE;
1626 *target_bpp = 3;
1627 break;
1628
1629 case WINED3DFMT_L6V5U5:
1630 *convert = CONVERT_L6V5U5;
1631 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1632 *target_bpp = 3;
1633 /* Use format and types from table */
1634 } else {
1635 /* Load it into unsigned R5G6B5, swap L and V channels, and revert that in the shader */
1636 *target_bpp = 2;
1637 *format = GL_RGB;
1638 *internal = GL_RGB5;
1639 *type = GL_UNSIGNED_SHORT_5_6_5;
1640 }
1641 break;
1642
1643 case WINED3DFMT_X8L8V8U8:
1644 *convert = CONVERT_X8L8V8U8;
1645 *target_bpp = 4;
1646 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1647 /* Use formats from gl table. It is a bit unfortunate, but the conversion
1648 * is needed to set the X format to 255 to get 1.0 for alpha when sampling
1649 * the texture. OpenGL can't use GL_DSDT8_MAG8_NV as internal format with
1650 * the needed type and format parameter, so the internal format contains a
1651 * 4th component, which is returned as alpha
1652 */
1653 } else {
1654 /* Not supported by GL_ATI_envmap_bumpmap */
1655 *format = GL_BGRA;
1656 *internal = GL_RGB8;
1657 *type = GL_UNSIGNED_INT_8_8_8_8_REV;
1658 }
1659 break;
1660
1661 case WINED3DFMT_Q8W8V8U8:
1662 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1663 *convert = CONVERT_Q8W8V8U8;
1664 *format = GL_BGRA;
1665 *internal = GL_RGBA8;
1666 *type = GL_UNSIGNED_BYTE;
1667 *target_bpp = 4;
1668 /* Not supported by GL_ATI_envmap_bumpmap */
1669 break;
1670
1671 case WINED3DFMT_V16U16:
1672 if(GL_SUPPORT(NV_TEXTURE_SHADER3)) break;
1673 *convert = CONVERT_V16U16;
1674 *format = GL_BGR;
1675 *internal = GL_RGB16_EXT;
1676 *type = GL_UNSIGNED_SHORT;
1677 *target_bpp = 6;
1678 /* What should I do here about GL_ATI_envmap_bumpmap?
1679 * Convert it or allow data loss by loading it into a 8 bit / channel texture?
1680 */
1681 break;
1682
1683 case WINED3DFMT_A4L4:
1684 /* A4L4 exists as an internal gl format, but for some reason there is not
1685 * format+type combination to load it. Thus convert it to A8L8, then load it
1686 * with A4L4 internal, but A8L8 format+type
1687 */
1688 *convert = CONVERT_A4L4;
1689 *format = GL_LUMINANCE_ALPHA;
1690 *internal = GL_LUMINANCE4_ALPHA4;
1691 *type = GL_UNSIGNED_BYTE;
1692 *target_bpp = 2;
1693 break;
1694
1695 case WINED3DFMT_R32F:
1696 /* Can be loaded in theory with fmt=GL_RED, type=GL_FLOAT, but this fails. The reason
1697 * is that D3D expects the undefined green, blue and alpha channels to return 1.0
1698 * when sampling, but OpenGL sets green and blue to 0.0 instead. Thus we have to inject
1699 * 1.0 instead.
1700 *
1701 * The alpha channel defaults to 1.0 in opengl, so nothing has to be done about it.
1702 */
1703 *convert = CONVERT_R32F;
1704 *format = GL_RGB;
1705 *internal = GL_RGB32F_ARB;
1706 *type = GL_FLOAT;
1707 *target_bpp = 12;
1708 break;
1709
1710 case WINED3DFMT_R16F:
1711 /* Similar to R32F */
1712 *convert = CONVERT_R16F;
1713 *format = GL_RGB;
1714 *internal = GL_RGB16F_ARB;
1715 *type = GL_HALF_FLOAT_ARB;
1716 *target_bpp = 6;
1717 break;
1718
1719 case WINED3DFMT_G16R16:
1720 *convert = CONVERT_G16R16;
1721 *format = GL_RGB;
1722 *internal = GL_RGB16_EXT;
1723 *type = GL_UNSIGNED_SHORT;
1724 *target_bpp = 6;
1725 break;
1726
1727 default:
1728 break;
1729 }
1730
1731 return WINED3D_OK;
1732 }
1733
1734 HRESULT d3dfmt_convert_surface(BYTE *src, BYTE *dst, UINT pitch, UINT width, UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This) {
1735 BYTE *source, *dest;
1736 TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
1737
1738 switch (convert) {
1739 case NO_CONVERSION:
1740 {
1741 memcpy(dst, src, pitch * height);
1742 break;
1743 }
1744 case CONVERT_PALETTED:
1745 case CONVERT_PALETTED_CK:
1746 {
1747 IWineD3DPaletteImpl* pal = This->palette;
1748 BYTE table[256][4];
1749 unsigned int x, y;
1750
1751 if( pal == NULL) {
1752 /* TODO: If we are a sublevel, try to get the palette from level 0 */
1753 }
1754
1755 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
1756
1757 for (y = 0; y < height; y++)
1758 {
1759 source = src + pitch * y;
1760 dest = dst + outpitch * y;
1761 /* This is an 1 bpp format, using the width here is fine */
1762 for (x = 0; x < width; x++) {
1763 BYTE color = *source++;
1764 *dest++ = table[color][0];
1765 *dest++ = table[color][1];
1766 *dest++ = table[color][2];
1767 *dest++ = table[color][3];
1768 }
1769 }
1770 }
1771 break;
1772
1773 case CONVERT_CK_565:
1774 {
1775 /* Converting the 565 format in 5551 packed to emulate color-keying.
1776
1777 Note : in all these conversion, it would be best to average the averaging
1778 pixels to get the color of the pixel that will be color-keyed to
1779 prevent 'color bleeding'. This will be done later on if ever it is
1780 too visible.
1781
1782 Note2: Nvidia documents say that their driver does not support alpha + color keying
1783 on the same surface and disables color keying in such a case
1784 */
1785 unsigned int x, y;
1786 WORD *Source;
1787 WORD *Dest;
1788
1789 TRACE("Color keyed 565\n");
1790
1791 for (y = 0; y < height; y++) {
1792 Source = (WORD *) (src + y * pitch);
1793 Dest = (WORD *) (dst + y * outpitch);
1794 for (x = 0; x < width; x++ ) {
1795 WORD color = *Source++;
1796 *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
1797 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1798 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1799 *Dest |= 0x0001;
1800 }
1801 Dest++;
1802 }
1803 }
1804 }
1805 break;
1806
1807 case CONVERT_CK_5551:
1808 {
1809 /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
1810 unsigned int x, y;
1811 WORD *Source;
1812 WORD *Dest;
1813 TRACE("Color keyed 5551\n");
1814 for (y = 0; y < height; y++) {
1815 Source = (WORD *) (src + y * pitch);
1816 Dest = (WORD *) (dst + y * outpitch);
1817 for (x = 0; x < width; x++ ) {
1818 WORD color = *Source++;
1819 *Dest = color;
1820 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1821 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1822 *Dest |= (1 << 15);
1823 }
1824 else {
1825 *Dest &= ~(1 << 15);
1826 }
1827 Dest++;
1828 }
1829 }
1830 }
1831 break;
1832
1833 case CONVERT_RGB32_888:
1834 {
1835 /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
1836 unsigned int x, y;
1837 for (y = 0; y < height; y++)
1838 {
1839 source = src + pitch * y;
1840 dest = dst + outpitch * y;
1841 for (x = 0; x < width; x++) {
1842 DWORD color = 0xffffff & *(DWORD*)source;
1843 DWORD dstcolor = color << 8;
1844 if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
1845 (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
1846 dstcolor |= 0xff;
1847 }
1848 *(DWORD*)dest = dstcolor;
1849 source += 4;
1850 dest += 4;
1851 }
1852 }
1853 }
1854 break;
1855
1856 case CONVERT_V8U8:
1857 {
1858 unsigned int x, y;
1859 short *Source;
1860 unsigned char *Dest;
1861 for(y = 0; y < height; y++) {
1862 Source = (short *) (src + y * pitch);
1863 Dest = dst + y * outpitch;
1864 for (x = 0; x < width; x++ ) {
1865 long color = (*Source++);
1866 /* B */ Dest[0] = 0xff;
1867 /* G */ Dest[1] = (color >> 8) + 128; /* V */
1868 /* R */ Dest[2] = (color) + 128; /* U */
1869 Dest += 3;
1870 }
1871 }
1872 break;
1873 }
1874
1875 case CONVERT_V16U16:
1876 {
1877 unsigned int x, y;
1878 DWORD *Source;
1879 unsigned short *Dest;
1880 for(y = 0; y < height; y++) {
1881 Source = (DWORD *) (src + y * pitch);
1882 Dest = (unsigned short *) (dst + y * outpitch);
1883 for (x = 0; x < width; x++ ) {
1884 DWORD color = (*Source++);
1885 /* B */ Dest[0] = 0xffff;
1886 /* G */ Dest[1] = (color >> 16) + 32768; /* V */
1887 /* R */ Dest[2] = (color ) + 32768; /* U */
1888 Dest += 3;
1889 }
1890 }
1891 break;
1892 }
1893
1894 case CONVERT_Q8W8V8U8:
1895 {
1896 unsigned int x, y;
1897 DWORD *Source;
1898 unsigned char *Dest;
1899 for(y = 0; y < height; y++) {
1900 Source = (DWORD *) (src + y * pitch);
1901 Dest = dst + y * outpitch;
1902 for (x = 0; x < width; x++ ) {
1903 long color = (*Source++);
1904 /* B */ Dest[0] = ((color >> 16) & 0xff) + 128; /* W */
1905 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
1906 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
1907 /* A */ Dest[3] = ((color >> 24) & 0xff) + 128; /* Q */
1908 Dest += 4;
1909 }
1910 }
1911 break;
1912 }
1913
1914 case CONVERT_L6V5U5:
1915 {
1916 unsigned int x, y;
1917 WORD *Source;
1918 unsigned char *Dest;
1919
1920 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1921 /* This makes the gl surface bigger(24 bit instead of 16), but it works with
1922 * fixed function and shaders without further conversion once the surface is
1923 * loaded
1924 */
1925 for(y = 0; y < height; y++) {
1926 Source = (WORD *) (src + y * pitch);
1927 Dest = dst + y * outpitch;
1928 for (x = 0; x < width; x++ ) {
1929 short color = (*Source++);
1930 unsigned char l = ((color >> 10) & 0xfc);
1931 char v = ((color >> 5) & 0x3e);
1932 char u = ((color ) & 0x1f);
1933
1934 /* 8 bits destination, 6 bits source, 8th bit is the sign. gl ignores the sign
1935 * and doubles the positive range. Thus shift left only once, gl does the 2nd
1936 * shift. GL reads a signed value and converts it into an unsigned value.
1937 */
1938 /* M */ Dest[2] = l << 1;
1939
1940 /* Those are read as signed, but kept signed. Just left-shift 3 times to scale
1941 * from 5 bit values to 8 bit values.
1942 */
1943 /* V */ Dest[1] = v << 3;
1944 /* U */ Dest[0] = u << 3;
1945 Dest += 3;
1946 }
1947 }
1948 } else {
1949 for(y = 0; y < height; y++) {
1950 unsigned short *Dest_s = (unsigned short *) (dst + y * outpitch);
1951 Source = (WORD *) (src + y * pitch);
1952 for (x = 0; x < width; x++ ) {
1953 short color = (*Source++);
1954 unsigned char l = ((color >> 10) & 0xfc);
1955 short v = ((color >> 5) & 0x3e);
1956 short u = ((color ) & 0x1f);
1957 short v_conv = v + 16;
1958 short u_conv = u + 16;
1959
1960 *Dest_s = ((v_conv << 11) & 0xf800) | ((l << 5) & 0x7e0) | (u_conv & 0x1f);
1961 Dest_s += 1;
1962 }
1963 }
1964 }
1965 break;
1966 }
1967
1968 case CONVERT_X8L8V8U8:
1969 {
1970 unsigned int x, y;
1971 DWORD *Source;
1972 unsigned char *Dest;
1973
1974 if(GL_SUPPORT(NV_TEXTURE_SHADER)) {
1975 /* This implementation works with the fixed function pipeline and shaders
1976 * without further modification after converting the surface.
1977 */
1978 for(y = 0; y < height; y++) {
1979 Source = (DWORD *) (src + y * pitch);
1980 Dest = dst + y * outpitch;
1981 for (x = 0; x < width; x++ ) {
1982 long color = (*Source++);
1983 /* L */ Dest[2] = ((color >> 16) & 0xff); /* L */
1984 /* V */ Dest[1] = ((color >> 8 ) & 0xff); /* V */
1985 /* U */ Dest[0] = (color & 0xff); /* U */
1986 /* I */ Dest[3] = 255; /* X */
1987 Dest += 4;
1988 }
1989 }
1990 } else {
1991 /* Doesn't work correctly with the fixed function pipeline, but can work in
1992 * shaders if the shader is adjusted. (There's no use for this format in gl's
1993 * standard fixed function pipeline anyway).
1994 */
1995 for(y = 0; y < height; y++) {
1996 Source = (DWORD *) (src + y * pitch);
1997 Dest = dst + y * outpitch;
1998 for (x = 0; x < width; x++ ) {
1999 long color = (*Source++);
2000 /* B */ Dest[0] = ((color >> 16) & 0xff); /* L */
2001 /* G */ Dest[1] = ((color >> 8 ) & 0xff) + 128; /* V */
2002 /* R */ Dest[2] = (color & 0xff) + 128; /* U */
2003 Dest += 4;
2004 }
2005 }
2006 }
2007 break;
2008 }
2009
2010 case CONVERT_A4L4:
2011 {
2012 unsigned int x, y;
2013 unsigned char *Source;
2014 unsigned char *Dest;
2015 for(y = 0; y < height; y++) {
2016 Source = src + y * pitch;
2017 Dest = dst + y * outpitch;
2018 for (x = 0; x < width; x++ ) {
2019 unsigned char color = (*Source++);
2020 /* A */ Dest[1] = (color & 0xf0) << 0;
2021 /* L */ Dest[0] = (color & 0x0f) << 4;
2022 Dest += 2;
2023 }
2024 }
2025 break;
2026 }
2027
2028 case CONVERT_R32F:
2029 {
2030 unsigned int x, y;
2031 float *Source;
2032 float *Dest;
2033 for(y = 0; y < height; y++) {
2034 Source = (float *) (src + y * pitch);
2035 Dest = (float *) (dst + y * outpitch);
2036 for (x = 0; x < width; x++ ) {
2037 float color = (*Source++);
2038 Dest[0] = color;
2039 Dest[1] = 1.0;
2040 Dest[2] = 1.0;
2041 Dest += 3;
2042 }
2043 }
2044 break;
2045 }
2046
2047 case CONVERT_R16F:
2048 {
2049 unsigned int x, y;
2050 WORD *Source;
2051 WORD *Dest;
2052 WORD one = 0x3c00;
2053 for(y = 0; y < height; y++) {
2054 Source = (WORD *) (src + y * pitch);
2055 Dest = (WORD *) (dst + y * outpitch);
2056 for (x = 0; x < width; x++ ) {
2057 WORD color = (*Source++);
2058 Dest[0] = color;
2059 Dest[1] = one;
2060 Dest[2] = one;
2061 Dest += 3;
2062 }
2063 }
2064 break;
2065 }
2066
2067 case CONVERT_G16R16:
2068 {
2069 unsigned int x, y;
2070 WORD *Source;
2071 WORD *Dest;
2072
2073 for(y = 0; y < height; y++) {
2074 Source = (WORD *) (src + y * pitch);
2075 Dest = (WORD *) (dst + y * outpitch);
2076 for (x = 0; x < width; x++ ) {
2077 WORD green = (*Source++);
2078 WORD red = (*Source++);
2079 Dest[0] = green;
2080 Dest[1] = red;
2081 Dest[2] = 0xffff;
2082 Dest += 3;
2083 }
2084 }
2085 break;
2086 }
2087
2088 default:
2089 ERR("Unsupported conversation type %d\n", convert);
2090 }
2091 return WINED3D_OK;
2092 }
2093
2094 static void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey) {
2095 IWineD3DPaletteImpl* pal = This->palette;
2096 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2097 BOOL index_in_alpha = FALSE;
2098 int dxVersion = ( (IWineD3DImpl *) device->wineD3D)->dxVersion;
2099 int i;
2100
2101 /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2102 * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2103 * is slow. Further RGB->P8 conversion is not possible because palettes can have
2104 * duplicate entries. Store the color key in the unused alpha component to speed the
2105 * download up and to make conversion unneeded. */
2106 index_in_alpha = primary_render_target_is_p8(device);
2107
2108 if (pal == NULL) {
2109 /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2110 if(dxVersion <= 7) {
2111 ERR("This code should never get entered for DirectDraw!, expect problems\n");
2112 if(index_in_alpha) {
2113 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2114 there's no palette at this time. */
2115 for (i = 0; i < 256; i++) table[i][3] = i;
2116 }
2117 } else {
2118 /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2119 alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2120 capability flag is present (wine does advertise this capability) */
2121 for (i = 0; i < 256; i++) {
2122 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2123 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2124 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2125 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2126 }
2127 }
2128 } else {
2129 TRACE("Using surface palette %p\n", pal);
2130 /* Get the surface's palette */
2131 for (i = 0; i < 256; i++) {
2132 table[i][0] = pal->palents[i].peRed;
2133 table[i][1] = pal->palents[i].peGreen;
2134 table[i][2] = pal->palents[i].peBlue;
2135
2136 /* When index_in_alpha is the palette index is stored in the alpha component. In case of a readback
2137 we can then read GL_ALPHA. Color keying is handled in BltOverride using a GL_ALPHA_TEST using GL_NOT_EQUAL.
2138 In case of index_in_alpha the color key itself is passed to glAlphaFunc in other cases the alpha component
2139 of pixels that should be masked away is set to 0. */
2140 if(index_in_alpha) {
2141 table[i][3] = i;
2142 } else if(colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue) && (i <= This->SrcBltCKey.dwColorSpaceHighValue)) {
2143 table[i][3] = 0x00;
2144 } else if(pal->Flags & WINEDDPCAPS_ALPHA) {
2145 table[i][3] = pal->palents[i].peFlags;
2146 } else {
2147 table[i][3] = 0xFF;
2148 }
2149 }
2150 }
2151 }
2152
2153 const char *fragment_palette_conversion =
2154 "!!ARBfp1.0\n"
2155 "TEMP index;\n"
2156 "PARAM constants = { 0.996, 0.00195, 0, 0 };\n" /* { 255/256, 0.5/255*255/256, 0, 0 } */
2157 "TEX index, fragment.texcoord[0], texture[0], 2D;\n" /* The alpha-component contains the palette index */
2158 "MAD index.a, index.a, constants.x, constants.y;\n" /* Scale the index by 255/256 and add a bias of '0.5' in order to sample in the middle */
2159 "TEX result.color, index.a, texture[1], 1D;\n" /* use the alpha-component as a index in the palette to get the final color */
2160 "END";
2161
2162 /* This function is used in case of 8bit paletted textures to upload the palette.
2163 It supports GL_EXT_paletted_texture and GL_ARB_fragment_program, support for other
2164 extensions like ATI_fragment_shaders is possible.
2165 */
2166 static void d3dfmt_p8_upload_palette(IWineD3DSurface *iface, CONVERT_TYPES convert) {
2167 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2168 BYTE table[256][4];
2169 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2170
2171 d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2172
2173 /* Try to use the paletted texture extension */
2174 if(GL_SUPPORT(EXT_PALETTED_TEXTURE))
2175 {
2176 TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
2177 GL_EXTCALL(glColorTableEXT(This->glDescription.target,GL_RGBA,256,GL_RGBA,GL_UNSIGNED_BYTE, table));
2178 }
2179 else
2180 {
2181 /* Let a fragment shader do the color conversion by uploading the palette to a 1D texture.
2182 * The 8bit pixel data will be used as an index in this palette texture to retrieve the final color. */
2183 TRACE("Using fragment shaders for emulating 8-bit paletted texture support\n");
2184
2185 /* Create the fragment program if we don't have it */
2186 if(!device->paletteConversionShader)
2187 {
2188 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2189 GL_EXTCALL(glGenProgramsARB(1, &device->paletteConversionShader));
2190 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2191 GL_EXTCALL(glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(fragment_palette_conversion), (const GLbyte *)fragment_palette_conversion));
2192 glDisable(GL_FRAGMENT_PROGRAM_ARB);
2193 }
2194
2195 glEnable(GL_FRAGMENT_PROGRAM_ARB);
2196 GL_EXTCALL(glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, device->paletteConversionShader));
2197
2198 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE1));
2199 glEnable(GL_TEXTURE_1D);
2200 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
2201
2202 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2203 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Make sure we have discrete color levels. */
2204 glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
2205 glTexImage1D(GL_TEXTURE_1D, 0, GL_RGBA, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, table); /* Upload the palette */
2206
2207 /* Switch back to unit 0 in which the 2D texture will be stored. */
2208 GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0));
2209
2210 /* Rebind the texture because it isn't bound anymore */
2211 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2212 }
2213 }
2214
2215 BOOL palette9_changed(IWineD3DSurfaceImpl *This) {
2216 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2217
2218 if(This->palette || (This->resource.format != WINED3DFMT_P8 && This->resource.format != WINED3DFMT_A8P8)) {
2219 /* If a ddraw-style palette is attached assume no d3d9 palette change.
2220 * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2221 */
2222 return FALSE;
2223 }
2224
2225 if(This->palette9) {
2226 if(memcmp(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256) == 0) {
2227 return FALSE;
2228 }
2229 } else {
2230 This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2231 }
2232 memcpy(This->palette9, &device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2233 return TRUE;
2234 }
2235
2236 static inline void clear_unused_channels(IWineD3DSurfaceImpl *This) {
2237 GLboolean oldwrite[4];
2238
2239 /* Some formats have only some color channels, and the others are 1.0.
2240 * since our rendering renders to all channels, and those pixel formats
2241 * are emulated by using a full texture with the other channels set to 1.0
2242 * manually, clear the unused channels.
2243 *
2244 * This could be done with hacking colorwriteenable to mask the colors,
2245 * but before drawing the buffer would have to be cleared too, so there's
2246 * no gain in that
2247 */
2248 switch(This->resource.format) {
2249 case WINED3DFMT_R16F:
2250 case WINED3DFMT_R32F:
2251 TRACE("R16F or R32F format, clearing green, blue and alpha to 1.0\n");
2252 /* Do not activate a context, the correct drawable is active already
2253 * though just the read buffer is set, make sure to have the correct draw
2254 * buffer too
2255 */
2256 glDrawBuffer(This->resource.wineD3DDevice->offscreenBuffer);
2257 glDisable(GL_SCISSOR_TEST);
2258 glGetBooleanv(GL_COLOR_WRITEMASK, oldwrite);
2259 glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2260 glClearColor(0.0, 1.0, 1.0, 1.0);
2261 glClear(GL_COLOR_BUFFER_BIT);
2262 glColorMask(oldwrite[0], oldwrite[1], oldwrite[2], oldwrite[3]);
2263 if(!This->resource.wineD3DDevice->render_offscreen) glDrawBuffer(GL_BACK);
2264 checkGLcall("Unused channel clear\n");
2265 break;
2266
2267 default: break;
2268 }
2269 }
2270
2271 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, BOOL srgb_mode) {
2272 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2273
2274 if (!(This->Flags & SFLAG_INTEXTURE)) {
2275 TRACE("Reloading because surface is dirty\n");
2276 } else if(/* Reload: gl texture has ck, now no ckey is set OR */
2277 ((This->Flags & SFLAG_GLCKEY) && (!(This->CKeyFlags & WINEDDSD_CKSRCBLT))) ||
2278 /* Reload: vice versa OR */
2279 ((!(This->Flags & SFLAG_GLCKEY)) && (This->CKeyFlags & WINEDDSD_CKSRCBLT)) ||
2280 /* Also reload: Color key is active AND the color key has changed */
2281 ((This->CKeyFlags & WINEDDSD_CKSRCBLT) && (
2282 (This->glCKey.dwColorSpaceLowValue != This->SrcBltCKey.dwColorSpaceLowValue) ||
2283 (This->glCKey.dwColorSpaceHighValue != This->SrcBltCKey.dwColorSpaceHighValue)))) {
2284 TRACE("Reloading because of color keying\n");
2285 /* To perform the color key conversion we need a sysmem copy of
2286 * the surface. Make sure we have it
2287 */
2288
2289 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
2290 /* Make sure the texture is reloaded because of the color key change, this kills performance though :( */
2291 /* TODO: This is not necessarily needed with hw palettized texture support */
2292 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2293 } else {
2294 TRACE("surface is already in texture\n");
2295 return WINED3D_OK;
2296 }
2297
2298 /* Resources are placed in system RAM and do not need to be recreated when a device is lost.
2299 * These resources are not bound by device size or format restrictions. Because of this,
2300 * these resources cannot be accessed by the Direct3D device nor set as textures or render targets.
2301 * However, these resources can always be created, locked, and copied.
2302 */
2303 if (This->resource.pool == WINED3DPOOL_SCRATCH )
2304 {
2305 FIXME("(%p) Operation not supported for scratch textures\n",This);
2306 return WINED3DERR_INVALIDCALL;
2307 }
2308
2309 This->srgb = srgb_mode;
2310 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL /* no partial locking for textures yet */);
2311
2312 #if 0
2313 {
2314 static unsigned int gen = 0;
2315 char buffer[4096];
2316 ++gen;
2317 if ((gen % 10) == 0) {
2318 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, This->glDescription.target, This->glDescription.level, gen);
2319 IWineD3DSurfaceImpl_SaveSnapshot(iface, buffer);
2320 }
2321 /*
2322 * debugging crash code
2323 if (gen == 250) {
2324 void** test = NULL;
2325 *test = 0;
2326 }
2327 */
2328 }
2329 #endif
2330
2331 if (!(This->Flags & SFLAG_DONOTFREE)) {
2332 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
2333 This->resource.allocatedMemory = NULL;
2334 This->resource.heapMemory = NULL;
2335 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, FALSE);
2336 }
2337
2338 return WINED3D_OK;
2339 }
2340
2341 static void WINAPI IWineD3DSurfaceImpl_BindTexture(IWineD3DSurface *iface) {
2342 /* TODO: check for locks */
2343 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2344 IWineD3DBaseTexture *baseTexture = NULL;
2345 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
2346
2347 TRACE("(%p)Checking to see if the container is a base texture\n", This);
2348 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2349 TRACE("Passing to container\n");
2350 IWineD3DBaseTexture_BindTexture(baseTexture);
2351 IWineD3DBaseTexture_Release(baseTexture);
2352 } else {
2353 TRACE("(%p) : Binding surface\n", This);
2354
2355 if(!device->isInDraw) {
2356 ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
2357 }
2358
2359 ENTER_GL();
2360
2361 glEnable(This->glDescription.target);
2362
2363 if (!This->glDescription.level) {
2364 if (!This->glDescription.textureName) {
2365 glGenTextures(1, &This->glDescription.textureName);
2366 checkGLcall("glGenTextures");
2367 TRACE("Surface %p given name %d\n", This, This->glDescription.textureName);
2368 }
2369 /* This is where we should be reducing the amount of GLMemoryUsed */
2370 } else if (This->glDescription.textureName) {
2371 /* Mipmap surfaces should have a base texture container */
2372 ERR("Mipmap surface has a glTexture bound to it!\n");
2373 }
2374
2375 glBindTexture(This->glDescription.target, This->glDescription.textureName);
2376 checkGLcall("glBindTexture");
2377
2378 LEAVE_GL();
2379 }
2380 return;
2381 }
2382
2383 #include <errno.h>
2384 #include <stdio.h>
2385 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
2386 FILE* f = NULL;
2387 UINT i, y;
2388 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2389 char *allocatedMemory;
2390 char *textureRow;
2391 IWineD3DSwapChain *swapChain = NULL;
2392 int width, height;
2393 GLuint tmpTexture = 0;
2394 DWORD color;
2395 /*FIXME:
2396 Textures may not be stored in ->allocatedgMemory and a GlTexture
2397 so we should lock the surface before saving a snapshot, or at least check that
2398 */
2399 /* TODO: Compressed texture images can be obtained from the GL in uncompressed form
2400 by calling GetTexImage and in compressed form by calling
2401 GetCompressedTexImageARB. Queried compressed images can be saved and
2402 later reused by calling CompressedTexImage[123]DARB. Pre-compressed
2403 texture images do not need to be processed by the GL and should
2404 significantly improve texture loading performance relative to uncompressed
2405 images. */
2406
2407 /* Setup the width and height to be the internal texture width and height. */
2408 width = This->pow2Width;
2409 height = This->pow2Height;
2410 /* check to see if we're a 'virtual' texture, e.g. we're not a pbuffer of texture, we're a back buffer*/
2411 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapChain);
2412
2413 if (This->Flags & SFLAG_INDRAWABLE && !(This->Flags & SFLAG_INTEXTURE)) {
2414 /* if were not a real texture then read the back buffer into a real texture */
2415 /* we don't want to interfere with the back buffer so read the data into a temporary
2416 * texture and then save the data out of the temporary texture
2417 */
2418 GLint prevRead;
2419 ENTER_GL();
2420 TRACE("(%p) Reading render target into texture\n", This);
2421 glEnable(GL_TEXTURE_2D);
2422
2423 glGenTextures(1, &tmpTexture);
2424 glBindTexture(GL_TEXTURE_2D, tmpTexture);
2425
2426 glTexImage2D(GL_TEXTURE_2D,
2427 0,
2428 GL_RGBA,
2429 width,
2430 height,
2431 0/*border*/,
2432 GL_RGBA,
2433 GL_UNSIGNED_INT_8_8_8_8_REV,
2434 NULL);
2435
2436 glGetIntegerv(GL_READ_BUFFER, &prevRead);
2437 vcheckGLcall("glGetIntegerv");
2438 glReadBuffer(swapChain ? GL_BACK : This->resource.wineD3DDevice->offscreenBuffer);
2439 vcheckGLcall("glReadBuffer");
2440 glCopyTexImage2D(GL_TEXTURE_2D,
2441 0,
2442 GL_RGBA,
2443 0,
2444 0,
2445 width,
2446 height,
2447 0);
2448
2449 checkGLcall("glCopyTexImage2D");
2450 glReadBuffer(prevRead);
2451 LEAVE_GL();
2452
2453 } else { /* bind the real texture, and make sure it up to date */
2454 IWineD3DSurface_PreLoad(iface);
2455 surface_bind_and_dirtify(This);
2456 }
2457 allocatedMemory = HeapAlloc(GetProcessHeap(), 0, width * height * 4);
2458 ENTER_GL();
2459 FIXME("Saving texture level %d width %d height %d\n", This->glDescription.level, width, height);
2460 glGetTexImage(GL_TEXTURE_2D,
2461 This->glDescription.level,
2462 GL_RGBA,
2463 GL_UNSIGNED_INT_8_8_8_8_REV,
2464 allocatedMemory);
2465 checkGLcall("glTexImage2D");
2466 if (tmpTexture) {
2467 glBindTexture(GL_TEXTURE_2D, 0);
2468 glDeleteTextures(1, &tmpTexture);
2469 }
2470 LEAVE_GL();
2471
2472 f = fopen(filename, "w+");
2473 if (NULL == f) {
2474 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
2475 return WINED3DERR_INVALIDCALL;
2476 }
2477 /* Save the data out to a TGA file because 1: it's an easy raw format, 2: it supports an alpha channel */
2478 TRACE("(%p) opened %s with format %s\n", This, filename, debug_d3dformat(This->resource.format));
2479 /* TGA header */
2480 fputc(0,f);
2481 fputc(0,f);
2482 fputc(2,f);
2483 fputc(0,f);
2484 fputc(0,f);
2485 fputc(0,f);
2486 fputc(0,f);
2487 fputc(0,f);
2488 fputc(0,f);
2489 fputc(0,f);
2490 fputc(0,f);
2491 fputc(0,f);
2492 /* short width*/
2493 fwrite(&width,2,1,f);
2494 /* short height */
2495 fwrite(&height,2,1,f);
2496 /* format rgba */
2497 fputc(0x20,f);
2498 fputc(0x28,f);
2499 /* raw data */
2500 /* 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 */
2501 if(swapChain)
2502 textureRow = allocatedMemory + (width * (height - 1) *4);
2503 else
2504 textureRow = allocatedMemory;
2505 for (y = 0 ; y < height; y++) {
2506 for (i = 0; i < width; i++) {
2507 color = *((DWORD*)textureRow);
2508 fputc((color >> 16) & 0xFF, f); /* B */
2509 fputc((color >> 8) & 0xFF, f); /* G */
2510 fputc((color >> 0) & 0xFF, f); /* R */
2511 fputc((color >> 24) & 0xFF, f); /* A */
2512 textureRow += 4;
2513 }
2514 /* take two rows of the pointer to the texture memory */
2515 if(swapChain)
2516 (textureRow-= width << 3);
2517
2518 }
2519 TRACE("Closing file\n");
2520 fclose(f);
2521
2522 if(swapChain) {
2523 IWineD3DSwapChain_Release(swapChain);
2524 }
2525 HeapFree(GetProcessHeap(), 0, allocatedMemory);
2526 return WINED3D_OK;
2527 }
2528
2529 /**
2530 * Slightly inefficient way to handle multiple dirty rects but it works :)
2531 */
2532 HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
2533 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2534 IWineD3DBaseTexture *baseTexture = NULL;
2535
2536 if (!(This->Flags & SFLAG_INSYSMEM) && (This->Flags & SFLAG_INTEXTURE))
2537 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL /* no partial locking for textures yet */);
2538
2539 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2540 if (NULL != pDirtyRect) {
2541 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
2542 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
2543 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
2544 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
2545 } else {
2546 This->dirtyRect.left = 0;
2547 This->dirtyRect.top = 0;
2548 This->dirtyRect.right = This->currentDesc.Width;
2549 This->dirtyRect.bottom = This->currentDesc.Height;
2550 }
2551 TRACE("(%p) : Dirty: yes, Rect:(%d,%d,%d,%d)\n", This, This->dirtyRect.left,
2552 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
2553 /* if the container is a basetexture then mark it dirty. */
2554 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&baseTexture) == WINED3D_OK) {
2555 TRACE("Passing to container\n");
2556 IWineD3DBaseTexture_SetDirty(baseTexture, TRUE);
2557 IWineD3DBaseTexture_Release(baseTexture);
2558 }
2559 return WINED3D_OK;
2560 }
2561
2562 HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, WINED3DFORMAT format) {
2563 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2564 HRESULT hr;
2565 const GlPixelFormatDesc *glDesc;
2566 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
2567
2568 TRACE("(%p) : Calling base function first\n", This);
2569 hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2570 if(SUCCEEDED(hr)) {
2571 /* Setup some glformat defaults */
2572 This->glDescription.glFormat = glDesc->glFormat;
2573 This->glDescription.glFormatInternal = glDesc->glInternal;
2574 This->glDescription.glType = glDesc->glType;
2575
2576 This->Flags &= ~SFLAG_ALLOCATED;
2577 TRACE("(%p) : glFormat %d, glFotmatInternal %d, glType %d\n", This,
2578 This->glDescription.glFormat, This->glDescription.glFormatInternal, This->glDescription.glType);
2579 }
2580 return hr;
2581 }
2582
2583 HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2584 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2585
2586 if(This->Flags & (SFLAG_LOCKED | SFLAG_DCINUSE)) {
2587 WARN("Surface is locked or the HDC is in use\n");
2588 return WINED3DERR_INVALIDCALL;
2589 }
2590
2591 if(Mem && Mem != This->resource.allocatedMemory) {
2592 void *release = NULL;
2593
2594 /* Do I have to copy the old surface content? */
2595 if(This->Flags & SFLAG_DIBSECTION) {
2596 /* Release the DC. No need to hold the critical section for the update
2597 * Thread because this thread runs only on front buffers, but this method
2598 * fails for render targets in the check above.
2599 */
2600 SelectObject(This->hDC, This->dib.holdbitmap);
2601 DeleteDC(This->hDC);
2602 /* Release the DIB section */
2603 DeleteObject(This->dib.DIBsection);
2604 This->dib.bitmap_data = NULL;
2605 This->resource.allocatedMemory = NULL;
2606 This->hDC = NULL;
2607 This->Flags &= ~SFLAG_DIBSECTION;
2608 } else if(!(This->Flags & SFLAG_USERPTR)) {
2609 release = This->resource.heapMemory;
2610 This->resource.heapMemory = NULL;
2611 }
2612 This->resource.allocatedMemory = Mem;
2613 This->Flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
2614
2615 /* Now the surface memory is most up do date. Invalidate drawable and texture */
2616 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
2617
2618 /* For client textures opengl has to be notified */
2619 if(This->Flags & SFLAG_CLIENT) {
2620 This->Flags &= ~SFLAG_ALLOCATED;
2621 IWineD3DSurface_PreLoad(iface);
2622 /* And hope that the app behaves correctly and did not free the old surface memory before setting a new pointer */
2623 }
2624
2625 /* Now free the old memory if any */
2626 HeapFree(GetProcessHeap(), 0, release);
2627 } else if(This->Flags & SFLAG_USERPTR) {
2628 /* LockRect and GetDC will re-create the dib section and allocated memory */
2629 This->resource.allocatedMemory = NULL;
2630 /* HeapMemory should be NULL already */
2631 if(This->resource.heapMemory != NULL) ERR("User pointer surface has heap memory allocated\n");
2632 This->Flags &= ~SFLAG_USERPTR;
2633
2634 if(This->Flags & SFLAG_CLIENT) {
2635 This->Flags &= ~SFLAG_ALLOCATED;
2636 /* This respecifies an empty texture and opengl knows that the old memory is gone */
2637 IWineD3DSurface_PreLoad(iface);
2638 }
2639 }
2640 return WINED3D_OK;
2641 }
2642
2643 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2644
2645 /* Flip the surface contents */
2646 /* Flip the DC */
2647 {
2648 HDC tmp;
2649 tmp = front->hDC;
2650 front->hDC = back->hDC;
2651 back->hDC = tmp;
2652 }
2653
2654 /* Flip the DIBsection */
2655 {
2656 HBITMAP tmp;
2657 BOOL hasDib = front->Flags & SFLAG_DIBSECTION;
2658 tmp = front->dib.DIBsection;
2659 front->dib.DIBsection = back->dib.DIBsection;
2660 back->dib.DIBsection = tmp;
2661
2662 if(back->Flags & SFLAG_DIBSECTION) front->Flags |= SFLAG_DIBSECTION;
2663 else front->Flags &= ~SFLAG_DIBSECTION;
2664 if(hasDib) back->Flags |= SFLAG_DIBSECTION;
2665 else back->Flags &= ~SFLAG_DIBSECTION;
2666 }
2667
2668 /* Flip the surface data */
2669 {
2670 void* tmp;
2671
2672 tmp = front->dib.bitmap_data;
2673 front->dib.bitmap_data = back->dib.bitmap_data;
2674 back->dib.bitmap_data = tmp;
2675
2676 tmp = front->resource.allocatedMemory;
2677 front->resource.allocatedMemory = back->resource.allocatedMemory;
2678 back->resource.allocatedMemory = tmp;
2679
2680 tmp = front->resource.heapMemory;
2681 front->resource.heapMemory = back->resource.heapMemory;
2682 back->resource.heapMemory = tmp;
2683 }
2684
2685 /* Flip the PBO */
2686 {
2687 GLuint tmp_pbo = front->pbo;
2688 front->pbo = back->pbo;
2689 back->pbo = tmp_pbo;
2690 }
2691
2692 /* client_memory should not be different, but just in case */
2693 {
2694 BOOL tmp;
2695 tmp = front->dib.client_memory;
2696 front->dib.client_memory = back->dib.client_memory;
2697 back->dib.client_memory = tmp;
2698 }
2699
2700 /* Flip the opengl texture */
2701 {
2702 glDescriptor tmp_desc = back->glDescription;
2703 back->glDescription = front->glDescription;
2704 front->glDescription = tmp_desc;
2705 }
2706
2707 {
2708 DWORD tmp_flags = back->Flags;
2709 back->Flags = front->Flags;
2710 front->Flags = tmp_flags;
2711 }
2712 }
2713
2714 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD Flags) {
2715 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2716 IWineD3DSwapChainImpl *swapchain = NULL;
2717 HRESULT hr;
2718 TRACE("(%p)->(%p,%x)\n", This, override, Flags);
2719
2720 /* Flipping is only supported on RenderTargets and overlays*/
2721 if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2722 WARN("Tried to flip a non-render target, non-overlay surface\n");
2723 return WINEDDERR_NOTFLIPPABLE;
2724 }
2725
2726 if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2727 flip_surface(This, (IWineD3DSurfaceImpl *) override);
2728
2729 /* Update the overlay if it is visible */
2730 if(This->overlay_dest) {
2731 return IWineD3DSurface_DrawOverlay((IWineD3DSurface *) This);
2732 } else {
2733 return WINED3D_OK;
2734 }
2735 }
2736
2737 if(override) {
2738 /* DDraw sets this for the X11 surfaces, so don't confuse the user
2739 * FIXME("(%p) Target override is not supported by now\n", This);
2740 * Additionally, it isn't really possible to support triple-buffering
2741 * properly on opengl at all
2742 */
2743 }
2744
2745 IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **) &swapchain);
2746 if(!swapchain) {
2747 ERR("Flipped surface is not on a swapchain\n");
2748 return WINEDDERR_NOTFLIPPABLE;
2749 }
2750
2751 /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2752 * and only d3d8 and d3d9 apps specify the presentation interval
2753 */
2754 if((Flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)) == 0) {
2755 /* Most common case first to avoid wasting time on all the other cases */
2756 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2757 } else if(Flags & WINEDDFLIP_NOVSYNC) {
2758 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2759 } else if(Flags & WINEDDFLIP_INTERVAL2) {
2760 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2761 } else if(Flags & WINEDDFLIP_INTERVAL3) {
2762 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2763 } else {
2764 swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2765 }
2766
2767 /* Flipping a OpenGL surface -> Use WineD3DDevice::Present */
2768 hr = IWineD3DSwapChain_Present((IWineD3DSwapChain *) swapchain, NULL, NULL, 0, NULL, 0);
2769 IWineD3DSwapChain_Release((IWineD3DSwapChain *) swapchain);
2770 return hr;
2771 }
2772
2773 /* Does a direct frame buffer -> texture copy. Stretching is done
2774 * with single pixel copy calls
2775 */
2776 static inline void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2777 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2778 float xrel, yrel;
2779 UINT row;
2780 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2781
2782
2783 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2784 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2785 ENTER_GL();
2786
2787 /* TODO: Do we need GL_TEXTURE_2D enabled fpr copyteximage? */
2788 glEnable(This->glDescription.target);
2789 checkGLcall("glEnable(This->glDescription.target)");
2790
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);
2801 }
2802 checkGLcall("glReadBuffer");
2803
2804 xrel = (float) (srect->x2 - srect->x1) / (float) (drect->x2 - drect->x1);
2805 yrel = (float) (srect->y2 - srect->y1) / (float) (drect->y2 - drect->y1);
2806
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");
2809
2810 if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2811 ERR("Texture filtering not supported in direct blit\n");
2812 }
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");
2815 }
2816
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 */
2821
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
2832 *
2833 * However, stretching in x direction can be avoided if not necessary
2834 */
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
2839 */
2840 UINT col;
2841
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);
2848 }
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);
2855 }
2856 }
2857 }
2858 vcheckGLcall("glCopyTexSubImage2D");
2859
2860 /* Leave the opengl state valid for blitting */
2861 glDisable(This->glDescription.target);
2862 checkGLcall("glDisable(This->glDescription.target)");
2863
2864 LEAVE_GL();
2865 }
2866
2867 /* Uses the hardware to stretch and flip the image */
2868 static inline void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *This, IWineD3DSurface *SrcSurface, IWineD3DSwapChainImpl *swapchain, WINED3DRECT *srect, WINED3DRECT *drect, BOOL upsidedown, WINED3DTEXTUREFILTERTYPE Filter) {
2869 GLuint src, backup = 0;
2870 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
2871 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
2872 float left, right, top, bottom; /* Texture coordinates */
2873 UINT fbwidth = Src->currentDesc.Width;
2874 UINT fbheight = Src->currentDesc.Height;
2875 GLenum drawBuffer = GL_BACK;
2876 GLenum texture_target;
2877 BOOL noBackBufferBackup;
2878
2879 TRACE("Using hwstretch blit\n");
2880 /* Activate the Proper context for reading from the source surface, set it up for blitting */
2881 ActivateContext(myDevice, SrcSurface, CTXUSAGE_BLIT);
2882 IWineD3DSurface_PreLoad((IWineD3DSurface *) This);
2883
2884 noBackBufferBackup = !swapchain && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
2885 if(!noBackBufferBackup && Src->glDescription.textureName == 0) {
2886 /* Get it a description */
2887 IWineD3DSurface_PreLoad(SrcSurface);
2888 }
2889 ENTER_GL();
2890
2891 /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
2892 * This way we don't have to wait for the 2nd readback to finish to leave this function.
2893 */
2894 if(myDevice->activeContext->aux_buffers >= 2) {
2895 /* Got more than one aux buffer? Use the 2nd aux buffer */
2896 drawBuffer = GL_AUX1;
2897 } else if((swapchain || myDevice->offscreenBuffer == GL_BACK) && myDevice->activeContext->aux_buffers >= 1) {
2898 /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
2899 drawBuffer = GL_AUX0;
2900 }
2901
2902 if(noBackBufferBackup) {
2903 glGenTextures(1, &backup);
2904 checkGLcall("glGenTextures\n");
2905 glBindTexture(GL_TEXTURE_2D, backup);
2906 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
2907 texture_target = GL_TEXTURE_2D;
2908 } else {
2909 /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
2910 * we are reading from the back buffer, the backup can be used as source texture
2911 */
2912 texture_target = Src->glDescription.target;
2913 glBindTexture(texture_target, Src->glDescription.textureName);
2914 checkGLcall("glBindTexture(texture_target, Src->glDescription.textureName)");
2915 glEnable(texture_target);
2916 checkGLcall("glEnable(texture_target)");
2917
2918 /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
2919 Src->Flags &= ~SFLAG_INTEXTURE;
2920 }
2921
2922 if(swapchain) {
2923 glReadBuffer(surface_get_gl_buffer(SrcSurface, (IWineD3DSwapChain *)swapchain));
2924 } else {
2925 TRACE("Reading from an offscreen target\n");
2926 upsidedown = !upsidedown;
2927 glReadBuffer(myDevice->offscreenBuffer);
2928 }
2929
2930 /* TODO: Only back up the part that will be overwritten */
2931 glCopyTexSubImage2D(texture_target, 0,
2932 0, 0 /* read offsets */,
2933 0, 0,
2934 fbwidth,
2935 fbheight);
2936
2937 checkGLcall("glCopyTexSubImage2D");
2938
2939 /* No issue with overriding these - the sampler is dirty due to blit usage */
2940 glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
2941 magLookup[Filter - WINED3DTEXF_NONE]);
2942 checkGLcall("glTexParameteri");
2943 glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
2944 minMipLookup[Filter][WINED3DTEXF_NONE]);
2945 checkGLcall("glTexParameteri");
2946
2947 if(!swapchain || (IWineD3DSurface *) Src == swapchain->backBuffer[0]) {
2948 src = backup ? backup : Src->glDescription.textureName;
2949 } else {
2950 glReadBuffer(GL_FRONT);
2951 checkGLcall("glReadBuffer(GL_FRONT)");
2952
2953 glGenTextures(1, &src);
2954 checkGLcall("glGenTextures(1, &src)");
2955 glBindTexture(GL_TEXTURE_2D, src);
2956 checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
2957
2958 /* TODO: Only copy the part that will be read. Use srect->x1, srect->y2 as origin, but with the width watch
2959 * out for power of 2 sizes
2960 */
2961 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Src->pow2Width, Src->pow2Height, 0,
2962 GL_RGBA, GL_UNSIGNED_BYTE, NULL);
2963 checkGLcall("glTexImage2D");
2964 glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
2965 0, 0 /* read offsets */,
2966 0, 0,
2967 fbwidth,
2968 fbheight);
2969
2970 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2971 checkGLcall("glTexParameteri");
2972 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2973 checkGLcall("glTexParameteri");
2974
2975 glReadBuffer(GL_BACK);
2976 checkGLcall("glReadBuffer(GL_BACK)");
2977
2978 if(texture_target != GL_TEXTURE_2D) {
2979 glDisable(texture_target);
2980 glEnable(GL_TEXTURE_2D);
2981 texture_target = GL_TEXTURE_2D;
2982 }
2983 }
2984 checkGLcall("glEnd and previous");
2985
2986 left = srect->x1;
2987 right = srect->x2;
2988
2989 if(upsidedown) {
2990 top = Src->currentDesc.Height - srect->y1;
2991 bottom = Src->currentDesc.Height - srect->y2;
2992 } else {
2993 top = Src->currentDesc.Height - srect->y2;
2994 bottom = Src->currentDesc.Height - srect->y1;
2995 }
2996
2997 if(Src->Flags & SFLAG_NORMCOORD) {
2998 left /= Src->pow2Width;
2999 right /= Src->pow2Width;
3000 top /= Src->pow2Height;
3001 bottom /= Src->pow2Height;
3002 }
3003
3004 /* draw the source texture stretched and upside down. The correct surface is bound already */
3005 glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3006 glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3007
3008 glDrawBuffer(drawBuffer);
3009 glReadBuffer(drawBuffer);
3010
3011 glBegin(GL_QUADS);
3012 /* bottom left */
3013 glTexCoord2f(left, bottom);
3014 glVertex2i(0, fbheight);
3015
3016 /* top left */
3017 glTexCoord2f(left, top);
3018 glVertex2i(0, fbheight - drect->y2 - drect->y1);
3019
3020 /* top right */
3021 glTexCoord2f(right, top);
3022 glVertex2i(drect->x2 - drect->x1, fbheight - drect->y2 - drect->y1);
3023
3024 /* bottom right */
3025 glTexCoord2f(right, bottom);
3026 glVertex2i(drect->x2 - drect->x1, fbheight);
3027 glEnd();
3028 checkGLcall("glEnd and previous");
3029
3030 if(texture_target != This->glDescription.target) {
3031 glDisable(texture_target);
3032 glEnable(This->glDescription.target);
3033 texture_target = This->glDescription.target;
3034 }
3035
3036 /* Now read the stretched and upside down image into the destination texture */
3037 glBindTexture(texture_target, This->glDescription.textureName);
3038 checkGLcall("glBindTexture");
3039 glCopyTexSubImage2D(texture_target,
3040 0,
3041 drect->x1, drect->y1, /* xoffset, yoffset */
3042 0, 0, /* We blitted the image to the origin */
3043 drect->x2 - drect->x1, drect->y2 - drect->y1);
3044 checkGLcall("glCopyTexSubImage2D");
3045
3046 if(drawBuffer == GL_BACK) {
3047 /* Write the back buffer backup back */
3048 if(backup) {
3049 if(texture_target != GL_TEXTURE_2D) {
3050 glDisable(texture_target);
3051 glEnable(GL_TEXTURE_2D);
3052 texture_target = GL_TEXTURE_2D;
3053 }
3054 glBindTexture(GL_TEXTURE_2D, backup);
3055 checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3056 } else {
3057 if(texture_target != Src->glDescription.target) {
3058 glDisable(texture_target);
3059 glEnable(Src->glDescription.target);
3060 texture_target = Src->glDescription.target;
3061 }
3062 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3063 checkGLcall("glBindTexture(Src->glDescription.target, Src->glDescription.textureName)");
3064 }
3065
3066 glBegin(GL_QUADS);
3067 /* top left */
3068 glTexCoord2f(0.0, (float) fbheight / (float) Src->pow2Height);
3069 glVertex2i(0, 0);
3070
3071 /* bottom left */
3072 glTexCoord2f(0.0, 0.0);
3073 glVertex2i(0, fbheight);
3074
3075 /* bottom right */
3076 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, 0.0);
3077 glVertex2i(fbwidth, Src->currentDesc.Height);
3078
3079 /* top right */
3080 glTexCoord2f((float) fbwidth / (float) Src->pow2Width, (float) fbheight / (float) Src->pow2Height);
3081 glVertex2i(fbwidth, 0);
3082 glEnd();
3083 } else {
3084 /* Restore the old draw buffer */
3085 glDrawBuffer(GL_BACK);
3086 }
3087 glDisable(texture_target);
3088 checkGLcall("glDisable(texture_target)");
3089
3090 /* Cleanup */
3091 if(src != Src->glDescription.textureName && src != backup) {
3092 glDeleteTextures(1, &src);
3093 checkGLcall("glDeleteTextures(1, &src)");
3094 }
3095 if(backup) {
3096 glDeleteTextures(1, &backup);
3097 checkGLcall("glDeleteTextures(1, &backup)");
3098 }
3099
3100 LEAVE_GL();
3101 }
3102
3103 /* Not called from the VTable */
3104 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3105 WINED3DRECT rect;
3106 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3107 IWineD3DSwapChainImpl *srcSwapchain = NULL, *dstSwapchain = NULL;
3108 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3109
3110 TRACE("(%p)->(%p,%p,%p,%08x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3111
3112 /* Get the swapchain. One of the surfaces has to be a primary surface */
3113 if(This->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3114 WARN("Destination is in sysmem, rejecting gl blt\n");
3115 return WINED3DERR_INVALIDCALL;
3116 }
3117 IWineD3DSurface_GetContainer( (IWineD3DSurface *) This, &IID_IWineD3DSwapChain, (void **)&dstSwapchain);
3118 if(dstSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) dstSwapchain);
3119 if(Src) {
3120 if(Src->resource.pool == WINED3DPOOL_SYSTEMMEM) {
3121 WARN("Src is in sysmem, rejecting gl blt\n");
3122 return WINED3DERR_INVALIDCALL;
3123 }
3124 IWineD3DSurface_GetContainer( (IWineD3DSurface *) Src, &IID_IWineD3DSwapChain, (void **)&srcSwapchain);
3125 if(srcSwapchain) IWineD3DSwapChain_Release((IWineD3DSwapChain *) srcSwapchain);
3126 }
3127
3128 /* Early sort out of cases where no render target is used */
3129 if(!dstSwapchain && !srcSwapchain &&
3130 SrcSurface != myDevice->render_targets[0] && This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3131 TRACE("No surface is render target, not using hardware blit. Src = %p, dst = %p\n", Src, This);
3132 return WINED3DERR_INVALIDCALL;
3133 }
3134
3135 /* No destination color keying supported */
3136 if(Flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE)) {
3137 /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3138 TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3139 return WINED3DERR_INVALIDCALL;
3140 }
3141
3142 if (DestRect) {
3143 rect.x1 = DestRect->left;
3144 rect.y1 = DestRect->top;
3145 rect.x2 = DestRect->right;
3146 rect.y2 = DestRect->bottom;
3147 } else {
3148 rect.x1 = 0;
3149 rect.y1 = 0;
3150 rect.x2 = This->currentDesc.Width;
3151 rect.y2 = This->currentDesc.Height;
3152 }
3153
3154 /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3155 if(dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->backBuffer &&
3156 ((IWineD3DSurface *) This == dstSwapchain->frontBuffer) && SrcSurface == dstSwapchain->backBuffer[0]) {
3157 /* Half-life does a Blt from the back buffer to the front buffer,
3158 * Full surface size, no flags... Use present instead
3159 *
3160 * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3161 */
3162
3163 /* Check rects - IWineD3DDevice_Present doesn't handle them */
3164 while(1)
3165 {
3166 RECT mySrcRect;
3167 TRACE("Looking if a Present can be done...\n");
3168 /* Source Rectangle must be full surface */
3169 if( SrcRect ) {
3170 if(SrcRect->left != 0 || SrcRect->top != 0 ||
3171 SrcRect->right != Src->currentDesc.Width || SrcRect->bottom != Src->currentDesc.Height) {
3172 TRACE("No, Source rectangle doesn't match\n");
3173 break;
3174 }
3175 }
3176 mySrcRect.left = 0;
3177 mySrcRect.top = 0;
3178 mySrcRect.right = Src->currentDesc.Width;
3179 mySrcRect.bottom = Src->currentDesc.Height;
3180
3181 /* No stretching may occur */
3182 if(mySrcRect.right != rect.x2 - rect.x1 ||
3183 mySrcRect.bottom != rect.y2 - rect.y1) {
3184 TRACE("No, stretching is done\n");
3185 break;
3186 }
3187
3188 /* Destination must be full surface or match the clipping rectangle */
3189 if(This->clipper && ((IWineD3DClipperImpl *) This->clipper)->hWnd)
3190 {
3191 RECT cliprect;
3192 POINT pos[2];
3193 GetClientRect(((IWineD3DClipperImpl *) This->clipper)->hWnd, &cliprect);
3194 pos[0].x = rect.x1;
3195 pos[0].y = rect.y1;
3196 pos[1].x = rect.x2;
3197 pos[1].y = rect.y2;
3198 MapWindowPoints(GetDesktopWindow(), ((IWineD3DClipperImpl *) This->clipper)->hWnd,
3199 pos, 2);
3200
3201 if(pos[0].x != cliprect.left || pos[0].y != cliprect.top ||
3202 pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3203 {
3204 TRACE("No, dest rectangle doesn't match(clipper)\n");
3205 TRACE("Clip rect at (%d,%d)-(%d,%d)\n", cliprect.left, cliprect.top, cliprect.right, cliprect.bottom);
3206 TRACE("Blt dest: (%d,%d)-(%d,%d)\n", rect.x1, rect.y1, rect.x2, rect.y2);
3207 break;
3208 }
3209 }
3210 else
3211 {
3212 if(rect.x1 != 0 || rect.y1 != 0 ||
3213 rect.x2 != This->currentDesc.Width || rect.y2 != This->currentDesc.Height) {
3214 TRACE("No, dest rectangle doesn't match(surface size)\n");
3215 break;
3216 }
3217 }
3218
3219 TRACE("Yes\n");
3220
3221 /* These flags are unimportant for the flag check, remove them */
3222 if((Flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)) == 0) {
3223 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3224
3225 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3226 * take very long, while a flip is fast.
3227 * This applies to Half-Life, which does such Blts every time it finished
3228 * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3229 * menu. This is also used by all apps when they do windowed rendering
3230 *
3231 * The problem is that flipping is not really the same as copying. After a
3232 * Blt the front buffer is a copy of the back buffer, and the back buffer is
3233 * untouched. Therefore it's necessary to override the swap effect
3234 * and to set it back after the flip.
3235 *
3236 * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3237 * testcases.
3238 */
3239
3240 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3241 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3242
3243 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead\n");
3244 IWineD3DSwapChain_Present((IWineD3DSwapChain *) dstSwapchain, NULL, NULL, 0, NULL, 0);
3245
3246 dstSwapchain->presentParms.SwapEffect = orig_swap;
3247
3248 return WINED3D_OK;
3249 }
3250 break;
3251 }
3252
3253 TRACE("Unsupported blit between buffers on the same swapchain\n");
3254 return WINED3DERR_INVALIDCALL;
3255 } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3256 FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3257 return WINED3DERR_INVALIDCALL;
3258 } else if(dstSwapchain && srcSwapchain) {
3259 FIXME("Implement hardware blit between two different swapchains\n");
3260 return WINED3DERR_INVALIDCALL;
3261 } else if(dstSwapchain) {
3262 if(SrcSurface == myDevice->render_targets[0]) {
3263 TRACE("Blit from active render target to a swapchain\n");
3264 /* Handled with regular texture -> swapchain blit */
3265 }
3266 } else if(srcSwapchain && This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0]) {
3267 FIXME("Implement blit from a swapchain to the active render target\n");
3268 return WINED3DERR_INVALIDCALL;
3269 }
3270
3271 if((srcSwapchain || SrcSurface == myDevice->render_targets[0]) && !dstSwapchain) {
3272 /* Blit from render target to texture */
3273 WINED3DRECT srect;
3274 BOOL upsideDown, stretchx;
3275 BOOL paletteOverride = FALSE;
3276
3277 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3278 TRACE("Color keying not supported by frame buffer to texture blit\n");
3279 return WINED3DERR_INVALIDCALL;
3280 /* Destination color key is checked above */
3281 }
3282
3283 /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3284 * glCopyTexSubImage is a bit picky about the parameters we pass to it
3285 */
3286 if(SrcRect) {
3287 if(SrcRect->top < SrcRect->bottom) {
3288 srect.y1 = SrcRect->top;
3289 srect.y2 = SrcRect->bottom;
3290 upsideDown = FALSE;
3291 } else {
3292 srect.y1 = SrcRect->bottom;
3293 srect.y2 = SrcRect->top;
3294 upsideDown = TRUE;
3295 }
3296 srect.x1 = SrcRect->left;
3297 srect.x2 = SrcRect->right;
3298 } else {
3299 srect.x1 = 0;
3300 srect.y1 = 0;
3301 srect.x2 = Src->currentDesc.Width;
3302 srect.y2 = Src->currentDesc.Height;
3303 upsideDown = FALSE;
3304 }
3305 if(rect.x1 > rect.x2) {
3306 UINT tmp = rect.x2;
3307 rect.x2 = rect.x1;
3308 rect.x1 = tmp;
3309 upsideDown = !upsideDown;
3310 }
3311
3312 if(rect.x2 - rect.x1 != srect.x2 - srect.x1) {
3313 stretchx = TRUE;
3314 } else {
3315 stretchx = FALSE;
3316 }
3317
3318 /* When blitting from a render target a texture, the texture isn't required to have a palette.
3319 * In this case grab the palette from the render target. */
3320 if((This->resource.format == WINED3DFMT_P8) && (This->palette == NULL)) {
3321 paletteOverride = TRUE;
3322 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3323 This->palette = Src->palette;
3324 }
3325
3326 /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3327 * flip the image nor scale it.
3328 *
3329 * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3330 * -> If the app wants a image width an unscaled width, copy it line per line
3331 * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3332 * than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3333 * back buffer. This is slower than reading line per line, thus not used for flipping
3334 * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3335 * pixel by pixel
3336 *
3337 * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3338 * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3339 * backends.
3340 */
3341 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT)) {
3342 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, &srect,
3343 (IWineD3DSurface *)This, &rect, Filter, upsideDown);
3344 } else if((!stretchx) || rect.x2 - rect.x1 > Src->currentDesc.Width ||
3345 rect.y2 - rect.y1 > Src->currentDesc.Height) {
3346 TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3347 fb_copy_to_texture_direct(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3348 } else {
3349 TRACE("Using hardware stretching to flip / stretch the texture\n");
3350 fb_copy_to_texture_hwstretch(This, SrcSurface, srcSwapchain, &srect, &rect, upsideDown, Filter);
3351 }
3352
3353 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3354 if(paletteOverride)
3355 This->palette = NULL;
3356
3357 if(!(This->Flags & SFLAG_DONOTFREE)) {
3358 HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
3359 This->resource.allocatedMemory = NULL;
3360 This->resource.heapMemory = NULL;
3361 } else {
3362 This->Flags &= ~SFLAG_INSYSMEM;
3363 }
3364 /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3365 * path is never entered
3366 */
3367 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INTEXTURE, TRUE);
3368
3369 return WINED3D_OK;
3370 } else if(Src) {
3371 /* Blit from offscreen surface to render target */
3372 float glTexCoord[4];
3373 DWORD oldCKeyFlags = Src->CKeyFlags;
3374 WINEDDCOLORKEY oldBltCKey = Src->SrcBltCKey;
3375 RECT SourceRectangle;
3376 BOOL paletteOverride = FALSE;
3377
3378 TRACE("Blt from surface %p to rendertarget %p\n", Src, This);
3379
3380 if(SrcRect) {
3381 SourceRectangle.left = SrcRect->left;
3382 SourceRectangle.right = SrcRect->right;
3383 SourceRectangle.top = SrcRect->top;
3384 SourceRectangle.bottom = SrcRect->bottom;
3385 } else {
3386 SourceRectangle.left = 0;
3387 SourceRectangle.right = Src->currentDesc.Width;
3388 SourceRectangle.top = 0;
3389 SourceRectangle.bottom = Src->currentDesc.Height;
3390 }
3391
3392 /* When blitting from an offscreen surface to a rendertarget, the source
3393 * surface is not required to have a palette. Our rendering / conversion
3394 * code further down the road retrieves the palette from the surface, so
3395 * it must have a palette set. */
3396 if((Src->resource.format == WINED3DFMT_P8) && (Src->palette == NULL)) {
3397 paletteOverride = TRUE;
3398 TRACE("Source surface (%p) lacks palette, overriding palette with palette %p of destination surface (%p)\n", Src, This->palette, This);
3399 Src->palette = This->palette;
3400 }
3401
3402 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && GL_SUPPORT(EXT_FRAMEBUFFER_BLIT) &&
3403 (Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) == 0) {
3404 TRACE("Using stretch_rect_fbo\n");
3405 /* The source is always a texture, but never the currently active render target, and the texture
3406 * contents are never upside down
3407 */
3408 stretch_rect_fbo((IWineD3DDevice *)myDevice, SrcSurface, (WINED3DRECT *) &SourceRectangle,
3409 (IWineD3DSurface *)This, &rect, Filter, FALSE);
3410
3411 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3412 if(paletteOverride)
3413 Src->palette = NULL;
3414 return WINED3D_OK;
3415 }
3416
3417 if(!CalculateTexRect(Src, &SourceRectangle, glTexCoord)) {
3418 /* Fall back to software */
3419 WARN("(%p) Source texture area (%d,%d)-(%d,%d) is too big\n", Src,
3420 SourceRectangle.left, SourceRectangle.top,
3421 SourceRectangle.right, SourceRectangle.bottom);
3422 return WINED3DERR_INVALIDCALL;
3423 }
3424
3425 /* Color keying: Check if we have to do a color keyed blt,
3426 * and if not check if a color key is activated.
3427 *
3428 * Just modify the color keying parameters in the surface and restore them afterwards
3429 * The surface keeps track of the color key last used to load the opengl surface.
3430 * PreLoad will catch the change to the flags and color key and reload if necessary.
3431 */
3432 if(Flags & WINEDDBLT_KEYSRC) {
3433 /* Use color key from surface */
3434 } else if(Flags & WINEDDBLT_KEYSRCOVERRIDE) {
3435 /* Use color key from DDBltFx */
3436 Src->CKeyFlags |= WINEDDSD_CKSRCBLT;
3437 Src->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3438 } else {
3439 /* Do not use color key */
3440 Src->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3441 }
3442
3443 /* Now load the surface */
3444 IWineD3DSurface_PreLoad((IWineD3DSurface *) Src);
3445
3446 /* Activate the destination context, set it up for blitting */
3447 ActivateContext(myDevice, (IWineD3DSurface *) This, CTXUSAGE_BLIT);
3448
3449 if (dstSwapchain && (IWineD3DSurface *)This == dstSwapchain->frontBuffer) {
3450 RECT windowsize;
3451 POINT offset = {0,0};
3452 UINT h;
3453 ClientToScreen(dstSwapchain->win_handle, &offset);
3454 GetClientRect(dstSwapchain->win_handle, &windowsize);
3455 h = windowsize.bottom - windowsize.top;
3456 rect.x1 -= offset.x; rect.x2 -=offset.x;
3457 rect.y1 -= offset.y; rect.y2 -=offset.y;
3458 rect.y1 += This->currentDesc.Height - h; rect.y2 += This->currentDesc.Height - h;
3459 }
3460
3461 ENTER_GL();
3462 myDevice->blitter->set_shader((IWineD3DDevice *) myDevice, Src->resource.format,
3463 Src->glDescription.target, Src->pow2Width, Src->pow2Height);
3464
3465 /* Bind the texture */
3466 glBindTexture(Src->glDescription.target, Src->glDescription.textureName);
3467 checkGLcall("glBindTexture");
3468
3469 /* Filtering for StretchRect */
3470 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MAG_FILTER,
3471 magLookup[Filter - WINED3DTEXF_NONE]);
3472 checkGLcall("glTexParameteri");
3473 glTexParameteri(Src->glDescription.target, GL_TEXTURE_MIN_FILTER,
3474 minMipLookup[Filter][WINED3DTEXF_NONE]);
3475 checkGLcall("glTexParameteri");
3476 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3477 glTexParameteri(Src->glDescription.target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3478 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
3479 checkGLcall("glTexEnvi");
3480
3481 /* This is for color keying */
3482 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3483 glEnable(GL_ALPHA_TEST);
3484 checkGLcall("glEnable GL_ALPHA_TEST");
3485
3486 /* When the primary render target uses P8, the alpha component contains the palette index.
3487 * Which means that the colorkey is one of the palette entries. In other cases pixels that
3488 * should be masked away have alpha set to 0. */
3489 if(primary_render_target_is_p8(myDevice))
3490 glAlphaFunc(GL_NOTEQUAL, (float)Src->SrcBltCKey.dwColorSpaceLowValue / 256.0);
3491 else
3492 glAlphaFunc(GL_NOTEQUAL, 0.0);
3493 checkGLcall("glAlphaFunc\n");
3494 } else {
3495 glDisable(GL_ALPHA_TEST);
3496 checkGLcall("glDisable GL_ALPHA_TEST");
3497 }
3498
3499 /* Draw a textured quad
3500 */
3501 glBegin(GL_QUADS);
3502
3503 glColor3d(1.0f, 1.0f, 1.0f);
3504 glTexCoord2f(glTexCoord[0], glTexCoord[2]);
3505 glVertex3f(rect.x1,
3506 rect.y1,
3507 0.0);
3508
3509 glTexCoord2f(glTexCoord[0], glTexCoord[3]);
3510 glVertex3f(rect.x1, rect.y2, 0.0);
3511
3512 glTexCoord2f(glTexCoord[1], glTexCoord[3]);
3513 glVertex3f(rect.x2,
3514 rect.y2,
3515 0.0);
3516
3517 glTexCoord2f(glTexCoord[1], glTexCoord[2]);
3518 glVertex3f(rect.x2,
3519 rect.y1,
3520 0.0);
3521 glEnd();
3522 checkGLcall("glEnd");
3523
3524 if(Flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE)) {
3525 glDisable(GL_ALPHA_TEST);
3526 checkGLcall("glDisable(GL_ALPHA_TEST)");
3527 }
3528
3529 glBindTexture(Src->glDescription.target, 0);
3530 checkGLcall("glBindTexture(Src->glDescription.target, 0)");
3531 /* Leave the opengl state valid for blitting */
3532 myDevice->blitter->unset_shader((IWineD3DDevice *) myDevice);
3533
3534 /* Restore the color key parameters */
3535 Src->CKeyFlags = oldCKeyFlags;
3536 Src->SrcBltCKey = oldBltCKey;
3537
3538 /* Clear the palette as the surface didn't have a palette attached, it would confuse GetPalette and other calls */
3539 if(paletteOverride)
3540 Src->palette = NULL;
3541
3542 LEAVE_GL();
3543
3544 /* Flush in case the drawable is used by multiple GL contexts */
3545 if(dstSwapchain && (This == (IWineD3DSurfaceImpl *) dstSwapchain->frontBuffer || dstSwapchain->num_contexts >= 2))
3546 glFlush();
3547
3548 /* TODO: If the surface is locked often, perform the Blt in software on the memory instead */
3549 /* The surface is now in the drawable. On onscreen surfaces or without fbos the texture
3550 * is outdated now
3551 */
3552 IWineD3DSurface_ModifyLocation((IWineD3DSurface *) This, SFLAG_INDRAWABLE, TRUE);
3553
3554 return WINED3D_OK;
3555 } else {
3556 /* Source-Less Blit to render target */
3557 if (Flags & WINEDDBLT_COLORFILL) {
3558 /* This is easy to handle for the D3D Device... */
3559 DWORD color;
3560
3561 TRACE("Colorfill\n");
3562
3563 /* This == (IWineD3DSurfaceImpl *) myDevice->render_targets[0] || dstSwapchain
3564 must be true if we are here */
3565 if (This != (IWineD3DSurfaceImpl *) myDevice->render_targets[0] &&
3566 !(This == (IWineD3DSurfaceImpl*) dstSwapchain->frontBuffer ||
3567 (dstSwapchain->backBuffer && This == (IWineD3DSurfaceImpl*) dstSwapchain->backBuffer[0]))) {
3568 TRACE("Surface is higher back buffer, falling back to software\n");
3569 return WINED3DERR_INVALIDCALL;
3570 }
3571
3572 /* The color as given in the Blt function is in the format of the frame-buffer...
3573 * 'clear' expect it in ARGB format => we need to do some conversion :-)
3574 */
3575 if (This->resource.format == WINED3DFMT_P8) {
3576 DWORD alpha;
3577
3578 if (primary_render_target_is_p8(myDevice)) alpha = DDBltFx->u5.dwFillColor << 24;
3579 else alpha = 0xFF000000;
3580
3581 if (This->palette) {
3582 color = (alpha |
3583 (This->palette->palents[DDBltFx->u5.dwFillColor].peRed << 16) |
3584 (This->palette->palents[DDBltFx->u5.dwFillColor].peGreen << 8) |
3585 (This->palette->palents[DDBltFx->u5.dwFillColor].peBlue));
3586 } else {
3587 color = alpha;
3588 }
3589 }
3590 else if (This->resource.format == WINED3DFMT_R5G6B5) {
3591 if (DDBltFx->u5.dwFillColor == 0xFFFF) {
3592 color = 0xFFFFFFFF;
3593 } else {
3594 color = ((0xFF000000) |
3595 ((DDBltFx->u5.dwFillColor & 0xF800) << 8) |
3596 ((DDBltFx->u5.dwFillColor & 0x07E0) << 5) |
3597 ((DDBltFx->u5.dwFillColor & 0x001F) << 3));
3598 }
3599 }
3600 else if ((This->resource.format == WINED3DFMT_R8G8B8) ||
3601 (This->resource.format == WINED3DFMT_X8R8G8B8) ) {
3602 color = 0xFF000000 | DDBltFx->u5.dwFillColor;
3603 }
3604 else if (This->resource.format == WINED3DFMT_A8R8G8B8) {
3605 color = DDBltFx->u5.dwFillColor;
3606 }
3607 else {
3608 ERR("Wrong surface type for BLT override(Format doesn't match) !\n");
3609 return WINED3DERR_INVALIDCALL;
3610 }
3611
3612 TRACE("(%p) executing Render Target override, color = %x\n", This, color);
3613 IWineD3DDeviceImpl_ClearSurface(myDevice, This,
3614 1, /* Number of rectangles */
3615 &rect, WINED3DCLEAR_TARGET, color,
3616 0.0 /* Z */,
3617 0 /* Stencil */);
3618 return WINED3D_OK;
3619 }
3620 }
3621
3622 /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3623 TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3624 return WINED3DERR_INVALIDCALL;
3625 }
3626
3627 static HRESULT WINAPI IWineD3DSurfaceImpl_BltZ(IWineD3DSurfaceImpl *This, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx)
3628 {
3629 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3630 float depth;
3631
3632 if (Flags & WINEDDBLT_DEPTHFILL) {
3633 switch(This->resource.format) {
3634 case WINED3DFMT_D16:
3635 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000ffff;
3636 break;
3637 case WINED3DFMT_D15S1:
3638 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x0000fffe;
3639 break;
3640 case WINED3DFMT_D24S8:
3641 case WINED3DFMT_D24X8:
3642 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0x00ffffff;
3643 break;
3644 case WINED3DFMT_D32:
3645 depth = (float) DDBltFx->u5.dwFillDepth / (float) 0xffffffff;
3646 break;
3647 default:
3648 depth = 0.0;
3649 ERR("Unexpected format for depth fill: %s\n", debug_d3dformat(This->resource.format));
3650 }
3651
3652 return IWineD3DDevice_Clear((IWineD3DDevice *) myDevice,
3653 DestRect == NULL ? 0 : 1,
3654 (WINED3DRECT *) DestRect,
3655 WINED3DCLEAR_ZBUFFER,
3656 0x00000000,
3657 depth,
3658 0x00000000);
3659 }
3660
3661 FIXME("(%p): Unsupp depthstencil blit\n", This);
3662 return WINED3DERR_INVALIDCALL;
3663 }
3664
3665 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, RECT *DestRect, IWineD3DSurface *SrcSurface, RECT *SrcRect, DWORD Flags, WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter) {
3666 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3667 IWineD3DSurfaceImpl *Src = (IWineD3DSurfaceImpl *) SrcSurface;
3668 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3669 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx);
3670 TRACE("(%p): Usage is %s\n", This, debug_d3dusage(This->resource.usage));
3671
3672 if ( (This->Flags & SFLAG_LOCKED) || ((Src != NULL) && (Src->Flags & SFLAG_LOCKED)))
3673 {
3674 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3675 return WINEDDERR_SURFACEBUSY;
3676 }
3677
3678 /* Accessing the depth stencil is supposed to fail between a BeginScene and EndScene pair,
3679 * except depth blits, which seem to work
3680 */
3681 if(iface == myDevice->stencilBufferTarget || (SrcSurface && SrcSurface == myDevice->stencilBufferTarget)) {
3682 if(myDevice->inScene && !(Flags & WINEDDBLT_DEPTHFILL)) {
3683 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3684 return WINED3DERR_INVALIDCALL;
3685 } else if(IWineD3DSurfaceImpl_BltZ(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx) == WINED3D_OK) {
3686 TRACE("Z Blit override handled the blit\n");
3687 return WINED3D_OK;
3688 }
3689 }
3690
3691 /* Special cases for RenderTargets */
3692 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3693 ( Src && (Src->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3694 if(IWineD3DSurfaceImpl_BltOverride(This, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter) == WINED3D_OK) return WINED3D_OK;
3695 }
3696
3697 /* For the rest call the X11 surface implementation.
3698 * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
3699 * other Blts are rather rare
3700 */
3701 return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, SrcSurface, SrcRect, Flags, DDBltFx, Filter);
3702 }
3703
3704 HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty, IWineD3DSurface *Source, RECT *rsrc, DWORD trans) {
3705 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3706 IWineD3DSurfaceImpl *srcImpl = (IWineD3DSurfaceImpl *) Source;
3707 IWineD3DDeviceImpl *myDevice = This->resource.wineD3DDevice;
3708 TRACE("(%p)->(%d, %d, %p, %p, %08x\n", iface, dstx, dsty, Source, rsrc, trans);
3709
3710 if ( (This->Flags & SFLAG_LOCKED) || ((srcImpl != NULL) && (srcImpl->Flags & SFLAG_LOCKED)))
3711 {
3712 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
3713 return WINEDDERR_SURFACEBUSY;
3714 }
3715
3716 if(myDevice->inScene &&
3717 (iface == myDevice->stencilBufferTarget ||
3718 (Source && Source == myDevice->stencilBufferTarget))) {
3719 TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
3720 return WINED3DERR_INVALIDCALL;
3721 }
3722
3723 /* Special cases for RenderTargets */
3724 if( (This->resource.usage & WINED3DUSAGE_RENDERTARGET) ||
3725 ( srcImpl && (srcImpl->resource.usage & WINED3DUSAGE_RENDERTARGET) )) {
3726
3727 RECT SrcRect, DstRect;
3728 DWORD Flags=0;
3729
3730 if(rsrc) {
3731 SrcRect.left = rsrc->left;
3732 SrcRect.top= rsrc->top;
3733 SrcRect.bottom = rsrc->bottom;
3734 SrcRect.right = rsrc->right;
3735 } else {
3736 SrcRect.left = 0;
3737 SrcRect.top = 0;
3738 SrcRect.right = srcImpl->currentDesc.Width;
3739 SrcRect.bottom = srcImpl->currentDesc.Height;
3740 }
3741
3742 DstRect.left = dstx;
3743 DstRect.top=dsty;
3744 DstRect.right = dstx + SrcRect.right - SrcRect.left;
3745 DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
3746
3747 /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
3748 if(trans & WINEDDBLTFAST_SRCCOLORKEY)
3749 Flags |= WINEDDBLT_KEYSRC;
3750 if(trans & WINEDDBLTFAST_DESTCOLORKEY)
3751 Flags |= WINEDDBLT_KEYDEST;
3752 if(trans & WINEDDBLTFAST_WAIT)
3753 Flags |= WINEDDBLT_WAIT;
3754 if(trans & WINEDDBLTFAST_DONOTWAIT)
3755 Flags |= WINEDDBLT_DONOTWAIT;
3756
3757 if(IWineD3DSurfaceImpl_BltOverride(This, &DstRect, Source, &SrcRect, Flags, NULL, WINED3DTEXF_POINT) == WINED3D_OK) return WINED3D_OK;
3758 }
3759
3760
3761 return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, Source, rsrc, trans);
3762 }
3763
3764 HRESULT WINAPI IWineD3DSurfaceImpl_RealizePalette(IWineD3DSurface *iface) {
3765 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3766 RGBQUAD col[256];
3767 IWineD3DPaletteImpl *pal = This->palette;
3768 unsigned int n;
3769 TRACE("(%p)\n", This);
3770
3771 if (!pal) return WINED3D_OK;
3772
3773 if(This->resource.format == WINED3DFMT_P8 ||
3774 This->resource.format == WINED3DFMT_A8P8)
3775 {
3776 int bpp;
3777 GLenum format, internal, type;
3778 CONVERT_TYPES convert;
3779
3780 /* Check if we are using a RTL mode which uses texturing for uploads */
3781 BOOL use_texture = (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX);
3782
3783 /* Check if we have hardware palette conversion if we have convert is set to NO_CONVERSION */
3784 d3dfmt_get_conv(This, TRUE, use_texture, &format, &internal, &type, &convert, &bpp, This->srgb);
3785
3786 if((This->resource.usage & WINED3DUSAGE_RENDERTARGET) && (convert == NO_CONVERSION))
3787 {
3788 /* Make sure the texture is up to date. This call doesn't do anything if the texture is already up to date. */
3789 IWineD3DSurface_LoadLocation(iface, SFLAG_INTEXTURE, NULL);
3790
3791 /* We want to force a palette refresh, so mark the drawable as not being up to date */
3792 IWineD3DSurface_ModifyLocation(iface, SFLAG_INDRAWABLE, FALSE);
3793
3794 /* Re-upload the palette */
3795 d3dfmt_p8_upload_palette(iface, convert);
3796 } else {
3797 if(!(This->Flags & SFLAG_INSYSMEM)) {
3798 TRACE("Palette changed with surface that does not have an up to date system memory copy\n");
3799 IWineD3DSurface_LoadLocation(iface, SFLAG_INSYSMEM, NULL);
3800 }
3801 TRACE("Dirtifying surface\n");
3802 IWineD3DSurface_ModifyLocation(iface, SFLAG_INSYSMEM, TRUE);
3803 }
3804 }
3805
3806 if(This->Flags & SFLAG_DIBSECTION) {
3807 TRACE("(%p): Updating the hdc's palette\n", This);
3808 for (n=0; n<256; n++) {
3809 col[n].rgbRed = pal->palents[n].peRed;
3810 col[n].rgbGreen = pal->palents[n].peGreen;
3811 col[n].rgbBlue = pal->palents[n].peBlue;
3812 col[n].rgbReserved = 0;
3813 }
3814 SetDIBColorTable(This->hDC, 0, 256, col);
3815 }
3816
3817 /* Propagate the changes to the drawable when we have a palette. */
3818 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET)
3819 IWineD3DSurface_LoadLocation(iface, SFLAG_INDRAWABLE, NULL);
3820
3821 return WINED3D_OK;
3822 }
3823
3824 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
3825 /** Check against the maximum texture sizes supported by the video card **/
3826 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
3827 unsigned int pow2Width, pow2Height;
3828 const GlPixelFormatDesc *glDesc;
3829
3830 getFormatDescEntry(This->resource.format, &GLINFO_LOCATION, &glDesc);
3831 /* Setup some glformat defaults */
3832 This->glDescription.glFormat = glDesc->glFormat;
3833 This->glDescription.glFormatInternal = glDesc->glInternal;
3834 This->glDescription.glType = glDesc->glType;
3835
3836 This->glDescription.textureName = 0;
3837 This->glDescription.target = GL_TEXTURE_2D;
3838
3839 /* Non-power2 support */
3840 if (GL_SUPPORT(ARB_TEXTURE_NON_POWER_OF_TWO) || GL_SUPPORT(WINE_NORMALIZED_TEXRECT)) {
3841 pow2Width = This->currentDesc.Width;
3842 pow2Height = This->currentDesc.Height;
3843 } else {
3844 /* Find the nearest pow2 match */
3845 pow2Width = pow2Height = 1;
3846 while (pow2Width < This->currentDesc.Width) pow2Width <<= 1;
3847 while (pow2Height < This->currentDesc.Height) pow2Height <<= 1;
3848 }
3849 This->pow2Width = pow2Width;
3850 This->pow2Height = pow2Height;
3851
3852 if (pow2Width > This->currentDesc.Width || pow2Height > This->currentDesc.Height) {
3853 WINED3DFORMAT Format = This->resource.format;
3854 /** TODO: add support for non power two compressed textures **/
3855 if (Format == WINED3DFMT_DXT1 || Format == WINED3DFMT_DXT2 || Format == WINED3DFMT_DXT3
3856 || Format == WINED3DFMT_DXT4 || Format == WINED3DFMT_DXT5
3857 || This->resource.format == WINED3DFMT_ATI2N) {
3858 FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
3859 This, This->currentDesc.Width, This->currentDesc.Height);
3860 return WINED3DERR_NOTAVAILABLE;
3861 }
3862 }
3863
3864 if(pow2Width != This->currentDesc.Width ||
3865 pow2Height != This->currentDesc.Height) {
3866 This->Flags |= SFLAG_NONPOW2;
3867 }
3868
3869 TRACE("%p\n", This);
3870 if ((This->pow2Width > GL_LIMITS(texture_size) || This->pow2Height > GL_LIMITS(texture_size)) && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))) {
3871 /* one of three options
3872 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)
3873 2: Set the texture to the maximum size (bad idea)
3874 3: WARN and return WINED3DERR_NOTAVAILABLE;
3875 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.
3876 */
3877 WARN("(%p) Creating an oversized surface\n", This);
3878 This->Flags |= SFLAG_OVERSIZE;
3879
3880 /* This will be initialized on the first blt */
3881 This->glRect.left = 0;
3882 This->glRect.top = 0;
3883 This->glRect.right = 0;
3884 This->glRect.bottom = 0;
3885 } else {
3886 /* Check this after the oversize check - do not make an oversized surface a texture_rectangle one.
3887 Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
3888 is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
3889 doesn't work in combination with ARB_TEXTURE_RECTANGLE.
3890 */
3891 if(This->Flags & SFLAG_NONPOW2 && GL_SUPPORT(ARB_TEXTURE_RECTANGLE) &&
3892 !((This->resource.format == WINED3DFMT_P8) && GL_SUPPORT(EXT_PALETTED_TEXTURE) && (wined3d_settings.rendertargetlock_mode == RTL_READTEX || wined3d_settings.rendertargetlock_mode == RTL_TEXTEX)))
3893 {
3894 This->glDescription.target = GL_TEXTURE_RECTANGLE_ARB;
3895 This->pow2Width = This->currentDesc.Width;
3896 This->pow2Height = This->currentDesc.Height;
3897 This->Flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
3898 }
3899
3900 /* No oversize, gl rect is the full texture size */
3901 This->Flags &= ~SFLAG_OVERSIZE;
3902 This->glRect.left = 0;
3903 This->glRect.top = 0;
3904 This->glRect.right = This->pow2Width;
3905 This->glRect.bottom = This->pow2Height;
3906 }
3907
3908 if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
3909 switch(wined3d_settings.offscreen_rendering_mode) {
3910 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
3911 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
3912 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
3913 }
3914 }
3915
3916 This->Flags |= SFLAG_INSYSMEM;
3917
3918 return WINED3D_OK;
3919 }
3920
3921 void surface_modify_ds_location(IWineD3DSurface *iface, DWORD location) {
3922 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3923
3924 TRACE("(%p) New location %#x\n", This, location);
3925
3926 if (location & ~SFLAG_DS_LOCATIONS) {
3927 FIXME("(%p) Invalid location (%#x) specified\n", This, location);
3928 }
3929
3930 This->Flags &= ~SFLAG_DS_LOCATIONS;
3931 This->Flags |= location;
3932 }
3933
3934 void surface_load_ds_location(IWineD3DSurface *iface, DWORD location) {
3935 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
3936 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
3937
3938 TRACE("(%p) New location %#x\n", This, location);
3939
3940 /* TODO: Make this work for modes other than FBO */
3941 if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
3942
3943 if (This->Flags & location) {
3944 TRACE("(%p) Location (%#x) is already up to date\n", This, location);
3945 return;
3946 }
3947
3948 if (This->current_renderbuffer) {
3949 FIXME("(%p) Not supported with fixed up depth stencil\n", This);
3950 return;
3951 }
3952
3953 if (location == SFLAG_DS_OFFSCREEN) {
3954 if (This->Flags & SFLAG_DS_ONSCREEN) {
3955 GLint old_binding = 0;
3956
3957 TRACE("(%p) Copying onscreen depth buffer to depth texture\n", This);
3958
3959 ENTER_GL();
3960
3961 if (!device->depth_blt_texture) {
3962 glGenTextures(1, &device->depth_blt_texture);
3963 }
3964
3965 /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
3966 * directly on the FBO texture. That's because we need to flip. */
3967 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
3968 glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
3969 glBindTexture(GL_TEXTURE_2D, device->depth_blt_texture);
3970 glCopyTexImage2D(This->glDescription.target,
3971 This->glDescription.level,
3972 This->glDescription.glFormatInternal,
3973 0,
3974 0,
3975 This->currentDesc.Width,
3976 This->currentDesc.Height,
3977 0);
3978 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3979 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3980 glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
3981 glBindTexture(GL_TEXTURE_2D, old_binding);
3982
3983 /* Setup the destination */
3984 if (!device->depth_blt_rb) {
3985 GL_EXTCALL(glGenRenderbuffersEXT(1, &device->depth_blt_rb));
3986 checkGLcall("glGenRenderbuffersEXT");
3987 }
3988 if (device->depth_blt_rb_w != This->currentDesc.Width
3989 || device->depth_blt_rb_h != This->currentDesc.Height) {
3990 GL_EXTCALL(glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, device->depth_blt_rb));
3991 checkGLcall("glBindRenderbufferEXT");
3992 GL_EXTCALL(glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, This->currentDesc.Width, This->currentDesc.Height));
3993 checkGLcall("glRenderbufferStorageEXT");
3994 device->depth_blt_rb_w = This->currentDesc.Width;
3995 device->depth_blt_rb_h = This->currentDesc.Height;
3996 }
3997
3998 context_bind_fbo((IWineD3DDevice *)device, GL_FRAMEBUFFER_EXT, &device->activeContext->dst_fbo);
3999 GL_EXTCALL(glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, device->depth_blt_rb));
4000 checkGLcall("glFramebufferRenderbufferEXT");
4001 context_attach_depth_stencil_fbo(device, GL_FRAMEBUFFER_EXT, iface, FALSE);
4002
4003 /* Do the actual blit */
4004 depth_blt((IWineD3DDevice *)device, device->depth_blt_texture, This->currentDesc.Width, This->currentDesc.Height);
4005 checkGLcall("depth_blt");
4006
4007 if (device->activeContext->current_fbo) {
4008 context_bind_fbo((IWineD3DDevice *)device, GL_FRAMEBUFFER_EXT, &device->activeContext->current_fbo->id);
4009 } else {
4010 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
4011 checkGLcall("glBindFramebuffer()");
4012 }
4013
4014 LEAVE_GL();
4015 } else {
4016 FIXME("No up to date depth stencil location\n");
4017 }
4018 } else if (location == SFLAG_DS_ONSCREEN) {
4019 if (This->Flags & SFLAG_DS_OFFSCREEN) {
4020 TRACE("(%p) Copying depth texture to onscreen depth buffer\n", This);
4021
4022 ENTER_GL();
4023
4024 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0));
4025 checkGLcall("glBindFramebuffer()");
4026 depth_blt((IWineD3DDevice *)device, This->glDescription.textureName, This->currentDesc.Width, This->currentDesc.Height);
4027 checkGLcall("depth_blt");
4028
4029 if (device->activeContext->current_fbo) {
4030 GL_EXTCALL(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, device->activeContext->current_fbo->id));
4031 checkGLcall("glBindFramebuffer()");
4032 }
4033
4034 LEAVE_GL();
4035 } else {
4036 FIXME("No up to date depth stencil location\n");
4037 }
4038 } else {
4039 ERR("(%p) Invalid location (%#x) specified\n", This, location);
4040 }
4041
4042 This->Flags |= location;
4043 }
4044
4045 static void WINAPI IWineD3DSurfaceImpl_ModifyLocation(IWineD3DSurface *iface, DWORD flag, BOOL persistent) {
4046 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4047 IWineD3DBaseTexture *texture;
4048 IWineD3DSurfaceImpl *overlay;
4049
4050 TRACE("(%p)->(%s, %s)\n", iface,
4051 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
4052 persistent ? "TRUE" : "FALSE");
4053
4054 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4055 IWineD3DSwapChain *swapchain = NULL;
4056
4057 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
4058 TRACE("Surface %p is an onscreen surface\n", iface);
4059
4060 IWineD3DSwapChain_Release(swapchain);
4061 } else {
4062 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4063 if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4064 }
4065 }
4066
4067 if(persistent) {
4068 if((This->Flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE)) {
4069 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4070 TRACE("Passing to container\n");
4071 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4072 IWineD3DBaseTexture_Release(texture);
4073 }
4074 }
4075 This->Flags &= ~SFLAG_LOCATIONS;
4076 This->Flags |= flag;
4077
4078 /* Redraw emulated overlays, if any */
4079 if(flag & SFLAG_INDRAWABLE && !list_empty(&This->overlays)) {
4080 LIST_FOR_EACH_ENTRY(overlay, &This->overlays, IWineD3DSurfaceImpl, overlay_entry) {
4081 IWineD3DSurface_DrawOverlay((IWineD3DSurface *) overlay);
4082 }
4083 }
4084 } else {
4085 if((This->Flags & SFLAG_INTEXTURE) && (flag & SFLAG_INTEXTURE)) {
4086 if (IWineD3DSurface_GetContainer(iface, &IID_IWineD3DBaseTexture, (void **)&texture) == WINED3D_OK) {
4087 TRACE("Passing to container\n");
4088 IWineD3DBaseTexture_SetDirty(texture, TRUE);
4089 IWineD3DBaseTexture_Release(texture);
4090 }
4091 }
4092 This->Flags &= ~flag;
4093 }
4094 }
4095
4096 struct coords {
4097 GLfloat x, y, z;
4098 };
4099
4100 static inline void surface_blt_to_drawable(IWineD3DSurfaceImpl *This, const RECT *rect_in) {
4101 struct coords coords[4];
4102 RECT rect;
4103 IWineD3DSwapChain *swapchain = NULL;
4104 IWineD3DBaseTexture *texture = NULL;
4105 HRESULT hr;
4106 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4107
4108 if(rect_in) {
4109 rect = *rect_in;
4110 } else {
4111 rect.left = 0;
4112 rect.top = 0;
4113 rect.right = This->currentDesc.Width;
4114 rect.bottom = This->currentDesc.Height;
4115 }
4116
4117 ActivateContext(device, (IWineD3DSurface*)This, CTXUSAGE_BLIT);
4118 ENTER_GL();
4119
4120 if(This->glDescription.target == GL_TEXTURE_RECTANGLE_ARB) {
4121 glEnable(GL_TEXTURE_RECTANGLE_ARB);
4122 checkGLcall("glEnable(GL_TEXTURE_RECTANGLE_ARB)");
4123 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName);
4124 checkGLcall("GL_TEXTURE_RECTANGLE_ARB, This->glDescription.textureName)");
4125 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4126 checkGLcall("glTexParameteri");
4127 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4128 checkGLcall("glTexParameteri");
4129
4130 coords[0].x = rect.left;
4131 coords[0].z = 0;
4132
4133 coords[1].x = rect.left;
4134 coords[1].z = 0;
4135
4136 coords[2].x = rect.right;
4137 coords[2].z = 0;
4138
4139 coords[3].x = rect.right;
4140 coords[3].z = 0;
4141
4142 coords[0].y = rect.top;
4143 coords[1].y = rect.bottom;
4144 coords[2].y = rect.bottom;
4145 coords[3].y = rect.top;
4146 } else if(This->glDescription.target == GL_TEXTURE_2D) {
4147 glEnable(GL_TEXTURE_2D);
4148 checkGLcall("glEnable(GL_TEXTURE_2D)");
4149 glBindTexture(GL_TEXTURE_2D, This->glDescription.textureName);
4150 checkGLcall("GL_TEXTURE_2D, This->glDescription.textureName)");
4151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4152 checkGLcall("glTexParameteri");
4153 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4154 checkGLcall("glTexParameteri");
4155
4156 coords[0].x = (float)rect.left / This->pow2Width;
4157 coords[0].z = 0;
4158
4159 coords[1].x = (float)rect.left / This->pow2Width;
4160 coords[1].z = 0;
4161
4162 coords[2].x = (float)rect.right / This->pow2Width;
4163 coords[2].z = 0;
4164
4165 coords[3].x = (float)rect.right / This->pow2Width;
4166 coords[3].z = 0;
4167
4168 coords[0].y = (float)rect.top / This->pow2Height;
4169 coords[1].y = (float)rect.bottom / This->pow2Height;
4170 coords[2].y = (float)rect.bottom / This->pow2Height;
4171 coords[3].y = (float)rect.top / This->pow2Height;
4172 } else {
4173 /* Must be a cube map */
4174 glEnable(GL_TEXTURE_CUBE_MAP_ARB);
4175 checkGLcall("glEnable(GL_TEXTURE_CUBE_MAP_ARB)");
4176 glBindTexture(GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName);
4177 checkGLcall("GL_TEXTURE_CUBE_MAP_ARB, This->glDescription.textureName)");
4178 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4179 checkGLcall("glTexParameteri");
4180 glTexParameteri(GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4181 checkGLcall("glTexParameteri");
4182
4183 switch(This->glDescription.target) {
4184 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
4185 coords[0].x = 1; coords[0].y = -1; coords[0].z = 1;
4186 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
4187 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
4188 coords[3].x = 1; coords[3].y = -1; coords[3].z = -1;
4189 break;
4190
4191 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
4192 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
4193 coords[1].x = -1; coords[1].y = 1; coords[1].z = 1;
4194 coords[2].x = -1; coords[2].y = 1; coords[2].z = -1;
4195 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
4196 break;
4197
4198 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
4199 coords[0].x = -1; coords[0].y = 1; coords[0].z = 1;
4200 coords[1].x = 1; coords[1].y = 1; coords[1].z = 1;
4201 coords[2].x = 1; coords[2].y = 1; coords[2].z = -1;
4202 coords[3].x = -1; coords[3].y = 1; coords[3].z = -1;
4203 break;
4204
4205 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
4206 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
4207 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
4208 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
4209 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
4210 break;
4211
4212 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
4213 coords[0].x = -1; coords[0].y = -1; coords[0].z = 1;
4214 coords[1].x = 1; coords[1].y = -1; coords[1].z = 1;
4215 coords[2].x = 1; coords[2].y = -1; coords[2].z = 1;
4216 coords[3].x = -1; coords[3].y = -1; coords[3].z = 1;
4217 break;
4218
4219 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
4220 coords[0].x = -1; coords[0].y = -1; coords[0].z = -1;
4221 coords[1].x = 1; coords[1].y = -1; coords[1].z = -1;
4222 coords[2].x = 1; coords[2].y = -1; coords[2].z = -1;
4223 coords[3].x = -1; coords[3].y = -1; coords[3].z = -1;
4224 break;
4225
4226 default:
4227 ERR("Unexpected texture target\n");
4228 LEAVE_GL();
4229 return;
4230 }
4231 }
4232
4233 glBegin(GL_QUADS);
4234 glTexCoord3fv(&coords[0].x);
4235 glVertex2i(rect.left, device->render_offscreen ? rect.bottom : rect.top);
4236
4237 glTexCoord3fv(&coords[1].x);
4238 glVertex2i(rect.left, device->render_offscreen ? rect.top : rect.bottom);
4239
4240 glTexCoord3fv(&coords[2].x);
4241 glVertex2i(rect.right, device->render_offscreen ? rect.top : rect.bottom);
4242
4243 glTexCoord3fv(&coords[3].x);
4244 glVertex2i(rect.right, device->render_offscreen ? rect.bottom : rect.top);
4245 glEnd();
4246 checkGLcall("glEnd");
4247
4248 if(This->glDescription.target != GL_TEXTURE_2D) {
4249 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4250 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4251 } else {
4252 glDisable(GL_TEXTURE_2D);
4253 checkGLcall("glDisable(GL_TEXTURE_2D)");
4254 }
4255 LEAVE_GL();
4256
4257 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DSwapChain, (void **) &swapchain);
4258 if(hr == WINED3D_OK && swapchain) {
4259 /* Make sure to flush the buffers. This is needed in apps like Red Alert II and Tiberian SUN that use multiple WGL contexts. */
4260 if(((IWineD3DSwapChainImpl*)swapchain)->frontBuffer == (IWineD3DSurface*)This ||
4261 ((IWineD3DSwapChainImpl*)swapchain)->num_contexts >= 2)
4262 glFlush();
4263
4264 IWineD3DSwapChain_Release(swapchain);
4265 } else {
4266 /* We changed the filtering settings on the texture. Inform the container about this to get the filters
4267 * reset properly next draw
4268 */
4269 hr = IWineD3DSurface_GetContainer((IWineD3DSurface*)This, &IID_IWineD3DBaseTexture, (void **) &texture);
4270 if(hr == WINED3D_OK && texture) {
4271 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
4272 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
4273 ((IWineD3DBaseTextureImpl *) texture)->baseTexture.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
4274 IWineD3DBaseTexture_Release(texture);
4275 }
4276 }
4277 }
4278
4279 /*****************************************************************************
4280 * IWineD3DSurface::LoadLocation
4281 *
4282 * Copies the current surface data from wherever it is to the requested
4283 * location. The location is one of the surface flags, SFLAG_INSYSMEM,
4284 * SFLAG_INTEXTURE and SFLAG_INDRAWABLE. When the surface is current in
4285 * multiple locations, the gl texture is preferred over the drawable, which is
4286 * preferred over system memory. The PBO counts as system memory. If rect is
4287 * not NULL, only the specified rectangle is copied (only supported for
4288 * sysmem<->drawable copies at the moment). If rect is NULL, the destination
4289 * location is marked up to date after the copy.
4290 *
4291 * Parameters:
4292 * flag: Surface location flag to be updated
4293 * rect: rectangle to be copied
4294 *
4295 * Returns:
4296 * WINED3D_OK on success
4297 * WINED3DERR_DEVICELOST on an internal error
4298 *
4299 *****************************************************************************/
4300 static HRESULT WINAPI IWineD3DSurfaceImpl_LoadLocation(IWineD3DSurface *iface, DWORD flag, const RECT *rect) {
4301 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4302 IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
4303 IWineD3DSwapChain *swapchain = NULL;
4304 GLenum format, internal, type;
4305 CONVERT_TYPES convert;
4306 int bpp;
4307 int width, pitch, outpitch;
4308 BYTE *mem;
4309
4310 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO) {
4311 if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
4312 TRACE("Surface %p is an onscreen surface\n", iface);
4313
4314 IWineD3DSwapChain_Release(swapchain);
4315 } else {
4316 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4317 * Prefer SFLAG_INTEXTURE. */
4318 if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4319 }
4320 }
4321
4322 TRACE("(%p)->(%s, %p)\n", iface,
4323 flag == SFLAG_INSYSMEM ? "SFLAG_INSYSMEM" : flag == SFLAG_INDRAWABLE ? "SFLAG_INDRAWABLE" : "SFLAG_INTEXTURE",
4324 rect);
4325 if(rect) {
4326 TRACE("Rectangle: (%d,%d)-(%d,%d)\n", rect->left, rect->top, rect->right, rect->bottom);
4327 }
4328
4329 if(This->Flags & flag) {
4330 TRACE("Location already up to date\n");
4331 return WINED3D_OK;
4332 }
4333
4334 if(!(This->Flags & SFLAG_LOCATIONS)) {
4335 ERR("Surface does not have any up to date location\n");
4336 This->Flags |= SFLAG_LOST;
4337 return WINED3DERR_DEVICELOST;
4338 }
4339
4340 if(flag == SFLAG_INSYSMEM) {
4341 surface_prepare_system_memory(This);
4342
4343 /* Download the surface to system memory */
4344 if(This->Flags & SFLAG_INTEXTURE) {
4345 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4346 surface_bind_and_dirtify(This);
4347
4348 surface_download_data(This);
4349 } else {
4350 read_from_framebuffer(This, rect,
4351 This->resource.allocatedMemory,
4352 IWineD3DSurface_GetPitch(iface));
4353 }
4354 } else if(flag == SFLAG_INDRAWABLE) {
4355 if(This->Flags & SFLAG_INTEXTURE) {
4356 surface_blt_to_drawable(This, rect);
4357 } else {
4358 d3dfmt_get_conv(This, TRUE /* We need color keying */, FALSE /* We won't use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4359
4360 /* The width is in 'length' not in bytes */
4361 width = This->currentDesc.Width;
4362 pitch = IWineD3DSurface_GetPitch(iface);
4363
4364 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4365 * but it isn't set (yet) in all cases it is getting called. */
4366 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4367 TRACE("Removing the pbo attached to surface %p\n", This);
4368 surface_remove_pbo(This);
4369 }
4370
4371 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4372 int height = This->currentDesc.Height;
4373
4374 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4375 outpitch = width * bpp;
4376 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4377
4378 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4379 if(!mem) {
4380 ERR("Out of memory %d, %d!\n", outpitch, height);
4381 return WINED3DERR_OUTOFVIDEOMEMORY;
4382 }
4383 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4384
4385 This->Flags |= SFLAG_CONVERTED;
4386 } else {
4387 This->Flags &= ~SFLAG_CONVERTED;
4388 mem = This->resource.allocatedMemory;
4389 }
4390
4391 flush_to_framebuffer_drawpixels(This, format, type, bpp, mem);
4392
4393 /* Don't delete PBO memory */
4394 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4395 HeapFree(GetProcessHeap(), 0, mem);
4396 }
4397 } else /* if(flag == SFLAG_INTEXTURE) */ {
4398 if (This->Flags & SFLAG_INDRAWABLE) {
4399 read_from_framebuffer_texture(This);
4400 } else { /* Upload from system memory */
4401 d3dfmt_get_conv(This, TRUE /* We need color keying */, TRUE /* We will use textures */, &format, &internal, &type, &convert, &bpp, This->srgb);
4402
4403 if(!device->isInDraw) ActivateContext(device, device->lastActiveRenderTarget, CTXUSAGE_RESOURCELOAD);
4404 surface_bind_and_dirtify(This);
4405 ENTER_GL();
4406
4407 /* The only place where LoadTexture() might get called when isInDraw=1
4408 * is ActivateContext where lastActiveRenderTarget is preloaded.
4409 */
4410 if(iface == device->lastActiveRenderTarget && device->isInDraw)
4411 ERR("Reading back render target but SFLAG_INDRAWABLE not set\n");
4412
4413 /* Otherwise: System memory copy must be most up to date */
4414
4415 if(This->CKeyFlags & WINEDDSD_CKSRCBLT) {
4416 This->Flags |= SFLAG_GLCKEY;
4417 This->glCKey = This->SrcBltCKey;
4418 }
4419 else This->Flags &= ~SFLAG_GLCKEY;
4420
4421 /* The width is in 'length' not in bytes */
4422 width = This->currentDesc.Width;
4423 pitch = IWineD3DSurface_GetPitch(iface);
4424
4425 /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4426 * but it isn't set (yet) in all cases it is getting called. */
4427 if((convert != NO_CONVERSION) && (This->Flags & SFLAG_PBO)) {
4428 TRACE("Removing the pbo attached to surface %p\n", This);
4429 surface_remove_pbo(This);
4430 }
4431
4432 if((convert != NO_CONVERSION) && This->resource.allocatedMemory) {
4433 int height = This->currentDesc.Height;
4434
4435 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4436 outpitch = width * bpp;
4437 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4438
4439 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4440 if(!mem) {
4441 ERR("Out of memory %d, %d!\n", outpitch, height);
4442 return WINED3DERR_OUTOFVIDEOMEMORY;
4443 }
4444 d3dfmt_convert_surface(This->resource.allocatedMemory, mem, pitch, width, height, outpitch, convert, This);
4445
4446 This->Flags |= SFLAG_CONVERTED;
4447 } else if( (This->resource.format == WINED3DFMT_P8) && (GL_SUPPORT(EXT_PALETTED_TEXTURE) || GL_SUPPORT(ARB_FRAGMENT_PROGRAM)) ) {
4448 d3dfmt_p8_upload_palette(iface, convert);
4449 This->Flags &= ~SFLAG_CONVERTED;
4450 mem = This->resource.allocatedMemory;
4451 } else {
4452 This->Flags &= ~SFLAG_CONVERTED;
4453 mem = This->resource.allocatedMemory;
4454 }
4455
4456 /* Make sure the correct pitch is used */
4457 glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4458
4459 if ((This->Flags & SFLAG_NONPOW2) && !(This->Flags & SFLAG_OVERSIZE)) {
4460 TRACE("non power of two support\n");
4461 if(!(This->Flags & SFLAG_ALLOCATED)) {
4462 surface_allocate_surface(This, internal, This->pow2Width, This->pow2Height, format, type);
4463 }
4464 if (mem || (This->Flags & SFLAG_PBO)) {
4465 surface_upload_data(This, internal, This->currentDesc.Width, This->currentDesc.Height, format, type, mem);
4466 }
4467 } else {
4468 /* When making the realloc conditional, keep in mind that GL_APPLE_client_storage may be in use, and This->resource.allocatedMemory
4469 * changed. So also keep track of memory changes. In this case the texture has to be reallocated
4470 */
4471 if(!(This->Flags & SFLAG_ALLOCATED)) {
4472 surface_allocate_surface(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type);
4473 }
4474 if (mem || (This->Flags & SFLAG_PBO)) {
4475 surface_upload_data(This, internal, This->glRect.right - This->glRect.left, This->glRect.bottom - This->glRect.top, format, type, mem);
4476 }
4477 }
4478
4479 /* Restore the default pitch */
4480 glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4481 LEAVE_GL();
4482
4483 /* Don't delete PBO memory */
4484 if((mem != This->resource.allocatedMemory) && !(This->Flags & SFLAG_PBO))
4485 HeapFree(GetProcessHeap(), 0, mem);
4486 }
4487 }
4488
4489 if(rect == NULL) {
4490 This->Flags |= flag;
4491 }
4492
4493 if (wined3d_settings.offscreen_rendering_mode == ORM_FBO && !swapchain
4494 && (This->Flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE))) {
4495 /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4496 This->Flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4497 }
4498
4499 return WINED3D_OK;
4500 }
4501
4502 HRESULT WINAPI IWineD3DSurfaceImpl_SetContainer(IWineD3DSurface *iface, IWineD3DBase *container) {
4503 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4504 IWineD3DSwapChain *swapchain = NULL;
4505
4506 /* Update the drawable size method */
4507 if(container) {
4508 IWineD3DBase_QueryInterface(container, &IID_IWineD3DSwapChain, (void **) &swapchain);
4509 }
4510 if(swapchain) {
4511 This->get_drawable_size = get_drawable_size_swapchain;
4512 IWineD3DSwapChain_Release(swapchain);
4513 } else if(This->resource.usage & WINED3DUSAGE_RENDERTARGET) {
4514 switch(wined3d_settings.offscreen_rendering_mode) {
4515 case ORM_FBO: This->get_drawable_size = get_drawable_size_fbo; break;
4516 case ORM_PBUFFER: This->get_drawable_size = get_drawable_size_pbuffer; break;
4517 case ORM_BACKBUFFER: This->get_drawable_size = get_drawable_size_backbuffer; break;
4518 }
4519 }
4520
4521 return IWineD3DBaseSurfaceImpl_SetContainer(iface, container);
4522 }
4523
4524 static WINED3DSURFTYPE WINAPI IWineD3DSurfaceImpl_GetImplType(IWineD3DSurface *iface) {
4525 return SURFACE_OPENGL;
4526 }
4527
4528 static HRESULT WINAPI IWineD3DSurfaceImpl_DrawOverlay(IWineD3DSurface *iface) {
4529 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4530 HRESULT hr;
4531
4532 /* If there's no destination surface there is nothing to do */
4533 if(!This->overlay_dest) return WINED3D_OK;
4534
4535 /* Blt calls ModifyLocation on the dest surface, which in turn calls DrawOverlay to
4536 * update the overlay. Prevent an endless recursion
4537 */
4538 if(This->overlay_dest->Flags & SFLAG_INOVERLAYDRAW) {
4539 return WINED3D_OK;
4540 }
4541 This->overlay_dest->Flags |= SFLAG_INOVERLAYDRAW;
4542 hr = IWineD3DSurfaceImpl_Blt((IWineD3DSurface *) This->overlay_dest, &This->overlay_destrect,
4543 iface, &This->overlay_srcrect, WINEDDBLT_WAIT,
4544 NULL, WINED3DTEXF_LINEAR);
4545 This->overlay_dest->Flags &= ~SFLAG_INOVERLAYDRAW;
4546
4547 return hr;
4548 }
4549
4550 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4551 {
4552 /* IUnknown */
4553 IWineD3DBaseSurfaceImpl_QueryInterface,
4554 IWineD3DBaseSurfaceImpl_AddRef,
4555 IWineD3DSurfaceImpl_Release,
4556 /* IWineD3DResource */
4557 IWineD3DBaseSurfaceImpl_GetParent,
4558 IWineD3DBaseSurfaceImpl_GetDevice,
4559 IWineD3DBaseSurfaceImpl_SetPrivateData,
4560 IWineD3DBaseSurfaceImpl_GetPrivateData,
4561 IWineD3DBaseSurfaceImpl_FreePrivateData,
4562 IWineD3DBaseSurfaceImpl_SetPriority,
4563 IWineD3DBaseSurfaceImpl_GetPriority,
4564 IWineD3DSurfaceImpl_PreLoad,
4565 IWineD3DSurfaceImpl_UnLoad,
4566 IWineD3DBaseSurfaceImpl_GetType,
4567 /* IWineD3DSurface */
4568 IWineD3DBaseSurfaceImpl_GetContainer,
4569 IWineD3DBaseSurfaceImpl_GetDesc,
4570 IWineD3DSurfaceImpl_LockRect,
4571 IWineD3DSurfaceImpl_UnlockRect,
4572 IWineD3DSurfaceImpl_GetDC,
4573 IWineD3DSurfaceImpl_ReleaseDC,
4574 IWineD3DSurfaceImpl_Flip,
4575 IWineD3DSurfaceImpl_Blt,
4576 IWineD3DBaseSurfaceImpl_GetBltStatus,
4577 IWineD3DBaseSurfaceImpl_GetFlipStatus,
4578 IWineD3DBaseSurfaceImpl_IsLost,
4579 IWineD3DBaseSurfaceImpl_Restore,
4580 IWineD3DSurfaceImpl_BltFast,
4581 IWineD3DBaseSurfaceImpl_GetPalette,
4582 IWineD3DBaseSurfaceImpl_SetPalette,
4583 IWineD3DSurfaceImpl_RealizePalette,
4584 IWineD3DBaseSurfaceImpl_SetColorKey,
4585 IWineD3DBaseSurfaceImpl_GetPitch,
4586 IWineD3DSurfaceImpl_SetMem,
4587 IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4588 IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4589 IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4590 IWineD3DBaseSurfaceImpl_UpdateOverlay,
4591 IWineD3DBaseSurfaceImpl_SetClipper,
4592 IWineD3DBaseSurfaceImpl_GetClipper,
4593 /* Internal use: */
4594 IWineD3DSurfaceImpl_AddDirtyRect,
4595 IWineD3DSurfaceImpl_LoadTexture,
4596 IWineD3DSurfaceImpl_BindTexture,
4597 IWineD3DSurfaceImpl_SaveSnapshot,
4598 IWineD3DSurfaceImpl_SetContainer,
4599 IWineD3DSurfaceImpl_SetGlTextureDesc,
4600 IWineD3DSurfaceImpl_GetGlDesc,
4601 IWineD3DSurfaceImpl_GetData,
4602 IWineD3DSurfaceImpl_SetFormat,
4603 IWineD3DSurfaceImpl_PrivateSetup,
4604 IWineD3DSurfaceImpl_ModifyLocation,
4605 IWineD3DSurfaceImpl_LoadLocation,
4606 IWineD3DSurfaceImpl_GetImplType,
4607 IWineD3DSurfaceImpl_DrawOverlay
4608 };
4609 #undef GLINFO_LOCATION
4610
4611 #define GLINFO_LOCATION device->adapter->gl_info
4612 static HRESULT ffp_blit_alloc(IWineD3DDevice *iface) { return WINED3D_OK; }
4613 static void ffp_blit_free(IWineD3DDevice *iface) { }
4614
4615 static HRESULT ffp_blit_set(IWineD3DDevice *iface, WINED3DFORMAT fmt, GLenum textype, UINT width, UINT height) {
4616 glEnable(textype);
4617 checkGLcall("glEnable(textype)");
4618 return WINED3D_OK;
4619 }
4620
4621 static void ffp_blit_unset(IWineD3DDevice *iface) {
4622 IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *) iface;
4623 glDisable(GL_TEXTURE_2D);
4624 checkGLcall("glDisable(GL_TEXTURE_2D)");
4625 if(GL_SUPPORT(ARB_TEXTURE_CUBE_MAP)) {
4626 glDisable(GL_TEXTURE_CUBE_MAP_ARB);
4627 checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
4628 }
4629 if(GL_SUPPORT(ARB_TEXTURE_RECTANGLE)) {
4630 glDisable(GL_TEXTURE_RECTANGLE_ARB);
4631 checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
4632 }
4633 }
4634
4635 static BOOL ffp_blit_conv_supported(WINED3DFORMAT fmt) {
4636 TRACE("Checking blit format support for format %s: [FAILED]\n", debug_d3dformat(fmt));
4637 return FALSE;
4638 }
4639
4640 const struct blit_shader ffp_blit = {
4641 ffp_blit_alloc,
4642 ffp_blit_free,
4643 ffp_blit_set,
4644 ffp_blit_unset,
4645 ffp_blit_conv_supported
4646 };