2 * IWineD3DSurface Implementation of management(non-rendering) functions
4 * Copyright 1998 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2002-2005 Jason Edmeades
7 * Copyright 2002-2003 Raphael Junqueira
8 * Copyright 2004 Christian Costa
9 * Copyright 2005 Oliver Stieber
10 * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
11 * Copyright 2007 Henri Verbeet
12 * Copyright 2006-2007 Roderick Colenbrander
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/port.h"
31 #include "wined3d_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface
);
37 /* See also float_16_to_32() in wined3d_private.h */
38 static inline unsigned short float_32_to_16(const float *in
)
41 float tmp
= fabs(*in
);
42 unsigned int mantissa
;
45 /* Deal with special numbers */
46 if(*in
== 0.0) return 0x0000;
47 if(isnan(*in
)) return 0x7C01;
48 if(isinf(*in
)) return (*in
< 0.0 ? 0xFC00 : 0x7c00);
50 if(tmp
< pow(2, 10)) {
55 }while(tmp
< pow(2, 10));
56 } else if(tmp
>= pow(2, 11)) {
61 }while(tmp
>= pow(2, 11));
64 mantissa
= (unsigned int) tmp
;
65 if(tmp
- mantissa
>= 0.5) mantissa
++; /* round to nearest, away from zero */
67 exp
+= 10; /* Normalize the mantissa */
68 exp
+= 15; /* Exponent is encoded with excess 15 */
70 if(exp
> 30) { /* too big */
71 ret
= 0x7c00; /* INF */
73 /* exp == 0: Non-normalized mantissa. Returns 0x0000 (=0.0) for too small numbers */
75 mantissa
= mantissa
>> 1;
78 ret
= mantissa
& 0x3ff;
80 ret
= (exp
<< 10) | (mantissa
& 0x3ff);
83 ret
|= ((*in
< 0.0 ? 1 : 0) << 15); /* Add the sign */
88 /* Do NOT define GLINFO_LOCATION in this file. THIS CODE MUST NOT USE IT */
90 /* *******************************************
91 IWineD3DSurface IUnknown parts follow
92 ******************************************* */
93 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_QueryInterface(IWineD3DSurface
*iface
, REFIID riid
, LPVOID
*ppobj
)
95 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
96 /* Warn ,but be nice about things */
97 TRACE("(%p)->(%s,%p)\n", This
,debugstr_guid(riid
),ppobj
);
99 if (IsEqualGUID(riid
, &IID_IUnknown
)
100 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
101 || IsEqualGUID(riid
, &IID_IWineD3DResource
)
102 || IsEqualGUID(riid
, &IID_IWineD3DSurface
)) {
103 IUnknown_AddRef((IUnknown
*)iface
);
108 return E_NOINTERFACE
;
111 ULONG WINAPI
IWineD3DBaseSurfaceImpl_AddRef(IWineD3DSurface
*iface
) {
112 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
113 ULONG ref
= InterlockedIncrement(&This
->resource
.ref
);
114 TRACE("(%p) : AddRef increasing from %d\n", This
,ref
- 1);
118 /* ****************************************************
119 IWineD3DSurface IWineD3DResource parts follow
120 **************************************************** */
121 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetDevice(IWineD3DSurface
*iface
, IWineD3DDevice
** ppDevice
) {
122 return IWineD3DResourceImpl_GetDevice((IWineD3DResource
*)iface
, ppDevice
);
125 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetPrivateData(IWineD3DSurface
*iface
, REFGUID refguid
, CONST
void* pData
, DWORD SizeOfData
, DWORD Flags
) {
126 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, SizeOfData
, Flags
);
129 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetPrivateData(IWineD3DSurface
*iface
, REFGUID refguid
, void* pData
, DWORD
* pSizeOfData
) {
130 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource
*)iface
, refguid
, pData
, pSizeOfData
);
133 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_FreePrivateData(IWineD3DSurface
*iface
, REFGUID refguid
) {
134 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource
*)iface
, refguid
);
137 DWORD WINAPI
IWineD3DBaseSurfaceImpl_SetPriority(IWineD3DSurface
*iface
, DWORD PriorityNew
) {
138 return IWineD3DResourceImpl_SetPriority((IWineD3DResource
*)iface
, PriorityNew
);
141 DWORD WINAPI
IWineD3DBaseSurfaceImpl_GetPriority(IWineD3DSurface
*iface
) {
142 return IWineD3DResourceImpl_GetPriority((IWineD3DResource
*)iface
);
145 WINED3DRESOURCETYPE WINAPI
IWineD3DBaseSurfaceImpl_GetType(IWineD3DSurface
*iface
) {
146 TRACE("(%p) : calling resourceimpl_GetType\n", iface
);
147 return IWineD3DResourceImpl_GetType((IWineD3DResource
*)iface
);
150 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetParent(IWineD3DSurface
*iface
, IUnknown
**pParent
) {
151 TRACE("(%p) : calling resourceimpl_GetParent\n", iface
);
152 return IWineD3DResourceImpl_GetParent((IWineD3DResource
*)iface
, pParent
);
155 /* ******************************************************
156 IWineD3DSurface IWineD3DSurface parts follow
157 ****************************************************** */
159 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetContainer(IWineD3DSurface
* iface
, REFIID riid
, void** ppContainer
) {
160 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
161 IWineD3DBase
*container
= 0;
163 TRACE("(This %p, riid %s, ppContainer %p)\n", This
, debugstr_guid(riid
), ppContainer
);
166 ERR("Called without a valid ppContainer.\n");
170 * If the surface is created using CreateImageSurface/CreateOffscreenPlainSurface, CreateRenderTarget,
171 * or CreateDepthStencilSurface, the surface is considered stand alone. In this case,
172 * GetContainer will return the Direct3D device used to create the surface.
174 if (This
->container
) {
175 container
= This
->container
;
177 container
= (IWineD3DBase
*)This
->resource
.wineD3DDevice
;
180 TRACE("Relaying to QueryInterface\n");
181 return IUnknown_QueryInterface(container
, riid
, ppContainer
);
184 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetDesc(IWineD3DSurface
*iface
, WINED3DSURFACE_DESC
*pDesc
) {
185 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
187 TRACE("(%p) : copying into %p\n", This
, pDesc
);
188 if(pDesc
->Format
!= NULL
) *(pDesc
->Format
) = This
->resource
.format
;
189 if(pDesc
->Type
!= NULL
) *(pDesc
->Type
) = This
->resource
.resourceType
;
190 if(pDesc
->Usage
!= NULL
) *(pDesc
->Usage
) = This
->resource
.usage
;
191 if(pDesc
->Pool
!= NULL
) *(pDesc
->Pool
) = This
->resource
.pool
;
192 if(pDesc
->Size
!= NULL
) *(pDesc
->Size
) = This
->resource
.size
; /* dx8 only */
193 if(pDesc
->MultiSampleType
!= NULL
) *(pDesc
->MultiSampleType
) = This
->currentDesc
.MultiSampleType
;
194 if(pDesc
->MultiSampleQuality
!= NULL
) *(pDesc
->MultiSampleQuality
) = This
->currentDesc
.MultiSampleQuality
;
195 if(pDesc
->Width
!= NULL
) *(pDesc
->Width
) = This
->currentDesc
.Width
;
196 if(pDesc
->Height
!= NULL
) *(pDesc
->Height
) = This
->currentDesc
.Height
;
200 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetBltStatus(IWineD3DSurface
*iface
, DWORD Flags
) {
201 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
202 TRACE("(%p)->(%x)\n", This
, Flags
);
206 case WINEDDGBS_CANBLT
:
207 case WINEDDGBS_ISBLTDONE
:
211 return WINED3DERR_INVALIDCALL
;
215 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetFlipStatus(IWineD3DSurface
*iface
, DWORD Flags
) {
216 /* XXX: DDERR_INVALIDSURFACETYPE */
218 TRACE("(%p)->(%08x)\n",iface
,Flags
);
220 case WINEDDGFS_CANFLIP
:
221 case WINEDDGFS_ISFLIPDONE
:
225 return WINED3DERR_INVALIDCALL
;
229 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_IsLost(IWineD3DSurface
*iface
) {
230 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
231 TRACE("(%p)\n", This
);
233 /* D3D8 and 9 loose full devices, ddraw only surfaces */
234 return This
->Flags
& SFLAG_LOST
? WINED3DERR_DEVICELOST
: WINED3D_OK
;
237 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_Restore(IWineD3DSurface
*iface
) {
238 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
239 TRACE("(%p)\n", This
);
241 /* So far we don't lose anything :) */
242 This
->Flags
&= ~SFLAG_LOST
;
246 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetPalette(IWineD3DSurface
*iface
, IWineD3DPalette
*Pal
) {
247 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
248 IWineD3DPaletteImpl
*PalImpl
= (IWineD3DPaletteImpl
*) Pal
;
249 TRACE("(%p)->(%p)\n", This
, Pal
);
251 if(This
->palette
== PalImpl
) {
252 TRACE("Nop palette change\n");
256 if(This
->palette
!= NULL
)
257 if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
)
258 This
->palette
->Flags
&= ~WINEDDPCAPS_PRIMARYSURFACE
;
260 This
->palette
= PalImpl
;
262 if(PalImpl
!= NULL
) {
263 if(This
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
) {
264 (PalImpl
)->Flags
|= WINEDDPCAPS_PRIMARYSURFACE
;
267 return IWineD3DSurface_RealizePalette(iface
);
269 else return WINED3D_OK
;
272 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetColorKey(IWineD3DSurface
*iface
, DWORD Flags
, WINEDDCOLORKEY
*CKey
) {
273 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
274 TRACE("(%p)->(%08x,%p)\n", This
, Flags
, CKey
);
276 if ((Flags
& WINEDDCKEY_COLORSPACE
) != 0) {
277 FIXME(" colorkey value not supported (%08x) !\n", Flags
);
278 return WINED3DERR_INVALIDCALL
;
281 /* Dirtify the surface, but only if a key was changed */
283 switch (Flags
& ~WINEDDCKEY_COLORSPACE
) {
284 case WINEDDCKEY_DESTBLT
:
285 This
->DestBltCKey
= *CKey
;
286 This
->CKeyFlags
|= WINEDDSD_CKDESTBLT
;
289 case WINEDDCKEY_DESTOVERLAY
:
290 This
->DestOverlayCKey
= *CKey
;
291 This
->CKeyFlags
|= WINEDDSD_CKDESTOVERLAY
;
294 case WINEDDCKEY_SRCOVERLAY
:
295 This
->SrcOverlayCKey
= *CKey
;
296 This
->CKeyFlags
|= WINEDDSD_CKSRCOVERLAY
;
299 case WINEDDCKEY_SRCBLT
:
300 This
->SrcBltCKey
= *CKey
;
301 This
->CKeyFlags
|= WINEDDSD_CKSRCBLT
;
306 switch (Flags
& ~WINEDDCKEY_COLORSPACE
) {
307 case WINEDDCKEY_DESTBLT
:
308 This
->CKeyFlags
&= ~WINEDDSD_CKDESTBLT
;
311 case WINEDDCKEY_DESTOVERLAY
:
312 This
->CKeyFlags
&= ~WINEDDSD_CKDESTOVERLAY
;
315 case WINEDDCKEY_SRCOVERLAY
:
316 This
->CKeyFlags
&= ~WINEDDSD_CKSRCOVERLAY
;
319 case WINEDDCKEY_SRCBLT
:
320 This
->CKeyFlags
&= ~WINEDDSD_CKSRCBLT
;
328 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetPalette(IWineD3DSurface
*iface
, IWineD3DPalette
**Pal
) {
329 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
330 TRACE("(%p)->(%p)\n", This
, Pal
);
332 *Pal
= (IWineD3DPalette
*) This
->palette
;
336 DWORD WINAPI
IWineD3DBaseSurfaceImpl_GetPitch(IWineD3DSurface
*iface
) {
337 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
339 TRACE("(%p)\n", This
);
341 /* DXTn formats don't have exact pitches as they are to the new row of blocks,
342 where each block is 4x4 pixels, 8 bytes (dxt1) and 16 bytes (dxt2/3/4/5)
343 ie pitch = (width/4) * bytes per block */
344 if (This
->resource
.format
== WINED3DFMT_DXT1
) /* DXT1 is 8 bytes per block */
345 ret
= ((This
->currentDesc
.Width
+ 3) >> 2) << 3;
346 else if (This
->resource
.format
== WINED3DFMT_DXT2
|| This
->resource
.format
== WINED3DFMT_DXT3
||
347 This
->resource
.format
== WINED3DFMT_DXT4
|| This
->resource
.format
== WINED3DFMT_DXT5
) /* DXT2/3/4/5 is 16 bytes per block */
348 ret
= ((This
->currentDesc
.Width
+ 3) >> 2) << 4;
350 unsigned char alignment
= This
->resource
.wineD3DDevice
->surface_alignment
;
351 ret
= This
->bytesPerPixel
* This
->currentDesc
.Width
; /* Bytes / row */
352 ret
= (ret
+ alignment
- 1) & ~(alignment
- 1);
354 TRACE("(%p) Returning %d\n", This
, ret
);
358 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetOverlayPosition(IWineD3DSurface
*iface
, LONG X
, LONG Y
) {
359 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
361 FIXME("(%p)->(%d,%d) Stub!\n", This
, X
, Y
);
363 if(!(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
))
365 TRACE("(%p): Not an overlay surface\n", This
);
366 return WINEDDERR_NOTAOVERLAYSURFACE
;
372 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetOverlayPosition(IWineD3DSurface
*iface
, LONG
*X
, LONG
*Y
) {
373 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
375 FIXME("(%p)->(%p,%p) Stub!\n", This
, X
, Y
);
377 if(!(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
))
379 TRACE("(%p): Not an overlay surface\n", This
);
380 return WINEDDERR_NOTAOVERLAYSURFACE
;
386 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder(IWineD3DSurface
*iface
, DWORD Flags
, IWineD3DSurface
*Ref
) {
387 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
388 IWineD3DSurfaceImpl
*RefImpl
= (IWineD3DSurfaceImpl
*) Ref
;
390 FIXME("(%p)->(%08x,%p) Stub!\n", This
, Flags
, RefImpl
);
392 if(!(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
))
394 TRACE("(%p): Not an overlay surface\n", This
);
395 return WINEDDERR_NOTAOVERLAYSURFACE
;
401 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_UpdateOverlay(IWineD3DSurface
*iface
, RECT
*SrcRect
, IWineD3DSurface
*DstSurface
, RECT
*DstRect
, DWORD Flags
, WINEDDOVERLAYFX
*FX
) {
402 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
403 IWineD3DSurfaceImpl
*Dst
= (IWineD3DSurfaceImpl
*) DstSurface
;
404 FIXME("(%p)->(%p, %p, %p, %08x, %p)\n", This
, SrcRect
, Dst
, DstRect
, Flags
, FX
);
406 if(!(This
->resource
.usage
& WINED3DUSAGE_OVERLAY
))
408 TRACE("(%p): Not an overlay surface\n", This
);
409 return WINEDDERR_NOTAOVERLAYSURFACE
;
415 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetClipper(IWineD3DSurface
*iface
, IWineD3DClipper
*clipper
)
417 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
418 TRACE("(%p)->(%p)\n", This
, clipper
);
420 This
->clipper
= clipper
;
424 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_GetClipper(IWineD3DSurface
*iface
, IWineD3DClipper
**clipper
)
426 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
427 TRACE("(%p)->(%p)\n", This
, clipper
);
429 *clipper
= This
->clipper
;
431 IWineD3DClipper_AddRef(*clipper
);
436 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetContainer(IWineD3DSurface
*iface
, IWineD3DBase
*container
) {
437 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
439 TRACE("This %p, container %p\n", This
, container
);
441 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
443 TRACE("Setting container to %p from %p\n", container
, This
->container
);
444 This
->container
= container
;
449 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_SetFormat(IWineD3DSurface
*iface
, WINED3DFORMAT format
) {
450 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
451 const StaticPixelFormatDesc
*formatEntry
= getFormatDescEntry(format
, NULL
, NULL
);
453 if (This
->resource
.format
!= WINED3DFMT_UNKNOWN
) {
454 FIXME("(%p) : The format of the surface must be WINED3DFORMAT_UNKNOWN\n", This
);
455 return WINED3DERR_INVALIDCALL
;
458 TRACE("(%p) : Setting texture format to (%d,%s)\n", This
, format
, debug_d3dformat(format
));
459 if (format
== WINED3DFMT_UNKNOWN
) {
460 This
->resource
.size
= 0;
461 } else if (format
== WINED3DFMT_DXT1
) {
462 /* DXT1 is half byte per pixel */
463 This
->resource
.size
= ((max(This
->pow2Width
, 4) * formatEntry
->bpp
) * max(This
->pow2Height
, 4)) >> 1;
465 } else if (format
== WINED3DFMT_DXT2
|| format
== WINED3DFMT_DXT3
||
466 format
== WINED3DFMT_DXT4
|| format
== WINED3DFMT_DXT5
) {
467 This
->resource
.size
= ((max(This
->pow2Width
, 4) * formatEntry
->bpp
) * max(This
->pow2Height
, 4));
469 unsigned char alignment
= This
->resource
.wineD3DDevice
->surface_alignment
;
470 This
->resource
.size
= ((This
->pow2Width
* formatEntry
->bpp
) + alignment
- 1) & ~(alignment
- 1);
471 This
->resource
.size
*= This
->pow2Height
;
474 if (format
!= WINED3DFMT_UNKNOWN
) {
475 This
->bytesPerPixel
= formatEntry
->bpp
;
477 This
->bytesPerPixel
= 0;
480 This
->Flags
|= (WINED3DFMT_D16_LOCKABLE
== format
) ? SFLAG_LOCKABLE
: 0;
482 This
->resource
.format
= format
;
484 TRACE("(%p) : Size %d, bytesPerPixel %d\n", This
, This
->resource
.size
, This
->bytesPerPixel
);
489 HRESULT
IWineD3DBaseSurfaceImpl_CreateDIBSection(IWineD3DSurface
*iface
) {
490 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
496 const StaticPixelFormatDesc
*formatEntry
= getFormatDescEntry(This
->resource
.format
, NULL
, NULL
);
499 switch (This
->bytesPerPixel
) {
502 /* Allocate extra space to store the RGB bit masks. */
503 b_info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
) + 3 * sizeof(DWORD
));
507 b_info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(BITMAPINFOHEADER
));
511 /* Allocate extra space for a palette. */
512 b_info
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
513 sizeof(BITMAPINFOHEADER
)
515 * (1 << (This
->bytesPerPixel
* 8)));
520 return E_OUTOFMEMORY
;
522 /* Some apps access the surface in via DWORDs, and do not take the necessary care at the end of the
523 * surface. So we need at least extra 4 bytes at the end of the surface. Check against the page size,
524 * if the last page used for the surface has at least 4 spare bytes we're safe, otherwise
525 * add an extra line to the dib section
527 GetSystemInfo(&sysInfo
);
528 if( ((This
->resource
.size
+ 3) % sysInfo
.dwPageSize
) < 4) {
530 TRACE("Adding an extra line to the dib section\n");
533 b_info
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
534 /* TODO: Is there a nicer way to force a specific alignment? (8 byte for ddraw) */
535 b_info
->bmiHeader
.biWidth
= IWineD3DSurface_GetPitch(iface
) / This
->bytesPerPixel
;
536 b_info
->bmiHeader
.biHeight
= -This
->currentDesc
.Height
-extraline
;
537 b_info
->bmiHeader
.biSizeImage
= ( This
->currentDesc
.Height
+ extraline
) * IWineD3DSurface_GetPitch(iface
);
538 b_info
->bmiHeader
.biPlanes
= 1;
539 b_info
->bmiHeader
.biBitCount
= This
->bytesPerPixel
* 8;
541 b_info
->bmiHeader
.biXPelsPerMeter
= 0;
542 b_info
->bmiHeader
.biYPelsPerMeter
= 0;
543 b_info
->bmiHeader
.biClrUsed
= 0;
544 b_info
->bmiHeader
.biClrImportant
= 0;
546 /* Get the bit masks */
547 masks
= (DWORD
*) &(b_info
->bmiColors
);
548 switch (This
->resource
.format
) {
549 case WINED3DFMT_R8G8B8
:
550 usage
= DIB_RGB_COLORS
;
551 b_info
->bmiHeader
.biCompression
= BI_RGB
;
554 case WINED3DFMT_X1R5G5B5
:
555 case WINED3DFMT_A1R5G5B5
:
556 case WINED3DFMT_A4R4G4B4
:
557 case WINED3DFMT_X4R4G4B4
:
558 case WINED3DFMT_R3G3B2
:
559 case WINED3DFMT_A8R3G3B2
:
560 case WINED3DFMT_A2B10G10R10
:
561 case WINED3DFMT_A8B8G8R8
:
562 case WINED3DFMT_X8B8G8R8
:
563 case WINED3DFMT_A2R10G10B10
:
564 case WINED3DFMT_R5G6B5
:
565 case WINED3DFMT_A16B16G16R16
:
567 b_info
->bmiHeader
.biCompression
= BI_BITFIELDS
;
568 masks
[0] = formatEntry
->redMask
;
569 masks
[1] = formatEntry
->greenMask
;
570 masks
[2] = formatEntry
->blueMask
;
574 /* Don't know palette */
575 b_info
->bmiHeader
.biCompression
= BI_RGB
;
582 HeapFree(GetProcessHeap(), 0, b_info
);
583 return HRESULT_FROM_WIN32(GetLastError());
586 TRACE("Creating a DIB section with size %dx%dx%d, size=%d\n", b_info
->bmiHeader
.biWidth
, b_info
->bmiHeader
.biHeight
, b_info
->bmiHeader
.biBitCount
, b_info
->bmiHeader
.biSizeImage
);
587 This
->dib
.DIBsection
= CreateDIBSection(ddc
, b_info
, usage
, &This
->dib
.bitmap_data
, 0 /* Handle */, 0 /* Offset */);
590 if (!This
->dib
.DIBsection
) {
591 ERR("CreateDIBSection failed!\n");
592 HeapFree(GetProcessHeap(), 0, b_info
);
593 return HRESULT_FROM_WIN32(GetLastError());
596 TRACE("DIBSection at : %p\n", This
->dib
.bitmap_data
);
597 /* copy the existing surface to the dib section */
598 if(This
->resource
.allocatedMemory
) {
599 memcpy(This
->dib
.bitmap_data
, This
->resource
.allocatedMemory
, This
->currentDesc
.Height
* IWineD3DSurface_GetPitch(iface
));
601 /* This is to make LockRect read the gl Texture although memory is allocated */
602 This
->Flags
&= ~SFLAG_INSYSMEM
;
604 This
->dib
.bitmap_size
= b_info
->bmiHeader
.biSizeImage
;
606 HeapFree(GetProcessHeap(), 0, b_info
);
608 /* Now allocate a HDC */
609 This
->hDC
= CreateCompatibleDC(0);
610 This
->dib
.holdbitmap
= SelectObject(This
->hDC
, This
->dib
.DIBsection
);
611 TRACE("using wined3d palette %p\n", This
->palette
);
612 SelectPalette(This
->hDC
,
613 This
->palette
? This
->palette
->hpal
: 0,
616 This
->Flags
|= SFLAG_DIBSECTION
;
618 HeapFree(GetProcessHeap(), 0, This
->resource
.heapMemory
);
619 This
->resource
.heapMemory
= NULL
;
624 void convert_r32f_r16f(BYTE
*src
, BYTE
*dst
, DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
) {
627 unsigned short *dst_s
;
629 TRACE("Converting %dx%d pixels, pitches %d %d\n", w
, h
, pitch_in
, pitch_out
);
630 for(y
= 0; y
< h
; y
++) {
631 src_f
= (float *) (src
+ y
* pitch_in
);
632 dst_s
= (unsigned short *) (dst
+ y
* pitch_out
);
633 for(x
= 0; x
< w
; x
++) {
634 dst_s
[x
] = float_32_to_16(src_f
+ x
);
639 struct d3dfmt_convertor_desc
{
640 WINED3DFORMAT from
, to
;
641 void (*convert
)(BYTE
*src
, BYTE
*dst
, DWORD pitch_in
, DWORD pitch_out
, unsigned int w
, unsigned int h
);
644 struct d3dfmt_convertor_desc convertors
[] = {
645 {WINED3DFMT_R32F
, WINED3DFMT_R16F
, convert_r32f_r16f
},
648 static inline struct d3dfmt_convertor_desc
*find_convertor(WINED3DFORMAT from
, WINED3DFORMAT to
) {
650 for(i
= 0; i
< (sizeof(convertors
) / sizeof(convertors
[0])); i
++) {
651 if(convertors
[i
].from
== from
&& convertors
[i
].to
== to
) {
652 return &convertors
[i
];
658 /*****************************************************************************
659 * surface_convert_format
661 * Creates a duplicate of a surface in a different format. Is used by Blt to
662 * blit between surfaces with different formats
665 * source: Source surface
666 * fmt: Requested destination format
668 *****************************************************************************/
669 IWineD3DSurfaceImpl
*surface_convert_format(IWineD3DSurfaceImpl
*source
, WINED3DFORMAT to_fmt
) {
670 IWineD3DSurface
*ret
= NULL
;
671 struct d3dfmt_convertor_desc
*conv
;
672 WINED3DLOCKED_RECT lock_src
, lock_dst
;
675 conv
= find_convertor(source
->resource
.format
, to_fmt
);
677 FIXME("Cannot find a conversion function from format %s to %s\n",
678 debug_d3dformat(source
->resource
.format
), debug_d3dformat(to_fmt
));
682 IWineD3DDevice_CreateSurface((IWineD3DDevice
*) source
->resource
.wineD3DDevice
,
683 source
->currentDesc
.Width
,
684 source
->currentDesc
.Height
,
690 WINED3DRTYPE_SURFACE
,
693 WINED3DMULTISAMPLE_NONE
, /* TODO: Multisampled conversion */
694 0, /* MultiSampleQuality */
695 NULL
, /* SharedHandle */
696 IWineD3DSurface_GetImplType((IWineD3DSurface
*) source
),
699 ERR("Failed to create a destination surface for conversion\n");
703 memset(&lock_src
, 0, sizeof(lock_src
));
704 memset(&lock_dst
, 0, sizeof(lock_dst
));
706 hr
= IWineD3DSurface_LockRect((IWineD3DSurface
*) source
, &lock_src
, NULL
, WINED3DLOCK_READONLY
);
708 ERR("Failed to lock the source surface\n");
709 IWineD3DSurface_Release(ret
);
712 hr
= IWineD3DSurface_LockRect(ret
, &lock_dst
, NULL
, WINED3DLOCK_READONLY
);
714 ERR("Failed to lock the dest surface\n");
715 IWineD3DSurface_UnlockRect((IWineD3DSurface
*) source
);
716 IWineD3DSurface_Release(ret
);
720 conv
->convert(lock_src
.pBits
, lock_dst
.pBits
, lock_src
.Pitch
, lock_dst
.Pitch
,
721 source
->currentDesc
.Width
, source
->currentDesc
.Height
);
723 IWineD3DSurface_UnlockRect(ret
);
724 IWineD3DSurface_UnlockRect((IWineD3DSurface
*) source
);
726 return (IWineD3DSurfaceImpl
*) ret
;
729 /*****************************************************************************
732 * Helper function that fills a memory area with a specific color
735 * buf: memory address to start filling at
736 * width, height: Dimensions of the area to fill
737 * bpp: Bit depth of the surface
738 * lPitch: pitch of the surface
739 * color: Color to fill with
741 *****************************************************************************/
743 _Blt_ColorFill(BYTE
*buf
,
744 int width
, int height
,
745 int bpp
, LONG lPitch
,
753 #define COLORFILL_ROW(type) \
755 type *d = (type *) buf; \
756 for (x = 0; x < width; x++) \
757 d[x] = (type) color; \
762 case 1: COLORFILL_ROW(BYTE
)
763 case 2: COLORFILL_ROW(WORD
)
767 for (x
= 0; x
< width
; x
++,d
+=3)
769 d
[0] = (color
) & 0xFF;
770 d
[1] = (color
>> 8) & 0xFF;
771 d
[2] = (color
>>16) & 0xFF;
775 case 4: COLORFILL_ROW(DWORD
)
777 FIXME("Color fill not implemented for bpp %d!\n", bpp
*8);
778 return WINED3DERR_NOTAVAILABLE
;
783 /* Now copy first row */
785 for (y
= 1; y
< height
; y
++)
788 memcpy(buf
, first
, width
* bpp
);
793 /*****************************************************************************
794 * IWineD3DSurface::Blt, SW emulation version
796 * Performs blits to a surface, eigher from a source of source-less blts
797 * This is the main functionality of DirectDraw
800 * DestRect: Destination rectangle to write to
801 * SrcSurface: Source surface, can be NULL
802 * SrcRect: Source rectangle
803 *****************************************************************************/
805 IWineD3DBaseSurfaceImpl_Blt(IWineD3DSurface
*iface
,
807 IWineD3DSurface
*SrcSurface
,
810 WINEDDBLTFX
*DDBltFx
,
811 WINED3DTEXTUREFILTERTYPE Filter
)
813 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
814 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) SrcSurface
;
816 HRESULT ret
= WINED3D_OK
;
817 WINED3DLOCKED_RECT dlock
, slock
;
818 WINED3DFORMAT dfmt
= WINED3DFMT_UNKNOWN
, sfmt
= WINED3DFMT_UNKNOWN
;
819 int bpp
, srcheight
, srcwidth
, dstheight
, dstwidth
, width
;
821 const StaticPixelFormatDesc
*sEntry
, *dEntry
;
823 TRACE("(%p)->(%p,%p,%p,%x,%p)\n", This
, DestRect
, Src
, SrcRect
, Flags
, DDBltFx
);
825 if (TRACE_ON(d3d_surface
))
827 if (DestRect
) TRACE("\tdestrect :%dx%d-%dx%d\n",
828 DestRect
->left
, DestRect
->top
, DestRect
->right
, DestRect
->bottom
);
829 if (SrcRect
) TRACE("\tsrcrect :%dx%d-%dx%d\n",
830 SrcRect
->left
, SrcRect
->top
, SrcRect
->right
, SrcRect
->bottom
);
833 DDRAW_dump_DDBLT(Flags
);
834 if (Flags
& WINEDDBLT_DDFX
)
837 DDRAW_dump_DDBLTFX(DDBltFx
->dwDDFX
);
842 if ( (This
->Flags
& SFLAG_LOCKED
) || ((Src
!= NULL
) && (Src
->Flags
& SFLAG_LOCKED
)))
844 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
845 return WINEDDERR_SURFACEBUSY
;
848 if(Filter
!= WINED3DTEXF_NONE
&& Filter
!= WINED3DTEXF_POINT
) {
849 /* Can happen when d3d9 apps do a StretchRect call which isn't handled in gl */
850 FIXME("Filters not supported in software blit\n");
855 IWineD3DSurface_LockRect(iface
, &dlock
, NULL
, 0);
856 dfmt
= This
->resource
.format
;
859 sEntry
= getFormatDescEntry(sfmt
, NULL
, NULL
);
864 dfmt
= This
->resource
.format
;
865 dEntry
= getFormatDescEntry(dfmt
, NULL
, NULL
);
868 if(This
->resource
.format
!= Src
->resource
.format
) {
869 Src
= surface_convert_format(Src
, dfmt
);
871 /* The conv function writes a FIXME */
872 WARN("Cannot convert source surface format to dest format\n");
876 IWineD3DSurface_LockRect((IWineD3DSurface
*) Src
, &slock
, NULL
, WINED3DLOCK_READONLY
);
877 sfmt
= Src
->resource
.format
;
879 sEntry
= getFormatDescEntry(sfmt
, NULL
, NULL
);
880 IWineD3DSurface_LockRect(iface
, &dlock
,NULL
,0);
883 if (!DDBltFx
|| !(DDBltFx
->dwDDFX
)) Flags
&= ~WINEDDBLT_DDFX
;
885 if (sEntry
->isFourcc
&& dEntry
->isFourcc
)
887 memcpy(dlock
.pBits
, slock
.pBits
, This
->resource
.size
);
898 xdst
.bottom
= This
->currentDesc
.Height
;
900 xdst
.right
= This
->currentDesc
.Width
;
912 xsrc
.bottom
= Src
->currentDesc
.Height
;
914 xsrc
.right
= Src
->currentDesc
.Width
;
918 memset(&xsrc
,0,sizeof(xsrc
));
922 /* First check for the validity of source / destination rectangles. This was
923 * verified using a test application + by MSDN.
926 ((xsrc
.bottom
> Src
->currentDesc
.Height
) || (xsrc
.bottom
< 0) ||
927 (xsrc
.top
> Src
->currentDesc
.Height
) || (xsrc
.top
< 0) ||
928 (xsrc
.left
> Src
->currentDesc
.Width
) || (xsrc
.left
< 0) ||
929 (xsrc
.right
> Src
->currentDesc
.Width
) || (xsrc
.right
< 0) ||
930 (xsrc
.right
< xsrc
.left
) || (xsrc
.bottom
< xsrc
.top
)))
932 WARN("Application gave us bad source rectangle for Blt.\n");
933 ret
= WINEDDERR_INVALIDRECT
;
936 /* For the Destination rect, it can be out of bounds on the condition that a clipper
937 * is set for the given surface.
939 if ((/*This->clipper == NULL*/ TRUE
) &&
940 ((xdst
.bottom
> This
->currentDesc
.Height
) || (xdst
.bottom
< 0) ||
941 (xdst
.top
> This
->currentDesc
.Height
) || (xdst
.top
< 0) ||
942 (xdst
.left
> This
->currentDesc
.Width
) || (xdst
.left
< 0) ||
943 (xdst
.right
> This
->currentDesc
.Width
) || (xdst
.right
< 0) ||
944 (xdst
.right
< xdst
.left
) || (xdst
.bottom
< xdst
.top
)))
946 WARN("Application gave us bad destination rectangle for Blt without a clipper set.\n");
947 ret
= WINEDDERR_INVALIDRECT
;
951 /* Now handle negative values in the rectangles. Warning: only supported for now
952 in the 'simple' cases (ie not in any stretching / rotation cases).
954 First, the case where nothing is to be done.
956 if (((xdst
.bottom
<= 0) || (xdst
.right
<= 0) ||
957 (xdst
.top
>= (int) This
->currentDesc
.Height
) ||
958 (xdst
.left
>= (int) This
->currentDesc
.Width
)) ||
960 ((xsrc
.bottom
<= 0) || (xsrc
.right
<= 0) ||
961 (xsrc
.top
>= (int) Src
->currentDesc
.Height
) ||
962 (xsrc
.left
>= (int) Src
->currentDesc
.Width
)) ))
964 TRACE("Nothing to be done !\n");
968 /* The easy case : the source-less blits.... */
972 RECT temp_rect
; /* No idea if intersect rect can be the same as one of the source rect */
976 full_rect
.right
= This
->currentDesc
.Width
;
977 full_rect
.bottom
= This
->currentDesc
.Height
;
978 IntersectRect(&temp_rect
, &full_rect
, &xdst
);
983 /* Only handle clipping on the destination rectangle */
984 int clip_horiz
= (xdst
.left
< 0) || (xdst
.right
> (int) This
->currentDesc
.Width
);
985 int clip_vert
= (xdst
.top
< 0) || (xdst
.bottom
> (int) This
->currentDesc
.Height
);
986 if (clip_vert
|| clip_horiz
)
988 /* Now check if this is a special case or not... */
989 if ((((xdst
.bottom
- xdst
.top
) != (xsrc
.bottom
- xsrc
.top
)) && clip_vert
) ||
990 (((xdst
.right
- xdst
.left
) != (xsrc
.right
- xsrc
.left
)) && clip_horiz
) ||
991 (Flags
& WINEDDBLT_DDFX
))
993 WARN("Out of screen rectangle in special case. Not handled right now.\n");
999 if (xdst
.left
< 0) { xsrc
.left
-= xdst
.left
; xdst
.left
= 0; }
1000 if (xdst
.right
> This
->currentDesc
.Width
)
1002 xsrc
.right
-= (xdst
.right
- (int) This
->currentDesc
.Width
);
1003 xdst
.right
= (int) This
->currentDesc
.Width
;
1010 xsrc
.top
-= xdst
.top
;
1013 if (xdst
.bottom
> This
->currentDesc
.Height
)
1015 xsrc
.bottom
-= (xdst
.bottom
- (int) This
->currentDesc
.Height
);
1016 xdst
.bottom
= (int) This
->currentDesc
.Height
;
1019 /* And check if after clipping something is still to be done... */
1020 if ((xdst
.bottom
<= 0) || (xdst
.right
<= 0) ||
1021 (xdst
.top
>= (int) This
->currentDesc
.Height
) ||
1022 (xdst
.left
>= (int) This
->currentDesc
.Width
) ||
1023 (xsrc
.bottom
<= 0) || (xsrc
.right
<= 0) ||
1024 (xsrc
.top
>= (int) Src
->currentDesc
.Height
) ||
1025 (xsrc
.left
>= (int) Src
->currentDesc
.Width
))
1027 TRACE("Nothing to be done after clipping !\n");
1033 bpp
= This
->bytesPerPixel
;
1034 srcheight
= xsrc
.bottom
- xsrc
.top
;
1035 srcwidth
= xsrc
.right
- xsrc
.left
;
1036 dstheight
= xdst
.bottom
- xdst
.top
;
1037 dstwidth
= xdst
.right
- xdst
.left
;
1038 width
= (xdst
.right
- xdst
.left
) * bpp
;
1040 assert(width
<= dlock
.Pitch
);
1042 dbuf
= (BYTE
*)dlock
.pBits
+(xdst
.top
*dlock
.Pitch
)+(xdst
.left
*bpp
);
1044 if (Flags
& WINEDDBLT_WAIT
)
1046 Flags
&= ~WINEDDBLT_WAIT
;
1048 if (Flags
& WINEDDBLT_ASYNC
)
1050 static BOOL displayed
= FALSE
;
1052 FIXME("Can't handle WINEDDBLT_ASYNC flag right now.\n");
1054 Flags
&= ~WINEDDBLT_ASYNC
;
1056 if (Flags
& WINEDDBLT_DONOTWAIT
)
1058 /* WINEDDBLT_DONOTWAIT appeared in DX7 */
1059 static BOOL displayed
= FALSE
;
1061 FIXME("Can't handle WINEDDBLT_DONOTWAIT flag right now.\n");
1063 Flags
&= ~WINEDDBLT_DONOTWAIT
;
1066 /* First, all the 'source-less' blits */
1067 if (Flags
& WINEDDBLT_COLORFILL
)
1069 ret
= _Blt_ColorFill(dbuf
, dstwidth
, dstheight
, bpp
,
1070 dlock
.Pitch
, DDBltFx
->u5
.dwFillColor
);
1071 Flags
&= ~WINEDDBLT_COLORFILL
;
1074 if (Flags
& WINEDDBLT_DEPTHFILL
)
1076 FIXME("DDBLT_DEPTHFILL needs to be implemented!\n");
1078 if (Flags
& WINEDDBLT_ROP
)
1080 /* Catch some degenerate cases here */
1081 switch(DDBltFx
->dwROP
)
1084 ret
= _Blt_ColorFill(dbuf
,dstwidth
,dstheight
,bpp
,dlock
.Pitch
,0);
1086 case 0xAA0029: /* No-op */
1089 ret
= _Blt_ColorFill(dbuf
,dstwidth
,dstheight
,bpp
,dlock
.Pitch
,~0);
1091 case SRCCOPY
: /* well, we do that below ? */
1094 FIXME("Unsupported raster op: %08x Pattern: %p\n", DDBltFx
->dwROP
, DDBltFx
->u5
.lpDDSPattern
);
1097 Flags
&= ~WINEDDBLT_ROP
;
1099 if (Flags
& WINEDDBLT_DDROPS
)
1101 FIXME("\tDdraw Raster Ops: %08x Pattern: %p\n", DDBltFx
->dwDDROP
, DDBltFx
->u5
.lpDDSPattern
);
1103 /* Now the 'with source' blits */
1107 int sx
, xinc
, sy
, yinc
;
1109 if (!dstwidth
|| !dstheight
) /* hmm... stupid program ? */
1111 sbase
= (BYTE
*)slock
.pBits
+(xsrc
.top
*slock
.Pitch
)+xsrc
.left
*bpp
;
1112 xinc
= (srcwidth
<< 16) / dstwidth
;
1113 yinc
= (srcheight
<< 16) / dstheight
;
1117 /* No effects, we can cheat here */
1118 if (dstwidth
== srcwidth
)
1120 if (dstheight
== srcheight
)
1122 /* No stretching in either direction. This needs to be as
1123 * fast as possible */
1126 /* check for overlapping surfaces */
1127 if (Src
!= This
|| xdst
.top
< xsrc
.top
||
1128 xdst
.right
<= xsrc
.left
|| xsrc
.right
<= xdst
.left
)
1130 /* no overlap, or dst above src, so copy from top downwards */
1131 for (y
= 0; y
< dstheight
; y
++)
1133 memcpy(dbuf
, sbuf
, width
);
1134 sbuf
+= slock
.Pitch
;
1135 dbuf
+= dlock
.Pitch
;
1138 else if (xdst
.top
> xsrc
.top
) /* copy from bottom upwards */
1140 sbuf
+= (slock
.Pitch
*dstheight
);
1141 dbuf
+= (dlock
.Pitch
*dstheight
);
1142 for (y
= 0; y
< dstheight
; y
++)
1144 sbuf
-= slock
.Pitch
;
1145 dbuf
-= dlock
.Pitch
;
1146 memcpy(dbuf
, sbuf
, width
);
1149 else /* src and dst overlapping on the same line, use memmove */
1151 for (y
= 0; y
< dstheight
; y
++)
1153 memmove(dbuf
, sbuf
, width
);
1154 sbuf
+= slock
.Pitch
;
1155 dbuf
+= dlock
.Pitch
;
1159 /* Stretching in Y direction only */
1160 for (y
= sy
= 0; y
< dstheight
; y
++, sy
+= yinc
) {
1161 sbuf
= sbase
+ (sy
>> 16) * slock
.Pitch
;
1162 memcpy(dbuf
, sbuf
, width
);
1163 dbuf
+= dlock
.Pitch
;
1169 /* Stretching in X direction */
1171 for (y
= sy
= 0; y
< dstheight
; y
++, sy
+= yinc
)
1173 sbuf
= sbase
+ (sy
>> 16) * slock
.Pitch
;
1175 if ((sy
>> 16) == (last_sy
>> 16))
1177 /* this sourcerow is the same as last sourcerow -
1178 * copy already stretched row
1180 memcpy(dbuf
, dbuf
- dlock
.Pitch
, width
);
1184 #define STRETCH_ROW(type) { \
1185 type *s = (type *) sbuf, *d = (type *) dbuf; \
1186 for (x = sx = 0; x < dstwidth; x++, sx += xinc) \
1187 d[x] = s[sx >> 16]; \
1192 case 1: STRETCH_ROW(BYTE
)
1193 case 2: STRETCH_ROW(WORD
)
1194 case 4: STRETCH_ROW(DWORD
)
1198 for (x
= sx
= 0; x
< dstwidth
; x
++, sx
+= xinc
)
1202 s
= sbuf
+3*(sx
>>16);
1203 pixel
= s
[0]|(s
[1]<<8)|(s
[2]<<16);
1204 d
[0] = (pixel
)&0xff;
1205 d
[1] = (pixel
>> 8)&0xff;
1206 d
[2] = (pixel
>>16)&0xff;
1212 FIXME("Stretched blit not implemented for bpp %d!\n", bpp
*8);
1213 ret
= WINED3DERR_NOTAVAILABLE
;
1218 dbuf
+= dlock
.Pitch
;
1225 LONG dstyinc
= dlock
.Pitch
, dstxinc
= bpp
;
1226 DWORD keylow
= 0xFFFFFFFF, keyhigh
= 0, keymask
= 0xFFFFFFFF;
1227 DWORD destkeylow
= 0x0, destkeyhigh
= 0xFFFFFFFF, destkeymask
= 0xFFFFFFFF;
1228 if (Flags
& (WINEDDBLT_KEYSRC
| WINEDDBLT_KEYDEST
| WINEDDBLT_KEYSRCOVERRIDE
| WINEDDBLT_KEYDESTOVERRIDE
))
1230 /* The color keying flags are checked for correctness in ddraw */
1231 if (Flags
& WINEDDBLT_KEYSRC
)
1233 keylow
= Src
->SrcBltCKey
.dwColorSpaceLowValue
;
1234 keyhigh
= Src
->SrcBltCKey
.dwColorSpaceHighValue
;
1236 else if (Flags
& WINEDDBLT_KEYSRCOVERRIDE
)
1238 keylow
= DDBltFx
->ddckSrcColorkey
.dwColorSpaceLowValue
;
1239 keyhigh
= DDBltFx
->ddckSrcColorkey
.dwColorSpaceHighValue
;
1242 if (Flags
& WINEDDBLT_KEYDEST
)
1244 /* Destination color keys are taken from the source surface ! */
1245 destkeylow
= Src
->DestBltCKey
.dwColorSpaceLowValue
;
1246 destkeyhigh
= Src
->DestBltCKey
.dwColorSpaceHighValue
;
1248 else if (Flags
& WINEDDBLT_KEYDESTOVERRIDE
)
1250 destkeylow
= DDBltFx
->ddckDestColorkey
.dwColorSpaceLowValue
;
1251 destkeyhigh
= DDBltFx
->ddckDestColorkey
.dwColorSpaceHighValue
;
1260 keymask
= sEntry
->redMask
|
1264 Flags
&= ~(WINEDDBLT_KEYSRC
| WINEDDBLT_KEYDEST
| WINEDDBLT_KEYSRCOVERRIDE
| WINEDDBLT_KEYDESTOVERRIDE
);
1267 if (Flags
& WINEDDBLT_DDFX
)
1269 LPBYTE dTopLeft
, dTopRight
, dBottomLeft
, dBottomRight
, tmp
;
1272 dTopRight
= dbuf
+((dstwidth
-1)*bpp
);
1273 dBottomLeft
= dTopLeft
+((dstheight
-1)*dlock
.Pitch
);
1274 dBottomRight
= dBottomLeft
+((dstwidth
-1)*bpp
);
1276 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_ARITHSTRETCHY
)
1278 /* I don't think we need to do anything about this flag */
1279 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_ARITHSTRETCHY\n");
1281 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_MIRRORLEFTRIGHT
)
1284 dTopRight
= dTopLeft
;
1287 dBottomRight
= dBottomLeft
;
1289 dstxinc
= dstxinc
*-1;
1291 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_MIRRORUPDOWN
)
1294 dTopLeft
= dBottomLeft
;
1297 dTopRight
= dBottomRight
;
1299 dstyinc
= dstyinc
*-1;
1301 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_NOTEARING
)
1303 /* I don't think we need to do anything about this flag */
1304 WARN("Flags=DDBLT_DDFX nothing done for WINEDDBLTFX_NOTEARING\n");
1306 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_ROTATE180
)
1309 dBottomRight
= dTopLeft
;
1312 dBottomLeft
= dTopRight
;
1314 dstxinc
= dstxinc
* -1;
1315 dstyinc
= dstyinc
* -1;
1317 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_ROTATE270
)
1320 dTopLeft
= dBottomLeft
;
1321 dBottomLeft
= dBottomRight
;
1322 dBottomRight
= dTopRight
;
1327 dstxinc
= dstxinc
* -1;
1329 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_ROTATE90
)
1332 dTopLeft
= dTopRight
;
1333 dTopRight
= dBottomRight
;
1334 dBottomRight
= dBottomLeft
;
1339 dstyinc
= dstyinc
* -1;
1341 if (DDBltFx
->dwDDFX
& WINEDDBLTFX_ZBUFFERBASEDEST
)
1343 /* I don't think we need to do anything about this flag */
1344 WARN("Flags=WINEDDBLT_DDFX nothing done for WINEDDBLTFX_ZBUFFERBASEDEST\n");
1347 Flags
&= ~(WINEDDBLT_DDFX
);
1350 #define COPY_COLORKEY_FX(type) { \
1351 type *s, *d = (type *) dbuf, *dx, tmp; \
1352 for (y = sy = 0; y < dstheight; y++, sy += yinc) { \
1353 s = (type*)(sbase + (sy >> 16) * slock.Pitch); \
1355 for (x = sx = 0; x < dstwidth; x++, sx += xinc) { \
1356 tmp = s[sx >> 16]; \
1357 if (((tmp & keymask) < keylow || (tmp & keymask) > keyhigh) && \
1358 ((dx[0] & destkeymask) >= destkeylow && (dx[0] & destkeymask) <= destkeyhigh)) { \
1361 dx = (type*)(((LPBYTE)dx)+dstxinc); \
1363 d = (type*)(((LPBYTE)d)+dstyinc); \
1368 case 1: COPY_COLORKEY_FX(BYTE
)
1369 case 2: COPY_COLORKEY_FX(WORD
)
1370 case 4: COPY_COLORKEY_FX(DWORD
)
1373 LPBYTE s
,d
= dbuf
, dx
;
1374 for (y
= sy
= 0; y
< dstheight
; y
++, sy
+= yinc
)
1376 sbuf
= sbase
+ (sy
>> 16) * slock
.Pitch
;
1378 for (x
= sx
= 0; x
< dstwidth
; x
++, sx
+= xinc
)
1380 DWORD pixel
, dpixel
= 0;
1381 s
= sbuf
+3*(sx
>>16);
1382 pixel
= s
[0]|(s
[1]<<8)|(s
[2]<<16);
1383 dpixel
= dx
[0]|(dx
[1]<<8)|(dx
[2]<<16);
1384 if (((pixel
& keymask
) < keylow
|| (pixel
& keymask
) > keyhigh
) &&
1385 ((dpixel
& keymask
) >= destkeylow
|| (dpixel
& keymask
) <= keyhigh
))
1387 dx
[0] = (pixel
)&0xff;
1388 dx
[1] = (pixel
>> 8)&0xff;
1389 dx
[2] = (pixel
>>16)&0xff;
1398 FIXME("%s color-keyed blit not implemented for bpp %d!\n",
1399 (Flags
& WINEDDBLT_KEYSRC
) ? "Source" : "Destination", bpp
*8);
1400 ret
= WINED3DERR_NOTAVAILABLE
;
1402 #undef COPY_COLORKEY_FX
1408 if (Flags
&& FIXME_ON(d3d_surface
))
1410 FIXME("\tUnsupported flags: %08x\n", Flags
);
1414 IWineD3DSurface_UnlockRect(iface
);
1415 if (Src
&& Src
!= This
) IWineD3DSurface_UnlockRect((IWineD3DSurface
*) Src
);
1416 /* Release the converted surface if any */
1417 if (Src
&& SrcSurface
!= (IWineD3DSurface
*) Src
) IWineD3DSurface_Release((IWineD3DSurface
*) Src
);
1421 /*****************************************************************************
1422 * IWineD3DSurface::BltFast, SW emulation version
1424 * This is the software implementation of BltFast, as used by GDI surfaces
1425 * and as a fallback for OpenGL surfaces. This code is taken from the old
1426 * DirectDraw code, and was originally written by TransGaming.
1431 * Source: Source surface to copy from
1432 * rsrc: Source rectangle
1436 * WINED3D_OK on success
1438 *****************************************************************************/
1440 IWineD3DBaseSurfaceImpl_BltFast(IWineD3DSurface
*iface
,
1443 IWineD3DSurface
*Source
,
1447 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*) iface
;
1448 IWineD3DSurfaceImpl
*Src
= (IWineD3DSurfaceImpl
*) Source
;
1450 int bpp
, w
, h
, x
, y
;
1451 WINED3DLOCKED_RECT dlock
,slock
;
1452 HRESULT ret
= WINED3D_OK
;
1454 RECT lock_src
, lock_dst
, lock_union
;
1456 const StaticPixelFormatDesc
*sEntry
, *dEntry
;
1458 if (TRACE_ON(d3d_surface
))
1460 TRACE("(%p)->(%d,%d,%p,%p,%08x)\n", This
,dstx
,dsty
,Src
,rsrc
,trans
);
1464 TRACE("\tsrcrect: %dx%d-%dx%d\n",rsrc
->left
,rsrc
->top
,
1465 rsrc
->right
,rsrc
->bottom
);
1469 TRACE(" srcrect: NULL\n");
1473 if ((This
->Flags
& SFLAG_LOCKED
) ||
1474 ((Src
!= NULL
) && (Src
->Flags
& SFLAG_LOCKED
)))
1476 WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
1477 return WINEDDERR_SURFACEBUSY
;
1482 WARN("rsrc is NULL!\n");
1486 rsrc
->right
= Src
->currentDesc
.Width
;
1487 rsrc
->bottom
= Src
->currentDesc
.Height
;
1490 /* Check source rect for validity. Copied from normal Blt. Fixes Baldur's Gate.*/
1491 if ((rsrc
->bottom
> Src
->currentDesc
.Height
) || (rsrc
->bottom
< 0) ||
1492 (rsrc
->top
> Src
->currentDesc
.Height
) || (rsrc
->top
< 0) ||
1493 (rsrc
->left
> Src
->currentDesc
.Width
) || (rsrc
->left
< 0) ||
1494 (rsrc
->right
> Src
->currentDesc
.Width
) || (rsrc
->right
< 0) ||
1495 (rsrc
->right
< rsrc
->left
) || (rsrc
->bottom
< rsrc
->top
))
1497 WARN("Application gave us bad source rectangle for BltFast.\n");
1498 return WINEDDERR_INVALIDRECT
;
1501 h
= rsrc
->bottom
- rsrc
->top
;
1502 if (h
> This
->currentDesc
.Height
-dsty
) h
= This
->currentDesc
.Height
-dsty
;
1503 if (h
> Src
->currentDesc
.Height
-rsrc
->top
) h
=Src
->currentDesc
.Height
-rsrc
->top
;
1504 if (h
<= 0) return WINEDDERR_INVALIDRECT
;
1506 w
= rsrc
->right
- rsrc
->left
;
1507 if (w
> This
->currentDesc
.Width
-dstx
) w
= This
->currentDesc
.Width
-dstx
;
1508 if (w
> Src
->currentDesc
.Width
-rsrc
->left
) w
= Src
->currentDesc
.Width
-rsrc
->left
;
1509 if (w
<= 0) return WINEDDERR_INVALIDRECT
;
1511 /* Now compute the locking rectangle... */
1512 lock_src
.left
= rsrc
->left
;
1513 lock_src
.top
= rsrc
->top
;
1514 lock_src
.right
= lock_src
.left
+ w
;
1515 lock_src
.bottom
= lock_src
.top
+ h
;
1517 lock_dst
.left
= dstx
;
1518 lock_dst
.top
= dsty
;
1519 lock_dst
.right
= dstx
+ w
;
1520 lock_dst
.bottom
= dsty
+ h
;
1522 bpp
= This
->bytesPerPixel
;
1524 /* We need to lock the surfaces, or we won't get refreshes when done. */
1529 UnionRect(&lock_union
, &lock_src
, &lock_dst
);
1531 /* Lock the union of the two rectangles */
1532 ret
= IWineD3DSurface_LockRect(iface
, &dlock
, &lock_union
, 0);
1533 if(ret
!= WINED3D_OK
) goto error
;
1535 pitch
= dlock
.Pitch
;
1536 slock
.Pitch
= dlock
.Pitch
;
1538 /* Since slock was originally copied from this surface's description, we can just reuse it */
1539 assert(This
->resource
.allocatedMemory
!= NULL
);
1540 sbuf
= This
->resource
.allocatedMemory
+ lock_src
.top
* pitch
+ lock_src
.left
* bpp
;
1541 dbuf
= This
->resource
.allocatedMemory
+ lock_dst
.top
* pitch
+ lock_dst
.left
* bpp
;
1542 sEntry
= getFormatDescEntry(Src
->resource
.format
, NULL
, NULL
);
1547 ret
= IWineD3DSurface_LockRect(Source
, &slock
, &lock_src
, WINED3DLOCK_READONLY
);
1548 if(ret
!= WINED3D_OK
) goto error
;
1549 ret
= IWineD3DSurface_LockRect(iface
, &dlock
, &lock_dst
, 0);
1550 if(ret
!= WINED3D_OK
) goto error
;
1554 TRACE("Dst is at %p, Src is at %p\n", dbuf
, sbuf
);
1556 sEntry
= getFormatDescEntry(Src
->resource
.format
, NULL
, NULL
);
1557 dEntry
= getFormatDescEntry(This
->resource
.format
, NULL
, NULL
);
1560 /* Handle first the FOURCC surfaces... */
1561 if (sEntry
->isFourcc
&& dEntry
->isFourcc
)
1563 TRACE("Fourcc -> Fourcc copy\n");
1565 FIXME("trans arg not supported when a FOURCC surface is involved\n");
1567 FIXME("offset for destination surface is not supported\n");
1568 if (Src
->resource
.format
!= This
->resource
.format
)
1570 FIXME("FOURCC->FOURCC copy only supported for the same type of surface\n");
1571 ret
= WINED3DERR_WRONGTEXTUREFORMAT
;
1574 /* FIXME: Watch out that the size is correct for FOURCC surfaces */
1575 memcpy(dbuf
, sbuf
, This
->resource
.size
);
1578 if (sEntry
->isFourcc
&& !dEntry
->isFourcc
)
1580 /* TODO: Use the libtxc_dxtn.so shared library to do
1581 * software decompression
1583 ERR("DXTC decompression not supported by now\n");
1587 if (trans
& (WINEDDBLTFAST_SRCCOLORKEY
| WINEDDBLTFAST_DESTCOLORKEY
))
1589 DWORD keylow
, keyhigh
;
1590 TRACE("Color keyed copy\n");
1591 if (trans
& WINEDDBLTFAST_SRCCOLORKEY
)
1593 keylow
= Src
->SrcBltCKey
.dwColorSpaceLowValue
;
1594 keyhigh
= Src
->SrcBltCKey
.dwColorSpaceHighValue
;
1598 /* I'm not sure if this is correct */
1599 FIXME("WINEDDBLTFAST_DESTCOLORKEY not fully supported yet.\n");
1600 keylow
= This
->DestBltCKey
.dwColorSpaceLowValue
;
1601 keyhigh
= This
->DestBltCKey
.dwColorSpaceHighValue
;
1604 #define COPYBOX_COLORKEY(type) { \
1606 s = (type *) sbuf; \
1607 d = (type *) dbuf; \
1608 for (y = 0; y < h; y++) { \
1609 for (x = 0; x < w; x++) { \
1611 if (tmp < keylow || tmp > keyhigh) d[x] = tmp; \
1613 s = (type *)((BYTE *)s + slock.Pitch); \
1614 d = (type *)((BYTE *)d + dlock.Pitch); \
1620 case 1: COPYBOX_COLORKEY(BYTE
)
1621 case 2: COPYBOX_COLORKEY(WORD
)
1622 case 4: COPYBOX_COLORKEY(DWORD
)
1629 for (y
= 0; y
< h
; y
++)
1631 for (x
= 0; x
< w
* 3; x
+= 3)
1633 tmp
= (DWORD
)s
[x
] + ((DWORD
)s
[x
+ 1] << 8) + ((DWORD
)s
[x
+ 2] << 16);
1634 if (tmp
< keylow
|| tmp
> keyhigh
)
1636 d
[x
+ 0] = s
[x
+ 0];
1637 d
[x
+ 1] = s
[x
+ 1];
1638 d
[x
+ 2] = s
[x
+ 2];
1647 FIXME("Source color key blitting not supported for bpp %d\n",bpp
*8);
1648 ret
= WINED3DERR_NOTAVAILABLE
;
1651 #undef COPYBOX_COLORKEY
1652 TRACE("Copy Done\n");
1656 int width
= w
* bpp
;
1657 INT sbufpitch
, dbufpitch
;
1659 TRACE("NO color key copy\n");
1660 /* Handle overlapping surfaces */
1663 sbuf
+= (h
- 1) * slock
.Pitch
;
1664 dbuf
+= (h
- 1) * dlock
.Pitch
;
1665 sbufpitch
= -slock
.Pitch
;
1666 dbufpitch
= -dlock
.Pitch
;
1670 sbufpitch
= slock
.Pitch
;
1671 dbufpitch
= dlock
.Pitch
;
1673 for (y
= 0; y
< h
; y
++)
1675 /* This is pretty easy, a line for line memcpy */
1676 memmove(dbuf
, sbuf
, width
);
1680 TRACE("Copy done\n");
1686 IWineD3DSurface_UnlockRect(iface
);
1690 IWineD3DSurface_UnlockRect(iface
);
1691 IWineD3DSurface_UnlockRect(Source
);
1697 HRESULT WINAPI
IWineD3DBaseSurfaceImpl_LockRect(IWineD3DSurface
*iface
, WINED3DLOCKED_RECT
* pLockedRect
, CONST RECT
* pRect
, DWORD Flags
)
1699 IWineD3DSurfaceImpl
*This
= (IWineD3DSurfaceImpl
*)iface
;
1701 TRACE("(%p) : rect@%p flags(%08x), output lockedRect@%p, memory@%p\n",
1702 This
, pRect
, Flags
, pLockedRect
, This
->resource
.allocatedMemory
);
1704 pLockedRect
->Pitch
= IWineD3DSurface_GetPitch(iface
);
1708 pLockedRect
->pBits
= This
->resource
.allocatedMemory
;
1709 This
->lockedRect
.left
= 0;
1710 This
->lockedRect
.top
= 0;
1711 This
->lockedRect
.right
= This
->currentDesc
.Width
;
1712 This
->lockedRect
.bottom
= This
->currentDesc
.Height
;
1714 TRACE("Locked Rect (%p) = l %d, t %d, r %d, b %d\n",
1715 &This
->lockedRect
, This
->lockedRect
.left
, This
->lockedRect
.top
,
1716 This
->lockedRect
.right
, This
->lockedRect
.bottom
);
1720 TRACE("Lock Rect (%p) = l %d, t %d, r %d, b %d\n",
1721 pRect
, pRect
->left
, pRect
->top
, pRect
->right
, pRect
->bottom
);
1723 /* DXTn textures are based on compressed blocks of 4x4 pixels, each
1724 * 16 bytes large (8 bytes in case of DXT1). Because of that Pitch has
1725 * slightly different meaning compared to regular textures. For DXTn
1726 * textures Pitch is the size of a row of blocks, 4 high and "width"
1727 * long. The x offset is calculated differently as well, since moving 4
1728 * pixels to the right actually moves an entire 4x4 block to right, ie
1729 * 16 bytes (8 in case of DXT1). */
1730 if (This
->resource
.format
== WINED3DFMT_DXT1
)
1732 pLockedRect
->pBits
= This
->resource
.allocatedMemory
+ (pLockedRect
->Pitch
* pRect
->top
/ 4) + (pRect
->left
* 2);
1734 else if (This
->resource
.format
== WINED3DFMT_DXT2
|| This
->resource
.format
== WINED3DFMT_DXT3
||
1735 This
->resource
.format
== WINED3DFMT_DXT4
|| This
->resource
.format
== WINED3DFMT_DXT5
)
1737 pLockedRect
->pBits
= This
->resource
.allocatedMemory
+ (pLockedRect
->Pitch
* pRect
->top
/ 4) + (pRect
->left
* 4);
1741 pLockedRect
->pBits
= This
->resource
.allocatedMemory
+
1742 (pLockedRect
->Pitch
* pRect
->top
) +
1743 (pRect
->left
* This
->bytesPerPixel
);
1745 This
->lockedRect
.left
= pRect
->left
;
1746 This
->lockedRect
.top
= pRect
->top
;
1747 This
->lockedRect
.right
= pRect
->right
;
1748 This
->lockedRect
.bottom
= pRect
->bottom
;
1751 /* No dirtifying is needed for this surface implementation */
1752 TRACE("returning memory@%p, pitch(%d)\n", pLockedRect
->pBits
, pLockedRect
->Pitch
);
1757 void WINAPI
IWineD3DBaseSurfaceImpl_BindTexture(IWineD3DSurface
*iface
) {
1758 ERR("Should not be called on base texture\n");