wined3d: Merge texture2d_prepare_texture() and texture3d_prepare_texture().
[wine.git] / dlls / wined3d / texture.c
blob124aa316e6c5da60b757b1460ef96fdfb98359c1
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 struct wined3d_texture_idx
35 struct wined3d_texture *texture;
36 unsigned int sub_resource_idx;
39 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
41 return !(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
42 && texture->resource.usage & WINED3DUSAGE_DYNAMIC
43 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
44 && !texture->resource.format->conv_byte_count
45 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
48 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
49 const struct wined3d_gl_info *gl_info)
51 /* We don't expect to create texture views for textures with height-scaled formats.
52 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
53 return gl_info->supported[ARB_TEXTURE_STORAGE]
54 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
57 /* Front buffer coordinates are always full screen coordinates, but our GL
58 * drawable is limited to the window's client area. The sysmem and texture
59 * copies do have the full screen size. Note that GL has a bottom-left
60 * origin, while D3D has a top-left origin. */
61 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
63 unsigned int drawable_height;
64 POINT offset = {0, 0};
65 RECT windowsize;
67 if (!texture->swapchain)
68 return;
70 if (texture == texture->swapchain->front_buffer)
72 ScreenToClient(window, &offset);
73 OffsetRect(rect, offset.x, offset.y);
76 GetClientRect(window, &windowsize);
77 drawable_height = windowsize.bottom - windowsize.top;
79 rect->top = drawable_height - rect->top;
80 rect->bottom = drawable_height - rect->bottom;
83 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
85 const struct wined3d_swapchain *swapchain = texture->swapchain;
87 TRACE("texture %p.\n", texture);
89 if (!swapchain)
91 ERR("Texture %p is not part of a swapchain.\n", texture);
92 return GL_NONE;
95 if (texture == swapchain->front_buffer)
97 TRACE("Returning GL_FRONT.\n");
98 return GL_FRONT;
101 if (texture == swapchain->back_buffers[0])
103 TRACE("Returning GL_BACK.\n");
104 return GL_BACK;
107 FIXME("Higher back buffer, returning GL_BACK.\n");
108 return GL_BACK;
111 static DWORD wined3d_resource_access_from_location(DWORD location)
113 switch (location)
115 case WINED3D_LOCATION_DISCARDED:
116 return 0;
118 case WINED3D_LOCATION_SYSMEM:
119 case WINED3D_LOCATION_USER_MEMORY:
120 return WINED3D_RESOURCE_ACCESS_CPU;
122 case WINED3D_LOCATION_BUFFER:
123 case WINED3D_LOCATION_DRAWABLE:
124 case WINED3D_LOCATION_TEXTURE_RGB:
125 case WINED3D_LOCATION_TEXTURE_SRGB:
126 case WINED3D_LOCATION_RB_MULTISAMPLE:
127 case WINED3D_LOCATION_RB_RESOLVED:
128 return WINED3D_RESOURCE_ACCESS_GPU;
130 default:
131 FIXME("Unhandled location %#x.\n", location);
132 return 0;
136 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
138 struct wined3d_texture_sub_resource *sub_resource;
139 unsigned int i, sub_count;
141 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
142 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
144 TRACE("Not evicting system memory for texture %p.\n", texture);
145 return;
148 TRACE("Evicting system memory for texture %p.\n", texture);
150 sub_count = texture->level_count * texture->layer_count;
151 for (i = 0; i < sub_count; ++i)
153 sub_resource = &texture->sub_resources[i];
154 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
155 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
156 i, texture);
157 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
159 wined3d_resource_free_sysmem(&texture->resource);
162 void wined3d_texture_validate_location(struct wined3d_texture *texture,
163 unsigned int sub_resource_idx, DWORD location)
165 struct wined3d_texture_sub_resource *sub_resource;
166 DWORD previous_locations;
168 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
169 texture, sub_resource_idx, wined3d_debug_location(location));
171 sub_resource = &texture->sub_resources[sub_resource_idx];
172 previous_locations = sub_resource->locations;
173 sub_resource->locations |= location;
174 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
175 && !--texture->sysmem_count)
176 wined3d_texture_evict_sysmem(texture);
178 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
181 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
183 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
186 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
187 unsigned int sub_resource_idx, DWORD location)
189 struct wined3d_texture_sub_resource *sub_resource;
190 DWORD previous_locations;
192 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
193 texture, sub_resource_idx, wined3d_debug_location(location));
195 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
196 wined3d_texture_set_dirty(texture);
198 sub_resource = &texture->sub_resources[sub_resource_idx];
199 previous_locations = sub_resource->locations;
200 sub_resource->locations &= ~location;
201 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
202 ++texture->sysmem_count;
204 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
206 if (!sub_resource->locations)
207 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
208 sub_resource_idx, texture);
211 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
212 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
214 unsigned int size = texture->sub_resources[sub_resource_idx].size;
215 struct wined3d_device *device = texture->resource.device;
216 const struct wined3d_gl_info *gl_info;
217 struct wined3d_bo_address dst, src;
219 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
220 return FALSE;
222 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
223 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
224 texture->sub_resources[sub_resource_idx].locations);
226 if (dst.buffer_object)
228 context = context_acquire(device, NULL, 0);
229 gl_info = context->gl_info;
230 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
231 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
232 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
233 checkGLcall("PBO upload");
234 context_release(context);
235 return TRUE;
238 if (src.buffer_object)
240 context = context_acquire(device, NULL, 0);
241 gl_info = context->gl_info;
242 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
243 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
244 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
245 checkGLcall("PBO download");
246 context_release(context);
247 return TRUE;
250 memcpy(dst.addr, src.addr, size);
251 return TRUE;
254 /* Context activation is done by the caller. Context may be NULL in
255 * WINED3D_NO3D mode. */
256 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
257 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
259 static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
260 | WINED3D_LOCATION_BUFFER;
261 DWORD current = texture->sub_resources[sub_resource_idx].locations;
262 BOOL ret;
264 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
265 texture, sub_resource_idx, context, wined3d_debug_location(location));
267 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
269 if (current & location)
271 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
272 return TRUE;
275 if (WARN_ON(d3d))
277 DWORD required_access = wined3d_resource_access_from_location(location);
278 if ((texture->resource.access & required_access) != required_access)
279 WARN("Operation requires %#x access, but texture only has %#x.\n",
280 required_access, texture->resource.access);
283 if (current & WINED3D_LOCATION_DISCARDED)
285 TRACE("Sub-resource previously discarded, nothing to do.\n");
286 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
287 return FALSE;
288 wined3d_texture_validate_location(texture, sub_resource_idx, location);
289 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
290 return TRUE;
293 if (!current)
295 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
296 sub_resource_idx, texture);
297 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
298 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
301 if ((location & sysmem_locations) && (current & sysmem_locations))
302 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
303 else
304 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
306 if (ret)
307 wined3d_texture_validate_location(texture, sub_resource_idx, location);
309 return ret;
312 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
313 struct wined3d_bo_address *data, DWORD locations)
315 struct wined3d_texture_sub_resource *sub_resource;
317 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
318 texture, sub_resource_idx, data, wined3d_debug_location(locations));
320 sub_resource = &texture->sub_resources[sub_resource_idx];
321 if (locations & WINED3D_LOCATION_BUFFER)
323 data->addr = NULL;
324 data->buffer_object = sub_resource->buffer_object;
325 return;
327 if (locations & WINED3D_LOCATION_USER_MEMORY)
329 data->addr = texture->user_memory;
330 data->buffer_object = 0;
331 return;
333 if (locations & WINED3D_LOCATION_SYSMEM)
335 data->addr = texture->resource.heap_memory;
336 data->addr += sub_resource->offset;
337 data->buffer_object = 0;
338 return;
341 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
342 data->addr = NULL;
343 data->buffer_object = 0;
346 /* Context activation is done by the caller. */
347 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
348 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
350 GLuint *buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
352 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
353 checkGLcall("glDeleteBuffers");
355 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
356 *buffer_object, texture, sub_resource_idx);
358 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
359 *buffer_object = 0;
362 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
364 unsigned int sub_count = texture->level_count * texture->layer_count;
365 const struct wined3d_device *device = texture->resource.device;
366 DWORD map_binding = texture->update_map_binding;
367 struct wined3d_context *context = NULL;
368 unsigned int i;
370 if (device->d3d_initialized)
371 context = context_acquire(device, NULL, 0);
373 for (i = 0; i < sub_count; ++i)
375 if (texture->sub_resources[i].locations == texture->resource.map_binding
376 && !wined3d_texture_load_location(texture, i, context, map_binding))
377 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
378 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
379 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
382 if (context)
383 context_release(context);
385 texture->resource.map_binding = map_binding;
386 texture->update_map_binding = 0;
389 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
391 texture->update_map_binding = map_binding;
392 if (!texture->resource.map_count)
393 wined3d_texture_update_map_binding(texture);
396 /* A GL context is provided by the caller */
397 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
398 struct gl_texture *tex)
400 context_gl_resource_released(device, tex->name, FALSE);
401 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
402 tex->name = 0;
405 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture *texture)
407 const struct wined3d_format *format = texture->resource.format;
409 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
410 * feature through type == MULTISAMPLE_XX and quality != 0. This could
411 * be mapped to GL_NV_framebuffer_multisample_coverage.
413 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
414 * (EQAA), but it does not have an equivalent OpenGL extension. */
416 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
417 * levels as the count of advertised multisample types for the texture
418 * format. */
419 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
421 unsigned int i, count = 0;
423 for (i = 0; i < sizeof(format->multisample_types) * CHAR_BIT; ++i)
425 if (format->multisample_types & 1u << i)
427 if (texture->resource.multisample_quality == count++)
428 break;
431 return i + 1;
434 return texture->resource.multisample_type;
437 /* Context activation is done by the caller. */
438 /* The caller is responsible for binding the correct texture. */
439 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
440 GLenum gl_internal_format, const struct wined3d_format *format,
441 const struct wined3d_gl_info *gl_info)
443 unsigned int level, level_count, layer, layer_count;
444 GLsizei width, height, depth;
445 GLenum target;
447 level_count = texture->level_count;
448 layer_count = texture->target == GL_TEXTURE_2D_ARRAY ? 1 : texture->layer_count;
450 for (layer = 0; layer < layer_count; ++layer)
452 target = wined3d_texture_get_sub_resource_target(texture, layer * level_count);
454 for (level = 0; level < level_count; ++level)
456 width = wined3d_texture_get_level_pow2_width(texture, level);
457 height = wined3d_texture_get_level_pow2_height(texture, level);
458 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
460 height *= format->height_scale.numerator;
461 height /= format->height_scale.denominator;
464 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
465 texture, layer, level, target, width, height);
467 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
469 depth = wined3d_texture_get_level_depth(texture, level);
470 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
471 target == GL_TEXTURE_2D_ARRAY ? texture->layer_count : depth, 0,
472 format->glFormat, format->glType, NULL));
473 checkGLcall("glTexImage3D");
475 else
477 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format,
478 width, height, 0, format->glFormat, format->glType, NULL);
479 checkGLcall("glTexImage2D");
485 /* Context activation is done by the caller. */
486 /* The caller is responsible for binding the correct texture. */
487 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
488 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
490 unsigned int samples = wined3d_texture_get_gl_sample_count(texture);
491 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
492 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
494 switch (texture->target)
496 case GL_TEXTURE_3D:
497 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count,
498 gl_internal_format, width, height, wined3d_texture_get_level_depth(texture, 0)));
499 break;
500 case GL_TEXTURE_2D_ARRAY:
501 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count,
502 gl_internal_format, width, height, texture->layer_count));
503 break;
504 case GL_TEXTURE_2D_MULTISAMPLE:
505 GL_EXTCALL(glTexStorage2DMultisample(texture->target, samples,
506 gl_internal_format, width, height, GL_FALSE));
507 break;
508 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
509 GL_EXTCALL(glTexStorage3DMultisample(texture->target, samples,
510 gl_internal_format, width, height, texture->layer_count, GL_FALSE));
511 break;
512 default:
513 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count,
514 gl_internal_format, width, height));
515 break;
518 checkGLcall("allocate immutable storage");
521 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
523 struct wined3d_device *device = texture->resource.device;
524 const struct wined3d_gl_info *gl_info = NULL;
525 struct wined3d_context *context = NULL;
527 if (texture->texture_rgb.name || texture->texture_srgb.name
528 || texture->rb_multisample || texture->rb_resolved)
530 context = context_acquire(device, NULL, 0);
531 gl_info = context->gl_info;
534 if (texture->texture_rgb.name)
535 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
537 if (texture->texture_srgb.name)
538 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
540 if (texture->rb_multisample)
542 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
543 context_gl_resource_released(device, texture->rb_multisample, TRUE);
544 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
545 texture->rb_multisample = 0;
548 if (texture->rb_resolved)
550 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
551 context_gl_resource_released(device, texture->rb_resolved, TRUE);
552 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
553 texture->rb_resolved = 0;
556 if (context) context_release(context);
558 wined3d_texture_set_dirty(texture);
560 resource_unload(&texture->resource);
563 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
565 unsigned int sub_count = texture->level_count * texture->layer_count;
566 struct wined3d_texture_sub_resource *sub_resource;
567 unsigned int i;
569 for (i = 0; i < sub_count; ++i)
571 sub_resource = &texture->sub_resources[i];
572 if (sub_resource->parent)
574 TRACE("sub-resource %u.\n", i);
575 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
576 sub_resource->parent = NULL;
581 static void wined3d_texture_create_dc(void *object)
583 const struct wined3d_texture_idx *idx = object;
584 struct wined3d_context *context = NULL;
585 unsigned int sub_resource_idx, level;
586 const struct wined3d_format *format;
587 unsigned int row_pitch, slice_pitch;
588 struct wined3d_texture *texture;
589 struct wined3d_dc_info *dc_info;
590 struct wined3d_bo_address data;
591 D3DKMT_CREATEDCFROMMEMORY desc;
592 struct wined3d_device *device;
593 NTSTATUS status;
595 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
597 texture = idx->texture;
598 sub_resource_idx = idx->sub_resource_idx;
599 level = sub_resource_idx % texture->level_count;
600 device = texture->resource.device;
602 format = texture->resource.format;
603 if (!format->ddi_format)
605 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
606 return;
609 if (!texture->dc_info)
611 unsigned int sub_count = texture->level_count * texture->layer_count;
613 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
615 ERR("Failed to allocate DC info.\n");
616 return;
620 if (device->d3d_initialized)
621 context = context_acquire(device, NULL, 0);
623 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
624 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
625 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
626 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
627 desc.pMemory = context_map_bo_address(context, &data,
628 texture->sub_resources[sub_resource_idx].size,
629 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
631 if (context)
632 context_release(context);
634 desc.Format = format->ddi_format;
635 desc.Width = wined3d_texture_get_level_width(texture, level);
636 desc.Height = wined3d_texture_get_level_height(texture, level);
637 desc.Pitch = row_pitch;
638 desc.hDeviceDc = CreateCompatibleDC(NULL);
639 desc.pColorTable = NULL;
641 status = D3DKMTCreateDCFromMemory(&desc);
642 DeleteDC(desc.hDeviceDc);
643 if (status)
645 WARN("Failed to create DC, status %#x.\n", status);
646 return;
649 dc_info = &texture->dc_info[sub_resource_idx];
650 dc_info->dc = desc.hDc;
651 dc_info->bitmap = desc.hBitmap;
653 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
656 static void wined3d_texture_destroy_dc(void *object)
658 const struct wined3d_texture_idx *idx = object;
659 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
660 struct wined3d_context *context = NULL;
661 struct wined3d_texture *texture;
662 struct wined3d_dc_info *dc_info;
663 struct wined3d_bo_address data;
664 unsigned int sub_resource_idx;
665 struct wined3d_device *device;
666 NTSTATUS status;
668 texture = idx->texture;
669 sub_resource_idx = idx->sub_resource_idx;
670 device = texture->resource.device;
671 dc_info = &texture->dc_info[sub_resource_idx];
673 if (!dc_info->dc)
675 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
676 return;
679 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
681 destroy_desc.hDc = dc_info->dc;
682 destroy_desc.hBitmap = dc_info->bitmap;
683 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
684 ERR("Failed to destroy dc, status %#x.\n", status);
685 dc_info->dc = NULL;
686 dc_info->bitmap = NULL;
688 if (device->d3d_initialized)
689 context = context_acquire(device, NULL, 0);
691 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
692 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
694 if (context)
695 context_release(context);
698 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
700 unsigned int sub_count = texture->level_count * texture->layer_count;
701 struct wined3d_device *device = texture->resource.device;
702 struct wined3d_renderbuffer_entry *entry, *entry2;
703 const struct wined3d_gl_info *gl_info = NULL;
704 struct wined3d_context *context = NULL;
705 struct wined3d_dc_info *dc_info;
706 GLuint buffer_object;
707 unsigned int i;
709 TRACE("texture %p.\n", texture);
711 for (i = 0; i < sub_count; ++i)
713 if (!(buffer_object = texture->sub_resources[i].buffer_object))
714 continue;
716 TRACE("Deleting buffer object %u.\n", buffer_object);
718 /* We may not be able to get a context in wined3d_texture_cleanup() in
719 * general, but if a buffer object was previously created we can. */
720 if (!context)
722 context = context_acquire(device, NULL, 0);
723 gl_info = context->gl_info;
726 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
729 if (!context && !list_empty(&texture->renderbuffers))
731 context = context_acquire(device, NULL, 0);
732 gl_info = context->gl_info;
735 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture->renderbuffers, struct wined3d_renderbuffer_entry, entry)
737 TRACE("Deleting renderbuffer %u.\n", entry->id);
738 context_gl_resource_released(device, entry->id, TRUE);
739 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
740 heap_free(entry);
743 if (context)
744 context_release(context);
746 if ((dc_info = texture->dc_info))
748 for (i = 0; i < sub_count; ++i)
750 if (dc_info[i].dc)
752 struct wined3d_texture_idx texture_idx = {texture, i};
754 wined3d_texture_destroy_dc(&texture_idx);
757 heap_free(dc_info);
760 if (texture->overlay_info)
762 for (i = 0; i < sub_count; ++i)
764 struct wined3d_overlay_info *info = &texture->overlay_info[i];
765 struct wined3d_overlay_info *overlay, *cur;
767 list_remove(&info->entry);
768 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
770 list_remove(&overlay->entry);
773 heap_free(texture->overlay_info);
775 wined3d_texture_unload_gl_texture(texture);
778 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
780 texture->swapchain = swapchain;
781 wined3d_resource_update_draw_binding(&texture->resource);
784 /* Context activation is done by the caller. */
785 void wined3d_texture_bind(struct wined3d_texture *texture,
786 struct wined3d_context *context, BOOL srgb)
788 const struct wined3d_gl_info *gl_info = context->gl_info;
789 const struct wined3d_format *format = texture->resource.format;
790 const struct color_fixup_desc fixup = format->color_fixup;
791 struct gl_texture *gl_tex;
792 GLenum target;
794 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
796 if (!needs_separate_srgb_gl_texture(context, texture))
797 srgb = FALSE;
799 /* sRGB mode cache for preload() calls outside drawprim. */
800 if (srgb)
801 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
802 else
803 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
805 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
806 target = texture->target;
808 if (gl_tex->name)
810 context_bind_texture(context, target, gl_tex->name);
811 return;
814 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
815 checkGLcall("glGenTextures");
816 TRACE("Generated texture %d.\n", gl_tex->name);
818 if (!gl_tex->name)
820 ERR("Failed to generate a texture name.\n");
821 return;
824 /* Initialise the state of the texture object to the OpenGL defaults, not
825 * the wined3d defaults. */
826 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
827 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
828 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
829 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
830 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
831 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
832 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
833 gl_tex->sampler_desc.lod_bias = 0.0f;
834 gl_tex->sampler_desc.min_lod = -1000.0f;
835 gl_tex->sampler_desc.max_lod = 1000.0f;
836 gl_tex->sampler_desc.max_anisotropy = 1;
837 gl_tex->sampler_desc.compare = FALSE;
838 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
839 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
840 gl_tex->sampler_desc.srgb_decode = TRUE;
841 else
842 gl_tex->sampler_desc.srgb_decode = srgb;
843 gl_tex->base_level = 0;
844 wined3d_texture_set_dirty(texture);
846 context_bind_texture(context, target, gl_tex->name);
848 /* For a new texture we have to set the texture levels after binding the
849 * texture. Beware that texture rectangles do not support mipmapping, but
850 * set the maxmiplevel if we're relying on the partial
851 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
852 * (I.e., do not care about cond_np2 here, just look for
853 * GL_TEXTURE_RECTANGLE_ARB.) */
854 if (target != GL_TEXTURE_RECTANGLE_ARB)
856 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
857 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
858 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
861 if (target == GL_TEXTURE_CUBE_MAP_ARB)
863 /* Cubemaps are always set to clamp, regardless of the sampler state. */
864 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
865 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
866 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
869 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
871 /* Conditinal non power of two textures use a different clamping
872 * default. If we're using the GL_WINE_normalized_texrect partial
873 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
874 * has the address mode set to repeat - something that prevents us
875 * from hitting the accelerated codepath. Thus manually set the GL
876 * state. The same applies to filtering. Even if the texture has only
877 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
878 * fallback on macos. */
879 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
880 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
881 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
882 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
883 checkGLcall("glTexParameteri");
884 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
885 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
886 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
887 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
888 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
891 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
893 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
894 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
897 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
899 static const GLenum swizzle_source[] =
901 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
902 GL_ONE, /* CHANNEL_SOURCE_ONE */
903 GL_RED, /* CHANNEL_SOURCE_X */
904 GL_GREEN, /* CHANNEL_SOURCE_Y */
905 GL_BLUE, /* CHANNEL_SOURCE_Z */
906 GL_ALPHA, /* CHANNEL_SOURCE_W */
908 struct
910 GLint x, y, z, w;
912 swizzle;
914 swizzle.x = swizzle_source[fixup.x_source];
915 swizzle.y = swizzle_source[fixup.y_source];
916 swizzle.z = swizzle_source[fixup.z_source];
917 swizzle.w = swizzle_source[fixup.w_source];
918 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
919 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
923 /* Context activation is done by the caller. */
924 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
925 struct wined3d_context *context, BOOL srgb)
927 /* We don't need a specific texture unit, but after binding the texture
928 * the current unit is dirty. Read the unit back instead of switching to
929 * 0, this avoids messing around with the state manager's GL states. The
930 * current texture unit should always be a valid one.
932 * To be more specific, this is tricky because we can implicitly be
933 * called from sampler() in state.c. This means we can't touch anything
934 * other than whatever happens to be the currently active texture, or we
935 * would risk marking already applied sampler states dirty again. */
936 if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
938 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
939 if (active_sampler != WINED3D_UNMAPPED_STAGE)
940 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
942 /* FIXME: Ideally we'd only do this when touching a binding that's used by
943 * a shader. */
944 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
945 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
947 wined3d_texture_bind(texture, context, srgb);
950 /* Context activation is done by the caller (state handler). */
951 /* This function relies on the correct texture being bound and loaded. */
952 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
953 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
955 const struct wined3d_gl_info *gl_info = context->gl_info;
956 GLenum target = texture->target;
957 struct gl_texture *gl_tex;
958 DWORD state;
960 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
962 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
964 state = sampler_desc->address_u;
965 if (state != gl_tex->sampler_desc.address_u)
967 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
968 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
969 gl_tex->sampler_desc.address_u = state;
972 state = sampler_desc->address_v;
973 if (state != gl_tex->sampler_desc.address_v)
975 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
976 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
977 gl_tex->sampler_desc.address_v = state;
980 state = sampler_desc->address_w;
981 if (state != gl_tex->sampler_desc.address_w)
983 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
984 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
985 gl_tex->sampler_desc.address_w = state;
988 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
989 sizeof(gl_tex->sampler_desc.border_color)))
991 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
992 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
993 sizeof(gl_tex->sampler_desc.border_color));
996 state = sampler_desc->mag_filter;
997 if (state != gl_tex->sampler_desc.mag_filter)
999 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1000 gl_tex->sampler_desc.mag_filter = state;
1003 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1004 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1006 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1007 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1008 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1009 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1012 state = sampler_desc->max_anisotropy;
1013 if (state != gl_tex->sampler_desc.max_anisotropy)
1015 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1016 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1017 else
1018 WARN("Anisotropic filtering not supported.\n");
1019 gl_tex->sampler_desc.max_anisotropy = state;
1022 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1023 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1024 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1026 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1027 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1028 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1031 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1033 if (sampler_desc->compare)
1034 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1035 else
1036 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1037 gl_tex->sampler_desc.compare = sampler_desc->compare;
1040 checkGLcall("Texture parameter application");
1042 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1044 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1045 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1046 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1050 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1052 ULONG refcount;
1054 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1056 if (texture->swapchain)
1057 return wined3d_swapchain_incref(texture->swapchain);
1059 refcount = InterlockedIncrement(&texture->resource.ref);
1060 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1062 return refcount;
1065 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1067 wined3d_texture_sub_resources_destroyed(texture);
1068 resource_cleanup(&texture->resource);
1069 wined3d_resource_wait_idle(&texture->resource);
1070 wined3d_texture_cleanup(texture);
1073 static void wined3d_texture_destroy_object(void *object)
1075 wined3d_texture_cleanup(object);
1076 heap_free(object);
1079 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1081 ULONG refcount;
1083 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1085 if (texture->swapchain)
1086 return wined3d_swapchain_decref(texture->swapchain);
1088 refcount = InterlockedDecrement(&texture->resource.ref);
1089 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1091 if (!refcount)
1093 /* Wait for the texture to become idle if it's using user memory,
1094 * since the application is allowed to free that memory once the
1095 * texture is destroyed. Note that this implies that
1096 * wined3d_texture_destroy_object() can't access that memory either. */
1097 if (texture->user_memory)
1098 wined3d_resource_wait_idle(&texture->resource);
1099 wined3d_texture_sub_resources_destroyed(texture);
1100 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
1101 resource_cleanup(&texture->resource);
1102 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1105 return refcount;
1108 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1110 TRACE("texture %p.\n", texture);
1112 return &texture->resource;
1115 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1117 return c1->color_space_low_value == c2->color_space_low_value
1118 && c1->color_space_high_value == c2->color_space_high_value;
1121 /* Context activation is done by the caller */
1122 void wined3d_texture_load(struct wined3d_texture *texture,
1123 struct wined3d_context *context, BOOL srgb)
1125 UINT sub_count = texture->level_count * texture->layer_count;
1126 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1127 DWORD flag;
1128 UINT i;
1130 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1132 if (!needs_separate_srgb_gl_texture(context, texture))
1133 srgb = FALSE;
1135 if (srgb)
1136 flag = WINED3D_TEXTURE_SRGB_VALID;
1137 else
1138 flag = WINED3D_TEXTURE_RGB_VALID;
1140 if (!d3d_info->shader_color_key
1141 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1142 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1143 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1144 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1146 unsigned int sub_count = texture->level_count * texture->layer_count;
1147 unsigned int i;
1149 TRACE("Reloading because of color key value change.\n");
1150 for (i = 0; i < sub_count; i++)
1152 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1153 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1154 else
1155 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1158 texture->async.gl_color_key = texture->async.src_blt_color_key;
1161 if (texture->flags & flag)
1163 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1164 return;
1167 /* Reload the surfaces if the texture is marked dirty. */
1168 for (i = 0; i < sub_count; ++i)
1170 if (!wined3d_texture_load_location(texture, i, context,
1171 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1172 ERR("Failed to load location (srgb %#x).\n", srgb);
1174 texture->flags |= flag;
1177 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1179 TRACE("texture %p.\n", texture);
1181 return texture->resource.parent;
1184 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1185 unsigned int level, const struct wined3d_box *box)
1187 const struct wined3d_format *format = texture->resource.format;
1188 unsigned int width_mask, height_mask, width, height, depth;
1190 width = wined3d_texture_get_level_width(texture, level);
1191 height = wined3d_texture_get_level_height(texture, level);
1192 depth = wined3d_texture_get_level_depth(texture, level);
1194 if (box->left >= box->right || box->right > width
1195 || box->top >= box->bottom || box->bottom > height
1196 || box->front >= box->back || box->back > depth)
1198 WARN("Box %s is invalid.\n", debug_box(box));
1199 return WINEDDERR_INVALIDRECT;
1202 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1204 /* This assumes power of two block sizes, but NPOT block sizes would
1205 * be silly anyway.
1207 * This also assumes that the format's block depth is 1. */
1208 width_mask = format->block_width - 1;
1209 height_mask = format->block_height - 1;
1211 if ((box->left & width_mask) || (box->top & height_mask)
1212 || (box->right & width_mask && box->right != width)
1213 || (box->bottom & height_mask && box->bottom != height))
1215 WARN("Box %s is misaligned for %ux%u blocks.\n",
1216 debug_box(box), format->block_width, format->block_height);
1217 return WINED3DERR_INVALIDCALL;
1221 return WINED3D_OK;
1224 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1225 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1227 const struct wined3d_resource *resource = &texture->resource;
1228 unsigned int width = wined3d_texture_get_level_width(texture, level);
1229 unsigned int height = wined3d_texture_get_level_height(texture, level);
1231 if (texture->row_pitch)
1233 *row_pitch = texture->row_pitch;
1234 *slice_pitch = texture->slice_pitch;
1235 return;
1238 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1239 width, height, row_pitch, slice_pitch);
1242 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1244 DWORD old = texture->lod;
1246 TRACE("texture %p, lod %u.\n", texture, lod);
1248 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1249 * textures. The call always returns 0, and GetLOD always returns 0. */
1250 if (!wined3d_resource_access_is_managed(texture->resource.access))
1252 TRACE("Ignoring LOD on texture with resource access %s.\n",
1253 wined3d_debug_resource_access(texture->resource.access));
1254 return 0;
1257 if (lod >= texture->level_count)
1258 lod = texture->level_count - 1;
1260 if (texture->lod != lod)
1262 struct wined3d_device *device = texture->resource.device;
1264 wined3d_resource_wait_idle(&texture->resource);
1265 texture->lod = lod;
1267 texture->texture_rgb.base_level = ~0u;
1268 texture->texture_srgb.base_level = ~0u;
1269 if (texture->resource.bind_count)
1270 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1271 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1274 return old;
1277 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1279 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1281 return texture->lod;
1284 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1286 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1288 return texture->level_count;
1291 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1292 DWORD flags, const struct wined3d_color_key *color_key)
1294 struct wined3d_device *device = texture->resource.device;
1295 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1296 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1298 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1300 if (flags & ~all_flags)
1302 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1303 return WINED3DERR_INVALIDCALL;
1306 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1308 return WINED3D_OK;
1311 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1312 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1313 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1314 /* Context activation is done by the caller. */
1315 void wined3d_texture_set_compatible_renderbuffer(struct wined3d_texture *texture,
1316 unsigned int level, const struct wined3d_rendertarget_info *rt)
1318 struct wined3d_renderbuffer_entry *entry;
1319 const struct wined3d_gl_info *gl_info;
1320 unsigned int src_width, src_height;
1321 unsigned int width, height;
1322 GLuint renderbuffer = 0;
1324 gl_info = &texture->resource.device->adapter->gl_info;
1325 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1326 return;
1328 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1330 struct wined3d_texture *rt_texture;
1331 unsigned int rt_level;
1333 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1335 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1336 return;
1338 rt_texture = wined3d_texture_from_resource(rt->resource);
1339 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1341 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1342 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1344 else
1346 width = wined3d_texture_get_level_pow2_width(texture, level);
1347 height = wined3d_texture_get_level_pow2_height(texture, level);
1350 src_width = wined3d_texture_get_level_pow2_width(texture, level);
1351 src_height = wined3d_texture_get_level_pow2_height(texture, level);
1353 /* A depth stencil smaller than the render target is not valid */
1354 if (width > src_width || height > src_height)
1355 return;
1357 /* Remove any renderbuffer set if the sizes match */
1358 if (width == src_width && height == src_height)
1360 texture->current_renderbuffer = NULL;
1361 return;
1364 /* Look if we've already got a renderbuffer of the correct dimensions */
1365 LIST_FOR_EACH_ENTRY(entry, &texture->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1367 if (entry->width == width && entry->height == height)
1369 renderbuffer = entry->id;
1370 texture->current_renderbuffer = entry;
1371 break;
1375 if (!renderbuffer)
1377 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1378 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1379 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
1380 texture->resource.format->glInternal, width, height);
1382 entry = heap_alloc(sizeof(*entry));
1383 entry->width = width;
1384 entry->height = height;
1385 entry->id = renderbuffer;
1386 list_add_head(&texture->renderbuffers, &entry->entry);
1388 texture->current_renderbuffer = entry;
1391 checkGLcall("set_compatible_renderbuffer");
1394 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1395 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1396 UINT multisample_quality, void *mem, UINT pitch)
1398 struct wined3d_device *device = texture->resource.device;
1399 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1400 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id, texture->resource.usage);
1401 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1402 struct wined3d_texture_sub_resource *sub_resource;
1403 DWORD valid_location = 0;
1404 BOOL create_dib = FALSE;
1406 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1407 "mem %p, pitch %u.\n",
1408 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1410 if (!resource_size)
1411 return WINED3DERR_INVALIDCALL;
1413 if (texture->level_count * texture->layer_count > 1)
1415 WARN("Texture has multiple sub-resources, not supported.\n");
1416 return WINED3DERR_INVALIDCALL;
1419 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1421 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1422 return WINED3DERR_INVALIDCALL;
1425 if (texture->resource.map_count)
1427 WARN("Texture is mapped.\n");
1428 return WINED3DERR_INVALIDCALL;
1431 /* We have no way of supporting a pitch that is not a multiple of the pixel
1432 * byte width short of uploading the texture row-by-row.
1433 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1434 * for user-memory textures (it always expects packed data) while DirectDraw
1435 * requires a 4-byte aligned pitch and doesn't support texture formats
1436 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1437 * This check is here to verify that the assumption holds. */
1438 if (pitch % texture->resource.format->byte_count)
1440 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1441 return WINED3DERR_INVALIDCALL;
1444 if (device->d3d_initialized)
1445 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1446 wined3d_resource_wait_idle(&texture->resource);
1448 sub_resource = &texture->sub_resources[0];
1449 if (texture->dc_info && texture->dc_info[0].dc)
1451 struct wined3d_texture_idx texture_idx = {texture, 0};
1453 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1454 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1455 create_dib = TRUE;
1458 wined3d_resource_free_sysmem(&texture->resource);
1460 if ((texture->row_pitch = pitch))
1461 texture->slice_pitch = height * pitch;
1462 else
1463 /* User memory surfaces don't have the regular surface alignment. */
1464 wined3d_format_calculate_pitch(format, 1, width, height,
1465 &texture->row_pitch, &texture->slice_pitch);
1467 texture->resource.format = format;
1468 texture->resource.multisample_type = multisample_type;
1469 texture->resource.multisample_quality = multisample_quality;
1470 texture->resource.width = width;
1471 texture->resource.height = height;
1472 texture->resource.size = texture->slice_pitch;
1473 sub_resource->size = texture->slice_pitch;
1474 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1476 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1477 texture->target = GL_TEXTURE_2D_MULTISAMPLE;
1478 else
1479 texture->target = GL_TEXTURE_2D;
1481 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1482 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1484 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1485 texture->pow2_width = texture->pow2_height = 1;
1486 while (texture->pow2_width < width)
1487 texture->pow2_width <<= 1;
1488 while (texture->pow2_height < height)
1489 texture->pow2_height <<= 1;
1491 else
1493 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1494 texture->pow2_width = width;
1495 texture->pow2_height = height;
1498 if ((texture->user_memory = mem))
1500 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1501 valid_location = WINED3D_LOCATION_USER_MEMORY;
1503 else
1505 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1506 valid_location = WINED3D_LOCATION_SYSMEM;
1509 /* The format might be changed to a format that needs conversion.
1510 * If the surface didn't use PBOs previously but could now, don't
1511 * change it - whatever made us not use PBOs might come back, e.g.
1512 * color keys. */
1513 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1514 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1516 wined3d_texture_validate_location(texture, 0, valid_location);
1517 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1519 if (create_dib)
1521 struct wined3d_texture_idx texture_idx = {texture, 0};
1523 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1524 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1527 return WINED3D_OK;
1530 /* Context activation is done by the caller. */
1531 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1532 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1534 struct wined3d_texture_sub_resource *sub_resource;
1536 sub_resource = &texture->sub_resources[sub_resource_idx];
1537 if (sub_resource->buffer_object)
1538 return;
1540 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1541 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1542 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1543 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1544 checkGLcall("Create buffer object");
1546 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1547 sub_resource->buffer_object, texture, sub_resource_idx);
1550 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1552 unsigned int sub_count = texture->level_count * texture->layer_count;
1553 unsigned int i;
1555 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1556 | WINED3D_TEXTURE_CONVERTED);
1557 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1558 for (i = 0; i < sub_count; ++i)
1560 wined3d_texture_invalidate_location(texture, i,
1561 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1565 /* Context activation is done by the caller. */
1566 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1568 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1569 const struct wined3d_format *format = texture->resource.format;
1570 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1571 const struct wined3d_gl_info *gl_info = context->gl_info;
1572 const struct wined3d_color_key_conversion *conversion;
1573 GLenum internal;
1575 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1577 if (!d3d_info->shader_color_key
1578 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1579 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1581 wined3d_texture_force_reload(texture);
1583 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1584 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1587 if (texture->flags & alloc_flag)
1588 return;
1590 if (format->conv_byte_count)
1592 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1594 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1596 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1597 format = wined3d_get_format(gl_info, conversion->dst_format, texture->resource.usage);
1598 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1601 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1603 if (srgb)
1604 internal = format->glGammaInternal;
1605 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1606 && wined3d_resource_is_offscreen(&texture->resource))
1607 internal = format->rtInternal;
1608 else
1609 internal = format->glInternal;
1611 if (!internal)
1612 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1614 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1616 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1617 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1618 else
1619 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1620 texture->flags |= alloc_flag;
1623 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1624 const struct wined3d_gl_info *gl_info, BOOL multisample)
1626 const struct wined3d_format *format = texture->resource.format;
1628 if (multisample)
1630 DWORD samples;
1632 if (texture->rb_multisample)
1633 return;
1635 samples = wined3d_texture_get_gl_sample_count(texture);
1637 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1638 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1639 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1640 format->glInternal, texture->resource.width, texture->resource.height);
1641 checkGLcall("glRenderbufferStorageMultisample()");
1642 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1644 else
1646 if (texture->rb_resolved)
1647 return;
1649 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1650 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1651 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1652 texture->resource.width, texture->resource.height);
1653 checkGLcall("glRenderbufferStorage()");
1654 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1658 /* Context activation is done by the caller. Context may be NULL in
1659 * WINED3D_NO3D mode. */
1660 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1661 struct wined3d_context *context, DWORD location)
1663 switch (location)
1665 case WINED3D_LOCATION_SYSMEM:
1666 if (texture->resource.heap_memory)
1667 return TRUE;
1669 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1671 ERR("Failed to allocate system memory.\n");
1672 return FALSE;
1674 return TRUE;
1676 case WINED3D_LOCATION_USER_MEMORY:
1677 if (!texture->user_memory)
1678 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1679 return TRUE;
1681 case WINED3D_LOCATION_BUFFER:
1682 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1683 return TRUE;
1685 case WINED3D_LOCATION_TEXTURE_RGB:
1686 wined3d_texture_prepare_texture(texture, context, FALSE);
1687 return TRUE;
1689 case WINED3D_LOCATION_TEXTURE_SRGB:
1690 wined3d_texture_prepare_texture(texture, context, TRUE);
1691 return TRUE;
1693 case WINED3D_LOCATION_DRAWABLE:
1694 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1695 ERR("Texture %p does not have a drawable.\n", texture);
1696 return TRUE;
1698 case WINED3D_LOCATION_RB_MULTISAMPLE:
1699 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1700 return TRUE;
1702 case WINED3D_LOCATION_RB_RESOLVED:
1703 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1704 return TRUE;
1706 default:
1707 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1708 return FALSE;
1712 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1713 unsigned int sub_resource_idx)
1715 UINT sub_count = texture->level_count * texture->layer_count;
1717 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1719 if (sub_resource_idx >= sub_count)
1721 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1722 return NULL;
1725 return &texture->sub_resources[sub_resource_idx];
1728 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1729 UINT layer, const struct wined3d_box *dirty_region)
1731 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1733 if (layer >= texture->layer_count)
1735 WARN("Invalid layer %u specified.\n", layer);
1736 return WINED3DERR_INVALIDCALL;
1739 if (dirty_region)
1740 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1742 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
1744 return WINED3D_OK;
1747 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1748 const struct wined3d_context *context, const struct wined3d_box *box,
1749 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1751 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1752 context, box, data, row_pitch, slice_pitch);
1755 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1756 const struct wined3d_context *context, const struct wined3d_box *box,
1757 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
1759 unsigned int texture_level;
1760 POINT dst_point;
1761 RECT src_rect;
1763 src_rect.left = 0;
1764 src_rect.top = 0;
1765 if (box)
1767 dst_point.x = box->left;
1768 dst_point.y = box->top;
1769 src_rect.right = box->right - box->left;
1770 src_rect.bottom = box->bottom - box->top;
1772 else
1774 dst_point.x = dst_point.y = 0;
1775 texture_level = sub_resource_idx % texture->level_count;
1776 src_rect.right = wined3d_texture_get_level_width(texture, texture_level);
1777 src_rect.bottom = wined3d_texture_get_level_height(texture, texture_level);
1780 wined3d_surface_upload_data(texture, sub_resource_idx, context->gl_info,
1781 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1784 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
1785 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1786 struct wined3d_context *context, DWORD location)
1788 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1789 texture, sub_resource_idx, context, wined3d_debug_location(location));
1791 switch (location)
1793 case WINED3D_LOCATION_USER_MEMORY:
1794 case WINED3D_LOCATION_SYSMEM:
1795 case WINED3D_LOCATION_BUFFER:
1796 return texture2d_load_sysmem(texture, sub_resource_idx, context, location);
1798 case WINED3D_LOCATION_DRAWABLE:
1799 return texture2d_load_drawable(texture, sub_resource_idx, context);
1801 case WINED3D_LOCATION_RB_RESOLVED:
1802 case WINED3D_LOCATION_RB_MULTISAMPLE:
1803 return texture2d_load_renderbuffer(texture, sub_resource_idx, context, location);
1805 case WINED3D_LOCATION_TEXTURE_RGB:
1806 case WINED3D_LOCATION_TEXTURE_SRGB:
1807 return texture2d_load_texture(texture, sub_resource_idx, context,
1808 location == WINED3D_LOCATION_TEXTURE_SRGB);
1810 default:
1811 ERR("Don't know how to handle location %#x.\n", location);
1812 return FALSE;
1816 static const struct wined3d_texture_ops texture2d_ops =
1818 texture2d_upload_data,
1819 texture2d_load_location,
1822 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1824 return texture_from_resource(resource);
1827 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1829 return wined3d_texture_incref(texture_from_resource(resource));
1832 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1834 return wined3d_texture_decref(texture_from_resource(resource));
1837 static void texture_resource_preload(struct wined3d_resource *resource)
1839 struct wined3d_texture *texture = texture_from_resource(resource);
1840 struct wined3d_context *context;
1842 context = context_acquire(resource->device, NULL, 0);
1843 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1844 context_release(context);
1847 static void wined3d_texture_unload(struct wined3d_resource *resource)
1849 struct wined3d_texture *texture = texture_from_resource(resource);
1850 UINT sub_count = texture->level_count * texture->layer_count;
1851 struct wined3d_renderbuffer_entry *entry, *entry2;
1852 struct wined3d_device *device = resource->device;
1853 const struct wined3d_gl_info *gl_info;
1854 struct wined3d_context *context;
1855 UINT i;
1857 TRACE("texture %p.\n", texture);
1859 context = context_acquire(device, NULL, 0);
1860 gl_info = context->gl_info;
1862 for (i = 0; i < sub_count; ++i)
1864 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1866 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
1867 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1869 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1871 else
1873 /* We should only get here on device reset/teardown for implicit
1874 * resources. */
1875 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
1876 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1877 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
1878 debug_d3dresourcetype(resource->type), resource, i,
1879 wined3d_debug_resource_access(resource->access));
1880 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1881 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1884 if (sub_resource->buffer_object)
1885 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1888 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1890 context_gl_resource_released(device, entry->id, TRUE);
1891 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1892 list_remove(&entry->entry);
1893 heap_free(entry);
1895 list_init(&texture->renderbuffers);
1896 texture->current_renderbuffer = NULL;
1898 context_release(context);
1900 wined3d_texture_force_reload(texture);
1901 wined3d_texture_unload_gl_texture(texture);
1904 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1905 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1907 const struct wined3d_format *format = resource->format;
1908 struct wined3d_texture_sub_resource *sub_resource;
1909 struct wined3d_device *device = resource->device;
1910 unsigned int fmt_flags = resource->format_flags;
1911 struct wined3d_context *context = NULL;
1912 struct wined3d_texture *texture;
1913 struct wined3d_bo_address data;
1914 unsigned int texture_level;
1915 BYTE *base_memory;
1916 BOOL ret;
1918 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1919 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1921 texture = texture_from_resource(resource);
1922 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1923 return E_INVALIDARG;
1925 texture_level = sub_resource_idx % texture->level_count;
1926 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
1928 WARN("Map box is invalid.\n");
1929 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
1930 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1931 return WINED3DERR_INVALIDCALL;
1934 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1936 WARN("DC is in use.\n");
1937 return WINED3DERR_INVALIDCALL;
1940 if (sub_resource->map_count)
1942 WARN("Sub-resource is already mapped.\n");
1943 return WINED3DERR_INVALIDCALL;
1946 if (device->d3d_initialized)
1947 context = context_acquire(device, NULL, 0);
1949 if (flags & WINED3D_MAP_DISCARD)
1951 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1952 wined3d_debug_location(resource->map_binding));
1953 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
1954 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
1956 else
1958 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1959 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1960 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
1963 if (!ret)
1965 ERR("Failed to prepare location.\n");
1966 context_release(context);
1967 return E_OUTOFMEMORY;
1970 if (flags & WINED3D_MAP_WRITE
1971 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1972 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
1974 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
1975 base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
1976 TRACE("Base memory pointer %p.\n", base_memory);
1978 if (context)
1979 context_release(context);
1981 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1983 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1984 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1986 else
1988 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1991 if (!box)
1993 map_desc->data = base_memory;
1995 else
1997 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1999 /* Compressed textures are block based, so calculate the offset of
2000 * the block that contains the top-left pixel of the mapped box. */
2001 map_desc->data = base_memory
2002 + (box->front * map_desc->slice_pitch)
2003 + ((box->top / format->block_height) * map_desc->row_pitch)
2004 + ((box->left / format->block_width) * format->block_byte_count);
2006 else
2008 map_desc->data = base_memory
2009 + (box->front * map_desc->slice_pitch)
2010 + (box->top * map_desc->row_pitch)
2011 + (box->left * format->byte_count);
2015 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2017 RECT *r = &texture->swapchain->front_buffer_update;
2019 if (!box)
2020 SetRect(r, 0, 0, resource->width, resource->height);
2021 else
2022 SetRect(r, box->left, box->top, box->right, box->bottom);
2023 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
2026 ++resource->map_count;
2027 ++sub_resource->map_count;
2029 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2030 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2032 return WINED3D_OK;
2035 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
2037 struct wined3d_texture_sub_resource *sub_resource;
2038 struct wined3d_device *device = resource->device;
2039 struct wined3d_context *context = NULL;
2040 struct wined3d_texture *texture;
2041 struct wined3d_bo_address data;
2043 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
2045 texture = texture_from_resource(resource);
2046 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2047 return E_INVALIDARG;
2049 if (!sub_resource->map_count)
2051 WARN("Trying to unmap unmapped sub-resource.\n");
2052 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2053 return WINED3D_OK;
2054 return WINEDDERR_NOTLOCKED;
2057 if (device->d3d_initialized)
2058 context = context_acquire(device, NULL, 0);
2060 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2061 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2063 if (context)
2064 context_release(context);
2066 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2068 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2069 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2072 --sub_resource->map_count;
2073 if (!--resource->map_count && texture->update_map_binding)
2074 wined3d_texture_update_map_binding(texture);
2076 return WINED3D_OK;
2079 static const struct wined3d_resource_ops texture_resource_ops =
2081 texture_resource_incref,
2082 texture_resource_decref,
2083 texture_resource_preload,
2084 wined3d_texture_unload,
2085 texture_resource_sub_resource_map,
2086 texture_resource_sub_resource_unmap,
2089 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2090 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2091 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_texture_ops *texture_ops)
2093 struct wined3d_device_parent *device_parent = device->device_parent;
2094 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2095 unsigned int sub_count, i, j, size, offset = 0;
2096 unsigned int pow2_width, pow2_height;
2097 const struct wined3d_format *format;
2098 HRESULT hr;
2100 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2101 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2102 "flags %#x, device %p, parent %p, parent_ops %p, texture_ops %p.\n",
2103 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
2104 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
2105 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
2106 layer_count, level_count, flags, device, parent, parent_ops, texture_ops);
2108 if (!desc->width || !desc->height || !desc->depth)
2109 return WINED3DERR_INVALIDCALL;
2111 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2113 if (layer_count != 1)
2115 ERR("Invalid layer count for volume texture.\n");
2116 return E_INVALIDARG;
2119 if (!gl_info->supported[EXT_TEXTURE3D])
2121 WARN("OpenGL implementation does not support 3D textures.\n");
2122 return WINED3DERR_INVALIDCALL;
2126 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2127 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2129 WARN("OpenGL implementation does not support array textures.\n");
2130 return WINED3DERR_INVALIDCALL;
2133 /* TODO: It should only be possible to create textures for formats
2134 * that are reported as supported. */
2135 if (WINED3DFMT_UNKNOWN >= desc->format)
2137 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2138 return WINED3DERR_INVALIDCALL;
2141 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
2142 || desc->usage & WINED3DUSAGE_SCRATCH))
2144 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2145 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
2146 return WINED3DERR_INVALIDCALL;
2149 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2150 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2151 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2152 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->access & WINED3D_RESOURCE_ACCESS_CPU)
2153 FIXME("Trying to create a CPU accessible render target.\n");
2155 pow2_width = desc->width;
2156 pow2_height = desc->height;
2157 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
2158 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2160 /* level_count == 0 returns an error as well. */
2161 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2163 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
2165 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2166 "texture without unconditional NPOT support.\n");
2167 return WINED3DERR_INVALIDCALL;
2170 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2172 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2174 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !gl_info->supported[ARB_TEXTURE_RECTANGLE]
2175 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2177 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2179 /* TODO: Add support for non-power-of-two compressed textures. */
2180 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2181 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2183 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2184 desc->width, desc->height);
2185 return WINED3DERR_NOTAVAILABLE;
2188 /* Find the nearest pow2 match. */
2189 pow2_width = pow2_height = 1;
2190 while (pow2_width < desc->width)
2191 pow2_width <<= 1;
2192 while (pow2_height < desc->height)
2193 pow2_height <<= 1;
2194 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2197 texture->pow2_width = pow2_width;
2198 texture->pow2_height = pow2_height;
2200 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2201 && (desc->usage & WINED3DUSAGE_TEXTURE))
2203 /* One of four options:
2204 * 1: Do the same as we do with NPOT and scale the texture. (Any
2205 * texture ops would require the texture to be scaled which is
2206 * potentially slow.)
2207 * 2: Set the texture to the maximum size (bad idea).
2208 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2209 * 4: Create the surface, but allow it to be used only for DirectDraw
2210 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2211 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2212 * the render target. */
2213 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
2215 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2216 return WINED3DERR_NOTAVAILABLE;
2219 /* We should never use this surface in combination with OpenGL. */
2220 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2223 format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
2224 for (i = 0; i < layer_count; ++i)
2226 for (j = 0; j < level_count; ++j)
2228 unsigned int idx = i * level_count + j;
2230 size = wined3d_format_calculate_size(format, device->surface_alignment,
2231 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
2232 texture->sub_resources[idx].offset = offset;
2233 texture->sub_resources[idx].size = size;
2234 offset += size;
2236 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
2239 if (!offset)
2240 return WINED3DERR_INVALIDCALL;
2242 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
2243 desc->multisample_type, desc->multisample_quality, desc->usage, desc->access,
2244 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
2246 static unsigned int once;
2248 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
2249 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
2250 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
2251 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
2252 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
2253 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
2255 WARN("Failed to initialize resource, returning %#x\n", hr);
2256 return hr;
2258 wined3d_resource_update_draw_binding(&texture->resource);
2259 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
2260 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2262 texture->texture_ops = texture_ops;
2264 texture->layer_count = layer_count;
2265 texture->level_count = level_count;
2266 texture->lod = 0;
2267 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
2268 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
2269 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
2270 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
2271 texture->flags |= WINED3D_TEXTURE_GET_DC;
2272 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
2273 texture->flags |= WINED3D_TEXTURE_DISCARD;
2274 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
2276 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
2277 WARN("Format doesn't support mipmaps generation, "
2278 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
2279 else
2280 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
2283 list_init(&texture->renderbuffers);
2285 /* Precalculated scaling for 'faked' non power of two texture coords. */
2286 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2288 texture->pow2_matrix[0] = (float)desc->width;
2289 texture->pow2_matrix[5] = (float)desc->height;
2290 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2291 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2293 else
2295 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2297 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2298 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2299 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2301 else
2303 texture->pow2_matrix[0] = 1.0f;
2304 texture->pow2_matrix[5] = 1.0f;
2306 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2308 texture->target = GL_TEXTURE_3D;
2310 else if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2312 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2314 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
2316 if (layer_count > 1)
2317 texture->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
2318 else
2319 texture->target = GL_TEXTURE_2D_MULTISAMPLE;
2321 else
2323 if (layer_count > 1)
2324 texture->target = GL_TEXTURE_2D_ARRAY;
2325 else
2326 texture->target = GL_TEXTURE_2D;
2329 texture->pow2_matrix[10] = 1.0f;
2330 texture->pow2_matrix[15] = 1.0f;
2331 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2333 if (wined3d_texture_use_pbo(texture, gl_info))
2335 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D
2336 || (texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2337 wined3d_resource_free_sysmem(&texture->resource);
2338 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2341 sub_count = level_count * layer_count;
2342 if (sub_count / layer_count != level_count)
2344 wined3d_texture_cleanup_sync(texture);
2345 return E_OUTOFMEMORY;
2348 if (desc->usage & WINED3DUSAGE_OVERLAY)
2350 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
2352 wined3d_texture_cleanup_sync(texture);
2353 return E_OUTOFMEMORY;
2356 for (i = 0; i < sub_count; ++i)
2358 list_init(&texture->overlay_info[i].entry);
2359 list_init(&texture->overlay_info[i].overlays);
2363 /* Generate all sub-resources. */
2364 for (i = 0; i < sub_count; ++i)
2366 struct wined3d_texture_sub_resource *sub_resource;
2368 sub_resource = &texture->sub_resources[i];
2369 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2370 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
2371 && !(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2373 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
2374 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
2377 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
2378 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2380 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
2381 sub_resource->parent = NULL;
2382 wined3d_texture_cleanup_sync(texture);
2383 return hr;
2386 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2388 TRACE("Created sub-resource %u (level %u, layer %u).\n",
2389 i, i % texture->level_count, i / texture->level_count);
2391 if ((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2393 struct wined3d_texture_idx texture_idx = {texture, i};
2395 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2396 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2397 if (!texture->dc_info || !texture->dc_info[i].dc)
2399 wined3d_texture_cleanup_sync(texture);
2400 return WINED3DERR_INVALIDCALL;
2405 return WINED3D_OK;
2408 /* This call just uploads data, the caller is responsible for binding the
2409 * correct texture. */
2410 /* Context activation is done by the caller. */
2411 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2412 const struct wined3d_context *context, const struct wined3d_box *box,
2413 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch)
2415 const struct wined3d_format *format = texture->resource.format;
2416 unsigned int level = sub_resource_idx % texture->level_count;
2417 const struct wined3d_gl_info *gl_info = context->gl_info;
2418 unsigned int x, y, z, update_w, update_h, update_d;
2419 unsigned int dst_row_pitch, dst_slice_pitch;
2420 unsigned int width, height, depth;
2421 const void *mem = data->addr;
2422 void *converted_mem = NULL;
2424 TRACE("texture %p, sub_resource_idx %u, context %p, box %s, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2425 texture, sub_resource_idx, context, debug_box(box),
2426 data->buffer_object, data->addr, row_pitch, slice_pitch);
2428 width = wined3d_texture_get_level_width(texture, level);
2429 height = wined3d_texture_get_level_height(texture, level);
2430 depth = wined3d_texture_get_level_depth(texture, level);
2432 if (!box)
2434 x = y = z = 0;
2435 update_w = width;
2436 update_h = height;
2437 update_d = depth;
2439 else
2441 x = box->left;
2442 y = box->top;
2443 z = box->front;
2444 update_w = box->right - box->left;
2445 update_h = box->bottom - box->top;
2446 update_d = box->back - box->front;
2449 if (format->conv_byte_count)
2451 if (data->buffer_object)
2452 ERR("Loading a converted texture from a PBO.\n");
2453 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2454 ERR("Converting a block-based format.\n");
2456 dst_row_pitch = update_w * format->conv_byte_count;
2457 dst_slice_pitch = dst_row_pitch * update_h;
2459 converted_mem = heap_calloc(update_d, dst_slice_pitch);
2460 format->upload(data->addr, converted_mem, row_pitch, slice_pitch,
2461 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2462 mem = converted_mem;
2464 else
2466 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2467 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2468 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2471 if (data->buffer_object)
2473 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2474 checkGLcall("glBindBuffer");
2477 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, x, y, z,
2478 update_w, update_h, update_d, format->glFormat, format->glType, mem));
2479 checkGLcall("glTexSubImage3D");
2481 if (data->buffer_object)
2483 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2484 checkGLcall("glBindBuffer");
2487 heap_free(converted_mem);
2490 /* Context activation is done by the caller. */
2491 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2492 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2494 const struct wined3d_format *format = texture->resource.format;
2495 const struct wined3d_gl_info *gl_info = context->gl_info;
2497 if (format->conv_byte_count)
2499 FIXME("Attempting to download a converted volume, format %s.\n",
2500 debug_d3dformat(format->id));
2501 return;
2504 if (data->buffer_object)
2506 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2507 checkGLcall("glBindBuffer");
2510 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2511 format->glFormat, format->glType, data->addr);
2512 checkGLcall("glGetTexImage");
2514 if (data->buffer_object)
2516 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2517 checkGLcall("glBindBuffer");
2522 /* Context activation is done by the caller. */
2523 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2524 struct wined3d_context *context, BOOL dest_is_srgb)
2526 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2527 unsigned int row_pitch, slice_pitch;
2528 struct wined3d_bo_address data;
2530 /* Optimisations are possible, but the effort should be put into either
2531 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2532 * picked the wrong copy for the original upload and fixing that.
2534 * Also keep in mind that we want to avoid using resource.heap_memory
2535 * for DEFAULT pool surfaces. */
2536 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2537 data.buffer_object = 0;
2538 if (!(data.addr = heap_alloc(sub_resource->size)))
2539 return;
2541 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2542 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2543 texture3d_download_data(texture, sub_resource_idx, context, &data);
2544 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2545 texture3d_upload_data(texture, sub_resource_idx, context,
2546 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2548 heap_free(data.addr);
2551 /* Context activation is done by the caller. */
2552 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2553 struct wined3d_context *context, DWORD location)
2555 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2556 unsigned int row_pitch, slice_pitch;
2558 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2559 return FALSE;
2561 switch (location)
2563 case WINED3D_LOCATION_TEXTURE_RGB:
2564 case WINED3D_LOCATION_TEXTURE_SRGB:
2565 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2567 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2568 data.addr += sub_resource->offset;
2569 wined3d_texture_bind_and_dirtify(texture, context,
2570 location == WINED3D_LOCATION_TEXTURE_SRGB);
2571 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2572 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2574 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2576 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2577 wined3d_texture_bind_and_dirtify(texture, context,
2578 location == WINED3D_LOCATION_TEXTURE_SRGB);
2579 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2580 texture3d_upload_data(texture, sub_resource_idx, context, NULL, &data, row_pitch, slice_pitch);
2582 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2584 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2586 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2588 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2590 else
2592 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2593 return FALSE;
2595 break;
2597 case WINED3D_LOCATION_SYSMEM:
2598 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2600 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2602 data.addr += sub_resource->offset;
2603 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2604 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2605 else
2606 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2608 texture3d_download_data(texture, sub_resource_idx, context, &data);
2609 ++texture->download_count;
2611 else
2613 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2614 wined3d_debug_location(sub_resource->locations));
2615 return FALSE;
2617 break;
2619 case WINED3D_LOCATION_BUFFER:
2620 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2622 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2624 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2625 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2626 else
2627 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2629 texture3d_download_data(texture, sub_resource_idx, context, &data);
2631 else
2633 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2634 wined3d_debug_location(sub_resource->locations));
2635 return FALSE;
2637 break;
2639 default:
2640 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2641 wined3d_debug_location(sub_resource->locations));
2642 return FALSE;
2645 return TRUE;
2648 static const struct wined3d_texture_ops texture3d_ops =
2650 texture3d_upload_data,
2651 texture3d_load_location,
2654 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2655 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2656 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2658 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
2659 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
2660 unsigned int dst_format_flags, src_format_flags = 0;
2661 HRESULT hr;
2663 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2664 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2665 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2666 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2668 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
2669 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2670 return WINED3DERR_INVALIDCALL;
2672 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
2673 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2674 return WINED3DERR_INVALIDCALL;
2676 dst_format_flags = dst_texture->resource.format_flags;
2677 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
2678 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
2679 return hr;
2681 src_format_flags = src_texture->resource.format_flags;
2682 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
2683 src_sub_resource_idx % src_texture->level_count, &src_box)))
2684 return hr;
2686 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
2687 || src_texture->sub_resources[src_sub_resource_idx].map_count)
2689 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2690 return WINEDDERR_SURFACEBUSY;
2693 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2694 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
2696 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2697 return WINED3DERR_INVALIDCALL;
2700 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
2701 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
2703 return WINED3D_OK;
2706 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2707 unsigned int sub_resource_idx, LONG *x, LONG *y)
2709 struct wined3d_overlay_info *overlay;
2711 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2713 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
2714 || sub_resource_idx >= texture->level_count * texture->layer_count)
2716 WARN("Invalid sub-resource specified.\n");
2717 return WINEDDERR_NOTAOVERLAYSURFACE;
2720 overlay = &texture->overlay_info[sub_resource_idx];
2721 if (!overlay->dst_texture)
2723 TRACE("Overlay not visible.\n");
2724 *x = 0;
2725 *y = 0;
2726 return WINEDDERR_OVERLAYNOTVISIBLE;
2729 *x = overlay->dst_rect.left;
2730 *y = overlay->dst_rect.top;
2732 TRACE("Returning position %d, %d.\n", *x, *y);
2734 return WINED3D_OK;
2737 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2738 unsigned int sub_resource_idx, LONG x, LONG y)
2740 struct wined3d_overlay_info *overlay;
2741 LONG w, h;
2743 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2745 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
2746 || sub_resource_idx >= texture->level_count * texture->layer_count)
2748 WARN("Invalid sub-resource specified.\n");
2749 return WINEDDERR_NOTAOVERLAYSURFACE;
2752 overlay = &texture->overlay_info[sub_resource_idx];
2753 w = overlay->dst_rect.right - overlay->dst_rect.left;
2754 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
2755 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
2757 return WINED3D_OK;
2760 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2761 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2762 const RECT *dst_rect, DWORD flags)
2764 struct wined3d_overlay_info *overlay;
2765 unsigned int level, dst_level;
2767 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2768 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2769 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2770 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2772 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2773 || sub_resource_idx >= texture->level_count * texture->layer_count)
2775 WARN("Invalid sub-resource specified.\n");
2776 return WINEDDERR_NOTAOVERLAYSURFACE;
2779 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2780 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
2782 WARN("Invalid destination sub-resource specified.\n");
2783 return WINED3DERR_INVALIDCALL;
2786 overlay = &texture->overlay_info[sub_resource_idx];
2788 level = sub_resource_idx % texture->level_count;
2789 if (src_rect)
2790 overlay->src_rect = *src_rect;
2791 else
2792 SetRect(&overlay->src_rect, 0, 0,
2793 wined3d_texture_get_level_width(texture, level),
2794 wined3d_texture_get_level_height(texture, level));
2796 dst_level = dst_sub_resource_idx % dst_texture->level_count;
2797 if (dst_rect)
2798 overlay->dst_rect = *dst_rect;
2799 else
2800 SetRect(&overlay->dst_rect, 0, 0,
2801 wined3d_texture_get_level_width(dst_texture, dst_level),
2802 wined3d_texture_get_level_height(dst_texture, dst_level));
2804 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
2805 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
2807 overlay->dst_texture = NULL;
2808 list_remove(&overlay->entry);
2811 if (flags & WINEDDOVER_SHOW)
2813 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
2815 overlay->dst_texture = dst_texture;
2816 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
2817 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
2820 else if (flags & WINEDDOVER_HIDE)
2822 /* Tests show that the rectangles are erased on hide. */
2823 SetRectEmpty(&overlay->src_rect);
2824 SetRectEmpty(&overlay->dst_rect);
2825 overlay->dst_texture = NULL;
2828 return WINED3D_OK;
2831 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2833 unsigned int sub_count = texture->level_count * texture->layer_count;
2835 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2837 if (sub_resource_idx >= sub_count)
2839 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2840 return NULL;
2843 return texture->sub_resources[sub_resource_idx].parent;
2846 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2847 unsigned int sub_resource_idx, void *parent)
2849 unsigned int sub_count = texture->level_count * texture->layer_count;
2851 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2853 if (sub_resource_idx >= sub_count)
2855 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2856 return;
2859 texture->sub_resources[sub_resource_idx].parent = parent;
2862 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2863 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2865 unsigned int sub_count = texture->level_count * texture->layer_count;
2866 const struct wined3d_resource *resource;
2867 unsigned int level_idx;
2869 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2871 if (sub_resource_idx >= sub_count)
2873 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2874 return WINED3DERR_INVALIDCALL;
2877 resource = &texture->resource;
2878 desc->format = resource->format->id;
2879 desc->multisample_type = resource->multisample_type;
2880 desc->multisample_quality = resource->multisample_quality;
2881 desc->usage = resource->usage;
2882 desc->access = resource->access;
2884 level_idx = sub_resource_idx % texture->level_count;
2885 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2886 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2887 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2888 desc->size = texture->sub_resources[sub_resource_idx].size;
2890 return WINED3D_OK;
2893 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2894 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2895 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2897 struct wined3d_texture *object;
2898 HRESULT hr;
2900 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2901 "parent %p, parent_ops %p, texture %p.\n",
2902 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2904 if (!layer_count)
2906 WARN("Invalid layer count.\n");
2907 return E_INVALIDARG;
2909 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2911 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2912 layer_count = 6;
2915 if (!level_count)
2917 WARN("Invalid level count.\n");
2918 return WINED3DERR_INVALIDCALL;
2921 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2923 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2924 desc->format, desc->usage);
2926 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2927 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2929 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2930 desc->multisample_quality);
2931 return WINED3DERR_NOTAVAILABLE;
2933 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2934 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2935 || desc->multisample_quality))
2937 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2938 desc->multisample_quality);
2939 return WINED3DERR_NOTAVAILABLE;
2943 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture,
2944 sub_resources[level_count * layer_count]))))
2945 return E_OUTOFMEMORY;
2947 switch (desc->resource_type)
2949 case WINED3D_RTYPE_TEXTURE_2D:
2950 hr = wined3d_texture_init(object, desc, layer_count, level_count,
2951 flags, device, parent, parent_ops, &texture2d_ops);
2952 break;
2954 case WINED3D_RTYPE_TEXTURE_3D:
2955 hr = wined3d_texture_init(object, desc, layer_count, level_count,
2956 flags, device, parent, parent_ops, &texture3d_ops);
2957 break;
2959 default:
2960 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2961 hr = WINED3DERR_INVALIDCALL;
2962 break;
2965 if (FAILED(hr))
2967 WARN("Failed to initialize texture, returning %#x.\n", hr);
2968 heap_free(object);
2969 return hr;
2972 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2973 * in this case. */
2974 if (data)
2976 unsigned int sub_count = level_count * layer_count;
2977 unsigned int i;
2979 for (i = 0; i < sub_count; ++i)
2981 if (!data[i].data)
2983 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2984 wined3d_texture_cleanup_sync(object);
2985 heap_free(object);
2986 return E_INVALIDARG;
2990 for (i = 0; i < sub_count; ++i)
2992 wined3d_device_update_sub_resource(device, &object->resource,
2993 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
2997 TRACE("Created texture %p.\n", object);
2998 *texture = object;
3000 return WINED3D_OK;
3003 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
3005 struct wined3d_device *device = texture->resource.device;
3006 struct wined3d_texture_sub_resource *sub_resource;
3007 struct wined3d_dc_info *dc_info;
3009 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3011 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
3013 WARN("Texture does not support GetDC\n");
3014 /* Don't touch the DC */
3015 return WINED3DERR_INVALIDCALL;
3018 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3019 return WINED3DERR_INVALIDCALL;
3021 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3023 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3024 return WINED3DERR_INVALIDCALL;
3027 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3028 return WINED3DERR_INVALIDCALL;
3030 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3032 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3034 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3035 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3036 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3037 return WINED3DERR_INVALIDCALL;
3040 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3041 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3042 ++texture->resource.map_count;
3043 ++sub_resource->map_count;
3045 *dc = dc_info[sub_resource_idx].dc;
3046 TRACE("Returning dc %p.\n", *dc);
3048 return WINED3D_OK;
3051 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3053 struct wined3d_device *device = texture->resource.device;
3054 struct wined3d_texture_sub_resource *sub_resource;
3055 struct wined3d_dc_info *dc_info;
3057 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3059 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3060 return WINED3DERR_INVALIDCALL;
3062 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3064 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3065 return WINED3DERR_INVALIDCALL;
3068 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3069 return WINED3DERR_INVALIDCALL;
3071 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
3073 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3074 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
3075 return WINED3DERR_INVALIDCALL;
3078 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3080 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3082 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
3083 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3086 --sub_resource->map_count;
3087 if (!--texture->resource.map_count && texture->update_map_binding)
3088 wined3d_texture_update_map_binding(texture);
3089 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3090 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3092 return WINED3D_OK;