Added checks for ICU libraries (based on a patch by Shachar Shemesh).
[wine] / dlls / d3d8 / surface.c
1 /*
2  * IDirect3DSurface8 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/debug.h"
28 #include <stdio.h>
29
30 #include "d3d8_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
33
34 /* IDirect3DVolume IUnknown parts follow: */
35 HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface,REFIID riid,LPVOID *ppobj)
36 {
37     ICOM_THIS(IDirect3DSurface8Impl,iface);
38
39     if (IsEqualGUID(riid, &IID_IUnknown)
40         || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
41         IDirect3DSurface8Impl_AddRef(iface);
42         *ppobj = This;
43         return D3D_OK;
44     }
45
46     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
47     return E_NOINTERFACE;
48 }
49
50 ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
51     ICOM_THIS(IDirect3DSurface8Impl,iface);
52     TRACE("(%p) : AddRef from %ld\n", This, This->ref);
53     return ++(This->ref);
54 }
55
56 ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
57     ICOM_THIS(IDirect3DSurface8Impl,iface);
58     ULONG ref = --This->ref;
59     TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
60     if (ref == 0) {
61       HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
62       HeapFree(GetProcessHeap(), 0, This);
63     }
64     return ref;
65 }
66
67 /* IDirect3DSurface8: */
68 HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8** ppDevice) {
69     ICOM_THIS(IDirect3DSurface8Impl,iface);
70     TRACE("(%p) : returning %p\n", This, This->Device);
71     *ppDevice = (LPDIRECT3DDEVICE8) This->Device;
72     /**
73      * Note  Calling this method will increase the internal reference count 
74      * on the IDirect3DDevice8 interface. 
75      */
76     IDirect3DDevice8Impl_AddRef(*ppDevice);
77     return D3D_OK;
78 }
79 HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
80     ICOM_THIS(IDirect3DSurface8Impl,iface);
81     FIXME("(%p) : stub\n", This);    
82     return D3D_OK;
83 }
84 HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
85     ICOM_THIS(IDirect3DSurface8Impl,iface);
86     FIXME("(%p) : stub\n", This);    
87     return D3D_OK;
88 }
89 HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
90     ICOM_THIS(IDirect3DSurface8Impl,iface);
91     FIXME("(%p) : stub\n", This);    
92     return D3D_OK;
93 }
94 HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void** ppContainer) {
95     ICOM_THIS(IDirect3DSurface8Impl,iface);
96     HRESULT res;
97     res = IUnknown_QueryInterface(This->Container, riid, ppContainer);
98     if (E_NOINTERFACE == res) { 
99       /**
100        * If the surface is created using CreateImageSurface, CreateRenderTarget, 
101        * or CreateDepthStencilSurface, the surface is considered stand alone. In this case, 
102        * GetContainer will return the Direct3D device used to create the surface. 
103        */
104       res = IUnknown_QueryInterface(This->Container, &IID_IDirect3DDevice8, ppContainer);
105     }
106     TRACE("(%p) : returning %p\n", This, *ppContainer);
107     return res;
108 }
109 HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
110     ICOM_THIS(IDirect3DSurface8Impl,iface);
111
112     TRACE("(%p) : copying into %p\n", This, pDesc);
113     memcpy(pDesc, &This->myDesc, sizeof(D3DSURFACE_DESC));
114     return D3D_OK;
115 }
116 HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
117     HRESULT hr;
118     ICOM_THIS(IDirect3DSurface8Impl,iface);
119   
120     /* fixme: should we really lock as such? */
121
122     if (FALSE == This->lockable) {
123       ERR("trying to lock unlockable surf@%p\n", This);  
124       return D3DERR_INVALIDCALL;
125     }
126
127     if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer || This->Device->depthStencilBuffer) {
128       if (This == This->Device->backBuffer) {
129         TRACE("(%p, backBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
130       } else if (This == This->Device->frontBuffer) {
131         TRACE("(%p, frontBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
132       } else if (This == This->Device->renderTarget) {
133         TRACE("(%p, renderTarget) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
134       } else if (This == This->Device->depthStencilBuffer) {
135         TRACE("(%p, stencilBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
136       }
137     } else {
138       TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
139     }
140
141     pLockedRect->Pitch = This->bytesPerPixel * This->myDesc.Width;  /* Bytes / row */    
142     
143     if (NULL == pRect) {
144       pLockedRect->pBits = This->allocatedMemory;
145       This->lockedRect.left   = 0;
146       This->lockedRect.top    = 0;
147       This->lockedRect.right  = This->myDesc.Width;
148       This->lockedRect.bottom = This->myDesc.Height;
149       TRACE("Locked Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", &This->lockedRect, This->lockedRect.left, This->lockedRect.top, This->lockedRect.right, This->lockedRect.bottom);
150     } else {
151       TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
152       pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
153       This->lockedRect.left   = pRect->left;
154       This->lockedRect.top    = pRect->top;
155       This->lockedRect.right  = pRect->right;
156       This->lockedRect.bottom = pRect->bottom;
157     }
158
159
160     if (0 == This->myDesc.Usage) { /* classic surface */
161
162       /* Nothing to do ;) */
163
164     } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
165       
166       if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer) {
167         GLint  prev_store;
168         GLenum prev_read;
169         
170         ENTER_GL();
171
172         /**
173          * for render->surface copy begin to begin of allocatedMemory
174          * unlock can be more easy
175          */
176         pLockedRect->pBits = This->allocatedMemory;
177         
178         glFlush();
179         vcheckGLcall("glFlush");
180         glGetIntegerv(GL_READ_BUFFER, &prev_read);
181         vcheckGLcall("glIntegerv");
182         glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
183         vcheckGLcall("glIntegerv");
184
185         if (This == This->Device->backBuffer) {
186           glReadBuffer(GL_BACK);
187         } else if (This == This->Device->frontBuffer || This == This->Device->renderTarget) {
188           glReadBuffer(GL_FRONT);
189         } else if (This == This->Device->depthStencilBuffer) {
190           ERR("Stencil Buffer lock unsupported for now\n");
191         }
192         vcheckGLcall("glReadBuffer");
193
194         {
195           long j;
196           GLenum format = D3DFmt2GLFmt(This->Device, This->myDesc.Format);
197           GLenum type   = D3DFmt2GLType(This->Device, This->myDesc.Format);
198           for (j = This->lockedRect.top; j < This->lockedRect.bottom - This->lockedRect.top; ++j) {
199             glReadPixels(This->lockedRect.left, 
200                          This->lockedRect.bottom - j - 1, 
201                          This->lockedRect.right - This->lockedRect.left, 
202                          1,
203                          format, 
204                          type, 
205                          (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
206             vcheckGLcall("glReadPixels");
207           }
208         }
209
210         glReadBuffer(prev_read);
211         vcheckGLcall("glReadBuffer");
212
213         LEAVE_GL();
214
215       } else {
216         FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
217       }
218
219     } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
220
221       FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
222
223     } else {
224       FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
225     }
226
227     if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
228       /* Dont dirtify */
229     } else {
230       /**
231        * Dirtify on lock
232        * as seen in msdn docs
233        */
234       IDirect3DSurface8Impl_AddDirtyRect(iface, &This->lockedRect);
235
236       /** Dirtify Container if needed */
237       if (NULL != This->Container) {
238         IDirect3DBaseTexture8* cont = NULL;
239         hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
240         
241         if (SUCCEEDED(hr) && NULL != cont) {
242           IDirect3DBaseTexture8Impl_SetDirty(cont, TRUE);
243           IDirect3DBaseTexture8_Release(cont);
244           cont = NULL;
245         }
246       }
247     }
248
249     TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
250
251     This->locked = TRUE;
252     return D3D_OK;
253 }
254 HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
255     ICOM_THIS(IDirect3DSurface8Impl,iface);
256     
257     if (FALSE == This->locked) {
258       ERR("trying to lock unlocked surf@%p\n", This);  
259       return D3DERR_INVALIDCALL;
260     }
261
262     if (This == This->Device->backBuffer || This == This->Device->frontBuffer || This->Device->depthStencilBuffer) {
263       if (This == This->Device->backBuffer) {
264         TRACE("(%p, backBuffer) : dirtyfied(%d)\n", This, This->Dirty);
265       } else if (This == This->Device->frontBuffer) {
266         TRACE("(%p, frontBuffer) : dirtyfied(%d)\n", This, This->Dirty);
267       } else if (This == This->Device->depthStencilBuffer) {
268         TRACE("(%p, stencilBuffer) : dirtyfied(%d)\n", This, This->Dirty);
269       }
270     } else {
271       TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
272     }
273     /*TRACE("(%p) see if behavior is correct\n", This);*/
274
275     if (FALSE == This->Dirty) {
276       TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
277       goto unlock_end;
278     }
279
280     if (0 == This->myDesc.Usage) { /* classic surface */
281       /**
282        * nothing to do
283        * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
284        */
285     } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
286
287       if (This == This->Device->backBuffer || This == This->Device->frontBuffer) {
288         GLint  prev_store;
289         GLenum prev_draw;
290         GLint  prev_rasterpos[4];
291
292         ENTER_GL();
293         
294         glFlush();
295         vcheckGLcall("glFlush");
296         glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
297         vcheckGLcall("glIntegerv");
298         glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
299         vcheckGLcall("glIntegerv");
300         glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
301         vcheckGLcall("glIntegerv");
302
303         if (This == This->Device->backBuffer) {
304           glDrawBuffer(GL_BACK);
305         } else if (This == This->Device->frontBuffer) {
306           glDrawBuffer(GL_FRONT);
307         }
308         vcheckGLcall("glDrawBuffer");
309
310         glRasterPos2i(This->lockedRect.left, This->lockedRect.top);
311         vcheckGLcall("glRasterPos2f");
312         switch (This->myDesc.Format) {
313         case D3DFMT_R5G6B5:
314           {
315             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
316                          GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
317             vcheckGLcall("glDrawPixels");
318           }
319           break;
320         case D3DFMT_R8G8B8:
321           {
322             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
323                          GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
324             vcheckGLcall("glDrawPixels");
325           }
326           break;
327         case D3DFMT_A8R8G8B8:
328           {
329             glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
330             vcheckGLcall("glPixelStorei");
331             glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
332                          GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
333             vcheckGLcall("glDrawPixels");
334             glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
335             vcheckGLcall("glPixelStorei");
336           }
337           break;
338         default:
339           FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
340         }
341
342         glDrawBuffer(prev_draw);
343         vcheckGLcall("glDrawBuffer");
344         glRasterPos3iv(&prev_rasterpos[0]);
345         vcheckGLcall("glRasterPos3iv");
346
347         LEAVE_GL();
348
349         /** restore clean dirty state */
350         IDirect3DSurface8Impl_CleanDirtyRect(iface);
351
352       } else {
353         FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
354       }
355
356     } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
357     
358       if (This == This->Device->depthStencilBuffer) {
359         FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
360       } else {
361         FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
362       }
363
364     } else {
365       FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
366     }
367
368 unlock_end:
369     This->locked = FALSE;
370     memset(&This->lockedRect, 0, sizeof(RECT));
371     return D3D_OK;
372 }
373
374 ICOM_VTABLE(IDirect3DSurface8) Direct3DSurface8_Vtbl =
375 {
376     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
377     IDirect3DSurface8Impl_QueryInterface,
378     IDirect3DSurface8Impl_AddRef,
379     IDirect3DSurface8Impl_Release,
380     IDirect3DSurface8Impl_GetDevice,
381     IDirect3DSurface8Impl_SetPrivateData,
382     IDirect3DSurface8Impl_GetPrivateData,
383     IDirect3DSurface8Impl_FreePrivateData,
384     IDirect3DSurface8Impl_GetContainer,
385     IDirect3DSurface8Impl_GetDesc,
386     IDirect3DSurface8Impl_LockRect,
387     IDirect3DSurface8Impl_UnlockRect,
388 };
389
390 HRESULT WINAPI IDirect3DSurface8Impl_LoadTexture(LPDIRECT3DSURFACE8 iface, GLenum gl_target, GLenum gl_level) {
391   ICOM_THIS(IDirect3DSurface8Impl,iface);
392
393   if ((This->myDesc.Format == D3DFMT_P8 || This->myDesc.Format == D3DFMT_A8P8) 
394 #if defined(GL_EXT_paletted_texture)
395       && !GL_SUPPORT_DEV(EXT_PALETTED_TEXTURE, This->Device)
396 #endif
397       ) {
398     /**
399      * wanted a paletted texture and not really support it in HW 
400      * so software emulation code begin
401      */
402     UINT i;
403     PALETTEENTRY* pal = This->Device->palettes[This->Device->currentPalette];
404     VOID* surface = (VOID*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->myDesc.Width * This->myDesc.Height * sizeof(DWORD));
405     BYTE* dst = (BYTE*) surface;
406     BYTE* src = (BYTE*) This->allocatedMemory;
407           
408     for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
409       BYTE color = *src++;
410       *dst++ = pal[color].peRed;
411       *dst++ = pal[color].peGreen;
412       *dst++ = pal[color].peBlue;
413       if (This->myDesc.Format == D3DFMT_A8P8)
414         *dst++ = pal[color].peFlags; 
415       else
416         *dst++ = 0xFF; 
417     }
418
419     ENTER_GL();
420     
421     TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
422           gl_target,
423           gl_level, 
424           GL_RGBA,
425           This->myDesc.Width, 
426           This->myDesc.Height, 
427           0, 
428           GL_RGBA,
429           GL_UNSIGNED_BYTE,
430           surface);
431     glTexImage2D(gl_target,
432                  gl_level, 
433                  GL_RGBA,
434                  This->myDesc.Width,
435                  This->myDesc.Height,
436                  0,
437                  GL_RGBA,
438                  GL_UNSIGNED_BYTE,
439                  surface);
440     checkGLcall("glTexImage2D");
441     HeapFree(GetProcessHeap(), 0, surface);
442
443     LEAVE_GL();
444
445     return D3D_OK;    
446   }
447
448   if (This->myDesc.Format == D3DFMT_DXT1 || 
449       This->myDesc.Format == D3DFMT_DXT3 || 
450       This->myDesc.Format == D3DFMT_DXT5) {
451 #if defined(GL_EXT_texture_compression_s3tc)
452     if (GL_SUPPORT_DEV(EXT_TEXTURE_COMPRESSION_S3TC, This->Device)) {
453       TRACE("Calling glCompressedTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, sz=%d, Mem=%p\n",
454             gl_target, 
455             gl_level, 
456             D3DFmt2GLIntFmt(This->Device, This->myDesc.Format), 
457             This->myDesc.Width, 
458             This->myDesc.Height, 
459             0, 
460             This->myDesc.Size,
461             This->allocatedMemory);
462       
463       ENTER_GL();
464
465       glCompressedTexImage2DARB(gl_target, 
466                                 gl_level, 
467                                 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
468                                 This->myDesc.Width,
469                                 This->myDesc.Height,
470                                 0,
471                                 This->myDesc.Size,
472                                 This->allocatedMemory);
473       checkGLcall("glCommpressedTexTexImage2D");
474
475       LEAVE_GL();
476     }
477 #endif
478   } else {
479     TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
480           gl_target, 
481           gl_level, 
482           D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
483           This->myDesc.Width, 
484           This->myDesc.Height, 
485           0, 
486           D3DFmt2GLFmt(This->Device, This->myDesc.Format), 
487           D3DFmt2GLType(This->Device, This->myDesc.Format),
488           This->allocatedMemory);
489
490     ENTER_GL();
491
492     glTexImage2D(gl_target, 
493                  gl_level,
494                  D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
495                  This->myDesc.Width,
496                  This->myDesc.Height,
497                  0,
498                  D3DFmt2GLFmt(This->Device, This->myDesc.Format),
499                  D3DFmt2GLType(This->Device, This->myDesc.Format),
500                  This->allocatedMemory);
501     checkGLcall("glTexImage2D");
502
503     LEAVE_GL();
504
505 #if 0
506     {
507       static unsigned int gen = 0;
508       char buffer[4096];
509       ++gen;
510       if ((gen % 10) == 0) {
511         snprintf(buffer, sizeof(buffer), "/tmp/surface%u_level%u_%u.ppm", gl_target, gl_level, gen);
512         IDirect3DSurface8Impl_SaveSnapshot((LPDIRECT3DSURFACE8) This, buffer);
513       }
514     }
515 #endif
516   }
517
518   return D3D_OK;
519 }
520
521 #include <errno.h>
522 HRESULT WINAPI IDirect3DSurface8Impl_SaveSnapshot(LPDIRECT3DSURFACE8 iface, const char* filename) {
523   FILE* f = NULL;
524   ULONG i;
525   ICOM_THIS(IDirect3DSurface8Impl,iface);
526
527   f = fopen(filename, "w+");
528   if (NULL == f) {
529     ERR("opening of %s failed with: %s\n", filename, strerror(errno));
530     return D3DERR_INVALIDCALL;
531   }
532
533   TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->myDesc.Format));
534
535   fprintf(f, "P6\n%u %u\n255\n", This->myDesc.Width, This->myDesc.Height);
536   switch (This->myDesc.Format) {
537   case D3DFMT_X8R8G8B8:
538   case D3DFMT_A8R8G8B8:
539     {
540       DWORD color;
541       for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
542         color = ((DWORD*) This->allocatedMemory)[i];
543         fputc((color >> 16) & 0xFF, f);
544         fputc((color >>  8) & 0xFF, f);
545         fputc((color >>  0) & 0xFF, f);
546       }
547     }
548     break;
549   case D3DFMT_R8G8B8:
550     {
551       BYTE* color;
552       for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
553         color = ((BYTE*) This->allocatedMemory) + (3 * i);
554         fputc((color[0]) & 0xFF, f);
555         fputc((color[1]) & 0xFF, f);
556         fputc((color[2]) & 0xFF, f);
557       }
558     }
559     break;
560   case D3DFMT_A1R5G5B5: 
561     {
562       WORD color;
563       for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
564         color = ((WORD*) This->allocatedMemory)[i];
565         fputc(((color >> 10) & 0x1F) * 255 / 31, f);
566         fputc(((color >>  5) & 0x1F) * 255 / 31, f);
567         fputc(((color >>  0) & 0x1F) * 255 / 31, f);
568       }
569     }
570     break;
571   case D3DFMT_A4R4G4B4:
572     {
573       WORD color;
574       for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
575         color = ((WORD*) This->allocatedMemory)[i];
576         fputc(((color >>  8) & 0x0F) * 255 / 15, f);
577         fputc(((color >>  4) & 0x0F) * 255 / 15, f);
578         fputc(((color >>  0) & 0x0F) * 255 / 15, f);
579       }
580     }
581     break;
582
583   case D3DFMT_R5G6B5: 
584     {
585       WORD color;
586       for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
587         color = ((WORD*) This->allocatedMemory)[i];
588         fputc(((color >> 11) & 0x1F) * 255 / 31, f);
589         fputc(((color >>  5) & 0x3F) * 255 / 63, f);
590         fputc(((color >>  0) & 0x1F) * 255 / 31, f);
591       }
592     }
593     break;
594   default: 
595     FIXME("Unimplemented dump mode format(%u,%s)\n", This->myDesc.Format, debug_d3dformat(This->myDesc.Format));
596   }
597   fclose(f);
598   return D3D_OK;
599 }
600
601 HRESULT WINAPI IDirect3DSurface8Impl_CleanDirtyRect(LPDIRECT3DSURFACE8 iface) {
602   ICOM_THIS(IDirect3DSurface8Impl,iface);
603   This->Dirty = FALSE;
604   This->dirtyRect.left   = This->myDesc.Width;
605   This->dirtyRect.top    = This->myDesc.Height;
606   This->dirtyRect.right  = 0;
607   This->dirtyRect.bottom = 0;
608   return D3D_OK;
609 }
610
611 /**
612  * Raphael:
613  *   very stupid way to handle multiple dirty rects but it works :)
614  */
615 extern HRESULT WINAPI IDirect3DSurface8Impl_AddDirtyRect(LPDIRECT3DSURFACE8 iface, CONST RECT* pDirtyRect) {
616   ICOM_THIS(IDirect3DSurface8Impl,iface);
617   This->Dirty = TRUE;
618   if (NULL != pDirtyRect) {
619     This->dirtyRect.left   = min(This->dirtyRect.left,   pDirtyRect->left);
620     This->dirtyRect.top    = min(This->dirtyRect.top,    pDirtyRect->top);
621     This->dirtyRect.right  = max(This->dirtyRect.right,  pDirtyRect->right);
622     This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
623   } else {
624     This->dirtyRect.left   = 0;
625     This->dirtyRect.top    = 0;
626     This->dirtyRect.right  = This->myDesc.Width;
627     This->dirtyRect.bottom = This->myDesc.Height;
628   }
629   return D3D_OK;
630 }