2 * IWineD3DSurface Implementation
4 * Copyright 2002-2005 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
24 #include "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION ((IWineD3DImpl *)(((IWineD3DDeviceImpl *)This->resource.wineD3DDevice)->wineD3D))->gl_info
29 /* *******************************************
30 IWineD3DSurface IUnknown parts follow
31 ******************************************* */
32 HRESULT WINAPI IWineD3DSurfaceImpl_QueryInterface(IWineD3DSurface *iface, REFIID riid, LPVOID *ppobj)
34 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
35 WARN("(%p)->(%s,%p) should not be called\n",This,debugstr_guid(riid),ppobj);
39 ULONG WINAPI IWineD3DSurfaceImpl_AddRef(IWineD3DSurface *iface) {
40 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
41 ULONG ref = InterlockedIncrement(&This->resource.ref);
42 TRACE("(%p) : AddRef increasing from %ld\n", This,ref - 1);
43 IUnknown_AddRef(This->resource.parent);
47 ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface) {
48 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
49 ULONG ref = InterlockedDecrement(&This->resource.ref);
50 TRACE("(%p) : Releasing from %ld\n", This, ref + 1);
52 HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
53 IWineD3DDevice_Release((IWineD3DDevice *)This->resource.wineD3DDevice);
54 HeapFree(GetProcessHeap(), 0, This);
56 IUnknown_Release(This->resource.parent); /* Released the reference to the d3dx object */
61 /* ****************************************************
62 IWineD3DSurface IWineD3DResource parts follow
63 **************************************************** */
64 HRESULT WINAPI IWineD3DSurfaceImpl_GetDevice(IWineD3DSurface *iface, IWineD3DDevice** ppDevice) {
65 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
68 HRESULT WINAPI IWineD3DSurfaceImpl_SetPrivateData(IWineD3DSurface *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
69 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
72 HRESULT WINAPI IWineD3DSurfaceImpl_GetPrivateData(IWineD3DSurface *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
73 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
76 HRESULT WINAPI IWineD3DSurfaceImpl_FreePrivateData(IWineD3DSurface *iface, REFGUID refguid) {
77 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
80 DWORD WINAPI IWineD3DSurfaceImpl_SetPriority(IWineD3DSurface *iface, DWORD PriorityNew) {
81 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
84 DWORD WINAPI IWineD3DSurfaceImpl_GetPriority(IWineD3DSurface *iface) {
85 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
88 void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface) {
89 return IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
92 D3DRESOURCETYPE WINAPI IWineD3DSurfaceImpl_GetType(IWineD3DSurface *iface) {
93 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
96 HRESULT WINAPI IWineD3DSurfaceImpl_GetParent(IWineD3DSurface *iface, IUnknown **pParent) {
97 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
100 /* ******************************************************
101 IWineD3DSurface IWineD3DSurface parts follow
102 ****************************************************** */
104 HRESULT WINAPI IWineD3DSurfaceImpl_GetContainer(IWineD3DSurface *iface, REFIID riid, void** ppContainer) {
105 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
108 * If the surface is created using CreateImageSurface/CreateOffscreenPlainSurface, CreateRenderTarget,
109 * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
110 * GetContainer will return the Direct3D device used to create the surface.
112 WARN("Query of container implementation currently ignores riid\n");
113 *ppContainer = This->container;
114 IUnknown_AddRef((IUnknown *)*ppContainer);
115 TRACE("(%p) : returning %p\n", This, *ppContainer);
119 HRESULT WINAPI IWineD3DSurfaceImpl_GetDesc(IWineD3DSurface *iface, WINED3DSURFACE_DESC *pDesc) {
120 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
122 TRACE("(%p) : copying into %p\n", This, pDesc);
123 *(pDesc->Format) = This->currentDesc.Format;
124 *(pDesc->Type) = This->currentDesc.Type;
125 *(pDesc->Usage) = This->currentDesc.Usage;
126 *(pDesc->Pool) = This->currentDesc.Pool;
127 *(pDesc->Size) = This->currentDesc_size; /* dx8 only */
128 *(pDesc->MultiSampleType) = This->currentDesc.MultiSampleType;
129 *(pDesc->MultiSampleQuality) = This->currentDesc.MultiSampleQuality;
130 *(pDesc->Width) = This->currentDesc.Width;
131 *(pDesc->Height) = This->currentDesc.Height;
135 HRESULT WINAPI IWineD3DSurfaceImpl_LockRect(IWineD3DSurface *iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
136 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
138 /* fixme: should we really lock as such? */
139 if (This->inTexture && This->inPBuffer) {
140 FIXME("Warning: Surface is in texture memory or pbuffer\n");
145 if (FALSE == This->lockable) {
146 /* Note: UpdateTextures calls CopyRects which calls this routine to populate the
147 texture regions, and since the destination is an unlockable region we need
149 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
150 /*return D3DERR_INVALIDCALL; */
153 if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->renderTarget || This == This->resource.wineD3DDevice->frontBuffer || This->resource.wineD3DDevice->depthStencilBuffer) {
154 if (This == This->resource.wineD3DDevice->backBuffer) {
155 TRACE("(%p, backBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
156 } else if (This == This->resource.wineD3DDevice->frontBuffer) {
157 TRACE("(%p, frontBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
158 } else if (This == This->resource.wineD3DDevice->renderTarget) {
159 TRACE("(%p, renderTarget) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
160 } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
161 TRACE("(%p, stencilBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
164 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
167 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
168 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt3/5)
169 ie pitch = (width/4) * bytes per block */
170 if (This->currentDesc.Format == D3DFMT_DXT1) /* DXT1 is 8 bytes per block */
171 pLockedRect->Pitch = (This->currentDesc.Width/4) * 8;
172 else if (This->currentDesc.Format == D3DFMT_DXT3 || This->currentDesc.Format == D3DFMT_DXT5) /* DXT3/5 is 16 bytes per block */
173 pLockedRect->Pitch = (This->currentDesc.Width/4) * 16;
175 pLockedRect->Pitch = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
178 pLockedRect->pBits = This->allocatedMemory;
179 This->lockedRect.left = 0;
180 This->lockedRect.top = 0;
181 This->lockedRect.right = This->currentDesc.Width;
182 This->lockedRect.bottom = This->currentDesc.Height;
183 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);
185 TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
187 if (This->currentDesc.Format == D3DFMT_DXT1) { /* DXT1 is half byte per pixel */
188 pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + ((pRect->left * This->bytesPerPixel/2));
190 pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
192 This->lockedRect.left = pRect->left;
193 This->lockedRect.top = pRect->top;
194 This->lockedRect.right = pRect->right;
195 This->lockedRect.bottom = pRect->bottom;
199 if (0 == This->currentDesc.Usage) { /* classic surface */
201 /* Nothing to do ;) */
203 } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage && !(Flags&D3DLOCK_DISCARD)) { /* render surfaces */
205 if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->renderTarget || This == This->resource.wineD3DDevice->frontBuffer) {
212 * for render->surface copy begin to begin of allocatedMemory
213 * unlock can be more easy
215 pLockedRect->pBits = This->allocatedMemory;
218 vcheckGLcall("glFlush");
219 glGetIntegerv(GL_READ_BUFFER, &prev_read);
220 vcheckGLcall("glIntegerv");
221 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
222 vcheckGLcall("glIntegerv");
224 if (This == This->resource.wineD3DDevice->backBuffer) {
225 glReadBuffer(GL_BACK);
226 } else if (This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
227 glReadBuffer(GL_FRONT);
228 } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
229 ERR("Stencil Buffer lock unsupported for now\n");
231 vcheckGLcall("glReadBuffer");
235 GLenum format = D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format);
236 GLenum type = D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format);
237 for (j = This->lockedRect.top; j < This->lockedRect.bottom - This->lockedRect.top; ++j) {
238 glReadPixels(This->lockedRect.left,
239 This->lockedRect.bottom - j - 1,
240 This->lockedRect.right - This->lockedRect.left,
244 (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
245 vcheckGLcall("glReadPixels");
249 glReadBuffer(prev_read);
250 vcheckGLcall("glReadBuffer");
255 FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
258 } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
260 FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
263 FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
266 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
271 * as seen in msdn docs
273 IWineD3DSurface_AddDirtyRect(iface, &This->lockedRect);
275 /** Dirtify Container if needed */
276 if ((NULL != This->container) && ((IWineD3DDeviceImpl *)This->container != This->resource.wineD3DDevice)) {
277 IWineD3DBaseTexture_SetDirty((IWineD3DBaseTexture *)This->container, TRUE);
281 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
287 HRESULT WINAPI IWineD3DSurfaceImpl_UnlockRect(IWineD3DSurface *iface) {
289 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
291 if (FALSE == This->locked) {
292 ERR("trying to Unlock an unlocked surf@%p\n", This);
293 return D3DERR_INVALIDCALL;
296 if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->frontBuffer || This->resource.wineD3DDevice->depthStencilBuffer || This == This->resource.wineD3DDevice->renderTarget) {
297 if (This == This->resource.wineD3DDevice->backBuffer) {
298 TRACE("(%p, backBuffer) : dirtyfied(%d)\n", This, This->Dirty);
299 } else if (This == This->resource.wineD3DDevice->frontBuffer) {
300 TRACE("(%p, frontBuffer) : dirtyfied(%d)\n", This, This->Dirty);
301 } else if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
302 TRACE("(%p, stencilBuffer) : dirtyfied(%d)\n", This, This->Dirty);
303 } else if (This == This->resource.wineD3DDevice->renderTarget) {
304 TRACE("(%p, renderTarget) : dirtyfied(%d)\n", This, This->Dirty);
307 TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
310 if (FALSE == This->Dirty) {
311 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
315 if (0 == This->currentDesc.Usage) { /* classic surface */
318 * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
320 } else if (D3DUSAGE_RENDERTARGET & This->currentDesc.Usage) { /* render surfaces */
322 if (This == This->resource.wineD3DDevice->backBuffer || This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
325 GLint prev_rasterpos[4];
330 vcheckGLcall("glFlush");
331 glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
332 vcheckGLcall("glIntegerv");
333 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
334 vcheckGLcall("glIntegerv");
335 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
336 vcheckGLcall("glIntegerv");
337 glPixelZoom(1.0, -1.0);
338 vcheckGLcall("glPixelZoom");
340 /* glDrawPixels transforms the raster position as though it was a vertex -
341 we want to draw at screen position 0,0 - Set up ortho (rhw) mode as
342 per drawprim (and leave set - it will sort itself out due to last_was_rhw */
343 if (!This->resource.wineD3DDevice->last_was_rhw) {
345 double X, Y, height, width, minZ, maxZ;
346 This->resource.wineD3DDevice->last_was_rhw = TRUE;
348 /* Transformed already into viewport coordinates, so we do not need transform
349 matrices. Reset all matrices to identity and leave the default matrix in world
351 glMatrixMode(GL_MODELVIEW);
352 checkGLcall("glMatrixMode");
354 checkGLcall("glLoadIdentity");
356 glMatrixMode(GL_PROJECTION);
357 checkGLcall("glMatrixMode");
359 checkGLcall("glLoadIdentity");
361 /* Set up the viewport to be full viewport */
362 X = This->resource.wineD3DDevice->stateBlock->viewport.X;
363 Y = This->resource.wineD3DDevice->stateBlock->viewport.Y;
364 height = This->resource.wineD3DDevice->stateBlock->viewport.Height;
365 width = This->resource.wineD3DDevice->stateBlock->viewport.Width;
366 minZ = This->resource.wineD3DDevice->stateBlock->viewport.MinZ;
367 maxZ = This->resource.wineD3DDevice->stateBlock->viewport.MaxZ;
368 TRACE("Calling glOrtho with %f, %f, %f, %f\n", width, height, -minZ, -maxZ);
369 glOrtho(X, X + width, Y + height, Y, -minZ, -maxZ);
370 checkGLcall("glOrtho");
372 /* Window Coord 0 is the middle of the first pixel, so translate by half
373 a pixel (See comment above glTranslate below) */
374 glTranslatef(0.5, 0.5, 0);
375 checkGLcall("glTranslatef(0.5, 0.5, 0)");
378 if (This == This->resource.wineD3DDevice->backBuffer) {
379 glDrawBuffer(GL_BACK);
380 } else if (This == This->resource.wineD3DDevice->frontBuffer || This == This->resource.wineD3DDevice->renderTarget) {
381 glDrawBuffer(GL_FRONT);
383 vcheckGLcall("glDrawBuffer");
385 /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
386 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &skipBytes);
387 glPixelStorei(GL_UNPACK_ROW_LENGTH, This->currentDesc.Width);
389 /* And back buffers are not blended */
392 glRasterPos3i(This->lockedRect.left, This->lockedRect.top, 1);
393 vcheckGLcall("glRasterPos2f");
394 switch (This->currentDesc.Format) {
397 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
398 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
399 vcheckGLcall("glDrawPixels");
404 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
405 GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
406 vcheckGLcall("glDrawPixels");
409 case D3DFMT_A8R8G8B8:
411 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
412 vcheckGLcall("glPixelStorei");
413 glDrawPixels(This->lockedRect.right - This->lockedRect.left, (This->lockedRect.bottom - This->lockedRect.top)-1,
414 GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
415 vcheckGLcall("glDrawPixels");
416 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
417 vcheckGLcall("glPixelStorei");
421 FIXME("Unsupported Format %u in locking func\n", This->currentDesc.Format);
424 glPixelZoom(1.0,1.0);
425 vcheckGLcall("glPixelZoom");
426 glDrawBuffer(prev_draw);
427 vcheckGLcall("glDrawBuffer");
428 glRasterPos3iv(&prev_rasterpos[0]);
429 vcheckGLcall("glRasterPos3iv");
431 /* Reset to previous pack row length / blending state */
432 glPixelStorei(GL_UNPACK_ROW_LENGTH, skipBytes);
433 if (This->resource.wineD3DDevice->stateBlock->renderState[D3DRS_ALPHABLENDENABLE]) glEnable(GL_BLEND);
437 /** restore clean dirty state */
438 IWineD3DSurface_CleanDirtyRect(iface);
441 FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
444 } else if (D3DUSAGE_DEPTHSTENCIL & This->currentDesc.Usage) { /* stencil surfaces */
446 if (This == This->resource.wineD3DDevice->depthStencilBuffer) {
447 FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
449 FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
453 FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->currentDesc.Usage);
457 This->locked = FALSE;
458 memset(&This->lockedRect, 0, sizeof(RECT));
462 HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC) {
463 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
464 FIXME("No support for GetDC yet for surface %p\n", This);
465 return D3DERR_INVALIDCALL;
468 HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC) {
469 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
470 FIXME("No support for ReleaseDC yet for surface %p\n", This);
471 return D3DERR_INVALIDCALL;
474 /* ******************************************************
475 IWineD3DSurface Internal (No mapping to directx api) parts follow
476 ****************************************************** */
477 HRESULT WINAPI IWineD3DSurfaceImpl_LoadTexture(IWineD3DSurface *iface, GLenum gl_target, GLenum gl_level) {
478 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
483 if (This->inPBuffer) {
487 FIXME("Surface in texture is only supported for level 0\n");
488 else if (This->currentDesc.Format == D3DFMT_P8 || This->currentDesc.Format == D3DFMT_A8P8 ||
489 This->currentDesc.Format == D3DFMT_DXT1 || This->currentDesc.Format == D3DFMT_DXT3 ||
490 This->currentDesc.Format == D3DFMT_DXT5)
491 FIXME("Format %d not supported\n", This->currentDesc.Format);
493 glCopyTexImage2D(gl_target,
495 D3DFmt2GLIntFmt(This->resource.wineD3DDevice,
496 This->currentDesc.Format),
499 This->currentDesc.Width,
500 This->currentDesc.Height,
502 TRACE("Updating target %d\n", gl_target);
503 This->inTexture = TRUE;
509 if ((This->currentDesc.Format == D3DFMT_P8 || This->currentDesc.Format == D3DFMT_A8P8) &&
510 !GL_SUPPORT(EXT_PALETTED_TEXTURE)) {
512 * wanted a paletted texture and not really support it in HW
513 * so software emulation code begin
516 PALETTEENTRY* pal = This->resource.wineD3DDevice->palettes[This->resource.wineD3DDevice->currentPalette];
517 VOID* surface = (VOID*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->currentDesc.Width * This->currentDesc.Height * sizeof(DWORD));
518 BYTE* dst = (BYTE*) surface;
519 BYTE* src = (BYTE*) This->allocatedMemory;
521 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
523 *dst++ = pal[color].peRed;
524 *dst++ = pal[color].peGreen;
525 *dst++ = pal[color].peBlue;
526 if (This->currentDesc.Format == D3DFMT_A8P8)
527 *dst++ = pal[color].peFlags;
534 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
538 This->currentDesc.Width,
539 This->currentDesc.Height,
544 glTexImage2D(gl_target,
547 This->currentDesc.Width,
548 This->currentDesc.Height,
553 checkGLcall("glTexImage2D");
554 HeapFree(GetProcessHeap(), 0, surface);
561 if (This->currentDesc.Format == D3DFMT_DXT1 ||
562 This->currentDesc.Format == D3DFMT_DXT3 ||
563 This->currentDesc.Format == D3DFMT_DXT5) {
564 if (GL_SUPPORT(EXT_TEXTURE_COMPRESSION_S3TC)) {
565 TRACE("Calling glCompressedTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, sz=%d, Mem=%p\n",
568 D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
569 This->currentDesc.Width,
570 This->currentDesc.Height,
572 This->currentDesc_size,
573 This->allocatedMemory);
577 GL_EXTCALL(glCompressedTexImage2DARB)(gl_target,
579 D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
580 This->currentDesc.Width,
581 This->currentDesc.Height,
583 This->currentDesc_size,
584 This->allocatedMemory);
585 checkGLcall("glCommpressedTexTexImage2D");
589 FIXME("Using DXT1/3/5 without advertized support\n");
593 TRACE("Calling glTexImage2D %x i=%d, d3dfmt=%s, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
596 debug_d3dformat(This->currentDesc.Format),
597 D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
598 This->currentDesc.Width,
599 This->currentDesc.Height,
601 D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
602 D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
603 This->allocatedMemory);
607 glTexImage2D(gl_target,
609 D3DFmt2GLIntFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
610 This->currentDesc.Width,
611 This->currentDesc.Height,
613 D3DFmt2GLFmt(This->resource.wineD3DDevice, This->currentDesc.Format),
614 D3DFmt2GLType(This->resource.wineD3DDevice, This->currentDesc.Format),
615 This->allocatedMemory);
616 checkGLcall("glTexImage2D");
622 static unsigned int gen = 0;
625 if ((gen % 10) == 0) {
626 snprintf(buffer, sizeof(buffer), "/tmp/surface%p_type%u_level%u_%u.ppm", This, gl_target, gl_level, gen);
627 IWineD3DSurfaceImpl_SaveSnapshot((LPDIRECT3DSURFACE8) This, buffer);
630 * debugging crash code
645 HRESULT WINAPI IWineD3DSurfaceImpl_SaveSnapshot(IWineD3DSurface *iface, const char* filename) {
648 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
650 f = fopen(filename, "w+");
652 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
653 return D3DERR_INVALIDCALL;
656 TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->currentDesc.Format));
658 fprintf(f, "P6\n%u %u\n255\n", This->currentDesc.Width, This->currentDesc.Height);
659 switch (This->currentDesc.Format) {
660 case D3DFMT_X8R8G8B8:
661 case D3DFMT_A8R8G8B8:
664 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
665 color = ((DWORD*) This->allocatedMemory)[i];
666 fputc((color >> 16) & 0xFF, f);
667 fputc((color >> 8) & 0xFF, f);
668 fputc((color >> 0) & 0xFF, f);
675 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
676 color = ((BYTE*) This->allocatedMemory) + (3 * i);
677 fputc((color[0]) & 0xFF, f);
678 fputc((color[1]) & 0xFF, f);
679 fputc((color[2]) & 0xFF, f);
683 case D3DFMT_A1R5G5B5:
686 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
687 color = ((WORD*) This->allocatedMemory)[i];
688 fputc(((color >> 10) & 0x1F) * 255 / 31, f);
689 fputc(((color >> 5) & 0x1F) * 255 / 31, f);
690 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
694 case D3DFMT_A4R4G4B4:
697 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
698 color = ((WORD*) This->allocatedMemory)[i];
699 fputc(((color >> 8) & 0x0F) * 255 / 15, f);
700 fputc(((color >> 4) & 0x0F) * 255 / 15, f);
701 fputc(((color >> 0) & 0x0F) * 255 / 15, f);
709 for (i = 0; i < This->currentDesc.Width * This->currentDesc.Height; i++) {
710 color = ((WORD*) This->allocatedMemory)[i];
711 fputc(((color >> 11) & 0x1F) * 255 / 31, f);
712 fputc(((color >> 5) & 0x3F) * 255 / 63, f);
713 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
718 FIXME("Unimplemented dump mode format(%u,%s)\n", This->currentDesc.Format, debug_d3dformat(This->currentDesc.Format));
724 HRESULT WINAPI IWineD3DSurfaceImpl_CleanDirtyRect(IWineD3DSurface *iface) {
725 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
727 This->dirtyRect.left = This->currentDesc.Width;
728 This->dirtyRect.top = This->currentDesc.Height;
729 This->dirtyRect.right = 0;
730 This->dirtyRect.bottom = 0;
731 TRACE("(%p) : Dirty?%d, Rect:(%ld,%ld,%ld,%ld)\n", This, This->Dirty, This->dirtyRect.left,
732 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
737 * Slightly inefficient way to handle multiple dirty rects but it works :)
739 extern HRESULT WINAPI IWineD3DSurfaceImpl_AddDirtyRect(IWineD3DSurface *iface, CONST RECT* pDirtyRect) {
740 IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
742 if (NULL != pDirtyRect) {
743 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
744 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
745 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
746 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
748 This->dirtyRect.left = 0;
749 This->dirtyRect.top = 0;
750 This->dirtyRect.right = This->currentDesc.Width;
751 This->dirtyRect.bottom = This->currentDesc.Height;
753 TRACE("(%p) : Dirty?%d, Rect:(%ld,%ld,%ld,%ld)\n", This, This->Dirty, This->dirtyRect.left,
754 This->dirtyRect.top, This->dirtyRect.right, This->dirtyRect.bottom);
758 IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
760 IWineD3DSurfaceImpl_QueryInterface,
761 IWineD3DSurfaceImpl_AddRef,
762 IWineD3DSurfaceImpl_Release,
763 IWineD3DSurfaceImpl_GetParent,
764 IWineD3DSurfaceImpl_GetDevice,
765 IWineD3DSurfaceImpl_SetPrivateData,
766 IWineD3DSurfaceImpl_GetPrivateData,
767 IWineD3DSurfaceImpl_FreePrivateData,
768 IWineD3DSurfaceImpl_SetPriority,
769 IWineD3DSurfaceImpl_GetPriority,
770 IWineD3DSurfaceImpl_PreLoad,
771 IWineD3DSurfaceImpl_GetType,
772 IWineD3DSurfaceImpl_GetContainer,
773 IWineD3DSurfaceImpl_GetDesc,
774 IWineD3DSurfaceImpl_LockRect,
775 IWineD3DSurfaceImpl_UnlockRect,
776 IWineD3DSurfaceImpl_GetDC,
777 IWineD3DSurfaceImpl_ReleaseDC,
779 IWineD3DSurfaceImpl_CleanDirtyRect,
780 IWineD3DSurfaceImpl_AddDirtyRect,
781 IWineD3DSurfaceImpl_LoadTexture,
782 IWineD3DSurfaceImpl_SaveSnapshot