wined3d: Add ARB_texture_filter_anisotropic extension.
[wine.git] / dlls / wined3d / texture.c
blob230f94c6fa6e633310ec544258110b0718b9fc70
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 (texture == swapchain->front_buffer)
65 TRACE("Returning GL_FRONT.\n");
66 return GL_FRONT;
69 if (texture == swapchain->back_buffers[0])
71 TRACE("Returning GL_BACK.\n");
72 return GL_BACK;
75 FIXME("Higher back buffer, returning GL_BACK.\n");
76 return GL_BACK;
79 static DWORD wined3d_resource_access_from_location(DWORD location)
81 switch (location)
83 case WINED3D_LOCATION_DISCARDED:
84 return 0;
86 case WINED3D_LOCATION_SYSMEM:
87 case WINED3D_LOCATION_USER_MEMORY:
88 return WINED3D_RESOURCE_ACCESS_CPU;
90 case WINED3D_LOCATION_BUFFER:
91 case WINED3D_LOCATION_DRAWABLE:
92 case WINED3D_LOCATION_TEXTURE_RGB:
93 case WINED3D_LOCATION_TEXTURE_SRGB:
94 case WINED3D_LOCATION_RB_MULTISAMPLE:
95 case WINED3D_LOCATION_RB_RESOLVED:
96 return WINED3D_RESOURCE_ACCESS_GPU;
98 default:
99 FIXME("Unhandled location %#x.\n", location);
100 return 0;
104 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
106 struct wined3d_texture_sub_resource *sub_resource;
107 unsigned int i, sub_count;
109 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
110 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
112 TRACE("Not evicting system memory for texture %p.\n", texture);
113 return;
116 TRACE("Evicting system memory for texture %p.\n", texture);
118 sub_count = texture->level_count * texture->layer_count;
119 for (i = 0; i < sub_count; ++i)
121 sub_resource = &texture->sub_resources[i];
122 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
123 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
124 i, texture);
125 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
127 wined3d_resource_free_sysmem(&texture->resource);
130 void wined3d_texture_validate_location(struct wined3d_texture *texture,
131 unsigned int sub_resource_idx, DWORD location)
133 struct wined3d_texture_sub_resource *sub_resource;
134 DWORD previous_locations;
136 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
137 texture, sub_resource_idx, wined3d_debug_location(location));
139 sub_resource = &texture->sub_resources[sub_resource_idx];
140 previous_locations = sub_resource->locations;
141 sub_resource->locations |= location;
142 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
143 && !--texture->sysmem_count)
144 wined3d_texture_evict_sysmem(texture);
146 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
149 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
151 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
154 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
155 unsigned int sub_resource_idx, DWORD location)
157 struct wined3d_texture_sub_resource *sub_resource;
158 DWORD previous_locations;
160 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
161 texture, sub_resource_idx, wined3d_debug_location(location));
163 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
164 wined3d_texture_set_dirty(texture);
166 sub_resource = &texture->sub_resources[sub_resource_idx];
167 previous_locations = sub_resource->locations;
168 sub_resource->locations &= ~location;
169 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
170 ++texture->sysmem_count;
172 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
174 if (!sub_resource->locations)
175 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
176 sub_resource_idx, texture);
179 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
180 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
182 unsigned int size = texture->sub_resources[sub_resource_idx].size;
183 struct wined3d_device *device = texture->resource.device;
184 const struct wined3d_gl_info *gl_info;
185 struct wined3d_bo_address dst, src;
187 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
188 return FALSE;
190 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
191 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
192 texture->sub_resources[sub_resource_idx].locations);
194 if (dst.buffer_object)
196 context = context_acquire(device, NULL, 0);
197 gl_info = context->gl_info;
198 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
199 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
200 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
201 checkGLcall("PBO upload");
202 context_release(context);
203 return TRUE;
206 if (src.buffer_object)
208 context = context_acquire(device, NULL, 0);
209 gl_info = context->gl_info;
210 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
211 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
212 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
213 checkGLcall("PBO download");
214 context_release(context);
215 return TRUE;
218 memcpy(dst.addr, src.addr, size);
219 return TRUE;
222 /* Context activation is done by the caller. Context may be NULL in
223 * WINED3D_NO3D mode. */
224 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
225 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
227 static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
228 | WINED3D_LOCATION_BUFFER;
229 DWORD current = texture->sub_resources[sub_resource_idx].locations;
230 BOOL ret;
232 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
233 texture, sub_resource_idx, context, wined3d_debug_location(location));
235 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
237 if (current & location)
239 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
240 return TRUE;
243 if (WARN_ON(d3d))
245 DWORD required_access = wined3d_resource_access_from_location(location);
246 if ((texture->resource.access_flags & required_access) != required_access)
247 WARN("Operation requires %#x access, but texture only has %#x.\n",
248 required_access, texture->resource.access_flags);
251 if (current & WINED3D_LOCATION_DISCARDED)
253 TRACE("Sub-resource previously discarded, nothing to do.\n");
254 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
255 return FALSE;
256 wined3d_texture_validate_location(texture, sub_resource_idx, location);
257 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
258 return TRUE;
261 if (!current)
263 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
264 sub_resource_idx, texture);
265 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
266 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
269 if ((location & sysmem_locations) && (current & sysmem_locations))
270 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
271 else
272 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
274 if (ret)
275 wined3d_texture_validate_location(texture, sub_resource_idx, location);
277 return ret;
280 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
281 struct wined3d_bo_address *data, DWORD locations)
283 struct wined3d_texture_sub_resource *sub_resource;
285 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
286 texture, sub_resource_idx, data, wined3d_debug_location(locations));
288 sub_resource = &texture->sub_resources[sub_resource_idx];
289 if (locations & WINED3D_LOCATION_BUFFER)
291 data->addr = NULL;
292 data->buffer_object = sub_resource->buffer_object;
293 return;
295 if (locations & WINED3D_LOCATION_USER_MEMORY)
297 data->addr = texture->user_memory;
298 data->buffer_object = 0;
299 return;
301 if (locations & WINED3D_LOCATION_SYSMEM)
303 data->addr = texture->resource.heap_memory;
304 data->addr += sub_resource->offset;
305 data->buffer_object = 0;
306 return;
309 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
310 data->addr = NULL;
311 data->buffer_object = 0;
314 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
315 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
316 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
317 const struct wined3d_resource_ops *resource_ops)
319 unsigned int i, j, size, offset = 0;
320 const struct wined3d_format *format;
321 HRESULT hr;
323 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
324 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
325 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
326 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
327 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
328 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
329 flags, device, parent, parent_ops, resource_ops);
331 if (!desc->width || !desc->height || !desc->depth)
332 return WINED3DERR_INVALIDCALL;
334 format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
336 for (i = 0; i < layer_count; ++i)
338 for (j = 0; j < level_count; ++j)
340 unsigned int idx = i * level_count + j;
342 size = wined3d_format_calculate_size(format, device->surface_alignment,
343 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
344 texture->sub_resources[idx].offset = offset;
345 texture->sub_resources[idx].size = size;
346 offset += size;
348 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
351 if (!offset)
352 return WINED3DERR_INVALIDCALL;
354 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
355 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
356 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
358 static unsigned int once;
360 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
361 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
362 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
363 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
364 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
365 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
367 WARN("Failed to initialize resource, returning %#x\n", hr);
368 return hr;
370 wined3d_resource_update_draw_binding(&texture->resource);
371 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
372 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
374 texture->texture_ops = texture_ops;
376 texture->layer_count = layer_count;
377 texture->level_count = level_count;
378 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
379 texture->lod = 0;
380 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
381 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
382 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
383 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
384 texture->flags |= WINED3D_TEXTURE_GET_DC;
385 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
386 texture->flags |= WINED3D_TEXTURE_DISCARD;
388 return WINED3D_OK;
391 /* Context activation is done by the caller. */
392 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
393 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
395 GLuint *buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
397 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
398 checkGLcall("glDeleteBuffers");
400 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
401 *buffer_object, texture, sub_resource_idx);
403 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
404 *buffer_object = 0;
407 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
409 unsigned int sub_count = texture->level_count * texture->layer_count;
410 const struct wined3d_device *device = texture->resource.device;
411 DWORD map_binding = texture->update_map_binding;
412 struct wined3d_context *context = NULL;
413 unsigned int i;
415 if (device->d3d_initialized)
416 context = context_acquire(device, NULL, 0);
418 for (i = 0; i < sub_count; ++i)
420 if (texture->sub_resources[i].locations == texture->resource.map_binding
421 && !wined3d_texture_load_location(texture, i, context, map_binding))
422 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
423 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
424 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
427 if (context)
428 context_release(context);
430 texture->resource.map_binding = map_binding;
431 texture->update_map_binding = 0;
434 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
436 texture->update_map_binding = map_binding;
437 if (!texture->resource.map_count)
438 wined3d_texture_update_map_binding(texture);
441 /* A GL context is provided by the caller */
442 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
443 struct gl_texture *tex)
445 context_gl_resource_released(device, tex->name, FALSE);
446 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
447 tex->name = 0;
450 /* Context activation is done by the caller. */
451 /* The caller is responsible for binding the correct texture. */
452 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
453 GLenum gl_internal_format, const struct wined3d_format *format,
454 const struct wined3d_gl_info *gl_info)
456 unsigned int i, sub_call_count;
458 sub_call_count = texture->level_count;
459 if (texture->target != GL_TEXTURE_2D_ARRAY)
460 sub_call_count *= texture->layer_count;
462 for (i = 0; i < sub_call_count; ++i)
464 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
465 GLsizei width, height;
467 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
468 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
469 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
471 height *= format->height_scale.numerator;
472 height /= format->height_scale.denominator;
475 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
476 surface, surface->texture_target, surface->texture_level, width, height);
478 if (texture->target == GL_TEXTURE_2D_ARRAY)
480 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
481 gl_internal_format, width, height, texture->layer_count, 0,
482 format->glFormat, format->glType, NULL));
483 checkGLcall("glTexImage3D");
485 else
487 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
488 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
489 checkGLcall("glTexImage2D");
494 /* Context activation is done by the caller. */
495 /* The caller is responsible for binding the correct texture. */
496 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
497 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
499 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
500 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
502 if (texture->target == GL_TEXTURE_2D_ARRAY)
504 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
505 width, height, texture->layer_count));
506 checkGLcall("glTexStorage3D");
508 else
510 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
511 width, height));
512 checkGLcall("glTexStorage2D");
516 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
518 struct wined3d_device *device = texture->resource.device;
519 const struct wined3d_gl_info *gl_info = NULL;
520 struct wined3d_context *context = NULL;
522 if (texture->texture_rgb.name || texture->texture_srgb.name
523 || texture->rb_multisample || texture->rb_resolved)
525 context = context_acquire(device, NULL, 0);
526 gl_info = context->gl_info;
529 if (texture->texture_rgb.name)
530 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
532 if (texture->texture_srgb.name)
533 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
535 if (texture->rb_multisample)
537 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
538 context_gl_resource_released(device, texture->rb_multisample, TRUE);
539 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
540 texture->rb_multisample = 0;
543 if (texture->rb_resolved)
545 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
546 context_gl_resource_released(device, texture->rb_resolved, TRUE);
547 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
548 texture->rb_resolved = 0;
551 if (context) context_release(context);
553 wined3d_texture_set_dirty(texture);
555 resource_unload(&texture->resource);
558 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
560 unsigned int sub_count = texture->level_count * texture->layer_count;
561 struct wined3d_texture_sub_resource *sub_resource;
562 unsigned int i;
564 for (i = 0; i < sub_count; ++i)
566 sub_resource = &texture->sub_resources[i];
567 if (sub_resource->parent)
569 TRACE("sub-resource %u.\n", i);
570 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
571 sub_resource->parent = NULL;
576 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
578 unsigned int sub_count = texture->level_count * texture->layer_count;
579 struct wined3d_device *device = texture->resource.device;
580 struct wined3d_context *context = NULL;
581 const struct wined3d_gl_info *gl_info;
582 GLuint buffer_object;
583 unsigned int i;
585 TRACE("texture %p.\n", texture);
587 for (i = 0; i < sub_count; ++i)
589 if (!(buffer_object = texture->sub_resources[i].buffer_object))
590 continue;
592 TRACE("Deleting buffer object %u.\n", buffer_object);
594 /* We may not be able to get a context in wined3d_texture_cleanup() in
595 * general, but if a buffer object was previously created we can. */
596 if (!context)
598 context = context_acquire(device, NULL, 0);
599 gl_info = context->gl_info;
602 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
604 if (context)
605 context_release(context);
607 texture->texture_ops->texture_cleanup_sub_resources(texture);
608 wined3d_texture_unload_gl_texture(texture);
611 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
613 texture->swapchain = swapchain;
614 wined3d_resource_update_draw_binding(&texture->resource);
617 /* Context activation is done by the caller. */
618 void wined3d_texture_bind(struct wined3d_texture *texture,
619 struct wined3d_context *context, BOOL srgb)
621 const struct wined3d_gl_info *gl_info = context->gl_info;
622 const struct wined3d_format *format = texture->resource.format;
623 const struct color_fixup_desc fixup = format->color_fixup;
624 struct gl_texture *gl_tex;
625 GLenum target;
627 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
629 if (!needs_separate_srgb_gl_texture(context, texture))
630 srgb = FALSE;
632 /* sRGB mode cache for preload() calls outside drawprim. */
633 if (srgb)
634 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
635 else
636 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
638 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
639 target = texture->target;
641 if (gl_tex->name)
643 context_bind_texture(context, target, gl_tex->name);
644 return;
647 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
648 checkGLcall("glGenTextures");
649 TRACE("Generated texture %d.\n", gl_tex->name);
651 if (!gl_tex->name)
653 ERR("Failed to generate a texture name.\n");
654 return;
657 /* Initialise the state of the texture object to the OpenGL defaults, not
658 * the wined3d defaults. */
659 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
660 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
661 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
662 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
663 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
664 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
665 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
666 gl_tex->sampler_desc.lod_bias = 0.0f;
667 gl_tex->sampler_desc.min_lod = -1000.0f;
668 gl_tex->sampler_desc.max_lod = 1000.0f;
669 gl_tex->sampler_desc.max_anisotropy = 1;
670 gl_tex->sampler_desc.compare = FALSE;
671 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
672 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
673 gl_tex->sampler_desc.srgb_decode = TRUE;
674 else
675 gl_tex->sampler_desc.srgb_decode = srgb;
676 gl_tex->base_level = 0;
677 wined3d_texture_set_dirty(texture);
679 context_bind_texture(context, target, gl_tex->name);
681 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
683 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
684 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
687 /* For a new texture we have to set the texture levels after binding the
688 * texture. Beware that texture rectangles do not support mipmapping, but
689 * set the maxmiplevel if we're relying on the partial
690 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
691 * (I.e., do not care about cond_np2 here, just look for
692 * GL_TEXTURE_RECTANGLE_ARB.) */
693 if (target != GL_TEXTURE_RECTANGLE_ARB)
695 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
696 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
697 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
700 if (target == GL_TEXTURE_CUBE_MAP_ARB)
702 /* Cubemaps are always set to clamp, regardless of the sampler state. */
703 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
704 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
705 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
708 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
710 /* Conditinal non power of two textures use a different clamping
711 * default. If we're using the GL_WINE_normalized_texrect partial
712 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
713 * has the address mode set to repeat - something that prevents us
714 * from hitting the accelerated codepath. Thus manually set the GL
715 * state. The same applies to filtering. Even if the texture has only
716 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
717 * fallback on macos. */
718 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
719 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
720 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
721 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
722 checkGLcall("glTexParameteri");
723 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
724 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
725 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
726 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
727 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
730 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
732 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
733 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
736 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
738 static const GLenum swizzle_source[] =
740 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
741 GL_ONE, /* CHANNEL_SOURCE_ONE */
742 GL_RED, /* CHANNEL_SOURCE_X */
743 GL_GREEN, /* CHANNEL_SOURCE_Y */
744 GL_BLUE, /* CHANNEL_SOURCE_Z */
745 GL_ALPHA, /* CHANNEL_SOURCE_W */
747 struct
749 GLint x, y, z, w;
751 swizzle;
753 swizzle.x = swizzle_source[fixup.x_source];
754 swizzle.y = swizzle_source[fixup.y_source];
755 swizzle.z = swizzle_source[fixup.z_source];
756 swizzle.w = swizzle_source[fixup.w_source];
757 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
758 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
762 /* Context activation is done by the caller. */
763 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
764 struct wined3d_context *context, BOOL srgb)
766 /* We don't need a specific texture unit, but after binding the texture
767 * the current unit is dirty. Read the unit back instead of switching to
768 * 0, this avoids messing around with the state manager's GL states. The
769 * current texture unit should always be a valid one.
771 * To be more specific, this is tricky because we can implicitly be
772 * called from sampler() in state.c. This means we can't touch anything
773 * other than whatever happens to be the currently active texture, or we
774 * would risk marking already applied sampler states dirty again. */
775 if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
777 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
778 if (active_sampler != WINED3D_UNMAPPED_STAGE)
779 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
781 /* FIXME: Ideally we'd only do this when touching a binding that's used by
782 * a shader. */
783 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
784 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
786 wined3d_texture_bind(texture, context, srgb);
789 /* Context activation is done by the caller (state handler). */
790 /* This function relies on the correct texture being bound and loaded. */
791 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
792 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
794 const struct wined3d_gl_info *gl_info = context->gl_info;
795 GLenum target = texture->target;
796 struct gl_texture *gl_tex;
797 DWORD state;
799 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
801 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
803 state = sampler_desc->address_u;
804 if (state != gl_tex->sampler_desc.address_u)
806 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
807 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
808 gl_tex->sampler_desc.address_u = state;
811 state = sampler_desc->address_v;
812 if (state != gl_tex->sampler_desc.address_v)
814 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
815 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
816 gl_tex->sampler_desc.address_v = state;
819 state = sampler_desc->address_w;
820 if (state != gl_tex->sampler_desc.address_w)
822 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
823 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
824 gl_tex->sampler_desc.address_w = state;
827 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
828 sizeof(gl_tex->sampler_desc.border_color)))
830 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
831 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
832 sizeof(gl_tex->sampler_desc.border_color));
835 state = sampler_desc->mag_filter;
836 if (state != gl_tex->sampler_desc.mag_filter)
838 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
839 gl_tex->sampler_desc.mag_filter = state;
842 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
843 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
845 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
846 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
847 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
848 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
851 state = sampler_desc->max_anisotropy;
852 if (state != gl_tex->sampler_desc.max_anisotropy)
854 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
855 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
856 else
857 WARN("Anisotropic filtering not supported.\n");
858 gl_tex->sampler_desc.max_anisotropy = state;
861 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
862 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
863 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
865 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
866 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
867 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
870 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
872 if (sampler_desc->compare)
873 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
874 else
875 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
876 gl_tex->sampler_desc.compare = sampler_desc->compare;
879 checkGLcall("Texture parameter application");
881 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
883 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
884 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
885 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
889 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
891 ULONG refcount;
893 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
895 if (texture->swapchain)
896 return wined3d_swapchain_incref(texture->swapchain);
898 refcount = InterlockedIncrement(&texture->resource.ref);
899 TRACE("%p increasing refcount to %u.\n", texture, refcount);
901 return refcount;
904 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
906 wined3d_texture_sub_resources_destroyed(texture);
907 resource_cleanup(&texture->resource);
908 wined3d_resource_wait_idle(&texture->resource);
909 wined3d_texture_cleanup(texture);
912 static void wined3d_texture_destroy_object(void *object)
914 wined3d_texture_cleanup(object);
915 HeapFree(GetProcessHeap(), 0, object);
918 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
920 ULONG refcount;
922 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
924 if (texture->swapchain)
925 return wined3d_swapchain_decref(texture->swapchain);
927 refcount = InterlockedDecrement(&texture->resource.ref);
928 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
930 if (!refcount)
932 /* Wait for the texture to become idle if it's using user memory,
933 * since the application is allowed to free that memory once the
934 * texture is destroyed. Note that this implies that
935 * wined3d_texture_destroy_object() can't access that memory either. */
936 if (texture->user_memory)
937 wined3d_resource_wait_idle(&texture->resource);
938 wined3d_texture_sub_resources_destroyed(texture);
939 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
940 resource_cleanup(&texture->resource);
941 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
944 return refcount;
947 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
949 TRACE("texture %p.\n", texture);
951 return &texture->resource;
954 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
956 return c1->color_space_low_value == c2->color_space_low_value
957 && c1->color_space_high_value == c2->color_space_high_value;
960 /* Context activation is done by the caller */
961 void wined3d_texture_load(struct wined3d_texture *texture,
962 struct wined3d_context *context, BOOL srgb)
964 UINT sub_count = texture->level_count * texture->layer_count;
965 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
966 DWORD flag;
967 UINT i;
969 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
971 if (!needs_separate_srgb_gl_texture(context, texture))
972 srgb = FALSE;
974 if (srgb)
975 flag = WINED3D_TEXTURE_SRGB_VALID;
976 else
977 flag = WINED3D_TEXTURE_RGB_VALID;
979 if (!d3d_info->shader_color_key
980 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
981 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
982 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
983 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
985 unsigned int sub_count = texture->level_count * texture->layer_count;
986 unsigned int i;
988 TRACE("Reloading because of color key value change.\n");
989 for (i = 0; i < sub_count; i++)
991 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
992 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
993 else
994 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
997 texture->async.gl_color_key = texture->async.src_blt_color_key;
1000 if (texture->flags & flag)
1002 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1003 return;
1006 /* Reload the surfaces if the texture is marked dirty. */
1007 for (i = 0; i < sub_count; ++i)
1009 if (!wined3d_texture_load_location(texture, i, context,
1010 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1011 ERR("Failed to load location (srgb %#x).\n", srgb);
1013 texture->flags |= flag;
1016 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1018 TRACE("texture %p.\n", texture);
1020 return texture->resource.parent;
1023 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1024 unsigned int level, const struct wined3d_box *box)
1026 const struct wined3d_format *format = texture->resource.format;
1027 unsigned int width_mask, height_mask, width, height, depth;
1029 width = wined3d_texture_get_level_width(texture, level);
1030 height = wined3d_texture_get_level_height(texture, level);
1031 depth = wined3d_texture_get_level_depth(texture, level);
1033 if (box->left >= box->right || box->right > width
1034 || box->top >= box->bottom || box->bottom > height
1035 || box->front >= box->back || box->back > depth)
1037 WARN("Box %s is invalid.\n", debug_box(box));
1038 return WINEDDERR_INVALIDRECT;
1041 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1043 /* This assumes power of two block sizes, but NPOT block sizes would
1044 * be silly anyway.
1046 * This also assumes that the format's block depth is 1. */
1047 width_mask = format->block_width - 1;
1048 height_mask = format->block_height - 1;
1050 if ((box->left & width_mask) || (box->top & height_mask)
1051 || (box->right & width_mask && box->right != width)
1052 || (box->bottom & height_mask && box->bottom != height))
1054 WARN("Box %s is misaligned for %ux%u blocks.\n",
1055 debug_box(box), format->block_width, format->block_height);
1056 return WINED3DERR_INVALIDCALL;
1060 return WINED3D_OK;
1063 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1064 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1066 const struct wined3d_resource *resource = &texture->resource;
1067 unsigned int width = wined3d_texture_get_level_width(texture, level);
1068 unsigned int height = wined3d_texture_get_level_height(texture, level);
1070 if (texture->row_pitch)
1072 *row_pitch = texture->row_pitch;
1073 *slice_pitch = texture->slice_pitch;
1074 return;
1077 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1078 width, height, row_pitch, slice_pitch);
1081 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1083 DWORD old = texture->lod;
1085 TRACE("texture %p, lod %u.\n", texture, lod);
1087 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1088 * textures. The call always returns 0, and GetLOD always returns 0. */
1089 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1091 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1092 return 0;
1095 if (lod >= texture->level_count)
1096 lod = texture->level_count - 1;
1098 if (texture->lod != lod)
1100 struct wined3d_device *device = texture->resource.device;
1102 wined3d_resource_wait_idle(&texture->resource);
1103 texture->lod = lod;
1105 texture->texture_rgb.base_level = ~0u;
1106 texture->texture_srgb.base_level = ~0u;
1107 if (texture->resource.bind_count)
1108 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1109 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1112 return old;
1115 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1117 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1119 return texture->lod;
1122 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1124 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1126 return texture->level_count;
1129 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1130 enum wined3d_texture_filter_type filter_type)
1132 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1134 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1136 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1137 return WINED3DERR_INVALIDCALL;
1140 texture->filter_type = filter_type;
1142 return WINED3D_OK;
1145 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1147 TRACE("texture %p.\n", texture);
1149 return texture->filter_type;
1152 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1153 DWORD flags, const struct wined3d_color_key *color_key)
1155 struct wined3d_device *device = texture->resource.device;
1156 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1157 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1159 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1161 if (flags & ~all_flags)
1163 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1164 return WINED3DERR_INVALIDCALL;
1167 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1169 return WINED3D_OK;
1172 static void texture2d_create_dc(void *object)
1174 struct wined3d_surface *surface = object;
1175 struct wined3d_context *context = NULL;
1176 const struct wined3d_format *format;
1177 unsigned int row_pitch, slice_pitch;
1178 struct wined3d_texture *texture;
1179 struct wined3d_bo_address data;
1180 D3DKMT_CREATEDCFROMMEMORY desc;
1181 unsigned int sub_resource_idx;
1182 struct wined3d_device *device;
1183 NTSTATUS status;
1185 TRACE("surface %p.\n", surface);
1187 texture = surface->container;
1188 sub_resource_idx = surface_get_sub_resource_idx(surface);
1189 device = texture->resource.device;
1191 format = texture->resource.format;
1192 if (!format->ddi_format)
1194 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
1195 return;
1198 if (device->d3d_initialized)
1199 context = context_acquire(device, NULL, 0);
1201 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1202 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1203 wined3d_texture_get_pitch(texture, surface->texture_level, &row_pitch, &slice_pitch);
1204 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1205 desc.pMemory = context_map_bo_address(context, &data,
1206 texture->sub_resources[sub_resource_idx].size, GL_PIXEL_UNPACK_BUFFER, 0);
1208 if (context)
1209 context_release(context);
1211 desc.Format = format->ddi_format;
1212 desc.Width = wined3d_texture_get_level_width(texture, surface->texture_level);
1213 desc.Height = wined3d_texture_get_level_height(texture, surface->texture_level);
1214 desc.Pitch = row_pitch;
1215 desc.hDeviceDc = CreateCompatibleDC(NULL);
1216 desc.pColorTable = NULL;
1218 status = D3DKMTCreateDCFromMemory(&desc);
1219 DeleteDC(desc.hDeviceDc);
1220 if (status)
1222 WARN("Failed to create DC, status %#x.\n", status);
1223 return;
1226 surface->dc = desc.hDc;
1227 surface->bitmap = desc.hBitmap;
1229 TRACE("Created DC %p, bitmap %p for surface %p.\n", surface->dc, surface->bitmap, surface);
1232 static void texture2d_destroy_dc(void *object)
1234 struct wined3d_surface *surface = object;
1235 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
1236 struct wined3d_context *context = NULL;
1237 struct wined3d_texture *texture;
1238 struct wined3d_bo_address data;
1239 unsigned int sub_resource_idx;
1240 struct wined3d_device *device;
1241 NTSTATUS status;
1243 texture = surface->container;
1244 sub_resource_idx = surface_get_sub_resource_idx(surface);
1245 device = texture->resource.device;
1247 if (!surface->dc)
1249 ERR("Surface %p has no DC.\n", surface);
1250 return;
1253 TRACE("dc %p, bitmap %p.\n", surface->dc, surface->bitmap);
1255 destroy_desc.hDc = surface->dc;
1256 destroy_desc.hBitmap = surface->bitmap;
1257 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
1258 ERR("Failed to destroy dc, status %#x.\n", status);
1259 surface->dc = NULL;
1260 surface->bitmap = NULL;
1262 if (device->d3d_initialized)
1263 context = context_acquire(device, NULL, 0);
1265 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1266 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
1268 if (context)
1269 context_release(context);
1272 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1273 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1274 UINT multisample_quality, void *mem, UINT pitch)
1276 struct wined3d_device *device = texture->resource.device;
1277 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1278 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1279 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1280 struct wined3d_texture_sub_resource *sub_resource;
1281 struct wined3d_surface *surface;
1282 DWORD valid_location = 0;
1283 BOOL create_dib = FALSE;
1285 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1286 "mem %p, pitch %u.\n",
1287 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1289 if (!resource_size)
1290 return WINED3DERR_INVALIDCALL;
1292 if (texture->level_count * texture->layer_count > 1)
1294 WARN("Texture has multiple sub-resources, not supported.\n");
1295 return WINED3DERR_INVALIDCALL;
1298 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1300 WARN("Not supported on 3D textures.\n");
1301 return WINED3DERR_INVALIDCALL;
1304 if (texture->resource.map_count)
1306 WARN("Texture is mapped.\n");
1307 return WINED3DERR_INVALIDCALL;
1310 /* We have no way of supporting a pitch that is not a multiple of the pixel
1311 * byte width short of uploading the texture row-by-row.
1312 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1313 * for user-memory textures (it always expects packed data) while DirectDraw
1314 * requires a 4-byte aligned pitch and doesn't support texture formats
1315 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1316 * This check is here to verify that the assumption holds. */
1317 if (pitch % texture->resource.format->byte_count)
1319 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1320 return WINED3DERR_INVALIDCALL;
1323 if (device->d3d_initialized)
1324 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1325 wined3d_resource_wait_idle(&texture->resource);
1327 sub_resource = &texture->sub_resources[0];
1328 surface = sub_resource->u.surface;
1329 if (surface->dc)
1331 wined3d_cs_destroy_object(device->cs, texture2d_destroy_dc, surface);
1332 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1333 create_dib = TRUE;
1336 wined3d_resource_free_sysmem(&texture->resource);
1338 if ((texture->row_pitch = pitch))
1339 texture->slice_pitch = height * pitch;
1340 else
1341 /* User memory surfaces don't have the regular surface alignment. */
1342 wined3d_format_calculate_pitch(format, 1, width, height,
1343 &texture->row_pitch, &texture->slice_pitch);
1345 texture->resource.format = format;
1346 texture->resource.multisample_type = multisample_type;
1347 texture->resource.multisample_quality = multisample_quality;
1348 texture->resource.width = width;
1349 texture->resource.height = height;
1350 texture->resource.size = texture->slice_pitch;
1351 sub_resource->size = texture->slice_pitch;
1352 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1354 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1355 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1357 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1358 texture->pow2_width = texture->pow2_height = 1;
1359 while (texture->pow2_width < width)
1360 texture->pow2_width <<= 1;
1361 while (texture->pow2_height < height)
1362 texture->pow2_height <<= 1;
1364 else
1366 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1367 texture->pow2_width = width;
1368 texture->pow2_height = height;
1371 if ((texture->user_memory = mem))
1373 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1374 valid_location = WINED3D_LOCATION_USER_MEMORY;
1376 else
1378 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1379 valid_location = WINED3D_LOCATION_SYSMEM;
1382 /* The format might be changed to a format that needs conversion.
1383 * If the surface didn't use PBOs previously but could now, don't
1384 * change it - whatever made us not use PBOs might come back, e.g.
1385 * color keys. */
1386 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1387 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1389 wined3d_texture_validate_location(texture, 0, valid_location);
1390 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1392 if (create_dib)
1394 wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
1395 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1398 return WINED3D_OK;
1401 /* Context activation is done by the caller. */
1402 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1403 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1405 struct wined3d_texture_sub_resource *sub_resource;
1407 sub_resource = &texture->sub_resources[sub_resource_idx];
1408 if (sub_resource->buffer_object)
1409 return;
1411 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1412 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1413 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1414 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1415 checkGLcall("Create buffer object");
1417 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1418 sub_resource->buffer_object, texture, sub_resource_idx);
1421 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1423 unsigned int sub_count = texture->level_count * texture->layer_count;
1424 unsigned int i;
1426 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1427 | WINED3D_TEXTURE_CONVERTED);
1428 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1429 for (i = 0; i < sub_count; ++i)
1431 wined3d_texture_invalidate_location(texture, i,
1432 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1436 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1438 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1439 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1441 if (!d3d_info->shader_color_key
1442 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1443 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1445 wined3d_texture_force_reload(texture);
1447 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1448 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1451 if (texture->flags & alloc_flag)
1452 return;
1454 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1455 texture->flags |= alloc_flag;
1458 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1459 const struct wined3d_gl_info *gl_info, BOOL multisample)
1461 const struct wined3d_format *format = texture->resource.format;
1463 if (multisample)
1465 DWORD samples;
1467 if (texture->rb_multisample)
1468 return;
1470 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1471 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1472 * be mapped to GL_NV_framebuffer_multisample_coverage.
1474 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1475 * (EQAA), but it does not have an equivalent OpenGL extension. */
1477 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1478 * levels as the count of advertised multisample types for the texture
1479 * format. */
1480 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1482 unsigned int i, count = 0;
1484 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1486 if (format->multisample_types & 1u << i)
1488 if (texture->resource.multisample_quality == count++)
1489 break;
1492 samples = i + 1;
1494 else
1496 samples = texture->resource.multisample_type;
1499 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1500 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1501 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1502 format->glInternal, texture->resource.width, texture->resource.height);
1503 checkGLcall("glRenderbufferStorageMultisample()");
1504 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1506 else
1508 if (texture->rb_resolved)
1509 return;
1511 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1512 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1513 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1514 texture->resource.width, texture->resource.height);
1515 checkGLcall("glRenderbufferStorage()");
1516 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1520 /* Context activation is done by the caller. Context may be NULL in
1521 * WINED3D_NO3D mode. */
1522 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1523 struct wined3d_context *context, DWORD location)
1525 switch (location)
1527 case WINED3D_LOCATION_SYSMEM:
1528 if (texture->resource.heap_memory)
1529 return TRUE;
1531 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1533 ERR("Failed to allocate system memory.\n");
1534 return FALSE;
1536 return TRUE;
1538 case WINED3D_LOCATION_USER_MEMORY:
1539 if (!texture->user_memory)
1540 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1541 return TRUE;
1543 case WINED3D_LOCATION_BUFFER:
1544 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1545 return TRUE;
1547 case WINED3D_LOCATION_TEXTURE_RGB:
1548 wined3d_texture_prepare_texture(texture, context, FALSE);
1549 return TRUE;
1551 case WINED3D_LOCATION_TEXTURE_SRGB:
1552 wined3d_texture_prepare_texture(texture, context, TRUE);
1553 return TRUE;
1555 case WINED3D_LOCATION_DRAWABLE:
1556 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1557 ERR("Texture %p does not have a drawable.\n", texture);
1558 return TRUE;
1560 case WINED3D_LOCATION_RB_MULTISAMPLE:
1561 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1562 return TRUE;
1564 case WINED3D_LOCATION_RB_RESOLVED:
1565 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1566 return TRUE;
1568 default:
1569 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1570 return FALSE;
1574 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1576 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1577 FIXME("texture %p stub!\n", texture);
1580 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1581 unsigned int sub_resource_idx)
1583 UINT sub_count = texture->level_count * texture->layer_count;
1585 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1587 if (sub_resource_idx >= sub_count)
1589 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1590 return NULL;
1593 return &texture->sub_resources[sub_resource_idx];
1596 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1597 UINT layer, const struct wined3d_box *dirty_region)
1599 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1601 if (layer >= texture->layer_count)
1603 WARN("Invalid layer %u specified.\n", layer);
1604 return WINED3DERR_INVALIDCALL;
1607 if (dirty_region)
1608 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1610 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
1612 return WINED3D_OK;
1615 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1616 const struct wined3d_context *context, const struct wined3d_box *box,
1617 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1619 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1620 context, box, data, row_pitch, slice_pitch);
1623 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1624 const struct wined3d_context *context, const struct wined3d_box *box,
1625 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1627 unsigned int texture_level;
1628 POINT dst_point;
1629 RECT src_rect;
1631 src_rect.left = 0;
1632 src_rect.top = 0;
1633 if (box)
1635 dst_point.x = box->left;
1636 dst_point.y = box->top;
1637 src_rect.right = box->right - box->left;
1638 src_rect.bottom = box->bottom - box->top;
1640 else
1642 dst_point.x = dst_point.y = 0;
1643 texture_level = sub_resource_idx % texture->level_count;
1644 src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
1645 src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
1648 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1649 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1652 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1653 struct wined3d_context *context, DWORD location)
1655 return surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location);
1658 /* Context activation is done by the caller. */
1659 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1661 const struct wined3d_format *format = texture->resource.format;
1662 const struct wined3d_gl_info *gl_info = context->gl_info;
1663 const struct wined3d_color_key_conversion *conversion;
1664 GLenum internal;
1666 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1668 if (format->convert)
1670 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1672 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1674 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1675 format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
1676 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1679 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1681 if (srgb)
1682 internal = format->glGammaInternal;
1683 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1684 && wined3d_resource_is_offscreen(&texture->resource))
1685 internal = format->rtInternal;
1686 else
1687 internal = format->glInternal;
1689 if (!internal)
1690 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1692 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1694 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1695 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1696 else
1697 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1700 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1702 unsigned int sub_count = texture->level_count * texture->layer_count;
1703 struct wined3d_device *device = texture->resource.device;
1704 struct wined3d_texture_sub_resource *sub_resource;
1705 struct wined3d_renderbuffer_entry *entry, *entry2;
1706 const struct wined3d_gl_info *gl_info = NULL;
1707 struct wined3d_context *context = NULL;
1708 struct wined3d_surface *overlay, *cur;
1709 struct wined3d_surface *surface;
1710 unsigned int i;
1712 for (i = 0; i < sub_count; ++i)
1714 sub_resource = &texture->sub_resources[i];
1715 if (!(surface = sub_resource->u.surface))
1716 continue;
1718 TRACE("surface %p.\n", surface);
1720 if (!context && !list_empty(&surface->renderbuffers))
1722 context = context_acquire(device, NULL, 0);
1723 gl_info = context->gl_info;
1726 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1728 TRACE("Deleting renderbuffer %u.\n", entry->id);
1729 context_gl_resource_released(device, entry->id, TRUE);
1730 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1731 HeapFree(GetProcessHeap(), 0, entry);
1734 if (surface->dc)
1735 texture2d_destroy_dc(surface);
1737 if (surface->overlay_dest)
1738 list_remove(&surface->overlay_entry);
1740 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1742 list_remove(&overlay->overlay_entry);
1743 overlay->overlay_dest = NULL;
1746 if (context)
1747 context_release(context);
1748 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1751 static const struct wined3d_texture_ops texture2d_ops =
1753 texture2d_upload_data,
1754 texture2d_load_location,
1755 texture2d_prepare_texture,
1756 texture2d_cleanup_sub_resources,
1759 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1761 return texture_from_resource(resource);
1764 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1766 return wined3d_texture_incref(texture_from_resource(resource));
1769 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1771 return wined3d_texture_decref(texture_from_resource(resource));
1774 static void texture_resource_preload(struct wined3d_resource *resource)
1776 struct wined3d_texture *texture = texture_from_resource(resource);
1777 struct wined3d_context *context;
1779 context = context_acquire(resource->device, NULL, 0);
1780 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1781 context_release(context);
1784 static void wined3d_texture_unload(struct wined3d_resource *resource)
1786 struct wined3d_texture *texture = texture_from_resource(resource);
1787 UINT sub_count = texture->level_count * texture->layer_count;
1788 struct wined3d_device *device = resource->device;
1789 const struct wined3d_gl_info *gl_info;
1790 struct wined3d_context *context;
1791 UINT i;
1793 TRACE("texture %p.\n", texture);
1795 context = context_acquire(device, NULL, 0);
1796 gl_info = context->gl_info;
1798 for (i = 0; i < sub_count; ++i)
1800 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1802 if (resource->pool != WINED3D_POOL_DEFAULT
1803 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1805 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1807 else
1809 /* We should only get here on device reset/teardown for implicit
1810 * resources. */
1811 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1812 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1813 resource, i, debug_d3dpool(resource->pool));
1814 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1815 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1818 if (sub_resource->buffer_object)
1819 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1821 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1823 struct wined3d_surface *surface = sub_resource->u.surface;
1824 struct wined3d_renderbuffer_entry *entry, *entry2;
1826 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1828 context_gl_resource_released(device, entry->id, TRUE);
1829 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1830 list_remove(&entry->entry);
1831 HeapFree(GetProcessHeap(), 0, entry);
1833 list_init(&surface->renderbuffers);
1834 surface->current_renderbuffer = NULL;
1838 context_release(context);
1840 wined3d_texture_force_reload(texture);
1841 wined3d_texture_unload_gl_texture(texture);
1844 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1845 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1847 const struct wined3d_format *format = resource->format;
1848 struct wined3d_texture_sub_resource *sub_resource;
1849 struct wined3d_device *device = resource->device;
1850 unsigned int fmt_flags = resource->format_flags;
1851 struct wined3d_context *context = NULL;
1852 struct wined3d_texture *texture;
1853 struct wined3d_bo_address data;
1854 unsigned int texture_level;
1855 BYTE *base_memory;
1856 BOOL ret;
1858 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1859 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1861 texture = texture_from_resource(resource);
1862 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1863 return E_INVALIDARG;
1865 texture_level = sub_resource_idx % texture->level_count;
1866 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
1868 WARN("Map box is invalid.\n");
1869 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && resource->pool == WINED3D_POOL_DEFAULT)
1870 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1871 return WINED3DERR_INVALIDCALL;
1874 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1876 WARN("Trying to map unmappable texture.\n");
1877 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1878 return WINED3DERR_INVALIDCALL;
1881 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1883 WARN("DC is in use.\n");
1884 return WINED3DERR_INVALIDCALL;
1887 if (sub_resource->map_count)
1889 WARN("Sub-resource is already mapped.\n");
1890 return WINED3DERR_INVALIDCALL;
1893 if (device->d3d_initialized)
1894 context = context_acquire(device, NULL, 0);
1896 if (flags & WINED3D_MAP_DISCARD)
1898 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1899 wined3d_debug_location(resource->map_binding));
1900 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
1901 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
1903 else
1905 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1906 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1907 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
1910 if (!ret)
1912 ERR("Failed to prepare location.\n");
1913 context_release(context);
1914 return E_OUTOFMEMORY;
1917 if (!(flags & WINED3D_MAP_READONLY)
1918 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1919 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
1921 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
1922 base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
1923 TRACE("Base memory pointer %p.\n", base_memory);
1925 if (context)
1926 context_release(context);
1928 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1930 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1931 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1933 else
1935 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1938 if (!box)
1940 map_desc->data = base_memory;
1942 else
1944 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1946 /* Compressed textures are block based, so calculate the offset of
1947 * the block that contains the top-left pixel of the mapped box. */
1948 map_desc->data = base_memory
1949 + (box->front * map_desc->slice_pitch)
1950 + ((box->top / format->block_height) * map_desc->row_pitch)
1951 + ((box->left / format->block_width) * format->block_byte_count);
1953 else
1955 map_desc->data = base_memory
1956 + (box->front * map_desc->slice_pitch)
1957 + (box->top * map_desc->row_pitch)
1958 + (box->left * format->byte_count);
1962 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1964 RECT *r = &texture->swapchain->front_buffer_update;
1966 if (!box)
1967 SetRect(r, 0, 0, resource->width, resource->height);
1968 else
1969 SetRect(r, box->left, box->top, box->right, box->bottom);
1970 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1973 ++resource->map_count;
1974 ++sub_resource->map_count;
1976 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1977 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1979 return WINED3D_OK;
1982 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1984 struct wined3d_texture_sub_resource *sub_resource;
1985 struct wined3d_device *device = resource->device;
1986 struct wined3d_context *context = NULL;
1987 struct wined3d_texture *texture;
1988 struct wined3d_bo_address data;
1990 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1992 texture = texture_from_resource(resource);
1993 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1994 return E_INVALIDARG;
1996 if (!sub_resource->map_count)
1998 WARN("Trying to unmap unmapped sub-resource.\n");
1999 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2000 return WINED3D_OK;
2001 return WINEDDERR_NOTLOCKED;
2004 if (device->d3d_initialized)
2005 context = context_acquire(device, NULL, 0);
2007 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2008 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2010 if (context)
2011 context_release(context);
2013 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2015 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2016 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2019 --sub_resource->map_count;
2020 if (!--resource->map_count && texture->update_map_binding)
2021 wined3d_texture_update_map_binding(texture);
2023 return WINED3D_OK;
2026 static const struct wined3d_resource_ops texture_resource_ops =
2028 texture_resource_incref,
2029 texture_resource_decref,
2030 texture_resource_preload,
2031 wined3d_texture_unload,
2032 texture_resource_sub_resource_map,
2033 texture_resource_sub_resource_unmap,
2036 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2037 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2038 void *parent, const struct wined3d_parent_ops *parent_ops)
2040 struct wined3d_device_parent *device_parent = device->device_parent;
2041 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2042 struct wined3d_surface *surfaces;
2043 UINT pow2_width, pow2_height;
2044 unsigned int i, j;
2045 HRESULT hr;
2047 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2048 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2050 WARN("OpenGL implementation does not support array textures.\n");
2051 return WINED3DERR_INVALIDCALL;
2054 /* TODO: It should only be possible to create textures for formats
2055 * that are reported as supported. */
2056 if (WINED3DFMT_UNKNOWN >= desc->format)
2058 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2059 return WINED3DERR_INVALIDCALL;
2062 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
2063 FIXME("Trying to create a managed texture with dynamic usage.\n");
2064 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2065 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2066 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
2067 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
2068 FIXME("Trying to create a render target that isn't in the default pool.\n");
2070 pow2_width = desc->width;
2071 pow2_height = desc->height;
2072 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
2073 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2075 /* level_count == 0 returns an error as well. */
2076 if (level_count != 1 || layer_count != 1)
2078 if (desc->pool != WINED3D_POOL_SCRATCH)
2080 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2081 return WINED3DERR_INVALIDCALL;
2084 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2086 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2088 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2090 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2092 /* TODO: Add support for non-power-of-two compressed textures. */
2093 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2094 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2096 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2097 desc->width, desc->height);
2098 return WINED3DERR_NOTAVAILABLE;
2101 /* Find the nearest pow2 match. */
2102 pow2_width = pow2_height = 1;
2103 while (pow2_width < desc->width)
2104 pow2_width <<= 1;
2105 while (pow2_height < desc->height)
2106 pow2_height <<= 1;
2107 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2110 texture->pow2_width = pow2_width;
2111 texture->pow2_height = pow2_height;
2113 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2114 && (desc->usage & WINED3DUSAGE_TEXTURE))
2116 /* One of four options:
2117 * 1: Do the same as we do with NPOT and scale the texture. (Any
2118 * texture ops would require the texture to be scaled which is
2119 * potentially slow.)
2120 * 2: Set the texture to the maximum size (bad idea).
2121 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2122 * 4: Create the surface, but allow it to be used only for DirectDraw
2123 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2124 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2125 * the render target. */
2126 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
2128 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2129 return WINED3DERR_NOTAVAILABLE;
2132 /* We should never use this surface in combination with OpenGL. */
2133 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2136 /* Calculate levels for mip mapping. */
2137 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2139 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2141 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
2142 return WINED3DERR_INVALIDCALL;
2145 if (level_count != 1)
2147 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2148 return WINED3DERR_INVALIDCALL;
2152 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
2153 flags, device, parent, parent_ops, &texture_resource_ops)))
2155 WARN("Failed to initialize texture, returning %#x.\n", hr);
2156 return hr;
2159 /* Precalculated scaling for 'faked' non power of two texture coords. */
2160 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2162 texture->pow2_matrix[0] = (float)desc->width;
2163 texture->pow2_matrix[5] = (float)desc->height;
2164 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2165 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2167 else
2169 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2171 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2172 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2173 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2175 else
2177 texture->pow2_matrix[0] = 1.0f;
2178 texture->pow2_matrix[5] = 1.0f;
2180 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2181 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2182 else if (layer_count > 1)
2183 texture->target = GL_TEXTURE_2D_ARRAY;
2184 else
2185 texture->target = GL_TEXTURE_2D;
2187 texture->pow2_matrix[10] = 1.0f;
2188 texture->pow2_matrix[15] = 1.0f;
2189 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2191 if (wined3d_texture_use_pbo(texture, gl_info))
2192 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2194 if (level_count > ~(SIZE_T)0 / layer_count
2195 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2197 wined3d_texture_cleanup_sync(texture);
2198 return E_OUTOFMEMORY;
2201 /* Generate all the surfaces. */
2202 for (i = 0; i < texture->level_count; ++i)
2204 for (j = 0; j < texture->layer_count; ++j)
2206 static const GLenum cube_targets[6] =
2208 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2209 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2210 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2211 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2212 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2213 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2215 struct wined3d_texture_sub_resource *sub_resource;
2216 unsigned int idx = j * texture->level_count + i;
2217 struct wined3d_surface *surface;
2219 surface = &surfaces[idx];
2220 surface->container = texture;
2221 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2222 surface->texture_level = i;
2223 surface->texture_layer = j;
2224 list_init(&surface->renderbuffers);
2225 list_init(&surface->overlays);
2227 sub_resource = &texture->sub_resources[idx];
2228 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2229 sub_resource->u.surface = surface;
2230 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2232 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2233 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2236 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2237 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2239 WARN("Failed to create surface parent, hr %#x.\n", hr);
2240 sub_resource->parent = NULL;
2241 wined3d_texture_cleanup_sync(texture);
2242 return hr;
2245 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2247 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2249 if ((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2251 wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
2252 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2253 if (!surface->dc)
2255 wined3d_texture_cleanup_sync(texture);
2256 return WINED3DERR_INVALIDCALL;
2262 return WINED3D_OK;
2265 /* This call just uploads data, the caller is responsible for binding the
2266 * correct texture. */
2267 /* Context activation is done by the caller. */
2268 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2269 const struct wined3d_context *context, const struct wined3d_box *box,
2270 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2272 const struct wined3d_format *format = texture->resource.format;
2273 unsigned int level = sub_resource_idx % texture->level_count;
2274 const struct wined3d_gl_info *gl_info = context->gl_info;
2275 unsigned int x, y, z, update_w, update_h, update_d;
2276 unsigned int dst_row_pitch, dst_slice_pitch;
2277 unsigned int width, height, depth;
2278 const void *mem = data->addr;
2279 void *converted_mem = NULL;
2281 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2282 texture, sub_resource_idx, context, debug_box(box),
2283 data->buffer_object, data->addr, row_pitch, slice_pitch);
2285 width = wined3d_texture_get_level_width(texture, level);
2286 height = wined3d_texture_get_level_height(texture, level);
2287 depth = wined3d_texture_get_level_depth(texture, level);
2289 if (!box)
2291 x = y = z = 0;
2292 update_w = width;
2293 update_h = height;
2294 update_d = depth;
2296 else
2298 x = box->left;
2299 y = box->top;
2300 z = box->front;
2301 update_w = box->right - box->left;
2302 update_h = box->bottom - box->top;
2303 update_d = box->back - box->front;
2306 if (format->convert)
2308 if (data->buffer_object)
2309 ERR("Loading a converted texture from a PBO.\n");
2310 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2311 ERR("Converting a block-based format.\n");
2313 dst_row_pitch = update_w * format->conv_byte_count;
2314 dst_slice_pitch = dst_row_pitch * update_h;
2316 converted_mem = wined3d_calloc(update_d, dst_slice_pitch);
2317 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2318 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2319 mem = converted_mem;
2321 else
2323 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2324 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2325 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2328 if (data->buffer_object)
2330 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2331 checkGLcall("glBindBuffer");
2334 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2335 update_w, update_h, update_d, format->glFormat, format->glType, mem));
2336 checkGLcall("glTexSubImage3D");
2338 if (data->buffer_object)
2340 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2341 checkGLcall("glBindBuffer");
2344 HeapFree(GetProcessHeap(), 0, converted_mem);
2347 /* Context activation is done by the caller. */
2348 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2349 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2351 const struct wined3d_format *format = texture->resource.format;
2352 const struct wined3d_gl_info *gl_info = context->gl_info;
2354 if (format->convert)
2356 FIXME("Attempting to download a converted volume, format %s.\n",
2357 debug_d3dformat(format->id));
2358 return;
2361 if (data->buffer_object)
2363 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2364 checkGLcall("glBindBuffer");
2367 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2368 format->glFormat, format->glType, data->addr);
2369 checkGLcall("glGetTexImage");
2371 if (data->buffer_object)
2373 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2374 checkGLcall("glBindBuffer");
2379 /* Context activation is done by the caller. */
2380 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2381 struct wined3d_context *context, BOOL dest_is_srgb)
2383 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2384 unsigned int row_pitch, slice_pitch;
2385 struct wined3d_bo_address data;
2387 /* Optimisations are possible, but the effort should be put into either
2388 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2389 * picked the wrong copy for the original upload and fixing that.
2391 * Also keep in mind that we want to avoid using resource.heap_memory
2392 * for DEFAULT pool surfaces. */
2393 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2394 data.buffer_object = 0;
2395 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2396 return;
2398 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2399 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2400 texture3d_download_data(texture, sub_resource_idx, context, &data);
2401 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2402 texture3d_upload_data(texture, sub_resource_idx, context,
2403 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2405 HeapFree(GetProcessHeap(), 0, data.addr);
2408 /* Context activation is done by the caller. */
2409 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2410 struct wined3d_context *context, DWORD location)
2412 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2413 unsigned int row_pitch, slice_pitch;
2415 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2416 return FALSE;
2418 switch (location)
2420 case WINED3D_LOCATION_TEXTURE_RGB:
2421 case WINED3D_LOCATION_TEXTURE_SRGB:
2422 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2424 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2425 data.addr += sub_resource->offset;
2426 wined3d_texture_bind_and_dirtify(texture, context,
2427 location == WINED3D_LOCATION_TEXTURE_SRGB);
2428 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2429 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2431 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2433 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2434 wined3d_texture_bind_and_dirtify(texture, context,
2435 location == WINED3D_LOCATION_TEXTURE_SRGB);
2436 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2437 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2439 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2441 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2443 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2445 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2447 else
2449 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2450 return FALSE;
2452 break;
2454 case WINED3D_LOCATION_SYSMEM:
2455 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2457 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2459 data.addr += sub_resource->offset;
2460 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2461 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2462 else
2463 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2465 texture3d_download_data(texture, sub_resource_idx, context, &data);
2466 ++texture->download_count;
2468 else
2470 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2471 wined3d_debug_location(sub_resource->locations));
2472 return FALSE;
2474 break;
2476 case WINED3D_LOCATION_BUFFER:
2477 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2479 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2481 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2482 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2483 else
2484 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2486 texture3d_download_data(texture, sub_resource_idx, context, &data);
2488 else
2490 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2491 wined3d_debug_location(sub_resource->locations));
2492 return FALSE;
2494 break;
2496 default:
2497 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2498 wined3d_debug_location(sub_resource->locations));
2499 return FALSE;
2502 return TRUE;
2505 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2507 const struct wined3d_format *format = texture->resource.format;
2508 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2509 unsigned int sub_count = texture->level_count * texture->layer_count;
2510 const struct wined3d_gl_info *gl_info = context->gl_info;
2511 unsigned int i;
2513 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2515 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2517 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2518 wined3d_texture_get_level_width(texture, 0),
2519 wined3d_texture_get_level_height(texture, 0),
2520 wined3d_texture_get_level_depth(texture, 0)));
2521 checkGLcall("glTexStorage3D");
2523 else
2525 for (i = 0; i < sub_count; ++i)
2527 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2528 wined3d_texture_get_level_width(texture, i),
2529 wined3d_texture_get_level_height(texture, i),
2530 wined3d_texture_get_level_depth(texture, i),
2531 0, format->glFormat, format->glType, NULL));
2532 checkGLcall("glTexImage3D");
2537 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2541 static const struct wined3d_texture_ops texture3d_ops =
2543 texture3d_upload_data,
2544 texture3d_load_location,
2545 texture3d_prepare_texture,
2546 texture3d_cleanup_sub_resources,
2549 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2550 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2551 const struct wined3d_parent_ops *parent_ops)
2553 struct wined3d_device_parent *device_parent = device->device_parent;
2554 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2555 unsigned int i;
2556 HRESULT hr;
2558 if (layer_count != 1)
2560 ERR("Invalid layer count for volume texture.\n");
2561 return E_INVALIDARG;
2564 /* TODO: It should only be possible to create textures for formats
2565 * that are reported as supported. */
2566 if (WINED3DFMT_UNKNOWN >= desc->format)
2568 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2569 return WINED3DERR_INVALIDCALL;
2572 if (!gl_info->supported[EXT_TEXTURE3D])
2574 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2575 return WINED3DERR_INVALIDCALL;
2578 /* Calculate levels for mip mapping. */
2579 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2581 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2583 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2584 return WINED3DERR_INVALIDCALL;
2587 if (level_count != 1)
2589 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2590 return WINED3DERR_INVALIDCALL;
2594 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2595 || desc->pool == WINED3D_POOL_SCRATCH))
2597 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2598 return WINED3DERR_INVALIDCALL;
2601 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2603 UINT pow2_w, pow2_h, pow2_d;
2604 pow2_w = 1;
2605 while (pow2_w < desc->width)
2606 pow2_w <<= 1;
2607 pow2_h = 1;
2608 while (pow2_h < desc->height)
2609 pow2_h <<= 1;
2610 pow2_d = 1;
2611 while (pow2_d < desc->depth)
2612 pow2_d <<= 1;
2614 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2616 if (desc->pool == WINED3D_POOL_SCRATCH)
2618 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2620 else
2622 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2623 desc->width, desc->height, desc->depth);
2624 return WINED3DERR_INVALIDCALL;
2629 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2630 0, device, parent, parent_ops, &texture_resource_ops)))
2632 WARN("Failed to initialize texture, returning %#x.\n", hr);
2633 return hr;
2636 texture->pow2_matrix[0] = 1.0f;
2637 texture->pow2_matrix[5] = 1.0f;
2638 texture->pow2_matrix[10] = 1.0f;
2639 texture->pow2_matrix[15] = 1.0f;
2640 texture->target = GL_TEXTURE_3D;
2642 if (wined3d_texture_use_pbo(texture, gl_info))
2644 wined3d_resource_free_sysmem(&texture->resource);
2645 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2648 /* Generate all the surfaces. */
2649 for (i = 0; i < texture->level_count; ++i)
2651 struct wined3d_texture_sub_resource *sub_resource;
2653 sub_resource = &texture->sub_resources[i];
2654 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2656 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2657 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2659 WARN("Failed to create volume parent, hr %#x.\n", hr);
2660 sub_resource->parent = NULL;
2661 wined3d_texture_cleanup_sync(texture);
2662 return hr;
2665 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2667 TRACE("Created volume level %u.\n", i);
2670 return WINED3D_OK;
2673 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2674 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2675 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2677 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
2678 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
2679 unsigned int dst_format_flags, src_format_flags = 0;
2680 HRESULT hr;
2682 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2683 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2684 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2685 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2687 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
2688 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2689 return WINED3DERR_INVALIDCALL;
2691 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
2692 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2693 return WINED3DERR_INVALIDCALL;
2695 dst_format_flags = dst_texture->resource.format_flags;
2696 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
2697 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
2698 return hr;
2700 src_format_flags = src_texture->resource.format_flags;
2701 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
2702 src_sub_resource_idx % src_texture->level_count, &src_box)))
2703 return hr;
2705 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
2706 || src_texture->sub_resources[src_sub_resource_idx].map_count)
2708 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2709 return WINEDDERR_SURFACEBUSY;
2712 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2713 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
2715 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2716 return WINED3DERR_INVALIDCALL;
2719 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
2720 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
2722 return WINED3D_OK;
2725 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2726 unsigned int sub_resource_idx, LONG *x, LONG *y)
2728 struct wined3d_surface *surface;
2730 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2732 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2733 || sub_resource_idx >= texture->level_count * texture->layer_count)
2735 WARN("Invalid sub-resource specified.\n");
2736 return WINEDDERR_NOTAOVERLAYSURFACE;
2739 surface = texture->sub_resources[sub_resource_idx].u.surface;
2740 if (!surface->overlay_dest)
2742 TRACE("Overlay not visible.\n");
2743 *x = 0;
2744 *y = 0;
2745 return WINEDDERR_OVERLAYNOTVISIBLE;
2748 *x = surface->overlay_destrect.left;
2749 *y = surface->overlay_destrect.top;
2751 TRACE("Returning position %d, %d.\n", *x, *y);
2753 return WINED3D_OK;
2756 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2757 unsigned int sub_resource_idx, LONG x, LONG y)
2759 struct wined3d_texture_sub_resource *sub_resource;
2760 struct wined3d_surface *surface;
2761 LONG w, h;
2763 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2765 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2766 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2768 WARN("Invalid sub-resource specified.\n");
2769 return WINEDDERR_NOTAOVERLAYSURFACE;
2772 surface = sub_resource->u.surface;
2773 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2774 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2775 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2777 return WINED3D_OK;
2780 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2781 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2782 const RECT *dst_rect, DWORD flags)
2784 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2785 struct wined3d_surface *surface, *dst_surface;
2787 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2788 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2789 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2790 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2792 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2793 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2795 WARN("Invalid sub-resource specified.\n");
2796 return WINEDDERR_NOTAOVERLAYSURFACE;
2799 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2800 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2802 WARN("Invalid destination sub-resource specified.\n");
2803 return WINED3DERR_INVALIDCALL;
2806 surface = sub_resource->u.surface;
2807 if (src_rect)
2808 surface->overlay_srcrect = *src_rect;
2809 else
2810 SetRect(&surface->overlay_srcrect, 0, 0,
2811 wined3d_texture_get_level_width(texture, surface->texture_level),
2812 wined3d_texture_get_level_height(texture, surface->texture_level));
2814 dst_surface = dst_sub_resource->u.surface;
2815 if (dst_rect)
2816 surface->overlay_destrect = *dst_rect;
2817 else
2818 SetRect(&surface->overlay_destrect, 0, 0,
2819 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2820 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2822 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2824 surface->overlay_dest = NULL;
2825 list_remove(&surface->overlay_entry);
2828 if (flags & WINEDDOVER_SHOW)
2830 if (surface->overlay_dest != dst_surface)
2832 surface->overlay_dest = dst_surface;
2833 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2836 else if (flags & WINEDDOVER_HIDE)
2838 /* Tests show that the rectangles are erased on hide. */
2839 SetRectEmpty(&surface->overlay_srcrect);
2840 SetRectEmpty(&surface->overlay_destrect);
2841 surface->overlay_dest = NULL;
2844 return WINED3D_OK;
2847 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2849 unsigned int sub_count = texture->level_count * texture->layer_count;
2851 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2853 if (sub_resource_idx >= sub_count)
2855 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2856 return NULL;
2859 return texture->sub_resources[sub_resource_idx].parent;
2862 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2863 unsigned int sub_resource_idx, void *parent)
2865 unsigned int sub_count = texture->level_count * texture->layer_count;
2867 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2869 if (sub_resource_idx >= sub_count)
2871 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2872 return;
2875 texture->sub_resources[sub_resource_idx].parent = parent;
2878 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2879 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2881 unsigned int sub_count = texture->level_count * texture->layer_count;
2882 const struct wined3d_resource *resource;
2883 unsigned int level_idx;
2885 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2887 if (sub_resource_idx >= sub_count)
2889 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2890 return WINED3DERR_INVALIDCALL;
2893 resource = &texture->resource;
2894 desc->format = resource->format->id;
2895 desc->multisample_type = resource->multisample_type;
2896 desc->multisample_quality = resource->multisample_quality;
2897 desc->usage = resource->usage;
2898 desc->pool = resource->pool;
2900 level_idx = sub_resource_idx % texture->level_count;
2901 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2902 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2903 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2904 desc->size = texture->sub_resources[sub_resource_idx].size;
2906 return WINED3D_OK;
2909 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2910 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2911 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2913 struct wined3d_texture *object;
2914 HRESULT hr;
2916 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2917 "parent %p, parent_ops %p, texture %p.\n",
2918 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2920 if (!layer_count)
2922 WARN("Invalid layer count.\n");
2923 return E_INVALIDARG;
2925 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2927 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2928 layer_count = 6;
2931 if (!level_count)
2933 WARN("Invalid level count.\n");
2934 return WINED3DERR_INVALIDCALL;
2937 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2939 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2940 desc->format, desc->usage);
2942 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2943 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2945 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2946 desc->multisample_quality);
2947 return WINED3DERR_NOTAVAILABLE;
2949 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2950 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2951 || desc->multisample_quality))
2953 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2954 desc->multisample_quality);
2955 return WINED3DERR_NOTAVAILABLE;
2959 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2960 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2961 return E_OUTOFMEMORY;
2963 switch (desc->resource_type)
2965 case WINED3D_RTYPE_TEXTURE_2D:
2966 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2967 break;
2969 case WINED3D_RTYPE_TEXTURE_3D:
2970 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2971 break;
2973 default:
2974 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2975 hr = WINED3DERR_INVALIDCALL;
2976 break;
2979 if (FAILED(hr))
2981 WARN("Failed to initialize texture, returning %#x.\n", hr);
2982 HeapFree(GetProcessHeap(), 0, object);
2983 return hr;
2986 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2987 * in this case. */
2988 if (data)
2990 unsigned int sub_count = level_count * layer_count;
2991 unsigned int i;
2993 for (i = 0; i < sub_count; ++i)
2995 if (!data[i].data)
2997 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2998 wined3d_texture_cleanup_sync(object);
2999 HeapFree(GetProcessHeap(), 0, object);
3000 return E_INVALIDARG;
3004 for (i = 0; i < sub_count; ++i)
3006 wined3d_device_update_sub_resource(device, &object->resource,
3007 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
3011 TRACE("Created texture %p.\n", object);
3012 *texture = object;
3014 return WINED3D_OK;
3017 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
3019 struct wined3d_device *device = texture->resource.device;
3020 struct wined3d_texture_sub_resource *sub_resource;
3021 struct wined3d_surface *surface;
3023 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3025 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
3027 WARN("Texture does not support GetDC\n");
3028 /* Don't touch the DC */
3029 return WINED3DERR_INVALIDCALL;
3032 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3033 return WINED3DERR_INVALIDCALL;
3035 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3037 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3038 return WINED3DERR_INVALIDCALL;
3041 surface = sub_resource->u.surface;
3043 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3044 return WINED3DERR_INVALIDCALL;
3046 if (!surface->dc)
3048 wined3d_cs_init_object(device->cs, texture2d_create_dc, surface);
3049 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3051 if (!surface->dc)
3052 return WINED3DERR_INVALIDCALL;
3054 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3055 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3056 ++texture->resource.map_count;
3057 ++sub_resource->map_count;
3059 *dc = surface->dc;
3060 TRACE("Returning dc %p.\n", *dc);
3062 return WINED3D_OK;
3065 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3067 struct wined3d_device *device = texture->resource.device;
3068 struct wined3d_texture_sub_resource *sub_resource;
3069 struct wined3d_surface *surface;
3071 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3073 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3074 return WINED3DERR_INVALIDCALL;
3076 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3078 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3079 return WINED3DERR_INVALIDCALL;
3082 surface = sub_resource->u.surface;
3084 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3085 return WINED3DERR_INVALIDCALL;
3087 if (surface->dc != dc)
3089 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
3090 return WINED3DERR_INVALIDCALL;
3093 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3095 wined3d_cs_destroy_object(device->cs, texture2d_destroy_dc, surface);
3096 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3099 --sub_resource->map_count;
3100 if (!--texture->resource.map_count && texture->update_map_binding)
3101 wined3d_texture_update_map_binding(texture);
3102 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3103 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3105 return WINED3D_OK;