wined3d: Synchronise WINED3D_CS_OP_UNLOAD_RESOURCE resource access.
[wine.git] / dlls / wined3d / texture.c
blob4e75064cb36747749b2e49f8acbbf0890ee5aa91
1 /*
2 * Copyright 2002-2005 Jason Edmeades
3 * Copyright 2002-2005 Raphael Junqueira
4 * Copyright 2005 Oliver Stieber
5 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
6 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
35 return texture->resource.pool == WINED3D_POOL_DEFAULT
36 && texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
37 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
38 && !texture->resource.format->convert
39 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
42 static BOOL wined3d_texture_use_immutable_storage(const struct wined3d_texture *texture,
43 const struct wined3d_gl_info *gl_info)
45 /* We don't expect to create texture views for textures with height-scaled formats.
46 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
47 return gl_info->supported[ARB_TEXTURE_STORAGE]
48 && !(texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE);
51 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
53 const struct wined3d_swapchain *swapchain = texture->swapchain;
55 TRACE("texture %p.\n", texture);
57 if (!swapchain)
59 ERR("Texture %p is not part of a swapchain.\n", texture);
60 return GL_NONE;
63 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
65 if (swapchain->render_to_fbo)
67 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
68 return GL_COLOR_ATTACHMENT0;
70 TRACE("Returning GL_BACK.\n");
71 return GL_BACK;
73 else if (texture == swapchain->front_buffer)
75 TRACE("Returning GL_FRONT.\n");
76 return GL_FRONT;
79 FIXME("Higher back buffer, returning GL_BACK.\n");
80 return GL_BACK;
83 static DWORD wined3d_resource_access_from_location(DWORD location)
85 switch (location)
87 case WINED3D_LOCATION_DISCARDED:
88 return 0;
90 case WINED3D_LOCATION_SYSMEM:
91 return WINED3D_RESOURCE_ACCESS_CPU;
93 case WINED3D_LOCATION_BUFFER:
94 case WINED3D_LOCATION_TEXTURE_RGB:
95 case WINED3D_LOCATION_TEXTURE_SRGB:
96 return WINED3D_RESOURCE_ACCESS_GPU;
98 default:
99 FIXME("Unhandled location %#x.\n", location);
100 return 0;
104 static void wined3d_texture_evict_sysmem(struct wined3d_texture *texture)
106 struct wined3d_texture_sub_resource *sub_resource;
107 unsigned int i, sub_count;
109 if (texture->flags & (WINED3D_TEXTURE_CONVERTED | WINED3D_TEXTURE_PIN_SYSMEM)
110 || texture->download_count > WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD)
112 TRACE("Not evicting system memory for texture %p.\n", texture);
113 return;
116 TRACE("Evicting system memory for texture %p.\n", texture);
118 sub_count = texture->level_count * texture->layer_count;
119 for (i = 0; i < sub_count; ++i)
121 sub_resource = &texture->sub_resources[i];
122 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
123 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
124 i, texture);
125 sub_resource->locations &= ~WINED3D_LOCATION_SYSMEM;
127 wined3d_resource_free_sysmem(&texture->resource);
130 void wined3d_texture_validate_location(struct wined3d_texture *texture,
131 unsigned int sub_resource_idx, DWORD location)
133 struct wined3d_texture_sub_resource *sub_resource;
134 DWORD previous_locations;
136 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
137 texture, sub_resource_idx, wined3d_debug_location(location));
139 sub_resource = &texture->sub_resources[sub_resource_idx];
140 previous_locations = sub_resource->locations;
141 sub_resource->locations |= location;
142 if (previous_locations == WINED3D_LOCATION_SYSMEM && location != WINED3D_LOCATION_SYSMEM
143 && !--texture->sysmem_count)
144 wined3d_texture_evict_sysmem(texture);
146 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
149 static void wined3d_texture_set_dirty(struct wined3d_texture *texture)
151 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
154 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
155 unsigned int sub_resource_idx, DWORD location)
157 struct wined3d_texture_sub_resource *sub_resource;
159 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
160 texture, sub_resource_idx, wined3d_debug_location(location));
162 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
163 wined3d_texture_set_dirty(texture);
165 sub_resource = &texture->sub_resources[sub_resource_idx];
166 sub_resource->locations &= ~location;
167 if (sub_resource->locations == WINED3D_LOCATION_SYSMEM)
168 ++texture->sysmem_count;
170 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
172 if (!sub_resource->locations)
173 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
174 sub_resource_idx, texture);
177 /* Context activation is done by the caller. Context may be NULL in
178 * WINED3D_NO3D mode. */
179 BOOL wined3d_texture_load_location(struct wined3d_texture *texture,
180 unsigned int sub_resource_idx, struct wined3d_context *context, DWORD location)
182 return texture->texture_ops->texture_load_location(texture, sub_resource_idx, context, location);
185 /* Context activation is done by the caller. */
186 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
187 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
189 BYTE *memory;
191 if (!data->buffer_object)
192 return data->addr;
194 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
196 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
198 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
199 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
201 else
203 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
204 memory += (INT_PTR)data->addr;
207 GL_EXTCALL(glBindBuffer(binding, 0));
208 checkGLcall("Map buffer object");
210 return memory;
213 /* Context activation is done by the caller. */
214 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
215 const struct wined3d_gl_info *gl_info, GLenum binding)
217 if (!data->buffer_object)
218 return;
220 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
221 GL_EXTCALL(glUnmapBuffer(binding));
222 GL_EXTCALL(glBindBuffer(binding, 0));
223 checkGLcall("Unmap buffer object");
226 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
227 struct wined3d_bo_address *data, DWORD locations)
229 struct wined3d_texture_sub_resource *sub_resource;
231 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
232 texture, sub_resource_idx, data, wined3d_debug_location(locations));
234 sub_resource = &texture->sub_resources[sub_resource_idx];
235 if (locations & WINED3D_LOCATION_BUFFER)
237 data->addr = NULL;
238 data->buffer_object = sub_resource->buffer_object;
239 return;
241 if (locations & WINED3D_LOCATION_USER_MEMORY)
243 data->addr = texture->user_memory;
244 data->buffer_object = 0;
245 return;
247 if (locations & WINED3D_LOCATION_SYSMEM)
249 data->addr = texture->resource.heap_memory;
250 data->addr += sub_resource->offset;
251 data->buffer_object = 0;
252 return;
255 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
256 data->addr = NULL;
257 data->buffer_object = 0;
260 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
261 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
262 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
263 const struct wined3d_resource_ops *resource_ops)
265 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
266 unsigned int i, j, size, offset = 0;
267 HRESULT hr;
269 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
270 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
271 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
272 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
273 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
274 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
275 flags, device, parent, parent_ops, resource_ops);
277 if (!desc->width || !desc->height || !desc->depth)
278 return WINED3DERR_INVALIDCALL;
280 for (i = 0; i < layer_count; ++i)
282 for (j = 0; j < level_count; ++j)
284 unsigned int idx = i * level_count + j;
286 size = wined3d_format_calculate_size(format, device->surface_alignment,
287 max(1, desc->width >> j), max(1, desc->height >> j), max(1, desc->depth >> j));
288 texture->sub_resources[idx].offset = offset;
289 texture->sub_resources[idx].size = size;
290 offset += size;
292 offset = (offset + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1);
295 if (!offset)
296 return WINED3DERR_INVALIDCALL;
298 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
299 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
300 desc->width, desc->height, desc->depth, offset, parent, parent_ops, resource_ops)))
302 static unsigned int once;
304 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
305 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
306 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
307 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
308 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
309 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
311 WARN("Failed to initialize resource, returning %#x\n", hr);
312 return hr;
314 wined3d_resource_update_draw_binding(&texture->resource);
315 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
316 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
318 texture->texture_ops = texture_ops;
320 texture->layer_count = layer_count;
321 texture->level_count = level_count;
322 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
323 texture->lod = 0;
324 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
325 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
326 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
327 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
328 texture->flags |= WINED3D_TEXTURE_DISCARD;
330 return WINED3D_OK;
333 /* Context activation is done by the caller. */
334 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
335 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
337 GLuint *buffer_object;
339 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
340 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
341 checkGLcall("glDeleteBuffers");
342 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
343 *buffer_object = 0;
345 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
346 *buffer_object, texture, sub_resource_idx);
349 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
351 unsigned int sub_count = texture->level_count * texture->layer_count;
352 const struct wined3d_device *device = texture->resource.device;
353 DWORD map_binding = texture->update_map_binding;
354 struct wined3d_context *context = NULL;
355 unsigned int i;
357 if (device->d3d_initialized)
358 context = context_acquire(device, NULL);
360 for (i = 0; i < sub_count; ++i)
362 if (texture->sub_resources[i].locations == texture->resource.map_binding
363 && !wined3d_texture_load_location(texture, i, context, map_binding))
364 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
365 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
366 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
369 if (context)
370 context_release(context);
372 texture->resource.map_binding = map_binding;
373 texture->update_map_binding = 0;
376 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
378 texture->update_map_binding = map_binding;
379 if (!texture->resource.map_count)
380 wined3d_texture_update_map_binding(texture);
383 /* A GL context is provided by the caller */
384 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
385 struct gl_texture *tex)
387 context_gl_resource_released(device, tex->name, FALSE);
388 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
389 tex->name = 0;
392 /* Context activation is done by the caller. */
393 /* The caller is responsible for binding the correct texture. */
394 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
395 GLenum gl_internal_format, const struct wined3d_format *format,
396 const struct wined3d_gl_info *gl_info)
398 unsigned int i, sub_call_count;
400 sub_call_count = texture->level_count;
401 if (texture->target != GL_TEXTURE_2D_ARRAY)
402 sub_call_count *= texture->layer_count;
404 for (i = 0; i < sub_call_count; ++i)
406 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
407 GLsizei width, height;
409 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
410 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
411 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
413 height *= format->height_scale.numerator;
414 height /= format->height_scale.denominator;
417 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
418 surface, surface->texture_target, surface->texture_level, width, height);
420 if (texture->target == GL_TEXTURE_2D_ARRAY)
422 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
423 gl_internal_format, width, height, texture->layer_count, 0,
424 format->glFormat, format->glType, NULL));
425 checkGLcall("glTexImage3D");
427 else
429 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
430 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
431 checkGLcall("glTexImage2D");
436 /* Context activation is done by the caller. */
437 /* The caller is responsible for binding the correct texture. */
438 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
439 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
441 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
442 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
444 if (texture->target == GL_TEXTURE_2D_ARRAY)
446 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
447 width, height, texture->layer_count));
448 checkGLcall("glTexStorage3D");
450 else
452 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
453 width, height));
454 checkGLcall("glTexStorage2D");
458 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
460 struct wined3d_device *device = texture->resource.device;
461 const struct wined3d_gl_info *gl_info = NULL;
462 struct wined3d_context *context = NULL;
464 if (texture->texture_rgb.name || texture->texture_srgb.name
465 || texture->rb_multisample || texture->rb_resolved)
467 context = context_acquire(device, NULL);
468 gl_info = context->gl_info;
471 if (texture->texture_rgb.name)
472 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
474 if (texture->texture_srgb.name)
475 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
477 if (texture->rb_multisample)
479 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
480 context_gl_resource_released(device, texture->rb_multisample, TRUE);
481 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
482 texture->rb_multisample = 0;
485 if (texture->rb_resolved)
487 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
488 context_gl_resource_released(device, texture->rb_resolved, TRUE);
489 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
490 texture->rb_resolved = 0;
493 if (context) context_release(context);
495 wined3d_texture_set_dirty(texture);
497 resource_unload(&texture->resource);
500 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
502 unsigned int sub_count = texture->level_count * texture->layer_count;
503 struct wined3d_texture_sub_resource *sub_resource;
504 unsigned int i;
506 for (i = 0; i < sub_count; ++i)
508 sub_resource = &texture->sub_resources[i];
509 if (sub_resource->parent)
511 TRACE("sub-resource %u.\n", i);
512 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
513 sub_resource->parent = NULL;
518 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
520 unsigned int sub_count = texture->level_count * texture->layer_count;
521 struct wined3d_device *device = texture->resource.device;
522 struct wined3d_context *context = NULL;
523 const struct wined3d_gl_info *gl_info;
524 GLuint buffer_object;
525 unsigned int i;
527 TRACE("texture %p.\n", texture);
529 for (i = 0; i < sub_count; ++i)
531 if (!(buffer_object = texture->sub_resources[i].buffer_object))
532 continue;
534 TRACE("Deleting buffer object %u.\n", buffer_object);
536 /* We may not be able to get a context in wined3d_texture_cleanup() in
537 * general, but if a buffer object was previously created we can. */
538 if (!context)
540 context = context_acquire(device, NULL);
541 gl_info = context->gl_info;
544 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
546 if (context)
547 context_release(context);
549 texture->texture_ops->texture_cleanup_sub_resources(texture);
550 wined3d_texture_unload_gl_texture(texture);
553 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
555 texture->swapchain = swapchain;
556 wined3d_resource_update_draw_binding(&texture->resource);
559 /* Context activation is done by the caller. */
560 void wined3d_texture_bind(struct wined3d_texture *texture,
561 struct wined3d_context *context, BOOL srgb)
563 const struct wined3d_gl_info *gl_info = context->gl_info;
564 const struct wined3d_format *format = texture->resource.format;
565 const struct color_fixup_desc fixup = format->color_fixup;
566 struct gl_texture *gl_tex;
567 GLenum target;
569 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
571 if (!needs_separate_srgb_gl_texture(context))
572 srgb = FALSE;
574 /* sRGB mode cache for preload() calls outside drawprim. */
575 if (srgb)
576 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
577 else
578 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
580 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
581 target = texture->target;
583 if (gl_tex->name)
585 context_bind_texture(context, target, gl_tex->name);
586 return;
589 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
590 checkGLcall("glGenTextures");
591 TRACE("Generated texture %d.\n", gl_tex->name);
593 if (!gl_tex->name)
595 ERR("Failed to generate a texture name.\n");
596 return;
599 /* Initialise the state of the texture object to the OpenGL defaults, not
600 * the wined3d defaults. */
601 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
602 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
603 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
604 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
605 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
606 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
607 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
608 gl_tex->sampler_desc.lod_bias = 0.0f;
609 gl_tex->sampler_desc.min_lod = -1000.0f;
610 gl_tex->sampler_desc.max_lod = 1000.0f;
611 gl_tex->sampler_desc.max_anisotropy = 1;
612 gl_tex->sampler_desc.compare = FALSE;
613 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
614 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
615 gl_tex->sampler_desc.srgb_decode = TRUE;
616 else
617 gl_tex->sampler_desc.srgb_decode = srgb;
618 gl_tex->base_level = 0;
619 wined3d_texture_set_dirty(texture);
621 context_bind_texture(context, target, gl_tex->name);
623 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
625 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
626 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
629 /* For a new texture we have to set the texture levels after binding the
630 * texture. Beware that texture rectangles do not support mipmapping, but
631 * set the maxmiplevel if we're relying on the partial
632 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
633 * (I.e., do not care about cond_np2 here, just look for
634 * GL_TEXTURE_RECTANGLE_ARB.) */
635 if (target != GL_TEXTURE_RECTANGLE_ARB)
637 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
638 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
639 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
642 if (target == GL_TEXTURE_CUBE_MAP_ARB)
644 /* Cubemaps are always set to clamp, regardless of the sampler state. */
645 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
646 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
647 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
650 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
652 /* Conditinal non power of two textures use a different clamping
653 * default. If we're using the GL_WINE_normalized_texrect partial
654 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
655 * has the address mode set to repeat - something that prevents us
656 * from hitting the accelerated codepath. Thus manually set the GL
657 * state. The same applies to filtering. Even if the texture has only
658 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
659 * fallback on macos. */
660 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
661 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
662 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
663 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
664 checkGLcall("glTexParameteri");
665 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
666 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
667 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
668 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
669 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
672 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
674 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
675 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
678 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
680 static const GLenum swizzle_source[] =
682 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
683 GL_ONE, /* CHANNEL_SOURCE_ONE */
684 GL_RED, /* CHANNEL_SOURCE_X */
685 GL_GREEN, /* CHANNEL_SOURCE_Y */
686 GL_BLUE, /* CHANNEL_SOURCE_Z */
687 GL_ALPHA, /* CHANNEL_SOURCE_W */
689 struct
691 GLint x, y, z, w;
693 swizzle;
695 swizzle.x = swizzle_source[fixup.x_source];
696 swizzle.y = swizzle_source[fixup.y_source];
697 swizzle.z = swizzle_source[fixup.z_source];
698 swizzle.w = swizzle_source[fixup.w_source];
699 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
700 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
704 /* Context activation is done by the caller. */
705 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
706 struct wined3d_context *context, BOOL srgb)
708 DWORD active_sampler;
710 /* We don't need a specific texture unit, but after binding the texture
711 * the current unit is dirty. Read the unit back instead of switching to
712 * 0, this avoids messing around with the state manager's GL states. The
713 * current texture unit should always be a valid one.
715 * To be more specific, this is tricky because we can implicitly be
716 * called from sampler() in state.c. This means we can't touch anything
717 * other than whatever happens to be the currently active texture, or we
718 * would risk marking already applied sampler states dirty again. */
719 active_sampler = context->rev_tex_unit_map[context->active_texture];
720 if (active_sampler != WINED3D_UNMAPPED_STAGE)
721 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
722 /* FIXME: Ideally we'd only do this when touching a binding that's used by
723 * a shader. */
724 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
726 wined3d_texture_bind(texture, context, srgb);
729 /* Context activation is done by the caller (state handler). */
730 /* This function relies on the correct texture being bound and loaded. */
731 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
732 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
734 const struct wined3d_gl_info *gl_info = context->gl_info;
735 GLenum target = texture->target;
736 struct gl_texture *gl_tex;
737 DWORD state;
739 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
741 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
743 state = sampler_desc->address_u;
744 if (state != gl_tex->sampler_desc.address_u)
746 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
747 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
748 gl_tex->sampler_desc.address_u = state;
751 state = sampler_desc->address_v;
752 if (state != gl_tex->sampler_desc.address_v)
754 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
755 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
756 gl_tex->sampler_desc.address_v = state;
759 state = sampler_desc->address_w;
760 if (state != gl_tex->sampler_desc.address_w)
762 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
763 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
764 gl_tex->sampler_desc.address_w = state;
767 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
768 sizeof(gl_tex->sampler_desc.border_color)))
770 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
771 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
772 sizeof(gl_tex->sampler_desc.border_color));
775 state = sampler_desc->mag_filter;
776 if (state != gl_tex->sampler_desc.mag_filter)
778 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
779 gl_tex->sampler_desc.mag_filter = state;
782 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
783 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
785 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
786 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
787 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
788 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
791 state = sampler_desc->max_anisotropy;
792 if (state != gl_tex->sampler_desc.max_anisotropy)
794 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
795 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
796 else
797 WARN("Anisotropic filtering not supported.\n");
798 gl_tex->sampler_desc.max_anisotropy = state;
801 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
802 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
803 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
805 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
806 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
807 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
810 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
812 if (sampler_desc->compare)
813 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
814 else
815 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
816 gl_tex->sampler_desc.compare = sampler_desc->compare;
819 checkGLcall("Texture parameter application");
821 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
823 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
824 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
825 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
829 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
831 ULONG refcount;
833 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
835 if (texture->swapchain)
836 return wined3d_swapchain_incref(texture->swapchain);
838 refcount = InterlockedIncrement(&texture->resource.ref);
839 TRACE("%p increasing refcount to %u.\n", texture, refcount);
841 return refcount;
844 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
846 wined3d_texture_sub_resources_destroyed(texture);
847 resource_cleanup(&texture->resource);
848 wined3d_resource_wait_idle(&texture->resource);
849 wined3d_texture_cleanup(texture);
852 static void wined3d_texture_destroy_object(void *object)
854 wined3d_texture_cleanup(object);
855 HeapFree(GetProcessHeap(), 0, object);
858 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
860 ULONG refcount;
862 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
864 if (texture->swapchain)
865 return wined3d_swapchain_decref(texture->swapchain);
867 refcount = InterlockedDecrement(&texture->resource.ref);
868 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
870 if (!refcount)
872 wined3d_texture_sub_resources_destroyed(texture);
873 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
874 resource_cleanup(&texture->resource);
875 wined3d_cs_emit_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
878 return refcount;
881 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
883 TRACE("texture %p.\n", texture);
885 return &texture->resource;
888 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
890 return c1->color_space_low_value == c2->color_space_low_value
891 && c1->color_space_high_value == c2->color_space_high_value;
894 /* Context activation is done by the caller */
895 void wined3d_texture_load(struct wined3d_texture *texture,
896 struct wined3d_context *context, BOOL srgb)
898 UINT sub_count = texture->level_count * texture->layer_count;
899 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
900 DWORD flag;
901 UINT i;
903 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
905 if (!needs_separate_srgb_gl_texture(context))
906 srgb = FALSE;
908 if (srgb)
909 flag = WINED3D_TEXTURE_SRGB_VALID;
910 else
911 flag = WINED3D_TEXTURE_RGB_VALID;
913 if (!d3d_info->shader_color_key
914 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
915 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
916 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
917 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
919 unsigned int sub_count = texture->level_count * texture->layer_count;
920 unsigned int i;
922 TRACE("Reloading because of color key value change.\n");
923 for (i = 0; i < sub_count; i++)
925 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
926 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
927 else
928 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
931 texture->async.gl_color_key = texture->async.src_blt_color_key;
934 if (texture->flags & flag)
936 TRACE("Texture %p not dirty, nothing to do.\n", texture);
937 return;
940 /* Reload the surfaces if the texture is marked dirty. */
941 for (i = 0; i < sub_count; ++i)
943 if (!wined3d_texture_load_location(texture, i, context,
944 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
945 ERR("Failed to load location (srgb %#x).\n", srgb);
947 texture->flags |= flag;
950 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
952 struct wined3d_context *context;
953 context = context_acquire(texture->resource.device, NULL);
954 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
955 context_release(context);
958 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
960 TRACE("texture %p.\n", texture);
962 return texture->resource.parent;
965 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
966 unsigned int level, const struct wined3d_box *box)
968 if (box->left >= box->right
969 || box->top >= box->bottom
970 || box->front >= box->back)
971 return FALSE;
973 if (box->right > wined3d_texture_get_level_width(texture, level)
974 || box->bottom > wined3d_texture_get_level_height(texture, level)
975 || box->back > wined3d_texture_get_level_depth(texture, level))
976 return FALSE;
978 return TRUE;
981 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
982 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
984 const struct wined3d_resource *resource = &texture->resource;
985 unsigned int width = wined3d_texture_get_level_width(texture, level);
986 unsigned int height = wined3d_texture_get_level_height(texture, level);
988 if (texture->row_pitch)
990 *row_pitch = texture->row_pitch;
991 *slice_pitch = texture->slice_pitch;
992 return;
995 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
996 width, height, row_pitch, slice_pitch);
999 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
1001 DWORD old = texture->lod;
1003 TRACE("texture %p, lod %u.\n", texture, lod);
1005 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1006 * textures. The call always returns 0, and GetLOD always returns 0. */
1007 if (texture->resource.pool != WINED3D_POOL_MANAGED)
1009 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
1010 return 0;
1013 if (lod >= texture->level_count)
1014 lod = texture->level_count - 1;
1016 if (texture->lod != lod)
1018 texture->lod = lod;
1020 texture->texture_rgb.base_level = ~0u;
1021 texture->texture_srgb.base_level = ~0u;
1022 if (texture->resource.bind_count)
1023 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1026 return old;
1029 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1031 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1033 return texture->lod;
1036 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1038 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1040 return texture->level_count;
1043 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1044 enum wined3d_texture_filter_type filter_type)
1046 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1048 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1050 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1051 return WINED3DERR_INVALIDCALL;
1054 texture->filter_type = filter_type;
1056 return WINED3D_OK;
1059 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1061 TRACE("texture %p.\n", texture);
1063 return texture->filter_type;
1066 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1067 DWORD flags, const struct wined3d_color_key *color_key)
1069 struct wined3d_device *device = texture->resource.device;
1070 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1071 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1073 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1075 if (flags & ~all_flags)
1077 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1078 return WINED3DERR_INVALIDCALL;
1081 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1083 return WINED3D_OK;
1086 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1087 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1088 UINT multisample_quality, void *mem, UINT pitch)
1090 struct wined3d_device *device = texture->resource.device;
1091 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1092 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1093 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1094 struct wined3d_texture_sub_resource *sub_resource;
1095 struct wined3d_surface *surface;
1096 DWORD valid_location = 0;
1097 BOOL create_dib = FALSE;
1099 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1100 "mem %p, pitch %u.\n",
1101 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1103 if (!resource_size)
1104 return WINED3DERR_INVALIDCALL;
1106 if (texture->level_count * texture->layer_count > 1)
1108 WARN("Texture has multiple sub-resources, not supported.\n");
1109 return WINED3DERR_INVALIDCALL;
1112 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1114 WARN("Not supported on 3D textures.\n");
1115 return WINED3DERR_INVALIDCALL;
1118 if (texture->resource.map_count)
1120 WARN("Texture is mapped.\n");
1121 return WINED3DERR_INVALIDCALL;
1124 /* We have no way of supporting a pitch that is not a multiple of the pixel
1125 * byte width short of uploading the texture row-by-row.
1126 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1127 * for user-memory textures (it always expects packed data) while DirectDraw
1128 * requires a 4-byte aligned pitch and doesn't support texture formats
1129 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1130 * This check is here to verify that the assumption holds. */
1131 if (pitch % texture->resource.format->byte_count)
1133 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1134 return WINED3DERR_INVALIDCALL;
1137 if (device->d3d_initialized)
1138 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1140 sub_resource = &texture->sub_resources[0];
1141 surface = sub_resource->u.surface;
1142 if (surface->dc)
1144 wined3d_surface_destroy_dc(surface);
1145 create_dib = TRUE;
1148 wined3d_resource_free_sysmem(&texture->resource);
1150 if ((texture->row_pitch = pitch))
1151 texture->slice_pitch = height * pitch;
1152 else
1153 /* User memory surfaces don't have the regular surface alignment. */
1154 wined3d_format_calculate_pitch(format, 1, width, height,
1155 &texture->row_pitch, &texture->slice_pitch);
1157 texture->resource.format = format;
1158 texture->resource.multisample_type = multisample_type;
1159 texture->resource.multisample_quality = multisample_quality;
1160 texture->resource.width = width;
1161 texture->resource.height = height;
1162 texture->resource.size = texture->slice_pitch;
1163 sub_resource->size = texture->slice_pitch;
1164 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1166 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1167 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1169 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1170 texture->pow2_width = texture->pow2_height = 1;
1171 while (texture->pow2_width < width)
1172 texture->pow2_width <<= 1;
1173 while (texture->pow2_height < height)
1174 texture->pow2_height <<= 1;
1176 else
1178 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1179 texture->pow2_width = width;
1180 texture->pow2_height = height;
1183 if ((texture->user_memory = mem))
1185 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1186 valid_location = WINED3D_LOCATION_USER_MEMORY;
1188 else
1190 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1191 valid_location = WINED3D_LOCATION_SYSMEM;
1194 /* The format might be changed to a format that needs conversion.
1195 * If the surface didn't use PBOs previously but could now, don't
1196 * change it - whatever made us not use PBOs might come back, e.g.
1197 * color keys. */
1198 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1199 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1201 wined3d_texture_validate_location(texture, 0, valid_location);
1202 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1204 if (create_dib)
1205 wined3d_surface_create_dc(surface);
1207 return WINED3D_OK;
1210 /* Context activation is done by the caller. */
1211 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1212 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1214 struct wined3d_texture_sub_resource *sub_resource;
1216 sub_resource = &texture->sub_resources[sub_resource_idx];
1217 if (sub_resource->buffer_object)
1218 return;
1220 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1221 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1222 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1223 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1224 checkGLcall("Create buffer object");
1226 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1227 sub_resource->buffer_object, texture, sub_resource_idx);
1230 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1232 unsigned int sub_count = texture->level_count * texture->layer_count;
1233 unsigned int i;
1235 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1236 | WINED3D_TEXTURE_CONVERTED);
1237 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1238 for (i = 0; i < sub_count; ++i)
1240 wined3d_texture_invalidate_location(texture, i,
1241 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1245 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1247 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1248 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1250 if (!d3d_info->shader_color_key
1251 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1252 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1254 wined3d_texture_force_reload(texture);
1256 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1257 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1260 if (texture->flags & alloc_flag)
1261 return;
1263 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1264 texture->flags |= alloc_flag;
1267 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1268 const struct wined3d_gl_info *gl_info, BOOL multisample)
1270 const struct wined3d_format *format = texture->resource.format;
1272 if (multisample)
1274 DWORD samples;
1276 if (texture->rb_multisample)
1277 return;
1279 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1280 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1281 * be mapped to GL_NV_framebuffer_multisample_coverage.
1283 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1284 * (EQAA), but it does not have an equivalent OpenGL extension. */
1286 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1287 * levels as the count of advertised multisample types for the texture
1288 * format. */
1289 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1291 unsigned int i, count = 0;
1293 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1295 if (format->multisample_types & 1u << i)
1297 if (texture->resource.multisample_quality == count++)
1298 break;
1301 samples = i + 1;
1303 else
1305 samples = texture->resource.multisample_type;
1308 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1309 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1310 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1311 format->glInternal, texture->resource.width, texture->resource.height);
1312 checkGLcall("glRenderbufferStorageMultisample()");
1313 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1315 else
1317 if (texture->rb_resolved)
1318 return;
1320 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1321 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1322 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1323 texture->resource.width, texture->resource.height);
1324 checkGLcall("glRenderbufferStorage()");
1325 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1329 /* Context activation is done by the caller. Context may be NULL in
1330 * WINED3D_NO3D mode. */
1331 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1332 struct wined3d_context *context, DWORD location)
1334 switch (location)
1336 case WINED3D_LOCATION_SYSMEM:
1337 if (texture->resource.heap_memory)
1338 return TRUE;
1340 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1342 ERR("Failed to allocate system memory.\n");
1343 return FALSE;
1345 return TRUE;
1347 case WINED3D_LOCATION_USER_MEMORY:
1348 if (!texture->user_memory)
1349 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1350 return TRUE;
1352 case WINED3D_LOCATION_BUFFER:
1353 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1354 return TRUE;
1356 case WINED3D_LOCATION_TEXTURE_RGB:
1357 wined3d_texture_prepare_texture(texture, context, FALSE);
1358 return TRUE;
1360 case WINED3D_LOCATION_TEXTURE_SRGB:
1361 wined3d_texture_prepare_texture(texture, context, TRUE);
1362 return TRUE;
1364 case WINED3D_LOCATION_DRAWABLE:
1365 if (!texture->swapchain)
1366 ERR("Texture %p does not have a drawable.\n", texture);
1367 return TRUE;
1369 case WINED3D_LOCATION_RB_MULTISAMPLE:
1370 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1371 return TRUE;
1373 case WINED3D_LOCATION_RB_RESOLVED:
1374 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1375 return TRUE;
1377 default:
1378 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1379 return FALSE;
1383 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1385 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1386 FIXME("texture %p stub!\n", texture);
1389 struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1390 unsigned int sub_resource_idx)
1392 UINT sub_count = texture->level_count * texture->layer_count;
1394 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1396 if (sub_resource_idx >= sub_count)
1398 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1399 return NULL;
1402 return &texture->sub_resources[sub_resource_idx];
1405 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1406 UINT layer, const struct wined3d_box *dirty_region)
1408 struct wined3d_context *context;
1409 unsigned int sub_resource_idx;
1411 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1413 if (layer >= texture->layer_count)
1415 WARN("Invalid layer %u specified.\n", layer);
1416 return WINED3DERR_INVALIDCALL;
1418 sub_resource_idx = layer * texture->level_count;
1420 if (dirty_region)
1421 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1423 context = context_acquire(texture->resource.device, NULL);
1424 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1426 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1427 context_release(context);
1428 return E_OUTOFMEMORY;
1430 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1431 context_release(context);
1433 return WINED3D_OK;
1436 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1437 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1438 unsigned int row_pitch, unsigned int slice_pitch)
1440 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1441 context, data, row_pitch, slice_pitch);
1444 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1445 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1446 unsigned int row_pitch, unsigned int slice_pitch)
1448 static const POINT dst_point = {0, 0};
1449 unsigned int texture_level;
1450 RECT src_rect;
1452 texture_level = sub_resource_idx % texture->level_count;
1453 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(texture, texture_level),
1454 wined3d_texture_get_level_height(texture, texture_level));
1456 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1457 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1460 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1461 struct wined3d_context *context, DWORD location)
1463 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1466 /* Context activation is done by the caller. */
1467 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1469 const struct wined3d_format *format = texture->resource.format;
1470 const struct wined3d_gl_info *gl_info = context->gl_info;
1471 const struct wined3d_color_key_conversion *conversion;
1472 GLenum internal;
1474 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1476 if (format->convert)
1478 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1480 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1482 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1483 format = wined3d_get_format(gl_info, conversion->dst_format);
1484 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1487 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1489 if (srgb)
1490 internal = format->glGammaInternal;
1491 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1492 && wined3d_resource_is_offscreen(&texture->resource))
1493 internal = format->rtInternal;
1494 else
1495 internal = format->glInternal;
1497 if (!internal)
1498 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1500 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1502 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1503 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1504 else
1505 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1508 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1510 unsigned int sub_count = texture->level_count * texture->layer_count;
1511 struct wined3d_device *device = texture->resource.device;
1512 struct wined3d_texture_sub_resource *sub_resource;
1513 struct wined3d_renderbuffer_entry *entry, *entry2;
1514 const struct wined3d_gl_info *gl_info = NULL;
1515 struct wined3d_context *context = NULL;
1516 struct wined3d_surface *overlay, *cur;
1517 struct wined3d_surface *surface;
1518 unsigned int i;
1520 for (i = 0; i < sub_count; ++i)
1522 sub_resource = &texture->sub_resources[i];
1523 if (!(surface = sub_resource->u.surface))
1524 continue;
1526 TRACE("surface %p.\n", surface);
1528 if (!context && !list_empty(&surface->renderbuffers))
1530 context = context_acquire(device, NULL);
1531 gl_info = context->gl_info;
1534 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1536 TRACE("Deleting renderbuffer %u.\n", entry->id);
1537 context_gl_resource_released(device, entry->id, TRUE);
1538 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1539 HeapFree(GetProcessHeap(), 0, entry);
1542 if (surface->dc)
1543 wined3d_surface_destroy_dc(surface);
1545 if (surface->overlay_dest)
1546 list_remove(&surface->overlay_entry);
1548 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1550 list_remove(&overlay->overlay_entry);
1551 overlay->overlay_dest = NULL;
1554 if (context)
1555 context_release(context);
1556 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1559 static const struct wined3d_texture_ops texture2d_ops =
1561 texture2d_upload_data,
1562 texture2d_load_location,
1563 texture2d_prepare_texture,
1564 texture2d_cleanup_sub_resources,
1567 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1569 return texture_from_resource(resource);
1572 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1574 return wined3d_texture_incref(texture_from_resource(resource));
1577 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1579 return wined3d_texture_decref(texture_from_resource(resource));
1582 static void wined3d_texture_unload(struct wined3d_resource *resource)
1584 struct wined3d_texture *texture = texture_from_resource(resource);
1585 UINT sub_count = texture->level_count * texture->layer_count;
1586 struct wined3d_device *device = resource->device;
1587 const struct wined3d_gl_info *gl_info;
1588 struct wined3d_context *context;
1589 UINT i;
1591 TRACE("texture %p.\n", texture);
1593 context = context_acquire(device, NULL);
1594 gl_info = context->gl_info;
1596 for (i = 0; i < sub_count; ++i)
1598 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1600 if (resource->pool != WINED3D_POOL_DEFAULT
1601 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1603 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1605 else
1607 /* We should only get here on device reset/teardown for implicit
1608 * resources. */
1609 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1610 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1611 resource, i, debug_d3dpool(resource->pool));
1612 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1613 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1616 if (sub_resource->buffer_object)
1617 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1619 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1621 struct wined3d_surface *surface = sub_resource->u.surface;
1622 struct wined3d_renderbuffer_entry *entry, *entry2;
1624 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1626 context_gl_resource_released(device, entry->id, TRUE);
1627 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1628 list_remove(&entry->entry);
1629 HeapFree(GetProcessHeap(), 0, entry);
1631 list_init(&surface->renderbuffers);
1632 surface->current_renderbuffer = NULL;
1636 context_release(context);
1638 wined3d_texture_force_reload(texture);
1639 wined3d_texture_unload_gl_texture(texture);
1642 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1643 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1645 const struct wined3d_format *format = resource->format;
1646 struct wined3d_texture_sub_resource *sub_resource;
1647 struct wined3d_device *device = resource->device;
1648 unsigned int fmt_flags = resource->format_flags;
1649 const struct wined3d_gl_info *gl_info = NULL;
1650 struct wined3d_context *context = NULL;
1651 struct wined3d_texture *texture;
1652 struct wined3d_bo_address data;
1653 unsigned int texture_level;
1654 BYTE *base_memory;
1655 BOOL ret;
1657 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1658 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1660 texture = texture_from_resource(resource);
1661 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1662 return E_INVALIDARG;
1664 texture_level = sub_resource_idx % texture->level_count;
1665 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1667 WARN("Map box is invalid.\n");
1668 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1669 return WINED3DERR_INVALIDCALL;
1672 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1673 && !wined3d_texture_check_block_align(texture, texture_level, box))
1675 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1676 debug_box(box), format->block_width, format->block_height);
1677 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1678 return WINED3DERR_INVALIDCALL;
1681 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1683 WARN("Trying to map unmappable texture.\n");
1684 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1685 return WINED3DERR_INVALIDCALL;
1688 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1690 WARN("DC is in use.\n");
1691 return WINED3DERR_INVALIDCALL;
1694 if (sub_resource->map_count)
1696 WARN("Sub-resource is already mapped.\n");
1697 return WINED3DERR_INVALIDCALL;
1700 flags = wined3d_resource_sanitize_map_flags(resource, flags);
1702 if (device->d3d_initialized)
1704 context = context_acquire(device, NULL);
1705 gl_info = context->gl_info;
1708 if (flags & WINED3D_MAP_DISCARD)
1710 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1711 wined3d_debug_location(texture->resource.map_binding));
1712 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1713 context, texture->resource.map_binding)))
1714 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1716 else
1718 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1719 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1720 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1723 if (!ret)
1725 ERR("Failed to prepare location.\n");
1726 context_release(context);
1727 return E_OUTOFMEMORY;
1730 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1731 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1733 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1734 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1735 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1736 TRACE("Base memory pointer %p.\n", base_memory);
1738 if (context)
1739 context_release(context);
1741 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1743 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1744 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1746 else
1748 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1751 if (!box)
1753 map_desc->data = base_memory;
1755 else
1757 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1759 /* Compressed textures are block based, so calculate the offset of
1760 * the block that contains the top-left pixel of the mapped box. */
1761 map_desc->data = base_memory
1762 + (box->front * map_desc->slice_pitch)
1763 + ((box->top / format->block_height) * map_desc->row_pitch)
1764 + ((box->left / format->block_width) * format->block_byte_count);
1766 else
1768 map_desc->data = base_memory
1769 + (box->front * map_desc->slice_pitch)
1770 + (box->top * map_desc->row_pitch)
1771 + (box->left * format->byte_count);
1775 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1777 RECT *r = &texture->swapchain->front_buffer_update;
1779 if (!box)
1780 SetRect(r, 0, 0, resource->width, resource->height);
1781 else
1782 SetRect(r, box->left, box->top, box->right, box->bottom);
1783 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1786 ++resource->map_count;
1787 ++sub_resource->map_count;
1789 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1790 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1792 return WINED3D_OK;
1795 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1797 struct wined3d_texture_sub_resource *sub_resource;
1798 struct wined3d_device *device = resource->device;
1799 const struct wined3d_gl_info *gl_info = NULL;
1800 struct wined3d_context *context = NULL;
1801 struct wined3d_texture *texture;
1802 struct wined3d_bo_address data;
1804 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1806 texture = texture_from_resource(resource);
1807 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1808 return E_INVALIDARG;
1810 if (!sub_resource->map_count)
1812 WARN("Trying to unmap unmapped sub-resource.\n");
1813 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1814 return WINED3D_OK;
1815 return WINEDDERR_NOTLOCKED;
1818 if (device->d3d_initialized)
1820 context = context_acquire(device, NULL);
1821 gl_info = context->gl_info;
1824 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1825 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1827 if (context)
1828 context_release(context);
1830 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1832 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1833 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1835 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1837 FIXME("Depth / stencil buffer locking is not implemented.\n");
1840 --sub_resource->map_count;
1841 if (!--resource->map_count && texture->update_map_binding)
1842 wined3d_texture_update_map_binding(texture);
1844 return WINED3D_OK;
1847 static const struct wined3d_resource_ops texture_resource_ops =
1849 texture_resource_incref,
1850 texture_resource_decref,
1851 wined3d_texture_unload,
1852 texture_resource_sub_resource_map,
1853 texture_resource_sub_resource_unmap,
1856 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1857 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1858 void *parent, const struct wined3d_parent_ops *parent_ops)
1860 struct wined3d_device_parent *device_parent = device->device_parent;
1861 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1862 struct wined3d_surface *surfaces;
1863 UINT pow2_width, pow2_height;
1864 unsigned int i, j;
1865 HRESULT hr;
1867 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
1868 && !gl_info->supported[EXT_TEXTURE_ARRAY])
1870 WARN("OpenGL implementation does not support array textures.\n");
1871 return WINED3DERR_INVALIDCALL;
1874 /* TODO: It should only be possible to create textures for formats
1875 * that are reported as supported. */
1876 if (WINED3DFMT_UNKNOWN >= desc->format)
1878 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1879 return WINED3DERR_INVALIDCALL;
1882 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1883 FIXME("Trying to create a managed texture with dynamic usage.\n");
1884 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1885 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1886 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1887 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1888 FIXME("Trying to create a render target that isn't in the default pool.\n");
1890 pow2_width = desc->width;
1891 pow2_height = desc->height;
1892 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1893 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1895 /* level_count == 0 returns an error as well. */
1896 if (level_count != 1 || layer_count != 1)
1898 if (desc->pool != WINED3D_POOL_SCRATCH)
1900 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
1901 return WINED3DERR_INVALIDCALL;
1904 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
1906 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1908 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1910 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1912 /* TODO: Add support for non-power-of-two compressed textures. */
1913 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1914 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1916 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1917 desc->width, desc->height);
1918 return WINED3DERR_NOTAVAILABLE;
1921 /* Find the nearest pow2 match. */
1922 pow2_width = pow2_height = 1;
1923 while (pow2_width < desc->width)
1924 pow2_width <<= 1;
1925 while (pow2_height < desc->height)
1926 pow2_height <<= 1;
1927 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1930 texture->pow2_width = pow2_width;
1931 texture->pow2_height = pow2_height;
1933 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1934 && (desc->usage & WINED3DUSAGE_TEXTURE))
1936 /* One of four options:
1937 * 1: Do the same as we do with NPOT and scale the texture. (Any
1938 * texture ops would require the texture to be scaled which is
1939 * potentially slow.)
1940 * 2: Set the texture to the maximum size (bad idea).
1941 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1942 * 4: Create the surface, but allow it to be used only for DirectDraw
1943 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1944 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1945 * the render target. */
1946 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1948 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1949 return WINED3DERR_NOTAVAILABLE;
1952 /* We should never use this surface in combination with OpenGL. */
1953 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1956 /* Calculate levels for mip mapping. */
1957 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1959 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1961 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1962 return WINED3DERR_INVALIDCALL;
1965 if (level_count != 1)
1967 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1968 return WINED3DERR_INVALIDCALL;
1972 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1973 flags, device, parent, parent_ops, &texture_resource_ops)))
1975 WARN("Failed to initialize texture, returning %#x.\n", hr);
1976 return hr;
1979 /* Precalculated scaling for 'faked' non power of two texture coords. */
1980 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1982 texture->pow2_matrix[0] = (float)desc->width;
1983 texture->pow2_matrix[5] = (float)desc->height;
1984 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1985 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1987 else
1989 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1991 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1992 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1993 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1995 else
1997 texture->pow2_matrix[0] = 1.0f;
1998 texture->pow2_matrix[5] = 1.0f;
2000 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2001 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2002 else if (layer_count > 1)
2003 texture->target = GL_TEXTURE_2D_ARRAY;
2004 else
2005 texture->target = GL_TEXTURE_2D;
2007 texture->pow2_matrix[10] = 1.0f;
2008 texture->pow2_matrix[15] = 1.0f;
2009 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2011 if (wined3d_texture_use_pbo(texture, gl_info))
2012 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2014 if (level_count > ~(SIZE_T)0 / layer_count
2015 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2017 wined3d_texture_cleanup_sync(texture);
2018 return E_OUTOFMEMORY;
2021 /* Generate all the surfaces. */
2022 for (i = 0; i < texture->level_count; ++i)
2024 for (j = 0; j < texture->layer_count; ++j)
2026 static const GLenum cube_targets[6] =
2028 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2029 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2030 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2031 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2032 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2033 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2035 struct wined3d_texture_sub_resource *sub_resource;
2036 unsigned int idx = j * texture->level_count + i;
2037 struct wined3d_surface *surface;
2039 surface = &surfaces[idx];
2040 surface->container = texture;
2041 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2042 surface->texture_level = i;
2043 surface->texture_layer = j;
2044 list_init(&surface->renderbuffers);
2045 list_init(&surface->overlays);
2047 sub_resource = &texture->sub_resources[idx];
2048 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2049 sub_resource->u.surface = surface;
2050 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2052 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2053 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2056 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2057 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2059 WARN("Failed to create surface parent, hr %#x.\n", hr);
2060 sub_resource->parent = NULL;
2061 wined3d_texture_cleanup_sync(texture);
2062 return hr;
2065 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2067 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2069 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2070 && FAILED(hr = wined3d_surface_create_dc(surface)))
2072 wined3d_texture_cleanup_sync(texture);
2073 return hr;
2078 return WINED3D_OK;
2081 /* This call just uploads data, the caller is responsible for binding the
2082 * correct texture. */
2083 /* Context activation is done by the caller. */
2084 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2085 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
2086 unsigned int row_pitch, unsigned int slice_pitch)
2088 const struct wined3d_format *format = texture->resource.format;
2089 unsigned int level = sub_resource_idx % texture->level_count;
2090 const struct wined3d_gl_info *gl_info = context->gl_info;
2091 unsigned int dst_row_pitch, dst_slice_pitch;
2092 unsigned int width, height, depth;
2093 const void *mem = data->addr;
2094 void *converted_mem = NULL;
2096 TRACE("texture %p, sub_resource_idx %u, context %p, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2097 texture, sub_resource_idx, context, data->buffer_object, data->addr, row_pitch, slice_pitch);
2099 width = wined3d_texture_get_level_width(texture, level);
2100 height = wined3d_texture_get_level_height(texture, level);
2101 depth = wined3d_texture_get_level_depth(texture, level);
2103 if (format->convert)
2105 if (data->buffer_object)
2106 ERR("Loading a converted texture from a PBO.\n");
2107 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2108 ERR("Converting a block-based format.\n");
2110 dst_row_pitch = width * format->conv_byte_count;
2111 dst_slice_pitch = dst_row_pitch * height;
2113 converted_mem = wined3d_calloc(depth, dst_slice_pitch);
2114 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2115 dst_row_pitch, dst_slice_pitch, width, height, depth);
2116 mem = converted_mem;
2118 else
2120 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2121 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2122 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2125 if (data->buffer_object)
2127 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2128 checkGLcall("glBindBuffer");
2131 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, 0, 0, 0,
2132 width, height, depth, format->glFormat, format->glType, mem));
2133 checkGLcall("glTexSubImage3D");
2135 if (data->buffer_object)
2137 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2138 checkGLcall("glBindBuffer");
2141 HeapFree(GetProcessHeap(), 0, converted_mem);
2144 /* Context activation is done by the caller. */
2145 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2146 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2148 const struct wined3d_format *format = texture->resource.format;
2149 const struct wined3d_gl_info *gl_info = context->gl_info;
2151 if (format->convert)
2153 FIXME("Attempting to download a converted volume, format %s.\n",
2154 debug_d3dformat(format->id));
2155 return;
2158 if (data->buffer_object)
2160 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2161 checkGLcall("glBindBuffer");
2164 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2165 format->glFormat, format->glType, data->addr);
2166 checkGLcall("glGetTexImage");
2168 if (data->buffer_object)
2170 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2171 checkGLcall("glBindBuffer");
2176 /* Context activation is done by the caller. */
2177 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2178 struct wined3d_context *context, BOOL dest_is_srgb)
2180 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2181 unsigned int row_pitch, slice_pitch;
2182 struct wined3d_bo_address data;
2184 /* Optimisations are possible, but the effort should be put into either
2185 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2186 * picked the wrong copy for the original upload and fixing that.
2188 * Also keep in mind that we want to avoid using resource.heap_memory
2189 * for DEFAULT pool surfaces. */
2190 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2191 data.buffer_object = 0;
2192 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2193 return;
2195 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2196 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2197 texture3d_download_data(texture, sub_resource_idx, context, &data);
2198 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2199 texture3d_upload_data(texture, sub_resource_idx, context,
2200 wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2202 HeapFree(GetProcessHeap(), 0, data.addr);
2205 /* Context activation is done by the caller. */
2206 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2207 struct wined3d_context *context, DWORD location)
2209 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2210 DWORD required_access = wined3d_resource_access_from_location(location);
2211 unsigned int row_pitch, slice_pitch;
2213 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2214 texture, sub_resource_idx, context, wined3d_debug_location(location));
2216 TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource->locations));
2218 if ((sub_resource->locations & location) == location)
2220 TRACE("Location(s) already up to date.\n");
2221 return TRUE;
2224 if ((texture->resource.access_flags & required_access) != required_access)
2226 ERR("Operation requires %#x access, but volume only has %#x.\n",
2227 required_access, texture->resource.access_flags);
2228 return FALSE;
2231 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2232 return FALSE;
2234 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
2236 TRACE("Volume previously discarded, nothing to do.\n");
2237 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2238 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
2239 goto done;
2242 switch (location)
2244 case WINED3D_LOCATION_TEXTURE_RGB:
2245 case WINED3D_LOCATION_TEXTURE_SRGB:
2246 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2248 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2249 data.addr += sub_resource->offset;
2250 wined3d_texture_bind_and_dirtify(texture, context,
2251 location == WINED3D_LOCATION_TEXTURE_SRGB);
2252 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2253 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2255 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2257 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2258 wined3d_texture_bind_and_dirtify(texture, context,
2259 location == WINED3D_LOCATION_TEXTURE_SRGB);
2260 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2261 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2263 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2265 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2267 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2269 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2271 else
2273 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2274 return FALSE;
2276 break;
2278 case WINED3D_LOCATION_SYSMEM:
2279 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2281 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2283 data.addr += sub_resource->offset;
2284 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2285 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2286 else
2287 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2289 texture3d_download_data(texture, sub_resource_idx, context, &data);
2290 ++texture->download_count;
2292 else
2294 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2295 wined3d_debug_location(sub_resource->locations));
2296 return FALSE;
2298 break;
2300 case WINED3D_LOCATION_BUFFER:
2301 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2303 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2305 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2306 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2307 else
2308 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2310 texture3d_download_data(texture, sub_resource_idx, context, &data);
2312 else
2314 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2315 wined3d_debug_location(sub_resource->locations));
2316 return FALSE;
2318 break;
2320 default:
2321 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2322 wined3d_debug_location(sub_resource->locations));
2323 return FALSE;
2326 done:
2327 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2329 return TRUE;
2332 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2334 const struct wined3d_format *format = texture->resource.format;
2335 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2336 unsigned int sub_count = texture->level_count * texture->layer_count;
2337 const struct wined3d_gl_info *gl_info = context->gl_info;
2338 unsigned int i;
2340 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2342 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2344 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2345 wined3d_texture_get_level_width(texture, 0),
2346 wined3d_texture_get_level_height(texture, 0),
2347 wined3d_texture_get_level_depth(texture, 0)));
2348 checkGLcall("glTexStorage3D");
2350 else
2352 for (i = 0; i < sub_count; ++i)
2354 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2355 wined3d_texture_get_level_width(texture, i),
2356 wined3d_texture_get_level_height(texture, i),
2357 wined3d_texture_get_level_depth(texture, i),
2358 0, format->glFormat, format->glType, NULL));
2359 checkGLcall("glTexImage3D");
2364 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2368 static const struct wined3d_texture_ops texture3d_ops =
2370 texture3d_upload_data,
2371 texture3d_load_location,
2372 texture3d_prepare_texture,
2373 texture3d_cleanup_sub_resources,
2376 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2377 unsigned int level, const struct wined3d_box *box)
2379 const struct wined3d_format *format = texture->resource.format;
2380 unsigned int height = wined3d_texture_get_level_height(texture, level);
2381 unsigned int width = wined3d_texture_get_level_width(texture, level);
2382 unsigned int width_mask, height_mask;
2384 if ((box->left >= box->right)
2385 || (box->top >= box->bottom)
2386 || (box->right > width)
2387 || (box->bottom > height))
2388 return FALSE;
2390 /* This assumes power of two block sizes, but NPOT block sizes would be
2391 * silly anyway.
2393 * This also assumes that the format's block depth is 1. */
2394 width_mask = format->block_width - 1;
2395 height_mask = format->block_height - 1;
2397 if ((box->left & width_mask) || (box->top & height_mask)
2398 || (box->right & width_mask && box->right != width)
2399 || (box->bottom & height_mask && box->bottom != height))
2400 return FALSE;
2402 return TRUE;
2405 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2406 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2407 const struct wined3d_parent_ops *parent_ops)
2409 struct wined3d_device_parent *device_parent = device->device_parent;
2410 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2411 unsigned int i;
2412 HRESULT hr;
2414 if (layer_count != 1)
2416 ERR("Invalid layer count for volume texture.\n");
2417 return E_INVALIDARG;
2420 /* TODO: It should only be possible to create textures for formats
2421 * that are reported as supported. */
2422 if (WINED3DFMT_UNKNOWN >= desc->format)
2424 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2425 return WINED3DERR_INVALIDCALL;
2428 if (!gl_info->supported[EXT_TEXTURE3D])
2430 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2431 return WINED3DERR_INVALIDCALL;
2434 /* Calculate levels for mip mapping. */
2435 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2437 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2439 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2440 return WINED3DERR_INVALIDCALL;
2443 if (level_count != 1)
2445 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2446 return WINED3DERR_INVALIDCALL;
2450 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2451 || desc->pool == WINED3D_POOL_SCRATCH))
2453 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2454 return WINED3DERR_INVALIDCALL;
2457 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2459 UINT pow2_w, pow2_h, pow2_d;
2460 pow2_w = 1;
2461 while (pow2_w < desc->width)
2462 pow2_w <<= 1;
2463 pow2_h = 1;
2464 while (pow2_h < desc->height)
2465 pow2_h <<= 1;
2466 pow2_d = 1;
2467 while (pow2_d < desc->depth)
2468 pow2_d <<= 1;
2470 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2472 if (desc->pool == WINED3D_POOL_SCRATCH)
2474 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2476 else
2478 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2479 desc->width, desc->height, desc->depth);
2480 return WINED3DERR_INVALIDCALL;
2485 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2486 0, device, parent, parent_ops, &texture_resource_ops)))
2488 WARN("Failed to initialize texture, returning %#x.\n", hr);
2489 return hr;
2492 texture->pow2_matrix[0] = 1.0f;
2493 texture->pow2_matrix[5] = 1.0f;
2494 texture->pow2_matrix[10] = 1.0f;
2495 texture->pow2_matrix[15] = 1.0f;
2496 texture->target = GL_TEXTURE_3D;
2498 if (wined3d_texture_use_pbo(texture, gl_info))
2500 wined3d_resource_free_sysmem(&texture->resource);
2501 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2504 /* Generate all the surfaces. */
2505 for (i = 0; i < texture->level_count; ++i)
2507 struct wined3d_texture_sub_resource *sub_resource;
2509 sub_resource = &texture->sub_resources[i];
2510 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2512 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2513 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2515 WARN("Failed to create volume parent, hr %#x.\n", hr);
2516 sub_resource->parent = NULL;
2517 wined3d_texture_cleanup_sync(texture);
2518 return hr;
2521 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2523 TRACE("Created volume level %u.\n", i);
2526 return WINED3D_OK;
2529 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2530 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2531 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2533 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2535 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2536 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2537 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2538 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2540 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2541 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2542 return WINED3DERR_INVALIDCALL;
2544 if (src_texture)
2546 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2547 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2548 return WINED3DERR_INVALIDCALL;
2551 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2552 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2555 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2556 unsigned int sub_resource_idx, LONG *x, LONG *y)
2558 struct wined3d_surface *surface;
2560 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2562 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2563 || sub_resource_idx >= texture->level_count * texture->layer_count)
2565 WARN("Invalid sub-resource specified.\n");
2566 return WINEDDERR_NOTAOVERLAYSURFACE;
2569 surface = texture->sub_resources[sub_resource_idx].u.surface;
2570 if (!surface->overlay_dest)
2572 TRACE("Overlay not visible.\n");
2573 *x = 0;
2574 *y = 0;
2575 return WINEDDERR_OVERLAYNOTVISIBLE;
2578 *x = surface->overlay_destrect.left;
2579 *y = surface->overlay_destrect.top;
2581 TRACE("Returning position %d, %d.\n", *x, *y);
2583 return WINED3D_OK;
2586 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2587 unsigned int sub_resource_idx, LONG x, LONG y)
2589 struct wined3d_texture_sub_resource *sub_resource;
2590 struct wined3d_surface *surface;
2591 LONG w, h;
2593 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2595 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2596 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2598 WARN("Invalid sub-resource specified.\n");
2599 return WINEDDERR_NOTAOVERLAYSURFACE;
2602 surface = sub_resource->u.surface;
2603 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2604 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2605 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2607 return WINED3D_OK;
2610 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2611 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2612 const RECT *dst_rect, DWORD flags)
2614 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2615 struct wined3d_surface *surface, *dst_surface;
2617 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2618 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2619 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2620 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2622 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2623 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2625 WARN("Invalid sub-resource specified.\n");
2626 return WINEDDERR_NOTAOVERLAYSURFACE;
2629 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2630 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2632 WARN("Invalid destination sub-resource specified.\n");
2633 return WINED3DERR_INVALIDCALL;
2636 surface = sub_resource->u.surface;
2637 if (src_rect)
2638 surface->overlay_srcrect = *src_rect;
2639 else
2640 SetRect(&surface->overlay_srcrect, 0, 0,
2641 wined3d_texture_get_level_width(texture, surface->texture_level),
2642 wined3d_texture_get_level_height(texture, surface->texture_level));
2644 dst_surface = dst_sub_resource->u.surface;
2645 if (dst_rect)
2646 surface->overlay_destrect = *dst_rect;
2647 else
2648 SetRect(&surface->overlay_destrect, 0, 0,
2649 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2650 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2652 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2654 surface->overlay_dest = NULL;
2655 list_remove(&surface->overlay_entry);
2658 if (flags & WINEDDOVER_SHOW)
2660 if (surface->overlay_dest != dst_surface)
2662 surface->overlay_dest = dst_surface;
2663 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2666 else if (flags & WINEDDOVER_HIDE)
2668 /* Tests show that the rectangles are erased on hide. */
2669 SetRectEmpty(&surface->overlay_srcrect);
2670 SetRectEmpty(&surface->overlay_destrect);
2671 surface->overlay_dest = NULL;
2674 return WINED3D_OK;
2677 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2679 unsigned int sub_count = texture->level_count * texture->layer_count;
2681 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2683 if (sub_resource_idx >= sub_count)
2685 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2686 return NULL;
2689 return texture->sub_resources[sub_resource_idx].parent;
2692 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2693 unsigned int sub_resource_idx, void *parent)
2695 unsigned int sub_count = texture->level_count * texture->layer_count;
2697 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2699 if (sub_resource_idx >= sub_count)
2701 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2702 return;
2705 texture->sub_resources[sub_resource_idx].parent = parent;
2708 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2709 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2711 unsigned int sub_count = texture->level_count * texture->layer_count;
2712 const struct wined3d_resource *resource;
2713 unsigned int level_idx;
2715 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2717 if (sub_resource_idx >= sub_count)
2719 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2720 return WINED3DERR_INVALIDCALL;
2723 resource = &texture->resource;
2724 desc->format = resource->format->id;
2725 desc->multisample_type = resource->multisample_type;
2726 desc->multisample_quality = resource->multisample_quality;
2727 desc->usage = resource->usage;
2728 desc->pool = resource->pool;
2730 level_idx = sub_resource_idx % texture->level_count;
2731 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2732 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2733 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2734 desc->size = texture->sub_resources[sub_resource_idx].size;
2736 return WINED3D_OK;
2739 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2740 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2741 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2743 struct wined3d_texture *object;
2744 HRESULT hr;
2746 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2747 "parent %p, parent_ops %p, texture %p.\n",
2748 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2750 if (!layer_count)
2752 WARN("Invalid layer count.\n");
2753 return E_INVALIDARG;
2755 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2757 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2758 layer_count = 6;
2761 if (!level_count)
2763 WARN("Invalid level count.\n");
2764 return WINED3DERR_INVALIDCALL;
2767 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2769 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2771 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2772 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2774 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2775 desc->multisample_quality);
2776 return WINED3DERR_NOTAVAILABLE;
2778 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2779 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2780 || desc->multisample_quality))
2782 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2783 desc->multisample_quality);
2784 return WINED3DERR_NOTAVAILABLE;
2788 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2789 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2790 return E_OUTOFMEMORY;
2792 switch (desc->resource_type)
2794 case WINED3D_RTYPE_TEXTURE_2D:
2795 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2796 break;
2798 case WINED3D_RTYPE_TEXTURE_3D:
2799 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2800 break;
2802 default:
2803 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2804 hr = WINED3DERR_INVALIDCALL;
2805 break;
2808 if (FAILED(hr))
2810 WARN("Failed to initialize texture, returning %#x.\n", hr);
2811 HeapFree(GetProcessHeap(), 0, object);
2812 return hr;
2815 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2816 * in this case. */
2817 if (data)
2819 unsigned int sub_count = level_count * layer_count;
2820 struct wined3d_context *context;
2821 unsigned int i;
2823 for (i = 0; i < sub_count; ++i)
2825 if (!data[i].data)
2827 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2828 wined3d_texture_cleanup_sync(object);
2829 HeapFree(GetProcessHeap(), 0, object);
2830 return E_INVALIDARG;
2834 context = context_acquire(device, NULL);
2836 wined3d_texture_prepare_texture(object, context, FALSE);
2837 wined3d_texture_bind_and_dirtify(object, context, FALSE);
2839 for (i = 0; i < sub_count; ++i)
2841 const struct wined3d_const_bo_address addr = {0, data[i].data};
2843 wined3d_texture_upload_data(object, i, context, &addr, data[i].row_pitch, data[i].slice_pitch);
2844 wined3d_texture_validate_location(object, i, WINED3D_LOCATION_TEXTURE_RGB);
2845 wined3d_texture_invalidate_location(object, i, ~WINED3D_LOCATION_TEXTURE_RGB);
2848 context_release(context);
2851 TRACE("Created texture %p.\n", object);
2852 *texture = object;
2854 return WINED3D_OK;
2857 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2859 struct wined3d_device *device = texture->resource.device;
2860 struct wined3d_texture_sub_resource *sub_resource;
2861 struct wined3d_context *context = NULL;
2862 struct wined3d_surface *surface;
2863 HRESULT hr = WINED3D_OK;
2865 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2867 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2868 return WINED3DERR_INVALIDCALL;
2870 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2872 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2873 return WINED3DERR_INVALIDCALL;
2876 surface = sub_resource->u.surface;
2878 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2879 return WINED3DERR_INVALIDCALL;
2881 if (device->d3d_initialized)
2882 context = context_acquire(device, NULL);
2884 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2885 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2887 if (!surface->dc)
2888 hr = wined3d_surface_create_dc(surface);
2889 if (context)
2890 context_release(context);
2891 if (FAILED(hr))
2892 return WINED3DERR_INVALIDCALL;
2894 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2895 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2896 ++texture->resource.map_count;
2897 ++sub_resource->map_count;
2899 *dc = surface->dc;
2900 TRACE("Returning dc %p.\n", *dc);
2902 return hr;
2905 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2907 struct wined3d_device *device = texture->resource.device;
2908 struct wined3d_texture_sub_resource *sub_resource;
2909 struct wined3d_surface *surface;
2911 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2913 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2914 return WINED3DERR_INVALIDCALL;
2916 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2918 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2919 return WINED3DERR_INVALIDCALL;
2922 surface = sub_resource->u.surface;
2924 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2925 return WINED3DERR_INVALIDCALL;
2927 if (surface->dc != dc)
2929 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2930 return WINED3DERR_INVALIDCALL;
2933 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2934 wined3d_surface_destroy_dc(surface);
2936 --sub_resource->map_count;
2937 if (!--texture->resource.map_count && texture->update_map_binding)
2938 wined3d_texture_update_map_binding(texture);
2939 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2940 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2942 return WINED3D_OK;