winejoystick.drv: Use CP_UNIXCP instead of CP_ACP when converting a string that comes...
[wine] / dlls / wined3d / volume.c
1 /*
2  * IWineD3DVolume implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
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.
12  *
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.
17  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wined3d_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
27 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
28
29 static void volume_bind_and_dirtify(IWineD3DVolume *iface) {
30     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
31     IWineD3DVolumeTexture *texture;
32     int active_sampler;
33
34     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
35      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
36      * gl states. The current texture unit should always be a valid one.
37      *
38      * To be more specific, this is tricky because we can implicitly be called
39      * from sampler() in state.c. This means we can't touch anything other than
40      * whatever happens to be the currently active texture, or we would risk
41      * marking already applied sampler states dirty again.
42      *
43      * TODO: Track the current active texture per GL context instead of using glGet
44      */
45     if (GL_SUPPORT(ARB_MULTITEXTURE)) {
46         GLint active_texture;
47         ENTER_GL();
48         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
49         LEAVE_GL();
50         active_sampler = This->resource.wineD3DDevice->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
51     } else {
52         active_sampler = 0;
53     }
54
55     if (active_sampler != -1) {
56         IWineD3DDeviceImpl_MarkStateDirty(This->resource.wineD3DDevice, STATE_SAMPLER(active_sampler));
57     }
58
59     if (SUCCEEDED(IWineD3DSurface_GetContainer(iface, &IID_IWineD3DVolumeTexture, (void **)&texture))) {
60         IWineD3DVolumeTexture_BindTexture(texture, FALSE);
61         IWineD3DVolumeTexture_Release(texture);
62     } else {
63         ERR("Volume should be part of a volume texture\n");
64     }
65 }
66
67 void volume_add_dirty_box(IWineD3DVolume *iface, const WINED3DBOX *dirty_box)
68 {
69     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
70
71     This->dirty = TRUE;
72     if (dirty_box)
73     {
74         This->lockedBox.Left = min(This->lockedBox.Left, dirty_box->Left);
75         This->lockedBox.Top = min(This->lockedBox.Top, dirty_box->Top);
76         This->lockedBox.Front = min(This->lockedBox.Front, dirty_box->Front);
77         This->lockedBox.Right = max(This->lockedBox.Right, dirty_box->Right);
78         This->lockedBox.Bottom = max(This->lockedBox.Bottom, dirty_box->Bottom);
79         This->lockedBox.Back = max(This->lockedBox.Back, dirty_box->Back);
80     }
81     else
82     {
83         This->lockedBox.Left = 0;
84         This->lockedBox.Top = 0;
85         This->lockedBox.Front = 0;
86         This->lockedBox.Right = This->currentDesc.Width;
87         This->lockedBox.Bottom = This->currentDesc.Height;
88         This->lockedBox.Back = This->currentDesc.Depth;
89     }
90 }
91
92 /* *******************************************
93    IWineD3DVolume IUnknown parts follow
94    ******************************************* */
95 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
96 {
97     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
98     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_IWineD3DVolume)){
102         IUnknown_AddRef(iface);
103         *ppobj = This;
104         return S_OK;
105     }
106     *ppobj = NULL;
107     return E_NOINTERFACE;
108 }
109
110 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
111     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
112     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
113     return InterlockedIncrement(&This->resource.ref);
114 }
115
116 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
117     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
118     ULONG ref;
119     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
120     ref = InterlockedDecrement(&This->resource.ref);
121     if (ref == 0) {
122         resource_cleanup((IWineD3DResource *)iface);
123         HeapFree(GetProcessHeap(), 0, This);
124     }
125     return ref;
126 }
127
128 /* ****************************************************
129    IWineD3DVolume IWineD3DResource parts follow
130    **************************************************** */
131 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
132     return resource_get_parent((IWineD3DResource *)iface, pParent);
133 }
134
135 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
136     return resource_get_device((IWineD3DResource *)iface, ppDevice);
137 }
138
139 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
140     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
141 }
142
143 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID  refguid, void* pData, DWORD* pSizeOfData) {
144     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
145 }
146
147 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
148     return resource_free_private_data((IWineD3DResource *)iface, refguid);
149 }
150
151 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
152     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
153 }
154
155 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
156     return resource_get_priority((IWineD3DResource *)iface);
157 }
158
159 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
160     FIXME("iface %p stub!\n", iface);
161 }
162
163 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
164     /* The whole content is shadowed on This->resource.allocatedMemory, and the
165      * texture name is managed by the VolumeTexture container
166      */
167     TRACE("(%p): Nothing to do\n", iface);
168 }
169
170 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
171     return resource_get_type((IWineD3DResource *)iface);
172 }
173
174 /* *******************************************
175    IWineD3DVolume parts follow
176    ******************************************* */
177 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
178     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
179
180     TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
181
182     if (!ppContainer) {
183         ERR("Called without a valid ppContainer.\n");
184     }
185
186     /* Although surfaces can be standalone, volumes can't */
187     if (!This->container) {
188         ERR("Volume without an container. Should not happen.\n");
189     }
190
191     TRACE("Relaying to QueryInterface\n");
192     return IUnknown_QueryInterface(This->container, riid, ppContainer);
193 }
194
195 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
196     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
197     TRACE("(%p) : copying into %p\n", This, pDesc);
198
199     *(pDesc->Format)  = This->resource.format;
200     *(pDesc->Type)    = This->resource.resourceType;
201     *(pDesc->Usage)   = This->resource.usage;
202     *(pDesc->Pool)    = This->resource.pool;
203     *(pDesc->Size)    = This->resource.size; /* dx8 only */
204     *(pDesc->Width)   = This->currentDesc.Width;
205     *(pDesc->Height)  = This->currentDesc.Height;
206     *(pDesc->Depth)   = This->currentDesc.Depth;
207     return WINED3D_OK;
208 }
209
210 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
211     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
212     FIXME("(%p) : pBox=%p stub\n", This, pBox);
213
214     if(!This->resource.allocatedMemory) {
215         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
216     }
217
218     /* fixme: should we really lock as such? */
219     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
220
221     pLockedVolume->RowPitch   = This->bytesPerPixel * This->currentDesc.Width;                        /* Bytes / row   */
222     pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height;  /* Bytes / slice */
223     if (!pBox) {
224         TRACE("No box supplied - all is ok\n");
225         pLockedVolume->pBits = This->resource.allocatedMemory;
226         This->lockedBox.Left   = 0;
227         This->lockedBox.Top    = 0;
228         This->lockedBox.Front  = 0;
229         This->lockedBox.Right  = This->currentDesc.Width;
230         This->lockedBox.Bottom = This->currentDesc.Height;
231         This->lockedBox.Back   = This->currentDesc.Depth;
232     } else {
233         TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n", pBox, pBox->Left, pBox->Top, pBox->Right, pBox->Bottom, pBox->Front, pBox->Back);
234         pLockedVolume->pBits = This->resource.allocatedMemory +
235           (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
236           (pLockedVolume->RowPitch * pBox->Top) +
237           (pBox->Left * This->bytesPerPixel);
238         This->lockedBox.Left   = pBox->Left;
239         This->lockedBox.Top    = pBox->Top;
240         This->lockedBox.Front  = pBox->Front;
241         This->lockedBox.Right  = pBox->Right;
242         This->lockedBox.Bottom = pBox->Bottom;
243         This->lockedBox.Back   = pBox->Back;
244     }
245
246     if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
247       /* Don't dirtify */
248     } else {
249       /**
250        * Dirtify on lock
251        * as seen in msdn docs
252        */
253       volume_add_dirty_box(iface, &This->lockedBox);
254
255       /**  Dirtify Container if needed */
256       if (NULL != This->container) {
257
258         IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
259         WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
260
261         if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
262           IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
263           pTexture->baseTexture.dirty = TRUE;
264         } else {
265           FIXME("Set dirty on container type %d\n", containerType);
266         }
267       }
268     }
269
270     This->locked = TRUE;
271     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
272     return WINED3D_OK;
273 }
274
275 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
276     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
277     if (!This->locked) {
278       ERR("trying to lock unlocked volume@%p\n", This);
279       return WINED3DERR_INVALIDCALL;
280     }
281     TRACE("(%p) : unlocking volume\n", This);
282     This->locked = FALSE;
283     memset(&This->lockedBox, 0, sizeof(RECT));
284     return WINED3D_OK;
285 }
286
287 /* Internal use functions follow : */
288
289 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
290     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
291
292     TRACE("This %p, container %p\n", This, container);
293
294     /* We can't keep a reference to the container, since the container already keeps a reference to us. */
295
296     TRACE("Setting container to %p from %p\n", container, This->container);
297     This->container = container;
298
299     return WINED3D_OK;
300 }
301
302 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
303     IWineD3DVolumeImpl *This     = (IWineD3DVolumeImpl *)iface;
304     WINED3DFORMAT format = This->resource.format;
305     const struct GlPixelFormatDesc *glDesc;
306     getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
307
308     TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(format), format);
309
310     volume_bind_and_dirtify(iface);
311
312     TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
313             GL_TEXTURE_3D,
314             gl_level,
315             glDesc->glInternal,
316             This->currentDesc.Width,
317             This->currentDesc.Height,
318             This->currentDesc.Depth,
319             0,
320             glDesc->glFormat,
321             glDesc->glType,
322             This->resource.allocatedMemory);
323
324     ENTER_GL();
325     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
326                 gl_level,
327                 glDesc->glInternal,
328                 This->currentDesc.Width,
329                 This->currentDesc.Height,
330                 This->currentDesc.Depth,
331                 0,
332                 glDesc->glFormat,
333                 glDesc->glType,
334                 This->resource.allocatedMemory));
335     checkGLcall("glTexImage3D");
336     LEAVE_GL();
337
338     /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
339      * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
340      * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
341      */
342     return WINED3D_OK;
343
344 }
345
346 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
347 {
348     /* IUnknown */
349     IWineD3DVolumeImpl_QueryInterface,
350     IWineD3DVolumeImpl_AddRef,
351     IWineD3DVolumeImpl_Release,
352     /* IWineD3DResource */
353     IWineD3DVolumeImpl_GetParent,
354     IWineD3DVolumeImpl_GetDevice,
355     IWineD3DVolumeImpl_SetPrivateData,
356     IWineD3DVolumeImpl_GetPrivateData,
357     IWineD3DVolumeImpl_FreePrivateData,
358     IWineD3DVolumeImpl_SetPriority,
359     IWineD3DVolumeImpl_GetPriority,
360     IWineD3DVolumeImpl_PreLoad,
361     IWineD3DVolumeImpl_UnLoad,
362     IWineD3DVolumeImpl_GetType,
363     /* IWineD3DVolume */
364     IWineD3DVolumeImpl_GetContainer,
365     IWineD3DVolumeImpl_GetDesc,
366     IWineD3DVolumeImpl_LockBox,
367     IWineD3DVolumeImpl_UnlockBox,
368     /* Internal interface */
369     IWineD3DVolumeImpl_LoadTexture,
370     IWineD3DVolumeImpl_SetContainer
371 };