urlmon/tests: Added CoInternetParseUrl tests.
[wine] / dlls / wined3d / texture.c
1 /*
2  * Copyright 2002-2005 Jason Edmeades
3  * Copyright 2002-2005 Raphael Junqueira
4  * Copyright 2005 Oliver Stieber
5  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
6  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
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_texture);
27
28 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
29         UINT layer_count, UINT level_count, WINED3DRESOURCETYPE resource_type, IWineD3DDeviceImpl *device,
30         DWORD usage, const struct wined3d_format *format, WINED3DPOOL pool, void *parent,
31         const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
32 {
33     HRESULT hr;
34
35     hr = resource_init(&texture->resource, device, resource_type, format,
36             WINED3DMULTISAMPLE_NONE, 0, usage, pool, 0, 0, 0, 0,
37             parent, parent_ops, resource_ops);
38     if (FAILED(hr))
39     {
40         WARN("Failed to initialize resource, returning %#x\n", hr);
41         return hr;
42     }
43
44     texture->baseTexture.texture_ops = texture_ops;
45     texture->baseTexture.sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
46             level_count * layer_count * sizeof(*texture->baseTexture.sub_resources));
47     if (!texture->baseTexture.sub_resources)
48     {
49         ERR("Failed to allocate sub-resource array.\n");
50         resource_cleanup(&texture->resource);
51         return E_OUTOFMEMORY;
52     }
53
54     texture->baseTexture.layer_count = layer_count;
55     texture->baseTexture.level_count = level_count;
56     texture->baseTexture.filterType = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3DTEXF_LINEAR : WINED3DTEXF_NONE;
57     texture->baseTexture.LOD = 0;
58     texture->baseTexture.texture_rgb.dirty = TRUE;
59     texture->baseTexture.texture_srgb.dirty = TRUE;
60     texture->baseTexture.is_srgb = FALSE;
61     texture->baseTexture.pow2Matrix_identity = TRUE;
62
63     if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
64     {
65         texture->baseTexture.minMipLookup = minMipLookup;
66         texture->baseTexture.magLookup = magLookup;
67     }
68     else
69     {
70         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
71         texture->baseTexture.magLookup = magLookup_noFilter;
72     }
73
74     return WINED3D_OK;
75 }
76
77 /* A GL context is provided by the caller */
78 static void gltexture_delete(struct gl_texture *tex)
79 {
80     ENTER_GL();
81     glDeleteTextures(1, &tex->name);
82     LEAVE_GL();
83     tex->name = 0;
84 }
85
86 static void wined3d_texture_unload(struct wined3d_texture *texture)
87 {
88     IWineD3DDeviceImpl *device = texture->resource.device;
89     struct wined3d_context *context = NULL;
90
91     if (texture->baseTexture.texture_rgb.name || texture->baseTexture.texture_srgb.name)
92     {
93         context = context_acquire(device, NULL);
94     }
95
96     if (texture->baseTexture.texture_rgb.name)
97         gltexture_delete(&texture->baseTexture.texture_rgb);
98
99     if (texture->baseTexture.texture_srgb.name)
100         gltexture_delete(&texture->baseTexture.texture_srgb);
101
102     if (context) context_release(context);
103
104     wined3d_texture_set_dirty(texture, TRUE);
105
106     resource_unload(&texture->resource);
107 }
108
109 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
110 {
111     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
112     UINT i;
113
114     TRACE("texture %p.\n", texture);
115
116     for (i = 0; i < sub_count; ++i)
117     {
118         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
119
120         if (sub_resource)
121             texture->baseTexture.texture_ops->texture_sub_resource_cleanup(sub_resource);
122     }
123
124     wined3d_texture_unload(texture);
125     HeapFree(GetProcessHeap(), 0, texture->baseTexture.sub_resources);
126     resource_cleanup(&texture->resource);
127 }
128
129 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
130 {
131     texture->baseTexture.texture_rgb.dirty = dirty;
132     texture->baseTexture.texture_srgb.dirty = dirty;
133 }
134
135 /* Context activation is done by the caller. */
136 static HRESULT wined3d_texture_bind(struct wined3d_texture *texture,
137         const struct wined3d_gl_info *gl_info, BOOL srgb, BOOL *set_surface_desc)
138 {
139     struct gl_texture *gl_tex;
140     BOOL new_texture = FALSE;
141     HRESULT hr = WINED3D_OK;
142     GLenum target;
143
144     TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, srgb, set_surface_desc);
145
146     texture->baseTexture.is_srgb = srgb; /* sRGB mode cache for preload() calls outside drawprim. */
147     gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb);
148     target = texture->baseTexture.target;
149
150     ENTER_GL();
151     /* Generate a texture name if we don't already have one. */
152     if (!gl_tex->name)
153     {
154         *set_surface_desc = TRUE;
155         glGenTextures(1, &gl_tex->name);
156         checkGLcall("glGenTextures");
157         TRACE("Generated texture %d.\n", gl_tex->name);
158         if (texture->resource.pool == WINED3DPOOL_DEFAULT)
159         {
160             /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
161             GLclampf tmp = 0.9f;
162             glPrioritizeTextures(1, &gl_tex->name, &tmp);
163         }
164         /* Initialise the state of the texture object to the OpenGL defaults,
165          * not the D3D defaults. */
166         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_WRAP;
167         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_WRAP;
168         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3DTADDRESS_WRAP;
169         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
170         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_LINEAR;
171         gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
172         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
173         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
174         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
175         if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
176             gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
177         else
178             gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
179         gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
180         wined3d_texture_set_dirty(texture, TRUE);
181         new_texture = TRUE;
182
183         if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
184         {
185             /* This means double binding the texture at creation, but keeps
186              * the code simpler all in all, and the run-time path free from
187              * additional checks. */
188             glBindTexture(target, gl_tex->name);
189             checkGLcall("glBindTexture");
190             glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
191             checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
192         }
193     }
194     else
195     {
196         *set_surface_desc = FALSE;
197     }
198
199     if (gl_tex->name)
200     {
201         glBindTexture(target, gl_tex->name);
202         checkGLcall("glBindTexture");
203         if (new_texture)
204         {
205             /* For a new texture we have to set the texture levels after
206              * binding the texture. Beware that texture rectangles do not
207              * support mipmapping, but set the maxmiplevel if we're relying
208              * on the partial GL_ARB_texture_non_power_of_two emulation with
209              * texture rectangles. (I.e., do not care about cond_np2 here,
210              * just look for GL_TEXTURE_RECTANGLE_ARB.) */
211             if (target != GL_TEXTURE_RECTANGLE_ARB)
212             {
213                 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->baseTexture.level_count - 1);
214                 glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count - 1);
215                 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->baseTexture.level_count)");
216             }
217             if (target == GL_TEXTURE_CUBE_MAP_ARB)
218             {
219                 /* Cubemaps are always set to clamp, regardless of the sampler state. */
220                 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
221                 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
222                 glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
223             }
224         }
225     }
226     else
227     {
228         ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
229         hr = WINED3DERR_INVALIDCALL;
230     }
231
232     LEAVE_GL();
233     return hr;
234 }
235
236 /* GL locking is done by the caller */
237 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
238         WINED3DTEXTUREADDRESS d3d_wrap, GLenum param, BOOL cond_np2)
239 {
240     GLint gl_wrap;
241
242     if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
243     {
244         FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
245         return;
246     }
247
248     /* Cubemaps are always set to clamp, regardless of the sampler state. */
249     if (target == GL_TEXTURE_CUBE_MAP_ARB
250             || (cond_np2 && d3d_wrap == WINED3DTADDRESS_WRAP))
251         gl_wrap = GL_CLAMP_TO_EDGE;
252     else
253         gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3DTADDRESS_WRAP];
254
255     TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
256     glTexParameteri(target, param, gl_wrap);
257     checkGLcall("glTexParameteri(target, param, gl_wrap)");
258 }
259
260 /* GL locking is done by the caller (state handler) */
261 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
262         const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
263         const struct wined3d_gl_info *gl_info)
264 {
265     BOOL cond_np2 = texture->baseTexture.cond_np2;
266     GLenum target = texture->baseTexture.target;
267     struct gl_texture *gl_tex;
268     DWORD state;
269     DWORD aniso;
270
271     TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
272
273     gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, texture->baseTexture.is_srgb);
274
275     /* This function relies on the correct texture being bound and loaded. */
276
277     if (sampler_states[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
278     {
279         state = sampler_states[WINED3DSAMP_ADDRESSU];
280         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
281         gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
282     }
283
284     if (sampler_states[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
285     {
286         state = sampler_states[WINED3DSAMP_ADDRESSV];
287         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
288         gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
289     }
290
291     if (sampler_states[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
292     {
293         state = sampler_states[WINED3DSAMP_ADDRESSW];
294         apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
295         gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
296     }
297
298     if (sampler_states[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
299     {
300         float col[4];
301
302         state = sampler_states[WINED3DSAMP_BORDERCOLOR];
303         D3DCOLORTOGLFLOAT4(state, col);
304         TRACE("Setting border color for %#x to %#x.\n", target, state);
305         glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
306         checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
307         gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
308     }
309
310     if (sampler_states[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
311     {
312         GLint gl_value;
313
314         state = sampler_states[WINED3DSAMP_MAGFILTER];
315         if (state > WINED3DTEXF_ANISOTROPIC)
316             FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
317
318         gl_value = wined3d_gl_mag_filter(texture->baseTexture.magLookup,
319                 min(max(state, WINED3DTEXF_POINT), WINED3DTEXF_LINEAR));
320         TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
321         glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
322
323         gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
324     }
325
326     if ((sampler_states[WINED3DSAMP_MINFILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
327             || sampler_states[WINED3DSAMP_MIPFILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
328             || sampler_states[WINED3DSAMP_MAXMIPLEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
329     {
330         GLint gl_value;
331
332         gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3DSAMP_MIPFILTER];
333         gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3DSAMP_MINFILTER];
334         gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3DSAMP_MAXMIPLEVEL];
335
336         if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
337             || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
338         {
339             FIXME("Unrecognized or unsupported D3DSAMP_MINFILTER value %#x D3DSAMP_MIPFILTER value %#x.\n",
340                   gl_tex->states[WINED3DTEXSTA_MINFILTER],
341                   gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
342         }
343         gl_value = wined3d_gl_min_mip_filter(texture->baseTexture.minMipLookup,
344                 min(max(sampler_states[WINED3DSAMP_MINFILTER], WINED3DTEXF_POINT), WINED3DTEXF_LINEAR),
345                 min(max(sampler_states[WINED3DSAMP_MIPFILTER], WINED3DTEXF_NONE), WINED3DTEXF_LINEAR));
346
347         TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
348               sampler_states[WINED3DSAMP_MINFILTER],
349               sampler_states[WINED3DSAMP_MIPFILTER], gl_value);
350         glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
351         checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
352
353         if (!cond_np2)
354         {
355             if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3DTEXF_NONE)
356                 gl_value = texture->baseTexture.LOD;
357             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->baseTexture.level_count)
358                 gl_value = texture->baseTexture.level_count - 1;
359             else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->baseTexture.LOD)
360                 /* baseTexture.LOD is already clamped in the setter. */
361                 gl_value = texture->baseTexture.LOD;
362             else
363                 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
364
365             /* Note that WINED3DSAMP_MAXMIPLEVEL specifies the largest mipmap
366              * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
367              * mimap used (default 1000). So WINED3DSAMP_MAXMIPLEVEL
368              * corresponds to GL_TEXTURE_BASE_LEVEL. */
369             glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
370         }
371     }
372
373     if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3DTEXF_ANISOTROPIC
374          && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3DTEXF_ANISOTROPIC
375          && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3DTEXF_ANISOTROPIC)
376             || cond_np2)
377         aniso = 1;
378     else
379         aniso = sampler_states[WINED3DSAMP_MAXANISOTROPY];
380
381     if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
382     {
383         if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
384         {
385             glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
386             checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
387         }
388         else
389         {
390             WARN("Anisotropic filtering not supported.\n");
391         }
392         gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
393     }
394
395     /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
396     if (sampler_states[WINED3DSAMP_SRGBTEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
397     {
398         glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
399                 sampler_states[WINED3DSAMP_SRGBTEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
400         checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
401     }
402
403     if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
404             != !gl_tex->states[WINED3DTEXSTA_SHADOW])
405     {
406         if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
407         {
408             glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
409             glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
410             checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
411             gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
412         }
413         else
414         {
415             glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
416             checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
417             gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
418         }
419     }
420 }
421
422 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
423 {
424     ULONG refcount = InterlockedIncrement(&texture->resource.ref);
425
426     TRACE("%p increasing refcount to %u.\n", texture, refcount);
427
428     return refcount;
429 }
430
431 /* Do not call while under the GL lock. */
432 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
433 {
434     ULONG refcount = InterlockedDecrement(&texture->resource.ref);
435
436     TRACE("%p decreasing refcount to %u.\n", texture, refcount);
437
438     if (!refcount)
439     {
440         wined3d_texture_cleanup(texture);
441         texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
442         HeapFree(GetProcessHeap(), 0, texture);
443     }
444
445     return refcount;
446 }
447
448 HRESULT CDECL wined3d_texture_set_private_data(struct wined3d_texture *texture,
449         REFGUID guid, const void *data, DWORD data_size, DWORD flags)
450 {
451     return resource_set_private_data(&texture->resource, guid, data, data_size, flags);
452 }
453
454 HRESULT CDECL wined3d_texture_get_private_data(const struct wined3d_texture *texture,
455         REFGUID guid, void *data, DWORD *data_size)
456 {
457     return resource_get_private_data(&texture->resource, guid, data, data_size);
458 }
459
460 HRESULT CDECL wined3d_texture_free_private_data(struct wined3d_texture *texture, REFGUID guid)
461 {
462     return resource_free_private_data(&texture->resource, guid);
463 }
464
465 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
466 {
467     return resource_set_priority(&texture->resource, priority);
468 }
469
470 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
471 {
472     return resource_get_priority(&texture->resource);
473 }
474
475 /* Do not call while under the GL lock. */
476 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
477 {
478     texture->baseTexture.texture_ops->texture_preload(texture, SRGB_ANY);
479 }
480
481 WINED3DRESOURCETYPE CDECL wined3d_texture_get_type(const struct wined3d_texture *texture)
482 {
483     TRACE("texture %p.\n", texture);
484
485     return texture->resource.resourceType;
486 }
487
488 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
489 {
490     TRACE("texture %p.\n", texture);
491
492     return texture->resource.parent;
493 }
494
495 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
496 {
497     DWORD old = texture->baseTexture.LOD;
498
499     TRACE("texture %p, lod %u.\n", texture, lod);
500
501     /* The d3d9:texture test shows that SetLOD is ignored on non-managed
502      * textures. The call always returns 0, and GetLOD always returns 0. */
503     if (texture->resource.pool != WINED3DPOOL_MANAGED)
504     {
505         TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
506         return 0;
507     }
508
509     if (lod >= texture->baseTexture.level_count)
510         lod = texture->baseTexture.level_count - 1;
511
512     if (texture->baseTexture.LOD != lod)
513     {
514         texture->baseTexture.LOD = lod;
515
516         texture->baseTexture.texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
517         texture->baseTexture.texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
518         if (texture->baseTexture.bindCount)
519             IWineD3DDeviceImpl_MarkStateDirty(texture->resource.device, STATE_SAMPLER(texture->baseTexture.sampler));
520     }
521
522     return old;
523 }
524
525 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
526 {
527     TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.LOD);
528
529     return texture->baseTexture.LOD;
530 }
531
532 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
533 {
534     TRACE("texture %p, returning %u.\n", texture, texture->baseTexture.level_count);
535
536     return texture->baseTexture.level_count;
537 }
538
539 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
540         WINED3DTEXTUREFILTERTYPE filter_type)
541 {
542     TRACE("texture %p, filter_type %s.\n", texture, debug_d3dtexturefiltertype(filter_type));
543
544     if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
545     {
546         WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
547         return WINED3DERR_INVALIDCALL;
548     }
549
550     if (texture->baseTexture.filterType != filter_type)
551     {
552         GLenum target = texture->baseTexture.target;
553         struct wined3d_context *context;
554
555         context = context_acquire(texture->resource.device, NULL);
556
557         ENTER_GL();
558         glBindTexture(target, texture->baseTexture.texture_rgb.name);
559         checkGLcall("glBindTexture");
560         switch (filter_type)
561         {
562             case WINED3DTEXF_NONE:
563             case WINED3DTEXF_POINT:
564                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST);
565                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_FASTEST)");
566                 break;
567
568             case WINED3DTEXF_LINEAR:
569                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
570                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
571                 break;
572
573             default:
574                 WARN("Unexpected filter type %#x, setting to GL_NICEST.\n", filter_type);
575                 glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST);
576                 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_HINT_SGIS, GL_NICEST)");
577                 break;
578         }
579         LEAVE_GL();
580
581         context_release(context);
582     }
583     texture->baseTexture.filterType = filter_type;
584
585     return WINED3D_OK;
586 }
587
588 WINED3DTEXTUREFILTERTYPE CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
589 {
590     TRACE("texture %p.\n", texture);
591
592     return texture->baseTexture.filterType;
593 }
594
595 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
596 {
597     /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
598     FIXME("texture %p stub!\n", texture);
599 }
600
601 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
602         UINT sub_resource_idx)
603 {
604     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
605
606     TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
607
608     if (sub_resource_idx >= sub_count)
609     {
610         WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
611         return NULL;
612     }
613
614     return texture->baseTexture.sub_resources[sub_resource_idx];
615 }
616
617 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
618         UINT layer, const WINED3DBOX *dirty_region)
619 {
620     struct wined3d_resource *sub_resource;
621
622     TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
623
624     if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->baseTexture.level_count)))
625     {
626         WARN("Failed to get sub-resource.\n");
627         return WINED3DERR_INVALIDCALL;
628     }
629
630     wined3d_texture_set_dirty(texture, TRUE);
631     texture->baseTexture.texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
632
633     return WINED3D_OK;
634 }
635
636 /* Context activation is done by the caller. */
637 static HRESULT texture2d_bind(struct wined3d_texture *texture,
638         const struct wined3d_gl_info *gl_info, BOOL srgb)
639 {
640     BOOL set_gl_texture_desc;
641     HRESULT hr;
642
643     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
644
645     hr = wined3d_texture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
646     if (set_gl_texture_desc && SUCCEEDED(hr))
647     {
648         UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
649         BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE] && texture->baseTexture.is_srgb;
650         struct gl_texture *gl_tex;
651         UINT i;
652
653         gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_tex);
654
655         for (i = 0; i < sub_count; ++i)
656         {
657             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
658             surface_set_texture_name(surface, gl_tex->name, srgb_tex);
659         }
660
661         /* Conditinal non power of two textures use a different clamping
662          * default. If we're using the GL_WINE_normalized_texrect partial
663          * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
664          * has the address mode set to repeat - something that prevents us
665          * from hitting the accelerated codepath. Thus manually set the GL
666          * state. The same applies to filtering. Even if the texture has only
667          * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
668          * fallback on macos. */
669         if (texture->baseTexture.cond_np2)
670         {
671             GLenum target = texture->baseTexture.target;
672
673             ENTER_GL();
674             glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
675             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
676             glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
677             checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
678             glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
679             checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
680             glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
681             checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
682             LEAVE_GL();
683             gl_tex->states[WINED3DTEXSTA_ADDRESSU]      = WINED3DTADDRESS_CLAMP;
684             gl_tex->states[WINED3DTEXSTA_ADDRESSV]      = WINED3DTADDRESS_CLAMP;
685             gl_tex->states[WINED3DTEXSTA_MAGFILTER]     = WINED3DTEXF_POINT;
686             gl_tex->states[WINED3DTEXSTA_MINFILTER]     = WINED3DTEXF_POINT;
687             gl_tex->states[WINED3DTEXSTA_MIPFILTER]     = WINED3DTEXF_NONE;
688         }
689     }
690
691     return hr;
692 }
693
694 /* Do not call while under the GL lock. */
695 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
696 {
697     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
698     IWineD3DDeviceImpl *device = texture->resource.device;
699     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
700     struct wined3d_context *context = NULL;
701     struct gl_texture *gl_tex;
702     BOOL srgb_mode;
703     UINT i;
704
705     TRACE("texture %p, srgb %#x.\n", texture, srgb);
706
707     switch (srgb)
708     {
709         case SRGB_RGB:
710             srgb_mode = FALSE;
711             break;
712
713         case SRGB_BOTH:
714             texture2d_preload(texture, SRGB_RGB);
715             /* Fallthrough */
716         case SRGB_SRGB:
717             srgb_mode = TRUE;
718             break;
719
720         default:
721             srgb_mode = texture->baseTexture.is_srgb;
722             break;
723     }
724
725     gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
726
727     if (!device->isInDraw)
728     {
729         /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
730          * when loading offscreen render targets into the texture. */
731         context = context_acquire(device, NULL);
732     }
733
734     if (texture->resource.format->id == WINED3DFMT_P8_UINT
735             || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
736     {
737         for (i = 0; i < sub_count; ++i)
738         {
739             IWineD3DSurfaceImpl *surface = surface_from_resource(texture->baseTexture.sub_resources[i]);
740
741             if (palette9_changed(surface))
742             {
743                 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
744                 /* TODO: This is not necessarily needed with hw palettized texture support. */
745                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
746                 /* Make sure the texture is reloaded because of the palette
747                  * change, this kills performance though :( */
748                 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
749             }
750         }
751     }
752
753     if (gl_tex->dirty)
754     {
755         /* Reload the surfaces if the texture is marked dirty. */
756         for (i = 0; i < sub_count; ++i)
757         {
758             surface_load(surface_from_resource(texture->baseTexture.sub_resources[i]), srgb_mode);
759         }
760     }
761     else
762     {
763         TRACE("Texture %p not dirty, nothing to do.\n", texture);
764     }
765
766     /* No longer dirty. */
767     gl_tex->dirty = FALSE;
768
769     if (context) context_release(context);
770 }
771
772 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
773         const WINED3DBOX *dirty_region)
774 {
775     surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
776 }
777
778 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
779 {
780     IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
781
782     /* Clean out the texture name we gave to the surface so that the
783      * surface doesn't try and release it. */
784     surface_set_texture_name(surface, 0, TRUE);
785     surface_set_texture_name(surface, 0, FALSE);
786     surface_set_texture_target(surface, 0);
787     surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
788     IWineD3DSurface_Release((IWineD3DSurface *)surface);
789 }
790
791 /* Do not call while under the GL lock. */
792 static void texture2d_unload(struct wined3d_resource *resource)
793 {
794     struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
795     UINT sub_count = texture->baseTexture.level_count * texture->baseTexture.layer_count;
796     UINT i;
797
798     TRACE("texture %p.\n", texture);
799
800     for (i = 0; i < sub_count; ++i)
801     {
802         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
803         IWineD3DSurfaceImpl *surface = surface_from_resource(sub_resource);
804
805         sub_resource->resource_ops->resource_unload(sub_resource);
806         surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
807         surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
808     }
809
810     wined3d_texture_unload(texture);
811 }
812
813 static const struct wined3d_texture_ops texture2d_ops =
814 {
815     texture2d_bind,
816     texture2d_preload,
817     texture2d_sub_resource_add_dirty_region,
818     texture2d_sub_resource_cleanup,
819 };
820
821 static const struct wined3d_resource_ops texture2d_resource_ops =
822 {
823     texture2d_unload,
824 };
825
826 HRESULT cubetexture_init(struct wined3d_texture *texture, UINT edge_length, UINT levels,
827         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
828         void *parent, const struct wined3d_parent_ops *parent_ops)
829 {
830     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
831     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
832     UINT pow2_edge_length;
833     unsigned int i, j;
834     UINT tmp_w;
835     HRESULT hr;
836
837     /* TODO: It should only be possible to create textures for formats
838      * that are reported as supported. */
839     if (WINED3DFMT_UNKNOWN >= format_id)
840     {
841         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
842         return WINED3DERR_INVALIDCALL;
843     }
844
845     if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
846     {
847         WARN("(%p) : Tried to create not supported cube texture.\n", texture);
848         return WINED3DERR_INVALIDCALL;
849     }
850
851     /* Calculate levels for mip mapping */
852     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
853     {
854         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
855         {
856             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
857             return WINED3DERR_INVALIDCALL;
858         }
859
860         if (levels > 1)
861         {
862             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
863             return WINED3DERR_INVALIDCALL;
864         }
865
866         levels = 1;
867     }
868     else if (!levels)
869     {
870         levels = wined3d_log2i(edge_length) + 1;
871         TRACE("Calculated levels = %u.\n", levels);
872     }
873
874     hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
875             WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
876             parent, parent_ops, &texture2d_resource_ops);
877     if (FAILED(hr))
878     {
879         WARN("Failed to initialize texture, returning %#x\n", hr);
880         return hr;
881     }
882
883     /* Find the nearest pow2 match. */
884     pow2_edge_length = 1;
885     while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
886
887     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
888     {
889         /* Precalculated scaling for 'faked' non power of two texture coords. */
890         texture->baseTexture.pow2Matrix[0] = 1.0f;
891         texture->baseTexture.pow2Matrix[5] = 1.0f;
892         texture->baseTexture.pow2Matrix[10] = 1.0f;
893         texture->baseTexture.pow2Matrix[15] = 1.0f;
894     }
895     else
896     {
897         /* Precalculated scaling for 'faked' non power of two texture coords. */
898         texture->baseTexture.pow2Matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
899         texture->baseTexture.pow2Matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
900         texture->baseTexture.pow2Matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
901         texture->baseTexture.pow2Matrix[15] = 1.0f;
902         texture->baseTexture.pow2Matrix_identity = FALSE;
903     }
904     texture->baseTexture.target = GL_TEXTURE_CUBE_MAP_ARB;
905
906     /* Generate all the surfaces. */
907     tmp_w = edge_length;
908     for (i = 0; i < texture->baseTexture.level_count; ++i)
909     {
910         /* Create the 6 faces. */
911         for (j = 0; j < 6; ++j)
912         {
913             static const GLenum cube_targets[6] =
914             {
915                 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
916                 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
917                 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
918                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
919                 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
920                 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
921             };
922             UINT idx = j * texture->baseTexture.level_count + i;
923             IWineD3DSurface *surface;
924
925             hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_w,
926                     format_id, usage, pool, i /* Level */, j, &surface);
927             if (FAILED(hr))
928             {
929                 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
930                 wined3d_texture_cleanup(texture);
931                 return hr;
932             }
933
934             surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, texture);
935             surface_set_texture_target((IWineD3DSurfaceImpl *)surface, cube_targets[j]);
936             texture->baseTexture.sub_resources[idx] = &((IWineD3DSurfaceImpl *)surface)->resource;
937             TRACE("Created surface level %u @ %p.\n", i, surface);
938         }
939         tmp_w = max(1, tmp_w >> 1);
940     }
941
942     return WINED3D_OK;
943 }
944
945 HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
946         IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
947         void *parent, const struct wined3d_parent_ops *parent_ops)
948 {
949     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
950     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
951     UINT pow2_width, pow2_height;
952     UINT tmp_w, tmp_h;
953     unsigned int i;
954     HRESULT hr;
955
956     /* TODO: It should only be possible to create textures for formats
957      * that are reported as supported. */
958     if (WINED3DFMT_UNKNOWN >= format_id)
959     {
960         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
961         return WINED3DERR_INVALIDCALL;
962     }
963
964     /* Non-power2 support. */
965     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
966     {
967         pow2_width = width;
968         pow2_height = height;
969     }
970     else
971     {
972         /* Find the nearest pow2 match. */
973         pow2_width = pow2_height = 1;
974         while (pow2_width < width) pow2_width <<= 1;
975         while (pow2_height < height) pow2_height <<= 1;
976
977         if (pow2_width != width || pow2_height != height)
978         {
979             if (levels > 1)
980             {
981                 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
982                 return WINED3DERR_INVALIDCALL;
983             }
984             levels = 1;
985         }
986     }
987
988     /* Calculate levels for mip mapping. */
989     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
990     {
991         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
992         {
993             WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
994             return WINED3DERR_INVALIDCALL;
995         }
996
997         if (levels > 1)
998         {
999             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
1000             return WINED3DERR_INVALIDCALL;
1001         }
1002
1003         levels = 1;
1004     }
1005     else if (!levels)
1006     {
1007         levels = wined3d_log2i(max(width, height)) + 1;
1008         TRACE("Calculated levels = %u.\n", levels);
1009     }
1010
1011     hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
1012             WINED3DRTYPE_TEXTURE, device, usage, format, pool,
1013             parent, parent_ops, &texture2d_resource_ops);
1014     if (FAILED(hr))
1015     {
1016         WARN("Failed to initialize texture, returning %#x.\n", hr);
1017         return hr;
1018     }
1019
1020     /* Precalculated scaling for 'faked' non power of two texture coords.
1021      * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
1022      * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
1023      * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
1024     if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
1025     {
1026         texture->baseTexture.pow2Matrix[0] = 1.0f;
1027         texture->baseTexture.pow2Matrix[5] = 1.0f;
1028         texture->baseTexture.pow2Matrix[10] = 1.0f;
1029         texture->baseTexture.pow2Matrix[15] = 1.0f;
1030         texture->baseTexture.target = GL_TEXTURE_2D;
1031         texture->baseTexture.cond_np2 = TRUE;
1032         texture->baseTexture.minMipLookup = minMipLookup_noFilter;
1033     }
1034     else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
1035             && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
1036             && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
1037     {
1038         if ((width != 1) || (height != 1)) texture->baseTexture.pow2Matrix_identity = FALSE;
1039
1040         texture->baseTexture.pow2Matrix[0] = (float)width;
1041         texture->baseTexture.pow2Matrix[5] = (float)height;
1042         texture->baseTexture.pow2Matrix[10] = 1.0f;
1043         texture->baseTexture.pow2Matrix[15] = 1.0f;
1044         texture->baseTexture.target = GL_TEXTURE_RECTANGLE_ARB;
1045         texture->baseTexture.cond_np2 = TRUE;
1046
1047         if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1048         {
1049             texture->baseTexture.minMipLookup = minMipLookup_noMip;
1050         }
1051         else
1052         {
1053             texture->baseTexture.minMipLookup = minMipLookup_noFilter;
1054         }
1055     }
1056     else
1057     {
1058         if ((width != pow2_width) || (height != pow2_height))
1059         {
1060             texture->baseTexture.pow2Matrix_identity = FALSE;
1061             texture->baseTexture.pow2Matrix[0] = (((float)width) / ((float)pow2_width));
1062             texture->baseTexture.pow2Matrix[5] = (((float)height) / ((float)pow2_height));
1063         }
1064         else
1065         {
1066             texture->baseTexture.pow2Matrix[0] = 1.0f;
1067             texture->baseTexture.pow2Matrix[5] = 1.0f;
1068         }
1069
1070         texture->baseTexture.pow2Matrix[10] = 1.0f;
1071         texture->baseTexture.pow2Matrix[15] = 1.0f;
1072         texture->baseTexture.target = GL_TEXTURE_2D;
1073         texture->baseTexture.cond_np2 = FALSE;
1074     }
1075     TRACE("xf(%f) yf(%f)\n", texture->baseTexture.pow2Matrix[0], texture->baseTexture.pow2Matrix[5]);
1076
1077     /* Generate all the surfaces. */
1078     tmp_w = width;
1079     tmp_h = height;
1080     for (i = 0; i < texture->baseTexture.level_count; ++i)
1081     {
1082         IWineD3DSurface *surface;
1083
1084         /* Use the callback to create the texture surface. */
1085         hr = IWineD3DDeviceParent_CreateSurface(device->device_parent, parent, tmp_w, tmp_h,
1086                 format->id, usage, pool, i, 0, &surface);
1087         if (FAILED(hr))
1088         {
1089             FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1090             wined3d_texture_cleanup(texture);
1091             return hr;
1092         }
1093
1094         surface_set_container((IWineD3DSurfaceImpl *)surface, WINED3D_CONTAINER_TEXTURE, texture);
1095         surface_set_texture_target((IWineD3DSurfaceImpl *)surface, texture->baseTexture.target);
1096         texture->baseTexture.sub_resources[i] = &((IWineD3DSurfaceImpl *)surface)->resource;
1097         TRACE("Created surface level %u @ %p.\n", i, surface);
1098         /* Calculate the next mipmap level. */
1099         tmp_w = max(1, tmp_w >> 1);
1100         tmp_h = max(1, tmp_h >> 1);
1101     }
1102
1103     return WINED3D_OK;
1104 }
1105
1106 /* Context activation is done by the caller. */
1107 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1108         const struct wined3d_gl_info *gl_info, BOOL srgb)
1109 {
1110     BOOL dummy;
1111
1112     TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
1113
1114     return wined3d_texture_bind(texture, gl_info, srgb, &dummy);
1115 }
1116
1117 /* Do not call while under the GL lock. */
1118 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1119 {
1120     IWineD3DDeviceImpl *device = texture->resource.device;
1121     struct wined3d_context *context = NULL;
1122     BOOL srgb_mode = texture->baseTexture.is_srgb;
1123     BOOL srgb_was_toggled = FALSE;
1124     unsigned int i;
1125
1126     TRACE("texture %p, srgb %#x.\n", texture, srgb);
1127
1128     if (!device->isInDraw)
1129         context = context_acquire(device, NULL);
1130     else if (texture->baseTexture.bindCount > 0)
1131     {
1132         srgb_mode = device->stateBlock->state.sampler_states[texture->baseTexture.sampler][WINED3DSAMP_SRGBTEXTURE];
1133         srgb_was_toggled = texture->baseTexture.is_srgb != srgb_mode;
1134         texture->baseTexture.is_srgb = srgb_mode;
1135     }
1136
1137     /* If the texture is marked dirty or the sRGB sampler setting has changed
1138      * since the last load then reload the volumes. */
1139     if (texture->baseTexture.texture_rgb.dirty)
1140     {
1141         for (i = 0; i < texture->baseTexture.level_count; ++i)
1142         {
1143             volume_load(volume_from_resource(texture->baseTexture.sub_resources[i]), i, srgb_mode);
1144         }
1145     }
1146     else if (srgb_was_toggled)
1147     {
1148         for (i = 0; i < texture->baseTexture.level_count; ++i)
1149         {
1150             IWineD3DVolumeImpl *volume = volume_from_resource(texture->baseTexture.sub_resources[i]);
1151             volume_add_dirty_box(volume, NULL);
1152             volume_load(volume, i, srgb_mode);
1153         }
1154     }
1155     else
1156     {
1157         TRACE("Texture %p not dirty, nothing to do.\n", texture);
1158     }
1159
1160     if (context)
1161         context_release(context);
1162
1163     /* No longer dirty */
1164     texture->baseTexture.texture_rgb.dirty = FALSE;
1165 }
1166
1167 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1168         const WINED3DBOX *dirty_region)
1169 {
1170     volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1171 }
1172
1173 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1174 {
1175     IWineD3DVolumeImpl *volume = volume_from_resource(sub_resource);
1176
1177     /* Cleanup the container. */
1178     volume_set_container(volume, NULL);
1179     IWineD3DVolume_Release((IWineD3DVolume *)volume);
1180 }
1181
1182 /* Do not call while under the GL lock. */
1183 static void texture3d_unload(struct wined3d_resource *resource)
1184 {
1185     struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1186     UINT i;
1187
1188     TRACE("texture %p.\n", texture);
1189
1190     for (i = 0; i < texture->baseTexture.level_count; ++i)
1191     {
1192         struct wined3d_resource *sub_resource = texture->baseTexture.sub_resources[i];
1193         sub_resource->resource_ops->resource_unload(sub_resource);
1194     }
1195
1196     wined3d_texture_unload(texture);
1197 }
1198
1199 static const struct wined3d_texture_ops texture3d_ops =
1200 {
1201     texture3d_bind,
1202     texture3d_preload,
1203     texture3d_sub_resource_add_dirty_region,
1204     texture3d_sub_resource_cleanup,
1205 };
1206
1207 static const struct wined3d_resource_ops texture3d_resource_ops =
1208 {
1209     texture3d_unload,
1210 };
1211
1212 HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1213         UINT depth, UINT levels, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
1214         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1215 {
1216     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1217     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1218     UINT tmp_w, tmp_h, tmp_d;
1219     unsigned int i;
1220     HRESULT hr;
1221
1222     /* TODO: It should only be possible to create textures for formats
1223      * that are reported as supported. */
1224     if (WINED3DFMT_UNKNOWN >= format_id)
1225     {
1226         WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1227         return WINED3DERR_INVALIDCALL;
1228     }
1229
1230     if (!gl_info->supported[EXT_TEXTURE3D])
1231     {
1232         WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1233         return WINED3DERR_INVALIDCALL;
1234     }
1235
1236     /* Calculate levels for mip mapping. */
1237     if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1238     {
1239         if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1240         {
1241             WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1242             return WINED3DERR_INVALIDCALL;
1243         }
1244
1245         if (levels > 1)
1246         {
1247             WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1248             return WINED3DERR_INVALIDCALL;
1249         }
1250
1251         levels = 1;
1252     }
1253     else if (!levels)
1254     {
1255         levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1256         TRACE("Calculated levels = %u.\n", levels);
1257     }
1258
1259     hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1260             WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
1261             parent, parent_ops, &texture3d_resource_ops);
1262     if (FAILED(hr))
1263     {
1264         WARN("Failed to initialize texture, returning %#x.\n", hr);
1265         return hr;
1266     }
1267
1268     /* Is NP2 support for volumes needed? */
1269     texture->baseTexture.pow2Matrix[0] = 1.0f;
1270     texture->baseTexture.pow2Matrix[5] = 1.0f;
1271     texture->baseTexture.pow2Matrix[10] = 1.0f;
1272     texture->baseTexture.pow2Matrix[15] = 1.0f;
1273     texture->baseTexture.target = GL_TEXTURE_3D;
1274
1275     /* Generate all the surfaces. */
1276     tmp_w = width;
1277     tmp_h = height;
1278     tmp_d = depth;
1279
1280     for (i = 0; i < texture->baseTexture.level_count; ++i)
1281     {
1282         IWineD3DVolume *volume;
1283
1284         /* Create the volume. */
1285         hr = IWineD3DDeviceParent_CreateVolume(device->device_parent, parent,
1286                 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1287         if (FAILED(hr))
1288         {
1289             ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1290             wined3d_texture_cleanup(texture);
1291             return hr;
1292         }
1293
1294         /* Set its container to this texture. */
1295         volume_set_container((IWineD3DVolumeImpl *)volume, texture);
1296         texture->baseTexture.sub_resources[i] = &((IWineD3DVolumeImpl *)volume)->resource;
1297
1298         /* Calculate the next mipmap level. */
1299         tmp_w = max(1, tmp_w >> 1);
1300         tmp_h = max(1, tmp_h >> 1);
1301         tmp_d = max(1, tmp_d >> 1);
1302     }
1303
1304     return WINED3D_OK;
1305 }