wined3d: Get rid of wined3d_texture_get_type().
[wine/wine-gecko.git] / dlls / wined3d / texture.c
blobc138d01ba50d903816b300a9233e269d190ed5ae
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 "wined3d_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d_texture);
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)
33 HRESULT hr;
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))
40 WARN("Failed to initialize resource, returning %#x\n", hr);
41 return hr;
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)
49 ERR("Failed to allocate sub-resource array.\n");
50 resource_cleanup(&texture->resource);
51 return E_OUTOFMEMORY;
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;
62 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
64 texture->min_mip_lookup = minMipLookup;
65 texture->mag_lookup = magLookup;
67 else
69 texture->min_mip_lookup = minMipLookup_noFilter;
70 texture->mag_lookup = magLookup_noFilter;
73 return WINED3D_OK;
76 /* A GL context is provided by the caller */
77 static void gltexture_delete(struct gl_texture *tex)
79 ENTER_GL();
80 glDeleteTextures(1, &tex->name);
81 LEAVE_GL();
82 tex->name = 0;
85 static void wined3d_texture_unload(struct wined3d_texture *texture)
87 struct wined3d_device *device = texture->resource.device;
88 struct wined3d_context *context = NULL;
90 if (texture->texture_rgb.name || texture->texture_srgb.name)
92 context = context_acquire(device, NULL);
95 if (texture->texture_rgb.name)
96 gltexture_delete(&texture->texture_rgb);
98 if (texture->texture_srgb.name)
99 gltexture_delete(&texture->texture_srgb);
101 if (context) context_release(context);
103 wined3d_texture_set_dirty(texture, TRUE);
105 resource_unload(&texture->resource);
108 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
110 UINT sub_count = texture->level_count * texture->layer_count;
111 UINT i;
113 TRACE("texture %p.\n", texture);
115 for (i = 0; i < sub_count; ++i)
117 struct wined3d_resource *sub_resource = texture->sub_resources[i];
119 if (sub_resource)
120 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
123 wined3d_texture_unload(texture);
124 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
125 resource_cleanup(&texture->resource);
128 void wined3d_texture_set_dirty(struct wined3d_texture *texture, BOOL dirty)
130 texture->texture_rgb.dirty = dirty;
131 texture->texture_srgb.dirty = dirty;
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)
138 struct gl_texture *gl_tex;
139 BOOL new_texture = FALSE;
140 HRESULT hr = WINED3D_OK;
141 GLenum target;
143 TRACE("texture %p, srgb %#x, set_surface_desc %p.\n", texture, 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, gl_info, srgb);
152 target = texture->target;
154 ENTER_GL();
155 /* Generate a texture name if we don't already have one. */
156 if (!gl_tex->name)
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)
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);
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;
187 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
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)");
198 else
200 *set_surface_desc = FALSE;
203 if (gl_tex->name)
205 glBindTexture(target, gl_tex->name);
206 checkGLcall("glBindTexture");
207 if (new_texture)
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)
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)");
221 if (target == GL_TEXTURE_CUBE_MAP_ARB)
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);
230 else
232 ERR("This texture doesn't have an OpenGL texture assigned to it.\n");
233 hr = WINED3DERR_INVALIDCALL;
236 LEAVE_GL();
237 return hr;
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)
244 GLint gl_wrap;
246 if (d3d_wrap < WINED3DTADDRESS_WRAP || d3d_wrap > WINED3DTADDRESS_MIRRORONCE)
248 FIXME("Unrecognized or unsupported WINED3DTEXTUREADDRESS %#x.\n", d3d_wrap);
249 return;
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];
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)");
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)
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;
275 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
277 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info,
278 texture->flags & WINED3D_TEXTURE_IS_SRGB);
280 /* This function relies on the correct texture being bound and loaded. */
282 if (sampler_states[WINED3DSAMP_ADDRESSU] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
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;
289 if (sampler_states[WINED3DSAMP_ADDRESSV] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
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;
296 if (sampler_states[WINED3DSAMP_ADDRESSW] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
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;
303 if (sampler_states[WINED3DSAMP_BORDERCOLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
305 float col[4];
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;
315 if (sampler_states[WINED3DSAMP_MAGFILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
317 GLint gl_value;
319 state = sampler_states[WINED3DSAMP_MAGFILTER];
320 if (state > WINED3DTEXF_ANISOTROPIC)
321 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
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);
328 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
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]))
335 GLint gl_value;
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];
341 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3DTEXF_ANISOTROPIC
342 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3DTEXF_ANISOTROPIC)
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]);
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));
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, ...");
358 if (!cond_np2)
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];
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);
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];
386 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
388 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
390 glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
391 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
393 else
395 WARN("Anisotropic filtering not supported.\n");
397 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
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])
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)");
408 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
409 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
411 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
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;
418 else
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;
427 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
429 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
431 TRACE("%p increasing refcount to %u.\n", texture, refcount);
433 return refcount;
436 /* Do not call while under the GL lock. */
437 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
439 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
441 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
443 if (!refcount)
445 wined3d_texture_cleanup(texture);
446 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
447 HeapFree(GetProcessHeap(), 0, texture);
450 return refcount;
453 HRESULT CDECL wined3d_texture_set_private_data(struct wined3d_texture *texture,
454 REFGUID guid, const void *data, DWORD data_size, DWORD flags)
456 return resource_set_private_data(&texture->resource, guid, data, data_size, flags);
459 HRESULT CDECL wined3d_texture_get_private_data(const struct wined3d_texture *texture,
460 REFGUID guid, void *data, DWORD *data_size)
462 return resource_get_private_data(&texture->resource, guid, data, data_size);
465 HRESULT CDECL wined3d_texture_free_private_data(struct wined3d_texture *texture, REFGUID guid)
467 return resource_free_private_data(&texture->resource, guid);
470 DWORD CDECL wined3d_texture_set_priority(struct wined3d_texture *texture, DWORD priority)
472 return resource_set_priority(&texture->resource, priority);
475 DWORD CDECL wined3d_texture_get_priority(const struct wined3d_texture *texture)
477 return resource_get_priority(&texture->resource);
480 /* Do not call while under the GL lock. */
481 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
483 texture->texture_ops->texture_preload(texture, SRGB_ANY);
486 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
488 TRACE("texture %p.\n", texture);
490 return texture->resource.parent;
493 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
495 DWORD old = texture->lod;
497 TRACE("texture %p, lod %u.\n", texture, lod);
499 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
500 * textures. The call always returns 0, and GetLOD always returns 0. */
501 if (texture->resource.pool != WINED3DPOOL_MANAGED)
503 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
504 return 0;
507 if (lod >= texture->level_count)
508 lod = texture->level_count - 1;
510 if (texture->lod != lod)
512 texture->lod = lod;
514 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
515 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
516 if (texture->bind_count)
517 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
520 return old;
523 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
525 TRACE("texture %p, returning %u.\n", texture, texture->lod);
527 return texture->lod;
530 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
532 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
534 return texture->level_count;
537 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
538 WINED3DTEXTUREFILTERTYPE filter_type)
540 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
542 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
544 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
545 return WINED3DERR_INVALIDCALL;
548 texture->filter_type = filter_type;
550 return WINED3D_OK;
553 WINED3DTEXTUREFILTERTYPE CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
555 TRACE("texture %p.\n", texture);
557 return texture->filter_type;
560 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
562 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
563 FIXME("texture %p stub!\n", texture);
566 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
567 UINT sub_resource_idx)
569 UINT sub_count = texture->level_count * texture->layer_count;
571 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
573 if (sub_resource_idx >= sub_count)
575 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
576 return NULL;
579 return texture->sub_resources[sub_resource_idx];
582 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
583 UINT layer, const WINED3DBOX *dirty_region)
585 struct wined3d_resource *sub_resource;
587 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
589 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
591 WARN("Failed to get sub-resource.\n");
592 return WINED3DERR_INVALIDCALL;
595 wined3d_texture_set_dirty(texture, TRUE);
596 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
598 return WINED3D_OK;
601 /* Context activation is done by the caller. */
602 static HRESULT texture2d_bind(struct wined3d_texture *texture,
603 const struct wined3d_gl_info *gl_info, BOOL srgb)
605 BOOL set_gl_texture_desc;
606 HRESULT hr;
608 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
610 hr = wined3d_texture_bind(texture, gl_info, srgb, &set_gl_texture_desc);
611 if (set_gl_texture_desc && SUCCEEDED(hr))
613 UINT sub_count = texture->level_count * texture->layer_count;
614 BOOL srgb_tex = !gl_info->supported[EXT_TEXTURE_SRGB_DECODE]
615 && (texture->flags & WINED3D_TEXTURE_IS_SRGB);
616 struct gl_texture *gl_tex;
617 UINT i;
619 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_tex);
621 for (i = 0; i < sub_count; ++i)
623 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
624 surface_set_texture_name(surface, gl_tex->name, srgb_tex);
627 /* Conditinal non power of two textures use a different clamping
628 * default. If we're using the GL_WINE_normalized_texrect partial
629 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
630 * has the address mode set to repeat - something that prevents us
631 * from hitting the accelerated codepath. Thus manually set the GL
632 * state. The same applies to filtering. Even if the texture has only
633 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
634 * fallback on macos. */
635 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
637 GLenum target = texture->target;
639 ENTER_GL();
640 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
641 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)");
642 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
643 checkGLcall("glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)");
644 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
645 checkGLcall("glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST)");
646 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
647 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST)");
648 LEAVE_GL();
649 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3DTADDRESS_CLAMP;
650 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3DTADDRESS_CLAMP;
651 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
652 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
653 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
657 return hr;
660 static BOOL texture_srgb_mode(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
662 switch (srgb)
664 case SRGB_RGB:
665 return FALSE;
667 case SRGB_SRGB:
668 return TRUE;
670 default:
671 return texture->flags & WINED3D_TEXTURE_IS_SRGB;
675 /* Do not call while under the GL lock. */
676 static void texture2d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
678 UINT sub_count = texture->level_count * texture->layer_count;
679 struct wined3d_device *device = texture->resource.device;
680 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
681 struct wined3d_context *context = NULL;
682 struct gl_texture *gl_tex;
683 BOOL srgb_mode;
684 UINT i;
686 TRACE("texture %p, srgb %#x.\n", texture, srgb);
688 srgb_mode = texture_srgb_mode(texture, srgb);
689 gl_tex = wined3d_texture_get_gl_texture(texture, gl_info, srgb_mode);
691 if (!device->isInDraw)
693 /* No danger of recursive calls, context_acquire() sets isInDraw to TRUE
694 * when loading offscreen render targets into the texture. */
695 context = context_acquire(device, NULL);
698 if (texture->resource.format->id == WINED3DFMT_P8_UINT
699 || texture->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
701 for (i = 0; i < sub_count; ++i)
703 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
705 if (palette9_changed(surface))
707 TRACE("Reloading surface %p because the d3d8/9 palette was changed.\n", surface);
708 /* TODO: This is not necessarily needed with hw palettized texture support. */
709 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
710 /* Make sure the texture is reloaded because of the palette
711 * change, this kills performance though :( */
712 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
717 if (gl_tex->dirty)
719 /* Reload the surfaces if the texture is marked dirty. */
720 for (i = 0; i < sub_count; ++i)
722 surface_load(surface_from_resource(texture->sub_resources[i]), srgb_mode);
725 else
727 TRACE("Texture %p not dirty, nothing to do.\n", texture);
730 /* No longer dirty. */
731 gl_tex->dirty = FALSE;
733 if (context) context_release(context);
736 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
737 const WINED3DBOX *dirty_region)
739 surface_add_dirty_rect(surface_from_resource(sub_resource), dirty_region);
742 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
744 struct wined3d_surface *surface = surface_from_resource(sub_resource);
746 /* Clean out the texture name we gave to the surface so that the
747 * surface doesn't try and release it. */
748 surface_set_texture_name(surface, 0, TRUE);
749 surface_set_texture_name(surface, 0, FALSE);
750 surface_set_texture_target(surface, 0);
751 surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
752 wined3d_surface_decref(surface);
755 /* Do not call while under the GL lock. */
756 static void texture2d_unload(struct wined3d_resource *resource)
758 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
759 UINT sub_count = texture->level_count * texture->layer_count;
760 UINT i;
762 TRACE("texture %p.\n", texture);
764 for (i = 0; i < sub_count; ++i)
766 struct wined3d_resource *sub_resource = texture->sub_resources[i];
767 struct wined3d_surface *surface = surface_from_resource(sub_resource);
769 sub_resource->resource_ops->resource_unload(sub_resource);
770 surface_set_texture_name(surface, 0, FALSE); /* Delete RGB name */
771 surface_set_texture_name(surface, 0, TRUE); /* Delete sRGB name */
774 wined3d_texture_unload(texture);
777 static const struct wined3d_texture_ops texture2d_ops =
779 texture2d_bind,
780 texture2d_preload,
781 texture2d_sub_resource_add_dirty_region,
782 texture2d_sub_resource_cleanup,
785 static const struct wined3d_resource_ops texture2d_resource_ops =
787 texture2d_unload,
790 static HRESULT cubetexture_init(struct wined3d_texture *texture, UINT edge_length, UINT levels,
791 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
792 void *parent, const struct wined3d_parent_ops *parent_ops)
794 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
795 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
796 UINT pow2_edge_length;
797 unsigned int i, j;
798 UINT tmp_w;
799 HRESULT hr;
801 /* TODO: It should only be possible to create textures for formats
802 * that are reported as supported. */
803 if (WINED3DFMT_UNKNOWN >= format_id)
805 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
806 return WINED3DERR_INVALIDCALL;
809 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && pool != WINED3DPOOL_SCRATCH)
811 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
812 return WINED3DERR_INVALIDCALL;
815 /* Calculate levels for mip mapping */
816 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
818 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
820 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
821 return WINED3DERR_INVALIDCALL;
824 if (levels > 1)
826 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
827 return WINED3DERR_INVALIDCALL;
830 levels = 1;
832 else if (!levels)
834 levels = wined3d_log2i(edge_length) + 1;
835 TRACE("Calculated levels = %u.\n", levels);
838 hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
839 WINED3DRTYPE_CUBETEXTURE, device, usage, format, pool,
840 parent, parent_ops, &texture2d_resource_ops);
841 if (FAILED(hr))
843 WARN("Failed to initialize texture, returning %#x\n", hr);
844 return hr;
847 /* Find the nearest pow2 match. */
848 pow2_edge_length = 1;
849 while (pow2_edge_length < edge_length) pow2_edge_length <<= 1;
851 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || (edge_length == pow2_edge_length))
853 /* Precalculated scaling for 'faked' non power of two texture coords. */
854 texture->pow2_matrix[0] = 1.0f;
855 texture->pow2_matrix[5] = 1.0f;
856 texture->pow2_matrix[10] = 1.0f;
857 texture->pow2_matrix[15] = 1.0f;
859 else
861 /* Precalculated scaling for 'faked' non power of two texture coords. */
862 texture->pow2_matrix[0] = ((float)edge_length) / ((float)pow2_edge_length);
863 texture->pow2_matrix[5] = ((float)edge_length) / ((float)pow2_edge_length);
864 texture->pow2_matrix[10] = ((float)edge_length) / ((float)pow2_edge_length);
865 texture->pow2_matrix[15] = 1.0f;
866 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
868 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
870 /* Generate all the surfaces. */
871 tmp_w = edge_length;
872 for (i = 0; i < texture->level_count; ++i)
874 /* Create the 6 faces. */
875 for (j = 0; j < 6; ++j)
877 static const GLenum cube_targets[6] =
879 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
880 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
881 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
882 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
883 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
884 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
886 UINT idx = j * texture->level_count + i;
887 struct wined3d_surface *surface;
889 hr = device->device_parent->ops->create_surface(device->device_parent, parent, tmp_w, tmp_w,
890 format_id, usage, pool, i /* Level */, j, &surface);
891 if (FAILED(hr))
893 FIXME("(%p) Failed to create surface, hr %#x.\n", texture, hr);
894 wined3d_texture_cleanup(texture);
895 return hr;
898 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
899 surface_set_texture_target(surface, cube_targets[j]);
900 texture->sub_resources[idx] = &surface->resource;
901 TRACE("Created surface level %u @ %p.\n", i, surface);
903 tmp_w = max(1, tmp_w >> 1);
906 return WINED3D_OK;
909 static HRESULT texture_init(struct wined3d_texture *texture, UINT width, UINT height, UINT levels,
910 struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
911 void *parent, const struct wined3d_parent_ops *parent_ops)
913 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
914 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
915 UINT pow2_width, pow2_height;
916 UINT tmp_w, tmp_h;
917 unsigned int i;
918 HRESULT hr;
920 /* TODO: It should only be possible to create textures for formats
921 * that are reported as supported. */
922 if (WINED3DFMT_UNKNOWN >= format_id)
924 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
925 return WINED3DERR_INVALIDCALL;
928 /* Non-power2 support. */
929 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
931 pow2_width = width;
932 pow2_height = height;
934 else
936 /* Find the nearest pow2 match. */
937 pow2_width = pow2_height = 1;
938 while (pow2_width < width) pow2_width <<= 1;
939 while (pow2_height < height) pow2_height <<= 1;
941 if (pow2_width != width || pow2_height != height)
943 if (levels > 1)
945 WARN("Attempted to create a mipmapped np2 texture without unconditional np2 support.\n");
946 return WINED3DERR_INVALIDCALL;
948 levels = 1;
952 /* Calculate levels for mip mapping. */
953 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
955 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
957 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
958 return WINED3DERR_INVALIDCALL;
961 if (levels > 1)
963 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
964 return WINED3DERR_INVALIDCALL;
967 levels = 1;
969 else if (!levels)
971 levels = wined3d_log2i(max(width, height)) + 1;
972 TRACE("Calculated levels = %u.\n", levels);
975 hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
976 WINED3DRTYPE_TEXTURE, device, usage, format, pool,
977 parent, parent_ops, &texture2d_resource_ops);
978 if (FAILED(hr))
980 WARN("Failed to initialize texture, returning %#x.\n", hr);
981 return hr;
984 /* Precalculated scaling for 'faked' non power of two texture coords.
985 * Second also don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
986 * is used in combination with texture uploads (RTL_READTEX). The reason is that EXT_PALETTED_TEXTURE
987 * doesn't work in combination with ARB_TEXTURE_RECTANGLE. */
988 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT] && (width != pow2_width || height != pow2_height))
990 texture->pow2_matrix[0] = 1.0f;
991 texture->pow2_matrix[5] = 1.0f;
992 texture->pow2_matrix[10] = 1.0f;
993 texture->pow2_matrix[15] = 1.0f;
994 texture->target = GL_TEXTURE_2D;
995 texture->flags |= WINED3D_TEXTURE_COND_NP2;
996 texture->min_mip_lookup = minMipLookup_noFilter;
998 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE] && (width != pow2_width || height != pow2_height)
999 && !(format->id == WINED3DFMT_P8_UINT && gl_info->supported[EXT_PALETTED_TEXTURE]
1000 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
1002 if (width != 1 || height != 1)
1003 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1005 texture->pow2_matrix[0] = (float)width;
1006 texture->pow2_matrix[5] = (float)height;
1007 texture->pow2_matrix[10] = 1.0f;
1008 texture->pow2_matrix[15] = 1.0f;
1009 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1010 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1012 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1013 texture->min_mip_lookup = minMipLookup_noMip;
1014 else
1015 texture->min_mip_lookup = minMipLookup_noFilter;
1017 else
1019 if ((width != pow2_width) || (height != pow2_height))
1021 texture->pow2_matrix[0] = (((float)width) / ((float)pow2_width));
1022 texture->pow2_matrix[5] = (((float)height) / ((float)pow2_height));
1023 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1025 else
1027 texture->pow2_matrix[0] = 1.0f;
1028 texture->pow2_matrix[5] = 1.0f;
1031 texture->pow2_matrix[10] = 1.0f;
1032 texture->pow2_matrix[15] = 1.0f;
1033 texture->target = GL_TEXTURE_2D;
1035 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1037 /* Generate all the surfaces. */
1038 tmp_w = width;
1039 tmp_h = height;
1040 for (i = 0; i < texture->level_count; ++i)
1042 struct wined3d_surface *surface;
1044 /* Use the callback to create the texture surface. */
1045 hr = device->device_parent->ops->create_surface(device->device_parent, parent, tmp_w, tmp_h,
1046 format->id, usage, pool, i, 0, &surface);
1047 if (FAILED(hr))
1049 FIXME("Failed to create surface %p, hr %#x\n", texture, hr);
1050 wined3d_texture_cleanup(texture);
1051 return hr;
1054 surface_set_container(surface, WINED3D_CONTAINER_TEXTURE, texture);
1055 surface_set_texture_target(surface, texture->target);
1056 texture->sub_resources[i] = &surface->resource;
1057 TRACE("Created surface level %u @ %p.\n", i, surface);
1058 /* Calculate the next mipmap level. */
1059 tmp_w = max(1, tmp_w >> 1);
1060 tmp_h = max(1, tmp_h >> 1);
1063 return WINED3D_OK;
1066 /* Context activation is done by the caller. */
1067 static HRESULT texture3d_bind(struct wined3d_texture *texture,
1068 const struct wined3d_gl_info *gl_info, BOOL srgb)
1070 BOOL dummy;
1072 TRACE("texture %p, gl_info %p, srgb %#x.\n", texture, gl_info, srgb);
1074 return wined3d_texture_bind(texture, gl_info, srgb, &dummy);
1077 /* Do not call while under the GL lock. */
1078 static void texture3d_preload(struct wined3d_texture *texture, enum WINED3DSRGB srgb)
1080 struct wined3d_device *device = texture->resource.device;
1081 struct wined3d_context *context = NULL;
1082 BOOL srgb_was_toggled = FALSE;
1083 unsigned int i;
1085 TRACE("texture %p, srgb %#x.\n", texture, srgb);
1087 if (!device->isInDraw)
1088 context = context_acquire(device, NULL);
1089 else if (texture->bind_count > 0)
1091 BOOL texture_srgb = texture->flags & WINED3D_TEXTURE_IS_SRGB;
1092 BOOL sampler_srgb = texture_srgb_mode(texture, srgb);
1093 srgb_was_toggled = !texture_srgb != !sampler_srgb;
1095 if (srgb_was_toggled)
1097 if (sampler_srgb)
1098 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
1099 else
1100 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
1104 /* If the texture is marked dirty or the sRGB sampler setting has changed
1105 * since the last load then reload the volumes. */
1106 if (texture->texture_rgb.dirty)
1108 for (i = 0; i < texture->level_count; ++i)
1110 volume_load(volume_from_resource(texture->sub_resources[i]), i,
1111 texture->flags & WINED3D_TEXTURE_IS_SRGB);
1114 else if (srgb_was_toggled)
1116 for (i = 0; i < texture->level_count; ++i)
1118 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1119 volume_add_dirty_box(volume, NULL);
1120 volume_load(volume, i, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1123 else
1125 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1128 if (context)
1129 context_release(context);
1131 /* No longer dirty */
1132 texture->texture_rgb.dirty = FALSE;
1135 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1136 const WINED3DBOX *dirty_region)
1138 volume_add_dirty_box(volume_from_resource(sub_resource), dirty_region);
1141 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1143 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1145 /* Cleanup the container. */
1146 volume_set_container(volume, NULL);
1147 wined3d_volume_decref(volume);
1150 /* Do not call while under the GL lock. */
1151 static void texture3d_unload(struct wined3d_resource *resource)
1153 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1154 UINT i;
1156 TRACE("texture %p.\n", texture);
1158 for (i = 0; i < texture->level_count; ++i)
1160 struct wined3d_resource *sub_resource = texture->sub_resources[i];
1161 sub_resource->resource_ops->resource_unload(sub_resource);
1164 wined3d_texture_unload(texture);
1167 static const struct wined3d_texture_ops texture3d_ops =
1169 texture3d_bind,
1170 texture3d_preload,
1171 texture3d_sub_resource_add_dirty_region,
1172 texture3d_sub_resource_cleanup,
1175 static const struct wined3d_resource_ops texture3d_resource_ops =
1177 texture3d_unload,
1180 static HRESULT volumetexture_init(struct wined3d_texture *texture, UINT width, UINT height,
1181 UINT depth, UINT levels, struct wined3d_device *device, DWORD usage, enum wined3d_format_id format_id,
1182 WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
1184 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1185 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1186 UINT tmp_w, tmp_h, tmp_d;
1187 unsigned int i;
1188 HRESULT hr;
1190 /* TODO: It should only be possible to create textures for formats
1191 * that are reported as supported. */
1192 if (WINED3DFMT_UNKNOWN >= format_id)
1194 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1195 return WINED3DERR_INVALIDCALL;
1198 if (!gl_info->supported[EXT_TEXTURE3D])
1200 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1201 return WINED3DERR_INVALIDCALL;
1204 /* Calculate levels for mip mapping. */
1205 if (usage & WINED3DUSAGE_AUTOGENMIPMAP)
1207 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1209 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1210 return WINED3DERR_INVALIDCALL;
1213 if (levels > 1)
1215 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1216 return WINED3DERR_INVALIDCALL;
1219 levels = 1;
1221 else if (!levels)
1223 levels = wined3d_log2i(max(max(width, height), depth)) + 1;
1224 TRACE("Calculated levels = %u.\n", levels);
1227 hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1228 WINED3DRTYPE_VOLUMETEXTURE, device, usage, format, pool,
1229 parent, parent_ops, &texture3d_resource_ops);
1230 if (FAILED(hr))
1232 WARN("Failed to initialize texture, returning %#x.\n", hr);
1233 return hr;
1236 /* Is NP2 support for volumes needed? */
1237 texture->pow2_matrix[0] = 1.0f;
1238 texture->pow2_matrix[5] = 1.0f;
1239 texture->pow2_matrix[10] = 1.0f;
1240 texture->pow2_matrix[15] = 1.0f;
1241 texture->target = GL_TEXTURE_3D;
1243 /* Generate all the surfaces. */
1244 tmp_w = width;
1245 tmp_h = height;
1246 tmp_d = depth;
1248 for (i = 0; i < texture->level_count; ++i)
1250 struct wined3d_volume *volume;
1252 /* Create the volume. */
1253 hr = device->device_parent->ops->create_volume(device->device_parent, parent,
1254 tmp_w, tmp_h, tmp_d, format_id, pool, usage, &volume);
1255 if (FAILED(hr))
1257 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1258 wined3d_texture_cleanup(texture);
1259 return hr;
1262 /* Set its container to this texture. */
1263 volume_set_container(volume, texture);
1264 texture->sub_resources[i] = &volume->resource;
1266 /* Calculate the next mipmap level. */
1267 tmp_w = max(1, tmp_w >> 1);
1268 tmp_h = max(1, tmp_h >> 1);
1269 tmp_d = max(1, tmp_d >> 1);
1272 return WINED3D_OK;
1275 HRESULT CDECL wined3d_texture_create_2d(struct wined3d_device *device, UINT width, UINT height,
1276 UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1277 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1279 struct wined3d_texture *object;
1280 HRESULT hr;
1282 TRACE("device %p, width %u, height %u, level_count %u, usage %#x\n",
1283 device, width, height, level_count, usage);
1284 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1285 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1287 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1288 if (!object)
1290 ERR("Out of memory.\n");
1291 *texture = NULL;
1292 return WINED3DERR_OUTOFVIDEOMEMORY;
1295 hr = texture_init(object, width, height, level_count,
1296 device, usage, format_id, pool, parent, parent_ops);
1297 if (FAILED(hr))
1299 WARN("Failed to initialize texture, returning %#x.\n", hr);
1300 HeapFree(GetProcessHeap(), 0, object);
1301 *texture = NULL;
1302 return hr;
1305 TRACE("Created texture %p.\n", object);
1306 *texture = object;
1308 return WINED3D_OK;
1311 HRESULT CDECL wined3d_texture_create_3d(struct wined3d_device *device, UINT width, UINT height, UINT depth,
1312 UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1313 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1315 struct wined3d_texture *object;
1316 HRESULT hr;
1318 TRACE("device %p, width %u, height %u, depth %u, level_count %u, usage %#x\n",
1319 device, width, height, depth, level_count, usage);
1320 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1321 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1323 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1324 if (!object)
1326 ERR("Out of memory\n");
1327 *texture = NULL;
1328 return WINED3DERR_OUTOFVIDEOMEMORY;
1331 hr = volumetexture_init(object, width, height, depth, level_count,
1332 device, usage, format_id, pool, parent, parent_ops);
1333 if (FAILED(hr))
1335 WARN("Failed to initialize volumetexture, returning %#x\n", hr);
1336 HeapFree(GetProcessHeap(), 0, object);
1337 *texture = NULL;
1338 return hr;
1341 TRACE("Created texture %p.\n", object);
1342 *texture = object;
1344 return WINED3D_OK;
1347 HRESULT CDECL wined3d_texture_create_cube(struct wined3d_device *device, UINT edge_length,
1348 UINT level_count, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool, void *parent,
1349 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1351 struct wined3d_texture *object;
1352 HRESULT hr;
1354 TRACE("device %p, edge_length %u, level_count %u, usage %#x\n",
1355 device, edge_length, level_count, usage);
1356 TRACE("format %s, pool %#x, parent %p, parent_ops %p, texture %p.\n",
1357 debug_d3dformat(format_id), pool, parent, parent_ops, texture);
1359 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1360 if (!object)
1362 ERR("Out of memory\n");
1363 *texture = NULL;
1364 return WINED3DERR_OUTOFVIDEOMEMORY;
1367 hr = cubetexture_init(object, edge_length, level_count,
1368 device, usage, format_id, pool, parent, parent_ops);
1369 if (FAILED(hr))
1371 WARN("Failed to initialize cubetexture, returning %#x\n", hr);
1372 HeapFree(GetProcessHeap(), 0, object);
1373 *texture = NULL;
1374 return hr;
1377 TRACE("Created texture %p.\n", object);
1378 *texture = object;
1380 return WINED3D_OK;