2 * IDirect3DSurface8 implementation
4 * Copyright 2002 Jason Edmeades
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.
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.
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
27 #include "wine/debug.h"
30 #include "d3d8_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
34 /* IDirect3DVolume IUnknown parts follow: */
35 HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface,REFIID riid,LPVOID *ppobj)
37 ICOM_THIS(IDirect3DSurface8Impl,iface);
39 if (IsEqualGUID(riid, &IID_IUnknown)
40 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
41 IDirect3DSurface8Impl_AddRef(iface);
46 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
50 ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
51 ICOM_THIS(IDirect3DSurface8Impl,iface);
52 TRACE("(%p) : AddRef from %ld\n", This, This->ref);
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);
61 HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
62 HeapFree(GetProcessHeap(), 0, This);
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;
73 * Note Calling this method will increase the internal reference count
74 * on the IDirect3DDevice8 interface.
76 IDirect3DDevice8Impl_AddRef(*ppDevice);
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);
84 HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
85 ICOM_THIS(IDirect3DSurface8Impl,iface);
86 FIXME("(%p) : stub\n", This);
89 HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
90 ICOM_THIS(IDirect3DSurface8Impl,iface);
91 FIXME("(%p) : stub\n", This);
94 HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void** ppContainer) {
95 ICOM_THIS(IDirect3DSurface8Impl,iface);
97 res = IUnknown_QueryInterface(This->Container, riid, ppContainer);
98 if (E_NOINTERFACE == res) {
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.
104 res = IUnknown_QueryInterface(This->Container, &IID_IDirect3DDevice8, ppContainer);
106 TRACE("(%p) : returning %p\n", This, *ppContainer);
109 HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
110 ICOM_THIS(IDirect3DSurface8Impl,iface);
112 TRACE("(%p) : copying into %p\n", This, pDesc);
113 memcpy(pDesc, &This->myDesc, sizeof(D3DSURFACE_DESC));
116 HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
118 ICOM_THIS(IDirect3DSurface8Impl,iface);
120 /* fixme: should we really lock as such? */
122 if (FALSE == This->lockable) {
123 ERR("trying to lock unlockable surf@%p\n", This);
124 return D3DERR_INVALIDCALL;
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);
138 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
141 pLockedRect->Pitch = This->bytesPerPixel * This->myDesc.Width; /* Bytes / row */
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);
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;
160 if (0 == This->myDesc.Usage) { /* classic surface */
162 /* Nothing to do ;) */
164 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
166 if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer) {
173 * for render->surface copy begin to begin of allocatedMemory
174 * unlock can be more easy
176 pLockedRect->pBits = This->allocatedMemory;
179 vcheckGLcall("glFlush");
180 glGetIntegerv(GL_READ_BUFFER, &prev_read);
181 vcheckGLcall("glIntegerv");
182 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
183 vcheckGLcall("glIntegerv");
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");
192 vcheckGLcall("glReadBuffer");
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,
205 (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
206 vcheckGLcall("glReadPixels");
210 glReadBuffer(prev_read);
211 vcheckGLcall("glReadBuffer");
216 FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
219 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
221 FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
224 FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
227 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
232 * as seen in msdn docs
234 IDirect3DSurface8Impl_AddDirtyRect(iface, &This->lockedRect);
236 /** Dirtify Container if needed */
237 if (NULL != This->Container) {
238 IDirect3DBaseTexture8* cont = NULL;
239 hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
241 if (SUCCEEDED(hr) && NULL != cont) {
242 IDirect3DBaseTexture8Impl_SetDirty(cont, TRUE);
243 IDirect3DBaseTexture8_Release(cont);
249 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
254 HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
255 ICOM_THIS(IDirect3DSurface8Impl,iface);
257 if (FALSE == This->locked) {
258 ERR("trying to lock unlocked surf@%p\n", This);
259 return D3DERR_INVALIDCALL;
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);
271 TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
273 /*TRACE("(%p) see if behavior is correct\n", This);*/
275 if (FALSE == This->Dirty) {
276 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
280 if (0 == This->myDesc.Usage) { /* classic surface */
283 * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
285 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
287 if (This == This->Device->backBuffer || This == This->Device->frontBuffer) {
290 GLint prev_rasterpos[4];
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");
303 if (This == This->Device->backBuffer) {
304 glDrawBuffer(GL_BACK);
305 } else if (This == This->Device->frontBuffer) {
306 glDrawBuffer(GL_FRONT);
308 vcheckGLcall("glDrawBuffer");
310 glRasterPos2i(This->lockedRect.left, This->lockedRect.top);
311 vcheckGLcall("glRasterPos2f");
312 switch (This->myDesc.Format) {
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");
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");
327 case D3DFMT_A8R8G8B8:
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");
339 FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
342 glDrawBuffer(prev_draw);
343 vcheckGLcall("glDrawBuffer");
344 glRasterPos3iv(&prev_rasterpos[0]);
345 vcheckGLcall("glRasterPos3iv");
349 /** restore clean dirty state */
350 IDirect3DSurface8Impl_CleanDirtyRect(iface);
353 FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
356 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
358 if (This == This->Device->depthStencilBuffer) {
359 FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
361 FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
365 FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
369 This->locked = FALSE;
370 memset(&This->lockedRect, 0, sizeof(RECT));
374 ICOM_VTABLE(IDirect3DSurface8) Direct3DSurface8_Vtbl =
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,
390 HRESULT WINAPI IDirect3DSurface8Impl_LoadTexture(LPDIRECT3DSURFACE8 iface, GLenum gl_target, GLenum gl_level) {
391 ICOM_THIS(IDirect3DSurface8Impl,iface);
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)
399 * wanted a paletted texture and not really support it in HW
400 * so software emulation code begin
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;
408 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
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;
421 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
431 glTexImage2D(gl_target,
440 checkGLcall("glTexImage2D");
441 HeapFree(GetProcessHeap(), 0, surface);
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",
456 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
461 This->allocatedMemory);
465 glCompressedTexImage2DARB(gl_target,
467 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
472 This->allocatedMemory);
473 checkGLcall("glCommpressedTexTexImage2D");
479 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
482 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
486 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
487 D3DFmt2GLType(This->Device, This->myDesc.Format),
488 This->allocatedMemory);
492 glTexImage2D(gl_target,
494 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
498 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
499 D3DFmt2GLType(This->Device, This->myDesc.Format),
500 This->allocatedMemory);
501 checkGLcall("glTexImage2D");
507 static unsigned int gen = 0;
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);
522 HRESULT WINAPI IDirect3DSurface8Impl_SaveSnapshot(LPDIRECT3DSURFACE8 iface, const char* filename) {
525 ICOM_THIS(IDirect3DSurface8Impl,iface);
527 f = fopen(filename, "w+");
529 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
530 return D3DERR_INVALIDCALL;
533 TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->myDesc.Format));
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:
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);
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);
560 case D3DFMT_A1R5G5B5:
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);
571 case D3DFMT_A4R4G4B4:
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);
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);
595 FIXME("Unimplemented dump mode format(%u,%s)\n", This->myDesc.Format, debug_d3dformat(This->myDesc.Format));
601 HRESULT WINAPI IDirect3DSurface8Impl_CleanDirtyRect(LPDIRECT3DSURFACE8 iface) {
602 ICOM_THIS(IDirect3DSurface8Impl,iface);
604 This->dirtyRect.left = This->myDesc.Width;
605 This->dirtyRect.top = This->myDesc.Height;
606 This->dirtyRect.right = 0;
607 This->dirtyRect.bottom = 0;
613 * very stupid way to handle multiple dirty rects but it works :)
615 extern HRESULT WINAPI IDirect3DSurface8Impl_AddDirtyRect(LPDIRECT3DSURFACE8 iface, CONST RECT* pDirtyRect) {
616 ICOM_THIS(IDirect3DSurface8Impl,iface);
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);
624 This->dirtyRect.left = 0;
625 This->dirtyRect.top = 0;
626 This->dirtyRect.right = This->myDesc.Width;
627 This->dirtyRect.bottom = This->myDesc.Height;