reg/tests: Test import with non-standard registry file headers.
[wine.git] / dlls / wined3d / texture.c
blob20a6c0ece4c5877119626039b0d857287f008e9f
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 case WINED3D_LOCATION_USER_MEMORY:
92 return WINED3D_RESOURCE_ACCESS_CPU;
94 case WINED3D_LOCATION_BUFFER:
95 case WINED3D_LOCATION_DRAWABLE:
96 case WINED3D_LOCATION_TEXTURE_RGB:
97 case WINED3D_LOCATION_TEXTURE_SRGB:
98 case WINED3D_LOCATION_RB_MULTISAMPLE:
99 case WINED3D_LOCATION_RB_RESOLVED:
100 return WINED3D_RESOURCE_ACCESS_GPU;
102 default:
103 FIXME("Unhandled location %#x.\n", location);
104 return 0;
108 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
110 struct wined3d_texture_sub_resource *sub_resource;
111 unsigned int i, sub_count;
113 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
114 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
116 TRACE("Not evicting system memory for texture %p.\n", texture);
117 return;
120 TRACE("Evicting system memory for texture %p.\n", texture);
122 sub_count = texture->level_count * texture->layer_count;
123 for (i = 0; i < sub_count; ++i)
125 sub_resource = &texture->sub_resources[i];
126 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
127 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
128 i, texture);
129 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
131 wined3d_resource_free_sysmem(&texture->resource);
134 void wined3d_texture_validate_location(struct wined3d_texture *texture,
135 unsigned int sub_resource_idx, DWORD location)
137 struct wined3d_texture_sub_resource *sub_resource;
138 DWORD previous_locations;
140 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
141 texture, sub_resource_idx, wined3d_debug_location(location));
143 sub_resource = &texture->sub_resources[sub_resource_idx];
144 previous_locations = sub_resource->locations;
145 sub_resource->locations |= location;
146 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
147 && !--texture->sysmem_count)
148 wined3d_texture_evict_sysmem(texture);
150 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
153 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
155 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
158 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
159 unsigned int sub_resource_idx, DWORD location)
161 struct wined3d_texture_sub_resource *sub_resource;
163 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
164 texture, sub_resource_idx, wined3d_debug_location(location));
166 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
167 wined3d_texture_set_dirty(texture);
169 sub_resource = &texture->sub_resources[sub_resource_idx];
170 sub_resource->locations &= ~location;
171 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
172 ++texture->sysmem_count;
174 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
176 if (!sub_resource->locations)
177 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
178 sub_resource_idx, texture);
181 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
182 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
184 unsigned int size = texture->sub_resources[sub_resource_idx].size;
185 struct wined3d_device *device = texture->resource.device;
186 const struct wined3d_gl_info *gl_info;
187 struct wined3d_bo_address dst, src;
189 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
190 return FALSE;
192 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
193 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
194 texture->sub_resources[sub_resource_idx].locations);
196 if (dst.buffer_object)
198 context = context_acquire(device, NULL, 0);
199 gl_info = context->gl_info;
200 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
201 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
202 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
203 checkGLcall("PBO upload");
204 context_release(context);
205 return TRUE;
208 if (src.buffer_object)
210 context = context_acquire(device, NULL, 0);
211 gl_info = context->gl_info;
212 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
213 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
214 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
215 checkGLcall("PBO download");
216 context_release(context);
217 return TRUE;
220 memcpy(dst.addr, src.addr, size);
221 return TRUE;
224 /* Context activation is done by the caller. Context may be NULL in
225 * WINED3D_NO3D mode. */
226 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
227 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
229 static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
230 | WINED3D_LOCATION_BUFFER;
231 DWORD current = texture->sub_resources[sub_resource_idx].locations;
232 BOOL ret;
234 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
235 texture, sub_resource_idx, context, wined3d_debug_location(location));
237 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
239 if (current & location)
241 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
242 return TRUE;
245 if (WARN_ON(d3d))
247 DWORD required_access = wined3d_resource_access_from_location(location);
248 if ((texture->resource.access_flags & required_access) != required_access)
249 WARN("Operation requires %#x access, but texture only has %#x.\n",
250 required_access, texture->resource.access_flags);
253 if (current & WINED3D_LOCATION_DISCARDED)
255 TRACE("Sub-resource previously discarded, nothing to do.\n");
256 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
257 return FALSE;
258 wined3d_texture_validate_location(texture, sub_resource_idx, location);
259 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
260 return TRUE;
263 if (!current)
265 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
266 sub_resource_idx, texture);
267 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
268 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
271 if ((location & sysmem_locations) && (current & sysmem_locations))
272 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
273 else
274 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
276 if (ret)
277 wined3d_texture_validate_location(texture, sub_resource_idx, location);
279 return ret;
282 /* Context activation is done by the caller. */
283 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
284 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
286 BYTE *memory;
288 if (!data->buffer_object)
289 return data->addr;
291 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
293 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
295 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
296 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
298 else
300 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
301 memory += (INT_PTR)data->addr;
304 GL_EXTCALL(glBindBuffer(binding, 0));
305 checkGLcall("Map buffer object");
307 return memory;
310 /* Context activation is done by the caller. */
311 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
312 const struct wined3d_gl_info *gl_info, GLenum binding)
314 if (!data->buffer_object)
315 return;
317 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
318 GL_EXTCALL(glUnmapBuffer(binding));
319 GL_EXTCALL(glBindBuffer(binding, 0));
320 checkGLcall("Unmap buffer object");
323 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
324 struct wined3d_bo_address *data, DWORD locations)
326 struct wined3d_texture_sub_resource *sub_resource;
328 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
329 texture, sub_resource_idx, data, wined3d_debug_location(locations));
331 sub_resource = &texture->sub_resources[sub_resource_idx];
332 if (locations & WINED3D_LOCATION_BUFFER)
334 data->addr = NULL;
335 data->buffer_object = sub_resource->buffer_object;
336 return;
338 if (locations & WINED3D_LOCATION_USER_MEMORY)
340 data->addr = texture->user_memory;
341 data->buffer_object = 0;
342 return;
344 if (locations & WINED3D_LOCATION_SYSMEM)
346 data->addr = texture->resource.heap_memory;
347 data->addr += sub_resource->offset;
348 data->buffer_object = 0;
349 return;
352 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
353 data->addr = NULL;
354 data->buffer_object = 0;
357 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
358 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
359 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
360 const struct wined3d_resource_ops *resource_ops)
362 unsigned int i, j, size, offset = 0;
363 const struct wined3d_format *format;
364 HRESULT hr;
366 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
367 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
368 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
369 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
370 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
371 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
372 flags, device, parent, parent_ops, resource_ops);
374 if (!desc->width || !desc->height || !desc->depth)
375 return WINED3DERR_INVALIDCALL;
377 format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
379 for (i = 0; i < layer_count; ++i)
381 for (j = 0; j < level_count; ++j)
383 unsigned int idx = i * level_count + j;
385 size = wined3d_format_calculate_size(format, device->surface_alignment,
386 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
387 texture->sub_resources[idx].offset = offset;
388 texture->sub_resources[idx].size = size;
389 offset += size;
391 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
394 if (!offset)
395 return WINED3DERR_INVALIDCALL;
397 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
398 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
399 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
401 static unsigned int once;
403 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
404 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
405 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
406 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
407 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
408 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
410 WARN("Failed to initialize resource, returning %#x\n", hr);
411 return hr;
413 wined3d_resource_update_draw_binding(&texture->resource);
414 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
415 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
417 texture->texture_ops = texture_ops;
419 texture->layer_count = layer_count;
420 texture->level_count = level_count;
421 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
422 texture->lod = 0;
423 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
424 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
425 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
426 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
427 texture->flags |= WINED3D_TEXTURE_GET_DC;
428 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
429 texture->flags |= WINED3D_TEXTURE_DISCARD;
431 return WINED3D_OK;
434 /* Context activation is done by the caller. */
435 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
436 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
438 GLuint *buffer_object;
440 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
441 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
442 checkGLcall("glDeleteBuffers");
443 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
444 *buffer_object = 0;
446 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
447 *buffer_object, texture, sub_resource_idx);
450 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
452 unsigned int sub_count = texture->level_count * texture->layer_count;
453 const struct wined3d_device *device = texture->resource.device;
454 DWORD map_binding = texture->update_map_binding;
455 struct wined3d_context *context = NULL;
456 unsigned int i;
458 if (device->d3d_initialized)
459 context = context_acquire(device, NULL, 0);
461 for (i = 0; i < sub_count; ++i)
463 if (texture->sub_resources[i].locations == texture->resource.map_binding
464 && !wined3d_texture_load_location(texture, i, context, map_binding))
465 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
466 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
467 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
470 if (context)
471 context_release(context);
473 texture->resource.map_binding = map_binding;
474 texture->update_map_binding = 0;
477 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
479 texture->update_map_binding = map_binding;
480 if (!texture->resource.map_count)
481 wined3d_texture_update_map_binding(texture);
484 /* A GL context is provided by the caller */
485 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
486 struct gl_texture *tex)
488 context_gl_resource_released(device, tex->name, FALSE);
489 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
490 tex->name = 0;
493 /* Context activation is done by the caller. */
494 /* The caller is responsible for binding the correct texture. */
495 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
496 GLenum gl_internal_format, const struct wined3d_format *format,
497 const struct wined3d_gl_info *gl_info)
499 unsigned int i, sub_call_count;
501 sub_call_count = texture->level_count;
502 if (texture->target != GL_TEXTURE_2D_ARRAY)
503 sub_call_count *= texture->layer_count;
505 for (i = 0; i < sub_call_count; ++i)
507 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
508 GLsizei width, height;
510 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
511 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
512 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
514 height *= format->height_scale.numerator;
515 height /= format->height_scale.denominator;
518 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
519 surface, surface->texture_target, surface->texture_level, width, height);
521 if (texture->target == GL_TEXTURE_2D_ARRAY)
523 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
524 gl_internal_format, width, height, texture->layer_count, 0,
525 format->glFormat, format->glType, NULL));
526 checkGLcall("glTexImage3D");
528 else
530 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
531 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
532 checkGLcall("glTexImage2D");
537 /* Context activation is done by the caller. */
538 /* The caller is responsible for binding the correct texture. */
539 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
540 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
542 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
543 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
545 if (texture->target == GL_TEXTURE_2D_ARRAY)
547 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
548 width, height, texture->layer_count));
549 checkGLcall("glTexStorage3D");
551 else
553 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
554 width, height));
555 checkGLcall("glTexStorage2D");
559 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
561 struct wined3d_device *device = texture->resource.device;
562 const struct wined3d_gl_info *gl_info = NULL;
563 struct wined3d_context *context = NULL;
565 if (texture->texture_rgb.name || texture->texture_srgb.name
566 || texture->rb_multisample || texture->rb_resolved)
568 context = context_acquire(device, NULL, 0);
569 gl_info = context->gl_info;
572 if (texture->texture_rgb.name)
573 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
575 if (texture->texture_srgb.name)
576 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
578 if (texture->rb_multisample)
580 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
581 context_gl_resource_released(device, texture->rb_multisample, TRUE);
582 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
583 texture->rb_multisample = 0;
586 if (texture->rb_resolved)
588 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
589 context_gl_resource_released(device, texture->rb_resolved, TRUE);
590 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
591 texture->rb_resolved = 0;
594 if (context) context_release(context);
596 wined3d_texture_set_dirty(texture);
598 resource_unload(&texture->resource);
601 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
603 unsigned int sub_count = texture->level_count * texture->layer_count;
604 struct wined3d_texture_sub_resource *sub_resource;
605 unsigned int i;
607 for (i = 0; i < sub_count; ++i)
609 sub_resource = &texture->sub_resources[i];
610 if (sub_resource->parent)
612 TRACE("sub-resource %u.\n", i);
613 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
614 sub_resource->parent = NULL;
619 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
621 unsigned int sub_count = texture->level_count * texture->layer_count;
622 struct wined3d_device *device = texture->resource.device;
623 struct wined3d_context *context = NULL;
624 const struct wined3d_gl_info *gl_info;
625 GLuint buffer_object;
626 unsigned int i;
628 TRACE("texture %p.\n", texture);
630 for (i = 0; i < sub_count; ++i)
632 if (!(buffer_object = texture->sub_resources[i].buffer_object))
633 continue;
635 TRACE("Deleting buffer object %u.\n", buffer_object);
637 /* We may not be able to get a context in wined3d_texture_cleanup() in
638 * general, but if a buffer object was previously created we can. */
639 if (!context)
641 context = context_acquire(device, NULL, 0);
642 gl_info = context->gl_info;
645 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
647 if (context)
648 context_release(context);
650 texture->texture_ops->texture_cleanup_sub_resources(texture);
651 wined3d_texture_unload_gl_texture(texture);
654 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
656 texture->swapchain = swapchain;
657 wined3d_resource_update_draw_binding(&texture->resource);
660 /* Context activation is done by the caller. */
661 void wined3d_texture_bind(struct wined3d_texture *texture,
662 struct wined3d_context *context, BOOL srgb)
664 const struct wined3d_gl_info *gl_info = context->gl_info;
665 const struct wined3d_format *format = texture->resource.format;
666 const struct color_fixup_desc fixup = format->color_fixup;
667 struct gl_texture *gl_tex;
668 GLenum target;
670 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
672 if (!needs_separate_srgb_gl_texture(context, texture))
673 srgb = FALSE;
675 /* sRGB mode cache for preload() calls outside drawprim. */
676 if (srgb)
677 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
678 else
679 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
681 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
682 target = texture->target;
684 if (gl_tex->name)
686 context_bind_texture(context, target, gl_tex->name);
687 return;
690 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
691 checkGLcall("glGenTextures");
692 TRACE("Generated texture %d.\n", gl_tex->name);
694 if (!gl_tex->name)
696 ERR("Failed to generate a texture name.\n");
697 return;
700 /* Initialise the state of the texture object to the OpenGL defaults, not
701 * the wined3d defaults. */
702 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
703 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
704 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
705 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
706 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
707 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
708 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
709 gl_tex->sampler_desc.lod_bias = 0.0f;
710 gl_tex->sampler_desc.min_lod = -1000.0f;
711 gl_tex->sampler_desc.max_lod = 1000.0f;
712 gl_tex->sampler_desc.max_anisotropy = 1;
713 gl_tex->sampler_desc.compare = FALSE;
714 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
715 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
716 gl_tex->sampler_desc.srgb_decode = TRUE;
717 else
718 gl_tex->sampler_desc.srgb_decode = srgb;
719 gl_tex->base_level = 0;
720 wined3d_texture_set_dirty(texture);
722 context_bind_texture(context, target, gl_tex->name);
724 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
726 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
727 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
730 /* For a new texture we have to set the texture levels after binding the
731 * texture. Beware that texture rectangles do not support mipmapping, but
732 * set the maxmiplevel if we're relying on the partial
733 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
734 * (I.e., do not care about cond_np2 here, just look for
735 * GL_TEXTURE_RECTANGLE_ARB.) */
736 if (target != GL_TEXTURE_RECTANGLE_ARB)
738 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
739 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
740 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
743 if (target == GL_TEXTURE_CUBE_MAP_ARB)
745 /* Cubemaps are always set to clamp, regardless of the sampler state. */
746 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
747 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
748 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
751 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
753 /* Conditinal non power of two textures use a different clamping
754 * default. If we're using the GL_WINE_normalized_texrect partial
755 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
756 * has the address mode set to repeat - something that prevents us
757 * from hitting the accelerated codepath. Thus manually set the GL
758 * state. The same applies to filtering. Even if the texture has only
759 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
760 * fallback on macos. */
761 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
762 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
763 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
764 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
765 checkGLcall("glTexParameteri");
766 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
767 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
768 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
769 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
770 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
773 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
775 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
776 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
779 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
781 static const GLenum swizzle_source[] =
783 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
784 GL_ONE, /* CHANNEL_SOURCE_ONE */
785 GL_RED, /* CHANNEL_SOURCE_X */
786 GL_GREEN, /* CHANNEL_SOURCE_Y */
787 GL_BLUE, /* CHANNEL_SOURCE_Z */
788 GL_ALPHA, /* CHANNEL_SOURCE_W */
790 struct
792 GLint x, y, z, w;
794 swizzle;
796 swizzle.x = swizzle_source[fixup.x_source];
797 swizzle.y = swizzle_source[fixup.y_source];
798 swizzle.z = swizzle_source[fixup.z_source];
799 swizzle.w = swizzle_source[fixup.w_source];
800 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
801 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
805 /* Context activation is done by the caller. */
806 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
807 struct wined3d_context *context, BOOL srgb)
809 /* We don't need a specific texture unit, but after binding the texture
810 * the current unit is dirty. Read the unit back instead of switching to
811 * 0, this avoids messing around with the state manager's GL states. The
812 * current texture unit should always be a valid one.
814 * To be more specific, this is tricky because we can implicitly be
815 * called from sampler() in state.c. This means we can't touch anything
816 * other than whatever happens to be the currently active texture, or we
817 * would risk marking already applied sampler states dirty again. */
818 if (context->active_texture < MAX_COMBINED_SAMPLERS)
820 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
821 if (active_sampler != WINED3D_UNMAPPED_STAGE)
822 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
824 /* FIXME: Ideally we'd only do this when touching a binding that's used by
825 * a shader. */
826 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
827 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
829 wined3d_texture_bind(texture, context, srgb);
832 /* Context activation is done by the caller (state handler). */
833 /* This function relies on the correct texture being bound and loaded. */
834 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
835 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
837 const struct wined3d_gl_info *gl_info = context->gl_info;
838 GLenum target = texture->target;
839 struct gl_texture *gl_tex;
840 DWORD state;
842 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
844 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
846 state = sampler_desc->address_u;
847 if (state != gl_tex->sampler_desc.address_u)
849 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
850 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
851 gl_tex->sampler_desc.address_u = state;
854 state = sampler_desc->address_v;
855 if (state != gl_tex->sampler_desc.address_v)
857 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
858 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
859 gl_tex->sampler_desc.address_v = state;
862 state = sampler_desc->address_w;
863 if (state != gl_tex->sampler_desc.address_w)
865 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
866 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
867 gl_tex->sampler_desc.address_w = state;
870 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
871 sizeof(gl_tex->sampler_desc.border_color)))
873 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
874 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
875 sizeof(gl_tex->sampler_desc.border_color));
878 state = sampler_desc->mag_filter;
879 if (state != gl_tex->sampler_desc.mag_filter)
881 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
882 gl_tex->sampler_desc.mag_filter = state;
885 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
886 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
888 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
889 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
890 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
891 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
894 state = sampler_desc->max_anisotropy;
895 if (state != gl_tex->sampler_desc.max_anisotropy)
897 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
898 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
899 else
900 WARN("Anisotropic filtering not supported.\n");
901 gl_tex->sampler_desc.max_anisotropy = state;
904 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
905 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
906 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
908 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
909 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
910 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
913 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
915 if (sampler_desc->compare)
916 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
917 else
918 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
919 gl_tex->sampler_desc.compare = sampler_desc->compare;
922 checkGLcall("Texture parameter application");
924 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
926 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
927 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
928 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
932 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
934 ULONG refcount;
936 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
938 if (texture->swapchain)
939 return wined3d_swapchain_incref(texture->swapchain);
941 refcount = InterlockedIncrement(&texture->resource.ref);
942 TRACE("%p increasing refcount to %u.\n", texture, refcount);
944 return refcount;
947 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
949 wined3d_texture_sub_resources_destroyed(texture);
950 resource_cleanup(&texture->resource);
951 wined3d_resource_wait_idle(&texture->resource);
952 wined3d_texture_cleanup(texture);
955 static void wined3d_texture_destroy_object(void *object)
957 wined3d_texture_cleanup(object);
958 HeapFree(GetProcessHeap(), 0, object);
961 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
963 ULONG refcount;
965 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
967 if (texture->swapchain)
968 return wined3d_swapchain_decref(texture->swapchain);
970 refcount = InterlockedDecrement(&texture->resource.ref);
971 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
973 if (!refcount)
975 /* Wait for the texture to become idle if it's using user memory,
976 * since the application is allowed to free that memory once the
977 * texture is destroyed. Note that this implies that
978 * wined3d_texture_destroy_object() can't access that memory either. */
979 if (texture->user_memory)
980 wined3d_resource_wait_idle(&texture->resource);
981 wined3d_texture_sub_resources_destroyed(texture);
982 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
983 resource_cleanup(&texture->resource);
984 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
987 return refcount;
990 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
992 TRACE("texture %p.\n", texture);
994 return &texture->resource;
997 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
999 return c1->color_space_low_value == c2->color_space_low_value
1000 && c1->color_space_high_value == c2->color_space_high_value;
1003 /* Context activation is done by the caller */
1004 void wined3d_texture_load(struct wined3d_texture *texture,
1005 struct wined3d_context *context, BOOL srgb)
1007 UINT sub_count = texture->level_count * texture->layer_count;
1008 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1009 DWORD flag;
1010 UINT i;
1012 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1014 if (!needs_separate_srgb_gl_texture(context, texture))
1015 srgb = FALSE;
1017 if (srgb)
1018 flag = WINED3D_TEXTURE_SRGB_VALID;
1019 else
1020 flag = WINED3D_TEXTURE_RGB_VALID;
1022 if (!d3d_info->shader_color_key
1023 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1024 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1025 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1026 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1028 unsigned int sub_count = texture->level_count * texture->layer_count;
1029 unsigned int i;
1031 TRACE("Reloading because of color key value change.\n");
1032 for (i = 0; i < sub_count; i++)
1034 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1035 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1036 else
1037 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1040 texture->async.gl_color_key = texture->async.src_blt_color_key;
1043 if (texture->flags & flag)
1045 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1046 return;
1049 /* Reload the surfaces if the texture is marked dirty. */
1050 for (i = 0; i < sub_count; ++i)
1052 if (!wined3d_texture_load_location(texture, i, context,
1053 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1054 ERR("Failed to load location (srgb %#x).\n", srgb);
1056 texture->flags |= flag;
1059 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1061 TRACE("texture %p.\n", texture);
1063 return texture->resource.parent;
1066 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1067 unsigned int level, const struct wined3d_box *box)
1069 if (box->left >= box->right
1070 || box->top >= box->bottom
1071 || box->front >= box->back)
1072 return FALSE;
1074 if (box->right > wined3d_texture_get_level_width(texture, level)
1075 || box->bottom > wined3d_texture_get_level_height(texture, level)
1076 || box->back > wined3d_texture_get_level_depth(texture, level))
1077 return FALSE;
1079 return TRUE;
1082 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1083 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1085 const struct wined3d_resource *resource = &texture->resource;
1086 unsigned int width = wined3d_texture_get_level_width(texture, level);
1087 unsigned int height = wined3d_texture_get_level_height(texture, level);
1089 if (texture->row_pitch)
1091 *row_pitch = texture->row_pitch;
1092 *slice_pitch = texture->slice_pitch;
1093 return;
1096 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1097 width, height, row_pitch, slice_pitch);
1100 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1102 DWORD old = texture->lod;
1104 TRACE("texture %p, lod %u.\n", texture, lod);
1106 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1107 * textures. The call always returns 0, and GetLOD always returns 0. */
1108 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1110 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1111 return 0;
1114 if (lod >= texture->level_count)
1115 lod = texture->level_count - 1;
1117 if (texture->lod != lod)
1119 wined3d_resource_wait_idle(&texture->resource);
1120 texture->lod = lod;
1122 texture->texture_rgb.base_level = ~0u;
1123 texture->texture_srgb.base_level = ~0u;
1124 if (texture->resource.bind_count)
1125 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1128 return old;
1131 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1133 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1135 return texture->lod;
1138 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1140 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1142 return texture->level_count;
1145 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1146 enum wined3d_texture_filter_type filter_type)
1148 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1150 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1152 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1153 return WINED3DERR_INVALIDCALL;
1156 texture->filter_type = filter_type;
1158 return WINED3D_OK;
1161 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1163 TRACE("texture %p.\n", texture);
1165 return texture->filter_type;
1168 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1169 DWORD flags, const struct wined3d_color_key *color_key)
1171 struct wined3d_device *device = texture->resource.device;
1172 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1173 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1175 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1177 if (flags & ~all_flags)
1179 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1180 return WINED3DERR_INVALIDCALL;
1183 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1185 return WINED3D_OK;
1188 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1189 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1190 UINT multisample_quality, void *mem, UINT pitch)
1192 struct wined3d_device *device = texture->resource.device;
1193 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1194 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1195 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1196 struct wined3d_texture_sub_resource *sub_resource;
1197 struct wined3d_surface *surface;
1198 DWORD valid_location = 0;
1199 BOOL create_dib = FALSE;
1201 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1202 "mem %p, pitch %u.\n",
1203 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1205 if (!resource_size)
1206 return WINED3DERR_INVALIDCALL;
1208 if (texture->level_count * texture->layer_count > 1)
1210 WARN("Texture has multiple sub-resources, not supported.\n");
1211 return WINED3DERR_INVALIDCALL;
1214 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1216 WARN("Not supported on 3D textures.\n");
1217 return WINED3DERR_INVALIDCALL;
1220 if (texture->resource.map_count)
1222 WARN("Texture is mapped.\n");
1223 return WINED3DERR_INVALIDCALL;
1226 /* We have no way of supporting a pitch that is not a multiple of the pixel
1227 * byte width short of uploading the texture row-by-row.
1228 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1229 * for user-memory textures (it always expects packed data) while DirectDraw
1230 * requires a 4-byte aligned pitch and doesn't support texture formats
1231 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1232 * This check is here to verify that the assumption holds. */
1233 if (pitch % texture->resource.format->byte_count)
1235 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1236 return WINED3DERR_INVALIDCALL;
1239 if (device->d3d_initialized)
1240 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1241 wined3d_resource_wait_idle(&texture->resource);
1243 sub_resource = &texture->sub_resources[0];
1244 surface = sub_resource->u.surface;
1245 if (surface->dc)
1247 wined3d_surface_destroy_dc(surface);
1248 create_dib = TRUE;
1251 wined3d_resource_free_sysmem(&texture->resource);
1253 if ((texture->row_pitch = pitch))
1254 texture->slice_pitch = height * pitch;
1255 else
1256 /* User memory surfaces don't have the regular surface alignment. */
1257 wined3d_format_calculate_pitch(format, 1, width, height,
1258 &texture->row_pitch, &texture->slice_pitch);
1260 texture->resource.format = format;
1261 texture->resource.multisample_type = multisample_type;
1262 texture->resource.multisample_quality = multisample_quality;
1263 texture->resource.width = width;
1264 texture->resource.height = height;
1265 texture->resource.size = texture->slice_pitch;
1266 sub_resource->size = texture->slice_pitch;
1267 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1269 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1270 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1272 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1273 texture->pow2_width = texture->pow2_height = 1;
1274 while (texture->pow2_width < width)
1275 texture->pow2_width <<= 1;
1276 while (texture->pow2_height < height)
1277 texture->pow2_height <<= 1;
1279 else
1281 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1282 texture->pow2_width = width;
1283 texture->pow2_height = height;
1286 if ((texture->user_memory = mem))
1288 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1289 valid_location = WINED3D_LOCATION_USER_MEMORY;
1291 else
1293 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1294 valid_location = WINED3D_LOCATION_SYSMEM;
1297 /* The format might be changed to a format that needs conversion.
1298 * If the surface didn't use PBOs previously but could now, don't
1299 * change it - whatever made us not use PBOs might come back, e.g.
1300 * color keys. */
1301 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1302 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1304 wined3d_texture_validate_location(texture, 0, valid_location);
1305 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1307 if (create_dib)
1308 wined3d_surface_create_dc(surface);
1310 return WINED3D_OK;
1313 /* Context activation is done by the caller. */
1314 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1315 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1317 struct wined3d_texture_sub_resource *sub_resource;
1319 sub_resource = &texture->sub_resources[sub_resource_idx];
1320 if (sub_resource->buffer_object)
1321 return;
1323 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1324 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1325 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1326 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1327 checkGLcall("Create buffer object");
1329 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1330 sub_resource->buffer_object, texture, sub_resource_idx);
1333 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1335 unsigned int sub_count = texture->level_count * texture->layer_count;
1336 unsigned int i;
1338 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1339 | WINED3D_TEXTURE_CONVERTED);
1340 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1341 for (i = 0; i < sub_count; ++i)
1343 wined3d_texture_invalidate_location(texture, i,
1344 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1348 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1350 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1351 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1353 if (!d3d_info->shader_color_key
1354 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1355 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1357 wined3d_texture_force_reload(texture);
1359 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1360 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1363 if (texture->flags & alloc_flag)
1364 return;
1366 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1367 texture->flags |= alloc_flag;
1370 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1371 const struct wined3d_gl_info *gl_info, BOOL multisample)
1373 const struct wined3d_format *format = texture->resource.format;
1375 if (multisample)
1377 DWORD samples;
1379 if (texture->rb_multisample)
1380 return;
1382 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1383 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1384 * be mapped to GL_NV_framebuffer_multisample_coverage.
1386 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1387 * (EQAA), but it does not have an equivalent OpenGL extension. */
1389 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1390 * levels as the count of advertised multisample types for the texture
1391 * format. */
1392 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1394 unsigned int i, count = 0;
1396 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1398 if (format->multisample_types & 1u << i)
1400 if (texture->resource.multisample_quality == count++)
1401 break;
1404 samples = i + 1;
1406 else
1408 samples = texture->resource.multisample_type;
1411 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1412 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1413 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1414 format->glInternal, texture->resource.width, texture->resource.height);
1415 checkGLcall("glRenderbufferStorageMultisample()");
1416 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1418 else
1420 if (texture->rb_resolved)
1421 return;
1423 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1424 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1425 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1426 texture->resource.width, texture->resource.height);
1427 checkGLcall("glRenderbufferStorage()");
1428 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1432 /* Context activation is done by the caller. Context may be NULL in
1433 * WINED3D_NO3D mode. */
1434 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1435 struct wined3d_context *context, DWORD location)
1437 switch (location)
1439 case WINED3D_LOCATION_SYSMEM:
1440 if (texture->resource.heap_memory)
1441 return TRUE;
1443 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1445 ERR("Failed to allocate system memory.\n");
1446 return FALSE;
1448 return TRUE;
1450 case WINED3D_LOCATION_USER_MEMORY:
1451 if (!texture->user_memory)
1452 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1453 return TRUE;
1455 case WINED3D_LOCATION_BUFFER:
1456 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1457 return TRUE;
1459 case WINED3D_LOCATION_TEXTURE_RGB:
1460 wined3d_texture_prepare_texture(texture, context, FALSE);
1461 return TRUE;
1463 case WINED3D_LOCATION_TEXTURE_SRGB:
1464 wined3d_texture_prepare_texture(texture, context, TRUE);
1465 return TRUE;
1467 case WINED3D_LOCATION_DRAWABLE:
1468 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1469 ERR("Texture %p does not have a drawable.\n", texture);
1470 return TRUE;
1472 case WINED3D_LOCATION_RB_MULTISAMPLE:
1473 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1474 return TRUE;
1476 case WINED3D_LOCATION_RB_RESOLVED:
1477 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1478 return TRUE;
1480 default:
1481 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1482 return FALSE;
1486 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1488 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1489 FIXME("texture %p stub!\n", texture);
1492 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1493 unsigned int sub_resource_idx)
1495 UINT sub_count = texture->level_count * texture->layer_count;
1497 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1499 if (sub_resource_idx >= sub_count)
1501 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1502 return NULL;
1505 return &texture->sub_resources[sub_resource_idx];
1508 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1509 UINT layer, const struct wined3d_box *dirty_region)
1511 struct wined3d_context *context;
1512 unsigned int sub_resource_idx;
1514 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1516 if (layer >= texture->layer_count)
1518 WARN("Invalid layer %u specified.\n", layer);
1519 return WINED3DERR_INVALIDCALL;
1521 sub_resource_idx = layer * texture->level_count;
1523 if (dirty_region)
1524 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1526 context = context_acquire(texture->resource.device, NULL, 0);
1527 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1529 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1530 context_release(context);
1531 return E_OUTOFMEMORY;
1533 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1534 context_release(context);
1536 return WINED3D_OK;
1539 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1540 const struct wined3d_context *context, const struct wined3d_box *box,
1541 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1543 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1544 context, box, data, row_pitch, slice_pitch);
1547 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1548 const struct wined3d_context *context, const struct wined3d_box *box,
1549 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1551 unsigned int texture_level;
1552 POINT dst_point;
1553 RECT src_rect;
1555 src_rect.left = 0;
1556 src_rect.top = 0;
1557 if (box)
1559 dst_point.x = box->left;
1560 dst_point.y = box->top;
1561 src_rect.right = box->right - box->left;
1562 src_rect.bottom = box->bottom - box->top;
1564 else
1566 dst_point.x = dst_point.y = 0;
1567 texture_level = sub_resource_idx % texture->level_count;
1568 src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
1569 src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
1572 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1573 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1576 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1577 struct wined3d_context *context, DWORD location)
1579 return surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location);
1582 /* Context activation is done by the caller. */
1583 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1585 const struct wined3d_format *format = texture->resource.format;
1586 const struct wined3d_gl_info *gl_info = context->gl_info;
1587 const struct wined3d_color_key_conversion *conversion;
1588 GLenum internal;
1590 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1592 if (format->convert)
1594 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1596 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1598 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1599 format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
1600 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1603 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1605 if (srgb)
1606 internal = format->glGammaInternal;
1607 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1608 && wined3d_resource_is_offscreen(&texture->resource))
1609 internal = format->rtInternal;
1610 else
1611 internal = format->glInternal;
1613 if (!internal)
1614 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1616 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1618 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1619 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1620 else
1621 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1624 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1626 unsigned int sub_count = texture->level_count * texture->layer_count;
1627 struct wined3d_device *device = texture->resource.device;
1628 struct wined3d_texture_sub_resource *sub_resource;
1629 struct wined3d_renderbuffer_entry *entry, *entry2;
1630 const struct wined3d_gl_info *gl_info = NULL;
1631 struct wined3d_context *context = NULL;
1632 struct wined3d_surface *overlay, *cur;
1633 struct wined3d_surface *surface;
1634 unsigned int i;
1636 for (i = 0; i < sub_count; ++i)
1638 sub_resource = &texture->sub_resources[i];
1639 if (!(surface = sub_resource->u.surface))
1640 continue;
1642 TRACE("surface %p.\n", surface);
1644 if (!context && !list_empty(&surface->renderbuffers))
1646 context = context_acquire(device, NULL, 0);
1647 gl_info = context->gl_info;
1650 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1652 TRACE("Deleting renderbuffer %u.\n", entry->id);
1653 context_gl_resource_released(device, entry->id, TRUE);
1654 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1655 HeapFree(GetProcessHeap(), 0, entry);
1658 if (surface->dc)
1659 wined3d_surface_destroy_dc(surface);
1661 if (surface->overlay_dest)
1662 list_remove(&surface->overlay_entry);
1664 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1666 list_remove(&overlay->overlay_entry);
1667 overlay->overlay_dest = NULL;
1670 if (context)
1671 context_release(context);
1672 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1675 static const struct wined3d_texture_ops texture2d_ops =
1677 texture2d_upload_data,
1678 texture2d_load_location,
1679 texture2d_prepare_texture,
1680 texture2d_cleanup_sub_resources,
1683 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1685 return texture_from_resource(resource);
1688 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1690 return wined3d_texture_incref(texture_from_resource(resource));
1693 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1695 return wined3d_texture_decref(texture_from_resource(resource));
1698 static void texture_resource_preload(struct wined3d_resource *resource)
1700 struct wined3d_texture *texture = texture_from_resource(resource);
1701 struct wined3d_context *context;
1703 context = context_acquire(resource->device, NULL, 0);
1704 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1705 context_release(context);
1708 static void wined3d_texture_unload(struct wined3d_resource *resource)
1710 struct wined3d_texture *texture = texture_from_resource(resource);
1711 UINT sub_count = texture->level_count * texture->layer_count;
1712 struct wined3d_device *device = resource->device;
1713 const struct wined3d_gl_info *gl_info;
1714 struct wined3d_context *context;
1715 UINT i;
1717 TRACE("texture %p.\n", texture);
1719 context = context_acquire(device, NULL, 0);
1720 gl_info = context->gl_info;
1722 for (i = 0; i < sub_count; ++i)
1724 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1726 if (resource->pool != WINED3D_POOL_DEFAULT
1727 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1729 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1731 else
1733 /* We should only get here on device reset/teardown for implicit
1734 * resources. */
1735 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1736 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1737 resource, i, debug_d3dpool(resource->pool));
1738 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1739 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1742 if (sub_resource->buffer_object)
1743 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1745 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1747 struct wined3d_surface *surface = sub_resource->u.surface;
1748 struct wined3d_renderbuffer_entry *entry, *entry2;
1750 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1752 context_gl_resource_released(device, entry->id, TRUE);
1753 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1754 list_remove(&entry->entry);
1755 HeapFree(GetProcessHeap(), 0, entry);
1757 list_init(&surface->renderbuffers);
1758 surface->current_renderbuffer = NULL;
1762 context_release(context);
1764 wined3d_texture_force_reload(texture);
1765 wined3d_texture_unload_gl_texture(texture);
1768 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1769 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1771 const struct wined3d_format *format = resource->format;
1772 struct wined3d_texture_sub_resource *sub_resource;
1773 struct wined3d_device *device = resource->device;
1774 unsigned int fmt_flags = resource->format_flags;
1775 const struct wined3d_gl_info *gl_info = NULL;
1776 struct wined3d_context *context = NULL;
1777 struct wined3d_texture *texture;
1778 struct wined3d_bo_address data;
1779 unsigned int texture_level;
1780 BYTE *base_memory;
1781 BOOL ret;
1783 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1784 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1786 texture = texture_from_resource(resource);
1787 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1788 return E_INVALIDARG;
1790 texture_level = sub_resource_idx % texture->level_count;
1791 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1793 WARN("Map box is invalid.\n");
1794 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1795 return WINED3DERR_INVALIDCALL;
1798 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1799 && !wined3d_texture_check_block_align(texture, texture_level, box))
1801 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1802 debug_box(box), format->block_width, format->block_height);
1803 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1804 return WINED3DERR_INVALIDCALL;
1807 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1809 WARN("Trying to map unmappable texture.\n");
1810 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1811 return WINED3DERR_INVALIDCALL;
1814 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1816 WARN("DC is in use.\n");
1817 return WINED3DERR_INVALIDCALL;
1820 if (sub_resource->map_count)
1822 WARN("Sub-resource is already mapped.\n");
1823 return WINED3DERR_INVALIDCALL;
1826 if (device->d3d_initialized)
1828 context = context_acquire(device, NULL, 0);
1829 gl_info = context->gl_info;
1832 if (flags & WINED3D_MAP_DISCARD)
1834 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1835 wined3d_debug_location(texture->resource.map_binding));
1836 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1837 context, texture->resource.map_binding)))
1838 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1840 else
1842 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1843 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1844 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1847 if (!ret)
1849 ERR("Failed to prepare location.\n");
1850 context_release(context);
1851 return E_OUTOFMEMORY;
1854 if (!(flags & WINED3D_MAP_READONLY)
1855 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1856 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1858 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1859 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1860 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1861 TRACE("Base memory pointer %p.\n", base_memory);
1863 if (context)
1864 context_release(context);
1866 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1868 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1869 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1871 else
1873 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1876 if (!box)
1878 map_desc->data = base_memory;
1880 else
1882 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1884 /* Compressed textures are block based, so calculate the offset of
1885 * the block that contains the top-left pixel of the mapped box. */
1886 map_desc->data = base_memory
1887 + (box->front * map_desc->slice_pitch)
1888 + ((box->top / format->block_height) * map_desc->row_pitch)
1889 + ((box->left / format->block_width) * format->block_byte_count);
1891 else
1893 map_desc->data = base_memory
1894 + (box->front * map_desc->slice_pitch)
1895 + (box->top * map_desc->row_pitch)
1896 + (box->left * format->byte_count);
1900 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1902 RECT *r = &texture->swapchain->front_buffer_update;
1904 if (!box)
1905 SetRect(r, 0, 0, resource->width, resource->height);
1906 else
1907 SetRect(r, box->left, box->top, box->right, box->bottom);
1908 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1911 ++resource->map_count;
1912 ++sub_resource->map_count;
1914 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1915 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1917 return WINED3D_OK;
1920 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1922 struct wined3d_texture_sub_resource *sub_resource;
1923 struct wined3d_device *device = resource->device;
1924 const struct wined3d_gl_info *gl_info = NULL;
1925 struct wined3d_context *context = NULL;
1926 struct wined3d_texture *texture;
1927 struct wined3d_bo_address data;
1929 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1931 texture = texture_from_resource(resource);
1932 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1933 return E_INVALIDARG;
1935 if (!sub_resource->map_count)
1937 WARN("Trying to unmap unmapped sub-resource.\n");
1938 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1939 return WINED3D_OK;
1940 return WINEDDERR_NOTLOCKED;
1943 if (device->d3d_initialized)
1945 context = context_acquire(device, NULL, 0);
1946 gl_info = context->gl_info;
1949 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1950 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1952 if (context)
1953 context_release(context);
1955 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1957 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1958 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1961 --sub_resource->map_count;
1962 if (!--resource->map_count && texture->update_map_binding)
1963 wined3d_texture_update_map_binding(texture);
1965 return WINED3D_OK;
1968 static const struct wined3d_resource_ops texture_resource_ops =
1970 texture_resource_incref,
1971 texture_resource_decref,
1972 texture_resource_preload,
1973 wined3d_texture_unload,
1974 texture_resource_sub_resource_map,
1975 texture_resource_sub_resource_unmap,
1978 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1979 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1980 void *parent, const struct wined3d_parent_ops *parent_ops)
1982 struct wined3d_device_parent *device_parent = device->device_parent;
1983 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1984 struct wined3d_surface *surfaces;
1985 UINT pow2_width, pow2_height;
1986 unsigned int i, j;
1987 HRESULT hr;
1989 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
1990 && !gl_info->supported[EXT_TEXTURE_ARRAY])
1992 WARN("OpenGL implementation does not support array textures.\n");
1993 return WINED3DERR_INVALIDCALL;
1996 /* TODO: It should only be possible to create textures for formats
1997 * that are reported as supported. */
1998 if (WINED3DFMT_UNKNOWN >= desc->format)
2000 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2001 return WINED3DERR_INVALIDCALL;
2004 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
2005 FIXME("Trying to create a managed texture with dynamic usage.\n");
2006 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2007 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2008 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
2009 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
2010 FIXME("Trying to create a render target that isn't in the default pool.\n");
2012 pow2_width = desc->width;
2013 pow2_height = desc->height;
2014 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
2015 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2017 /* level_count == 0 returns an error as well. */
2018 if (level_count != 1 || layer_count != 1)
2020 if (desc->pool != WINED3D_POOL_SCRATCH)
2022 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2023 return WINED3DERR_INVALIDCALL;
2026 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2028 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2030 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2032 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2034 /* TODO: Add support for non-power-of-two compressed textures. */
2035 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2036 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2038 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2039 desc->width, desc->height);
2040 return WINED3DERR_NOTAVAILABLE;
2043 /* Find the nearest pow2 match. */
2044 pow2_width = pow2_height = 1;
2045 while (pow2_width < desc->width)
2046 pow2_width <<= 1;
2047 while (pow2_height < desc->height)
2048 pow2_height <<= 1;
2049 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2052 texture->pow2_width = pow2_width;
2053 texture->pow2_height = pow2_height;
2055 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2056 && (desc->usage & WINED3DUSAGE_TEXTURE))
2058 /* One of four options:
2059 * 1: Do the same as we do with NPOT and scale the texture. (Any
2060 * texture ops would require the texture to be scaled which is
2061 * potentially slow.)
2062 * 2: Set the texture to the maximum size (bad idea).
2063 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2064 * 4: Create the surface, but allow it to be used only for DirectDraw
2065 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2066 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2067 * the render target. */
2068 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
2070 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2071 return WINED3DERR_NOTAVAILABLE;
2074 /* We should never use this surface in combination with OpenGL. */
2075 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2078 /* Calculate levels for mip mapping. */
2079 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2081 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2083 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
2084 return WINED3DERR_INVALIDCALL;
2087 if (level_count != 1)
2089 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2090 return WINED3DERR_INVALIDCALL;
2094 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
2095 flags, device, parent, parent_ops, &texture_resource_ops)))
2097 WARN("Failed to initialize texture, returning %#x.\n", hr);
2098 return hr;
2101 /* Precalculated scaling for 'faked' non power of two texture coords. */
2102 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2104 texture->pow2_matrix[0] = (float)desc->width;
2105 texture->pow2_matrix[5] = (float)desc->height;
2106 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2107 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2109 else
2111 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2113 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2114 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2115 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2117 else
2119 texture->pow2_matrix[0] = 1.0f;
2120 texture->pow2_matrix[5] = 1.0f;
2122 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2123 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2124 else if (layer_count > 1)
2125 texture->target = GL_TEXTURE_2D_ARRAY;
2126 else
2127 texture->target = GL_TEXTURE_2D;
2129 texture->pow2_matrix[10] = 1.0f;
2130 texture->pow2_matrix[15] = 1.0f;
2131 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2133 if (wined3d_texture_use_pbo(texture, gl_info))
2134 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2136 if (level_count > ~(SIZE_T)0 / layer_count
2137 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2139 wined3d_texture_cleanup_sync(texture);
2140 return E_OUTOFMEMORY;
2143 /* Generate all the surfaces. */
2144 for (i = 0; i < texture->level_count; ++i)
2146 for (j = 0; j < texture->layer_count; ++j)
2148 static const GLenum cube_targets[6] =
2150 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2151 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2152 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2153 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2154 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2155 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2157 struct wined3d_texture_sub_resource *sub_resource;
2158 unsigned int idx = j * texture->level_count + i;
2159 struct wined3d_surface *surface;
2161 surface = &surfaces[idx];
2162 surface->container = texture;
2163 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2164 surface->texture_level = i;
2165 surface->texture_layer = j;
2166 list_init(&surface->renderbuffers);
2167 list_init(&surface->overlays);
2169 sub_resource = &texture->sub_resources[idx];
2170 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2171 sub_resource->u.surface = surface;
2172 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2174 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2175 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2178 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2179 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2181 WARN("Failed to create surface parent, hr %#x.\n", hr);
2182 sub_resource->parent = NULL;
2183 wined3d_texture_cleanup_sync(texture);
2184 return hr;
2187 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2189 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2191 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2192 && FAILED(hr = wined3d_surface_create_dc(surface)))
2194 wined3d_texture_cleanup_sync(texture);
2195 return hr;
2200 return WINED3D_OK;
2203 /* This call just uploads data, the caller is responsible for binding the
2204 * correct texture. */
2205 /* Context activation is done by the caller. */
2206 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2207 const struct wined3d_context *context, const struct wined3d_box *box,
2208 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2210 const struct wined3d_format *format = texture->resource.format;
2211 unsigned int level = sub_resource_idx % texture->level_count;
2212 const struct wined3d_gl_info *gl_info = context->gl_info;
2213 unsigned int x, y, z, update_w, update_h, update_d;
2214 unsigned int dst_row_pitch, dst_slice_pitch;
2215 unsigned int width, height, depth;
2216 const void *mem = data->addr;
2217 void *converted_mem = NULL;
2219 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2220 texture, sub_resource_idx, context, debug_box(box),
2221 data->buffer_object, data->addr, row_pitch, slice_pitch);
2223 width = wined3d_texture_get_level_width(texture, level);
2224 height = wined3d_texture_get_level_height(texture, level);
2225 depth = wined3d_texture_get_level_depth(texture, level);
2227 if (!box)
2229 x = y = z = 0;
2230 update_w = width;
2231 update_h = height;
2232 update_d = depth;
2234 else
2236 x = box->left;
2237 y = box->top;
2238 z = box->front;
2239 update_w = box->right - box->left;
2240 update_h = box->bottom - box->top;
2241 update_d = box->back - box->front;
2244 if (format->convert)
2246 if (data->buffer_object)
2247 ERR("Loading a converted texture from a PBO.\n");
2248 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2249 ERR("Converting a block-based format.\n");
2251 dst_row_pitch = update_w * format->conv_byte_count;
2252 dst_slice_pitch = dst_row_pitch * update_h;
2254 converted_mem = wined3d_calloc(update_d, dst_slice_pitch);
2255 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2256 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2257 mem = converted_mem;
2259 else
2261 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2262 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2263 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2266 if (data->buffer_object)
2268 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2269 checkGLcall("glBindBuffer");
2272 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2273 update_w, update_h, update_d, format->glFormat, format->glType, mem));
2274 checkGLcall("glTexSubImage3D");
2276 if (data->buffer_object)
2278 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2279 checkGLcall("glBindBuffer");
2282 HeapFree(GetProcessHeap(), 0, converted_mem);
2285 /* Context activation is done by the caller. */
2286 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2287 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2289 const struct wined3d_format *format = texture->resource.format;
2290 const struct wined3d_gl_info *gl_info = context->gl_info;
2292 if (format->convert)
2294 FIXME("Attempting to download a converted volume, format %s.\n",
2295 debug_d3dformat(format->id));
2296 return;
2299 if (data->buffer_object)
2301 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2302 checkGLcall("glBindBuffer");
2305 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2306 format->glFormat, format->glType, data->addr);
2307 checkGLcall("glGetTexImage");
2309 if (data->buffer_object)
2311 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2312 checkGLcall("glBindBuffer");
2317 /* Context activation is done by the caller. */
2318 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2319 struct wined3d_context *context, BOOL dest_is_srgb)
2321 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2322 unsigned int row_pitch, slice_pitch;
2323 struct wined3d_bo_address data;
2325 /* Optimisations are possible, but the effort should be put into either
2326 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2327 * picked the wrong copy for the original upload and fixing that.
2329 * Also keep in mind that we want to avoid using resource.heap_memory
2330 * for DEFAULT pool surfaces. */
2331 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2332 data.buffer_object = 0;
2333 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2334 return;
2336 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2337 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2338 texture3d_download_data(texture, sub_resource_idx, context, &data);
2339 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2340 texture3d_upload_data(texture, sub_resource_idx, context,
2341 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2343 HeapFree(GetProcessHeap(), 0, data.addr);
2346 /* Context activation is done by the caller. */
2347 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2348 struct wined3d_context *context, DWORD location)
2350 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2351 unsigned int row_pitch, slice_pitch;
2353 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2354 return FALSE;
2356 switch (location)
2358 case WINED3D_LOCATION_TEXTURE_RGB:
2359 case WINED3D_LOCATION_TEXTURE_SRGB:
2360 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2362 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2363 data.addr += sub_resource->offset;
2364 wined3d_texture_bind_and_dirtify(texture, context,
2365 location == WINED3D_LOCATION_TEXTURE_SRGB);
2366 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2367 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2369 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2371 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2372 wined3d_texture_bind_and_dirtify(texture, context,
2373 location == WINED3D_LOCATION_TEXTURE_SRGB);
2374 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2375 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2377 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2379 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2381 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2383 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2385 else
2387 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2388 return FALSE;
2390 break;
2392 case WINED3D_LOCATION_SYSMEM:
2393 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2395 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2397 data.addr += sub_resource->offset;
2398 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2399 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2400 else
2401 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2403 texture3d_download_data(texture, sub_resource_idx, context, &data);
2404 ++texture->download_count;
2406 else
2408 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2409 wined3d_debug_location(sub_resource->locations));
2410 return FALSE;
2412 break;
2414 case WINED3D_LOCATION_BUFFER:
2415 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2417 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2419 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2420 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2421 else
2422 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2424 texture3d_download_data(texture, sub_resource_idx, context, &data);
2426 else
2428 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2429 wined3d_debug_location(sub_resource->locations));
2430 return FALSE;
2432 break;
2434 default:
2435 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2436 wined3d_debug_location(sub_resource->locations));
2437 return FALSE;
2440 return TRUE;
2443 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2445 const struct wined3d_format *format = texture->resource.format;
2446 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2447 unsigned int sub_count = texture->level_count * texture->layer_count;
2448 const struct wined3d_gl_info *gl_info = context->gl_info;
2449 unsigned int i;
2451 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2453 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2455 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2456 wined3d_texture_get_level_width(texture, 0),
2457 wined3d_texture_get_level_height(texture, 0),
2458 wined3d_texture_get_level_depth(texture, 0)));
2459 checkGLcall("glTexStorage3D");
2461 else
2463 for (i = 0; i < sub_count; ++i)
2465 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2466 wined3d_texture_get_level_width(texture, i),
2467 wined3d_texture_get_level_height(texture, i),
2468 wined3d_texture_get_level_depth(texture, i),
2469 0, format->glFormat, format->glType, NULL));
2470 checkGLcall("glTexImage3D");
2475 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2479 static const struct wined3d_texture_ops texture3d_ops =
2481 texture3d_upload_data,
2482 texture3d_load_location,
2483 texture3d_prepare_texture,
2484 texture3d_cleanup_sub_resources,
2487 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2488 unsigned int level, const struct wined3d_box *box)
2490 const struct wined3d_format *format = texture->resource.format;
2491 unsigned int height = wined3d_texture_get_level_height(texture, level);
2492 unsigned int width = wined3d_texture_get_level_width(texture, level);
2493 unsigned int width_mask, height_mask;
2495 if ((box->left >= box->right)
2496 || (box->top >= box->bottom)
2497 || (box->right > width)
2498 || (box->bottom > height))
2499 return FALSE;
2501 /* This assumes power of two block sizes, but NPOT block sizes would be
2502 * silly anyway.
2504 * This also assumes that the format's block depth is 1. */
2505 width_mask = format->block_width - 1;
2506 height_mask = format->block_height - 1;
2508 if ((box->left & width_mask) || (box->top & height_mask)
2509 || (box->right & width_mask && box->right != width)
2510 || (box->bottom & height_mask && box->bottom != height))
2511 return FALSE;
2513 return TRUE;
2516 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2517 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2518 const struct wined3d_parent_ops *parent_ops)
2520 struct wined3d_device_parent *device_parent = device->device_parent;
2521 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2522 unsigned int i;
2523 HRESULT hr;
2525 if (layer_count != 1)
2527 ERR("Invalid layer count for volume texture.\n");
2528 return E_INVALIDARG;
2531 /* TODO: It should only be possible to create textures for formats
2532 * that are reported as supported. */
2533 if (WINED3DFMT_UNKNOWN >= desc->format)
2535 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2536 return WINED3DERR_INVALIDCALL;
2539 if (!gl_info->supported[EXT_TEXTURE3D])
2541 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2542 return WINED3DERR_INVALIDCALL;
2545 /* Calculate levels for mip mapping. */
2546 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2548 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2550 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2551 return WINED3DERR_INVALIDCALL;
2554 if (level_count != 1)
2556 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2557 return WINED3DERR_INVALIDCALL;
2561 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2562 || desc->pool == WINED3D_POOL_SCRATCH))
2564 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2565 return WINED3DERR_INVALIDCALL;
2568 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2570 UINT pow2_w, pow2_h, pow2_d;
2571 pow2_w = 1;
2572 while (pow2_w < desc->width)
2573 pow2_w <<= 1;
2574 pow2_h = 1;
2575 while (pow2_h < desc->height)
2576 pow2_h <<= 1;
2577 pow2_d = 1;
2578 while (pow2_d < desc->depth)
2579 pow2_d <<= 1;
2581 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2583 if (desc->pool == WINED3D_POOL_SCRATCH)
2585 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2587 else
2589 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2590 desc->width, desc->height, desc->depth);
2591 return WINED3DERR_INVALIDCALL;
2596 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2597 0, device, parent, parent_ops, &texture_resource_ops)))
2599 WARN("Failed to initialize texture, returning %#x.\n", hr);
2600 return hr;
2603 texture->pow2_matrix[0] = 1.0f;
2604 texture->pow2_matrix[5] = 1.0f;
2605 texture->pow2_matrix[10] = 1.0f;
2606 texture->pow2_matrix[15] = 1.0f;
2607 texture->target = GL_TEXTURE_3D;
2609 if (wined3d_texture_use_pbo(texture, gl_info))
2611 wined3d_resource_free_sysmem(&texture->resource);
2612 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2615 /* Generate all the surfaces. */
2616 for (i = 0; i < texture->level_count; ++i)
2618 struct wined3d_texture_sub_resource *sub_resource;
2620 sub_resource = &texture->sub_resources[i];
2621 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2623 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2624 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2626 WARN("Failed to create volume parent, hr %#x.\n", hr);
2627 sub_resource->parent = NULL;
2628 wined3d_texture_cleanup_sync(texture);
2629 return hr;
2632 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2634 TRACE("Created volume level %u.\n", i);
2637 return WINED3D_OK;
2640 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2641 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2642 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2644 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2646 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2647 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2648 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2649 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2651 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2652 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2653 return WINED3DERR_INVALIDCALL;
2655 if (src_texture)
2657 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2658 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2659 return WINED3DERR_INVALIDCALL;
2662 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2663 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2666 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2667 unsigned int sub_resource_idx, LONG *x, LONG *y)
2669 struct wined3d_surface *surface;
2671 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2673 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2674 || sub_resource_idx >= texture->level_count * texture->layer_count)
2676 WARN("Invalid sub-resource specified.\n");
2677 return WINEDDERR_NOTAOVERLAYSURFACE;
2680 surface = texture->sub_resources[sub_resource_idx].u.surface;
2681 if (!surface->overlay_dest)
2683 TRACE("Overlay not visible.\n");
2684 *x = 0;
2685 *y = 0;
2686 return WINEDDERR_OVERLAYNOTVISIBLE;
2689 *x = surface->overlay_destrect.left;
2690 *y = surface->overlay_destrect.top;
2692 TRACE("Returning position %d, %d.\n", *x, *y);
2694 return WINED3D_OK;
2697 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2698 unsigned int sub_resource_idx, LONG x, LONG y)
2700 struct wined3d_texture_sub_resource *sub_resource;
2701 struct wined3d_surface *surface;
2702 LONG w, h;
2704 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2706 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2707 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2709 WARN("Invalid sub-resource specified.\n");
2710 return WINEDDERR_NOTAOVERLAYSURFACE;
2713 surface = sub_resource->u.surface;
2714 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2715 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2716 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2718 return WINED3D_OK;
2721 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2722 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2723 const RECT *dst_rect, DWORD flags)
2725 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2726 struct wined3d_surface *surface, *dst_surface;
2728 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2729 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2730 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2731 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2733 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2734 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2736 WARN("Invalid sub-resource specified.\n");
2737 return WINEDDERR_NOTAOVERLAYSURFACE;
2740 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2741 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2743 WARN("Invalid destination sub-resource specified.\n");
2744 return WINED3DERR_INVALIDCALL;
2747 surface = sub_resource->u.surface;
2748 if (src_rect)
2749 surface->overlay_srcrect = *src_rect;
2750 else
2751 SetRect(&surface->overlay_srcrect, 0, 0,
2752 wined3d_texture_get_level_width(texture, surface->texture_level),
2753 wined3d_texture_get_level_height(texture, surface->texture_level));
2755 dst_surface = dst_sub_resource->u.surface;
2756 if (dst_rect)
2757 surface->overlay_destrect = *dst_rect;
2758 else
2759 SetRect(&surface->overlay_destrect, 0, 0,
2760 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2761 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2763 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2765 surface->overlay_dest = NULL;
2766 list_remove(&surface->overlay_entry);
2769 if (flags & WINEDDOVER_SHOW)
2771 if (surface->overlay_dest != dst_surface)
2773 surface->overlay_dest = dst_surface;
2774 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2777 else if (flags & WINEDDOVER_HIDE)
2779 /* Tests show that the rectangles are erased on hide. */
2780 SetRectEmpty(&surface->overlay_srcrect);
2781 SetRectEmpty(&surface->overlay_destrect);
2782 surface->overlay_dest = NULL;
2785 return WINED3D_OK;
2788 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2790 unsigned int sub_count = texture->level_count * texture->layer_count;
2792 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2794 if (sub_resource_idx >= sub_count)
2796 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2797 return NULL;
2800 return texture->sub_resources[sub_resource_idx].parent;
2803 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2804 unsigned int sub_resource_idx, void *parent)
2806 unsigned int sub_count = texture->level_count * texture->layer_count;
2808 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2810 if (sub_resource_idx >= sub_count)
2812 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2813 return;
2816 texture->sub_resources[sub_resource_idx].parent = parent;
2819 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2820 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2822 unsigned int sub_count = texture->level_count * texture->layer_count;
2823 const struct wined3d_resource *resource;
2824 unsigned int level_idx;
2826 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2828 if (sub_resource_idx >= sub_count)
2830 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2831 return WINED3DERR_INVALIDCALL;
2834 resource = &texture->resource;
2835 desc->format = resource->format->id;
2836 desc->multisample_type = resource->multisample_type;
2837 desc->multisample_quality = resource->multisample_quality;
2838 desc->usage = resource->usage;
2839 desc->pool = resource->pool;
2841 level_idx = sub_resource_idx % texture->level_count;
2842 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2843 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2844 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2845 desc->size = texture->sub_resources[sub_resource_idx].size;
2847 return WINED3D_OK;
2850 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2851 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2852 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2854 struct wined3d_texture *object;
2855 HRESULT hr;
2857 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2858 "parent %p, parent_ops %p, texture %p.\n",
2859 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2861 if (!layer_count)
2863 WARN("Invalid layer count.\n");
2864 return E_INVALIDARG;
2866 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2868 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2869 layer_count = 6;
2872 if (!level_count)
2874 WARN("Invalid level count.\n");
2875 return WINED3DERR_INVALIDCALL;
2878 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2880 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2881 desc->format, desc->usage);
2883 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2884 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2886 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2887 desc->multisample_quality);
2888 return WINED3DERR_NOTAVAILABLE;
2890 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2891 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2892 || desc->multisample_quality))
2894 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2895 desc->multisample_quality);
2896 return WINED3DERR_NOTAVAILABLE;
2900 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2901 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2902 return E_OUTOFMEMORY;
2904 switch (desc->resource_type)
2906 case WINED3D_RTYPE_TEXTURE_2D:
2907 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2908 break;
2910 case WINED3D_RTYPE_TEXTURE_3D:
2911 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2912 break;
2914 default:
2915 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2916 hr = WINED3DERR_INVALIDCALL;
2917 break;
2920 if (FAILED(hr))
2922 WARN("Failed to initialize texture, returning %#x.\n", hr);
2923 HeapFree(GetProcessHeap(), 0, object);
2924 return hr;
2927 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2928 * in this case. */
2929 if (data)
2931 unsigned int sub_count = level_count * layer_count;
2932 unsigned int i;
2934 for (i = 0; i < sub_count; ++i)
2936 if (!data[i].data)
2938 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2939 wined3d_texture_cleanup_sync(object);
2940 HeapFree(GetProcessHeap(), 0, object);
2941 return E_INVALIDARG;
2945 for (i = 0; i < sub_count; ++i)
2947 wined3d_device_update_sub_resource(device, &object->resource,
2948 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
2952 TRACE("Created texture %p.\n", object);
2953 *texture = object;
2955 return WINED3D_OK;
2958 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2960 struct wined3d_device *device = texture->resource.device;
2961 struct wined3d_texture_sub_resource *sub_resource;
2962 struct wined3d_context *context = NULL;
2963 struct wined3d_surface *surface;
2964 HRESULT hr = WINED3D_OK;
2966 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2968 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
2970 WARN("Texture does not support GetDC\n");
2971 /* Don't touch the DC */
2972 return WINED3DERR_INVALIDCALL;
2975 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2976 return WINED3DERR_INVALIDCALL;
2978 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2980 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2981 return WINED3DERR_INVALIDCALL;
2984 surface = sub_resource->u.surface;
2986 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2987 return WINED3DERR_INVALIDCALL;
2989 if (device->d3d_initialized)
2990 context = context_acquire(device, NULL, 0);
2992 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2993 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2995 if (!surface->dc)
2996 hr = wined3d_surface_create_dc(surface);
2997 if (context)
2998 context_release(context);
2999 if (FAILED(hr))
3000 return WINED3DERR_INVALIDCALL;
3002 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3003 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3004 ++texture->resource.map_count;
3005 ++sub_resource->map_count;
3007 *dc = surface->dc;
3008 TRACE("Returning dc %p.\n", *dc);
3010 return hr;
3013 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3015 struct wined3d_device *device = texture->resource.device;
3016 struct wined3d_texture_sub_resource *sub_resource;
3017 struct wined3d_surface *surface;
3019 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3021 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3022 return WINED3DERR_INVALIDCALL;
3024 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3026 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3027 return WINED3DERR_INVALIDCALL;
3030 surface = sub_resource->u.surface;
3032 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3033 return WINED3DERR_INVALIDCALL;
3035 if (surface->dc != dc)
3037 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
3038 return WINED3DERR_INVALIDCALL;
3041 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3042 wined3d_surface_destroy_dc(surface);
3044 --sub_resource->map_count;
3045 if (!--texture->resource.map_count && texture->update_map_binding)
3046 wined3d_texture_update_map_binding(texture);
3047 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3048 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3050 return WINED3D_OK;