msvcirt: Implement stdiostream constructors.
[wine.git] / dlls / wined3d / texture.c
blobc82016e55511ea9952fdd28fceb8a9fe48827a6e
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_GET_DC | WINED3D_TEXTURE_CREATE_GET_DC_LENIENT))
328 texture->flags |= WINED3D_TEXTURE_GET_DC;
329 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
330 texture->flags |= WINED3D_TEXTURE_DISCARD;
332 return WINED3D_OK;
335 /* Context activation is done by the caller. */
336 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
337 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
339 GLuint *buffer_object;
341 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
342 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
343 checkGLcall("glDeleteBuffers");
344 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
345 *buffer_object = 0;
347 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
348 *buffer_object, texture, sub_resource_idx);
351 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
353 unsigned int sub_count = texture->level_count * texture->layer_count;
354 const struct wined3d_device *device = texture->resource.device;
355 DWORD map_binding = texture->update_map_binding;
356 struct wined3d_context *context = NULL;
357 unsigned int i;
359 if (device->d3d_initialized)
360 context = context_acquire(device, NULL);
362 for (i = 0; i < sub_count; ++i)
364 if (texture->sub_resources[i].locations == texture->resource.map_binding
365 && !wined3d_texture_load_location(texture, i, context, map_binding))
366 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
367 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
368 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
371 if (context)
372 context_release(context);
374 texture->resource.map_binding = map_binding;
375 texture->update_map_binding = 0;
378 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
380 texture->update_map_binding = map_binding;
381 if (!texture->resource.map_count)
382 wined3d_texture_update_map_binding(texture);
385 /* A GL context is provided by the caller */
386 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
387 struct gl_texture *tex)
389 context_gl_resource_released(device, tex->name, FALSE);
390 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
391 tex->name = 0;
394 /* Context activation is done by the caller. */
395 /* The caller is responsible for binding the correct texture. */
396 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture *texture,
397 GLenum gl_internal_format, const struct wined3d_format *format,
398 const struct wined3d_gl_info *gl_info)
400 unsigned int i, sub_call_count;
402 sub_call_count = texture->level_count;
403 if (texture->target != GL_TEXTURE_2D_ARRAY)
404 sub_call_count *= texture->layer_count;
406 for (i = 0; i < sub_call_count; ++i)
408 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
409 GLsizei width, height;
411 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
412 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
413 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
415 height *= format->height_scale.numerator;
416 height /= format->height_scale.denominator;
419 TRACE("surface %p, target %#x, level %u, width %u, height %u.\n",
420 surface, surface->texture_target, surface->texture_level, width, height);
422 if (texture->target == GL_TEXTURE_2D_ARRAY)
424 GL_EXTCALL(glTexImage3D(surface->texture_target, surface->texture_level,
425 gl_internal_format, width, height, texture->layer_count, 0,
426 format->glFormat, format->glType, NULL));
427 checkGLcall("glTexImage3D");
429 else
431 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
432 gl_internal_format, width, height, 0, format->glFormat, format->glType, NULL);
433 checkGLcall("glTexImage2D");
438 /* Context activation is done by the caller. */
439 /* The caller is responsible for binding the correct texture. */
440 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture *texture,
441 GLenum gl_internal_format, const struct wined3d_gl_info *gl_info)
443 GLsizei width = wined3d_texture_get_level_pow2_width(texture, 0);
444 GLsizei height = wined3d_texture_get_level_pow2_height(texture, 0);
446 if (texture->target == GL_TEXTURE_2D_ARRAY)
448 GL_EXTCALL(glTexStorage3D(texture->target, texture->level_count, gl_internal_format,
449 width, height, texture->layer_count));
450 checkGLcall("glTexStorage3D");
452 else
454 GL_EXTCALL(glTexStorage2D(texture->target, texture->level_count, gl_internal_format,
455 width, height));
456 checkGLcall("glTexStorage2D");
460 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
462 struct wined3d_device *device = texture->resource.device;
463 const struct wined3d_gl_info *gl_info = NULL;
464 struct wined3d_context *context = NULL;
466 if (texture->texture_rgb.name || texture->texture_srgb.name
467 || texture->rb_multisample || texture->rb_resolved)
469 context = context_acquire(device, NULL);
470 gl_info = context->gl_info;
473 if (texture->texture_rgb.name)
474 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
476 if (texture->texture_srgb.name)
477 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
479 if (texture->rb_multisample)
481 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
482 context_gl_resource_released(device, texture->rb_multisample, TRUE);
483 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
484 texture->rb_multisample = 0;
487 if (texture->rb_resolved)
489 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
490 context_gl_resource_released(device, texture->rb_resolved, TRUE);
491 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
492 texture->rb_resolved = 0;
495 if (context) context_release(context);
497 wined3d_texture_set_dirty(texture);
499 resource_unload(&texture->resource);
502 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture *texture)
504 unsigned int sub_count = texture->level_count * texture->layer_count;
505 struct wined3d_texture_sub_resource *sub_resource;
506 unsigned int i;
508 for (i = 0; i < sub_count; ++i)
510 sub_resource = &texture->sub_resources[i];
511 if (sub_resource->parent)
513 TRACE("sub-resource %u.\n", i);
514 sub_resource->parent_ops->wined3d_object_destroyed(sub_resource->parent);
515 sub_resource->parent = NULL;
520 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
522 unsigned int sub_count = texture->level_count * texture->layer_count;
523 struct wined3d_device *device = texture->resource.device;
524 struct wined3d_context *context = NULL;
525 const struct wined3d_gl_info *gl_info;
526 GLuint buffer_object;
527 unsigned int i;
529 TRACE("texture %p.\n", texture);
531 for (i = 0; i < sub_count; ++i)
533 if (!(buffer_object = texture->sub_resources[i].buffer_object))
534 continue;
536 TRACE("Deleting buffer object %u.\n", buffer_object);
538 /* We may not be able to get a context in wined3d_texture_cleanup() in
539 * general, but if a buffer object was previously created we can. */
540 if (!context)
542 context = context_acquire(device, NULL);
543 gl_info = context->gl_info;
546 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
548 if (context)
549 context_release(context);
551 texture->texture_ops->texture_cleanup_sub_resources(texture);
552 wined3d_texture_unload_gl_texture(texture);
555 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
557 texture->swapchain = swapchain;
558 wined3d_resource_update_draw_binding(&texture->resource);
561 /* Context activation is done by the caller. */
562 void wined3d_texture_bind(struct wined3d_texture *texture,
563 struct wined3d_context *context, BOOL srgb)
565 const struct wined3d_gl_info *gl_info = context->gl_info;
566 const struct wined3d_format *format = texture->resource.format;
567 const struct color_fixup_desc fixup = format->color_fixup;
568 struct gl_texture *gl_tex;
569 GLenum target;
571 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
573 if (!needs_separate_srgb_gl_texture(context))
574 srgb = FALSE;
576 /* sRGB mode cache for preload() calls outside drawprim. */
577 if (srgb)
578 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
579 else
580 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
582 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
583 target = texture->target;
585 if (gl_tex->name)
587 context_bind_texture(context, target, gl_tex->name);
588 return;
591 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
592 checkGLcall("glGenTextures");
593 TRACE("Generated texture %d.\n", gl_tex->name);
595 if (!gl_tex->name)
597 ERR("Failed to generate a texture name.\n");
598 return;
601 /* Initialise the state of the texture object to the OpenGL defaults, not
602 * the wined3d defaults. */
603 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
604 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
605 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
606 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
607 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
608 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
609 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
610 gl_tex->sampler_desc.lod_bias = 0.0f;
611 gl_tex->sampler_desc.min_lod = -1000.0f;
612 gl_tex->sampler_desc.max_lod = 1000.0f;
613 gl_tex->sampler_desc.max_anisotropy = 1;
614 gl_tex->sampler_desc.compare = FALSE;
615 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
616 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
617 gl_tex->sampler_desc.srgb_decode = TRUE;
618 else
619 gl_tex->sampler_desc.srgb_decode = srgb;
620 gl_tex->base_level = 0;
621 wined3d_texture_set_dirty(texture);
623 context_bind_texture(context, target, gl_tex->name);
625 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
627 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
628 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
631 /* For a new texture we have to set the texture levels after binding the
632 * texture. Beware that texture rectangles do not support mipmapping, but
633 * set the maxmiplevel if we're relying on the partial
634 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
635 * (I.e., do not care about cond_np2 here, just look for
636 * GL_TEXTURE_RECTANGLE_ARB.) */
637 if (target != GL_TEXTURE_RECTANGLE_ARB)
639 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
640 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
641 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
644 if (target == GL_TEXTURE_CUBE_MAP_ARB)
646 /* Cubemaps are always set to clamp, regardless of the sampler state. */
647 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
648 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
649 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
652 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
654 /* Conditinal non power of two textures use a different clamping
655 * default. If we're using the GL_WINE_normalized_texrect partial
656 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
657 * has the address mode set to repeat - something that prevents us
658 * from hitting the accelerated codepath. Thus manually set the GL
659 * state. The same applies to filtering. Even if the texture has only
660 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
661 * fallback on macos. */
662 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
663 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
664 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
665 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
666 checkGLcall("glTexParameteri");
667 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
668 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
669 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
670 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
671 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
674 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
676 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
677 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
680 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
682 static const GLenum swizzle_source[] =
684 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
685 GL_ONE, /* CHANNEL_SOURCE_ONE */
686 GL_RED, /* CHANNEL_SOURCE_X */
687 GL_GREEN, /* CHANNEL_SOURCE_Y */
688 GL_BLUE, /* CHANNEL_SOURCE_Z */
689 GL_ALPHA, /* CHANNEL_SOURCE_W */
691 struct
693 GLint x, y, z, w;
695 swizzle;
697 swizzle.x = swizzle_source[fixup.x_source];
698 swizzle.y = swizzle_source[fixup.y_source];
699 swizzle.z = swizzle_source[fixup.z_source];
700 swizzle.w = swizzle_source[fixup.w_source];
701 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
702 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
706 /* Context activation is done by the caller. */
707 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
708 struct wined3d_context *context, BOOL srgb)
710 DWORD active_sampler;
712 /* We don't need a specific texture unit, but after binding the texture
713 * the current unit is dirty. Read the unit back instead of switching to
714 * 0, this avoids messing around with the state manager's GL states. The
715 * current texture unit should always be a valid one.
717 * To be more specific, this is tricky because we can implicitly be
718 * called from sampler() in state.c. This means we can't touch anything
719 * other than whatever happens to be the currently active texture, or we
720 * would risk marking already applied sampler states dirty again. */
721 active_sampler = context->rev_tex_unit_map[context->active_texture];
722 if (active_sampler != WINED3D_UNMAPPED_STAGE)
723 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
724 /* FIXME: Ideally we'd only do this when touching a binding that's used by
725 * a shader. */
726 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
728 wined3d_texture_bind(texture, context, srgb);
731 /* Context activation is done by the caller (state handler). */
732 /* This function relies on the correct texture being bound and loaded. */
733 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
734 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
736 const struct wined3d_gl_info *gl_info = context->gl_info;
737 GLenum target = texture->target;
738 struct gl_texture *gl_tex;
739 DWORD state;
741 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
743 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
745 state = sampler_desc->address_u;
746 if (state != gl_tex->sampler_desc.address_u)
748 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
749 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
750 gl_tex->sampler_desc.address_u = state;
753 state = sampler_desc->address_v;
754 if (state != gl_tex->sampler_desc.address_v)
756 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
757 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
758 gl_tex->sampler_desc.address_v = state;
761 state = sampler_desc->address_w;
762 if (state != gl_tex->sampler_desc.address_w)
764 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
765 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
766 gl_tex->sampler_desc.address_w = state;
769 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
770 sizeof(gl_tex->sampler_desc.border_color)))
772 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
773 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
774 sizeof(gl_tex->sampler_desc.border_color));
777 state = sampler_desc->mag_filter;
778 if (state != gl_tex->sampler_desc.mag_filter)
780 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
781 gl_tex->sampler_desc.mag_filter = state;
784 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
785 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
787 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
788 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
789 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
790 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
793 state = sampler_desc->max_anisotropy;
794 if (state != gl_tex->sampler_desc.max_anisotropy)
796 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
797 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
798 else
799 WARN("Anisotropic filtering not supported.\n");
800 gl_tex->sampler_desc.max_anisotropy = state;
803 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
804 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
805 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
807 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
808 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
809 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
812 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
814 if (sampler_desc->compare)
815 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
816 else
817 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
818 gl_tex->sampler_desc.compare = sampler_desc->compare;
821 checkGLcall("Texture parameter application");
823 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
825 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
826 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
827 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
831 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
833 ULONG refcount;
835 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
837 if (texture->swapchain)
838 return wined3d_swapchain_incref(texture->swapchain);
840 refcount = InterlockedIncrement(&texture->resource.ref);
841 TRACE("%p increasing refcount to %u.\n", texture, refcount);
843 return refcount;
846 static void wined3d_texture_cleanup_sync(struct wined3d_texture *texture)
848 wined3d_texture_sub_resources_destroyed(texture);
849 resource_cleanup(&texture->resource);
850 wined3d_resource_wait_idle(&texture->resource);
851 wined3d_texture_cleanup(texture);
854 static void wined3d_texture_destroy_object(void *object)
856 wined3d_texture_cleanup(object);
857 HeapFree(GetProcessHeap(), 0, object);
860 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
862 ULONG refcount;
864 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
866 if (texture->swapchain)
867 return wined3d_swapchain_decref(texture->swapchain);
869 refcount = InterlockedDecrement(&texture->resource.ref);
870 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
872 if (!refcount)
874 /* Wait for the texture to become idle if it's using user memory,
875 * since the application is allowed to free that memory once the
876 * texture is destroyed. Note that this implies that
877 * wined3d_texture_destroy_object() can't access that memory either. */
878 if (texture->user_memory)
879 wined3d_resource_wait_idle(&texture->resource);
880 wined3d_texture_sub_resources_destroyed(texture);
881 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
882 resource_cleanup(&texture->resource);
883 wined3d_cs_emit_destroy_object(texture->resource.device->cs, wined3d_texture_destroy_object, texture);
886 return refcount;
889 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
891 TRACE("texture %p.\n", texture);
893 return &texture->resource;
896 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
898 return c1->color_space_low_value == c2->color_space_low_value
899 && c1->color_space_high_value == c2->color_space_high_value;
902 /* Context activation is done by the caller */
903 void wined3d_texture_load(struct wined3d_texture *texture,
904 struct wined3d_context *context, BOOL srgb)
906 UINT sub_count = texture->level_count * texture->layer_count;
907 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
908 DWORD flag;
909 UINT i;
911 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
913 if (!needs_separate_srgb_gl_texture(context))
914 srgb = FALSE;
916 if (srgb)
917 flag = WINED3D_TEXTURE_SRGB_VALID;
918 else
919 flag = WINED3D_TEXTURE_RGB_VALID;
921 if (!d3d_info->shader_color_key
922 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
923 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
924 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
925 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
927 unsigned int sub_count = texture->level_count * texture->layer_count;
928 unsigned int i;
930 TRACE("Reloading because of color key value change.\n");
931 for (i = 0; i < sub_count; i++)
933 if (!wined3d_texture_load_location(texture, i, context, texture->resource.map_binding))
934 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
935 else
936 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
939 texture->async.gl_color_key = texture->async.src_blt_color_key;
942 if (texture->flags & flag)
944 TRACE("Texture %p not dirty, nothing to do.\n", texture);
945 return;
948 /* Reload the surfaces if the texture is marked dirty. */
949 for (i = 0; i < sub_count; ++i)
951 if (!wined3d_texture_load_location(texture, i, context,
952 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
953 ERR("Failed to load location (srgb %#x).\n", srgb);
955 texture->flags |= flag;
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 wined3d_resource_wait_idle(&texture->resource);
1019 texture->lod = lod;
1021 texture->texture_rgb.base_level = ~0u;
1022 texture->texture_srgb.base_level = ~0u;
1023 if (texture->resource.bind_count)
1024 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
1027 return old;
1030 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
1032 TRACE("texture %p, returning %u.\n", texture, texture->lod);
1034 return texture->lod;
1037 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
1039 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
1041 return texture->level_count;
1044 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
1045 enum wined3d_texture_filter_type filter_type)
1047 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
1049 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
1051 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
1052 return WINED3DERR_INVALIDCALL;
1055 texture->filter_type = filter_type;
1057 return WINED3D_OK;
1060 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
1062 TRACE("texture %p.\n", texture);
1064 return texture->filter_type;
1067 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
1068 DWORD flags, const struct wined3d_color_key *color_key)
1070 struct wined3d_device *device = texture->resource.device;
1071 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
1072 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
1074 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
1076 if (flags & ~all_flags)
1078 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1079 return WINED3DERR_INVALIDCALL;
1082 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
1084 return WINED3D_OK;
1087 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
1088 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
1089 UINT multisample_quality, void *mem, UINT pitch)
1091 struct wined3d_device *device = texture->resource.device;
1092 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1093 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
1094 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
1095 struct wined3d_texture_sub_resource *sub_resource;
1096 struct wined3d_surface *surface;
1097 DWORD valid_location = 0;
1098 BOOL create_dib = FALSE;
1100 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1101 "mem %p, pitch %u.\n",
1102 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
1104 if (!resource_size)
1105 return WINED3DERR_INVALIDCALL;
1107 if (texture->level_count * texture->layer_count > 1)
1109 WARN("Texture has multiple sub-resources, not supported.\n");
1110 return WINED3DERR_INVALIDCALL;
1113 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
1115 WARN("Not supported on 3D textures.\n");
1116 return WINED3DERR_INVALIDCALL;
1119 if (texture->resource.map_count)
1121 WARN("Texture is mapped.\n");
1122 return WINED3DERR_INVALIDCALL;
1125 /* We have no way of supporting a pitch that is not a multiple of the pixel
1126 * byte width short of uploading the texture row-by-row.
1127 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1128 * for user-memory textures (it always expects packed data) while DirectDraw
1129 * requires a 4-byte aligned pitch and doesn't support texture formats
1130 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1131 * This check is here to verify that the assumption holds. */
1132 if (pitch % texture->resource.format->byte_count)
1134 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1135 return WINED3DERR_INVALIDCALL;
1138 if (device->d3d_initialized)
1139 wined3d_cs_emit_unload_resource(device->cs, &texture->resource);
1140 wined3d_resource_wait_idle(&texture->resource);
1142 sub_resource = &texture->sub_resources[0];
1143 surface = sub_resource->u.surface;
1144 if (surface->dc)
1146 wined3d_surface_destroy_dc(surface);
1147 create_dib = TRUE;
1150 wined3d_resource_free_sysmem(&texture->resource);
1152 if ((texture->row_pitch = pitch))
1153 texture->slice_pitch = height * pitch;
1154 else
1155 /* User memory surfaces don't have the regular surface alignment. */
1156 wined3d_format_calculate_pitch(format, 1, width, height,
1157 &texture->row_pitch, &texture->slice_pitch);
1159 texture->resource.format = format;
1160 texture->resource.multisample_type = multisample_type;
1161 texture->resource.multisample_quality = multisample_quality;
1162 texture->resource.width = width;
1163 texture->resource.height = height;
1164 texture->resource.size = texture->slice_pitch;
1165 sub_resource->size = texture->slice_pitch;
1166 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
1168 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
1169 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1171 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1172 texture->pow2_width = texture->pow2_height = 1;
1173 while (texture->pow2_width < width)
1174 texture->pow2_width <<= 1;
1175 while (texture->pow2_height < height)
1176 texture->pow2_height <<= 1;
1178 else
1180 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
1181 texture->pow2_width = width;
1182 texture->pow2_height = height;
1185 if ((texture->user_memory = mem))
1187 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
1188 valid_location = WINED3D_LOCATION_USER_MEMORY;
1190 else
1192 wined3d_texture_prepare_location(texture, 0, NULL, WINED3D_LOCATION_SYSMEM);
1193 valid_location = WINED3D_LOCATION_SYSMEM;
1196 /* The format might be changed to a format that needs conversion.
1197 * If the surface didn't use PBOs previously but could now, don't
1198 * change it - whatever made us not use PBOs might come back, e.g.
1199 * color keys. */
1200 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1201 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1203 wined3d_texture_validate_location(texture, 0, valid_location);
1204 wined3d_texture_invalidate_location(texture, 0, ~valid_location);
1206 if (create_dib)
1207 wined3d_surface_create_dc(surface);
1209 return WINED3D_OK;
1212 /* Context activation is done by the caller. */
1213 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1214 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1216 struct wined3d_texture_sub_resource *sub_resource;
1218 sub_resource = &texture->sub_resources[sub_resource_idx];
1219 if (sub_resource->buffer_object)
1220 return;
1222 GL_EXTCALL(glGenBuffers(1, &sub_resource->buffer_object));
1223 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, sub_resource->buffer_object));
1224 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER, sub_resource->size, NULL, GL_STREAM_DRAW));
1225 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1226 checkGLcall("Create buffer object");
1228 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1229 sub_resource->buffer_object, texture, sub_resource_idx);
1232 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1234 unsigned int sub_count = texture->level_count * texture->layer_count;
1235 unsigned int i;
1237 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1238 | WINED3D_TEXTURE_CONVERTED);
1239 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1240 for (i = 0; i < sub_count; ++i)
1242 wined3d_texture_invalidate_location(texture, i,
1243 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1247 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1249 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1250 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1252 if (!d3d_info->shader_color_key
1253 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1254 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1256 wined3d_texture_force_reload(texture);
1258 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1259 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1262 if (texture->flags & alloc_flag)
1263 return;
1265 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1266 texture->flags |= alloc_flag;
1269 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1270 const struct wined3d_gl_info *gl_info, BOOL multisample)
1272 const struct wined3d_format *format = texture->resource.format;
1274 if (multisample)
1276 DWORD samples;
1278 if (texture->rb_multisample)
1279 return;
1281 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1282 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1283 * be mapped to GL_NV_framebuffer_multisample_coverage.
1285 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1286 * (EQAA), but it does not have an equivalent OpenGL extension. */
1288 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1289 * levels as the count of advertised multisample types for the texture
1290 * format. */
1291 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1293 unsigned int i, count = 0;
1295 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1297 if (format->multisample_types & 1u << i)
1299 if (texture->resource.multisample_quality == count++)
1300 break;
1303 samples = i + 1;
1305 else
1307 samples = texture->resource.multisample_type;
1310 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1311 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1312 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1313 format->glInternal, texture->resource.width, texture->resource.height);
1314 checkGLcall("glRenderbufferStorageMultisample()");
1315 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1317 else
1319 if (texture->rb_resolved)
1320 return;
1322 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1323 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1324 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1325 texture->resource.width, texture->resource.height);
1326 checkGLcall("glRenderbufferStorage()");
1327 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1331 /* Context activation is done by the caller. Context may be NULL in
1332 * WINED3D_NO3D mode. */
1333 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1334 struct wined3d_context *context, DWORD location)
1336 switch (location)
1338 case WINED3D_LOCATION_SYSMEM:
1339 if (texture->resource.heap_memory)
1340 return TRUE;
1342 if (!wined3d_resource_allocate_sysmem(&texture->resource))
1344 ERR("Failed to allocate system memory.\n");
1345 return FALSE;
1347 return TRUE;
1349 case WINED3D_LOCATION_USER_MEMORY:
1350 if (!texture->user_memory)
1351 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1352 return TRUE;
1354 case WINED3D_LOCATION_BUFFER:
1355 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1356 return TRUE;
1358 case WINED3D_LOCATION_TEXTURE_RGB:
1359 wined3d_texture_prepare_texture(texture, context, FALSE);
1360 return TRUE;
1362 case WINED3D_LOCATION_TEXTURE_SRGB:
1363 wined3d_texture_prepare_texture(texture, context, TRUE);
1364 return TRUE;
1366 case WINED3D_LOCATION_DRAWABLE:
1367 if (!texture->swapchain)
1368 ERR("Texture %p does not have a drawable.\n", texture);
1369 return TRUE;
1371 case WINED3D_LOCATION_RB_MULTISAMPLE:
1372 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1373 return TRUE;
1375 case WINED3D_LOCATION_RB_RESOLVED:
1376 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1377 return TRUE;
1379 default:
1380 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1381 return FALSE;
1385 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1387 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1388 FIXME("texture %p stub!\n", texture);
1391 struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1392 unsigned int sub_resource_idx)
1394 UINT sub_count = texture->level_count * texture->layer_count;
1396 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1398 if (sub_resource_idx >= sub_count)
1400 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1401 return NULL;
1404 return &texture->sub_resources[sub_resource_idx];
1407 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1408 UINT layer, const struct wined3d_box *dirty_region)
1410 struct wined3d_context *context;
1411 unsigned int sub_resource_idx;
1413 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1415 if (layer >= texture->layer_count)
1417 WARN("Invalid layer %u specified.\n", layer);
1418 return WINED3DERR_INVALIDCALL;
1420 sub_resource_idx = layer * texture->level_count;
1422 if (dirty_region)
1423 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1425 context = context_acquire(texture->resource.device, NULL);
1426 if (!wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding))
1428 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1429 context_release(context);
1430 return E_OUTOFMEMORY;
1432 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1433 context_release(context);
1435 return WINED3D_OK;
1438 void wined3d_texture_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1439 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1440 unsigned int row_pitch, unsigned int slice_pitch)
1442 texture->texture_ops->texture_upload_data(texture, sub_resource_idx,
1443 context, data, row_pitch, slice_pitch);
1446 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1447 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
1448 unsigned int row_pitch, unsigned int slice_pitch)
1450 static const POINT dst_point = {0, 0};
1451 unsigned int texture_level;
1452 RECT src_rect;
1454 texture_level = sub_resource_idx % texture->level_count;
1455 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(texture, texture_level),
1456 wined3d_texture_get_level_height(texture, texture_level));
1458 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1459 texture->resource.format, &src_rect, row_pitch, &dst_point, FALSE, data);
1462 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1463 struct wined3d_context *context, DWORD location)
1465 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1468 /* Context activation is done by the caller. */
1469 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1471 const struct wined3d_format *format = texture->resource.format;
1472 const struct wined3d_gl_info *gl_info = context->gl_info;
1473 const struct wined3d_color_key_conversion *conversion;
1474 GLenum internal;
1476 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1478 if (format->convert)
1480 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1482 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1484 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1485 format = wined3d_get_format(gl_info, conversion->dst_format);
1486 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1489 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1491 if (srgb)
1492 internal = format->glGammaInternal;
1493 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1494 && wined3d_resource_is_offscreen(&texture->resource))
1495 internal = format->rtInternal;
1496 else
1497 internal = format->glInternal;
1499 if (!internal)
1500 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1502 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1504 if (wined3d_texture_use_immutable_storage(texture, gl_info))
1505 wined3d_texture_allocate_gl_immutable_storage(texture, internal, gl_info);
1506 else
1507 wined3d_texture_allocate_gl_mutable_storage(texture, internal, format, gl_info);
1510 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1512 unsigned int sub_count = texture->level_count * texture->layer_count;
1513 struct wined3d_device *device = texture->resource.device;
1514 struct wined3d_texture_sub_resource *sub_resource;
1515 struct wined3d_renderbuffer_entry *entry, *entry2;
1516 const struct wined3d_gl_info *gl_info = NULL;
1517 struct wined3d_context *context = NULL;
1518 struct wined3d_surface *overlay, *cur;
1519 struct wined3d_surface *surface;
1520 unsigned int i;
1522 for (i = 0; i < sub_count; ++i)
1524 sub_resource = &texture->sub_resources[i];
1525 if (!(surface = sub_resource->u.surface))
1526 continue;
1528 TRACE("surface %p.\n", surface);
1530 if (!context && !list_empty(&surface->renderbuffers))
1532 context = context_acquire(device, NULL);
1533 gl_info = context->gl_info;
1536 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1538 TRACE("Deleting renderbuffer %u.\n", entry->id);
1539 context_gl_resource_released(device, entry->id, TRUE);
1540 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1541 HeapFree(GetProcessHeap(), 0, entry);
1544 if (surface->dc)
1545 wined3d_surface_destroy_dc(surface);
1547 if (surface->overlay_dest)
1548 list_remove(&surface->overlay_entry);
1550 LIST_FOR_EACH_ENTRY_SAFE(overlay, cur, &surface->overlays, struct wined3d_surface, overlay_entry)
1552 list_remove(&overlay->overlay_entry);
1553 overlay->overlay_dest = NULL;
1556 if (context)
1557 context_release(context);
1558 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1561 static const struct wined3d_texture_ops texture2d_ops =
1563 texture2d_upload_data,
1564 texture2d_load_location,
1565 texture2d_prepare_texture,
1566 texture2d_cleanup_sub_resources,
1569 struct wined3d_texture * __cdecl wined3d_texture_from_resource(struct wined3d_resource *resource)
1571 return texture_from_resource(resource);
1574 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1576 return wined3d_texture_incref(texture_from_resource(resource));
1579 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1581 return wined3d_texture_decref(texture_from_resource(resource));
1584 static void texture_resource_preload(struct wined3d_resource *resource)
1586 struct wined3d_texture *texture = texture_from_resource(resource);
1587 struct wined3d_context *context;
1589 context = context_acquire(resource->device, NULL);
1590 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
1591 context_release(context);
1594 static void wined3d_texture_unload(struct wined3d_resource *resource)
1596 struct wined3d_texture *texture = texture_from_resource(resource);
1597 UINT sub_count = texture->level_count * texture->layer_count;
1598 struct wined3d_device *device = resource->device;
1599 const struct wined3d_gl_info *gl_info;
1600 struct wined3d_context *context;
1601 UINT i;
1603 TRACE("texture %p.\n", texture);
1605 context = context_acquire(device, NULL);
1606 gl_info = context->gl_info;
1608 for (i = 0; i < sub_count; ++i)
1610 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1612 if (resource->pool != WINED3D_POOL_DEFAULT
1613 && wined3d_texture_load_location(texture, i, context, resource->map_binding))
1615 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1617 else
1619 /* We should only get here on device reset/teardown for implicit
1620 * resources. */
1621 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1622 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1623 resource, i, debug_d3dpool(resource->pool));
1624 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1625 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1628 if (sub_resource->buffer_object)
1629 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1631 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1633 struct wined3d_surface *surface = sub_resource->u.surface;
1634 struct wined3d_renderbuffer_entry *entry, *entry2;
1636 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1638 context_gl_resource_released(device, entry->id, TRUE);
1639 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1640 list_remove(&entry->entry);
1641 HeapFree(GetProcessHeap(), 0, entry);
1643 list_init(&surface->renderbuffers);
1644 surface->current_renderbuffer = NULL;
1648 context_release(context);
1650 wined3d_texture_force_reload(texture);
1651 wined3d_texture_unload_gl_texture(texture);
1654 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1655 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1657 const struct wined3d_format *format = resource->format;
1658 struct wined3d_texture_sub_resource *sub_resource;
1659 struct wined3d_device *device = resource->device;
1660 unsigned int fmt_flags = resource->format_flags;
1661 const struct wined3d_gl_info *gl_info = NULL;
1662 struct wined3d_context *context = NULL;
1663 struct wined3d_texture *texture;
1664 struct wined3d_bo_address data;
1665 unsigned int texture_level;
1666 BYTE *base_memory;
1667 BOOL ret;
1669 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1670 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1672 texture = texture_from_resource(resource);
1673 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1674 return E_INVALIDARG;
1676 texture_level = sub_resource_idx % texture->level_count;
1677 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1679 WARN("Map box is invalid.\n");
1680 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1681 return WINED3DERR_INVALIDCALL;
1684 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1685 && !wined3d_texture_check_block_align(texture, texture_level, box))
1687 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1688 debug_box(box), format->block_width, format->block_height);
1689 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1690 return WINED3DERR_INVALIDCALL;
1693 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1695 WARN("Trying to map unmappable texture.\n");
1696 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1697 return WINED3DERR_INVALIDCALL;
1700 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1702 WARN("DC is in use.\n");
1703 return WINED3DERR_INVALIDCALL;
1706 if (sub_resource->map_count)
1708 WARN("Sub-resource is already mapped.\n");
1709 return WINED3DERR_INVALIDCALL;
1712 if (device->d3d_initialized)
1714 context = context_acquire(device, NULL);
1715 gl_info = context->gl_info;
1718 if (flags & WINED3D_MAP_DISCARD)
1720 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1721 wined3d_debug_location(texture->resource.map_binding));
1722 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1723 context, texture->resource.map_binding)))
1724 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1726 else
1728 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1729 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1730 ret = wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
1733 if (!ret)
1735 ERR("Failed to prepare location.\n");
1736 context_release(context);
1737 return E_OUTOFMEMORY;
1740 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1741 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1743 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1744 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->size,
1745 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1746 TRACE("Base memory pointer %p.\n", base_memory);
1748 if (context)
1749 context_release(context);
1751 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1753 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1754 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1756 else
1758 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1761 if (!box)
1763 map_desc->data = base_memory;
1765 else
1767 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1769 /* Compressed textures are block based, so calculate the offset of
1770 * the block that contains the top-left pixel of the mapped box. */
1771 map_desc->data = base_memory
1772 + (box->front * map_desc->slice_pitch)
1773 + ((box->top / format->block_height) * map_desc->row_pitch)
1774 + ((box->left / format->block_width) * format->block_byte_count);
1776 else
1778 map_desc->data = base_memory
1779 + (box->front * map_desc->slice_pitch)
1780 + (box->top * map_desc->row_pitch)
1781 + (box->left * format->byte_count);
1785 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1787 RECT *r = &texture->swapchain->front_buffer_update;
1789 if (!box)
1790 SetRect(r, 0, 0, resource->width, resource->height);
1791 else
1792 SetRect(r, box->left, box->top, box->right, box->bottom);
1793 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1796 ++resource->map_count;
1797 ++sub_resource->map_count;
1799 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1800 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1802 return WINED3D_OK;
1805 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1807 struct wined3d_texture_sub_resource *sub_resource;
1808 struct wined3d_device *device = resource->device;
1809 const struct wined3d_gl_info *gl_info = NULL;
1810 struct wined3d_context *context = NULL;
1811 struct wined3d_texture *texture;
1812 struct wined3d_bo_address data;
1814 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1816 texture = texture_from_resource(resource);
1817 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1818 return E_INVALIDARG;
1820 if (!sub_resource->map_count)
1822 WARN("Trying to unmap unmapped sub-resource.\n");
1823 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1824 return WINED3D_OK;
1825 return WINEDDERR_NOTLOCKED;
1828 if (device->d3d_initialized)
1830 context = context_acquire(device, NULL);
1831 gl_info = context->gl_info;
1834 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1835 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1837 if (context)
1838 context_release(context);
1840 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1842 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1843 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1845 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1847 FIXME("Depth / stencil buffer locking is not implemented.\n");
1850 --sub_resource->map_count;
1851 if (!--resource->map_count && texture->update_map_binding)
1852 wined3d_texture_update_map_binding(texture);
1854 return WINED3D_OK;
1857 static const struct wined3d_resource_ops texture_resource_ops =
1859 texture_resource_incref,
1860 texture_resource_decref,
1861 texture_resource_preload,
1862 wined3d_texture_unload,
1863 texture_resource_sub_resource_map,
1864 texture_resource_sub_resource_unmap,
1867 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1868 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1869 void *parent, const struct wined3d_parent_ops *parent_ops)
1871 struct wined3d_device_parent *device_parent = device->device_parent;
1872 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1873 struct wined3d_surface *surfaces;
1874 UINT pow2_width, pow2_height;
1875 unsigned int i, j;
1876 HRESULT hr;
1878 if (!(desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count > 1
1879 && !gl_info->supported[EXT_TEXTURE_ARRAY])
1881 WARN("OpenGL implementation does not support array textures.\n");
1882 return WINED3DERR_INVALIDCALL;
1885 /* TODO: It should only be possible to create textures for formats
1886 * that are reported as supported. */
1887 if (WINED3DFMT_UNKNOWN >= desc->format)
1889 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1890 return WINED3DERR_INVALIDCALL;
1893 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1894 FIXME("Trying to create a managed texture with dynamic usage.\n");
1895 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1896 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1897 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1898 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1899 FIXME("Trying to create a render target that isn't in the default pool.\n");
1901 pow2_width = desc->width;
1902 pow2_height = desc->height;
1903 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1904 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1906 /* level_count == 0 returns an error as well. */
1907 if (level_count != 1 || layer_count != 1)
1909 if (desc->pool != WINED3D_POOL_SCRATCH)
1911 WARN("Attempted to create a mipmapped/cube/array NPOT texture without unconditional NPOT support.\n");
1912 return WINED3DERR_INVALIDCALL;
1915 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
1917 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1919 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1921 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1923 /* TODO: Add support for non-power-of-two compressed textures. */
1924 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1925 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1927 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1928 desc->width, desc->height);
1929 return WINED3DERR_NOTAVAILABLE;
1932 /* Find the nearest pow2 match. */
1933 pow2_width = pow2_height = 1;
1934 while (pow2_width < desc->width)
1935 pow2_width <<= 1;
1936 while (pow2_height < desc->height)
1937 pow2_height <<= 1;
1938 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1941 texture->pow2_width = pow2_width;
1942 texture->pow2_height = pow2_height;
1944 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1945 && (desc->usage & WINED3DUSAGE_TEXTURE))
1947 /* One of four options:
1948 * 1: Do the same as we do with NPOT and scale the texture. (Any
1949 * texture ops would require the texture to be scaled which is
1950 * potentially slow.)
1951 * 2: Set the texture to the maximum size (bad idea).
1952 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1953 * 4: Create the surface, but allow it to be used only for DirectDraw
1954 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1955 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1956 * the render target. */
1957 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1959 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1960 return WINED3DERR_NOTAVAILABLE;
1963 /* We should never use this surface in combination with OpenGL. */
1964 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1967 /* Calculate levels for mip mapping. */
1968 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1970 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1972 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1973 return WINED3DERR_INVALIDCALL;
1976 if (level_count != 1)
1978 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1979 return WINED3DERR_INVALIDCALL;
1983 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1984 flags, device, parent, parent_ops, &texture_resource_ops)))
1986 WARN("Failed to initialize texture, returning %#x.\n", hr);
1987 return hr;
1990 /* Precalculated scaling for 'faked' non power of two texture coords. */
1991 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1993 texture->pow2_matrix[0] = (float)desc->width;
1994 texture->pow2_matrix[5] = (float)desc->height;
1995 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1996 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1998 else
2000 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
2002 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
2003 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
2004 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
2006 else
2008 texture->pow2_matrix[0] = 1.0f;
2009 texture->pow2_matrix[5] = 1.0f;
2011 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
2012 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
2013 else if (layer_count > 1)
2014 texture->target = GL_TEXTURE_2D_ARRAY;
2015 else
2016 texture->target = GL_TEXTURE_2D;
2018 texture->pow2_matrix[10] = 1.0f;
2019 texture->pow2_matrix[15] = 1.0f;
2020 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
2022 if (wined3d_texture_use_pbo(texture, gl_info))
2023 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2025 if (level_count > ~(SIZE_T)0 / layer_count
2026 || !(surfaces = wined3d_calloc(level_count * layer_count, sizeof(*surfaces))))
2028 wined3d_texture_cleanup_sync(texture);
2029 return E_OUTOFMEMORY;
2032 /* Generate all the surfaces. */
2033 for (i = 0; i < texture->level_count; ++i)
2035 for (j = 0; j < texture->layer_count; ++j)
2037 static const GLenum cube_targets[6] =
2039 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
2040 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
2041 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
2042 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
2043 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
2044 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
2046 struct wined3d_texture_sub_resource *sub_resource;
2047 unsigned int idx = j * texture->level_count + i;
2048 struct wined3d_surface *surface;
2050 surface = &surfaces[idx];
2051 surface->container = texture;
2052 surface->texture_target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
2053 surface->texture_level = i;
2054 surface->texture_layer = j;
2055 list_init(&surface->renderbuffers);
2056 list_init(&surface->overlays);
2058 sub_resource = &texture->sub_resources[idx];
2059 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2060 sub_resource->u.surface = surface;
2061 if (!(texture->resource.usage & WINED3DUSAGE_DEPTHSTENCIL))
2063 wined3d_texture_validate_location(texture, idx, WINED3D_LOCATION_SYSMEM);
2064 wined3d_texture_invalidate_location(texture, idx, ~WINED3D_LOCATION_SYSMEM);
2067 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
2068 texture, idx, &sub_resource->parent, &sub_resource->parent_ops)))
2070 WARN("Failed to create surface parent, hr %#x.\n", hr);
2071 sub_resource->parent = NULL;
2072 wined3d_texture_cleanup_sync(texture);
2073 return hr;
2076 TRACE("parent %p, parent_ops %p.\n", sub_resource->parent, sub_resource->parent_ops);
2078 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
2080 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
2081 && FAILED(hr = wined3d_surface_create_dc(surface)))
2083 wined3d_texture_cleanup_sync(texture);
2084 return hr;
2089 return WINED3D_OK;
2092 /* This call just uploads data, the caller is responsible for binding the
2093 * correct texture. */
2094 /* Context activation is done by the caller. */
2095 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2096 const struct wined3d_context *context, const struct wined3d_const_bo_address *data,
2097 unsigned int row_pitch, unsigned int slice_pitch)
2099 const struct wined3d_format *format = texture->resource.format;
2100 unsigned int level = sub_resource_idx % texture->level_count;
2101 const struct wined3d_gl_info *gl_info = context->gl_info;
2102 unsigned int dst_row_pitch, dst_slice_pitch;
2103 unsigned int width, height, depth;
2104 const void *mem = data->addr;
2105 void *converted_mem = NULL;
2107 TRACE("texture %p, sub_resource_idx %u, context %p, data {%#x:%p}, row_pitch %#x, slice_pitch %#x.\n",
2108 texture, sub_resource_idx, context, data->buffer_object, data->addr, row_pitch, slice_pitch);
2110 width = wined3d_texture_get_level_width(texture, level);
2111 height = wined3d_texture_get_level_height(texture, level);
2112 depth = wined3d_texture_get_level_depth(texture, level);
2114 if (format->convert)
2116 if (data->buffer_object)
2117 ERR("Loading a converted texture from a PBO.\n");
2118 if (texture->resource.format_flags & WINED3DFMT_FLAG_BLOCKS)
2119 ERR("Converting a block-based format.\n");
2121 dst_row_pitch = width * format->conv_byte_count;
2122 dst_slice_pitch = dst_row_pitch * height;
2124 converted_mem = wined3d_calloc(depth, dst_slice_pitch);
2125 format->convert(data->addr, converted_mem, row_pitch, slice_pitch,
2126 dst_row_pitch, dst_slice_pitch, width, height, depth);
2127 mem = converted_mem;
2129 else
2131 wined3d_texture_get_pitch(texture, sub_resource_idx, &dst_row_pitch, &dst_slice_pitch);
2132 if (row_pitch != dst_row_pitch || slice_pitch != dst_slice_pitch)
2133 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch, slice_pitch);
2136 if (data->buffer_object)
2138 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, data->buffer_object));
2139 checkGLcall("glBindBuffer");
2142 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D, level, 0, 0, 0,
2143 width, height, depth, format->glFormat, format->glType, mem));
2144 checkGLcall("glTexSubImage3D");
2146 if (data->buffer_object)
2148 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
2149 checkGLcall("glBindBuffer");
2152 HeapFree(GetProcessHeap(), 0, converted_mem);
2155 /* Context activation is done by the caller. */
2156 static void texture3d_download_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2157 const struct wined3d_context *context, const struct wined3d_bo_address *data)
2159 const struct wined3d_format *format = texture->resource.format;
2160 const struct wined3d_gl_info *gl_info = context->gl_info;
2162 if (format->convert)
2164 FIXME("Attempting to download a converted volume, format %s.\n",
2165 debug_d3dformat(format->id));
2166 return;
2169 if (data->buffer_object)
2171 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, data->buffer_object));
2172 checkGLcall("glBindBuffer");
2175 gl_info->gl_ops.gl.p_glGetTexImage(GL_TEXTURE_3D, sub_resource_idx,
2176 format->glFormat, format->glType, data->addr);
2177 checkGLcall("glGetTexImage");
2179 if (data->buffer_object)
2181 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER, 0));
2182 checkGLcall("glBindBuffer");
2187 /* Context activation is done by the caller. */
2188 static void texture3d_srgb_transfer(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2189 struct wined3d_context *context, BOOL dest_is_srgb)
2191 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2192 unsigned int row_pitch, slice_pitch;
2193 struct wined3d_bo_address data;
2195 /* Optimisations are possible, but the effort should be put into either
2196 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2197 * picked the wrong copy for the original upload and fixing that.
2199 * Also keep in mind that we want to avoid using resource.heap_memory
2200 * for DEFAULT pool surfaces. */
2201 WARN_(d3d_perf)("Performing slow rgb/srgb volume transfer.\n");
2202 data.buffer_object = 0;
2203 if (!(data.addr = HeapAlloc(GetProcessHeap(), 0, sub_resource->size)))
2204 return;
2206 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2207 wined3d_texture_bind_and_dirtify(texture, context, !dest_is_srgb);
2208 texture3d_download_data(texture, sub_resource_idx, context, &data);
2209 wined3d_texture_bind_and_dirtify(texture, context, dest_is_srgb);
2210 texture3d_upload_data(texture, sub_resource_idx, context,
2211 wined3d_const_bo_address(&data), row_pitch, slice_pitch);
2213 HeapFree(GetProcessHeap(), 0, data.addr);
2216 /* Context activation is done by the caller. */
2217 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2218 struct wined3d_context *context, DWORD location)
2220 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[sub_resource_idx];
2221 DWORD required_access = wined3d_resource_access_from_location(location);
2222 unsigned int row_pitch, slice_pitch;
2224 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
2225 texture, sub_resource_idx, context, wined3d_debug_location(location));
2227 TRACE("Current resource location %s.\n", wined3d_debug_location(sub_resource->locations));
2229 if ((sub_resource->locations & location) == location)
2231 TRACE("Location(s) already up to date.\n");
2232 return TRUE;
2235 if ((texture->resource.access_flags & required_access) != required_access)
2237 ERR("Operation requires %#x access, but volume only has %#x.\n",
2238 required_access, texture->resource.access_flags);
2239 return FALSE;
2242 if (!wined3d_texture_prepare_location(texture, sub_resource_idx, context, location))
2243 return FALSE;
2245 if (sub_resource->locations & WINED3D_LOCATION_DISCARDED)
2247 TRACE("Volume previously discarded, nothing to do.\n");
2248 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2249 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_DISCARDED);
2250 goto done;
2253 switch (location)
2255 case WINED3D_LOCATION_TEXTURE_RGB:
2256 case WINED3D_LOCATION_TEXTURE_SRGB:
2257 if (sub_resource->locations & WINED3D_LOCATION_SYSMEM)
2259 struct wined3d_const_bo_address data = {0, texture->resource.heap_memory};
2260 data.addr += sub_resource->offset;
2261 wined3d_texture_bind_and_dirtify(texture, context,
2262 location == WINED3D_LOCATION_TEXTURE_SRGB);
2263 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2264 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2266 else if (sub_resource->locations & WINED3D_LOCATION_BUFFER)
2268 struct wined3d_const_bo_address data = {sub_resource->buffer_object, NULL};
2269 wined3d_texture_bind_and_dirtify(texture, context,
2270 location == WINED3D_LOCATION_TEXTURE_SRGB);
2271 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
2272 texture3d_upload_data(texture, sub_resource_idx, context, &data, row_pitch, slice_pitch);
2274 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2276 texture3d_srgb_transfer(texture, sub_resource_idx, context, TRUE);
2278 else if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_SRGB)
2280 texture3d_srgb_transfer(texture, sub_resource_idx, context, FALSE);
2282 else
2284 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource->locations));
2285 return FALSE;
2287 break;
2289 case WINED3D_LOCATION_SYSMEM:
2290 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2292 struct wined3d_bo_address data = {0, texture->resource.heap_memory};
2294 data.addr += sub_resource->offset;
2295 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2296 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2297 else
2298 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2300 texture3d_download_data(texture, sub_resource_idx, context, &data);
2301 ++texture->download_count;
2303 else
2305 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2306 wined3d_debug_location(sub_resource->locations));
2307 return FALSE;
2309 break;
2311 case WINED3D_LOCATION_BUFFER:
2312 if (sub_resource->locations & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
2314 struct wined3d_bo_address data = {sub_resource->buffer_object, NULL};
2316 if (sub_resource->locations & WINED3D_LOCATION_TEXTURE_RGB)
2317 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
2318 else
2319 wined3d_texture_bind_and_dirtify(texture, context, TRUE);
2321 texture3d_download_data(texture, sub_resource_idx, context, &data);
2323 else
2325 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2326 wined3d_debug_location(sub_resource->locations));
2327 return FALSE;
2329 break;
2331 default:
2332 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location),
2333 wined3d_debug_location(sub_resource->locations));
2334 return FALSE;
2337 done:
2338 wined3d_texture_validate_location(texture, sub_resource_idx, location);
2340 return TRUE;
2343 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
2345 const struct wined3d_format *format = texture->resource.format;
2346 GLenum internal = srgb ? format->glGammaInternal : format->glInternal;
2347 unsigned int sub_count = texture->level_count * texture->layer_count;
2348 const struct wined3d_gl_info *gl_info = context->gl_info;
2349 unsigned int i;
2351 wined3d_texture_bind_and_dirtify(texture, context, srgb);
2353 if (wined3d_texture_use_immutable_storage(texture, gl_info))
2355 GL_EXTCALL(glTexStorage3D(GL_TEXTURE_3D, texture->level_count, internal,
2356 wined3d_texture_get_level_width(texture, 0),
2357 wined3d_texture_get_level_height(texture, 0),
2358 wined3d_texture_get_level_depth(texture, 0)));
2359 checkGLcall("glTexStorage3D");
2361 else
2363 for (i = 0; i < sub_count; ++i)
2365 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, i, internal,
2366 wined3d_texture_get_level_width(texture, i),
2367 wined3d_texture_get_level_height(texture, i),
2368 wined3d_texture_get_level_depth(texture, i),
2369 0, format->glFormat, format->glType, NULL));
2370 checkGLcall("glTexImage3D");
2375 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
2379 static const struct wined3d_texture_ops texture3d_ops =
2381 texture3d_upload_data,
2382 texture3d_load_location,
2383 texture3d_prepare_texture,
2384 texture3d_cleanup_sub_resources,
2387 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
2388 unsigned int level, const struct wined3d_box *box)
2390 const struct wined3d_format *format = texture->resource.format;
2391 unsigned int height = wined3d_texture_get_level_height(texture, level);
2392 unsigned int width = wined3d_texture_get_level_width(texture, level);
2393 unsigned int width_mask, height_mask;
2395 if ((box->left >= box->right)
2396 || (box->top >= box->bottom)
2397 || (box->right > width)
2398 || (box->bottom > height))
2399 return FALSE;
2401 /* This assumes power of two block sizes, but NPOT block sizes would be
2402 * silly anyway.
2404 * This also assumes that the format's block depth is 1. */
2405 width_mask = format->block_width - 1;
2406 height_mask = format->block_height - 1;
2408 if ((box->left & width_mask) || (box->top & height_mask)
2409 || (box->right & width_mask && box->right != width)
2410 || (box->bottom & height_mask && box->bottom != height))
2411 return FALSE;
2413 return TRUE;
2416 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
2417 UINT layer_count, UINT level_count, struct wined3d_device *device, void *parent,
2418 const struct wined3d_parent_ops *parent_ops)
2420 struct wined3d_device_parent *device_parent = device->device_parent;
2421 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2422 unsigned int i;
2423 HRESULT hr;
2425 if (layer_count != 1)
2427 ERR("Invalid layer count for volume texture.\n");
2428 return E_INVALIDARG;
2431 /* TODO: It should only be possible to create textures for formats
2432 * that are reported as supported. */
2433 if (WINED3DFMT_UNKNOWN >= desc->format)
2435 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2436 return WINED3DERR_INVALIDCALL;
2439 if (!gl_info->supported[EXT_TEXTURE3D])
2441 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2442 return WINED3DERR_INVALIDCALL;
2445 /* Calculate levels for mip mapping. */
2446 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2448 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2450 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2451 return WINED3DERR_INVALIDCALL;
2454 if (level_count != 1)
2456 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2457 return WINED3DERR_INVALIDCALL;
2461 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2462 || desc->pool == WINED3D_POOL_SCRATCH))
2464 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2465 return WINED3DERR_INVALIDCALL;
2468 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2470 UINT pow2_w, pow2_h, pow2_d;
2471 pow2_w = 1;
2472 while (pow2_w < desc->width)
2473 pow2_w <<= 1;
2474 pow2_h = 1;
2475 while (pow2_h < desc->height)
2476 pow2_h <<= 1;
2477 pow2_d = 1;
2478 while (pow2_d < desc->depth)
2479 pow2_d <<= 1;
2481 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2483 if (desc->pool == WINED3D_POOL_SCRATCH)
2485 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2487 else
2489 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2490 desc->width, desc->height, desc->depth);
2491 return WINED3DERR_INVALIDCALL;
2496 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, level_count, desc,
2497 0, device, parent, parent_ops, &texture_resource_ops)))
2499 WARN("Failed to initialize texture, returning %#x.\n", hr);
2500 return hr;
2503 texture->pow2_matrix[0] = 1.0f;
2504 texture->pow2_matrix[5] = 1.0f;
2505 texture->pow2_matrix[10] = 1.0f;
2506 texture->pow2_matrix[15] = 1.0f;
2507 texture->target = GL_TEXTURE_3D;
2509 if (wined3d_texture_use_pbo(texture, gl_info))
2511 wined3d_resource_free_sysmem(&texture->resource);
2512 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2515 /* Generate all the surfaces. */
2516 for (i = 0; i < texture->level_count; ++i)
2518 struct wined3d_texture_sub_resource *sub_resource;
2520 sub_resource = &texture->sub_resources[i];
2521 sub_resource->locations = WINED3D_LOCATION_DISCARDED;
2523 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2524 texture, i, &sub_resource->parent, &sub_resource->parent_ops)))
2526 WARN("Failed to create volume parent, hr %#x.\n", hr);
2527 sub_resource->parent = NULL;
2528 wined3d_texture_cleanup_sync(texture);
2529 return hr;
2532 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2534 TRACE("Created volume level %u.\n", i);
2537 return WINED3D_OK;
2540 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2541 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2542 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2544 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2546 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2547 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2548 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2549 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2551 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2552 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2553 return WINED3DERR_INVALIDCALL;
2555 if (src_texture)
2557 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2558 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2559 return WINED3DERR_INVALIDCALL;
2562 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2563 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2566 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2567 unsigned int sub_resource_idx, LONG *x, LONG *y)
2569 struct wined3d_surface *surface;
2571 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2573 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2574 || sub_resource_idx >= texture->level_count * texture->layer_count)
2576 WARN("Invalid sub-resource specified.\n");
2577 return WINEDDERR_NOTAOVERLAYSURFACE;
2580 surface = texture->sub_resources[sub_resource_idx].u.surface;
2581 if (!surface->overlay_dest)
2583 TRACE("Overlay not visible.\n");
2584 *x = 0;
2585 *y = 0;
2586 return WINEDDERR_OVERLAYNOTVISIBLE;
2589 *x = surface->overlay_destrect.left;
2590 *y = surface->overlay_destrect.top;
2592 TRACE("Returning position %d, %d.\n", *x, *y);
2594 return WINED3D_OK;
2597 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2598 unsigned int sub_resource_idx, LONG x, LONG y)
2600 struct wined3d_texture_sub_resource *sub_resource;
2601 struct wined3d_surface *surface;
2602 LONG w, h;
2604 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2606 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2607 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2609 WARN("Invalid sub-resource specified.\n");
2610 return WINEDDERR_NOTAOVERLAYSURFACE;
2613 surface = sub_resource->u.surface;
2614 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2615 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2616 SetRect(&surface->overlay_destrect, x, y, x + w, y + h);
2618 return WINED3D_OK;
2621 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2622 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2623 const RECT *dst_rect, DWORD flags)
2625 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2626 struct wined3d_surface *surface, *dst_surface;
2628 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2629 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2630 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2631 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2633 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2634 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2636 WARN("Invalid sub-resource specified.\n");
2637 return WINEDDERR_NOTAOVERLAYSURFACE;
2640 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2641 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2643 WARN("Invalid destination sub-resource specified.\n");
2644 return WINED3DERR_INVALIDCALL;
2647 surface = sub_resource->u.surface;
2648 if (src_rect)
2649 surface->overlay_srcrect = *src_rect;
2650 else
2651 SetRect(&surface->overlay_srcrect, 0, 0,
2652 wined3d_texture_get_level_width(texture, surface->texture_level),
2653 wined3d_texture_get_level_height(texture, surface->texture_level));
2655 dst_surface = dst_sub_resource->u.surface;
2656 if (dst_rect)
2657 surface->overlay_destrect = *dst_rect;
2658 else
2659 SetRect(&surface->overlay_destrect, 0, 0,
2660 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2661 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2663 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2665 surface->overlay_dest = NULL;
2666 list_remove(&surface->overlay_entry);
2669 if (flags & WINEDDOVER_SHOW)
2671 if (surface->overlay_dest != dst_surface)
2673 surface->overlay_dest = dst_surface;
2674 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2677 else if (flags & WINEDDOVER_HIDE)
2679 /* Tests show that the rectangles are erased on hide. */
2680 SetRectEmpty(&surface->overlay_srcrect);
2681 SetRectEmpty(&surface->overlay_destrect);
2682 surface->overlay_dest = NULL;
2685 return WINED3D_OK;
2688 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2690 unsigned int sub_count = texture->level_count * texture->layer_count;
2692 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2694 if (sub_resource_idx >= sub_count)
2696 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2697 return NULL;
2700 return texture->sub_resources[sub_resource_idx].parent;
2703 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2704 unsigned int sub_resource_idx, void *parent)
2706 unsigned int sub_count = texture->level_count * texture->layer_count;
2708 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2710 if (sub_resource_idx >= sub_count)
2712 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2713 return;
2716 texture->sub_resources[sub_resource_idx].parent = parent;
2719 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2720 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2722 unsigned int sub_count = texture->level_count * texture->layer_count;
2723 const struct wined3d_resource *resource;
2724 unsigned int level_idx;
2726 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2728 if (sub_resource_idx >= sub_count)
2730 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2731 return WINED3DERR_INVALIDCALL;
2734 resource = &texture->resource;
2735 desc->format = resource->format->id;
2736 desc->multisample_type = resource->multisample_type;
2737 desc->multisample_quality = resource->multisample_quality;
2738 desc->usage = resource->usage;
2739 desc->pool = resource->pool;
2741 level_idx = sub_resource_idx % texture->level_count;
2742 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2743 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2744 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2745 desc->size = texture->sub_resources[sub_resource_idx].size;
2747 return WINED3D_OK;
2750 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2751 UINT layer_count, UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data,
2752 void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2754 struct wined3d_texture *object;
2755 HRESULT hr;
2757 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
2758 "parent %p, parent_ops %p, texture %p.\n",
2759 device, desc, layer_count, level_count, flags, data, parent, parent_ops, texture);
2761 if (!layer_count)
2763 WARN("Invalid layer count.\n");
2764 return E_INVALIDARG;
2766 if ((desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP) && layer_count != 6)
2768 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count);
2769 layer_count = 6;
2772 if (!level_count)
2774 WARN("Invalid level count.\n");
2775 return WINED3DERR_INVALIDCALL;
2778 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2780 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2782 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2783 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2785 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2786 desc->multisample_quality);
2787 return WINED3DERR_NOTAVAILABLE;
2789 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2790 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2791 || desc->multisample_quality))
2793 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2794 desc->multisample_quality);
2795 return WINED3DERR_NOTAVAILABLE;
2799 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2800 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2801 return E_OUTOFMEMORY;
2803 switch (desc->resource_type)
2805 case WINED3D_RTYPE_TEXTURE_2D:
2806 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2807 break;
2809 case WINED3D_RTYPE_TEXTURE_3D:
2810 hr = volumetexture_init(object, desc, layer_count, level_count, device, parent, parent_ops);
2811 break;
2813 default:
2814 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2815 hr = WINED3DERR_INVALIDCALL;
2816 break;
2819 if (FAILED(hr))
2821 WARN("Failed to initialize texture, returning %#x.\n", hr);
2822 HeapFree(GetProcessHeap(), 0, object);
2823 return hr;
2826 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2827 * in this case. */
2828 if (data)
2830 unsigned int sub_count = level_count * layer_count;
2831 unsigned int i;
2833 for (i = 0; i < sub_count; ++i)
2835 if (!data[i].data)
2837 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
2838 wined3d_texture_cleanup_sync(object);
2839 HeapFree(GetProcessHeap(), 0, object);
2840 return E_INVALIDARG;
2844 for (i = 0; i < sub_count; ++i)
2846 wined3d_device_update_sub_resource(device, &object->resource,
2847 i, NULL, data[i].data, data[i].row_pitch, data[i].slice_pitch);
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 (!(texture->flags & WINED3D_TEXTURE_GET_DC))
2869 WARN("Texture does not support GetDC\n");
2870 /* Don't touch the DC */
2871 return WINED3DERR_INVALIDCALL;
2874 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2875 return WINED3DERR_INVALIDCALL;
2877 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2879 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2880 return WINED3DERR_INVALIDCALL;
2883 surface = sub_resource->u.surface;
2885 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2886 return WINED3DERR_INVALIDCALL;
2888 if (device->d3d_initialized)
2889 context = context_acquire(device, NULL);
2891 wined3d_texture_load_location(texture, sub_resource_idx, context, texture->resource.map_binding);
2892 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2894 if (!surface->dc)
2895 hr = wined3d_surface_create_dc(surface);
2896 if (context)
2897 context_release(context);
2898 if (FAILED(hr))
2899 return WINED3DERR_INVALIDCALL;
2901 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2902 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2903 ++texture->resource.map_count;
2904 ++sub_resource->map_count;
2906 *dc = surface->dc;
2907 TRACE("Returning dc %p.\n", *dc);
2909 return hr;
2912 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2914 struct wined3d_device *device = texture->resource.device;
2915 struct wined3d_texture_sub_resource *sub_resource;
2916 struct wined3d_surface *surface;
2918 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2920 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2921 return WINED3DERR_INVALIDCALL;
2923 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2925 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2926 return WINED3DERR_INVALIDCALL;
2929 surface = sub_resource->u.surface;
2931 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2932 return WINED3DERR_INVALIDCALL;
2934 if (surface->dc != dc)
2936 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2937 return WINED3DERR_INVALIDCALL;
2940 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2941 wined3d_surface_destroy_dc(surface);
2943 --sub_resource->map_count;
2944 if (!--texture->resource.map_count && texture->update_map_binding)
2945 wined3d_texture_update_map_binding(texture);
2946 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2947 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2949 return WINED3D_OK;