wined3d: Use the texture dimensions in swapchain_blit().
[wine.git] / dlls / wined3d / texture.c
blobada571c757f04679f259db17d824e1111417392d
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 static BOOL wined3d_texture_use_pbo(const struct wined3d_texture *texture, const struct wined3d_gl_info *gl_info)
33 return texture->resource.pool == WINED3D_POOL_DEFAULT
34 && texture->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU
35 && gl_info->supported[ARB_PIXEL_BUFFER_OBJECT]
36 && !texture->resource.format->convert
37 && !(texture->flags & (WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_COND_NP2_EMULATED));
40 GLenum wined3d_texture_get_gl_buffer(const struct wined3d_texture *texture)
42 const struct wined3d_swapchain *swapchain = texture->swapchain;
44 TRACE("texture %p.\n", texture);
46 if (!swapchain)
48 ERR("Texture %p is not part of a swapchain.\n", texture);
49 return GL_NONE;
52 if (swapchain->back_buffers && swapchain->back_buffers[0] == texture)
54 if (swapchain->render_to_fbo)
56 TRACE("Returning GL_COLOR_ATTACHMENT0.\n");
57 return GL_COLOR_ATTACHMENT0;
59 TRACE("Returning GL_BACK.\n");
60 return GL_BACK;
62 else if (texture == swapchain->front_buffer)
64 TRACE("Returning GL_FRONT.\n");
65 return GL_FRONT;
68 FIXME("Higher back buffer, returning GL_BACK.\n");
69 return GL_BACK;
72 void wined3d_texture_validate_location(struct wined3d_texture *texture,
73 unsigned int sub_resource_idx, DWORD location)
75 struct wined3d_texture_sub_resource *sub_resource;
77 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
78 texture, sub_resource_idx, wined3d_debug_location(location));
80 sub_resource = &texture->sub_resources[sub_resource_idx];
81 sub_resource->locations |= location;
83 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
86 void wined3d_texture_invalidate_location(struct wined3d_texture *texture,
87 unsigned int sub_resource_idx, DWORD location)
89 struct wined3d_texture_sub_resource *sub_resource;
91 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
92 texture, sub_resource_idx, wined3d_debug_location(location));
94 if (location & (WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB))
95 wined3d_texture_set_dirty(texture);
97 sub_resource = &texture->sub_resources[sub_resource_idx];
98 sub_resource->locations &= ~location;
100 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource->locations));
102 if (!sub_resource->locations)
103 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
104 sub_resource_idx, texture);
107 /* Context activation is done by the caller. */
108 void *wined3d_texture_map_bo_address(const struct wined3d_bo_address *data, size_t size,
109 const struct wined3d_gl_info *gl_info, GLenum binding, DWORD flags)
111 BYTE *memory;
113 if (!data->buffer_object)
114 return data->addr;
116 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
118 if (gl_info->supported[ARB_MAP_BUFFER_RANGE])
120 GLbitfield map_flags = wined3d_resource_gl_map_flags(flags) & ~GL_MAP_FLUSH_EXPLICIT_BIT;
121 memory = GL_EXTCALL(glMapBufferRange(binding, (INT_PTR)data->addr, size, map_flags));
123 else
125 memory = GL_EXTCALL(glMapBuffer(binding, wined3d_resource_gl_legacy_map_flags(flags)));
126 memory += (INT_PTR)data->addr;
129 GL_EXTCALL(glBindBuffer(binding, 0));
130 checkGLcall("Map buffer object");
132 return memory;
135 /* Context activation is done by the caller. */
136 void wined3d_texture_unmap_bo_address(const struct wined3d_bo_address *data,
137 const struct wined3d_gl_info *gl_info, GLenum binding)
139 if (!data->buffer_object)
140 return;
142 GL_EXTCALL(glBindBuffer(binding, data->buffer_object));
143 GL_EXTCALL(glUnmapBuffer(binding));
144 GL_EXTCALL(glBindBuffer(binding, 0));
145 checkGLcall("Unmap buffer object");
148 void wined3d_texture_get_memory(struct wined3d_texture *texture, unsigned int sub_resource_idx,
149 struct wined3d_bo_address *data, DWORD locations)
151 struct wined3d_texture_sub_resource *sub_resource;
153 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
154 texture, sub_resource_idx, data, wined3d_debug_location(locations));
156 sub_resource = &texture->sub_resources[sub_resource_idx];
157 if (locations & WINED3D_LOCATION_BUFFER)
159 data->addr = NULL;
160 data->buffer_object = sub_resource->buffer_object;
161 return;
163 if (locations & WINED3D_LOCATION_USER_MEMORY)
165 data->addr = texture->user_memory;
166 data->buffer_object = 0;
167 return;
169 if (locations & WINED3D_LOCATION_SYSMEM)
171 data->addr = sub_resource->resource->heap_memory;
172 data->buffer_object = 0;
173 return;
176 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations));
177 data->addr = NULL;
178 data->buffer_object = 0;
181 static HRESULT wined3d_texture_init(struct wined3d_texture *texture, const struct wined3d_texture_ops *texture_ops,
182 UINT layer_count, UINT level_count, const struct wined3d_resource_desc *desc, DWORD flags,
183 struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops,
184 const struct wined3d_resource_ops *resource_ops)
186 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
187 HRESULT hr;
189 TRACE("texture %p, texture_ops %p, layer_count %u, level_count %u, resource_type %s, format %s, "
190 "multisample_type %#x, multisample_quality %#x, usage %s, pool %s, width %u, height %u, depth %u, "
191 "flags %#x, device %p, parent %p, parent_ops %p, resource_ops %p.\n",
192 texture, texture_ops, layer_count, level_count, debug_d3dresourcetype(desc->resource_type),
193 debug_d3dformat(desc->format), desc->multisample_type, desc->multisample_quality,
194 debug_d3dusage(desc->usage), debug_d3dpool(desc->pool), desc->width, desc->height, desc->depth,
195 flags, device, parent, parent_ops, resource_ops);
197 if (FAILED(hr = resource_init(&texture->resource, device, desc->resource_type, format,
198 desc->multisample_type, desc->multisample_quality, desc->usage, desc->pool,
199 desc->width, desc->height, desc->depth, 0, parent, parent_ops, resource_ops)))
201 static unsigned int once;
203 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
204 if ((desc->format == WINED3DFMT_DXT1 || desc->format == WINED3DFMT_DXT2 || desc->format == WINED3DFMT_DXT3
205 || desc->format == WINED3DFMT_DXT4 || desc->format == WINED3DFMT_DXT5)
206 && !(format->flags[WINED3D_GL_RES_TYPE_TEX_2D] & WINED3DFMT_FLAG_TEXTURE)
207 && desc->resource_type != WINED3D_RTYPE_TEXTURE_3D && !once++)
208 ERR_(winediag)("The application tried to create a DXTn texture, but the driver does not support them.\n");
210 WARN("Failed to initialize resource, returning %#x\n", hr);
211 return hr;
213 wined3d_resource_update_draw_binding(&texture->resource);
214 if ((flags & WINED3D_TEXTURE_CREATE_MAPPABLE) || desc->format == WINED3DFMT_D16_LOCKABLE)
215 texture->resource.access_flags |= WINED3D_RESOURCE_ACCESS_CPU;
217 texture->texture_ops = texture_ops;
219 texture->layer_count = layer_count;
220 texture->level_count = level_count;
221 texture->filter_type = (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP) ? WINED3D_TEXF_LINEAR : WINED3D_TEXF_NONE;
222 texture->lod = 0;
223 texture->flags |= WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS;
224 if (flags & WINED3D_TEXTURE_CREATE_GET_DC_LENIENT)
225 texture->flags |= WINED3D_TEXTURE_PIN_SYSMEM | WINED3D_TEXTURE_GET_DC_LENIENT;
226 if (flags & WINED3D_TEXTURE_CREATE_DISCARD)
227 texture->flags |= WINED3D_TEXTURE_DISCARD;
229 return WINED3D_OK;
232 /* Context activation is done by the caller. */
233 static void wined3d_texture_remove_buffer_object(struct wined3d_texture *texture,
234 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
236 GLuint *buffer_object;
238 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
239 GL_EXTCALL(glDeleteBuffers(1, buffer_object));
240 checkGLcall("glDeleteBuffers");
241 wined3d_texture_invalidate_location(texture, sub_resource_idx, WINED3D_LOCATION_BUFFER);
242 *buffer_object = 0;
244 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
245 *buffer_object, texture, sub_resource_idx);
248 static void wined3d_texture_update_map_binding(struct wined3d_texture *texture)
250 unsigned int sub_count = texture->level_count * texture->layer_count;
251 const struct wined3d_device *device = texture->resource.device;
252 DWORD map_binding = texture->update_map_binding;
253 struct wined3d_context *context = NULL;
254 unsigned int i;
256 if (device->d3d_initialized)
257 context = context_acquire(device, NULL);
259 for (i = 0; i < sub_count; ++i)
261 if (texture->sub_resources[i].locations == texture->resource.map_binding
262 && !texture->texture_ops->texture_load_location(texture, i, context, map_binding))
263 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding));
264 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER)
265 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
268 if (context)
269 context_release(context);
271 texture->resource.map_binding = map_binding;
272 texture->update_map_binding = 0;
275 void wined3d_texture_set_map_binding(struct wined3d_texture *texture, DWORD map_binding)
277 texture->update_map_binding = map_binding;
278 if (!texture->resource.map_count)
279 wined3d_texture_update_map_binding(texture);
282 /* A GL context is provided by the caller */
283 static void gltexture_delete(struct wined3d_device *device, const struct wined3d_gl_info *gl_info,
284 struct gl_texture *tex)
286 context_gl_resource_released(device, tex->name, FALSE);
287 gl_info->gl_ops.gl.p_glDeleteTextures(1, &tex->name);
288 tex->name = 0;
291 static void wined3d_texture_unload_gl_texture(struct wined3d_texture *texture)
293 struct wined3d_device *device = texture->resource.device;
294 const struct wined3d_gl_info *gl_info = NULL;
295 struct wined3d_context *context = NULL;
297 if (texture->texture_rgb.name || texture->texture_srgb.name
298 || texture->rb_multisample || texture->rb_resolved)
300 context = context_acquire(device, NULL);
301 gl_info = context->gl_info;
304 if (texture->texture_rgb.name)
305 gltexture_delete(device, context->gl_info, &texture->texture_rgb);
307 if (texture->texture_srgb.name)
308 gltexture_delete(device, context->gl_info, &texture->texture_srgb);
310 if (texture->rb_multisample)
312 TRACE("Deleting multisample renderbuffer %u.\n", texture->rb_multisample);
313 context_gl_resource_released(device, texture->rb_multisample, TRUE);
314 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_multisample);
317 if (texture->rb_resolved)
319 TRACE("Deleting resolved renderbuffer %u.\n", texture->rb_resolved);
320 context_gl_resource_released(device, texture->rb_resolved, TRUE);
321 gl_info->fbo_ops.glDeleteRenderbuffers(1, &texture->rb_resolved);
324 if (context) context_release(context);
326 wined3d_texture_set_dirty(texture);
328 resource_unload(&texture->resource);
331 static void wined3d_texture_cleanup(struct wined3d_texture *texture)
333 unsigned int sub_count = texture->level_count * texture->layer_count;
334 struct wined3d_device *device = texture->resource.device;
335 struct wined3d_context *context = NULL;
336 const struct wined3d_gl_info *gl_info;
337 GLuint buffer_object;
338 unsigned int i;
340 TRACE("texture %p.\n", texture);
342 for (i = 0; i < sub_count; ++i)
344 if (!(buffer_object = texture->sub_resources[i].buffer_object))
345 continue;
347 TRACE("Deleting buffer object %u.\n", buffer_object);
349 /* We may not be able to get a context in wined3d_texture_cleanup() in
350 * general, but if a buffer object was previously created we can. */
351 if (!context)
353 context = context_acquire(device, NULL);
354 gl_info = context->gl_info;
357 GL_EXTCALL(glDeleteBuffers(1, &buffer_object));
359 if (context)
360 context_release(context);
362 texture->texture_ops->texture_cleanup_sub_resources(texture);
363 wined3d_texture_unload_gl_texture(texture);
364 resource_cleanup(&texture->resource);
367 void wined3d_texture_set_swapchain(struct wined3d_texture *texture, struct wined3d_swapchain *swapchain)
369 texture->swapchain = swapchain;
370 wined3d_resource_update_draw_binding(&texture->resource);
373 void wined3d_texture_set_dirty(struct wined3d_texture *texture)
375 texture->flags &= ~(WINED3D_TEXTURE_RGB_VALID | WINED3D_TEXTURE_SRGB_VALID);
378 /* Context activation is done by the caller. */
379 void wined3d_texture_bind(struct wined3d_texture *texture,
380 struct wined3d_context *context, BOOL srgb)
382 const struct wined3d_gl_info *gl_info = context->gl_info;
383 const struct wined3d_format *format = texture->resource.format;
384 const struct color_fixup_desc fixup = format->color_fixup;
385 struct gl_texture *gl_tex;
386 GLenum target;
388 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
390 if (!needs_separate_srgb_gl_texture(context))
391 srgb = FALSE;
393 /* sRGB mode cache for preload() calls outside drawprim. */
394 if (srgb)
395 texture->flags |= WINED3D_TEXTURE_IS_SRGB;
396 else
397 texture->flags &= ~WINED3D_TEXTURE_IS_SRGB;
399 gl_tex = wined3d_texture_get_gl_texture(texture, srgb);
400 target = texture->target;
402 if (gl_tex->name)
404 context_bind_texture(context, target, gl_tex->name);
405 return;
408 gl_info->gl_ops.gl.p_glGenTextures(1, &gl_tex->name);
409 checkGLcall("glGenTextures");
410 TRACE("Generated texture %d.\n", gl_tex->name);
412 if (!gl_tex->name)
414 ERR("Failed to generate a texture name.\n");
415 return;
418 /* Initialise the state of the texture object to the OpenGL defaults, not
419 * the wined3d defaults. */
420 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_WRAP;
421 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_WRAP;
422 gl_tex->sampler_desc.address_w = WINED3D_TADDRESS_WRAP;
423 memset(gl_tex->sampler_desc.border_color, 0, sizeof(gl_tex->sampler_desc.border_color));
424 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_LINEAR;
425 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT; /* GL_NEAREST_MIPMAP_LINEAR */
426 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_LINEAR; /* GL_NEAREST_MIPMAP_LINEAR */
427 gl_tex->sampler_desc.lod_bias = 0.0f;
428 gl_tex->sampler_desc.min_lod = -1000.0f;
429 gl_tex->sampler_desc.max_lod = 1000.0f;
430 gl_tex->sampler_desc.max_anisotropy = 1;
431 gl_tex->sampler_desc.compare = FALSE;
432 gl_tex->sampler_desc.comparison_func = WINED3D_CMP_LESSEQUAL;
433 if (context->gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
434 gl_tex->sampler_desc.srgb_decode = TRUE;
435 else
436 gl_tex->sampler_desc.srgb_decode = srgb;
437 gl_tex->base_level = 0;
438 wined3d_texture_set_dirty(texture);
440 context_bind_texture(context, target, gl_tex->name);
442 if (texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP)
444 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
445 checkGLcall("glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE)");
448 /* For a new texture we have to set the texture levels after binding the
449 * texture. Beware that texture rectangles do not support mipmapping, but
450 * set the maxmiplevel if we're relying on the partial
451 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
452 * (I.e., do not care about cond_np2 here, just look for
453 * GL_TEXTURE_RECTANGLE_ARB.) */
454 if (target != GL_TEXTURE_RECTANGLE_ARB)
456 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture->level_count - 1);
457 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count - 1);
458 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
461 if (target == GL_TEXTURE_CUBE_MAP_ARB)
463 /* Cubemaps are always set to clamp, regardless of the sampler state. */
464 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
465 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
466 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
469 if (texture->flags & WINED3D_TEXTURE_COND_NP2)
471 /* Conditinal non power of two textures use a different clamping
472 * default. If we're using the GL_WINE_normalized_texrect partial
473 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
474 * has the address mode set to repeat - something that prevents us
475 * from hitting the accelerated codepath. Thus manually set the GL
476 * state. The same applies to filtering. Even if the texture has only
477 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
478 * fallback on macos. */
479 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
480 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
481 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
482 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
483 checkGLcall("glTexParameteri");
484 gl_tex->sampler_desc.address_u = WINED3D_TADDRESS_CLAMP;
485 gl_tex->sampler_desc.address_v = WINED3D_TADDRESS_CLAMP;
486 gl_tex->sampler_desc.mag_filter = WINED3D_TEXF_POINT;
487 gl_tex->sampler_desc.min_filter = WINED3D_TEXF_POINT;
488 gl_tex->sampler_desc.mip_filter = WINED3D_TEXF_NONE;
491 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_DEPTH_TEXTURE])
493 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
494 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
497 if (!is_identity_fixup(fixup) && can_use_texture_swizzle(gl_info, format))
499 static const GLenum swizzle_source[] =
501 GL_ZERO, /* CHANNEL_SOURCE_ZERO */
502 GL_ONE, /* CHANNEL_SOURCE_ONE */
503 GL_RED, /* CHANNEL_SOURCE_X */
504 GL_GREEN, /* CHANNEL_SOURCE_Y */
505 GL_BLUE, /* CHANNEL_SOURCE_Z */
506 GL_ALPHA, /* CHANNEL_SOURCE_W */
508 struct
510 GLint x, y, z, w;
512 swizzle;
514 swizzle.x = swizzle_source[fixup.x_source];
515 swizzle.y = swizzle_source[fixup.y_source];
516 swizzle.z = swizzle_source[fixup.z_source];
517 swizzle.w = swizzle_source[fixup.w_source];
518 gl_info->gl_ops.gl.p_glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, &swizzle.x);
519 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
523 /* Context activation is done by the caller. */
524 void wined3d_texture_bind_and_dirtify(struct wined3d_texture *texture,
525 struct wined3d_context *context, BOOL srgb)
527 DWORD active_sampler;
529 /* We don't need a specific texture unit, but after binding the texture
530 * the current unit is dirty. Read the unit back instead of switching to
531 * 0, this avoids messing around with the state manager's GL states. The
532 * current texture unit should always be a valid one.
534 * To be more specific, this is tricky because we can implicitly be
535 * called from sampler() in state.c. This means we can't touch anything
536 * other than whatever happens to be the currently active texture, or we
537 * would risk marking already applied sampler states dirty again. */
538 active_sampler = context->rev_tex_unit_map[context->active_texture];
539 if (active_sampler != WINED3D_UNMAPPED_STAGE)
540 context_invalidate_state(context, STATE_SAMPLER(active_sampler));
541 /* FIXME: Ideally we'd only do this when touching a binding that's used by
542 * a shader. */
543 context_invalidate_state(context, STATE_SHADER_RESOURCE_BINDING);
545 wined3d_texture_bind(texture, context, srgb);
548 /* Context activation is done by the caller (state handler). */
549 /* This function relies on the correct texture being bound and loaded. */
550 void wined3d_texture_apply_sampler_desc(struct wined3d_texture *texture,
551 const struct wined3d_sampler_desc *sampler_desc, const struct wined3d_context *context)
553 const struct wined3d_gl_info *gl_info = context->gl_info;
554 GLenum target = texture->target;
555 struct gl_texture *gl_tex;
556 DWORD state;
558 TRACE("texture %p, sampler_desc %p, context %p.\n", texture, sampler_desc, context);
560 gl_tex = wined3d_texture_get_gl_texture(texture, texture->flags & WINED3D_TEXTURE_IS_SRGB);
562 state = sampler_desc->address_u;
563 if (state != gl_tex->sampler_desc.address_u)
565 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_S,
566 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
567 gl_tex->sampler_desc.address_u = state;
570 state = sampler_desc->address_v;
571 if (state != gl_tex->sampler_desc.address_v)
573 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_T,
574 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
575 gl_tex->sampler_desc.address_v = state;
578 state = sampler_desc->address_w;
579 if (state != gl_tex->sampler_desc.address_w)
581 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_WRAP_R,
582 gl_info->wrap_lookup[state - WINED3D_TADDRESS_WRAP]);
583 gl_tex->sampler_desc.address_w = state;
586 if (memcmp(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
587 sizeof(gl_tex->sampler_desc.border_color)))
589 gl_info->gl_ops.gl.p_glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, &sampler_desc->border_color[0]);
590 memcpy(gl_tex->sampler_desc.border_color, sampler_desc->border_color,
591 sizeof(gl_tex->sampler_desc.border_color));
594 state = sampler_desc->mag_filter;
595 if (state != gl_tex->sampler_desc.mag_filter)
597 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAG_FILTER, wined3d_gl_mag_filter(state));
598 gl_tex->sampler_desc.mag_filter = state;
601 if (sampler_desc->min_filter != gl_tex->sampler_desc.min_filter
602 || sampler_desc->mip_filter != gl_tex->sampler_desc.mip_filter)
604 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MIN_FILTER,
605 wined3d_gl_min_mip_filter(sampler_desc->min_filter, sampler_desc->mip_filter));
606 gl_tex->sampler_desc.min_filter = sampler_desc->min_filter;
607 gl_tex->sampler_desc.mip_filter = sampler_desc->mip_filter;
610 state = sampler_desc->max_anisotropy;
611 if (state != gl_tex->sampler_desc.max_anisotropy)
613 if (gl_info->supported[EXT_TEXTURE_FILTER_ANISOTROPIC])
614 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, state);
615 else
616 WARN("Anisotropic filtering not supported.\n");
617 gl_tex->sampler_desc.max_anisotropy = state;
620 if (!sampler_desc->srgb_decode != !gl_tex->sampler_desc.srgb_decode
621 && (context->d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
622 && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
624 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_SRGB_DECODE_EXT,
625 sampler_desc->srgb_decode ? GL_DECODE_EXT : GL_SKIP_DECODE_EXT);
626 gl_tex->sampler_desc.srgb_decode = sampler_desc->srgb_decode;
629 if (!sampler_desc->compare != !gl_tex->sampler_desc.compare)
631 if (sampler_desc->compare)
632 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
633 else
634 gl_info->gl_ops.gl.p_glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
635 gl_tex->sampler_desc.compare = sampler_desc->compare;
638 checkGLcall("Texture parameter application");
640 if (gl_info->supported[EXT_TEXTURE_LOD_BIAS])
642 gl_info->gl_ops.gl.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT,
643 GL_TEXTURE_LOD_BIAS_EXT, sampler_desc->lod_bias);
644 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
648 ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
650 ULONG refcount;
652 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
654 if (texture->swapchain)
655 return wined3d_swapchain_incref(texture->swapchain);
657 refcount = InterlockedIncrement(&texture->resource.ref);
658 TRACE("%p increasing refcount to %u.\n", texture, refcount);
660 return refcount;
663 ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
665 ULONG refcount;
667 TRACE("texture %p, swapchain %p.\n", texture, texture->swapchain);
669 if (texture->swapchain)
670 return wined3d_swapchain_decref(texture->swapchain);
672 refcount = InterlockedDecrement(&texture->resource.ref);
673 TRACE("%p decreasing refcount to %u.\n", texture, refcount);
675 if (!refcount)
677 wined3d_texture_cleanup(texture);
678 texture->resource.parent_ops->wined3d_object_destroyed(texture->resource.parent);
679 HeapFree(GetProcessHeap(), 0, texture);
682 return refcount;
685 struct wined3d_resource * CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
687 TRACE("texture %p.\n", texture);
689 return &texture->resource;
692 static BOOL color_key_equal(const struct wined3d_color_key *c1, struct wined3d_color_key *c2)
694 return c1->color_space_low_value == c2->color_space_low_value
695 && c1->color_space_high_value == c2->color_space_high_value;
698 /* Context activation is done by the caller */
699 void wined3d_texture_load(struct wined3d_texture *texture,
700 struct wined3d_context *context, BOOL srgb)
702 UINT sub_count = texture->level_count * texture->layer_count;
703 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
704 DWORD flag;
705 UINT i;
707 TRACE("texture %p, context %p, srgb %#x.\n", texture, context, srgb);
709 if (!needs_separate_srgb_gl_texture(context))
710 srgb = FALSE;
712 if (srgb)
713 flag = WINED3D_TEXTURE_SRGB_VALID;
714 else
715 flag = WINED3D_TEXTURE_RGB_VALID;
717 if (!d3d_info->shader_color_key
718 && (!(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
719 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
720 || (texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY
721 && !color_key_equal(&texture->async.gl_color_key, &texture->async.src_blt_color_key))))
723 unsigned int sub_count = texture->level_count * texture->layer_count;
724 unsigned int i;
726 TRACE("Reloading because of color key value change.\n");
727 for (i = 0; i < sub_count; i++)
729 if (!texture->texture_ops->texture_load_location(texture, i, context, texture->resource.map_binding))
730 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
731 else
732 wined3d_texture_invalidate_location(texture, i, ~texture->resource.map_binding);
735 texture->async.gl_color_key = texture->async.src_blt_color_key;
738 if (texture->flags & flag)
740 TRACE("Texture %p not dirty, nothing to do.\n", texture);
741 return;
744 /* Reload the surfaces if the texture is marked dirty. */
745 for (i = 0; i < sub_count; ++i)
747 if (!texture->texture_ops->texture_load_location(texture, i, context,
748 srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB))
749 ERR("Failed to load location (srgb %#x).\n", srgb);
751 texture->flags |= flag;
754 void CDECL wined3d_texture_preload(struct wined3d_texture *texture)
756 struct wined3d_context *context;
757 context = context_acquire(texture->resource.device, NULL);
758 wined3d_texture_load(texture, context, texture->flags & WINED3D_TEXTURE_IS_SRGB);
759 context_release(context);
762 void * CDECL wined3d_texture_get_parent(const struct wined3d_texture *texture)
764 TRACE("texture %p.\n", texture);
766 return texture->resource.parent;
769 static BOOL wined3d_texture_check_box_dimensions(const struct wined3d_texture *texture,
770 unsigned int level, const struct wined3d_box *box)
772 if (box->left >= box->right
773 || box->top >= box->bottom
774 || box->front >= box->back)
775 return FALSE;
777 if (box->right > wined3d_texture_get_level_width(texture, level)
778 || box->bottom > wined3d_texture_get_level_height(texture, level)
779 || box->back > wined3d_texture_get_level_depth(texture, level))
780 return FALSE;
782 return TRUE;
785 void CDECL wined3d_texture_get_pitch(const struct wined3d_texture *texture,
786 unsigned int level, unsigned int *row_pitch, unsigned int *slice_pitch)
788 const struct wined3d_resource *resource = &texture->resource;
789 unsigned int width = wined3d_texture_get_level_width(texture, level);
790 unsigned int height = wined3d_texture_get_level_height(texture, level);
792 if (texture->row_pitch)
794 *row_pitch = texture->row_pitch;
795 *slice_pitch = texture->slice_pitch;
796 return;
799 wined3d_format_calculate_pitch(resource->format, resource->device->surface_alignment,
800 width, height, row_pitch, slice_pitch);
803 DWORD CDECL wined3d_texture_set_lod(struct wined3d_texture *texture, DWORD lod)
805 DWORD old = texture->lod;
807 TRACE("texture %p, lod %u.\n", texture, lod);
809 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
810 * textures. The call always returns 0, and GetLOD always returns 0. */
811 if (texture->resource.pool != WINED3D_POOL_MANAGED)
813 TRACE("Ignoring SetLOD on %s texture, returning 0.\n", debug_d3dpool(texture->resource.pool));
814 return 0;
817 if (lod >= texture->level_count)
818 lod = texture->level_count - 1;
820 if (texture->lod != lod)
822 texture->lod = lod;
824 texture->texture_rgb.base_level = ~0u;
825 texture->texture_srgb.base_level = ~0u;
826 if (texture->resource.bind_count)
827 device_invalidate_state(texture->resource.device, STATE_SAMPLER(texture->sampler));
830 return old;
833 DWORD CDECL wined3d_texture_get_lod(const struct wined3d_texture *texture)
835 TRACE("texture %p, returning %u.\n", texture, texture->lod);
837 return texture->lod;
840 DWORD CDECL wined3d_texture_get_level_count(const struct wined3d_texture *texture)
842 TRACE("texture %p, returning %u.\n", texture, texture->level_count);
844 return texture->level_count;
847 HRESULT CDECL wined3d_texture_set_autogen_filter_type(struct wined3d_texture *texture,
848 enum wined3d_texture_filter_type filter_type)
850 FIXME("texture %p, filter_type %s stub!\n", texture, debug_d3dtexturefiltertype(filter_type));
852 if (!(texture->resource.usage & WINED3DUSAGE_AUTOGENMIPMAP))
854 WARN("Texture doesn't have AUTOGENMIPMAP usage.\n");
855 return WINED3DERR_INVALIDCALL;
858 texture->filter_type = filter_type;
860 return WINED3D_OK;
863 enum wined3d_texture_filter_type CDECL wined3d_texture_get_autogen_filter_type(const struct wined3d_texture *texture)
865 TRACE("texture %p.\n", texture);
867 return texture->filter_type;
870 HRESULT CDECL wined3d_texture_set_color_key(struct wined3d_texture *texture,
871 DWORD flags, const struct wined3d_color_key *color_key)
873 struct wined3d_device *device = texture->resource.device;
874 static const DWORD all_flags = WINED3D_CKEY_DST_BLT | WINED3D_CKEY_DST_OVERLAY
875 | WINED3D_CKEY_SRC_BLT | WINED3D_CKEY_SRC_OVERLAY;
877 TRACE("texture %p, flags %#x, color_key %p.\n", texture, flags, color_key);
879 if (flags & ~all_flags)
881 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
882 return WINED3DERR_INVALIDCALL;
885 wined3d_cs_emit_set_color_key(device->cs, texture, flags, color_key);
887 return WINED3D_OK;
890 HRESULT CDECL wined3d_texture_update_desc(struct wined3d_texture *texture, UINT width, UINT height,
891 enum wined3d_format_id format_id, enum wined3d_multisample_type multisample_type,
892 UINT multisample_quality, void *mem, UINT pitch)
894 struct wined3d_device *device = texture->resource.device;
895 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
896 const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
897 UINT resource_size = wined3d_format_calculate_size(format, device->surface_alignment, width, height, 1);
898 struct wined3d_texture_sub_resource *sub_resource;
899 struct wined3d_surface *surface;
900 DWORD valid_location = 0;
901 BOOL create_dib = FALSE;
903 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
904 "mem %p, pitch %u.\n",
905 texture, width, height, debug_d3dformat(format_id), multisample_type, multisample_quality, mem, pitch);
907 if (!resource_size)
908 return WINED3DERR_INVALIDCALL;
910 if (texture->level_count * texture->layer_count > 1)
912 WARN("Texture has multiple sub-resources, not supported.\n");
913 return WINED3DERR_INVALIDCALL;
916 if (texture->resource.type == WINED3D_RTYPE_TEXTURE_3D)
918 WARN("Not supported on 3D textures.\n");
919 return WINED3DERR_INVALIDCALL;
922 if (texture->resource.map_count)
924 WARN("Texture is mapped.\n");
925 return WINED3DERR_INVALIDCALL;
928 /* We have no way of supporting a pitch that is not a multiple of the pixel
929 * byte width short of uploading the texture row-by-row.
930 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
931 * for user-memory textures (it always expects packed data) while DirectDraw
932 * requires a 4-byte aligned pitch and doesn't support texture formats
933 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
934 * This check is here to verify that the assumption holds. */
935 if (pitch % texture->resource.format->byte_count)
937 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
938 return WINED3DERR_INVALIDCALL;
941 if (device->d3d_initialized)
942 texture->resource.resource_ops->resource_unload(&texture->resource);
944 sub_resource = &texture->sub_resources[0];
945 surface = sub_resource->u.surface;
946 if (surface->dc)
948 wined3d_surface_destroy_dc(surface);
949 create_dib = TRUE;
952 wined3d_resource_free_sysmem(sub_resource->resource);
954 if ((texture->row_pitch = pitch))
955 texture->slice_pitch = height * pitch;
956 else
957 /* User memory surfaces don't have the regular surface alignment. */
958 wined3d_format_calculate_pitch(format, 1, width, height,
959 &texture->row_pitch, &texture->slice_pitch);
961 texture->resource.format = format;
962 texture->resource.multisample_type = multisample_type;
963 texture->resource.multisample_quality = multisample_quality;
964 texture->resource.width = width;
965 texture->resource.height = height;
967 sub_resource->resource->format = format;
968 sub_resource->resource->multisample_type = multisample_type;
969 sub_resource->resource->multisample_quality = multisample_quality;
970 sub_resource->resource->width = width;
971 sub_resource->resource->height = height;
972 sub_resource->resource->size = texture->slice_pitch;
974 if (((width & (width - 1)) || (height & (height - 1))) && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO]
975 && !gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
977 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
978 texture->pow2_width = texture->pow2_height = 1;
979 while (texture->pow2_width < width)
980 texture->pow2_width <<= 1;
981 while (texture->pow2_height < height)
982 texture->pow2_height <<= 1;
984 else
986 texture->flags &= ~WINED3D_TEXTURE_COND_NP2_EMULATED;
987 texture->pow2_width = width;
988 texture->pow2_height = height;
991 sub_resource->locations = 0;
993 if ((texture->user_memory = mem))
995 texture->resource.map_binding = WINED3D_LOCATION_USER_MEMORY;
996 valid_location = WINED3D_LOCATION_USER_MEMORY;
998 else
1000 wined3d_surface_prepare(surface, NULL, WINED3D_LOCATION_SYSMEM);
1001 valid_location = WINED3D_LOCATION_SYSMEM;
1004 /* The format might be changed to a format that needs conversion.
1005 * If the surface didn't use PBOs previously but could now, don't
1006 * change it - whatever made us not use PBOs might come back, e.g.
1007 * color keys. */
1008 if (texture->resource.map_binding == WINED3D_LOCATION_BUFFER && !wined3d_texture_use_pbo(texture, gl_info))
1009 texture->resource.map_binding = WINED3D_LOCATION_SYSMEM;
1011 wined3d_texture_validate_location(texture, 0, valid_location);
1013 if (create_dib)
1014 wined3d_surface_create_dc(surface);
1016 return WINED3D_OK;
1019 /* Context activation is done by the caller. */
1020 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture *texture,
1021 unsigned int sub_resource_idx, const struct wined3d_gl_info *gl_info)
1023 GLuint *buffer_object;
1025 buffer_object = &texture->sub_resources[sub_resource_idx].buffer_object;
1026 if (*buffer_object)
1027 return;
1029 GL_EXTCALL(glGenBuffers(1, buffer_object));
1030 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, *buffer_object));
1031 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER,
1032 texture->sub_resources[sub_resource_idx].resource->size, NULL, GL_STREAM_DRAW));
1033 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
1034 checkGLcall("Create buffer object");
1036 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1037 *buffer_object, texture, sub_resource_idx);
1040 static void wined3d_texture_force_reload(struct wined3d_texture *texture)
1042 unsigned int sub_count = texture->level_count * texture->layer_count;
1043 unsigned int i;
1045 texture->flags &= ~(WINED3D_TEXTURE_RGB_ALLOCATED | WINED3D_TEXTURE_SRGB_ALLOCATED
1046 | WINED3D_TEXTURE_CONVERTED);
1047 texture->async.flags &= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1048 for (i = 0; i < sub_count; ++i)
1050 wined3d_texture_invalidate_location(texture, i,
1051 WINED3D_LOCATION_TEXTURE_RGB | WINED3D_LOCATION_TEXTURE_SRGB);
1055 void wined3d_texture_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1057 DWORD alloc_flag = srgb ? WINED3D_TEXTURE_SRGB_ALLOCATED : WINED3D_TEXTURE_RGB_ALLOCATED;
1058 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
1060 if (!d3d_info->shader_color_key
1061 && !(texture->async.flags & WINED3D_TEXTURE_ASYNC_COLOR_KEY)
1062 != !(texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT))
1064 wined3d_texture_force_reload(texture);
1066 if (texture->async.color_key_flags & WINED3D_CKEY_SRC_BLT)
1067 texture->async.flags |= WINED3D_TEXTURE_ASYNC_COLOR_KEY;
1070 if (texture->flags & alloc_flag)
1071 return;
1073 texture->texture_ops->texture_prepare_texture(texture, context, srgb);
1074 texture->flags |= alloc_flag;
1077 static void wined3d_texture_prepare_rb(struct wined3d_texture *texture,
1078 const struct wined3d_gl_info *gl_info, BOOL multisample)
1080 const struct wined3d_format *format = texture->resource.format;
1082 if (multisample)
1084 DWORD samples;
1086 if (texture->rb_multisample)
1087 return;
1089 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
1090 * feature through type == MULTISAMPLE_XX and quality != 0. This could
1091 * be mapped to GL_NV_framebuffer_multisample_coverage.
1093 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
1094 * (EQAA), but it does not have an equivalent OpenGL extension. */
1096 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
1097 * levels as the count of advertised multisample types for the texture
1098 * format. */
1099 if (texture->resource.multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE)
1101 unsigned int i, count = 0;
1103 for (i = 0; i < sizeof(format->multisample_types) * 8; ++i)
1105 if (format->multisample_types & 1u << i)
1107 if (texture->resource.multisample_quality == count++)
1108 break;
1111 samples = i + 1;
1113 else
1115 samples = texture->resource.multisample_type;
1118 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_multisample);
1119 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_multisample);
1120 gl_info->fbo_ops.glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples,
1121 format->glInternal, texture->resource.width, texture->resource.height);
1122 checkGLcall("glRenderbufferStorageMultisample()");
1123 TRACE("Created multisample rb %u.\n", texture->rb_multisample);
1125 else
1127 if (texture->rb_resolved)
1128 return;
1130 gl_info->fbo_ops.glGenRenderbuffers(1, &texture->rb_resolved);
1131 gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, texture->rb_resolved);
1132 gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, format->glInternal,
1133 texture->resource.width, texture->resource.height);
1134 checkGLcall("glRenderbufferStorage()");
1135 TRACE("Created resolved rb %u.\n", texture->rb_resolved);
1139 /* Context activation is done by the caller. Context may be NULL in
1140 * WINED3D_NO3D mode. */
1141 BOOL wined3d_texture_prepare_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1142 struct wined3d_context *context, DWORD location)
1144 switch (location)
1146 case WINED3D_LOCATION_SYSMEM:
1147 if (texture->sub_resources[sub_resource_idx].resource->heap_memory)
1148 return TRUE;
1150 if (!wined3d_resource_allocate_sysmem(texture->sub_resources[sub_resource_idx].resource))
1152 ERR("Failed to allocate system memory.\n");
1153 return FALSE;
1155 return TRUE;
1157 case WINED3D_LOCATION_USER_MEMORY:
1158 if (!texture->user_memory)
1159 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1160 return TRUE;
1162 case WINED3D_LOCATION_BUFFER:
1163 wined3d_texture_prepare_buffer_object(texture, sub_resource_idx, context->gl_info);
1164 return TRUE;
1166 case WINED3D_LOCATION_TEXTURE_RGB:
1167 wined3d_texture_prepare_texture(texture, context, FALSE);
1168 return TRUE;
1170 case WINED3D_LOCATION_TEXTURE_SRGB:
1171 wined3d_texture_prepare_texture(texture, context, TRUE);
1172 return TRUE;
1174 case WINED3D_LOCATION_DRAWABLE:
1175 if (!texture->swapchain)
1176 ERR("Texture %p does not have a drawable.\n", texture);
1177 return TRUE;
1179 case WINED3D_LOCATION_RB_MULTISAMPLE:
1180 wined3d_texture_prepare_rb(texture, context->gl_info, TRUE);
1181 return TRUE;
1183 case WINED3D_LOCATION_RB_RESOLVED:
1184 wined3d_texture_prepare_rb(texture, context->gl_info, FALSE);
1185 return TRUE;
1187 default:
1188 ERR("Invalid location %s.\n", wined3d_debug_location(location));
1189 return FALSE;
1193 void CDECL wined3d_texture_generate_mipmaps(struct wined3d_texture *texture)
1195 /* TODO: Implement filters using GL_SGI_generate_mipmaps. */
1196 FIXME("texture %p stub!\n", texture);
1199 struct wined3d_texture_sub_resource *wined3d_texture_get_sub_resource(struct wined3d_texture *texture,
1200 unsigned int sub_resource_idx)
1202 UINT sub_count = texture->level_count * texture->layer_count;
1204 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
1206 if (sub_resource_idx >= sub_count)
1208 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
1209 return NULL;
1212 return &texture->sub_resources[sub_resource_idx];
1215 HRESULT CDECL wined3d_texture_add_dirty_region(struct wined3d_texture *texture,
1216 UINT layer, const struct wined3d_box *dirty_region)
1218 struct wined3d_context *context;
1219 unsigned int sub_resource_idx;
1221 TRACE("texture %p, layer %u, dirty_region %s.\n", texture, layer, debug_box(dirty_region));
1223 if (layer >= texture->layer_count)
1225 WARN("Invalid layer %u specified.\n", layer);
1226 return WINED3DERR_INVALIDCALL;
1228 sub_resource_idx = layer * texture->level_count;
1230 if (dirty_region)
1231 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region));
1233 context = context_acquire(texture->resource.device, NULL);
1234 if (!texture->texture_ops->texture_load_location(texture, sub_resource_idx,
1235 context, texture->resource.map_binding))
1237 ERR("Failed to load location %s.\n", wined3d_debug_location(texture->resource.map_binding));
1238 context_release(context);
1239 return E_OUTOFMEMORY;
1241 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1242 context_release(context);
1244 return WINED3D_OK;
1247 static HRESULT wined3d_texture_upload_data(struct wined3d_texture *texture,
1248 const struct wined3d_sub_resource_data *data)
1250 unsigned int sub_count = texture->level_count * texture->layer_count;
1251 struct wined3d_context *context;
1252 unsigned int i;
1254 for (i = 0; i < sub_count; ++i)
1256 if (!data[i].data)
1258 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i);
1259 return E_INVALIDARG;
1263 context = context_acquire(texture->resource.device, NULL);
1265 wined3d_texture_prepare_texture(texture, context, FALSE);
1266 wined3d_texture_bind_and_dirtify(texture, context, FALSE);
1268 for (i = 0; i < sub_count; ++i)
1270 texture->texture_ops->texture_upload_data(texture, i, context, &data[i]);
1271 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_TEXTURE_RGB);
1272 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_TEXTURE_RGB);
1275 context_release(context);
1277 return WINED3D_OK;
1280 static void texture2d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1281 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1283 static const POINT dst_point = {0, 0};
1284 struct wined3d_const_bo_address addr;
1285 unsigned int texture_level;
1286 RECT src_rect;
1288 texture_level = sub_resource_idx % texture->level_count;
1289 SetRect(&src_rect, 0, 0, wined3d_texture_get_level_width(texture, texture_level),
1290 wined3d_texture_get_level_height(texture, texture_level));
1292 addr.buffer_object = 0;
1293 addr.addr = data->data;
1295 wined3d_surface_upload_data(texture->sub_resources[sub_resource_idx].u.surface, context->gl_info,
1296 texture->resource.format, &src_rect, data->row_pitch, &dst_point, FALSE, &addr);
1299 static BOOL texture2d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1300 struct wined3d_context *context, DWORD location)
1302 return SUCCEEDED(surface_load_location(texture->sub_resources[sub_resource_idx].u.surface, context, location));
1305 /* Context activation is done by the caller. */
1306 static void texture2d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1308 UINT sub_count = texture->level_count * texture->layer_count;
1309 const struct wined3d_format *format = texture->resource.format;
1310 const struct wined3d_gl_info *gl_info = context->gl_info;
1311 const struct wined3d_color_key_conversion *conversion;
1312 GLenum internal;
1313 UINT i;
1315 TRACE("texture %p, context %p, format %s.\n", texture, context, debug_d3dformat(format->id));
1317 if (format->convert)
1319 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1321 else if ((conversion = wined3d_format_get_color_key_conversion(texture, TRUE)))
1323 texture->flags |= WINED3D_TEXTURE_CONVERTED;
1324 format = wined3d_get_format(gl_info, conversion->dst_format);
1325 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format->id));
1328 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1330 if (srgb)
1331 internal = format->glGammaInternal;
1332 else if (texture->resource.usage & WINED3DUSAGE_RENDERTARGET
1333 && wined3d_resource_is_offscreen(&texture->resource))
1334 internal = format->rtInternal;
1335 else
1336 internal = format->glInternal;
1338 if (!internal)
1339 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format->id));
1341 TRACE("internal %#x, format %#x, type %#x.\n", internal, format->glFormat, format->glType);
1343 for (i = 0; i < sub_count; ++i)
1345 struct wined3d_surface *surface = texture->sub_resources[i].u.surface;
1346 GLsizei width, height;
1348 width = wined3d_texture_get_level_pow2_width(texture, surface->texture_level);
1349 height = wined3d_texture_get_level_pow2_height(texture, surface->texture_level);
1350 if (texture->resource.format_flags & WINED3DFMT_FLAG_HEIGHT_SCALE)
1352 height *= format->height_scale.numerator;
1353 height /= format->height_scale.denominator;
1356 TRACE("surface %p, target %#x, level %d, width %d, height %d.\n",
1357 surface, surface->texture_target, surface->texture_level, width, height);
1359 gl_info->gl_ops.gl.p_glTexImage2D(surface->texture_target, surface->texture_level,
1360 internal, width, height, 0, format->glFormat, format->glType, NULL);
1361 checkGLcall("glTexImage2D");
1365 static void texture2d_cleanup_sub_resources(struct wined3d_texture *texture)
1367 unsigned int sub_count = texture->level_count * texture->layer_count;
1368 struct wined3d_surface *surface;
1369 unsigned int i;
1371 for (i = 0; i < sub_count; ++i)
1373 if ((surface = texture->sub_resources[i].u.surface))
1375 TRACE("surface %p.\n", surface);
1377 wined3d_surface_cleanup(surface);
1378 surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
1381 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.surface);
1384 static const struct wined3d_texture_ops texture2d_ops =
1386 texture2d_upload_data,
1387 texture2d_load_location,
1388 texture2d_prepare_texture,
1389 texture2d_cleanup_sub_resources,
1392 static ULONG texture_resource_incref(struct wined3d_resource *resource)
1394 return wined3d_texture_incref(wined3d_texture_from_resource(resource));
1397 static ULONG texture_resource_decref(struct wined3d_resource *resource)
1399 return wined3d_texture_decref(wined3d_texture_from_resource(resource));
1402 static void wined3d_texture_unload(struct wined3d_resource *resource)
1404 struct wined3d_texture *texture = wined3d_texture_from_resource(resource);
1405 UINT sub_count = texture->level_count * texture->layer_count;
1406 struct wined3d_device *device = resource->device;
1407 const struct wined3d_gl_info *gl_info;
1408 struct wined3d_context *context;
1409 UINT i;
1411 TRACE("texture %p.\n", texture);
1413 context = context_acquire(device, NULL);
1414 gl_info = context->gl_info;
1416 for (i = 0; i < sub_count; ++i)
1418 struct wined3d_texture_sub_resource *sub_resource = &texture->sub_resources[i];
1420 if (resource->pool != WINED3D_POOL_DEFAULT
1421 && texture->texture_ops->texture_load_location(texture, i, context, resource->map_binding))
1423 wined3d_texture_invalidate_location(texture, i, ~resource->map_binding);
1425 else
1427 /* We should only get here on device reset/teardown for implicit
1428 * resources. */
1429 if (resource->pool != WINED3D_POOL_DEFAULT || resource->type != WINED3D_RTYPE_TEXTURE_2D)
1430 ERR("Discarding %s %p sub-resource %u in the %s pool.\n", debug_d3dresourcetype(resource->type),
1431 resource, i, debug_d3dpool(resource->pool));
1432 wined3d_texture_validate_location(texture, i, WINED3D_LOCATION_DISCARDED);
1433 wined3d_texture_invalidate_location(texture, i, ~WINED3D_LOCATION_DISCARDED);
1436 if (sub_resource->buffer_object)
1437 wined3d_texture_remove_buffer_object(texture, i, context->gl_info);
1439 if (resource->type == WINED3D_RTYPE_TEXTURE_2D)
1441 struct wined3d_surface *surface = sub_resource->u.surface;
1442 struct wined3d_renderbuffer_entry *entry, *entry2;
1444 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, struct wined3d_renderbuffer_entry, entry)
1446 context_gl_resource_released(device, entry->id, TRUE);
1447 gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
1448 list_remove(&entry->entry);
1449 HeapFree(GetProcessHeap(), 0, entry);
1451 list_init(&surface->renderbuffers);
1452 surface->current_renderbuffer = NULL;
1455 resource_unload(sub_resource->resource);
1458 context_release(context);
1460 wined3d_texture_force_reload(texture);
1461 wined3d_texture_unload_gl_texture(texture);
1464 static HRESULT texture_resource_sub_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx,
1465 struct wined3d_map_desc *map_desc, const struct wined3d_box *box, DWORD flags)
1467 const struct wined3d_format *format = resource->format;
1468 struct wined3d_texture_sub_resource *sub_resource;
1469 struct wined3d_device *device = resource->device;
1470 unsigned int fmt_flags = resource->format_flags;
1471 const struct wined3d_gl_info *gl_info = NULL;
1472 struct wined3d_context *context = NULL;
1473 struct wined3d_texture *texture;
1474 struct wined3d_bo_address data;
1475 unsigned int texture_level;
1476 BYTE *base_memory;
1477 BOOL ret;
1479 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
1480 resource, sub_resource_idx, map_desc, debug_box(box), flags);
1482 texture = wined3d_texture_from_resource(resource);
1483 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1484 return E_INVALIDARG;
1486 texture_level = sub_resource_idx % texture->level_count;
1487 if (box && !wined3d_texture_check_box_dimensions(texture, texture_level, box))
1489 WARN("Map box is invalid.\n");
1490 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1491 return WINED3DERR_INVALIDCALL;
1494 if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
1495 && !wined3d_texture_check_block_align(texture, texture_level, box))
1497 WARN("Map box %s is misaligned for %ux%u blocks.\n",
1498 debug_box(box), format->block_width, format->block_height);
1499 if (resource->type != WINED3D_RTYPE_TEXTURE_2D || resource->pool == WINED3D_POOL_DEFAULT)
1500 return WINED3DERR_INVALIDCALL;
1503 if (!(resource->access_flags & WINED3D_RESOURCE_ACCESS_CPU))
1505 WARN("Trying to map unmappable texture.\n");
1506 if (resource->type != WINED3D_RTYPE_TEXTURE_2D)
1507 return WINED3DERR_INVALIDCALL;
1510 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1512 WARN("DC is in use.\n");
1513 return WINED3DERR_INVALIDCALL;
1516 if (sub_resource->map_count)
1518 WARN("Sub-resource is already mapped.\n");
1519 return WINED3DERR_INVALIDCALL;
1522 flags = wined3d_resource_sanitize_map_flags(resource, flags);
1524 if (device->d3d_initialized)
1526 context = context_acquire(device, NULL);
1527 gl_info = context->gl_info;
1530 if (flags & WINED3D_MAP_DISCARD)
1532 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
1533 wined3d_debug_location(texture->resource.map_binding));
1534 if ((ret = wined3d_texture_prepare_location(texture, sub_resource_idx,
1535 context, texture->resource.map_binding)))
1536 wined3d_texture_validate_location(texture, sub_resource_idx, texture->resource.map_binding);
1538 else
1540 if (resource->usage & WINED3DUSAGE_DYNAMIC)
1541 WARN_(d3d_perf)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
1542 ret = texture->texture_ops->texture_load_location(texture,
1543 sub_resource_idx, context, texture->resource.map_binding);
1546 if (!ret)
1548 ERR("Failed to prepare location.\n");
1549 context_release(context);
1550 return E_OUTOFMEMORY;
1553 if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
1554 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
1556 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1557 base_memory = wined3d_texture_map_bo_address(&data, sub_resource->resource->size,
1558 gl_info, GL_PIXEL_UNPACK_BUFFER, flags);
1559 TRACE("Base memory pointer %p.\n", base_memory);
1561 if (context)
1562 context_release(context);
1564 if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
1566 map_desc->row_pitch = wined3d_texture_get_level_width(texture, texture_level) * format->byte_count;
1567 map_desc->slice_pitch = wined3d_texture_get_level_height(texture, texture_level) * map_desc->row_pitch;
1569 else
1571 wined3d_texture_get_pitch(texture, texture_level, &map_desc->row_pitch, &map_desc->slice_pitch);
1574 if (!box)
1576 map_desc->data = base_memory;
1578 else
1580 if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
1582 /* Compressed textures are block based, so calculate the offset of
1583 * the block that contains the top-left pixel of the mapped box. */
1584 map_desc->data = base_memory
1585 + (box->front * map_desc->slice_pitch)
1586 + ((box->top / format->block_height) * map_desc->row_pitch)
1587 + ((box->left / format->block_width) * format->block_byte_count);
1589 else
1591 map_desc->data = base_memory
1592 + (box->front * map_desc->slice_pitch)
1593 + (box->top * map_desc->row_pitch)
1594 + (box->left * format->byte_count);
1598 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1600 RECT *r = &texture->swapchain->front_buffer_update;
1602 if (!box)
1603 SetRect(r, 0, 0, resource->width, resource->height);
1604 else
1605 SetRect(r, box->left, box->top, box->right, box->bottom);
1606 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r));
1609 ++resource->map_count;
1610 ++sub_resource->map_count;
1612 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
1613 map_desc->data, map_desc->row_pitch, map_desc->slice_pitch);
1615 return WINED3D_OK;
1618 static HRESULT texture_resource_sub_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
1620 struct wined3d_texture_sub_resource *sub_resource;
1621 struct wined3d_device *device = resource->device;
1622 const struct wined3d_gl_info *gl_info = NULL;
1623 struct wined3d_context *context = NULL;
1624 struct wined3d_texture *texture;
1625 struct wined3d_bo_address data;
1627 TRACE("resource %p, sub_resource_idx %u.\n", resource, sub_resource_idx);
1629 texture = wined3d_texture_from_resource(resource);
1630 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
1631 return E_INVALIDARG;
1633 if (!sub_resource->map_count)
1635 WARN("Trying to unmap unmapped sub-resource.\n");
1636 if (texture->flags & WINED3D_TEXTURE_DC_IN_USE)
1637 return WINED3D_OK;
1638 return WINEDDERR_NOTLOCKED;
1641 if (device->d3d_initialized)
1643 context = context_acquire(device, NULL);
1644 gl_info = context->gl_info;
1647 wined3d_texture_get_memory(texture, sub_resource_idx, &data, texture->resource.map_binding);
1648 wined3d_texture_unmap_bo_address(&data, gl_info, GL_PIXEL_UNPACK_BUFFER);
1650 if (context)
1651 context_release(context);
1653 if (texture->swapchain && texture->swapchain->front_buffer == texture)
1655 if (!(sub_resource->locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB)))
1656 texture->swapchain->swapchain_ops->swapchain_frontbuffer_updated(texture->swapchain);
1658 else if (resource->format_flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
1660 FIXME("Depth / stencil buffer locking is not implemented.\n");
1663 --sub_resource->map_count;
1664 if (!--resource->map_count && texture->update_map_binding)
1665 wined3d_texture_update_map_binding(texture);
1667 return WINED3D_OK;
1670 static const struct wined3d_resource_ops texture_resource_ops =
1672 texture_resource_incref,
1673 texture_resource_decref,
1674 wined3d_texture_unload,
1675 texture_resource_sub_resource_map,
1676 texture_resource_sub_resource_unmap,
1679 static HRESULT texture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1680 unsigned int layer_count, unsigned int level_count, DWORD flags, struct wined3d_device *device,
1681 void *parent, const struct wined3d_parent_ops *parent_ops)
1683 struct wined3d_device_parent *device_parent = device->device_parent;
1684 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1685 struct wined3d_resource_desc surface_desc;
1686 struct wined3d_surface *surfaces;
1687 UINT pow2_width, pow2_height;
1688 unsigned int i, j;
1689 HRESULT hr;
1691 /* TODO: It should only be possible to create textures for formats
1692 * that are reported as supported. */
1693 if (WINED3DFMT_UNKNOWN >= desc->format)
1695 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
1696 return WINED3DERR_INVALIDCALL;
1699 if (desc->usage & WINED3DUSAGE_DYNAMIC && desc->pool == WINED3D_POOL_MANAGED)
1700 FIXME("Trying to create a managed texture with dynamic usage.\n");
1701 if (!(desc->usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL))
1702 && (flags & WINED3D_TEXTURE_CREATE_MAPPABLE))
1703 WARN("Creating a mappable texture in the default pool that doesn't specify dynamic usage.\n");
1704 if (desc->usage & WINED3DUSAGE_RENDERTARGET && desc->pool != WINED3D_POOL_DEFAULT)
1705 FIXME("Trying to create a render target that isn't in the default pool.\n");
1707 pow2_width = desc->width;
1708 pow2_height = desc->height;
1709 if (((desc->width & (desc->width - 1)) || (desc->height & (desc->height - 1)))
1710 && !gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
1712 /* level_count == 0 returns an error as well. */
1713 if (level_count != 1 || desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1715 if (desc->pool != WINED3D_POOL_SCRATCH)
1717 WARN("Attempted to create a mipmapped/cube NPOT texture without unconditional NPOT support.\n");
1718 return WINED3DERR_INVALIDCALL;
1721 WARN("Creating a scratch mipmapped/cube NPOT texture despite lack of HW support.\n");
1723 texture->flags |= WINED3D_TEXTURE_COND_NP2;
1725 if (!gl_info->supported[ARB_TEXTURE_RECTANGLE] && !gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
1727 const struct wined3d_format *format = wined3d_get_format(gl_info, desc->format);
1729 /* TODO: Add support for non-power-of-two compressed textures. */
1730 if (format->flags[WINED3D_GL_RES_TYPE_TEX_2D]
1731 & (WINED3DFMT_FLAG_COMPRESSED | WINED3DFMT_FLAG_HEIGHT_SCALE))
1733 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
1734 desc->width, desc->height);
1735 return WINED3DERR_NOTAVAILABLE;
1738 /* Find the nearest pow2 match. */
1739 pow2_width = pow2_height = 1;
1740 while (pow2_width < desc->width)
1741 pow2_width <<= 1;
1742 while (pow2_height < desc->height)
1743 pow2_height <<= 1;
1744 texture->flags |= WINED3D_TEXTURE_COND_NP2_EMULATED;
1747 texture->pow2_width = pow2_width;
1748 texture->pow2_height = pow2_height;
1750 if ((pow2_width > gl_info->limits.texture_size || pow2_height > gl_info->limits.texture_size)
1751 && (desc->usage & WINED3DUSAGE_TEXTURE))
1753 /* One of four options:
1754 * 1: Do the same as we do with NPOT and scale the texture. (Any
1755 * texture ops would require the texture to be scaled which is
1756 * potentially slow.)
1757 * 2: Set the texture to the maximum size (bad idea).
1758 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
1759 * 4: Create the surface, but allow it to be used only for DirectDraw
1760 * Blts. Some apps (e.g. Swat 3) create textures with a height of
1761 * 16 and a width > 3000 and blt 16x16 letter areas from them to
1762 * the render target. */
1763 if (desc->pool == WINED3D_POOL_DEFAULT || desc->pool == WINED3D_POOL_MANAGED)
1765 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width, pow2_height);
1766 return WINED3DERR_NOTAVAILABLE;
1769 /* We should never use this surface in combination with OpenGL. */
1770 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width, pow2_height);
1773 /* Calculate levels for mip mapping. */
1774 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
1776 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
1778 WARN("No mipmap generation support, returning WINED3DERR_INVALIDCALL.\n");
1779 return WINED3DERR_INVALIDCALL;
1782 if (level_count != 1)
1784 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning WINED3DERR_INVALIDCALL.\n");
1785 return WINED3DERR_INVALIDCALL;
1789 if (FAILED(hr = wined3d_texture_init(texture, &texture2d_ops, layer_count, level_count, desc,
1790 flags, device, parent, parent_ops, &texture_resource_ops)))
1792 WARN("Failed to initialize texture, returning %#x.\n", hr);
1793 return hr;
1796 /* Precalculated scaling for 'faked' non power of two texture coords. */
1797 if (texture->resource.gl_type == WINED3D_GL_RES_TYPE_TEX_RECT)
1799 texture->pow2_matrix[0] = (float)desc->width;
1800 texture->pow2_matrix[5] = (float)desc->height;
1801 texture->flags &= ~(WINED3D_TEXTURE_POW2_MAT_IDENT | WINED3D_TEXTURE_NORMALIZED_COORDS);
1802 texture->target = GL_TEXTURE_RECTANGLE_ARB;
1804 else
1806 if (texture->flags & WINED3D_TEXTURE_COND_NP2_EMULATED)
1808 texture->pow2_matrix[0] = (((float)desc->width) / ((float)pow2_width));
1809 texture->pow2_matrix[5] = (((float)desc->height) / ((float)pow2_height));
1810 texture->flags &= ~WINED3D_TEXTURE_POW2_MAT_IDENT;
1812 else
1814 texture->pow2_matrix[0] = 1.0f;
1815 texture->pow2_matrix[5] = 1.0f;
1817 if (desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP)
1818 texture->target = GL_TEXTURE_CUBE_MAP_ARB;
1819 else
1820 texture->target = GL_TEXTURE_2D;
1822 texture->pow2_matrix[10] = 1.0f;
1823 texture->pow2_matrix[15] = 1.0f;
1824 TRACE("x scale %.8e, y scale %.8e.\n", texture->pow2_matrix[0], texture->pow2_matrix[5]);
1826 if (wined3d_texture_use_pbo(texture, gl_info))
1827 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
1829 if (!(surfaces = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*surfaces) * level_count * layer_count)))
1831 wined3d_texture_cleanup(texture);
1832 return E_OUTOFMEMORY;
1835 /* Generate all the surfaces. */
1836 surface_desc = *desc;
1837 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
1838 for (i = 0; i < texture->level_count; ++i)
1840 for (j = 0; j < texture->layer_count; ++j)
1842 static const GLenum cube_targets[6] =
1844 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
1845 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
1846 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
1847 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
1848 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
1849 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB,
1851 GLenum target = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? cube_targets[j] : texture->target;
1852 unsigned int idx = j * texture->level_count + i;
1853 struct wined3d_surface *surface;
1855 surface = &surfaces[idx];
1856 if (FAILED(hr = wined3d_surface_init(surface, texture, &surface_desc, target, i, j)))
1858 WARN("Failed to initialize surface, returning %#x.\n", hr);
1859 wined3d_texture_cleanup(texture);
1860 if (!idx)
1861 HeapFree(GetProcessHeap(), 0, surfaces);
1862 return hr;
1865 if (FAILED(hr = device_parent->ops->surface_created(device_parent,
1866 texture, idx, &parent, &parent_ops)))
1868 WARN("Failed to create surface parent, hr %#x.\n", hr);
1869 wined3d_surface_cleanup(surface);
1870 wined3d_texture_cleanup(texture);
1871 return hr;
1874 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
1876 surface->resource.parent = parent;
1877 surface->resource.parent_ops = parent_ops;
1878 texture->sub_resources[idx].resource = &surface->resource;
1879 texture->sub_resources[idx].u.surface = surface;
1880 TRACE("Created surface level %u, layer %u @ %p.\n", i, j, surface);
1882 if (((desc->usage & WINED3DUSAGE_OWNDC) || (device->wined3d->flags & WINED3D_NO3D))
1883 && FAILED(hr = wined3d_surface_create_dc(surface)))
1885 wined3d_texture_cleanup(texture);
1886 return hr;
1889 /* Calculate the next mipmap level. */
1890 surface_desc.width = max(1, surface_desc.width >> 1);
1891 surface_desc.height = max(1, surface_desc.height >> 1);
1894 return WINED3D_OK;
1897 static void texture3d_upload_data(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1898 const struct wined3d_context *context, const struct wined3d_sub_resource_data *data)
1900 struct wined3d_const_bo_address addr;
1901 unsigned int row_pitch, slice_pitch;
1903 wined3d_texture_get_pitch(texture, sub_resource_idx, &row_pitch, &slice_pitch);
1904 if (row_pitch != data->row_pitch || slice_pitch != data->slice_pitch)
1905 FIXME("Ignoring row/slice pitch (%u/%u).\n", data->row_pitch, data->slice_pitch);
1907 addr.buffer_object = 0;
1908 addr.addr = data->data;
1910 wined3d_volume_upload_data(texture->sub_resources[sub_resource_idx].u.volume, context, &addr);
1913 static BOOL texture3d_load_location(struct wined3d_texture *texture, unsigned int sub_resource_idx,
1914 struct wined3d_context *context, DWORD location)
1916 return wined3d_volume_load_location(texture->sub_resources[sub_resource_idx].u.volume, context, location);
1919 static void texture3d_prepare_texture(struct wined3d_texture *texture, struct wined3d_context *context, BOOL srgb)
1921 unsigned int sub_count = texture->level_count * texture->layer_count;
1922 const struct wined3d_format *format = texture->resource.format;
1923 const struct wined3d_gl_info *gl_info = context->gl_info;
1924 unsigned int i;
1926 wined3d_texture_bind_and_dirtify(texture, context, srgb);
1928 for (i = 0; i < sub_count; ++i)
1930 struct wined3d_volume *volume = texture->sub_resources[i].u.volume;
1932 GL_EXTCALL(glTexImage3D(GL_TEXTURE_3D, volume->texture_level,
1933 srgb ? format->glGammaInternal : format->glInternal,
1934 wined3d_texture_get_level_width(texture, volume->texture_level),
1935 wined3d_texture_get_level_height(texture, volume->texture_level),
1936 wined3d_texture_get_level_depth(texture, volume->texture_level),
1937 0, format->glFormat, format->glType, NULL));
1938 checkGLcall("glTexImage3D");
1942 static void texture3d_cleanup_sub_resources(struct wined3d_texture *texture)
1944 unsigned int sub_count = texture->level_count * texture->layer_count;
1945 struct wined3d_volume *volume;
1946 unsigned int i;
1948 for (i = 0; i < sub_count; ++i)
1950 if ((volume = texture->sub_resources[i].u.volume))
1952 TRACE("volume %p.\n", volume);
1954 wined3d_volume_cleanup(volume);
1955 volume->resource.parent_ops->wined3d_object_destroyed(volume->resource.parent);
1958 HeapFree(GetProcessHeap(), 0, texture->sub_resources[0].u.volume);
1961 static const struct wined3d_texture_ops texture3d_ops =
1963 texture3d_upload_data,
1964 texture3d_load_location,
1965 texture3d_prepare_texture,
1966 texture3d_cleanup_sub_resources,
1969 BOOL wined3d_texture_check_block_align(const struct wined3d_texture *texture,
1970 unsigned int level, const struct wined3d_box *box)
1972 const struct wined3d_format *format = texture->resource.format;
1973 unsigned int height = wined3d_texture_get_level_height(texture, level);
1974 unsigned int width = wined3d_texture_get_level_width(texture, level);
1975 unsigned int width_mask, height_mask;
1977 if ((box->left >= box->right)
1978 || (box->top >= box->bottom)
1979 || (box->right > width)
1980 || (box->bottom > height))
1981 return FALSE;
1983 /* This assumes power of two block sizes, but NPOT block sizes would be
1984 * silly anyway.
1986 * This also assumes that the format's block depth is 1. */
1987 width_mask = format->block_width - 1;
1988 height_mask = format->block_height - 1;
1990 if ((box->left & width_mask) || (box->top & height_mask)
1991 || (box->right & width_mask && box->right != width)
1992 || (box->bottom & height_mask && box->bottom != height))
1993 return FALSE;
1995 return TRUE;
1998 static HRESULT volumetexture_init(struct wined3d_texture *texture, const struct wined3d_resource_desc *desc,
1999 UINT levels, struct wined3d_device *device, void *parent, const struct wined3d_parent_ops *parent_ops)
2001 struct wined3d_device_parent *device_parent = device->device_parent;
2002 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2003 struct wined3d_resource_desc volume_desc;
2004 struct wined3d_volume *volumes;
2005 unsigned int i;
2006 HRESULT hr;
2008 /* TODO: It should only be possible to create textures for formats
2009 * that are reported as supported. */
2010 if (WINED3DFMT_UNKNOWN >= desc->format)
2012 WARN("(%p) : Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n", texture);
2013 return WINED3DERR_INVALIDCALL;
2016 if (!gl_info->supported[EXT_TEXTURE3D])
2018 WARN("(%p) : Texture cannot be created - no volume texture support.\n", texture);
2019 return WINED3DERR_INVALIDCALL;
2022 /* Calculate levels for mip mapping. */
2023 if (desc->usage & WINED3DUSAGE_AUTOGENMIPMAP)
2025 if (!gl_info->supported[SGIS_GENERATE_MIPMAP])
2027 WARN("No mipmap generation support, returning D3DERR_INVALIDCALL.\n");
2028 return WINED3DERR_INVALIDCALL;
2031 if (levels != 1)
2033 WARN("WINED3DUSAGE_AUTOGENMIPMAP is set, and level count != 1, returning D3DERR_INVALIDCALL.\n");
2034 return WINED3DERR_INVALIDCALL;
2038 if (desc->usage & WINED3DUSAGE_DYNAMIC && (desc->pool == WINED3D_POOL_MANAGED
2039 || desc->pool == WINED3D_POOL_SCRATCH))
2041 WARN("Attempted to create a DYNAMIC texture in pool %s.\n", debug_d3dpool(desc->pool));
2042 return WINED3DERR_INVALIDCALL;
2045 if (!gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO])
2047 UINT pow2_w, pow2_h, pow2_d;
2048 pow2_w = 1;
2049 while (pow2_w < desc->width)
2050 pow2_w <<= 1;
2051 pow2_h = 1;
2052 while (pow2_h < desc->height)
2053 pow2_h <<= 1;
2054 pow2_d = 1;
2055 while (pow2_d < desc->depth)
2056 pow2_d <<= 1;
2058 if (pow2_w != desc->width || pow2_h != desc->height || pow2_d != desc->depth)
2060 if (desc->pool == WINED3D_POOL_SCRATCH)
2062 WARN("Creating a scratch NPOT volume texture despite lack of HW support.\n");
2064 else
2066 WARN("Attempted to create a NPOT volume texture (%u, %u, %u) without GL support.\n",
2067 desc->width, desc->height, desc->depth);
2068 return WINED3DERR_INVALIDCALL;
2073 if (FAILED(hr = wined3d_texture_init(texture, &texture3d_ops, 1, levels, desc,
2074 0, device, parent, parent_ops, &texture_resource_ops)))
2076 WARN("Failed to initialize texture, returning %#x.\n", hr);
2077 return hr;
2080 texture->pow2_matrix[0] = 1.0f;
2081 texture->pow2_matrix[5] = 1.0f;
2082 texture->pow2_matrix[10] = 1.0f;
2083 texture->pow2_matrix[15] = 1.0f;
2084 texture->target = GL_TEXTURE_3D;
2086 if (wined3d_texture_use_pbo(texture, gl_info))
2087 texture->resource.map_binding = WINED3D_LOCATION_BUFFER;
2089 if (!(volumes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*volumes) * levels)))
2091 wined3d_texture_cleanup(texture);
2092 return E_OUTOFMEMORY;
2095 /* Generate all the surfaces. */
2096 volume_desc = *desc;
2097 volume_desc.resource_type = WINED3D_RTYPE_VOLUME;
2098 for (i = 0; i < texture->level_count; ++i)
2100 struct wined3d_volume *volume;
2102 volume = &volumes[i];
2103 if (FAILED(hr = wined3d_volume_init(volume, texture, &volume_desc, i)))
2105 WARN("Failed to initialize volume, returning %#x.\n", hr);
2106 wined3d_texture_cleanup(texture);
2107 if (!i)
2108 HeapFree(GetProcessHeap(), 0, volumes);
2109 return hr;
2112 if (FAILED(hr = device_parent->ops->volume_created(device_parent,
2113 texture, i, &parent, &parent_ops)))
2115 WARN("Failed to create volume parent, hr %#x.\n", hr);
2116 wined3d_volume_cleanup(volume);
2117 wined3d_texture_cleanup(texture);
2118 return hr;
2121 TRACE("parent %p, parent_ops %p.\n", parent, parent_ops);
2123 volume->resource.parent = parent;
2124 volume->resource.parent_ops = parent_ops;
2125 texture->sub_resources[i].resource = &volume->resource;
2126 texture->sub_resources[i].u.volume = volume;
2127 TRACE("Created volume level %u @ %p.\n", i, volume);
2129 /* Calculate the next mipmap level. */
2130 volume_desc.width = max(1, volume_desc.width >> 1);
2131 volume_desc.height = max(1, volume_desc.height >> 1);
2132 volume_desc.depth = max(1, volume_desc.depth >> 1);
2135 return WINED3D_OK;
2138 HRESULT CDECL wined3d_texture_blt(struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2139 const RECT *dst_rect, struct wined3d_texture *src_texture, unsigned int src_sub_resource_idx,
2140 const RECT *src_rect, DWORD flags, const struct wined3d_blt_fx *fx, enum wined3d_texture_filter_type filter)
2142 struct wined3d_texture_sub_resource *dst_resource, *src_resource = NULL;
2144 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2145 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2146 dst_texture, dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), src_texture,
2147 src_sub_resource_idx, wine_dbgstr_rect(src_rect), flags, fx, debug_d3dtexturefiltertype(filter));
2149 if (!(dst_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx))
2150 || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2151 return WINED3DERR_INVALIDCALL;
2153 if (src_texture)
2155 if (!(src_resource = wined3d_texture_get_sub_resource(src_texture, src_sub_resource_idx))
2156 || src_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2157 return WINED3DERR_INVALIDCALL;
2160 return wined3d_surface_blt(dst_resource->u.surface, dst_rect,
2161 src_resource ? src_resource->u.surface : NULL, src_rect, flags, fx, filter);
2164 HRESULT CDECL wined3d_texture_get_overlay_position(const struct wined3d_texture *texture,
2165 unsigned int sub_resource_idx, LONG *x, LONG *y)
2167 struct wined3d_surface *surface;
2169 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture, sub_resource_idx, x, y);
2171 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2172 || sub_resource_idx >= texture->level_count * texture->layer_count)
2174 WARN("Invalid sub-resource specified.\n");
2175 return WINEDDERR_NOTAOVERLAYSURFACE;
2178 surface = texture->sub_resources[sub_resource_idx].u.surface;
2179 if (!surface->overlay_dest)
2181 TRACE("Overlay not visible.\n");
2182 *x = 0;
2183 *y = 0;
2184 return WINEDDERR_OVERLAYNOTVISIBLE;
2187 *x = surface->overlay_destrect.left;
2188 *y = surface->overlay_destrect.top;
2190 TRACE("Returning position %d, %d.\n", *x, *y);
2192 return WINED3D_OK;
2195 HRESULT CDECL wined3d_texture_set_overlay_position(struct wined3d_texture *texture,
2196 unsigned int sub_resource_idx, LONG x, LONG y)
2198 struct wined3d_texture_sub_resource *sub_resource;
2199 struct wined3d_surface *surface;
2200 LONG w, h;
2202 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture, sub_resource_idx, x, y);
2204 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2205 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2207 WARN("Invalid sub-resource specified.\n");
2208 return WINEDDERR_NOTAOVERLAYSURFACE;
2211 surface = sub_resource->u.surface;
2212 w = surface->overlay_destrect.right - surface->overlay_destrect.left;
2213 h = surface->overlay_destrect.bottom - surface->overlay_destrect.top;
2214 surface->overlay_destrect.left = x;
2215 surface->overlay_destrect.top = y;
2216 surface->overlay_destrect.right = x + w;
2217 surface->overlay_destrect.bottom = y + h;
2219 return WINED3D_OK;
2222 HRESULT CDECL wined3d_texture_update_overlay(struct wined3d_texture *texture, unsigned int sub_resource_idx,
2223 const RECT *src_rect, struct wined3d_texture *dst_texture, unsigned int dst_sub_resource_idx,
2224 const RECT *dst_rect, DWORD flags)
2226 struct wined3d_texture_sub_resource *sub_resource, *dst_sub_resource;
2227 struct wined3d_surface *surface, *dst_surface;
2229 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2230 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2231 texture, sub_resource_idx, wine_dbgstr_rect(src_rect), dst_texture,
2232 dst_sub_resource_idx, wine_dbgstr_rect(dst_rect), flags);
2234 if (!(texture->resource.usage & WINED3DUSAGE_OVERLAY) || texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2235 || !(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2237 WARN("Invalid sub-resource specified.\n");
2238 return WINEDDERR_NOTAOVERLAYSURFACE;
2241 if (!dst_texture || dst_texture->resource.type != WINED3D_RTYPE_TEXTURE_2D
2242 || !(dst_sub_resource = wined3d_texture_get_sub_resource(dst_texture, dst_sub_resource_idx)))
2244 WARN("Invalid destination sub-resource specified.\n");
2245 return WINED3DERR_INVALIDCALL;
2248 surface = sub_resource->u.surface;
2249 if (src_rect)
2250 surface->overlay_srcrect = *src_rect;
2251 else
2252 SetRect(&surface->overlay_srcrect, 0, 0,
2253 wined3d_texture_get_level_width(texture, surface->texture_level),
2254 wined3d_texture_get_level_height(texture, surface->texture_level));
2256 dst_surface = dst_sub_resource->u.surface;
2257 if (dst_rect)
2258 surface->overlay_destrect = *dst_rect;
2259 else
2260 SetRect(&surface->overlay_destrect, 0, 0,
2261 wined3d_texture_get_level_width(dst_texture, dst_surface->texture_level),
2262 wined3d_texture_get_level_height(dst_texture, dst_surface->texture_level));
2264 if (surface->overlay_dest && (surface->overlay_dest != dst_surface || flags & WINEDDOVER_HIDE))
2266 surface->overlay_dest = NULL;
2267 list_remove(&surface->overlay_entry);
2270 if (flags & WINEDDOVER_SHOW)
2272 if (surface->overlay_dest != dst_surface)
2274 surface->overlay_dest = dst_surface;
2275 list_add_tail(&dst_surface->overlays, &surface->overlay_entry);
2278 else if (flags & WINEDDOVER_HIDE)
2280 /* Tests show that the rectangles are erased on hide. */
2281 SetRectEmpty(&surface->overlay_srcrect);
2282 SetRectEmpty(&surface->overlay_destrect);
2283 surface->overlay_dest = NULL;
2286 return WINED3D_OK;
2289 void * CDECL wined3d_texture_get_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx)
2291 unsigned int sub_count = texture->level_count * texture->layer_count;
2293 TRACE("texture %p, sub_resource_idx %u.\n", texture, sub_resource_idx);
2295 if (sub_resource_idx >= sub_count)
2297 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2298 return NULL;
2301 return texture->sub_resources[sub_resource_idx].resource->parent;
2304 void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture,
2305 unsigned int sub_resource_idx, void *parent)
2307 unsigned int sub_count = texture->level_count * texture->layer_count;
2309 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture, sub_resource_idx, parent);
2311 if (sub_resource_idx >= sub_count)
2313 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2314 return;
2317 texture->sub_resources[sub_resource_idx].resource->parent = parent;
2320 HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture,
2321 unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
2323 unsigned int sub_count = texture->level_count * texture->layer_count;
2324 const struct wined3d_resource *resource;
2325 unsigned int level_idx;
2327 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture, sub_resource_idx, desc);
2329 if (sub_resource_idx >= sub_count)
2331 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx, sub_count);
2332 return WINED3DERR_INVALIDCALL;
2335 resource = &texture->resource;
2336 desc->format = resource->format->id;
2337 desc->multisample_type = resource->multisample_type;
2338 desc->multisample_quality = resource->multisample_quality;
2339 desc->usage = resource->usage;
2340 desc->pool = resource->pool;
2342 level_idx = sub_resource_idx % texture->level_count;
2343 desc->width = wined3d_texture_get_level_width(texture, level_idx);
2344 desc->height = wined3d_texture_get_level_height(texture, level_idx);
2345 desc->depth = wined3d_texture_get_level_depth(texture, level_idx);
2346 desc->size = texture->sub_resources[sub_resource_idx].resource->size;
2348 return WINED3D_OK;
2351 HRESULT CDECL wined3d_texture_create(struct wined3d_device *device, const struct wined3d_resource_desc *desc,
2352 UINT level_count, DWORD flags, const struct wined3d_sub_resource_data *data, void *parent,
2353 const struct wined3d_parent_ops *parent_ops, struct wined3d_texture **texture)
2355 unsigned int layer_count = desc->usage & WINED3DUSAGE_LEGACY_CUBEMAP ? 6 : 1;
2356 struct wined3d_texture *object;
2357 HRESULT hr;
2359 TRACE("device %p, desc %p, level_count %u, flags %#x, data %p, parent %p, parent_ops %p, texture %p.\n",
2360 device, desc, level_count, flags, data, parent, parent_ops, texture);
2362 if (!level_count)
2364 WARN("Invalid level count.\n");
2365 return WINED3DERR_INVALIDCALL;
2368 if (desc->multisample_type != WINED3D_MULTISAMPLE_NONE)
2370 const struct wined3d_format *format = wined3d_get_format(&device->adapter->gl_info, desc->format);
2372 if (desc->multisample_type == WINED3D_MULTISAMPLE_NON_MASKABLE
2373 && desc->multisample_quality >= wined3d_popcount(format->multisample_types))
2375 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
2376 desc->multisample_quality);
2377 return WINED3DERR_NOTAVAILABLE;
2379 if (desc->multisample_type != WINED3D_MULTISAMPLE_NON_MASKABLE
2380 && (!(format->multisample_types & 1u << (desc->multisample_type - 1))
2381 || desc->multisample_quality))
2383 WARN("Unsupported multisample type %u quality %u requested.\n", desc->multisample_type,
2384 desc->multisample_quality);
2385 return WINED3DERR_NOTAVAILABLE;
2389 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2390 FIELD_OFFSET(struct wined3d_texture, sub_resources[level_count * layer_count]))))
2391 return E_OUTOFMEMORY;
2393 switch (desc->resource_type)
2395 case WINED3D_RTYPE_TEXTURE_2D:
2396 hr = texture_init(object, desc, layer_count, level_count, flags, device, parent, parent_ops);
2397 break;
2399 case WINED3D_RTYPE_TEXTURE_3D:
2400 hr = volumetexture_init(object, desc, level_count, device, parent, parent_ops);
2401 break;
2403 default:
2404 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc->resource_type));
2405 hr = WINED3DERR_INVALIDCALL;
2406 break;
2409 if (FAILED(hr))
2411 WARN("Failed to initialize texture, returning %#x.\n", hr);
2412 HeapFree(GetProcessHeap(), 0, object);
2413 return hr;
2416 /* FIXME: We'd like to avoid ever allocating system memory for the texture
2417 * in this case. */
2418 if (data && FAILED(hr = wined3d_texture_upload_data(object, data)))
2420 wined3d_texture_cleanup(object);
2421 HeapFree(GetProcessHeap(), 0, object);
2422 return hr;
2425 TRACE("Created texture %p.\n", object);
2426 *texture = object;
2428 return WINED3D_OK;
2431 HRESULT CDECL wined3d_texture_get_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC *dc)
2433 struct wined3d_device *device = texture->resource.device;
2434 struct wined3d_texture_sub_resource *sub_resource;
2435 struct wined3d_context *context = NULL;
2436 struct wined3d_surface *surface;
2437 HRESULT hr = WINED3D_OK;
2439 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2441 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2442 return WINED3DERR_INVALIDCALL;
2444 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2446 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2447 return WINED3DERR_INVALIDCALL;
2450 surface = sub_resource->u.surface;
2452 if (texture->resource.map_count && !(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2453 return WINED3DERR_INVALIDCALL;
2455 if (device->d3d_initialized)
2456 context = context_acquire(device, NULL);
2458 surface_load_location(surface, context, texture->resource.map_binding);
2459 wined3d_texture_invalidate_location(texture, sub_resource_idx, ~texture->resource.map_binding);
2461 if (!surface->dc)
2462 hr = wined3d_surface_create_dc(surface);
2463 if (context)
2464 context_release(context);
2465 if (FAILED(hr))
2466 return WINED3DERR_INVALIDCALL;
2468 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2469 texture->flags |= WINED3D_TEXTURE_DC_IN_USE;
2470 ++texture->resource.map_count;
2471 ++sub_resource->map_count;
2473 *dc = surface->dc;
2474 TRACE("Returning dc %p.\n", *dc);
2476 return hr;
2479 HRESULT CDECL wined3d_texture_release_dc(struct wined3d_texture *texture, unsigned int sub_resource_idx, HDC dc)
2481 struct wined3d_device *device = texture->resource.device;
2482 struct wined3d_texture_sub_resource *sub_resource;
2483 struct wined3d_surface *surface;
2485 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture, sub_resource_idx, dc);
2487 if (!(sub_resource = wined3d_texture_get_sub_resource(texture, sub_resource_idx)))
2488 return WINED3DERR_INVALIDCALL;
2490 if (texture->resource.type != WINED3D_RTYPE_TEXTURE_2D)
2492 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture->resource.type));
2493 return WINED3DERR_INVALIDCALL;
2496 surface = sub_resource->u.surface;
2498 if (!(texture->flags & (WINED3D_TEXTURE_GET_DC_LENIENT | WINED3D_TEXTURE_DC_IN_USE)))
2499 return WINED3DERR_INVALIDCALL;
2501 if (surface->dc != dc)
2503 WARN("Application tries to release invalid DC %p, surface DC is %p.\n", dc, surface->dc);
2504 return WINED3DERR_INVALIDCALL;
2507 if (!(texture->resource.usage & WINED3DUSAGE_OWNDC) && !(device->wined3d->flags & WINED3D_NO3D))
2508 wined3d_surface_destroy_dc(surface);
2510 --sub_resource->map_count;
2511 if (!--texture->resource.map_count && texture->update_map_binding)
2512 wined3d_texture_update_map_binding(texture);
2513 if (!(texture->flags & WINED3D_TEXTURE_GET_DC_LENIENT))
2514 texture->flags &= ~WINED3D_TEXTURE_DC_IN_USE;
2516 return WINED3D_OK;