vbscript: Added support for title and type arguments of MsgBox.
[wine.git] / dlls / wined3d / texture.c
blob8beb9f800ad238d07e27f4ea74d2fd70731c6612
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);
28 WINE_DECLARE_DEBUG_CHANNEL(winediag);
30 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
31 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, struct wined3d_device *device,
32 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_resource_ops *resource_ops)
34 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
35 HRESULT hr;
37 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
38 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
39 "device %p, parent %p, parent_ops %p, resource_ops %p.\n",
40 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
41 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
42 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
43 device, parent, parent_ops, resource_ops);
45 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY)) == WINED3DFMT_FLAG_BLOCKS)
47 UINT width_mask = format->block_width - 1;
48 UINT height_mask = format->block_height - 1;
49 if (desc->width & width_mask || desc->height & height_mask)
50 return WINED3DERR_INVALIDCALL;
53 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
54 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
55 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
57 static unsigned int once;
59 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
60 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
61 && !(format->flags & WINED3DFMT_FLAG_TEXTURE) && !once++)
62 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
64 WARN("Failed to initialize resource, returning %#x\n", hr);
65 return hr;
68 texture->texture_ops = texture_ops;
69 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
70 level_count * layer_count * sizeof(*texture->sub_resources));
71 if (!texture->sub_resources)
73 ERR("Failed to allocate sub-resource array.\n");
74 resource_cleanup(&texture->resource);
75 return E_OUTOFMEMORY;
78 texture->layer_count = layer_count;
79 texture->level_count = level_count;
80 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
81 texture->lod = 0;
82 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT;
84 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
86 texture->min_mip_lookup = minMipLookup;
87 texture->mag_lookup = magLookup;
89 else
91 texture->min_mip_lookup = minMipLookup_noFilter;
92 texture->mag_lookup = magLookup_noFilter;
95 return WINED3D_OK;
98 /* A GL context is provided by the caller */
99 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
101 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
102 tex->name = 0;
105 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
107 struct wined3d_device *device = texture->resource.device;
108 struct wined3d_context *context = NULL;
110 if (texture->texture_rgb.name || texture->texture_srgb.name)
112 context = context_acquire(device, NULL);
115 if (texture->texture_rgb.name)
116 gltexture_delete(context->gl_info, &texture->texture_rgb);
118 if (texture->texture_srgb.name)
119 gltexture_delete(context->gl_info, &texture->texture_srgb);
121 if (context) context_release(context);
123 wined3d_texture_set_dirty(texture);
125 resource_unload(&texture->resource);
128 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
130 UINT sub_count = texture->level_count * texture->layer_count;
131 UINT i;
133 TRACE("texture %p.\n", texture);
135 for (i = 0; i < sub_count; ++i)
137 struct wined3d_resource *sub_resource = texture->sub_resources[i];
139 if (sub_resource)
140 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
143 wined3d_texture_unload_gl_texture(texture);
144 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
145 resource_cleanup(&texture->resource);
148 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
150 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
153 /* Context activation is done by the caller. */
154 void wined3d_texture_bind(struct wined3d_texture *texture,
155 struct wined3d_context *context, BOOL srgb)
157 const struct wined3d_gl_info *gl_info = context->gl_info;
158 struct gl_texture *gl_tex;
159 GLenum target;
161 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
163 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
164 srgb = FALSE;
166 /* sRGB mode cache for preload() calls outside drawprim. */
167 if (srgb)
168 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
169 else
170 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
172 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
173 target = texture->target;
175 if (gl_tex->name)
177 context_bind_texture(context, target, gl_tex->name);
178 return;
181 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
182 checkGLcall("glGenTextures");
183 TRACE("Generated texture %d.\n", gl_tex->name);
185 if (!gl_tex->name)
187 ERR("Failed to generate a texture name.\n");
188 return;
191 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
193 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
194 GLclampf tmp = 0.9f;
195 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
198 /* Initialise the state of the texture object to the OpenGL defaults, not
199 * the wined3d defaults. */
200 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_WRAP;
201 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_WRAP;
202 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = WINED3D_TADDRESS_WRAP;
203 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = 0;
204 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_LINEAR;
205 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
206 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
207 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = 0;
208 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = 1;
209 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
210 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = TRUE;
211 else
212 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = srgb;
213 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
214 wined3d_texture_set_dirty(texture);
216 context_bind_texture(context, target, gl_tex->name);
218 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
220 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
221 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
224 /* For a new texture we have to set the texture levels after binding the
225 * texture. Beware that texture rectangles do not support mipmapping, but
226 * set the maxmiplevel if we're relying on the partial
227 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
228 * (I.e., do not care about cond_np2 here, just look for
229 * GL_TEXTURE_RECTANGLE_ARB.) */
230 if (target != GL_TEXTURE_RECTANGLE_ARB)
232 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
233 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
234 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
237 if (target == GL_TEXTURE_CUBE_MAP_ARB)
239 /* Cubemaps are always set to clamp, regardless of the sampler state. */
240 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
241 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
242 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
245 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
247 /* Conditinal non power of two textures use a different clamping
248 * default. If we're using the GL_WINE_normalized_texrect partial
249 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
250 * has the address mode set to repeat - something that prevents us
251 * from hitting the accelerated codepath. Thus manually set the GL
252 * state. The same applies to filtering. Even if the texture has only
253 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
254 * fallback on macos. */
255 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
256 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
257 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
258 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
259 checkGLcall("glTexParameteri");
260 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = WINED3D_TADDRESS_CLAMP;
261 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = WINED3D_TADDRESS_CLAMP;
262 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = WINED3D_TEXF_POINT;
263 gl_tex->states[WINED3DTEXSTA_MINFILTER] = WINED3D_TEXF_POINT;
264 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = WINED3D_TEXF_NONE;
268 /* Context activation is done by the caller. */
269 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
270 struct wined3d_context *context, BOOL srgb)
272 DWORD active_sampler;
274 /* We don't need a specific texture unit, but after binding the texture
275 * the current unit is dirty. Read the unit back instead of switching to
276 * 0, this avoids messing around with the state manager's GL states. The
277 * current texture unit should always be a valid one.
279 * To be more specific, this is tricky because we can implicitly be
280 * called from sampler() in state.c. This means we can't touch anything
281 * other than whatever happens to be the currently active texture, or we
282 * would risk marking already applied sampler states dirty again. */
283 active_sampler = context->rev_tex_unit_map[context->active_texture];
284 if (active_sampler != WINED3D_UNMAPPED_STAGE)
285 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
287 wined3d_texture_bind(texture, context, srgb);
290 /* Context activation is done by the caller. */
291 static void apply_wrap(const struct wined3d_gl_info *gl_info, GLenum target,
292 enum wined3d_texture_address d3d_wrap, GLenum param, BOOL cond_np2)
294 GLint gl_wrap;
296 if (d3d_wrap < WINED3D_TADDRESS_WRAP || d3d_wrap > WINED3D_TADDRESS_MIRROR_ONCE)
298 FIXME("Unrecognized or unsupported texture address mode %#x.\n", d3d_wrap);
299 return;
302 /* Cubemaps are always set to clamp, regardless of the sampler state. */
303 if (target == GL_TEXTURE_CUBE_MAP_ARB
304 || (cond_np2 && d3d_wrap == WINED3D_TADDRESS_WRAP))
305 gl_wrap = GL_CLAMP_TO_EDGE;
306 else
307 gl_wrap = gl_info->wrap_lookup[d3d_wrap - WINED3D_TADDRESS_WRAP];
309 TRACE("Setting param %#x to %#x for target %#x.\n", param, gl_wrap, target);
310 gl_info->gl_ops.gl.p_glTexParameteri(target, param, gl_wrap);
311 checkGLcall("glTexParameteri(target, param, gl_wrap)");
314 /* Context activation is done by the caller (state handler). */
315 void wined3d_texture_apply_state_changes(struct wined3d_texture *texture,
316 const DWORD sampler_states[WINED3D_HIGHEST_SAMPLER_STATE + 1],
317 const struct wined3d_gl_info *gl_info)
319 BOOL cond_np2 = texture->flags & WINED3D_TEXTURE_COND_NP2;
320 GLenum target = texture->target;
321 struct gl_texture *gl_tex;
322 DWORD state;
323 DWORD aniso;
325 TRACE("texture %p, sampler_states %p.\n", texture, sampler_states);
327 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
329 /* This function relies on the correct texture being bound and loaded. */
331 if (sampler_states[WINED3D_SAMP_ADDRESS_U] != gl_tex->states[WINED3DTEXSTA_ADDRESSU])
333 state = sampler_states[WINED3D_SAMP_ADDRESS_U];
334 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_S, cond_np2);
335 gl_tex->states[WINED3DTEXSTA_ADDRESSU] = state;
338 if (sampler_states[WINED3D_SAMP_ADDRESS_V] != gl_tex->states[WINED3DTEXSTA_ADDRESSV])
340 state = sampler_states[WINED3D_SAMP_ADDRESS_V];
341 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_T, cond_np2);
342 gl_tex->states[WINED3DTEXSTA_ADDRESSV] = state;
345 if (sampler_states[WINED3D_SAMP_ADDRESS_W] != gl_tex->states[WINED3DTEXSTA_ADDRESSW])
347 state = sampler_states[WINED3D_SAMP_ADDRESS_W];
348 apply_wrap(gl_info, target, state, GL_TEXTURE_WRAP_R, cond_np2);
349 gl_tex->states[WINED3DTEXSTA_ADDRESSW] = state;
352 if (sampler_states[WINED3D_SAMP_BORDER_COLOR] != gl_tex->states[WINED3DTEXSTA_BORDERCOLOR])
354 float col[4];
356 state = sampler_states[WINED3D_SAMP_BORDER_COLOR];
357 D3DCOLORTOGLFLOAT4(state, col);
358 TRACE("Setting border color for %#x to %#x.\n", target, state);
359 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &col[0]);
360 checkGLcall("glTexParameterfv(..., GL_TEXTURE_BORDER_COLOR, ...)");
361 gl_tex->states[WINED3DTEXSTA_BORDERCOLOR] = state;
364 if (sampler_states[WINED3D_SAMP_MAG_FILTER] != gl_tex->states[WINED3DTEXSTA_MAGFILTER])
366 GLint gl_value;
368 state = sampler_states[WINED3D_SAMP_MAG_FILTER];
369 if (state > WINED3D_TEXF_ANISOTROPIC)
370 FIXME("Unrecognized or unsupported MAGFILTER* value %d.\n", state);
372 gl_value = wined3d_gl_mag_filter(texture->mag_lookup,
373 min(max(state, WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR));
374 TRACE("ValueMAG=%#x setting MAGFILTER to %#x.\n", state, gl_value);
375 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, gl_value);
377 gl_tex->states[WINED3DTEXSTA_MAGFILTER] = state;
380 if ((sampler_states[WINED3D_SAMP_MIN_FILTER] != gl_tex->states[WINED3DTEXSTA_MINFILTER]
381 || sampler_states[WINED3D_SAMP_MIP_FILTER] != gl_tex->states[WINED3DTEXSTA_MIPFILTER]
382 || sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL] != gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL]))
384 GLint gl_value;
386 gl_tex->states[WINED3DTEXSTA_MIPFILTER] = sampler_states[WINED3D_SAMP_MIP_FILTER];
387 gl_tex->states[WINED3DTEXSTA_MINFILTER] = sampler_states[WINED3D_SAMP_MIN_FILTER];
388 gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] = sampler_states[WINED3D_SAMP_MAX_MIP_LEVEL];
390 if (gl_tex->states[WINED3DTEXSTA_MINFILTER] > WINED3D_TEXF_ANISOTROPIC
391 || gl_tex->states[WINED3DTEXSTA_MIPFILTER] > WINED3D_TEXF_ANISOTROPIC)
393 FIXME("Unrecognized or unsupported MIN_FILTER value %#x MIP_FILTER value %#x.\n",
394 gl_tex->states[WINED3DTEXSTA_MINFILTER],
395 gl_tex->states[WINED3DTEXSTA_MIPFILTER]);
397 gl_value = wined3d_gl_min_mip_filter(texture->min_mip_lookup,
398 min(max(sampler_states[WINED3D_SAMP_MIN_FILTER], WINED3D_TEXF_POINT), WINED3D_TEXF_LINEAR),
399 min(max(sampler_states[WINED3D_SAMP_MIP_FILTER], WINED3D_TEXF_NONE), WINED3D_TEXF_LINEAR));
401 TRACE("ValueMIN=%#x, ValueMIP=%#x, setting MINFILTER to %#x.\n",
402 sampler_states[WINED3D_SAMP_MIN_FILTER],
403 sampler_states[WINED3D_SAMP_MIP_FILTER], gl_value);
404 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, gl_value);
405 checkGLcall("glTexParameter GL_TEXTURE_MIN_FILTER, ...");
407 if (!cond_np2)
409 if (gl_tex->states[WINED3DTEXSTA_MIPFILTER] == WINED3D_TEXF_NONE)
410 gl_value = texture->lod;
411 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] >= texture->level_count)
412 gl_value = texture->level_count - 1;
413 else if (gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL] < texture->lod)
414 /* texture->lod is already clamped in the setter. */
415 gl_value = texture->lod;
416 else
417 gl_value = gl_tex->states[WINED3DTEXSTA_MAXMIPLEVEL];
419 /* Note that WINED3D_SAMP_MAX_MIP_LEVEL specifies the largest mipmap
420 * (default 0), while GL_TEXTURE_MAX_LEVEL specifies the smallest
421 * mimap used (default 1000). So WINED3D_SAMP_MAX_MIP_LEVEL
422 * corresponds to GL_TEXTURE_BASE_LEVEL. */
423 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_BASE_LEVEL, gl_value);
427 if ((gl_tex->states[WINED3DTEXSTA_MAGFILTER] != WINED3D_TEXF_ANISOTROPIC
428 && gl_tex->states[WINED3DTEXSTA_MINFILTER] != WINED3D_TEXF_ANISOTROPIC
429 && gl_tex->states[WINED3DTEXSTA_MIPFILTER] != WINED3D_TEXF_ANISOTROPIC)
430 || cond_np2)
431 aniso = 1;
432 else
433 aniso = sampler_states[WINED3D_SAMP_MAX_ANISOTROPY];
435 if (gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] != aniso)
437 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
439 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);
440 checkGLcall("glTexParameteri(GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso)");
442 else
444 WARN("Anisotropic filtering not supported.\n");
446 gl_tex->states[WINED3DTEXSTA_MAXANISOTROPY] = aniso;
449 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
450 if (sampler_states[WINED3D_SAMP_SRGB_TEXTURE] != gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE])
452 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
453 sampler_states[WINED3D_SAMP_SRGB_TEXTURE] ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
454 checkGLcall("glTexParameteri(GL_TEXTURE_SRGB_DECODE_EXT)");
455 gl_tex->states[WINED3DTEXSTA_SRGBTEXTURE] = sampler_states[WINED3D_SAMP_SRGB_TEXTURE];
458 if (!(texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
459 != !gl_tex->states[WINED3DTEXSTA_SHADOW])
461 if (texture->resource.format->flags & WINED3DFMT_FLAG_SHADOW)
463 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
464 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
465 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB)");
466 gl_tex->states[WINED3DTEXSTA_SHADOW] = TRUE;
468 else
470 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
471 checkGLcall("glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE)");
472 gl_tex->states[WINED3DTEXSTA_SHADOW] = FALSE;
477 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
479 ULONG refcount = InterlockedIncrement(&texture->resource.ref);
481 TRACE("%p increasing refcount to %u.\n", texture, refcount);
483 return refcount;
486 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
488 ULONG refcount = InterlockedDecrement(&texture->resource.ref);
490 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
492 if (!refcount)
494 wined3d_texture_cleanup(texture);
495 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
496 HeapFree(GetProcessHeap(), 0, texture);
499 return refcount;
502 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
504 TRACE("texture %p.\n", texture);
506 return &texture->resource;
509 /* Context activation is done by the caller */
510 void wined3d_texture_load(struct wined3d_texture *texture,
511 struct wined3d_context *context, BOOL srgb)
513 UINT sub_count = texture->level_count * texture->layer_count;
514 const struct wined3d_gl_info *gl_info = context->gl_info;
515 DWORD flag;
516 UINT i;
518 TRACE("texture %p, srgb %#x.\n", texture, srgb);
520 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
521 srgb = FALSE;
523 if (srgb)
524 flag = WINED3D_TEXTURE_SRGB_VALID;
525 else
526 flag = WINED3D_TEXTURE_RGB_VALID;
528 if (texture->flags & flag)
530 TRACE("Texture %p not dirty, nothing to do.\n", texture);
531 return;
534 /* Reload the surfaces if the texture is marked dirty. */
535 for (i = 0; i < sub_count; ++i)
537 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i], context, srgb);
539 texture->flags |= flag;
542 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
544 struct wined3d_context *context;
545 context = context_acquire(texture->resource.device, NULL);
546 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
547 context_release(context);
550 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
552 TRACE("texture %p.\n", texture);
554 return texture->resource.parent;
557 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
559 DWORD old = texture->lod;
561 TRACE("texture %p, lod %u.\n", texture, lod);
563 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
564 * textures. The call always returns 0, and GetLOD always returns 0. */
565 if (texture->resource.pool != WINED3D_POOL_MANAGED)
567 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
568 return 0;
571 if (lod >= texture->level_count)
572 lod = texture->level_count - 1;
574 if (texture->lod != lod)
576 texture->lod = lod;
578 texture->texture_rgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
579 texture->texture_srgb.states[WINED3DTEXSTA_MAXMIPLEVEL] = ~0U;
580 if (texture->resource.bind_count)
581 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
584 return old;
587 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
589 TRACE("texture %p, returning %u.\n", texture, texture->lod);
591 return texture->lod;
594 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
596 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
598 return texture->level_count;
601 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
602 enum wined3d_texture_filter_type filter_type)
604 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
606 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
608 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
609 return WINED3DERR_INVALIDCALL;
612 texture->filter_type = filter_type;
614 return WINED3D_OK;
617 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
619 TRACE("texture %p.\n", texture);
621 return texture->filter_type;
624 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
625 DWORD flags, const struct wined3d_color_key *color_key)
627 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
629 if (flags & WINEDDCKEY_COLORSPACE)
631 FIXME("Unhandled flags %#x.\n", flags);
632 return WINED3DERR_INVALIDCALL;
635 if (color_key)
637 switch (flags & ~WINEDDCKEY_COLORSPACE)
639 case WINEDDCKEY_DESTBLT:
640 texture->dst_blt_color_key = *color_key;
641 texture->color_key_flags |= WINEDDSD_CKDESTBLT;
642 break;
644 case WINEDDCKEY_DESTOVERLAY:
645 texture->dst_overlay_color_key = *color_key;
646 texture->color_key_flags |= WINEDDSD_CKDESTOVERLAY;
647 break;
649 case WINEDDCKEY_SRCOVERLAY:
650 texture->src_overlay_color_key = *color_key;
651 texture->color_key_flags |= WINEDDSD_CKSRCOVERLAY;
652 break;
654 case WINEDDCKEY_SRCBLT:
655 texture->src_blt_color_key = *color_key;
656 texture->color_key_flags |= WINEDDSD_CKSRCBLT;
657 break;
660 else
662 switch (flags & ~WINEDDCKEY_COLORSPACE)
664 case WINEDDCKEY_DESTBLT:
665 texture->color_key_flags &= ~WINEDDSD_CKDESTBLT;
666 break;
668 case WINEDDCKEY_DESTOVERLAY:
669 texture->color_key_flags &= ~WINEDDSD_CKDESTOVERLAY;
670 break;
672 case WINEDDCKEY_SRCOVERLAY:
673 texture->color_key_flags &= ~WINEDDSD_CKSRCOVERLAY;
674 break;
676 case WINEDDCKEY_SRCBLT:
677 texture->color_key_flags &= ~WINEDDSD_CKSRCBLT;
678 break;
682 return WINED3D_OK;
685 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
687 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
688 FIXME("texture %p stub!\n", texture);
691 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
692 UINT sub_resource_idx)
694 UINT sub_count = texture->level_count * texture->layer_count;
696 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
698 if (sub_resource_idx >= sub_count)
700 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
701 return NULL;
704 return texture->sub_resources[sub_resource_idx];
707 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
708 UINT layer, const struct wined3d_box *dirty_region)
710 struct wined3d_resource *sub_resource;
712 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
714 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
716 WARN("Failed to get sub-resource.\n");
717 return WINED3DERR_INVALIDCALL;
720 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
722 return WINED3D_OK;
725 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
726 struct wined3d_context *context, BOOL srgb)
728 surface_load(surface_from_resource(sub_resource), srgb);
731 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
732 const struct wined3d_box *dirty_region)
734 struct wined3d_surface *surface = surface_from_resource(sub_resource);
736 surface_prepare_map_memory(surface);
737 surface_load_location(surface, surface->map_binding);
738 surface_invalidate_location(surface, ~surface->map_binding);
741 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
743 struct wined3d_surface *surface = surface_from_resource(sub_resource);
745 surface_set_container(surface, NULL);
746 wined3d_surface_decref(surface);
749 static const struct wined3d_texture_ops texture2d_ops =
751 texture2d_sub_resource_load,
752 texture2d_sub_resource_add_dirty_region,
753 texture2d_sub_resource_cleanup,
756 static void wined3d_texture_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];
768 sub_resource->resource_ops->resource_unload(sub_resource);
771 wined3d_texture_unload_gl_texture(texture);
774 static const struct wined3d_resource_ops texture_resource_ops =
776 wined3d_texture_unload,
779 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
780 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
781 const struct wined3d_parent_ops *parent_ops)
783 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
784 struct wined3d_resource_desc surface_desc;
785 unsigned int i, j;
786 HRESULT hr;
788 /* TODO: It should only be possible to create textures for formats
789 * that are reported as supported. */
790 if (WINED3DFMT_UNKNOWN >= desc->format)
792 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
793 return WINED3DERR_INVALIDCALL;
796 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
798 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
799 return WINED3DERR_INVALIDCALL;
802 /* Calculate levels for mip mapping */
803 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
805 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
807 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
808 return WINED3DERR_INVALIDCALL;
811 if (levels > 1)
813 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
814 return WINED3DERR_INVALIDCALL;
817 levels = 1;
819 else if (!levels)
821 levels = wined3d_log2i(desc->width) + 1;
822 TRACE("Calculated levels = %u.\n", levels);
825 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
827 UINT pow2_edge_length = 1;
828 while (pow2_edge_length < desc->width)
829 pow2_edge_length <<= 1;
831 if (desc->width != pow2_edge_length)
833 if (desc->pool == WINED3D_POOL_SCRATCH)
835 /* SCRATCH textures cannot be used for texturing */
836 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
838 else
840 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
841 return WINED3DERR_INVALIDCALL;
846 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels,
847 desc, device, parent, parent_ops, &texture_resource_ops)))
849 WARN("Failed to initialize texture, returning %#x\n", hr);
850 return hr;
853 texture->pow2_matrix[0] = 1.0f;
854 texture->pow2_matrix[5] = 1.0f;
855 texture->pow2_matrix[10] = 1.0f;
856 texture->pow2_matrix[15] = 1.0f;
857 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
859 /* Generate all the surfaces. */
860 surface_desc = *desc;
861 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
862 for (i = 0; i < texture->level_count; ++i)
864 /* Create the 6 faces. */
865 for (j = 0; j < 6; ++j)
867 static const GLenum cube_targets[6] =
869 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
870 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
871 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
872 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
873 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
874 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
876 UINT idx = j * texture->level_count + i;
877 struct wined3d_surface *surface;
879 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
880 cube_targets[j], i, surface_flags, &surface)))
882 WARN("Failed to create surface, hr %#x.\n", hr);
883 wined3d_texture_cleanup(texture);
884 return hr;
887 texture->sub_resources[idx] = &surface->resource;
888 TRACE("Created surface level %u @ %p.\n", i, surface);
890 surface_desc.width = max(1, surface_desc.width >> 1);
891 surface_desc.height = surface_desc.width;
894 return WINED3D_OK;
897 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
898 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
899 const struct wined3d_parent_ops *parent_ops)
901 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
902 struct wined3d_resource_desc surface_desc;
903 UINT pow2_width, pow2_height;
904 unsigned int i;
905 HRESULT hr;
907 /* TODO: It should only be possible to create textures for formats
908 * that are reported as supported. */
909 if (WINED3DFMT_UNKNOWN >= desc->format)
911 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
912 return WINED3DERR_INVALIDCALL;
915 /* Non-power2 support. */
916 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
918 pow2_width = desc->width;
919 pow2_height = desc->height;
921 else
923 /* Find the nearest pow2 match. */
924 pow2_width = pow2_height = 1;
925 while (pow2_width < desc->width)
926 pow2_width <<= 1;
927 while (pow2_height < desc->height)
928 pow2_height <<= 1;
930 if (pow2_width != desc->width || pow2_height != desc->height)
932 /* levels == 0 returns an error as well */
933 if (levels != 1)
935 if (desc->pool == WINED3D_POOL_SCRATCH)
937 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
939 else
941 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
942 return WINED3DERR_INVALIDCALL;
948 /* Calculate levels for mip mapping. */
949 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
951 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
953 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
954 return WINED3DERR_INVALIDCALL;
957 if (levels > 1)
959 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning WINED3DERR_INVALIDCALL.\n");
960 return WINED3DERR_INVALIDCALL;
963 levels = 1;
965 else if (!levels)
967 levels = wined3d_log2i(max(desc->width, desc->height)) + 1;
968 TRACE("Calculated levels = %u.\n", levels);
971 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels,
972 desc, device, parent, parent_ops, &texture_resource_ops)))
974 WARN("Failed to initialize texture, returning %#x.\n", hr);
975 return hr;
978 /* Precalculated scaling for 'faked' non power of two texture coords. */
979 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
980 && (desc->width != pow2_width || desc->height != pow2_height))
982 texture->pow2_matrix[0] = 1.0f;
983 texture->pow2_matrix[5] = 1.0f;
984 texture->pow2_matrix[10] = 1.0f;
985 texture->pow2_matrix[15] = 1.0f;
986 texture->target = GL_TEXTURE_2D;
987 texture->flags |= WINED3D_TEXTURE_COND_NP2;
988 texture->min_mip_lookup = minMipLookup_noFilter;
990 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
991 && (desc->width != pow2_width || desc->height != pow2_height))
993 texture->pow2_matrix[0] = (float)desc->width;
994 texture->pow2_matrix[5] = (float)desc->height;
995 texture->pow2_matrix[10] = 1.0f;
996 texture->pow2_matrix[15] = 1.0f;
997 texture->target = GL_TEXTURE_RECTANGLE_ARB;
998 texture->flags |= WINED3D_TEXTURE_COND_NP2;
999 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1001 if (texture->resource.format->flags & WINED3DFMT_FLAG_FILTERING)
1002 texture->min_mip_lookup = minMipLookup_noMip;
1003 else
1004 texture->min_mip_lookup = minMipLookup_noFilter;
1006 else
1008 if ((desc->width != pow2_width) || (desc->height != pow2_height))
1010 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1011 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1012 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1014 else
1016 texture->pow2_matrix[0] = 1.0f;
1017 texture->pow2_matrix[5] = 1.0f;
1020 texture->pow2_matrix[10] = 1.0f;
1021 texture->pow2_matrix[15] = 1.0f;
1022 texture->target = GL_TEXTURE_2D;
1024 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1026 /* Generate all the surfaces. */
1027 surface_desc = *desc;
1028 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1029 for (i = 0; i < texture->level_count; ++i)
1031 struct wined3d_surface *surface;
1033 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1034 texture->target, i, surface_flags, &surface)))
1036 WARN("Failed to create surface, hr %#x.\n", hr);
1037 wined3d_texture_cleanup(texture);
1038 return hr;
1041 texture->sub_resources[i] = &surface->resource;
1042 TRACE("Created surface level %u @ %p.\n", i, surface);
1043 /* Calculate the next mipmap level. */
1044 surface_desc.width = max(1, surface_desc.width >> 1);
1045 surface_desc.height = max(1, surface_desc.height >> 1);
1048 return WINED3D_OK;
1051 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1052 struct wined3d_context *context, BOOL srgb)
1054 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1057 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1058 const struct wined3d_box *dirty_region)
1060 wined3d_texture_set_dirty(volume_from_resource(sub_resource)->container);
1063 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1065 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1067 /* Cleanup the container. */
1068 volume_set_container(volume, NULL);
1069 wined3d_volume_decref(volume);
1072 static const struct wined3d_texture_ops texture3d_ops =
1074 texture3d_sub_resource_load,
1075 texture3d_sub_resource_add_dirty_region,
1076 texture3d_sub_resource_cleanup,
1079 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1080 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1082 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1083 struct wined3d_resource_desc volume_desc;
1084 unsigned int i;
1085 HRESULT hr;
1087 /* TODO: It should only be possible to create textures for formats
1088 * that are reported as supported. */
1089 if (WINED3DFMT_UNKNOWN >= desc->format)
1091 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1092 return WINED3DERR_INVALIDCALL;
1095 if (!gl_info->supported[EXT_TEXTURE3D])
1097 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1098 return WINED3DERR_INVALIDCALL;
1101 /* Calculate levels for mip mapping. */
1102 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1104 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1106 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1107 return WINED3DERR_INVALIDCALL;
1110 if (levels > 1)
1112 WARN("D3DUSAGE_AUTOGENMIPMAP is set, and level count > 1, returning D3DERR_INVALIDCALL.\n");
1113 return WINED3DERR_INVALIDCALL;
1116 levels = 1;
1118 else if (!levels)
1120 levels = wined3d_log2i(max(max(desc->width, desc->height), desc->depth)) + 1;
1121 TRACE("Calculated levels = %u.\n", levels);
1124 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1126 UINT pow2_w, pow2_h, pow2_d;
1127 pow2_w = 1;
1128 while (pow2_w < desc->width)
1129 pow2_w <<= 1;
1130 pow2_h = 1;
1131 while (pow2_h < desc->height)
1132 pow2_h <<= 1;
1133 pow2_d = 1;
1134 while (pow2_d < desc->depth)
1135 pow2_d <<= 1;
1137 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1139 if (desc->pool == WINED3D_POOL_SCRATCH)
1141 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1143 else
1145 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1146 desc->width, desc->height, desc->depth);
1147 return WINED3DERR_INVALIDCALL;
1152 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels,
1153 desc, device, parent, parent_ops, &texture_resource_ops)))
1155 WARN("Failed to initialize texture, returning %#x.\n", hr);
1156 return hr;
1159 texture->pow2_matrix[0] = 1.0f;
1160 texture->pow2_matrix[5] = 1.0f;
1161 texture->pow2_matrix[10] = 1.0f;
1162 texture->pow2_matrix[15] = 1.0f;
1163 texture->target = GL_TEXTURE_3D;
1165 /* Generate all the surfaces. */
1166 volume_desc = *desc;
1167 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1168 for (i = 0; i < texture->level_count; ++i)
1170 struct wined3d_volume *volume;
1172 if (FAILED(hr = wined3d_volume_create(texture, &volume_desc, i, &volume)))
1174 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1175 wined3d_texture_cleanup(texture);
1176 return hr;
1179 texture->sub_resources[i] = &volume->resource;
1181 /* Calculate the next mipmap level. */
1182 volume_desc.width = max(1, volume_desc.width >> 1);
1183 volume_desc.height = max(1, volume_desc.height >> 1);
1184 volume_desc.depth = max(1, volume_desc.depth >> 1);
1187 return WINED3D_OK;
1190 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1191 UINT level_count, DWORD surface_flags, void *parent, const struct wined3d_parent_ops *parent_ops,
1192 struct wined3d_texture **texture)
1194 struct wined3d_texture *object;
1195 HRESULT hr;
1197 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, parent %p, parent_ops %p, texture %p.\n",
1198 device, desc, level_count, surface_flags, parent, parent_ops, texture);
1200 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1201 return E_OUTOFMEMORY;
1203 switch (desc->resource_type)
1205 case WINED3D_RTYPE_TEXTURE:
1206 hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1207 break;
1209 case WINED3D_RTYPE_VOLUME_TEXTURE:
1210 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
1211 break;
1213 case WINED3D_RTYPE_CUBE_TEXTURE:
1214 hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1215 break;
1217 default:
1218 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
1219 hr = WINED3DERR_INVALIDCALL;
1220 break;
1223 if (FAILED(hr))
1225 WARN("Failed to initialize texture, returning %#x.\n", hr);
1226 HeapFree(GetProcessHeap(), 0, object);
1227 return hr;
1230 TRACE("Created texture %p.\n", object);
1231 *texture = object;
1233 return WINED3D_OK;