wined3d: Allow using more than MAX_COMBINED_SAMPLERS texture image units.
[wine.git] / dlls / wined3d / texture.c
bloba1aba368ff0e31fa849844cbb4588146be3f0e0d
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
35 return texture->resource.pool == WINED3D_POOL_DEFAULT
36 && texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
37 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
38 && !texture->resource.format->convert
39 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
42 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
43 const struct wined3d_gl_info *gl_info)
45 /* We don't expect to create texture views for textures with height-scaled formats.
46 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
47 return gl_info->supported[ARB_TEXTURE_STORAGE]
48 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
51 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
53 const struct wined3d_swapchain *swapchain = texture->swapchain;
55 TRACE("texture %p.\n", texture);
57 if (!swapchain)
59 ERR("Texture %p is not part of a swapchain.\n", texture);
60 return GL_NONE;
63 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
65 if (swapchain->render_to_fbo)
67 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
68 return GL_COLOR_ATTACHMENT0;
70 TRACE("Returning GL_BACK.\n");
71 return GL_BACK;
73 else if (texture == swapchain->front_buffer)
75 TRACE("Returning GL_FRONT.\n");
76 return GL_FRONT;
79 FIXME("Higher back buffer, returning GL_BACK.\n");
80 return GL_BACK;
83 static DWORD wined3d_resource_access_from_location(DWORD location)
85 switch (location)
87 case WINED3D_LOCATION_DISCARDED:
88 return 0;
90 case WINED3D_LOCATION_SYSMEM:
91 case WINED3D_LOCATION_USER_MEMORY:
92 return WINED3D_RESOURCE_ACCESS_CPU;
94 case WINED3D_LOCATION_BUFFER:
95 case WINED3D_LOCATION_DRAWABLE:
96 case WINED3D_LOCATION_TEXTURE_RGB:
97 case WINED3D_LOCATION_TEXTURE_SRGB:
98 case WINED3D_LOCATION_RB_MULTISAMPLE:
99 case WINED3D_LOCATION_RB_RESOLVED:
100 return WINED3D_RESOURCE_ACCESS_GPU;
102 default:
103 FIXME("Unhandled location %#x.\n", location);
104 return 0;
108 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
110 struct wined3d_texture_sub_resource *sub_resource;
111 unsigned int i, sub_count;
113 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
114 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
116 TRACE("Not evicting system memory for texture %p.\n", texture);
117 return;
120 TRACE("Evicting system memory for texture %p.\n", texture);
122 sub_count = texture->level_count * texture->layer_count;
123 for (i = 0; i < sub_count; ++i)
125 sub_resource = &texture->sub_resources[i];
126 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
127 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
128 i, texture);
129 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
131 wined3d_resource_free_sysmem(&texture->resource);
134 void wined3d_texture_validate_location(struct wined3d_texture *texture,
135 unsigned int sub_resource_idx, DWORD location)
137 struct wined3d_texture_sub_resource *sub_resource;
138 DWORD previous_locations;
140 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
141 texture, sub_resource_idx, wined3d_debug_location(location));
143 sub_resource = &texture->sub_resources[sub_resource_idx];
144 previous_locations = sub_resource->locations;
145 sub_resource->locations |= location;
146 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
147 && !--texture->sysmem_count)
148 wined3d_texture_evict_sysmem(texture);
150 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
153 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
155 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
158 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
159 unsigned int sub_resource_idx, DWORD location)
161 struct wined3d_texture_sub_resource *sub_resource;
163 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
164 texture, sub_resource_idx, wined3d_debug_location(location));
166 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
167 wined3d_texture_set_dirty(texture);
169 sub_resource = &texture->sub_resources[sub_resource_idx];
170 sub_resource->locations &= ~location;
171 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
172 ++texture->sysmem_count;
174 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
176 if (!sub_resource->locations)
177 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
178 sub_resource_idx, texture);
181 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
182 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
184 unsigned int size = texture->sub_resources[sub_resource_idx].size;
185 struct wined3d_device *device = texture->resource.device;
186 const struct wined3d_gl_info *gl_info;
187 struct wined3d_bo_address dst, src;
189 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
190 return FALSE;
192 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
193 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
194 texture->sub_resources[sub_resource_idx].locations);
196 if (dst.buffer_object)
198 context = context_acquire(device, NULL, 0);
199 gl_info = context->gl_info;
200 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
201 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
202 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
203 checkGLcall("PBO upload");
204 context_release(context);
205 return TRUE;
208 if (src.buffer_object)
210 context = context_acquire(device, NULL, 0);
211 gl_info = context->gl_info;
212 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
213 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
214 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
215 checkGLcall("PBO download");
216 context_release(context);
217 return TRUE;
220 memcpy(dst.addr, src.addr, size);
221 return TRUE;
224 /* Context activation is done by the caller. Context may be NULL in
225 * WINED3D_NO3D mode. */
226 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
227 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
229 static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
230 | WINED3D_LOCATION_BUFFER;
231 DWORD current = texture->sub_resources[sub_resource_idx].locations;
232 BOOL ret;
234 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
235 texture, sub_resource_idx, context, wined3d_debug_location(location));
237 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
239 if (current & location)
241 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
242 return TRUE;
245 if (WARN_ON(d3d))
247 DWORD required_access = wined3d_resource_access_from_location(location);
248 if ((texture->resource.access_flags & required_access) != required_access)
249 WARN("Operation requires %#x access, but texture only has %#x.\n",
250 required_access, texture->resource.access_flags);
253 if (current & WINED3D_LOCATION_DISCARDED)
255 TRACE("Sub-resource previously discarded, nothing to do.\n");
256 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
257 return FALSE;
258 wined3d_texture_validate_location(texture, sub_resource_idx, location);
259 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
260 return TRUE;
263 if (!current)
265 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
266 sub_resource_idx, texture);
267 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
268 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
271 if ((location & sysmem_locations) && (current & sysmem_locations))
272 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
273 else
274 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
276 if (ret)
277 wined3d_texture_validate_location(texture, sub_resource_idx, location);
279 return ret;
282 /* Context activation is done by the caller. */
283 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
284 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
286 BYTE *memory;
288 if (!data->buffer_object)
289 return data->addr;
291 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
293 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
295 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
296 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
298 else
300 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
301 memory += (INT_PTR)data->addr;
304 GL_EXTCALL(glBindBuffer(binding, 0));
305 checkGLcall("Map buffer object");
307 return memory;
310 /* Context activation is done by the caller. */
311 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
312 const struct wined3d_gl_info *gl_info, GLenum binding)
314 if (!data->buffer_object)
315 return;
317 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
318 GL_EXTCALL(glUnmapBuffer(binding));
319 GL_EXTCALL(glBindBuffer(binding, 0));
320 checkGLcall("Unmap buffer object");
323 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
324 struct wined3d_bo_address *data, DWORD locations)
326 struct wined3d_texture_sub_resource *sub_resource;
328 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
329 texture, sub_resource_idx, data, wined3d_debug_location(locations));
331 sub_resource = &texture->sub_resources[sub_resource_idx];
332 if (locations & WINED3D_LOCATION_BUFFER)
334 data->addr = NULL;
335 data->buffer_object = sub_resource->buffer_object;
336 return;
338 if (locations & WINED3D_LOCATION_USER_MEMORY)
340 data->addr = texture->user_memory;
341 data->buffer_object = 0;
342 return;
344 if (locations & WINED3D_LOCATION_SYSMEM)
346 data->addr = texture->resource.heap_memory;
347 data->addr += sub_resource->offset;
348 data->buffer_object = 0;
349 return;
352 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
353 data->addr = NULL;
354 data->buffer_object = 0;
357 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
358 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
359 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
360 const struct wined3d_resource_ops *resource_ops)
362 unsigned int i, j, size, offset = 0;
363 const struct wined3d_format *format;
364 HRESULT hr;
366 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
367 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
368 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
369 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
370 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
371 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
372 flags, device, parent, parent_ops, resource_ops);
374 if (!desc->width || !desc->height || !desc->depth)
375 return WINED3DERR_INVALIDCALL;
377 format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
379 for (i = 0; i < layer_count; ++i)
381 for (j = 0; j < level_count; ++j)
383 unsigned int idx = i * level_count + j;
385 size = wined3d_format_calculate_size(format, device->surface_alignment,
386 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
387 texture->sub_resources[idx].offset = offset;
388 texture->sub_resources[idx].size = size;
389 offset += size;
391 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
394 if (!offset)
395 return WINED3DERR_INVALIDCALL;
397 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
398 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
399 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
401 static unsigned int once;
403 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
404 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
405 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
406 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
407 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
408 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
410 WARN("Failed to initialize resource, returning %#x\n", hr);
411 return hr;
413 wined3d_resource_update_draw_binding(&texture->resource);
414 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
415 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
417 texture->texture_ops = texture_ops;
419 texture->layer_count = layer_count;
420 texture->level_count = level_count;
421 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
422 texture->lod = 0;
423 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
424 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
425 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
426 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
427 texture->flags |= WINED3D_TEXTURE_GET_DC;
428 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
429 texture->flags |= WINED3D_TEXTURE_DISCARD;
431 return WINED3D_OK;
434 /* Context activation is done by the caller. */
435 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
436 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
438 GLuint *buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
440 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
441 checkGLcall("glDeleteBuffers");
443 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
444 *buffer_object, texture, sub_resource_idx);
446 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
447 *buffer_object = 0;
450 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
452 unsigned int sub_count = texture->level_count * texture->layer_count;
453 const struct wined3d_device *device = texture->resource.device;
454 DWORD map_binding = texture->update_map_binding;
455 struct wined3d_context *context = NULL;
456 unsigned int i;
458 if (device->d3d_initialized)
459 context = context_acquire(device, NULL, 0);
461 for (i = 0; i < sub_count; ++i)
463 if (texture->sub_resources[i].locations == texture->resource.map_binding
464 && !wined3d_texture_load_location(texture, i, context, map_binding))
465 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
466 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
467 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
470 if (context)
471 context_release(context);
473 texture->resource.map_binding = map_binding;
474 texture->update_map_binding = 0;
477 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
479 texture->update_map_binding = map_binding;
480 if (!texture->resource.map_count)
481 wined3d_texture_update_map_binding(texture);
484 /* A GL context is provided by the caller */
485 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
486 struct gl_texture *tex)
488 context_gl_resource_released(device, tex->name, FALSE);
489 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
490 tex->name = 0;
493 /* Context activation is done by the caller. */
494 /* The caller is responsible for binding the correct texture. */
495 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
496 GLenum gl_internal_format, const struct wined3d_format *format,
497 const struct wined3d_gl_info *gl_info)
499 unsigned int i, sub_call_count;
501 sub_call_count = texture->level_count;
502 if (texture->target != GL_TEXTURE_2D_ARRAY)
503 sub_call_count *= texture->layer_count;
505 for (i = 0; i < sub_call_count; ++i)
507 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
508 GLsizei width, height;
510 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
511 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
512 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
514 height *= format->height_scale.numerator;
515 height /= format->height_scale.denominator;
518 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
519 surface, surface->texture_target, surface->texture_level, width, height);
521 if (texture->target == GL_TEXTURE_2D_ARRAY)
523 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
524 gl_internal_format, width, height, texture->layer_count, 0,
525 format->glFormat, format->glType, NULL));
526 checkGLcall("glTexImage3D");
528 else
530 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
531 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
532 checkGLcall("glTexImage2D");
537 /* Context activation is done by the caller. */
538 /* The caller is responsible for binding the correct texture. */
539 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
540 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
542 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
543 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
545 if (texture->target == GL_TEXTURE_2D_ARRAY)
547 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
548 width, height, texture->layer_count));
549 checkGLcall("glTexStorage3D");
551 else
553 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
554 width, height));
555 checkGLcall("glTexStorage2D");
559 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
561 struct wined3d_device *device = texture->resource.device;
562 const struct wined3d_gl_info *gl_info = NULL;
563 struct wined3d_context *context = NULL;
565 if (texture->texture_rgb.name || texture->texture_srgb.name
566 || texture->rb_multisample || texture->rb_resolved)
568 context = context_acquire(device, NULL, 0);
569 gl_info = context->gl_info;
572 if (texture->texture_rgb.name)
573 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
575 if (texture->texture_srgb.name)
576 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
578 if (texture->rb_multisample)
580 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
581 context_gl_resource_released(device, texture->rb_multisample, TRUE);
582 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
583 texture->rb_multisample = 0;
586 if (texture->rb_resolved)
588 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
589 context_gl_resource_released(device, texture->rb_resolved, TRUE);
590 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
591 texture->rb_resolved = 0;
594 if (context) context_release(context);
596 wined3d_texture_set_dirty(texture);
598 resource_unload(&texture->resource);
601 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
603 unsigned int sub_count = texture->level_count * texture->layer_count;
604 struct wined3d_texture_sub_resource *sub_resource;
605 unsigned int i;
607 for (i = 0; i < sub_count; ++i)
609 sub_resource = &texture->sub_resources[i];
610 if (sub_resource->parent)
612 TRACE("sub-resource %u.\n", i);
613 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
614 sub_resource->parent = NULL;
619 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
621 unsigned int sub_count = texture->level_count * texture->layer_count;
622 struct wined3d_device *device = texture->resource.device;
623 struct wined3d_context *context = NULL;
624 const struct wined3d_gl_info *gl_info;
625 GLuint buffer_object;
626 unsigned int i;
628 TRACE("texture %p.\n", texture);
630 for (i = 0; i < sub_count; ++i)
632 if (!(buffer_object = texture->sub_resources[i].buffer_object))
633 continue;
635 TRACE("Deleting buffer object %u.\n", buffer_object);
637 /* We may not be able to get a context in wined3d_texture_cleanup() in
638 * general, but if a buffer object was previously created we can. */
639 if (!context)
641 context = context_acquire(device, NULL, 0);
642 gl_info = context->gl_info;
645 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
647 if (context)
648 context_release(context);
650 texture->texture_ops->texture_cleanup_sub_resources(texture);
651 wined3d_texture_unload_gl_texture(texture);
654 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
656 texture->swapchain = swapchain;
657 wined3d_resource_update_draw_binding(&texture->resource);
660 /* Context activation is done by the caller. */
661 void wined3d_texture_bind(struct wined3d_texture *texture,
662 struct wined3d_context *context, BOOL srgb)
664 const struct wined3d_gl_info *gl_info = context->gl_info;
665 const struct wined3d_format *format = texture->resource.format;
666 const struct color_fixup_desc fixup = format->color_fixup;
667 struct gl_texture *gl_tex;
668 GLenum target;
670 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
672 if (!needs_separate_srgb_gl_texture(context, texture))
673 srgb = FALSE;
675 /* sRGB mode cache for preload() calls outside drawprim. */
676 if (srgb)
677 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
678 else
679 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
681 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
682 target = texture->target;
684 if (gl_tex->name)
686 context_bind_texture(context, target, gl_tex->name);
687 return;
690 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
691 checkGLcall("glGenTextures");
692 TRACE("Generated texture %d.\n", gl_tex->name);
694 if (!gl_tex->name)
696 ERR("Failed to generate a texture name.\n");
697 return;
700 /* Initialise the state of the texture object to the OpenGL defaults, not
701 * the wined3d defaults. */
702 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
703 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
704 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
705 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
706 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
707 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
708 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
709 gl_tex->sampler_desc.lod_bias = 0.0f;
710 gl_tex->sampler_desc.min_lod = -1000.0f;
711 gl_tex->sampler_desc.max_lod = 1000.0f;
712 gl_tex->sampler_desc.max_anisotropy = 1;
713 gl_tex->sampler_desc.compare = FALSE;
714 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
715 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
716 gl_tex->sampler_desc.srgb_decode = TRUE;
717 else
718 gl_tex->sampler_desc.srgb_decode = srgb;
719 gl_tex->base_level = 0;
720 wined3d_texture_set_dirty(texture);
722 context_bind_texture(context, target, gl_tex->name);
724 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
726 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
727 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
730 /* For a new texture we have to set the texture levels after binding the
731 * texture. Beware that texture rectangles do not support mipmapping, but
732 * set the maxmiplevel if we're relying on the partial
733 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
734 * (I.e., do not care about cond_np2 here, just look for
735 * GL_TEXTURE_RECTANGLE_ARB.) */
736 if (target != GL_TEXTURE_RECTANGLE_ARB)
738 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
739 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
740 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
743 if (target == GL_TEXTURE_CUBE_MAP_ARB)
745 /* Cubemaps are always set to clamp, regardless of the sampler state. */
746 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
747 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
748 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
751 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
753 /* Conditinal non power of two textures use a different clamping
754 * default. If we're using the GL_WINE_normalized_texrect partial
755 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
756 * has the address mode set to repeat - something that prevents us
757 * from hitting the accelerated codepath. Thus manually set the GL
758 * state. The same applies to filtering. Even if the texture has only
759 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
760 * fallback on macos. */
761 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
762 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
763 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
764 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
765 checkGLcall("glTexParameteri");
766 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
767 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
768 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
769 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
770 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
773 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
775 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
776 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
779 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
781 static const GLenum swizzle_source[] =
783 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
784 GL_ONE, /* CHANNEL_SOURCE_ONE */
785 GL_RED, /* CHANNEL_SOURCE_X */
786 GL_GREEN, /* CHANNEL_SOURCE_Y */
787 GL_BLUE, /* CHANNEL_SOURCE_Z */
788 GL_ALPHA, /* CHANNEL_SOURCE_W */
790 struct
792 GLint x, y, z, w;
794 swizzle;
796 swizzle.x = swizzle_source[fixup.x_source];
797 swizzle.y = swizzle_source[fixup.y_source];
798 swizzle.z = swizzle_source[fixup.z_source];
799 swizzle.w = swizzle_source[fixup.w_source];
800 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
801 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
805 /* Context activation is done by the caller. */
806 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
807 struct wined3d_context *context, BOOL srgb)
809 /* We don't need a specific texture unit, but after binding the texture
810 * the current unit is dirty. Read the unit back instead of switching to
811 * 0, this avoids messing around with the state manager's GL states. The
812 * current texture unit should always be a valid one.
814 * To be more specific, this is tricky because we can implicitly be
815 * called from sampler() in state.c. This means we can't touch anything
816 * other than whatever happens to be the currently active texture, or we
817 * would risk marking already applied sampler states dirty again. */
818 if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
820 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
821 if (active_sampler != WINED3D_UNMAPPED_STAGE)
822 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
824 /* FIXME: Ideally we'd only do this when touching a binding that's used by
825 * a shader. */
826 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
827 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
829 wined3d_texture_bind(texture, context, srgb);
832 /* Context activation is done by the caller (state handler). */
833 /* This function relies on the correct texture being bound and loaded. */
834 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
835 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
837 const struct wined3d_gl_info *gl_info = context->gl_info;
838 GLenum target = texture->target;
839 struct gl_texture *gl_tex;
840 DWORD state;
842 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
844 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
846 state = sampler_desc->address_u;
847 if (state != gl_tex->sampler_desc.address_u)
849 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
850 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
851 gl_tex->sampler_desc.address_u = state;
854 state = sampler_desc->address_v;
855 if (state != gl_tex->sampler_desc.address_v)
857 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
858 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
859 gl_tex->sampler_desc.address_v = state;
862 state = sampler_desc->address_w;
863 if (state != gl_tex->sampler_desc.address_w)
865 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
866 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
867 gl_tex->sampler_desc.address_w = state;
870 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
871 sizeof(gl_tex->sampler_desc.border_color)))
873 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
874 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
875 sizeof(gl_tex->sampler_desc.border_color));
878 state = sampler_desc->mag_filter;
879 if (state != gl_tex->sampler_desc.mag_filter)
881 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
882 gl_tex->sampler_desc.mag_filter = state;
885 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
886 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
888 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
889 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
890 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
891 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
894 state = sampler_desc->max_anisotropy;
895 if (state != gl_tex->sampler_desc.max_anisotropy)
897 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
898 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
899 else
900 WARN("Anisotropic filtering not supported.\n");
901 gl_tex->sampler_desc.max_anisotropy = state;
904 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
905 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
906 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
908 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
909 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
910 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
913 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
915 if (sampler_desc->compare)
916 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
917 else
918 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
919 gl_tex->sampler_desc.compare = sampler_desc->compare;
922 checkGLcall("Texture parameter application");
924 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
926 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
927 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
928 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
932 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
934 ULONG refcount;
936 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
938 if (texture->swapchain)
939 return wined3d_swapchain_incref(texture->swapchain);
941 refcount = InterlockedIncrement(&texture->resource.ref);
942 TRACE("%p increasing refcount to %u.\n", texture, refcount);
944 return refcount;
947 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
949 wined3d_texture_sub_resources_destroyed(texture);
950 resource_cleanup(&texture->resource);
951 wined3d_resource_wait_idle(&texture->resource);
952 wined3d_texture_cleanup(texture);
955 static void wined3d_texture_destroy_object(void *object)
957 wined3d_texture_cleanup(object);
958 HeapFree(GetProcessHeap(), 0, object);
961 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
963 ULONG refcount;
965 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
967 if (texture->swapchain)
968 return wined3d_swapchain_decref(texture->swapchain);
970 refcount = InterlockedDecrement(&texture->resource.ref);
971 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
973 if (!refcount)
975 /* Wait for the texture to become idle if it's using user memory,
976 * since the application is allowed to free that memory once the
977 * texture is destroyed. Note that this implies that
978 * wined3d_texture_destroy_object() can't access that memory either. */
979 if (texture->user_memory)
980 wined3d_resource_wait_idle(&texture->resource);
981 wined3d_texture_sub_resources_destroyed(texture);
982 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
983 resource_cleanup(&texture->resource);
984 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
987 return refcount;
990 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
992 TRACE("texture %p.\n", texture);
994 return &texture->resource;
997 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
999 return c1->color_space_low_value == c2->color_space_low_value
1000 && c1->color_space_high_value == c2->color_space_high_value;
1003 /* Context activation is done by the caller */
1004 void wined3d_texture_load(struct wined3d_texture *texture,
1005 struct wined3d_context *context, BOOL srgb)
1007 UINT sub_count = texture->level_count * texture->layer_count;
1008 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1009 DWORD flag;
1010 UINT i;
1012 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1014 if (!needs_separate_srgb_gl_texture(context, texture))
1015 srgb = FALSE;
1017 if (srgb)
1018 flag = WINED3D_TEXTURE_SRGB_VALID;
1019 else
1020 flag = WINED3D_TEXTURE_RGB_VALID;
1022 if (!d3d_info->shader_color_key
1023 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1024 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1025 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1026 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1028 unsigned int sub_count = texture->level_count * texture->layer_count;
1029 unsigned int i;
1031 TRACE("Reloading because of color key value change.\n");
1032 for (i = 0; i < sub_count; i++)
1034 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1035 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1036 else
1037 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1040 texture->async.gl_color_key = texture->async.src_blt_color_key;
1043 if (texture->flags & flag)
1045 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1046 return;
1049 /* Reload the surfaces if the texture is marked dirty. */
1050 for (i = 0; i < sub_count; ++i)
1052 if (!wined3d_texture_load_location(texture, i, context,
1053 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1054 ERR("Failed to load location (srgb %#x).\n", srgb);
1056 texture->flags |= flag;
1059 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1061 TRACE("texture %p.\n", texture);
1063 return texture->resource.parent;
1066 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1067 unsigned int level, const struct wined3d_box *box)
1069 const struct wined3d_format *format = texture->resource.format;
1070 unsigned int width_mask, height_mask, width, height, depth;
1072 width = wined3d_texture_get_level_width(texture, level);
1073 height = wined3d_texture_get_level_height(texture, level);
1074 depth = wined3d_texture_get_level_depth(texture, level);
1076 if (box->left >= box->right || box->right > width
1077 || box->top >= box->bottom || box->bottom > height
1078 || box->front >= box->back || box->back > depth)
1080 WARN("Box %s is invalid.\n", debug_box(box));
1081 return WINEDDERR_INVALIDRECT;
1084 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1086 /* This assumes power of two block sizes, but NPOT block sizes would
1087 * be silly anyway.
1089 * This also assumes that the format's block depth is 1. */
1090 width_mask = format->block_width - 1;
1091 height_mask = format->block_height - 1;
1093 if ((box->left & width_mask) || (box->top & height_mask)
1094 || (box->right & width_mask && box->right != width)
1095 || (box->bottom & height_mask && box->bottom != height))
1097 WARN("Box %s is misaligned for %ux%u blocks.\n",
1098 debug_box(box), format->block_width, format->block_height);
1099 return WINED3DERR_INVALIDCALL;
1103 return WINED3D_OK;
1106 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1107 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1109 const struct wined3d_resource *resource = &texture->resource;
1110 unsigned int width = wined3d_texture_get_level_width(texture, level);
1111 unsigned int height = wined3d_texture_get_level_height(texture, level);
1113 if (texture->row_pitch)
1115 *row_pitch = texture->row_pitch;
1116 *slice_pitch = texture->slice_pitch;
1117 return;
1120 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1121 width, height, row_pitch, slice_pitch);
1124 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1126 DWORD old = texture->lod;
1128 TRACE("texture %p, lod %u.\n", texture, lod);
1130 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1131 * textures. The call always returns 0, and GetLOD always returns 0. */
1132 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1134 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1135 return 0;
1138 if (lod >= texture->level_count)
1139 lod = texture->level_count - 1;
1141 if (texture->lod != lod)
1143 wined3d_resource_wait_idle(&texture->resource);
1144 texture->lod = lod;
1146 texture->texture_rgb.base_level = ~0u;
1147 texture->texture_srgb.base_level = ~0u;
1148 if (texture->resource.bind_count)
1149 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1152 return old;
1155 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1157 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1159 return texture->lod;
1162 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1164 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1166 return texture->level_count;
1169 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1170 enum wined3d_texture_filter_type filter_type)
1172 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1174 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1176 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1177 return WINED3DERR_INVALIDCALL;
1180 texture->filter_type = filter_type;
1182 return WINED3D_OK;
1185 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1187 TRACE("texture %p.\n", texture);
1189 return texture->filter_type;
1192 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1193 DWORD flags, const struct wined3d_color_key *color_key)
1195 struct wined3d_device *device = texture->resource.device;
1196 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1197 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1199 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1201 if (flags & ~all_flags)
1203 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1204 return WINED3DERR_INVALIDCALL;
1207 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1209 return WINED3D_OK;
1212 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1213 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1214 UINT multisample_quality, void *mem, UINT pitch)
1216 struct wined3d_device *device = texture->resource.device;
1217 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1218 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1219 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1220 struct wined3d_texture_sub_resource *sub_resource;
1221 struct wined3d_surface *surface;
1222 DWORD valid_location = 0;
1223 BOOL create_dib = FALSE;
1225 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1226 "mem %p, pitch %u.\n",
1227 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1229 if (!resource_size)
1230 return WINED3DERR_INVALIDCALL;
1232 if (texture->level_count * texture->layer_count > 1)
1234 WARN("Texture has multiple sub-resources, not supported.\n");
1235 return WINED3DERR_INVALIDCALL;
1238 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1240 WARN("Not supported on 3D textures.\n");
1241 return WINED3DERR_INVALIDCALL;
1244 if (texture->resource.map_count)
1246 WARN("Texture is mapped.\n");
1247 return WINED3DERR_INVALIDCALL;
1250 /* We have no way of supporting a pitch that is not a multiple of the pixel
1251 * byte width short of uploading the texture row-by-row.
1252 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1253 * for user-memory textures (it always expects packed data) while DirectDraw
1254 * requires a 4-byte aligned pitch and doesn't support texture formats
1255 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1256 * This check is here to verify that the assumption holds. */
1257 if (pitch % texture->resource.format->byte_count)
1259 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1260 return WINED3DERR_INVALIDCALL;
1263 if (device->d3d_initialized)
1264 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1265 wined3d_resource_wait_idle(&texture->resource);
1267 sub_resource = &texture->sub_resources[0];
1268 surface = sub_resource->u.surface;
1269 if (surface->dc)
1271 wined3d_surface_destroy_dc(surface);
1272 create_dib = TRUE;
1275 wined3d_resource_free_sysmem(&texture->resource);
1277 if ((texture->row_pitch = pitch))
1278 texture->slice_pitch = height * pitch;
1279 else
1280 /* User memory surfaces don't have the regular surface alignment. */
1281 wined3d_format_calculate_pitch(format, 1, width, height,
1282 &texture->row_pitch, &texture->slice_pitch);
1284 texture->resource.format = format;
1285 texture->resource.multisample_type = multisample_type;
1286 texture->resource.multisample_quality = multisample_quality;
1287 texture->resource.width = width;
1288 texture->resource.height = height;
1289 texture->resource.size = texture->slice_pitch;
1290 sub_resource->size = texture->slice_pitch;
1291 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1293 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1294 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1296 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1297 texture->pow2_width = texture->pow2_height = 1;
1298 while (texture->pow2_width < width)
1299 texture->pow2_width <<= 1;
1300 while (texture->pow2_height < height)
1301 texture->pow2_height <<= 1;
1303 else
1305 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1306 texture->pow2_width = width;
1307 texture->pow2_height = height;
1310 if ((texture->user_memory = mem))
1312 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1313 valid_location = WINED3D_LOCATION_USER_MEMORY;
1315 else
1317 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1318 valid_location = WINED3D_LOCATION_SYSMEM;
1321 /* The format might be changed to a format that needs conversion.
1322 * If the surface didn't use PBOs previously but could now, don't
1323 * change it - whatever made us not use PBOs might come back, e.g.
1324 * color keys. */
1325 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1326 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1328 wined3d_texture_validate_location(texture, 0, valid_location);
1329 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1331 if (create_dib)
1332 wined3d_surface_create_dc(surface);
1334 return WINED3D_OK;
1337 /* Context activation is done by the caller. */
1338 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1339 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1341 struct wined3d_texture_sub_resource *sub_resource;
1343 sub_resource = &texture->sub_resources[sub_resource_idx];
1344 if (sub_resource->buffer_object)
1345 return;
1347 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1348 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1349 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1350 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1351 checkGLcall("Create buffer object");
1353 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1354 sub_resource->buffer_object, texture, sub_resource_idx);
1357 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1359 unsigned int sub_count = texture->level_count * texture->layer_count;
1360 unsigned int i;
1362 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1363 | WINED3D_TEXTURE_CONVERTED);
1364 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1365 for (i = 0; i < sub_count; ++i)
1367 wined3d_texture_invalidate_location(texture, i,
1368 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1372 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1374 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1375 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1377 if (!d3d_info->shader_color_key
1378 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1379 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1381 wined3d_texture_force_reload(texture);
1383 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1384 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1387 if (texture->flags & alloc_flag)
1388 return;
1390 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1391 texture->flags |= alloc_flag;
1394 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1395 const struct wined3d_gl_info *gl_info, BOOL multisample)
1397 const struct wined3d_format *format = texture->resource.format;
1399 if (multisample)
1401 DWORD samples;
1403 if (texture->rb_multisample)
1404 return;
1406 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1407 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1408 * be mapped to GL_NV_framebuffer_multisample_coverage.
1410 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1411 * (EQAA), but it does not have an equivalent OpenGL extension. */
1413 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1414 * levels as the count of advertised multisample types for the texture
1415 * format. */
1416 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1418 unsigned int i, count = 0;
1420 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1422 if (format->multisample_types & 1u << i)
1424 if (texture->resource.multisample_quality == count++)
1425 break;
1428 samples = i + 1;
1430 else
1432 samples = texture->resource.multisample_type;
1435 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1436 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1437 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1438 format->glInternal, texture->resource.width, texture->resource.height);
1439 checkGLcall("glRenderbufferStorageMultisample()");
1440 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1442 else
1444 if (texture->rb_resolved)
1445 return;
1447 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1448 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1449 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1450 texture->resource.width, texture->resource.height);
1451 checkGLcall("glRenderbufferStorage()");
1452 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1456 /* Context activation is done by the caller. Context may be NULL in
1457 * WINED3D_NO3D mode. */
1458 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1459 struct wined3d_context *context, DWORD location)
1461 switch (location)
1463 case WINED3D_LOCATION_SYSMEM:
1464 if (texture->resource.heap_memory)
1465 return TRUE;
1467 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1469 ERR("Failed to allocate system memory.\n");
1470 return FALSE;
1472 return TRUE;
1474 case WINED3D_LOCATION_USER_MEMORY:
1475 if (!texture->user_memory)
1476 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1477 return TRUE;
1479 case WINED3D_LOCATION_BUFFER:
1480 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1481 return TRUE;
1483 case WINED3D_LOCATION_TEXTURE_RGB:
1484 wined3d_texture_prepare_texture(texture, context, FALSE);
1485 return TRUE;
1487 case WINED3D_LOCATION_TEXTURE_SRGB:
1488 wined3d_texture_prepare_texture(texture, context, TRUE);
1489 return TRUE;
1491 case WINED3D_LOCATION_DRAWABLE:
1492 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1493 ERR("Texture %p does not have a drawable.\n", texture);
1494 return TRUE;
1496 case WINED3D_LOCATION_RB_MULTISAMPLE:
1497 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1498 return TRUE;
1500 case WINED3D_LOCATION_RB_RESOLVED:
1501 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1502 return TRUE;
1504 default:
1505 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1506 return FALSE;
1510 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1512 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1513 FIXME("texture %p stub!\n", texture);
1516 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1517 unsigned int sub_resource_idx)
1519 UINT sub_count = texture->level_count * texture->layer_count;
1521 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1523 if (sub_resource_idx >= sub_count)
1525 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1526 return NULL;
1529 return &texture->sub_resources[sub_resource_idx];
1532 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1533 UINT layer, const struct wined3d_box *dirty_region)
1535 struct wined3d_context *context;
1536 unsigned int sub_resource_idx;
1538 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1540 if (layer >= texture->layer_count)
1542 WARN("Invalid layer %u specified.\n", layer);
1543 return WINED3DERR_INVALIDCALL;
1545 sub_resource_idx = layer * texture->level_count;
1547 if (dirty_region)
1548 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1550 context = context_acquire(texture->resource.device, NULL, 0);
1551 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1553 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1554 context_release(context);
1555 return E_OUTOFMEMORY;
1557 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1558 context_release(context);
1560 return WINED3D_OK;
1563 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1564 const struct wined3d_context *context, const struct wined3d_box *box,
1565 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1567 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1568 context, box, data, row_pitch, slice_pitch);
1571 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1572 const struct wined3d_context *context, const struct wined3d_box *box,
1573 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1575 unsigned int texture_level;
1576 POINT dst_point;
1577 RECT src_rect;
1579 src_rect.left = 0;
1580 src_rect.top = 0;
1581 if (box)
1583 dst_point.x = box->left;
1584 dst_point.y = box->top;
1585 src_rect.right = box->right - box->left;
1586 src_rect.bottom = box->bottom - box->top;
1588 else
1590 dst_point.x = dst_point.y = 0;
1591 texture_level = sub_resource_idx % texture->level_count;
1592 src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
1593 src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
1596 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1597 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1600 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1601 struct wined3d_context *context, DWORD location)
1603 return surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location);
1606 /* Context activation is done by the caller. */
1607 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1609 const struct wined3d_format *format = texture->resource.format;
1610 const struct wined3d_gl_info *gl_info = context->gl_info;
1611 const struct wined3d_color_key_conversion *conversion;
1612 GLenum internal;
1614 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1616 if (format->convert)
1618 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1620 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1622 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1623 format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
1624 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1627 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1629 if (srgb)
1630 internal = format->glGammaInternal;
1631 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1632 && wined3d_resource_is_offscreen(&texture->resource))
1633 internal = format->rtInternal;
1634 else
1635 internal = format->glInternal;
1637 if (!internal)
1638 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1640 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1642 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1643 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1644 else
1645 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1648 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1650 unsigned int sub_count = texture->level_count * texture->layer_count;
1651 struct wined3d_device *device = texture->resource.device;
1652 struct wined3d_texture_sub_resource *sub_resource;
1653 struct wined3d_renderbuffer_entry *entry, *entry2;
1654 const struct wined3d_gl_info *gl_info = NULL;
1655 struct wined3d_context *context = NULL;
1656 struct wined3d_surface *overlay, *cur;
1657 struct wined3d_surface *surface;
1658 unsigned int i;
1660 for (i = 0; i < sub_count; ++i)
1662 sub_resource = &texture->sub_resources[i];
1663 if (!(surface = sub_resource->u.surface))
1664 continue;
1666 TRACE("surface %p.\n", surface);
1668 if (!context && !list_empty(&surface->renderbuffers))
1670 context = context_acquire(device, NULL, 0);
1671 gl_info = context->gl_info;
1674 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1676 TRACE("Deleting renderbuffer %u.\n", entry->id);
1677 context_gl_resource_released(device, entry->id, TRUE);
1678 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1679 HeapFree(GetProcessHeap(), 0, entry);
1682 if (surface->dc)
1683 wined3d_surface_destroy_dc(surface);
1685 if (surface->overlay_dest)
1686 list_remove(&surface->overlay_entry);
1688 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1690 list_remove(&overlay->overlay_entry);
1691 overlay->overlay_dest = NULL;
1694 if (context)
1695 context_release(context);
1696 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1699 static const struct wined3d_texture_ops texture2d_ops =
1701 texture2d_upload_data,
1702 texture2d_load_location,
1703 texture2d_prepare_texture,
1704 texture2d_cleanup_sub_resources,
1707 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1709 return texture_from_resource(resource);
1712 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1714 return wined3d_texture_incref(texture_from_resource(resource));
1717 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1719 return wined3d_texture_decref(texture_from_resource(resource));
1722 static void texture_resource_preload(struct wined3d_resource *resource)
1724 struct wined3d_texture *texture = texture_from_resource(resource);
1725 struct wined3d_context *context;
1727 context = context_acquire(resource->device, NULL, 0);
1728 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1729 context_release(context);
1732 static void wined3d_texture_unload(struct wined3d_resource *resource)
1734 struct wined3d_texture *texture = texture_from_resource(resource);
1735 UINT sub_count = texture->level_count * texture->layer_count;
1736 struct wined3d_device *device = resource->device;
1737 const struct wined3d_gl_info *gl_info;
1738 struct wined3d_context *context;
1739 UINT i;
1741 TRACE("texture %p.\n", texture);
1743 context = context_acquire(device, NULL, 0);
1744 gl_info = context->gl_info;
1746 for (i = 0; i < sub_count; ++i)
1748 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1750 if (resource->pool != WINED3D_POOL_DEFAULT
1751 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1753 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1755 else
1757 /* We should only get here on device reset/teardown for implicit
1758 * resources. */
1759 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1760 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1761 resource, i, debug_d3dpool(resource->pool));
1762 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1763 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1766 if (sub_resource->buffer_object)
1767 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1769 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1771 struct wined3d_surface *surface = sub_resource->u.surface;
1772 struct wined3d_renderbuffer_entry *entry, *entry2;
1774 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1776 context_gl_resource_released(device, entry->id, TRUE);
1777 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1778 list_remove(&entry->entry);
1779 HeapFree(GetProcessHeap(), 0, entry);
1781 list_init(&surface->renderbuffers);
1782 surface->current_renderbuffer = NULL;
1786 context_release(context);
1788 wined3d_texture_force_reload(texture);
1789 wined3d_texture_unload_gl_texture(texture);
1792 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1793 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1795 const struct wined3d_format *format = resource->format;
1796 struct wined3d_texture_sub_resource *sub_resource;
1797 struct wined3d_device *device = resource->device;
1798 unsigned int fmt_flags = resource->format_flags;
1799 const struct wined3d_gl_info *gl_info = NULL;
1800 struct wined3d_context *context = NULL;
1801 struct wined3d_texture *texture;
1802 struct wined3d_bo_address data;
1803 unsigned int texture_level;
1804 BYTE *base_memory;
1805 BOOL ret;
1807 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1808 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1810 texture = texture_from_resource(resource);
1811 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1812 return E_INVALIDARG;
1814 texture_level = sub_resource_idx % texture->level_count;
1815 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
1817 WARN("Map box is invalid.\n");
1818 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && resource->pool == WINED3D_POOL_DEFAULT)
1819 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1820 return WINED3DERR_INVALIDCALL;
1823 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1825 WARN("Trying to map unmappable texture.\n");
1826 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1827 return WINED3DERR_INVALIDCALL;
1830 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1832 WARN("DC is in use.\n");
1833 return WINED3DERR_INVALIDCALL;
1836 if (sub_resource->map_count)
1838 WARN("Sub-resource is already mapped.\n");
1839 return WINED3DERR_INVALIDCALL;
1842 if (device->d3d_initialized)
1844 context = context_acquire(device, NULL, 0);
1845 gl_info = context->gl_info;
1848 if (flags & WINED3D_MAP_DISCARD)
1850 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1851 wined3d_debug_location(resource->map_binding));
1852 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
1853 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
1855 else
1857 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1858 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1859 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
1862 if (!ret)
1864 ERR("Failed to prepare location.\n");
1865 context_release(context);
1866 return E_OUTOFMEMORY;
1869 if (!(flags & WINED3D_MAP_READONLY)
1870 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1871 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
1873 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
1874 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1875 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1876 TRACE("Base memory pointer %p.\n", base_memory);
1878 if (context)
1879 context_release(context);
1881 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1883 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1884 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1886 else
1888 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1891 if (!box)
1893 map_desc->data = base_memory;
1895 else
1897 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1899 /* Compressed textures are block based, so calculate the offset of
1900 * the block that contains the top-left pixel of the mapped box. */
1901 map_desc->data = base_memory
1902 + (box->front * map_desc->slice_pitch)
1903 + ((box->top / format->block_height) * map_desc->row_pitch)
1904 + ((box->left / format->block_width) * format->block_byte_count);
1906 else
1908 map_desc->data = base_memory
1909 + (box->front * map_desc->slice_pitch)
1910 + (box->top * map_desc->row_pitch)
1911 + (box->left * format->byte_count);
1915 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1917 RECT *r = &texture->swapchain->front_buffer_update;
1919 if (!box)
1920 SetRect(r, 0, 0, resource->width, resource->height);
1921 else
1922 SetRect(r, box->left, box->top, box->right, box->bottom);
1923 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1926 ++resource->map_count;
1927 ++sub_resource->map_count;
1929 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1930 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1932 return WINED3D_OK;
1935 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1937 struct wined3d_texture_sub_resource *sub_resource;
1938 struct wined3d_device *device = resource->device;
1939 const struct wined3d_gl_info *gl_info = NULL;
1940 struct wined3d_context *context = NULL;
1941 struct wined3d_texture *texture;
1942 struct wined3d_bo_address data;
1944 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1946 texture = texture_from_resource(resource);
1947 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1948 return E_INVALIDARG;
1950 if (!sub_resource->map_count)
1952 WARN("Trying to unmap unmapped sub-resource.\n");
1953 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1954 return WINED3D_OK;
1955 return WINEDDERR_NOTLOCKED;
1958 if (device->d3d_initialized)
1960 context = context_acquire(device, NULL, 0);
1961 gl_info = context->gl_info;
1964 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1965 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1967 if (context)
1968 context_release(context);
1970 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1972 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1973 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1976 --sub_resource->map_count;
1977 if (!--resource->map_count && texture->update_map_binding)
1978 wined3d_texture_update_map_binding(texture);
1980 return WINED3D_OK;
1983 static const struct wined3d_resource_ops texture_resource_ops =
1985 texture_resource_incref,
1986 texture_resource_decref,
1987 texture_resource_preload,
1988 wined3d_texture_unload,
1989 texture_resource_sub_resource_map,
1990 texture_resource_sub_resource_unmap,
1993 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1994 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1995 void *parent, const struct wined3d_parent_ops *parent_ops)
1997 struct wined3d_device_parent *device_parent = device->device_parent;
1998 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1999 struct wined3d_surface *surfaces;
2000 UINT pow2_width, pow2_height;
2001 unsigned int i, j;
2002 HRESULT hr;
2004 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2005 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2007 WARN("OpenGL implementation does not support array textures.\n");
2008 return WINED3DERR_INVALIDCALL;
2011 /* TODO: It should only be possible to create textures for formats
2012 * that are reported as supported. */
2013 if (WINED3DFMT_UNKNOWN >= desc->format)
2015 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2016 return WINED3DERR_INVALIDCALL;
2019 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
2020 FIXME("Trying to create a managed texture with dynamic usage.\n");
2021 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2022 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2023 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
2024 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
2025 FIXME("Trying to create a render target that isn't in the default pool.\n");
2027 pow2_width = desc->width;
2028 pow2_height = desc->height;
2029 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
2030 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2032 /* level_count == 0 returns an error as well. */
2033 if (level_count != 1 || layer_count != 1)
2035 if (desc->pool != WINED3D_POOL_SCRATCH)
2037 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
2038 return WINED3DERR_INVALIDCALL;
2041 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2043 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2045 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2047 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2049 /* TODO: Add support for non-power-of-two compressed textures. */
2050 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2051 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2053 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2054 desc->width, desc->height);
2055 return WINED3DERR_NOTAVAILABLE;
2058 /* Find the nearest pow2 match. */
2059 pow2_width = pow2_height = 1;
2060 while (pow2_width < desc->width)
2061 pow2_width <<= 1;
2062 while (pow2_height < desc->height)
2063 pow2_height <<= 1;
2064 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2067 texture->pow2_width = pow2_width;
2068 texture->pow2_height = pow2_height;
2070 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2071 && (desc->usage & WINED3DUSAGE_TEXTURE))
2073 /* One of four options:
2074 * 1: Do the same as we do with NPOT and scale the texture. (Any
2075 * texture ops would require the texture to be scaled which is
2076 * potentially slow.)
2077 * 2: Set the texture to the maximum size (bad idea).
2078 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2079 * 4: Create the surface, but allow it to be used only for DirectDraw
2080 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2081 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2082 * the render target. */
2083 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
2085 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2086 return WINED3DERR_NOTAVAILABLE;
2089 /* We should never use this surface in combination with OpenGL. */
2090 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2093 /* Calculate levels for mip mapping. */
2094 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2096 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2098 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
2099 return WINED3DERR_INVALIDCALL;
2102 if (level_count != 1)
2104 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
2105 return WINED3DERR_INVALIDCALL;
2109 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
2110 flags, device, parent, parent_ops, &texture_resource_ops)))
2112 WARN("Failed to initialize texture, returning %#x.\n", hr);
2113 return hr;
2116 /* Precalculated scaling for 'faked' non power of two texture coords. */
2117 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2119 texture->pow2_matrix[0] = (float)desc->width;
2120 texture->pow2_matrix[5] = (float)desc->height;
2121 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2122 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2124 else
2126 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2128 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2129 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2130 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2132 else
2134 texture->pow2_matrix[0] = 1.0f;
2135 texture->pow2_matrix[5] = 1.0f;
2137 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2138 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2139 else if (layer_count > 1)
2140 texture->target = GL_TEXTURE_2D_ARRAY;
2141 else
2142 texture->target = GL_TEXTURE_2D;
2144 texture->pow2_matrix[10] = 1.0f;
2145 texture->pow2_matrix[15] = 1.0f;
2146 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2148 if (wined3d_texture_use_pbo(texture, gl_info))
2149 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2151 if (level_count > ~(SIZE_T)0 / layer_count
2152 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2154 wined3d_texture_cleanup_sync(texture);
2155 return E_OUTOFMEMORY;
2158 /* Generate all the surfaces. */
2159 for (i = 0; i < texture->level_count; ++i)
2161 for (j = 0; j < texture->layer_count; ++j)
2163 static const GLenum cube_targets[6] =
2165 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2166 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2167 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2168 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2169 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2170 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2172 struct wined3d_texture_sub_resource *sub_resource;
2173 unsigned int idx = j * texture->level_count + i;
2174 struct wined3d_surface *surface;
2176 surface = &surfaces[idx];
2177 surface->container = texture;
2178 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2179 surface->texture_level = i;
2180 surface->texture_layer = j;
2181 list_init(&surface->renderbuffers);
2182 list_init(&surface->overlays);
2184 sub_resource = &texture->sub_resources[idx];
2185 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2186 sub_resource->u.surface = surface;
2187 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2189 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2190 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2193 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2194 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2196 WARN("Failed to create surface parent, hr %#x.\n", hr);
2197 sub_resource->parent = NULL;
2198 wined3d_texture_cleanup_sync(texture);
2199 return hr;
2202 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2204 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2206 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2207 && FAILED(hr = wined3d_surface_create_dc(surface)))
2209 wined3d_texture_cleanup_sync(texture);
2210 return hr;
2215 return WINED3D_OK;
2218 /* This call just uploads data, the caller is responsible for binding the
2219 * correct texture. */
2220 /* Context activation is done by the caller. */
2221 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2222 const struct wined3d_context *context, const struct wined3d_box *box,
2223 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2225 const struct wined3d_format *format = texture->resource.format;
2226 unsigned int level = sub_resource_idx % texture->level_count;
2227 const struct wined3d_gl_info *gl_info = context->gl_info;
2228 unsigned int x, y, z, update_w, update_h, update_d;
2229 unsigned int dst_row_pitch, dst_slice_pitch;
2230 unsigned int width, height, depth;
2231 const void *mem = data->addr;
2232 void *converted_mem = NULL;
2234 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2235 texture, sub_resource_idx, context, debug_box(box),
2236 data->buffer_object, data->addr, row_pitch, slice_pitch);
2238 width = wined3d_texture_get_level_width(texture, level);
2239 height = wined3d_texture_get_level_height(texture, level);
2240 depth = wined3d_texture_get_level_depth(texture, level);
2242 if (!box)
2244 x = y = z = 0;
2245 update_w = width;
2246 update_h = height;
2247 update_d = depth;
2249 else
2251 x = box->left;
2252 y = box->top;
2253 z = box->front;
2254 update_w = box->right - box->left;
2255 update_h = box->bottom - box->top;
2256 update_d = box->back - box->front;
2259 if (format->convert)
2261 if (data->buffer_object)
2262 ERR("Loading a converted texture from a PBO.\n");
2263 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2264 ERR("Converting a block-based format.\n");
2266 dst_row_pitch = update_w * format->conv_byte_count;
2267 dst_slice_pitch = dst_row_pitch * update_h;
2269 converted_mem = wined3d_calloc(update_d, dst_slice_pitch);
2270 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2271 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2272 mem = converted_mem;
2274 else
2276 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2277 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2278 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2281 if (data->buffer_object)
2283 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2284 checkGLcall("glBindBuffer");
2287 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2288 update_w, update_h, update_d, format->glFormat, format->glType, mem));
2289 checkGLcall("glTexSubImage3D");
2291 if (data->buffer_object)
2293 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2294 checkGLcall("glBindBuffer");
2297 HeapFree(GetProcessHeap(), 0, converted_mem);
2300 /* Context activation is done by the caller. */
2301 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2302 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2304 const struct wined3d_format *format = texture->resource.format;
2305 const struct wined3d_gl_info *gl_info = context->gl_info;
2307 if (format->convert)
2309 FIXME("Attempting to download a converted volume, format %s.\n",
2310 debug_d3dformat(format->id));
2311 return;
2314 if (data->buffer_object)
2316 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2317 checkGLcall("glBindBuffer");
2320 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2321 format->glFormat, format->glType, data->addr);
2322 checkGLcall("glGetTexImage");
2324 if (data->buffer_object)
2326 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2327 checkGLcall("glBindBuffer");
2332 /* Context activation is done by the caller. */
2333 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2334 struct wined3d_context *context, BOOL dest_is_srgb)
2336 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2337 unsigned int row_pitch, slice_pitch;
2338 struct wined3d_bo_address data;
2340 /* Optimisations are possible, but the effort should be put into either
2341 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2342 * picked the wrong copy for the original upload and fixing that.
2344 * Also keep in mind that we want to avoid using resource.heap_memory
2345 * for DEFAULT pool surfaces. */
2346 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2347 data.buffer_object = 0;
2348 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2349 return;
2351 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2352 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2353 texture3d_download_data(texture, sub_resource_idx, context, &data);
2354 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2355 texture3d_upload_data(texture, sub_resource_idx, context,
2356 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2358 HeapFree(GetProcessHeap(), 0, data.addr);
2361 /* Context activation is done by the caller. */
2362 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2363 struct wined3d_context *context, DWORD location)
2365 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2366 unsigned int row_pitch, slice_pitch;
2368 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2369 return FALSE;
2371 switch (location)
2373 case WINED3D_LOCATION_TEXTURE_RGB:
2374 case WINED3D_LOCATION_TEXTURE_SRGB:
2375 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2377 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2378 data.addr += sub_resource->offset;
2379 wined3d_texture_bind_and_dirtify(texture, context,
2380 location == WINED3D_LOCATION_TEXTURE_SRGB);
2381 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2382 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2384 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2386 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2387 wined3d_texture_bind_and_dirtify(texture, context,
2388 location == WINED3D_LOCATION_TEXTURE_SRGB);
2389 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2390 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2392 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2394 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2396 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2398 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2400 else
2402 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2403 return FALSE;
2405 break;
2407 case WINED3D_LOCATION_SYSMEM:
2408 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2410 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2412 data.addr += sub_resource->offset;
2413 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2414 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2415 else
2416 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2418 texture3d_download_data(texture, sub_resource_idx, context, &data);
2419 ++texture->download_count;
2421 else
2423 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2424 wined3d_debug_location(sub_resource->locations));
2425 return FALSE;
2427 break;
2429 case WINED3D_LOCATION_BUFFER:
2430 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2432 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2434 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2435 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2436 else
2437 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2439 texture3d_download_data(texture, sub_resource_idx, context, &data);
2441 else
2443 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2444 wined3d_debug_location(sub_resource->locations));
2445 return FALSE;
2447 break;
2449 default:
2450 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2451 wined3d_debug_location(sub_resource->locations));
2452 return FALSE;
2455 return TRUE;
2458 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2460 const struct wined3d_format *format = texture->resource.format;
2461 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2462 unsigned int sub_count = texture->level_count * texture->layer_count;
2463 const struct wined3d_gl_info *gl_info = context->gl_info;
2464 unsigned int i;
2466 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2468 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2470 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2471 wined3d_texture_get_level_width(texture, 0),
2472 wined3d_texture_get_level_height(texture, 0),
2473 wined3d_texture_get_level_depth(texture, 0)));
2474 checkGLcall("glTexStorage3D");
2476 else
2478 for (i = 0; i < sub_count; ++i)
2480 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2481 wined3d_texture_get_level_width(texture, i),
2482 wined3d_texture_get_level_height(texture, i),
2483 wined3d_texture_get_level_depth(texture, i),
2484 0, format->glFormat, format->glType, NULL));
2485 checkGLcall("glTexImage3D");
2490 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2494 static const struct wined3d_texture_ops texture3d_ops =
2496 texture3d_upload_data,
2497 texture3d_load_location,
2498 texture3d_prepare_texture,
2499 texture3d_cleanup_sub_resources,
2502 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2503 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2504 const struct wined3d_parent_ops *parent_ops)
2506 struct wined3d_device_parent *device_parent = device->device_parent;
2507 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2508 unsigned int i;
2509 HRESULT hr;
2511 if (layer_count != 1)
2513 ERR("Invalid layer count for volume texture.\n");
2514 return E_INVALIDARG;
2517 /* TODO: It should only be possible to create textures for formats
2518 * that are reported as supported. */
2519 if (WINED3DFMT_UNKNOWN >= desc->format)
2521 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2522 return WINED3DERR_INVALIDCALL;
2525 if (!gl_info->supported[EXT_TEXTURE3D])
2527 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2528 return WINED3DERR_INVALIDCALL;
2531 /* Calculate levels for mip mapping. */
2532 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2534 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2536 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2537 return WINED3DERR_INVALIDCALL;
2540 if (level_count != 1)
2542 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2543 return WINED3DERR_INVALIDCALL;
2547 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2548 || desc->pool == WINED3D_POOL_SCRATCH))
2550 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2551 return WINED3DERR_INVALIDCALL;
2554 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2556 UINT pow2_w, pow2_h, pow2_d;
2557 pow2_w = 1;
2558 while (pow2_w < desc->width)
2559 pow2_w <<= 1;
2560 pow2_h = 1;
2561 while (pow2_h < desc->height)
2562 pow2_h <<= 1;
2563 pow2_d = 1;
2564 while (pow2_d < desc->depth)
2565 pow2_d <<= 1;
2567 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2569 if (desc->pool == WINED3D_POOL_SCRATCH)
2571 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2573 else
2575 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2576 desc->width, desc->height, desc->depth);
2577 return WINED3DERR_INVALIDCALL;
2582 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2583 0, device, parent, parent_ops, &texture_resource_ops)))
2585 WARN("Failed to initialize texture, returning %#x.\n", hr);
2586 return hr;
2589 texture->pow2_matrix[0] = 1.0f;
2590 texture->pow2_matrix[5] = 1.0f;
2591 texture->pow2_matrix[10] = 1.0f;
2592 texture->pow2_matrix[15] = 1.0f;
2593 texture->target = GL_TEXTURE_3D;
2595 if (wined3d_texture_use_pbo(texture, gl_info))
2597 wined3d_resource_free_sysmem(&texture->resource);
2598 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2601 /* Generate all the surfaces. */
2602 for (i = 0; i < texture->level_count; ++i)
2604 struct wined3d_texture_sub_resource *sub_resource;
2606 sub_resource = &texture->sub_resources[i];
2607 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2609 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2610 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2612 WARN("Failed to create volume parent, hr %#x.\n", hr);
2613 sub_resource->parent = NULL;
2614 wined3d_texture_cleanup_sync(texture);
2615 return hr;
2618 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2620 TRACE("Created volume level %u.\n", i);
2623 return WINED3D_OK;
2626 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2627 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2628 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2630 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
2631 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
2632 unsigned int dst_format_flags, src_format_flags = 0;
2633 HRESULT hr;
2635 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2636 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2637 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2638 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2640 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
2641 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2642 return WINED3DERR_INVALIDCALL;
2644 dst_format_flags = dst_texture->resource.format_flags;
2645 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
2646 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
2647 return hr;
2649 if (src_texture)
2651 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
2652 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2653 return WINED3DERR_INVALIDCALL;
2655 src_format_flags = src_texture->resource.format_flags;
2656 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
2657 src_sub_resource_idx % src_texture->level_count, &src_box)))
2658 return hr;
2661 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
2662 || (src_texture && src_texture->sub_resources[src_sub_resource_idx].map_count))
2664 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2665 return WINEDDERR_SURFACEBUSY;
2668 if ((dst_format_flags & WINED3DFMT_FLAG_BLOCKS) && (flags & WINED3D_BLT_COLOR_FILL))
2670 WARN("Color fill not supported on block-based formats.\n");
2671 return WINED3DERR_INVALIDCALL;
2674 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2675 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2676 && !(flags & WINED3D_BLT_DEPTH_FILL))
2678 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2679 return WINED3DERR_INVALIDCALL;
2682 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
2683 &dst_box, src_texture ? &src_texture->resource : NULL, src_sub_resource_idx, &src_box, flags, fx, filter);
2685 return WINED3D_OK;
2688 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2689 unsigned int sub_resource_idx, LONG *x, LONG *y)
2691 struct wined3d_surface *surface;
2693 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2695 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2696 || sub_resource_idx >= texture->level_count * texture->layer_count)
2698 WARN("Invalid sub-resource specified.\n");
2699 return WINEDDERR_NOTAOVERLAYSURFACE;
2702 surface = texture->sub_resources[sub_resource_idx].u.surface;
2703 if (!surface->overlay_dest)
2705 TRACE("Overlay not visible.\n");
2706 *x = 0;
2707 *y = 0;
2708 return WINEDDERR_OVERLAYNOTVISIBLE;
2711 *x = surface->overlay_destrect.left;
2712 *y = surface->overlay_destrect.top;
2714 TRACE("Returning position %d, %d.\n", *x, *y);
2716 return WINED3D_OK;
2719 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2720 unsigned int sub_resource_idx, LONG x, LONG y)
2722 struct wined3d_texture_sub_resource *sub_resource;
2723 struct wined3d_surface *surface;
2724 LONG w, h;
2726 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2728 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2729 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2731 WARN("Invalid sub-resource specified.\n");
2732 return WINEDDERR_NOTAOVERLAYSURFACE;
2735 surface = sub_resource->u.surface;
2736 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2737 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2738 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2740 return WINED3D_OK;
2743 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2744 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2745 const RECT *dst_rect, DWORD flags)
2747 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2748 struct wined3d_surface *surface, *dst_surface;
2750 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2751 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2752 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2753 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2755 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2756 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2758 WARN("Invalid sub-resource specified.\n");
2759 return WINEDDERR_NOTAOVERLAYSURFACE;
2762 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2763 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2765 WARN("Invalid destination sub-resource specified.\n");
2766 return WINED3DERR_INVALIDCALL;
2769 surface = sub_resource->u.surface;
2770 if (src_rect)
2771 surface->overlay_srcrect = *src_rect;
2772 else
2773 SetRect(&surface->overlay_srcrect, 0, 0,
2774 wined3d_texture_get_level_width(texture, surface->texture_level),
2775 wined3d_texture_get_level_height(texture, surface->texture_level));
2777 dst_surface = dst_sub_resource->u.surface;
2778 if (dst_rect)
2779 surface->overlay_destrect = *dst_rect;
2780 else
2781 SetRect(&surface->overlay_destrect, 0, 0,
2782 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2783 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2785 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2787 surface->overlay_dest = NULL;
2788 list_remove(&surface->overlay_entry);
2791 if (flags & WINEDDOVER_SHOW)
2793 if (surface->overlay_dest != dst_surface)
2795 surface->overlay_dest = dst_surface;
2796 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2799 else if (flags & WINEDDOVER_HIDE)
2801 /* Tests show that the rectangles are erased on hide. */
2802 SetRectEmpty(&surface->overlay_srcrect);
2803 SetRectEmpty(&surface->overlay_destrect);
2804 surface->overlay_dest = NULL;
2807 return WINED3D_OK;
2810 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2812 unsigned int sub_count = texture->level_count * texture->layer_count;
2814 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2816 if (sub_resource_idx >= sub_count)
2818 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2819 return NULL;
2822 return texture->sub_resources[sub_resource_idx].parent;
2825 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2826 unsigned int sub_resource_idx, void *parent)
2828 unsigned int sub_count = texture->level_count * texture->layer_count;
2830 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2832 if (sub_resource_idx >= sub_count)
2834 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2835 return;
2838 texture->sub_resources[sub_resource_idx].parent = parent;
2841 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2842 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2844 unsigned int sub_count = texture->level_count * texture->layer_count;
2845 const struct wined3d_resource *resource;
2846 unsigned int level_idx;
2848 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2850 if (sub_resource_idx >= sub_count)
2852 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2853 return WINED3DERR_INVALIDCALL;
2856 resource = &texture->resource;
2857 desc->format = resource->format->id;
2858 desc->multisample_type = resource->multisample_type;
2859 desc->multisample_quality = resource->multisample_quality;
2860 desc->usage = resource->usage;
2861 desc->pool = resource->pool;
2863 level_idx = sub_resource_idx % texture->level_count;
2864 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2865 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2866 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2867 desc->size = texture->sub_resources[sub_resource_idx].size;
2869 return WINED3D_OK;
2872 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2873 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2874 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2876 struct wined3d_texture *object;
2877 HRESULT hr;
2879 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2880 "parent %p, parent_ops %p, texture %p.\n",
2881 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2883 if (!layer_count)
2885 WARN("Invalid layer count.\n");
2886 return E_INVALIDARG;
2888 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2890 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2891 layer_count = 6;
2894 if (!level_count)
2896 WARN("Invalid level count.\n");
2897 return WINED3DERR_INVALIDCALL;
2900 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2902 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2903 desc->format, desc->usage);
2905 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2906 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2908 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2909 desc->multisample_quality);
2910 return WINED3DERR_NOTAVAILABLE;
2912 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2913 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2914 || desc->multisample_quality))
2916 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2917 desc->multisample_quality);
2918 return WINED3DERR_NOTAVAILABLE;
2922 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2923 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2924 return E_OUTOFMEMORY;
2926 switch (desc->resource_type)
2928 case WINED3D_RTYPE_TEXTURE_2D:
2929 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2930 break;
2932 case WINED3D_RTYPE_TEXTURE_3D:
2933 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2934 break;
2936 default:
2937 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2938 hr = WINED3DERR_INVALIDCALL;
2939 break;
2942 if (FAILED(hr))
2944 WARN("Failed to initialize texture, returning %#x.\n", hr);
2945 HeapFree(GetProcessHeap(), 0, object);
2946 return hr;
2949 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2950 * in this case. */
2951 if (data)
2953 unsigned int sub_count = level_count * layer_count;
2954 unsigned int i;
2956 for (i = 0; i < sub_count; ++i)
2958 if (!data[i].data)
2960 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2961 wined3d_texture_cleanup_sync(object);
2962 HeapFree(GetProcessHeap(), 0, object);
2963 return E_INVALIDARG;
2967 for (i = 0; i < sub_count; ++i)
2969 wined3d_device_update_sub_resource(device, &object->resource,
2970 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
2974 TRACE("Created texture %p.\n", object);
2975 *texture = object;
2977 return WINED3D_OK;
2980 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2982 struct wined3d_device *device = texture->resource.device;
2983 struct wined3d_texture_sub_resource *sub_resource;
2984 struct wined3d_context *context = NULL;
2985 struct wined3d_surface *surface;
2986 HRESULT hr = WINED3D_OK;
2988 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2990 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
2992 WARN("Texture does not support GetDC\n");
2993 /* Don't touch the DC */
2994 return WINED3DERR_INVALIDCALL;
2997 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2998 return WINED3DERR_INVALIDCALL;
3000 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3002 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3003 return WINED3DERR_INVALIDCALL;
3006 surface = sub_resource->u.surface;
3008 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3009 return WINED3DERR_INVALIDCALL;
3011 if (device->d3d_initialized)
3012 context = context_acquire(device, NULL, 0);
3014 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
3015 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
3017 if (!surface->dc)
3018 hr = wined3d_surface_create_dc(surface);
3019 if (context)
3020 context_release(context);
3021 if (FAILED(hr))
3022 return WINED3DERR_INVALIDCALL;
3024 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3025 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3026 ++texture->resource.map_count;
3027 ++sub_resource->map_count;
3029 *dc = surface->dc;
3030 TRACE("Returning dc %p.\n", *dc);
3032 return hr;
3035 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3037 struct wined3d_device *device = texture->resource.device;
3038 struct wined3d_texture_sub_resource *sub_resource;
3039 struct wined3d_surface *surface;
3041 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3043 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3044 return WINED3DERR_INVALIDCALL;
3046 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3048 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3049 return WINED3DERR_INVALIDCALL;
3052 surface = sub_resource->u.surface;
3054 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3055 return WINED3DERR_INVALIDCALL;
3057 if (surface->dc != dc)
3059 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
3060 return WINED3DERR_INVALIDCALL;
3063 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3064 wined3d_surface_destroy_dc(surface);
3066 --sub_resource->map_count;
3067 if (!--texture->resource.map_count && texture->update_map_binding)
3068 wined3d_texture_update_map_binding(texture);
3069 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3070 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3072 return WINED3D_OK;