Implemented DllCanUnloadNow.
[wine/multimedia.git] / dlls / d3d8 / surface.c
blob5c7aab1a0fb7c6faa1be87d751e02390d8f102d7
1 /*
2 * IDirect3DSurface8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Christian Costa
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdio.h>
28 #define COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "wingdi.h"
36 #include "wine/debug.h"
38 #include "d3d8_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
42 /* IDirect3DVolume IUnknown parts follow: */
43 HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface,REFIID riid,LPVOID *ppobj)
45 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
47 if (IsEqualGUID(riid, &IID_IUnknown)
48 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
49 IDirect3DSurface8Impl_AddRef(iface);
50 *ppobj = This;
51 return D3D_OK;
54 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
55 return E_NOINTERFACE;
58 ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
59 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
60 TRACE("(%p) : AddRef from %ld\n", This, This->ref);
61 return ++(This->ref);
64 ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
65 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
66 ULONG ref = --This->ref;
67 TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
68 if (ref == 0) {
69 HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
70 HeapFree(GetProcessHeap(), 0, This);
72 return ref;
75 /* IDirect3DSurface8: */
76 HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8** ppDevice) {
77 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
78 TRACE("(%p) : returning %p\n", This, This->Device);
79 *ppDevice = (LPDIRECT3DDEVICE8) This->Device;
80 /**
81 * Note Calling this method will increase the internal reference count
82 * on the IDirect3DDevice8 interface.
84 IDirect3DDevice8Impl_AddRef(*ppDevice);
85 return D3D_OK;
88 HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
89 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
90 FIXME("(%p) : stub\n", This);
91 return D3D_OK;
94 HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
95 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
96 FIXME("(%p) : stub\n", This);
97 return D3D_OK;
100 HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
101 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
102 FIXME("(%p) : stub\n", This);
103 return D3D_OK;
106 HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void** ppContainer) {
107 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
108 HRESULT res;
109 res = IUnknown_QueryInterface(This->Container, riid, ppContainer);
110 if (E_NOINTERFACE == res) {
112 * If the surface is created using CreateImageSurface, CreateRenderTarget,
113 * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
114 * GetContainer will return the Direct3D device used to create the surface.
116 res = IUnknown_QueryInterface(This->Container, &IID_IDirect3DDevice8, ppContainer);
118 TRACE("(%p) : returning %p\n", This, *ppContainer);
119 return res;
122 HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
123 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
125 TRACE("(%p) : copying into %p\n", This, pDesc);
126 memcpy(pDesc, &This->myDesc, sizeof(D3DSURFACE_DESC));
127 return D3D_OK;
130 HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
131 HRESULT hr;
132 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
134 /* fixme: should we really lock as such? */
135 if (This->inTexture && This->inPBuffer) {
136 FIXME("Warning: Surface is in texture memory or pbuffer\n");
137 This->inTexture = 0;
138 This->inPBuffer = 0;
141 if (FALSE == This->lockable) {
142 /* Note: UpdateTextures calls CopyRects which calls this routine to populate the
143 texture regions, and since the destination is an unlockable region we need
144 to tolerate this */
145 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
146 /*return D3DERR_INVALIDCALL; */
149 if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer || This->Device->depthStencilBuffer) {
150 if (This == This->Device->backBuffer) {
151 TRACE("(%p, backBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
152 } else if (This == This->Device->frontBuffer) {
153 TRACE("(%p, frontBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
154 } else if (This == This->Device->renderTarget) {
155 TRACE("(%p, renderTarget) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
156 } else if (This == This->Device->depthStencilBuffer) {
157 TRACE("(%p, stencilBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
159 } else {
160 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
163 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
164 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt3/5)
165 ie pitch = (width/4) * bytes per block */
166 if (This->myDesc.Format == D3DFMT_DXT1) /* DXT1 is 8 bytes per block */
167 pLockedRect->Pitch = (This->myDesc.Width/4) * 8;
168 else if (This->myDesc.Format == D3DFMT_DXT3 || This->myDesc.Format == D3DFMT_DXT5) /* DXT3/5 is 16 bytes per block */
169 pLockedRect->Pitch = (This->myDesc.Width/4) * 16;
170 else
171 pLockedRect->Pitch = This->bytesPerPixel * This->myDesc.Width; /* Bytes / row */
173 if (NULL == pRect) {
174 pLockedRect->pBits = This->allocatedMemory;
175 This->lockedRect.left = 0;
176 This->lockedRect.top = 0;
177 This->lockedRect.right = This->myDesc.Width;
178 This->lockedRect.bottom = This->myDesc.Height;
179 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);
180 } else {
181 TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
183 if (This->myDesc.Format == D3DFMT_DXT1) { /* DXT1 is half byte per pixel */
184 pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + ((pRect->left * This->bytesPerPixel/2));
185 } else {
186 pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
188 This->lockedRect.left = pRect->left;
189 This->lockedRect.top = pRect->top;
190 This->lockedRect.right = pRect->right;
191 This->lockedRect.bottom = pRect->bottom;
195 if (0 == This->myDesc.Usage) { /* classic surface */
197 /* Nothing to do ;) */
199 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage && !(Flags&D3DLOCK_DISCARD)) { /* render surfaces */
201 if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer) {
202 GLint prev_store;
203 GLenum prev_read;
205 ENTER_GL();
208 * for render->surface copy begin to begin of allocatedMemory
209 * unlock can be more easy
211 pLockedRect->pBits = This->allocatedMemory;
213 glFlush();
214 vcheckGLcall("glFlush");
215 glGetIntegerv(GL_READ_BUFFER, &prev_read);
216 vcheckGLcall("glIntegerv");
217 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
218 vcheckGLcall("glIntegerv");
220 if (This == This->Device->backBuffer) {
221 glReadBuffer(GL_BACK);
222 } else if (This == This->Device->frontBuffer || This == This->Device->renderTarget) {
223 glReadBuffer(GL_FRONT);
224 } else if (This == This->Device->depthStencilBuffer) {
225 ERR("Stencil Buffer lock unsupported for now\n");
227 vcheckGLcall("glReadBuffer");
230 long j;
231 GLenum format = D3DFmt2GLFmt(This->Device, This->myDesc.Format);
232 GLenum type = D3DFmt2GLType(This->Device, This->myDesc.Format);
233 for (j = This->lockedRect.top; j < This->lockedRect.bottom - This->lockedRect.top; ++j) {
234 glReadPixels(This->lockedRect.left,
235 This->lockedRect.bottom - j - 1,
236 This->lockedRect.right - This->lockedRect.left,
238 format,
239 type,
240 (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
241 vcheckGLcall("glReadPixels");
245 glReadBuffer(prev_read);
246 vcheckGLcall("glReadBuffer");
248 LEAVE_GL();
250 } else {
251 FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
254 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
256 FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
258 } else {
259 FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
262 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
263 /* Don't dirtify */
264 } else {
266 * Dirtify on lock
267 * as seen in msdn docs
269 IDirect3DSurface8Impl_AddDirtyRect(iface, &This->lockedRect);
271 /** Dirtify Container if needed */
272 if (NULL != This->Container) {
273 IDirect3DBaseTexture8* cont = NULL;
274 hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
276 if (SUCCEEDED(hr) && NULL != cont) {
277 IDirect3DBaseTexture8Impl_SetDirty(cont, TRUE);
278 IDirect3DBaseTexture8_Release(cont);
279 cont = NULL;
284 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
286 This->locked = TRUE;
287 return D3D_OK;
290 HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
291 GLint skipBytes = 0;
292 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
294 if (FALSE == This->locked) {
295 ERR("trying to Unlock an unlocked surf@%p\n", This);
296 return D3DERR_INVALIDCALL;
299 if (This == This->Device->backBuffer || This == This->Device->frontBuffer || This->Device->depthStencilBuffer || This == This->Device->renderTarget) {
300 if (This == This->Device->backBuffer) {
301 TRACE("(%p, backBuffer) : dirtyfied(%d)\n", This, This->Dirty);
302 } else if (This == This->Device->frontBuffer) {
303 TRACE("(%p, frontBuffer) : dirtyfied(%d)\n", This, This->Dirty);
304 } else if (This == This->Device->depthStencilBuffer) {
305 TRACE("(%p, stencilBuffer) : dirtyfied(%d)\n", This, This->Dirty);
306 } else if (This == This->Device->renderTarget) {
307 TRACE("(%p, renderTarget) : dirtyfied(%d)\n", This, This->Dirty);
309 } else {
310 TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
313 if (FALSE == This->Dirty) {
314 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
315 goto unlock_end;
318 if (0 == This->myDesc.Usage) { /* classic surface */
320 * nothing to do
321 * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
323 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
325 if (This == This->Device->backBuffer || This == This->Device->frontBuffer || This == This->Device->renderTarget) {
326 GLint prev_store;
327 GLenum prev_draw;
328 GLint prev_rasterpos[4];
330 ENTER_GL();
332 glFlush();
333 vcheckGLcall("glFlush");
334 glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
335 vcheckGLcall("glIntegerv");
336 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
337 vcheckGLcall("glIntegerv");
338 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
339 vcheckGLcall("glIntegerv");
340 glPixelZoom(1.0, -1.0);
341 vcheckGLcall("glPixelZoom");
343 /* glDrawPixels transforms the raster position as though it was a vertex -
344 we want to draw at screen position 0,0 - Set up ortho (rhw) mode as
345 per drawprim (and leave set - it will sort itself out due to last_was_rhw */
346 if (!This->Device->last_was_rhw) {
348 double X, Y, height, width, minZ, maxZ;
349 This->Device->last_was_rhw = TRUE;
351 /* Transformed already into viewport coordinates, so we do not need transform
352 matrices. Reset all matrices to identity and leave the default matrix in world
353 mode. */
354 glMatrixMode(GL_MODELVIEW);
355 checkGLcall("glMatrixMode");
356 glLoadIdentity();
357 checkGLcall("glLoadIdentity");
359 glMatrixMode(GL_PROJECTION);
360 checkGLcall("glMatrixMode");
361 glLoadIdentity();
362 checkGLcall("glLoadIdentity");
364 /* Set up the viewport to be full viewport */
365 X = This->Device->StateBlock->viewport.X;
366 Y = This->Device->StateBlock->viewport.Y;
367 height = This->Device->StateBlock->viewport.Height;
368 width = This->Device->StateBlock->viewport.Width;
369 minZ = This->Device->StateBlock->viewport.MinZ;
370 maxZ = This->Device->StateBlock->viewport.MaxZ;
371 TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
372 glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
373 checkGLcall("glOrtho");
375 /* Window Coord 0 is the middle of the first pixel, so translate by half
376 a pixel (See comment above glTranslate below) */
377 glTranslatef(0.5, 0.5, 0);
378 checkGLcall("glTranslatef(0.5, 0.5, 0)");
381 if (This == This->Device->backBuffer) {
382 glDrawBuffer(GL_BACK);
383 } else if (This == This->Device->frontBuffer || This == This->Device->renderTarget) {
384 glDrawBuffer(GL_FRONT);
386 vcheckGLcall("glDrawBuffer");
388 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
389 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
390 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->myDesc.Width);
392 /* And back buffers are not blended */
393 glDisable(GL_BLEND);
395 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
396 vcheckGLcall("glRasterPos2f");
397 switch (This->myDesc.Format) {
398 case D3DFMT_R5G6B5:
400 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
401 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
402 vcheckGLcall("glDrawPixels");
404 break;
405 case D3DFMT_R8G8B8:
407 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
408 GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
409 vcheckGLcall("glDrawPixels");
411 break;
412 case D3DFMT_A8R8G8B8:
414 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
415 vcheckGLcall("glPixelStorei");
416 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
417 GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
418 vcheckGLcall("glDrawPixels");
419 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
420 vcheckGLcall("glPixelStorei");
422 break;
423 default:
424 FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
427 glPixelZoom(1.0,1.0);
428 vcheckGLcall("glPixelZoom");
429 glDrawBuffer(prev_draw);
430 vcheckGLcall("glDrawBuffer");
431 glRasterPos3iv(&prev_rasterpos[0]);
432 vcheckGLcall("glRasterPos3iv");
434 /* Reset to previous pack row length / blending state */
435 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
436 if (This->Device->StateBlock->renderstate[D3DRS_ALPHABLENDENABLE]) glEnable(GL_BLEND);
438 LEAVE_GL();
440 /** restore clean dirty state */
441 IDirect3DSurface8Impl_CleanDirtyRect(iface);
443 } else {
444 FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
447 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
449 if (This == This->Device->depthStencilBuffer) {
450 FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
451 } else {
452 FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
455 } else {
456 FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
459 unlock_end:
460 This->locked = FALSE;
461 memset(&This->lockedRect, 0, sizeof(RECT));
462 return D3D_OK;
466 IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
468 IDirect3DSurface8Impl_QueryInterface,
469 IDirect3DSurface8Impl_AddRef,
470 IDirect3DSurface8Impl_Release,
471 IDirect3DSurface8Impl_GetDevice,
472 IDirect3DSurface8Impl_SetPrivateData,
473 IDirect3DSurface8Impl_GetPrivateData,
474 IDirect3DSurface8Impl_FreePrivateData,
475 IDirect3DSurface8Impl_GetContainer,
476 IDirect3DSurface8Impl_GetDesc,
477 IDirect3DSurface8Impl_LockRect,
478 IDirect3DSurface8Impl_UnlockRect,
482 HRESULT WINAPI IDirect3DSurface8Impl_LoadTexture(LPDIRECT3DSURFACE8 iface, GLenum gl_target, GLenum gl_level) {
483 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
485 if (This->inTexture)
486 return D3D_OK;
487 if (This->inPBuffer) {
488 ENTER_GL();
489 if (gl_level != 0)
490 FIXME("Surface in texture is only supported for level 0\n");
491 else if (This->myDesc.Format == D3DFMT_P8 || This->myDesc.Format == D3DFMT_A8P8 ||
492 This->myDesc.Format == D3DFMT_DXT1 || This->myDesc.Format == D3DFMT_DXT3 ||
493 This->myDesc.Format == D3DFMT_DXT5)
494 FIXME("Format %d not supported\n", This->myDesc.Format);
495 else {
496 glCopyTexImage2D(gl_target,
498 D3DFmt2GLIntFmt(This->Device,
499 This->myDesc.Format),
501 0,/*This->surfaces[j][i]->myDesc.Height-1,*/
502 This->myDesc.Width,
503 This->myDesc.Height,
505 TRACE("Updating target %d\n", gl_target);
506 This->inTexture = TRUE;
508 LEAVE_GL();
509 return D3D_OK;
512 if ((This->myDesc.Format == D3DFMT_P8 || This->myDesc.Format == D3DFMT_A8P8) &&
513 !GL_SUPPORT_DEV(EXT_PALETTED_TEXTURE, This->Device)) {
515 * wanted a paletted texture and not really support it in HW
516 * so software emulation code begin
518 UINT i;
519 PALETTEENTRY* pal = This->Device->palettes[This->Device->currentPalette];
520 VOID* surface = (VOID*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->myDesc.Width * This->myDesc.Height * sizeof(DWORD));
521 BYTE* dst = (BYTE*) surface;
522 BYTE* src = (BYTE*) This->allocatedMemory;
524 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
525 BYTE color = *src++;
526 *dst++ = pal[color].peRed;
527 *dst++ = pal[color].peGreen;
528 *dst++ = pal[color].peBlue;
529 if (This->myDesc.Format == D3DFMT_A8P8)
530 *dst++ = pal[color].peFlags;
531 else
532 *dst++ = 0xFF;
535 ENTER_GL();
537 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
538 gl_target,
539 gl_level,
540 GL_RGBA,
541 This->myDesc.Width,
542 This->myDesc.Height,
544 GL_RGBA,
545 GL_UNSIGNED_BYTE,
546 surface);
547 glTexImage2D(gl_target,
548 gl_level,
549 GL_RGBA,
550 This->myDesc.Width,
551 This->myDesc.Height,
553 GL_RGBA,
554 GL_UNSIGNED_BYTE,
555 surface);
556 checkGLcall("glTexImage2D");
557 HeapFree(GetProcessHeap(), 0, surface);
559 LEAVE_GL();
561 return D3D_OK;
564 if (This->myDesc.Format == D3DFMT_DXT1 ||
565 This->myDesc.Format == D3DFMT_DXT3 ||
566 This->myDesc.Format == D3DFMT_DXT5) {
567 if (GL_SUPPORT_DEV(EXT_TEXTURE_COMPRESSION_S3TC, This->Device)) {
568 TRACE("Calling glCompressedTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, sz=%d, Mem=%p\n",
569 gl_target,
570 gl_level,
571 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
572 This->myDesc.Width,
573 This->myDesc.Height,
575 This->myDesc.Size,
576 This->allocatedMemory);
578 ENTER_GL();
580 GL_EXTCALL_DEV(glCompressedTexImage2DARB, This->Device)(gl_target,
581 gl_level,
582 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
583 This->myDesc.Width,
584 This->myDesc.Height,
586 This->myDesc.Size,
587 This->allocatedMemory);
588 checkGLcall("glCommpressedTexTexImage2D");
590 LEAVE_GL();
591 } else {
592 FIXME("Using DXT1/3/5 without advertized support\n");
594 } else {
596 TRACE("Calling glTexImage2D %x i=%d, d3dfmt=%s, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
597 gl_target,
598 gl_level,
599 debug_d3dformat(This->myDesc.Format),
600 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
601 This->myDesc.Width,
602 This->myDesc.Height,
604 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
605 D3DFmt2GLType(This->Device, This->myDesc.Format),
606 This->allocatedMemory);
608 ENTER_GL();
610 glTexImage2D(gl_target,
611 gl_level,
612 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
613 This->myDesc.Width,
614 This->myDesc.Height,
616 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
617 D3DFmt2GLType(This->Device, This->myDesc.Format),
618 This->allocatedMemory);
619 checkGLcall("glTexImage2D");
621 LEAVE_GL();
623 #if 0
625 static unsigned int gen = 0;
626 char buffer[4096];
627 ++gen;
628 if ((gen % 10) == 0) {
629 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, gl_target, gl_level, gen);
630 IDirect3DSurface8Impl_SaveSnapshot((LPDIRECT3DSURFACE8) This, buffer);
633 * debugging crash code
634 if (gen == 250) {
635 void** test = NULL;
636 *test = 0;
640 #endif
643 return D3D_OK;
646 #include <errno.h>
647 HRESULT WINAPI IDirect3DSurface8Impl_SaveSnapshot(LPDIRECT3DSURFACE8 iface, const char* filename) {
648 FILE* f = NULL;
649 ULONG i;
650 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
652 f = fopen(filename, "w+");
653 if (NULL == f) {
654 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
655 return D3DERR_INVALIDCALL;
658 TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->myDesc.Format));
660 fprintf(f, "P6\n%u %u\n255\n", This->myDesc.Width, This->myDesc.Height);
661 switch (This->myDesc.Format) {
662 case D3DFMT_X8R8G8B8:
663 case D3DFMT_A8R8G8B8:
665 DWORD color;
666 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
667 color = ((DWORD*) This->allocatedMemory)[i];
668 fputc((color >> 16) & 0xFF, f);
669 fputc((color >> 8) & 0xFF, f);
670 fputc((color >> 0) & 0xFF, f);
673 break;
674 case D3DFMT_R8G8B8:
676 BYTE* color;
677 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
678 color = ((BYTE*) This->allocatedMemory) + (3 * i);
679 fputc((color[0]) & 0xFF, f);
680 fputc((color[1]) & 0xFF, f);
681 fputc((color[2]) & 0xFF, f);
684 break;
685 case D3DFMT_A1R5G5B5:
687 WORD color;
688 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
689 color = ((WORD*) This->allocatedMemory)[i];
690 fputc(((color >> 10) & 0x1F) * 255 / 31, f);
691 fputc(((color >> 5) & 0x1F) * 255 / 31, f);
692 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
695 break;
696 case D3DFMT_A4R4G4B4:
698 WORD color;
699 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
700 color = ((WORD*) This->allocatedMemory)[i];
701 fputc(((color >> 8) & 0x0F) * 255 / 15, f);
702 fputc(((color >> 4) & 0x0F) * 255 / 15, f);
703 fputc(((color >> 0) & 0x0F) * 255 / 15, f);
706 break;
708 case D3DFMT_R5G6B5:
710 WORD color;
711 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
712 color = ((WORD*) This->allocatedMemory)[i];
713 fputc(((color >> 11) & 0x1F) * 255 / 31, f);
714 fputc(((color >> 5) & 0x3F) * 255 / 63, f);
715 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
718 break;
719 default:
720 FIXME("Unimplemented dump mode format(%u,%s)\n", This->myDesc.Format, debug_d3dformat(This->myDesc.Format));
722 fclose(f);
723 return D3D_OK;
726 HRESULT WINAPI IDirect3DSurface8Impl_CleanDirtyRect(LPDIRECT3DSURFACE8 iface) {
727 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
728 This->Dirty = FALSE;
729 This->dirtyRect.left = This->myDesc.Width;
730 This->dirtyRect.top = This->myDesc.Height;
731 This->dirtyRect.right = 0;
732 This->dirtyRect.bottom = 0;
733 return D3D_OK;
737 * Raphael:
738 * very stupid way to handle multiple dirty rects but it works :)
740 extern HRESULT WINAPI IDirect3DSurface8Impl_AddDirtyRect(LPDIRECT3DSURFACE8 iface, CONST RECT* pDirtyRect) {
741 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
742 This->Dirty = TRUE;
743 if (NULL != pDirtyRect) {
744 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
745 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
746 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
747 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
748 } else {
749 This->dirtyRect.left = 0;
750 This->dirtyRect.top = 0;
751 This->dirtyRect.right = This->myDesc.Width;
752 This->dirtyRect.bottom = This->myDesc.Height;
754 return D3D_OK;