wined3d: Pass a wined3d_resource_desc structure to device_parent_create_texture_surfa...
[wine/multimedia.git] / dlls / wined3d / texture.c
blobe4ef6e877cab17fedf39b1d73965cc3571fece72
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 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, UINT width, UINT height, UINT depth,
32 void *parent, 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, width, height, depth, 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, 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 struct wined3d_resource_desc desc;
764 unsigned int i, j;
765 HRESULT hr;
767 /* TODO: It should only be possible to create textures for formats
768 * that are reported as supported. */
769 if (WINED3DFMT_UNKNOWN >= format_id)
771 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
772 return WINED3DERR_INVALIDCALL;
775 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3D_POOL_SCRATCH)
777 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
778 return WINED3DERR_INVALIDCALL;
781 /* Calculate levels for mip mapping */
782 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
784 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
786 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
787 return WINED3DERR_INVALIDCALL;
790 if (levels > 1)
792 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
793 return WINED3DERR_INVALIDCALL;
796 levels = 1;
798 else if (!levels)
800 levels = wined3d_log2i(edge_length) + 1;
801 TRACE("Calculated levels = %u.\n", levels);
804 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
806 UINT pow2_edge_length = 1;
807 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
809 if (edge_length != pow2_edge_length)
811 if (pool == WINED3D_POOL_SCRATCH)
813 /* SCRATCH textures cannot be used for texturing */
814 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
816 else
818 WARN("Attempted to create a NPOT cube texture (edge_length=%u) without GL support.\n",
819 edge_length);
820 return WINED3DERR_INVALIDCALL;
825 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels, WINED3D_RTYPE_CUBE_TEXTURE, device,
826 usage, format, pool, edge_length, edge_length, 1, parent, parent_ops, &texture2d_resource_ops)))
828 WARN("Failed to initialize texture, returning %#x\n", hr);
829 return hr;
832 texture->pow2_matrix[0] = 1.0f;
833 texture->pow2_matrix[5] = 1.0f;
834 texture->pow2_matrix[10] = 1.0f;
835 texture->pow2_matrix[15] = 1.0f;
836 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
838 /* Generate all the surfaces. */
839 wined3d_resource_get_desc(&texture->resource, &desc);
840 desc.resource_type = WINED3D_RTYPE_SURFACE;
841 for (i = 0; i < texture->level_count; ++i)
843 /* Create the 6 faces. */
844 for (j = 0; j < 6; ++j)
846 static const GLenum cube_targets[6] =
848 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
849 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
850 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
851 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
852 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
853 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
855 UINT idx = j * texture->level_count + i;
856 struct wined3d_surface *surface;
858 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
859 parent, &desc, idx, &surface)))
861 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
862 wined3d_texture_cleanup(texture);
863 return hr;
866 surface_set_container(surface, texture);
867 surface_set_texture_target(surface, cube_targets[j], i);
868 texture->sub_resources[idx] = &surface->resource;
869 TRACE("Created surface level %u @ %p.\n", i, surface);
871 desc.width = max(1, desc.width >> 1);
872 desc.height = desc.width;
875 return WINED3D_OK;
878 static HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
879 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool,
880 void *parent, const struct wined3d_parent_ops *parent_ops)
882 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
883 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
884 struct wined3d_resource_desc desc;
885 UINT pow2_width, pow2_height;
886 unsigned int i;
887 HRESULT hr;
889 /* TODO: It should only be possible to create textures for formats
890 * that are reported as supported. */
891 if (WINED3DFMT_UNKNOWN >= format_id)
893 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
894 return WINED3DERR_INVALIDCALL;
897 /* Non-power2 support. */
898 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
900 pow2_width = width;
901 pow2_height = height;
903 else
905 /* Find the nearest pow2 match. */
906 pow2_width = pow2_height = 1;
907 while (pow2_width < width) pow2_width <<= 1;
908 while (pow2_height < height) pow2_height <<= 1;
910 if (pow2_width != width || pow2_height != height)
912 /* levels == 0 returns an error as well */
913 if (levels != 1)
915 if (pool == WINED3D_POOL_SCRATCH)
917 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
919 else
921 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
922 return WINED3DERR_INVALIDCALL;
928 /* Calculate levels for mip mapping. */
929 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
931 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
933 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
934 return WINED3DERR_INVALIDCALL;
937 if (levels > 1)
939 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
940 return WINED3DERR_INVALIDCALL;
943 levels = 1;
945 else if (!levels)
947 levels = wined3d_log2i(max(width, height)) + 1;
948 TRACE("Calculated levels = %u.\n", levels);
951 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels, WINED3D_RTYPE_TEXTURE, device,
952 usage, format, pool, width, height, 1, parent, parent_ops, &texture2d_resource_ops)))
954 WARN("Failed to initialize texture, returning %#x.\n", hr);
955 return hr;
958 /* Precalculated scaling for 'faked' non power of two texture coords.
959 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
960 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
961 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
962 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
964 texture->pow2_matrix[0] = 1.0f;
965 texture->pow2_matrix[5] = 1.0f;
966 texture->pow2_matrix[10] = 1.0f;
967 texture->pow2_matrix[15] = 1.0f;
968 texture->target = GL_TEXTURE_2D;
969 texture->flags |= WINED3D_TEXTURE_COND_NP2;
970 texture->min_mip_lookup = minMipLookup_noFilter;
972 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
973 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
974 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
976 texture->pow2_matrix[0] = (float)width;
977 texture->pow2_matrix[5] = (float)height;
978 texture->pow2_matrix[10] = 1.0f;
979 texture->pow2_matrix[15] = 1.0f;
980 texture->target = GL_TEXTURE_RECTANGLE_ARB;
981 texture->flags |= WINED3D_TEXTURE_COND_NP2;
982 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
984 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
985 texture->min_mip_lookup = minMipLookup_noMip;
986 else
987 texture->min_mip_lookup = minMipLookup_noFilter;
989 else
991 if ((width != pow2_width) || (height != pow2_height))
993 texture->pow2_matrix[0] = (((float)width) / ((float)pow2_width));
994 texture->pow2_matrix[5] = (((float)height) / ((float)pow2_height));
995 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
997 else
999 texture->pow2_matrix[0] = 1.0f;
1000 texture->pow2_matrix[5] = 1.0f;
1003 texture->pow2_matrix[10] = 1.0f;
1004 texture->pow2_matrix[15] = 1.0f;
1005 texture->target = GL_TEXTURE_2D;
1007 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1009 /* Generate all the surfaces. */
1010 wined3d_resource_get_desc(&texture->resource, &desc);
1011 desc.resource_type = WINED3D_RTYPE_SURFACE;
1012 for (i = 0; i < texture->level_count; ++i)
1014 struct wined3d_surface *surface;
1016 /* Use the callback to create the texture surface. */
1017 if (FAILED(hr = device->device_parent->ops->create_texture_surface(device->device_parent,
1018 parent, &desc, i, &surface)))
1020 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1021 wined3d_texture_cleanup(texture);
1022 return hr;
1025 surface_set_container(surface, texture);
1026 surface_set_texture_target(surface, texture->target, i);
1027 texture->sub_resources[i] = &surface->resource;
1028 TRACE("Created surface level %u @ %p.\n", i, surface);
1029 /* Calculate the next mipmap level. */
1030 desc.width = max(1, desc.width >> 1);
1031 desc.height = max(1, desc.height >> 1);
1034 return WINED3D_OK;
1037 /* Context activation is done by the caller. */
1038 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1039 struct wined3d_context *context, BOOL srgb)
1041 BOOL dummy;
1043 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1045 return wined3d_texture_bind(texture, context, srgb, &dummy);
1048 /* Do not call while under the GL lock. */
1049 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1051 struct wined3d_device *device = texture->resource.device;
1052 struct wined3d_context *context;
1053 BOOL srgb_was_toggled = FALSE;
1054 unsigned int i;
1056 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1058 /* TODO: Use already acquired context when possible. */
1059 context = context_acquire(device, NULL);
1060 if (texture->resource.bind_count > 0)
1062 BOOL texture_srgb = texture->flags & WINED3D_TEXTURE_IS_SRGB;
1063 BOOL sampler_srgb = texture_srgb_mode(texture, srgb);
1064 srgb_was_toggled = !texture_srgb != !sampler_srgb;
1066 if (srgb_was_toggled)
1068 if (sampler_srgb)
1069 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
1070 else
1071 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
1075 /* If the texture is marked dirty or the sRGB sampler setting has changed
1076 * since the last load then reload the volumes. */
1077 if (texture->texture_rgb.dirty)
1079 for (i = 0; i < texture->level_count; ++i)
1081 volume_load(volume_from_resource(texture->sub_resources[i]), context, i,
1082 texture->flags & WINED3D_TEXTURE_IS_SRGB);
1085 else if (srgb_was_toggled)
1087 for (i = 0; i < texture->level_count; ++i)
1089 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1090 volume_add_dirty_box(volume, NULL);
1091 volume_load(volume, context, i, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1094 else
1096 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1099 context_release(context);
1101 /* No longer dirty */
1102 texture->texture_rgb.dirty = FALSE;
1105 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1106 const struct wined3d_box *dirty_region)
1108 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1111 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1113 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1115 /* Cleanup the container. */
1116 volume_set_container(volume, NULL);
1117 wined3d_volume_decref(volume);
1120 /* Do not call while under the GL lock. */
1121 static void texture3d_unload(struct wined3d_resource *resource)
1123 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1124 UINT i;
1126 TRACE("texture %p.\n", texture);
1128 for (i = 0; i < texture->level_count; ++i)
1130 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1131 sub_resource->resource_ops->resource_unload(sub_resource);
1134 wined3d_texture_unload(texture);
1137 static const struct wined3d_texture_ops texture3d_ops =
1139 texture3d_bind,
1140 texture3d_preload,
1141 texture3d_sub_resource_add_dirty_region,
1142 texture3d_sub_resource_cleanup,
1145 static const struct wined3d_resource_ops texture3d_resource_ops =
1147 texture3d_unload,
1150 static HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1151 UINT depth, UINT levels, struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id,
1152 enum wined3d_pool pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1154 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1155 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1156 UINT tmp_w, tmp_h, tmp_d;
1157 unsigned int i;
1158 HRESULT hr;
1160 /* TODO: It should only be possible to create textures for formats
1161 * that are reported as supported. */
1162 if (WINED3DFMT_UNKNOWN >= format_id)
1164 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1165 return WINED3DERR_INVALIDCALL;
1168 if (!gl_info->supported[EXT_TEXTURE3D])
1170 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1171 return WINED3DERR_INVALIDCALL;
1174 /* Calculate levels for mip mapping. */
1175 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1177 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1179 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1180 return WINED3DERR_INVALIDCALL;
1183 if (levels > 1)
1185 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1186 return WINED3DERR_INVALIDCALL;
1189 levels = 1;
1191 else if (!levels)
1193 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1194 TRACE("Calculated levels = %u.\n", levels);
1197 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1199 UINT pow2_w, pow2_h, pow2_d;
1200 pow2_w = 1;
1201 while (pow2_w < width) pow2_w <<= 1;
1202 pow2_h = 1;
1203 while (pow2_h < height) pow2_h <<= 1;
1204 pow2_d = 1;
1205 while (pow2_d < depth) pow2_d <<= 1;
1207 if (pow2_w != width || pow2_h != height || pow2_d != depth)
1209 if (pool == WINED3D_POOL_SCRATCH)
1211 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1213 else
1215 WARN("Attempted to create a NPOT volume texture (%u,%u,%u) without GL support.\n",
1216 width, height, depth);
1217 return WINED3DERR_INVALIDCALL;
1222 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, WINED3D_RTYPE_VOLUME_TEXTURE, device,
1223 usage, format, pool, width, height, depth, parent, parent_ops, &texture3d_resource_ops)))
1225 WARN("Failed to initialize texture, returning %#x.\n", hr);
1226 return hr;
1229 texture->pow2_matrix[0] = 1.0f;
1230 texture->pow2_matrix[5] = 1.0f;
1231 texture->pow2_matrix[10] = 1.0f;
1232 texture->pow2_matrix[15] = 1.0f;
1233 texture->target = GL_TEXTURE_3D;
1235 /* Generate all the surfaces. */
1236 tmp_w = width;
1237 tmp_h = height;
1238 tmp_d = depth;
1240 for (i = 0; i < texture->level_count; ++i)
1242 struct wined3d_volume *volume;
1244 /* Create the volume. */
1245 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1246 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1247 if (FAILED(hr))
1249 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1250 wined3d_texture_cleanup(texture);
1251 return hr;
1254 /* Set its container to this texture. */
1255 volume_set_container(volume, texture);
1256 texture->sub_resources[i] = &volume->resource;
1258 /* Calculate the next mipmap level. */
1259 tmp_w = max(1, tmp_w >> 1);
1260 tmp_h = max(1, tmp_h >> 1);
1261 tmp_d = max(1, tmp_d >> 1);
1264 return WINED3D_OK;
1267 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, UINT width, UINT height,
1268 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1269 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1271 struct wined3d_texture *object;
1272 HRESULT hr;
1274 TRACE("device %p, width %u, height %u, level_count %u, usage %#x\n",
1275 device, width, height, level_count, usage);
1276 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1277 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1279 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1280 if (!object)
1282 *texture = NULL;
1283 return WINED3DERR_OUTOFVIDEOMEMORY;
1286 hr = texture_init(object, width, height, level_count,
1287 device, usage, format_id, pool, parent, parent_ops);
1288 if (FAILED(hr))
1290 WARN("Failed to initialize texture, returning %#x.\n", hr);
1291 HeapFree(GetProcessHeap(), 0, object);
1292 *texture = NULL;
1293 return hr;
1296 TRACE("Created texture %p.\n", object);
1297 *texture = object;
1299 return WINED3D_OK;
1302 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, UINT width, UINT height, UINT depth,
1303 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1304 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1306 struct wined3d_texture *object;
1307 HRESULT hr;
1309 TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1310 device, width, height, depth, level_count, usage);
1311 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1312 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1314 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1315 if (!object)
1317 *texture = NULL;
1318 return WINED3DERR_OUTOFVIDEOMEMORY;
1321 hr = volumetexture_init(object, width, height, depth, level_count,
1322 device, usage, format_id, pool, parent, parent_ops);
1323 if (FAILED(hr))
1325 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1326 HeapFree(GetProcessHeap(), 0, object);
1327 *texture = NULL;
1328 return hr;
1331 TRACE("Created texture %p.\n", object);
1332 *texture = object;
1334 return WINED3D_OK;
1337 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, UINT edge_length,
1338 UINT level_count, DWORD usage, enum wined3d_format_id format_id, enum wined3d_pool pool, void *parent,
1339 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1341 struct wined3d_texture *object;
1342 HRESULT hr;
1344 TRACE("device %p, edge_length %u, level_count %u, usage %#x\n",
1345 device, edge_length, level_count, usage);
1346 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1347 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1349 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1350 if (!object)
1352 *texture = NULL;
1353 return WINED3DERR_OUTOFVIDEOMEMORY;
1356 hr = cubetexture_init(object, edge_length, level_count,
1357 device, usage, format_id, pool, parent, parent_ops);
1358 if (FAILED(hr))
1360 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1361 HeapFree(GetProcessHeap(), 0, object);
1362 *texture = NULL;
1363 return hr;
1366 TRACE("Created texture %p.\n", object);
1367 *texture = object;
1369 return WINED3D_OK;