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