wined3d: Allow wined3d_texture_upload_data() to upload to WINED3D_LOCATION_TEXTURE_SRGB.
[wine.git] / dlls / wined3d / texture.c
blob29bcfb1e4dfa831960016f01559cdf8d835feb79
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_format *format, const struct wined3d_box *src_box,
1749 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch,
1750 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, BOOL srgb)
1752 texture->texture_ops->texture_upload_data(texture, sub_resource_idx, context,
1753 format, src_box, data, row_pitch, slice_pitch, dst_x, dst_y, dst_z, srgb);
1756 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1757 const struct wined3d_context *context, const struct wined3d_format *format, const struct wined3d_box *src_box,
1758 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch,
1759 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, BOOL srgb)
1761 wined3d_surface_upload_data(texture, sub_resource_idx, context->gl_info,
1762 format, src_box, row_pitch, dst_x, dst_y, srgb, data);
1765 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
1766 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1767 struct wined3d_context *context, DWORD location)
1769 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1770 texture, sub_resource_idx, context, wined3d_debug_location(location));
1772 switch (location)
1774 case WINED3D_LOCATION_USER_MEMORY:
1775 case WINED3D_LOCATION_SYSMEM:
1776 case WINED3D_LOCATION_BUFFER:
1777 return texture2d_load_sysmem(texture, sub_resource_idx, context, location);
1779 case WINED3D_LOCATION_DRAWABLE:
1780 return texture2d_load_drawable(texture, sub_resource_idx, context);
1782 case WINED3D_LOCATION_RB_RESOLVED:
1783 case WINED3D_LOCATION_RB_MULTISAMPLE:
1784 return texture2d_load_renderbuffer(texture, sub_resource_idx, context, location);
1786 case WINED3D_LOCATION_TEXTURE_RGB:
1787 case WINED3D_LOCATION_TEXTURE_SRGB:
1788 return texture2d_load_texture(texture, sub_resource_idx, context,
1789 location == WINED3D_LOCATION_TEXTURE_SRGB);
1791 default:
1792 ERR("Don't know how to handle location %#x.\n", location);
1793 return FALSE;
1797 static const struct wined3d_texture_ops texture2d_ops =
1799 texture2d_upload_data,
1800 texture2d_load_location,
1803 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1805 return texture_from_resource(resource);
1808 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1810 return wined3d_texture_incref(texture_from_resource(resource));
1813 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1815 return wined3d_texture_decref(texture_from_resource(resource));
1818 static void texture_resource_preload(struct wined3d_resource *resource)
1820 struct wined3d_texture *texture = texture_from_resource(resource);
1821 struct wined3d_context *context;
1823 context = context_acquire(resource->device, NULL, 0);
1824 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1825 context_release(context);
1828 static void wined3d_texture_unload(struct wined3d_resource *resource)
1830 struct wined3d_texture *texture = texture_from_resource(resource);
1831 UINT sub_count = texture->level_count * texture->layer_count;
1832 struct wined3d_renderbuffer_entry *entry, *entry2;
1833 struct wined3d_device *device = resource->device;
1834 const struct wined3d_gl_info *gl_info;
1835 struct wined3d_context *context;
1836 UINT i;
1838 TRACE("texture %p.\n", texture);
1840 context = context_acquire(device, NULL, 0);
1841 gl_info = context->gl_info;
1843 for (i = 0; i < sub_count; ++i)
1845 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1847 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
1848 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1850 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1852 else
1854 /* We should only get here on device reset/teardown for implicit
1855 * resources. */
1856 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
1857 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1858 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
1859 debug_d3dresourcetype(resource->type), resource, i,
1860 wined3d_debug_resource_access(resource->access));
1861 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1862 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1865 if (sub_resource->buffer_object)
1866 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1869 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1871 context_gl_resource_released(device, entry->id, TRUE);
1872 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1873 list_remove(&entry->entry);
1874 heap_free(entry);
1876 list_init(&texture->renderbuffers);
1877 texture->current_renderbuffer = NULL;
1879 context_release(context);
1881 wined3d_texture_force_reload(texture);
1882 wined3d_texture_unload_gl_texture(texture);
1885 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1886 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1888 const struct wined3d_format *format = resource->format;
1889 struct wined3d_texture_sub_resource *sub_resource;
1890 struct wined3d_device *device = resource->device;
1891 unsigned int fmt_flags = resource->format_flags;
1892 struct wined3d_context *context = NULL;
1893 struct wined3d_texture *texture;
1894 struct wined3d_bo_address data;
1895 unsigned int texture_level;
1896 BYTE *base_memory;
1897 BOOL ret;
1899 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1900 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1902 texture = texture_from_resource(resource);
1903 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1904 return E_INVALIDARG;
1906 texture_level = sub_resource_idx % texture->level_count;
1907 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
1909 WARN("Map box is invalid.\n");
1910 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
1911 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1912 return WINED3DERR_INVALIDCALL;
1915 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1917 WARN("DC is in use.\n");
1918 return WINED3DERR_INVALIDCALL;
1921 if (sub_resource->map_count)
1923 WARN("Sub-resource is already mapped.\n");
1924 return WINED3DERR_INVALIDCALL;
1927 if (device->d3d_initialized)
1928 context = context_acquire(device, NULL, 0);
1930 if (flags & WINED3D_MAP_DISCARD)
1932 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1933 wined3d_debug_location(resource->map_binding));
1934 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
1935 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
1937 else
1939 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1940 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1941 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
1944 if (!ret)
1946 ERR("Failed to prepare location.\n");
1947 context_release(context);
1948 return E_OUTOFMEMORY;
1951 if (flags & WINED3D_MAP_WRITE
1952 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
1953 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
1955 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
1956 base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
1957 TRACE("Base memory pointer %p.\n", base_memory);
1959 if (context)
1960 context_release(context);
1962 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1964 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1965 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1967 else
1969 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1972 if (!box)
1974 map_desc->data = base_memory;
1976 else
1978 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1980 /* Compressed textures are block based, so calculate the offset of
1981 * the block that contains the top-left pixel of the mapped box. */
1982 map_desc->data = base_memory
1983 + (box->front * map_desc->slice_pitch)
1984 + ((box->top / format->block_height) * map_desc->row_pitch)
1985 + ((box->left / format->block_width) * format->block_byte_count);
1987 else
1989 map_desc->data = base_memory
1990 + (box->front * map_desc->slice_pitch)
1991 + (box->top * map_desc->row_pitch)
1992 + (box->left * format->byte_count);
1996 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1998 RECT *r = &texture->swapchain->front_buffer_update;
2000 if (!box)
2001 SetRect(r, 0, 0, resource->width, resource->height);
2002 else
2003 SetRect(r, box->left, box->top, box->right, box->bottom);
2004 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
2007 ++resource->map_count;
2008 ++sub_resource->map_count;
2010 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2011 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2013 return WINED3D_OK;
2016 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
2018 struct wined3d_texture_sub_resource *sub_resource;
2019 struct wined3d_device *device = resource->device;
2020 struct wined3d_context *context = NULL;
2021 struct wined3d_texture *texture;
2022 struct wined3d_bo_address data;
2024 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
2026 texture = texture_from_resource(resource);
2027 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2028 return E_INVALIDARG;
2030 if (!sub_resource->map_count)
2032 WARN("Trying to unmap unmapped sub-resource.\n");
2033 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2034 return WINED3D_OK;
2035 return WINEDDERR_NOTLOCKED;
2038 if (device->d3d_initialized)
2039 context = context_acquire(device, NULL, 0);
2041 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2042 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2044 if (context)
2045 context_release(context);
2047 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2049 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2050 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2053 --sub_resource->map_count;
2054 if (!--resource->map_count && texture->update_map_binding)
2055 wined3d_texture_update_map_binding(texture);
2057 return WINED3D_OK;
2060 static const struct wined3d_resource_ops texture_resource_ops =
2062 texture_resource_incref,
2063 texture_resource_decref,
2064 texture_resource_preload,
2065 wined3d_texture_unload,
2066 texture_resource_sub_resource_map,
2067 texture_resource_sub_resource_unmap,
2070 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2071 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2072 void *parent, const struct wined3d_parent_ops *parent_ops, const struct wined3d_texture_ops *texture_ops)
2074 struct wined3d_device_parent *device_parent = device->device_parent;
2075 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2076 unsigned int sub_count, i, j, size, offset = 0;
2077 unsigned int pow2_width, pow2_height;
2078 const struct wined3d_format *format;
2079 HRESULT hr;
2081 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2082 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2083 "flags %#x, device %p, parent %p, parent_ops %p, texture_ops %p.\n",
2084 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
2085 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
2086 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
2087 layer_count, level_count, flags, device, parent, parent_ops, texture_ops);
2089 if (!desc->width || !desc->height || !desc->depth)
2090 return WINED3DERR_INVALIDCALL;
2092 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2094 if (layer_count != 1)
2096 ERR("Invalid layer count for volume texture.\n");
2097 return E_INVALIDARG;
2100 if (!gl_info->supported[EXT_TEXTURE3D])
2102 WARN("OpenGL implementation does not support 3D textures.\n");
2103 return WINED3DERR_INVALIDCALL;
2107 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2108 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2110 WARN("OpenGL implementation does not support array textures.\n");
2111 return WINED3DERR_INVALIDCALL;
2114 /* TODO: It should only be possible to create textures for formats
2115 * that are reported as supported. */
2116 if (WINED3DFMT_UNKNOWN >= desc->format)
2118 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2119 return WINED3DERR_INVALIDCALL;
2122 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
2123 || desc->usage & WINED3DUSAGE_SCRATCH))
2125 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2126 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
2127 return WINED3DERR_INVALIDCALL;
2130 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2131 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2132 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2133 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->access & WINED3D_RESOURCE_ACCESS_CPU)
2134 FIXME("Trying to create a CPU accessible render target.\n");
2136 pow2_width = desc->width;
2137 pow2_height = desc->height;
2138 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
2139 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2141 /* level_count == 0 returns an error as well. */
2142 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2144 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
2146 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2147 "texture without unconditional NPOT support.\n");
2148 return WINED3DERR_INVALIDCALL;
2151 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2153 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2155 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !gl_info->supported[ARB_TEXTURE_RECTANGLE]
2156 && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
2158 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format, desc->usage);
2160 /* TODO: Add support for non-power-of-two compressed textures. */
2161 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2162 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2164 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2165 desc->width, desc->height);
2166 return WINED3DERR_NOTAVAILABLE;
2169 /* Find the nearest pow2 match. */
2170 pow2_width = pow2_height = 1;
2171 while (pow2_width < desc->width)
2172 pow2_width <<= 1;
2173 while (pow2_height < desc->height)
2174 pow2_height <<= 1;
2175 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2178 texture->pow2_width = pow2_width;
2179 texture->pow2_height = pow2_height;
2181 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
2182 && (desc->usage & WINED3DUSAGE_TEXTURE))
2184 /* One of four options:
2185 * 1: Do the same as we do with NPOT and scale the texture. (Any
2186 * texture ops would require the texture to be scaled which is
2187 * potentially slow.)
2188 * 2: Set the texture to the maximum size (bad idea).
2189 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2190 * 4: Create the surface, but allow it to be used only for DirectDraw
2191 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2192 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2193 * the render target. */
2194 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
2196 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2197 return WINED3DERR_NOTAVAILABLE;
2200 /* We should never use this surface in combination with OpenGL. */
2201 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2204 format = wined3d_get_format(&device->adapter->gl_info, desc->format, desc->usage);
2205 for (i = 0; i < layer_count; ++i)
2207 for (j = 0; j < level_count; ++j)
2209 unsigned int idx = i * level_count + j;
2211 size = wined3d_format_calculate_size(format, device->surface_alignment,
2212 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
2213 texture->sub_resources[idx].offset = offset;
2214 texture->sub_resources[idx].size = size;
2215 offset += size;
2217 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
2220 if (!offset)
2221 return WINED3DERR_INVALIDCALL;
2223 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
2224 desc->multisample_type, desc->multisample_quality, desc->usage, desc->access,
2225 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
2227 static unsigned int once;
2229 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
2230 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
2231 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
2232 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
2233 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
2234 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
2236 WARN("Failed to initialize resource, returning %#x\n", hr);
2237 return hr;
2239 wined3d_resource_update_draw_binding(&texture->resource);
2240 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
2241 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
2243 texture->texture_ops = texture_ops;
2245 texture->layer_count = layer_count;
2246 texture->level_count = level_count;
2247 texture->lod = 0;
2248 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
2249 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
2250 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
2251 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
2252 texture->flags |= WINED3D_TEXTURE_GET_DC;
2253 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
2254 texture->flags |= WINED3D_TEXTURE_DISCARD;
2255 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
2257 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
2258 WARN("Format doesn't support mipmaps generation, "
2259 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
2260 else
2261 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
2264 list_init(&texture->renderbuffers);
2266 /* Precalculated scaling for 'faked' non power of two texture coords. */
2267 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
2269 texture->pow2_matrix[0] = (float)desc->width;
2270 texture->pow2_matrix[5] = (float)desc->height;
2271 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
2272 texture->target = GL_TEXTURE_RECTANGLE_ARB;
2274 else
2276 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2278 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2279 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2280 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2282 else
2284 texture->pow2_matrix[0] = 1.0f;
2285 texture->pow2_matrix[5] = 1.0f;
2287 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2289 texture->target = GL_TEXTURE_3D;
2291 else if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2293 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2295 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
2297 if (layer_count > 1)
2298 texture->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
2299 else
2300 texture->target = GL_TEXTURE_2D_MULTISAMPLE;
2302 else
2304 if (layer_count > 1)
2305 texture->target = GL_TEXTURE_2D_ARRAY;
2306 else
2307 texture->target = GL_TEXTURE_2D;
2310 texture->pow2_matrix[10] = 1.0f;
2311 texture->pow2_matrix[15] = 1.0f;
2312 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2314 if (wined3d_texture_use_pbo(texture, gl_info))
2316 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D
2317 || (texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2318 wined3d_resource_free_sysmem(&texture->resource);
2319 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2322 sub_count = level_count * layer_count;
2323 if (sub_count / layer_count != level_count)
2325 wined3d_texture_cleanup_sync(texture);
2326 return E_OUTOFMEMORY;
2329 if (desc->usage & WINED3DUSAGE_OVERLAY)
2331 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
2333 wined3d_texture_cleanup_sync(texture);
2334 return E_OUTOFMEMORY;
2337 for (i = 0; i < sub_count; ++i)
2339 list_init(&texture->overlay_info[i].entry);
2340 list_init(&texture->overlay_info[i].overlays);
2344 /* Generate all sub-resources. */
2345 for (i = 0; i < sub_count; ++i)
2347 struct wined3d_texture_sub_resource *sub_resource;
2349 sub_resource = &texture->sub_resources[i];
2350 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2351 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
2352 && !(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2354 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
2355 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
2358 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
2359 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2361 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
2362 sub_resource->parent = NULL;
2363 wined3d_texture_cleanup_sync(texture);
2364 return hr;
2367 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2369 TRACE("Created sub-resource %u (level %u, layer %u).\n",
2370 i, i % texture->level_count, i / texture->level_count);
2372 if ((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2374 struct wined3d_texture_idx texture_idx = {texture, i};
2376 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
2377 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
2378 if (!texture->dc_info || !texture->dc_info[i].dc)
2380 wined3d_texture_cleanup_sync(texture);
2381 return WINED3DERR_INVALIDCALL;
2386 return WINED3D_OK;
2389 /* This call just uploads data, the caller is responsible for binding the
2390 * correct texture. */
2391 /* Context activation is done by the caller. */
2392 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2393 const struct wined3d_context *context, const struct wined3d_format *format, const struct wined3d_box *src_box,
2394 const struct wined3d_const_bo_address *data, unsigned int row_pitch, unsigned int slice_pitch,
2395 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, BOOL srgb)
2397 unsigned int level = sub_resource_idx % texture->level_count;
2398 const struct wined3d_gl_info *gl_info = context->gl_info;
2399 unsigned int dst_row_pitch, dst_slice_pitch;
2400 unsigned int update_w, update_h, update_d;
2401 const BYTE *addr = data->addr;
2402 void *converted_mem = NULL;
2404 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data {%#x:%p}, "
2405 "row_pitch %#x, slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
2406 texture, sub_resource_idx, context, debug_d3dformat(format->id), debug_box(src_box),
2407 data->buffer_object, data->addr, row_pitch, slice_pitch, dst_x, dst_y, dst_z, srgb);
2409 if (src_box)
2411 addr += src_box->front * slice_pitch;
2412 addr += src_box->top * row_pitch;
2413 addr += src_box->left * format->byte_count;
2415 update_w = src_box->right - src_box->left;
2416 update_h = src_box->bottom - src_box->top;
2417 update_d = src_box->back - src_box->front;
2419 else
2421 update_w = wined3d_texture_get_level_width(texture, level);
2422 update_h = wined3d_texture_get_level_height(texture, level);
2423 update_d = wined3d_texture_get_level_depth(texture, level);
2426 if (format->conv_byte_count)
2428 if (data->buffer_object)
2429 ERR("Loading a converted texture from a PBO.\n");
2430 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2431 ERR("Converting a block-based format.\n");
2433 dst_row_pitch = update_w * format->conv_byte_count;
2434 dst_slice_pitch = dst_row_pitch * update_h;
2436 converted_mem = heap_calloc(update_d, dst_slice_pitch);
2437 format->upload(addr, converted_mem, row_pitch, slice_pitch,
2438 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2439 addr = converted_mem;
2441 else
2443 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2444 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2445 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2448 if (data->buffer_object)
2450 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2451 checkGLcall("glBindBuffer");
2454 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, dst_x, dst_y, dst_z,
2455 update_w, update_h, update_d, format->glFormat, format->glType, addr));
2456 checkGLcall("glTexSubImage3D");
2458 if (data->buffer_object)
2460 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2461 checkGLcall("glBindBuffer");
2464 heap_free(converted_mem);
2467 /* Context activation is done by the caller. */
2468 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2469 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2471 const struct wined3d_format *format = texture->resource.format;
2472 const struct wined3d_gl_info *gl_info = context->gl_info;
2474 if (format->conv_byte_count)
2476 FIXME("Attempting to download a converted volume, format %s.\n",
2477 debug_d3dformat(format->id));
2478 return;
2481 if (data->buffer_object)
2483 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2484 checkGLcall("glBindBuffer");
2487 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2488 format->glFormat, format->glType, data->addr);
2489 checkGLcall("glGetTexImage");
2491 if (data->buffer_object)
2493 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2494 checkGLcall("glBindBuffer");
2499 /* Context activation is done by the caller. */
2500 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2501 struct wined3d_context *context, BOOL dest_is_srgb)
2503 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2504 unsigned int row_pitch, slice_pitch;
2505 struct wined3d_bo_address data;
2507 /* Optimisations are possible, but the effort should be put into either
2508 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2509 * picked the wrong copy for the original upload and fixing that.
2511 * Also keep in mind that we want to avoid using resource.heap_memory
2512 * for DEFAULT pool surfaces. */
2513 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2514 data.buffer_object = 0;
2515 if (!(data.addr = heap_alloc(sub_resource->size)))
2516 return;
2518 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2519 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2520 texture3d_download_data(texture, sub_resource_idx, context, &data);
2521 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2522 texture3d_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2523 NULL, wined3d_const_bo_address(&data), row_pitch, slice_pitch, 0, 0, 0, FALSE);
2525 heap_free(data.addr);
2528 /* Context activation is done by the caller. */
2529 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2530 struct wined3d_context *context, DWORD location)
2532 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2533 unsigned int row_pitch, slice_pitch;
2535 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2536 return FALSE;
2538 switch (location)
2540 case WINED3D_LOCATION_TEXTURE_RGB:
2541 case WINED3D_LOCATION_TEXTURE_SRGB:
2542 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2544 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2545 data.addr += sub_resource->offset;
2546 wined3d_texture_bind_and_dirtify(texture, context,
2547 location == WINED3D_LOCATION_TEXTURE_SRGB);
2548 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2549 texture3d_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2550 NULL, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2552 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2554 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2555 wined3d_texture_bind_and_dirtify(texture, context,
2556 location == WINED3D_LOCATION_TEXTURE_SRGB);
2557 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2558 texture3d_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2559 NULL, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2561 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2563 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2565 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2567 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2569 else
2571 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2572 return FALSE;
2574 break;
2576 case WINED3D_LOCATION_SYSMEM:
2577 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2579 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2581 data.addr += sub_resource->offset;
2582 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2583 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2584 else
2585 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2587 texture3d_download_data(texture, sub_resource_idx, context, &data);
2588 ++texture->download_count;
2590 else
2592 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2593 wined3d_debug_location(sub_resource->locations));
2594 return FALSE;
2596 break;
2598 case WINED3D_LOCATION_BUFFER:
2599 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2601 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
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);
2610 else
2612 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2613 wined3d_debug_location(sub_resource->locations));
2614 return FALSE;
2616 break;
2618 default:
2619 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2620 wined3d_debug_location(sub_resource->locations));
2621 return FALSE;
2624 return TRUE;
2627 static const struct wined3d_texture_ops texture3d_ops =
2629 texture3d_upload_data,
2630 texture3d_load_location,
2633 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2634 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2635 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2637 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
2638 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
2639 unsigned int dst_format_flags, src_format_flags = 0;
2640 HRESULT hr;
2642 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2643 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2644 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2645 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2647 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
2648 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2649 return WINED3DERR_INVALIDCALL;
2651 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
2652 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2653 return WINED3DERR_INVALIDCALL;
2655 dst_format_flags = dst_texture->resource.format_flags;
2656 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
2657 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
2658 return hr;
2660 src_format_flags = src_texture->resource.format_flags;
2661 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
2662 src_sub_resource_idx % src_texture->level_count, &src_box)))
2663 return hr;
2665 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
2666 || src_texture->sub_resources[src_sub_resource_idx].map_count)
2668 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2669 return WINEDDERR_SURFACEBUSY;
2672 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2673 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
2675 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2676 return WINED3DERR_INVALIDCALL;
2679 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
2680 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
2682 return WINED3D_OK;
2685 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2686 unsigned int sub_resource_idx, LONG *x, LONG *y)
2688 struct wined3d_overlay_info *overlay;
2690 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2692 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
2693 || sub_resource_idx >= texture->level_count * texture->layer_count)
2695 WARN("Invalid sub-resource specified.\n");
2696 return WINEDDERR_NOTAOVERLAYSURFACE;
2699 overlay = &texture->overlay_info[sub_resource_idx];
2700 if (!overlay->dst_texture)
2702 TRACE("Overlay not visible.\n");
2703 *x = 0;
2704 *y = 0;
2705 return WINEDDERR_OVERLAYNOTVISIBLE;
2708 *x = overlay->dst_rect.left;
2709 *y = overlay->dst_rect.top;
2711 TRACE("Returning position %d, %d.\n", *x, *y);
2713 return WINED3D_OK;
2716 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2717 unsigned int sub_resource_idx, LONG x, LONG y)
2719 struct wined3d_overlay_info *overlay;
2720 LONG w, h;
2722 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2724 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
2725 || sub_resource_idx >= texture->level_count * texture->layer_count)
2727 WARN("Invalid sub-resource specified.\n");
2728 return WINEDDERR_NOTAOVERLAYSURFACE;
2731 overlay = &texture->overlay_info[sub_resource_idx];
2732 w = overlay->dst_rect.right - overlay->dst_rect.left;
2733 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
2734 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
2736 return WINED3D_OK;
2739 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2740 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2741 const RECT *dst_rect, DWORD flags)
2743 struct wined3d_overlay_info *overlay;
2744 unsigned int level, dst_level;
2746 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2747 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2748 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2749 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2751 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2752 || sub_resource_idx >= texture->level_count * texture->layer_count)
2754 WARN("Invalid sub-resource specified.\n");
2755 return WINEDDERR_NOTAOVERLAYSURFACE;
2758 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2759 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
2761 WARN("Invalid destination sub-resource specified.\n");
2762 return WINED3DERR_INVALIDCALL;
2765 overlay = &texture->overlay_info[sub_resource_idx];
2767 level = sub_resource_idx % texture->level_count;
2768 if (src_rect)
2769 overlay->src_rect = *src_rect;
2770 else
2771 SetRect(&overlay->src_rect, 0, 0,
2772 wined3d_texture_get_level_width(texture, level),
2773 wined3d_texture_get_level_height(texture, level));
2775 dst_level = dst_sub_resource_idx % dst_texture->level_count;
2776 if (dst_rect)
2777 overlay->dst_rect = *dst_rect;
2778 else
2779 SetRect(&overlay->dst_rect, 0, 0,
2780 wined3d_texture_get_level_width(dst_texture, dst_level),
2781 wined3d_texture_get_level_height(dst_texture, dst_level));
2783 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
2784 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
2786 overlay->dst_texture = NULL;
2787 list_remove(&overlay->entry);
2790 if (flags & WINEDDOVER_SHOW)
2792 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
2794 overlay->dst_texture = dst_texture;
2795 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
2796 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
2799 else if (flags & WINEDDOVER_HIDE)
2801 /* Tests show that the rectangles are erased on hide. */
2802 SetRectEmpty(&overlay->src_rect);
2803 SetRectEmpty(&overlay->dst_rect);
2804 overlay->dst_texture = NULL;
2807 return WINED3D_OK;
2810 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2812 unsigned int sub_count = texture->level_count * texture->layer_count;
2814 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2816 if (sub_resource_idx >= sub_count)
2818 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2819 return NULL;
2822 return texture->sub_resources[sub_resource_idx].parent;
2825 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2826 unsigned int sub_resource_idx, void *parent)
2828 unsigned int sub_count = texture->level_count * texture->layer_count;
2830 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2832 if (sub_resource_idx >= sub_count)
2834 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2835 return;
2838 texture->sub_resources[sub_resource_idx].parent = parent;
2841 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2842 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2844 unsigned int sub_count = texture->level_count * texture->layer_count;
2845 const struct wined3d_resource *resource;
2846 unsigned int level_idx;
2848 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2850 if (sub_resource_idx >= sub_count)
2852 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2853 return WINED3DERR_INVALIDCALL;
2856 resource = &texture->resource;
2857 desc->format = resource->format->id;
2858 desc->multisample_type = resource->multisample_type;
2859 desc->multisample_quality = resource->multisample_quality;
2860 desc->usage = resource->usage;
2861 desc->access = resource->access;
2863 level_idx = sub_resource_idx % texture->level_count;
2864 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2865 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2866 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2867 desc->size = texture->sub_resources[sub_resource_idx].size;
2869 return WINED3D_OK;
2872 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2873 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2874 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2876 struct wined3d_texture *object;
2877 HRESULT hr;
2879 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2880 "parent %p, parent_ops %p, texture %p.\n",
2881 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2883 if (!layer_count)
2885 WARN("Invalid layer count.\n");
2886 return E_INVALIDARG;
2888 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2890 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2891 layer_count = 6;
2894 if (!level_count)
2896 WARN("Invalid level count.\n");
2897 return WINED3DERR_INVALIDCALL;
2900 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2902 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info,
2903 desc->format, desc->usage);
2905 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2906 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2908 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2909 desc->multisample_quality);
2910 return WINED3DERR_NOTAVAILABLE;
2912 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2913 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2914 || desc->multisample_quality))
2916 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2917 desc->multisample_quality);
2918 return WINED3DERR_NOTAVAILABLE;
2922 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture,
2923 sub_resources[level_count * layer_count]))))
2924 return E_OUTOFMEMORY;
2926 switch (desc->resource_type)
2928 case WINED3D_RTYPE_TEXTURE_2D:
2929 hr = wined3d_texture_init(object, desc, layer_count, level_count,
2930 flags, device, parent, parent_ops, &texture2d_ops);
2931 break;
2933 case WINED3D_RTYPE_TEXTURE_3D:
2934 hr = wined3d_texture_init(object, desc, layer_count, level_count,
2935 flags, device, parent, parent_ops, &texture3d_ops);
2936 break;
2938 default:
2939 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2940 hr = WINED3DERR_INVALIDCALL;
2941 break;
2944 if (FAILED(hr))
2946 WARN("Failed to initialize texture, returning %#x.\n", hr);
2947 heap_free(object);
2948 return hr;
2951 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2952 * in this case. */
2953 if (data)
2955 unsigned int sub_count = level_count * layer_count;
2956 unsigned int i;
2958 for (i = 0; i < sub_count; ++i)
2960 if (!data[i].data)
2962 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2963 wined3d_texture_cleanup_sync(object);
2964 heap_free(object);
2965 return E_INVALIDARG;
2969 for (i = 0; i < sub_count; ++i)
2971 wined3d_device_update_sub_resource(device, &object->resource,
2972 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
2976 TRACE("Created texture %p.\n", object);
2977 *texture = object;
2979 return WINED3D_OK;
2982 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2984 struct wined3d_device *device = texture->resource.device;
2985 struct wined3d_texture_sub_resource *sub_resource;
2986 struct wined3d_dc_info *dc_info;
2988 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2990 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
2992 WARN("Texture does not support GetDC\n");
2993 /* Don't touch the DC */
2994 return WINED3DERR_INVALIDCALL;
2997 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2998 return WINED3DERR_INVALIDCALL;
3000 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3002 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3003 return WINED3DERR_INVALIDCALL;
3006 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3007 return WINED3DERR_INVALIDCALL;
3009 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3011 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3013 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3014 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3015 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3016 return WINED3DERR_INVALIDCALL;
3019 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3020 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3021 ++texture->resource.map_count;
3022 ++sub_resource->map_count;
3024 *dc = dc_info[sub_resource_idx].dc;
3025 TRACE("Returning dc %p.\n", *dc);
3027 return WINED3D_OK;
3030 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3032 struct wined3d_device *device = texture->resource.device;
3033 struct wined3d_texture_sub_resource *sub_resource;
3034 struct wined3d_dc_info *dc_info;
3036 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3038 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3039 return WINED3DERR_INVALIDCALL;
3041 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3043 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3044 return WINED3DERR_INVALIDCALL;
3047 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3048 return WINED3DERR_INVALIDCALL;
3050 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
3052 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3053 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
3054 return WINED3DERR_INVALIDCALL;
3057 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
3059 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3061 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
3062 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3065 --sub_resource->map_count;
3066 if (!--texture->resource.map_count && texture->update_map_binding)
3067 wined3d_texture_update_map_binding(texture);
3068 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3069 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3071 return WINED3D_OK;