user32: Prefer loading color cursors in LoadImage.
[wine.git] / dlls / wined3d / texture.c
blob9bf4c2ee393822c73286cff929a585a3db6c386b
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
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
29 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
30 UINT layer_count, UINT level_count, enum wined3d_resource_type resource_type, struct wined3d_device *device,
31 DWORD usage, const struct wined3d_format *format, enum wined3d_pool pool, void *parent,
32 const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
34 HRESULT hr;
36 hr = resource_init(&texture->resource, device, resource_type, format,
37 WINED3D_MULTISAMPLE_NONE, 0, usage, pool, 0, 0, 0, 0,
38 parent, parent_ops, resource_ops);
39 if (FAILED(hr))
41 WARN("Failed to initialize resource, returning %#x\n", hr);
42 return hr;
45 texture->texture_ops = texture_ops;
46 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
47 level_count * layer_count * sizeof(*texture->sub_resources));
48 if (!texture->sub_resources)
50 ERR("Failed to allocate sub-resource array.\n");
51 resource_cleanup(&texture->resource);
52 return E_OUTOFMEMORY;
55 texture->layer_count = layer_count;
56 texture->level_count = level_count;
57 texture->filter_type = (usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
58 texture->lod = 0;
59 texture->texture_rgb.dirty = TRUE;
60 texture->texture_srgb.dirty = TRUE;
61 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
63 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
65 texture->min_mip_lookup = minMipLookup;
66 texture->mag_lookup = magLookup;
68 else
70 texture->min_mip_lookup = minMipLookup_noFilter;
71 texture->mag_lookup = magLookup_noFilter;
74 return WINED3D_OK;
77 /* A GL context is provided by the caller */
78 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
80 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
81 tex->name = 0;
84 static void wined3d_texture_unload(struct wined3d_texture *texture)
86 struct wined3d_device *device = texture->resource.device;
87 struct wined3d_context *context = NULL;
89 if (texture->texture_rgb.name || texture->texture_srgb.name)
91 context = context_acquire(device, NULL);
94 if (texture->texture_rgb.name)
95 gltexture_delete(context->gl_info, &texture->texture_rgb);
97 if (texture->texture_srgb.name)
98 gltexture_delete(context->gl_info, &texture->texture_srgb);
100 if (context) context_release(context);
102 wined3d_texture_set_dirty(texture, TRUE);
104 resource_unload(&texture->resource);
107 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
109 UINT sub_count = texture->level_count * texture->layer_count;
110 UINT i;
112 TRACE("texture %p.\n", texture);
114 for (i = 0; i < sub_count; ++i)
116 struct wined3d_resource *sub_resource = texture->sub_resources[i];
118 if (sub_resource)
119 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
122 wined3d_texture_unload(texture);
123 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
124 resource_cleanup(&texture->resource);
127 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
129 texture->texture_rgb.dirty = dirty;
130 texture->texture_srgb.dirty = dirty;
133 /* Context activation is done by the caller. */
134 static HRESULT wined3d_texture_bind(struct wined3d_texture *texture,
135 struct wined3d_context *context, BOOL srgb, BOOL *set_surface_desc)
137 const struct wined3d_gl_info *gl_info = context->gl_info;
138 struct gl_texture *gl_tex;
139 BOOL new_texture = FALSE;
140 HRESULT hr = WINED3D_OK;
141 GLenum target;
143 TRACE("texture %p, context %p, srgb %#x, set_surface_desc %p.\n", texture, context, srgb, set_surface_desc);
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;
151 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb);
152 target = texture->target;
154 /* Generate a texture name if we don't already have one. */
155 if (!gl_tex->name)
157 *set_surface_desc = TRUE;
158 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
159 checkGLcall("glGenTextures");
160 TRACE("Generated texture %d.\n", gl_tex->name);
161 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
163 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
164 GLclampf tmp = 0.9f;
165 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
167 /* Initialise the state of the texture object to the OpenGL defaults,
168 * not the D3D defaults. */
169 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
170 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
171 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
172 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
173 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
174 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
175 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
176 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
177 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
178 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
179 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
180 else
181 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
182 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
183 wined3d_texture_set_dirty(texture, TRUE);
184 new_texture = TRUE;
186 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
188 /* This means double binding the texture at creation, but keeps
189 * the code simpler all in all, and the run-time path free from
190 * additional checks. */
191 context_bind_texture(context, target, gl_tex->name);
192 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
193 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
196 else
198 *set_surface_desc = FALSE;
201 if (gl_tex->name)
203 context_bind_texture(context, target, gl_tex->name);
204 if (new_texture)
206 /* For a new texture we have to set the texture levels after
207 * binding the texture. Beware that texture rectangles do not
208 * support mipmapping, but set the maxmiplevel if we're relying
209 * on the partial GL_ARB_texture_non_power_of_two emulation with
210 * texture rectangles. (I.e., do not care about cond_np2 here,
211 * just look for GL_TEXTURE_RECTANGLE_ARB.) */
212 if (target != GL_TEXTURE_RECTANGLE_ARB)
214 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
215 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
216 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
218 if (target == GL_TEXTURE_CUBE_MAP_ARB)
220 /* Cubemaps are always set to clamp, regardless of the sampler state. */
221 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
222 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
223 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
227 else
229 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
230 hr = WINED3DERR_INVALIDCALL;
233 return hr;
236 /* Context activation is done by the caller. */
237 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
238 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
240 GLint gl_wrap;
242 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
244 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
245 return;
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 == WINED3D_TADDRESS_WRAP))
251 gl_wrap = GL_CLAMP_TO_EDGE;
252 else
253 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
255 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
256 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
257 checkGLcall("glTexParameteri(target, param, gl_wrap)");
260 /* Context activation 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)
265 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
266 GLenum target = texture->target;
267 struct gl_texture *gl_tex;
268 DWORD state;
269 DWORD aniso;
271 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
273 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
274 texture->flags & WINED3D_TEXTURE_IS_SRGB);
276 /* This function relies on the correct texture being bound and loaded. */
278 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
280 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
281 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
282 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
285 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
287 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
288 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
289 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
292 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
294 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
295 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
296 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
299 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
301 float col[4];
303 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
304 D3DCOLORTOGLFLOAT4(state, col);
305 TRACE("Setting border color for %#x to %#x.\n", target, state);
306 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
307 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
308 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
311 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
313 GLint gl_value;
315 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
316 if (state > WINED3D_TEXF_ANISOTROPIC)
317 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
319 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
320 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
321 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
322 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
324 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
327 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
328 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
329 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
331 GLint gl_value;
333 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
334 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
335 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
337 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
338 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
340 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
341 gl_tex->states[WINED3DTEXSTA_MINFILTER],
342 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
344 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
345 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
346 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
348 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
349 sampler_states[WINED3D_SAMP_MIN_FILTER],
350 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
351 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
352 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
354 if (!cond_np2)
356 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
357 gl_value = texture->lod;
358 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
359 gl_value = texture->level_count - 1;
360 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
361 /* texture->lod is already clamped in the setter. */
362 gl_value = texture->lod;
363 else
364 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
366 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
367 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
368 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
369 * corresponds to GL_TEXTURE_BASE_LEVEL. */
370 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
374 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
375 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
376 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
377 || cond_np2)
378 aniso = 1;
379 else
380 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
382 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
384 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
386 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
387 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
389 else
391 WARN("Anisotropic filtering not supported.\n");
393 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
396 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
397 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
399 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
400 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
401 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
402 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
405 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
406 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
408 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
410 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
411 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
412 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
413 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
415 else
417 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
418 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
419 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
424 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
426 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
428 TRACE("%p increasing refcount to %u.\n", texture, refcount);
430 return refcount;
433 /* Do not call while under the GL lock. */
434 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
436 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
438 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
440 if (!refcount)
442 wined3d_texture_cleanup(texture);
443 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
444 HeapFree(GetProcessHeap(), 0, texture);
447 return refcount;
450 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
452 TRACE("texture %p.\n", texture);
454 return &texture->resource;
457 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
459 return resource_set_priority(&texture->resource, priority);
462 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
464 return resource_get_priority(&texture->resource);
467 /* Do not call while under the GL lock. */
468 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
470 texture->texture_ops->texture_preload(texture, SRGB_ANY);
473 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
475 TRACE("texture %p.\n", texture);
477 return texture->resource.parent;
480 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
482 DWORD old = texture->lod;
484 TRACE("texture %p, lod %u.\n", texture, lod);
486 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
487 * textures. The call always returns 0, and GetLOD always returns 0. */
488 if (texture->resource.pool != WINED3D_POOL_MANAGED)
490 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
491 return 0;
494 if (lod >= texture->level_count)
495 lod = texture->level_count - 1;
497 if (texture->lod != lod)
499 texture->lod = lod;
501 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
502 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
503 if (texture->resource.bind_count)
504 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
507 return old;
510 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
512 TRACE("texture %p, returning %u.\n", texture, texture->lod);
514 return texture->lod;
517 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
519 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
521 return texture->level_count;
524 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
525 enum wined3d_texture_filter_type filter_type)
527 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
529 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
531 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
532 return WINED3DERR_INVALIDCALL;
535 texture->filter_type = filter_type;
537 return WINED3D_OK;
540 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
542 TRACE("texture %p.\n", texture);
544 return texture->filter_type;
547 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
549 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
550 FIXME("texture %p stub!\n", texture);
553 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
554 UINT sub_resource_idx)
556 UINT sub_count = texture->level_count * texture->layer_count;
558 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
560 if (sub_resource_idx >= sub_count)
562 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
563 return NULL;
566 return texture->sub_resources[sub_resource_idx];
569 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
570 UINT layer, const struct wined3d_box *dirty_region)
572 struct wined3d_resource *sub_resource;
574 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
576 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
578 WARN("Failed to get sub-resource.\n");
579 return WINED3DERR_INVALIDCALL;
582 wined3d_texture_set_dirty(texture, TRUE);
583 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
585 return WINED3D_OK;
588 /* Context activation is done by the caller. */
589 static HRESULT texture2d_bind(struct wined3d_texture *texture,
590 struct wined3d_context *context, BOOL srgb)
592 const struct wined3d_gl_info *gl_info = context->gl_info;
593 BOOL set_gl_texture_desc;
594 HRESULT hr;
596 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
598 hr = wined3d_texture_bind(texture, context, srgb, &set_gl_texture_desc);
599 if (set_gl_texture_desc && SUCCEEDED(hr))
601 UINT sub_count = texture->level_count * texture->layer_count;
602 BOOL srgb_tex = !context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
603 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
604 struct gl_texture *gl_tex;
605 UINT i;
607 gl_tex = wined3d_texture_get_gl_texture(texture, context->gl_info, srgb_tex);
609 for (i = 0; i < sub_count; ++i)
611 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
612 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
615 /* Conditinal non power of two textures use a different clamping
616 * default. If we're using the GL_WINE_normalized_texrect partial
617 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
618 * has the address mode set to repeat - something that prevents us
619 * from hitting the accelerated codepath. Thus manually set the GL
620 * state. The same applies to filtering. Even if the texture has only
621 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
622 * fallback on macos. */
623 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
625 GLenum target = texture->target;
627 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
628 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
629 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
630 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
631 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
632 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
633 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
634 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
635 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
636 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
637 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
638 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
639 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
643 return hr;
646 static BOOL texture_srgb_mode(const struct wined3d_texture *texture, enum WINED3DSRGB srgb)
648 switch (srgb)
650 case SRGB_RGB:
651 return FALSE;
653 case SRGB_SRGB:
654 return TRUE;
656 default:
657 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
661 /* Do not call while under the GL lock. */
662 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
664 UINT sub_count = texture->level_count * texture->layer_count;
665 struct wined3d_device *device = texture->resource.device;
666 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
667 struct wined3d_context *context = NULL;
668 struct gl_texture *gl_tex;
669 BOOL srgb_mode;
670 UINT i;
672 TRACE("texture %p, srgb %#x.\n", texture, srgb);
674 srgb_mode = texture_srgb_mode(texture, srgb);
675 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
677 if (!device->isInDraw)
679 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
680 * when loading offscreen render targets into the texture. */
681 context = context_acquire(device, NULL);
684 if (gl_tex->dirty)
686 /* Reload the surfaces if the texture is marked dirty. */
687 for (i = 0; i < sub_count; ++i)
689 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
692 else
694 TRACE("Texture %p not dirty, nothing to do.\n", texture);
697 /* No longer dirty. */
698 gl_tex->dirty = FALSE;
700 if (context) context_release(context);
703 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
704 const struct wined3d_box *dirty_region)
706 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
709 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
711 struct wined3d_surface *surface = surface_from_resource(sub_resource);
713 /* Clean out the texture name we gave to the surface so that the
714 * surface doesn't try and release it. */
715 surface_set_texture_name(surface, 0, TRUE);
716 surface_set_texture_name(surface, 0, FALSE);
717 surface_set_texture_target(surface, 0, 0);
718 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
719 wined3d_surface_decref(surface);
722 /* Do not call while under the GL lock. */
723 static void texture2d_unload(struct wined3d_resource *resource)
725 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
726 UINT sub_count = texture->level_count * texture->layer_count;
727 UINT i;
729 TRACE("texture %p.\n", texture);
731 for (i = 0; i < sub_count; ++i)
733 struct wined3d_resource *sub_resource = texture->sub_resources[i];
734 struct wined3d_surface *surface = surface_from_resource(sub_resource);
736 sub_resource->resource_ops->resource_unload(sub_resource);
737 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
738 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
741 wined3d_texture_unload(texture);
744 static const struct wined3d_texture_ops texture2d_ops =
746 texture2d_bind,
747 texture2d_preload,
748 texture2d_sub_resource_add_dirty_region,
749 texture2d_sub_resource_cleanup,
752 static const struct wined3d_resource_ops texture2d_resource_ops =
754 texture2d_unload,
757 static HRESULT cubetexture_init(struct wined3d_texture *texture, UINT edge_length, UINT levels,
758 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
759 void *parent, const struct wined3d_parent_ops *parent_ops)
761 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
762 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
763 UINT pow2_edge_length;
764 unsigned int i, j;
765 UINT tmp_w;
766 HRESULT hr;
768 /* TODO: It should only be possible to create textures for formats
769 * that are reported as supported. */
770 if (WINED3DFMT_UNKNOWN >= format_id)
772 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
773 return WINED3DERR_INVALIDCALL;
776 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3D_POOL_SCRATCH)
778 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
779 return WINED3DERR_INVALIDCALL;
782 /* Calculate levels for mip mapping */
783 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
785 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
787 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
788 return WINED3DERR_INVALIDCALL;
791 if (levels > 1)
793 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
794 return WINED3DERR_INVALIDCALL;
797 levels = 1;
799 else if (!levels)
801 levels = wined3d_log2i(edge_length) + 1;
802 TRACE("Calculated levels = %u.\n", levels);
805 hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
806 WINED3D_RTYPE_CUBE_TEXTURE, device, usage, format, pool,
807 parent, parent_ops, &texture2d_resource_ops);
808 if (FAILED(hr))
810 WARN("Failed to initialize texture, returning %#x\n", hr);
811 return hr;
814 /* Find the nearest pow2 match. */
815 pow2_edge_length = 1;
816 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
818 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
820 /* Precalculated scaling for 'faked' non power of two texture coords. */
821 texture->pow2_matrix[0] = 1.0f;
822 texture->pow2_matrix[5] = 1.0f;
823 texture->pow2_matrix[10] = 1.0f;
824 texture->pow2_matrix[15] = 1.0f;
826 else
828 /* Precalculated scaling for 'faked' non power of two texture coords. */
829 texture->pow2_matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
830 texture->pow2_matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
831 texture->pow2_matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
832 texture->pow2_matrix[15] = 1.0f;
833 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
835 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
837 /* Generate all the surfaces. */
838 tmp_w = edge_length;
839 for (i = 0; i < texture->level_count; ++i)
841 /* Create the 6 faces. */
842 for (j = 0; j < 6; ++j)
844 static const GLenum cube_targets[6] =
846 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
847 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
848 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
849 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
850 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
851 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
853 UINT idx = j * texture->level_count + i;
854 struct wined3d_surface *surface;
856 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
857 parent, tmp_w, tmp_w, format_id, usage, pool, idx, &surface)))
859 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
860 wined3d_texture_cleanup(texture);
861 return hr;
864 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
865 surface_set_texture_target(surface, cube_targets[j], i);
866 texture->sub_resources[idx] = &surface->resource;
867 TRACE("Created surface level %u @ %p.\n", i, surface);
869 tmp_w = max(1, tmp_w >> 1);
872 return WINED3D_OK;
875 static HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
876 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
877 void *parent, const struct wined3d_parent_ops *parent_ops)
879 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
880 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
881 UINT pow2_width, pow2_height;
882 UINT tmp_w, tmp_h;
883 unsigned int i;
884 HRESULT hr;
886 /* TODO: It should only be possible to create textures for formats
887 * that are reported as supported. */
888 if (WINED3DFMT_UNKNOWN >= format_id)
890 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
891 return WINED3DERR_INVALIDCALL;
894 /* Non-power2 support. */
895 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
897 pow2_width = width;
898 pow2_height = height;
900 else
902 /* Find the nearest pow2 match. */
903 pow2_width = pow2_height = 1;
904 while (pow2_width < width) pow2_width <<= 1;
905 while (pow2_height < height) pow2_height <<= 1;
907 if (pow2_width != width || pow2_height != height)
909 if (levels > 1)
911 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
912 return WINED3DERR_INVALIDCALL;
914 levels = 1;
918 /* Calculate levels for mip mapping. */
919 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
921 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
923 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
924 return WINED3DERR_INVALIDCALL;
927 if (levels > 1)
929 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
930 return WINED3DERR_INVALIDCALL;
933 levels = 1;
935 else if (!levels)
937 levels = wined3d_log2i(max(width, height)) + 1;
938 TRACE("Calculated levels = %u.\n", levels);
941 hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
942 WINED3D_RTYPE_TEXTURE, device, usage, format, pool,
943 parent, parent_ops, &texture2d_resource_ops);
944 if (FAILED(hr))
946 WARN("Failed to initialize texture, returning %#x.\n", hr);
947 return hr;
950 /* Precalculated scaling for 'faked' non power of two texture coords.
951 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
952 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
953 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
954 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
956 texture->pow2_matrix[0] = 1.0f;
957 texture->pow2_matrix[5] = 1.0f;
958 texture->pow2_matrix[10] = 1.0f;
959 texture->pow2_matrix[15] = 1.0f;
960 texture->target = GL_TEXTURE_2D;
961 texture->flags |= WINED3D_TEXTURE_COND_NP2;
962 texture->min_mip_lookup = minMipLookup_noFilter;
964 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
965 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
966 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
968 if (width != 1 || height != 1)
969 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
971 texture->pow2_matrix[0] = (float)width;
972 texture->pow2_matrix[5] = (float)height;
973 texture->pow2_matrix[10] = 1.0f;
974 texture->pow2_matrix[15] = 1.0f;
975 texture->target = GL_TEXTURE_RECTANGLE_ARB;
976 texture->flags |= WINED3D_TEXTURE_COND_NP2;
978 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
979 texture->min_mip_lookup = minMipLookup_noMip;
980 else
981 texture->min_mip_lookup = minMipLookup_noFilter;
983 else
985 if ((width != pow2_width) || (height != pow2_height))
987 texture->pow2_matrix[0] = (((float)width) / ((float)pow2_width));
988 texture->pow2_matrix[5] = (((float)height) / ((float)pow2_height));
989 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
991 else
993 texture->pow2_matrix[0] = 1.0f;
994 texture->pow2_matrix[5] = 1.0f;
997 texture->pow2_matrix[10] = 1.0f;
998 texture->pow2_matrix[15] = 1.0f;
999 texture->target = GL_TEXTURE_2D;
1001 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1003 /* Generate all the surfaces. */
1004 tmp_w = width;
1005 tmp_h = height;
1006 for (i = 0; i < texture->level_count; ++i)
1008 struct wined3d_surface *surface;
1010 /* Use the callback to create the texture surface. */
1011 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
1012 parent, tmp_w, tmp_h, format->id, usage, pool, i, &surface)))
1014 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1015 wined3d_texture_cleanup(texture);
1016 return hr;
1019 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
1020 surface_set_texture_target(surface, texture->target, i);
1021 texture->sub_resources[i] = &surface->resource;
1022 TRACE("Created surface level %u @ %p.\n", i, surface);
1023 /* Calculate the next mipmap level. */
1024 tmp_w = max(1, tmp_w >> 1);
1025 tmp_h = max(1, tmp_h >> 1);
1028 return WINED3D_OK;
1031 /* Context activation is done by the caller. */
1032 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1033 struct wined3d_context *context, BOOL srgb)
1035 BOOL dummy;
1037 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1039 return wined3d_texture_bind(texture, context, srgb, &dummy);
1042 /* Do not call while under the GL lock. */
1043 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1045 struct wined3d_device *device = texture->resource.device;
1046 struct wined3d_context *context;
1047 BOOL srgb_was_toggled = FALSE;
1048 unsigned int i;
1050 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1052 /* TODO: Use already acquired context when possible. */
1053 context = context_acquire(device, NULL);
1054 if (texture->resource.bind_count > 0)
1056 BOOL texture_srgb = texture->flags & WINED3D_TEXTURE_IS_SRGB;
1057 BOOL sampler_srgb = texture_srgb_mode(texture, srgb);
1058 srgb_was_toggled = !texture_srgb != !sampler_srgb;
1060 if (srgb_was_toggled)
1062 if (sampler_srgb)
1063 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
1064 else
1065 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
1069 /* If the texture is marked dirty or the sRGB sampler setting has changed
1070 * since the last load then reload the volumes. */
1071 if (texture->texture_rgb.dirty)
1073 for (i = 0; i < texture->level_count; ++i)
1075 volume_load(volume_from_resource(texture->sub_resources[i]), context, i,
1076 texture->flags & WINED3D_TEXTURE_IS_SRGB);
1079 else if (srgb_was_toggled)
1081 for (i = 0; i < texture->level_count; ++i)
1083 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1084 volume_add_dirty_box(volume, NULL);
1085 volume_load(volume, context, i, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1088 else
1090 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1093 context_release(context);
1095 /* No longer dirty */
1096 texture->texture_rgb.dirty = FALSE;
1099 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1100 const struct wined3d_box *dirty_region)
1102 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1105 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1107 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1109 /* Cleanup the container. */
1110 volume_set_container(volume, NULL);
1111 wined3d_volume_decref(volume);
1114 /* Do not call while under the GL lock. */
1115 static void texture3d_unload(struct wined3d_resource *resource)
1117 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1118 UINT i;
1120 TRACE("texture %p.\n", texture);
1122 for (i = 0; i < texture->level_count; ++i)
1124 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1125 sub_resource->resource_ops->resource_unload(sub_resource);
1128 wined3d_texture_unload(texture);
1131 static const struct wined3d_texture_ops texture3d_ops =
1133 texture3d_bind,
1134 texture3d_preload,
1135 texture3d_sub_resource_add_dirty_region,
1136 texture3d_sub_resource_cleanup,
1139 static const struct wined3d_resource_ops texture3d_resource_ops =
1141 texture3d_unload,
1144 static HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1145 UINT depth, UINT levels, struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id,
1146 enum wined3d_pool pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1148 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1149 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1150 UINT tmp_w, tmp_h, tmp_d;
1151 unsigned int i;
1152 HRESULT hr;
1154 /* TODO: It should only be possible to create textures for formats
1155 * that are reported as supported. */
1156 if (WINED3DFMT_UNKNOWN >= format_id)
1158 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1159 return WINED3DERR_INVALIDCALL;
1162 if (!gl_info->supported[EXT_TEXTURE3D])
1164 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1165 return WINED3DERR_INVALIDCALL;
1168 /* Calculate levels for mip mapping. */
1169 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1171 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1173 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1174 return WINED3DERR_INVALIDCALL;
1177 if (levels > 1)
1179 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1180 return WINED3DERR_INVALIDCALL;
1183 levels = 1;
1185 else if (!levels)
1187 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1188 TRACE("Calculated levels = %u.\n", levels);
1191 hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1192 WINED3D_RTYPE_VOLUME_TEXTURE, device, usage, format, pool,
1193 parent, parent_ops, &texture3d_resource_ops);
1194 if (FAILED(hr))
1196 WARN("Failed to initialize texture, returning %#x.\n", hr);
1197 return hr;
1200 /* Is NP2 support for volumes needed? */
1201 texture->pow2_matrix[0] = 1.0f;
1202 texture->pow2_matrix[5] = 1.0f;
1203 texture->pow2_matrix[10] = 1.0f;
1204 texture->pow2_matrix[15] = 1.0f;
1205 texture->target = GL_TEXTURE_3D;
1207 /* Generate all the surfaces. */
1208 tmp_w = width;
1209 tmp_h = height;
1210 tmp_d = depth;
1212 for (i = 0; i < texture->level_count; ++i)
1214 struct wined3d_volume *volume;
1216 /* Create the volume. */
1217 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1218 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1219 if (FAILED(hr))
1221 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1222 wined3d_texture_cleanup(texture);
1223 return hr;
1226 /* Set its container to this texture. */
1227 volume_set_container(volume, texture);
1228 texture->sub_resources[i] = &volume->resource;
1230 /* Calculate the next mipmap level. */
1231 tmp_w = max(1, tmp_w >> 1);
1232 tmp_h = max(1, tmp_h >> 1);
1233 tmp_d = max(1, tmp_d >> 1);
1236 return WINED3D_OK;
1239 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, UINT width, UINT height,
1240 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1241 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1243 struct wined3d_texture *object;
1244 HRESULT hr;
1246 TRACE("device %p, width %u, height %u, level_count %u, usage %#x\n",
1247 device, width, height, level_count, usage);
1248 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1249 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1251 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1252 if (!object)
1254 *texture = NULL;
1255 return WINED3DERR_OUTOFVIDEOMEMORY;
1258 hr = texture_init(object, width, height, level_count,
1259 device, usage, format_id, pool, parent, parent_ops);
1260 if (FAILED(hr))
1262 WARN("Failed to initialize texture, returning %#x.\n", hr);
1263 HeapFree(GetProcessHeap(), 0, object);
1264 *texture = NULL;
1265 return hr;
1268 TRACE("Created texture %p.\n", object);
1269 *texture = object;
1271 return WINED3D_OK;
1274 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, UINT width, UINT height, UINT depth,
1275 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1276 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1278 struct wined3d_texture *object;
1279 HRESULT hr;
1281 TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1282 device, width, height, depth, level_count, usage);
1283 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1284 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1286 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1287 if (!object)
1289 *texture = NULL;
1290 return WINED3DERR_OUTOFVIDEOMEMORY;
1293 hr = volumetexture_init(object, width, height, depth, level_count,
1294 device, usage, format_id, pool, parent, parent_ops);
1295 if (FAILED(hr))
1297 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1298 HeapFree(GetProcessHeap(), 0, object);
1299 *texture = NULL;
1300 return hr;
1303 TRACE("Created texture %p.\n", object);
1304 *texture = object;
1306 return WINED3D_OK;
1309 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, UINT edge_length,
1310 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1311 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1313 struct wined3d_texture *object;
1314 HRESULT hr;
1316 TRACE("device %p, edge_length %u, level_count %u, usage %#x\n",
1317 device, edge_length, level_count, usage);
1318 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1319 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1321 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1322 if (!object)
1324 *texture = NULL;
1325 return WINED3DERR_OUTOFVIDEOMEMORY;
1328 hr = cubetexture_init(object, edge_length, level_count,
1329 device, usage, format_id, pool, parent, parent_ops);
1330 if (FAILED(hr))
1332 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1333 HeapFree(GetProcessHeap(), 0, object);
1334 *texture = NULL;
1335 return hr;
1338 TRACE("Created texture %p.\n", object);
1339 *texture = object;
1341 return WINED3D_OK;