wined3d: Create a relative addressing token for 1.x shaders in the frontend.
[wine] / dlls / d3d9 / volumetexture.c
1 /*
2  * IDirect3DVolumeTexture9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  *                     Raphael Junqueira
6  *
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.
11  *
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.
16  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3DVolumeTexture9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_QueryInterface(LPDIRECT3DVOLUMETEXTURE9 iface, REFIID riid, LPVOID* ppobj) {
29     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
30
31     if (IsEqualGUID(riid, &IID_IUnknown)
32     || IsEqualGUID(riid, &IID_IDirect3DResource9)
33     || IsEqualGUID(riid, &IID_IDirect3DBaseTexture9)
34     || IsEqualGUID(riid, &IID_IDirect3DVolumeTexture9)) {
35         IDirect3DVolumeTexture9_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DVolumeTexture9Impl_AddRef(LPDIRECT3DVOLUMETEXTURE9 iface) {
46     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50
51     return ref;
52 }
53
54 static ULONG WINAPI IDirect3DVolumeTexture9Impl_Release(LPDIRECT3DVOLUMETEXTURE9 iface) {
55     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
56     ULONG ref = InterlockedDecrement(&This->ref);
57
58     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
59
60     if (ref == 0) {
61         EnterCriticalSection(&d3d9_cs);
62         IWineD3DVolumeTexture_Destroy(This->wineD3DVolumeTexture, D3D9CB_DestroyVolume);
63         LeaveCriticalSection(&d3d9_cs);
64         IDirect3DDevice9Ex_Release(This->parentDevice);
65         HeapFree(GetProcessHeap(), 0, This);
66     }
67     return ref;
68 }
69
70 /* IDirect3DVolumeTexture9 IDirect3DResource9 Interface follow: */
71 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetDevice(LPDIRECT3DVOLUMETEXTURE9 iface, IDirect3DDevice9** ppDevice) {
72     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
73     IWineD3DDevice *wined3d_device;
74     HRESULT hr;
75
76     TRACE("(%p) Relay\n", This);
77
78     EnterCriticalSection(&d3d9_cs);
79     hr = IWineD3DStateBlock_GetDevice(This->wineD3DVolumeTexture, &wined3d_device);
80     if (SUCCEEDED(hr))
81     {
82         IWineD3DDevice_GetParent(wined3d_device, (IUnknown **)ppDevice);
83         IWineD3DDevice_Release(wined3d_device);
84     }
85     LeaveCriticalSection(&d3d9_cs);
86
87     return hr;
88 }
89
90 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_SetPrivateData(LPDIRECT3DVOLUMETEXTURE9 iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
91     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
92     HRESULT hr;
93
94     TRACE("(%p) Relay\n", This);
95
96     EnterCriticalSection(&d3d9_cs);
97
98     hr = IWineD3DVolumeTexture_SetPrivateData(This->wineD3DVolumeTexture, refguid, pData, SizeOfData, Flags);
99
100     LeaveCriticalSection(&d3d9_cs);
101
102     return hr;
103 }
104
105 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetPrivateData(LPDIRECT3DVOLUMETEXTURE9 iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
106     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
107     HRESULT hr;
108
109     TRACE("(%p) Relay\n", This);
110
111     EnterCriticalSection(&d3d9_cs);
112
113     hr = IWineD3DVolumeTexture_GetPrivateData(This->wineD3DVolumeTexture, refguid, pData, pSizeOfData);
114
115     LeaveCriticalSection(&d3d9_cs);
116
117     return hr;
118 }
119
120 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_FreePrivateData(LPDIRECT3DVOLUMETEXTURE9 iface, REFGUID refguid) {
121     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
122     HRESULT hr;
123
124     TRACE("(%p) Relay\n", This);
125
126     EnterCriticalSection(&d3d9_cs);
127
128     hr = IWineD3DVolumeTexture_FreePrivateData(This->wineD3DVolumeTexture, refguid);
129
130     LeaveCriticalSection(&d3d9_cs);
131
132     return hr;
133 }
134
135 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetPriority(LPDIRECT3DVOLUMETEXTURE9 iface, DWORD PriorityNew) {
136     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
137     DWORD priority;
138
139     TRACE("(%p) Relay\n", This);
140
141     EnterCriticalSection(&d3d9_cs);
142
143     priority = IWineD3DVolumeTexture_SetPriority(This->wineD3DVolumeTexture, PriorityNew);
144
145     LeaveCriticalSection(&d3d9_cs);
146
147     return priority;
148 }
149
150 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetPriority(LPDIRECT3DVOLUMETEXTURE9 iface) {
151     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
152     DWORD priority;
153
154     TRACE("(%p) Relay\n", This);
155
156     EnterCriticalSection(&d3d9_cs);
157
158     priority = IWineD3DVolumeTexture_GetPriority(This->wineD3DVolumeTexture);
159
160     LeaveCriticalSection(&d3d9_cs);
161
162     return priority;
163 }
164
165 static void WINAPI IDirect3DVolumeTexture9Impl_PreLoad(LPDIRECT3DVOLUMETEXTURE9 iface) {
166     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
167
168     TRACE("(%p) Relay\n", This);
169
170     EnterCriticalSection(&d3d9_cs);
171
172     IWineD3DVolumeTexture_PreLoad(This->wineD3DVolumeTexture);
173
174     LeaveCriticalSection(&d3d9_cs);
175 }
176
177 static D3DRESOURCETYPE WINAPI IDirect3DVolumeTexture9Impl_GetType(LPDIRECT3DVOLUMETEXTURE9 iface) {
178     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
179     D3DRESOURCETYPE type;
180
181     TRACE("(%p) Relay\n", This);
182
183     EnterCriticalSection(&d3d9_cs);
184
185     type = IWineD3DVolumeTexture_GetType(This->wineD3DVolumeTexture);
186
187     LeaveCriticalSection(&d3d9_cs);
188
189     return type;
190 }
191
192 /* IDirect3DVolumeTexture9 IDirect3DBaseTexture9 Interface follow: */
193 static DWORD WINAPI IDirect3DVolumeTexture9Impl_SetLOD(LPDIRECT3DVOLUMETEXTURE9 iface, DWORD LODNew) {
194     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
195     DWORD lod;
196
197     TRACE("(%p) Relay\n", This);
198
199     EnterCriticalSection(&d3d9_cs);
200
201     lod = IWineD3DVolumeTexture_SetLOD(This->wineD3DVolumeTexture, LODNew);
202
203     LeaveCriticalSection(&d3d9_cs);
204
205     return lod;
206 }
207
208 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLOD(LPDIRECT3DVOLUMETEXTURE9 iface) {
209     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
210     DWORD lod;
211
212     TRACE("(%p) Relay\n", This);
213
214     EnterCriticalSection(&d3d9_cs);
215
216     lod = IWineD3DVolumeTexture_GetLOD(This->wineD3DVolumeTexture);
217
218     LeaveCriticalSection(&d3d9_cs);
219
220     return lod;
221 }
222
223 static DWORD WINAPI IDirect3DVolumeTexture9Impl_GetLevelCount(LPDIRECT3DVOLUMETEXTURE9 iface) {
224     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
225     DWORD level_count;
226
227     TRACE("(%p) Relay\n", This);
228
229     EnterCriticalSection(&d3d9_cs);
230
231     level_count = IWineD3DVolumeTexture_GetLevelCount(This->wineD3DVolumeTexture);
232
233     LeaveCriticalSection(&d3d9_cs);
234
235     return level_count;
236 }
237
238 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_SetAutoGenFilterType(LPDIRECT3DVOLUMETEXTURE9 iface, D3DTEXTUREFILTERTYPE FilterType) {
239     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
240     HRESULT hr;
241
242     TRACE("(%p) Relay\n", This);
243
244     EnterCriticalSection(&d3d9_cs);
245
246     hr = IWineD3DVolumeTexture_SetAutoGenFilterType(This->wineD3DVolumeTexture, (WINED3DTEXTUREFILTERTYPE) FilterType);
247
248     LeaveCriticalSection(&d3d9_cs);
249
250     return hr;
251 }
252
253 static D3DTEXTUREFILTERTYPE WINAPI IDirect3DVolumeTexture9Impl_GetAutoGenFilterType(LPDIRECT3DVOLUMETEXTURE9 iface) {
254     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
255     D3DTEXTUREFILTERTYPE filter_type;
256
257     TRACE("(%p) Relay\n", This);
258
259     EnterCriticalSection(&d3d9_cs);
260
261     filter_type = (D3DTEXTUREFILTERTYPE)IWineD3DVolumeTexture_GetAutoGenFilterType(This->wineD3DVolumeTexture);
262
263     LeaveCriticalSection(&d3d9_cs);
264
265     return filter_type;
266 }
267
268 static void WINAPI IDirect3DVolumeTexture9Impl_GenerateMipSubLevels(LPDIRECT3DVOLUMETEXTURE9 iface) {
269     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
270
271     TRACE("(%p) Relay\n", This);
272
273     EnterCriticalSection(&d3d9_cs);
274
275     IWineD3DVolumeTexture_GenerateMipSubLevels(This->wineD3DVolumeTexture);
276
277     LeaveCriticalSection(&d3d9_cs);
278 }
279
280 /* IDirect3DVolumeTexture9 Interface follow: */
281 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetLevelDesc(LPDIRECT3DVOLUMETEXTURE9 iface, UINT Level, D3DVOLUME_DESC* pDesc) {
282     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
283     WINED3DVOLUME_DESC     wined3ddesc;
284     UINT                   tmpInt = -1;
285     WINED3DFORMAT format;
286     HRESULT hr;
287
288     TRACE("(%p) Relay\n", This);
289
290     /* As d3d8 and d3d9 structures differ, pass in ptrs to where data needs to go */
291     wined3ddesc.Format              = &format;
292     wined3ddesc.Type                = (WINED3DRESOURCETYPE *)&pDesc->Type;
293     wined3ddesc.Usage               = &pDesc->Usage;
294     wined3ddesc.Pool                = (WINED3DPOOL *) &pDesc->Pool;
295     wined3ddesc.Size                = &tmpInt;
296     wined3ddesc.Width               = &pDesc->Width;
297     wined3ddesc.Height              = &pDesc->Height;
298     wined3ddesc.Depth               = &pDesc->Depth;
299
300     EnterCriticalSection(&d3d9_cs);
301
302     hr = IWineD3DVolumeTexture_GetLevelDesc(This->wineD3DVolumeTexture, Level, &wined3ddesc);
303
304     LeaveCriticalSection(&d3d9_cs);
305
306     if (SUCCEEDED(hr)) pDesc->Format = d3dformat_from_wined3dformat(format);
307
308     return hr;
309 }
310
311 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_GetVolumeLevel(LPDIRECT3DVOLUMETEXTURE9 iface, UINT Level, IDirect3DVolume9** ppVolumeLevel) {
312     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
313     HRESULT hrc = D3D_OK;
314     IWineD3DVolume *myVolume = NULL;
315
316     TRACE("(%p) Relay\n", This);
317
318     EnterCriticalSection(&d3d9_cs);
319
320     hrc = IWineD3DVolumeTexture_GetVolumeLevel(This->wineD3DVolumeTexture, Level, &myVolume);
321     if (hrc == D3D_OK && NULL != ppVolumeLevel) {
322        IWineD3DVolumeTexture_GetParent(myVolume, (IUnknown **)ppVolumeLevel);
323        IWineD3DVolumeTexture_Release(myVolume);
324     }
325
326     LeaveCriticalSection(&d3d9_cs);
327
328     return hrc;
329 }
330
331 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_LockBox(LPDIRECT3DVOLUMETEXTURE9 iface, UINT Level, D3DLOCKED_BOX* pLockedVolume, CONST D3DBOX* pBox, DWORD Flags) {
332     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
333     HRESULT hr;
334
335     TRACE("(%p) Relay %p %p %p %d\n", This, This->wineD3DVolumeTexture, pLockedVolume, pBox,Flags);
336
337     EnterCriticalSection(&d3d9_cs);
338
339     hr = IWineD3DVolumeTexture_LockBox(This->wineD3DVolumeTexture, Level, (WINED3DLOCKED_BOX *)pLockedVolume,
340             (const WINED3DBOX *)pBox, Flags);
341
342     LeaveCriticalSection(&d3d9_cs);
343
344     return hr;
345 }
346
347 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_UnlockBox(LPDIRECT3DVOLUMETEXTURE9 iface, UINT Level) {
348     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
349     HRESULT hr;
350
351     TRACE("(%p) Relay %p %d\n", This, This->wineD3DVolumeTexture, Level);
352
353     EnterCriticalSection(&d3d9_cs);
354
355     hr = IWineD3DVolumeTexture_UnlockBox(This->wineD3DVolumeTexture, Level);
356
357     LeaveCriticalSection(&d3d9_cs);
358
359     return hr;
360 }
361
362 static HRESULT WINAPI IDirect3DVolumeTexture9Impl_AddDirtyBox(LPDIRECT3DVOLUMETEXTURE9 iface, CONST D3DBOX* pDirtyBox) {
363     IDirect3DVolumeTexture9Impl *This = (IDirect3DVolumeTexture9Impl *)iface;
364     HRESULT hr;
365
366     TRACE("(%p) Relay\n", This);
367
368     EnterCriticalSection(&d3d9_cs);
369
370     hr = IWineD3DVolumeTexture_AddDirtyBox(This->wineD3DVolumeTexture, (CONST WINED3DBOX *)pDirtyBox);
371
372     LeaveCriticalSection(&d3d9_cs);
373
374     return hr;
375 }
376
377
378 static const IDirect3DVolumeTexture9Vtbl Direct3DVolumeTexture9_Vtbl =
379 {
380     /* IUnknown */
381     IDirect3DVolumeTexture9Impl_QueryInterface,
382     IDirect3DVolumeTexture9Impl_AddRef,
383     IDirect3DVolumeTexture9Impl_Release,
384     /* IDirect3DResource9 */
385     IDirect3DVolumeTexture9Impl_GetDevice,
386     IDirect3DVolumeTexture9Impl_SetPrivateData,
387     IDirect3DVolumeTexture9Impl_GetPrivateData,
388     IDirect3DVolumeTexture9Impl_FreePrivateData,
389     IDirect3DVolumeTexture9Impl_SetPriority,
390     IDirect3DVolumeTexture9Impl_GetPriority,
391     IDirect3DVolumeTexture9Impl_PreLoad,
392     IDirect3DVolumeTexture9Impl_GetType,
393     /* IDirect3DBaseTexture9 */
394     IDirect3DVolumeTexture9Impl_SetLOD,
395     IDirect3DVolumeTexture9Impl_GetLOD,
396     IDirect3DVolumeTexture9Impl_GetLevelCount,
397     IDirect3DVolumeTexture9Impl_SetAutoGenFilterType,
398     IDirect3DVolumeTexture9Impl_GetAutoGenFilterType,
399     IDirect3DVolumeTexture9Impl_GenerateMipSubLevels,
400     /* IDirect3DVolumeTexture9 */
401     IDirect3DVolumeTexture9Impl_GetLevelDesc,
402     IDirect3DVolumeTexture9Impl_GetVolumeLevel,
403     IDirect3DVolumeTexture9Impl_LockBox,
404     IDirect3DVolumeTexture9Impl_UnlockBox,
405     IDirect3DVolumeTexture9Impl_AddDirtyBox
406 };
407
408
409 /* IDirect3DDevice9 IDirect3DVolumeTexture9 Methods follow: */
410 HRESULT  WINAPI  IDirect3DDevice9Impl_CreateVolumeTexture(LPDIRECT3DDEVICE9EX iface,
411                                                           UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, 
412                                                           D3DFORMAT Format, D3DPOOL Pool, 
413                                                           IDirect3DVolumeTexture9** ppVolumeTexture, HANDLE* pSharedHandle) {
414
415     IDirect3DVolumeTexture9Impl *object;
416     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
417     HRESULT hrc = D3D_OK;
418
419     TRACE("(%p) Relay\n", This);
420
421     /* Allocate the storage for the device */
422     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVolumeTexture9Impl));
423     if (NULL == object) {
424         ERR("(%p) allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n", This);
425         return D3DERR_OUTOFVIDEOMEMORY;
426     }
427
428     object->lpVtbl = &Direct3DVolumeTexture9_Vtbl;
429     object->ref = 1;
430
431     EnterCriticalSection(&d3d9_cs);
432
433     hrc = IWineD3DDevice_CreateVolumeTexture(This->WineD3DDevice, Width, Height, Depth, Levels,
434             Usage & WINED3DUSAGE_MASK, wined3dformat_from_d3dformat(Format), Pool,
435             &object->wineD3DVolumeTexture, pSharedHandle, (IUnknown *)object);
436
437     LeaveCriticalSection(&d3d9_cs);
438
439     if (hrc != D3D_OK) {
440
441         /* free up object */
442         WARN("(%p) call to IWineD3DDevice_CreateVolumeTexture failed\n", This);
443         HeapFree(GetProcessHeap(), 0, object);
444     } else {
445         IDirect3DDevice9Ex_AddRef(iface);
446         object->parentDevice = iface;
447         *ppVolumeTexture = (LPDIRECT3DVOLUMETEXTURE9) object;
448         TRACE("(%p) : Created volume texture %p\n", This, object);
449     }
450     TRACE("(%p)  returning %p\n", This , *ppVolumeTexture);
451     return hrc;
452 }