wined3d: Return a BOOL from surface_load_location().
[wine.git] / dlls / wined3d / texture.c
blob037682d48c1e442b6e81a63d3d0570a969e45ff9
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 DWORD active_sampler;
811 /* We don't need a specific texture unit, but after binding the texture
812 * the current unit is dirty. Read the unit back instead of switching to
813 * 0, this avoids messing around with the state manager's GL states. The
814 * current texture unit should always be a valid one.
816 * To be more specific, this is tricky because we can implicitly be
817 * called from sampler() in state.c. This means we can't touch anything
818 * other than whatever happens to be the currently active texture, or we
819 * would risk marking already applied sampler states dirty again. */
820 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));
823 /* FIXME: Ideally we'd only do this when touching a binding that's used by
824 * a shader. */
825 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
827 wined3d_texture_bind(texture, context, srgb);
830 /* Context activation is done by the caller (state handler). */
831 /* This function relies on the correct texture being bound and loaded. */
832 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
833 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
835 const struct wined3d_gl_info *gl_info = context->gl_info;
836 GLenum target = texture->target;
837 struct gl_texture *gl_tex;
838 DWORD state;
840 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
842 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
844 state = sampler_desc->address_u;
845 if (state != gl_tex->sampler_desc.address_u)
847 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
848 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
849 gl_tex->sampler_desc.address_u = state;
852 state = sampler_desc->address_v;
853 if (state != gl_tex->sampler_desc.address_v)
855 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
856 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
857 gl_tex->sampler_desc.address_v = state;
860 state = sampler_desc->address_w;
861 if (state != gl_tex->sampler_desc.address_w)
863 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
864 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
865 gl_tex->sampler_desc.address_w = state;
868 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
869 sizeof(gl_tex->sampler_desc.border_color)))
871 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
872 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
873 sizeof(gl_tex->sampler_desc.border_color));
876 state = sampler_desc->mag_filter;
877 if (state != gl_tex->sampler_desc.mag_filter)
879 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
880 gl_tex->sampler_desc.mag_filter = state;
883 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
884 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
886 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
887 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
888 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
889 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
892 state = sampler_desc->max_anisotropy;
893 if (state != gl_tex->sampler_desc.max_anisotropy)
895 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
896 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
897 else
898 WARN("Anisotropic filtering not supported.\n");
899 gl_tex->sampler_desc.max_anisotropy = state;
902 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
903 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
904 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
906 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
907 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
908 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
911 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
913 if (sampler_desc->compare)
914 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
915 else
916 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
917 gl_tex->sampler_desc.compare = sampler_desc->compare;
920 checkGLcall("Texture parameter application");
922 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
924 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
925 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
926 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
930 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
932 ULONG refcount;
934 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
936 if (texture->swapchain)
937 return wined3d_swapchain_incref(texture->swapchain);
939 refcount = InterlockedIncrement(&texture->resource.ref);
940 TRACE("%p increasing refcount to %u.\n", texture, refcount);
942 return refcount;
945 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
947 wined3d_texture_sub_resources_destroyed(texture);
948 resource_cleanup(&texture->resource);
949 wined3d_resource_wait_idle(&texture->resource);
950 wined3d_texture_cleanup(texture);
953 static void wined3d_texture_destroy_object(void *object)
955 wined3d_texture_cleanup(object);
956 HeapFree(GetProcessHeap(), 0, object);
959 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
961 ULONG refcount;
963 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
965 if (texture->swapchain)
966 return wined3d_swapchain_decref(texture->swapchain);
968 refcount = InterlockedDecrement(&texture->resource.ref);
969 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
971 if (!refcount)
973 /* Wait for the texture to become idle if it's using user memory,
974 * since the application is allowed to free that memory once the
975 * texture is destroyed. Note that this implies that
976 * wined3d_texture_destroy_object() can't access that memory either. */
977 if (texture->user_memory)
978 wined3d_resource_wait_idle(&texture->resource);
979 wined3d_texture_sub_resources_destroyed(texture);
980 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
981 resource_cleanup(&texture->resource);
982 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
985 return refcount;
988 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
990 TRACE("texture %p.\n", texture);
992 return &texture->resource;
995 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
997 return c1->color_space_low_value == c2->color_space_low_value
998 && c1->color_space_high_value == c2->color_space_high_value;
1001 /* Context activation is done by the caller */
1002 void wined3d_texture_load(struct wined3d_texture *texture,
1003 struct wined3d_context *context, BOOL srgb)
1005 UINT sub_count = texture->level_count * texture->layer_count;
1006 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1007 DWORD flag;
1008 UINT i;
1010 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1012 if (!needs_separate_srgb_gl_texture(context, texture))
1013 srgb = FALSE;
1015 if (srgb)
1016 flag = WINED3D_TEXTURE_SRGB_VALID;
1017 else
1018 flag = WINED3D_TEXTURE_RGB_VALID;
1020 if (!d3d_info->shader_color_key
1021 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1022 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1023 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1024 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1026 unsigned int sub_count = texture->level_count * texture->layer_count;
1027 unsigned int i;
1029 TRACE("Reloading because of color key value change.\n");
1030 for (i = 0; i < sub_count; i++)
1032 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1033 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1034 else
1035 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1038 texture->async.gl_color_key = texture->async.src_blt_color_key;
1041 if (texture->flags & flag)
1043 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1044 return;
1047 /* Reload the surfaces if the texture is marked dirty. */
1048 for (i = 0; i < sub_count; ++i)
1050 if (!wined3d_texture_load_location(texture, i, context,
1051 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1052 ERR("Failed to load location (srgb %#x).\n", srgb);
1054 texture->flags |= flag;
1057 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1059 TRACE("texture %p.\n", texture);
1061 return texture->resource.parent;
1064 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1065 unsigned int level, const struct wined3d_box *box)
1067 if (box->left >= box->right
1068 || box->top >= box->bottom
1069 || box->front >= box->back)
1070 return FALSE;
1072 if (box->right > wined3d_texture_get_level_width(texture, level)
1073 || box->bottom > wined3d_texture_get_level_height(texture, level)
1074 || box->back > wined3d_texture_get_level_depth(texture, level))
1075 return FALSE;
1077 return TRUE;
1080 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1081 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1083 const struct wined3d_resource *resource = &texture->resource;
1084 unsigned int width = wined3d_texture_get_level_width(texture, level);
1085 unsigned int height = wined3d_texture_get_level_height(texture, level);
1087 if (texture->row_pitch)
1089 *row_pitch = texture->row_pitch;
1090 *slice_pitch = texture->slice_pitch;
1091 return;
1094 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1095 width, height, row_pitch, slice_pitch);
1098 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1100 DWORD old = texture->lod;
1102 TRACE("texture %p, lod %u.\n", texture, lod);
1104 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1105 * textures. The call always returns 0, and GetLOD always returns 0. */
1106 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1108 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1109 return 0;
1112 if (lod >= texture->level_count)
1113 lod = texture->level_count - 1;
1115 if (texture->lod != lod)
1117 wined3d_resource_wait_idle(&texture->resource);
1118 texture->lod = lod;
1120 texture->texture_rgb.base_level = ~0u;
1121 texture->texture_srgb.base_level = ~0u;
1122 if (texture->resource.bind_count)
1123 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1126 return old;
1129 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1131 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1133 return texture->lod;
1136 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1138 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1140 return texture->level_count;
1143 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1144 enum wined3d_texture_filter_type filter_type)
1146 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1148 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1150 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1151 return WINED3DERR_INVALIDCALL;
1154 texture->filter_type = filter_type;
1156 return WINED3D_OK;
1159 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1161 TRACE("texture %p.\n", texture);
1163 return texture->filter_type;
1166 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1167 DWORD flags, const struct wined3d_color_key *color_key)
1169 struct wined3d_device *device = texture->resource.device;
1170 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1171 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1173 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1175 if (flags & ~all_flags)
1177 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1178 return WINED3DERR_INVALIDCALL;
1181 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1183 return WINED3D_OK;
1186 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1187 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1188 UINT multisample_quality, void *mem, UINT pitch)
1190 struct wined3d_device *device = texture->resource.device;
1191 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1192 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1193 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1194 struct wined3d_texture_sub_resource *sub_resource;
1195 struct wined3d_surface *surface;
1196 DWORD valid_location = 0;
1197 BOOL create_dib = FALSE;
1199 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1200 "mem %p, pitch %u.\n",
1201 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1203 if (!resource_size)
1204 return WINED3DERR_INVALIDCALL;
1206 if (texture->level_count * texture->layer_count > 1)
1208 WARN("Texture has multiple sub-resources, not supported.\n");
1209 return WINED3DERR_INVALIDCALL;
1212 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1214 WARN("Not supported on 3D textures.\n");
1215 return WINED3DERR_INVALIDCALL;
1218 if (texture->resource.map_count)
1220 WARN("Texture is mapped.\n");
1221 return WINED3DERR_INVALIDCALL;
1224 /* We have no way of supporting a pitch that is not a multiple of the pixel
1225 * byte width short of uploading the texture row-by-row.
1226 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1227 * for user-memory textures (it always expects packed data) while DirectDraw
1228 * requires a 4-byte aligned pitch and doesn't support texture formats
1229 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1230 * This check is here to verify that the assumption holds. */
1231 if (pitch % texture->resource.format->byte_count)
1233 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1234 return WINED3DERR_INVALIDCALL;
1237 if (device->d3d_initialized)
1238 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1239 wined3d_resource_wait_idle(&texture->resource);
1241 sub_resource = &texture->sub_resources[0];
1242 surface = sub_resource->u.surface;
1243 if (surface->dc)
1245 wined3d_surface_destroy_dc(surface);
1246 create_dib = TRUE;
1249 wined3d_resource_free_sysmem(&texture->resource);
1251 if ((texture->row_pitch = pitch))
1252 texture->slice_pitch = height * pitch;
1253 else
1254 /* User memory surfaces don't have the regular surface alignment. */
1255 wined3d_format_calculate_pitch(format, 1, width, height,
1256 &texture->row_pitch, &texture->slice_pitch);
1258 texture->resource.format = format;
1259 texture->resource.multisample_type = multisample_type;
1260 texture->resource.multisample_quality = multisample_quality;
1261 texture->resource.width = width;
1262 texture->resource.height = height;
1263 texture->resource.size = texture->slice_pitch;
1264 sub_resource->size = texture->slice_pitch;
1265 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1267 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1268 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1270 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1271 texture->pow2_width = texture->pow2_height = 1;
1272 while (texture->pow2_width < width)
1273 texture->pow2_width <<= 1;
1274 while (texture->pow2_height < height)
1275 texture->pow2_height <<= 1;
1277 else
1279 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1280 texture->pow2_width = width;
1281 texture->pow2_height = height;
1284 if ((texture->user_memory = mem))
1286 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1287 valid_location = WINED3D_LOCATION_USER_MEMORY;
1289 else
1291 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1292 valid_location = WINED3D_LOCATION_SYSMEM;
1295 /* The format might be changed to a format that needs conversion.
1296 * If the surface didn't use PBOs previously but could now, don't
1297 * change it - whatever made us not use PBOs might come back, e.g.
1298 * color keys. */
1299 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1300 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1302 wined3d_texture_validate_location(texture, 0, valid_location);
1303 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1305 if (create_dib)
1306 wined3d_surface_create_dc(surface);
1308 return WINED3D_OK;
1311 /* Context activation is done by the caller. */
1312 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1313 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1315 struct wined3d_texture_sub_resource *sub_resource;
1317 sub_resource = &texture->sub_resources[sub_resource_idx];
1318 if (sub_resource->buffer_object)
1319 return;
1321 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1322 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1323 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1324 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1325 checkGLcall("Create buffer object");
1327 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1328 sub_resource->buffer_object, texture, sub_resource_idx);
1331 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1333 unsigned int sub_count = texture->level_count * texture->layer_count;
1334 unsigned int i;
1336 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1337 | WINED3D_TEXTURE_CONVERTED);
1338 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1339 for (i = 0; i < sub_count; ++i)
1341 wined3d_texture_invalidate_location(texture, i,
1342 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1346 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1348 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1349 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1351 if (!d3d_info->shader_color_key
1352 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1353 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1355 wined3d_texture_force_reload(texture);
1357 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1358 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1361 if (texture->flags & alloc_flag)
1362 return;
1364 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1365 texture->flags |= alloc_flag;
1368 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1369 const struct wined3d_gl_info *gl_info, BOOL multisample)
1371 const struct wined3d_format *format = texture->resource.format;
1373 if (multisample)
1375 DWORD samples;
1377 if (texture->rb_multisample)
1378 return;
1380 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1381 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1382 * be mapped to GL_NV_framebuffer_multisample_coverage.
1384 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1385 * (EQAA), but it does not have an equivalent OpenGL extension. */
1387 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1388 * levels as the count of advertised multisample types for the texture
1389 * format. */
1390 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1392 unsigned int i, count = 0;
1394 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1396 if (format->multisample_types & 1u << i)
1398 if (texture->resource.multisample_quality == count++)
1399 break;
1402 samples = i + 1;
1404 else
1406 samples = texture->resource.multisample_type;
1409 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1410 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1411 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1412 format->glInternal, texture->resource.width, texture->resource.height);
1413 checkGLcall("glRenderbufferStorageMultisample()");
1414 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1416 else
1418 if (texture->rb_resolved)
1419 return;
1421 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1422 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1423 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1424 texture->resource.width, texture->resource.height);
1425 checkGLcall("glRenderbufferStorage()");
1426 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1430 /* Context activation is done by the caller. Context may be NULL in
1431 * WINED3D_NO3D mode. */
1432 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1433 struct wined3d_context *context, DWORD location)
1435 switch (location)
1437 case WINED3D_LOCATION_SYSMEM:
1438 if (texture->resource.heap_memory)
1439 return TRUE;
1441 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1443 ERR("Failed to allocate system memory.\n");
1444 return FALSE;
1446 return TRUE;
1448 case WINED3D_LOCATION_USER_MEMORY:
1449 if (!texture->user_memory)
1450 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1451 return TRUE;
1453 case WINED3D_LOCATION_BUFFER:
1454 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1455 return TRUE;
1457 case WINED3D_LOCATION_TEXTURE_RGB:
1458 wined3d_texture_prepare_texture(texture, context, FALSE);
1459 return TRUE;
1461 case WINED3D_LOCATION_TEXTURE_SRGB:
1462 wined3d_texture_prepare_texture(texture, context, TRUE);
1463 return TRUE;
1465 case WINED3D_LOCATION_DRAWABLE:
1466 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1467 ERR("Texture %p does not have a drawable.\n", texture);
1468 return TRUE;
1470 case WINED3D_LOCATION_RB_MULTISAMPLE:
1471 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1472 return TRUE;
1474 case WINED3D_LOCATION_RB_RESOLVED:
1475 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1476 return TRUE;
1478 default:
1479 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1480 return FALSE;
1484 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1486 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1487 FIXME("texture %p stub!\n", texture);
1490 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1491 unsigned int sub_resource_idx)
1493 UINT sub_count = texture->level_count * texture->layer_count;
1495 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1497 if (sub_resource_idx >= sub_count)
1499 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1500 return NULL;
1503 return &texture->sub_resources[sub_resource_idx];
1506 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1507 UINT layer, const struct wined3d_box *dirty_region)
1509 struct wined3d_context *context;
1510 unsigned int sub_resource_idx;
1512 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1514 if (layer >= texture->layer_count)
1516 WARN("Invalid layer %u specified.\n", layer);
1517 return WINED3DERR_INVALIDCALL;
1519 sub_resource_idx = layer * texture->level_count;
1521 if (dirty_region)
1522 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1524 context = context_acquire(texture->resource.device, NULL, 0);
1525 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1527 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1528 context_release(context);
1529 return E_OUTOFMEMORY;
1531 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1532 context_release(context);
1534 return WINED3D_OK;
1537 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1538 const struct wined3d_context *context, const struct wined3d_box *box,
1539 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1541 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1542 context, box, data, row_pitch, slice_pitch);
1545 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1546 const struct wined3d_context *context, const struct wined3d_box *box,
1547 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1549 unsigned int texture_level;
1550 POINT dst_point;
1551 RECT src_rect;
1553 src_rect.left = 0;
1554 src_rect.top = 0;
1555 if (box)
1557 dst_point.x = box->left;
1558 dst_point.y = box->top;
1559 src_rect.right = box->right - box->left;
1560 src_rect.bottom = box->bottom - box->top;
1562 else
1564 dst_point.x = dst_point.y = 0;
1565 texture_level = sub_resource_idx % texture->level_count;
1566 src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
1567 src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
1570 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1571 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1574 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1575 struct wined3d_context *context, DWORD location)
1577 return surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location);
1580 /* Context activation is done by the caller. */
1581 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1583 const struct wined3d_format *format = texture->resource.format;
1584 const struct wined3d_gl_info *gl_info = context->gl_info;
1585 const struct wined3d_color_key_conversion *conversion;
1586 GLenum internal;
1588 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1590 if (format->convert)
1592 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1594 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1596 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1597 format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
1598 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1601 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1603 if (srgb)
1604 internal = format->glGammaInternal;
1605 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1606 && wined3d_resource_is_offscreen(&texture->resource))
1607 internal = format->rtInternal;
1608 else
1609 internal = format->glInternal;
1611 if (!internal)
1612 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1614 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1616 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1617 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1618 else
1619 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1622 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1624 unsigned int sub_count = texture->level_count * texture->layer_count;
1625 struct wined3d_device *device = texture->resource.device;
1626 struct wined3d_texture_sub_resource *sub_resource;
1627 struct wined3d_renderbuffer_entry *entry, *entry2;
1628 const struct wined3d_gl_info *gl_info = NULL;
1629 struct wined3d_context *context = NULL;
1630 struct wined3d_surface *overlay, *cur;
1631 struct wined3d_surface *surface;
1632 unsigned int i;
1634 for (i = 0; i < sub_count; ++i)
1636 sub_resource = &texture->sub_resources[i];
1637 if (!(surface = sub_resource->u.surface))
1638 continue;
1640 TRACE("surface %p.\n", surface);
1642 if (!context && !list_empty(&surface->renderbuffers))
1644 context = context_acquire(device, NULL, 0);
1645 gl_info = context->gl_info;
1648 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1650 TRACE("Deleting renderbuffer %u.\n", entry->id);
1651 context_gl_resource_released(device, entry->id, TRUE);
1652 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1653 HeapFree(GetProcessHeap(), 0, entry);
1656 if (surface->dc)
1657 wined3d_surface_destroy_dc(surface);
1659 if (surface->overlay_dest)
1660 list_remove(&surface->overlay_entry);
1662 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1664 list_remove(&overlay->overlay_entry);
1665 overlay->overlay_dest = NULL;
1668 if (context)
1669 context_release(context);
1670 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1673 static const struct wined3d_texture_ops texture2d_ops =
1675 texture2d_upload_data,
1676 texture2d_load_location,
1677 texture2d_prepare_texture,
1678 texture2d_cleanup_sub_resources,
1681 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1683 return texture_from_resource(resource);
1686 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1688 return wined3d_texture_incref(texture_from_resource(resource));
1691 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1693 return wined3d_texture_decref(texture_from_resource(resource));
1696 static void texture_resource_preload(struct wined3d_resource *resource)
1698 struct wined3d_texture *texture = texture_from_resource(resource);
1699 struct wined3d_context *context;
1701 context = context_acquire(resource->device, NULL, 0);
1702 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1703 context_release(context);
1706 static void wined3d_texture_unload(struct wined3d_resource *resource)
1708 struct wined3d_texture *texture = texture_from_resource(resource);
1709 UINT sub_count = texture->level_count * texture->layer_count;
1710 struct wined3d_device *device = resource->device;
1711 const struct wined3d_gl_info *gl_info;
1712 struct wined3d_context *context;
1713 UINT i;
1715 TRACE("texture %p.\n", texture);
1717 context = context_acquire(device, NULL, 0);
1718 gl_info = context->gl_info;
1720 for (i = 0; i < sub_count; ++i)
1722 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1724 if (resource->pool != WINED3D_POOL_DEFAULT
1725 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1727 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1729 else
1731 /* We should only get here on device reset/teardown for implicit
1732 * resources. */
1733 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1734 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1735 resource, i, debug_d3dpool(resource->pool));
1736 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1737 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1740 if (sub_resource->buffer_object)
1741 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1743 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1745 struct wined3d_surface *surface = sub_resource->u.surface;
1746 struct wined3d_renderbuffer_entry *entry, *entry2;
1748 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1750 context_gl_resource_released(device, entry->id, TRUE);
1751 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1752 list_remove(&entry->entry);
1753 HeapFree(GetProcessHeap(), 0, entry);
1755 list_init(&surface->renderbuffers);
1756 surface->current_renderbuffer = NULL;
1760 context_release(context);
1762 wined3d_texture_force_reload(texture);
1763 wined3d_texture_unload_gl_texture(texture);
1766 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1767 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1769 const struct wined3d_format *format = resource->format;
1770 struct wined3d_texture_sub_resource *sub_resource;
1771 struct wined3d_device *device = resource->device;
1772 unsigned int fmt_flags = resource->format_flags;
1773 const struct wined3d_gl_info *gl_info = NULL;
1774 struct wined3d_context *context = NULL;
1775 struct wined3d_texture *texture;
1776 struct wined3d_bo_address data;
1777 unsigned int texture_level;
1778 BYTE *base_memory;
1779 BOOL ret;
1781 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1782 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1784 texture = texture_from_resource(resource);
1785 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1786 return E_INVALIDARG;
1788 texture_level = sub_resource_idx % texture->level_count;
1789 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1791 WARN("Map box is invalid.\n");
1792 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1793 return WINED3DERR_INVALIDCALL;
1796 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1797 && !wined3d_texture_check_block_align(texture, texture_level, box))
1799 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1800 debug_box(box), format->block_width, format->block_height);
1801 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1802 return WINED3DERR_INVALIDCALL;
1805 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1807 WARN("Trying to map unmappable texture.\n");
1808 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1809 return WINED3DERR_INVALIDCALL;
1812 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1814 WARN("DC is in use.\n");
1815 return WINED3DERR_INVALIDCALL;
1818 if (sub_resource->map_count)
1820 WARN("Sub-resource is already mapped.\n");
1821 return WINED3DERR_INVALIDCALL;
1824 if (device->d3d_initialized)
1826 context = context_acquire(device, NULL, 0);
1827 gl_info = context->gl_info;
1830 if (flags & WINED3D_MAP_DISCARD)
1832 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1833 wined3d_debug_location(texture->resource.map_binding));
1834 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1835 context, texture->resource.map_binding)))
1836 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1838 else
1840 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1841 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1842 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1845 if (!ret)
1847 ERR("Failed to prepare location.\n");
1848 context_release(context);
1849 return E_OUTOFMEMORY;
1852 if (!(flags & WINED3D_MAP_READONLY)
1853 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1854 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1856 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1857 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1858 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1859 TRACE("Base memory pointer %p.\n", base_memory);
1861 if (context)
1862 context_release(context);
1864 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1866 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1867 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1869 else
1871 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1874 if (!box)
1876 map_desc->data = base_memory;
1878 else
1880 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1882 /* Compressed textures are block based, so calculate the offset of
1883 * the block that contains the top-left pixel of the mapped box. */
1884 map_desc->data = base_memory
1885 + (box->front * map_desc->slice_pitch)
1886 + ((box->top / format->block_height) * map_desc->row_pitch)
1887 + ((box->left / format->block_width) * format->block_byte_count);
1889 else
1891 map_desc->data = base_memory
1892 + (box->front * map_desc->slice_pitch)
1893 + (box->top * map_desc->row_pitch)
1894 + (box->left * format->byte_count);
1898 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1900 RECT *r = &texture->swapchain->front_buffer_update;
1902 if (!box)
1903 SetRect(r, 0, 0, resource->width, resource->height);
1904 else
1905 SetRect(r, box->left, box->top, box->right, box->bottom);
1906 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1909 ++resource->map_count;
1910 ++sub_resource->map_count;
1912 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1913 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1915 return WINED3D_OK;
1918 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1920 struct wined3d_texture_sub_resource *sub_resource;
1921 struct wined3d_device *device = resource->device;
1922 const struct wined3d_gl_info *gl_info = NULL;
1923 struct wined3d_context *context = NULL;
1924 struct wined3d_texture *texture;
1925 struct wined3d_bo_address data;
1927 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1929 texture = texture_from_resource(resource);
1930 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1931 return E_INVALIDARG;
1933 if (!sub_resource->map_count)
1935 WARN("Trying to unmap unmapped sub-resource.\n");
1936 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1937 return WINED3D_OK;
1938 return WINEDDERR_NOTLOCKED;
1941 if (device->d3d_initialized)
1943 context = context_acquire(device, NULL, 0);
1944 gl_info = context->gl_info;
1947 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1948 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1950 if (context)
1951 context_release(context);
1953 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1955 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1956 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1958 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1960 FIXME("Depth / stencil buffer locking is not implemented.\n");
1963 --sub_resource->map_count;
1964 if (!--resource->map_count && texture->update_map_binding)
1965 wined3d_texture_update_map_binding(texture);
1967 return WINED3D_OK;
1970 static const struct wined3d_resource_ops texture_resource_ops =
1972 texture_resource_incref,
1973 texture_resource_decref,
1974 texture_resource_preload,
1975 wined3d_texture_unload,
1976 texture_resource_sub_resource_map,
1977 texture_resource_sub_resource_unmap,
1980 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1981 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1982 void *parent, const struct wined3d_parent_ops *parent_ops)
1984 struct wined3d_device_parent *device_parent = device->device_parent;
1985 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1986 struct wined3d_surface *surfaces;
1987 UINT pow2_width, pow2_height;
1988 unsigned int i, j;
1989 HRESULT hr;
1991 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
1992 && !gl_info->supported[EXT_TEXTURE_ARRAY])
1994 WARN("OpenGL implementation does not support array textures.\n");
1995 return WINED3DERR_INVALIDCALL;
1998 /* TODO: It should only be possible to create textures for formats
1999 * that are reported as supported. */
2000 if (WINED3DFMT_UNKNOWN >= desc->format)
2002 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2003 return WINED3DERR_INVALIDCALL;
2006 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
2007 FIXME("Trying to create a managed texture with dynamic usage.\n");
2008 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2009 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2010 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
2011 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
2012 FIXME("Trying to create a render target that isn't in the default pool.\n");
2014 pow2_width = desc->width;
2015 pow2_height = desc->height;
2016 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
2017 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2019 /* level_count == 0 returns an error as well. */
2020 if (level_count != 1 || layer_count != 1)
2022 if (desc->pool != WINED3D_POOL_SCRATCH)
2024 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2025 return WINED3DERR_INVALIDCALL;
2028 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2030 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2032 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2034 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2036 /* TODO: Add support for non-power-of-two compressed textures. */
2037 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2038 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2040 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2041 desc->width, desc->height);
2042 return WINED3DERR_NOTAVAILABLE;
2045 /* Find the nearest pow2 match. */
2046 pow2_width = pow2_height = 1;
2047 while (pow2_width < desc->width)
2048 pow2_width <<= 1;
2049 while (pow2_height < desc->height)
2050 pow2_height <<= 1;
2051 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2054 texture->pow2_width = pow2_width;
2055 texture->pow2_height = pow2_height;
2057 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2058 && (desc->usage & WINED3DUSAGE_TEXTURE))
2060 /* One of four options:
2061 * 1: Do the same as we do with NPOT and scale the texture. (Any
2062 * texture ops would require the texture to be scaled which is
2063 * potentially slow.)
2064 * 2: Set the texture to the maximum size (bad idea).
2065 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2066 * 4: Create the surface, but allow it to be used only for DirectDraw
2067 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2068 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2069 * the render target. */
2070 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
2072 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2073 return WINED3DERR_NOTAVAILABLE;
2076 /* We should never use this surface in combination with OpenGL. */
2077 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2080 /* Calculate levels for mip mapping. */
2081 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2083 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2085 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
2086 return WINED3DERR_INVALIDCALL;
2089 if (level_count != 1)
2091 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2092 return WINED3DERR_INVALIDCALL;
2096 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
2097 flags, device, parent, parent_ops, &texture_resource_ops)))
2099 WARN("Failed to initialize texture, returning %#x.\n", hr);
2100 return hr;
2103 /* Precalculated scaling for 'faked' non power of two texture coords. */
2104 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2106 texture->pow2_matrix[0] = (float)desc->width;
2107 texture->pow2_matrix[5] = (float)desc->height;
2108 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2109 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2111 else
2113 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2115 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2116 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2117 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2119 else
2121 texture->pow2_matrix[0] = 1.0f;
2122 texture->pow2_matrix[5] = 1.0f;
2124 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2125 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2126 else if (layer_count > 1)
2127 texture->target = GL_TEXTURE_2D_ARRAY;
2128 else
2129 texture->target = GL_TEXTURE_2D;
2131 texture->pow2_matrix[10] = 1.0f;
2132 texture->pow2_matrix[15] = 1.0f;
2133 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2135 if (wined3d_texture_use_pbo(texture, gl_info))
2136 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2138 if (level_count > ~(SIZE_T)0 / layer_count
2139 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2141 wined3d_texture_cleanup_sync(texture);
2142 return E_OUTOFMEMORY;
2145 /* Generate all the surfaces. */
2146 for (i = 0; i < texture->level_count; ++i)
2148 for (j = 0; j < texture->layer_count; ++j)
2150 static const GLenum cube_targets[6] =
2152 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2153 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2154 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2155 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2156 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2157 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2159 struct wined3d_texture_sub_resource *sub_resource;
2160 unsigned int idx = j * texture->level_count + i;
2161 struct wined3d_surface *surface;
2163 surface = &surfaces[idx];
2164 surface->container = texture;
2165 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2166 surface->texture_level = i;
2167 surface->texture_layer = j;
2168 list_init(&surface->renderbuffers);
2169 list_init(&surface->overlays);
2171 sub_resource = &texture->sub_resources[idx];
2172 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2173 sub_resource->u.surface = surface;
2174 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2176 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2177 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2180 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2181 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2183 WARN("Failed to create surface parent, hr %#x.\n", hr);
2184 sub_resource->parent = NULL;
2185 wined3d_texture_cleanup_sync(texture);
2186 return hr;
2189 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2191 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2193 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2194 && FAILED(hr = wined3d_surface_create_dc(surface)))
2196 wined3d_texture_cleanup_sync(texture);
2197 return hr;
2202 return WINED3D_OK;
2205 /* This call just uploads data, the caller is responsible for binding the
2206 * correct texture. */
2207 /* Context activation is done by the caller. */
2208 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2209 const struct wined3d_context *context, const struct wined3d_box *box,
2210 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2212 const struct wined3d_format *format = texture->resource.format;
2213 unsigned int level = sub_resource_idx % texture->level_count;
2214 const struct wined3d_gl_info *gl_info = context->gl_info;
2215 unsigned int x, y, z, update_w, update_h, update_d;
2216 unsigned int dst_row_pitch, dst_slice_pitch;
2217 unsigned int width, height, depth;
2218 const void *mem = data->addr;
2219 void *converted_mem = NULL;
2221 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2222 texture, sub_resource_idx, context, debug_box(box),
2223 data->buffer_object, data->addr, row_pitch, slice_pitch);
2225 width = wined3d_texture_get_level_width(texture, level);
2226 height = wined3d_texture_get_level_height(texture, level);
2227 depth = wined3d_texture_get_level_depth(texture, level);
2229 if (!box)
2231 x = y = z = 0;
2232 update_w = width;
2233 update_h = height;
2234 update_d = depth;
2236 else
2238 x = box->left;
2239 y = box->top;
2240 z = box->front;
2241 update_w = box->right - box->left;
2242 update_h = box->bottom - box->top;
2243 update_d = box->back - box->front;
2246 if (format->convert)
2248 if (data->buffer_object)
2249 ERR("Loading a converted texture from a PBO.\n");
2250 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2251 ERR("Converting a block-based format.\n");
2253 dst_row_pitch = update_w * format->conv_byte_count;
2254 dst_slice_pitch = dst_row_pitch * update_h;
2256 converted_mem = wined3d_calloc(update_d, dst_slice_pitch);
2257 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2258 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2259 mem = converted_mem;
2261 else
2263 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2264 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2265 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2268 if (data->buffer_object)
2270 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2271 checkGLcall("glBindBuffer");
2274 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2275 update_w, update_h, update_d, format->glFormat, format->glType, mem));
2276 checkGLcall("glTexSubImage3D");
2278 if (data->buffer_object)
2280 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2281 checkGLcall("glBindBuffer");
2284 HeapFree(GetProcessHeap(), 0, converted_mem);
2287 /* Context activation is done by the caller. */
2288 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2289 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2291 const struct wined3d_format *format = texture->resource.format;
2292 const struct wined3d_gl_info *gl_info = context->gl_info;
2294 if (format->convert)
2296 FIXME("Attempting to download a converted volume, format %s.\n",
2297 debug_d3dformat(format->id));
2298 return;
2301 if (data->buffer_object)
2303 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2304 checkGLcall("glBindBuffer");
2307 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2308 format->glFormat, format->glType, data->addr);
2309 checkGLcall("glGetTexImage");
2311 if (data->buffer_object)
2313 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2314 checkGLcall("glBindBuffer");
2319 /* Context activation is done by the caller. */
2320 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2321 struct wined3d_context *context, BOOL dest_is_srgb)
2323 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2324 unsigned int row_pitch, slice_pitch;
2325 struct wined3d_bo_address data;
2327 /* Optimisations are possible, but the effort should be put into either
2328 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2329 * picked the wrong copy for the original upload and fixing that.
2331 * Also keep in mind that we want to avoid using resource.heap_memory
2332 * for DEFAULT pool surfaces. */
2333 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2334 data.buffer_object = 0;
2335 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2336 return;
2338 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2339 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2340 texture3d_download_data(texture, sub_resource_idx, context, &data);
2341 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2342 texture3d_upload_data(texture, sub_resource_idx, context,
2343 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2345 HeapFree(GetProcessHeap(), 0, data.addr);
2348 /* Context activation is done by the caller. */
2349 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2350 struct wined3d_context *context, DWORD location)
2352 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2353 unsigned int row_pitch, slice_pitch;
2355 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2356 return FALSE;
2358 switch (location)
2360 case WINED3D_LOCATION_TEXTURE_RGB:
2361 case WINED3D_LOCATION_TEXTURE_SRGB:
2362 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2364 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2365 data.addr += sub_resource->offset;
2366 wined3d_texture_bind_and_dirtify(texture, context,
2367 location == WINED3D_LOCATION_TEXTURE_SRGB);
2368 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2369 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2371 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2373 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2374 wined3d_texture_bind_and_dirtify(texture, context,
2375 location == WINED3D_LOCATION_TEXTURE_SRGB);
2376 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2377 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2379 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2381 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2383 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2385 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2387 else
2389 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2390 return FALSE;
2392 break;
2394 case WINED3D_LOCATION_SYSMEM:
2395 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2397 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2399 data.addr += sub_resource->offset;
2400 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2401 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2402 else
2403 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2405 texture3d_download_data(texture, sub_resource_idx, context, &data);
2406 ++texture->download_count;
2408 else
2410 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2411 wined3d_debug_location(sub_resource->locations));
2412 return FALSE;
2414 break;
2416 case WINED3D_LOCATION_BUFFER:
2417 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2419 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2421 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2422 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2423 else
2424 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2426 texture3d_download_data(texture, sub_resource_idx, context, &data);
2428 else
2430 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2431 wined3d_debug_location(sub_resource->locations));
2432 return FALSE;
2434 break;
2436 default:
2437 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2438 wined3d_debug_location(sub_resource->locations));
2439 return FALSE;
2442 return TRUE;
2445 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2447 const struct wined3d_format *format = texture->resource.format;
2448 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2449 unsigned int sub_count = texture->level_count * texture->layer_count;
2450 const struct wined3d_gl_info *gl_info = context->gl_info;
2451 unsigned int i;
2453 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2455 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2457 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2458 wined3d_texture_get_level_width(texture, 0),
2459 wined3d_texture_get_level_height(texture, 0),
2460 wined3d_texture_get_level_depth(texture, 0)));
2461 checkGLcall("glTexStorage3D");
2463 else
2465 for (i = 0; i < sub_count; ++i)
2467 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2468 wined3d_texture_get_level_width(texture, i),
2469 wined3d_texture_get_level_height(texture, i),
2470 wined3d_texture_get_level_depth(texture, i),
2471 0, format->glFormat, format->glType, NULL));
2472 checkGLcall("glTexImage3D");
2477 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2481 static const struct wined3d_texture_ops texture3d_ops =
2483 texture3d_upload_data,
2484 texture3d_load_location,
2485 texture3d_prepare_texture,
2486 texture3d_cleanup_sub_resources,
2489 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2490 unsigned int level, const struct wined3d_box *box)
2492 const struct wined3d_format *format = texture->resource.format;
2493 unsigned int height = wined3d_texture_get_level_height(texture, level);
2494 unsigned int width = wined3d_texture_get_level_width(texture, level);
2495 unsigned int width_mask, height_mask;
2497 if ((box->left >= box->right)
2498 || (box->top >= box->bottom)
2499 || (box->right > width)
2500 || (box->bottom > height))
2501 return FALSE;
2503 /* This assumes power of two block sizes, but NPOT block sizes would be
2504 * silly anyway.
2506 * This also assumes that the format's block depth is 1. */
2507 width_mask = format->block_width - 1;
2508 height_mask = format->block_height - 1;
2510 if ((box->left & width_mask) || (box->top & height_mask)
2511 || (box->right & width_mask && box->right != width)
2512 || (box->bottom & height_mask && box->bottom != height))
2513 return FALSE;
2515 return TRUE;
2518 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2519 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2520 const struct wined3d_parent_ops *parent_ops)
2522 struct wined3d_device_parent *device_parent = device->device_parent;
2523 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2524 unsigned int i;
2525 HRESULT hr;
2527 if (layer_count != 1)
2529 ERR("Invalid layer count for volume texture.\n");
2530 return E_INVALIDARG;
2533 /* TODO: It should only be possible to create textures for formats
2534 * that are reported as supported. */
2535 if (WINED3DFMT_UNKNOWN >= desc->format)
2537 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2538 return WINED3DERR_INVALIDCALL;
2541 if (!gl_info->supported[EXT_TEXTURE3D])
2543 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2544 return WINED3DERR_INVALIDCALL;
2547 /* Calculate levels for mip mapping. */
2548 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2550 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2552 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2553 return WINED3DERR_INVALIDCALL;
2556 if (level_count != 1)
2558 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2559 return WINED3DERR_INVALIDCALL;
2563 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2564 || desc->pool == WINED3D_POOL_SCRATCH))
2566 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2567 return WINED3DERR_INVALIDCALL;
2570 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2572 UINT pow2_w, pow2_h, pow2_d;
2573 pow2_w = 1;
2574 while (pow2_w < desc->width)
2575 pow2_w <<= 1;
2576 pow2_h = 1;
2577 while (pow2_h < desc->height)
2578 pow2_h <<= 1;
2579 pow2_d = 1;
2580 while (pow2_d < desc->depth)
2581 pow2_d <<= 1;
2583 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2585 if (desc->pool == WINED3D_POOL_SCRATCH)
2587 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2589 else
2591 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2592 desc->width, desc->height, desc->depth);
2593 return WINED3DERR_INVALIDCALL;
2598 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2599 0, device, parent, parent_ops, &texture_resource_ops)))
2601 WARN("Failed to initialize texture, returning %#x.\n", hr);
2602 return hr;
2605 texture->pow2_matrix[0] = 1.0f;
2606 texture->pow2_matrix[5] = 1.0f;
2607 texture->pow2_matrix[10] = 1.0f;
2608 texture->pow2_matrix[15] = 1.0f;
2609 texture->target = GL_TEXTURE_3D;
2611 if (wined3d_texture_use_pbo(texture, gl_info))
2613 wined3d_resource_free_sysmem(&texture->resource);
2614 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2617 /* Generate all the surfaces. */
2618 for (i = 0; i < texture->level_count; ++i)
2620 struct wined3d_texture_sub_resource *sub_resource;
2622 sub_resource = &texture->sub_resources[i];
2623 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2625 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2626 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2628 WARN("Failed to create volume parent, hr %#x.\n", hr);
2629 sub_resource->parent = NULL;
2630 wined3d_texture_cleanup_sync(texture);
2631 return hr;
2634 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2636 TRACE("Created volume level %u.\n", i);
2639 return WINED3D_OK;
2642 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2643 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2644 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2646 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2648 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2649 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2650 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2651 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2653 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2654 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2655 return WINED3DERR_INVALIDCALL;
2657 if (src_texture)
2659 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2660 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2661 return WINED3DERR_INVALIDCALL;
2664 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2665 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2668 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2669 unsigned int sub_resource_idx, LONG *x, LONG *y)
2671 struct wined3d_surface *surface;
2673 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2675 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2676 || sub_resource_idx >= texture->level_count * texture->layer_count)
2678 WARN("Invalid sub-resource specified.\n");
2679 return WINEDDERR_NOTAOVERLAYSURFACE;
2682 surface = texture->sub_resources[sub_resource_idx].u.surface;
2683 if (!surface->overlay_dest)
2685 TRACE("Overlay not visible.\n");
2686 *x = 0;
2687 *y = 0;
2688 return WINEDDERR_OVERLAYNOTVISIBLE;
2691 *x = surface->overlay_destrect.left;
2692 *y = surface->overlay_destrect.top;
2694 TRACE("Returning position %d, %d.\n", *x, *y);
2696 return WINED3D_OK;
2699 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2700 unsigned int sub_resource_idx, LONG x, LONG y)
2702 struct wined3d_texture_sub_resource *sub_resource;
2703 struct wined3d_surface *surface;
2704 LONG w, h;
2706 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2708 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2709 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2711 WARN("Invalid sub-resource specified.\n");
2712 return WINEDDERR_NOTAOVERLAYSURFACE;
2715 surface = sub_resource->u.surface;
2716 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2717 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2718 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2720 return WINED3D_OK;
2723 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2724 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2725 const RECT *dst_rect, DWORD flags)
2727 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2728 struct wined3d_surface *surface, *dst_surface;
2730 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2731 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2732 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2733 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2735 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2736 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2738 WARN("Invalid sub-resource specified.\n");
2739 return WINEDDERR_NOTAOVERLAYSURFACE;
2742 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2743 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2745 WARN("Invalid destination sub-resource specified.\n");
2746 return WINED3DERR_INVALIDCALL;
2749 surface = sub_resource->u.surface;
2750 if (src_rect)
2751 surface->overlay_srcrect = *src_rect;
2752 else
2753 SetRect(&surface->overlay_srcrect, 0, 0,
2754 wined3d_texture_get_level_width(texture, surface->texture_level),
2755 wined3d_texture_get_level_height(texture, surface->texture_level));
2757 dst_surface = dst_sub_resource->u.surface;
2758 if (dst_rect)
2759 surface->overlay_destrect = *dst_rect;
2760 else
2761 SetRect(&surface->overlay_destrect, 0, 0,
2762 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2763 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2765 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2767 surface->overlay_dest = NULL;
2768 list_remove(&surface->overlay_entry);
2771 if (flags & WINEDDOVER_SHOW)
2773 if (surface->overlay_dest != dst_surface)
2775 surface->overlay_dest = dst_surface;
2776 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2779 else if (flags & WINEDDOVER_HIDE)
2781 /* Tests show that the rectangles are erased on hide. */
2782 SetRectEmpty(&surface->overlay_srcrect);
2783 SetRectEmpty(&surface->overlay_destrect);
2784 surface->overlay_dest = NULL;
2787 return WINED3D_OK;
2790 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2792 unsigned int sub_count = texture->level_count * texture->layer_count;
2794 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2796 if (sub_resource_idx >= sub_count)
2798 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2799 return NULL;
2802 return texture->sub_resources[sub_resource_idx].parent;
2805 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2806 unsigned int sub_resource_idx, void *parent)
2808 unsigned int sub_count = texture->level_count * texture->layer_count;
2810 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2812 if (sub_resource_idx >= sub_count)
2814 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2815 return;
2818 texture->sub_resources[sub_resource_idx].parent = parent;
2821 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2822 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2824 unsigned int sub_count = texture->level_count * texture->layer_count;
2825 const struct wined3d_resource *resource;
2826 unsigned int level_idx;
2828 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2830 if (sub_resource_idx >= sub_count)
2832 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2833 return WINED3DERR_INVALIDCALL;
2836 resource = &texture->resource;
2837 desc->format = resource->format->id;
2838 desc->multisample_type = resource->multisample_type;
2839 desc->multisample_quality = resource->multisample_quality;
2840 desc->usage = resource->usage;
2841 desc->pool = resource->pool;
2843 level_idx = sub_resource_idx % texture->level_count;
2844 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2845 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2846 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2847 desc->size = texture->sub_resources[sub_resource_idx].size;
2849 return WINED3D_OK;
2852 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2853 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2854 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2856 struct wined3d_texture *object;
2857 HRESULT hr;
2859 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2860 "parent %p, parent_ops %p, texture %p.\n",
2861 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2863 if (!layer_count)
2865 WARN("Invalid layer count.\n");
2866 return E_INVALIDARG;
2868 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2870 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2871 layer_count = 6;
2874 if (!level_count)
2876 WARN("Invalid level count.\n");
2877 return WINED3DERR_INVALIDCALL;
2880 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2882 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2883 desc->format, desc->usage);
2885 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2886 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2888 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2889 desc->multisample_quality);
2890 return WINED3DERR_NOTAVAILABLE;
2892 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2893 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2894 || desc->multisample_quality))
2896 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2897 desc->multisample_quality);
2898 return WINED3DERR_NOTAVAILABLE;
2902 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2903 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2904 return E_OUTOFMEMORY;
2906 switch (desc->resource_type)
2908 case WINED3D_RTYPE_TEXTURE_2D:
2909 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2910 break;
2912 case WINED3D_RTYPE_TEXTURE_3D:
2913 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2914 break;
2916 default:
2917 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2918 hr = WINED3DERR_INVALIDCALL;
2919 break;
2922 if (FAILED(hr))
2924 WARN("Failed to initialize texture, returning %#x.\n", hr);
2925 HeapFree(GetProcessHeap(), 0, object);
2926 return hr;
2929 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2930 * in this case. */
2931 if (data)
2933 unsigned int sub_count = level_count * layer_count;
2934 unsigned int i;
2936 for (i = 0; i < sub_count; ++i)
2938 if (!data[i].data)
2940 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2941 wined3d_texture_cleanup_sync(object);
2942 HeapFree(GetProcessHeap(), 0, object);
2943 return E_INVALIDARG;
2947 for (i = 0; i < sub_count; ++i)
2949 wined3d_device_update_sub_resource(device, &object->resource,
2950 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
2954 TRACE("Created texture %p.\n", object);
2955 *texture = object;
2957 return WINED3D_OK;
2960 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2962 struct wined3d_device *device = texture->resource.device;
2963 struct wined3d_texture_sub_resource *sub_resource;
2964 struct wined3d_context *context = NULL;
2965 struct wined3d_surface *surface;
2966 HRESULT hr = WINED3D_OK;
2968 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2970 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
2972 WARN("Texture does not support GetDC\n");
2973 /* Don't touch the DC */
2974 return WINED3DERR_INVALIDCALL;
2977 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2978 return WINED3DERR_INVALIDCALL;
2980 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2982 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2983 return WINED3DERR_INVALIDCALL;
2986 surface = sub_resource->u.surface;
2988 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2989 return WINED3DERR_INVALIDCALL;
2991 if (device->d3d_initialized)
2992 context = context_acquire(device, NULL, 0);
2994 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2995 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2997 if (!surface->dc)
2998 hr = wined3d_surface_create_dc(surface);
2999 if (context)
3000 context_release(context);
3001 if (FAILED(hr))
3002 return WINED3DERR_INVALIDCALL;
3004 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3005 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3006 ++texture->resource.map_count;
3007 ++sub_resource->map_count;
3009 *dc = surface->dc;
3010 TRACE("Returning dc %p.\n", *dc);
3012 return hr;
3015 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3017 struct wined3d_device *device = texture->resource.device;
3018 struct wined3d_texture_sub_resource *sub_resource;
3019 struct wined3d_surface *surface;
3021 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3023 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3024 return WINED3DERR_INVALIDCALL;
3026 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3028 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3029 return WINED3DERR_INVALIDCALL;
3032 surface = sub_resource->u.surface;
3034 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3035 return WINED3DERR_INVALIDCALL;
3037 if (surface->dc != dc)
3039 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
3040 return WINED3DERR_INVALIDCALL;
3043 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3044 wined3d_surface_destroy_dc(surface);
3046 --sub_resource->map_count;
3047 if (!--texture->resource.map_count && texture->update_map_binding)
3048 wined3d_texture_update_map_binding(texture);
3049 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3050 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3052 return WINED3D_OK;