wined3d: Store resource bind flags in the wined3d_resource structure.
[wine.git] / dlls / wined3d / texture.c
blob87998d5552e71cba2f02d11bb84ce7c9c2c4fa10
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_gl *texture_gl,
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_gl->t.level_count;
166 w = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
167 h = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
168 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, 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_gl_allocate_mutable_storage(struct wined3d_texture_gl *texture_gl,
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_gl->t.level_count;
576 if (texture_gl->target == GL_TEXTURE_1D_ARRAY || texture_gl->target == GL_TEXTURE_2D_ARRAY)
577 layer_count = 1;
578 else
579 layer_count = texture_gl->t.layer_count;
581 for (layer = 0; layer < layer_count; ++layer)
583 target = wined3d_texture_gl_get_sub_resource_target(texture_gl, layer * level_count);
585 for (level = 0; level < level_count; ++level)
587 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
588 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
589 if (texture_gl->t.resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
591 height *= format->f.height_scale.numerator;
592 height /= format->f.height_scale.denominator;
595 TRACE("texture_gl %p, layer %u, level %u, target %#x, width %u, height %u.\n",
596 texture_gl, layer, level, target, width, height);
598 if (target == GL_TEXTURE_3D || target == GL_TEXTURE_2D_ARRAY)
600 depth = wined3d_texture_get_level_depth(&texture_gl->t, level);
601 GL_EXTCALL(glTexImage3D(target, level, gl_internal_format, width, height,
602 target == GL_TEXTURE_2D_ARRAY ? texture_gl->t.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_gl->t.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_gl_allocate_immutable_storage(struct wined3d_texture_gl *texture_gl,
625 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
627 unsigned int samples = wined3d_texture_get_gl_sample_count(&texture_gl->t);
628 GLsizei height = wined3d_texture_get_level_pow2_height(&texture_gl->t, 0);
629 GLsizei width = wined3d_texture_get_level_pow2_width(&texture_gl->t, 0);
630 GLboolean standard_pattern = texture_gl->t.resource.multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
631 && texture_gl->t.resource.multisample_quality == WINED3D_STANDARD_MULTISAMPLE_PATTERN;
633 switch (texture_gl->target)
635 case GL_TEXTURE_3D:
636 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
637 gl_internal_format, width, height, wined3d_texture_get_level_depth(&texture_gl->t, 0)));
638 break;
639 case GL_TEXTURE_2D_ARRAY:
640 GL_EXTCALL(glTexStorage3D(texture_gl->target, texture_gl->t.level_count,
641 gl_internal_format, width, height, texture_gl->t.layer_count));
642 break;
643 case GL_TEXTURE_2D_MULTISAMPLE:
644 GL_EXTCALL(glTexStorage2DMultisample(texture_gl->target, samples,
645 gl_internal_format, width, height, standard_pattern));
646 break;
647 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
648 GL_EXTCALL(glTexStorage3DMultisample(texture_gl->target, samples,
649 gl_internal_format, width, height, texture_gl->t.layer_count, standard_pattern));
650 break;
651 case GL_TEXTURE_1D_ARRAY:
652 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
653 gl_internal_format, width, texture_gl->t.layer_count));
654 break;
655 case GL_TEXTURE_1D:
656 GL_EXTCALL(glTexStorage1D(texture_gl->target, texture_gl->t.level_count, gl_internal_format, width));
657 break;
658 default:
659 GL_EXTCALL(glTexStorage2D(texture_gl->target, texture_gl->t.level_count,
660 gl_internal_format, width, height));
661 break;
664 checkGLcall("allocate immutable storage");
667 static void wined3d_texture_gl_unload_texture(struct wined3d_texture_gl *texture_gl)
669 struct wined3d_device *device = texture_gl->t.resource.device;
670 const struct wined3d_gl_info *gl_info = NULL;
671 struct wined3d_context *context = NULL;
673 if (texture_gl->texture_rgb.name || texture_gl->texture_srgb.name
674 || texture_gl->rb_multisample || texture_gl->rb_resolved)
676 context = context_acquire(device, NULL, 0);
677 gl_info = context->gl_info;
680 if (texture_gl->texture_rgb.name)
681 gltexture_delete(device, context->gl_info, &texture_gl->texture_rgb);
683 if (texture_gl->texture_srgb.name)
684 gltexture_delete(device, context->gl_info, &texture_gl->texture_srgb);
686 if (texture_gl->rb_multisample)
688 TRACE("Deleting multisample renderbuffer %u.\n", texture_gl->rb_multisample);
689 context_gl_resource_released(device, texture_gl->rb_multisample, TRUE);
690 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_multisample);
691 texture_gl->rb_multisample = 0;
694 if (texture_gl->rb_resolved)
696 TRACE("Deleting resolved renderbuffer %u.\n", texture_gl->rb_resolved);
697 context_gl_resource_released(device, texture_gl->rb_resolved, TRUE);
698 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture_gl->rb_resolved);
699 texture_gl->rb_resolved = 0;
702 if (context) context_release(context);
704 wined3d_texture_set_dirty(&texture_gl->t);
706 resource_unload(&texture_gl->t.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);
908 static void wined3d_texture_gl_cleanup(struct wined3d_texture_gl *texture_gl)
910 struct wined3d_device *device = texture_gl->t.resource.device;
911 struct wined3d_renderbuffer_entry *entry, *entry2;
912 const struct wined3d_gl_info *gl_info;
913 struct wined3d_context *context;
915 if (!list_empty(&texture_gl->renderbuffers))
917 context = context_acquire(device, NULL, 0);
918 gl_info = context->gl_info;
920 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
922 TRACE("Deleting renderbuffer %u.\n", entry->id);
923 context_gl_resource_released(device, entry->id, TRUE);
924 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
925 heap_free(entry);
928 context_release(context);
931 wined3d_texture_cleanup(&texture_gl->t);
932 wined3d_texture_gl_unload_texture(texture_gl);
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_gl_bind(struct wined3d_texture_gl *texture_gl,
961 struct wined3d_context *context, BOOL srgb)
963 const struct wined3d_format *format = texture_gl->t.resource.format;
964 const struct color_fixup_desc fixup = format->color_fixup;
965 const struct wined3d_gl_info *gl_info = context->gl_info;
966 struct gl_texture *gl_tex;
967 GLenum target;
969 TRACE("texture_gl %p, context %p, srgb %#x.\n", texture_gl, context, srgb);
971 if (!needs_separate_srgb_gl_texture(context, &texture_gl->t))
972 srgb = FALSE;
974 /* sRGB mode cache for preload() calls outside drawprim. */
975 if (srgb)
976 texture_gl->t.flags |= WINED3D_TEXTURE_IS_SRGB;
977 else
978 texture_gl->t.flags &= ~WINED3D_TEXTURE_IS_SRGB;
980 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
981 target = texture_gl->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_gl->t);
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_gl->t.level_count - 1);
1032 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture_gl->t.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_gl->t.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_gl_bind_and_dirtify(struct wined3d_texture_gl *texture_gl,
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_gl_bind(texture_gl, 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_gl_apply_sampler_desc(struct wined3d_texture_gl *texture_gl,
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_gl->target;
1116 struct gl_texture *gl_tex;
1117 DWORD state;
1119 TRACE("texture_gl %p, sampler_desc %p, context %p.\n", texture_gl, sampler_desc, context);
1121 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, texture_gl->t.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 struct wined3d_resource *resource;
1406 DWORD old = texture->lod;
1408 TRACE("texture %p, lod %u.\n", texture, lod);
1410 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1411 * textures. The call always returns 0, and GetLOD always returns 0. */
1412 resource = &texture->resource;
1413 if (!wined3d_resource_access_is_managed(resource->access))
1415 TRACE("Ignoring LOD on texture with resource access %s.\n",
1416 wined3d_debug_resource_access(resource->access));
1417 return 0;
1420 if (lod >= texture->level_count)
1421 lod = texture->level_count - 1;
1423 if (texture->lod != lod)
1425 struct wined3d_device *device = resource->device;
1427 wined3d_resource_wait_idle(resource);
1428 texture->lod = lod;
1430 wined3d_texture_gl(texture)->texture_rgb.base_level = ~0u;
1431 wined3d_texture_gl(texture)->texture_srgb.base_level = ~0u;
1432 if (resource->bind_count)
1433 wined3d_cs_emit_set_sampler_state(device->cs, texture->sampler, WINED3D_SAMP_MAX_MIP_LEVEL,
1434 device->state.sampler_states[texture->sampler][WINED3D_SAMP_MAX_MIP_LEVEL]);
1437 return old;
1440 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1442 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1444 return texture->lod;
1447 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1449 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1451 return texture->level_count;
1454 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1455 DWORD flags, const struct wined3d_color_key *color_key)
1457 struct wined3d_device *device = texture->resource.device;
1458 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1459 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1461 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1463 if (flags & ~all_flags)
1465 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1466 return WINED3DERR_INVALIDCALL;
1469 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1471 return WINED3D_OK;
1474 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1475 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1476 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1477 /* Context activation is done by the caller. */
1478 void wined3d_texture_gl_set_compatible_renderbuffer(struct wined3d_texture_gl *texture_gl,
1479 struct wined3d_context *context, unsigned int level, const struct wined3d_rendertarget_info *rt)
1481 const struct wined3d_gl_info *gl_info = context->gl_info;
1482 struct wined3d_renderbuffer_entry *entry;
1483 unsigned int src_width, src_height;
1484 unsigned int width, height;
1485 GLuint renderbuffer = 0;
1487 if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT])
1488 return;
1490 if (rt && rt->resource->format->id != WINED3DFMT_NULL)
1492 struct wined3d_texture *rt_texture;
1493 unsigned int rt_level;
1495 if (rt->resource->type == WINED3D_RTYPE_BUFFER)
1497 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt->resource->type));
1498 return;
1500 rt_texture = wined3d_texture_from_resource(rt->resource);
1501 rt_level = rt->sub_resource_idx % rt_texture->level_count;
1503 width = wined3d_texture_get_level_pow2_width(rt_texture, rt_level);
1504 height = wined3d_texture_get_level_pow2_height(rt_texture, rt_level);
1506 else
1508 width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1509 height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1512 src_width = wined3d_texture_get_level_pow2_width(&texture_gl->t, level);
1513 src_height = wined3d_texture_get_level_pow2_height(&texture_gl->t, level);
1515 /* A depth stencil smaller than the render target is not valid */
1516 if (width > src_width || height > src_height)
1517 return;
1519 /* Remove any renderbuffer set if the sizes match */
1520 if (width == src_width && height == src_height)
1522 texture_gl->current_renderbuffer = NULL;
1523 return;
1526 /* Look if we've already got a renderbuffer of the correct dimensions */
1527 LIST_FOR_EACH_ENTRY(entry, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1529 if (entry->width == width && entry->height == height)
1531 renderbuffer = entry->id;
1532 texture_gl->current_renderbuffer = entry;
1533 break;
1537 if (!renderbuffer)
1539 const struct wined3d_format_gl *format_gl;
1541 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1542 gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1543 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1544 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal, width, height);
1546 entry = heap_alloc(sizeof(*entry));
1547 entry->width = width;
1548 entry->height = height;
1549 entry->id = renderbuffer;
1550 list_add_head(&texture_gl->renderbuffers, &entry->entry);
1552 texture_gl->current_renderbuffer = entry;
1555 checkGLcall("set compatible renderbuffer");
1558 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1559 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1560 UINT multisample_quality, void *mem, UINT pitch)
1562 struct wined3d_texture_sub_resource *sub_resource;
1563 const struct wined3d_d3d_info *d3d_info;
1564 const struct wined3d_gl_info *gl_info;
1565 const struct wined3d_format *format;
1566 struct wined3d_device *device;
1567 unsigned int resource_size;
1568 DWORD valid_location = 0;
1569 BOOL create_dib = FALSE;
1571 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1572 "mem %p, pitch %u.\n",
1573 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1575 device = texture->resource.device;
1576 gl_info = &device->adapter->gl_info;
1577 d3d_info = &device->adapter->d3d_info;
1578 format = wined3d_get_format(device->adapter, format_id, texture->resource.usage);
1579 resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1581 if (!resource_size)
1582 return WINED3DERR_INVALIDCALL;
1584 if (texture->level_count * texture->layer_count > 1)
1586 WARN("Texture has multiple sub-resources, not supported.\n");
1587 return WINED3DERR_INVALIDCALL;
1590 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
1592 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture->resource.type));
1593 return WINED3DERR_INVALIDCALL;
1596 if (texture->resource.map_count)
1598 WARN("Texture is mapped.\n");
1599 return WINED3DERR_INVALIDCALL;
1602 /* We have no way of supporting a pitch that is not a multiple of the pixel
1603 * byte width short of uploading the texture row-by-row.
1604 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1605 * for user-memory textures (it always expects packed data) while DirectDraw
1606 * requires a 4-byte aligned pitch and doesn't support texture formats
1607 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1608 * This check is here to verify that the assumption holds. */
1609 if (pitch % texture->resource.format->byte_count)
1611 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1612 return WINED3DERR_INVALIDCALL;
1615 if (device->d3d_initialized)
1616 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1617 wined3d_resource_wait_idle(&texture->resource);
1619 sub_resource = &texture->sub_resources[0];
1620 if (texture->dc_info && texture->dc_info[0].dc)
1622 struct wined3d_texture_idx texture_idx = {texture, 0};
1624 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
1625 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1626 create_dib = TRUE;
1629 wined3d_resource_free_sysmem(&texture->resource);
1631 if ((texture->row_pitch = pitch))
1632 texture->slice_pitch = height * pitch;
1633 else
1634 /* User memory surfaces don't have the regular surface alignment. */
1635 wined3d_format_calculate_pitch(format, 1, width, height,
1636 &texture->row_pitch, &texture->slice_pitch);
1638 texture->resource.format = format;
1639 texture->resource.multisample_type = multisample_type;
1640 texture->resource.multisample_quality = multisample_quality;
1641 texture->resource.width = width;
1642 texture->resource.height = height;
1643 texture->resource.size = texture->slice_pitch;
1644 sub_resource->size = texture->slice_pitch;
1645 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1647 if (multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
1648 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D_MULTISAMPLE;
1649 else
1650 wined3d_texture_gl(texture)->target = GL_TEXTURE_2D;
1652 if (((width & (width - 1)) || (height & (height - 1))) && !d3d_info->texture_npot
1653 && !d3d_info->texture_npot_conditional)
1655 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1656 texture->pow2_width = texture->pow2_height = 1;
1657 while (texture->pow2_width < width)
1658 texture->pow2_width <<= 1;
1659 while (texture->pow2_height < height)
1660 texture->pow2_height <<= 1;
1662 else
1664 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1665 texture->pow2_width = width;
1666 texture->pow2_height = height;
1669 if ((texture->user_memory = mem))
1671 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1672 valid_location = WINED3D_LOCATION_USER_MEMORY;
1674 else
1676 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1677 valid_location = WINED3D_LOCATION_SYSMEM;
1680 /* The format might be changed to a format that needs conversion.
1681 * If the surface didn't use PBOs previously but could now, don't
1682 * change it - whatever made us not use PBOs might come back, e.g.
1683 * color keys. */
1684 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1685 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1687 wined3d_texture_validate_location(texture, 0, valid_location);
1688 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1690 if (create_dib)
1692 struct wined3d_texture_idx texture_idx = {texture, 0};
1694 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
1695 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
1698 return WINED3D_OK;
1701 /* Context activation is done by the caller. */
1702 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1703 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1705 struct wined3d_texture_sub_resource *sub_resource;
1707 sub_resource = &texture->sub_resources[sub_resource_idx];
1708 if (sub_resource->buffer_object)
1709 return;
1711 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1712 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1713 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1714 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1715 checkGLcall("Create buffer object");
1717 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1718 sub_resource->buffer_object, texture, sub_resource_idx);
1721 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1723 unsigned int sub_count = texture->level_count * texture->layer_count;
1724 unsigned int i;
1726 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1727 | WINED3D_TEXTURE_CONVERTED);
1728 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1729 for (i = 0; i < sub_count; ++i)
1731 wined3d_texture_invalidate_location(texture, i,
1732 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1736 /* Context activation is done by the caller. */
1737 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1739 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1740 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1741 const struct wined3d_gl_info *gl_info = context->gl_info;
1742 struct wined3d_resource *resource = &texture->resource;
1743 const struct wined3d_device *device = resource->device;
1744 const struct wined3d_format *format = resource->format;
1745 const struct wined3d_color_key_conversion *conversion;
1746 const struct wined3d_format_gl *format_gl;
1747 GLenum internal;
1749 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1751 if (!d3d_info->shader_color_key
1752 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1753 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1755 wined3d_texture_force_reload(texture);
1757 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1758 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1761 if (texture->flags & alloc_flag)
1762 return;
1764 if (resource->format_flags & WINED3DFMT_FLAG_DECOMPRESS)
1766 TRACE("WINED3DFMT_FLAG_DECOMPRESS set.\n");
1767 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1768 format = wined3d_resource_get_decompress_format(resource);
1770 else if (format->conv_byte_count)
1772 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1774 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1776 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1777 format = wined3d_get_format(device->adapter, conversion->dst_format, resource->usage);
1778 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1780 format_gl = wined3d_format_gl(format);
1782 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, srgb);
1784 if (srgb)
1785 internal = format_gl->srgb_internal;
1786 else if (resource->usage & WINED3DUSAGE_RENDERTARGET && wined3d_resource_is_offscreen(resource))
1787 internal = format_gl->rt_internal;
1788 else
1789 internal = format_gl->internal;
1791 if (!internal)
1792 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1794 TRACE("internal %#x, format %#x, type %#x.\n", internal, format_gl->format, format_gl->type);
1796 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1797 wined3d_texture_gl_allocate_immutable_storage(wined3d_texture_gl(texture), internal, gl_info);
1798 else
1799 wined3d_texture_gl_allocate_mutable_storage(wined3d_texture_gl(texture), internal, format_gl, gl_info);
1800 texture->flags |= alloc_flag;
1803 static void wined3d_texture_gl_prepare_rb(struct wined3d_texture_gl *texture_gl,
1804 const struct wined3d_gl_info *gl_info, BOOL multisample)
1806 const struct wined3d_format_gl *format_gl;
1808 format_gl = wined3d_format_gl(texture_gl->t.resource.format);
1809 if (multisample)
1811 DWORD samples;
1813 if (texture_gl->rb_multisample)
1814 return;
1816 samples = wined3d_texture_get_gl_sample_count(&texture_gl->t);
1818 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_multisample);
1819 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_multisample);
1820 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1821 format_gl->internal, texture_gl->t.resource.width, texture_gl->t.resource.height);
1822 checkGLcall("glRenderbufferStorageMultisample()");
1823 TRACE("Created multisample rb %u.\n", texture_gl->rb_multisample);
1825 else
1827 if (texture_gl->rb_resolved)
1828 return;
1830 gl_info->fbo_ops.glGenRenderbuffers(1, &texture_gl->rb_resolved);
1831 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture_gl->rb_resolved);
1832 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format_gl->internal,
1833 texture_gl->t.resource.width, texture_gl->t.resource.height);
1834 checkGLcall("glRenderbufferStorage()");
1835 TRACE("Created resolved rb %u.\n", texture_gl->rb_resolved);
1839 /* Context activation is done by the caller. Context may be NULL in
1840 * WINED3D_NO3D mode. */
1841 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1842 struct wined3d_context *context, DWORD location)
1844 switch (location)
1846 case WINED3D_LOCATION_SYSMEM:
1847 if (texture->resource.heap_memory)
1848 return TRUE;
1850 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1851 return FALSE;
1852 return TRUE;
1854 case WINED3D_LOCATION_USER_MEMORY:
1855 if (!texture->user_memory)
1856 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1857 return TRUE;
1859 case WINED3D_LOCATION_BUFFER:
1860 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1861 return TRUE;
1863 case WINED3D_LOCATION_TEXTURE_RGB:
1864 wined3d_texture_prepare_texture(texture, context, FALSE);
1865 return TRUE;
1867 case WINED3D_LOCATION_TEXTURE_SRGB:
1868 wined3d_texture_prepare_texture(texture, context, TRUE);
1869 return TRUE;
1871 case WINED3D_LOCATION_DRAWABLE:
1872 if (!texture->swapchain && wined3d_settings.offscreen_rendering_mode != ORM_BACKBUFFER)
1873 ERR("Texture %p does not have a drawable.\n", texture);
1874 return TRUE;
1876 case WINED3D_LOCATION_RB_MULTISAMPLE:
1877 wined3d_texture_gl_prepare_rb(wined3d_texture_gl(texture), context->gl_info, TRUE);
1878 return TRUE;
1880 case WINED3D_LOCATION_RB_RESOLVED:
1881 wined3d_texture_gl_prepare_rb(wined3d_texture_gl(texture), context->gl_info, FALSE);
1882 return TRUE;
1884 default:
1885 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1886 return FALSE;
1890 static struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1891 unsigned int sub_resource_idx)
1893 UINT sub_count = texture->level_count * texture->layer_count;
1895 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1897 if (sub_resource_idx >= sub_count)
1899 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1900 return NULL;
1903 return &texture->sub_resources[sub_resource_idx];
1906 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1907 UINT layer, const struct wined3d_box *dirty_region)
1909 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1911 if (layer >= texture->layer_count)
1913 WARN("Invalid layer %u specified.\n", layer);
1914 return WINED3DERR_INVALIDCALL;
1917 if (dirty_region)
1918 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1920 wined3d_cs_emit_add_dirty_texture_region(texture->resource.device->cs, texture, layer);
1922 return WINED3D_OK;
1925 /* This call just uploads data, the caller is responsible for binding the
1926 * correct texture. */
1927 /* Context activation is done by the caller. */
1928 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1929 struct wined3d_context *context, const struct wined3d_format *format, const struct wined3d_box *src_box,
1930 const struct wined3d_const_bo_address *data, unsigned int src_row_pitch, unsigned int src_slice_pitch,
1931 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, BOOL srgb)
1933 const struct wined3d_gl_info *gl_info = context->gl_info;
1934 unsigned int update_w = src_box->right - src_box->left;
1935 unsigned int update_h = src_box->bottom - src_box->top;
1936 unsigned int update_d = src_box->back - src_box->front;
1937 const struct wined3d_format_gl *format_gl;
1938 struct wined3d_bo_address bo;
1939 void *converted_mem = NULL;
1940 struct wined3d_format_gl f;
1941 unsigned int level;
1942 BOOL decompress;
1943 GLenum target;
1945 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data %s, "
1946 "src_row_pitch %#x, src_slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
1947 texture, sub_resource_idx, context, debug_d3dformat(format->id), debug_box(src_box),
1948 debug_const_bo_address(data), src_row_pitch, src_slice_pitch, dst_x, dst_y, dst_z, srgb);
1950 if (texture->sub_resources[sub_resource_idx].map_count)
1952 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
1953 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM;
1956 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_HEIGHT_SCALE)
1958 update_h *= format->height_scale.numerator;
1959 update_h /= format->height_scale.denominator;
1962 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(texture), sub_resource_idx);
1963 level = sub_resource_idx % texture->level_count;
1965 switch (target)
1967 case GL_TEXTURE_1D_ARRAY:
1968 dst_y = sub_resource_idx / texture->level_count;
1969 update_h = 1;
1970 break;
1971 case GL_TEXTURE_2D_ARRAY:
1972 dst_z = sub_resource_idx / texture->level_count;
1973 update_d = 1;
1974 break;
1975 case GL_TEXTURE_2D_MULTISAMPLE:
1976 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1977 FIXME("Not supported for multisample textures.\n");
1978 return;
1981 bo.buffer_object = data->buffer_object;
1982 bo.addr = (BYTE *)data->addr + src_box->front * src_slice_pitch;
1983 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
1985 bo.addr += (src_box->top / format->block_height) * src_row_pitch;
1986 bo.addr += (src_box->left / format->block_width) * format->block_byte_count;
1988 else
1990 bo.addr += src_box->top * src_row_pitch;
1991 bo.addr += src_box->left * format->byte_count;
1994 decompress = texture->resource.format_flags & WINED3DFMT_FLAG_DECOMPRESS;
1995 if (format->upload || decompress)
1997 const struct wined3d_format *compressed_format = format;
1998 unsigned int dst_row_pitch, dst_slice_pitch;
1999 void *src_mem;
2001 if (decompress)
2003 format = wined3d_resource_get_decompress_format(&texture->resource);
2005 else
2007 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2008 ERR("Converting a block-based format.\n");
2010 f = *wined3d_format_gl(format);
2011 f.f.byte_count = format->conv_byte_count;
2012 format = &f.f;
2015 wined3d_format_calculate_pitch(format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2017 /* Note that uploading 3D textures may require quite some address
2018 * space; it may make sense to upload them per-slice instead. */
2019 if (!(converted_mem = heap_calloc(update_d, dst_slice_pitch)))
2021 ERR("Failed to allocate upload buffer.\n");
2022 return;
2025 src_mem = context_map_bo_address(context, &bo, src_slice_pitch,
2026 GL_PIXEL_UNPACK_BUFFER, WINED3D_MAP_READ);
2027 if (decompress)
2028 compressed_format->decompress(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2029 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2030 else
2031 format->upload(src_mem, converted_mem, src_row_pitch, src_slice_pitch,
2032 dst_row_pitch, dst_slice_pitch, update_w, update_h, update_d);
2033 context_unmap_bo_address(context, &bo, GL_PIXEL_UNPACK_BUFFER);
2035 bo.buffer_object = 0;
2036 bo.addr = converted_mem;
2037 src_row_pitch = dst_row_pitch;
2038 src_slice_pitch = dst_slice_pitch;
2041 if (bo.buffer_object)
2043 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, bo.buffer_object));
2044 checkGLcall("glBindBuffer");
2047 format_gl = wined3d_format_gl(format);
2048 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_COMPRESSED)
2050 unsigned int dst_row_pitch, dst_slice_pitch;
2051 const BYTE *addr = bo.addr;
2052 GLenum internal;
2054 if (srgb)
2055 internal = format_gl->srgb_internal;
2056 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
2057 && wined3d_resource_is_offscreen(&texture->resource))
2058 internal = format_gl->rt_internal;
2059 else
2060 internal = format_gl->internal;
2062 wined3d_format_calculate_pitch(format, 1, update_w, update_h, &dst_row_pitch, &dst_slice_pitch);
2064 TRACE("Uploading compressed data, target %#x, level %u, x %u, y %u, z %u, "
2065 "w %u, h %u, d %u, format %#x, image_size %#x, addr %p.\n",
2066 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2067 update_d, internal, dst_slice_pitch, addr);
2069 if (target == GL_TEXTURE_1D)
2071 GL_EXTCALL(glCompressedTexSubImage1D(target, level, dst_x,
2072 update_w, internal, dst_row_pitch, addr));
2074 else if (dst_row_pitch == src_row_pitch)
2076 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2078 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2079 update_w, update_h, update_d, internal, dst_slice_pitch * update_d, addr));
2081 else
2083 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, dst_y,
2084 update_w, update_h, internal, dst_slice_pitch, addr));
2087 else
2089 unsigned int row_count = (update_h + format->block_height - 1) / format->block_height;
2090 unsigned int row, y, z;
2092 /* glCompressedTexSubImage2D() ignores pixel store state, so we
2093 * can't use the unpack row length like for glTexSubImage2D. */
2094 for (z = dst_z; z < dst_z + update_d; ++z)
2096 for (row = 0, y = dst_y; row < row_count; ++row)
2098 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2100 GL_EXTCALL(glCompressedTexSubImage3D(target, level, dst_x, y, z,
2101 update_w, format->block_height, 1, internal, dst_row_pitch, addr));
2103 else
2105 GL_EXTCALL(glCompressedTexSubImage2D(target, level, dst_x, y,
2106 update_w, format->block_height, internal, dst_row_pitch, addr));
2109 y += format->block_height;
2110 addr += src_row_pitch;
2114 checkGLcall("Upload compressed texture data");
2116 else
2118 TRACE("Uploading data, target %#x, level %u, x %u, y %u, z %u, "
2119 "w %u, h %u, d %u, format %#x, type %#x, addr %p.\n",
2120 target, level, dst_x, dst_y, dst_z, update_w, update_h,
2121 update_d, format_gl->format, format_gl->type, bo.addr);
2123 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, src_row_pitch / format->byte_count);
2124 if (target == GL_TEXTURE_2D_ARRAY || target == GL_TEXTURE_3D)
2126 GL_EXTCALL(glTexSubImage3D(target, level, dst_x, dst_y, dst_z,
2127 update_w, update_h, update_d, format_gl->format, format_gl->type, bo.addr));
2129 else if (target == GL_TEXTURE_1D)
2131 gl_info->gl_ops.gl.p_glTexSubImage1D(target, level, dst_x,
2132 update_w, format_gl->format, format_gl->type, bo.addr);
2134 else
2136 gl_info->gl_ops.gl.p_glTexSubImage2D(target, level, dst_x, dst_y,
2137 update_w, update_h, format_gl->format, format_gl->type, bo.addr);
2139 gl_info->gl_ops.gl.p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2140 checkGLcall("Upload texture data");
2143 if (bo.buffer_object)
2145 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2146 checkGLcall("glBindBuffer");
2148 heap_free(converted_mem);
2150 if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
2152 struct wined3d_device *device = texture->resource.device;
2153 unsigned int i;
2155 for (i = 0; i < device->context_count; ++i)
2157 context_texture_update(device->contexts[i], wined3d_texture_gl(texture));
2162 static void texture2d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2163 struct wined3d_context *context, const struct wined3d_bo_address *data)
2165 const struct wined3d_gl_info *gl_info = context->gl_info;
2166 struct wined3d_texture_sub_resource *sub_resource;
2167 unsigned int dst_row_pitch, dst_slice_pitch;
2168 unsigned int src_row_pitch, src_slice_pitch;
2169 const struct wined3d_format_gl *format_gl;
2170 BYTE *temporary_mem = NULL;
2171 unsigned int level;
2172 GLenum target;
2173 void *mem;
2175 format_gl = wined3d_format_gl(texture->resource.format);
2177 /* Only support read back of converted P8 textures. */
2178 if (texture->flags & WINED3D_TEXTURE_CONVERTED && format_gl->f.id != WINED3DFMT_P8_UINT && !format_gl->f.download)
2180 ERR("Trying to read back converted texture %p, %u with format %s.\n",
2181 texture, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2182 return;
2185 sub_resource = &texture->sub_resources[sub_resource_idx];
2186 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(texture), sub_resource_idx);
2187 level = sub_resource_idx % texture->level_count;
2189 if (target == GL_TEXTURE_2D_ARRAY)
2191 if (format_gl->f.download)
2193 FIXME("Reading back converted array texture %p is not supported.\n", texture);
2194 return;
2197 /* NP2 emulation is not allowed on array textures. */
2198 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2199 ERR("Array texture %p uses NP2 emulation.\n", texture);
2201 WARN_(d3d_perf)("Downloading all miplevel layers to get the data for a single sub-resource.\n");
2203 if (!(temporary_mem = heap_calloc(texture->layer_count, sub_resource->size)))
2205 ERR("Out of memory.\n");
2206 return;
2210 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2212 if (format_gl->f.download)
2214 FIXME("Reading back converted texture %p with NP2 emulation is not supported.\n", texture);
2215 return;
2218 wined3d_texture_get_pitch(texture, level, &dst_row_pitch, &dst_slice_pitch);
2219 wined3d_format_calculate_pitch(&format_gl->f, texture->resource.device->surface_alignment,
2220 wined3d_texture_get_level_pow2_width(texture, level),
2221 wined3d_texture_get_level_pow2_height(texture, level),
2222 &src_row_pitch, &src_slice_pitch);
2223 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2225 ERR("Out of memory.\n");
2226 return;
2229 if (data->buffer_object)
2230 ERR("NP2 emulated texture uses PBO unexpectedly.\n");
2231 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2232 ERR("Unexpected compressed format for NP2 emulated texture.\n");
2235 if (format_gl->f.download)
2237 struct wined3d_format f;
2239 if (data->buffer_object)
2240 ERR("Converted texture %p uses PBO unexpectedly.\n", texture);
2242 WARN_(d3d_perf)("Downloading converted texture %p, %u with format %s.\n",
2243 texture, sub_resource_idx, debug_d3dformat(format_gl->f.id));
2245 f = format_gl->f;
2246 f.byte_count = format_gl->f.conv_byte_count;
2247 wined3d_texture_get_pitch(texture, level, &dst_row_pitch, &dst_slice_pitch);
2248 wined3d_format_calculate_pitch(&f, texture->resource.device->surface_alignment,
2249 wined3d_texture_get_level_width(texture, level),
2250 wined3d_texture_get_level_height(texture, level),
2251 &src_row_pitch, &src_slice_pitch);
2253 if (!(temporary_mem = heap_alloc(src_slice_pitch)))
2255 ERR("Failed to allocate memory.\n");
2256 return;
2260 if (temporary_mem)
2262 mem = temporary_mem;
2264 else if (data->buffer_object)
2266 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2267 checkGLcall("glBindBuffer");
2268 mem = data->addr;
2270 else
2272 mem = data->addr;
2275 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2277 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2278 texture, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2280 GL_EXTCALL(glGetCompressedTexImage(target, level, mem));
2281 checkGLcall("glGetCompressedTexImage");
2283 else
2285 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2286 texture, sub_resource_idx, level, format_gl->format, format_gl->type, mem);
2288 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, mem);
2289 checkGLcall("glGetTexImage");
2292 if (format_gl->f.download)
2294 format_gl->f.download(mem, data->addr, src_row_pitch, src_slice_pitch, dst_row_pitch, dst_slice_pitch,
2295 wined3d_texture_get_level_width(texture, level),
2296 wined3d_texture_get_level_height(texture, level), 1);
2298 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2300 const BYTE *src_data;
2301 unsigned int h, y;
2302 BYTE *dst_data;
2303 /* Some games (e.g. Warhammer 40,000) don't properly handle texture
2304 * pitches, preventing us from using the texture pitch to box NPOT
2305 * textures. Instead, we repack the texture's CPU copy so that its
2306 * pitch equals bpp * width instead of bpp * pow2width.
2308 * Instead of boxing the texture:
2310 * │<── texture width ──>│ pow2 width ──>│
2311 * ├─────────────────────┼───────────────┼─
2312 * │111111111111111111111│ │ʌ
2313 * │222222222222222222222│ ││
2314 * │333333333333333333333│ padding │texture height
2315 * │444444444444444444444│ ││
2316 * │555555555555555555555│ │v
2317 * ├─────────────────────┘ ├─
2318 * │ │pow2 height
2319 * │ padding padding ││
2320 * │ │v
2321 * └─────────────────────────────────────┴─
2323 * we're repacking the data to the expected texture width
2325 * │<── texture width ──>│ pow2 width ──>│
2326 * ├─────────────────────┴───────────────┼─
2327 * │1111111111111111111112222222222222222│ʌ
2328 * │2222233333333333333333333344444444444││
2329 * │4444444444555555555555555555555 │texture height
2330 * │ ││
2331 * │ padding padding │v
2332 * │ ├─
2333 * │ │pow2 height
2334 * │ padding padding ││
2335 * │ │v
2336 * └─────────────────────────────────────┴─
2338 * == is the same as
2340 * │<── texture width ──>│
2341 * ├─────────────────────┼─
2342 * │111111111111111111111│ʌ
2343 * │222222222222222222222││
2344 * │333333333333333333333│texture height
2345 * │444444444444444444444││
2346 * │555555555555555555555│v
2347 * └─────────────────────┴─
2349 * This also means that any references to surface memory should work
2350 * with the data as if it were a standard texture with a NPOT width
2351 * instead of a texture boxed up to be a power-of-two texture. */
2352 src_data = mem;
2353 dst_data = data->addr;
2354 TRACE("Repacking the surface data from pitch %u to pitch %u.\n", src_row_pitch, dst_row_pitch);
2355 h = wined3d_texture_get_level_height(texture, level);
2356 for (y = 0; y < h; ++y)
2358 memcpy(dst_data, src_data, dst_row_pitch);
2359 src_data += src_row_pitch;
2360 dst_data += dst_row_pitch;
2363 else if (temporary_mem)
2365 unsigned int layer = sub_resource_idx / texture->level_count;
2366 void *src_data = temporary_mem + layer * sub_resource->size;
2367 if (data->buffer_object)
2369 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2370 checkGLcall("glBindBuffer");
2371 GL_EXTCALL(glBufferSubData(GL_PIXEL_PACK_BUFFER, 0, sub_resource->size, src_data));
2372 checkGLcall("glBufferSubData");
2374 else
2376 memcpy(data->addr, src_data, sub_resource->size);
2380 if (data->buffer_object)
2382 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2383 checkGLcall("glBindBuffer");
2386 heap_free(temporary_mem);
2389 /* This call just downloads data, the caller is responsible for binding the
2390 * correct texture. Partial downloads are not supported. */
2391 /* Context activation is done by the caller. */
2392 void wined3d_texture_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2393 struct wined3d_context *context, const struct wined3d_bo_address *data)
2395 const struct wined3d_gl_info *gl_info = context->gl_info;
2396 const struct wined3d_format_gl *format_gl;
2397 unsigned int level;
2398 GLenum target;
2400 format_gl = wined3d_format_gl(texture->resource.format);
2401 target = wined3d_texture_gl_get_sub_resource_target(wined3d_texture_gl(texture), sub_resource_idx);
2402 level = sub_resource_idx % texture->level_count;
2404 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_2D
2405 && (target == GL_TEXTURE_2D_ARRAY || format_gl->f.conv_byte_count
2406 || texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_COND_NP2_EMULATED)))
2408 /* 2D-specific special cases. */
2409 texture2d_download_data(texture, sub_resource_idx, context, data);
2410 return;
2413 if (format_gl->f.conv_byte_count)
2415 FIXME("Attempting to download a converted texture, type %s format %s.\n",
2416 debug_d3dresourcetype(texture->resource.type),
2417 debug_d3dformat(format_gl->f.id));
2418 return;
2421 if (data->buffer_object)
2423 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2424 checkGLcall("glBindBuffer");
2427 if (texture->resource.format_flags & WINED3DFMT_FLAG_COMPRESSED)
2429 TRACE("Downloading compressed texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2430 texture, sub_resource_idx, level, format_gl->format, format_gl->type, data->addr);
2432 GL_EXTCALL(glGetCompressedTexImage(target, level, data->addr));
2433 checkGLcall("glGetCompressedTexImage");
2435 else
2437 TRACE("Downloading texture %p, %u, level %u, format %#x, type %#x, data %p.\n",
2438 texture, sub_resource_idx, level, format_gl->format, format_gl->type, data->addr);
2440 gl_info->gl_ops.gl.p_glGetTexImage(target, level, format_gl->format, format_gl->type, data->addr);
2441 checkGLcall("glGetTexImage");
2444 if (data->buffer_object)
2446 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2447 checkGLcall("glBindBuffer");
2451 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
2452 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2453 struct wined3d_context *context, DWORD location)
2455 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2456 texture, sub_resource_idx, context, wined3d_debug_location(location));
2458 switch (location)
2460 case WINED3D_LOCATION_USER_MEMORY:
2461 case WINED3D_LOCATION_SYSMEM:
2462 case WINED3D_LOCATION_BUFFER:
2463 return texture2d_load_sysmem(texture, sub_resource_idx, context, location);
2465 case WINED3D_LOCATION_DRAWABLE:
2466 return texture2d_load_drawable(texture, sub_resource_idx, context);
2468 case WINED3D_LOCATION_RB_RESOLVED:
2469 case WINED3D_LOCATION_RB_MULTISAMPLE:
2470 return texture2d_load_renderbuffer(texture, sub_resource_idx, context, location);
2472 case WINED3D_LOCATION_TEXTURE_RGB:
2473 case WINED3D_LOCATION_TEXTURE_SRGB:
2474 return texture2d_load_texture(texture, sub_resource_idx, context,
2475 location == WINED3D_LOCATION_TEXTURE_SRGB);
2477 default:
2478 ERR("Don't know how to handle location %#x.\n", location);
2479 return FALSE;
2483 static const struct wined3d_texture_ops texture2d_ops =
2485 texture2d_load_location,
2488 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
2490 return texture_from_resource(resource);
2493 static ULONG texture_resource_incref(struct wined3d_resource *resource)
2495 return wined3d_texture_incref(texture_from_resource(resource));
2498 static ULONG texture_resource_decref(struct wined3d_resource *resource)
2500 return wined3d_texture_decref(texture_from_resource(resource));
2503 static void texture_resource_preload(struct wined3d_resource *resource)
2505 struct wined3d_texture *texture = texture_from_resource(resource);
2506 struct wined3d_context *context;
2508 context = context_acquire(resource->device, NULL, 0);
2509 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
2510 context_release(context);
2513 static void wined3d_texture_gl_unload(struct wined3d_resource *resource)
2515 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
2516 UINT sub_count = texture_gl->t.level_count * texture_gl->t.layer_count;
2517 struct wined3d_renderbuffer_entry *entry, *entry2;
2518 struct wined3d_device *device = resource->device;
2519 const struct wined3d_gl_info *gl_info;
2520 struct wined3d_context *context;
2521 UINT i;
2523 TRACE("texture_gl %p.\n", texture_gl);
2525 context = context_acquire(device, NULL, 0);
2526 gl_info = context->gl_info;
2528 for (i = 0; i < sub_count; ++i)
2530 struct wined3d_texture_sub_resource *sub_resource = &texture_gl->t.sub_resources[i];
2532 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
2533 && wined3d_texture_load_location(&texture_gl->t, i, context, resource->map_binding))
2535 wined3d_texture_invalidate_location(&texture_gl->t, i, ~resource->map_binding);
2537 else
2539 /* We should only get here on device reset/teardown for implicit
2540 * resources. */
2541 if (resource->access & WINED3D_RESOURCE_ACCESS_CPU
2542 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
2543 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
2544 debug_d3dresourcetype(resource->type), resource, i,
2545 wined3d_debug_resource_access(resource->access));
2546 wined3d_texture_validate_location(&texture_gl->t, i, WINED3D_LOCATION_DISCARDED);
2547 wined3d_texture_invalidate_location(&texture_gl->t, i, ~WINED3D_LOCATION_DISCARDED);
2550 if (sub_resource->buffer_object)
2551 wined3d_texture_remove_buffer_object(&texture_gl->t, i, context->gl_info);
2554 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &texture_gl->renderbuffers, struct wined3d_renderbuffer_entry, entry)
2556 context_gl_resource_released(device, entry->id, TRUE);
2557 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
2558 list_remove(&entry->entry);
2559 heap_free(entry);
2561 list_init(&texture_gl->renderbuffers);
2562 texture_gl->current_renderbuffer = NULL;
2564 context_release(context);
2566 wined3d_texture_force_reload(&texture_gl->t);
2567 wined3d_texture_gl_unload_texture(texture_gl);
2570 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
2571 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
2573 const struct wined3d_format *format = resource->format;
2574 struct wined3d_texture_sub_resource *sub_resource;
2575 struct wined3d_device *device = resource->device;
2576 unsigned int fmt_flags = resource->format_flags;
2577 struct wined3d_context *context = NULL;
2578 struct wined3d_texture *texture;
2579 struct wined3d_bo_address data;
2580 unsigned int texture_level;
2581 BYTE *base_memory;
2582 BOOL ret;
2584 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2585 resource, sub_resource_idx, map_desc, debug_box(box), flags);
2587 texture = texture_from_resource(resource);
2588 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2589 return E_INVALIDARG;
2591 texture_level = sub_resource_idx % texture->level_count;
2592 if (box && FAILED(wined3d_texture_check_box_dimensions(texture, texture_level, box)))
2594 WARN("Map box is invalid.\n");
2595 if (((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && !(resource->access & WINED3D_RESOURCE_ACCESS_CPU))
2596 || resource->type != WINED3D_RTYPE_TEXTURE_2D)
2597 return WINED3DERR_INVALIDCALL;
2600 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2602 WARN("DC is in use.\n");
2603 return WINED3DERR_INVALIDCALL;
2606 if (sub_resource->map_count)
2608 WARN("Sub-resource is already mapped.\n");
2609 return WINED3DERR_INVALIDCALL;
2612 if (device->d3d_initialized)
2613 context = context_acquire(device, NULL, 0);
2615 if (flags & WINED3D_MAP_DISCARD)
2617 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2618 wined3d_debug_location(resource->map_binding));
2619 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx, context, resource->map_binding)))
2620 wined3d_texture_validate_location(texture, sub_resource_idx, resource->map_binding);
2622 else
2624 if (resource->usage & WINED3DUSAGE_DYNAMIC)
2625 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2626 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, resource->map_binding);
2629 if (!ret)
2631 ERR("Failed to prepare location.\n");
2632 context_release(context);
2633 return E_OUTOFMEMORY;
2636 if (flags & WINED3D_MAP_WRITE
2637 && (!(flags & WINED3D_MAP_NO_DIRTY_UPDATE) || (resource->usage & WINED3DUSAGE_DYNAMIC)))
2638 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~resource->map_binding);
2640 wined3d_texture_get_memory(texture, sub_resource_idx, &data, resource->map_binding);
2641 base_memory = context_map_bo_address(context, &data, sub_resource->size, GL_PIXEL_UNPACK_BUFFER, flags);
2642 TRACE("Base memory pointer %p.\n", base_memory);
2644 if (context)
2645 context_release(context);
2647 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
2649 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
2650 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
2652 else
2654 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
2657 if (!box)
2659 map_desc->data = base_memory;
2661 else
2663 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
2665 /* Compressed textures are block based, so calculate the offset of
2666 * the block that contains the top-left pixel of the mapped box. */
2667 map_desc->data = base_memory
2668 + (box->front * map_desc->slice_pitch)
2669 + ((box->top / format->block_height) * map_desc->row_pitch)
2670 + ((box->left / format->block_width) * format->block_byte_count);
2672 else
2674 map_desc->data = base_memory
2675 + (box->front * map_desc->slice_pitch)
2676 + (box->top * map_desc->row_pitch)
2677 + (box->left * format->byte_count);
2681 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2683 RECT *r = &texture->swapchain->front_buffer_update;
2685 if (!box)
2686 SetRect(r, 0, 0, resource->width, resource->height);
2687 else
2688 SetRect(r, box->left, box->top, box->right, box->bottom);
2689 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
2692 ++resource->map_count;
2693 ++sub_resource->map_count;
2695 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2696 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
2698 return WINED3D_OK;
2701 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
2703 struct wined3d_texture_sub_resource *sub_resource;
2704 struct wined3d_device *device = resource->device;
2705 struct wined3d_context *context = NULL;
2706 struct wined3d_texture *texture;
2707 struct wined3d_bo_address data;
2709 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
2711 texture = texture_from_resource(resource);
2712 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2713 return E_INVALIDARG;
2715 if (!sub_resource->map_count)
2717 WARN("Trying to unmap unmapped sub-resource.\n");
2718 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
2719 return WINED3D_OK;
2720 return WINEDDERR_NOTLOCKED;
2723 if (device->d3d_initialized)
2724 context = context_acquire(device, NULL, 0);
2726 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
2727 context_unmap_bo_address(context, &data, GL_PIXEL_UNPACK_BUFFER);
2729 if (context)
2730 context_release(context);
2732 if (texture->swapchain && texture->swapchain->front_buffer == texture)
2734 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
2735 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
2738 --sub_resource->map_count;
2739 if (!--resource->map_count && texture->update_map_binding)
2740 wined3d_texture_update_map_binding(texture);
2742 return WINED3D_OK;
2745 static const struct wined3d_resource_ops texture_resource_ops =
2747 texture_resource_incref,
2748 texture_resource_decref,
2749 texture_resource_preload,
2750 wined3d_texture_gl_unload,
2751 texture_resource_sub_resource_map,
2752 texture_resource_sub_resource_unmap,
2755 /* Context activation is done by the caller. */
2756 static BOOL texture1d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2757 struct wined3d_context *context, DWORD location)
2759 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2760 unsigned int row_pitch, slice_pitch;
2762 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2763 texture, sub_resource_idx, context, wined3d_debug_location(location));
2765 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2766 return FALSE;
2768 switch (location)
2770 case WINED3D_LOCATION_TEXTURE_RGB:
2771 case WINED3D_LOCATION_TEXTURE_SRGB:
2772 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2774 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2775 struct wined3d_box src_box;
2777 data.addr += sub_resource->offset;
2778 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture),
2779 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
2780 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2781 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
2782 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2783 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2785 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2787 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2788 struct wined3d_box src_box;
2790 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context,
2791 location == WINED3D_LOCATION_TEXTURE_SRGB);
2792 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2793 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
2794 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
2795 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
2797 else
2799 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2800 return FALSE;
2802 break;
2804 case WINED3D_LOCATION_SYSMEM:
2805 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2807 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2809 data.addr += sub_resource->offset;
2810 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2811 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, FALSE);
2812 else
2813 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, TRUE);
2815 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
2816 ++texture->download_count;
2818 else
2820 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2821 wined3d_debug_location(sub_resource->locations));
2822 return FALSE;
2824 break;
2826 case WINED3D_LOCATION_BUFFER:
2827 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2829 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2831 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2832 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, FALSE);
2833 else
2834 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, TRUE);
2836 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
2838 else
2840 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2841 wined3d_debug_location(sub_resource->locations));
2842 return FALSE;
2844 break;
2846 default:
2847 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2848 wined3d_debug_location(sub_resource->locations));
2849 return FALSE;
2852 return TRUE;
2855 static const struct wined3d_texture_ops texture1d_ops =
2857 texture1d_load_location,
2860 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2861 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
2862 void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources,
2863 const struct wined3d_texture_ops *texture_ops)
2865 const struct wined3d_d3d_info *d3d_info = &device->adapter->d3d_info;
2866 struct wined3d_device_parent *device_parent = device->device_parent;
2867 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2868 unsigned int sub_count, i, j, size, offset = 0;
2869 unsigned int pow2_width, pow2_height;
2870 const struct wined3d_format *format;
2871 HRESULT hr;
2873 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2874 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2875 "flags %#x, device %p, parent %p, parent_ops %p, sub_resources %p, texture_ops %p.\n",
2876 texture, debug_d3dresourcetype(desc->resource_type), debug_d3dformat(desc->format),
2877 desc->multisample_type, desc->multisample_quality, debug_d3dusage(desc->usage),
2878 wined3d_debug_resource_access(desc->access), desc->width, desc->height, desc->depth,
2879 layer_count, level_count, flags, device, parent, parent_ops, sub_resources, texture_ops);
2881 if (!desc->width || !desc->height || !desc->depth)
2882 return WINED3DERR_INVALIDCALL;
2884 if (desc->resource_type == WINED3D_RTYPE_TEXTURE_3D && layer_count != 1)
2886 ERR("Invalid layer count for volume texture.\n");
2887 return E_INVALIDARG;
2890 texture->sub_resources = sub_resources;
2892 /* TODO: It should only be possible to create textures for formats
2893 * that are reported as supported. */
2894 if (WINED3DFMT_UNKNOWN >= desc->format)
2896 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2897 return WINED3DERR_INVALIDCALL;
2899 format = wined3d_get_format(device->adapter, desc->format, desc->usage);
2901 if (desc->usage & WINED3DUSAGE_DYNAMIC && (wined3d_resource_access_is_managed(desc->access)
2902 || desc->usage & WINED3DUSAGE_SCRATCH))
2904 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2905 wined3d_debug_resource_access(desc->access), debug_d3dusage(desc->usage));
2906 return WINED3DERR_INVALIDCALL;
2909 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
2910 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
2911 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2912 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->access & WINED3D_RESOURCE_ACCESS_CPU)
2913 FIXME("Trying to create a CPU accessible render target.\n");
2915 pow2_width = desc->width;
2916 pow2_height = desc->height;
2917 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)) || (desc->depth & (desc->depth - 1)))
2918 && !d3d_info->texture_npot)
2920 /* level_count == 0 returns an error as well. */
2921 if (level_count != 1 || layer_count != 1 || desc->resource_type == WINED3D_RTYPE_TEXTURE_3D)
2923 if (!(desc->usage & WINED3DUSAGE_SCRATCH))
2925 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2926 "texture without unconditional NPOT support.\n");
2927 return WINED3DERR_INVALIDCALL;
2930 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2932 texture->flags |= WINED3D_TEXTURE_COND_NP2;
2934 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !d3d_info->texture_npot_conditional)
2936 /* TODO: Add support for non-power-of-two compressed textures. */
2937 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
2938 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
2940 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2941 desc->width, desc->height);
2942 return WINED3DERR_NOTAVAILABLE;
2945 /* Find the nearest pow2 match. */
2946 pow2_width = pow2_height = 1;
2947 while (pow2_width < desc->width)
2948 pow2_width <<= 1;
2949 while (pow2_height < desc->height)
2950 pow2_height <<= 1;
2951 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
2954 texture->pow2_width = pow2_width;
2955 texture->pow2_height = pow2_height;
2957 if ((pow2_width > d3d_info->limits.texture_size || pow2_height > d3d_info->limits.texture_size)
2958 && (desc->usage & WINED3DUSAGE_TEXTURE))
2960 /* One of four options:
2961 * 1: Do the same as we do with NPOT and scale the texture. (Any
2962 * texture ops would require the texture to be scaled which is
2963 * potentially slow.)
2964 * 2: Set the texture to the maximum size (bad idea).
2965 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2966 * 4: Create the surface, but allow it to be used only for DirectDraw
2967 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2968 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2969 * the render target. */
2970 if (desc->access & WINED3D_RESOURCE_ACCESS_GPU)
2972 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
2973 return WINED3DERR_NOTAVAILABLE;
2976 /* We should never use this surface in combination with OpenGL. */
2977 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
2980 for (i = 0; i < layer_count; ++i)
2982 for (j = 0; j < level_count; ++j)
2984 unsigned int idx = i * level_count + j;
2986 size = wined3d_format_calculate_size(format, device->surface_alignment,
2987 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
2988 texture->sub_resources[idx].offset = offset;
2989 texture->sub_resources[idx].size = size;
2990 offset += size;
2992 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
2995 if (!offset)
2996 return WINED3DERR_INVALIDCALL;
2998 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
2999 desc->multisample_type, desc->multisample_quality, desc->usage, 0, desc->access,
3000 desc->width, desc->height, desc->depth, offset, parent, parent_ops, &texture_resource_ops)))
3002 static unsigned int once;
3004 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
3005 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
3006 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
3007 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
3008 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
3009 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
3011 WARN("Failed to initialize resource, returning %#x\n", hr);
3012 return hr;
3014 wined3d_resource_update_draw_binding(&texture->resource);
3015 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
3016 texture->resource.access |= WINED3D_RESOURCE_ACCESS_MAP_R | WINED3D_RESOURCE_ACCESS_MAP_W;
3018 texture->texture_ops = texture_ops;
3020 texture->layer_count = layer_count;
3021 texture->level_count = level_count;
3022 texture->lod = 0;
3023 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
3024 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
3025 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
3026 if (flags & (WINED3D_TEXTURE_CREATE_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
3027 texture->flags |= WINED3D_TEXTURE_GET_DC;
3028 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
3029 texture->flags |= WINED3D_TEXTURE_DISCARD;
3030 if (flags & WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS)
3032 if (!(texture->resource.format_flags & WINED3DFMT_FLAG_GEN_MIPMAP))
3033 WARN("Format doesn't support mipmaps generation, "
3034 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
3035 else
3036 texture->flags |= WINED3D_TEXTURE_GENERATE_MIPMAPS;
3039 /* Precalculated scaling for 'faked' non power of two texture coords. */
3040 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3042 texture->pow2_matrix[0] = (float)desc->width;
3043 texture->pow2_matrix[5] = (float)desc->height;
3044 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
3046 else if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
3048 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
3049 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
3050 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
3052 else
3054 texture->pow2_matrix[0] = 1.0f;
3055 texture->pow2_matrix[5] = 1.0f;
3057 texture->pow2_matrix[10] = 1.0f;
3058 texture->pow2_matrix[15] = 1.0f;
3059 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
3061 if (wined3d_texture_use_pbo(texture, gl_info))
3062 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
3064 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D
3065 || !wined3d_texture_use_pbo(texture, gl_info))
3067 if (!wined3d_resource_allocate_sysmem(&texture->resource))
3069 wined3d_texture_cleanup_sync(texture);
3070 return E_OUTOFMEMORY;
3074 sub_count = level_count * layer_count;
3075 if (sub_count / layer_count != level_count)
3077 wined3d_texture_cleanup_sync(texture);
3078 return E_OUTOFMEMORY;
3081 if (desc->usage & WINED3DUSAGE_OVERLAY)
3083 if (!(texture->overlay_info = heap_calloc(sub_count, sizeof(*texture->overlay_info))))
3085 wined3d_texture_cleanup_sync(texture);
3086 return E_OUTOFMEMORY;
3089 for (i = 0; i < sub_count; ++i)
3091 list_init(&texture->overlay_info[i].entry);
3092 list_init(&texture->overlay_info[i].overlays);
3096 /* Generate all sub-resources. */
3097 for (i = 0; i < sub_count; ++i)
3099 struct wined3d_texture_sub_resource *sub_resource;
3101 sub_resource = &texture->sub_resources[i];
3102 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
3103 if (desc->resource_type != WINED3D_RTYPE_TEXTURE_3D)
3105 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_SYSMEM);
3106 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_SYSMEM);
3109 if (FAILED(hr = device_parent->ops->texture_sub_resource_created(device_parent,
3110 desc->resource_type, texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
3112 WARN("Failed to create sub-resource parent, hr %#x.\n", hr);
3113 sub_resource->parent = NULL;
3114 wined3d_texture_cleanup_sync(texture);
3115 return hr;
3118 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
3120 TRACE("Created sub-resource %u (level %u, layer %u).\n",
3121 i, i % texture->level_count, i / texture->level_count);
3123 if (desc->usage & WINED3DUSAGE_OWNDC)
3125 struct wined3d_texture_idx texture_idx = {texture, i};
3127 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3128 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3129 if (!texture->dc_info || !texture->dc_info[i].dc)
3131 wined3d_texture_cleanup_sync(texture);
3132 return WINED3DERR_INVALIDCALL;
3137 return WINED3D_OK;
3140 /* Context activation is done by the caller. */
3141 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3142 struct wined3d_context *context, BOOL dest_is_srgb)
3144 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3145 unsigned int row_pitch, slice_pitch;
3146 struct wined3d_bo_address data;
3147 struct wined3d_box src_box;
3149 /* Optimisations are possible, but the effort should be put into either
3150 * implementing EXT_SRGB_DECODE in the driver or finding out why we
3151 * picked the wrong copy for the original upload and fixing that.
3153 * Also keep in mind that we want to avoid using resource.heap_memory
3154 * for DEFAULT pool surfaces. */
3155 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
3156 data.buffer_object = 0;
3157 if (!(data.addr = heap_alloc(sub_resource->size)))
3158 return;
3160 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3161 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3162 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, !dest_is_srgb);
3163 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3164 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, dest_is_srgb);
3165 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3166 &src_box, wined3d_const_bo_address(&data), row_pitch, slice_pitch, 0, 0, 0, FALSE);
3168 heap_free(data.addr);
3171 /* Context activation is done by the caller. */
3172 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3173 struct wined3d_context *context, DWORD location)
3175 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
3176 unsigned int row_pitch, slice_pitch;
3178 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
3179 return FALSE;
3181 switch (location)
3183 case WINED3D_LOCATION_TEXTURE_RGB:
3184 case WINED3D_LOCATION_TEXTURE_SRGB:
3185 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
3187 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
3188 struct wined3d_box src_box;
3190 data.addr += sub_resource->offset;
3191 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture),
3192 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
3193 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3194 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3195 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3196 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
3198 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
3200 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
3201 struct wined3d_box src_box;
3203 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture),
3204 context, location == WINED3D_LOCATION_TEXTURE_SRGB);
3205 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
3206 wined3d_texture_get_level_box(texture, sub_resource_idx % texture->level_count, &src_box);
3207 wined3d_texture_upload_data(texture, sub_resource_idx, context, texture->resource.format,
3208 &src_box, &data, row_pitch, slice_pitch, 0, 0, 0, FALSE);
3210 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3212 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
3214 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
3216 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
3218 else
3220 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
3221 return FALSE;
3223 break;
3225 case WINED3D_LOCATION_SYSMEM:
3226 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3228 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
3230 data.addr += sub_resource->offset;
3231 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3232 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, FALSE);
3233 else
3234 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, TRUE);
3236 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3237 ++texture->download_count;
3239 else
3241 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
3242 wined3d_debug_location(sub_resource->locations));
3243 return FALSE;
3245 break;
3247 case WINED3D_LOCATION_BUFFER:
3248 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
3250 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
3252 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
3253 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, FALSE);
3254 else
3255 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(texture), context, TRUE);
3257 wined3d_texture_download_data(texture, sub_resource_idx, context, &data);
3259 else
3261 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
3262 wined3d_debug_location(sub_resource->locations));
3263 return FALSE;
3265 break;
3267 default:
3268 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
3269 wined3d_debug_location(sub_resource->locations));
3270 return FALSE;
3273 return TRUE;
3276 static const struct wined3d_texture_ops texture3d_ops =
3278 texture3d_load_location,
3281 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3282 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
3283 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
3285 struct wined3d_box src_box = {src_rect->left, src_rect->top, src_rect->right, src_rect->bottom, 0, 1};
3286 struct wined3d_box dst_box = {dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, 0, 1};
3287 unsigned int dst_format_flags, src_format_flags = 0;
3288 HRESULT hr;
3290 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
3291 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
3292 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
3293 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
3295 if (dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count
3296 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3297 return WINED3DERR_INVALIDCALL;
3299 if (src_sub_resource_idx >= src_texture->level_count * src_texture->layer_count
3300 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3301 return WINED3DERR_INVALIDCALL;
3303 dst_format_flags = dst_texture->resource.format_flags;
3304 if (FAILED(hr = wined3d_texture_check_box_dimensions(dst_texture,
3305 dst_sub_resource_idx % dst_texture->level_count, &dst_box)))
3306 return hr;
3308 src_format_flags = src_texture->resource.format_flags;
3309 if (FAILED(hr = wined3d_texture_check_box_dimensions(src_texture,
3310 src_sub_resource_idx % src_texture->level_count, &src_box)))
3311 return hr;
3313 if (dst_texture->sub_resources[dst_sub_resource_idx].map_count
3314 || src_texture->sub_resources[src_sub_resource_idx].map_count)
3316 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
3317 return WINEDDERR_SURFACEBUSY;
3320 if ((src_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
3321 != (dst_format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
3323 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
3324 return WINED3DERR_INVALIDCALL;
3327 if (dst_texture->resource.device != src_texture->resource.device)
3329 FIXME("Rejecting cross-device blit.\n");
3330 return E_NOTIMPL;
3333 wined3d_cs_emit_blt_sub_resource(dst_texture->resource.device->cs, &dst_texture->resource, dst_sub_resource_idx,
3334 &dst_box, &src_texture->resource, src_sub_resource_idx, &src_box, flags, fx, filter);
3336 return WINED3D_OK;
3339 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
3340 unsigned int sub_resource_idx, LONG *x, LONG *y)
3342 struct wined3d_overlay_info *overlay;
3344 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
3346 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3347 || sub_resource_idx >= texture->level_count * texture->layer_count)
3349 WARN("Invalid sub-resource specified.\n");
3350 return WINEDDERR_NOTAOVERLAYSURFACE;
3353 overlay = &texture->overlay_info[sub_resource_idx];
3354 if (!overlay->dst_texture)
3356 TRACE("Overlay not visible.\n");
3357 *x = 0;
3358 *y = 0;
3359 return WINEDDERR_OVERLAYNOTVISIBLE;
3362 *x = overlay->dst_rect.left;
3363 *y = overlay->dst_rect.top;
3365 TRACE("Returning position %d, %d.\n", *x, *y);
3367 return WINED3D_OK;
3370 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
3371 unsigned int sub_resource_idx, LONG x, LONG y)
3373 struct wined3d_overlay_info *overlay;
3374 LONG w, h;
3376 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
3378 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY)
3379 || sub_resource_idx >= texture->level_count * texture->layer_count)
3381 WARN("Invalid sub-resource specified.\n");
3382 return WINEDDERR_NOTAOVERLAYSURFACE;
3385 overlay = &texture->overlay_info[sub_resource_idx];
3386 w = overlay->dst_rect.right - overlay->dst_rect.left;
3387 h = overlay->dst_rect.bottom - overlay->dst_rect.top;
3388 SetRect(&overlay->dst_rect, x, y, x + w, y + h);
3390 return WINED3D_OK;
3393 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
3394 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3395 const RECT *dst_rect, DWORD flags)
3397 struct wined3d_overlay_info *overlay;
3398 unsigned int level, dst_level;
3400 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
3401 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
3402 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
3403 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
3405 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3406 || sub_resource_idx >= texture->level_count * texture->layer_count)
3408 WARN("Invalid sub-resource specified.\n");
3409 return WINEDDERR_NOTAOVERLAYSURFACE;
3412 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
3413 || dst_sub_resource_idx >= dst_texture->level_count * dst_texture->layer_count)
3415 WARN("Invalid destination sub-resource specified.\n");
3416 return WINED3DERR_INVALIDCALL;
3419 overlay = &texture->overlay_info[sub_resource_idx];
3421 level = sub_resource_idx % texture->level_count;
3422 if (src_rect)
3423 overlay->src_rect = *src_rect;
3424 else
3425 SetRect(&overlay->src_rect, 0, 0,
3426 wined3d_texture_get_level_width(texture, level),
3427 wined3d_texture_get_level_height(texture, level));
3429 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3430 if (dst_rect)
3431 overlay->dst_rect = *dst_rect;
3432 else
3433 SetRect(&overlay->dst_rect, 0, 0,
3434 wined3d_texture_get_level_width(dst_texture, dst_level),
3435 wined3d_texture_get_level_height(dst_texture, dst_level));
3437 if (overlay->dst_texture && (overlay->dst_texture != dst_texture
3438 || overlay->dst_sub_resource_idx != dst_sub_resource_idx || flags & WINEDDOVER_HIDE))
3440 overlay->dst_texture = NULL;
3441 list_remove(&overlay->entry);
3444 if (flags & WINEDDOVER_SHOW)
3446 if (overlay->dst_texture != dst_texture || overlay->dst_sub_resource_idx != dst_sub_resource_idx)
3448 overlay->dst_texture = dst_texture;
3449 overlay->dst_sub_resource_idx = dst_sub_resource_idx;
3450 list_add_tail(&texture->overlay_info[dst_sub_resource_idx].overlays, &overlay->entry);
3453 else if (flags & WINEDDOVER_HIDE)
3455 /* Tests show that the rectangles are erased on hide. */
3456 SetRectEmpty(&overlay->src_rect);
3457 SetRectEmpty(&overlay->dst_rect);
3458 overlay->dst_texture = NULL;
3461 return WINED3D_OK;
3464 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
3466 unsigned int sub_count = texture->level_count * texture->layer_count;
3468 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
3470 if (sub_resource_idx >= sub_count)
3472 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3473 return NULL;
3476 return texture->sub_resources[sub_resource_idx].parent;
3479 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
3480 unsigned int sub_resource_idx, void *parent)
3482 unsigned int sub_count = texture->level_count * texture->layer_count;
3484 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
3486 if (sub_resource_idx >= sub_count)
3488 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3489 return;
3492 texture->sub_resources[sub_resource_idx].parent = parent;
3495 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
3496 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
3498 unsigned int sub_count = texture->level_count * texture->layer_count;
3499 const struct wined3d_resource *resource;
3500 unsigned int level_idx;
3502 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
3504 if (sub_resource_idx >= sub_count)
3506 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
3507 return WINED3DERR_INVALIDCALL;
3510 resource = &texture->resource;
3511 desc->format = resource->format->id;
3512 desc->multisample_type = resource->multisample_type;
3513 desc->multisample_quality = resource->multisample_quality;
3514 desc->usage = resource->usage;
3515 desc->access = resource->access;
3517 level_idx = sub_resource_idx % texture->level_count;
3518 desc->width = wined3d_texture_get_level_width(texture, level_idx);
3519 desc->height = wined3d_texture_get_level_height(texture, level_idx);
3520 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
3521 desc->size = texture->sub_resources[sub_resource_idx].size;
3523 return WINED3D_OK;
3526 static void *wined3d_texture_allocate_object_memory(SIZE_T s, SIZE_T level_count, SIZE_T layer_count)
3528 struct wined3d_texture *t;
3530 if (level_count > ((~(SIZE_T)0 - s) / sizeof(*t->sub_resources)) / layer_count)
3531 return NULL;
3533 return heap_alloc_zero(s + level_count * layer_count * sizeof(*t->sub_resources));
3536 static HRESULT wined3d_texture_gl_init(struct wined3d_texture_gl *texture_gl, struct wined3d_device *device,
3537 const struct wined3d_resource_desc *desc, unsigned int layer_count, unsigned int level_count,
3538 DWORD flags, void *parent, const struct wined3d_parent_ops *parent_ops, void *sub_resources)
3540 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3541 const struct wined3d_texture_ops *texture_ops;
3542 HRESULT hr;
3544 TRACE("texture_gl %p, device %p, desc %p, layer_count %u, level_count %u, "
3545 "flags %#x, parent %p, parent_ops %p, sub_resources %p.\n",
3546 texture_gl, device, desc, layer_count, level_count,
3547 flags, parent, parent_ops, sub_resources);
3549 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
3550 && !gl_info->supported[EXT_TEXTURE_ARRAY])
3552 WARN("OpenGL implementation does not support array textures.\n");
3553 return WINED3DERR_INVALIDCALL;
3556 switch (desc->resource_type)
3558 case WINED3D_RTYPE_TEXTURE_1D:
3559 texture_ops = &texture1d_ops;
3560 if (layer_count > 1)
3561 texture_gl->target = GL_TEXTURE_1D_ARRAY;
3562 else
3563 texture_gl->target = GL_TEXTURE_1D;
3564 break;
3566 case WINED3D_RTYPE_TEXTURE_2D:
3567 texture_ops = &texture2d_ops;
3568 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
3570 texture_gl->target = GL_TEXTURE_CUBE_MAP_ARB;
3572 else if (desc->multisample_type && gl_info->supported[ARB_TEXTURE_MULTISAMPLE])
3574 if (layer_count > 1)
3575 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE_ARRAY;
3576 else
3577 texture_gl->target = GL_TEXTURE_2D_MULTISAMPLE;
3579 else
3581 if (layer_count > 1)
3582 texture_gl->target = GL_TEXTURE_2D_ARRAY;
3583 else
3584 texture_gl->target = GL_TEXTURE_2D;
3586 break;
3588 case WINED3D_RTYPE_TEXTURE_3D:
3589 if (!gl_info->supported[EXT_TEXTURE3D])
3591 WARN("OpenGL implementation does not support 3D textures.\n");
3592 return WINED3DERR_INVALIDCALL;
3594 texture_ops = &texture3d_ops;
3595 texture_gl->target = GL_TEXTURE_3D;
3596 break;
3598 default:
3599 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
3600 return WINED3DERR_INVALIDCALL;
3603 list_init(&texture_gl->renderbuffers);
3605 if (FAILED(hr = wined3d_texture_init(&texture_gl->t, desc, layer_count, level_count,
3606 flags, device, parent, parent_ops, sub_resources, texture_ops)))
3607 return hr;
3609 if (texture_gl->t.resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
3610 texture_gl->target = GL_TEXTURE_RECTANGLE_ARB;
3612 return WINED3D_OK;
3615 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
3616 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
3617 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
3619 unsigned int sub_count = level_count * layer_count;
3620 struct wined3d_texture_gl *object;
3621 unsigned int i;
3622 HRESULT hr;
3624 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3625 "parent %p, parent_ops %p, texture %p.\n",
3626 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
3628 if (!layer_count)
3630 WARN("Invalid layer count.\n");
3631 return E_INVALIDARG;
3633 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
3635 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
3636 layer_count = 6;
3639 if (!level_count)
3641 WARN("Invalid level count.\n");
3642 return WINED3DERR_INVALIDCALL;
3645 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
3647 const struct wined3d_format *format = wined3d_get_format(device->adapter, desc->format, desc->usage);
3649 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
3650 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
3652 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3653 desc->multisample_quality);
3654 return WINED3DERR_NOTAVAILABLE;
3656 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
3657 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
3658 || (desc->multisample_quality && desc->multisample_quality != WINED3D_STANDARD_MULTISAMPLE_PATTERN)))
3660 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
3661 desc->multisample_quality);
3662 return WINED3DERR_NOTAVAILABLE;
3666 if (data)
3668 for (i = 0; i < sub_count; ++i)
3670 if (data[i].data)
3671 continue;
3673 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
3674 return E_INVALIDARG;
3678 if (!(object = wined3d_texture_allocate_object_memory(sizeof(*object), level_count, layer_count)))
3679 return E_OUTOFMEMORY;
3681 if (FAILED(hr = wined3d_texture_gl_init(object, device, desc, layer_count,
3682 level_count, flags, parent, parent_ops, &object[1])))
3684 WARN("Failed to initialize texture, returning %#x.\n", hr);
3685 heap_free(object);
3686 return hr;
3689 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3690 * in this case. */
3691 if (data)
3693 unsigned int level, width, height, depth;
3694 struct wined3d_box box;
3696 for (i = 0; i < sub_count; ++i)
3698 level = i % object->t.level_count;
3699 width = wined3d_texture_get_level_width(&object->t, level);
3700 height = wined3d_texture_get_level_height(&object->t, level);
3701 depth = wined3d_texture_get_level_depth(&object->t, level);
3702 wined3d_box_set(&box, 0, 0, width, height, 0, depth);
3704 wined3d_cs_emit_update_sub_resource(device->cs, &object->t.resource,
3705 i, &box, data[i].data, data[i].row_pitch, data[i].slice_pitch);
3709 TRACE("Created texture %p.\n", object);
3710 *texture = &object->t;
3712 return WINED3D_OK;
3715 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
3717 struct wined3d_device *device = texture->resource.device;
3718 struct wined3d_texture_sub_resource *sub_resource;
3719 struct wined3d_dc_info *dc_info;
3721 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3723 if (!(texture->flags & WINED3D_TEXTURE_GET_DC))
3725 WARN("Texture does not support GetDC\n");
3726 /* Don't touch the DC */
3727 return WINED3DERR_INVALIDCALL;
3730 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3731 return WINED3DERR_INVALIDCALL;
3733 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3735 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3736 return WINED3DERR_INVALIDCALL;
3739 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3740 return WINED3DERR_INVALIDCALL;
3742 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3744 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3746 wined3d_cs_init_object(device->cs, wined3d_texture_create_dc, &texture_idx);
3747 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3748 if (!(dc_info = texture->dc_info) || !dc_info[sub_resource_idx].dc)
3749 return WINED3DERR_INVALIDCALL;
3752 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3753 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
3754 ++texture->resource.map_count;
3755 ++sub_resource->map_count;
3757 *dc = dc_info[sub_resource_idx].dc;
3758 TRACE("Returning dc %p.\n", *dc);
3760 return WINED3D_OK;
3763 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
3765 struct wined3d_device *device = texture->resource.device;
3766 struct wined3d_texture_sub_resource *sub_resource;
3767 struct wined3d_dc_info *dc_info;
3769 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
3771 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
3772 return WINED3DERR_INVALIDCALL;
3774 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
3776 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
3777 return WINED3DERR_INVALIDCALL;
3780 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
3781 return WINED3DERR_INVALIDCALL;
3783 if (!(dc_info = texture->dc_info) || dc_info[sub_resource_idx].dc != dc)
3785 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3786 dc, dc_info ? dc_info[sub_resource_idx].dc : NULL);
3787 return WINED3DERR_INVALIDCALL;
3790 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC))
3792 struct wined3d_texture_idx texture_idx = {texture, sub_resource_idx};
3794 wined3d_cs_destroy_object(device->cs, wined3d_texture_destroy_dc, &texture_idx);
3795 device->cs->ops->finish(device->cs, WINED3D_CS_QUEUE_DEFAULT);
3798 --sub_resource->map_count;
3799 if (!--texture->resource.map_count && texture->update_map_binding)
3800 wined3d_texture_update_map_binding(texture);
3801 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
3802 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
3804 return WINED3D_OK;
3807 void wined3d_texture_upload_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3808 unsigned int dst_x, unsigned int dst_y, unsigned int dst_z, struct wined3d_texture *src_texture,
3809 unsigned int src_sub_resource_idx, const struct wined3d_box *src_box)
3811 unsigned int src_row_pitch, src_slice_pitch;
3812 unsigned int update_w, update_h, update_d;
3813 unsigned int src_level, dst_level;
3814 struct wined3d_context *context;
3815 struct wined3d_bo_address data;
3817 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_x %u, dst_y %u, dst_z %u, "
3818 "src_texture %p, src_sub_resource_idx %u, src_box %s.\n",
3819 dst_texture, dst_sub_resource_idx, dst_x, dst_y, dst_z,
3820 src_texture, src_sub_resource_idx, debug_box(src_box));
3822 context = context_acquire(dst_texture->resource.device, NULL, 0);
3824 /* Only load the sub-resource for partial updates. For newly allocated
3825 * textures the texture wouldn't be the current location, and we'd upload
3826 * zeroes just to overwrite them again. */
3827 update_w = src_box->right - src_box->left;
3828 update_h = src_box->bottom - src_box->top;
3829 update_d = src_box->back - src_box->front;
3830 dst_level = dst_sub_resource_idx % dst_texture->level_count;
3831 if (update_w == wined3d_texture_get_level_width(dst_texture, dst_level)
3832 && update_h == wined3d_texture_get_level_height(dst_texture, dst_level)
3833 && update_d == wined3d_texture_get_level_depth(dst_texture, dst_level))
3834 wined3d_texture_prepare_texture(dst_texture, context, FALSE);
3835 else
3836 wined3d_texture_load_location(dst_texture, dst_sub_resource_idx, context, WINED3D_LOCATION_TEXTURE_RGB);
3837 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(dst_texture), context, FALSE);
3839 src_level = src_sub_resource_idx % src_texture->level_count;
3840 wined3d_texture_get_memory(src_texture, src_sub_resource_idx, &data,
3841 src_texture->sub_resources[src_sub_resource_idx].locations);
3842 wined3d_texture_get_pitch(src_texture, src_level, &src_row_pitch, &src_slice_pitch);
3844 wined3d_texture_upload_data(dst_texture, dst_sub_resource_idx, context, src_texture->resource.format,
3845 src_box, wined3d_const_bo_address(&data), src_row_pitch, src_slice_pitch, dst_x, dst_y, dst_z, FALSE);
3847 context_release(context);
3849 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
3850 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
3853 /* Partial downloads are not supported. */
3854 void wined3d_texture_download_from_texture(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
3855 struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx)
3857 struct wined3d_context *context;
3858 struct wined3d_bo_address data;
3859 DWORD dst_location = dst_texture->resource.map_binding;
3861 context = context_acquire(src_texture->resource.device, NULL, 0);
3863 wined3d_texture_prepare_location(dst_texture, dst_sub_resource_idx, context, dst_location);
3864 wined3d_texture_get_memory(dst_texture, dst_sub_resource_idx, &data, dst_location);
3865 wined3d_texture_gl_bind_and_dirtify(wined3d_texture_gl(src_texture), context,
3866 !(src_texture->sub_resources[src_sub_resource_idx].locations & WINED3D_LOCATION_TEXTURE_RGB));
3867 wined3d_texture_download_data(src_texture, src_sub_resource_idx, context, &data);
3869 context_release(context);
3871 wined3d_texture_validate_location(dst_texture, dst_sub_resource_idx, dst_location);
3872 wined3d_texture_invalidate_location(dst_texture, dst_sub_resource_idx, ~dst_location);