wined3d: Move the depth renderbuffers from struct wined3d_texture to struct wined3d_t...
[wine.git] / dlls / wined3d / texture.c
blob74029bfb3d87228c811c897e8553c00af908e741
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 struct wined3d_rect_f
41 float l;
42 float t;
43 float r;
44 float b;
47 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
49 if (!gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
50 || texture->resource.format->conv_byte_count
51 || (texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED)))
52 return FALSE;
54 /* Use a PBO for dynamic textures and read-only staging textures. */
55 return (!(texture->resource.access & WINED3D_RESOURCE_ACCESS_CPU)
56 && texture->resource.usage & WINED3DUSAGE_DYNAMIC)
57 || texture->resource.access == (WINED3D_RESOURCE_ACCESS_CPU | WINED3D_RESOURCE_ACCESS_MAP_R);
60 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
61 const struct wined3d_gl_info *gl_info)
63 /* We don't expect to create texture views for textures with height-scaled formats.
64 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
65 return gl_info->supported[ARB_TEXTURE_STORAGE]
66 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
69 /* Front buffer coordinates are always full screen coordinates, but our GL
70 * drawable is limited to the window's client area. The sysmem and texture
71 * copies do have the full screen size. Note that GL has a bottom-left
72 * origin, while D3D has a top-left origin. */
73 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture *texture, HWND window, RECT *rect)
75 unsigned int drawable_height;
76 POINT offset = {0, 0};
77 RECT windowsize;
79 if (!texture->swapchain)
80 return;
82 if (texture == texture->swapchain->front_buffer)
84 ScreenToClient(window, &offset);
85 OffsetRect(rect, offset.x, offset.y);
88 GetClientRect(window, &windowsize);
89 drawable_height = windowsize.bottom - windowsize.top;
91 rect->top = drawable_height - rect->top;
92 rect->bottom = drawable_height - rect->bottom;
95 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
97 const struct wined3d_swapchain *swapchain = texture->swapchain;
99 TRACE("texture %p.\n", texture);
101 if (!swapchain)
103 ERR("Texture %p is not part of a swapchain.\n", texture);
104 return GL_NONE;
107 if (texture == swapchain->front_buffer)
109 TRACE("Returning GL_FRONT.\n");
110 return GL_FRONT;
113 if (texture == swapchain->back_buffers[0])
115 TRACE("Returning GL_BACK.\n");
116 return GL_BACK;
119 FIXME("Higher back buffer, returning GL_BACK.\n");
120 return GL_BACK;
123 static DWORD wined3d_resource_access_from_location(DWORD location)
125 switch (location)
127 case WINED3D_LOCATION_DISCARDED:
128 return 0;
130 case WINED3D_LOCATION_SYSMEM:
131 case WINED3D_LOCATION_USER_MEMORY:
132 return WINED3D_RESOURCE_ACCESS_CPU;
134 case WINED3D_LOCATION_BUFFER:
135 case WINED3D_LOCATION_DRAWABLE:
136 case WINED3D_LOCATION_TEXTURE_RGB:
137 case WINED3D_LOCATION_TEXTURE_SRGB:
138 case WINED3D_LOCATION_RB_MULTISAMPLE:
139 case WINED3D_LOCATION_RB_RESOLVED:
140 return WINED3D_RESOURCE_ACCESS_GPU;
142 default:
143 FIXME("Unhandled location %#x.\n", location);
144 return 0;
148 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct wined3d_rect_f *f)
150 f->l = ((r->left * 2.0f) / w) - 1.0f;
151 f->t = ((r->top * 2.0f) / h) - 1.0f;
152 f->r = ((r->right * 2.0f) / w) - 1.0f;
153 f->b = ((r->bottom * 2.0f) / h) - 1.0f;
156 void texture2d_get_blt_info(const struct wined3d_texture *texture,
157 unsigned int sub_resource_idx, const RECT *rect, struct wined3d_blt_info *info)
159 struct wined3d_vec3 *coords = info->texcoords;
160 struct wined3d_rect_f f;
161 unsigned int level;
162 GLenum target;
163 GLsizei w, h;
165 level = sub_resource_idx % texture->level_count;
166 w = wined3d_texture_get_level_pow2_width(texture, level);
167 h = wined3d_texture_get_level_pow2_height(texture, level);
168 target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
170 switch (target)
172 default:
173 FIXME("Unsupported texture target %#x.\n", target);
174 /* Fall back to GL_TEXTURE_2D */
175 case GL_TEXTURE_2D:
176 info->bind_target = GL_TEXTURE_2D;
177 coords[0].x = (float)rect->left / w;
178 coords[0].y = (float)rect->top / h;
179 coords[0].z = 0.0f;
181 coords[1].x = (float)rect->right / w;
182 coords[1].y = (float)rect->top / h;
183 coords[1].z = 0.0f;
185 coords[2].x = (float)rect->left / w;
186 coords[2].y = (float)rect->bottom / h;
187 coords[2].z = 0.0f;
189 coords[3].x = (float)rect->right / w;
190 coords[3].y = (float)rect->bottom / h;
191 coords[3].z = 0.0f;
192 break;
194 case GL_TEXTURE_RECTANGLE_ARB:
195 info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
196 coords[0].x = rect->left; coords[0].y = rect->top; coords[0].z = 0.0f;
197 coords[1].x = rect->right; coords[1].y = rect->top; coords[1].z = 0.0f;
198 coords[2].x = rect->left; coords[2].y = rect->bottom; coords[2].z = 0.0f;
199 coords[3].x = rect->right; coords[3].y = rect->bottom; coords[3].z = 0.0f;
200 break;
202 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
203 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
204 cube_coords_float(rect, w, h, &f);
206 coords[0].x = 1.0f; coords[0].y = -f.t; coords[0].z = -f.l;
207 coords[1].x = 1.0f; coords[1].y = -f.t; coords[1].z = -f.r;
208 coords[2].x = 1.0f; coords[2].y = -f.b; coords[2].z = -f.l;
209 coords[3].x = 1.0f; coords[3].y = -f.b; coords[3].z = -f.r;
210 break;
212 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
213 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
214 cube_coords_float(rect, w, h, &f);
216 coords[0].x = -1.0f; coords[0].y = -f.t; coords[0].z = f.l;
217 coords[1].x = -1.0f; coords[1].y = -f.t; coords[1].z = f.r;
218 coords[2].x = -1.0f; coords[2].y = -f.b; coords[2].z = f.l;
219 coords[3].x = -1.0f; coords[3].y = -f.b; coords[3].z = f.r;
220 break;
222 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
223 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
224 cube_coords_float(rect, w, h, &f);
226 coords[0].x = f.l; coords[0].y = 1.0f; coords[0].z = f.t;
227 coords[1].x = f.r; coords[1].y = 1.0f; coords[1].z = f.t;
228 coords[2].x = f.l; coords[2].y = 1.0f; coords[2].z = f.b;
229 coords[3].x = f.r; coords[3].y = 1.0f; coords[3].z = f.b;
230 break;
232 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
233 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
234 cube_coords_float(rect, w, h, &f);
236 coords[0].x = f.l; coords[0].y = -1.0f; coords[0].z = -f.t;
237 coords[1].x = f.r; coords[1].y = -1.0f; coords[1].z = -f.t;
238 coords[2].x = f.l; coords[2].y = -1.0f; coords[2].z = -f.b;
239 coords[3].x = f.r; coords[3].y = -1.0f; coords[3].z = -f.b;
240 break;
242 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
243 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
244 cube_coords_float(rect, w, h, &f);
246 coords[0].x = f.l; coords[0].y = -f.t; coords[0].z = 1.0f;
247 coords[1].x = f.r; coords[1].y = -f.t; coords[1].z = 1.0f;
248 coords[2].x = f.l; coords[2].y = -f.b; coords[2].z = 1.0f;
249 coords[3].x = f.r; coords[3].y = -f.b; coords[3].z = 1.0f;
250 break;
252 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
253 info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
254 cube_coords_float(rect, w, h, &f);
256 coords[0].x = -f.l; coords[0].y = -f.t; coords[0].z = -1.0f;
257 coords[1].x = -f.r; coords[1].y = -f.t; coords[1].z = -1.0f;
258 coords[2].x = -f.l; coords[2].y = -f.b; coords[2].z = -1.0f;
259 coords[3].x = -f.r; coords[3].y = -f.b; coords[3].z = -1.0f;
260 break;
264 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
266 struct wined3d_texture_sub_resource *sub_resource;
267 unsigned int i, sub_count;
269 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
270 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
272 TRACE("Not evicting system memory for texture %p.\n", texture);
273 return;
276 TRACE("Evicting system memory for texture %p.\n", texture);
278 sub_count = texture->level_count * texture->layer_count;
279 for (i = 0; i < sub_count; ++i)
281 sub_resource = &texture->sub_resources[i];
282 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
283 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
284 i, texture);
285 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
287 wined3d_resource_free_sysmem(&texture->resource);
290 void wined3d_texture_validate_location(struct wined3d_texture *texture,
291 unsigned int sub_resource_idx, DWORD location)
293 struct wined3d_texture_sub_resource *sub_resource;
294 DWORD previous_locations;
296 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
297 texture, sub_resource_idx, wined3d_debug_location(location));
299 sub_resource = &texture->sub_resources[sub_resource_idx];
300 previous_locations = sub_resource->locations;
301 sub_resource->locations |= location;
302 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
303 && !--texture->sysmem_count)
304 wined3d_texture_evict_sysmem(texture);
306 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
309 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
311 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
314 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
315 unsigned int sub_resource_idx, DWORD location)
317 struct wined3d_texture_sub_resource *sub_resource;
318 DWORD previous_locations;
320 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
321 texture, sub_resource_idx, wined3d_debug_location(location));
323 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
324 wined3d_texture_set_dirty(texture);
326 sub_resource = &texture->sub_resources[sub_resource_idx];
327 previous_locations = sub_resource->locations;
328 sub_resource->locations &= ~location;
329 if (previous_locations != WINED3D_LOCATION_SYSMEM && sub_resource->locations == WINED3D_LOCATION_SYSMEM)
330 ++texture->sysmem_count;
332 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
334 if (!sub_resource->locations)
335 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
336 sub_resource_idx, texture);
339 static BOOL wined3d_texture_copy_sysmem_location(struct wined3d_texture *texture,
340 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
342 unsigned int size = texture->sub_resources[sub_resource_idx].size;
343 struct wined3d_device *device = texture->resource.device;
344 const struct wined3d_gl_info *gl_info;
345 struct wined3d_bo_address dst, src;
347 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
348 return FALSE;
350 wined3d_texture_get_memory(texture, sub_resource_idx, &dst, location);
351 wined3d_texture_get_memory(texture, sub_resource_idx, &src,
352 texture->sub_resources[sub_resource_idx].locations);
354 if (dst.buffer_object)
356 context = context_acquire(device, NULL, 0);
357 gl_info = context->gl_info;
358 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dst.buffer_object));
359 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER, 0, size, src.addr));
360 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
361 checkGLcall("PBO upload");
362 context_release(context);
363 return TRUE;
366 if (src.buffer_object)
368 context = context_acquire(device, NULL, 0);
369 gl_info = context->gl_info;
370 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, src.buffer_object));
371 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER, 0, size, dst.addr));
372 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
373 checkGLcall("PBO download");
374 context_release(context);
375 return TRUE;
378 memcpy(dst.addr, src.addr, size);
379 return TRUE;
382 /* Context activation is done by the caller. Context may be NULL in
383 * WINED3D_NO3D mode. */
384 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
385 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
387 static const DWORD sysmem_locations = WINED3D_LOCATION_SYSMEM | WINED3D_LOCATION_USER_MEMORY
388 | WINED3D_LOCATION_BUFFER;
389 DWORD current = texture->sub_resources[sub_resource_idx].locations;
390 BOOL ret;
392 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
393 texture, sub_resource_idx, context, wined3d_debug_location(location));
395 TRACE("Current resource location %s.\n", wined3d_debug_location(current));
397 if (current & location)
399 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location));
400 return TRUE;
403 if (WARN_ON(d3d))
405 DWORD required_access = wined3d_resource_access_from_location(location);
406 if ((texture->resource.access & required_access) != required_access)
407 WARN("Operation requires %#x access, but texture only has %#x.\n",
408 required_access, texture->resource.access);
411 if (current & WINED3D_LOCATION_DISCARDED)
413 TRACE("Sub-resource previously discarded, nothing to do.\n");
414 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
415 return FALSE;
416 wined3d_texture_validate_location(texture, sub_resource_idx, location);
417 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
418 return TRUE;
421 if (!current)
423 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
424 sub_resource_idx, texture);
425 wined3d_texture_validate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
426 return wined3d_texture_load_location(texture, sub_resource_idx, context, location);
429 if ((location & sysmem_locations) && (current & sysmem_locations))
430 ret = wined3d_texture_copy_sysmem_location(texture, sub_resource_idx, context, location);
431 else
432 ret = texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
434 if (ret)
435 wined3d_texture_validate_location(texture, sub_resource_idx, location);
437 return ret;
440 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
441 struct wined3d_bo_address *data, DWORD locations)
443 struct wined3d_texture_sub_resource *sub_resource;
445 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
446 texture, sub_resource_idx, data, wined3d_debug_location(locations));
448 sub_resource = &texture->sub_resources[sub_resource_idx];
449 if (locations & WINED3D_LOCATION_BUFFER)
451 data->addr = NULL;
452 data->buffer_object = sub_resource->buffer_object;
453 return;
455 if (locations & WINED3D_LOCATION_USER_MEMORY)
457 data->addr = texture->user_memory;
458 data->buffer_object = 0;
459 return;
461 if (locations & WINED3D_LOCATION_SYSMEM)
463 data->addr = texture->resource.heap_memory;
464 data->addr += sub_resource->offset;
465 data->buffer_object = 0;
466 return;
469 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
470 data->addr = NULL;
471 data->buffer_object = 0;
474 /* Context activation is done by the caller. */
475 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
476 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
478 GLuint *buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
480 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
481 checkGLcall("glDeleteBuffers");
483 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
484 *buffer_object, texture, sub_resource_idx);
486 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
487 *buffer_object = 0;
490 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
492 unsigned int sub_count = texture->level_count * texture->layer_count;
493 const struct wined3d_device *device = texture->resource.device;
494 DWORD map_binding = texture->update_map_binding;
495 struct wined3d_context *context = NULL;
496 unsigned int i;
498 if (device->d3d_initialized)
499 context = context_acquire(device, NULL, 0);
501 for (i = 0; i < sub_count; ++i)
503 if (texture->sub_resources[i].locations == texture->resource.map_binding
504 && !wined3d_texture_load_location(texture, i, context, map_binding))
505 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
506 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
507 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
510 if (context)
511 context_release(context);
513 texture->resource.map_binding = map_binding;
514 texture->update_map_binding = 0;
517 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
519 texture->update_map_binding = map_binding;
520 if (!texture->resource.map_count)
521 wined3d_texture_update_map_binding(texture);
524 /* A GL context is provided by the caller */
525 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
526 struct gl_texture *tex)
528 context_gl_resource_released(device, tex->name, FALSE);
529 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
530 tex->name = 0;
533 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture *texture)
535 const struct wined3d_format *format = texture->resource.format;
537 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
538 * feature through type == MULTISAMPLE_XX and quality != 0. This could
539 * be mapped to GL_NV_framebuffer_multisample_coverage.
541 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
542 * (EQAA), but it does not have an equivalent OpenGL extension. */
544 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
545 * levels as the count of advertised multisample types for the texture
546 * format. */
547 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
549 unsigned int i, count = 0;
551 for (i = 0; i < sizeof(format->multisample_types) * CHAR_BIT; ++i)
553 if (format->multisample_types & 1u << i)
555 if (texture->resource.multisample_quality == count++)
556 break;
559 return i + 1;
562 return texture->resource.multisample_type;
565 /* Context activation is done by the caller. */
566 /* The caller is responsible for binding the correct texture. */
567 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
568 GLenum gl_internal_format, const struct wined3d_format_gl *format,
569 const struct wined3d_gl_info *gl_info)
571 unsigned int level, level_count, layer, layer_count;
572 GLsizei width, height, depth;
573 GLenum target;
575 level_count = texture->level_count;
576 if (texture->target == GL_TEXTURE_1D_ARRAY || texture->target == GL_TEXTURE_2D_ARRAY)
577 layer_count = 1;
578 else
579 layer_count = texture->layer_count;
581 for (layer = 0; layer < layer_count; ++layer)
583 target = wined3d_texture_get_sub_resource_target(texture, layer * level_count);
585 for (level = 0; level < level_count; ++level)
587 width = wined3d_texture_get_level_pow2_width(texture, level);
588 height = wined3d_texture_get_level_pow2_height(texture, level);
589 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
591 height *= format->f.height_scale.numerator;
592 height /= format->f.height_scale.denominator;
595 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
596 texture, layer, level, target, width, height);
598 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
600 depth = wined3d_texture_get_level_depth(texture, level);
601 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
602 target == GL_TEXTURE_2D_ARRAY ? texture->layer_count : depth, 0,
603 format->format, format->type, NULL));
604 checkGLcall("glTexImage3D");
606 else if (target == GL_TEXTURE_1D)
608 gl_info->gl_ops.gl.p_glTexImage1D(target, level, gl_internal_format,
609 width, 0, format->format, format->type, NULL);
611 else
613 gl_info->gl_ops.gl.p_glTexImage2D(target, level, gl_internal_format, width,
614 target == GL_TEXTURE_1D_ARRAY ? texture->layer_count : height, 0,
615 format->format, format->type, NULL);
616 checkGLcall("glTexImage2D");
622 /* Context activation is done by the caller. */
623 /* The caller is responsible for binding the correct texture. */
624 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
625 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
627 unsigned int samples = wined3d_texture_get_gl_sample_count(texture);
628 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
629 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
630 GLboolean standard_pattern = texture->resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
631 && texture->resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
633 switch (texture->target)
635 case GL_TEXTURE_3D:
636 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count,
637 gl_internal_format, width, height, wined3d_texture_get_level_depth(texture, 0)));
638 break;
639 case GL_TEXTURE_2D_ARRAY:
640 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count,
641 gl_internal_format, width, height, texture->layer_count));
642 break;
643 case GL_TEXTURE_2D_MULTISAMPLE:
644 GL_EXTCALL(glTexStorage2DMultisample(texture->target, samples,
645 gl_internal_format, width, height, standard_pattern));
646 break;
647 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
648 GL_EXTCALL(glTexStorage3DMultisample(texture->target, samples,
649 gl_internal_format, width, height, texture->layer_count, standard_pattern));
650 break;
651 case GL_TEXTURE_1D_ARRAY:
652 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count,
653 gl_internal_format, width, texture->layer_count));
654 break;
655 case GL_TEXTURE_1D:
656 GL_EXTCALL(glTexStorage1D(texture->target, texture->level_count, gl_internal_format, width));
657 break;
658 default:
659 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count,
660 gl_internal_format, width, height));
661 break;
664 checkGLcall("allocate immutable storage");
667 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
669 struct wined3d_device *device = texture->resource.device;
670 const struct wined3d_gl_info *gl_info = NULL;
671 struct wined3d_context *context = NULL;
673 if (texture->texture_rgb.name || texture->texture_srgb.name
674 || texture->rb_multisample || texture->rb_resolved)
676 context = context_acquire(device, NULL, 0);
677 gl_info = context->gl_info;
680 if (texture->texture_rgb.name)
681 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
683 if (texture->texture_srgb.name)
684 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
686 if (texture->rb_multisample)
688 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
689 context_gl_resource_released(device, texture->rb_multisample, TRUE);
690 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
691 texture->rb_multisample = 0;
694 if (texture->rb_resolved)
696 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
697 context_gl_resource_released(device, texture->rb_resolved, TRUE);
698 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
699 texture->rb_resolved = 0;
702 if (context) context_release(context);
704 wined3d_texture_set_dirty(texture);
706 resource_unload(&texture->resource);
709 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
711 unsigned int sub_count = texture->level_count * texture->layer_count;
712 struct wined3d_texture_sub_resource *sub_resource;
713 unsigned int i;
715 for (i = 0; i < sub_count; ++i)
717 sub_resource = &texture->sub_resources[i];
718 if (sub_resource->parent)
720 TRACE("sub-resource %u.\n", i);
721 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
722 sub_resource->parent = NULL;
727 static void wined3d_texture_create_dc(void *object)
729 const struct wined3d_texture_idx *idx = object;
730 struct wined3d_context *context = NULL;
731 unsigned int sub_resource_idx, level;
732 const struct wined3d_format *format;
733 unsigned int row_pitch, slice_pitch;
734 struct wined3d_texture *texture;
735 struct wined3d_dc_info *dc_info;
736 struct wined3d_bo_address data;
737 D3DKMT_CREATEDCFROMMEMORY desc;
738 struct wined3d_device *device;
739 NTSTATUS status;
741 TRACE("texture %p, sub_resource_idx %u.\n", idx->texture, idx->sub_resource_idx);
743 texture = idx->texture;
744 sub_resource_idx = idx->sub_resource_idx;
745 level = sub_resource_idx % texture->level_count;
746 device = texture->resource.device;
748 format = texture->resource.format;
749 if (!format->ddi_format)
751 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format->id));
752 return;
755 if (!texture->dc_info)
757 unsigned int sub_count = texture->level_count * texture->layer_count;
759 if (!(texture->dc_info = heap_calloc(sub_count, sizeof(*texture->dc_info))))
761 ERR("Failed to allocate DC info.\n");
762 return;
766 if (device->d3d_initialized)
767 context = context_acquire(device, NULL, 0);
769 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
770 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
771 wined3d_texture_get_pitch(texture, level, &row_pitch, &slice_pitch);
772 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
773 desc.pMemory = context_map_bo_address(context, &data,
774 texture->sub_resources[sub_resource_idx].size,
775 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READ | WINED3D_MAP_WRITE);
777 if (context)
778 context_release(context);
780 desc.Format = format->ddi_format;
781 desc.Width = wined3d_texture_get_level_width(texture, level);
782 desc.Height = wined3d_texture_get_level_height(texture, level);
783 desc.Pitch = row_pitch;
784 desc.hDeviceDc = CreateCompatibleDC(NULL);
785 desc.pColorTable = NULL;
787 status = D3DKMTCreateDCFromMemory(&desc);
788 DeleteDC(desc.hDeviceDc);
789 if (status)
791 WARN("Failed to create DC, status %#x.\n", status);
792 return;
795 dc_info = &texture->dc_info[sub_resource_idx];
796 dc_info->dc = desc.hDc;
797 dc_info->bitmap = desc.hBitmap;
799 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info->dc, dc_info->bitmap, texture, sub_resource_idx);
802 static void wined3d_texture_destroy_dc(void *object)
804 const struct wined3d_texture_idx *idx = object;
805 D3DKMT_DESTROYDCFROMMEMORY destroy_desc;
806 struct wined3d_context *context = NULL;
807 struct wined3d_texture *texture;
808 struct wined3d_dc_info *dc_info;
809 struct wined3d_bo_address data;
810 unsigned int sub_resource_idx;
811 struct wined3d_device *device;
812 NTSTATUS status;
814 texture = idx->texture;
815 sub_resource_idx = idx->sub_resource_idx;
816 device = texture->resource.device;
817 dc_info = &texture->dc_info[sub_resource_idx];
819 if (!dc_info->dc)
821 ERR("Sub-resource {%p, %u} has no DC.\n", texture, sub_resource_idx);
822 return;
825 TRACE("dc %p, bitmap %p.\n", dc_info->dc, dc_info->bitmap);
827 destroy_desc.hDc = dc_info->dc;
828 destroy_desc.hBitmap = dc_info->bitmap;
829 if ((status = D3DKMTDestroyDCFromMemory(&destroy_desc)))
830 ERR("Failed to destroy dc, status %#x.\n", status);
831 dc_info->dc = NULL;
832 dc_info->bitmap = NULL;
834 if (device->d3d_initialized)
835 context = context_acquire(device, NULL, 0);
837 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
838 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
840 if (context)
841 context_release(context);
844 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
846 unsigned int sub_count = texture->level_count * texture->layer_count;
847 struct wined3d_device *device = texture->resource.device;
848 const struct wined3d_gl_info *gl_info = NULL;
849 struct wined3d_context *context = NULL;
850 struct wined3d_dc_info *dc_info;
851 GLuint buffer_object;
852 unsigned int i;
854 TRACE("texture %p.\n", texture);
856 for (i = 0; i < sub_count; ++i)
858 if (!(buffer_object = texture->sub_resources[i].buffer_object))
859 continue;
861 TRACE("Deleting buffer object %u.\n", buffer_object);
863 /* We may not be able to get a context in wined3d_texture_cleanup() in
864 * general, but if a buffer object was previously created we can. */
865 if (!context)
867 context = context_acquire(device, NULL, 0);
868 gl_info = context->gl_info;
871 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
874 if (context)
875 context_release(context);
877 if ((dc_info = texture->dc_info))
879 for (i = 0; i < sub_count; ++i)
881 if (dc_info[i].dc)
883 struct wined3d_texture_idx texture_idx = {texture, i};
885 wined3d_texture_destroy_dc(&texture_idx);
888 heap_free(dc_info);
891 if (texture->overlay_info)
893 for (i = 0; i < sub_count; ++i)
895 struct wined3d_overlay_info *info = &texture->overlay_info[i];
896 struct wined3d_overlay_info *overlay, *cur;
898 list_remove(&info->entry);
899 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &info->overlays, struct wined3d_overlay_info, entry)
901 list_remove(&overlay->entry);
904 heap_free(texture->overlay_info);
906 wined3d_texture_unload_gl_texture(texture);
909 static void wined3d_texture_gl_cleanup(struct wined3d_texture_gl *texture_gl)
911 struct wined3d_device *device = texture_gl->t.resource.device;
912 struct wined3d_renderbuffer_entry *entry, *entry2;
913 const struct wined3d_gl_info *gl_info;
914 struct wined3d_context *context;
916 if (!list_empty(&texture_gl->renderbuffers))
918 context = context_acquire(device, NULL, 0);
919 gl_info = context->gl_info;
921 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
923 TRACE("Deleting renderbuffer %u.\n", entry->id);
924 context_gl_resource_released(device, entry->id, TRUE);
925 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
926 heap_free(entry);
929 context_release(context);
932 wined3d_texture_cleanup(&texture_gl->t);
935 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
937 texture->swapchain = swapchain;
938 wined3d_resource_update_draw_binding(&texture->resource);
941 void wined3d_gl_texture_swizzle_from_color_fixup(GLint swizzle[4], struct color_fixup_desc fixup)
943 static const GLenum swizzle_source[] =
945 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
946 GL_ONE, /* CHANNEL_SOURCE_ONE */
947 GL_RED, /* CHANNEL_SOURCE_X */
948 GL_GREEN, /* CHANNEL_SOURCE_Y */
949 GL_BLUE, /* CHANNEL_SOURCE_Z */
950 GL_ALPHA, /* CHANNEL_SOURCE_W */
953 swizzle[0] = swizzle_source[fixup.x_source];
954 swizzle[1] = swizzle_source[fixup.y_source];
955 swizzle[2] = swizzle_source[fixup.z_source];
956 swizzle[3] = swizzle_source[fixup.w_source];
959 /* Context activation is done by the caller. */
960 void wined3d_texture_bind(struct wined3d_texture *texture,
961 struct wined3d_context *context, BOOL srgb)
963 const struct wined3d_gl_info *gl_info = context->gl_info;
964 const struct wined3d_format *format = texture->resource.format;
965 const struct color_fixup_desc fixup = format->color_fixup;
966 struct gl_texture *gl_tex;
967 GLenum target;
969 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
971 if (!needs_separate_srgb_gl_texture(context, texture))
972 srgb = FALSE;
974 /* sRGB mode cache for preload() calls outside drawprim. */
975 if (srgb)
976 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
977 else
978 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
980 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
981 target = texture->target;
983 if (gl_tex->name)
985 context_bind_texture(context, target, gl_tex->name);
986 return;
989 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
990 checkGLcall("glGenTextures");
991 TRACE("Generated texture %d.\n", gl_tex->name);
993 if (!gl_tex->name)
995 ERR("Failed to generate a texture name.\n");
996 return;
999 /* Initialise the state of the texture object to the OpenGL defaults, not
1000 * the wined3d defaults. */
1001 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
1002 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
1003 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
1004 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
1005 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
1006 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
1007 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
1008 gl_tex->sampler_desc.lod_bias = 0.0f;
1009 gl_tex->sampler_desc.min_lod = -1000.0f;
1010 gl_tex->sampler_desc.max_lod = 1000.0f;
1011 gl_tex->sampler_desc.max_anisotropy = 1;
1012 gl_tex->sampler_desc.compare = FALSE;
1013 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
1014 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1015 gl_tex->sampler_desc.srgb_decode = TRUE;
1016 else
1017 gl_tex->sampler_desc.srgb_decode = srgb;
1018 gl_tex->base_level = 0;
1019 wined3d_texture_set_dirty(texture);
1021 context_bind_texture(context, target, gl_tex->name);
1023 /* For a new texture we have to set the texture levels after binding the
1024 * texture. Beware that texture rectangles do not support mipmapping, but
1025 * set the maxmiplevel if we're relying on the partial
1026 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
1027 * (I.e., do not care about cond_np2 here, just look for
1028 * GL_TEXTURE_RECTANGLE_ARB.) */
1029 if (target != GL_TEXTURE_RECTANGLE_ARB)
1031 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
1032 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
1033 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
1036 if (target == GL_TEXTURE_CUBE_MAP_ARB)
1038 /* Cubemaps are always set to clamp, regardless of the sampler state. */
1039 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1040 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1041 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
1044 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
1046 /* Conditinal non power of two textures use a different clamping
1047 * default. If we're using the GL_WINE_normalized_texrect partial
1048 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
1049 * has the address mode set to repeat - something that prevents us
1050 * from hitting the accelerated codepath. Thus manually set the GL
1051 * state. The same applies to filtering. Even if the texture has only
1052 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
1053 * fallback on macos. */
1054 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1055 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1056 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1057 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1058 checkGLcall("glTexParameteri");
1059 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
1060 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
1061 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
1062 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
1063 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
1066 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
1068 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
1069 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
1072 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
1074 GLint swizzle[4];
1076 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, fixup);
1077 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
1078 checkGLcall("set format swizzle");
1082 /* Context activation is done by the caller. */
1083 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
1084 struct wined3d_context *context, BOOL srgb)
1086 /* We don't need a specific texture unit, but after binding the texture
1087 * the current unit is dirty. Read the unit back instead of switching to
1088 * 0, this avoids messing around with the state manager's GL states. The
1089 * current texture unit should always be a valid one.
1091 * To be more specific, this is tricky because we can implicitly be
1092 * called from sampler() in state.c. This means we can't touch anything
1093 * other than whatever happens to be the currently active texture, or we
1094 * would risk marking already applied sampler states dirty again. */
1095 if (context->active_texture < ARRAY_SIZE(context->rev_tex_unit_map))
1097 DWORD active_sampler = context->rev_tex_unit_map[context->active_texture];
1098 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1099 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
1101 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1102 * a shader. */
1103 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1104 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1106 wined3d_texture_bind(texture, context, srgb);
1109 /* Context activation is done by the caller (state handler). */
1110 /* This function relies on the correct texture being bound and loaded. */
1111 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
1112 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
1114 const struct wined3d_gl_info *gl_info = context->gl_info;
1115 GLenum target = texture->target;
1116 struct gl_texture *gl_tex;
1117 DWORD state;
1119 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
1121 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1123 state = sampler_desc->address_u;
1124 if (state != gl_tex->sampler_desc.address_u)
1126 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
1127 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1128 gl_tex->sampler_desc.address_u = state;
1131 state = sampler_desc->address_v;
1132 if (state != gl_tex->sampler_desc.address_v)
1134 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
1135 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1136 gl_tex->sampler_desc.address_v = state;
1139 state = sampler_desc->address_w;
1140 if (state != gl_tex->sampler_desc.address_w)
1142 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
1143 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
1144 gl_tex->sampler_desc.address_w = state;
1147 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1148 sizeof(gl_tex->sampler_desc.border_color)))
1150 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
1151 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
1152 sizeof(gl_tex->sampler_desc.border_color));
1155 state = sampler_desc->mag_filter;
1156 if (state != gl_tex->sampler_desc.mag_filter)
1158 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
1159 gl_tex->sampler_desc.mag_filter = state;
1162 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
1163 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
1165 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
1166 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
1167 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
1168 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
1171 state = sampler_desc->max_anisotropy;
1172 if (state != gl_tex->sampler_desc.max_anisotropy)
1174 if (gl_info->supported[ARB_TEXTURE_FILTER_ANISOTROPIC])
1175 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY, state);
1176 else
1177 WARN("Anisotropic filtering not supported.\n");
1178 gl_tex->sampler_desc.max_anisotropy = state;
1181 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
1182 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1183 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
1185 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
1186 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
1187 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
1190 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
1192 if (sampler_desc->compare)
1193 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
1194 else
1195 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
1196 gl_tex->sampler_desc.compare = sampler_desc->compare;
1199 checkGLcall("Texture parameter application");
1201 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
1203 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
1204 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
1205 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1209 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
1211 ULONG refcount;
1213 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1215 if (texture->swapchain)
1216 return wined3d_swapchain_incref(texture->swapchain);
1218 refcount = InterlockedIncrement(&texture->resource.ref);
1219 TRACE("%p increasing refcount to %u.\n", texture, refcount);
1221 return refcount;
1224 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
1226 wined3d_texture_sub_resources_destroyed(texture);
1227 resource_cleanup(&texture->resource);
1228 wined3d_resource_wait_idle(&texture->resource);
1229 wined3d_texture_cleanup(texture);
1232 static void wined3d_texture_destroy_object(void *object)
1234 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(object);
1236 wined3d_texture_gl_cleanup(texture_gl);
1237 heap_free(texture_gl);
1240 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
1242 ULONG refcount;
1244 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
1246 if (texture->swapchain)
1247 return wined3d_swapchain_decref(texture->swapchain);
1249 refcount = InterlockedDecrement(&texture->resource.ref);
1250 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
1252 if (!refcount)
1254 /* Wait for the texture to become idle if it's using user memory,
1255 * since the application is allowed to free that memory once the
1256 * texture is destroyed. Note that this implies that
1257 * wined3d_texture_destroy_object() can't access that memory either. */
1258 if (texture->user_memory)
1259 wined3d_resource_wait_idle(&texture->resource);
1260 wined3d_texture_sub_resources_destroyed(texture);
1261 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
1262 resource_cleanup(&texture->resource);
1263 wined3d_cs_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
1266 return refcount;
1269 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
1271 TRACE("texture %p.\n", texture);
1273 return &texture->resource;
1276 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
1278 return c1->color_space_low_value == c2->color_space_low_value
1279 && c1->color_space_high_value == c2->color_space_high_value;
1282 /* Context activation is done by the caller */
1283 void wined3d_texture_load(struct wined3d_texture *texture,
1284 struct wined3d_context *context, BOOL srgb)
1286 UINT sub_count = texture->level_count * texture->layer_count;
1287 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1288 DWORD flag;
1289 UINT i;
1291 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
1293 if (!needs_separate_srgb_gl_texture(context, texture))
1294 srgb = FALSE;
1296 if (srgb)
1297 flag = WINED3D_TEXTURE_SRGB_VALID;
1298 else
1299 flag = WINED3D_TEXTURE_RGB_VALID;
1301 if (!d3d_info->shader_color_key
1302 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1303 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1304 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
1305 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
1307 unsigned int sub_count = texture->level_count * texture->layer_count;
1308 unsigned int i;
1310 TRACE("Reloading because of color key value change.\n");
1311 for (i = 0; i < sub_count; i++)
1313 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
1314 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1315 else
1316 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
1319 texture->async.gl_color_key = texture->async.src_blt_color_key;
1322 if (texture->flags & flag)
1324 TRACE("Texture %p not dirty, nothing to do.\n", texture);
1325 return;
1328 /* Reload the surfaces if the texture is marked dirty. */
1329 for (i = 0; i < sub_count; ++i)
1331 if (!wined3d_texture_load_location(texture, i, context,
1332 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
1333 ERR("Failed to load location (srgb %#x).\n", srgb);
1335 texture->flags |= flag;
1338 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
1340 TRACE("texture %p.\n", texture);
1342 return texture->resource.parent;
1345 HRESULT wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
1346 unsigned int level, const struct wined3d_box *box)
1348 const struct wined3d_format *format = texture->resource.format;
1349 unsigned int width_mask, height_mask, width, height, depth;
1351 width = wined3d_texture_get_level_width(texture, level);
1352 height = wined3d_texture_get_level_height(texture, level);
1353 depth = wined3d_texture_get_level_depth(texture, level);
1355 if (box->left >= box->right || box->right > width
1356 || box->top >= box->bottom || box->bottom > height
1357 || box->front >= box->back || box->back > depth)
1359 WARN("Box %s is invalid.\n", debug_box(box));
1360 return WINEDDERR_INVALIDRECT;
1363 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1365 /* This assumes power of two block sizes, but NPOT block sizes would
1366 * be silly anyway.
1368 * This also assumes that the format's block depth is 1. */
1369 width_mask = format->block_width - 1;
1370 height_mask = format->block_height - 1;
1372 if ((box->left & width_mask) || (box->top & height_mask)
1373 || (box->right & width_mask && box->right != width)
1374 || (box->bottom & height_mask && box->bottom != height))
1376 WARN("Box %s is misaligned for %ux%u blocks.\n",
1377 debug_box(box), format->block_width, format->block_height);
1378 return WINED3DERR_INVALIDCALL;
1382 return WINED3D_OK;
1385 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
1386 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
1388 const struct wined3d_resource *resource = &texture->resource;
1389 unsigned int width = wined3d_texture_get_level_width(texture, level);
1390 unsigned int height = wined3d_texture_get_level_height(texture, level);
1392 if (texture->row_pitch)
1394 *row_pitch = texture->row_pitch;
1395 *slice_pitch = texture->slice_pitch;
1396 return;
1399 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
1400 width, height, row_pitch, slice_pitch);
1403 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1405 DWORD old = texture->lod;
1407 TRACE("texture %p, lod %u.\n", texture, lod);
1409 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1410 * textures. The call always returns 0, and GetLOD always returns 0. */
1411 if (!wined3d_resource_access_is_managed(texture->resource.access))
1413 TRACE("Ignoring LOD on texture with resource access %s.\n",
1414 wined3d_debug_resource_access(texture->resource.access));
1415 return 0;
1418 if (lod >= texture->level_count)
1419 lod = texture->level_count - 1;
1421 if (texture->lod != lod)
1423 struct wined3d_device *device = texture->resource.device;
1425 wined3d_resource_wait_idle(&texture->resource);
1426 texture->lod = lod;
1428 texture->texture_rgb.base_level = ~0u;
1429 texture->texture_srgb.base_level = ~0u;
1430 if (texture->resource.bind_count)
1431 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1432 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1435 return old;
1438 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1440 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1442 return texture->lod;
1445 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1447 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1449 return texture->level_count;
1452 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1453 DWORD flags, const struct wined3d_color_key *color_key)
1455 struct wined3d_device *device = texture->resource.device;
1456 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1457 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1459 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1461 if (flags & ~all_flags)
1463 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1464 return WINED3DERR_INVALIDCALL;
1467 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1469 return WINED3D_OK;
1472 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1473 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1474 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1475 /* Context activation is done by the caller. */
1476 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1477 struct wined3d_context *context, unsigned int level, const struct wined3d_rendertarget_info *rt)
1479 const struct wined3d_gl_info *gl_info = context->gl_info;
1480 struct wined3d_renderbuffer_entry *entry;
1481 unsigned int src_width, src_height;
1482 unsigned int width, height;
1483 GLuint renderbuffer = 0;
1485 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1486 return;
1488 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1490 struct wined3d_texture *rt_texture;
1491 unsigned int rt_level;
1493 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1495 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1496 return;
1498 rt_texture = wined3d_texture_from_resource(rt->resource);
1499 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1501 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1502 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1504 else
1506 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1507 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1510 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1511 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1513 /* A depth stencil smaller than the render target is not valid */
1514 if (width > src_width || height > src_height)
1515 return;
1517 /* Remove any renderbuffer set if the sizes match */
1518 if (width == src_width && height == src_height)
1520 texture_gl->current_renderbuffer = NULL;
1521 return;
1524 /* Look if we've already got a renderbuffer of the correct dimensions */
1525 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1527 if (entry->width == width && entry->height == height)
1529 renderbuffer = entry->id;
1530 texture_gl->current_renderbuffer = entry;
1531 break;
1535 if (!renderbuffer)
1537 const struct wined3d_format_gl *format_gl;
1539 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1540 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1541 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1542 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1544 entry = heap_alloc(sizeof(*entry));
1545 entry->width = width;
1546 entry->height = height;
1547 entry->id = renderbuffer;
1548 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1550 texture_gl->current_renderbuffer = entry;
1553 checkGLcall("set compatible renderbuffer");
1556 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1557 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1558 UINT multisample_quality, void *mem, UINT pitch)
1560 struct wined3d_texture_sub_resource *sub_resource;
1561 const struct wined3d_d3d_info *d3d_info;
1562 const struct wined3d_gl_info *gl_info;
1563 const struct wined3d_format *format;
1564 struct wined3d_device *device;
1565 unsigned int resource_size;
1566 DWORD valid_location = 0;
1567 BOOL create_dib = FALSE;
1569 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1570 "mem %p, pitch %u.\n",
1571 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1573 device = texture->resource.device;
1574 gl_info = &device->adapter->gl_info;
1575 d3d_info = &device->adapter->d3d_info;
1576 format = wined3d_get_format(device->adapter, format_id, texture->resource.usage);
1577 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1579 if (!resource_size)
1580 return WINED3DERR_INVALIDCALL;
1582 if (texture->level_count * texture->layer_count > 1)
1584 WARN("Texture has multiple sub-resources, not supported.\n");
1585 return WINED3DERR_INVALIDCALL;
1588 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1590 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1591 return WINED3DERR_INVALIDCALL;
1594 if (texture->resource.map_count)
1596 WARN("Texture is mapped.\n");
1597 return WINED3DERR_INVALIDCALL;
1600 /* We have no way of supporting a pitch that is not a multiple of the pixel
1601 * byte width short of uploading the texture row-by-row.
1602 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1603 * for user-memory textures (it always expects packed data) while DirectDraw
1604 * requires a 4-byte aligned pitch and doesn't support texture formats
1605 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1606 * This check is here to verify that the assumption holds. */
1607 if (pitch % texture->resource.format->byte_count)
1609 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1610 return WINED3DERR_INVALIDCALL;
1613 if (device->d3d_initialized)
1614 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1615 wined3d_resource_wait_idle(&texture->resource);
1617 sub_resource = &texture->sub_resources[0];
1618 if (texture->dc_info && texture->dc_info[0].dc)
1620 struct wined3d_texture_idx texture_idx = {texture, 0};
1622 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1623 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1624 create_dib = TRUE;
1627 wined3d_resource_free_sysmem(&texture->resource);
1629 if ((texture->row_pitch = pitch))
1630 texture->slice_pitch = height * pitch;
1631 else
1632 /* User memory surfaces don't have the regular surface alignment. */
1633 wined3d_format_calculate_pitch(format, 1, width, height,
1634 &texture->row_pitch, &texture->slice_pitch);
1636 texture->resource.format = format;
1637 texture->resource.multisample_type = multisample_type;
1638 texture->resource.multisample_quality = multisample_quality;
1639 texture->resource.width = width;
1640 texture->resource.height = height;
1641 texture->resource.size = texture->slice_pitch;
1642 sub_resource->size = texture->slice_pitch;
1643 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1645 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1646 texture->target = GL_TEXTURE_2D_MULTISAMPLE;
1647 else
1648 texture->target = GL_TEXTURE_2D;
1650 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1651 && !d3d_info->texture_npot_conditional)
1653 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1654 texture->pow2_width = texture->pow2_height = 1;
1655 while (texture->pow2_width < width)
1656 texture->pow2_width <<= 1;
1657 while (texture->pow2_height < height)
1658 texture->pow2_height <<= 1;
1660 else
1662 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1663 texture->pow2_width = width;
1664 texture->pow2_height = height;
1667 if ((texture->user_memory = mem))
1669 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1670 valid_location = WINED3D_LOCATION_USER_MEMORY;
1672 else
1674 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1675 valid_location = WINED3D_LOCATION_SYSMEM;
1678 /* The format might be changed to a format that needs conversion.
1679 * If the surface didn't use PBOs previously but could now, don't
1680 * change it - whatever made us not use PBOs might come back, e.g.
1681 * color keys. */
1682 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1683 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1685 wined3d_texture_validate_location(texture, 0, valid_location);
1686 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1688 if (create_dib)
1690 struct wined3d_texture_idx texture_idx = {texture, 0};
1692 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1693 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1696 return WINED3D_OK;
1699 /* Context activation is done by the caller. */
1700 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1701 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1703 struct wined3d_texture_sub_resource *sub_resource;
1705 sub_resource = &texture->sub_resources[sub_resource_idx];
1706 if (sub_resource->buffer_object)
1707 return;
1709 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1710 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1711 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1712 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1713 checkGLcall("Create buffer object");
1715 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1716 sub_resource->buffer_object, texture, sub_resource_idx);
1719 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1721 unsigned int sub_count = texture->level_count * texture->layer_count;
1722 unsigned int i;
1724 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1725 | WINED3D_TEXTURE_CONVERTED);
1726 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1727 for (i = 0; i < sub_count; ++i)
1729 wined3d_texture_invalidate_location(texture, i,
1730 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1734 /* Context activation is done by the caller. */
1735 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1737 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1738 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1739 const struct wined3d_gl_info *gl_info = context->gl_info;
1740 struct wined3d_resource *resource = &texture->resource;
1741 const struct wined3d_device *device = resource->device;
1742 const struct wined3d_format *format = resource->format;
1743 const struct wined3d_color_key_conversion *conversion;
1744 const struct wined3d_format_gl *format_gl;
1745 GLenum internal;
1747 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1749 if (!d3d_info->shader_color_key
1750 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1751 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1753 wined3d_texture_force_reload(texture);
1755 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1756 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1759 if (texture->flags & alloc_flag)
1760 return;
1762 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1764 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1765 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1766 format = wined3d_resource_get_decompress_format(resource);
1768 else if (format->conv_byte_count)
1770 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1772 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1774 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1775 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->usage);
1776 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1778 format_gl = wined3d_format_gl(format);
1780 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1782 if (srgb)
1783 internal = format_gl->srgb_internal;
1784 else if (resource->usage & WINED3DUSAGE_RENDERTARGET && wined3d_resource_is_offscreen(resource))
1785 internal = format_gl->rt_internal;
1786 else
1787 internal = format_gl->internal;
1789 if (!internal)
1790 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1792 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
1794 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1795 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1796 else
1797 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format_gl, gl_info);
1798 texture->flags |= alloc_flag;
1801 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1802 const struct wined3d_gl_info *gl_info, BOOL multisample)
1804 const struct wined3d_format_gl *format_gl;
1806 format_gl = wined3d_format_gl(texture->resource.format);
1807 if (multisample)
1809 DWORD samples;
1811 if (texture->rb_multisample)
1812 return;
1814 samples = wined3d_texture_get_gl_sample_count(texture);
1816 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1817 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1818 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1819 format_gl->internal, texture->resource.width, texture->resource.height);
1820 checkGLcall("glRenderbufferStorageMultisample()");
1821 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1823 else
1825 if (texture->rb_resolved)
1826 return;
1828 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1829 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1830 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
1831 texture->resource.width, texture->resource.height);
1832 checkGLcall("glRenderbufferStorage()");
1833 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1837 /* Context activation is done by the caller. Context may be NULL in
1838 * WINED3D_NO3D mode. */
1839 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1840 struct wined3d_context *context, DWORD location)
1842 switch (location)
1844 case WINED3D_LOCATION_SYSMEM:
1845 if (texture->resource.heap_memory)
1846 return TRUE;
1848 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1849 return FALSE;
1850 return TRUE;
1852 case WINED3D_LOCATION_USER_MEMORY:
1853 if (!texture->user_memory)
1854 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1855 return TRUE;
1857 case WINED3D_LOCATION_BUFFER:
1858 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1859 return TRUE;
1861 case WINED3D_LOCATION_TEXTURE_RGB:
1862 wined3d_texture_prepare_texture(texture, context, FALSE);
1863 return TRUE;
1865 case WINED3D_LOCATION_TEXTURE_SRGB:
1866 wined3d_texture_prepare_texture(texture, context, TRUE);
1867 return TRUE;
1869 case WINED3D_LOCATION_DRAWABLE:
1870 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1871 ERR("Texture %p does not have a drawable.\n", texture);
1872 return TRUE;
1874 case WINED3D_LOCATION_RB_MULTISAMPLE:
1875 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1876 return TRUE;
1878 case WINED3D_LOCATION_RB_RESOLVED:
1879 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1880 return TRUE;
1882 default:
1883 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1884 return FALSE;
1888 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1889 unsigned int sub_resource_idx)
1891 UINT sub_count = texture->level_count * texture->layer_count;
1893 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1895 if (sub_resource_idx >= sub_count)
1897 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1898 return NULL;
1901 return &texture->sub_resources[sub_resource_idx];
1904 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1905 UINT layer, const struct wined3d_box *dirty_region)
1907 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1909 if (layer >= texture->layer_count)
1911 WARN("Invalid layer %u specified.\n", layer);
1912 return WINED3DERR_INVALIDCALL;
1915 if (dirty_region)
1916 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1918 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
1920 return WINED3D_OK;
1923 /* This call just uploads data, the caller is responsible for binding the
1924 * correct texture. */
1925 /* Context activation is done by the caller. */
1926 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1927 struct wined3d_context *context, const struct wined3d_format *format, const struct wined3d_box *src_box,
1928 const struct wined3d_const_bo_address *data, unsigned int src_row_pitch, unsigned int src_slice_pitch,
1929 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, BOOL srgb)
1931 const struct wined3d_gl_info *gl_info = context->gl_info;
1932 unsigned int update_w = src_box->right - src_box->left;
1933 unsigned int update_h = src_box->bottom - src_box->top;
1934 unsigned int update_d = src_box->back - src_box->front;
1935 const struct wined3d_format_gl *format_gl;
1936 struct wined3d_bo_address bo;
1937 void *converted_mem = NULL;
1938 struct wined3d_format_gl f;
1939 unsigned int level;
1940 BOOL decompress;
1941 GLenum target;
1943 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data %s, "
1944 "src_row_pitch %#x, src_slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
1945 texture, sub_resource_idx, context, debug_d3dformat(format->id), debug_box(src_box),
1946 debug_const_bo_address(data), src_row_pitch, src_slice_pitch, dst_x, dst_y, dst_z, srgb);
1948 if (texture->sub_resources[sub_resource_idx].map_count)
1950 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
1951 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
1954 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
1956 update_h *= format->height_scale.numerator;
1957 update_h /= format->height_scale.denominator;
1960 target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
1961 level = sub_resource_idx % texture->level_count;
1963 switch (target)
1965 case GL_TEXTURE_1D_ARRAY:
1966 dst_y = sub_resource_idx / texture->level_count;
1967 update_h = 1;
1968 break;
1969 case GL_TEXTURE_2D_ARRAY:
1970 dst_z = sub_resource_idx / texture->level_count;
1971 update_d = 1;
1972 break;
1973 case GL_TEXTURE_2D_MULTISAMPLE:
1974 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1975 FIXME("Not supported for multisample textures.\n");
1976 return;
1979 bo.buffer_object = data->buffer_object;
1980 bo.addr = (BYTE *)data->addr + src_box->front * src_slice_pitch;
1981 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1983 bo.addr += (src_box->top / format->block_height) * src_row_pitch;
1984 bo.addr += (src_box->left / format->block_width) * format->block_byte_count;
1986 else
1988 bo.addr += src_box->top * src_row_pitch;
1989 bo.addr += src_box->left * format->byte_count;
1992 decompress = texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS;
1993 if (format->upload || decompress)
1995 const struct wined3d_format *compressed_format = format;
1996 unsigned int dst_row_pitch, dst_slice_pitch;
1997 void *src_mem;
1999 if (decompress)
2001 format = wined3d_resource_get_decompress_format(&texture->resource);
2003 else
2005 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2006 ERR("Converting a block-based format.\n");
2008 f = *wined3d_format_gl(format);
2009 f.f.byte_count = format->conv_byte_count;
2010 format = &f.f;
2013 wined3d_format_calculate_pitch(format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2015 /* Note that uploading 3D textures may require quite some address
2016 * space; it may make sense to upload them per-slice instead. */
2017 if (!(converted_mem = heap_calloc(update_d, dst_slice_pitch)))
2019 ERR("Failed to allocate upload buffer.\n");
2020 return;
2023 src_mem = context_map_bo_address(context, &bo, src_slice_pitch,
2024 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READ);
2025 if (decompress)
2026 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2027 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2028 else
2029 format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2030 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2031 context_unmap_bo_address(context, &bo, GL_PIXEL_UNPACK_BUFFER);
2033 bo.buffer_object = 0;
2034 bo.addr = converted_mem;
2035 src_row_pitch = dst_row_pitch;
2036 src_slice_pitch = dst_slice_pitch;
2039 if (bo.buffer_object)
2041 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bo.buffer_object));
2042 checkGLcall("glBindBuffer");
2045 format_gl = wined3d_format_gl(format);
2046 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2048 unsigned int dst_row_pitch, dst_slice_pitch;
2049 const BYTE *addr = bo.addr;
2050 GLenum internal;
2052 if (srgb)
2053 internal = format_gl->srgb_internal;
2054 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
2055 && wined3d_resource_is_offscreen(&texture->resource))
2056 internal = format_gl->rt_internal;
2057 else
2058 internal = format_gl->internal;
2060 wined3d_format_calculate_pitch(format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2062 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2063 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2064 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2065 update_d, internal, dst_slice_pitch, addr);
2067 if (target == GL_TEXTURE_1D)
2069 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2070 update_w, internal, dst_row_pitch, addr));
2072 else if (dst_row_pitch == src_row_pitch)
2074 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2076 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2077 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2079 else
2081 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2082 update_w, update_h, internal, dst_slice_pitch, addr));
2085 else
2087 unsigned int row_count = (update_h + format->block_height - 1) / format->block_height;
2088 unsigned int row, y, z;
2090 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2091 * can't use the unpack row length like for glTexSubImage2D. */
2092 for (z = dst_z; z < dst_z + update_d; ++z)
2094 for (row = 0, y = dst_y; row < row_count; ++row)
2096 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2098 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2099 update_w, format->block_height, 1, internal, dst_row_pitch, addr));
2101 else
2103 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2104 update_w, format->block_height, internal, dst_row_pitch, addr));
2107 y += format->block_height;
2108 addr += src_row_pitch;
2112 checkGLcall("Upload compressed texture data");
2114 else
2116 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2117 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2118 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2119 update_d, format_gl->format, format_gl->type, bo.addr);
2121 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / format->byte_count);
2122 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2124 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2125 update_w, update_h, update_d, format_gl->format, format_gl->type, bo.addr));
2127 else if (target == GL_TEXTURE_1D)
2129 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2130 update_w, format_gl->format, format_gl->type, bo.addr);
2132 else
2134 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y,
2135 update_w, update_h, format_gl->format, format_gl->type, bo.addr);
2137 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2138 checkGLcall("Upload texture data");
2141 if (bo.buffer_object)
2143 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2144 checkGLcall("glBindBuffer");
2146 heap_free(converted_mem);
2148 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2150 struct wined3d_device *device = texture->resource.device;
2151 unsigned int i;
2153 for (i = 0; i < device->context_count; ++i)
2155 context_texture_update(device->contexts[i], texture);
2160 static void texture2d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2161 struct wined3d_context *context, const struct wined3d_bo_address *data)
2163 const struct wined3d_gl_info *gl_info = context->gl_info;
2164 struct wined3d_texture_sub_resource *sub_resource;
2165 unsigned int dst_row_pitch, dst_slice_pitch;
2166 unsigned int src_row_pitch, src_slice_pitch;
2167 const struct wined3d_format_gl *format_gl;
2168 BYTE *temporary_mem = NULL;
2169 unsigned int level;
2170 GLenum target;
2171 void *mem;
2173 format_gl = wined3d_format_gl(texture->resource.format);
2175 /* Only support read back of converted P8 textures. */
2176 if (texture->flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT && !format_gl->f.download)
2178 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2179 texture, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2180 return;
2183 sub_resource = &texture->sub_resources[sub_resource_idx];
2184 target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
2185 level = sub_resource_idx % texture->level_count;
2187 if (target == GL_TEXTURE_2D_ARRAY)
2189 if (format_gl->f.download)
2191 FIXME("Reading back converted array texture %p is not supported.\n", texture);
2192 return;
2195 /* NP2 emulation is not allowed on array textures. */
2196 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2197 ERR("Array texture %p uses NP2 emulation.\n", texture);
2199 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2201 if (!(temporary_mem = heap_calloc(texture->layer_count, sub_resource->size)))
2203 ERR("Out of memory.\n");
2204 return;
2208 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2210 if (format_gl->f.download)
2212 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture);
2213 return;
2216 wined3d_texture_get_pitch(texture, level, &dst_row_pitch, &dst_slice_pitch);
2217 wined3d_format_calculate_pitch(&format_gl->f, texture->resource.device->surface_alignment,
2218 wined3d_texture_get_level_pow2_width(texture, level),
2219 wined3d_texture_get_level_pow2_height(texture, level),
2220 &src_row_pitch, &src_slice_pitch);
2221 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2223 ERR("Out of memory.\n");
2224 return;
2227 if (data->buffer_object)
2228 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2229 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2230 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2233 if (format_gl->f.download)
2235 struct wined3d_format f;
2237 if (data->buffer_object)
2238 ERR("Converted texture %p uses PBO unexpectedly.\n", texture);
2240 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2241 texture, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2243 f = format_gl->f;
2244 f.byte_count = format_gl->f.conv_byte_count;
2245 wined3d_texture_get_pitch(texture, level, &dst_row_pitch, &dst_slice_pitch);
2246 wined3d_format_calculate_pitch(&f, texture->resource.device->surface_alignment,
2247 wined3d_texture_get_level_width(texture, level),
2248 wined3d_texture_get_level_height(texture, level),
2249 &src_row_pitch, &src_slice_pitch);
2251 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2253 ERR("Failed to allocate memory.\n");
2254 return;
2258 if (temporary_mem)
2260 mem = temporary_mem;
2262 else if (data->buffer_object)
2264 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2265 checkGLcall("glBindBuffer");
2266 mem = data->addr;
2268 else
2270 mem = data->addr;
2273 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2275 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2276 texture, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2278 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2279 checkGLcall("glGetCompressedTexImage");
2281 else
2283 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2284 texture, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2286 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2287 checkGLcall("glGetTexImage");
2290 if (format_gl->f.download)
2292 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2293 wined3d_texture_get_level_width(texture, level),
2294 wined3d_texture_get_level_height(texture, level), 1);
2296 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2298 const BYTE *src_data;
2299 unsigned int h, y;
2300 BYTE *dst_data;
2301 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2302 * pitches, preventing us from using the texture pitch to box NPOT
2303 * textures. Instead, we repack the texture's CPU copy so that its
2304 * pitch equals bpp * width instead of bpp * pow2width.
2306 * Instead of boxing the texture:
2308 * │<── texture width ──>│ pow2 width ──>│
2309 * ├─────────────────────┼───────────────┼─
2310 * │111111111111111111111│ │ʌ
2311 * │222222222222222222222│ ││
2312 * │333333333333333333333│ padding │texture height
2313 * │444444444444444444444│ ││
2314 * │555555555555555555555│ │v
2315 * ├─────────────────────┘ ├─
2316 * │ │pow2 height
2317 * │ padding padding ││
2318 * │ │v
2319 * └─────────────────────────────────────┴─
2321 * we're repacking the data to the expected texture width
2323 * │<── texture width ──>│ pow2 width ──>│
2324 * ├─────────────────────┴───────────────┼─
2325 * │1111111111111111111112222222222222222│ʌ
2326 * │2222233333333333333333333344444444444││
2327 * │4444444444555555555555555555555 │texture height
2328 * │ ││
2329 * │ padding padding │v
2330 * │ ├─
2331 * │ │pow2 height
2332 * │ padding padding ││
2333 * │ │v
2334 * └─────────────────────────────────────┴─
2336 * == is the same as
2338 * │<── texture width ──>│
2339 * ├─────────────────────┼─
2340 * │111111111111111111111│ʌ
2341 * │222222222222222222222││
2342 * │333333333333333333333│texture height
2343 * │444444444444444444444││
2344 * │555555555555555555555│v
2345 * └─────────────────────┴─
2347 * This also means that any references to surface memory should work
2348 * with the data as if it were a standard texture with a NPOT width
2349 * instead of a texture boxed up to be a power-of-two texture. */
2350 src_data = mem;
2351 dst_data = data->addr;
2352 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2353 h = wined3d_texture_get_level_height(texture, level);
2354 for (y = 0; y < h; ++y)
2356 memcpy(dst_data, src_data, dst_row_pitch);
2357 src_data += src_row_pitch;
2358 dst_data += dst_row_pitch;
2361 else if (temporary_mem)
2363 unsigned int layer = sub_resource_idx / texture->level_count;
2364 void *src_data = temporary_mem + layer * sub_resource->size;
2365 if (data->buffer_object)
2367 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2368 checkGLcall("glBindBuffer");
2369 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2370 checkGLcall("glBufferSubData");
2372 else
2374 memcpy(data->addr, src_data, sub_resource->size);
2378 if (data->buffer_object)
2380 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2381 checkGLcall("glBindBuffer");
2384 heap_free(temporary_mem);
2387 /* This call just downloads data, the caller is responsible for binding the
2388 * correct texture. Partial downloads are not supported. */
2389 /* Context activation is done by the caller. */
2390 void wined3d_texture_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2391 struct wined3d_context *context, const struct wined3d_bo_address *data)
2393 const struct wined3d_gl_info *gl_info = context->gl_info;
2394 const struct wined3d_format_gl *format_gl;
2395 unsigned int level;
2396 GLenum target;
2398 format_gl = wined3d_format_gl(texture->resource.format);
2399 target = wined3d_texture_get_sub_resource_target(texture, sub_resource_idx);
2400 level = sub_resource_idx % texture->level_count;
2402 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2403 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2404 || texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2406 /* 2D-specific special cases. */
2407 texture2d_download_data(texture, sub_resource_idx, context, data);
2408 return;
2411 if (format_gl->f.conv_byte_count)
2413 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2414 debug_d3dresourcetype(texture->resource.type),
2415 debug_d3dformat(format_gl->f.id));
2416 return;
2419 if (data->buffer_object)
2421 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2422 checkGLcall("glBindBuffer");
2425 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2427 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2428 texture, sub_resource_idx, level, format_gl->format, format_gl->type, data->addr);
2430 GL_EXTCALL(glGetCompressedTexImage(target, level, data->addr));
2431 checkGLcall("glGetCompressedTexImage");
2433 else
2435 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2436 texture, sub_resource_idx, level, format_gl->format, format_gl->type, data->addr);
2438 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, data->addr);
2439 checkGLcall("glGetTexImage");
2442 if (data->buffer_object)
2444 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2445 checkGLcall("glBindBuffer");
2449 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
2450 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2451 struct wined3d_context *context, DWORD location)
2453 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2454 texture, sub_resource_idx, context, wined3d_debug_location(location));
2456 switch (location)
2458 case WINED3D_LOCATION_USER_MEMORY:
2459 case WINED3D_LOCATION_SYSMEM:
2460 case WINED3D_LOCATION_BUFFER:
2461 return texture2d_load_sysmem(texture, sub_resource_idx, context, location);
2463 case WINED3D_LOCATION_DRAWABLE:
2464 return texture2d_load_drawable(texture, sub_resource_idx, context);
2466 case WINED3D_LOCATION_RB_RESOLVED:
2467 case WINED3D_LOCATION_RB_MULTISAMPLE:
2468 return texture2d_load_renderbuffer(texture, sub_resource_idx, context, location);
2470 case WINED3D_LOCATION_TEXTURE_RGB:
2471 case WINED3D_LOCATION_TEXTURE_SRGB:
2472 return texture2d_load_texture(texture, sub_resource_idx, context,
2473 location == WINED3D_LOCATION_TEXTURE_SRGB);
2475 default:
2476 ERR("Don't know how to handle location %#x.\n", location);
2477 return FALSE;
2481 static const struct wined3d_texture_ops texture2d_ops =
2483 texture2d_load_location,
2486 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
2488 return texture_from_resource(resource);
2491 static ULONG texture_resource_incref(struct wined3d_resource *resource)
2493 return wined3d_texture_incref(texture_from_resource(resource));
2496 static ULONG texture_resource_decref(struct wined3d_resource *resource)
2498 return wined3d_texture_decref(texture_from_resource(resource));
2501 static void texture_resource_preload(struct wined3d_resource *resource)
2503 struct wined3d_texture *texture = texture_from_resource(resource);
2504 struct wined3d_context *context;
2506 context = context_acquire(resource->device, NULL, 0);
2507 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
2508 context_release(context);
2511 static void wined3d_texture_gl_unload(struct wined3d_resource *resource)
2513 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
2514 UINT sub_count = texture_gl->t.level_count * texture_gl->t.layer_count;
2515 struct wined3d_renderbuffer_entry *entry, *entry2;
2516 struct wined3d_device *device = resource->device;
2517 const struct wined3d_gl_info *gl_info;
2518 struct wined3d_context *context;
2519 UINT i;
2521 TRACE("texture_gl %p.\n", texture_gl);
2523 context = context_acquire(device, NULL, 0);
2524 gl_info = context->gl_info;
2526 for (i = 0; i < sub_count; ++i)
2528 struct wined3d_texture_sub_resource *sub_resource = &texture_gl->t.sub_resources[i];
2530 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
2531 && wined3d_texture_load_location(&texture_gl->t, i, context, resource->map_binding))
2533 wined3d_texture_invalidate_location(&texture_gl->t, i, ~resource->map_binding);
2535 else
2537 /* We should only get here on device reset/teardown for implicit
2538 * resources. */
2539 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
2540 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
2541 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
2542 debug_d3dresourcetype(resource->type), resource, i,
2543 wined3d_debug_resource_access(resource->access));
2544 wined3d_texture_validate_location(&texture_gl->t, i, WINED3D_LOCATION_DISCARDED);
2545 wined3d_texture_invalidate_location(&texture_gl->t, i, ~WINED3D_LOCATION_DISCARDED);
2548 if (sub_resource->buffer_object)
2549 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context->gl_info);
2552 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
2554 context_gl_resource_released(device, entry->id, TRUE);
2555 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
2556 list_remove(&entry->entry);
2557 heap_free(entry);
2559 list_init(&texture_gl->renderbuffers);
2560 texture_gl->current_renderbuffer = NULL;
2562 context_release(context);
2564 wined3d_texture_force_reload(&texture_gl->t);
2565 wined3d_texture_unload_gl_texture(&texture_gl->t);
2568 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
2569 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
2571 const struct wined3d_format *format = resource->format;
2572 struct wined3d_texture_sub_resource *sub_resource;
2573 struct wined3d_device *device = resource->device;
2574 unsigned int fmt_flags = resource->format_flags;
2575 struct wined3d_context *context = NULL;
2576 struct wined3d_texture *texture;
2577 struct wined3d_bo_address data;
2578 unsigned int texture_level;
2579 BYTE *base_memory;
2580 BOOL ret;
2582 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2583 resource, sub_resource_idx, map_desc, debug_box(box), flags);
2585 texture = texture_from_resource(resource);
2586 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2587 return E_INVALIDARG;
2589 texture_level = sub_resource_idx % texture->level_count;
2590 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
2592 WARN("Map box is invalid.\n");
2593 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
2594 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
2595 return WINED3DERR_INVALIDCALL;
2598 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2600 WARN("DC is in use.\n");
2601 return WINED3DERR_INVALIDCALL;
2604 if (sub_resource->map_count)
2606 WARN("Sub-resource is already mapped.\n");
2607 return WINED3DERR_INVALIDCALL;
2610 if (device->d3d_initialized)
2611 context = context_acquire(device, NULL, 0);
2613 if (flags & WINED3D_MAP_DISCARD)
2615 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2616 wined3d_debug_location(resource->map_binding));
2617 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
2618 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
2620 else
2622 if (resource->usage & WINED3DUSAGE_DYNAMIC)
2623 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2624 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
2627 if (!ret)
2629 ERR("Failed to prepare location.\n");
2630 context_release(context);
2631 return E_OUTOFMEMORY;
2634 if (flags & WINED3D_MAP_WRITE
2635 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
2636 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
2638 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
2639 base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
2640 TRACE("Base memory pointer %p.\n", base_memory);
2642 if (context)
2643 context_release(context);
2645 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
2647 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
2648 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
2650 else
2652 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
2655 if (!box)
2657 map_desc->data = base_memory;
2659 else
2661 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
2663 /* Compressed textures are block based, so calculate the offset of
2664 * the block that contains the top-left pixel of the mapped box. */
2665 map_desc->data = base_memory
2666 + (box->front * map_desc->slice_pitch)
2667 + ((box->top / format->block_height) * map_desc->row_pitch)
2668 + ((box->left / format->block_width) * format->block_byte_count);
2670 else
2672 map_desc->data = base_memory
2673 + (box->front * map_desc->slice_pitch)
2674 + (box->top * map_desc->row_pitch)
2675 + (box->left * format->byte_count);
2679 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2681 RECT *r = &texture->swapchain->front_buffer_update;
2683 if (!box)
2684 SetRect(r, 0, 0, resource->width, resource->height);
2685 else
2686 SetRect(r, box->left, box->top, box->right, box->bottom);
2687 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
2690 ++resource->map_count;
2691 ++sub_resource->map_count;
2693 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2694 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2696 return WINED3D_OK;
2699 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
2701 struct wined3d_texture_sub_resource *sub_resource;
2702 struct wined3d_device *device = resource->device;
2703 struct wined3d_context *context = NULL;
2704 struct wined3d_texture *texture;
2705 struct wined3d_bo_address data;
2707 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
2709 texture = texture_from_resource(resource);
2710 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2711 return E_INVALIDARG;
2713 if (!sub_resource->map_count)
2715 WARN("Trying to unmap unmapped sub-resource.\n");
2716 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2717 return WINED3D_OK;
2718 return WINEDDERR_NOTLOCKED;
2721 if (device->d3d_initialized)
2722 context = context_acquire(device, NULL, 0);
2724 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2725 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2727 if (context)
2728 context_release(context);
2730 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2732 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2733 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2736 --sub_resource->map_count;
2737 if (!--resource->map_count && texture->update_map_binding)
2738 wined3d_texture_update_map_binding(texture);
2740 return WINED3D_OK;
2743 static const struct wined3d_resource_ops texture_resource_ops =
2745 texture_resource_incref,
2746 texture_resource_decref,
2747 texture_resource_preload,
2748 wined3d_texture_gl_unload,
2749 texture_resource_sub_resource_map,
2750 texture_resource_sub_resource_unmap,
2753 /* Context activation is done by the caller. */
2754 static BOOL texture1d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2755 struct wined3d_context *context, DWORD location)
2757 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2758 unsigned int row_pitch, slice_pitch;
2760 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2761 texture, sub_resource_idx, context, wined3d_debug_location(location));
2763 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2764 return FALSE;
2766 switch (location)
2768 case WINED3D_LOCATION_TEXTURE_RGB:
2769 case WINED3D_LOCATION_TEXTURE_SRGB:
2770 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2772 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2773 struct wined3d_box src_box;
2775 data.addr += sub_resource->offset;
2776 wined3d_texture_bind_and_dirtify(texture, context,
2777 location == WINED3D_LOCATION_TEXTURE_SRGB);
2778 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2779 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
2780 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2781 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2783 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2785 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2786 struct wined3d_box src_box;
2788 wined3d_texture_bind_and_dirtify(texture, context,
2789 location == WINED3D_LOCATION_TEXTURE_SRGB);
2790 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2791 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
2792 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2793 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2795 else
2797 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2798 return FALSE;
2800 break;
2802 case WINED3D_LOCATION_SYSMEM:
2803 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2805 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2807 data.addr += sub_resource->offset;
2808 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2809 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2810 else
2811 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2813 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
2814 ++texture->download_count;
2816 else
2818 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2819 wined3d_debug_location(sub_resource->locations));
2820 return FALSE;
2822 break;
2824 case WINED3D_LOCATION_BUFFER:
2825 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2827 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2829 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2830 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2831 else
2832 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2834 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
2836 else
2838 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2839 wined3d_debug_location(sub_resource->locations));
2840 return FALSE;
2842 break;
2844 default:
2845 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2846 wined3d_debug_location(sub_resource->locations));
2847 return FALSE;
2850 return TRUE;
2853 static const struct wined3d_texture_ops texture1d_ops =
2855 texture1d_load_location,
2858 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2859 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2860 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
2861 const struct wined3d_texture_ops *texture_ops)
2863 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2864 struct wined3d_device_parent *device_parent = device->device_parent;
2865 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2866 unsigned int sub_count, i, j, size, offset = 0;
2867 unsigned int pow2_width, pow2_height;
2868 const struct wined3d_format *format;
2869 HRESULT hr;
2871 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2872 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2873 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
2874 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
2875 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
2876 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
2877 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
2879 if (!desc->width || !desc->height || !desc->depth)
2880 return WINED3DERR_INVALIDCALL;
2882 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2884 if (layer_count != 1)
2886 ERR("Invalid layer count for volume texture.\n");
2887 return E_INVALIDARG;
2890 if (!gl_info->supported[EXT_TEXTURE3D])
2892 WARN("OpenGL implementation does not support 3D textures.\n");
2893 return WINED3DERR_INVALIDCALL;
2897 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
2898 && !gl_info->supported[EXT_TEXTURE_ARRAY])
2900 WARN("OpenGL implementation does not support array textures.\n");
2901 return WINED3DERR_INVALIDCALL;
2904 texture->sub_resources = sub_resources;
2906 /* TODO: It should only be possible to create textures for formats
2907 * that are reported as supported. */
2908 if (WINED3DFMT_UNKNOWN >= desc->format)
2910 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2911 return WINED3DERR_INVALIDCALL;
2913 format = wined3d_get_format(device->adapter, desc->format, desc->usage);
2915 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
2916 || desc->usage & WINED3DUSAGE_SCRATCH))
2918 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2919 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
2920 return WINED3DERR_INVALIDCALL;
2923 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2924 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2925 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2926 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->access & WINED3D_RESOURCE_ACCESS_CPU)
2927 FIXME("Trying to create a CPU accessible render target.\n");
2929 pow2_width = desc->width;
2930 pow2_height = desc->height;
2931 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
2932 && !d3d_info->texture_npot)
2934 /* level_count == 0 returns an error as well. */
2935 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2937 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
2939 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2940 "texture without unconditional NPOT support.\n");
2941 return WINED3DERR_INVALIDCALL;
2944 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2946 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2948 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
2950 /* TODO: Add support for non-power-of-two compressed textures. */
2951 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2952 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2954 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2955 desc->width, desc->height);
2956 return WINED3DERR_NOTAVAILABLE;
2959 /* Find the nearest pow2 match. */
2960 pow2_width = pow2_height = 1;
2961 while (pow2_width < desc->width)
2962 pow2_width <<= 1;
2963 while (pow2_height < desc->height)
2964 pow2_height <<= 1;
2965 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2968 texture->pow2_width = pow2_width;
2969 texture->pow2_height = pow2_height;
2971 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
2972 && (desc->usage & WINED3DUSAGE_TEXTURE))
2974 /* One of four options:
2975 * 1: Do the same as we do with NPOT and scale the texture. (Any
2976 * texture ops would require the texture to be scaled which is
2977 * potentially slow.)
2978 * 2: Set the texture to the maximum size (bad idea).
2979 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2980 * 4: Create the surface, but allow it to be used only for DirectDraw
2981 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2982 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2983 * the render target. */
2984 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
2986 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2987 return WINED3DERR_NOTAVAILABLE;
2990 /* We should never use this surface in combination with OpenGL. */
2991 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2994 for (i = 0; i < layer_count; ++i)
2996 for (j = 0; j < level_count; ++j)
2998 unsigned int idx = i * level_count + j;
3000 size = wined3d_format_calculate_size(format, device->surface_alignment,
3001 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
3002 texture->sub_resources[idx].offset = offset;
3003 texture->sub_resources[idx].size = size;
3004 offset += size;
3006 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
3009 if (!offset)
3010 return WINED3DERR_INVALIDCALL;
3012 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
3013 desc->multisample_type, desc->multisample_quality, desc->usage, desc->access,
3014 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3016 static unsigned int once;
3018 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3019 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3020 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3021 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3022 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3023 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3025 WARN("Failed to initialize resource, returning %#x\n", hr);
3026 return hr;
3028 wined3d_resource_update_draw_binding(&texture->resource);
3029 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
3030 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
3032 texture->texture_ops = texture_ops;
3034 texture->layer_count = layer_count;
3035 texture->level_count = level_count;
3036 texture->lod = 0;
3037 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
3038 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3039 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3040 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3041 texture->flags |= WINED3D_TEXTURE_GET_DC;
3042 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3043 texture->flags |= WINED3D_TEXTURE_DISCARD;
3044 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3046 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3047 WARN("Format doesn't support mipmaps generation, "
3048 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3049 else
3050 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3053 switch (desc->resource_type)
3055 case WINED3D_RTYPE_TEXTURE_1D:
3056 if (layer_count > 1)
3057 texture->target = GL_TEXTURE_1D_ARRAY;
3058 else
3059 texture->target = GL_TEXTURE_1D;
3060 break;
3062 case WINED3D_RTYPE_TEXTURE_2D:
3063 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
3065 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
3067 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
3069 if (layer_count > 1)
3070 texture->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
3071 else
3072 texture->target = GL_TEXTURE_2D_MULTISAMPLE;
3074 else
3076 if (layer_count > 1)
3077 texture->target = GL_TEXTURE_2D_ARRAY;
3078 else
3079 texture->target = GL_TEXTURE_2D;
3081 break;
3083 case WINED3D_RTYPE_TEXTURE_3D:
3084 texture->target = GL_TEXTURE_3D;
3085 break;
3087 default:
3088 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
3089 wined3d_texture_cleanup_sync(texture);
3090 return WINED3DERR_INVALIDCALL;
3093 /* Precalculated scaling for 'faked' non power of two texture coords. */
3094 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3096 texture->pow2_matrix[0] = (float)desc->width;
3097 texture->pow2_matrix[5] = (float)desc->height;
3098 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3099 texture->target = GL_TEXTURE_RECTANGLE_ARB;
3101 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3103 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3104 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3105 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3107 else
3109 texture->pow2_matrix[0] = 1.0f;
3110 texture->pow2_matrix[5] = 1.0f;
3112 texture->pow2_matrix[10] = 1.0f;
3113 texture->pow2_matrix[15] = 1.0f;
3114 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3116 if (wined3d_texture_use_pbo(texture, gl_info))
3117 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3119 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3120 || !wined3d_texture_use_pbo(texture, gl_info))
3122 if (!wined3d_resource_allocate_sysmem(&texture->resource))
3124 wined3d_texture_cleanup_sync(texture);
3125 return E_OUTOFMEMORY;
3129 sub_count = level_count * layer_count;
3130 if (sub_count / layer_count != level_count)
3132 wined3d_texture_cleanup_sync(texture);
3133 return E_OUTOFMEMORY;
3136 if (desc->usage & WINED3DUSAGE_OVERLAY)
3138 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3140 wined3d_texture_cleanup_sync(texture);
3141 return E_OUTOFMEMORY;
3144 for (i = 0; i < sub_count; ++i)
3146 list_init(&texture->overlay_info[i].entry);
3147 list_init(&texture->overlay_info[i].overlays);
3151 /* Generate all sub-resources. */
3152 for (i = 0; i < sub_count; ++i)
3154 struct wined3d_texture_sub_resource *sub_resource;
3156 sub_resource = &texture->sub_resources[i];
3157 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3158 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3160 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3161 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3164 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3165 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3167 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3168 sub_resource->parent = NULL;
3169 wined3d_texture_cleanup_sync(texture);
3170 return hr;
3173 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3175 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3176 i, i % texture->level_count, i / texture->level_count);
3178 if (desc->usage & WINED3DUSAGE_OWNDC)
3180 struct wined3d_texture_idx texture_idx = {texture, i};
3182 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3183 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3184 if (!texture->dc_info || !texture->dc_info[i].dc)
3186 wined3d_texture_cleanup_sync(texture);
3187 return WINED3DERR_INVALIDCALL;
3192 return WINED3D_OK;
3195 /* Context activation is done by the caller. */
3196 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3197 struct wined3d_context *context, BOOL dest_is_srgb)
3199 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3200 unsigned int row_pitch, slice_pitch;
3201 struct wined3d_bo_address data;
3202 struct wined3d_box src_box;
3204 /* Optimisations are possible, but the effort should be put into either
3205 * implementing EXT_SRGB_DECODE in the driver or finding out why we
3206 * picked the wrong copy for the original upload and fixing that.
3208 * Also keep in mind that we want to avoid using resource.heap_memory
3209 * for DEFAULT pool surfaces. */
3210 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
3211 data.buffer_object = 0;
3212 if (!(data.addr = heap_alloc(sub_resource->size)))
3213 return;
3215 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3216 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3217 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
3218 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3219 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
3220 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3221 &src_box, wined3d_const_bo_address(&data), row_pitch, slice_pitch, 0, 0, 0, FALSE);
3223 heap_free(data.addr);
3226 /* Context activation is done by the caller. */
3227 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3228 struct wined3d_context *context, DWORD location)
3230 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3231 unsigned int row_pitch, slice_pitch;
3233 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
3234 return FALSE;
3236 switch (location)
3238 case WINED3D_LOCATION_TEXTURE_RGB:
3239 case WINED3D_LOCATION_TEXTURE_SRGB:
3240 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
3242 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
3243 struct wined3d_box src_box;
3245 data.addr += sub_resource->offset;
3246 wined3d_texture_bind_and_dirtify(texture, context,
3247 location == WINED3D_LOCATION_TEXTURE_SRGB);
3248 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3249 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3250 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3251 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
3253 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
3255 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
3256 struct wined3d_box src_box;
3258 wined3d_texture_bind_and_dirtify(texture, context,
3259 location == WINED3D_LOCATION_TEXTURE_SRGB);
3260 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3261 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3262 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3263 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
3265 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3267 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
3269 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
3271 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
3273 else
3275 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
3276 return FALSE;
3278 break;
3280 case WINED3D_LOCATION_SYSMEM:
3281 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3283 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
3285 data.addr += sub_resource->offset;
3286 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3287 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
3288 else
3289 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
3291 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3292 ++texture->download_count;
3294 else
3296 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
3297 wined3d_debug_location(sub_resource->locations));
3298 return FALSE;
3300 break;
3302 case WINED3D_LOCATION_BUFFER:
3303 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3305 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
3307 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3308 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
3309 else
3310 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
3312 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3314 else
3316 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
3317 wined3d_debug_location(sub_resource->locations));
3318 return FALSE;
3320 break;
3322 default:
3323 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
3324 wined3d_debug_location(sub_resource->locations));
3325 return FALSE;
3328 return TRUE;
3331 static const struct wined3d_texture_ops texture3d_ops =
3333 texture3d_load_location,
3336 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3337 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3338 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3340 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3341 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3342 unsigned int dst_format_flags, src_format_flags = 0;
3343 HRESULT hr;
3345 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3346 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3347 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3348 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3350 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3351 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3352 return WINED3DERR_INVALIDCALL;
3354 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3355 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3356 return WINED3DERR_INVALIDCALL;
3358 dst_format_flags = dst_texture->resource.format_flags;
3359 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3360 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3361 return hr;
3363 src_format_flags = src_texture->resource.format_flags;
3364 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3365 src_sub_resource_idx % src_texture->level_count, &src_box)))
3366 return hr;
3368 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3369 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3371 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3372 return WINEDDERR_SURFACEBUSY;
3375 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
3376 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
3378 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3379 return WINED3DERR_INVALIDCALL;
3382 if (dst_texture->resource.device != src_texture->resource.device)
3384 FIXME("Rejecting cross-device blit.\n");
3385 return E_NOTIMPL;
3388 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3389 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3391 return WINED3D_OK;
3394 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3395 unsigned int sub_resource_idx, LONG *x, LONG *y)
3397 struct wined3d_overlay_info *overlay;
3399 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3401 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3402 || sub_resource_idx >= texture->level_count * texture->layer_count)
3404 WARN("Invalid sub-resource specified.\n");
3405 return WINEDDERR_NOTAOVERLAYSURFACE;
3408 overlay = &texture->overlay_info[sub_resource_idx];
3409 if (!overlay->dst_texture)
3411 TRACE("Overlay not visible.\n");
3412 *x = 0;
3413 *y = 0;
3414 return WINEDDERR_OVERLAYNOTVISIBLE;
3417 *x = overlay->dst_rect.left;
3418 *y = overlay->dst_rect.top;
3420 TRACE("Returning position %d, %d.\n", *x, *y);
3422 return WINED3D_OK;
3425 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3426 unsigned int sub_resource_idx, LONG x, LONG y)
3428 struct wined3d_overlay_info *overlay;
3429 LONG w, h;
3431 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3433 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3434 || sub_resource_idx >= texture->level_count * texture->layer_count)
3436 WARN("Invalid sub-resource specified.\n");
3437 return WINEDDERR_NOTAOVERLAYSURFACE;
3440 overlay = &texture->overlay_info[sub_resource_idx];
3441 w = overlay->dst_rect.right - overlay->dst_rect.left;
3442 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3443 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3445 return WINED3D_OK;
3448 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3449 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3450 const RECT *dst_rect, DWORD flags)
3452 struct wined3d_overlay_info *overlay;
3453 unsigned int level, dst_level;
3455 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3456 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3457 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3458 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3460 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3461 || sub_resource_idx >= texture->level_count * texture->layer_count)
3463 WARN("Invalid sub-resource specified.\n");
3464 return WINEDDERR_NOTAOVERLAYSURFACE;
3467 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3468 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3470 WARN("Invalid destination sub-resource specified.\n");
3471 return WINED3DERR_INVALIDCALL;
3474 overlay = &texture->overlay_info[sub_resource_idx];
3476 level = sub_resource_idx % texture->level_count;
3477 if (src_rect)
3478 overlay->src_rect = *src_rect;
3479 else
3480 SetRect(&overlay->src_rect, 0, 0,
3481 wined3d_texture_get_level_width(texture, level),
3482 wined3d_texture_get_level_height(texture, level));
3484 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3485 if (dst_rect)
3486 overlay->dst_rect = *dst_rect;
3487 else
3488 SetRect(&overlay->dst_rect, 0, 0,
3489 wined3d_texture_get_level_width(dst_texture, dst_level),
3490 wined3d_texture_get_level_height(dst_texture, dst_level));
3492 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
3493 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
3495 overlay->dst_texture = NULL;
3496 list_remove(&overlay->entry);
3499 if (flags & WINEDDOVER_SHOW)
3501 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
3503 overlay->dst_texture = dst_texture;
3504 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
3505 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
3508 else if (flags & WINEDDOVER_HIDE)
3510 /* Tests show that the rectangles are erased on hide. */
3511 SetRectEmpty(&overlay->src_rect);
3512 SetRectEmpty(&overlay->dst_rect);
3513 overlay->dst_texture = NULL;
3516 return WINED3D_OK;
3519 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
3521 unsigned int sub_count = texture->level_count * texture->layer_count;
3523 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
3525 if (sub_resource_idx >= sub_count)
3527 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3528 return NULL;
3531 return texture->sub_resources[sub_resource_idx].parent;
3534 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
3535 unsigned int sub_resource_idx, void *parent)
3537 unsigned int sub_count = texture->level_count * texture->layer_count;
3539 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
3541 if (sub_resource_idx >= sub_count)
3543 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3544 return;
3547 texture->sub_resources[sub_resource_idx].parent = parent;
3550 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
3551 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3553 unsigned int sub_count = texture->level_count * texture->layer_count;
3554 const struct wined3d_resource *resource;
3555 unsigned int level_idx;
3557 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
3559 if (sub_resource_idx >= sub_count)
3561 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3562 return WINED3DERR_INVALIDCALL;
3565 resource = &texture->resource;
3566 desc->format = resource->format->id;
3567 desc->multisample_type = resource->multisample_type;
3568 desc->multisample_quality = resource->multisample_quality;
3569 desc->usage = resource->usage;
3570 desc->access = resource->access;
3572 level_idx = sub_resource_idx % texture->level_count;
3573 desc->width = wined3d_texture_get_level_width(texture, level_idx);
3574 desc->height = wined3d_texture_get_level_height(texture, level_idx);
3575 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
3576 desc->size = texture->sub_resources[sub_resource_idx].size;
3578 return WINED3D_OK;
3581 static void *wined3d_texture_allocate_object_memory(SIZE_T s, SIZE_T level_count, SIZE_T layer_count)
3583 struct wined3d_texture *t;
3585 if (level_count > ((~(SIZE_T)0 - s) / sizeof(*t->sub_resources)) / layer_count)
3586 return NULL;
3588 return heap_alloc_zero(s + level_count * layer_count * sizeof(*t->sub_resources));
3591 static HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
3592 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
3593 DWORD flags, void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources)
3595 const struct wined3d_texture_ops *texture_ops;
3597 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, level_count %u, "
3598 "flags %#x, parent %p, parent_ops %p, sub_resources %p.\n",
3599 texture_gl, device, desc, layer_count, level_count,
3600 flags, parent, parent_ops, sub_resources);
3602 switch (desc->resource_type)
3604 case WINED3D_RTYPE_TEXTURE_1D:
3605 texture_ops = &texture1d_ops;
3606 break;
3607 case WINED3D_RTYPE_TEXTURE_2D:
3608 texture_ops = &texture2d_ops;
3609 break;
3610 case WINED3D_RTYPE_TEXTURE_3D:
3611 texture_ops = &texture3d_ops;
3612 break;
3613 default:
3614 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
3615 return WINED3DERR_INVALIDCALL;
3618 list_init(&texture_gl->renderbuffers);
3620 return wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
3621 flags, device, parent, parent_ops, sub_resources, texture_ops);
3624 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
3625 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
3626 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
3628 unsigned int sub_count = level_count * layer_count;
3629 struct wined3d_texture_gl *object;
3630 unsigned int i;
3631 HRESULT hr;
3633 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3634 "parent %p, parent_ops %p, texture %p.\n",
3635 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
3637 if (!layer_count)
3639 WARN("Invalid layer count.\n");
3640 return E_INVALIDARG;
3642 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
3644 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
3645 layer_count = 6;
3648 if (!level_count)
3650 WARN("Invalid level count.\n");
3651 return WINED3DERR_INVALIDCALL;
3654 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
3656 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->usage);
3658 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
3659 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
3661 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3662 desc->multisample_quality);
3663 return WINED3DERR_NOTAVAILABLE;
3665 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
3666 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
3667 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
3669 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
3670 desc->multisample_quality);
3671 return WINED3DERR_NOTAVAILABLE;
3675 if (data)
3677 for (i = 0; i < sub_count; ++i)
3679 if (data[i].data)
3680 continue;
3682 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
3683 return E_INVALIDARG;
3687 if (!(object = wined3d_texture_allocate_object_memory(sizeof(*object), level_count, layer_count)))
3688 return E_OUTOFMEMORY;
3690 if (FAILED(hr = wined3d_texture_gl_init(object, device, desc, layer_count,
3691 level_count, flags, parent, parent_ops, &object[1])))
3693 WARN("Failed to initialize texture, returning %#x.\n", hr);
3694 heap_free(object);
3695 return hr;
3698 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3699 * in this case. */
3700 if (data)
3702 unsigned int level, width, height, depth;
3703 struct wined3d_box box;
3705 for (i = 0; i < sub_count; ++i)
3707 level = i % object->t.level_count;
3708 width = wined3d_texture_get_level_width(&object->t, level);
3709 height = wined3d_texture_get_level_height(&object->t, level);
3710 depth = wined3d_texture_get_level_depth(&object->t, level);
3711 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3713 wined3d_cs_emit_update_sub_resource(device->cs, &object->t.resource,
3714 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
3718 TRACE("Created texture %p.\n", object);
3719 *texture = &object->t;
3721 return WINED3D_OK;
3724 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
3726 struct wined3d_device *device = texture->resource.device;
3727 struct wined3d_texture_sub_resource *sub_resource;
3728 struct wined3d_dc_info *dc_info;
3730 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3732 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
3734 WARN("Texture does not support GetDC\n");
3735 /* Don't touch the DC */
3736 return WINED3DERR_INVALIDCALL;
3739 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3740 return WINED3DERR_INVALIDCALL;
3742 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3744 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3745 return WINED3DERR_INVALIDCALL;
3748 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3749 return WINED3DERR_INVALIDCALL;
3751 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3753 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3755 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3756 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3757 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3758 return WINED3DERR_INVALIDCALL;
3761 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3762 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3763 ++texture->resource.map_count;
3764 ++sub_resource->map_count;
3766 *dc = dc_info[sub_resource_idx].dc;
3767 TRACE("Returning dc %p.\n", *dc);
3769 return WINED3D_OK;
3772 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3774 struct wined3d_device *device = texture->resource.device;
3775 struct wined3d_texture_sub_resource *sub_resource;
3776 struct wined3d_dc_info *dc_info;
3778 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3780 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3781 return WINED3DERR_INVALIDCALL;
3783 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3785 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3786 return WINED3DERR_INVALIDCALL;
3789 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3790 return WINED3DERR_INVALIDCALL;
3792 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
3794 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3795 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
3796 return WINED3DERR_INVALIDCALL;
3799 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
3801 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3803 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
3804 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3807 --sub_resource->map_count;
3808 if (!--texture->resource.map_count && texture->update_map_binding)
3809 wined3d_texture_update_map_binding(texture);
3810 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3811 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3813 return WINED3D_OK;
3816 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3817 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
3818 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3820 unsigned int src_row_pitch, src_slice_pitch;
3821 unsigned int update_w, update_h, update_d;
3822 unsigned int src_level, dst_level;
3823 struct wined3d_context *context;
3824 struct wined3d_bo_address data;
3826 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3827 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
3828 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3829 src_texture, src_sub_resource_idx, debug_box(src_box));
3831 context = context_acquire(dst_texture->resource.device, NULL, 0);
3833 /* Only load the sub-resource for partial updates. For newly allocated
3834 * textures the texture wouldn't be the current location, and we'd upload
3835 * zeroes just to overwrite them again. */
3836 update_w = src_box->right - src_box->left;
3837 update_h = src_box->bottom - src_box->top;
3838 update_d = src_box->back - src_box->front;
3839 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3840 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
3841 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
3842 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
3843 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3844 else
3845 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3846 wined3d_texture_bind_and_dirtify(dst_texture, context, FALSE);
3848 src_level = src_sub_resource_idx % src_texture->level_count;
3849 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
3850 src_texture->sub_resources[src_sub_resource_idx].locations);
3851 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
3853 wined3d_texture_upload_data(dst_texture, dst_sub_resource_idx, context, src_texture->resource.format,
3854 src_box, wined3d_const_bo_address(&data), src_row_pitch, src_slice_pitch, dst_x, dst_y, dst_z, FALSE);
3856 context_release(context);
3858 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
3859 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
3862 /* Partial downloads are not supported. */
3863 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3864 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
3866 struct wined3d_context *context;
3867 struct wined3d_bo_address data;
3868 DWORD dst_location = dst_texture->resource.map_binding;
3870 context = context_acquire(src_texture->resource.device, NULL, 0);
3872 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
3873 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
3874 wined3d_texture_bind_and_dirtify(src_texture, context,
3875 !(src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB));
3876 wined3d_texture_download_data(src_texture, src_sub_resource_idx, context, &data);
3878 context_release(context);
3880 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
3881 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);