jscript: Added Object function invocation implementation.
[wine] / dlls / wined3d / volumetexture.c
1 /*
2  * IWineD3DVolumeTexture implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  * Copyright 2009 Henri Verbeet for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
28
29 #define GLINFO_LOCATION (*gl_info)
30
31 static void volumetexture_internal_preload(IWineD3DBaseTexture *iface, enum WINED3DSRGB srgb)
32 {
33     /* Override the IWineD3DResource Preload method. */
34     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
35     IWineD3DDeviceImpl *device = This->resource.wineD3DDevice;
36     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
37     BOOL srgb_mode = This->baseTexture.is_srgb;
38     BOOL srgb_was_toggled = FALSE;
39     unsigned int i;
40
41     TRACE("(%p) : About to load texture.\n", This);
42
43     if (!device->isInDraw)
44     {
45         ActivateContext(device, NULL, CTXUSAGE_RESOURCELOAD);
46     }
47     else if (GL_SUPPORT(EXT_TEXTURE_SRGB) && This->baseTexture.bindCount > 0)
48     {
49         srgb_mode = device->stateBlock->samplerState[This->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
50         srgb_was_toggled = This->baseTexture.is_srgb != srgb_mode;
51         This->baseTexture.is_srgb = srgb_mode;
52     }
53
54     /* If the texture is marked dirty or the srgb sampler setting has changed
55      * since the last load then reload the volumes. */
56     if (This->baseTexture.dirty)
57     {
58         for (i = 0; i < This->baseTexture.levels; ++i)
59         {
60             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
61         }
62     }
63     else if (srgb_was_toggled)
64     {
65         for (i = 0; i < This->baseTexture.levels; ++i)
66         {
67             volume_add_dirty_box(This->volumes[i], NULL);
68             IWineD3DVolume_LoadTexture(This->volumes[i], i, srgb_mode);
69         }
70     }
71     else
72     {
73         TRACE("(%p) Texture not dirty, nothing to do.\n", iface);
74     }
75
76     /* No longer dirty */
77     This->baseTexture.dirty = FALSE;
78 }
79
80 static void volumetexture_cleanup(IWineD3DVolumeTextureImpl *This)
81 {
82     unsigned int i;
83
84     TRACE("(%p) : Cleaning up.\n", This);
85
86     for (i = 0; i < This->baseTexture.levels; ++i)
87     {
88         IWineD3DVolume *volume = This->volumes[i];
89
90         if (volume)
91         {
92             /* Cleanup the container. */
93             IWineD3DVolume_SetContainer(volume, NULL);
94             IWineD3DVolume_Release(volume);
95         }
96     }
97     basetexture_cleanup((IWineD3DBaseTexture *)This);
98 }
99
100 #undef GLINFO_LOCATION
101
102 /* *******************************************
103    IWineD3DTexture IUnknown parts follow
104    ******************************************* */
105
106 #define GLINFO_LOCATION This->resource.wineD3DDevice->adapter->gl_info
107
108 static HRESULT WINAPI IWineD3DVolumeTextureImpl_QueryInterface(IWineD3DVolumeTexture *iface, REFIID riid, LPVOID *ppobj)
109 {
110     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
111     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppobj);
112     if (IsEqualGUID(riid, &IID_IUnknown)
113         || IsEqualGUID(riid, &IID_IWineD3DBase)
114         || IsEqualGUID(riid, &IID_IWineD3DResource)
115         || IsEqualGUID(riid, &IID_IWineD3DBaseTexture)
116         || IsEqualGUID(riid, &IID_IWineD3DVolumeTexture)) {
117         IUnknown_AddRef(iface);
118         *ppobj = This;
119         return S_OK;
120     }
121     *ppobj = NULL;
122     return E_NOINTERFACE;
123 }
124
125 static ULONG WINAPI IWineD3DVolumeTextureImpl_AddRef(IWineD3DVolumeTexture *iface) {
126     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
127     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
128     return InterlockedIncrement(&This->resource.ref);
129 }
130
131 static ULONG WINAPI IWineD3DVolumeTextureImpl_Release(IWineD3DVolumeTexture *iface) {
132     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
133     ULONG ref;
134     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
135     ref = InterlockedDecrement(&This->resource.ref);
136     if (!ref)
137     {
138         volumetexture_cleanup(This);
139         This->parent_ops->wined3d_object_destroyed(This->resource.parent);
140         HeapFree(GetProcessHeap(), 0, This);
141     }
142     return ref;
143 }
144
145 /* ****************************************************
146    IWineD3DVolumeTexture IWineD3DResource parts follow
147    **************************************************** */
148 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetDevice(IWineD3DVolumeTexture *iface, IWineD3DDevice** ppDevice) {
149     return resource_get_device((IWineD3DResource *)iface, ppDevice);
150 }
151
152 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, CONST void* pData, DWORD SizeOfData, DWORD Flags) {
153     return resource_set_private_data((IWineD3DResource *)iface, refguid, pData, SizeOfData, Flags);
154 }
155
156 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetPrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid, void* pData, DWORD* pSizeOfData) {
157     return resource_get_private_data((IWineD3DResource *)iface, refguid, pData, pSizeOfData);
158 }
159
160 static HRESULT WINAPI IWineD3DVolumeTextureImpl_FreePrivateData(IWineD3DVolumeTexture *iface, REFGUID refguid) {
161     return resource_free_private_data((IWineD3DResource *)iface, refguid);
162 }
163
164 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetPriority(IWineD3DVolumeTexture *iface, DWORD PriorityNew) {
165     return resource_set_priority((IWineD3DResource *)iface, PriorityNew);
166 }
167
168 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetPriority(IWineD3DVolumeTexture *iface) {
169     return resource_get_priority((IWineD3DResource *)iface);
170 }
171
172 static void WINAPI IWineD3DVolumeTextureImpl_PreLoad(IWineD3DVolumeTexture *iface) {
173     volumetexture_internal_preload((IWineD3DBaseTexture *) iface, SRGB_ANY);
174 }
175
176 static void WINAPI IWineD3DVolumeTextureImpl_UnLoad(IWineD3DVolumeTexture *iface) {
177     unsigned int i;
178     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
179     TRACE("(%p)\n", This);
180
181     /* Unload all the surfaces and reset the texture name. If UnLoad was called on the
182      * surface before, this one will be a NOP and vice versa. Unloading an unloaded
183      * surface is fine
184      */
185     for (i = 0; i < This->baseTexture.levels; i++) {
186         IWineD3DVolume_UnLoad(This->volumes[i]);
187     }
188
189     basetexture_unload((IWineD3DBaseTexture *)iface);
190 }
191
192 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeTextureImpl_GetType(IWineD3DVolumeTexture *iface) {
193     return resource_get_type((IWineD3DResource *)iface);
194 }
195
196 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetParent(IWineD3DVolumeTexture *iface, IUnknown **pParent) {
197     return resource_get_parent((IWineD3DResource *)iface, pParent);
198 }
199
200 /* ******************************************************
201    IWineD3DVolumeTexture IWineD3DBaseTexture parts follow
202    ****************************************************** */
203 static DWORD WINAPI IWineD3DVolumeTextureImpl_SetLOD(IWineD3DVolumeTexture *iface, DWORD LODNew) {
204     return basetexture_set_lod((IWineD3DBaseTexture *)iface, LODNew);
205 }
206
207 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLOD(IWineD3DVolumeTexture *iface) {
208     return basetexture_get_lod((IWineD3DBaseTexture *)iface);
209 }
210
211 static DWORD WINAPI IWineD3DVolumeTextureImpl_GetLevelCount(IWineD3DVolumeTexture *iface) {
212     return basetexture_get_level_count((IWineD3DBaseTexture *)iface);
213 }
214
215 static HRESULT WINAPI IWineD3DVolumeTextureImpl_SetAutoGenFilterType(IWineD3DVolumeTexture *iface, WINED3DTEXTUREFILTERTYPE FilterType) {
216   return basetexture_set_autogen_filter_type((IWineD3DBaseTexture *)iface, FilterType);
217 }
218
219 static WINED3DTEXTUREFILTERTYPE WINAPI IWineD3DVolumeTextureImpl_GetAutoGenFilterType(IWineD3DVolumeTexture *iface) {
220   return basetexture_get_autogen_filter_type((IWineD3DBaseTexture *)iface);
221 }
222
223 static void WINAPI IWineD3DVolumeTextureImpl_GenerateMipSubLevels(IWineD3DVolumeTexture *iface) {
224     basetexture_generate_mipmaps((IWineD3DBaseTexture *)iface);
225 }
226
227 /* Internal function, No d3d mapping */
228 static BOOL WINAPI IWineD3DVolumeTextureImpl_SetDirty(IWineD3DVolumeTexture *iface, BOOL dirty) {
229     return basetexture_set_dirty((IWineD3DBaseTexture *)iface, dirty);
230 }
231
232 static BOOL WINAPI IWineD3DVolumeTextureImpl_GetDirty(IWineD3DVolumeTexture *iface) {
233     return basetexture_get_dirty((IWineD3DBaseTexture *)iface);
234 }
235
236 /* Context activation is done by the caller. */
237 static HRESULT WINAPI IWineD3DVolumeTextureImpl_BindTexture(IWineD3DVolumeTexture *iface, BOOL srgb) {
238     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
239     BOOL dummy;
240     TRACE("(%p) : relay to BaseTexture\n", This);
241     return basetexture_bind((IWineD3DBaseTexture *)iface, srgb, &dummy);
242 }
243
244 static UINT WINAPI IWineD3DVolumeTextureImpl_GetTextureDimensions(IWineD3DVolumeTexture *iface) {
245     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
246     TRACE("(%p)\n", This);
247     return GL_TEXTURE_3D;
248 }
249
250 static BOOL WINAPI IWineD3DVolumeTextureImpl_IsCondNP2(IWineD3DVolumeTexture *iface) {
251     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
252     TRACE("(%p)\n", This);
253
254     return FALSE;
255 }
256
257 /* *******************************************
258    IWineD3DVolumeTexture IWineD3DVolumeTexture parts follow
259    ******************************************* */
260 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetLevelDesc(IWineD3DVolumeTexture *iface, UINT Level,WINED3DVOLUME_DESC *pDesc) {
261     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
262     if (Level < This->baseTexture.levels) {
263         TRACE("(%p) Level (%d)\n", This, Level);
264         return IWineD3DVolume_GetDesc(This->volumes[Level], pDesc);
265     } else {
266         WARN("(%p) Level (%d)\n", This, Level);
267     }
268     return WINED3D_OK;
269 }
270 static HRESULT WINAPI IWineD3DVolumeTextureImpl_GetVolumeLevel(IWineD3DVolumeTexture *iface, UINT Level, IWineD3DVolume** ppVolumeLevel) {
271     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
272     if (Level < This->baseTexture.levels) {
273       *ppVolumeLevel = This->volumes[Level];
274       IWineD3DVolume_AddRef(*ppVolumeLevel);
275       TRACE("(%p) -> level(%d) returning volume@%p\n", This, Level, *ppVolumeLevel);
276     } else {
277       WARN("(%p) Level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
278       return WINED3DERR_INVALIDCALL;
279     }
280     return WINED3D_OK;
281
282 }
283 static HRESULT WINAPI IWineD3DVolumeTextureImpl_LockBox(IWineD3DVolumeTexture *iface, UINT Level, WINED3DLOCKED_BOX* pLockedVolume, CONST WINED3DBOX* pBox, DWORD Flags) {
284     HRESULT hr;
285     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
286
287     if (Level < This->baseTexture.levels) {
288       hr = IWineD3DVolume_LockBox(This->volumes[Level], pLockedVolume, pBox, Flags);
289       TRACE("(%p) Level (%d) success(%u)\n", This, Level, hr);
290
291     } else {
292       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
293       return WINED3DERR_INVALIDCALL;
294     }
295     return hr;
296 }
297
298 static HRESULT WINAPI IWineD3DVolumeTextureImpl_UnlockBox(IWineD3DVolumeTexture *iface, UINT Level) {
299     HRESULT hr;
300     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
301
302     if (Level < This->baseTexture.levels) {
303       hr = IWineD3DVolume_UnlockBox(This->volumes[Level]);
304       TRACE("(%p) -> level(%d) success(%u)\n", This, Level, hr);
305
306     } else {
307       FIXME("(%p) level(%d) overflow Levels(%d)\n", This, Level, This->baseTexture.levels);
308       return WINED3DERR_INVALIDCALL;
309     }
310     return hr;
311 }
312
313 static HRESULT WINAPI IWineD3DVolumeTextureImpl_AddDirtyBox(IWineD3DVolumeTexture *iface, CONST WINED3DBOX* pDirtyBox) {
314     IWineD3DVolumeTextureImpl *This = (IWineD3DVolumeTextureImpl *)iface;
315     This->baseTexture.dirty = TRUE;
316     TRACE("(%p) : dirtyfication of volume Level (0)\n", This);
317     volume_add_dirty_box(This->volumes[0], pDirtyBox);
318
319     return WINED3D_OK;
320 }
321
322 static const IWineD3DVolumeTextureVtbl IWineD3DVolumeTexture_Vtbl =
323 {
324     /* IUnknown */
325     IWineD3DVolumeTextureImpl_QueryInterface,
326     IWineD3DVolumeTextureImpl_AddRef,
327     IWineD3DVolumeTextureImpl_Release,
328     /* resource */
329     IWineD3DVolumeTextureImpl_GetParent,
330     IWineD3DVolumeTextureImpl_GetDevice,
331     IWineD3DVolumeTextureImpl_SetPrivateData,
332     IWineD3DVolumeTextureImpl_GetPrivateData,
333     IWineD3DVolumeTextureImpl_FreePrivateData,
334     IWineD3DVolumeTextureImpl_SetPriority,
335     IWineD3DVolumeTextureImpl_GetPriority,
336     IWineD3DVolumeTextureImpl_PreLoad,
337     IWineD3DVolumeTextureImpl_UnLoad,
338     IWineD3DVolumeTextureImpl_GetType,
339     /* BaseTexture */
340     IWineD3DVolumeTextureImpl_SetLOD,
341     IWineD3DVolumeTextureImpl_GetLOD,
342     IWineD3DVolumeTextureImpl_GetLevelCount,
343     IWineD3DVolumeTextureImpl_SetAutoGenFilterType,
344     IWineD3DVolumeTextureImpl_GetAutoGenFilterType,
345     IWineD3DVolumeTextureImpl_GenerateMipSubLevels,
346     IWineD3DVolumeTextureImpl_SetDirty,
347     IWineD3DVolumeTextureImpl_GetDirty,
348     /* not in d3d */
349     IWineD3DVolumeTextureImpl_BindTexture,
350     IWineD3DVolumeTextureImpl_GetTextureDimensions,
351     IWineD3DVolumeTextureImpl_IsCondNP2,
352     /* volume texture */
353     IWineD3DVolumeTextureImpl_GetLevelDesc,
354     IWineD3DVolumeTextureImpl_GetVolumeLevel,
355     IWineD3DVolumeTextureImpl_LockBox,
356     IWineD3DVolumeTextureImpl_UnlockBox,
357     IWineD3DVolumeTextureImpl_AddDirtyBox
358 };
359
360 HRESULT volumetexture_init(IWineD3DVolumeTextureImpl *texture, UINT width, UINT height,
361         UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, WINED3DFORMAT format,
362         WINED3DPOOL pool, IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
363 {
364     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
365     const struct GlPixelFormatDesc *format_desc = getFormatDescEntry(format, gl_info);
366     UINT tmp_w, tmp_h, tmp_d;
367     unsigned int i;
368     HRESULT hr;
369
370     /* TODO: It should only be possible to create textures for formats
371      * that are reported as supported. */
372     if (WINED3DFMT_UNKNOWN >= format)
373     {
374         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
375         return WINED3DERR_INVALIDCALL;
376     }
377
378     if (!gl_info->supported[EXT_TEXTURE3D])
379     {
380         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
381         return WINED3DERR_INVALIDCALL;
382     }
383
384     /* Calculate levels for mip mapping. */
385     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
386     {
387         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
388         {
389             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
390             return WINED3DERR_INVALIDCALL;
391         }
392
393         if (levels > 1)
394         {
395             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
396             return WINED3DERR_INVALIDCALL;
397         }
398
399         levels = 1;
400     }
401     else if (!levels)
402     {
403         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
404         TRACE("Calculated levels = %u.\n", levels);
405     }
406
407     texture->lpVtbl = &IWineD3DVolumeTexture_Vtbl;
408
409     hr = basetexture_init((IWineD3DBaseTextureImpl *)texture, levels,
410             WINED3DRTYPE_VOLUMETEXTURE, device, 0, usage, format_desc, pool, parent);
411     if (FAILED(hr))
412     {
413         WARN("Failed to initialize basetexture, returning %#x.\n", hr);
414         return hr;
415     }
416
417     texture->parent_ops = parent_ops;
418
419     /* Is NP2 support for volumes needed? */
420     texture->baseTexture.pow2Matrix[0] = 1.0f;
421     texture->baseTexture.pow2Matrix[5] = 1.0f;
422     texture->baseTexture.pow2Matrix[10] = 1.0f;
423     texture->baseTexture.pow2Matrix[15] = 1.0f;
424
425     /* Generate all the surfaces. */
426     tmp_w = width;
427     tmp_h = height;
428     tmp_d = depth;
429
430     for (i = 0; i < texture->baseTexture.levels; ++i)
431     {
432         /* Create the volume. */
433         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
434                 tmp_w, tmp_h, tmp_d, format, pool, usage, &texture->volumes[i]);
435         if (FAILED(hr))
436         {
437             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
438             texture->volumes[i] = NULL;
439             volumetexture_cleanup(texture);
440             return hr;
441         }
442
443         /* Set its container to this texture. */
444         IWineD3DVolume_SetContainer(texture->volumes[i], (IWineD3DBase *)texture);
445
446         /* Calculate the next mipmap level. */
447         tmp_w = max(1, tmp_w >> 1);
448         tmp_h = max(1, tmp_h >> 1);
449         tmp_d = max(1, tmp_d >> 1);
450     }
451     texture->baseTexture.internal_preload = volumetexture_internal_preload;
452
453     return WINED3D_OK;
454 }