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