makefiles: Always expand the library name for cross-compiled import libraries.
[wine.git] / dlls / wined3d / texture.c
blobe163e83f607182294a36271023e8dc12094f02ad
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(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
33 return texture->resource.pool == WINED3D_POOL_DEFAULT
34 && texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
35 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
36 && !texture->resource.format->convert
37 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
40 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
42 const struct wined3d_swapchain *swapchain = texture->swapchain;
44 TRACE("texture %p.\n", texture);
46 if (!swapchain)
48 ERR("Texture %p is not part of a swapchain.\n", texture);
49 return GL_NONE;
52 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
54 if (swapchain->render_to_fbo)
56 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
57 return GL_COLOR_ATTACHMENT0;
59 TRACE("Returning GL_BACK.\n");
60 return GL_BACK;
62 else if (texture == swapchain->front_buffer)
64 TRACE("Returning GL_FRONT.\n");
65 return GL_FRONT;
68 FIXME("Higher back buffer, returning GL_BACK.\n");
69 return GL_BACK;
72 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
73 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
74 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
75 const struct wined3d_resource_ops *resource_ops)
77 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
78 HRESULT hr;
80 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
81 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
82 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
83 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
84 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
85 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
86 flags, device, parent, parent_ops, resource_ops);
88 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
89 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
90 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
92 static unsigned int once;
94 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
95 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
96 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
97 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
98 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
99 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
101 WARN("Failed to initialize resource, returning %#x\n", hr);
102 return hr;
104 wined3d_resource_update_draw_binding(&texture->resource);
105 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
106 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
108 texture->texture_ops = texture_ops;
110 texture->layer_count = layer_count;
111 texture->level_count = level_count;
112 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
113 texture->lod = 0;
114 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
115 if (flags & WINED3D_TEXTURE_CREATE_PIN_SYSMEM)
116 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
118 return WINED3D_OK;
121 /* Context activation is done by the caller. */
122 void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
123 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
125 GLuint *buffer_object;
127 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
128 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
129 checkGLcall("glDeleteBuffers");
130 texture->texture_ops->texture_sub_resource_invalidate_location(
131 texture->sub_resources[sub_resource_idx].resource, WINED3D_LOCATION_BUFFER);
132 *buffer_object = 0;
134 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
135 *buffer_object, texture, sub_resource_idx);
138 /* A GL context is provided by the caller */
139 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
140 struct gl_texture *tex)
142 context_gl_resource_released(device, tex->name, FALSE);
143 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
144 tex->name = 0;
147 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
149 struct wined3d_device *device = texture->resource.device;
150 struct wined3d_context *context = NULL;
152 if (texture->texture_rgb.name || texture->texture_srgb.name)
154 context = context_acquire(device, NULL);
157 if (texture->texture_rgb.name)
158 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
160 if (texture->texture_srgb.name)
161 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
163 if (context) context_release(context);
165 wined3d_texture_set_dirty(texture);
167 resource_unload(&texture->resource);
170 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
172 unsigned int sub_count = texture->level_count * texture->layer_count;
173 struct wined3d_device *device = texture->resource.device;
174 struct wined3d_context *context = NULL;
175 const struct wined3d_gl_info *gl_info;
176 GLuint buffer_object;
177 unsigned int i;
179 TRACE("texture %p.\n", texture);
181 for (i = 0; i < sub_count; ++i)
183 if (!(buffer_object = texture->sub_resources[i].buffer_object))
184 continue;
186 TRACE("Deleting buffer object %u.\n", buffer_object);
188 /* We may not be able to get a context in wined3d_texture_cleanup() in
189 * general, but if a buffer object was previously created we can. */
190 if (!context)
192 context = context_acquire(device, NULL);
193 gl_info = context->gl_info;
196 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
198 if (context)
199 context_release(context);
201 texture->texture_ops->texture_cleanup_sub_resources(texture);
202 wined3d_texture_unload_gl_texture(texture);
203 resource_cleanup(&texture->resource);
206 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
208 texture->swapchain = swapchain;
209 wined3d_resource_update_draw_binding(&texture->resource);
212 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
214 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
217 /* Context activation is done by the caller. */
218 void wined3d_texture_bind(struct wined3d_texture *texture,
219 struct wined3d_context *context, BOOL srgb)
221 const struct wined3d_gl_info *gl_info = context->gl_info;
222 const struct wined3d_format *format = texture->resource.format;
223 const struct color_fixup_desc fixup = format->color_fixup;
224 struct gl_texture *gl_tex;
225 GLenum target;
227 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
229 if (!needs_separate_srgb_gl_texture(context))
230 srgb = FALSE;
232 /* sRGB mode cache for preload() calls outside drawprim. */
233 if (srgb)
234 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
235 else
236 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
238 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
239 target = texture->target;
241 if (gl_tex->name)
243 context_bind_texture(context, target, gl_tex->name);
244 return;
247 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
248 checkGLcall("glGenTextures");
249 TRACE("Generated texture %d.\n", gl_tex->name);
251 if (!gl_tex->name)
253 ERR("Failed to generate a texture name.\n");
254 return;
257 /* Initialise the state of the texture object to the OpenGL defaults, not
258 * the wined3d defaults. */
259 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
260 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
261 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
262 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
263 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
264 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
265 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
266 gl_tex->sampler_desc.lod_bias = 0.0f;
267 gl_tex->sampler_desc.min_lod = -1000.0f;
268 gl_tex->sampler_desc.max_lod = 1000.0f;
269 gl_tex->sampler_desc.max_anisotropy = 1;
270 gl_tex->sampler_desc.compare = FALSE;
271 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
272 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
273 gl_tex->sampler_desc.srgb_decode = TRUE;
274 else
275 gl_tex->sampler_desc.srgb_decode = srgb;
276 gl_tex->base_level = 0;
277 wined3d_texture_set_dirty(texture);
279 context_bind_texture(context, target, gl_tex->name);
281 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
283 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
284 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
287 /* For a new texture we have to set the texture levels after binding the
288 * texture. Beware that texture rectangles do not support mipmapping, but
289 * set the maxmiplevel if we're relying on the partial
290 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
291 * (I.e., do not care about cond_np2 here, just look for
292 * GL_TEXTURE_RECTANGLE_ARB.) */
293 if (target != GL_TEXTURE_RECTANGLE_ARB)
295 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
296 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
297 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
300 if (target == GL_TEXTURE_CUBE_MAP_ARB)
302 /* Cubemaps are always set to clamp, regardless of the sampler state. */
303 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
304 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
305 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
308 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
310 /* Conditinal non power of two textures use a different clamping
311 * default. If we're using the GL_WINE_normalized_texrect partial
312 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
313 * has the address mode set to repeat - something that prevents us
314 * from hitting the accelerated codepath. Thus manually set the GL
315 * state. The same applies to filtering. Even if the texture has only
316 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
317 * fallback on macos. */
318 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
319 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
320 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
321 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
322 checkGLcall("glTexParameteri");
323 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
324 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
325 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
326 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
327 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
330 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
332 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
333 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
336 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
338 static const GLenum swizzle_source[] =
340 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
341 GL_ONE, /* CHANNEL_SOURCE_ONE */
342 GL_RED, /* CHANNEL_SOURCE_X */
343 GL_GREEN, /* CHANNEL_SOURCE_Y */
344 GL_BLUE, /* CHANNEL_SOURCE_Z */
345 GL_ALPHA, /* CHANNEL_SOURCE_W */
347 struct
349 GLint x, y, z, w;
351 swizzle;
353 swizzle.x = swizzle_source[fixup.x_source];
354 swizzle.y = swizzle_source[fixup.y_source];
355 swizzle.z = swizzle_source[fixup.z_source];
356 swizzle.w = swizzle_source[fixup.w_source];
357 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
358 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
362 /* Context activation is done by the caller. */
363 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
364 struct wined3d_context *context, BOOL srgb)
366 DWORD active_sampler;
368 /* We don't need a specific texture unit, but after binding the texture
369 * the current unit is dirty. Read the unit back instead of switching to
370 * 0, this avoids messing around with the state manager's GL states. The
371 * current texture unit should always be a valid one.
373 * To be more specific, this is tricky because we can implicitly be
374 * called from sampler() in state.c. This means we can't touch anything
375 * other than whatever happens to be the currently active texture, or we
376 * would risk marking already applied sampler states dirty again. */
377 active_sampler = context->rev_tex_unit_map[context->active_texture];
378 if (active_sampler != WINED3D_UNMAPPED_STAGE)
379 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
380 /* FIXME: Ideally we'd only do this when touching a binding that's used by
381 * a shader. */
382 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
384 wined3d_texture_bind(texture, context, srgb);
387 /* Context activation is done by the caller (state handler). */
388 /* This function relies on the correct texture being bound and loaded. */
389 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
390 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
392 const struct wined3d_gl_info *gl_info = context->gl_info;
393 GLenum target = texture->target;
394 struct gl_texture *gl_tex;
395 DWORD state;
397 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
399 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
401 state = sampler_desc->address_u;
402 if (state != gl_tex->sampler_desc.address_u)
404 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
405 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
406 gl_tex->sampler_desc.address_u = state;
409 state = sampler_desc->address_v;
410 if (state != gl_tex->sampler_desc.address_v)
412 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
413 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
414 gl_tex->sampler_desc.address_v = state;
417 state = sampler_desc->address_w;
418 if (state != gl_tex->sampler_desc.address_w)
420 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
421 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
422 gl_tex->sampler_desc.address_w = state;
425 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
426 sizeof(gl_tex->sampler_desc.border_color)))
428 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
429 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
430 sizeof(gl_tex->sampler_desc.border_color));
433 state = sampler_desc->mag_filter;
434 if (state != gl_tex->sampler_desc.mag_filter)
436 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
437 gl_tex->sampler_desc.mag_filter = state;
440 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
441 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
443 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
444 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
445 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
446 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
449 state = sampler_desc->max_anisotropy;
450 if (state != gl_tex->sampler_desc.max_anisotropy)
452 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
453 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
454 else
455 WARN("Anisotropic filtering not supported.\n");
456 gl_tex->sampler_desc.max_anisotropy = state;
459 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
460 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
461 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
463 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
464 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
465 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
468 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
470 if (sampler_desc->compare)
471 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
472 else
473 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
474 gl_tex->sampler_desc.compare = sampler_desc->compare;
477 checkGLcall("Texture parameter application");
479 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
481 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
482 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
483 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
487 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
489 ULONG refcount;
491 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
493 if (texture->swapchain)
494 return wined3d_swapchain_incref(texture->swapchain);
496 refcount = InterlockedIncrement(&texture->resource.ref);
497 TRACE("%p increasing refcount to %u.\n", texture, refcount);
499 return refcount;
502 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
504 ULONG refcount;
506 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
508 if (texture->swapchain)
509 return wined3d_swapchain_decref(texture->swapchain);
511 refcount = InterlockedDecrement(&texture->resource.ref);
512 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
514 if (!refcount)
516 wined3d_texture_cleanup(texture);
517 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
518 HeapFree(GetProcessHeap(), 0, texture);
521 return refcount;
524 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
526 TRACE("texture %p.\n", texture);
528 return &texture->resource;
531 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
533 return c1->color_space_low_value == c2->color_space_low_value
534 && c1->color_space_high_value == c2->color_space_high_value;
537 /* Context activation is done by the caller */
538 void wined3d_texture_load(struct wined3d_texture *texture,
539 struct wined3d_context *context, BOOL srgb)
541 UINT sub_count = texture->level_count * texture->layer_count;
542 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
543 DWORD flag;
544 UINT i;
546 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
548 if (!needs_separate_srgb_gl_texture(context))
549 srgb = FALSE;
551 if (srgb)
552 flag = WINED3D_TEXTURE_SRGB_VALID;
553 else
554 flag = WINED3D_TEXTURE_RGB_VALID;
556 if (!d3d_info->shader_color_key
557 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
558 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
559 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
560 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
562 unsigned int sub_count = texture->level_count * texture->layer_count;
563 unsigned int i;
565 TRACE("Reloading because of color key value change.\n");
566 for (i = 0; i < sub_count; i++)
568 struct wined3d_resource *sub_resource = texture->sub_resources[i].resource;
570 if (!texture->texture_ops->texture_load_location(texture, i, context, sub_resource->map_binding))
571 ERR("Failed to load location %s.\n", wined3d_debug_location(sub_resource->map_binding));
572 else
573 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource,
574 ~sub_resource->map_binding);
577 texture->async.gl_color_key = texture->async.src_blt_color_key;
580 if (texture->flags & flag)
582 TRACE("Texture %p not dirty, nothing to do.\n", texture);
583 return;
586 /* Reload the surfaces if the texture is marked dirty. */
587 for (i = 0; i < sub_count; ++i)
589 texture->texture_ops->texture_sub_resource_load(texture->sub_resources[i].resource, context, srgb);
591 texture->flags |= flag;
594 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
596 struct wined3d_context *context;
597 context = context_acquire(texture->resource.device, NULL);
598 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
599 context_release(context);
602 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
604 TRACE("texture %p.\n", texture);
606 return texture->resource.parent;
609 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
610 unsigned int level, const struct wined3d_box *box)
612 if (box->left >= box->right
613 || box->top >= box->bottom
614 || box->front >= box->back)
615 return FALSE;
617 if (box->right > wined3d_texture_get_level_width(texture, level)
618 || box->bottom > wined3d_texture_get_level_height(texture, level)
619 || box->back > wined3d_texture_get_level_depth(texture, level))
620 return FALSE;
622 return TRUE;
625 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
626 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
628 const struct wined3d_resource *resource = &texture->resource;
629 unsigned int width = wined3d_texture_get_level_width(texture, level);
630 unsigned int height = wined3d_texture_get_level_height(texture, level);
632 if (texture->row_pitch)
634 *row_pitch = texture->row_pitch;
635 *slice_pitch = texture->slice_pitch;
636 return;
639 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
640 width, height, row_pitch, slice_pitch);
643 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
645 DWORD old = texture->lod;
647 TRACE("texture %p, lod %u.\n", texture, lod);
649 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
650 * textures. The call always returns 0, and GetLOD always returns 0. */
651 if (texture->resource.pool != WINED3D_POOL_MANAGED)
653 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
654 return 0;
657 if (lod >= texture->level_count)
658 lod = texture->level_count - 1;
660 if (texture->lod != lod)
662 texture->lod = lod;
664 texture->texture_rgb.base_level = ~0u;
665 texture->texture_srgb.base_level = ~0u;
666 if (texture->resource.bind_count)
667 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
670 return old;
673 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
675 TRACE("texture %p, returning %u.\n", texture, texture->lod);
677 return texture->lod;
680 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
682 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
684 return texture->level_count;
687 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
688 enum wined3d_texture_filter_type filter_type)
690 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
692 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
694 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
695 return WINED3DERR_INVALIDCALL;
698 texture->filter_type = filter_type;
700 return WINED3D_OK;
703 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
705 TRACE("texture %p.\n", texture);
707 return texture->filter_type;
710 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
711 DWORD flags, const struct wined3d_color_key *color_key)
713 struct wined3d_device *device = texture->resource.device;
714 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
715 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
717 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
719 if (flags & ~all_flags)
721 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
722 return WINED3DERR_INVALIDCALL;
725 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
727 return WINED3D_OK;
730 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
731 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
732 UINT multisample_quality, void *mem, UINT pitch)
734 struct wined3d_device *device = texture->resource.device;
735 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
736 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
737 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
738 struct wined3d_surface *surface;
739 DWORD valid_location = 0;
740 BOOL create_dib = FALSE;
742 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
743 "mem %p, pitch %u.\n",
744 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
746 if (!resource_size)
747 return WINED3DERR_INVALIDCALL;
749 if (texture->level_count * texture->layer_count > 1)
751 WARN("Texture has multiple sub-resources, not supported.\n");
752 return WINED3DERR_INVALIDCALL;
755 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
757 WARN("Not supported on 3D textures.\n");
758 return WINED3DERR_INVALIDCALL;
761 /* We have no way of supporting a pitch that is not a multiple of the pixel
762 * byte width short of uploading the texture row-by-row.
763 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
764 * for user-memory textures (it always expects packed data) while DirectDraw
765 * requires a 4-byte aligned pitch and doesn't support texture formats
766 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
767 * This check is here to verify that the assumption holds. */
768 if (pitch % texture->resource.format->byte_count)
770 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
771 return WINED3DERR_INVALIDCALL;
774 surface = texture->sub_resources[0].u.surface;
775 if (surface->resource.map_count || (surface->flags & SFLAG_DCINUSE))
777 WARN("Surface is mapped or the DC is in use.\n");
778 return WINED3DERR_INVALIDCALL;
781 if (device->d3d_initialized)
782 texture->resource.resource_ops->resource_unload(&texture->resource);
784 if (surface->flags & SFLAG_DIBSECTION)
786 DeleteDC(surface->hDC);
787 surface->hDC = NULL;
788 DeleteObject(surface->dib.DIBsection);
789 surface->dib.DIBsection = NULL;
790 surface->dib.bitmap_data = NULL;
791 surface->flags &= ~SFLAG_DIBSECTION;
792 create_dib = TRUE;
795 wined3d_resource_free_sysmem(&surface->resource);
797 if ((texture->row_pitch = pitch))
798 texture->slice_pitch = height * pitch;
799 else
800 /* User memory surfaces don't have the regular surface alignment. */
801 wined3d_format_calculate_pitch(format, 1, width, height,
802 &texture->row_pitch, &texture->slice_pitch);
804 texture->resource.format = format;
805 texture->resource.multisample_type = multisample_type;
806 texture->resource.multisample_quality = multisample_quality;
807 texture->resource.width = width;
808 texture->resource.height = height;
810 surface->resource.format = format;
811 surface->resource.multisample_type = multisample_type;
812 surface->resource.multisample_quality = multisample_quality;
813 surface->resource.width = width;
814 surface->resource.height = height;
815 surface->resource.size = texture->slice_pitch;
817 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
818 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
820 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
821 surface->pow2Width = surface->pow2Height = 1;
822 while (surface->pow2Width < width)
823 surface->pow2Width <<= 1;
824 while (surface->pow2Height < height)
825 surface->pow2Height <<= 1;
827 else
829 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
830 surface->pow2Width = width;
831 surface->pow2Height = height;
834 surface->locations = 0;
836 if ((texture->user_memory = mem))
838 surface->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
839 valid_location = WINED3D_LOCATION_USER_MEMORY;
841 else if (create_dib && SUCCEEDED(surface_create_dib_section(surface)))
843 valid_location = WINED3D_LOCATION_DIB;
845 else
847 wined3d_surface_prepare(surface, NULL, WINED3D_LOCATION_SYSMEM);
848 valid_location = WINED3D_LOCATION_SYSMEM;
851 /* The format might be changed to a format that needs conversion.
852 * If the surface didn't use PBOs previously but could now, don't
853 * change it - whatever made us not use PBOs might come back, e.g.
854 * color keys. */
855 if (surface->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
856 surface->resource.map_binding = surface->dib.DIBsection ? WINED3D_LOCATION_DIB : WINED3D_LOCATION_SYSMEM;
858 surface_validate_location(surface, valid_location);
860 return WINED3D_OK;
863 /* Context activation is done by the caller. */
864 void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
865 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
867 GLuint *buffer_object;
869 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
870 if (*buffer_object)
871 return;
873 GL_EXTCALL(glGenBuffers(1, buffer_object));
874 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *buffer_object));
875 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER,
876 texture->sub_resources[sub_resource_idx].resource->size, NULL, GL_STREAM_DRAW));
877 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
878 checkGLcall("Create buffer object");
880 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
881 *buffer_object, texture, sub_resource_idx);
884 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
886 unsigned int sub_count = texture->level_count * texture->layer_count;
887 unsigned int i;
889 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
890 | WINED3D_TEXTURE_CONVERTED);
891 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
892 for (i = 0; i < sub_count; ++i)
894 texture->texture_ops->texture_sub_resource_invalidate_location(texture->sub_resources[i].resource,
895 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
899 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
901 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
902 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
904 if (!d3d_info->shader_color_key
905 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
906 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
908 wined3d_texture_force_reload(texture);
910 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
911 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
914 if (texture->flags & alloc_flag)
915 return;
917 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
918 texture->flags |= alloc_flag;
921 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
923 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
924 FIXME("texture %p stub!\n", texture);
927 struct wined3d_resource *wined3d_texture_get_sub_resource(const struct wined3d_texture *texture,
928 UINT sub_resource_idx)
930 UINT sub_count = texture->level_count * texture->layer_count;
932 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
934 if (sub_resource_idx >= sub_count)
936 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
937 return NULL;
940 return texture->sub_resources[sub_resource_idx].resource;
943 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
944 UINT layer, const struct wined3d_box *dirty_region)
946 struct wined3d_resource *sub_resource;
947 struct wined3d_context *context;
948 unsigned int sub_resource_idx;
950 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
952 sub_resource_idx = layer * texture->level_count;
953 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
955 WARN("Failed to get sub-resource.\n");
956 return WINED3DERR_INVALIDCALL;
959 if (dirty_region)
960 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
962 context = context_acquire(texture->resource.device, NULL);
963 if (!texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, sub_resource->map_binding))
965 ERR("Failed to load location %s.\n", wined3d_debug_location(sub_resource->map_binding));
966 context_release(context);
967 return E_OUTOFMEMORY;
969 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource, ~sub_resource->map_binding);
970 context_release(context);
972 return WINED3D_OK;
975 static HRESULT wined3d_texture_upload_data(struct wined3d_texture *texture,
976 const struct wined3d_sub_resource_data *data)
978 unsigned int sub_count = texture->level_count * texture->layer_count;
979 struct wined3d_context *context;
980 unsigned int i;
982 for (i = 0; i < sub_count; ++i)
984 if (!data[i].data)
986 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
987 return E_INVALIDARG;
991 context = context_acquire(texture->resource.device, NULL);
993 wined3d_texture_prepare_texture(texture, context, FALSE);
994 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
996 for (i = 0; i < sub_count; ++i)
998 struct wined3d_resource *sub_resource = texture->sub_resources[i].resource;
1000 texture->texture_ops->texture_sub_resource_upload_data(sub_resource, context, &data[i]);
1001 texture->texture_ops->texture_sub_resource_validate_location(sub_resource, WINED3D_LOCATION_TEXTURE_RGB);
1002 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource, ~WINED3D_LOCATION_TEXTURE_RGB);
1005 context_release(context);
1007 return WINED3D_OK;
1010 static void texture2d_sub_resource_load(struct wined3d_resource *sub_resource,
1011 struct wined3d_context *context, BOOL srgb)
1013 surface_load(surface_from_resource(sub_resource), context, srgb);
1016 static void texture2d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
1018 struct wined3d_surface *surface = surface_from_resource(sub_resource);
1020 surface_invalidate_location(surface, location);
1023 static void texture2d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
1025 struct wined3d_surface *surface = surface_from_resource(sub_resource);
1027 surface_validate_location(surface, location);
1030 static void texture2d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
1031 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1033 struct wined3d_surface *surface = surface_from_resource(sub_resource);
1034 static const POINT dst_point = {0, 0};
1035 struct wined3d_const_bo_address addr;
1036 RECT src_rect;
1038 SetRect(&src_rect, 0, 0, surface->resource.width, surface->resource.height);
1040 addr.buffer_object = 0;
1041 addr.addr = data->data;
1043 wined3d_surface_upload_data(surface, context->gl_info, surface->container->resource.format,
1044 &src_rect, data->row_pitch, &dst_point, FALSE, &addr);
1047 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1048 struct wined3d_context *context, DWORD location)
1050 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1053 static BOOL texture2d_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1054 struct wined3d_context *context, DWORD location)
1056 wined3d_surface_prepare(texture->sub_resources[sub_resource_idx].u.surface, context, location);
1057 return TRUE;
1060 /* Context activation is done by the caller. */
1061 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1063 UINT sub_count = texture->level_count * texture->layer_count;
1064 const struct wined3d_format *format = texture->resource.format;
1065 const struct wined3d_gl_info *gl_info = context->gl_info;
1066 const struct wined3d_color_key_conversion *conversion;
1067 GLenum internal;
1068 UINT i;
1070 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1072 if (format->convert)
1074 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1076 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1078 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1079 format = wined3d_get_format(gl_info, conversion->dst_format);
1080 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1083 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1085 if (srgb)
1086 internal = format->glGammaInternal;
1087 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1088 && wined3d_resource_is_offscreen(&texture->resource))
1089 internal = format->rtInternal;
1090 else
1091 internal = format->glInternal;
1093 if (!internal)
1094 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1096 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1098 for (i = 0; i < sub_count; ++i)
1100 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
1101 GLsizei height = surface->pow2Height;
1102 GLsizei width = surface->pow2Width;
1104 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
1106 height *= format->height_scale.numerator;
1107 height /= format->height_scale.denominator;
1110 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
1111 surface, surface->texture_target, surface->texture_level, width, height);
1113 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
1114 internal, width, height, 0, format->glFormat, format->glType, NULL);
1115 checkGLcall("glTexImage2D");
1119 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1121 unsigned int sub_count = texture->level_count * texture->layer_count;
1122 struct wined3d_surface *surface;
1123 unsigned int i;
1125 for (i = 0; i < sub_count; ++i)
1127 if ((surface = texture->sub_resources[i].u.surface))
1129 TRACE("surface %p.\n", surface);
1131 wined3d_surface_cleanup(surface);
1132 surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
1135 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1138 static const struct wined3d_texture_ops texture2d_ops =
1140 texture2d_sub_resource_load,
1141 texture2d_sub_resource_invalidate_location,
1142 texture2d_sub_resource_validate_location,
1143 texture2d_sub_resource_upload_data,
1144 texture2d_load_location,
1145 texture2d_prepare_location,
1146 texture2d_prepare_texture,
1147 texture2d_cleanup_sub_resources,
1150 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1152 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
1155 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1157 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
1160 static void wined3d_texture_unload(struct wined3d_resource *resource)
1162 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1163 UINT sub_count = texture->level_count * texture->layer_count;
1164 struct wined3d_context *context = NULL;
1165 UINT i;
1167 TRACE("texture %p.\n", texture);
1169 for (i = 0; i < sub_count; ++i)
1171 struct wined3d_resource *sub_resource = texture->sub_resources[i].resource;
1173 sub_resource->resource_ops->resource_unload(sub_resource);
1175 if (texture->sub_resources[i].buffer_object)
1177 if (!context)
1178 context = context_acquire(texture->resource.device, NULL);
1179 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1182 if (context)
1183 context_release(context);
1185 wined3d_texture_force_reload(texture);
1186 wined3d_texture_unload_gl_texture(texture);
1189 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1190 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1192 const struct wined3d_format *format = resource->format;
1193 struct wined3d_device *device = resource->device;
1194 unsigned int fmt_flags = resource->format_flags;
1195 const struct wined3d_gl_info *gl_info = NULL;
1196 struct wined3d_context *context = NULL;
1197 struct wined3d_resource *sub_resource;
1198 struct wined3d_texture *texture;
1199 unsigned int texture_level;
1200 BYTE *base_memory;
1201 BOOL ret;
1203 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1204 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1206 texture = wined3d_texture_from_resource(resource);
1207 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1208 return E_INVALIDARG;
1210 texture_level = sub_resource_idx % texture->level_count;
1211 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1213 WARN("Map box is invalid.\n");
1214 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1215 return WINED3DERR_INVALIDCALL;
1218 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1219 && !wined3d_texture_check_block_align(texture, texture_level, box))
1221 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1222 debug_box(box), format->block_width, format->block_height);
1223 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1224 return WINED3DERR_INVALIDCALL;
1227 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1229 WARN("Trying to map unmappable texture.\n");
1230 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1231 return WINED3DERR_INVALIDCALL;
1234 if (sub_resource->map_count)
1236 WARN("Sub-resource is already mapped.\n");
1237 return WINED3DERR_INVALIDCALL;
1240 flags = wined3d_resource_sanitize_map_flags(resource, flags);
1242 if (device->d3d_initialized)
1244 context = context_acquire(device, NULL);
1245 gl_info = context->gl_info;
1248 if (flags & WINED3D_MAP_DISCARD)
1250 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1251 wined3d_debug_location(sub_resource->map_binding));
1252 if ((ret = texture->texture_ops->texture_prepare_location(texture,
1253 sub_resource_idx, context, sub_resource->map_binding)))
1254 texture->texture_ops->texture_sub_resource_validate_location(sub_resource, sub_resource->map_binding);
1256 else
1258 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1259 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1260 ret = texture->texture_ops->texture_load_location(texture,
1261 sub_resource_idx, context, sub_resource->map_binding);
1264 if (!ret)
1266 ERR("Failed to prepare location.\n");
1267 context_release(context);
1268 return E_OUTOFMEMORY;
1271 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1272 texture->texture_ops->texture_sub_resource_invalidate_location(sub_resource, ~sub_resource->map_binding);
1274 switch (sub_resource->map_binding)
1276 case WINED3D_LOCATION_SYSMEM:
1277 base_memory = sub_resource->heap_memory;
1278 break;
1280 case WINED3D_LOCATION_USER_MEMORY:
1281 base_memory = texture->user_memory;
1282 break;
1284 case WINED3D_LOCATION_DIB:
1285 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1286 ERR("Invalid map binding %#x for resource type %#x.\n",
1287 sub_resource->map_binding, resource->type);
1288 base_memory = texture->sub_resources[sub_resource_idx].u.surface->dib.bitmap_data;
1289 break;
1291 case WINED3D_LOCATION_BUFFER:
1292 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER,
1293 texture->sub_resources[sub_resource_idx].buffer_object));
1295 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
1297 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags);
1298 map_flags &= ~GL_MAP_FLUSH_EXPLICIT_BIT;
1299 base_memory = GL_EXTCALL(glMapBufferRange(GL_PIXEL_UNPACK_BUFFER,
1300 0, sub_resource->size, map_flags));
1302 else
1304 GLenum access = wined3d_resource_gl_legacy_map_flags(flags);
1305 base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, access));
1308 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1309 checkGLcall("Map PBO");
1310 break;
1312 default:
1313 ERR("Unexpected map binding %s.\n", wined3d_debug_location(sub_resource->map_binding));
1314 base_memory = NULL;
1315 break;
1318 if (context)
1319 context_release(context);
1321 TRACE("Base memory pointer %p.\n", base_memory);
1323 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1325 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1326 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1328 else
1330 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1333 if (!box)
1335 map_desc->data = base_memory;
1337 else
1339 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1341 /* Compressed textures are block based, so calculate the offset of
1342 * the block that contains the top-left pixel of the mapped box. */
1343 map_desc->data = base_memory
1344 + (box->front * map_desc->slice_pitch)
1345 + ((box->top / format->block_height) * map_desc->row_pitch)
1346 + ((box->left / format->block_width) * format->block_byte_count);
1348 else
1350 map_desc->data = base_memory
1351 + (box->front * map_desc->slice_pitch)
1352 + (box->top * map_desc->row_pitch)
1353 + (box->left * format->byte_count);
1357 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1359 RECT *r = &texture->swapchain->front_buffer_update;
1361 if (!box)
1362 SetRect(r, 0, 0, resource->width, resource->height);
1363 else
1364 SetRect(r, box->left, box->top, box->right, box->bottom);
1365 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1368 ++sub_resource->map_count;
1370 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1371 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1373 return WINED3D_OK;
1376 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1378 const struct wined3d_gl_info *gl_info;
1379 struct wined3d_resource *sub_resource;
1380 struct wined3d_texture *texture;
1381 struct wined3d_context *context;
1383 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1385 texture = wined3d_texture_from_resource(resource);
1386 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1387 return E_INVALIDARG;
1389 if (!sub_resource->map_count)
1391 WARN("Trying to unmap unmapped sub-resource.\n");
1392 return WINEDDERR_NOTLOCKED;
1395 switch (sub_resource->map_binding)
1397 case WINED3D_LOCATION_SYSMEM:
1398 case WINED3D_LOCATION_USER_MEMORY:
1399 case WINED3D_LOCATION_DIB:
1400 break;
1402 case WINED3D_LOCATION_BUFFER:
1403 context = context_acquire(resource->device, NULL);
1404 gl_info = context->gl_info;
1406 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER,
1407 texture->sub_resources[sub_resource_idx].buffer_object));
1408 GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
1409 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1410 checkGLcall("glUnmapBuffer");
1411 context_release(context);
1412 break;
1414 default:
1415 ERR("Unexpected map binding %s.\n", wined3d_debug_location(sub_resource->map_binding));
1416 break;
1419 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1421 struct wined3d_surface *surface = texture->sub_resources[sub_resource_idx].u.surface;
1423 if (!(surface->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1424 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1426 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1428 FIXME("Depth / stencil buffer locking is not implemented.\n");
1431 --sub_resource->map_count;
1433 return WINED3D_OK;
1436 static const struct wined3d_resource_ops texture_resource_ops =
1438 texture_resource_incref,
1439 texture_resource_decref,
1440 wined3d_texture_unload,
1441 texture_resource_sub_resource_map,
1442 texture_resource_sub_resource_unmap,
1445 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1446 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1447 void *parent, const struct wined3d_parent_ops *parent_ops)
1449 struct wined3d_device_parent *device_parent = device->device_parent;
1450 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1451 struct wined3d_resource_desc surface_desc;
1452 struct wined3d_surface *surfaces;
1453 UINT pow2_width, pow2_height;
1454 unsigned int i, j;
1455 HRESULT hr;
1457 /* TODO: It should only be possible to create textures for formats
1458 * that are reported as supported. */
1459 if (WINED3DFMT_UNKNOWN >= desc->format)
1461 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1462 return WINED3DERR_INVALIDCALL;
1465 pow2_width = desc->width;
1466 pow2_height = desc->height;
1467 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1468 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1470 /* level_count == 0 returns an error as well. */
1471 if (level_count != 1 || desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1473 if (desc->pool != WINED3D_POOL_SCRATCH)
1475 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1476 return WINED3DERR_INVALIDCALL;
1479 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1481 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1483 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1485 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1487 /* TODO: Add support for non-power-of-two compressed textures. */
1488 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1489 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1491 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1492 desc->width, desc->height);
1493 return WINED3DERR_NOTAVAILABLE;
1496 /* Find the nearest pow2 match. */
1497 pow2_width = pow2_height = 1;
1498 while (pow2_width < desc->width)
1499 pow2_width <<= 1;
1500 while (pow2_height < desc->height)
1501 pow2_height <<= 1;
1502 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1506 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1507 && (desc->usage & WINED3DUSAGE_TEXTURE))
1509 /* One of four options:
1510 * 1: Do the same as we do with NPOT and scale the texture. (Any
1511 * texture ops would require the texture to be scaled which is
1512 * potentially slow.)
1513 * 2: Set the texture to the maximum size (bad idea).
1514 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1515 * 4: Create the surface, but allow it to be used only for DirectDraw
1516 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1517 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1518 * the render target. */
1519 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1521 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1522 return WINED3DERR_NOTAVAILABLE;
1525 /* We should never use this surface in combination with OpenGL. */
1526 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1529 /* Calculate levels for mip mapping. */
1530 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1532 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1534 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1535 return WINED3DERR_INVALIDCALL;
1538 if (level_count != 1)
1540 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1541 return WINED3DERR_INVALIDCALL;
1545 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1546 flags, device, parent, parent_ops, &texture_resource_ops)))
1548 WARN("Failed to initialize texture, returning %#x.\n", hr);
1549 return hr;
1552 /* Precalculated scaling for 'faked' non power of two texture coords. */
1553 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1555 texture->pow2_matrix[0] = (float)desc->width;
1556 texture->pow2_matrix[5] = (float)desc->height;
1557 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1558 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1560 else
1562 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1564 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1565 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1566 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1568 else
1570 texture->pow2_matrix[0] = 1.0f;
1571 texture->pow2_matrix[5] = 1.0f;
1573 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1574 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1575 else
1576 texture->target = GL_TEXTURE_2D;
1578 texture->pow2_matrix[10] = 1.0f;
1579 texture->pow2_matrix[15] = 1.0f;
1580 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1582 if (!(surfaces = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*surfaces) * level_count * layer_count)))
1584 wined3d_texture_cleanup(texture);
1585 return E_OUTOFMEMORY;
1588 /* Generate all the surfaces. */
1589 surface_desc = *desc;
1590 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1591 for (i = 0; i < texture->level_count; ++i)
1593 for (j = 0; j < texture->layer_count; ++j)
1595 static const GLenum cube_targets[6] =
1597 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1598 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1599 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1600 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1601 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1602 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1604 GLenum target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
1605 unsigned int idx = j * texture->level_count + i;
1606 struct wined3d_surface *surface;
1608 surface = &surfaces[idx];
1609 if (FAILED(hr = wined3d_surface_init(surface, texture, &surface_desc, target, i, j, flags)))
1611 WARN("Failed to initialize surface, returning %#x.\n", hr);
1612 wined3d_texture_cleanup(texture);
1613 if (!idx)
1614 HeapFree(GetProcessHeap(), 0, surfaces);
1615 return hr;
1618 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
1619 texture, idx, &parent, &parent_ops)))
1621 WARN("Failed to create surface parent, hr %#x.\n", hr);
1622 wined3d_surface_cleanup(surface);
1623 wined3d_texture_cleanup(texture);
1624 return hr;
1627 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
1629 surface->resource.parent = parent;
1630 surface->resource.parent_ops = parent_ops;
1631 texture->sub_resources[idx].resource = &surface->resource;
1632 texture->sub_resources[idx].u.surface = surface;
1633 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
1635 /* Calculate the next mipmap level. */
1636 surface_desc.width = max(1, surface_desc.width >> 1);
1637 surface_desc.height = max(1, surface_desc.height >> 1);
1640 return WINED3D_OK;
1643 static void texture3d_sub_resource_load(struct wined3d_resource *sub_resource,
1644 struct wined3d_context *context, BOOL srgb)
1646 wined3d_volume_load(volume_from_resource(sub_resource), context, srgb);
1649 static void texture3d_sub_resource_invalidate_location(struct wined3d_resource *sub_resource, DWORD location)
1651 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1653 wined3d_volume_invalidate_location(volume, location);
1656 static void texture3d_sub_resource_validate_location(struct wined3d_resource *sub_resource, DWORD location)
1658 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1660 wined3d_volume_validate_location(volume, location);
1663 static void texture3d_sub_resource_upload_data(struct wined3d_resource *sub_resource,
1664 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1666 struct wined3d_volume *volume = volume_from_resource(sub_resource);
1667 struct wined3d_const_bo_address addr;
1668 unsigned int row_pitch, slice_pitch;
1670 wined3d_texture_get_pitch(volume->container, volume->texture_level, &row_pitch, &slice_pitch);
1671 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1672 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1674 addr.buffer_object = 0;
1675 addr.addr = data->data;
1677 wined3d_volume_upload_data(volume, context, &addr);
1680 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1681 struct wined3d_context *context, DWORD location)
1683 return wined3d_volume_load_location(texture->sub_resources[sub_resource_idx].u.volume, context, location);
1686 static BOOL texture3d_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1687 struct wined3d_context *context, DWORD location)
1689 return wined3d_volume_prepare_location(texture->sub_resources[sub_resource_idx].u.volume, context, location);
1692 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1694 unsigned int sub_count = texture->level_count * texture->layer_count;
1695 const struct wined3d_format *format = texture->resource.format;
1696 const struct wined3d_gl_info *gl_info = context->gl_info;
1697 unsigned int i;
1699 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1701 for (i = 0; i < sub_count; ++i)
1703 struct wined3d_volume *volume = texture->sub_resources[i].u.volume;
1705 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
1706 srgb ? format->glGammaInternal : format->glInternal,
1707 volume->resource.width, volume->resource.height, volume->resource.depth,
1708 0, format->glFormat, format->glType, NULL));
1709 checkGLcall("glTexImage3D");
1713 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
1715 unsigned int sub_count = texture->level_count * texture->layer_count;
1716 struct wined3d_volume *volume;
1717 unsigned int i;
1719 for (i = 0; i < sub_count; ++i)
1721 if ((volume = texture->sub_resources[i].u.volume))
1723 TRACE("volume %p.\n", volume);
1725 wined3d_volume_cleanup(volume);
1726 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
1729 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.volume);
1732 static const struct wined3d_texture_ops texture3d_ops =
1734 texture3d_sub_resource_load,
1735 texture3d_sub_resource_invalidate_location,
1736 texture3d_sub_resource_validate_location,
1737 texture3d_sub_resource_upload_data,
1738 texture3d_load_location,
1739 texture3d_prepare_location,
1740 texture3d_prepare_texture,
1741 texture3d_cleanup_sub_resources,
1744 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
1745 unsigned int level, const struct wined3d_box *box)
1747 const struct wined3d_format *format = texture->resource.format;
1748 unsigned int height = wined3d_texture_get_level_height(texture, level);
1749 unsigned int width = wined3d_texture_get_level_width(texture, level);
1750 unsigned int width_mask, height_mask;
1752 if ((box->left >= box->right)
1753 || (box->top >= box->bottom)
1754 || (box->right > width)
1755 || (box->bottom > height))
1756 return FALSE;
1758 /* This assumes power of two block sizes, but NPOT block sizes would be
1759 * silly anyway.
1761 * This also assumes that the format's block depth is 1. */
1762 width_mask = format->block_width - 1;
1763 height_mask = format->block_height - 1;
1765 if ((box->left & width_mask) || (box->top & height_mask)
1766 || (box->right & width_mask && box->right != width)
1767 || (box->bottom & height_mask && box->bottom != height))
1768 return FALSE;
1770 return TRUE;
1773 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1774 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
1776 struct wined3d_device_parent *device_parent = device->device_parent;
1777 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1778 struct wined3d_resource_desc volume_desc;
1779 struct wined3d_volume *volumes;
1780 unsigned int i;
1781 HRESULT hr;
1783 /* TODO: It should only be possible to create textures for formats
1784 * that are reported as supported. */
1785 if (WINED3DFMT_UNKNOWN >= desc->format)
1787 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1788 return WINED3DERR_INVALIDCALL;
1791 if (!gl_info->supported[EXT_TEXTURE3D])
1793 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
1794 return WINED3DERR_INVALIDCALL;
1797 /* Calculate levels for mip mapping. */
1798 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1800 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1802 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
1803 return WINED3DERR_INVALIDCALL;
1806 if (levels != 1)
1808 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
1809 return WINED3DERR_INVALIDCALL;
1813 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1815 UINT pow2_w, pow2_h, pow2_d;
1816 pow2_w = 1;
1817 while (pow2_w < desc->width)
1818 pow2_w <<= 1;
1819 pow2_h = 1;
1820 while (pow2_h < desc->height)
1821 pow2_h <<= 1;
1822 pow2_d = 1;
1823 while (pow2_d < desc->depth)
1824 pow2_d <<= 1;
1826 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
1828 if (desc->pool == WINED3D_POOL_SCRATCH)
1830 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
1832 else
1834 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
1835 desc->width, desc->height, desc->depth);
1836 return WINED3DERR_INVALIDCALL;
1841 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
1842 0, device, parent, parent_ops, &texture_resource_ops)))
1844 WARN("Failed to initialize texture, returning %#x.\n", hr);
1845 return hr;
1848 texture->pow2_matrix[0] = 1.0f;
1849 texture->pow2_matrix[5] = 1.0f;
1850 texture->pow2_matrix[10] = 1.0f;
1851 texture->pow2_matrix[15] = 1.0f;
1852 texture->target = GL_TEXTURE_3D;
1854 if (!(volumes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volumes) * levels)))
1856 wined3d_texture_cleanup(texture);
1857 return E_OUTOFMEMORY;
1860 /* Generate all the surfaces. */
1861 volume_desc = *desc;
1862 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
1863 for (i = 0; i < texture->level_count; ++i)
1865 struct wined3d_volume *volume;
1867 volume = &volumes[i];
1868 if (FAILED(hr = wined3d_volume_init(volume, texture, &volume_desc, i)))
1870 WARN("Failed to initialize volume, returning %#x.\n", hr);
1871 wined3d_texture_cleanup(texture);
1872 if (!i)
1873 HeapFree(GetProcessHeap(), 0, volumes);
1874 return hr;
1877 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
1878 texture, i, &parent, &parent_ops)))
1880 WARN("Failed to create volume parent, hr %#x.\n", hr);
1881 wined3d_volume_cleanup(volume);
1882 wined3d_texture_cleanup(texture);
1883 return hr;
1886 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
1888 volume->resource.parent = parent;
1889 volume->resource.parent_ops = parent_ops;
1890 texture->sub_resources[i].resource = &volume->resource;
1891 texture->sub_resources[i].u.volume = volume;
1892 TRACE("Created volume level %u @ %p.\n", i, volume);
1894 /* Calculate the next mipmap level. */
1895 volume_desc.width = max(1, volume_desc.width >> 1);
1896 volume_desc.height = max(1, volume_desc.height >> 1);
1897 volume_desc.depth = max(1, volume_desc.depth >> 1);
1900 return WINED3D_OK;
1903 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
1904 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
1905 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
1907 struct wined3d_resource *dst_resource, *src_resource = NULL;
1909 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
1910 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
1911 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
1912 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
1914 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
1915 || dst_resource->type != WINED3D_RTYPE_SURFACE)
1916 return WINED3DERR_INVALIDCALL;
1918 if (src_texture)
1920 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
1921 || src_resource->type != WINED3D_RTYPE_SURFACE)
1922 return WINED3DERR_INVALIDCALL;
1925 return wined3d_surface_blt(surface_from_resource(dst_resource), dst_rect,
1926 src_resource ? surface_from_resource(src_resource) : NULL, src_rect, flags, fx, filter);
1929 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
1930 unsigned int sub_resource_idx, LONG *x, LONG *y)
1932 struct wined3d_resource *sub_resource;
1933 struct wined3d_surface *surface;
1935 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
1937 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1938 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1940 WARN("Invalid sub-resource specified.\n");
1941 return WINEDDERR_NOTAOVERLAYSURFACE;
1944 surface = surface_from_resource(sub_resource);
1945 if (!surface->overlay_dest)
1947 TRACE("Overlay not visible.\n");
1948 *x = 0;
1949 *y = 0;
1950 return WINEDDERR_OVERLAYNOTVISIBLE;
1953 *x = surface->overlay_destrect.left;
1954 *y = surface->overlay_destrect.top;
1956 TRACE("Returning position %d, %d.\n", *x, *y);
1958 return WINED3D_OK;
1961 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
1962 unsigned int sub_resource_idx, LONG x, LONG y)
1964 struct wined3d_resource *sub_resource;
1965 struct wined3d_surface *surface;
1966 LONG w, h;
1968 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
1970 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
1971 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1973 WARN("Invalid sub-resource specified.\n");
1974 return WINEDDERR_NOTAOVERLAYSURFACE;
1977 surface = surface_from_resource(sub_resource);
1978 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
1979 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
1980 surface->overlay_destrect.left = x;
1981 surface->overlay_destrect.top = y;
1982 surface->overlay_destrect.right = x + w;
1983 surface->overlay_destrect.bottom = y + h;
1985 return WINED3D_OK;
1988 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1989 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
1990 const RECT *dst_rect, DWORD flags)
1992 struct wined3d_resource *sub_resource, *dst_sub_resource;
1993 struct wined3d_surface *surface, *dst_surface;
1995 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
1996 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
1997 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
1998 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2000 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2001 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2003 WARN("Invalid sub-resource specified.\n");
2004 return WINEDDERR_NOTAOVERLAYSURFACE;
2007 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2008 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2010 WARN("Invalid destination sub-resource specified.\n");
2011 return WINED3DERR_INVALIDCALL;
2014 surface = surface_from_resource(sub_resource);
2015 if (src_rect)
2016 surface->overlay_srcrect = *src_rect;
2017 else
2018 SetRect(&surface->overlay_srcrect, 0, 0, surface->resource.width, surface->resource.height);
2020 dst_surface = surface_from_resource(dst_sub_resource);
2021 if (dst_rect)
2022 surface->overlay_destrect = *dst_rect;
2023 else
2024 SetRect(&surface->overlay_destrect, 0, 0, dst_surface->resource.width, dst_surface->resource.height);
2026 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2028 surface->overlay_dest = NULL;
2029 list_remove(&surface->overlay_entry);
2032 if (flags & WINEDDOVER_SHOW)
2034 if (surface->overlay_dest != dst_surface)
2036 surface->overlay_dest = dst_surface;
2037 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2040 else if (flags & WINEDDOVER_HIDE)
2042 /* Tests show that the rectangles are erased on hide. */
2043 SetRectEmpty(&surface->overlay_srcrect);
2044 SetRectEmpty(&surface->overlay_destrect);
2045 surface->overlay_dest = NULL;
2048 return WINED3D_OK;
2051 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2053 unsigned int sub_count = texture->level_count * texture->layer_count;
2055 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2057 if (sub_resource_idx >= sub_count)
2059 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2060 return NULL;
2063 return texture->sub_resources[sub_resource_idx].resource->parent;
2066 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2067 unsigned int sub_resource_idx, void *parent)
2069 unsigned int sub_count = texture->level_count * texture->layer_count;
2071 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2073 if (sub_resource_idx >= sub_count)
2075 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2076 return;
2079 texture->sub_resources[sub_resource_idx].resource->parent = parent;
2082 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2083 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2085 unsigned int sub_count = texture->level_count * texture->layer_count;
2086 const struct wined3d_resource *resource;
2087 unsigned int level_idx;
2089 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2091 if (sub_resource_idx >= sub_count)
2093 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2094 return WINED3DERR_INVALIDCALL;
2097 resource = &texture->resource;
2098 desc->format = resource->format->id;
2099 desc->multisample_type = resource->multisample_type;
2100 desc->multisample_quality = resource->multisample_quality;
2101 desc->usage = resource->usage;
2102 desc->pool = resource->pool;
2104 level_idx = sub_resource_idx % texture->level_count;
2105 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2106 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2107 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2108 desc->size = texture->sub_resources[sub_resource_idx].resource->size;
2110 return WINED3D_OK;
2113 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2114 UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data, void *parent,
2115 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2117 unsigned int layer_count = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? 6 : 1;
2118 struct wined3d_texture *object;
2119 HRESULT hr;
2121 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2122 device, desc, level_count, flags, data, parent, parent_ops, texture);
2124 if (!level_count)
2126 WARN("Invalid level count.\n");
2127 return WINED3DERR_INVALIDCALL;
2130 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2132 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2134 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2135 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2137 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2138 desc->multisample_quality);
2139 return WINED3DERR_NOTAVAILABLE;
2141 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2142 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2143 || desc->multisample_quality))
2145 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2146 desc->multisample_quality);
2147 return WINED3DERR_NOTAVAILABLE;
2151 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2152 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2153 return E_OUTOFMEMORY;
2155 switch (desc->resource_type)
2157 case WINED3D_RTYPE_TEXTURE_2D:
2158 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2159 break;
2161 case WINED3D_RTYPE_TEXTURE_3D:
2162 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
2163 break;
2165 default:
2166 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2167 hr = WINED3DERR_INVALIDCALL;
2168 break;
2171 if (FAILED(hr))
2173 WARN("Failed to initialize texture, returning %#x.\n", hr);
2174 HeapFree(GetProcessHeap(), 0, object);
2175 return hr;
2178 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2179 * in this case. */
2180 if (data && FAILED(hr = wined3d_texture_upload_data(object, data)))
2182 wined3d_texture_cleanup(object);
2183 HeapFree(GetProcessHeap(), 0, object);
2184 return hr;
2187 TRACE("Created texture %p.\n", object);
2188 *texture = object;
2190 return WINED3D_OK;
2193 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2195 struct wined3d_device *device = texture->resource.device;
2196 struct wined3d_context *context = NULL;
2197 struct wined3d_resource *sub_resource;
2198 struct wined3d_surface *surface;
2199 HRESULT hr;
2201 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2203 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2204 return WINED3DERR_INVALIDCALL;
2206 if (sub_resource->type != WINED3D_RTYPE_SURFACE)
2208 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2209 return WINED3DERR_INVALIDCALL;
2212 surface = surface_from_resource(sub_resource);
2214 /* Give more detailed info for ddraw. */
2215 if (surface->flags & SFLAG_DCINUSE)
2216 return WINEDDERR_DCALREADYCREATED;
2218 /* Can't GetDC if the surface is locked. */
2219 if (surface->resource.map_count)
2220 return WINED3DERR_INVALIDCALL;
2222 if (device->d3d_initialized)
2223 context = context_acquire(device, NULL);
2225 /* Create a DIB section if there isn't a dc yet. */
2226 if (!surface->hDC)
2228 if (FAILED(hr = surface_create_dib_section(surface)))
2230 if (context)
2231 context_release(context);
2232 return WINED3DERR_INVALIDCALL;
2234 if (!(surface->resource.map_binding == WINED3D_LOCATION_USER_MEMORY
2235 || texture->flags & WINED3D_TEXTURE_PIN_SYSMEM
2236 || texture->sub_resources[sub_resource_idx].buffer_object))
2237 surface->resource.map_binding = WINED3D_LOCATION_DIB;
2240 surface_load_location(surface, context, WINED3D_LOCATION_DIB);
2241 surface_invalidate_location(surface, ~WINED3D_LOCATION_DIB);
2243 if (context)
2244 context_release(context);
2246 surface->flags |= SFLAG_DCINUSE;
2247 surface->resource.map_count++;
2249 *dc = surface->hDC;
2250 TRACE("Returning dc %p.\n", *dc);
2252 return WINED3D_OK;
2255 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2257 struct wined3d_device *device = texture->resource.device;
2258 struct wined3d_context *context = NULL;
2259 struct wined3d_resource *sub_resource;
2260 struct wined3d_surface *surface;
2262 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2264 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2265 return WINED3DERR_INVALIDCALL;
2267 if (sub_resource->type != WINED3D_RTYPE_SURFACE)
2269 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2270 return WINED3DERR_INVALIDCALL;
2273 surface = surface_from_resource(sub_resource);
2275 if (!(surface->flags & SFLAG_DCINUSE))
2276 return WINEDDERR_NODC;
2278 if (surface->hDC != dc)
2280 WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
2281 dc, surface->hDC);
2282 return WINEDDERR_NODC;
2285 surface->resource.map_count--;
2286 surface->flags &= ~SFLAG_DCINUSE;
2288 if (surface->resource.map_binding == WINED3D_LOCATION_USER_MEMORY
2289 || (surface->container->flags & WINED3D_TEXTURE_PIN_SYSMEM
2290 && surface->resource.map_binding != WINED3D_LOCATION_DIB))
2292 /* The game Salammbo modifies the surface contents without mapping the surface between
2293 * a GetDC/ReleaseDC operation and flipping the surface. If the DIB remains the active
2294 * copy and is copied to the screen, this update, which draws the mouse pointer, is lost.
2295 * Do not only copy the DIB to the map location, but also make sure the map location is
2296 * copied back to the DIB in the next getdc call.
2298 * The same consideration applies to user memory surfaces. */
2300 if (device->d3d_initialized)
2301 context = context_acquire(device, NULL);
2303 surface_load_location(surface, context, surface->resource.map_binding);
2304 surface_invalidate_location(surface, WINED3D_LOCATION_DIB);
2305 if (context)
2306 context_release(context);
2309 return WINED3D_OK;