2 * IDirect3DSurface8 implementation
4 * Copyright 2002-2003 Jason Edmeades
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
39 /* IDirect3DVolume IUnknown parts follow: */
40 HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface,REFIID riid,LPVOID *ppobj)
42 ICOM_THIS(IDirect3DSurface8Impl,iface);
44 if (IsEqualGUID(riid, &IID_IUnknown)
45 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
46 IDirect3DSurface8Impl_AddRef(iface);
51 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
55 ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
56 ICOM_THIS(IDirect3DSurface8Impl,iface);
57 TRACE("(%p) : AddRef from %ld\n", This, This->ref);
61 ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
62 ICOM_THIS(IDirect3DSurface8Impl,iface);
63 ULONG ref = --This->ref;
64 TRACE("(%p) : ReleaseRef to %ld\n", This, This->ref);
66 HeapFree(GetProcessHeap(), 0, This->allocatedMemory);
67 HeapFree(GetProcessHeap(), 0, This);
72 /* IDirect3DSurface8: */
73 HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(LPDIRECT3DSURFACE8 iface, IDirect3DDevice8** ppDevice) {
74 ICOM_THIS(IDirect3DSurface8Impl,iface);
75 TRACE("(%p) : returning %p\n", This, This->Device);
76 *ppDevice = (LPDIRECT3DDEVICE8) This->Device;
78 * Note Calling this method will increase the internal reference count
79 * on the IDirect3DDevice8 interface.
81 IDirect3DDevice8Impl_AddRef(*ppDevice);
85 HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
86 ICOM_THIS(IDirect3DSurface8Impl,iface);
87 FIXME("(%p) : stub\n", This);
91 HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
92 ICOM_THIS(IDirect3DSurface8Impl,iface);
93 FIXME("(%p) : stub\n", This);
97 HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
98 ICOM_THIS(IDirect3DSurface8Impl,iface);
99 FIXME("(%p) : stub\n", This);
103 HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void** ppContainer) {
104 ICOM_THIS(IDirect3DSurface8Impl,iface);
106 res = IUnknown_QueryInterface(This->Container, riid, ppContainer);
107 if (E_NOINTERFACE == res) {
109 * If the surface is created using CreateImageSurface, CreateRenderTarget,
110 * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
111 * GetContainer will return the Direct3D device used to create the surface.
113 res = IUnknown_QueryInterface(This->Container, &IID_IDirect3DDevice8, ppContainer);
115 TRACE("(%p) : returning %p\n", This, *ppContainer);
119 HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
120 ICOM_THIS(IDirect3DSurface8Impl,iface);
122 TRACE("(%p) : copying into %p\n", This, pDesc);
123 memcpy(pDesc, &This->myDesc, sizeof(D3DSURFACE_DESC));
127 HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT* pLockedRect, CONST RECT* pRect, DWORD Flags) {
129 ICOM_THIS(IDirect3DSurface8Impl,iface);
131 /* fixme: should we really lock as such? */
133 if (FALSE == This->lockable) {
134 /* Note: UpdateTextures calls CopyRects which calls this routine to populate the
135 texture regions, and since the destination is an unlockable region we need
137 TRACE("Warning: trying to lock unlockable surf@%p\n", This);
138 /*return D3DERR_INVALIDCALL; */
141 if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer || This->Device->depthStencilBuffer) {
142 if (This == This->Device->backBuffer) {
143 TRACE("(%p, backBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
144 } else if (This == This->Device->frontBuffer) {
145 TRACE("(%p, frontBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
146 } else if (This == This->Device->renderTarget) {
147 TRACE("(%p, renderTarget) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
148 } else if (This == This->Device->depthStencilBuffer) {
149 TRACE("(%p, stencilBuffer) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
152 TRACE("(%p) : rect@%p flags(%08lx), output lockedRect@%p, memory@%p\n", This, pRect, Flags, pLockedRect, This->allocatedMemory);
155 pLockedRect->Pitch = This->bytesPerPixel * This->myDesc.Width; /* Bytes / row */
158 pLockedRect->pBits = This->allocatedMemory;
159 This->lockedRect.left = 0;
160 This->lockedRect.top = 0;
161 This->lockedRect.right = This->myDesc.Width;
162 This->lockedRect.bottom = This->myDesc.Height;
163 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);
165 TRACE("Lock Rect (%p) = l %ld, t %ld, r %ld, b %ld\n", pRect, pRect->left, pRect->top, pRect->right, pRect->bottom);
166 pLockedRect->pBits = This->allocatedMemory + (pLockedRect->Pitch * pRect->top) + (pRect->left * This->bytesPerPixel);
167 This->lockedRect.left = pRect->left;
168 This->lockedRect.top = pRect->top;
169 This->lockedRect.right = pRect->right;
170 This->lockedRect.bottom = pRect->bottom;
174 if (0 == This->myDesc.Usage) { /* classic surface */
176 /* Nothing to do ;) */
178 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
180 if (This == This->Device->backBuffer || This == This->Device->renderTarget || This == This->Device->frontBuffer) {
187 * for render->surface copy begin to begin of allocatedMemory
188 * unlock can be more easy
190 pLockedRect->pBits = This->allocatedMemory;
193 vcheckGLcall("glFlush");
194 glGetIntegerv(GL_READ_BUFFER, &prev_read);
195 vcheckGLcall("glIntegerv");
196 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
197 vcheckGLcall("glIntegerv");
199 if (This == This->Device->backBuffer) {
200 glReadBuffer(GL_BACK);
201 } else if (This == This->Device->frontBuffer || This == This->Device->renderTarget) {
202 glReadBuffer(GL_FRONT);
203 } else if (This == This->Device->depthStencilBuffer) {
204 ERR("Stencil Buffer lock unsupported for now\n");
206 vcheckGLcall("glReadBuffer");
210 GLenum format = D3DFmt2GLFmt(This->Device, This->myDesc.Format);
211 GLenum type = D3DFmt2GLType(This->Device, This->myDesc.Format);
212 for (j = This->lockedRect.top; j < This->lockedRect.bottom - This->lockedRect.top; ++j) {
213 glReadPixels(This->lockedRect.left,
214 This->lockedRect.bottom - j - 1,
215 This->lockedRect.right - This->lockedRect.left,
219 (char *)pLockedRect->pBits + (pLockedRect->Pitch * (j-This->lockedRect.top)));
220 vcheckGLcall("glReadPixels");
224 glReadBuffer(prev_read);
225 vcheckGLcall("glReadBuffer");
230 FIXME("unsupported locking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
233 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
235 FIXME("TODO stencil depth surface locking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
238 FIXME("unsupported locking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
241 if (Flags & (D3DLOCK_NO_DIRTY_UPDATE | D3DLOCK_READONLY)) {
246 * as seen in msdn docs
248 IDirect3DSurface8Impl_AddDirtyRect(iface, &This->lockedRect);
250 /** Dirtify Container if needed */
251 if (NULL != This->Container) {
252 IDirect3DBaseTexture8* cont = NULL;
253 hr = IUnknown_QueryInterface(This->Container, &IID_IDirect3DBaseTexture8, (void**) &cont);
255 if (SUCCEEDED(hr) && NULL != cont) {
256 IDirect3DBaseTexture8Impl_SetDirty(cont, TRUE);
257 IDirect3DBaseTexture8_Release(cont);
263 TRACE("returning memory@%p, pitch(%d) dirtyfied(%d)\n", pLockedRect->pBits, pLockedRect->Pitch, This->Dirty);
269 HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
270 ICOM_THIS(IDirect3DSurface8Impl,iface);
272 if (FALSE == This->locked) {
273 ERR("trying to lock unlocked surf@%p\n", This);
274 return D3DERR_INVALIDCALL;
277 if (This == This->Device->backBuffer || This == This->Device->frontBuffer || This->Device->depthStencilBuffer) {
278 if (This == This->Device->backBuffer) {
279 TRACE("(%p, backBuffer) : dirtyfied(%d)\n", This, This->Dirty);
280 } else if (This == This->Device->frontBuffer) {
281 TRACE("(%p, frontBuffer) : dirtyfied(%d)\n", This, This->Dirty);
282 } else if (This == This->Device->depthStencilBuffer) {
283 TRACE("(%p, stencilBuffer) : dirtyfied(%d)\n", This, This->Dirty);
286 TRACE("(%p) : dirtyfied(%d)\n", This, This->Dirty);
288 /*TRACE("(%p) see if behavior is correct\n", This);*/
290 if (FALSE == This->Dirty) {
291 TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
295 if (0 == This->myDesc.Usage) { /* classic surface */
298 * waiting to reload the surface via IDirect3DDevice8::UpdateTexture
300 } else if (D3DUSAGE_RENDERTARGET & This->myDesc.Usage) { /* render surfaces */
302 if (This == This->Device->backBuffer || This == This->Device->frontBuffer) {
305 GLint prev_rasterpos[4];
310 vcheckGLcall("glFlush");
311 glGetIntegerv(GL_DRAW_BUFFER, &prev_draw);
312 vcheckGLcall("glIntegerv");
313 glGetIntegerv(GL_PACK_SWAP_BYTES, &prev_store);
314 vcheckGLcall("glIntegerv");
315 glGetIntegerv(GL_CURRENT_RASTER_POSITION, &prev_rasterpos[0]);
316 vcheckGLcall("glIntegerv");
318 if (This == This->Device->backBuffer) {
319 glDrawBuffer(GL_BACK);
320 } else if (This == This->Device->frontBuffer) {
321 glDrawBuffer(GL_FRONT);
323 vcheckGLcall("glDrawBuffer");
325 glRasterPos2i(This->lockedRect.left, This->lockedRect.top);
326 vcheckGLcall("glRasterPos2f");
327 switch (This->myDesc.Format) {
330 glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
331 GL_RGB, GL_UNSIGNED_SHORT_5_6_5, This->allocatedMemory);
332 vcheckGLcall("glDrawPixels");
337 glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
338 GL_RGB, GL_UNSIGNED_BYTE, This->allocatedMemory);
339 vcheckGLcall("glDrawPixels");
342 case D3DFMT_A8R8G8B8:
344 glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
345 vcheckGLcall("glPixelStorei");
346 glDrawPixels(This->lockedRect.right - This->lockedRect.left, This->lockedRect.bottom - This->lockedRect.top,
347 GL_BGRA, GL_UNSIGNED_BYTE, This->allocatedMemory);
348 vcheckGLcall("glDrawPixels");
349 glPixelStorei(GL_PACK_SWAP_BYTES, prev_store);
350 vcheckGLcall("glPixelStorei");
354 FIXME("Unsupported Format %u in locking func\n", This->myDesc.Format);
357 glDrawBuffer(prev_draw);
358 vcheckGLcall("glDrawBuffer");
359 glRasterPos3iv(&prev_rasterpos[0]);
360 vcheckGLcall("glRasterPos3iv");
364 /** restore clean dirty state */
365 IDirect3DSurface8Impl_CleanDirtyRect(iface);
368 FIXME("unsupported unlocking to Rendering surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
371 } else if (D3DUSAGE_DEPTHSTENCIL & This->myDesc.Usage) { /* stencil surfaces */
373 if (This == This->Device->depthStencilBuffer) {
374 FIXME("TODO stencil depth surface unlocking surf@%p usage(%lu)\n", This, This->myDesc.Usage);
376 FIXME("unsupported unlocking to StencilDepth surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
380 FIXME("unsupported unlocking to surface surf@%p usage(%lu)\n", This, This->myDesc.Usage);
384 This->locked = FALSE;
385 memset(&This->lockedRect, 0, sizeof(RECT));
390 ICOM_VTABLE(IDirect3DSurface8) Direct3DSurface8_Vtbl =
392 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
393 IDirect3DSurface8Impl_QueryInterface,
394 IDirect3DSurface8Impl_AddRef,
395 IDirect3DSurface8Impl_Release,
396 IDirect3DSurface8Impl_GetDevice,
397 IDirect3DSurface8Impl_SetPrivateData,
398 IDirect3DSurface8Impl_GetPrivateData,
399 IDirect3DSurface8Impl_FreePrivateData,
400 IDirect3DSurface8Impl_GetContainer,
401 IDirect3DSurface8Impl_GetDesc,
402 IDirect3DSurface8Impl_LockRect,
403 IDirect3DSurface8Impl_UnlockRect,
407 HRESULT WINAPI IDirect3DSurface8Impl_LoadTexture(LPDIRECT3DSURFACE8 iface, GLenum gl_target, GLenum gl_level) {
408 ICOM_THIS(IDirect3DSurface8Impl,iface);
410 if ((This->myDesc.Format == D3DFMT_P8 || This->myDesc.Format == D3DFMT_A8P8)
411 #if defined(GL_EXT_paletted_texture)
412 && !GL_SUPPORT_DEV(EXT_PALETTED_TEXTURE, This->Device)
416 * wanted a paletted texture and not really support it in HW
417 * so software emulation code begin
420 PALETTEENTRY* pal = This->Device->palettes[This->Device->currentPalette];
421 VOID* surface = (VOID*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->myDesc.Width * This->myDesc.Height * sizeof(DWORD));
422 BYTE* dst = (BYTE*) surface;
423 BYTE* src = (BYTE*) This->allocatedMemory;
425 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
427 *dst++ = pal[color].peRed;
428 *dst++ = pal[color].peGreen;
429 *dst++ = pal[color].peBlue;
430 if (This->myDesc.Format == D3DFMT_A8P8)
431 *dst++ = pal[color].peFlags;
438 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
448 glTexImage2D(gl_target,
457 checkGLcall("glTexImage2D");
458 HeapFree(GetProcessHeap(), 0, surface);
465 if (This->myDesc.Format == D3DFMT_DXT1 ||
466 This->myDesc.Format == D3DFMT_DXT3 ||
467 This->myDesc.Format == D3DFMT_DXT5) {
468 #if defined(GL_EXT_texture_compression_s3tc)
469 if (GL_SUPPORT_DEV(EXT_TEXTURE_COMPRESSION_S3TC, This->Device)) {
470 TRACE("Calling glCompressedTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, sz=%d, Mem=%p\n",
473 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
478 This->allocatedMemory);
482 glCompressedTexImage2DARB(gl_target,
484 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
489 This->allocatedMemory);
490 checkGLcall("glCommpressedTexTexImage2D");
496 TRACE("Calling glTexImage2D %x i=%d, intfmt=%x, w=%d, h=%d,0=%d, glFmt=%x, glType=%x, Mem=%p\n",
499 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
503 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
504 D3DFmt2GLType(This->Device, This->myDesc.Format),
505 This->allocatedMemory);
509 glTexImage2D(gl_target,
511 D3DFmt2GLIntFmt(This->Device, This->myDesc.Format),
515 D3DFmt2GLFmt(This->Device, This->myDesc.Format),
516 D3DFmt2GLType(This->Device, This->myDesc.Format),
517 This->allocatedMemory);
518 checkGLcall("glTexImage2D");
524 static unsigned int gen = 0;
527 if ((gen % 10) == 0) {
528 snprintf(buffer, sizeof(buffer), "/tmp/surface%u_level%u_%u.ppm", gl_target, gl_level, gen);
529 IDirect3DSurface8Impl_SaveSnapshot((LPDIRECT3DSURFACE8) This, buffer);
539 HRESULT WINAPI IDirect3DSurface8Impl_SaveSnapshot(LPDIRECT3DSURFACE8 iface, const char* filename) {
542 ICOM_THIS(IDirect3DSurface8Impl,iface);
544 f = fopen(filename, "w+");
546 ERR("opening of %s failed with: %s\n", filename, strerror(errno));
547 return D3DERR_INVALIDCALL;
550 TRACE("opened %s with format %s\n", filename, debug_d3dformat(This->myDesc.Format));
552 fprintf(f, "P6\n%u %u\n255\n", This->myDesc.Width, This->myDesc.Height);
553 switch (This->myDesc.Format) {
554 case D3DFMT_X8R8G8B8:
555 case D3DFMT_A8R8G8B8:
558 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
559 color = ((DWORD*) This->allocatedMemory)[i];
560 fputc((color >> 16) & 0xFF, f);
561 fputc((color >> 8) & 0xFF, f);
562 fputc((color >> 0) & 0xFF, f);
569 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
570 color = ((BYTE*) This->allocatedMemory) + (3 * i);
571 fputc((color[0]) & 0xFF, f);
572 fputc((color[1]) & 0xFF, f);
573 fputc((color[2]) & 0xFF, f);
577 case D3DFMT_A1R5G5B5:
580 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
581 color = ((WORD*) This->allocatedMemory)[i];
582 fputc(((color >> 10) & 0x1F) * 255 / 31, f);
583 fputc(((color >> 5) & 0x1F) * 255 / 31, f);
584 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
588 case D3DFMT_A4R4G4B4:
591 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
592 color = ((WORD*) This->allocatedMemory)[i];
593 fputc(((color >> 8) & 0x0F) * 255 / 15, f);
594 fputc(((color >> 4) & 0x0F) * 255 / 15, f);
595 fputc(((color >> 0) & 0x0F) * 255 / 15, f);
603 for (i = 0; i < This->myDesc.Width * This->myDesc.Height; i++) {
604 color = ((WORD*) This->allocatedMemory)[i];
605 fputc(((color >> 11) & 0x1F) * 255 / 31, f);
606 fputc(((color >> 5) & 0x3F) * 255 / 63, f);
607 fputc(((color >> 0) & 0x1F) * 255 / 31, f);
612 FIXME("Unimplemented dump mode format(%u,%s)\n", This->myDesc.Format, debug_d3dformat(This->myDesc.Format));
618 HRESULT WINAPI IDirect3DSurface8Impl_CleanDirtyRect(LPDIRECT3DSURFACE8 iface) {
619 ICOM_THIS(IDirect3DSurface8Impl,iface);
621 This->dirtyRect.left = This->myDesc.Width;
622 This->dirtyRect.top = This->myDesc.Height;
623 This->dirtyRect.right = 0;
624 This->dirtyRect.bottom = 0;
630 * very stupid way to handle multiple dirty rects but it works :)
632 extern HRESULT WINAPI IDirect3DSurface8Impl_AddDirtyRect(LPDIRECT3DSURFACE8 iface, CONST RECT* pDirtyRect) {
633 ICOM_THIS(IDirect3DSurface8Impl,iface);
635 if (NULL != pDirtyRect) {
636 This->dirtyRect.left = min(This->dirtyRect.left, pDirtyRect->left);
637 This->dirtyRect.top = min(This->dirtyRect.top, pDirtyRect->top);
638 This->dirtyRect.right = max(This->dirtyRect.right, pDirtyRect->right);
639 This->dirtyRect.bottom = max(This->dirtyRect.bottom, pDirtyRect->bottom);
641 This->dirtyRect.left = 0;
642 This->dirtyRect.top = 0;
643 This->dirtyRect.right = This->myDesc.Width;
644 This->dirtyRect.bottom = This->myDesc.Height;