po: Fix a mistake in Dutch translation.
[wine.git] / dlls / wined3d / texture.c
blob6b2e266ac76b0649002a24d459789ee95ad248a5
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, DWORD surface_flags,
32 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
33 const struct wined3d_resource_ops *resource_ops)
35 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
36 HRESULT hr;
38 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
39 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
40 "surface_flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
41 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
42 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
43 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
44 surface_flags, device, parent, parent_ops, resource_ops);
46 if ((format->flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BLOCKS_NO_VERIFY)) == WINED3DFMT_FLAG_BLOCKS)
48 UINT width_mask = format->block_width - 1;
49 UINT height_mask = format->block_height - 1;
50 if (desc->width & width_mask || desc->height & height_mask)
51 return WINED3DERR_INVALIDCALL;
54 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
55 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
56 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
58 static unsigned int once;
60 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
61 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
62 && !(format->flags & WINED3DFMT_FLAG_TEXTURE) && !once++)
63 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
65 WARN("Failed to initialize resource, returning %#x\n", hr);
66 return hr;
68 wined3d_resource_update_draw_binding(&texture->resource);
70 texture->texture_ops = texture_ops;
71 texture->sub_resources = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
72 level_count * layer_count * sizeof(*texture->sub_resources));
73 if (!texture->sub_resources)
75 ERR("Failed to allocate sub-resource array.\n");
76 resource_cleanup(&texture->resource);
77 return E_OUTOFMEMORY;
80 texture->layer_count = layer_count;
81 texture->level_count = level_count;
82 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
83 texture->lod = 0;
84 texture->flags = WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
85 if (surface_flags & WINED3D_SURFACE_PIN_SYSMEM)
86 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
88 return WINED3D_OK;
91 /* A GL context is provided by the caller */
92 static void gltexture_delete(const struct wined3d_gl_info *gl_info, struct gl_texture *tex)
94 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
95 tex->name = 0;
98 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
100 struct wined3d_device *device = texture->resource.device;
101 struct wined3d_context *context = NULL;
103 if (texture->texture_rgb.name || texture->texture_srgb.name)
105 context = context_acquire(device, NULL);
108 if (texture->texture_rgb.name)
109 gltexture_delete(context->gl_info, &texture->texture_rgb);
111 if (texture->texture_srgb.name)
112 gltexture_delete(context->gl_info, &texture->texture_srgb);
114 if (context) context_release(context);
116 wined3d_texture_set_dirty(texture);
118 resource_unload(&texture->resource);
121 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
123 UINT sub_count = texture->level_count * texture->layer_count;
124 UINT i;
126 TRACE("texture %p.\n", texture);
128 for (i = 0; i < sub_count; ++i)
130 struct wined3d_resource *sub_resource = texture->sub_resources[i];
132 if (sub_resource)
133 texture->texture_ops->texture_sub_resource_cleanup(sub_resource);
136 wined3d_texture_unload_gl_texture(texture);
137 HeapFree(GetProcessHeap(), 0, texture->sub_resources);
138 resource_cleanup(&texture->resource);
141 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
143 texture->swapchain = swapchain;
144 wined3d_resource_update_draw_binding(&texture->resource);
147 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
149 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
152 /* Context activation is done by the caller. */
153 void wined3d_texture_bind(struct wined3d_texture *texture,
154 struct wined3d_context *context, BOOL srgb)
156 const struct wined3d_gl_info *gl_info = context->gl_info;
157 struct gl_texture *gl_tex;
158 GLenum target;
160 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
162 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
163 srgb = FALSE;
165 /* sRGB mode cache for preload() calls outside drawprim. */
166 if (srgb)
167 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
168 else
169 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
171 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
172 target = texture->target;
174 if (gl_tex->name)
176 context_bind_texture(context, target, gl_tex->name);
177 return;
180 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
181 checkGLcall("glGenTextures");
182 TRACE("Generated texture %d.\n", gl_tex->name);
184 if (!gl_tex->name)
186 ERR("Failed to generate a texture name.\n");
187 return;
190 if (texture->resource.pool == WINED3D_POOL_DEFAULT)
192 /* Tell OpenGL to try and keep this texture in video ram (well mostly). */
193 GLclampf tmp = 0.9f;
194 gl_info->gl_ops.gl.p_glPrioritizeTextures(1, &gl_tex->name, &tmp);
197 /* Initialise the state of the texture object to the OpenGL defaults, not
198 * the wined3d defaults. */
199 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
200 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
201 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
202 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
203 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
204 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
205 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
206 gl_tex->sampler_desc.lod_bias = 0.0f;
207 gl_tex->sampler_desc.min_lod = -1000.0f;
208 gl_tex->sampler_desc.max_lod = 1000.0f;
209 gl_tex->sampler_desc.max_anisotropy = 1;
210 gl_tex->sampler_desc.compare = FALSE;
211 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
212 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
213 gl_tex->sampler_desc.srgb_decode = TRUE;
214 else
215 gl_tex->sampler_desc.srgb_decode = srgb;
216 gl_tex->base_level = 0;
217 wined3d_texture_set_dirty(texture);
219 context_bind_texture(context, target, gl_tex->name);
221 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
223 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
224 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
227 /* For a new texture we have to set the texture levels after binding the
228 * texture. Beware that texture rectangles do not support mipmapping, but
229 * set the maxmiplevel if we're relying on the partial
230 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
231 * (I.e., do not care about cond_np2 here, just look for
232 * GL_TEXTURE_RECTANGLE_ARB.) */
233 if (target != GL_TEXTURE_RECTANGLE_ARB)
235 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
236 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
237 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
240 if (target == GL_TEXTURE_CUBE_MAP_ARB)
242 /* Cubemaps are always set to clamp, regardless of the sampler state. */
243 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
244 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
245 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
248 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
250 /* Conditinal non power of two textures use a different clamping
251 * default. If we're using the GL_WINE_normalized_texrect partial
252 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
253 * has the address mode set to repeat - something that prevents us
254 * from hitting the accelerated codepath. Thus manually set the GL
255 * state. The same applies to filtering. Even if the texture has only
256 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
257 * fallback on macos. */
258 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
259 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
260 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
261 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
262 checkGLcall("glTexParameteri");
263 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
264 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
265 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
266 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
267 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
271 /* Context activation is done by the caller. */
272 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
273 struct wined3d_context *context, BOOL srgb)
275 DWORD active_sampler;
277 /* We don't need a specific texture unit, but after binding the texture
278 * the current unit is dirty. Read the unit back instead of switching to
279 * 0, this avoids messing around with the state manager's GL states. The
280 * current texture unit should always be a valid one.
282 * To be more specific, this is tricky because we can implicitly be
283 * called from sampler() in state.c. This means we can't touch anything
284 * other than whatever happens to be the currently active texture, or we
285 * would risk marking already applied sampler states dirty again. */
286 active_sampler = context->rev_tex_unit_map[context->active_texture];
287 if (active_sampler != WINED3D_UNMAPPED_STAGE)
288 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
290 wined3d_texture_bind(texture, context, srgb);
293 /* Context activation is done by the caller (state handler). */
294 /* This function relies on the correct texture being bound and loaded. */
295 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
296 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_gl_info *gl_info)
298 GLenum target = texture->target;
299 struct gl_texture *gl_tex;
300 DWORD state;
302 TRACE("texture %p, sampler_desc %p, gl_info %p.\n", texture, sampler_desc, gl_info);
304 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
306 state = sampler_desc->address_u;
307 if (state != gl_tex->sampler_desc.address_u)
309 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
310 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
311 gl_tex->sampler_desc.address_u = state;
314 state = sampler_desc->address_v;
315 if (state != gl_tex->sampler_desc.address_v)
317 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
318 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
319 gl_tex->sampler_desc.address_v = state;
322 state = sampler_desc->address_w;
323 if (state != gl_tex->sampler_desc.address_w)
325 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
326 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
327 gl_tex->sampler_desc.address_w = state;
330 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
331 sizeof(gl_tex->sampler_desc.border_color)))
333 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
334 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
335 sizeof(gl_tex->sampler_desc.border_color));
338 state = sampler_desc->mag_filter;
339 if (state != gl_tex->sampler_desc.mag_filter)
341 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
342 gl_tex->sampler_desc.mag_filter = state;
345 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
346 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
348 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
349 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
350 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
351 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
354 state = sampler_desc->max_anisotropy;
355 if (state != gl_tex->sampler_desc.max_anisotropy)
357 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
358 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
359 else
360 WARN("Anisotropic filtering not supported.\n");
361 gl_tex->sampler_desc.max_anisotropy = state;
364 /* These should always be the same unless EXT_texture_sRGB_decode is supported. */
365 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode)
367 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
368 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
369 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
372 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
374 if (sampler_desc->compare)
376 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
377 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
379 else
381 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
383 gl_tex->sampler_desc.compare = sampler_desc->compare;
386 checkGLcall("Texture parameter application");
388 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
390 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
391 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
392 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
396 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
398 ULONG refcount;
400 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
402 if (texture->swapchain)
403 return wined3d_swapchain_incref(texture->swapchain);
405 refcount = InterlockedIncrement(&texture->resource.ref);
406 TRACE("%p increasing refcount to %u.\n", texture, refcount);
408 return refcount;
411 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
413 ULONG refcount;
415 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
417 if (texture->swapchain)
418 return wined3d_swapchain_decref(texture->swapchain);
420 refcount = InterlockedDecrement(&texture->resource.ref);
421 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
423 if (!refcount)
425 wined3d_texture_cleanup(texture);
426 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
427 HeapFree(GetProcessHeap(), 0, texture);
430 return refcount;
433 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
435 TRACE("texture %p.\n", texture);
437 return &texture->resource;
440 /* Context activation is done by the caller */
441 void wined3d_texture_load(struct wined3d_texture *texture,
442 struct wined3d_context *context, BOOL srgb)
444 UINT sub_count = texture->level_count * texture->layer_count;
445 const struct wined3d_gl_info *gl_info = context->gl_info;
446 DWORD flag;
447 UINT i;
449 TRACE("texture %p, srgb %#x.\n", texture, srgb);
451 if (gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
452 srgb = FALSE;
454 if (srgb)
455 flag = WINED3D_TEXTURE_SRGB_VALID;
456 else
457 flag = WINED3D_TEXTURE_RGB_VALID;
459 if (!(texture->flags & WINED3D_TEXTURE_COLOR_KEY) != !(texture->color_key_flags & WINED3D_CKEY_SRC_BLT)
460 || (texture->flags & WINED3D_TEXTURE_COLOR_KEY
461 && (texture->gl_color_key.color_space_low_value != texture->src_blt_color_key.color_space_low_value
462 || texture->gl_color_key.color_space_high_value != texture->src_blt_color_key.color_space_high_value)))
464 unsigned int sub_count = texture->level_count * texture->layer_count;
465 unsigned int i;
467 TRACE("Reloading because of color key value change.\n");
468 for (i = 0; i < sub_count; i++)
469 texture->texture_ops->texture_sub_resource_add_dirty_region(texture->sub_resources[i], NULL);
470 wined3d_texture_set_dirty(texture);
472 texture->gl_color_key = texture->src_blt_color_key;
475 if (texture->flags & flag)
477 TRACE("Texture %p not dirty, nothing to do.\n", texture);
478 return;
481 /* Reload the surfaces if the texture is marked dirty. */
482 for (i = 0; i < sub_count; ++i)
484 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i], context, srgb);
486 texture->flags |= flag;
489 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
491 struct wined3d_context *context;
492 context = context_acquire(texture->resource.device, NULL);
493 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
494 context_release(context);
497 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
499 TRACE("texture %p.\n", texture);
501 return texture->resource.parent;
504 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
506 DWORD old = texture->lod;
508 TRACE("texture %p, lod %u.\n", texture, lod);
510 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
511 * textures. The call always returns 0, and GetLOD always returns 0. */
512 if (texture->resource.pool != WINED3D_POOL_MANAGED)
514 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
515 return 0;
518 if (lod >= texture->level_count)
519 lod = texture->level_count - 1;
521 if (texture->lod != lod)
523 texture->lod = lod;
525 texture->texture_rgb.base_level = ~0u;
526 texture->texture_srgb.base_level = ~0u;
527 if (texture->resource.bind_count)
528 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
531 return old;
534 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
536 TRACE("texture %p, returning %u.\n", texture, texture->lod);
538 return texture->lod;
541 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
543 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
545 return texture->level_count;
548 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
549 enum wined3d_texture_filter_type filter_type)
551 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
553 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
555 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
556 return WINED3DERR_INVALIDCALL;
559 texture->filter_type = filter_type;
561 return WINED3D_OK;
564 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
566 TRACE("texture %p.\n", texture);
568 return texture->filter_type;
571 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
572 DWORD flags, const struct wined3d_color_key *color_key)
574 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
576 if (flags & WINED3D_CKEY_COLORSPACE)
578 FIXME("Unhandled flags %#x.\n", flags);
579 return WINED3DERR_INVALIDCALL;
582 if (color_key)
584 switch (flags & ~WINED3D_CKEY_COLORSPACE)
586 case WINED3D_CKEY_DST_BLT:
587 texture->dst_blt_color_key = *color_key;
588 texture->color_key_flags |= WINED3D_CKEY_DST_BLT;
589 break;
591 case WINED3D_CKEY_DST_OVERLAY:
592 texture->dst_overlay_color_key = *color_key;
593 texture->color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
594 break;
596 case WINED3D_CKEY_SRC_BLT:
597 texture->src_blt_color_key = *color_key;
598 texture->color_key_flags |= WINED3D_CKEY_SRC_BLT;
599 break;
601 case WINED3D_CKEY_SRC_OVERLAY:
602 texture->src_overlay_color_key = *color_key;
603 texture->color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
604 break;
607 else
609 switch (flags & ~WINED3D_CKEY_COLORSPACE)
611 case WINED3D_CKEY_DST_BLT:
612 texture->color_key_flags &= ~WINED3D_CKEY_DST_BLT;
613 break;
615 case WINED3D_CKEY_DST_OVERLAY:
616 texture->color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
617 break;
619 case WINED3D_CKEY_SRC_BLT:
620 texture->color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
621 break;
623 case WINED3D_CKEY_SRC_OVERLAY:
624 texture->color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
625 break;
629 return WINED3D_OK;
632 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
633 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
634 UINT multisample_quality, void *mem, UINT pitch)
636 struct wined3d_device *device = texture->resource.device;
637 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
638 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
639 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
640 struct wined3d_surface *surface;
642 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
643 "mem %p, pitch %u.\n",
644 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_type, mem, pitch);
646 if (!resource_size)
647 return WINED3DERR_INVALIDCALL;
649 if (texture->level_count * texture->layer_count > 1)
651 WARN("Texture has multiple sub-resources, not supported.\n");
652 return WINED3DERR_INVALIDCALL;
655 if (texture->resource.type == WINED3D_RTYPE_VOLUME_TEXTURE)
657 WARN("Not supported on volume textures.\n");
658 return WINED3DERR_INVALIDCALL;
661 /* We have no way of supporting a pitch that is not a multiple of the pixel
662 * byte width short of uploading the texture row-by-row.
663 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
664 * for user-memory textures (it always expects packed data) while DirectDraw
665 * requires a 4-byte aligned pitch and doesn't support texture formats
666 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
667 * This check is here to verify that the assumption holds. */
668 if (pitch % texture->resource.format->byte_count)
670 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
671 return WINED3DERR_INVALIDCALL;
674 surface = surface_from_resource(texture->sub_resources[0]);
675 if (surface->resource.map_count || (surface->flags & SFLAG_DCINUSE))
677 WARN("Surface is mapped or the DC is in use.\n");
678 return WINED3DERR_INVALIDCALL;
681 if (device->d3d_initialized)
682 texture->resource.resource_ops->resource_unload(&texture->resource);
684 texture->resource.format = format;
685 texture->resource.multisample_type = multisample_type;
686 texture->resource.multisample_quality = multisample_quality;
687 texture->resource.width = width;
688 texture->resource.height = height;
690 return wined3d_surface_update_desc(surface, gl_info, mem, pitch);
693 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
695 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
697 if (!(texture->flags & WINED3D_TEXTURE_COLOR_KEY) != !(texture->color_key_flags & WINED3D_CKEY_SRC_BLT))
698 wined3d_texture_force_reload(texture);
700 if (texture->flags & alloc_flag)
701 return;
703 if (texture->color_key_flags & WINED3D_CKEY_SRC_BLT)
704 texture->flags |= WINED3D_TEXTURE_COLOR_KEY;
706 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
707 texture->flags |= alloc_flag;
710 void wined3d_texture_force_reload(struct wined3d_texture *texture)
712 unsigned int sub_count = texture->level_count * texture->layer_count;
713 unsigned int i;
715 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
716 | WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COLOR_KEY);
717 for (i = 0; i < sub_count; ++i)
719 texture->texture_ops->texture_sub_resource_invalidate_location(texture->sub_resources[i],
720 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
724 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
726 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
727 FIXME("texture %p stub!\n", texture);
730 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
731 UINT sub_resource_idx)
733 UINT sub_count = texture->level_count * texture->layer_count;
735 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
737 if (sub_resource_idx >= sub_count)
739 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
740 return NULL;
743 return texture->sub_resources[sub_resource_idx];
746 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
747 UINT layer, const struct wined3d_box *dirty_region)
749 struct wined3d_resource *sub_resource;
751 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
753 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
755 WARN("Failed to get sub-resource.\n");
756 return WINED3DERR_INVALIDCALL;
759 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
761 return WINED3D_OK;
764 static void wined3d_texture_upload_data(struct wined3d_texture *texture, const struct wined3d_sub_resource_data *data)
766 unsigned int sub_count = texture->level_count * texture->layer_count;
767 struct wined3d_context *context;
768 unsigned int i;
770 context = context_acquire(texture->resource.device, NULL);
772 wined3d_texture_prepare_texture(texture, context, FALSE);
773 wined3d_texture_bind(texture, context, FALSE);
775 for (i = 0; i < sub_count; ++i)
777 struct wined3d_resource *sub_resource = texture->sub_resources[i];
779 texture->texture_ops->texture_sub_resource_upload_data(sub_resource, context, &data[i]);
780 texture->texture_ops->texture_sub_resource_validate_location(sub_resource, WINED3D_LOCATION_TEXTURE_RGB);
781 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource, ~WINED3D_LOCATION_TEXTURE_RGB);
784 context_release(context);
787 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
788 struct wined3d_context *context, BOOL srgb)
790 surface_load(surface_from_resource(sub_resource), srgb);
793 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
794 const struct wined3d_box *dirty_region)
796 struct wined3d_surface *surface = surface_from_resource(sub_resource);
798 surface_prepare_map_memory(surface);
799 surface_load_location(surface, surface->resource.map_binding);
800 surface_invalidate_location(surface, ~surface->resource.map_binding);
803 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
805 struct wined3d_surface *surface = surface_from_resource(sub_resource);
807 wined3d_surface_destroy(surface);
810 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
812 struct wined3d_surface *surface = surface_from_resource(sub_resource);
814 surface_invalidate_location(surface, location);
817 static void texture2d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
819 struct wined3d_surface *surface = surface_from_resource(sub_resource);
821 surface_validate_location(surface, location);
824 static void texture2d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
825 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
827 struct wined3d_surface *surface = surface_from_resource(sub_resource);
828 static const POINT dst_point = {0, 0};
829 struct wined3d_const_bo_address addr;
830 RECT src_rect;
832 src_rect.left = 0;
833 src_rect.top = 0;
834 src_rect.right = surface->resource.width;
835 src_rect.bottom = surface->resource.height;
837 addr.buffer_object = 0;
838 addr.addr = data->data;
840 wined3d_surface_upload_data(surface, context->gl_info, surface->container->resource.format,
841 &src_rect, data->row_pitch, &dst_point, FALSE, &addr);
844 /* Context activation is done by the caller. */
845 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
847 UINT sub_count = texture->level_count * texture->layer_count;
848 const struct wined3d_format *format = texture->resource.format;
849 const struct wined3d_gl_info *gl_info = context->gl_info;
850 const struct wined3d_color_key_conversion *conversion;
851 GLenum internal;
852 UINT i;
854 TRACE("texture %p, format %s.\n", texture, debug_d3dformat(format->id));
856 if (format->convert)
858 texture->flags |= WINED3D_TEXTURE_CONVERTED;
860 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
862 texture->flags |= WINED3D_TEXTURE_CONVERTED;
863 format = wined3d_get_format(gl_info, conversion->dst_format);
864 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
867 wined3d_texture_bind_and_dirtify(texture, context, srgb);
869 if (srgb)
870 internal = format->glGammaInternal;
871 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
872 && wined3d_resource_is_offscreen(&texture->resource))
873 internal = format->rtInternal;
874 else
875 internal = format->glInternal;
877 if (!internal)
878 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
880 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
882 for (i = 0; i < sub_count; ++i)
884 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
885 GLsizei height = surface->pow2Height;
886 GLsizei width = surface->pow2Width;
887 const BYTE *mem = NULL;
889 if (format->flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
891 height *= format->height_scale.numerator;
892 height /= format->height_scale.denominator;
895 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
896 surface, surface->texture_target, surface->texture_level, width, height);
898 if (gl_info->supported[APPLE_CLIENT_STORAGE])
900 if (surface->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION)
901 || texture->flags & WINED3D_TEXTURE_CONVERTED
902 || !surface->resource.heap_memory)
904 /* In some cases we want to disable client storage.
905 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
906 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
907 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
908 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
910 surface->flags &= ~SFLAG_CLIENT;
912 else
914 surface->flags |= SFLAG_CLIENT;
915 mem = surface->resource.heap_memory;
917 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
918 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
922 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
924 GL_EXTCALL(glCompressedTexImage2D(surface->texture_target, surface->texture_level,
925 internal, width, height, 0, surface->resource.size, mem));
926 checkGLcall("glCompressedTexImage2D");
928 else
930 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
931 internal, width, height, 0, format->glFormat, format->glType, mem);
932 checkGLcall("glTexImage2D");
935 if (mem)
937 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
938 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
943 static const struct wined3d_texture_ops texture2d_ops =
945 texture2d_sub_resource_load,
946 texture2d_sub_resource_add_dirty_region,
947 texture2d_sub_resource_cleanup,
948 texture2d_sub_resource_invalidate_location,
949 texture2d_sub_resource_validate_location,
950 texture2d_sub_resource_upload_data,
951 texture2d_prepare_texture,
954 static ULONG texture_resource_incref(struct wined3d_resource *resource)
956 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
959 static ULONG texture_resource_decref(struct wined3d_resource *resource)
961 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
964 static void wined3d_texture_unload(struct wined3d_resource *resource)
966 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
967 UINT sub_count = texture->level_count * texture->layer_count;
968 UINT i;
970 TRACE("texture %p.\n", texture);
972 for (i = 0; i < sub_count; ++i)
974 struct wined3d_resource *sub_resource = texture->sub_resources[i];
976 sub_resource->resource_ops->resource_unload(sub_resource);
979 wined3d_texture_force_reload(texture);
980 wined3d_texture_unload_gl_texture(texture);
983 static const struct wined3d_resource_ops texture_resource_ops =
985 texture_resource_incref,
986 texture_resource_decref,
987 wined3d_texture_unload,
990 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
991 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
992 const struct wined3d_parent_ops *parent_ops)
994 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
995 struct wined3d_resource_desc surface_desc;
996 unsigned int i, j;
997 HRESULT hr;
999 /* TODO: It should only be possible to create textures for formats
1000 * that are reported as supported. */
1001 if (WINED3DFMT_UNKNOWN >= desc->format)
1003 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1004 return WINED3DERR_INVALIDCALL;
1007 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
1009 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
1010 return WINED3DERR_INVALIDCALL;
1013 /* Calculate levels for mip mapping */
1014 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1016 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1018 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1019 return WINED3DERR_INVALIDCALL;
1022 if (levels != 1)
1024 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1025 return WINED3DERR_INVALIDCALL;
1029 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1031 UINT pow2_edge_length = 1;
1032 while (pow2_edge_length < desc->width)
1033 pow2_edge_length <<= 1;
1035 if (desc->width != pow2_edge_length)
1037 if (desc->pool == WINED3D_POOL_SCRATCH)
1039 /* SCRATCH textures cannot be used for texturing */
1040 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1042 else
1044 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
1045 return WINED3DERR_INVALIDCALL;
1050 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels, desc,
1051 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1053 WARN("Failed to initialize texture, returning %#x\n", hr);
1054 return hr;
1057 texture->pow2_matrix[0] = 1.0f;
1058 texture->pow2_matrix[5] = 1.0f;
1059 texture->pow2_matrix[10] = 1.0f;
1060 texture->pow2_matrix[15] = 1.0f;
1061 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1063 /* Generate all the surfaces. */
1064 surface_desc = *desc;
1065 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1066 for (i = 0; i < texture->level_count; ++i)
1068 /* Create the 6 faces. */
1069 for (j = 0; j < 6; ++j)
1071 static const GLenum cube_targets[6] =
1073 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1074 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1075 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1076 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1077 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1078 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1080 UINT idx = j * texture->level_count + i;
1081 struct wined3d_surface *surface;
1083 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1084 cube_targets[j], i, j, surface_flags, &surface)))
1086 WARN("Failed to create surface, hr %#x.\n", hr);
1087 wined3d_texture_cleanup(texture);
1088 return hr;
1091 texture->sub_resources[idx] = &surface->resource;
1092 TRACE("Created surface level %u @ %p.\n", i, surface);
1094 surface_desc.width = max(1, surface_desc.width >> 1);
1095 surface_desc.height = surface_desc.width;
1098 return WINED3D_OK;
1101 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1102 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
1103 const struct wined3d_parent_ops *parent_ops)
1105 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1106 struct wined3d_resource_desc surface_desc;
1107 UINT pow2_width, pow2_height;
1108 unsigned int i;
1109 HRESULT hr;
1111 /* TODO: It should only be possible to create textures for formats
1112 * that are reported as supported. */
1113 if (WINED3DFMT_UNKNOWN >= desc->format)
1115 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1116 return WINED3DERR_INVALIDCALL;
1119 /* Non-power2 support. */
1120 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1122 pow2_width = desc->width;
1123 pow2_height = desc->height;
1125 else
1127 /* Find the nearest pow2 match. */
1128 pow2_width = pow2_height = 1;
1129 while (pow2_width < desc->width)
1130 pow2_width <<= 1;
1131 while (pow2_height < desc->height)
1132 pow2_height <<= 1;
1134 if (pow2_width != desc->width || pow2_height != desc->height)
1136 /* levels == 0 returns an error as well */
1137 if (levels != 1)
1139 if (desc->pool == WINED3D_POOL_SCRATCH)
1141 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1143 else
1145 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1146 return WINED3DERR_INVALIDCALL;
1152 /* Calculate levels for mip mapping. */
1153 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1155 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1157 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1158 return WINED3DERR_INVALIDCALL;
1161 if (levels != 1)
1163 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1164 return WINED3DERR_INVALIDCALL;
1168 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels, desc,
1169 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1171 WARN("Failed to initialize texture, returning %#x.\n", hr);
1172 return hr;
1175 /* Precalculated scaling for 'faked' non power of two texture coords. */
1176 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
1177 && (desc->width != pow2_width || desc->height != pow2_height))
1179 texture->pow2_matrix[0] = 1.0f;
1180 texture->pow2_matrix[5] = 1.0f;
1181 texture->pow2_matrix[10] = 1.0f;
1182 texture->pow2_matrix[15] = 1.0f;
1183 texture->target = GL_TEXTURE_2D;
1184 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1186 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
1187 && (desc->width != pow2_width || desc->height != pow2_height))
1189 texture->pow2_matrix[0] = (float)desc->width;
1190 texture->pow2_matrix[5] = (float)desc->height;
1191 texture->pow2_matrix[10] = 1.0f;
1192 texture->pow2_matrix[15] = 1.0f;
1193 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1194 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1195 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1197 else
1199 if ((desc->width != pow2_width) || (desc->height != pow2_height))
1201 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1202 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1203 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1205 else
1207 texture->pow2_matrix[0] = 1.0f;
1208 texture->pow2_matrix[5] = 1.0f;
1211 texture->pow2_matrix[10] = 1.0f;
1212 texture->pow2_matrix[15] = 1.0f;
1213 texture->target = GL_TEXTURE_2D;
1215 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1217 /* Generate all the surfaces. */
1218 surface_desc = *desc;
1219 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1220 for (i = 0; i < texture->level_count; ++i)
1222 struct wined3d_surface *surface;
1224 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1225 texture->target, i, 0, surface_flags, &surface)))
1227 WARN("Failed to create surface, hr %#x.\n", hr);
1228 wined3d_texture_cleanup(texture);
1229 return hr;
1232 texture->sub_resources[i] = &surface->resource;
1233 TRACE("Created surface level %u @ %p.\n", i, surface);
1234 /* Calculate the next mipmap level. */
1235 surface_desc.width = max(1, surface_desc.width >> 1);
1236 surface_desc.height = max(1, surface_desc.height >> 1);
1239 return WINED3D_OK;
1242 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1243 struct wined3d_context *context, BOOL srgb)
1245 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1248 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1249 const struct wined3d_box *dirty_region)
1251 wined3d_texture_set_dirty(volume_from_resource(sub_resource)->container);
1254 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1256 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1258 wined3d_volume_destroy(volume);
1261 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
1263 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1265 wined3d_volume_invalidate_location(volume, location);
1268 static void texture3d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
1270 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1272 wined3d_volume_validate_location(volume, location);
1275 static void texture3d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
1276 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1278 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1279 struct wined3d_const_bo_address addr;
1280 unsigned int row_pitch, slice_pitch;
1282 wined3d_volume_get_pitch(volume, &row_pitch, &slice_pitch);
1283 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1284 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1286 addr.buffer_object = 0;
1287 addr.addr = data->data;
1289 wined3d_volume_upload_data(volume, context, &addr);
1292 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1294 unsigned int sub_count = texture->level_count * texture->layer_count;
1295 const struct wined3d_format *format = texture->resource.format;
1296 const struct wined3d_gl_info *gl_info = context->gl_info;
1297 unsigned int i;
1299 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1301 for (i = 0; i < sub_count; ++i)
1303 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1304 void *mem = NULL;
1306 if (gl_info->supported[APPLE_CLIENT_STORAGE] && !format->convert
1307 && volume_prepare_system_memory(volume))
1309 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume);
1310 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1311 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1312 mem = volume->resource.heap_memory;
1313 volume->flags |= WINED3D_VFLAG_CLIENT_STORAGE;
1316 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
1317 srgb ? format->glGammaInternal : format->glInternal,
1318 volume->resource.width, volume->resource.height, volume->resource.depth,
1319 0, format->glFormat, format->glType, mem));
1320 checkGLcall("glTexImage3D");
1322 if (mem)
1324 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1325 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1330 static const struct wined3d_texture_ops texture3d_ops =
1332 texture3d_sub_resource_load,
1333 texture3d_sub_resource_add_dirty_region,
1334 texture3d_sub_resource_cleanup,
1335 texture3d_sub_resource_invalidate_location,
1336 texture3d_sub_resource_validate_location,
1337 texture3d_sub_resource_upload_data,
1338 texture3d_prepare_texture,
1341 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1342 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1344 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1345 struct wined3d_resource_desc volume_desc;
1346 unsigned int i;
1347 HRESULT hr;
1349 /* TODO: It should only be possible to create textures for formats
1350 * that are reported as supported. */
1351 if (WINED3DFMT_UNKNOWN >= desc->format)
1353 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1354 return WINED3DERR_INVALIDCALL;
1357 if (!gl_info->supported[EXT_TEXTURE3D])
1359 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1360 return WINED3DERR_INVALIDCALL;
1363 /* Calculate levels for mip mapping. */
1364 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1366 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1368 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1369 return WINED3DERR_INVALIDCALL;
1372 if (levels != 1)
1374 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1375 return WINED3DERR_INVALIDCALL;
1379 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1381 UINT pow2_w, pow2_h, pow2_d;
1382 pow2_w = 1;
1383 while (pow2_w < desc->width)
1384 pow2_w <<= 1;
1385 pow2_h = 1;
1386 while (pow2_h < desc->height)
1387 pow2_h <<= 1;
1388 pow2_d = 1;
1389 while (pow2_d < desc->depth)
1390 pow2_d <<= 1;
1392 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1394 if (desc->pool == WINED3D_POOL_SCRATCH)
1396 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1398 else
1400 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1401 desc->width, desc->height, desc->depth);
1402 return WINED3DERR_INVALIDCALL;
1407 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
1408 0, device, parent, parent_ops, &texture_resource_ops)))
1410 WARN("Failed to initialize texture, returning %#x.\n", hr);
1411 return hr;
1414 texture->pow2_matrix[0] = 1.0f;
1415 texture->pow2_matrix[5] = 1.0f;
1416 texture->pow2_matrix[10] = 1.0f;
1417 texture->pow2_matrix[15] = 1.0f;
1418 texture->target = GL_TEXTURE_3D;
1420 /* Generate all the surfaces. */
1421 volume_desc = *desc;
1422 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1423 for (i = 0; i < texture->level_count; ++i)
1425 struct wined3d_volume *volume;
1427 if (FAILED(hr = wined3d_volume_create(texture, &volume_desc, i, &volume)))
1429 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1430 wined3d_texture_cleanup(texture);
1431 return hr;
1434 texture->sub_resources[i] = &volume->resource;
1436 /* Calculate the next mipmap level. */
1437 volume_desc.width = max(1, volume_desc.width >> 1);
1438 volume_desc.height = max(1, volume_desc.height >> 1);
1439 volume_desc.depth = max(1, volume_desc.depth >> 1);
1442 return WINED3D_OK;
1445 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1446 UINT level_count, DWORD surface_flags, const struct wined3d_sub_resource_data *data, void *parent,
1447 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1449 struct wined3d_texture *object;
1450 HRESULT hr;
1452 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1453 device, desc, level_count, surface_flags, data, parent, parent_ops, texture);
1455 if (!level_count)
1457 WARN("Invalid level count.\n");
1458 return WINED3DERR_INVALIDCALL;
1461 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1462 return E_OUTOFMEMORY;
1464 switch (desc->resource_type)
1466 case WINED3D_RTYPE_TEXTURE:
1467 hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1468 break;
1470 case WINED3D_RTYPE_VOLUME_TEXTURE:
1471 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
1472 break;
1474 case WINED3D_RTYPE_CUBE_TEXTURE:
1475 hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1476 break;
1478 default:
1479 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
1480 hr = WINED3DERR_INVALIDCALL;
1481 break;
1484 if (FAILED(hr))
1486 WARN("Failed to initialize texture, returning %#x.\n", hr);
1487 HeapFree(GetProcessHeap(), 0, object);
1488 return hr;
1491 /* FIXME: We'd like to avoid ever allocating system memory for the texture
1492 * in this case. */
1493 if (data)
1494 wined3d_texture_upload_data(object, data);
1496 TRACE("Created texture %p.\n", object);
1497 *texture = object;
1499 return WINED3D_OK;