gdi32: Change an ERR to a WARN for fonts with too-long names.
[wine/multimedia.git] / dlls / wined3d / texture.c
blob57c8ef7726dfdc5509eeaa762a373b860fbcdc0b
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 & flag)
461 TRACE("Texture %p not dirty, nothing to do.\n", texture);
462 return;
465 /* Reload the surfaces if the texture is marked dirty. */
466 for (i = 0; i < sub_count; ++i)
468 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i], context, srgb);
470 texture->flags |= flag;
473 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
475 struct wined3d_context *context;
476 context = context_acquire(texture->resource.device, NULL);
477 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
478 context_release(context);
481 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
483 TRACE("texture %p.\n", texture);
485 return texture->resource.parent;
488 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
490 DWORD old = texture->lod;
492 TRACE("texture %p, lod %u.\n", texture, lod);
494 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
495 * textures. The call always returns 0, and GetLOD always returns 0. */
496 if (texture->resource.pool != WINED3D_POOL_MANAGED)
498 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
499 return 0;
502 if (lod >= texture->level_count)
503 lod = texture->level_count - 1;
505 if (texture->lod != lod)
507 texture->lod = lod;
509 texture->texture_rgb.base_level = ~0u;
510 texture->texture_srgb.base_level = ~0u;
511 if (texture->resource.bind_count)
512 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
515 return old;
518 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
520 TRACE("texture %p, returning %u.\n", texture, texture->lod);
522 return texture->lod;
525 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
527 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
529 return texture->level_count;
532 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
533 enum wined3d_texture_filter_type filter_type)
535 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
537 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
539 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
540 return WINED3DERR_INVALIDCALL;
543 texture->filter_type = filter_type;
545 return WINED3D_OK;
548 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
550 TRACE("texture %p.\n", texture);
552 return texture->filter_type;
555 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
556 DWORD flags, const struct wined3d_color_key *color_key)
558 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
560 if (flags & WINED3D_CKEY_COLORSPACE)
562 FIXME("Unhandled flags %#x.\n", flags);
563 return WINED3DERR_INVALIDCALL;
566 if (color_key)
568 switch (flags & ~WINED3D_CKEY_COLORSPACE)
570 case WINED3D_CKEY_DST_BLT:
571 texture->dst_blt_color_key = *color_key;
572 texture->color_key_flags |= WINED3D_CKEY_DST_BLT;
573 break;
575 case WINED3D_CKEY_DST_OVERLAY:
576 texture->dst_overlay_color_key = *color_key;
577 texture->color_key_flags |= WINED3D_CKEY_DST_OVERLAY;
578 break;
580 case WINED3D_CKEY_SRC_BLT:
581 texture->src_blt_color_key = *color_key;
582 texture->color_key_flags |= WINED3D_CKEY_SRC_BLT;
583 break;
585 case WINED3D_CKEY_SRC_OVERLAY:
586 texture->src_overlay_color_key = *color_key;
587 texture->color_key_flags |= WINED3D_CKEY_SRC_OVERLAY;
588 break;
591 else
593 switch (flags & ~WINED3D_CKEY_COLORSPACE)
595 case WINED3D_CKEY_DST_BLT:
596 texture->color_key_flags &= ~WINED3D_CKEY_DST_BLT;
597 break;
599 case WINED3D_CKEY_DST_OVERLAY:
600 texture->color_key_flags &= ~WINED3D_CKEY_DST_OVERLAY;
601 break;
603 case WINED3D_CKEY_SRC_BLT:
604 texture->color_key_flags &= ~WINED3D_CKEY_SRC_BLT;
605 break;
607 case WINED3D_CKEY_SRC_OVERLAY:
608 texture->color_key_flags &= ~WINED3D_CKEY_SRC_OVERLAY;
609 break;
613 return WINED3D_OK;
616 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
617 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
618 UINT multisample_quality, void *mem, UINT pitch)
620 struct wined3d_device *device = texture->resource.device;
621 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
622 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
623 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
624 struct wined3d_surface *surface;
626 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
627 "mem %p, pitch %u.\n",
628 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_type, mem, pitch);
630 if (!resource_size)
631 return WINED3DERR_INVALIDCALL;
633 if (texture->level_count * texture->layer_count > 1)
635 WARN("Texture has multiple sub-resources, not supported.\n");
636 return WINED3DERR_INVALIDCALL;
639 if (texture->resource.type == WINED3D_RTYPE_VOLUME_TEXTURE)
641 WARN("Not supported on volume textures.\n");
642 return WINED3DERR_INVALIDCALL;
645 surface = surface_from_resource(texture->sub_resources[0]);
646 if (surface->resource.map_count || (surface->flags & SFLAG_DCINUSE))
648 WARN("Surface is mapped or the DC is in use.\n");
649 return WINED3DERR_INVALIDCALL;
652 if (device->d3d_initialized)
653 texture->resource.resource_ops->resource_unload(&texture->resource);
655 texture->resource.format = format;
656 texture->resource.multisample_type = multisample_type;
657 texture->resource.multisample_quality = multisample_quality;
658 texture->resource.width = width;
659 texture->resource.height = height;
661 return wined3d_surface_update_desc(surface, gl_info, mem, pitch);
664 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
666 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
668 if (texture->flags & alloc_flag)
669 return;
671 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
672 texture->flags |= alloc_flag;
675 void wined3d_texture_force_reload(struct wined3d_texture *texture)
677 unsigned int sub_count = texture->level_count * texture->layer_count;
678 unsigned int i;
680 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED | WINED3D_TEXTURE_CONVERTED);
681 for (i = 0; i < sub_count; ++i)
683 texture->texture_ops->texture_sub_resource_invalidate_location(texture->sub_resources[i],
684 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
688 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
690 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
691 FIXME("texture %p stub!\n", texture);
694 struct wined3d_resource * CDECL wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
695 UINT sub_resource_idx)
697 UINT sub_count = texture->level_count * texture->layer_count;
699 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
701 if (sub_resource_idx >= sub_count)
703 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
704 return NULL;
707 return texture->sub_resources[sub_resource_idx];
710 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
711 UINT layer, const struct wined3d_box *dirty_region)
713 struct wined3d_resource *sub_resource;
715 TRACE("texture %p, layer %u, dirty_region %p.\n", texture, layer, dirty_region);
717 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, layer * texture->level_count)))
719 WARN("Failed to get sub-resource.\n");
720 return WINED3DERR_INVALIDCALL;
723 texture->texture_ops->texture_sub_resource_add_dirty_region(sub_resource, dirty_region);
725 return WINED3D_OK;
728 static void wined3d_texture_upload_data(struct wined3d_texture *texture, const struct wined3d_sub_resource_data *data)
730 unsigned int sub_count = texture->level_count * texture->layer_count;
731 struct wined3d_context *context;
732 unsigned int i;
734 context = context_acquire(texture->resource.device, NULL);
736 wined3d_texture_prepare_texture(texture, context, FALSE);
737 wined3d_texture_bind(texture, context, FALSE);
739 for (i = 0; i < sub_count; ++i)
741 struct wined3d_resource *sub_resource = texture->sub_resources[i];
743 texture->texture_ops->texture_sub_resource_upload_data(sub_resource, context, &data[i]);
744 texture->texture_ops->texture_sub_resource_validate_location(sub_resource, WINED3D_LOCATION_TEXTURE_RGB);
745 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource, ~WINED3D_LOCATION_TEXTURE_RGB);
748 context_release(context);
751 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
752 struct wined3d_context *context, BOOL srgb)
754 surface_load(surface_from_resource(sub_resource), srgb);
757 static void texture2d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
758 const struct wined3d_box *dirty_region)
760 struct wined3d_surface *surface = surface_from_resource(sub_resource);
762 surface_prepare_map_memory(surface);
763 surface_load_location(surface, surface->resource.map_binding);
764 surface_invalidate_location(surface, ~surface->resource.map_binding);
767 static void texture2d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
769 struct wined3d_surface *surface = surface_from_resource(sub_resource);
771 wined3d_surface_destroy(surface);
774 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
776 struct wined3d_surface *surface = surface_from_resource(sub_resource);
778 surface_invalidate_location(surface, location);
781 static void texture2d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
783 struct wined3d_surface *surface = surface_from_resource(sub_resource);
785 surface_validate_location(surface, location);
788 static void texture2d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
789 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
791 struct wined3d_surface *surface = surface_from_resource(sub_resource);
792 static const POINT dst_point = {0, 0};
793 struct wined3d_const_bo_address addr;
794 RECT src_rect;
796 src_rect.left = 0;
797 src_rect.top = 0;
798 src_rect.right = surface->resource.width;
799 src_rect.bottom = surface->resource.height;
801 addr.buffer_object = 0;
802 addr.addr = data->data;
804 wined3d_surface_upload_data(surface, context->gl_info, surface->container->resource.format,
805 &src_rect, data->row_pitch, &dst_point, FALSE, &addr);
808 /* Context activation is done by the caller. */
809 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
811 UINT sub_count = texture->level_count * texture->layer_count;
812 const struct wined3d_format *format = texture->resource.format;
813 const struct wined3d_gl_info *gl_info = context->gl_info;
814 const struct wined3d_color_key_conversion *conversion;
815 GLenum internal;
816 UINT i;
818 TRACE("texture %p, format %s.\n", texture, debug_d3dformat(format->id));
820 if (format->convert)
822 texture->flags |= WINED3D_TEXTURE_CONVERTED;
824 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
826 texture->flags |= WINED3D_TEXTURE_CONVERTED;
827 format = wined3d_get_format(gl_info, conversion->dst_format);
828 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
831 wined3d_texture_bind_and_dirtify(texture, context, srgb);
833 if (srgb)
834 internal = format->glGammaInternal;
835 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
836 && wined3d_resource_is_offscreen(&texture->resource))
837 internal = format->rtInternal;
838 else
839 internal = format->glInternal;
841 if (!internal)
842 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
844 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
846 for (i = 0; i < sub_count; ++i)
848 struct wined3d_surface *surface = surface_from_resource(texture->sub_resources[i]);
849 GLsizei height = surface->pow2Height;
850 GLsizei width = surface->pow2Width;
851 const BYTE *mem = NULL;
853 if (format->flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
855 height *= format->height_scale.numerator;
856 height /= format->height_scale.denominator;
859 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
860 surface, surface->texture_target, surface->texture_level, width, height);
862 if (gl_info->supported[APPLE_CLIENT_STORAGE])
864 if (surface->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION)
865 || texture->flags & WINED3D_TEXTURE_CONVERTED
866 || !surface->resource.heap_memory)
868 /* In some cases we want to disable client storage.
869 * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
870 * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
871 * WINED3D_TEXTURE_CONVERTED: The conversion destination memory is freed after loading the surface
872 * heap_memory == NULL: Not defined in the extension. Seems to disable client storage effectively
874 surface->flags &= ~SFLAG_CLIENT;
876 else
878 surface->flags |= SFLAG_CLIENT;
879 mem = surface->resource.heap_memory;
881 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
882 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
886 if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
888 GL_EXTCALL(glCompressedTexImage2D(surface->texture_target, surface->texture_level,
889 internal, width, height, 0, surface->resource.size, mem));
890 checkGLcall("glCompressedTexImage2D");
892 else
894 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
895 internal, width, height, 0, format->glFormat, format->glType, mem);
896 checkGLcall("glTexImage2D");
899 if (mem)
901 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
902 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
907 static const struct wined3d_texture_ops texture2d_ops =
909 texture2d_sub_resource_load,
910 texture2d_sub_resource_add_dirty_region,
911 texture2d_sub_resource_cleanup,
912 texture2d_sub_resource_invalidate_location,
913 texture2d_sub_resource_validate_location,
914 texture2d_sub_resource_upload_data,
915 texture2d_prepare_texture,
918 static ULONG texture_resource_incref(struct wined3d_resource *resource)
920 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
923 static ULONG texture_resource_decref(struct wined3d_resource *resource)
925 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
928 static void wined3d_texture_unload(struct wined3d_resource *resource)
930 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
931 UINT sub_count = texture->level_count * texture->layer_count;
932 UINT i;
934 TRACE("texture %p.\n", texture);
936 for (i = 0; i < sub_count; ++i)
938 struct wined3d_resource *sub_resource = texture->sub_resources[i];
940 sub_resource->resource_ops->resource_unload(sub_resource);
943 wined3d_texture_force_reload(texture);
944 wined3d_texture_unload_gl_texture(texture);
947 static const struct wined3d_resource_ops texture_resource_ops =
949 texture_resource_incref,
950 texture_resource_decref,
951 wined3d_texture_unload,
954 static HRESULT cubetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
955 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
956 const struct wined3d_parent_ops *parent_ops)
958 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
959 struct wined3d_resource_desc surface_desc;
960 unsigned int i, j;
961 HRESULT hr;
963 /* TODO: It should only be possible to create textures for formats
964 * that are reported as supported. */
965 if (WINED3DFMT_UNKNOWN >= desc->format)
967 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
968 return WINED3DERR_INVALIDCALL;
971 if (!gl_info->supported[ARB_TEXTURE_CUBE_MAP] && desc->pool != WINED3D_POOL_SCRATCH)
973 WARN("(%p) : Tried to create not supported cube texture.\n", texture);
974 return WINED3DERR_INVALIDCALL;
977 /* Calculate levels for mip mapping */
978 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
980 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
982 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
983 return WINED3DERR_INVALIDCALL;
986 if (levels != 1)
988 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
989 return WINED3DERR_INVALIDCALL;
993 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
995 UINT pow2_edge_length = 1;
996 while (pow2_edge_length < desc->width)
997 pow2_edge_length <<= 1;
999 if (desc->width != pow2_edge_length)
1001 if (desc->pool == WINED3D_POOL_SCRATCH)
1003 /* SCRATCH textures cannot be used for texturing */
1004 WARN("Creating a scratch NPOT cube texture despite lack of HW support.\n");
1006 else
1008 WARN("Attempted to create a NPOT cube texture (edge length %u) without GL support.\n", desc->width);
1009 return WINED3DERR_INVALIDCALL;
1014 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 6, levels, desc,
1015 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1017 WARN("Failed to initialize texture, returning %#x\n", hr);
1018 return hr;
1021 texture->pow2_matrix[0] = 1.0f;
1022 texture->pow2_matrix[5] = 1.0f;
1023 texture->pow2_matrix[10] = 1.0f;
1024 texture->pow2_matrix[15] = 1.0f;
1025 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1027 /* Generate all the surfaces. */
1028 surface_desc = *desc;
1029 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1030 for (i = 0; i < texture->level_count; ++i)
1032 /* Create the 6 faces. */
1033 for (j = 0; j < 6; ++j)
1035 static const GLenum cube_targets[6] =
1037 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1038 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1039 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1040 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1041 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1042 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1044 UINT idx = j * texture->level_count + i;
1045 struct wined3d_surface *surface;
1047 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1048 cube_targets[j], i, j, surface_flags, &surface)))
1050 WARN("Failed to create surface, hr %#x.\n", hr);
1051 wined3d_texture_cleanup(texture);
1052 return hr;
1055 texture->sub_resources[idx] = &surface->resource;
1056 TRACE("Created surface level %u @ %p.\n", i, surface);
1058 surface_desc.width = max(1, surface_desc.width >> 1);
1059 surface_desc.height = surface_desc.width;
1062 return WINED3D_OK;
1065 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1066 UINT levels, DWORD surface_flags, struct wined3d_device *device, void *parent,
1067 const struct wined3d_parent_ops *parent_ops)
1069 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1070 struct wined3d_resource_desc surface_desc;
1071 UINT pow2_width, pow2_height;
1072 unsigned int i;
1073 HRESULT hr;
1075 /* TODO: It should only be possible to create textures for formats
1076 * that are reported as supported. */
1077 if (WINED3DFMT_UNKNOWN >= desc->format)
1079 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1080 return WINED3DERR_INVALIDCALL;
1083 /* Non-power2 support. */
1084 if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1086 pow2_width = desc->width;
1087 pow2_height = desc->height;
1089 else
1091 /* Find the nearest pow2 match. */
1092 pow2_width = pow2_height = 1;
1093 while (pow2_width < desc->width)
1094 pow2_width <<= 1;
1095 while (pow2_height < desc->height)
1096 pow2_height <<= 1;
1098 if (pow2_width != desc->width || pow2_height != desc->height)
1100 /* levels == 0 returns an error as well */
1101 if (levels != 1)
1103 if (desc->pool == WINED3D_POOL_SCRATCH)
1105 WARN("Creating a scratch mipmapped NPOT texture despite lack of HW support.\n");
1107 else
1109 WARN("Attempted to create a mipmapped NPOT texture without unconditional NPOT support.\n");
1110 return WINED3DERR_INVALIDCALL;
1116 /* Calculate levels for mip mapping. */
1117 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1119 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1121 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1122 return WINED3DERR_INVALIDCALL;
1125 if (levels != 1)
1127 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1128 return WINED3DERR_INVALIDCALL;
1132 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, 1, levels, desc,
1133 surface_flags, device, parent, parent_ops, &texture_resource_ops)))
1135 WARN("Failed to initialize texture, returning %#x.\n", hr);
1136 return hr;
1139 /* Precalculated scaling for 'faked' non power of two texture coords. */
1140 if (gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT]
1141 && (desc->width != pow2_width || desc->height != pow2_height))
1143 texture->pow2_matrix[0] = 1.0f;
1144 texture->pow2_matrix[5] = 1.0f;
1145 texture->pow2_matrix[10] = 1.0f;
1146 texture->pow2_matrix[15] = 1.0f;
1147 texture->target = GL_TEXTURE_2D;
1148 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1150 else if (gl_info->supported[ARB_TEXTURE_RECTANGLE]
1151 && (desc->width != pow2_width || desc->height != pow2_height))
1153 texture->pow2_matrix[0] = (float)desc->width;
1154 texture->pow2_matrix[5] = (float)desc->height;
1155 texture->pow2_matrix[10] = 1.0f;
1156 texture->pow2_matrix[15] = 1.0f;
1157 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1158 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1159 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1161 else
1163 if ((desc->width != pow2_width) || (desc->height != pow2_height))
1165 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1166 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1167 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1169 else
1171 texture->pow2_matrix[0] = 1.0f;
1172 texture->pow2_matrix[5] = 1.0f;
1175 texture->pow2_matrix[10] = 1.0f;
1176 texture->pow2_matrix[15] = 1.0f;
1177 texture->target = GL_TEXTURE_2D;
1179 TRACE("xf(%f) yf(%f)\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1181 /* Generate all the surfaces. */
1182 surface_desc = *desc;
1183 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1184 for (i = 0; i < texture->level_count; ++i)
1186 struct wined3d_surface *surface;
1188 if (FAILED(hr = wined3d_surface_create(texture, &surface_desc,
1189 texture->target, i, 0, surface_flags, &surface)))
1191 WARN("Failed to create surface, hr %#x.\n", hr);
1192 wined3d_texture_cleanup(texture);
1193 return hr;
1196 texture->sub_resources[i] = &surface->resource;
1197 TRACE("Created surface level %u @ %p.\n", i, surface);
1198 /* Calculate the next mipmap level. */
1199 surface_desc.width = max(1, surface_desc.width >> 1);
1200 surface_desc.height = max(1, surface_desc.height >> 1);
1203 return WINED3D_OK;
1206 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1207 struct wined3d_context *context, BOOL srgb)
1209 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1212 static void texture3d_sub_resource_add_dirty_region(struct wined3d_resource *sub_resource,
1213 const struct wined3d_box *dirty_region)
1215 wined3d_texture_set_dirty(volume_from_resource(sub_resource)->container);
1218 static void texture3d_sub_resource_cleanup(struct wined3d_resource *sub_resource)
1220 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1222 wined3d_volume_destroy(volume);
1225 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
1227 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1229 wined3d_volume_invalidate_location(volume, location);
1232 static void texture3d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
1234 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1236 wined3d_volume_validate_location(volume, location);
1239 static void texture3d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
1240 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1242 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1243 struct wined3d_const_bo_address addr;
1244 unsigned int row_pitch, slice_pitch;
1246 wined3d_volume_get_pitch(volume, &row_pitch, &slice_pitch);
1247 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1248 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1250 addr.buffer_object = 0;
1251 addr.addr = data->data;
1253 wined3d_volume_upload_data(volume, context, &addr);
1256 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1258 unsigned int sub_count = texture->level_count * texture->layer_count;
1259 const struct wined3d_format *format = texture->resource.format;
1260 const struct wined3d_gl_info *gl_info = context->gl_info;
1261 unsigned int i;
1263 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1265 for (i = 0; i < sub_count; ++i)
1267 struct wined3d_volume *volume = volume_from_resource(texture->sub_resources[i]);
1268 void *mem = NULL;
1270 if (gl_info->supported[APPLE_CLIENT_STORAGE] && !format->convert
1271 && volume_prepare_system_memory(volume))
1273 TRACE("Enabling GL_UNPACK_CLIENT_STORAGE_APPLE for volume %p\n", volume);
1274 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1275 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1276 mem = volume->resource.heap_memory;
1277 volume->flags |= WINED3D_VFLAG_CLIENT_STORAGE;
1280 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
1281 srgb ? format->glGammaInternal : format->glInternal,
1282 volume->resource.width, volume->resource.height, volume->resource.depth,
1283 0, format->glFormat, format->glType, mem));
1284 checkGLcall("glTexImage3D");
1286 if (mem)
1288 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1289 checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1294 static const struct wined3d_texture_ops texture3d_ops =
1296 texture3d_sub_resource_load,
1297 texture3d_sub_resource_add_dirty_region,
1298 texture3d_sub_resource_cleanup,
1299 texture3d_sub_resource_invalidate_location,
1300 texture3d_sub_resource_validate_location,
1301 texture3d_sub_resource_upload_data,
1302 texture3d_prepare_texture,
1305 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1306 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1308 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1309 struct wined3d_resource_desc volume_desc;
1310 unsigned int i;
1311 HRESULT hr;
1313 /* TODO: It should only be possible to create textures for formats
1314 * that are reported as supported. */
1315 if (WINED3DFMT_UNKNOWN >= desc->format)
1317 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1318 return WINED3DERR_INVALIDCALL;
1321 if (!gl_info->supported[EXT_TEXTURE3D])
1323 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1324 return WINED3DERR_INVALIDCALL;
1327 /* Calculate levels for mip mapping. */
1328 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1330 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1332 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1333 return WINED3DERR_INVALIDCALL;
1336 if (levels != 1)
1338 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1339 return WINED3DERR_INVALIDCALL;
1343 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1345 UINT pow2_w, pow2_h, pow2_d;
1346 pow2_w = 1;
1347 while (pow2_w < desc->width)
1348 pow2_w <<= 1;
1349 pow2_h = 1;
1350 while (pow2_h < desc->height)
1351 pow2_h <<= 1;
1352 pow2_d = 1;
1353 while (pow2_d < desc->depth)
1354 pow2_d <<= 1;
1356 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1358 if (desc->pool == WINED3D_POOL_SCRATCH)
1360 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1362 else
1364 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1365 desc->width, desc->height, desc->depth);
1366 return WINED3DERR_INVALIDCALL;
1371 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
1372 0, device, parent, parent_ops, &texture_resource_ops)))
1374 WARN("Failed to initialize texture, returning %#x.\n", hr);
1375 return hr;
1378 texture->pow2_matrix[0] = 1.0f;
1379 texture->pow2_matrix[5] = 1.0f;
1380 texture->pow2_matrix[10] = 1.0f;
1381 texture->pow2_matrix[15] = 1.0f;
1382 texture->target = GL_TEXTURE_3D;
1384 /* Generate all the surfaces. */
1385 volume_desc = *desc;
1386 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1387 for (i = 0; i < texture->level_count; ++i)
1389 struct wined3d_volume *volume;
1391 if (FAILED(hr = wined3d_volume_create(texture, &volume_desc, i, &volume)))
1393 ERR("Creating a volume for the volume texture failed, hr %#x.\n", hr);
1394 wined3d_texture_cleanup(texture);
1395 return hr;
1398 texture->sub_resources[i] = &volume->resource;
1400 /* Calculate the next mipmap level. */
1401 volume_desc.width = max(1, volume_desc.width >> 1);
1402 volume_desc.height = max(1, volume_desc.height >> 1);
1403 volume_desc.depth = max(1, volume_desc.depth >> 1);
1406 return WINED3D_OK;
1409 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
1410 UINT level_count, DWORD surface_flags, const struct wined3d_sub_resource_data *data, void *parent,
1411 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
1413 struct wined3d_texture *object;
1414 HRESULT hr;
1416 TRACE("device %p, desc %p, level_count %u, surface_flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
1417 device, desc, level_count, surface_flags, data, parent, parent_ops, texture);
1419 if (!level_count)
1421 WARN("Invalid level count.\n");
1422 return WINED3DERR_INVALIDCALL;
1425 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
1426 return E_OUTOFMEMORY;
1428 switch (desc->resource_type)
1430 case WINED3D_RTYPE_TEXTURE:
1431 hr = texture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1432 break;
1434 case WINED3D_RTYPE_VOLUME_TEXTURE:
1435 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
1436 break;
1438 case WINED3D_RTYPE_CUBE_TEXTURE:
1439 hr = cubetexture_init(object, desc, level_count, surface_flags, device, parent, parent_ops);
1440 break;
1442 default:
1443 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
1444 hr = WINED3DERR_INVALIDCALL;
1445 break;
1448 if (FAILED(hr))
1450 WARN("Failed to initialize texture, returning %#x.\n", hr);
1451 HeapFree(GetProcessHeap(), 0, object);
1452 return hr;
1455 /* FIXME: We'd like to avoid ever allocating system memory for the texture
1456 * in this case. */
1457 if (data)
1458 wined3d_texture_upload_data(object, data);
1460 TRACE("Created texture %p.\n", object);
1461 *texture = object;
1463 return WINED3D_OK;