Visual C++ backend for rbuild (for now just a hacked mingw backend) and related compi...
[reactos.git] / dll / directx / wine / 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);
61 IWineD3DVolumeTexture_Release(texture);
62 } else {
63 ERR("Volume should be part of a volume texture\n");
64 }
65 }
66
67 /* *******************************************
68 IWineD3DVolume IUnknown parts follow
69 ******************************************* */
70 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, LPVOID *ppobj)
71 {
72 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
73 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
74 if (IsEqualGUID(riid, &IID_IUnknown)
75 || IsEqualGUID(riid, &IID_IWineD3DBase)
76 || IsEqualGUID(riid, &IID_IWineD3DVolume)){
77 IUnknown_AddRef(iface);
78 *ppobj = This;
79 return S_OK;
80 }
81 *ppobj = NULL;
82 return E_NOINTERFACE;
83 }
84
85 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
86 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
87 TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
88 return InterlockedIncrement(&This->resource.ref);
89 }
90
91 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
92 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
93 ULONG ref;
94 TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
95 ref = InterlockedDecrement(&This->resource.ref);
96 if (ref == 0) {
97 IWineD3DResourceImpl_CleanUp((IWineD3DResource *)iface);
98 HeapFree(GetProcessHeap(), 0, This);
99 }
100 return ref;
101 }
102
103 /* ****************************************************
104 IWineD3DVolume IWineD3DResource parts follow
105 **************************************************** */
106 static HRESULT WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface, IUnknown **pParent) {
107 return IWineD3DResourceImpl_GetParent((IWineD3DResource *)iface, pParent);
108 }
109
110 static HRESULT WINAPI IWineD3DVolumeImpl_GetDevice(IWineD3DVolume *iface, IWineD3DDevice** ppDevice) {
111 return IWineD3DResourceImpl_GetDevice((IWineD3DResource *)iface, ppDevice);
112 }
113
114 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
115 return IWineD3DResourceImpl_SetPrivateData((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
116 }
117
118 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
119 return IWineD3DResourceImpl_GetPrivateData((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
120 }
121
122 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid) {
123 return IWineD3DResourceImpl_FreePrivateData((IWineD3DResource *)iface, refguid);
124 }
125
126 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD PriorityNew) {
127 return IWineD3DResourceImpl_SetPriority((IWineD3DResource *)iface, PriorityNew);
128 }
129
130 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface) {
131 return IWineD3DResourceImpl_GetPriority((IWineD3DResource *)iface);
132 }
133
134 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
135 IWineD3DResourceImpl_PreLoad((IWineD3DResource *)iface);
136 }
137
138 static void WINAPI IWineD3DVolumeImpl_UnLoad(IWineD3DVolume *iface) {
139 /* The whole content is shadowed on This->resource.allocatedMemory, and the
140 * texture name is managed by the VolumeTexture container
141 */
142 TRACE("(%p): Nothing to do\n", iface);
143 }
144
145 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface) {
146 return IWineD3DResourceImpl_GetType((IWineD3DResource *)iface);
147 }
148
149 /* *******************************************
150 IWineD3DVolume parts follow
151 ******************************************* */
152 static HRESULT WINAPI IWineD3DVolumeImpl_GetContainer(IWineD3DVolume *iface, REFIID riid, void** ppContainer) {
153 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
154
155 TRACE("(This %p, riid %s, ppContainer %p)\n", This, debugstr_guid(riid), ppContainer);
156
157 if (!ppContainer) {
158 ERR("Called without a valid ppContainer.\n");
159 }
160
161 /* Although surfaces can be standalone, volumes can't */
162 if (!This->container) {
163 ERR("Volume without an container. Should not happen.\n");
164 }
165
166 TRACE("Relaying to QueryInterface\n");
167 return IUnknown_QueryInterface(This->container, riid, ppContainer);
168 }
169
170 static HRESULT WINAPI IWineD3DVolumeImpl_GetDesc(IWineD3DVolume *iface, WINED3DVOLUME_DESC* pDesc) {
171 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
172 TRACE("(%p) : copying into %p\n", This, pDesc);
173
174 *(pDesc->Format) = This->resource.format;
175 *(pDesc->Type) = This->resource.resourceType;
176 *(pDesc->Usage) = This->resource.usage;
177 *(pDesc->Pool) = This->resource.pool;
178 *(pDesc->Size) = This->resource.size; /* dx8 only */
179 *(pDesc->Width) = This->currentDesc.Width;
180 *(pDesc->Height) = This->currentDesc.Height;
181 *(pDesc->Depth) = This->currentDesc.Depth;
182 return WINED3D_OK;
183 }
184
185 static HRESULT WINAPI IWineD3DVolumeImpl_LockBox(IWineD3DVolume *iface, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
186 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
187 FIXME("(%p) : pBox=%p stub\n", This, pBox);
188
189 if(!This->resource.allocatedMemory) {
190 This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
191 }
192
193 /* fixme: should we really lock as such? */
194 TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
195
196 pLockedVolume->RowPitch = This->bytesPerPixel * This->currentDesc.Width; /* Bytes / row */
197 pLockedVolume->SlicePitch = This->bytesPerPixel * This->currentDesc.Width * This->currentDesc.Height; /* Bytes / slice */
198 if (!pBox) {
199 TRACE("No box supplied - all is ok\n");
200 pLockedVolume->pBits = This->resource.allocatedMemory;
201 This->lockedBox.Left = 0;
202 This->lockedBox.Top = 0;
203 This->lockedBox.Front = 0;
204 This->lockedBox.Right = This->currentDesc.Width;
205 This->lockedBox.Bottom = This->currentDesc.Height;
206 This->lockedBox.Back = This->currentDesc.Depth;
207 } else {
208 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);
209 pLockedVolume->pBits = This->resource.allocatedMemory +
210 (pLockedVolume->SlicePitch * pBox->Front) + /* FIXME: is front < back or vica versa? */
211 (pLockedVolume->RowPitch * pBox->Top) +
212 (pBox->Left * This->bytesPerPixel);
213 This->lockedBox.Left = pBox->Left;
214 This->lockedBox.Top = pBox->Top;
215 This->lockedBox.Front = pBox->Front;
216 This->lockedBox.Right = pBox->Right;
217 This->lockedBox.Bottom = pBox->Bottom;
218 This->lockedBox.Back = pBox->Back;
219 }
220
221 if (Flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)) {
222 /* Don't dirtify */
223 } else {
224 /**
225 * Dirtify on lock
226 * as seen in msdn docs
227 */
228 IWineD3DVolume_AddDirtyBox(iface, &This->lockedBox);
229
230 /** Dirtify Container if needed */
231 if (NULL != This->container) {
232
233 IWineD3DVolumeTexture *cont = (IWineD3DVolumeTexture*) This->container;
234 WINED3DRESOURCETYPE containerType = IWineD3DBaseTexture_GetType((IWineD3DBaseTexture *) cont);
235
236 if (containerType == WINED3DRTYPE_VOLUMETEXTURE) {
237 IWineD3DBaseTextureImpl* pTexture = (IWineD3DBaseTextureImpl*) cont;
238 pTexture->baseTexture.dirty = TRUE;
239 } else {
240 FIXME("Set dirty on container type %d\n", containerType);
241 }
242 }
243 }
244
245 This->locked = TRUE;
246 TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
247 return WINED3D_OK;
248 }
249
250 static HRESULT WINAPI IWineD3DVolumeImpl_UnlockBox(IWineD3DVolume *iface) {
251 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
252 if (!This->locked) {
253 ERR("trying to lock unlocked volume@%p\n", This);
254 return WINED3DERR_INVALIDCALL;
255 }
256 TRACE("(%p) : unlocking volume\n", This);
257 This->locked = FALSE;
258 memset(&This->lockedBox, 0, sizeof(RECT));
259 return WINED3D_OK;
260 }
261
262 /* Internal use functions follow : */
263
264 static HRESULT WINAPI IWineD3DVolumeImpl_CleanDirtyBox(IWineD3DVolume *iface) {
265 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
266 This->dirty = FALSE;
267 This->lockedBox.Left = This->currentDesc.Width;
268 This->lockedBox.Top = This->currentDesc.Height;
269 This->lockedBox.Front = This->currentDesc.Depth;
270 This->lockedBox.Right = 0;
271 This->lockedBox.Bottom = 0;
272 This->lockedBox.Back = 0;
273 return WINED3D_OK;
274 }
275
276 static HRESULT WINAPI IWineD3DVolumeImpl_AddDirtyBox(IWineD3DVolume *iface, CONST WINED3DBOX* pDirtyBox) {
277 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
278 This->dirty = TRUE;
279 if (NULL != pDirtyBox) {
280 This->lockedBox.Left = min(This->lockedBox.Left, pDirtyBox->Left);
281 This->lockedBox.Top = min(This->lockedBox.Top, pDirtyBox->Top);
282 This->lockedBox.Front = min(This->lockedBox.Front, pDirtyBox->Front);
283 This->lockedBox.Right = max(This->lockedBox.Right, pDirtyBox->Right);
284 This->lockedBox.Bottom = max(This->lockedBox.Bottom, pDirtyBox->Bottom);
285 This->lockedBox.Back = max(This->lockedBox.Back, pDirtyBox->Back);
286 } else {
287 This->lockedBox.Left = 0;
288 This->lockedBox.Top = 0;
289 This->lockedBox.Front = 0;
290 This->lockedBox.Right = This->currentDesc.Width;
291 This->lockedBox.Bottom = This->currentDesc.Height;
292 This->lockedBox.Back = This->currentDesc.Depth;
293 }
294 return WINED3D_OK;
295 }
296
297 static HRESULT WINAPI IWineD3DVolumeImpl_SetContainer(IWineD3DVolume *iface, IWineD3DBase* container) {
298 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
299
300 TRACE("This %p, container %p\n", This, container);
301
302 /* We can't keep a reference to the container, since the container already keeps a reference to us. */
303
304 TRACE("Setting container to %p from %p\n", container, This->container);
305 This->container = container;
306
307 return WINED3D_OK;
308 }
309
310 static HRESULT WINAPI IWineD3DVolumeImpl_LoadTexture(IWineD3DVolume *iface, int gl_level, BOOL srgb_mode) {
311 IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
312 WINED3DFORMAT format = This->resource.format;
313 const GlPixelFormatDesc *glDesc;
314 getFormatDescEntry(format, &GLINFO_LOCATION, &glDesc);
315
316 TRACE("(%p) : level %u, format %s (0x%08x)\n", This, gl_level, debug_d3dformat(format), format);
317
318 volume_bind_and_dirtify(iface);
319
320 TRACE("Calling glTexImage3D %x level=%d, intfmt=%x, w=%d, h=%d,d=%d, 0=%d, glFmt=%x, glType=%x, Mem=%p\n",
321 GL_TEXTURE_3D,
322 gl_level,
323 glDesc->glInternal,
324 This->currentDesc.Width,
325 This->currentDesc.Height,
326 This->currentDesc.Depth,
327 0,
328 glDesc->glFormat,
329 glDesc->glType,
330 This->resource.allocatedMemory);
331 GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D,
332 gl_level,
333 glDesc->glInternal,
334 This->currentDesc.Width,
335 This->currentDesc.Height,
336 This->currentDesc.Depth,
337 0,
338 glDesc->glFormat,
339 glDesc->glType,
340 This->resource.allocatedMemory));
341 checkGLcall("glTexImage3D");
342
343 /* When adding code releasing This->resource.allocatedMemory to save data keep in mind that
344 * GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by default if supported(GL_APPLE_client_storage).
345 * Thus do not release This->resource.allocatedMemory if GL_APPLE_client_storage is supported.
346 */
347 return WINED3D_OK;
348
349 }
350
351 const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
352 {
353 /* IUnknown */
354 IWineD3DVolumeImpl_QueryInterface,
355 IWineD3DVolumeImpl_AddRef,
356 IWineD3DVolumeImpl_Release,
357 /* IWineD3DResource */
358 IWineD3DVolumeImpl_GetParent,
359 IWineD3DVolumeImpl_GetDevice,
360 IWineD3DVolumeImpl_SetPrivateData,
361 IWineD3DVolumeImpl_GetPrivateData,
362 IWineD3DVolumeImpl_FreePrivateData,
363 IWineD3DVolumeImpl_SetPriority,
364 IWineD3DVolumeImpl_GetPriority,
365 IWineD3DVolumeImpl_PreLoad,
366 IWineD3DVolumeImpl_UnLoad,
367 IWineD3DVolumeImpl_GetType,
368 /* IWineD3DVolume */
369 IWineD3DVolumeImpl_GetContainer,
370 IWineD3DVolumeImpl_GetDesc,
371 IWineD3DVolumeImpl_LockBox,
372 IWineD3DVolumeImpl_UnlockBox,
373 /* Internal interface */
374 IWineD3DVolumeImpl_AddDirtyBox,
375 IWineD3DVolumeImpl_CleanDirtyBox,
376 IWineD3DVolumeImpl_LoadTexture,
377 IWineD3DVolumeImpl_SetContainer
378 };