wined3d: Don't enumerate sub-resources in wined3d_device_reset().
[wine.git] / dlls / wined3d / texture.c
bloba5270c66561ceea978a2a6c8aa2ac53f9ac0e22d
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);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
35 return texture->resource.pool == WINED3D_POOL_DEFAULT
36 && texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
37 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
38 && !texture->resource.format->convert
39 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
42 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
44 const struct wined3d_swapchain *swapchain = texture->swapchain;
46 TRACE("texture %p.\n", texture);
48 if (!swapchain)
50 ERR("Texture %p is not part of a swapchain.\n", texture);
51 return GL_NONE;
54 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
56 if (swapchain->render_to_fbo)
58 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
59 return GL_COLOR_ATTACHMENT0;
61 TRACE("Returning GL_BACK.\n");
62 return GL_BACK;
64 else if (texture == swapchain->front_buffer)
66 TRACE("Returning GL_FRONT.\n");
67 return GL_FRONT;
70 FIXME("Higher back buffer, returning GL_BACK.\n");
71 return GL_BACK;
74 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
76 struct wined3d_texture_sub_resource *sub_resource;
77 unsigned int i, sub_count;
79 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
80 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
82 TRACE("Not evicting system memory for texture %p.\n", texture);
83 return;
86 TRACE("Evicting system memory for texture %p.\n", texture);
88 sub_count = texture->level_count * texture->layer_count;
89 for (i = 0; i < sub_count; ++i)
91 sub_resource = &texture->sub_resources[i];
92 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
93 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
94 i, texture);
95 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
97 wined3d_resource_free_sysmem(&texture->resource);
100 void wined3d_texture_validate_location(struct wined3d_texture *texture,
101 unsigned int sub_resource_idx, DWORD location)
103 struct wined3d_texture_sub_resource *sub_resource;
104 DWORD previous_locations;
106 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
107 texture, sub_resource_idx, wined3d_debug_location(location));
109 sub_resource = &texture->sub_resources[sub_resource_idx];
110 previous_locations = sub_resource->locations;
111 sub_resource->locations |= location;
112 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
113 && !--texture->sysmem_count)
114 wined3d_texture_evict_sysmem(texture);
116 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
119 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
120 unsigned int sub_resource_idx, DWORD location)
122 struct wined3d_texture_sub_resource *sub_resource;
124 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
125 texture, sub_resource_idx, wined3d_debug_location(location));
127 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
128 wined3d_texture_set_dirty(texture);
130 sub_resource = &texture->sub_resources[sub_resource_idx];
131 sub_resource->locations &= ~location;
132 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
133 ++texture->sysmem_count;
135 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
137 if (!sub_resource->locations)
138 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
139 sub_resource_idx, texture);
142 /* Context activation is done by the caller. */
143 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
144 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
146 BYTE *memory;
148 if (!data->buffer_object)
149 return data->addr;
151 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
153 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
155 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
156 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
158 else
160 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
161 memory += (INT_PTR)data->addr;
164 GL_EXTCALL(glBindBuffer(binding, 0));
165 checkGLcall("Map buffer object");
167 return memory;
170 /* Context activation is done by the caller. */
171 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
172 const struct wined3d_gl_info *gl_info, GLenum binding)
174 if (!data->buffer_object)
175 return;
177 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
178 GL_EXTCALL(glUnmapBuffer(binding));
179 GL_EXTCALL(glBindBuffer(binding, 0));
180 checkGLcall("Unmap buffer object");
183 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
184 struct wined3d_bo_address *data, DWORD locations)
186 struct wined3d_texture_sub_resource *sub_resource;
188 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
189 texture, sub_resource_idx, data, wined3d_debug_location(locations));
191 sub_resource = &texture->sub_resources[sub_resource_idx];
192 if (locations & WINED3D_LOCATION_BUFFER)
194 data->addr = NULL;
195 data->buffer_object = sub_resource->buffer_object;
196 return;
198 if (locations & WINED3D_LOCATION_USER_MEMORY)
200 data->addr = texture->user_memory;
201 data->buffer_object = 0;
202 return;
204 if (locations & WINED3D_LOCATION_SYSMEM)
206 data->addr = texture->resource.heap_memory;
207 data->addr += sub_resource->offset;
208 data->buffer_object = 0;
209 return;
212 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
213 data->addr = NULL;
214 data->buffer_object = 0;
217 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
218 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
219 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
220 const struct wined3d_resource_ops *resource_ops)
222 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
223 unsigned int i, j, size, offset = 0;
224 HRESULT hr;
226 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
227 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
228 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
229 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
230 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
231 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
232 flags, device, parent, parent_ops, resource_ops);
234 if (!desc->width || !desc->height || !desc->depth)
235 return WINED3DERR_INVALIDCALL;
237 for (i = 0; i < layer_count; ++i)
239 for (j = 0; j < level_count; ++j)
241 unsigned int idx = i * level_count + j;
243 size = wined3d_format_calculate_size(format, device->surface_alignment,
244 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
245 texture->sub_resources[idx].offset = offset;
246 texture->sub_resources[idx].size = size;
247 offset += size;
249 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
252 if (!offset)
253 return WINED3DERR_INVALIDCALL;
255 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
256 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
257 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
259 static unsigned int once;
261 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
262 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
263 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
264 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
265 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
266 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
268 WARN("Failed to initialize resource, returning %#x\n", hr);
269 return hr;
271 wined3d_resource_update_draw_binding(&texture->resource);
272 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
273 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
275 texture->texture_ops = texture_ops;
277 texture->layer_count = layer_count;
278 texture->level_count = level_count;
279 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
280 texture->lod = 0;
281 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
282 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
283 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
284 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
285 texture->flags |= WINED3D_TEXTURE_DISCARD;
287 return WINED3D_OK;
290 /* Context activation is done by the caller. */
291 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
292 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
294 GLuint *buffer_object;
296 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
297 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
298 checkGLcall("glDeleteBuffers");
299 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
300 *buffer_object = 0;
302 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
303 *buffer_object, texture, sub_resource_idx);
306 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
308 unsigned int sub_count = texture->level_count * texture->layer_count;
309 const struct wined3d_device *device = texture->resource.device;
310 DWORD map_binding = texture->update_map_binding;
311 struct wined3d_context *context = NULL;
312 unsigned int i;
314 if (device->d3d_initialized)
315 context = context_acquire(device, NULL);
317 for (i = 0; i < sub_count; ++i)
319 if (texture->sub_resources[i].locations == texture->resource.map_binding
320 && !texture->texture_ops->texture_load_location(texture, i, context, map_binding))
321 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
322 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
323 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
326 if (context)
327 context_release(context);
329 texture->resource.map_binding = map_binding;
330 texture->update_map_binding = 0;
333 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
335 texture->update_map_binding = map_binding;
336 if (!texture->resource.map_count)
337 wined3d_texture_update_map_binding(texture);
340 /* A GL context is provided by the caller */
341 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
342 struct gl_texture *tex)
344 context_gl_resource_released(device, tex->name, FALSE);
345 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
346 tex->name = 0;
349 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
351 struct wined3d_device *device = texture->resource.device;
352 const struct wined3d_gl_info *gl_info = NULL;
353 struct wined3d_context *context = NULL;
355 if (texture->texture_rgb.name || texture->texture_srgb.name
356 || texture->rb_multisample || texture->rb_resolved)
358 context = context_acquire(device, NULL);
359 gl_info = context->gl_info;
362 if (texture->texture_rgb.name)
363 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
365 if (texture->texture_srgb.name)
366 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
368 if (texture->rb_multisample)
370 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
371 context_gl_resource_released(device, texture->rb_multisample, TRUE);
372 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
375 if (texture->rb_resolved)
377 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
378 context_gl_resource_released(device, texture->rb_resolved, TRUE);
379 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
382 if (context) context_release(context);
384 wined3d_texture_set_dirty(texture);
386 resource_unload(&texture->resource);
389 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
391 unsigned int sub_count = texture->level_count * texture->layer_count;
392 struct wined3d_device *device = texture->resource.device;
393 struct wined3d_context *context = NULL;
394 const struct wined3d_gl_info *gl_info;
395 GLuint buffer_object;
396 unsigned int i;
398 TRACE("texture %p.\n", texture);
400 for (i = 0; i < sub_count; ++i)
402 if (!(buffer_object = texture->sub_resources[i].buffer_object))
403 continue;
405 TRACE("Deleting buffer object %u.\n", buffer_object);
407 /* We may not be able to get a context in wined3d_texture_cleanup() in
408 * general, but if a buffer object was previously created we can. */
409 if (!context)
411 context = context_acquire(device, NULL);
412 gl_info = context->gl_info;
415 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
417 if (context)
418 context_release(context);
420 texture->texture_ops->texture_cleanup_sub_resources(texture);
421 wined3d_texture_unload_gl_texture(texture);
422 resource_cleanup(&texture->resource);
425 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
427 texture->swapchain = swapchain;
428 wined3d_resource_update_draw_binding(&texture->resource);
431 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
433 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
436 /* Context activation is done by the caller. */
437 void wined3d_texture_bind(struct wined3d_texture *texture,
438 struct wined3d_context *context, BOOL srgb)
440 const struct wined3d_gl_info *gl_info = context->gl_info;
441 const struct wined3d_format *format = texture->resource.format;
442 const struct color_fixup_desc fixup = format->color_fixup;
443 struct gl_texture *gl_tex;
444 GLenum target;
446 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
448 if (!needs_separate_srgb_gl_texture(context))
449 srgb = FALSE;
451 /* sRGB mode cache for preload() calls outside drawprim. */
452 if (srgb)
453 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
454 else
455 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
457 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
458 target = texture->target;
460 if (gl_tex->name)
462 context_bind_texture(context, target, gl_tex->name);
463 return;
466 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
467 checkGLcall("glGenTextures");
468 TRACE("Generated texture %d.\n", gl_tex->name);
470 if (!gl_tex->name)
472 ERR("Failed to generate a texture name.\n");
473 return;
476 /* Initialise the state of the texture object to the OpenGL defaults, not
477 * the wined3d defaults. */
478 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
479 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
480 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
481 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
482 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
483 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
484 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
485 gl_tex->sampler_desc.lod_bias = 0.0f;
486 gl_tex->sampler_desc.min_lod = -1000.0f;
487 gl_tex->sampler_desc.max_lod = 1000.0f;
488 gl_tex->sampler_desc.max_anisotropy = 1;
489 gl_tex->sampler_desc.compare = FALSE;
490 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
491 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
492 gl_tex->sampler_desc.srgb_decode = TRUE;
493 else
494 gl_tex->sampler_desc.srgb_decode = srgb;
495 gl_tex->base_level = 0;
496 wined3d_texture_set_dirty(texture);
498 context_bind_texture(context, target, gl_tex->name);
500 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
502 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
503 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
506 /* For a new texture we have to set the texture levels after binding the
507 * texture. Beware that texture rectangles do not support mipmapping, but
508 * set the maxmiplevel if we're relying on the partial
509 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
510 * (I.e., do not care about cond_np2 here, just look for
511 * GL_TEXTURE_RECTANGLE_ARB.) */
512 if (target != GL_TEXTURE_RECTANGLE_ARB)
514 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
515 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
516 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
519 if (target == GL_TEXTURE_CUBE_MAP_ARB)
521 /* Cubemaps are always set to clamp, regardless of the sampler state. */
522 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
523 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
524 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
527 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
529 /* Conditinal non power of two textures use a different clamping
530 * default. If we're using the GL_WINE_normalized_texrect partial
531 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
532 * has the address mode set to repeat - something that prevents us
533 * from hitting the accelerated codepath. Thus manually set the GL
534 * state. The same applies to filtering. Even if the texture has only
535 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
536 * fallback on macos. */
537 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
538 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
539 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
540 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
541 checkGLcall("glTexParameteri");
542 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
543 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
544 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
545 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
546 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
549 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
551 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
552 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
555 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
557 static const GLenum swizzle_source[] =
559 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
560 GL_ONE, /* CHANNEL_SOURCE_ONE */
561 GL_RED, /* CHANNEL_SOURCE_X */
562 GL_GREEN, /* CHANNEL_SOURCE_Y */
563 GL_BLUE, /* CHANNEL_SOURCE_Z */
564 GL_ALPHA, /* CHANNEL_SOURCE_W */
566 struct
568 GLint x, y, z, w;
570 swizzle;
572 swizzle.x = swizzle_source[fixup.x_source];
573 swizzle.y = swizzle_source[fixup.y_source];
574 swizzle.z = swizzle_source[fixup.z_source];
575 swizzle.w = swizzle_source[fixup.w_source];
576 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
577 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
581 /* Context activation is done by the caller. */
582 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
583 struct wined3d_context *context, BOOL srgb)
585 DWORD active_sampler;
587 /* We don't need a specific texture unit, but after binding the texture
588 * the current unit is dirty. Read the unit back instead of switching to
589 * 0, this avoids messing around with the state manager's GL states. The
590 * current texture unit should always be a valid one.
592 * To be more specific, this is tricky because we can implicitly be
593 * called from sampler() in state.c. This means we can't touch anything
594 * other than whatever happens to be the currently active texture, or we
595 * would risk marking already applied sampler states dirty again. */
596 active_sampler = context->rev_tex_unit_map[context->active_texture];
597 if (active_sampler != WINED3D_UNMAPPED_STAGE)
598 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
599 /* FIXME: Ideally we'd only do this when touching a binding that's used by
600 * a shader. */
601 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
603 wined3d_texture_bind(texture, context, srgb);
606 /* Context activation is done by the caller (state handler). */
607 /* This function relies on the correct texture being bound and loaded. */
608 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
609 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
611 const struct wined3d_gl_info *gl_info = context->gl_info;
612 GLenum target = texture->target;
613 struct gl_texture *gl_tex;
614 DWORD state;
616 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
618 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
620 state = sampler_desc->address_u;
621 if (state != gl_tex->sampler_desc.address_u)
623 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
624 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
625 gl_tex->sampler_desc.address_u = state;
628 state = sampler_desc->address_v;
629 if (state != gl_tex->sampler_desc.address_v)
631 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
632 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
633 gl_tex->sampler_desc.address_v = state;
636 state = sampler_desc->address_w;
637 if (state != gl_tex->sampler_desc.address_w)
639 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
640 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
641 gl_tex->sampler_desc.address_w = state;
644 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
645 sizeof(gl_tex->sampler_desc.border_color)))
647 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
648 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
649 sizeof(gl_tex->sampler_desc.border_color));
652 state = sampler_desc->mag_filter;
653 if (state != gl_tex->sampler_desc.mag_filter)
655 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
656 gl_tex->sampler_desc.mag_filter = state;
659 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
660 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
662 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
663 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
664 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
665 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
668 state = sampler_desc->max_anisotropy;
669 if (state != gl_tex->sampler_desc.max_anisotropy)
671 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
672 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
673 else
674 WARN("Anisotropic filtering not supported.\n");
675 gl_tex->sampler_desc.max_anisotropy = state;
678 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
679 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
680 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
682 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
683 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
684 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
687 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
689 if (sampler_desc->compare)
690 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
691 else
692 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
693 gl_tex->sampler_desc.compare = sampler_desc->compare;
696 checkGLcall("Texture parameter application");
698 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
700 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
701 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
702 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
706 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
708 ULONG refcount;
710 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
712 if (texture->swapchain)
713 return wined3d_swapchain_incref(texture->swapchain);
715 refcount = InterlockedIncrement(&texture->resource.ref);
716 TRACE("%p increasing refcount to %u.\n", texture, refcount);
718 return refcount;
721 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
723 ULONG refcount;
725 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
727 if (texture->swapchain)
728 return wined3d_swapchain_decref(texture->swapchain);
730 refcount = InterlockedDecrement(&texture->resource.ref);
731 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
733 if (!refcount)
735 wined3d_texture_cleanup(texture);
736 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
737 HeapFree(GetProcessHeap(), 0, texture);
740 return refcount;
743 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
745 TRACE("texture %p.\n", texture);
747 return &texture->resource;
750 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
752 return c1->color_space_low_value == c2->color_space_low_value
753 && c1->color_space_high_value == c2->color_space_high_value;
756 /* Context activation is done by the caller */
757 void wined3d_texture_load(struct wined3d_texture *texture,
758 struct wined3d_context *context, BOOL srgb)
760 UINT sub_count = texture->level_count * texture->layer_count;
761 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
762 DWORD flag;
763 UINT i;
765 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
767 if (!needs_separate_srgb_gl_texture(context))
768 srgb = FALSE;
770 if (srgb)
771 flag = WINED3D_TEXTURE_SRGB_VALID;
772 else
773 flag = WINED3D_TEXTURE_RGB_VALID;
775 if (!d3d_info->shader_color_key
776 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
777 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
778 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
779 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
781 unsigned int sub_count = texture->level_count * texture->layer_count;
782 unsigned int i;
784 TRACE("Reloading because of color key value change.\n");
785 for (i = 0; i < sub_count; i++)
787 if (!texture->texture_ops->texture_load_location(texture, i, context, texture->resource.map_binding))
788 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
789 else
790 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
793 texture->async.gl_color_key = texture->async.src_blt_color_key;
796 if (texture->flags & flag)
798 TRACE("Texture %p not dirty, nothing to do.\n", texture);
799 return;
802 /* Reload the surfaces if the texture is marked dirty. */
803 for (i = 0; i < sub_count; ++i)
805 if (!texture->texture_ops->texture_load_location(texture, i, context,
806 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
807 ERR("Failed to load location (srgb %#x).\n", srgb);
809 texture->flags |= flag;
812 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
814 struct wined3d_context *context;
815 context = context_acquire(texture->resource.device, NULL);
816 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
817 context_release(context);
820 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
822 TRACE("texture %p.\n", texture);
824 return texture->resource.parent;
827 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
828 unsigned int level, const struct wined3d_box *box)
830 if (box->left >= box->right
831 || box->top >= box->bottom
832 || box->front >= box->back)
833 return FALSE;
835 if (box->right > wined3d_texture_get_level_width(texture, level)
836 || box->bottom > wined3d_texture_get_level_height(texture, level)
837 || box->back > wined3d_texture_get_level_depth(texture, level))
838 return FALSE;
840 return TRUE;
843 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
844 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
846 const struct wined3d_resource *resource = &texture->resource;
847 unsigned int width = wined3d_texture_get_level_width(texture, level);
848 unsigned int height = wined3d_texture_get_level_height(texture, level);
850 if (texture->row_pitch)
852 *row_pitch = texture->row_pitch;
853 *slice_pitch = texture->slice_pitch;
854 return;
857 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
858 width, height, row_pitch, slice_pitch);
861 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
863 DWORD old = texture->lod;
865 TRACE("texture %p, lod %u.\n", texture, lod);
867 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
868 * textures. The call always returns 0, and GetLOD always returns 0. */
869 if (texture->resource.pool != WINED3D_POOL_MANAGED)
871 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
872 return 0;
875 if (lod >= texture->level_count)
876 lod = texture->level_count - 1;
878 if (texture->lod != lod)
880 texture->lod = lod;
882 texture->texture_rgb.base_level = ~0u;
883 texture->texture_srgb.base_level = ~0u;
884 if (texture->resource.bind_count)
885 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
888 return old;
891 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
893 TRACE("texture %p, returning %u.\n", texture, texture->lod);
895 return texture->lod;
898 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
900 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
902 return texture->level_count;
905 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
906 enum wined3d_texture_filter_type filter_type)
908 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
910 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
912 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
913 return WINED3DERR_INVALIDCALL;
916 texture->filter_type = filter_type;
918 return WINED3D_OK;
921 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
923 TRACE("texture %p.\n", texture);
925 return texture->filter_type;
928 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
929 DWORD flags, const struct wined3d_color_key *color_key)
931 struct wined3d_device *device = texture->resource.device;
932 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
933 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
935 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
937 if (flags & ~all_flags)
939 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
940 return WINED3DERR_INVALIDCALL;
943 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
945 return WINED3D_OK;
948 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
949 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
950 UINT multisample_quality, void *mem, UINT pitch)
952 struct wined3d_device *device = texture->resource.device;
953 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
954 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
955 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
956 struct wined3d_texture_sub_resource *sub_resource;
957 struct wined3d_surface *surface;
958 DWORD valid_location = 0;
959 BOOL create_dib = FALSE;
961 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
962 "mem %p, pitch %u.\n",
963 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
965 if (!resource_size)
966 return WINED3DERR_INVALIDCALL;
968 if (texture->level_count * texture->layer_count > 1)
970 WARN("Texture has multiple sub-resources, not supported.\n");
971 return WINED3DERR_INVALIDCALL;
974 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
976 WARN("Not supported on 3D textures.\n");
977 return WINED3DERR_INVALIDCALL;
980 if (texture->resource.map_count)
982 WARN("Texture is mapped.\n");
983 return WINED3DERR_INVALIDCALL;
986 /* We have no way of supporting a pitch that is not a multiple of the pixel
987 * byte width short of uploading the texture row-by-row.
988 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
989 * for user-memory textures (it always expects packed data) while DirectDraw
990 * requires a 4-byte aligned pitch and doesn't support texture formats
991 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
992 * This check is here to verify that the assumption holds. */
993 if (pitch % texture->resource.format->byte_count)
995 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
996 return WINED3DERR_INVALIDCALL;
999 if (device->d3d_initialized)
1000 texture->resource.resource_ops->resource_unload(&texture->resource);
1002 sub_resource = &texture->sub_resources[0];
1003 surface = sub_resource->u.surface;
1004 if (surface->dc)
1006 wined3d_surface_destroy_dc(surface);
1007 create_dib = TRUE;
1010 wined3d_resource_free_sysmem(&texture->resource);
1012 if ((texture->row_pitch = pitch))
1013 texture->slice_pitch = height * pitch;
1014 else
1015 /* User memory surfaces don't have the regular surface alignment. */
1016 wined3d_format_calculate_pitch(format, 1, width, height,
1017 &texture->row_pitch, &texture->slice_pitch);
1019 texture->resource.format = format;
1020 texture->resource.multisample_type = multisample_type;
1021 texture->resource.multisample_quality = multisample_quality;
1022 texture->resource.width = width;
1023 texture->resource.height = height;
1024 texture->resource.size = texture->slice_pitch;
1026 sub_resource->resource->format = format;
1027 sub_resource->resource->multisample_type = multisample_type;
1028 sub_resource->resource->multisample_quality = multisample_quality;
1029 sub_resource->resource->width = width;
1030 sub_resource->resource->height = height;
1031 sub_resource->size = texture->slice_pitch;
1032 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1034 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1035 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1037 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1038 texture->pow2_width = texture->pow2_height = 1;
1039 while (texture->pow2_width < width)
1040 texture->pow2_width <<= 1;
1041 while (texture->pow2_height < height)
1042 texture->pow2_height <<= 1;
1044 else
1046 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1047 texture->pow2_width = width;
1048 texture->pow2_height = height;
1051 if ((texture->user_memory = mem))
1053 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1054 valid_location = WINED3D_LOCATION_USER_MEMORY;
1056 else
1058 wined3d_surface_prepare(surface, NULL, WINED3D_LOCATION_SYSMEM);
1059 valid_location = WINED3D_LOCATION_SYSMEM;
1062 /* The format might be changed to a format that needs conversion.
1063 * If the surface didn't use PBOs previously but could now, don't
1064 * change it - whatever made us not use PBOs might come back, e.g.
1065 * color keys. */
1066 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1067 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1069 wined3d_texture_validate_location(texture, 0, valid_location);
1070 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1072 if (create_dib)
1073 wined3d_surface_create_dc(surface);
1075 return WINED3D_OK;
1078 /* Context activation is done by the caller. */
1079 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1080 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1082 struct wined3d_texture_sub_resource *sub_resource;
1084 sub_resource = &texture->sub_resources[sub_resource_idx];
1085 if (sub_resource->buffer_object)
1086 return;
1088 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1089 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1090 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1091 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1092 checkGLcall("Create buffer object");
1094 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1095 sub_resource->buffer_object, texture, sub_resource_idx);
1098 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1100 unsigned int sub_count = texture->level_count * texture->layer_count;
1101 unsigned int i;
1103 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1104 | WINED3D_TEXTURE_CONVERTED);
1105 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1106 for (i = 0; i < sub_count; ++i)
1108 wined3d_texture_invalidate_location(texture, i,
1109 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1113 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1115 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1116 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1118 if (!d3d_info->shader_color_key
1119 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1120 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1122 wined3d_texture_force_reload(texture);
1124 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1125 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1128 if (texture->flags & alloc_flag)
1129 return;
1131 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1132 texture->flags |= alloc_flag;
1135 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1136 const struct wined3d_gl_info *gl_info, BOOL multisample)
1138 const struct wined3d_format *format = texture->resource.format;
1140 if (multisample)
1142 DWORD samples;
1144 if (texture->rb_multisample)
1145 return;
1147 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1148 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1149 * be mapped to GL_NV_framebuffer_multisample_coverage.
1151 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1152 * (EQAA), but it does not have an equivalent OpenGL extension. */
1154 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1155 * levels as the count of advertised multisample types for the texture
1156 * format. */
1157 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1159 unsigned int i, count = 0;
1161 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1163 if (format->multisample_types & 1u << i)
1165 if (texture->resource.multisample_quality == count++)
1166 break;
1169 samples = i + 1;
1171 else
1173 samples = texture->resource.multisample_type;
1176 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1177 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1178 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1179 format->glInternal, texture->resource.width, texture->resource.height);
1180 checkGLcall("glRenderbufferStorageMultisample()");
1181 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1183 else
1185 if (texture->rb_resolved)
1186 return;
1188 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1189 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1190 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1191 texture->resource.width, texture->resource.height);
1192 checkGLcall("glRenderbufferStorage()");
1193 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1197 /* Context activation is done by the caller. Context may be NULL in
1198 * WINED3D_NO3D mode. */
1199 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1200 struct wined3d_context *context, DWORD location)
1202 switch (location)
1204 case WINED3D_LOCATION_SYSMEM:
1205 if (texture->resource.heap_memory)
1206 return TRUE;
1208 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1210 ERR("Failed to allocate system memory.\n");
1211 return FALSE;
1213 return TRUE;
1215 case WINED3D_LOCATION_USER_MEMORY:
1216 if (!texture->user_memory)
1217 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1218 return TRUE;
1220 case WINED3D_LOCATION_BUFFER:
1221 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1222 return TRUE;
1224 case WINED3D_LOCATION_TEXTURE_RGB:
1225 wined3d_texture_prepare_texture(texture, context, FALSE);
1226 return TRUE;
1228 case WINED3D_LOCATION_TEXTURE_SRGB:
1229 wined3d_texture_prepare_texture(texture, context, TRUE);
1230 return TRUE;
1232 case WINED3D_LOCATION_DRAWABLE:
1233 if (!texture->swapchain)
1234 ERR("Texture %p does not have a drawable.\n", texture);
1235 return TRUE;
1237 case WINED3D_LOCATION_RB_MULTISAMPLE:
1238 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1239 return TRUE;
1241 case WINED3D_LOCATION_RB_RESOLVED:
1242 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1243 return TRUE;
1245 default:
1246 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1247 return FALSE;
1251 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1253 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1254 FIXME("texture %p stub!\n", texture);
1257 struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1258 unsigned int sub_resource_idx)
1260 UINT sub_count = texture->level_count * texture->layer_count;
1262 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1264 if (sub_resource_idx >= sub_count)
1266 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1267 return NULL;
1270 return &texture->sub_resources[sub_resource_idx];
1273 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1274 UINT layer, const struct wined3d_box *dirty_region)
1276 struct wined3d_context *context;
1277 unsigned int sub_resource_idx;
1279 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1281 if (layer >= texture->layer_count)
1283 WARN("Invalid layer %u specified.\n", layer);
1284 return WINED3DERR_INVALIDCALL;
1286 sub_resource_idx = layer * texture->level_count;
1288 if (dirty_region)
1289 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1291 context = context_acquire(texture->resource.device, NULL);
1292 if (!texture->texture_ops->texture_load_location(texture, sub_resource_idx,
1293 context, texture->resource.map_binding))
1295 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1296 context_release(context);
1297 return E_OUTOFMEMORY;
1299 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1300 context_release(context);
1302 return WINED3D_OK;
1305 static HRESULT wined3d_texture_upload_data(struct wined3d_texture *texture,
1306 const struct wined3d_sub_resource_data *data)
1308 unsigned int sub_count = texture->level_count * texture->layer_count;
1309 struct wined3d_context *context;
1310 unsigned int i;
1312 for (i = 0; i < sub_count; ++i)
1314 if (!data[i].data)
1316 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
1317 return E_INVALIDARG;
1321 context = context_acquire(texture->resource.device, NULL);
1323 wined3d_texture_prepare_texture(texture, context, FALSE);
1324 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
1326 for (i = 0; i < sub_count; ++i)
1328 texture->texture_ops->texture_upload_data(texture, i, context, &data[i]);
1329 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_TEXTURE_RGB);
1330 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
1333 context_release(context);
1335 return WINED3D_OK;
1338 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1339 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1341 static const POINT dst_point = {0, 0};
1342 struct wined3d_const_bo_address addr;
1343 unsigned int texture_level;
1344 RECT src_rect;
1346 texture_level = sub_resource_idx % texture->level_count;
1347 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(texture, texture_level),
1348 wined3d_texture_get_level_height(texture, texture_level));
1350 addr.buffer_object = 0;
1351 addr.addr = data->data;
1353 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1354 texture->resource.format, &src_rect, data->row_pitch, &dst_point, FALSE, &addr);
1357 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1358 struct wined3d_context *context, DWORD location)
1360 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1363 /* Context activation is done by the caller. */
1364 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1366 UINT sub_count = texture->level_count * texture->layer_count;
1367 const struct wined3d_format *format = texture->resource.format;
1368 const struct wined3d_gl_info *gl_info = context->gl_info;
1369 const struct wined3d_color_key_conversion *conversion;
1370 GLenum internal;
1371 UINT i;
1373 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1375 if (format->convert)
1377 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1379 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1381 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1382 format = wined3d_get_format(gl_info, conversion->dst_format);
1383 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1386 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1388 if (srgb)
1389 internal = format->glGammaInternal;
1390 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1391 && wined3d_resource_is_offscreen(&texture->resource))
1392 internal = format->rtInternal;
1393 else
1394 internal = format->glInternal;
1396 if (!internal)
1397 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1399 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1401 for (i = 0; i < sub_count; ++i)
1403 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
1404 GLsizei width, height;
1406 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
1407 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
1408 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
1410 height *= format->height_scale.numerator;
1411 height /= format->height_scale.denominator;
1414 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
1415 surface, surface->texture_target, surface->texture_level, width, height);
1417 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
1418 internal, width, height, 0, format->glFormat, format->glType, NULL);
1419 checkGLcall("glTexImage2D");
1423 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1425 unsigned int sub_count = texture->level_count * texture->layer_count;
1426 struct wined3d_surface *surface;
1427 unsigned int i;
1429 for (i = 0; i < sub_count; ++i)
1431 if ((surface = texture->sub_resources[i].u.surface))
1433 TRACE("surface %p.\n", surface);
1435 wined3d_surface_cleanup(surface);
1436 surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
1439 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1442 static const struct wined3d_texture_ops texture2d_ops =
1444 texture2d_upload_data,
1445 texture2d_load_location,
1446 texture2d_prepare_texture,
1447 texture2d_cleanup_sub_resources,
1450 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1452 return texture_from_resource(resource);
1455 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1457 return wined3d_texture_incref(texture_from_resource(resource));
1460 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1462 return wined3d_texture_decref(texture_from_resource(resource));
1465 static void wined3d_texture_unload(struct wined3d_resource *resource)
1467 struct wined3d_texture *texture = texture_from_resource(resource);
1468 UINT sub_count = texture->level_count * texture->layer_count;
1469 struct wined3d_device *device = resource->device;
1470 const struct wined3d_gl_info *gl_info;
1471 struct wined3d_context *context;
1472 UINT i;
1474 TRACE("texture %p.\n", texture);
1476 context = context_acquire(device, NULL);
1477 gl_info = context->gl_info;
1479 for (i = 0; i < sub_count; ++i)
1481 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1483 if (resource->pool != WINED3D_POOL_DEFAULT
1484 && texture->texture_ops->texture_load_location(texture, i, context, resource->map_binding))
1486 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1488 else
1490 /* We should only get here on device reset/teardown for implicit
1491 * resources. */
1492 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1493 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1494 resource, i, debug_d3dpool(resource->pool));
1495 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1496 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1499 if (sub_resource->buffer_object)
1500 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1502 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1504 struct wined3d_surface *surface = sub_resource->u.surface;
1505 struct wined3d_renderbuffer_entry *entry, *entry2;
1507 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1509 context_gl_resource_released(device, entry->id, TRUE);
1510 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1511 list_remove(&entry->entry);
1512 HeapFree(GetProcessHeap(), 0, entry);
1514 list_init(&surface->renderbuffers);
1515 surface->current_renderbuffer = NULL;
1518 resource_unload(sub_resource->resource);
1521 context_release(context);
1523 wined3d_texture_force_reload(texture);
1524 wined3d_texture_unload_gl_texture(texture);
1527 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1528 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1530 const struct wined3d_format *format = resource->format;
1531 struct wined3d_texture_sub_resource *sub_resource;
1532 struct wined3d_device *device = resource->device;
1533 unsigned int fmt_flags = resource->format_flags;
1534 const struct wined3d_gl_info *gl_info = NULL;
1535 struct wined3d_context *context = NULL;
1536 struct wined3d_texture *texture;
1537 struct wined3d_bo_address data;
1538 unsigned int texture_level;
1539 BYTE *base_memory;
1540 BOOL ret;
1542 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1543 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1545 texture = texture_from_resource(resource);
1546 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1547 return E_INVALIDARG;
1549 texture_level = sub_resource_idx % texture->level_count;
1550 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1552 WARN("Map box is invalid.\n");
1553 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1554 return WINED3DERR_INVALIDCALL;
1557 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1558 && !wined3d_texture_check_block_align(texture, texture_level, box))
1560 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1561 debug_box(box), format->block_width, format->block_height);
1562 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1563 return WINED3DERR_INVALIDCALL;
1566 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1568 WARN("Trying to map unmappable texture.\n");
1569 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1570 return WINED3DERR_INVALIDCALL;
1573 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1575 WARN("DC is in use.\n");
1576 return WINED3DERR_INVALIDCALL;
1579 if (sub_resource->map_count)
1581 WARN("Sub-resource is already mapped.\n");
1582 return WINED3DERR_INVALIDCALL;
1585 flags = wined3d_resource_sanitize_map_flags(resource, flags);
1587 if (device->d3d_initialized)
1589 context = context_acquire(device, NULL);
1590 gl_info = context->gl_info;
1593 if (flags & WINED3D_MAP_DISCARD)
1595 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1596 wined3d_debug_location(texture->resource.map_binding));
1597 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1598 context, texture->resource.map_binding)))
1599 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1601 else
1603 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1604 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1605 ret = texture->texture_ops->texture_load_location(texture,
1606 sub_resource_idx, context, texture->resource.map_binding);
1609 if (!ret)
1611 ERR("Failed to prepare location.\n");
1612 context_release(context);
1613 return E_OUTOFMEMORY;
1616 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1617 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1619 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1620 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1621 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1622 TRACE("Base memory pointer %p.\n", base_memory);
1624 if (context)
1625 context_release(context);
1627 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1629 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1630 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1632 else
1634 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1637 if (!box)
1639 map_desc->data = base_memory;
1641 else
1643 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1645 /* Compressed textures are block based, so calculate the offset of
1646 * the block that contains the top-left pixel of the mapped box. */
1647 map_desc->data = base_memory
1648 + (box->front * map_desc->slice_pitch)
1649 + ((box->top / format->block_height) * map_desc->row_pitch)
1650 + ((box->left / format->block_width) * format->block_byte_count);
1652 else
1654 map_desc->data = base_memory
1655 + (box->front * map_desc->slice_pitch)
1656 + (box->top * map_desc->row_pitch)
1657 + (box->left * format->byte_count);
1661 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1663 RECT *r = &texture->swapchain->front_buffer_update;
1665 if (!box)
1666 SetRect(r, 0, 0, resource->width, resource->height);
1667 else
1668 SetRect(r, box->left, box->top, box->right, box->bottom);
1669 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1672 ++resource->map_count;
1673 ++sub_resource->map_count;
1675 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1676 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1678 return WINED3D_OK;
1681 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1683 struct wined3d_texture_sub_resource *sub_resource;
1684 struct wined3d_device *device = resource->device;
1685 const struct wined3d_gl_info *gl_info = NULL;
1686 struct wined3d_context *context = NULL;
1687 struct wined3d_texture *texture;
1688 struct wined3d_bo_address data;
1690 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1692 texture = texture_from_resource(resource);
1693 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1694 return E_INVALIDARG;
1696 if (!sub_resource->map_count)
1698 WARN("Trying to unmap unmapped sub-resource.\n");
1699 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1700 return WINED3D_OK;
1701 return WINEDDERR_NOTLOCKED;
1704 if (device->d3d_initialized)
1706 context = context_acquire(device, NULL);
1707 gl_info = context->gl_info;
1710 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1711 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1713 if (context)
1714 context_release(context);
1716 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1718 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1719 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1721 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1723 FIXME("Depth / stencil buffer locking is not implemented.\n");
1726 --sub_resource->map_count;
1727 if (!--resource->map_count && texture->update_map_binding)
1728 wined3d_texture_update_map_binding(texture);
1730 return WINED3D_OK;
1733 static const struct wined3d_resource_ops texture_resource_ops =
1735 texture_resource_incref,
1736 texture_resource_decref,
1737 wined3d_texture_unload,
1738 texture_resource_sub_resource_map,
1739 texture_resource_sub_resource_unmap,
1742 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1743 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1744 void *parent, const struct wined3d_parent_ops *parent_ops)
1746 struct wined3d_device_parent *device_parent = device->device_parent;
1747 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1748 struct wined3d_resource_desc surface_desc;
1749 struct wined3d_surface *surfaces;
1750 UINT pow2_width, pow2_height;
1751 unsigned int i, j;
1752 HRESULT hr;
1754 /* TODO: It should only be possible to create textures for formats
1755 * that are reported as supported. */
1756 if (WINED3DFMT_UNKNOWN >= desc->format)
1758 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1759 return WINED3DERR_INVALIDCALL;
1762 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1763 FIXME("Trying to create a managed texture with dynamic usage.\n");
1764 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1765 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1766 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1767 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1768 FIXME("Trying to create a render target that isn't in the default pool.\n");
1770 pow2_width = desc->width;
1771 pow2_height = desc->height;
1772 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1773 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1775 /* level_count == 0 returns an error as well. */
1776 if (level_count != 1 || desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1778 if (desc->pool != WINED3D_POOL_SCRATCH)
1780 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1781 return WINED3DERR_INVALIDCALL;
1784 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1786 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1788 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1790 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1792 /* TODO: Add support for non-power-of-two compressed textures. */
1793 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1794 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1796 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1797 desc->width, desc->height);
1798 return WINED3DERR_NOTAVAILABLE;
1801 /* Find the nearest pow2 match. */
1802 pow2_width = pow2_height = 1;
1803 while (pow2_width < desc->width)
1804 pow2_width <<= 1;
1805 while (pow2_height < desc->height)
1806 pow2_height <<= 1;
1807 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1810 texture->pow2_width = pow2_width;
1811 texture->pow2_height = pow2_height;
1813 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1814 && (desc->usage & WINED3DUSAGE_TEXTURE))
1816 /* One of four options:
1817 * 1: Do the same as we do with NPOT and scale the texture. (Any
1818 * texture ops would require the texture to be scaled which is
1819 * potentially slow.)
1820 * 2: Set the texture to the maximum size (bad idea).
1821 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1822 * 4: Create the surface, but allow it to be used only for DirectDraw
1823 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1824 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1825 * the render target. */
1826 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1828 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1829 return WINED3DERR_NOTAVAILABLE;
1832 /* We should never use this surface in combination with OpenGL. */
1833 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1836 /* Calculate levels for mip mapping. */
1837 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1839 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1841 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1842 return WINED3DERR_INVALIDCALL;
1845 if (level_count != 1)
1847 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1848 return WINED3DERR_INVALIDCALL;
1852 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1853 flags, device, parent, parent_ops, &texture_resource_ops)))
1855 WARN("Failed to initialize texture, returning %#x.\n", hr);
1856 return hr;
1859 /* Precalculated scaling for 'faked' non power of two texture coords. */
1860 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1862 texture->pow2_matrix[0] = (float)desc->width;
1863 texture->pow2_matrix[5] = (float)desc->height;
1864 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1865 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1867 else
1869 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1871 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1872 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1873 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1875 else
1877 texture->pow2_matrix[0] = 1.0f;
1878 texture->pow2_matrix[5] = 1.0f;
1880 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1881 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1882 else
1883 texture->target = GL_TEXTURE_2D;
1885 texture->pow2_matrix[10] = 1.0f;
1886 texture->pow2_matrix[15] = 1.0f;
1887 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1889 if (wined3d_texture_use_pbo(texture, gl_info))
1890 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
1892 if (!(surfaces = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*surfaces) * level_count * layer_count)))
1894 wined3d_texture_cleanup(texture);
1895 return E_OUTOFMEMORY;
1898 /* Generate all the surfaces. */
1899 surface_desc = *desc;
1900 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1901 for (i = 0; i < texture->level_count; ++i)
1903 for (j = 0; j < texture->layer_count; ++j)
1905 static const GLenum cube_targets[6] =
1907 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1908 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1909 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1910 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1911 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1912 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1914 GLenum target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
1915 struct wined3d_texture_sub_resource *sub_resource;
1916 unsigned int idx = j * texture->level_count + i;
1917 struct wined3d_surface *surface;
1919 surface = &surfaces[idx];
1920 if (FAILED(hr = wined3d_surface_init(surface, texture, &surface_desc, target, i, j)))
1922 WARN("Failed to initialize surface, returning %#x.\n", hr);
1923 wined3d_texture_cleanup(texture);
1924 if (!idx)
1925 HeapFree(GetProcessHeap(), 0, surfaces);
1926 return hr;
1929 sub_resource = &texture->sub_resources[idx];
1930 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1931 sub_resource->resource = &surface->resource;
1932 sub_resource->u.surface = surface;
1933 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
1935 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
1936 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
1939 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
1940 texture, idx, &parent, &parent_ops)))
1942 WARN("Failed to create surface parent, hr %#x.\n", hr);
1943 wined3d_surface_cleanup(surface);
1944 wined3d_texture_cleanup(texture);
1945 return hr;
1948 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
1950 surface->resource.parent = parent;
1951 surface->resource.parent_ops = parent_ops;
1952 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
1954 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
1955 && FAILED(hr = wined3d_surface_create_dc(surface)))
1957 wined3d_texture_cleanup(texture);
1958 return hr;
1961 /* Calculate the next mipmap level. */
1962 surface_desc.width = max(1, surface_desc.width >> 1);
1963 surface_desc.height = max(1, surface_desc.height >> 1);
1966 return WINED3D_OK;
1969 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1970 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1972 struct wined3d_const_bo_address addr;
1973 unsigned int row_pitch, slice_pitch;
1975 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1976 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1977 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1979 addr.buffer_object = 0;
1980 addr.addr = data->data;
1982 wined3d_volume_upload_data(texture->sub_resources[sub_resource_idx].u.volume, context, &addr);
1985 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1986 struct wined3d_context *context, DWORD location)
1988 return wined3d_volume_load_location(texture->sub_resources[sub_resource_idx].u.volume, context, location);
1991 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1993 unsigned int sub_count = texture->level_count * texture->layer_count;
1994 const struct wined3d_format *format = texture->resource.format;
1995 const struct wined3d_gl_info *gl_info = context->gl_info;
1996 unsigned int i;
1998 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2000 for (i = 0; i < sub_count; ++i)
2002 struct wined3d_volume *volume = texture->sub_resources[i].u.volume;
2004 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
2005 srgb ? format->glGammaInternal : format->glInternal,
2006 wined3d_texture_get_level_width(texture, volume->texture_level),
2007 wined3d_texture_get_level_height(texture, volume->texture_level),
2008 wined3d_texture_get_level_depth(texture, volume->texture_level),
2009 0, format->glFormat, format->glType, NULL));
2010 checkGLcall("glTexImage3D");
2014 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2016 unsigned int sub_count = texture->level_count * texture->layer_count;
2017 struct wined3d_volume *volume;
2018 unsigned int i;
2020 for (i = 0; i < sub_count; ++i)
2022 if ((volume = texture->sub_resources[i].u.volume))
2024 TRACE("volume %p.\n", volume);
2026 wined3d_volume_cleanup(volume);
2027 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
2030 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.volume);
2033 static const struct wined3d_texture_ops texture3d_ops =
2035 texture3d_upload_data,
2036 texture3d_load_location,
2037 texture3d_prepare_texture,
2038 texture3d_cleanup_sub_resources,
2041 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2042 unsigned int level, const struct wined3d_box *box)
2044 const struct wined3d_format *format = texture->resource.format;
2045 unsigned int height = wined3d_texture_get_level_height(texture, level);
2046 unsigned int width = wined3d_texture_get_level_width(texture, level);
2047 unsigned int width_mask, height_mask;
2049 if ((box->left >= box->right)
2050 || (box->top >= box->bottom)
2051 || (box->right > width)
2052 || (box->bottom > height))
2053 return FALSE;
2055 /* This assumes power of two block sizes, but NPOT block sizes would be
2056 * silly anyway.
2058 * This also assumes that the format's block depth is 1. */
2059 width_mask = format->block_width - 1;
2060 height_mask = format->block_height - 1;
2062 if ((box->left & width_mask) || (box->top & height_mask)
2063 || (box->right & width_mask && box->right != width)
2064 || (box->bottom & height_mask && box->bottom != height))
2065 return FALSE;
2067 return TRUE;
2070 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2071 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
2073 struct wined3d_device_parent *device_parent = device->device_parent;
2074 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2075 struct wined3d_resource_desc volume_desc;
2076 struct wined3d_volume *volumes;
2077 unsigned int i;
2078 HRESULT hr;
2080 /* TODO: It should only be possible to create textures for formats
2081 * that are reported as supported. */
2082 if (WINED3DFMT_UNKNOWN >= desc->format)
2084 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2085 return WINED3DERR_INVALIDCALL;
2088 if (!gl_info->supported[EXT_TEXTURE3D])
2090 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2091 return WINED3DERR_INVALIDCALL;
2094 /* Calculate levels for mip mapping. */
2095 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2097 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2099 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2100 return WINED3DERR_INVALIDCALL;
2103 if (levels != 1)
2105 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2106 return WINED3DERR_INVALIDCALL;
2110 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2111 || desc->pool == WINED3D_POOL_SCRATCH))
2113 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2114 return WINED3DERR_INVALIDCALL;
2117 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2119 UINT pow2_w, pow2_h, pow2_d;
2120 pow2_w = 1;
2121 while (pow2_w < desc->width)
2122 pow2_w <<= 1;
2123 pow2_h = 1;
2124 while (pow2_h < desc->height)
2125 pow2_h <<= 1;
2126 pow2_d = 1;
2127 while (pow2_d < desc->depth)
2128 pow2_d <<= 1;
2130 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2132 if (desc->pool == WINED3D_POOL_SCRATCH)
2134 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2136 else
2138 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2139 desc->width, desc->height, desc->depth);
2140 return WINED3DERR_INVALIDCALL;
2145 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
2146 0, device, parent, parent_ops, &texture_resource_ops)))
2148 WARN("Failed to initialize texture, returning %#x.\n", hr);
2149 return hr;
2152 texture->pow2_matrix[0] = 1.0f;
2153 texture->pow2_matrix[5] = 1.0f;
2154 texture->pow2_matrix[10] = 1.0f;
2155 texture->pow2_matrix[15] = 1.0f;
2156 texture->target = GL_TEXTURE_3D;
2158 if (wined3d_texture_use_pbo(texture, gl_info))
2160 wined3d_resource_free_sysmem(&texture->resource);
2161 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2164 if (!(volumes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volumes) * levels)))
2166 wined3d_texture_cleanup(texture);
2167 return E_OUTOFMEMORY;
2170 /* Generate all the surfaces. */
2171 volume_desc = *desc;
2172 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
2173 for (i = 0; i < texture->level_count; ++i)
2175 struct wined3d_texture_sub_resource *sub_resource;
2176 struct wined3d_volume *volume;
2178 volume = &volumes[i];
2179 if (FAILED(hr = wined3d_volume_init(volume, texture, &volume_desc, i)))
2181 WARN("Failed to initialize volume, returning %#x.\n", hr);
2182 wined3d_texture_cleanup(texture);
2183 if (!i)
2184 HeapFree(GetProcessHeap(), 0, volumes);
2185 return hr;
2188 sub_resource = &texture->sub_resources[i];
2189 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2190 sub_resource->resource = &volume->resource;
2191 sub_resource->u.volume = volume;
2193 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2194 texture, i, &parent, &parent_ops)))
2196 WARN("Failed to create volume parent, hr %#x.\n", hr);
2197 wined3d_volume_cleanup(volume);
2198 wined3d_texture_cleanup(texture);
2199 return hr;
2202 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2204 volume->resource.parent = parent;
2205 volume->resource.parent_ops = parent_ops;
2206 TRACE("Created volume level %u @ %p.\n", i, volume);
2208 /* Calculate the next mipmap level. */
2209 volume_desc.width = max(1, volume_desc.width >> 1);
2210 volume_desc.height = max(1, volume_desc.height >> 1);
2211 volume_desc.depth = max(1, volume_desc.depth >> 1);
2214 return WINED3D_OK;
2217 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2218 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2219 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2221 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2223 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2224 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2225 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2226 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2228 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2229 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2230 return WINED3DERR_INVALIDCALL;
2232 if (src_texture)
2234 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2235 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2236 return WINED3DERR_INVALIDCALL;
2239 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2240 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2243 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2244 unsigned int sub_resource_idx, LONG *x, LONG *y)
2246 struct wined3d_surface *surface;
2248 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2250 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2251 || sub_resource_idx >= texture->level_count * texture->layer_count)
2253 WARN("Invalid sub-resource specified.\n");
2254 return WINEDDERR_NOTAOVERLAYSURFACE;
2257 surface = texture->sub_resources[sub_resource_idx].u.surface;
2258 if (!surface->overlay_dest)
2260 TRACE("Overlay not visible.\n");
2261 *x = 0;
2262 *y = 0;
2263 return WINEDDERR_OVERLAYNOTVISIBLE;
2266 *x = surface->overlay_destrect.left;
2267 *y = surface->overlay_destrect.top;
2269 TRACE("Returning position %d, %d.\n", *x, *y);
2271 return WINED3D_OK;
2274 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2275 unsigned int sub_resource_idx, LONG x, LONG y)
2277 struct wined3d_texture_sub_resource *sub_resource;
2278 struct wined3d_surface *surface;
2279 LONG w, h;
2281 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2283 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2284 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2286 WARN("Invalid sub-resource specified.\n");
2287 return WINEDDERR_NOTAOVERLAYSURFACE;
2290 surface = sub_resource->u.surface;
2291 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2292 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2293 surface->overlay_destrect.left = x;
2294 surface->overlay_destrect.top = y;
2295 surface->overlay_destrect.right = x + w;
2296 surface->overlay_destrect.bottom = y + h;
2298 return WINED3D_OK;
2301 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2302 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2303 const RECT *dst_rect, DWORD flags)
2305 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2306 struct wined3d_surface *surface, *dst_surface;
2308 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2309 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2310 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2311 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2313 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2314 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2316 WARN("Invalid sub-resource specified.\n");
2317 return WINEDDERR_NOTAOVERLAYSURFACE;
2320 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2321 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2323 WARN("Invalid destination sub-resource specified.\n");
2324 return WINED3DERR_INVALIDCALL;
2327 surface = sub_resource->u.surface;
2328 if (src_rect)
2329 surface->overlay_srcrect = *src_rect;
2330 else
2331 SetRect(&surface->overlay_srcrect, 0, 0,
2332 wined3d_texture_get_level_width(texture, surface->texture_level),
2333 wined3d_texture_get_level_height(texture, surface->texture_level));
2335 dst_surface = dst_sub_resource->u.surface;
2336 if (dst_rect)
2337 surface->overlay_destrect = *dst_rect;
2338 else
2339 SetRect(&surface->overlay_destrect, 0, 0,
2340 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2341 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2343 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2345 surface->overlay_dest = NULL;
2346 list_remove(&surface->overlay_entry);
2349 if (flags & WINEDDOVER_SHOW)
2351 if (surface->overlay_dest != dst_surface)
2353 surface->overlay_dest = dst_surface;
2354 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2357 else if (flags & WINEDDOVER_HIDE)
2359 /* Tests show that the rectangles are erased on hide. */
2360 SetRectEmpty(&surface->overlay_srcrect);
2361 SetRectEmpty(&surface->overlay_destrect);
2362 surface->overlay_dest = NULL;
2365 return WINED3D_OK;
2368 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2370 unsigned int sub_count = texture->level_count * texture->layer_count;
2372 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2374 if (sub_resource_idx >= sub_count)
2376 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2377 return NULL;
2380 return texture->sub_resources[sub_resource_idx].resource->parent;
2383 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2384 unsigned int sub_resource_idx, void *parent)
2386 unsigned int sub_count = texture->level_count * texture->layer_count;
2388 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2390 if (sub_resource_idx >= sub_count)
2392 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2393 return;
2396 texture->sub_resources[sub_resource_idx].resource->parent = parent;
2399 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2400 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2402 unsigned int sub_count = texture->level_count * texture->layer_count;
2403 const struct wined3d_resource *resource;
2404 unsigned int level_idx;
2406 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2408 if (sub_resource_idx >= sub_count)
2410 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2411 return WINED3DERR_INVALIDCALL;
2414 resource = &texture->resource;
2415 desc->format = resource->format->id;
2416 desc->multisample_type = resource->multisample_type;
2417 desc->multisample_quality = resource->multisample_quality;
2418 desc->usage = resource->usage;
2419 desc->pool = resource->pool;
2421 level_idx = sub_resource_idx % texture->level_count;
2422 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2423 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2424 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2425 desc->size = texture->sub_resources[sub_resource_idx].size;
2427 return WINED3D_OK;
2430 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2431 UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data, void *parent,
2432 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2434 unsigned int layer_count = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? 6 : 1;
2435 struct wined3d_texture *object;
2436 HRESULT hr;
2438 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2439 device, desc, level_count, flags, data, parent, parent_ops, texture);
2441 if (!level_count)
2443 WARN("Invalid level count.\n");
2444 return WINED3DERR_INVALIDCALL;
2447 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2449 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2451 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2452 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2454 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2455 desc->multisample_quality);
2456 return WINED3DERR_NOTAVAILABLE;
2458 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2459 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2460 || desc->multisample_quality))
2462 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2463 desc->multisample_quality);
2464 return WINED3DERR_NOTAVAILABLE;
2468 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2469 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2470 return E_OUTOFMEMORY;
2472 switch (desc->resource_type)
2474 case WINED3D_RTYPE_TEXTURE_2D:
2475 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2476 break;
2478 case WINED3D_RTYPE_TEXTURE_3D:
2479 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
2480 break;
2482 default:
2483 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2484 hr = WINED3DERR_INVALIDCALL;
2485 break;
2488 if (FAILED(hr))
2490 WARN("Failed to initialize texture, returning %#x.\n", hr);
2491 HeapFree(GetProcessHeap(), 0, object);
2492 return hr;
2495 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2496 * in this case. */
2497 if (data && FAILED(hr = wined3d_texture_upload_data(object, data)))
2499 wined3d_texture_cleanup(object);
2500 HeapFree(GetProcessHeap(), 0, object);
2501 return hr;
2504 TRACE("Created texture %p.\n", object);
2505 *texture = object;
2507 return WINED3D_OK;
2510 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2512 struct wined3d_device *device = texture->resource.device;
2513 struct wined3d_texture_sub_resource *sub_resource;
2514 struct wined3d_context *context = NULL;
2515 struct wined3d_surface *surface;
2516 HRESULT hr = WINED3D_OK;
2518 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2520 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2521 return WINED3DERR_INVALIDCALL;
2523 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2525 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2526 return WINED3DERR_INVALIDCALL;
2529 surface = sub_resource->u.surface;
2531 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2532 return WINED3DERR_INVALIDCALL;
2534 if (device->d3d_initialized)
2535 context = context_acquire(device, NULL);
2537 surface_load_location(surface, context, texture->resource.map_binding);
2538 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2540 if (!surface->dc)
2541 hr = wined3d_surface_create_dc(surface);
2542 if (context)
2543 context_release(context);
2544 if (FAILED(hr))
2545 return WINED3DERR_INVALIDCALL;
2547 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2548 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2549 ++texture->resource.map_count;
2550 ++sub_resource->map_count;
2552 *dc = surface->dc;
2553 TRACE("Returning dc %p.\n", *dc);
2555 return hr;
2558 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2560 struct wined3d_device *device = texture->resource.device;
2561 struct wined3d_texture_sub_resource *sub_resource;
2562 struct wined3d_surface *surface;
2564 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2566 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2567 return WINED3DERR_INVALIDCALL;
2569 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2571 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2572 return WINED3DERR_INVALIDCALL;
2575 surface = sub_resource->u.surface;
2577 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2578 return WINED3DERR_INVALIDCALL;
2580 if (surface->dc != dc)
2582 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2583 return WINED3DERR_INVALIDCALL;
2586 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2587 wined3d_surface_destroy_dc(surface);
2589 --sub_resource->map_count;
2590 if (!--texture->resource.map_count && texture->update_map_binding)
2591 wined3d_texture_update_map_binding(texture);
2592 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2593 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2595 return WINED3D_OK;