wined3d: Allocate system memory for complete textures.
[wine.git] / dlls / wined3d / texture.c
blob2d92c8313eb6780ab623bfe0c2d208f002be94fc
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 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1452 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
1455 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1457 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
1460 static void wined3d_texture_unload(struct wined3d_resource *resource)
1462 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1463 UINT sub_count = texture->level_count * texture->layer_count;
1464 struct wined3d_device *device = resource->device;
1465 const struct wined3d_gl_info *gl_info;
1466 struct wined3d_context *context;
1467 UINT i;
1469 TRACE("texture %p.\n", texture);
1471 context = context_acquire(device, NULL);
1472 gl_info = context->gl_info;
1474 for (i = 0; i < sub_count; ++i)
1476 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1478 if (resource->pool != WINED3D_POOL_DEFAULT
1479 && texture->texture_ops->texture_load_location(texture, i, context, resource->map_binding))
1481 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1483 else
1485 /* We should only get here on device reset/teardown for implicit
1486 * resources. */
1487 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1488 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1489 resource, i, debug_d3dpool(resource->pool));
1490 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1491 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1494 if (sub_resource->buffer_object)
1495 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1497 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1499 struct wined3d_surface *surface = sub_resource->u.surface;
1500 struct wined3d_renderbuffer_entry *entry, *entry2;
1502 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1504 context_gl_resource_released(device, entry->id, TRUE);
1505 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1506 list_remove(&entry->entry);
1507 HeapFree(GetProcessHeap(), 0, entry);
1509 list_init(&surface->renderbuffers);
1510 surface->current_renderbuffer = NULL;
1513 resource_unload(sub_resource->resource);
1516 context_release(context);
1518 wined3d_texture_force_reload(texture);
1519 wined3d_texture_unload_gl_texture(texture);
1522 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1523 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1525 const struct wined3d_format *format = resource->format;
1526 struct wined3d_texture_sub_resource *sub_resource;
1527 struct wined3d_device *device = resource->device;
1528 unsigned int fmt_flags = resource->format_flags;
1529 const struct wined3d_gl_info *gl_info = NULL;
1530 struct wined3d_context *context = NULL;
1531 struct wined3d_texture *texture;
1532 struct wined3d_bo_address data;
1533 unsigned int texture_level;
1534 BYTE *base_memory;
1535 BOOL ret;
1537 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1538 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1540 texture = wined3d_texture_from_resource(resource);
1541 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1542 return E_INVALIDARG;
1544 texture_level = sub_resource_idx % texture->level_count;
1545 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1547 WARN("Map box is invalid.\n");
1548 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1549 return WINED3DERR_INVALIDCALL;
1552 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1553 && !wined3d_texture_check_block_align(texture, texture_level, box))
1555 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1556 debug_box(box), format->block_width, format->block_height);
1557 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1558 return WINED3DERR_INVALIDCALL;
1561 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1563 WARN("Trying to map unmappable texture.\n");
1564 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1565 return WINED3DERR_INVALIDCALL;
1568 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1570 WARN("DC is in use.\n");
1571 return WINED3DERR_INVALIDCALL;
1574 if (sub_resource->map_count)
1576 WARN("Sub-resource is already mapped.\n");
1577 return WINED3DERR_INVALIDCALL;
1580 flags = wined3d_resource_sanitize_map_flags(resource, flags);
1582 if (device->d3d_initialized)
1584 context = context_acquire(device, NULL);
1585 gl_info = context->gl_info;
1588 if (flags & WINED3D_MAP_DISCARD)
1590 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1591 wined3d_debug_location(texture->resource.map_binding));
1592 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1593 context, texture->resource.map_binding)))
1594 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1596 else
1598 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1599 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1600 ret = texture->texture_ops->texture_load_location(texture,
1601 sub_resource_idx, context, texture->resource.map_binding);
1604 if (!ret)
1606 ERR("Failed to prepare location.\n");
1607 context_release(context);
1608 return E_OUTOFMEMORY;
1611 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1612 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1614 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1615 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1616 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1617 TRACE("Base memory pointer %p.\n", base_memory);
1619 if (context)
1620 context_release(context);
1622 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1624 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1625 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1627 else
1629 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1632 if (!box)
1634 map_desc->data = base_memory;
1636 else
1638 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1640 /* Compressed textures are block based, so calculate the offset of
1641 * the block that contains the top-left pixel of the mapped box. */
1642 map_desc->data = base_memory
1643 + (box->front * map_desc->slice_pitch)
1644 + ((box->top / format->block_height) * map_desc->row_pitch)
1645 + ((box->left / format->block_width) * format->block_byte_count);
1647 else
1649 map_desc->data = base_memory
1650 + (box->front * map_desc->slice_pitch)
1651 + (box->top * map_desc->row_pitch)
1652 + (box->left * format->byte_count);
1656 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1658 RECT *r = &texture->swapchain->front_buffer_update;
1660 if (!box)
1661 SetRect(r, 0, 0, resource->width, resource->height);
1662 else
1663 SetRect(r, box->left, box->top, box->right, box->bottom);
1664 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1667 ++resource->map_count;
1668 ++sub_resource->map_count;
1670 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1671 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1673 return WINED3D_OK;
1676 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1678 struct wined3d_texture_sub_resource *sub_resource;
1679 struct wined3d_device *device = resource->device;
1680 const struct wined3d_gl_info *gl_info = NULL;
1681 struct wined3d_context *context = NULL;
1682 struct wined3d_texture *texture;
1683 struct wined3d_bo_address data;
1685 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1687 texture = wined3d_texture_from_resource(resource);
1688 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1689 return E_INVALIDARG;
1691 if (!sub_resource->map_count)
1693 WARN("Trying to unmap unmapped sub-resource.\n");
1694 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1695 return WINED3D_OK;
1696 return WINEDDERR_NOTLOCKED;
1699 if (device->d3d_initialized)
1701 context = context_acquire(device, NULL);
1702 gl_info = context->gl_info;
1705 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1706 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1708 if (context)
1709 context_release(context);
1711 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1713 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1714 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1716 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1718 FIXME("Depth / stencil buffer locking is not implemented.\n");
1721 --sub_resource->map_count;
1722 if (!--resource->map_count && texture->update_map_binding)
1723 wined3d_texture_update_map_binding(texture);
1725 return WINED3D_OK;
1728 static const struct wined3d_resource_ops texture_resource_ops =
1730 texture_resource_incref,
1731 texture_resource_decref,
1732 wined3d_texture_unload,
1733 texture_resource_sub_resource_map,
1734 texture_resource_sub_resource_unmap,
1737 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1738 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1739 void *parent, const struct wined3d_parent_ops *parent_ops)
1741 struct wined3d_device_parent *device_parent = device->device_parent;
1742 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1743 struct wined3d_resource_desc surface_desc;
1744 struct wined3d_surface *surfaces;
1745 UINT pow2_width, pow2_height;
1746 unsigned int i, j;
1747 HRESULT hr;
1749 /* TODO: It should only be possible to create textures for formats
1750 * that are reported as supported. */
1751 if (WINED3DFMT_UNKNOWN >= desc->format)
1753 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1754 return WINED3DERR_INVALIDCALL;
1757 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1758 FIXME("Trying to create a managed texture with dynamic usage.\n");
1759 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1760 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1761 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1762 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1763 FIXME("Trying to create a render target that isn't in the default pool.\n");
1765 pow2_width = desc->width;
1766 pow2_height = desc->height;
1767 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1768 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1770 /* level_count == 0 returns an error as well. */
1771 if (level_count != 1 || desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1773 if (desc->pool != WINED3D_POOL_SCRATCH)
1775 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1776 return WINED3DERR_INVALIDCALL;
1779 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1781 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1783 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1785 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1787 /* TODO: Add support for non-power-of-two compressed textures. */
1788 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1789 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1791 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1792 desc->width, desc->height);
1793 return WINED3DERR_NOTAVAILABLE;
1796 /* Find the nearest pow2 match. */
1797 pow2_width = pow2_height = 1;
1798 while (pow2_width < desc->width)
1799 pow2_width <<= 1;
1800 while (pow2_height < desc->height)
1801 pow2_height <<= 1;
1802 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1805 texture->pow2_width = pow2_width;
1806 texture->pow2_height = pow2_height;
1808 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1809 && (desc->usage & WINED3DUSAGE_TEXTURE))
1811 /* One of four options:
1812 * 1: Do the same as we do with NPOT and scale the texture. (Any
1813 * texture ops would require the texture to be scaled which is
1814 * potentially slow.)
1815 * 2: Set the texture to the maximum size (bad idea).
1816 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1817 * 4: Create the surface, but allow it to be used only for DirectDraw
1818 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1819 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1820 * the render target. */
1821 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1823 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1824 return WINED3DERR_NOTAVAILABLE;
1827 /* We should never use this surface in combination with OpenGL. */
1828 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1831 /* Calculate levels for mip mapping. */
1832 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1834 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1836 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1837 return WINED3DERR_INVALIDCALL;
1840 if (level_count != 1)
1842 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1843 return WINED3DERR_INVALIDCALL;
1847 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1848 flags, device, parent, parent_ops, &texture_resource_ops)))
1850 WARN("Failed to initialize texture, returning %#x.\n", hr);
1851 return hr;
1854 /* Precalculated scaling for 'faked' non power of two texture coords. */
1855 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1857 texture->pow2_matrix[0] = (float)desc->width;
1858 texture->pow2_matrix[5] = (float)desc->height;
1859 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1860 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1862 else
1864 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1866 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1867 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1868 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1870 else
1872 texture->pow2_matrix[0] = 1.0f;
1873 texture->pow2_matrix[5] = 1.0f;
1875 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1876 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1877 else
1878 texture->target = GL_TEXTURE_2D;
1880 texture->pow2_matrix[10] = 1.0f;
1881 texture->pow2_matrix[15] = 1.0f;
1882 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1884 if (wined3d_texture_use_pbo(texture, gl_info))
1885 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
1887 if (!(surfaces = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*surfaces) * level_count * layer_count)))
1889 wined3d_texture_cleanup(texture);
1890 return E_OUTOFMEMORY;
1893 /* Generate all the surfaces. */
1894 surface_desc = *desc;
1895 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1896 for (i = 0; i < texture->level_count; ++i)
1898 for (j = 0; j < texture->layer_count; ++j)
1900 static const GLenum cube_targets[6] =
1902 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1903 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1904 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1905 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1906 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1907 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1909 GLenum target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
1910 struct wined3d_texture_sub_resource *sub_resource;
1911 unsigned int idx = j * texture->level_count + i;
1912 struct wined3d_surface *surface;
1914 surface = &surfaces[idx];
1915 if (FAILED(hr = wined3d_surface_init(surface, texture, &surface_desc, target, i, j)))
1917 WARN("Failed to initialize surface, returning %#x.\n", hr);
1918 wined3d_texture_cleanup(texture);
1919 if (!idx)
1920 HeapFree(GetProcessHeap(), 0, surfaces);
1921 return hr;
1924 sub_resource = &texture->sub_resources[idx];
1925 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1926 sub_resource->resource = &surface->resource;
1927 sub_resource->u.surface = surface;
1928 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
1930 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
1931 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
1934 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
1935 texture, idx, &parent, &parent_ops)))
1937 WARN("Failed to create surface parent, hr %#x.\n", hr);
1938 wined3d_surface_cleanup(surface);
1939 wined3d_texture_cleanup(texture);
1940 return hr;
1943 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
1945 surface->resource.parent = parent;
1946 surface->resource.parent_ops = parent_ops;
1947 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
1949 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
1950 && FAILED(hr = wined3d_surface_create_dc(surface)))
1952 wined3d_texture_cleanup(texture);
1953 return hr;
1956 /* Calculate the next mipmap level. */
1957 surface_desc.width = max(1, surface_desc.width >> 1);
1958 surface_desc.height = max(1, surface_desc.height >> 1);
1961 return WINED3D_OK;
1964 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1965 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1967 struct wined3d_const_bo_address addr;
1968 unsigned int row_pitch, slice_pitch;
1970 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1971 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1972 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1974 addr.buffer_object = 0;
1975 addr.addr = data->data;
1977 wined3d_volume_upload_data(texture->sub_resources[sub_resource_idx].u.volume, context, &addr);
1980 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1981 struct wined3d_context *context, DWORD location)
1983 return wined3d_volume_load_location(texture->sub_resources[sub_resource_idx].u.volume, context, location);
1986 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1988 unsigned int sub_count = texture->level_count * texture->layer_count;
1989 const struct wined3d_format *format = texture->resource.format;
1990 const struct wined3d_gl_info *gl_info = context->gl_info;
1991 unsigned int i;
1993 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1995 for (i = 0; i < sub_count; ++i)
1997 struct wined3d_volume *volume = texture->sub_resources[i].u.volume;
1999 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
2000 srgb ? format->glGammaInternal : format->glInternal,
2001 wined3d_texture_get_level_width(texture, volume->texture_level),
2002 wined3d_texture_get_level_height(texture, volume->texture_level),
2003 wined3d_texture_get_level_depth(texture, volume->texture_level),
2004 0, format->glFormat, format->glType, NULL));
2005 checkGLcall("glTexImage3D");
2009 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2011 unsigned int sub_count = texture->level_count * texture->layer_count;
2012 struct wined3d_volume *volume;
2013 unsigned int i;
2015 for (i = 0; i < sub_count; ++i)
2017 if ((volume = texture->sub_resources[i].u.volume))
2019 TRACE("volume %p.\n", volume);
2021 wined3d_volume_cleanup(volume);
2022 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
2025 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.volume);
2028 static const struct wined3d_texture_ops texture3d_ops =
2030 texture3d_upload_data,
2031 texture3d_load_location,
2032 texture3d_prepare_texture,
2033 texture3d_cleanup_sub_resources,
2036 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2037 unsigned int level, const struct wined3d_box *box)
2039 const struct wined3d_format *format = texture->resource.format;
2040 unsigned int height = wined3d_texture_get_level_height(texture, level);
2041 unsigned int width = wined3d_texture_get_level_width(texture, level);
2042 unsigned int width_mask, height_mask;
2044 if ((box->left >= box->right)
2045 || (box->top >= box->bottom)
2046 || (box->right > width)
2047 || (box->bottom > height))
2048 return FALSE;
2050 /* This assumes power of two block sizes, but NPOT block sizes would be
2051 * silly anyway.
2053 * This also assumes that the format's block depth is 1. */
2054 width_mask = format->block_width - 1;
2055 height_mask = format->block_height - 1;
2057 if ((box->left & width_mask) || (box->top & height_mask)
2058 || (box->right & width_mask && box->right != width)
2059 || (box->bottom & height_mask && box->bottom != height))
2060 return FALSE;
2062 return TRUE;
2065 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2066 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
2068 struct wined3d_device_parent *device_parent = device->device_parent;
2069 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2070 struct wined3d_resource_desc volume_desc;
2071 struct wined3d_volume *volumes;
2072 unsigned int i;
2073 HRESULT hr;
2075 /* TODO: It should only be possible to create textures for formats
2076 * that are reported as supported. */
2077 if (WINED3DFMT_UNKNOWN >= desc->format)
2079 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2080 return WINED3DERR_INVALIDCALL;
2083 if (!gl_info->supported[EXT_TEXTURE3D])
2085 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2086 return WINED3DERR_INVALIDCALL;
2089 /* Calculate levels for mip mapping. */
2090 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2092 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2094 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2095 return WINED3DERR_INVALIDCALL;
2098 if (levels != 1)
2100 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2101 return WINED3DERR_INVALIDCALL;
2105 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2106 || desc->pool == WINED3D_POOL_SCRATCH))
2108 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2109 return WINED3DERR_INVALIDCALL;
2112 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2114 UINT pow2_w, pow2_h, pow2_d;
2115 pow2_w = 1;
2116 while (pow2_w < desc->width)
2117 pow2_w <<= 1;
2118 pow2_h = 1;
2119 while (pow2_h < desc->height)
2120 pow2_h <<= 1;
2121 pow2_d = 1;
2122 while (pow2_d < desc->depth)
2123 pow2_d <<= 1;
2125 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2127 if (desc->pool == WINED3D_POOL_SCRATCH)
2129 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2131 else
2133 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2134 desc->width, desc->height, desc->depth);
2135 return WINED3DERR_INVALIDCALL;
2140 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
2141 0, device, parent, parent_ops, &texture_resource_ops)))
2143 WARN("Failed to initialize texture, returning %#x.\n", hr);
2144 return hr;
2147 texture->pow2_matrix[0] = 1.0f;
2148 texture->pow2_matrix[5] = 1.0f;
2149 texture->pow2_matrix[10] = 1.0f;
2150 texture->pow2_matrix[15] = 1.0f;
2151 texture->target = GL_TEXTURE_3D;
2153 if (wined3d_texture_use_pbo(texture, gl_info))
2155 wined3d_resource_free_sysmem(&texture->resource);
2156 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2159 if (!(volumes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volumes) * levels)))
2161 wined3d_texture_cleanup(texture);
2162 return E_OUTOFMEMORY;
2165 /* Generate all the surfaces. */
2166 volume_desc = *desc;
2167 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
2168 for (i = 0; i < texture->level_count; ++i)
2170 struct wined3d_texture_sub_resource *sub_resource;
2171 struct wined3d_volume *volume;
2173 volume = &volumes[i];
2174 if (FAILED(hr = wined3d_volume_init(volume, texture, &volume_desc, i)))
2176 WARN("Failed to initialize volume, returning %#x.\n", hr);
2177 wined3d_texture_cleanup(texture);
2178 if (!i)
2179 HeapFree(GetProcessHeap(), 0, volumes);
2180 return hr;
2183 sub_resource = &texture->sub_resources[i];
2184 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2185 sub_resource->resource = &volume->resource;
2186 sub_resource->u.volume = volume;
2188 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2189 texture, i, &parent, &parent_ops)))
2191 WARN("Failed to create volume parent, hr %#x.\n", hr);
2192 wined3d_volume_cleanup(volume);
2193 wined3d_texture_cleanup(texture);
2194 return hr;
2197 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2199 volume->resource.parent = parent;
2200 volume->resource.parent_ops = parent_ops;
2201 TRACE("Created volume level %u @ %p.\n", i, volume);
2203 /* Calculate the next mipmap level. */
2204 volume_desc.width = max(1, volume_desc.width >> 1);
2205 volume_desc.height = max(1, volume_desc.height >> 1);
2206 volume_desc.depth = max(1, volume_desc.depth >> 1);
2209 return WINED3D_OK;
2212 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2213 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2214 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2216 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2218 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2219 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2220 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2221 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2223 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2224 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2225 return WINED3DERR_INVALIDCALL;
2227 if (src_texture)
2229 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2230 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2231 return WINED3DERR_INVALIDCALL;
2234 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2235 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2238 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2239 unsigned int sub_resource_idx, LONG *x, LONG *y)
2241 struct wined3d_surface *surface;
2243 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2245 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2246 || sub_resource_idx >= texture->level_count * texture->layer_count)
2248 WARN("Invalid sub-resource specified.\n");
2249 return WINEDDERR_NOTAOVERLAYSURFACE;
2252 surface = texture->sub_resources[sub_resource_idx].u.surface;
2253 if (!surface->overlay_dest)
2255 TRACE("Overlay not visible.\n");
2256 *x = 0;
2257 *y = 0;
2258 return WINEDDERR_OVERLAYNOTVISIBLE;
2261 *x = surface->overlay_destrect.left;
2262 *y = surface->overlay_destrect.top;
2264 TRACE("Returning position %d, %d.\n", *x, *y);
2266 return WINED3D_OK;
2269 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2270 unsigned int sub_resource_idx, LONG x, LONG y)
2272 struct wined3d_texture_sub_resource *sub_resource;
2273 struct wined3d_surface *surface;
2274 LONG w, h;
2276 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2278 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2279 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2281 WARN("Invalid sub-resource specified.\n");
2282 return WINEDDERR_NOTAOVERLAYSURFACE;
2285 surface = sub_resource->u.surface;
2286 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2287 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2288 surface->overlay_destrect.left = x;
2289 surface->overlay_destrect.top = y;
2290 surface->overlay_destrect.right = x + w;
2291 surface->overlay_destrect.bottom = y + h;
2293 return WINED3D_OK;
2296 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2297 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2298 const RECT *dst_rect, DWORD flags)
2300 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2301 struct wined3d_surface *surface, *dst_surface;
2303 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2304 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2305 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2306 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2308 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2309 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2311 WARN("Invalid sub-resource specified.\n");
2312 return WINEDDERR_NOTAOVERLAYSURFACE;
2315 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2316 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2318 WARN("Invalid destination sub-resource specified.\n");
2319 return WINED3DERR_INVALIDCALL;
2322 surface = sub_resource->u.surface;
2323 if (src_rect)
2324 surface->overlay_srcrect = *src_rect;
2325 else
2326 SetRect(&surface->overlay_srcrect, 0, 0,
2327 wined3d_texture_get_level_width(texture, surface->texture_level),
2328 wined3d_texture_get_level_height(texture, surface->texture_level));
2330 dst_surface = dst_sub_resource->u.surface;
2331 if (dst_rect)
2332 surface->overlay_destrect = *dst_rect;
2333 else
2334 SetRect(&surface->overlay_destrect, 0, 0,
2335 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2336 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2338 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2340 surface->overlay_dest = NULL;
2341 list_remove(&surface->overlay_entry);
2344 if (flags & WINEDDOVER_SHOW)
2346 if (surface->overlay_dest != dst_surface)
2348 surface->overlay_dest = dst_surface;
2349 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2352 else if (flags & WINEDDOVER_HIDE)
2354 /* Tests show that the rectangles are erased on hide. */
2355 SetRectEmpty(&surface->overlay_srcrect);
2356 SetRectEmpty(&surface->overlay_destrect);
2357 surface->overlay_dest = NULL;
2360 return WINED3D_OK;
2363 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2365 unsigned int sub_count = texture->level_count * texture->layer_count;
2367 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2369 if (sub_resource_idx >= sub_count)
2371 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2372 return NULL;
2375 return texture->sub_resources[sub_resource_idx].resource->parent;
2378 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2379 unsigned int sub_resource_idx, void *parent)
2381 unsigned int sub_count = texture->level_count * texture->layer_count;
2383 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2385 if (sub_resource_idx >= sub_count)
2387 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2388 return;
2391 texture->sub_resources[sub_resource_idx].resource->parent = parent;
2394 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2395 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2397 unsigned int sub_count = texture->level_count * texture->layer_count;
2398 const struct wined3d_resource *resource;
2399 unsigned int level_idx;
2401 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2403 if (sub_resource_idx >= sub_count)
2405 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2406 return WINED3DERR_INVALIDCALL;
2409 resource = &texture->resource;
2410 desc->format = resource->format->id;
2411 desc->multisample_type = resource->multisample_type;
2412 desc->multisample_quality = resource->multisample_quality;
2413 desc->usage = resource->usage;
2414 desc->pool = resource->pool;
2416 level_idx = sub_resource_idx % texture->level_count;
2417 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2418 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2419 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2420 desc->size = texture->sub_resources[sub_resource_idx].size;
2422 return WINED3D_OK;
2425 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2426 UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data, void *parent,
2427 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2429 unsigned int layer_count = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? 6 : 1;
2430 struct wined3d_texture *object;
2431 HRESULT hr;
2433 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2434 device, desc, level_count, flags, data, parent, parent_ops, texture);
2436 if (!level_count)
2438 WARN("Invalid level count.\n");
2439 return WINED3DERR_INVALIDCALL;
2442 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2444 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2446 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2447 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2449 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2450 desc->multisample_quality);
2451 return WINED3DERR_NOTAVAILABLE;
2453 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2454 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2455 || desc->multisample_quality))
2457 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2458 desc->multisample_quality);
2459 return WINED3DERR_NOTAVAILABLE;
2463 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2464 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2465 return E_OUTOFMEMORY;
2467 switch (desc->resource_type)
2469 case WINED3D_RTYPE_TEXTURE_2D:
2470 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2471 break;
2473 case WINED3D_RTYPE_TEXTURE_3D:
2474 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
2475 break;
2477 default:
2478 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2479 hr = WINED3DERR_INVALIDCALL;
2480 break;
2483 if (FAILED(hr))
2485 WARN("Failed to initialize texture, returning %#x.\n", hr);
2486 HeapFree(GetProcessHeap(), 0, object);
2487 return hr;
2490 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2491 * in this case. */
2492 if (data && FAILED(hr = wined3d_texture_upload_data(object, data)))
2494 wined3d_texture_cleanup(object);
2495 HeapFree(GetProcessHeap(), 0, object);
2496 return hr;
2499 TRACE("Created texture %p.\n", object);
2500 *texture = object;
2502 return WINED3D_OK;
2505 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2507 struct wined3d_device *device = texture->resource.device;
2508 struct wined3d_texture_sub_resource *sub_resource;
2509 struct wined3d_context *context = NULL;
2510 struct wined3d_surface *surface;
2511 HRESULT hr = WINED3D_OK;
2513 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2515 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2516 return WINED3DERR_INVALIDCALL;
2518 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2520 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2521 return WINED3DERR_INVALIDCALL;
2524 surface = sub_resource->u.surface;
2526 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2527 return WINED3DERR_INVALIDCALL;
2529 if (device->d3d_initialized)
2530 context = context_acquire(device, NULL);
2532 surface_load_location(surface, context, texture->resource.map_binding);
2533 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2535 if (!surface->dc)
2536 hr = wined3d_surface_create_dc(surface);
2537 if (context)
2538 context_release(context);
2539 if (FAILED(hr))
2540 return WINED3DERR_INVALIDCALL;
2542 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2543 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2544 ++texture->resource.map_count;
2545 ++sub_resource->map_count;
2547 *dc = surface->dc;
2548 TRACE("Returning dc %p.\n", *dc);
2550 return hr;
2553 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2555 struct wined3d_device *device = texture->resource.device;
2556 struct wined3d_texture_sub_resource *sub_resource;
2557 struct wined3d_surface *surface;
2559 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2561 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2562 return WINED3DERR_INVALIDCALL;
2564 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2566 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2567 return WINED3DERR_INVALIDCALL;
2570 surface = sub_resource->u.surface;
2572 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2573 return WINED3DERR_INVALIDCALL;
2575 if (surface->dc != dc)
2577 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2578 return WINED3DERR_INVALIDCALL;
2581 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2582 wined3d_surface_destroy_dc(surface);
2584 --sub_resource->map_count;
2585 if (!--texture->resource.map_count && texture->update_map_binding)
2586 wined3d_texture_update_map_binding(texture);
2587 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2588 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2590 return WINED3D_OK;