wined3d: Sanitise resource map flags in wined3d_resource_map().
[wine.git] / dlls / wined3d / texture.c
blobcf5fc93fa348f2b0e4a54bc97d02bae4ff6ed126
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 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
43 const struct wined3d_gl_info *gl_info)
45 /* We don't expect to create texture views for textures with height-scaled formats.
46 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
47 return gl_info->supported[ARB_TEXTURE_STORAGE]
48 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
51 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
53 const struct wined3d_swapchain *swapchain = texture->swapchain;
55 TRACE("texture %p.\n", texture);
57 if (!swapchain)
59 ERR("Texture %p is not part of a swapchain.\n", texture);
60 return GL_NONE;
63 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
65 if (swapchain->render_to_fbo)
67 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
68 return GL_COLOR_ATTACHMENT0;
70 TRACE("Returning GL_BACK.\n");
71 return GL_BACK;
73 else if (texture == swapchain->front_buffer)
75 TRACE("Returning GL_FRONT.\n");
76 return GL_FRONT;
79 FIXME("Higher back buffer, returning GL_BACK.\n");
80 return GL_BACK;
83 static DWORD wined3d_resource_access_from_location(DWORD location)
85 switch (location)
87 case WINED3D_LOCATION_DISCARDED:
88 return 0;
90 case WINED3D_LOCATION_SYSMEM:
91 return WINED3D_RESOURCE_ACCESS_CPU;
93 case WINED3D_LOCATION_BUFFER:
94 case WINED3D_LOCATION_TEXTURE_RGB:
95 case WINED3D_LOCATION_TEXTURE_SRGB:
96 return WINED3D_RESOURCE_ACCESS_GPU;
98 default:
99 FIXME("Unhandled location %#x.\n", location);
100 return 0;
104 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
106 struct wined3d_texture_sub_resource *sub_resource;
107 unsigned int i, sub_count;
109 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
110 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
112 TRACE("Not evicting system memory for texture %p.\n", texture);
113 return;
116 TRACE("Evicting system memory for texture %p.\n", texture);
118 sub_count = texture->level_count * texture->layer_count;
119 for (i = 0; i < sub_count; ++i)
121 sub_resource = &texture->sub_resources[i];
122 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
123 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
124 i, texture);
125 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
127 wined3d_resource_free_sysmem(&texture->resource);
130 void wined3d_texture_validate_location(struct wined3d_texture *texture,
131 unsigned int sub_resource_idx, DWORD location)
133 struct wined3d_texture_sub_resource *sub_resource;
134 DWORD previous_locations;
136 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
137 texture, sub_resource_idx, wined3d_debug_location(location));
139 sub_resource = &texture->sub_resources[sub_resource_idx];
140 previous_locations = sub_resource->locations;
141 sub_resource->locations |= location;
142 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
143 && !--texture->sysmem_count)
144 wined3d_texture_evict_sysmem(texture);
146 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
149 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
151 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
154 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
155 unsigned int sub_resource_idx, DWORD location)
157 struct wined3d_texture_sub_resource *sub_resource;
159 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
160 texture, sub_resource_idx, wined3d_debug_location(location));
162 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
163 wined3d_texture_set_dirty(texture);
165 sub_resource = &texture->sub_resources[sub_resource_idx];
166 sub_resource->locations &= ~location;
167 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
168 ++texture->sysmem_count;
170 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
172 if (!sub_resource->locations)
173 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
174 sub_resource_idx, texture);
177 /* Context activation is done by the caller. Context may be NULL in
178 * WINED3D_NO3D mode. */
179 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
180 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
182 return texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
185 /* Context activation is done by the caller. */
186 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
187 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
189 BYTE *memory;
191 if (!data->buffer_object)
192 return data->addr;
194 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
196 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
198 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
199 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
201 else
203 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
204 memory += (INT_PTR)data->addr;
207 GL_EXTCALL(glBindBuffer(binding, 0));
208 checkGLcall("Map buffer object");
210 return memory;
213 /* Context activation is done by the caller. */
214 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
215 const struct wined3d_gl_info *gl_info, GLenum binding)
217 if (!data->buffer_object)
218 return;
220 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
221 GL_EXTCALL(glUnmapBuffer(binding));
222 GL_EXTCALL(glBindBuffer(binding, 0));
223 checkGLcall("Unmap buffer object");
226 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
227 struct wined3d_bo_address *data, DWORD locations)
229 struct wined3d_texture_sub_resource *sub_resource;
231 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
232 texture, sub_resource_idx, data, wined3d_debug_location(locations));
234 sub_resource = &texture->sub_resources[sub_resource_idx];
235 if (locations & WINED3D_LOCATION_BUFFER)
237 data->addr = NULL;
238 data->buffer_object = sub_resource->buffer_object;
239 return;
241 if (locations & WINED3D_LOCATION_USER_MEMORY)
243 data->addr = texture->user_memory;
244 data->buffer_object = 0;
245 return;
247 if (locations & WINED3D_LOCATION_SYSMEM)
249 data->addr = texture->resource.heap_memory;
250 data->addr += sub_resource->offset;
251 data->buffer_object = 0;
252 return;
255 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
256 data->addr = NULL;
257 data->buffer_object = 0;
260 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
261 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
262 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
263 const struct wined3d_resource_ops *resource_ops)
265 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
266 unsigned int i, j, size, offset = 0;
267 HRESULT hr;
269 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
270 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
271 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
272 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
273 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
274 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
275 flags, device, parent, parent_ops, resource_ops);
277 if (!desc->width || !desc->height || !desc->depth)
278 return WINED3DERR_INVALIDCALL;
280 for (i = 0; i < layer_count; ++i)
282 for (j = 0; j < level_count; ++j)
284 unsigned int idx = i * level_count + j;
286 size = wined3d_format_calculate_size(format, device->surface_alignment,
287 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
288 texture->sub_resources[idx].offset = offset;
289 texture->sub_resources[idx].size = size;
290 offset += size;
292 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
295 if (!offset)
296 return WINED3DERR_INVALIDCALL;
298 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
299 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
300 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
302 static unsigned int once;
304 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
305 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
306 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
307 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
308 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
309 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
311 WARN("Failed to initialize resource, returning %#x\n", hr);
312 return hr;
314 wined3d_resource_update_draw_binding(&texture->resource);
315 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
316 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
318 texture->texture_ops = texture_ops;
320 texture->layer_count = layer_count;
321 texture->level_count = level_count;
322 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
323 texture->lod = 0;
324 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
325 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
326 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
327 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
328 texture->flags |= WINED3D_TEXTURE_DISCARD;
330 return WINED3D_OK;
333 /* Context activation is done by the caller. */
334 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
335 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
337 GLuint *buffer_object;
339 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
340 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
341 checkGLcall("glDeleteBuffers");
342 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
343 *buffer_object = 0;
345 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
346 *buffer_object, texture, sub_resource_idx);
349 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
351 unsigned int sub_count = texture->level_count * texture->layer_count;
352 const struct wined3d_device *device = texture->resource.device;
353 DWORD map_binding = texture->update_map_binding;
354 struct wined3d_context *context = NULL;
355 unsigned int i;
357 if (device->d3d_initialized)
358 context = context_acquire(device, NULL);
360 for (i = 0; i < sub_count; ++i)
362 if (texture->sub_resources[i].locations == texture->resource.map_binding
363 && !wined3d_texture_load_location(texture, i, context, map_binding))
364 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
365 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
366 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
369 if (context)
370 context_release(context);
372 texture->resource.map_binding = map_binding;
373 texture->update_map_binding = 0;
376 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
378 texture->update_map_binding = map_binding;
379 if (!texture->resource.map_count)
380 wined3d_texture_update_map_binding(texture);
383 /* A GL context is provided by the caller */
384 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
385 struct gl_texture *tex)
387 context_gl_resource_released(device, tex->name, FALSE);
388 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
389 tex->name = 0;
392 /* Context activation is done by the caller. */
393 /* The caller is responsible for binding the correct texture. */
394 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
395 GLenum gl_internal_format, const struct wined3d_format *format,
396 const struct wined3d_gl_info *gl_info)
398 unsigned int i, sub_call_count;
400 sub_call_count = texture->level_count;
401 if (texture->target != GL_TEXTURE_2D_ARRAY)
402 sub_call_count *= texture->layer_count;
404 for (i = 0; i < sub_call_count; ++i)
406 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
407 GLsizei width, height;
409 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
410 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
411 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
413 height *= format->height_scale.numerator;
414 height /= format->height_scale.denominator;
417 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
418 surface, surface->texture_target, surface->texture_level, width, height);
420 if (texture->target == GL_TEXTURE_2D_ARRAY)
422 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
423 gl_internal_format, width, height, texture->layer_count, 0,
424 format->glFormat, format->glType, NULL));
425 checkGLcall("glTexImage3D");
427 else
429 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
430 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
431 checkGLcall("glTexImage2D");
436 /* Context activation is done by the caller. */
437 /* The caller is responsible for binding the correct texture. */
438 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
439 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
441 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
442 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
444 if (texture->target == GL_TEXTURE_2D_ARRAY)
446 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
447 width, height, texture->layer_count));
448 checkGLcall("glTexStorage3D");
450 else
452 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
453 width, height));
454 checkGLcall("glTexStorage2D");
458 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
460 struct wined3d_device *device = texture->resource.device;
461 const struct wined3d_gl_info *gl_info = NULL;
462 struct wined3d_context *context = NULL;
464 if (texture->texture_rgb.name || texture->texture_srgb.name
465 || texture->rb_multisample || texture->rb_resolved)
467 context = context_acquire(device, NULL);
468 gl_info = context->gl_info;
471 if (texture->texture_rgb.name)
472 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
474 if (texture->texture_srgb.name)
475 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
477 if (texture->rb_multisample)
479 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
480 context_gl_resource_released(device, texture->rb_multisample, TRUE);
481 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
482 texture->rb_multisample = 0;
485 if (texture->rb_resolved)
487 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
488 context_gl_resource_released(device, texture->rb_resolved, TRUE);
489 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
490 texture->rb_resolved = 0;
493 if (context) context_release(context);
495 wined3d_texture_set_dirty(texture);
497 resource_unload(&texture->resource);
500 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
502 unsigned int sub_count = texture->level_count * texture->layer_count;
503 struct wined3d_texture_sub_resource *sub_resource;
504 unsigned int i;
506 for (i = 0; i < sub_count; ++i)
508 sub_resource = &texture->sub_resources[i];
509 if (sub_resource->parent)
511 TRACE("sub-resource %u.\n", i);
512 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
513 sub_resource->parent = NULL;
518 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
520 unsigned int sub_count = texture->level_count * texture->layer_count;
521 struct wined3d_device *device = texture->resource.device;
522 struct wined3d_context *context = NULL;
523 const struct wined3d_gl_info *gl_info;
524 GLuint buffer_object;
525 unsigned int i;
527 TRACE("texture %p.\n", texture);
529 for (i = 0; i < sub_count; ++i)
531 if (!(buffer_object = texture->sub_resources[i].buffer_object))
532 continue;
534 TRACE("Deleting buffer object %u.\n", buffer_object);
536 /* We may not be able to get a context in wined3d_texture_cleanup() in
537 * general, but if a buffer object was previously created we can. */
538 if (!context)
540 context = context_acquire(device, NULL);
541 gl_info = context->gl_info;
544 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
546 if (context)
547 context_release(context);
549 texture->texture_ops->texture_cleanup_sub_resources(texture);
550 wined3d_texture_unload_gl_texture(texture);
553 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
555 texture->swapchain = swapchain;
556 wined3d_resource_update_draw_binding(&texture->resource);
559 /* Context activation is done by the caller. */
560 void wined3d_texture_bind(struct wined3d_texture *texture,
561 struct wined3d_context *context, BOOL srgb)
563 const struct wined3d_gl_info *gl_info = context->gl_info;
564 const struct wined3d_format *format = texture->resource.format;
565 const struct color_fixup_desc fixup = format->color_fixup;
566 struct gl_texture *gl_tex;
567 GLenum target;
569 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
571 if (!needs_separate_srgb_gl_texture(context))
572 srgb = FALSE;
574 /* sRGB mode cache for preload() calls outside drawprim. */
575 if (srgb)
576 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
577 else
578 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
580 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
581 target = texture->target;
583 if (gl_tex->name)
585 context_bind_texture(context, target, gl_tex->name);
586 return;
589 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
590 checkGLcall("glGenTextures");
591 TRACE("Generated texture %d.\n", gl_tex->name);
593 if (!gl_tex->name)
595 ERR("Failed to generate a texture name.\n");
596 return;
599 /* Initialise the state of the texture object to the OpenGL defaults, not
600 * the wined3d defaults. */
601 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
602 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
603 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
604 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
605 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
606 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
607 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
608 gl_tex->sampler_desc.lod_bias = 0.0f;
609 gl_tex->sampler_desc.min_lod = -1000.0f;
610 gl_tex->sampler_desc.max_lod = 1000.0f;
611 gl_tex->sampler_desc.max_anisotropy = 1;
612 gl_tex->sampler_desc.compare = FALSE;
613 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
614 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
615 gl_tex->sampler_desc.srgb_decode = TRUE;
616 else
617 gl_tex->sampler_desc.srgb_decode = srgb;
618 gl_tex->base_level = 0;
619 wined3d_texture_set_dirty(texture);
621 context_bind_texture(context, target, gl_tex->name);
623 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
625 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
626 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
629 /* For a new texture we have to set the texture levels after binding the
630 * texture. Beware that texture rectangles do not support mipmapping, but
631 * set the maxmiplevel if we're relying on the partial
632 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
633 * (I.e., do not care about cond_np2 here, just look for
634 * GL_TEXTURE_RECTANGLE_ARB.) */
635 if (target != GL_TEXTURE_RECTANGLE_ARB)
637 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
638 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
639 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
642 if (target == GL_TEXTURE_CUBE_MAP_ARB)
644 /* Cubemaps are always set to clamp, regardless of the sampler state. */
645 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
646 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
647 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
650 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
652 /* Conditinal non power of two textures use a different clamping
653 * default. If we're using the GL_WINE_normalized_texrect partial
654 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
655 * has the address mode set to repeat - something that prevents us
656 * from hitting the accelerated codepath. Thus manually set the GL
657 * state. The same applies to filtering. Even if the texture has only
658 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
659 * fallback on macos. */
660 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
661 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
662 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
663 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
664 checkGLcall("glTexParameteri");
665 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
666 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
667 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
668 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
669 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
672 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
674 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
675 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
678 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
680 static const GLenum swizzle_source[] =
682 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
683 GL_ONE, /* CHANNEL_SOURCE_ONE */
684 GL_RED, /* CHANNEL_SOURCE_X */
685 GL_GREEN, /* CHANNEL_SOURCE_Y */
686 GL_BLUE, /* CHANNEL_SOURCE_Z */
687 GL_ALPHA, /* CHANNEL_SOURCE_W */
689 struct
691 GLint x, y, z, w;
693 swizzle;
695 swizzle.x = swizzle_source[fixup.x_source];
696 swizzle.y = swizzle_source[fixup.y_source];
697 swizzle.z = swizzle_source[fixup.z_source];
698 swizzle.w = swizzle_source[fixup.w_source];
699 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
700 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
704 /* Context activation is done by the caller. */
705 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
706 struct wined3d_context *context, BOOL srgb)
708 DWORD active_sampler;
710 /* We don't need a specific texture unit, but after binding the texture
711 * the current unit is dirty. Read the unit back instead of switching to
712 * 0, this avoids messing around with the state manager's GL states. The
713 * current texture unit should always be a valid one.
715 * To be more specific, this is tricky because we can implicitly be
716 * called from sampler() in state.c. This means we can't touch anything
717 * other than whatever happens to be the currently active texture, or we
718 * would risk marking already applied sampler states dirty again. */
719 active_sampler = context->rev_tex_unit_map[context->active_texture];
720 if (active_sampler != WINED3D_UNMAPPED_STAGE)
721 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
722 /* FIXME: Ideally we'd only do this when touching a binding that's used by
723 * a shader. */
724 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
726 wined3d_texture_bind(texture, context, srgb);
729 /* Context activation is done by the caller (state handler). */
730 /* This function relies on the correct texture being bound and loaded. */
731 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
732 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
734 const struct wined3d_gl_info *gl_info = context->gl_info;
735 GLenum target = texture->target;
736 struct gl_texture *gl_tex;
737 DWORD state;
739 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
741 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
743 state = sampler_desc->address_u;
744 if (state != gl_tex->sampler_desc.address_u)
746 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
747 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
748 gl_tex->sampler_desc.address_u = state;
751 state = sampler_desc->address_v;
752 if (state != gl_tex->sampler_desc.address_v)
754 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
755 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
756 gl_tex->sampler_desc.address_v = state;
759 state = sampler_desc->address_w;
760 if (state != gl_tex->sampler_desc.address_w)
762 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
763 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
764 gl_tex->sampler_desc.address_w = state;
767 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
768 sizeof(gl_tex->sampler_desc.border_color)))
770 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
771 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
772 sizeof(gl_tex->sampler_desc.border_color));
775 state = sampler_desc->mag_filter;
776 if (state != gl_tex->sampler_desc.mag_filter)
778 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
779 gl_tex->sampler_desc.mag_filter = state;
782 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
783 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
785 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
786 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
787 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
788 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
791 state = sampler_desc->max_anisotropy;
792 if (state != gl_tex->sampler_desc.max_anisotropy)
794 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
795 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
796 else
797 WARN("Anisotropic filtering not supported.\n");
798 gl_tex->sampler_desc.max_anisotropy = state;
801 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
802 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
803 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
805 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
806 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
807 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
810 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
812 if (sampler_desc->compare)
813 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
814 else
815 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
816 gl_tex->sampler_desc.compare = sampler_desc->compare;
819 checkGLcall("Texture parameter application");
821 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
823 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
824 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
825 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
829 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
831 ULONG refcount;
833 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
835 if (texture->swapchain)
836 return wined3d_swapchain_incref(texture->swapchain);
838 refcount = InterlockedIncrement(&texture->resource.ref);
839 TRACE("%p increasing refcount to %u.\n", texture, refcount);
841 return refcount;
844 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
846 wined3d_texture_sub_resources_destroyed(texture);
847 resource_cleanup(&texture->resource);
848 wined3d_resource_wait_idle(&texture->resource);
849 wined3d_texture_cleanup(texture);
852 static void wined3d_texture_destroy_object(void *object)
854 wined3d_texture_cleanup(object);
855 HeapFree(GetProcessHeap(), 0, object);
858 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
860 ULONG refcount;
862 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
864 if (texture->swapchain)
865 return wined3d_swapchain_decref(texture->swapchain);
867 refcount = InterlockedDecrement(&texture->resource.ref);
868 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
870 if (!refcount)
872 wined3d_texture_sub_resources_destroyed(texture);
873 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
874 resource_cleanup(&texture->resource);
875 wined3d_cs_emit_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
878 return refcount;
881 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
883 TRACE("texture %p.\n", texture);
885 return &texture->resource;
888 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
890 return c1->color_space_low_value == c2->color_space_low_value
891 && c1->color_space_high_value == c2->color_space_high_value;
894 /* Context activation is done by the caller */
895 void wined3d_texture_load(struct wined3d_texture *texture,
896 struct wined3d_context *context, BOOL srgb)
898 UINT sub_count = texture->level_count * texture->layer_count;
899 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
900 DWORD flag;
901 UINT i;
903 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
905 if (!needs_separate_srgb_gl_texture(context))
906 srgb = FALSE;
908 if (srgb)
909 flag = WINED3D_TEXTURE_SRGB_VALID;
910 else
911 flag = WINED3D_TEXTURE_RGB_VALID;
913 if (!d3d_info->shader_color_key
914 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
915 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
916 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
917 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
919 unsigned int sub_count = texture->level_count * texture->layer_count;
920 unsigned int i;
922 TRACE("Reloading because of color key value change.\n");
923 for (i = 0; i < sub_count; i++)
925 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
926 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
927 else
928 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
931 texture->async.gl_color_key = texture->async.src_blt_color_key;
934 if (texture->flags & flag)
936 TRACE("Texture %p not dirty, nothing to do.\n", texture);
937 return;
940 /* Reload the surfaces if the texture is marked dirty. */
941 for (i = 0; i < sub_count; ++i)
943 if (!wined3d_texture_load_location(texture, i, context,
944 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
945 ERR("Failed to load location (srgb %#x).\n", srgb);
947 texture->flags |= flag;
950 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
952 TRACE("texture %p.\n", texture);
954 return texture->resource.parent;
957 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
958 unsigned int level, const struct wined3d_box *box)
960 if (box->left >= box->right
961 || box->top >= box->bottom
962 || box->front >= box->back)
963 return FALSE;
965 if (box->right > wined3d_texture_get_level_width(texture, level)
966 || box->bottom > wined3d_texture_get_level_height(texture, level)
967 || box->back > wined3d_texture_get_level_depth(texture, level))
968 return FALSE;
970 return TRUE;
973 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
974 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
976 const struct wined3d_resource *resource = &texture->resource;
977 unsigned int width = wined3d_texture_get_level_width(texture, level);
978 unsigned int height = wined3d_texture_get_level_height(texture, level);
980 if (texture->row_pitch)
982 *row_pitch = texture->row_pitch;
983 *slice_pitch = texture->slice_pitch;
984 return;
987 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
988 width, height, row_pitch, slice_pitch);
991 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
993 DWORD old = texture->lod;
995 TRACE("texture %p, lod %u.\n", texture, lod);
997 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
998 * textures. The call always returns 0, and GetLOD always returns 0. */
999 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1001 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1002 return 0;
1005 if (lod >= texture->level_count)
1006 lod = texture->level_count - 1;
1008 if (texture->lod != lod)
1010 wined3d_resource_wait_idle(&texture->resource);
1011 texture->lod = lod;
1013 texture->texture_rgb.base_level = ~0u;
1014 texture->texture_srgb.base_level = ~0u;
1015 if (texture->resource.bind_count)
1016 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1019 return old;
1022 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1024 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1026 return texture->lod;
1029 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1031 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1033 return texture->level_count;
1036 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1037 enum wined3d_texture_filter_type filter_type)
1039 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1041 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1043 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1044 return WINED3DERR_INVALIDCALL;
1047 texture->filter_type = filter_type;
1049 return WINED3D_OK;
1052 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1054 TRACE("texture %p.\n", texture);
1056 return texture->filter_type;
1059 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1060 DWORD flags, const struct wined3d_color_key *color_key)
1062 struct wined3d_device *device = texture->resource.device;
1063 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1064 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1066 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1068 if (flags & ~all_flags)
1070 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1071 return WINED3DERR_INVALIDCALL;
1074 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1076 return WINED3D_OK;
1079 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1080 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1081 UINT multisample_quality, void *mem, UINT pitch)
1083 struct wined3d_device *device = texture->resource.device;
1084 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1085 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1086 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1087 struct wined3d_texture_sub_resource *sub_resource;
1088 struct wined3d_surface *surface;
1089 DWORD valid_location = 0;
1090 BOOL create_dib = FALSE;
1092 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1093 "mem %p, pitch %u.\n",
1094 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1096 if (!resource_size)
1097 return WINED3DERR_INVALIDCALL;
1099 if (texture->level_count * texture->layer_count > 1)
1101 WARN("Texture has multiple sub-resources, not supported.\n");
1102 return WINED3DERR_INVALIDCALL;
1105 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1107 WARN("Not supported on 3D textures.\n");
1108 return WINED3DERR_INVALIDCALL;
1111 if (texture->resource.map_count)
1113 WARN("Texture is mapped.\n");
1114 return WINED3DERR_INVALIDCALL;
1117 /* We have no way of supporting a pitch that is not a multiple of the pixel
1118 * byte width short of uploading the texture row-by-row.
1119 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1120 * for user-memory textures (it always expects packed data) while DirectDraw
1121 * requires a 4-byte aligned pitch and doesn't support texture formats
1122 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1123 * This check is here to verify that the assumption holds. */
1124 if (pitch % texture->resource.format->byte_count)
1126 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1127 return WINED3DERR_INVALIDCALL;
1130 if (device->d3d_initialized)
1131 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1133 sub_resource = &texture->sub_resources[0];
1134 surface = sub_resource->u.surface;
1135 if (surface->dc)
1137 wined3d_surface_destroy_dc(surface);
1138 create_dib = TRUE;
1141 wined3d_resource_free_sysmem(&texture->resource);
1143 if ((texture->row_pitch = pitch))
1144 texture->slice_pitch = height * pitch;
1145 else
1146 /* User memory surfaces don't have the regular surface alignment. */
1147 wined3d_format_calculate_pitch(format, 1, width, height,
1148 &texture->row_pitch, &texture->slice_pitch);
1150 texture->resource.format = format;
1151 texture->resource.multisample_type = multisample_type;
1152 texture->resource.multisample_quality = multisample_quality;
1153 texture->resource.width = width;
1154 texture->resource.height = height;
1155 texture->resource.size = texture->slice_pitch;
1156 sub_resource->size = texture->slice_pitch;
1157 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1159 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1160 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1162 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1163 texture->pow2_width = texture->pow2_height = 1;
1164 while (texture->pow2_width < width)
1165 texture->pow2_width <<= 1;
1166 while (texture->pow2_height < height)
1167 texture->pow2_height <<= 1;
1169 else
1171 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1172 texture->pow2_width = width;
1173 texture->pow2_height = height;
1176 if ((texture->user_memory = mem))
1178 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1179 valid_location = WINED3D_LOCATION_USER_MEMORY;
1181 else
1183 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1184 valid_location = WINED3D_LOCATION_SYSMEM;
1187 /* The format might be changed to a format that needs conversion.
1188 * If the surface didn't use PBOs previously but could now, don't
1189 * change it - whatever made us not use PBOs might come back, e.g.
1190 * color keys. */
1191 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1192 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1194 wined3d_texture_validate_location(texture, 0, valid_location);
1195 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1197 if (create_dib)
1198 wined3d_surface_create_dc(surface);
1200 return WINED3D_OK;
1203 /* Context activation is done by the caller. */
1204 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1205 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1207 struct wined3d_texture_sub_resource *sub_resource;
1209 sub_resource = &texture->sub_resources[sub_resource_idx];
1210 if (sub_resource->buffer_object)
1211 return;
1213 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1214 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1215 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1216 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1217 checkGLcall("Create buffer object");
1219 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1220 sub_resource->buffer_object, texture, sub_resource_idx);
1223 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1225 unsigned int sub_count = texture->level_count * texture->layer_count;
1226 unsigned int i;
1228 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1229 | WINED3D_TEXTURE_CONVERTED);
1230 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1231 for (i = 0; i < sub_count; ++i)
1233 wined3d_texture_invalidate_location(texture, i,
1234 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1238 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1240 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1241 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1243 if (!d3d_info->shader_color_key
1244 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1245 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1247 wined3d_texture_force_reload(texture);
1249 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1250 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1253 if (texture->flags & alloc_flag)
1254 return;
1256 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1257 texture->flags |= alloc_flag;
1260 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1261 const struct wined3d_gl_info *gl_info, BOOL multisample)
1263 const struct wined3d_format *format = texture->resource.format;
1265 if (multisample)
1267 DWORD samples;
1269 if (texture->rb_multisample)
1270 return;
1272 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1273 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1274 * be mapped to GL_NV_framebuffer_multisample_coverage.
1276 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1277 * (EQAA), but it does not have an equivalent OpenGL extension. */
1279 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1280 * levels as the count of advertised multisample types for the texture
1281 * format. */
1282 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1284 unsigned int i, count = 0;
1286 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1288 if (format->multisample_types & 1u << i)
1290 if (texture->resource.multisample_quality == count++)
1291 break;
1294 samples = i + 1;
1296 else
1298 samples = texture->resource.multisample_type;
1301 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1302 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1303 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1304 format->glInternal, texture->resource.width, texture->resource.height);
1305 checkGLcall("glRenderbufferStorageMultisample()");
1306 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1308 else
1310 if (texture->rb_resolved)
1311 return;
1313 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1314 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1315 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1316 texture->resource.width, texture->resource.height);
1317 checkGLcall("glRenderbufferStorage()");
1318 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1322 /* Context activation is done by the caller. Context may be NULL in
1323 * WINED3D_NO3D mode. */
1324 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1325 struct wined3d_context *context, DWORD location)
1327 switch (location)
1329 case WINED3D_LOCATION_SYSMEM:
1330 if (texture->resource.heap_memory)
1331 return TRUE;
1333 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1335 ERR("Failed to allocate system memory.\n");
1336 return FALSE;
1338 return TRUE;
1340 case WINED3D_LOCATION_USER_MEMORY:
1341 if (!texture->user_memory)
1342 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1343 return TRUE;
1345 case WINED3D_LOCATION_BUFFER:
1346 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1347 return TRUE;
1349 case WINED3D_LOCATION_TEXTURE_RGB:
1350 wined3d_texture_prepare_texture(texture, context, FALSE);
1351 return TRUE;
1353 case WINED3D_LOCATION_TEXTURE_SRGB:
1354 wined3d_texture_prepare_texture(texture, context, TRUE);
1355 return TRUE;
1357 case WINED3D_LOCATION_DRAWABLE:
1358 if (!texture->swapchain)
1359 ERR("Texture %p does not have a drawable.\n", texture);
1360 return TRUE;
1362 case WINED3D_LOCATION_RB_MULTISAMPLE:
1363 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1364 return TRUE;
1366 case WINED3D_LOCATION_RB_RESOLVED:
1367 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1368 return TRUE;
1370 default:
1371 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1372 return FALSE;
1376 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1378 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1379 FIXME("texture %p stub!\n", texture);
1382 struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1383 unsigned int sub_resource_idx)
1385 UINT sub_count = texture->level_count * texture->layer_count;
1387 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1389 if (sub_resource_idx >= sub_count)
1391 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1392 return NULL;
1395 return &texture->sub_resources[sub_resource_idx];
1398 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1399 UINT layer, const struct wined3d_box *dirty_region)
1401 struct wined3d_context *context;
1402 unsigned int sub_resource_idx;
1404 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1406 if (layer >= texture->layer_count)
1408 WARN("Invalid layer %u specified.\n", layer);
1409 return WINED3DERR_INVALIDCALL;
1411 sub_resource_idx = layer * texture->level_count;
1413 if (dirty_region)
1414 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1416 context = context_acquire(texture->resource.device, NULL);
1417 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1419 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1420 context_release(context);
1421 return E_OUTOFMEMORY;
1423 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1424 context_release(context);
1426 return WINED3D_OK;
1429 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1430 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1431 unsigned int row_pitch, unsigned int slice_pitch)
1433 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1434 context, data, row_pitch, slice_pitch);
1437 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1438 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1439 unsigned int row_pitch, unsigned int slice_pitch)
1441 static const POINT dst_point = {0, 0};
1442 unsigned int texture_level;
1443 RECT src_rect;
1445 texture_level = sub_resource_idx % texture->level_count;
1446 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(texture, texture_level),
1447 wined3d_texture_get_level_height(texture, texture_level));
1449 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1450 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1453 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1454 struct wined3d_context *context, DWORD location)
1456 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1459 /* Context activation is done by the caller. */
1460 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1462 const struct wined3d_format *format = texture->resource.format;
1463 const struct wined3d_gl_info *gl_info = context->gl_info;
1464 const struct wined3d_color_key_conversion *conversion;
1465 GLenum internal;
1467 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1469 if (format->convert)
1471 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1473 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1475 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1476 format = wined3d_get_format(gl_info, conversion->dst_format);
1477 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1480 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1482 if (srgb)
1483 internal = format->glGammaInternal;
1484 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1485 && wined3d_resource_is_offscreen(&texture->resource))
1486 internal = format->rtInternal;
1487 else
1488 internal = format->glInternal;
1490 if (!internal)
1491 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1493 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1495 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1496 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1497 else
1498 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1501 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1503 unsigned int sub_count = texture->level_count * texture->layer_count;
1504 struct wined3d_device *device = texture->resource.device;
1505 struct wined3d_texture_sub_resource *sub_resource;
1506 struct wined3d_renderbuffer_entry *entry, *entry2;
1507 const struct wined3d_gl_info *gl_info = NULL;
1508 struct wined3d_context *context = NULL;
1509 struct wined3d_surface *overlay, *cur;
1510 struct wined3d_surface *surface;
1511 unsigned int i;
1513 for (i = 0; i < sub_count; ++i)
1515 sub_resource = &texture->sub_resources[i];
1516 if (!(surface = sub_resource->u.surface))
1517 continue;
1519 TRACE("surface %p.\n", surface);
1521 if (!context && !list_empty(&surface->renderbuffers))
1523 context = context_acquire(device, NULL);
1524 gl_info = context->gl_info;
1527 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1529 TRACE("Deleting renderbuffer %u.\n", entry->id);
1530 context_gl_resource_released(device, entry->id, TRUE);
1531 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1532 HeapFree(GetProcessHeap(), 0, entry);
1535 if (surface->dc)
1536 wined3d_surface_destroy_dc(surface);
1538 if (surface->overlay_dest)
1539 list_remove(&surface->overlay_entry);
1541 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1543 list_remove(&overlay->overlay_entry);
1544 overlay->overlay_dest = NULL;
1547 if (context)
1548 context_release(context);
1549 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1552 static const struct wined3d_texture_ops texture2d_ops =
1554 texture2d_upload_data,
1555 texture2d_load_location,
1556 texture2d_prepare_texture,
1557 texture2d_cleanup_sub_resources,
1560 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1562 return texture_from_resource(resource);
1565 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1567 return wined3d_texture_incref(texture_from_resource(resource));
1570 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1572 return wined3d_texture_decref(texture_from_resource(resource));
1575 static void texture_resource_preload(struct wined3d_resource *resource)
1577 struct wined3d_texture *texture = texture_from_resource(resource);
1578 struct wined3d_context *context;
1580 context = context_acquire(resource->device, NULL);
1581 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1582 context_release(context);
1585 static void wined3d_texture_unload(struct wined3d_resource *resource)
1587 struct wined3d_texture *texture = texture_from_resource(resource);
1588 UINT sub_count = texture->level_count * texture->layer_count;
1589 struct wined3d_device *device = resource->device;
1590 const struct wined3d_gl_info *gl_info;
1591 struct wined3d_context *context;
1592 UINT i;
1594 TRACE("texture %p.\n", texture);
1596 context = context_acquire(device, NULL);
1597 gl_info = context->gl_info;
1599 for (i = 0; i < sub_count; ++i)
1601 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1603 if (resource->pool != WINED3D_POOL_DEFAULT
1604 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1606 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1608 else
1610 /* We should only get here on device reset/teardown for implicit
1611 * resources. */
1612 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1613 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1614 resource, i, debug_d3dpool(resource->pool));
1615 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1616 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1619 if (sub_resource->buffer_object)
1620 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1622 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1624 struct wined3d_surface *surface = sub_resource->u.surface;
1625 struct wined3d_renderbuffer_entry *entry, *entry2;
1627 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1629 context_gl_resource_released(device, entry->id, TRUE);
1630 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1631 list_remove(&entry->entry);
1632 HeapFree(GetProcessHeap(), 0, entry);
1634 list_init(&surface->renderbuffers);
1635 surface->current_renderbuffer = NULL;
1639 context_release(context);
1641 wined3d_texture_force_reload(texture);
1642 wined3d_texture_unload_gl_texture(texture);
1645 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1646 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1648 const struct wined3d_format *format = resource->format;
1649 struct wined3d_texture_sub_resource *sub_resource;
1650 struct wined3d_device *device = resource->device;
1651 unsigned int fmt_flags = resource->format_flags;
1652 const struct wined3d_gl_info *gl_info = NULL;
1653 struct wined3d_context *context = NULL;
1654 struct wined3d_texture *texture;
1655 struct wined3d_bo_address data;
1656 unsigned int texture_level;
1657 BYTE *base_memory;
1658 BOOL ret;
1660 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1661 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1663 texture = texture_from_resource(resource);
1664 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1665 return E_INVALIDARG;
1667 texture_level = sub_resource_idx % texture->level_count;
1668 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1670 WARN("Map box is invalid.\n");
1671 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1672 return WINED3DERR_INVALIDCALL;
1675 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1676 && !wined3d_texture_check_block_align(texture, texture_level, box))
1678 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1679 debug_box(box), format->block_width, format->block_height);
1680 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1681 return WINED3DERR_INVALIDCALL;
1684 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1686 WARN("Trying to map unmappable texture.\n");
1687 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1688 return WINED3DERR_INVALIDCALL;
1691 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1693 WARN("DC is in use.\n");
1694 return WINED3DERR_INVALIDCALL;
1697 if (sub_resource->map_count)
1699 WARN("Sub-resource is already mapped.\n");
1700 return WINED3DERR_INVALIDCALL;
1703 if (device->d3d_initialized)
1705 context = context_acquire(device, NULL);
1706 gl_info = context->gl_info;
1709 if (flags & WINED3D_MAP_DISCARD)
1711 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1712 wined3d_debug_location(texture->resource.map_binding));
1713 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1714 context, texture->resource.map_binding)))
1715 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1717 else
1719 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1720 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1721 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1724 if (!ret)
1726 ERR("Failed to prepare location.\n");
1727 context_release(context);
1728 return E_OUTOFMEMORY;
1731 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1732 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1734 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1735 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1736 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1737 TRACE("Base memory pointer %p.\n", base_memory);
1739 if (context)
1740 context_release(context);
1742 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1744 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1745 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1747 else
1749 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1752 if (!box)
1754 map_desc->data = base_memory;
1756 else
1758 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1760 /* Compressed textures are block based, so calculate the offset of
1761 * the block that contains the top-left pixel of the mapped box. */
1762 map_desc->data = base_memory
1763 + (box->front * map_desc->slice_pitch)
1764 + ((box->top / format->block_height) * map_desc->row_pitch)
1765 + ((box->left / format->block_width) * format->block_byte_count);
1767 else
1769 map_desc->data = base_memory
1770 + (box->front * map_desc->slice_pitch)
1771 + (box->top * map_desc->row_pitch)
1772 + (box->left * format->byte_count);
1776 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1778 RECT *r = &texture->swapchain->front_buffer_update;
1780 if (!box)
1781 SetRect(r, 0, 0, resource->width, resource->height);
1782 else
1783 SetRect(r, box->left, box->top, box->right, box->bottom);
1784 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1787 ++resource->map_count;
1788 ++sub_resource->map_count;
1790 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1791 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1793 return WINED3D_OK;
1796 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1798 struct wined3d_texture_sub_resource *sub_resource;
1799 struct wined3d_device *device = resource->device;
1800 const struct wined3d_gl_info *gl_info = NULL;
1801 struct wined3d_context *context = NULL;
1802 struct wined3d_texture *texture;
1803 struct wined3d_bo_address data;
1805 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1807 texture = texture_from_resource(resource);
1808 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1809 return E_INVALIDARG;
1811 if (!sub_resource->map_count)
1813 WARN("Trying to unmap unmapped sub-resource.\n");
1814 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1815 return WINED3D_OK;
1816 return WINEDDERR_NOTLOCKED;
1819 if (device->d3d_initialized)
1821 context = context_acquire(device, NULL);
1822 gl_info = context->gl_info;
1825 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1826 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1828 if (context)
1829 context_release(context);
1831 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1833 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1834 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1836 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1838 FIXME("Depth / stencil buffer locking is not implemented.\n");
1841 --sub_resource->map_count;
1842 if (!--resource->map_count && texture->update_map_binding)
1843 wined3d_texture_update_map_binding(texture);
1845 return WINED3D_OK;
1848 static const struct wined3d_resource_ops texture_resource_ops =
1850 texture_resource_incref,
1851 texture_resource_decref,
1852 texture_resource_preload,
1853 wined3d_texture_unload,
1854 texture_resource_sub_resource_map,
1855 texture_resource_sub_resource_unmap,
1858 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1859 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1860 void *parent, const struct wined3d_parent_ops *parent_ops)
1862 struct wined3d_device_parent *device_parent = device->device_parent;
1863 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1864 struct wined3d_surface *surfaces;
1865 UINT pow2_width, pow2_height;
1866 unsigned int i, j;
1867 HRESULT hr;
1869 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
1870 && !gl_info->supported[EXT_TEXTURE_ARRAY])
1872 WARN("OpenGL implementation does not support array textures.\n");
1873 return WINED3DERR_INVALIDCALL;
1876 /* TODO: It should only be possible to create textures for formats
1877 * that are reported as supported. */
1878 if (WINED3DFMT_UNKNOWN >= desc->format)
1880 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1881 return WINED3DERR_INVALIDCALL;
1884 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1885 FIXME("Trying to create a managed texture with dynamic usage.\n");
1886 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1887 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1888 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1889 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1890 FIXME("Trying to create a render target that isn't in the default pool.\n");
1892 pow2_width = desc->width;
1893 pow2_height = desc->height;
1894 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1895 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1897 /* level_count == 0 returns an error as well. */
1898 if (level_count != 1 || layer_count != 1)
1900 if (desc->pool != WINED3D_POOL_SCRATCH)
1902 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
1903 return WINED3DERR_INVALIDCALL;
1906 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
1908 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1910 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1912 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1914 /* TODO: Add support for non-power-of-two compressed textures. */
1915 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1916 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1918 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1919 desc->width, desc->height);
1920 return WINED3DERR_NOTAVAILABLE;
1923 /* Find the nearest pow2 match. */
1924 pow2_width = pow2_height = 1;
1925 while (pow2_width < desc->width)
1926 pow2_width <<= 1;
1927 while (pow2_height < desc->height)
1928 pow2_height <<= 1;
1929 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1932 texture->pow2_width = pow2_width;
1933 texture->pow2_height = pow2_height;
1935 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1936 && (desc->usage & WINED3DUSAGE_TEXTURE))
1938 /* One of four options:
1939 * 1: Do the same as we do with NPOT and scale the texture. (Any
1940 * texture ops would require the texture to be scaled which is
1941 * potentially slow.)
1942 * 2: Set the texture to the maximum size (bad idea).
1943 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1944 * 4: Create the surface, but allow it to be used only for DirectDraw
1945 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1946 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1947 * the render target. */
1948 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1950 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1951 return WINED3DERR_NOTAVAILABLE;
1954 /* We should never use this surface in combination with OpenGL. */
1955 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1958 /* Calculate levels for mip mapping. */
1959 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1961 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1963 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1964 return WINED3DERR_INVALIDCALL;
1967 if (level_count != 1)
1969 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1970 return WINED3DERR_INVALIDCALL;
1974 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1975 flags, device, parent, parent_ops, &texture_resource_ops)))
1977 WARN("Failed to initialize texture, returning %#x.\n", hr);
1978 return hr;
1981 /* Precalculated scaling for 'faked' non power of two texture coords. */
1982 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1984 texture->pow2_matrix[0] = (float)desc->width;
1985 texture->pow2_matrix[5] = (float)desc->height;
1986 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1987 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1989 else
1991 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1993 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1994 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1995 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1997 else
1999 texture->pow2_matrix[0] = 1.0f;
2000 texture->pow2_matrix[5] = 1.0f;
2002 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2003 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2004 else if (layer_count > 1)
2005 texture->target = GL_TEXTURE_2D_ARRAY;
2006 else
2007 texture->target = GL_TEXTURE_2D;
2009 texture->pow2_matrix[10] = 1.0f;
2010 texture->pow2_matrix[15] = 1.0f;
2011 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2013 if (wined3d_texture_use_pbo(texture, gl_info))
2014 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2016 if (level_count > ~(SIZE_T)0 / layer_count
2017 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2019 wined3d_texture_cleanup_sync(texture);
2020 return E_OUTOFMEMORY;
2023 /* Generate all the surfaces. */
2024 for (i = 0; i < texture->level_count; ++i)
2026 for (j = 0; j < texture->layer_count; ++j)
2028 static const GLenum cube_targets[6] =
2030 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2031 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2032 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2033 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2034 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2035 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2037 struct wined3d_texture_sub_resource *sub_resource;
2038 unsigned int idx = j * texture->level_count + i;
2039 struct wined3d_surface *surface;
2041 surface = &surfaces[idx];
2042 surface->container = texture;
2043 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2044 surface->texture_level = i;
2045 surface->texture_layer = j;
2046 list_init(&surface->renderbuffers);
2047 list_init(&surface->overlays);
2049 sub_resource = &texture->sub_resources[idx];
2050 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2051 sub_resource->u.surface = surface;
2052 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2054 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2055 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2058 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2059 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2061 WARN("Failed to create surface parent, hr %#x.\n", hr);
2062 sub_resource->parent = NULL;
2063 wined3d_texture_cleanup_sync(texture);
2064 return hr;
2067 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2069 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2071 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2072 && FAILED(hr = wined3d_surface_create_dc(surface)))
2074 wined3d_texture_cleanup_sync(texture);
2075 return hr;
2080 return WINED3D_OK;
2083 /* This call just uploads data, the caller is responsible for binding the
2084 * correct texture. */
2085 /* Context activation is done by the caller. */
2086 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2087 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
2088 unsigned int row_pitch, unsigned int slice_pitch)
2090 const struct wined3d_format *format = texture->resource.format;
2091 unsigned int level = sub_resource_idx % texture->level_count;
2092 const struct wined3d_gl_info *gl_info = context->gl_info;
2093 unsigned int dst_row_pitch, dst_slice_pitch;
2094 unsigned int width, height, depth;
2095 const void *mem = data->addr;
2096 void *converted_mem = NULL;
2098 TRACE("texture %p, sub_resource_idx %u, context %p, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2099 texture, sub_resource_idx, context, data->buffer_object, data->addr, row_pitch, slice_pitch);
2101 width = wined3d_texture_get_level_width(texture, level);
2102 height = wined3d_texture_get_level_height(texture, level);
2103 depth = wined3d_texture_get_level_depth(texture, level);
2105 if (format->convert)
2107 if (data->buffer_object)
2108 ERR("Loading a converted texture from a PBO.\n");
2109 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2110 ERR("Converting a block-based format.\n");
2112 dst_row_pitch = width * format->conv_byte_count;
2113 dst_slice_pitch = dst_row_pitch * height;
2115 converted_mem = wined3d_calloc(depth, dst_slice_pitch);
2116 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2117 dst_row_pitch, dst_slice_pitch, width, height, depth);
2118 mem = converted_mem;
2120 else
2122 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2123 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2124 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2127 if (data->buffer_object)
2129 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2130 checkGLcall("glBindBuffer");
2133 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, 0, 0, 0,
2134 width, height, depth, format->glFormat, format->glType, mem));
2135 checkGLcall("glTexSubImage3D");
2137 if (data->buffer_object)
2139 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2140 checkGLcall("glBindBuffer");
2143 HeapFree(GetProcessHeap(), 0, converted_mem);
2146 /* Context activation is done by the caller. */
2147 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2148 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2150 const struct wined3d_format *format = texture->resource.format;
2151 const struct wined3d_gl_info *gl_info = context->gl_info;
2153 if (format->convert)
2155 FIXME("Attempting to download a converted volume, format %s.\n",
2156 debug_d3dformat(format->id));
2157 return;
2160 if (data->buffer_object)
2162 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2163 checkGLcall("glBindBuffer");
2166 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2167 format->glFormat, format->glType, data->addr);
2168 checkGLcall("glGetTexImage");
2170 if (data->buffer_object)
2172 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2173 checkGLcall("glBindBuffer");
2178 /* Context activation is done by the caller. */
2179 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2180 struct wined3d_context *context, BOOL dest_is_srgb)
2182 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2183 unsigned int row_pitch, slice_pitch;
2184 struct wined3d_bo_address data;
2186 /* Optimisations are possible, but the effort should be put into either
2187 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2188 * picked the wrong copy for the original upload and fixing that.
2190 * Also keep in mind that we want to avoid using resource.heap_memory
2191 * for DEFAULT pool surfaces. */
2192 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2193 data.buffer_object = 0;
2194 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2195 return;
2197 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2198 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2199 texture3d_download_data(texture, sub_resource_idx, context, &data);
2200 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2201 texture3d_upload_data(texture, sub_resource_idx, context,
2202 wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2204 HeapFree(GetProcessHeap(), 0, data.addr);
2207 /* Context activation is done by the caller. */
2208 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2209 struct wined3d_context *context, DWORD location)
2211 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2212 DWORD required_access = wined3d_resource_access_from_location(location);
2213 unsigned int row_pitch, slice_pitch;
2215 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2216 texture, sub_resource_idx, context, wined3d_debug_location(location));
2218 TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource->locations));
2220 if ((sub_resource->locations & location) == location)
2222 TRACE("Location(s) already up to date.\n");
2223 return TRUE;
2226 if ((texture->resource.access_flags & required_access) != required_access)
2228 ERR("Operation requires %#x access, but volume only has %#x.\n",
2229 required_access, texture->resource.access_flags);
2230 return FALSE;
2233 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2234 return FALSE;
2236 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
2238 TRACE("Volume previously discarded, nothing to do.\n");
2239 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2240 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
2241 goto done;
2244 switch (location)
2246 case WINED3D_LOCATION_TEXTURE_RGB:
2247 case WINED3D_LOCATION_TEXTURE_SRGB:
2248 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2250 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2251 data.addr += sub_resource->offset;
2252 wined3d_texture_bind_and_dirtify(texture, context,
2253 location == WINED3D_LOCATION_TEXTURE_SRGB);
2254 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2255 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2257 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2259 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2260 wined3d_texture_bind_and_dirtify(texture, context,
2261 location == WINED3D_LOCATION_TEXTURE_SRGB);
2262 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2263 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2265 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2267 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2269 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2271 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2273 else
2275 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2276 return FALSE;
2278 break;
2280 case WINED3D_LOCATION_SYSMEM:
2281 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2283 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2285 data.addr += sub_resource->offset;
2286 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2287 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2288 else
2289 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2291 texture3d_download_data(texture, sub_resource_idx, context, &data);
2292 ++texture->download_count;
2294 else
2296 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2297 wined3d_debug_location(sub_resource->locations));
2298 return FALSE;
2300 break;
2302 case WINED3D_LOCATION_BUFFER:
2303 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2305 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2307 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2308 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2309 else
2310 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2312 texture3d_download_data(texture, sub_resource_idx, context, &data);
2314 else
2316 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2317 wined3d_debug_location(sub_resource->locations));
2318 return FALSE;
2320 break;
2322 default:
2323 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2324 wined3d_debug_location(sub_resource->locations));
2325 return FALSE;
2328 done:
2329 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2331 return TRUE;
2334 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2336 const struct wined3d_format *format = texture->resource.format;
2337 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2338 unsigned int sub_count = texture->level_count * texture->layer_count;
2339 const struct wined3d_gl_info *gl_info = context->gl_info;
2340 unsigned int i;
2342 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2344 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2346 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2347 wined3d_texture_get_level_width(texture, 0),
2348 wined3d_texture_get_level_height(texture, 0),
2349 wined3d_texture_get_level_depth(texture, 0)));
2350 checkGLcall("glTexStorage3D");
2352 else
2354 for (i = 0; i < sub_count; ++i)
2356 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2357 wined3d_texture_get_level_width(texture, i),
2358 wined3d_texture_get_level_height(texture, i),
2359 wined3d_texture_get_level_depth(texture, i),
2360 0, format->glFormat, format->glType, NULL));
2361 checkGLcall("glTexImage3D");
2366 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2370 static const struct wined3d_texture_ops texture3d_ops =
2372 texture3d_upload_data,
2373 texture3d_load_location,
2374 texture3d_prepare_texture,
2375 texture3d_cleanup_sub_resources,
2378 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2379 unsigned int level, const struct wined3d_box *box)
2381 const struct wined3d_format *format = texture->resource.format;
2382 unsigned int height = wined3d_texture_get_level_height(texture, level);
2383 unsigned int width = wined3d_texture_get_level_width(texture, level);
2384 unsigned int width_mask, height_mask;
2386 if ((box->left >= box->right)
2387 || (box->top >= box->bottom)
2388 || (box->right > width)
2389 || (box->bottom > height))
2390 return FALSE;
2392 /* This assumes power of two block sizes, but NPOT block sizes would be
2393 * silly anyway.
2395 * This also assumes that the format's block depth is 1. */
2396 width_mask = format->block_width - 1;
2397 height_mask = format->block_height - 1;
2399 if ((box->left & width_mask) || (box->top & height_mask)
2400 || (box->right & width_mask && box->right != width)
2401 || (box->bottom & height_mask && box->bottom != height))
2402 return FALSE;
2404 return TRUE;
2407 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2408 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2409 const struct wined3d_parent_ops *parent_ops)
2411 struct wined3d_device_parent *device_parent = device->device_parent;
2412 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2413 unsigned int i;
2414 HRESULT hr;
2416 if (layer_count != 1)
2418 ERR("Invalid layer count for volume texture.\n");
2419 return E_INVALIDARG;
2422 /* TODO: It should only be possible to create textures for formats
2423 * that are reported as supported. */
2424 if (WINED3DFMT_UNKNOWN >= desc->format)
2426 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2427 return WINED3DERR_INVALIDCALL;
2430 if (!gl_info->supported[EXT_TEXTURE3D])
2432 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2433 return WINED3DERR_INVALIDCALL;
2436 /* Calculate levels for mip mapping. */
2437 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2439 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2441 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2442 return WINED3DERR_INVALIDCALL;
2445 if (level_count != 1)
2447 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2448 return WINED3DERR_INVALIDCALL;
2452 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2453 || desc->pool == WINED3D_POOL_SCRATCH))
2455 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2456 return WINED3DERR_INVALIDCALL;
2459 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2461 UINT pow2_w, pow2_h, pow2_d;
2462 pow2_w = 1;
2463 while (pow2_w < desc->width)
2464 pow2_w <<= 1;
2465 pow2_h = 1;
2466 while (pow2_h < desc->height)
2467 pow2_h <<= 1;
2468 pow2_d = 1;
2469 while (pow2_d < desc->depth)
2470 pow2_d <<= 1;
2472 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2474 if (desc->pool == WINED3D_POOL_SCRATCH)
2476 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2478 else
2480 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2481 desc->width, desc->height, desc->depth);
2482 return WINED3DERR_INVALIDCALL;
2487 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2488 0, device, parent, parent_ops, &texture_resource_ops)))
2490 WARN("Failed to initialize texture, returning %#x.\n", hr);
2491 return hr;
2494 texture->pow2_matrix[0] = 1.0f;
2495 texture->pow2_matrix[5] = 1.0f;
2496 texture->pow2_matrix[10] = 1.0f;
2497 texture->pow2_matrix[15] = 1.0f;
2498 texture->target = GL_TEXTURE_3D;
2500 if (wined3d_texture_use_pbo(texture, gl_info))
2502 wined3d_resource_free_sysmem(&texture->resource);
2503 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2506 /* Generate all the surfaces. */
2507 for (i = 0; i < texture->level_count; ++i)
2509 struct wined3d_texture_sub_resource *sub_resource;
2511 sub_resource = &texture->sub_resources[i];
2512 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2514 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2515 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2517 WARN("Failed to create volume parent, hr %#x.\n", hr);
2518 sub_resource->parent = NULL;
2519 wined3d_texture_cleanup_sync(texture);
2520 return hr;
2523 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2525 TRACE("Created volume level %u.\n", i);
2528 return WINED3D_OK;
2531 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2532 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2533 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2535 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2537 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2538 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2539 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2540 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2542 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2543 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2544 return WINED3DERR_INVALIDCALL;
2546 if (src_texture)
2548 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2549 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2550 return WINED3DERR_INVALIDCALL;
2553 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2554 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2557 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2558 unsigned int sub_resource_idx, LONG *x, LONG *y)
2560 struct wined3d_surface *surface;
2562 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2564 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2565 || sub_resource_idx >= texture->level_count * texture->layer_count)
2567 WARN("Invalid sub-resource specified.\n");
2568 return WINEDDERR_NOTAOVERLAYSURFACE;
2571 surface = texture->sub_resources[sub_resource_idx].u.surface;
2572 if (!surface->overlay_dest)
2574 TRACE("Overlay not visible.\n");
2575 *x = 0;
2576 *y = 0;
2577 return WINEDDERR_OVERLAYNOTVISIBLE;
2580 *x = surface->overlay_destrect.left;
2581 *y = surface->overlay_destrect.top;
2583 TRACE("Returning position %d, %d.\n", *x, *y);
2585 return WINED3D_OK;
2588 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2589 unsigned int sub_resource_idx, LONG x, LONG y)
2591 struct wined3d_texture_sub_resource *sub_resource;
2592 struct wined3d_surface *surface;
2593 LONG w, h;
2595 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2597 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2598 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2600 WARN("Invalid sub-resource specified.\n");
2601 return WINEDDERR_NOTAOVERLAYSURFACE;
2604 surface = sub_resource->u.surface;
2605 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2606 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2607 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2609 return WINED3D_OK;
2612 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2613 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2614 const RECT *dst_rect, DWORD flags)
2616 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2617 struct wined3d_surface *surface, *dst_surface;
2619 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2620 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2621 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2622 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2624 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2625 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2627 WARN("Invalid sub-resource specified.\n");
2628 return WINEDDERR_NOTAOVERLAYSURFACE;
2631 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2632 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2634 WARN("Invalid destination sub-resource specified.\n");
2635 return WINED3DERR_INVALIDCALL;
2638 surface = sub_resource->u.surface;
2639 if (src_rect)
2640 surface->overlay_srcrect = *src_rect;
2641 else
2642 SetRect(&surface->overlay_srcrect, 0, 0,
2643 wined3d_texture_get_level_width(texture, surface->texture_level),
2644 wined3d_texture_get_level_height(texture, surface->texture_level));
2646 dst_surface = dst_sub_resource->u.surface;
2647 if (dst_rect)
2648 surface->overlay_destrect = *dst_rect;
2649 else
2650 SetRect(&surface->overlay_destrect, 0, 0,
2651 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2652 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2654 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2656 surface->overlay_dest = NULL;
2657 list_remove(&surface->overlay_entry);
2660 if (flags & WINEDDOVER_SHOW)
2662 if (surface->overlay_dest != dst_surface)
2664 surface->overlay_dest = dst_surface;
2665 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2668 else if (flags & WINEDDOVER_HIDE)
2670 /* Tests show that the rectangles are erased on hide. */
2671 SetRectEmpty(&surface->overlay_srcrect);
2672 SetRectEmpty(&surface->overlay_destrect);
2673 surface->overlay_dest = NULL;
2676 return WINED3D_OK;
2679 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2681 unsigned int sub_count = texture->level_count * texture->layer_count;
2683 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2685 if (sub_resource_idx >= sub_count)
2687 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2688 return NULL;
2691 return texture->sub_resources[sub_resource_idx].parent;
2694 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2695 unsigned int sub_resource_idx, void *parent)
2697 unsigned int sub_count = texture->level_count * texture->layer_count;
2699 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2701 if (sub_resource_idx >= sub_count)
2703 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2704 return;
2707 texture->sub_resources[sub_resource_idx].parent = parent;
2710 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2711 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2713 unsigned int sub_count = texture->level_count * texture->layer_count;
2714 const struct wined3d_resource *resource;
2715 unsigned int level_idx;
2717 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2719 if (sub_resource_idx >= sub_count)
2721 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2722 return WINED3DERR_INVALIDCALL;
2725 resource = &texture->resource;
2726 desc->format = resource->format->id;
2727 desc->multisample_type = resource->multisample_type;
2728 desc->multisample_quality = resource->multisample_quality;
2729 desc->usage = resource->usage;
2730 desc->pool = resource->pool;
2732 level_idx = sub_resource_idx % texture->level_count;
2733 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2734 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2735 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2736 desc->size = texture->sub_resources[sub_resource_idx].size;
2738 return WINED3D_OK;
2741 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2742 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2743 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2745 struct wined3d_texture *object;
2746 HRESULT hr;
2748 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2749 "parent %p, parent_ops %p, texture %p.\n",
2750 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2752 if (!layer_count)
2754 WARN("Invalid layer count.\n");
2755 return E_INVALIDARG;
2757 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2759 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2760 layer_count = 6;
2763 if (!level_count)
2765 WARN("Invalid level count.\n");
2766 return WINED3DERR_INVALIDCALL;
2769 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2771 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2773 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2774 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2776 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2777 desc->multisample_quality);
2778 return WINED3DERR_NOTAVAILABLE;
2780 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2781 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2782 || desc->multisample_quality))
2784 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2785 desc->multisample_quality);
2786 return WINED3DERR_NOTAVAILABLE;
2790 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2791 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2792 return E_OUTOFMEMORY;
2794 switch (desc->resource_type)
2796 case WINED3D_RTYPE_TEXTURE_2D:
2797 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2798 break;
2800 case WINED3D_RTYPE_TEXTURE_3D:
2801 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2802 break;
2804 default:
2805 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2806 hr = WINED3DERR_INVALIDCALL;
2807 break;
2810 if (FAILED(hr))
2812 WARN("Failed to initialize texture, returning %#x.\n", hr);
2813 HeapFree(GetProcessHeap(), 0, object);
2814 return hr;
2817 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2818 * in this case. */
2819 if (data)
2821 unsigned int sub_count = level_count * layer_count;
2822 struct wined3d_context *context;
2823 unsigned int i;
2825 for (i = 0; i < sub_count; ++i)
2827 if (!data[i].data)
2829 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2830 wined3d_texture_cleanup_sync(object);
2831 HeapFree(GetProcessHeap(), 0, object);
2832 return E_INVALIDARG;
2836 context = context_acquire(device, NULL);
2838 wined3d_texture_prepare_texture(object, context, FALSE);
2839 wined3d_texture_bind_and_dirtify(object, context, FALSE);
2841 for (i = 0; i < sub_count; ++i)
2843 const struct wined3d_const_bo_address addr = {0, data[i].data};
2845 wined3d_texture_upload_data(object, i, context, &addr, data[i].row_pitch, data[i].slice_pitch);
2846 wined3d_texture_validate_location(object, i, WINED3D_LOCATION_TEXTURE_RGB);
2847 wined3d_texture_invalidate_location(object, i, ~WINED3D_LOCATION_TEXTURE_RGB);
2850 context_release(context);
2853 TRACE("Created texture %p.\n", object);
2854 *texture = object;
2856 return WINED3D_OK;
2859 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2861 struct wined3d_device *device = texture->resource.device;
2862 struct wined3d_texture_sub_resource *sub_resource;
2863 struct wined3d_context *context = NULL;
2864 struct wined3d_surface *surface;
2865 HRESULT hr = WINED3D_OK;
2867 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2869 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2870 return WINED3DERR_INVALIDCALL;
2872 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2874 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2875 return WINED3DERR_INVALIDCALL;
2878 surface = sub_resource->u.surface;
2880 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2881 return WINED3DERR_INVALIDCALL;
2883 if (device->d3d_initialized)
2884 context = context_acquire(device, NULL);
2886 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2887 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2889 if (!surface->dc)
2890 hr = wined3d_surface_create_dc(surface);
2891 if (context)
2892 context_release(context);
2893 if (FAILED(hr))
2894 return WINED3DERR_INVALIDCALL;
2896 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2897 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2898 ++texture->resource.map_count;
2899 ++sub_resource->map_count;
2901 *dc = surface->dc;
2902 TRACE("Returning dc %p.\n", *dc);
2904 return hr;
2907 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2909 struct wined3d_device *device = texture->resource.device;
2910 struct wined3d_texture_sub_resource *sub_resource;
2911 struct wined3d_surface *surface;
2913 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2915 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2916 return WINED3DERR_INVALIDCALL;
2918 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2920 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2921 return WINED3DERR_INVALIDCALL;
2924 surface = sub_resource->u.surface;
2926 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2927 return WINED3DERR_INVALIDCALL;
2929 if (surface->dc != dc)
2931 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2932 return WINED3DERR_INVALIDCALL;
2935 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2936 wined3d_surface_destroy_dc(surface);
2938 --sub_resource->map_count;
2939 if (!--texture->resource.map_count && texture->update_map_binding)
2940 wined3d_texture_update_map_binding(texture);
2941 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2942 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2944 return WINED3D_OK;