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
24 #include "wine/port.h"
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
28 WINE_DECLARE_DEBUG_CHANNEL(d3d_perf
);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
31 #define WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD 50
33 struct wined3d_texture_idx
35 struct wined3d_texture
*texture
;
36 unsigned int sub_resource_idx
;
39 static BOOL
wined3d_texture_use_pbo(const struct wined3d_texture
*texture
, const struct wined3d_gl_info
*gl_info
)
41 return !(texture
->resource
.access
& WINED3D_RESOURCE_ACCESS_CPU
)
42 && texture
->resource
.usage
& WINED3DUSAGE_DYNAMIC
43 && gl_info
->supported
[ARB_PIXEL_BUFFER_OBJECT
]
44 && !texture
->resource
.format
->conv_byte_count
45 && !(texture
->flags
& (WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_COND_NP2_EMULATED
));
48 static BOOL
wined3d_texture_use_immutable_storage(const struct wined3d_texture
*texture
,
49 const struct wined3d_gl_info
*gl_info
)
51 /* We don't expect to create texture views for textures with height-scaled formats.
52 * Besides, ARB_texture_storage doesn't allow specifying exact sizes for all levels. */
53 return gl_info
->supported
[ARB_TEXTURE_STORAGE
]
54 && !(texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
);
57 /* Front buffer coordinates are always full screen coordinates, but our GL
58 * drawable is limited to the window's client area. The sysmem and texture
59 * copies do have the full screen size. Note that GL has a bottom-left
60 * origin, while D3D has a top-left origin. */
61 void wined3d_texture_translate_drawable_coords(const struct wined3d_texture
*texture
, HWND window
, RECT
*rect
)
63 unsigned int drawable_height
;
64 POINT offset
= {0, 0};
67 if (!texture
->swapchain
)
70 if (texture
== texture
->swapchain
->front_buffer
)
72 ScreenToClient(window
, &offset
);
73 OffsetRect(rect
, offset
.x
, offset
.y
);
76 GetClientRect(window
, &windowsize
);
77 drawable_height
= windowsize
.bottom
- windowsize
.top
;
79 rect
->top
= drawable_height
- rect
->top
;
80 rect
->bottom
= drawable_height
- rect
->bottom
;
83 GLenum
wined3d_texture_get_gl_buffer(const struct wined3d_texture
*texture
)
85 const struct wined3d_swapchain
*swapchain
= texture
->swapchain
;
87 TRACE("texture %p.\n", texture
);
91 ERR("Texture %p is not part of a swapchain.\n", texture
);
95 if (texture
== swapchain
->front_buffer
)
97 TRACE("Returning GL_FRONT.\n");
101 if (texture
== swapchain
->back_buffers
[0])
103 TRACE("Returning GL_BACK.\n");
107 FIXME("Higher back buffer, returning GL_BACK.\n");
111 static DWORD
wined3d_resource_access_from_location(DWORD location
)
115 case WINED3D_LOCATION_DISCARDED
:
118 case WINED3D_LOCATION_SYSMEM
:
119 case WINED3D_LOCATION_USER_MEMORY
:
120 return WINED3D_RESOURCE_ACCESS_CPU
;
122 case WINED3D_LOCATION_BUFFER
:
123 case WINED3D_LOCATION_DRAWABLE
:
124 case WINED3D_LOCATION_TEXTURE_RGB
:
125 case WINED3D_LOCATION_TEXTURE_SRGB
:
126 case WINED3D_LOCATION_RB_MULTISAMPLE
:
127 case WINED3D_LOCATION_RB_RESOLVED
:
128 return WINED3D_RESOURCE_ACCESS_GPU
;
131 FIXME("Unhandled location %#x.\n", location
);
136 static void wined3d_texture_evict_sysmem(struct wined3d_texture
*texture
)
138 struct wined3d_texture_sub_resource
*sub_resource
;
139 unsigned int i
, sub_count
;
141 if (texture
->flags
& (WINED3D_TEXTURE_CONVERTED
| WINED3D_TEXTURE_PIN_SYSMEM
)
142 || texture
->download_count
> WINED3D_TEXTURE_DYNAMIC_MAP_THRESHOLD
)
144 TRACE("Not evicting system memory for texture %p.\n", texture
);
148 TRACE("Evicting system memory for texture %p.\n", texture
);
150 sub_count
= texture
->level_count
* texture
->layer_count
;
151 for (i
= 0; i
< sub_count
; ++i
)
153 sub_resource
= &texture
->sub_resources
[i
];
154 if (sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
155 ERR("WINED3D_LOCATION_SYSMEM is the only location for sub-resource %u of texture %p.\n",
157 sub_resource
->locations
&= ~WINED3D_LOCATION_SYSMEM
;
159 wined3d_resource_free_sysmem(&texture
->resource
);
162 void wined3d_texture_validate_location(struct wined3d_texture
*texture
,
163 unsigned int sub_resource_idx
, DWORD location
)
165 struct wined3d_texture_sub_resource
*sub_resource
;
166 DWORD previous_locations
;
168 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
169 texture
, sub_resource_idx
, wined3d_debug_location(location
));
171 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
172 previous_locations
= sub_resource
->locations
;
173 sub_resource
->locations
|= location
;
174 if (previous_locations
== WINED3D_LOCATION_SYSMEM
&& location
!= WINED3D_LOCATION_SYSMEM
175 && !--texture
->sysmem_count
)
176 wined3d_texture_evict_sysmem(texture
);
178 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
181 static void wined3d_texture_set_dirty(struct wined3d_texture
*texture
)
183 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_VALID
| WINED3D_TEXTURE_SRGB_VALID
);
186 void wined3d_texture_invalidate_location(struct wined3d_texture
*texture
,
187 unsigned int sub_resource_idx
, DWORD location
)
189 struct wined3d_texture_sub_resource
*sub_resource
;
190 DWORD previous_locations
;
192 TRACE("texture %p, sub_resource_idx %u, location %s.\n",
193 texture
, sub_resource_idx
, wined3d_debug_location(location
));
195 if (location
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
196 wined3d_texture_set_dirty(texture
);
198 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
199 previous_locations
= sub_resource
->locations
;
200 sub_resource
->locations
&= ~location
;
201 if (previous_locations
!= WINED3D_LOCATION_SYSMEM
&& sub_resource
->locations
== WINED3D_LOCATION_SYSMEM
)
202 ++texture
->sysmem_count
;
204 TRACE("New locations flags are %s.\n", wined3d_debug_location(sub_resource
->locations
));
206 if (!sub_resource
->locations
)
207 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
208 sub_resource_idx
, texture
);
211 static BOOL
wined3d_texture_copy_sysmem_location(struct wined3d_texture
*texture
,
212 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
214 unsigned int size
= texture
->sub_resources
[sub_resource_idx
].size
;
215 struct wined3d_device
*device
= texture
->resource
.device
;
216 const struct wined3d_gl_info
*gl_info
;
217 struct wined3d_bo_address dst
, src
;
219 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
222 wined3d_texture_get_memory(texture
, sub_resource_idx
, &dst
, location
);
223 wined3d_texture_get_memory(texture
, sub_resource_idx
, &src
,
224 texture
->sub_resources
[sub_resource_idx
].locations
);
226 if (dst
.buffer_object
)
228 context
= context_acquire(device
, NULL
, 0);
229 gl_info
= context
->gl_info
;
230 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, dst
.buffer_object
));
231 GL_EXTCALL(glBufferSubData(GL_PIXEL_UNPACK_BUFFER
, 0, size
, src
.addr
));
232 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
233 checkGLcall("PBO upload");
234 context_release(context
);
238 if (src
.buffer_object
)
240 context
= context_acquire(device
, NULL
, 0);
241 gl_info
= context
->gl_info
;
242 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, src
.buffer_object
));
243 GL_EXTCALL(glGetBufferSubData(GL_PIXEL_PACK_BUFFER
, 0, size
, dst
.addr
));
244 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
245 checkGLcall("PBO download");
246 context_release(context
);
250 memcpy(dst
.addr
, src
.addr
, size
);
254 /* Context activation is done by the caller. Context may be NULL in
255 * WINED3D_NO3D mode. */
256 BOOL
wined3d_texture_load_location(struct wined3d_texture
*texture
,
257 unsigned int sub_resource_idx
, struct wined3d_context
*context
, DWORD location
)
259 static const DWORD sysmem_locations
= WINED3D_LOCATION_SYSMEM
| WINED3D_LOCATION_USER_MEMORY
260 | WINED3D_LOCATION_BUFFER
;
261 DWORD current
= texture
->sub_resources
[sub_resource_idx
].locations
;
264 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
265 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
267 TRACE("Current resource location %s.\n", wined3d_debug_location(current
));
269 if (current
& location
)
271 TRACE("Location %s is already up to date.\n", wined3d_debug_location(location
));
277 DWORD required_access
= wined3d_resource_access_from_location(location
);
278 if ((texture
->resource
.access
& required_access
) != required_access
)
279 WARN("Operation requires %#x access, but texture only has %#x.\n",
280 required_access
, texture
->resource
.access
);
283 if (current
& WINED3D_LOCATION_DISCARDED
)
285 TRACE("Sub-resource previously discarded, nothing to do.\n");
286 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
288 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
289 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
295 ERR("Sub-resource %u of texture %p does not have any up to date location.\n",
296 sub_resource_idx
, texture
);
297 wined3d_texture_validate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_DISCARDED
);
298 return wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
301 if ((location
& sysmem_locations
) && (current
& sysmem_locations
))
302 ret
= wined3d_texture_copy_sysmem_location(texture
, sub_resource_idx
, context
, location
);
304 ret
= texture
->texture_ops
->texture_load_location(texture
, sub_resource_idx
, context
, location
);
307 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
312 void wined3d_texture_get_memory(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
313 struct wined3d_bo_address
*data
, DWORD locations
)
315 struct wined3d_texture_sub_resource
*sub_resource
;
317 TRACE("texture %p, sub_resource_idx %u, data %p, locations %s.\n",
318 texture
, sub_resource_idx
, data
, wined3d_debug_location(locations
));
320 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
321 if (locations
& WINED3D_LOCATION_BUFFER
)
324 data
->buffer_object
= sub_resource
->buffer_object
;
327 if (locations
& WINED3D_LOCATION_USER_MEMORY
)
329 data
->addr
= texture
->user_memory
;
330 data
->buffer_object
= 0;
333 if (locations
& WINED3D_LOCATION_SYSMEM
)
335 data
->addr
= texture
->resource
.heap_memory
;
336 data
->addr
+= sub_resource
->offset
;
337 data
->buffer_object
= 0;
341 ERR("Unexpected locations %s.\n", wined3d_debug_location(locations
));
343 data
->buffer_object
= 0;
346 /* Context activation is done by the caller. */
347 static void wined3d_texture_remove_buffer_object(struct wined3d_texture
*texture
,
348 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
350 GLuint
*buffer_object
= &texture
->sub_resources
[sub_resource_idx
].buffer_object
;
352 GL_EXTCALL(glDeleteBuffers(1, buffer_object
));
353 checkGLcall("glDeleteBuffers");
355 TRACE("Deleted buffer object %u for texture %p, sub-resource %u.\n",
356 *buffer_object
, texture
, sub_resource_idx
);
358 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, WINED3D_LOCATION_BUFFER
);
362 static void wined3d_texture_update_map_binding(struct wined3d_texture
*texture
)
364 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
365 const struct wined3d_device
*device
= texture
->resource
.device
;
366 DWORD map_binding
= texture
->update_map_binding
;
367 struct wined3d_context
*context
= NULL
;
370 if (device
->d3d_initialized
)
371 context
= context_acquire(device
, NULL
, 0);
373 for (i
= 0; i
< sub_count
; ++i
)
375 if (texture
->sub_resources
[i
].locations
== texture
->resource
.map_binding
376 && !wined3d_texture_load_location(texture
, i
, context
, map_binding
))
377 ERR("Failed to load location %s.\n", wined3d_debug_location(map_binding
));
378 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
)
379 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
383 context_release(context
);
385 texture
->resource
.map_binding
= map_binding
;
386 texture
->update_map_binding
= 0;
389 void wined3d_texture_set_map_binding(struct wined3d_texture
*texture
, DWORD map_binding
)
391 texture
->update_map_binding
= map_binding
;
392 if (!texture
->resource
.map_count
)
393 wined3d_texture_update_map_binding(texture
);
396 /* A GL context is provided by the caller */
397 static void gltexture_delete(struct wined3d_device
*device
, const struct wined3d_gl_info
*gl_info
,
398 struct gl_texture
*tex
)
400 context_gl_resource_released(device
, tex
->name
, FALSE
);
401 gl_info
->gl_ops
.gl
.p_glDeleteTextures(1, &tex
->name
);
405 static unsigned int wined3d_texture_get_gl_sample_count(const struct wined3d_texture
*texture
)
407 const struct wined3d_format
*format
= texture
->resource
.format
;
409 /* TODO: NVIDIA expose their Coverage Sample Anti-Aliasing (CSAA)
410 * feature through type == MULTISAMPLE_XX and quality != 0. This could
411 * be mapped to GL_NV_framebuffer_multisample_coverage.
413 * AMD have a similar feature called Enhanced Quality Anti-Aliasing
414 * (EQAA), but it does not have an equivalent OpenGL extension. */
416 /* We advertise as many WINED3D_MULTISAMPLE_NON_MASKABLE quality
417 * levels as the count of advertised multisample types for the texture
419 if (texture
->resource
.multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
)
421 unsigned int i
, count
= 0;
423 for (i
= 0; i
< sizeof(format
->multisample_types
) * CHAR_BIT
; ++i
)
425 if (format
->multisample_types
& 1u << i
)
427 if (texture
->resource
.multisample_quality
== count
++)
434 return texture
->resource
.multisample_type
;
437 /* Context activation is done by the caller. */
438 /* The caller is responsible for binding the correct texture. */
439 static void wined3d_texture_allocate_gl_mutable_storage(struct wined3d_texture
*texture
,
440 GLenum gl_internal_format
, const struct wined3d_format
*format
,
441 const struct wined3d_gl_info
*gl_info
)
443 unsigned int level
, level_count
, layer
, layer_count
;
444 GLsizei width
, height
, depth
;
447 level_count
= texture
->level_count
;
448 layer_count
= texture
->target
== GL_TEXTURE_2D_ARRAY
? 1 : texture
->layer_count
;
450 for (layer
= 0; layer
< layer_count
; ++layer
)
452 target
= wined3d_texture_get_sub_resource_target(texture
, layer
* level_count
);
454 for (level
= 0; level
< level_count
; ++level
)
456 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
457 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
458 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_HEIGHT_SCALE
)
460 height
*= format
->height_scale
.numerator
;
461 height
/= format
->height_scale
.denominator
;
464 TRACE("texture %p, layer %u, level %u, target %#x, width %u, height %u.\n",
465 texture
, layer
, level
, target
, width
, height
);
467 if (target
== GL_TEXTURE_3D
|| target
== GL_TEXTURE_2D_ARRAY
)
469 depth
= wined3d_texture_get_level_depth(texture
, level
);
470 GL_EXTCALL(glTexImage3D(target
, level
, gl_internal_format
, width
, height
,
471 target
== GL_TEXTURE_2D_ARRAY
? texture
->layer_count
: depth
, 0,
472 format
->glFormat
, format
->glType
, NULL
));
473 checkGLcall("glTexImage3D");
477 gl_info
->gl_ops
.gl
.p_glTexImage2D(target
, level
, gl_internal_format
,
478 width
, height
, 0, format
->glFormat
, format
->glType
, NULL
);
479 checkGLcall("glTexImage2D");
485 /* Context activation is done by the caller. */
486 /* The caller is responsible for binding the correct texture. */
487 static void wined3d_texture_allocate_gl_immutable_storage(struct wined3d_texture
*texture
,
488 GLenum gl_internal_format
, const struct wined3d_gl_info
*gl_info
)
490 unsigned int samples
= wined3d_texture_get_gl_sample_count(texture
);
491 GLsizei height
= wined3d_texture_get_level_pow2_height(texture
, 0);
492 GLsizei width
= wined3d_texture_get_level_pow2_width(texture
, 0);
494 switch (texture
->target
)
497 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
498 gl_internal_format
, width
, height
, wined3d_texture_get_level_depth(texture
, 0)));
500 case GL_TEXTURE_2D_ARRAY
:
501 GL_EXTCALL(glTexStorage3D(texture
->target
, texture
->level_count
,
502 gl_internal_format
, width
, height
, texture
->layer_count
));
504 case GL_TEXTURE_2D_MULTISAMPLE
:
505 GL_EXTCALL(glTexStorage2DMultisample(texture
->target
, samples
,
506 gl_internal_format
, width
, height
, GL_FALSE
));
508 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
509 GL_EXTCALL(glTexStorage3DMultisample(texture
->target
, samples
,
510 gl_internal_format
, width
, height
, texture
->layer_count
, GL_FALSE
));
513 GL_EXTCALL(glTexStorage2D(texture
->target
, texture
->level_count
,
514 gl_internal_format
, width
, height
));
518 checkGLcall("allocate immutable storage");
521 static void wined3d_texture_unload_gl_texture(struct wined3d_texture
*texture
)
523 struct wined3d_device
*device
= texture
->resource
.device
;
524 const struct wined3d_gl_info
*gl_info
= NULL
;
525 struct wined3d_context
*context
= NULL
;
527 if (texture
->texture_rgb
.name
|| texture
->texture_srgb
.name
528 || texture
->rb_multisample
|| texture
->rb_resolved
)
530 context
= context_acquire(device
, NULL
, 0);
531 gl_info
= context
->gl_info
;
534 if (texture
->texture_rgb
.name
)
535 gltexture_delete(device
, context
->gl_info
, &texture
->texture_rgb
);
537 if (texture
->texture_srgb
.name
)
538 gltexture_delete(device
, context
->gl_info
, &texture
->texture_srgb
);
540 if (texture
->rb_multisample
)
542 TRACE("Deleting multisample renderbuffer %u.\n", texture
->rb_multisample
);
543 context_gl_resource_released(device
, texture
->rb_multisample
, TRUE
);
544 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_multisample
);
545 texture
->rb_multisample
= 0;
548 if (texture
->rb_resolved
)
550 TRACE("Deleting resolved renderbuffer %u.\n", texture
->rb_resolved
);
551 context_gl_resource_released(device
, texture
->rb_resolved
, TRUE
);
552 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &texture
->rb_resolved
);
553 texture
->rb_resolved
= 0;
556 if (context
) context_release(context
);
558 wined3d_texture_set_dirty(texture
);
560 resource_unload(&texture
->resource
);
563 static void wined3d_texture_sub_resources_destroyed(struct wined3d_texture
*texture
)
565 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
566 struct wined3d_texture_sub_resource
*sub_resource
;
569 for (i
= 0; i
< sub_count
; ++i
)
571 sub_resource
= &texture
->sub_resources
[i
];
572 if (sub_resource
->parent
)
574 TRACE("sub-resource %u.\n", i
);
575 sub_resource
->parent_ops
->wined3d_object_destroyed(sub_resource
->parent
);
576 sub_resource
->parent
= NULL
;
581 static void wined3d_texture_create_dc(void *object
)
583 const struct wined3d_texture_idx
*idx
= object
;
584 struct wined3d_context
*context
= NULL
;
585 unsigned int sub_resource_idx
, level
;
586 const struct wined3d_format
*format
;
587 unsigned int row_pitch
, slice_pitch
;
588 struct wined3d_texture
*texture
;
589 struct wined3d_dc_info
*dc_info
;
590 struct wined3d_bo_address data
;
591 D3DKMT_CREATEDCFROMMEMORY desc
;
592 struct wined3d_device
*device
;
595 TRACE("texture %p, sub_resource_idx %u.\n", idx
->texture
, idx
->sub_resource_idx
);
597 texture
= idx
->texture
;
598 sub_resource_idx
= idx
->sub_resource_idx
;
599 level
= sub_resource_idx
% texture
->level_count
;
600 device
= texture
->resource
.device
;
602 format
= texture
->resource
.format
;
603 if (!format
->ddi_format
)
605 WARN("Cannot create a DC for format %s.\n", debug_d3dformat(format
->id
));
609 if (!texture
->dc_info
)
611 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
613 if (!(texture
->dc_info
= heap_calloc(sub_count
, sizeof(*texture
->dc_info
))))
615 ERR("Failed to allocate DC info.\n");
620 if (device
->d3d_initialized
)
621 context
= context_acquire(device
, NULL
, 0);
623 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, texture
->resource
.map_binding
);
624 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~texture
->resource
.map_binding
);
625 wined3d_texture_get_pitch(texture
, level
, &row_pitch
, &slice_pitch
);
626 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
627 desc
.pMemory
= context_map_bo_address(context
, &data
,
628 texture
->sub_resources
[sub_resource_idx
].size
,
629 GL_PIXEL_UNPACK_BUFFER
, WINED3D_MAP_READ
| WINED3D_MAP_WRITE
);
632 context_release(context
);
634 desc
.Format
= format
->ddi_format
;
635 desc
.Width
= wined3d_texture_get_level_width(texture
, level
);
636 desc
.Height
= wined3d_texture_get_level_height(texture
, level
);
637 desc
.Pitch
= row_pitch
;
638 desc
.hDeviceDc
= CreateCompatibleDC(NULL
);
639 desc
.pColorTable
= NULL
;
641 status
= D3DKMTCreateDCFromMemory(&desc
);
642 DeleteDC(desc
.hDeviceDc
);
645 WARN("Failed to create DC, status %#x.\n", status
);
649 dc_info
= &texture
->dc_info
[sub_resource_idx
];
650 dc_info
->dc
= desc
.hDc
;
651 dc_info
->bitmap
= desc
.hBitmap
;
653 TRACE("Created DC %p, bitmap %p for texture %p, %u.\n", dc_info
->dc
, dc_info
->bitmap
, texture
, sub_resource_idx
);
656 static void wined3d_texture_destroy_dc(void *object
)
658 const struct wined3d_texture_idx
*idx
= object
;
659 D3DKMT_DESTROYDCFROMMEMORY destroy_desc
;
660 struct wined3d_context
*context
= NULL
;
661 struct wined3d_texture
*texture
;
662 struct wined3d_dc_info
*dc_info
;
663 struct wined3d_bo_address data
;
664 unsigned int sub_resource_idx
;
665 struct wined3d_device
*device
;
668 texture
= idx
->texture
;
669 sub_resource_idx
= idx
->sub_resource_idx
;
670 device
= texture
->resource
.device
;
671 dc_info
= &texture
->dc_info
[sub_resource_idx
];
675 ERR("Sub-resource {%p, %u} has no DC.\n", texture
, sub_resource_idx
);
679 TRACE("dc %p, bitmap %p.\n", dc_info
->dc
, dc_info
->bitmap
);
681 destroy_desc
.hDc
= dc_info
->dc
;
682 destroy_desc
.hBitmap
= dc_info
->bitmap
;
683 if ((status
= D3DKMTDestroyDCFromMemory(&destroy_desc
)))
684 ERR("Failed to destroy dc, status %#x.\n", status
);
686 dc_info
->bitmap
= NULL
;
688 if (device
->d3d_initialized
)
689 context
= context_acquire(device
, NULL
, 0);
691 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
692 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
695 context_release(context
);
698 static void wined3d_texture_cleanup(struct wined3d_texture
*texture
)
700 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
701 struct wined3d_device
*device
= texture
->resource
.device
;
702 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
703 const struct wined3d_gl_info
*gl_info
= NULL
;
704 struct wined3d_context
*context
= NULL
;
705 struct wined3d_dc_info
*dc_info
;
706 GLuint buffer_object
;
709 TRACE("texture %p.\n", texture
);
711 for (i
= 0; i
< sub_count
; ++i
)
713 if (!(buffer_object
= texture
->sub_resources
[i
].buffer_object
))
716 TRACE("Deleting buffer object %u.\n", buffer_object
);
718 /* We may not be able to get a context in wined3d_texture_cleanup() in
719 * general, but if a buffer object was previously created we can. */
722 context
= context_acquire(device
, NULL
, 0);
723 gl_info
= context
->gl_info
;
726 GL_EXTCALL(glDeleteBuffers(1, &buffer_object
));
729 if (!context
&& !list_empty(&texture
->renderbuffers
))
731 context
= context_acquire(device
, NULL
, 0);
732 gl_info
= context
->gl_info
;
735 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
737 TRACE("Deleting renderbuffer %u.\n", entry
->id
);
738 context_gl_resource_released(device
, entry
->id
, TRUE
);
739 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
744 context_release(context
);
746 if ((dc_info
= texture
->dc_info
))
748 for (i
= 0; i
< sub_count
; ++i
)
752 struct wined3d_texture_idx texture_idx
= {texture
, i
};
754 wined3d_texture_destroy_dc(&texture_idx
);
760 if (texture
->overlay_info
)
762 for (i
= 0; i
< sub_count
; ++i
)
764 struct wined3d_overlay_info
*info
= &texture
->overlay_info
[i
];
765 struct wined3d_overlay_info
*overlay
, *cur
;
767 list_remove(&info
->entry
);
768 LIST_FOR_EACH_ENTRY_SAFE(overlay
, cur
, &info
->overlays
, struct wined3d_overlay_info
, entry
)
770 list_remove(&overlay
->entry
);
773 heap_free(texture
->overlay_info
);
775 wined3d_texture_unload_gl_texture(texture
);
778 void wined3d_texture_set_swapchain(struct wined3d_texture
*texture
, struct wined3d_swapchain
*swapchain
)
780 texture
->swapchain
= swapchain
;
781 wined3d_resource_update_draw_binding(&texture
->resource
);
784 /* Context activation is done by the caller. */
785 void wined3d_texture_bind(struct wined3d_texture
*texture
,
786 struct wined3d_context
*context
, BOOL srgb
)
788 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
789 const struct wined3d_format
*format
= texture
->resource
.format
;
790 const struct color_fixup_desc fixup
= format
->color_fixup
;
791 struct gl_texture
*gl_tex
;
794 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
796 if (!needs_separate_srgb_gl_texture(context
, texture
))
799 /* sRGB mode cache for preload() calls outside drawprim. */
801 texture
->flags
|= WINED3D_TEXTURE_IS_SRGB
;
803 texture
->flags
&= ~WINED3D_TEXTURE_IS_SRGB
;
805 gl_tex
= wined3d_texture_get_gl_texture(texture
, srgb
);
806 target
= texture
->target
;
810 context_bind_texture(context
, target
, gl_tex
->name
);
814 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &gl_tex
->name
);
815 checkGLcall("glGenTextures");
816 TRACE("Generated texture %d.\n", gl_tex
->name
);
820 ERR("Failed to generate a texture name.\n");
824 /* Initialise the state of the texture object to the OpenGL defaults, not
825 * the wined3d defaults. */
826 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_WRAP
;
827 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_WRAP
;
828 gl_tex
->sampler_desc
.address_w
= WINED3D_TADDRESS_WRAP
;
829 memset(gl_tex
->sampler_desc
.border_color
, 0, sizeof(gl_tex
->sampler_desc
.border_color
));
830 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_LINEAR
;
831 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
; /* GL_NEAREST_MIPMAP_LINEAR */
832 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_LINEAR
; /* GL_NEAREST_MIPMAP_LINEAR */
833 gl_tex
->sampler_desc
.lod_bias
= 0.0f
;
834 gl_tex
->sampler_desc
.min_lod
= -1000.0f
;
835 gl_tex
->sampler_desc
.max_lod
= 1000.0f
;
836 gl_tex
->sampler_desc
.max_anisotropy
= 1;
837 gl_tex
->sampler_desc
.compare
= FALSE
;
838 gl_tex
->sampler_desc
.comparison_func
= WINED3D_CMP_LESSEQUAL
;
839 if (context
->gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
840 gl_tex
->sampler_desc
.srgb_decode
= TRUE
;
842 gl_tex
->sampler_desc
.srgb_decode
= srgb
;
843 gl_tex
->base_level
= 0;
844 wined3d_texture_set_dirty(texture
);
846 context_bind_texture(context
, target
, gl_tex
->name
);
848 /* For a new texture we have to set the texture levels after binding the
849 * texture. Beware that texture rectangles do not support mipmapping, but
850 * set the maxmiplevel if we're relying on the partial
851 * GL_ARB_texture_non_power_of_two emulation with texture rectangles.
852 * (I.e., do not care about cond_np2 here, just look for
853 * GL_TEXTURE_RECTANGLE_ARB.) */
854 if (target
!= GL_TEXTURE_RECTANGLE_ARB
)
856 TRACE("Setting GL_TEXTURE_MAX_LEVEL to %u.\n", texture
->level_count
- 1);
857 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_LEVEL
, texture
->level_count
- 1);
858 checkGLcall("glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, texture->level_count)");
861 if (target
== GL_TEXTURE_CUBE_MAP_ARB
)
863 /* Cubemaps are always set to clamp, regardless of the sampler state. */
864 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
865 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
866 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
, GL_CLAMP_TO_EDGE
);
869 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2
)
871 /* Conditinal non power of two textures use a different clamping
872 * default. If we're using the GL_WINE_normalized_texrect partial
873 * driver emulation, we're dealing with a GL_TEXTURE_2D texture which
874 * has the address mode set to repeat - something that prevents us
875 * from hitting the accelerated codepath. Thus manually set the GL
876 * state. The same applies to filtering. Even if the texture has only
877 * one mip level, the default LINEAR_MIPMAP_LINEAR filter causes a SW
878 * fallback on macos. */
879 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
880 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
881 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
882 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
883 checkGLcall("glTexParameteri");
884 gl_tex
->sampler_desc
.address_u
= WINED3D_TADDRESS_CLAMP
;
885 gl_tex
->sampler_desc
.address_v
= WINED3D_TADDRESS_CLAMP
;
886 gl_tex
->sampler_desc
.mag_filter
= WINED3D_TEXF_POINT
;
887 gl_tex
->sampler_desc
.min_filter
= WINED3D_TEXF_POINT
;
888 gl_tex
->sampler_desc
.mip_filter
= WINED3D_TEXF_NONE
;
891 if (gl_info
->supported
[WINED3D_GL_LEGACY_CONTEXT
] && gl_info
->supported
[ARB_DEPTH_TEXTURE
])
893 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_DEPTH_TEXTURE_MODE_ARB
, GL_INTENSITY
);
894 checkGLcall("glTexParameteri(GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY)");
897 if (!is_identity_fixup(fixup
) && can_use_texture_swizzle(gl_info
, format
))
899 static const GLenum swizzle_source
[] =
901 GL_ZERO
, /* CHANNEL_SOURCE_ZERO */
902 GL_ONE
, /* CHANNEL_SOURCE_ONE */
903 GL_RED
, /* CHANNEL_SOURCE_X */
904 GL_GREEN
, /* CHANNEL_SOURCE_Y */
905 GL_BLUE
, /* CHANNEL_SOURCE_Z */
906 GL_ALPHA
, /* CHANNEL_SOURCE_W */
914 swizzle
.x
= swizzle_source
[fixup
.x_source
];
915 swizzle
.y
= swizzle_source
[fixup
.y_source
];
916 swizzle
.z
= swizzle_source
[fixup
.z_source
];
917 swizzle
.w
= swizzle_source
[fixup
.w_source
];
918 gl_info
->gl_ops
.gl
.p_glTexParameteriv(target
, GL_TEXTURE_SWIZZLE_RGBA
, &swizzle
.x
);
919 checkGLcall("glTexParameteriv(GL_TEXTURE_SWIZZLE_RGBA)");
923 /* Context activation is done by the caller. */
924 void wined3d_texture_bind_and_dirtify(struct wined3d_texture
*texture
,
925 struct wined3d_context
*context
, BOOL srgb
)
927 /* We don't need a specific texture unit, but after binding the texture
928 * the current unit is dirty. Read the unit back instead of switching to
929 * 0, this avoids messing around with the state manager's GL states. The
930 * current texture unit should always be a valid one.
932 * To be more specific, this is tricky because we can implicitly be
933 * called from sampler() in state.c. This means we can't touch anything
934 * other than whatever happens to be the currently active texture, or we
935 * would risk marking already applied sampler states dirty again. */
936 if (context
->active_texture
< ARRAY_SIZE(context
->rev_tex_unit_map
))
938 DWORD active_sampler
= context
->rev_tex_unit_map
[context
->active_texture
];
939 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
940 context_invalidate_state(context
, STATE_SAMPLER(active_sampler
));
942 /* FIXME: Ideally we'd only do this when touching a binding that's used by
944 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
945 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
947 wined3d_texture_bind(texture
, context
, srgb
);
950 /* Context activation is done by the caller (state handler). */
951 /* This function relies on the correct texture being bound and loaded. */
952 void wined3d_texture_apply_sampler_desc(struct wined3d_texture
*texture
,
953 const struct wined3d_sampler_desc
*sampler_desc
, const struct wined3d_context
*context
)
955 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
956 GLenum target
= texture
->target
;
957 struct gl_texture
*gl_tex
;
960 TRACE("texture %p, sampler_desc %p, context %p.\n", texture
, sampler_desc
, context
);
962 gl_tex
= wined3d_texture_get_gl_texture(texture
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
964 state
= sampler_desc
->address_u
;
965 if (state
!= gl_tex
->sampler_desc
.address_u
)
967 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_S
,
968 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
969 gl_tex
->sampler_desc
.address_u
= state
;
972 state
= sampler_desc
->address_v
;
973 if (state
!= gl_tex
->sampler_desc
.address_v
)
975 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_T
,
976 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
977 gl_tex
->sampler_desc
.address_v
= state
;
980 state
= sampler_desc
->address_w
;
981 if (state
!= gl_tex
->sampler_desc
.address_w
)
983 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_WRAP_R
,
984 gl_info
->wrap_lookup
[state
- WINED3D_TADDRESS_WRAP
]);
985 gl_tex
->sampler_desc
.address_w
= state
;
988 if (memcmp(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
989 sizeof(gl_tex
->sampler_desc
.border_color
)))
991 gl_info
->gl_ops
.gl
.p_glTexParameterfv(target
, GL_TEXTURE_BORDER_COLOR
, &sampler_desc
->border_color
[0]);
992 memcpy(gl_tex
->sampler_desc
.border_color
, sampler_desc
->border_color
,
993 sizeof(gl_tex
->sampler_desc
.border_color
));
996 state
= sampler_desc
->mag_filter
;
997 if (state
!= gl_tex
->sampler_desc
.mag_filter
)
999 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAG_FILTER
, wined3d_gl_mag_filter(state
));
1000 gl_tex
->sampler_desc
.mag_filter
= state
;
1003 if (sampler_desc
->min_filter
!= gl_tex
->sampler_desc
.min_filter
1004 || sampler_desc
->mip_filter
!= gl_tex
->sampler_desc
.mip_filter
)
1006 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MIN_FILTER
,
1007 wined3d_gl_min_mip_filter(sampler_desc
->min_filter
, sampler_desc
->mip_filter
));
1008 gl_tex
->sampler_desc
.min_filter
= sampler_desc
->min_filter
;
1009 gl_tex
->sampler_desc
.mip_filter
= sampler_desc
->mip_filter
;
1012 state
= sampler_desc
->max_anisotropy
;
1013 if (state
!= gl_tex
->sampler_desc
.max_anisotropy
)
1015 if (gl_info
->supported
[ARB_TEXTURE_FILTER_ANISOTROPIC
])
1016 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_MAX_ANISOTROPY
, state
);
1018 WARN("Anisotropic filtering not supported.\n");
1019 gl_tex
->sampler_desc
.max_anisotropy
= state
;
1022 if (!sampler_desc
->srgb_decode
!= !gl_tex
->sampler_desc
.srgb_decode
1023 && (context
->d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
1024 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
1026 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_SRGB_DECODE_EXT
,
1027 sampler_desc
->srgb_decode
? GL_DECODE_EXT
: GL_SKIP_DECODE_EXT
);
1028 gl_tex
->sampler_desc
.srgb_decode
= sampler_desc
->srgb_decode
;
1031 if (!sampler_desc
->compare
!= !gl_tex
->sampler_desc
.compare
)
1033 if (sampler_desc
->compare
)
1034 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_COMPARE_R_TO_TEXTURE_ARB
);
1036 gl_info
->gl_ops
.gl
.p_glTexParameteri(target
, GL_TEXTURE_COMPARE_MODE_ARB
, GL_NONE
);
1037 gl_tex
->sampler_desc
.compare
= sampler_desc
->compare
;
1040 checkGLcall("Texture parameter application");
1042 if (gl_info
->supported
[EXT_TEXTURE_LOD_BIAS
])
1044 gl_info
->gl_ops
.gl
.p_glTexEnvf(GL_TEXTURE_FILTER_CONTROL_EXT
,
1045 GL_TEXTURE_LOD_BIAS_EXT
, sampler_desc
->lod_bias
);
1046 checkGLcall("glTexEnvf(GL_TEXTURE_LOD_BIAS_EXT, ...)");
1050 ULONG CDECL
wined3d_texture_incref(struct wined3d_texture
*texture
)
1054 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1056 if (texture
->swapchain
)
1057 return wined3d_swapchain_incref(texture
->swapchain
);
1059 refcount
= InterlockedIncrement(&texture
->resource
.ref
);
1060 TRACE("%p increasing refcount to %u.\n", texture
, refcount
);
1065 static void wined3d_texture_cleanup_sync(struct wined3d_texture
*texture
)
1067 wined3d_texture_sub_resources_destroyed(texture
);
1068 resource_cleanup(&texture
->resource
);
1069 wined3d_resource_wait_idle(&texture
->resource
);
1070 wined3d_texture_cleanup(texture
);
1073 static void wined3d_texture_destroy_object(void *object
)
1075 wined3d_texture_cleanup(object
);
1079 ULONG CDECL
wined3d_texture_decref(struct wined3d_texture
*texture
)
1083 TRACE("texture %p, swapchain %p.\n", texture
, texture
->swapchain
);
1085 if (texture
->swapchain
)
1086 return wined3d_swapchain_decref(texture
->swapchain
);
1088 refcount
= InterlockedDecrement(&texture
->resource
.ref
);
1089 TRACE("%p decreasing refcount to %u.\n", texture
, refcount
);
1093 /* Wait for the texture to become idle if it's using user memory,
1094 * since the application is allowed to free that memory once the
1095 * texture is destroyed. Note that this implies that
1096 * wined3d_texture_destroy_object() can't access that memory either. */
1097 if (texture
->user_memory
)
1098 wined3d_resource_wait_idle(&texture
->resource
);
1099 wined3d_texture_sub_resources_destroyed(texture
);
1100 texture
->resource
.parent_ops
->wined3d_object_destroyed(texture
->resource
.parent
);
1101 resource_cleanup(&texture
->resource
);
1102 wined3d_cs_destroy_object(texture
->resource
.device
->cs
, wined3d_texture_destroy_object
, texture
);
1108 struct wined3d_resource
* CDECL
wined3d_texture_get_resource(struct wined3d_texture
*texture
)
1110 TRACE("texture %p.\n", texture
);
1112 return &texture
->resource
;
1115 static BOOL
color_key_equal(const struct wined3d_color_key
*c1
, struct wined3d_color_key
*c2
)
1117 return c1
->color_space_low_value
== c2
->color_space_low_value
1118 && c1
->color_space_high_value
== c2
->color_space_high_value
;
1121 /* Context activation is done by the caller */
1122 void wined3d_texture_load(struct wined3d_texture
*texture
,
1123 struct wined3d_context
*context
, BOOL srgb
)
1125 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1126 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1130 TRACE("texture %p, context %p, srgb %#x.\n", texture
, context
, srgb
);
1132 if (!needs_separate_srgb_gl_texture(context
, texture
))
1136 flag
= WINED3D_TEXTURE_SRGB_VALID
;
1138 flag
= WINED3D_TEXTURE_RGB_VALID
;
1140 if (!d3d_info
->shader_color_key
1141 && (!(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1142 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1143 || (texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
1144 && !color_key_equal(&texture
->async
.gl_color_key
, &texture
->async
.src_blt_color_key
))))
1146 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1149 TRACE("Reloading because of color key value change.\n");
1150 for (i
= 0; i
< sub_count
; i
++)
1152 if (!wined3d_texture_load_location(texture
, i
, context
, texture
->resource
.map_binding
))
1153 ERR("Failed to load location %s.\n", wined3d_debug_location(texture
->resource
.map_binding
));
1155 wined3d_texture_invalidate_location(texture
, i
, ~texture
->resource
.map_binding
);
1158 texture
->async
.gl_color_key
= texture
->async
.src_blt_color_key
;
1161 if (texture
->flags
& flag
)
1163 TRACE("Texture %p not dirty, nothing to do.\n", texture
);
1167 /* Reload the surfaces if the texture is marked dirty. */
1168 for (i
= 0; i
< sub_count
; ++i
)
1170 if (!wined3d_texture_load_location(texture
, i
, context
,
1171 srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
))
1172 ERR("Failed to load location (srgb %#x).\n", srgb
);
1174 texture
->flags
|= flag
;
1177 void * CDECL
wined3d_texture_get_parent(const struct wined3d_texture
*texture
)
1179 TRACE("texture %p.\n", texture
);
1181 return texture
->resource
.parent
;
1184 HRESULT
wined3d_texture_check_box_dimensions(const struct wined3d_texture
*texture
,
1185 unsigned int level
, const struct wined3d_box
*box
)
1187 const struct wined3d_format
*format
= texture
->resource
.format
;
1188 unsigned int width_mask
, height_mask
, width
, height
, depth
;
1190 width
= wined3d_texture_get_level_width(texture
, level
);
1191 height
= wined3d_texture_get_level_height(texture
, level
);
1192 depth
= wined3d_texture_get_level_depth(texture
, level
);
1194 if (box
->left
>= box
->right
|| box
->right
> width
1195 || box
->top
>= box
->bottom
|| box
->bottom
> height
1196 || box
->front
>= box
->back
|| box
->back
> depth
)
1198 WARN("Box %s is invalid.\n", debug_box(box
));
1199 return WINEDDERR_INVALIDRECT
;
1202 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
1204 /* This assumes power of two block sizes, but NPOT block sizes would
1207 * This also assumes that the format's block depth is 1. */
1208 width_mask
= format
->block_width
- 1;
1209 height_mask
= format
->block_height
- 1;
1211 if ((box
->left
& width_mask
) || (box
->top
& height_mask
)
1212 || (box
->right
& width_mask
&& box
->right
!= width
)
1213 || (box
->bottom
& height_mask
&& box
->bottom
!= height
))
1215 WARN("Box %s is misaligned for %ux%u blocks.\n",
1216 debug_box(box
), format
->block_width
, format
->block_height
);
1217 return WINED3DERR_INVALIDCALL
;
1224 void CDECL
wined3d_texture_get_pitch(const struct wined3d_texture
*texture
,
1225 unsigned int level
, unsigned int *row_pitch
, unsigned int *slice_pitch
)
1227 const struct wined3d_resource
*resource
= &texture
->resource
;
1228 unsigned int width
= wined3d_texture_get_level_width(texture
, level
);
1229 unsigned int height
= wined3d_texture_get_level_height(texture
, level
);
1231 if (texture
->row_pitch
)
1233 *row_pitch
= texture
->row_pitch
;
1234 *slice_pitch
= texture
->slice_pitch
;
1238 wined3d_format_calculate_pitch(resource
->format
, resource
->device
->surface_alignment
,
1239 width
, height
, row_pitch
, slice_pitch
);
1242 DWORD CDECL
wined3d_texture_set_lod(struct wined3d_texture
*texture
, DWORD lod
)
1244 DWORD old
= texture
->lod
;
1246 TRACE("texture %p, lod %u.\n", texture
, lod
);
1248 /* The d3d9:texture test shows that SetLOD is ignored on non-managed
1249 * textures. The call always returns 0, and GetLOD always returns 0. */
1250 if (!wined3d_resource_access_is_managed(texture
->resource
.access
))
1252 TRACE("Ignoring LOD on texture with resource access %s.\n",
1253 wined3d_debug_resource_access(texture
->resource
.access
));
1257 if (lod
>= texture
->level_count
)
1258 lod
= texture
->level_count
- 1;
1260 if (texture
->lod
!= lod
)
1262 struct wined3d_device
*device
= texture
->resource
.device
;
1264 wined3d_resource_wait_idle(&texture
->resource
);
1267 texture
->texture_rgb
.base_level
= ~0u;
1268 texture
->texture_srgb
.base_level
= ~0u;
1269 if (texture
->resource
.bind_count
)
1270 wined3d_cs_emit_set_sampler_state(device
->cs
, texture
->sampler
, WINED3D_SAMP_MAX_MIP_LEVEL
,
1271 device
->state
.sampler_states
[texture
->sampler
][WINED3D_SAMP_MAX_MIP_LEVEL
]);
1277 DWORD CDECL
wined3d_texture_get_lod(const struct wined3d_texture
*texture
)
1279 TRACE("texture %p, returning %u.\n", texture
, texture
->lod
);
1281 return texture
->lod
;
1284 DWORD CDECL
wined3d_texture_get_level_count(const struct wined3d_texture
*texture
)
1286 TRACE("texture %p, returning %u.\n", texture
, texture
->level_count
);
1288 return texture
->level_count
;
1291 HRESULT CDECL
wined3d_texture_set_color_key(struct wined3d_texture
*texture
,
1292 DWORD flags
, const struct wined3d_color_key
*color_key
)
1294 struct wined3d_device
*device
= texture
->resource
.device
;
1295 static const DWORD all_flags
= WINED3D_CKEY_DST_BLT
| WINED3D_CKEY_DST_OVERLAY
1296 | WINED3D_CKEY_SRC_BLT
| WINED3D_CKEY_SRC_OVERLAY
;
1298 TRACE("texture %p, flags %#x, color_key %p.\n", texture
, flags
, color_key
);
1300 if (flags
& ~all_flags
)
1302 WARN("Invalid flags passed, returning WINED3DERR_INVALIDCALL.\n");
1303 return WINED3DERR_INVALIDCALL
;
1306 wined3d_cs_emit_set_color_key(device
->cs
, texture
, flags
, color_key
);
1311 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1312 * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1313 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1314 /* Context activation is done by the caller. */
1315 void wined3d_texture_set_compatible_renderbuffer(struct wined3d_texture
*texture
,
1316 unsigned int level
, const struct wined3d_rendertarget_info
*rt
)
1318 struct wined3d_renderbuffer_entry
*entry
;
1319 const struct wined3d_gl_info
*gl_info
;
1320 unsigned int src_width
, src_height
;
1321 unsigned int width
, height
;
1322 GLuint renderbuffer
= 0;
1324 gl_info
= &texture
->resource
.device
->adapter
->gl_info
;
1325 if (gl_info
->supported
[ARB_FRAMEBUFFER_OBJECT
])
1328 if (rt
&& rt
->resource
->format
->id
!= WINED3DFMT_NULL
)
1330 struct wined3d_texture
*rt_texture
;
1331 unsigned int rt_level
;
1333 if (rt
->resource
->type
== WINED3D_RTYPE_BUFFER
)
1335 FIXME("Unsupported resource type %s.\n", debug_d3dresourcetype(rt
->resource
->type
));
1338 rt_texture
= wined3d_texture_from_resource(rt
->resource
);
1339 rt_level
= rt
->sub_resource_idx
% rt_texture
->level_count
;
1341 width
= wined3d_texture_get_level_pow2_width(rt_texture
, rt_level
);
1342 height
= wined3d_texture_get_level_pow2_height(rt_texture
, rt_level
);
1346 width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1347 height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1350 src_width
= wined3d_texture_get_level_pow2_width(texture
, level
);
1351 src_height
= wined3d_texture_get_level_pow2_height(texture
, level
);
1353 /* A depth stencil smaller than the render target is not valid */
1354 if (width
> src_width
|| height
> src_height
)
1357 /* Remove any renderbuffer set if the sizes match */
1358 if (width
== src_width
&& height
== src_height
)
1360 texture
->current_renderbuffer
= NULL
;
1364 /* Look if we've already got a renderbuffer of the correct dimensions */
1365 LIST_FOR_EACH_ENTRY(entry
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
1367 if (entry
->width
== width
&& entry
->height
== height
)
1369 renderbuffer
= entry
->id
;
1370 texture
->current_renderbuffer
= entry
;
1377 gl_info
->fbo_ops
.glGenRenderbuffers(1, &renderbuffer
);
1378 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, renderbuffer
);
1379 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
,
1380 texture
->resource
.format
->glInternal
, width
, height
);
1382 entry
= heap_alloc(sizeof(*entry
));
1383 entry
->width
= width
;
1384 entry
->height
= height
;
1385 entry
->id
= renderbuffer
;
1386 list_add_head(&texture
->renderbuffers
, &entry
->entry
);
1388 texture
->current_renderbuffer
= entry
;
1391 checkGLcall("set_compatible_renderbuffer");
1394 HRESULT CDECL
wined3d_texture_update_desc(struct wined3d_texture
*texture
, UINT width
, UINT height
,
1395 enum wined3d_format_id format_id
, enum wined3d_multisample_type multisample_type
,
1396 UINT multisample_quality
, void *mem
, UINT pitch
)
1398 struct wined3d_device
*device
= texture
->resource
.device
;
1399 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
1400 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, format_id
, texture
->resource
.usage
);
1401 UINT resource_size
= wined3d_format_calculate_size(format
, device
->surface_alignment
, width
, height
, 1);
1402 struct wined3d_texture_sub_resource
*sub_resource
;
1403 DWORD valid_location
= 0;
1404 BOOL create_dib
= FALSE
;
1406 TRACE("texture %p, width %u, height %u, format %s, multisample_type %#x, multisample_quality %u, "
1407 "mem %p, pitch %u.\n",
1408 texture
, width
, height
, debug_d3dformat(format_id
), multisample_type
, multisample_quality
, mem
, pitch
);
1411 return WINED3DERR_INVALIDCALL
;
1413 if (texture
->level_count
* texture
->layer_count
> 1)
1415 WARN("Texture has multiple sub-resources, not supported.\n");
1416 return WINED3DERR_INVALIDCALL
;
1419 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
1421 WARN("Not supported on %s.\n", debug_d3dresourcetype(texture
->resource
.type
));
1422 return WINED3DERR_INVALIDCALL
;
1425 if (texture
->resource
.map_count
)
1427 WARN("Texture is mapped.\n");
1428 return WINED3DERR_INVALIDCALL
;
1431 /* We have no way of supporting a pitch that is not a multiple of the pixel
1432 * byte width short of uploading the texture row-by-row.
1433 * Fortunately that's not an issue since D3D9Ex doesn't allow a custom pitch
1434 * for user-memory textures (it always expects packed data) while DirectDraw
1435 * requires a 4-byte aligned pitch and doesn't support texture formats
1436 * larger than 4 bytes per pixel nor any format using 3 bytes per pixel.
1437 * This check is here to verify that the assumption holds. */
1438 if (pitch
% texture
->resource
.format
->byte_count
)
1440 WARN("Pitch unsupported, not a multiple of the texture format byte width.\n");
1441 return WINED3DERR_INVALIDCALL
;
1444 if (device
->d3d_initialized
)
1445 wined3d_cs_emit_unload_resource(device
->cs
, &texture
->resource
);
1446 wined3d_resource_wait_idle(&texture
->resource
);
1448 sub_resource
= &texture
->sub_resources
[0];
1449 if (texture
->dc_info
&& texture
->dc_info
[0].dc
)
1451 struct wined3d_texture_idx texture_idx
= {texture
, 0};
1453 wined3d_cs_destroy_object(device
->cs
, wined3d_texture_destroy_dc
, &texture_idx
);
1454 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1458 wined3d_resource_free_sysmem(&texture
->resource
);
1460 if ((texture
->row_pitch
= pitch
))
1461 texture
->slice_pitch
= height
* pitch
;
1463 /* User memory surfaces don't have the regular surface alignment. */
1464 wined3d_format_calculate_pitch(format
, 1, width
, height
,
1465 &texture
->row_pitch
, &texture
->slice_pitch
);
1467 texture
->resource
.format
= format
;
1468 texture
->resource
.multisample_type
= multisample_type
;
1469 texture
->resource
.multisample_quality
= multisample_quality
;
1470 texture
->resource
.width
= width
;
1471 texture
->resource
.height
= height
;
1472 texture
->resource
.size
= texture
->slice_pitch
;
1473 sub_resource
->size
= texture
->slice_pitch
;
1474 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
1476 if (multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
1477 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
1479 texture
->target
= GL_TEXTURE_2D
;
1481 if (((width
& (width
- 1)) || (height
& (height
- 1))) && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
]
1482 && !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
] && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
1484 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
1485 texture
->pow2_width
= texture
->pow2_height
= 1;
1486 while (texture
->pow2_width
< width
)
1487 texture
->pow2_width
<<= 1;
1488 while (texture
->pow2_height
< height
)
1489 texture
->pow2_height
<<= 1;
1493 texture
->flags
&= ~WINED3D_TEXTURE_COND_NP2_EMULATED
;
1494 texture
->pow2_width
= width
;
1495 texture
->pow2_height
= height
;
1498 if ((texture
->user_memory
= mem
))
1500 texture
->resource
.map_binding
= WINED3D_LOCATION_USER_MEMORY
;
1501 valid_location
= WINED3D_LOCATION_USER_MEMORY
;
1505 wined3d_texture_prepare_location(texture
, 0, NULL
, WINED3D_LOCATION_SYSMEM
);
1506 valid_location
= WINED3D_LOCATION_SYSMEM
;
1509 /* The format might be changed to a format that needs conversion.
1510 * If the surface didn't use PBOs previously but could now, don't
1511 * change it - whatever made us not use PBOs might come back, e.g.
1513 if (texture
->resource
.map_binding
== WINED3D_LOCATION_BUFFER
&& !wined3d_texture_use_pbo(texture
, gl_info
))
1514 texture
->resource
.map_binding
= WINED3D_LOCATION_SYSMEM
;
1516 wined3d_texture_validate_location(texture
, 0, valid_location
);
1517 wined3d_texture_invalidate_location(texture
, 0, ~valid_location
);
1521 struct wined3d_texture_idx texture_idx
= {texture
, 0};
1523 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
1524 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
1530 /* Context activation is done by the caller. */
1531 static void wined3d_texture_prepare_buffer_object(struct wined3d_texture
*texture
,
1532 unsigned int sub_resource_idx
, const struct wined3d_gl_info
*gl_info
)
1534 struct wined3d_texture_sub_resource
*sub_resource
;
1536 sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
1537 if (sub_resource
->buffer_object
)
1540 GL_EXTCALL(glGenBuffers(1, &sub_resource
->buffer_object
));
1541 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->buffer_object
));
1542 GL_EXTCALL(glBufferData(GL_PIXEL_UNPACK_BUFFER
, sub_resource
->size
, NULL
, GL_STREAM_DRAW
));
1543 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1544 checkGLcall("Create buffer object");
1546 TRACE("Created buffer object %u for texture %p, sub-resource %u.\n",
1547 sub_resource
->buffer_object
, texture
, sub_resource_idx
);
1550 static void wined3d_texture_force_reload(struct wined3d_texture
*texture
)
1552 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
1555 texture
->flags
&= ~(WINED3D_TEXTURE_RGB_ALLOCATED
| WINED3D_TEXTURE_SRGB_ALLOCATED
1556 | WINED3D_TEXTURE_CONVERTED
);
1557 texture
->async
.flags
&= ~WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1558 for (i
= 0; i
< sub_count
; ++i
)
1560 wined3d_texture_invalidate_location(texture
, i
,
1561 WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
);
1565 /* Context activation is done by the caller. */
1566 void wined3d_texture_prepare_texture(struct wined3d_texture
*texture
, struct wined3d_context
*context
, BOOL srgb
)
1568 DWORD alloc_flag
= srgb
? WINED3D_TEXTURE_SRGB_ALLOCATED
: WINED3D_TEXTURE_RGB_ALLOCATED
;
1569 const struct wined3d_format
*format
= texture
->resource
.format
;
1570 const struct wined3d_d3d_info
*d3d_info
= context
->d3d_info
;
1571 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1572 const struct wined3d_color_key_conversion
*conversion
;
1575 TRACE("texture %p, context %p, format %s.\n", texture
, context
, debug_d3dformat(format
->id
));
1577 if (!d3d_info
->shader_color_key
1578 && !(texture
->async
.flags
& WINED3D_TEXTURE_ASYNC_COLOR_KEY
)
1579 != !(texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
))
1581 wined3d_texture_force_reload(texture
);
1583 if (texture
->async
.color_key_flags
& WINED3D_CKEY_SRC_BLT
)
1584 texture
->async
.flags
|= WINED3D_TEXTURE_ASYNC_COLOR_KEY
;
1587 if (texture
->flags
& alloc_flag
)
1590 if (format
->conv_byte_count
)
1592 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1594 else if ((conversion
= wined3d_format_get_color_key_conversion(texture
, TRUE
)))
1596 texture
->flags
|= WINED3D_TEXTURE_CONVERTED
;
1597 format
= wined3d_get_format(gl_info
, conversion
->dst_format
, texture
->resource
.usage
);
1598 TRACE("Using format %s for color key conversion.\n", debug_d3dformat(format
->id
));
1601 wined3d_texture_bind_and_dirtify(texture
, context
, srgb
);
1604 internal
= format
->glGammaInternal
;
1605 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1606 && wined3d_resource_is_offscreen(&texture
->resource
))
1607 internal
= format
->rtInternal
;
1609 internal
= format
->glInternal
;
1612 FIXME("No GL internal format for format %s.\n", debug_d3dformat(format
->id
));
1614 TRACE("internal %#x, format %#x, type %#x.\n", internal
, format
->glFormat
, format
->glType
);
1616 if (wined3d_texture_use_immutable_storage(texture
, gl_info
))
1617 wined3d_texture_allocate_gl_immutable_storage(texture
, internal
, gl_info
);
1619 wined3d_texture_allocate_gl_mutable_storage(texture
, internal
, format
, gl_info
);
1620 texture
->flags
|= alloc_flag
;
1623 static void wined3d_texture_prepare_rb(struct wined3d_texture
*texture
,
1624 const struct wined3d_gl_info
*gl_info
, BOOL multisample
)
1626 const struct wined3d_format
*format
= texture
->resource
.format
;
1632 if (texture
->rb_multisample
)
1635 samples
= wined3d_texture_get_gl_sample_count(texture
);
1637 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_multisample
);
1638 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_multisample
);
1639 gl_info
->fbo_ops
.glRenderbufferStorageMultisample(GL_RENDERBUFFER
, samples
,
1640 format
->glInternal
, texture
->resource
.width
, texture
->resource
.height
);
1641 checkGLcall("glRenderbufferStorageMultisample()");
1642 TRACE("Created multisample rb %u.\n", texture
->rb_multisample
);
1646 if (texture
->rb_resolved
)
1649 gl_info
->fbo_ops
.glGenRenderbuffers(1, &texture
->rb_resolved
);
1650 gl_info
->fbo_ops
.glBindRenderbuffer(GL_RENDERBUFFER
, texture
->rb_resolved
);
1651 gl_info
->fbo_ops
.glRenderbufferStorage(GL_RENDERBUFFER
, format
->glInternal
,
1652 texture
->resource
.width
, texture
->resource
.height
);
1653 checkGLcall("glRenderbufferStorage()");
1654 TRACE("Created resolved rb %u.\n", texture
->rb_resolved
);
1658 /* Context activation is done by the caller. Context may be NULL in
1659 * WINED3D_NO3D mode. */
1660 BOOL
wined3d_texture_prepare_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1661 struct wined3d_context
*context
, DWORD location
)
1665 case WINED3D_LOCATION_SYSMEM
:
1666 if (texture
->resource
.heap_memory
)
1669 if (!wined3d_resource_allocate_sysmem(&texture
->resource
))
1671 ERR("Failed to allocate system memory.\n");
1676 case WINED3D_LOCATION_USER_MEMORY
:
1677 if (!texture
->user_memory
)
1678 ERR("Map binding is set to WINED3D_LOCATION_USER_MEMORY but surface->user_memory is NULL.\n");
1681 case WINED3D_LOCATION_BUFFER
:
1682 wined3d_texture_prepare_buffer_object(texture
, sub_resource_idx
, context
->gl_info
);
1685 case WINED3D_LOCATION_TEXTURE_RGB
:
1686 wined3d_texture_prepare_texture(texture
, context
, FALSE
);
1689 case WINED3D_LOCATION_TEXTURE_SRGB
:
1690 wined3d_texture_prepare_texture(texture
, context
, TRUE
);
1693 case WINED3D_LOCATION_DRAWABLE
:
1694 if (!texture
->swapchain
&& wined3d_settings
.offscreen_rendering_mode
!= ORM_BACKBUFFER
)
1695 ERR("Texture %p does not have a drawable.\n", texture
);
1698 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1699 wined3d_texture_prepare_rb(texture
, context
->gl_info
, TRUE
);
1702 case WINED3D_LOCATION_RB_RESOLVED
:
1703 wined3d_texture_prepare_rb(texture
, context
->gl_info
, FALSE
);
1707 ERR("Invalid location %s.\n", wined3d_debug_location(location
));
1712 static struct wined3d_texture_sub_resource
*wined3d_texture_get_sub_resource(struct wined3d_texture
*texture
,
1713 unsigned int sub_resource_idx
)
1715 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1717 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
1719 if (sub_resource_idx
>= sub_count
)
1721 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
1725 return &texture
->sub_resources
[sub_resource_idx
];
1728 HRESULT CDECL
wined3d_texture_add_dirty_region(struct wined3d_texture
*texture
,
1729 UINT layer
, const struct wined3d_box
*dirty_region
)
1731 TRACE("texture %p, layer %u, dirty_region %s.\n", texture
, layer
, debug_box(dirty_region
));
1733 if (layer
>= texture
->layer_count
)
1735 WARN("Invalid layer %u specified.\n", layer
);
1736 return WINED3DERR_INVALIDCALL
;
1740 FIXME("Ignoring dirty_region %s.\n", debug_box(dirty_region
));
1742 wined3d_cs_emit_add_dirty_texture_region(texture
->resource
.device
->cs
, texture
, layer
);
1747 void wined3d_texture_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1748 const struct wined3d_context
*context
, const struct wined3d_format
*format
, const struct wined3d_box
*src_box
,
1749 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
,
1750 unsigned int dst_x
, unsigned int dst_y
, unsigned int dst_z
, BOOL srgb
)
1752 texture
->texture_ops
->texture_upload_data(texture
, sub_resource_idx
, context
,
1753 format
, src_box
, data
, row_pitch
, slice_pitch
, dst_x
, dst_y
, dst_z
, srgb
);
1756 /* This call just uploads data, the caller is responsible for binding the
1757 * correct texture. */
1758 /* Context activation is done by the caller. */
1759 static void texture2d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1760 const struct wined3d_context
*context
, const struct wined3d_format
*format
, const struct wined3d_box
*src_box
,
1761 const struct wined3d_const_bo_address
*data
, unsigned int src_row_pitch
, unsigned int src_slice_pitch
,
1762 unsigned int dst_x
, unsigned int dst_y
, unsigned int dst_z
, BOOL srgb
)
1764 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
1765 unsigned int update_w
= src_box
->right
- src_box
->left
;
1766 unsigned int update_h
= src_box
->bottom
- src_box
->top
;
1767 unsigned int level
, layer
;
1770 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data {%#x:%p}, "
1771 "src_row_pitch %#x, src_slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
1772 texture
, sub_resource_idx
, context
, debug_d3dformat(format
->id
), debug_box(src_box
),
1773 data
->buffer_object
, data
->addr
, src_row_pitch
, src_slice_pitch
, dst_x
, dst_y
, dst_z
, srgb
);
1775 if (texture
->sub_resources
[sub_resource_idx
].map_count
)
1777 WARN("Uploading a texture that is currently mapped, setting WINED3D_TEXTURE_PIN_SYSMEM.\n");
1778 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
;
1781 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_HEIGHT_SCALE
)
1783 update_h
*= format
->height_scale
.numerator
;
1784 update_h
/= format
->height_scale
.denominator
;
1787 if (data
->buffer_object
)
1789 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
1790 checkGLcall("glBindBuffer");
1793 target
= wined3d_texture_get_sub_resource_target(texture
, sub_resource_idx
);
1794 level
= sub_resource_idx
% texture
->level_count
;
1795 layer
= sub_resource_idx
/ texture
->level_count
;
1797 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_COMPRESSED
)
1799 unsigned int dst_row_pitch
, dst_slice_pitch
;
1800 const BYTE
*addr
= data
->addr
;
1803 addr
+= (src_box
->top
/ format
->block_height
) * src_row_pitch
;
1804 addr
+= (src_box
->left
/ format
->block_width
) * format
->block_byte_count
;
1807 internal
= format
->glGammaInternal
;
1808 else if (texture
->resource
.usage
& WINED3DUSAGE_RENDERTARGET
1809 && wined3d_resource_is_offscreen(&texture
->resource
))
1810 internal
= format
->rtInternal
;
1812 internal
= format
->glInternal
;
1814 wined3d_format_calculate_pitch(format
, 1, update_w
, update_h
, &dst_row_pitch
, &dst_slice_pitch
);
1816 TRACE("Uploading compressed data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
1817 "format %#x, image_size %#x, addr %p.\n",
1818 target
, level
, layer
, dst_x
, dst_y
,
1819 update_w
, update_h
, internal
, dst_slice_pitch
, addr
);
1821 if (dst_row_pitch
== src_row_pitch
)
1823 if (target
== GL_TEXTURE_2D_ARRAY
)
1825 GL_EXTCALL(glCompressedTexSubImage3D(target
, level
, dst_x
, dst_y
,
1826 layer
, update_w
, update_h
, 1, internal
, dst_slice_pitch
, addr
));
1830 GL_EXTCALL(glCompressedTexSubImage2D(target
, level
, dst_x
, dst_y
,
1831 update_w
, update_h
, internal
, dst_slice_pitch
, addr
));
1836 UINT row_count
= (update_h
+ format
->block_height
- 1) / format
->block_height
;
1839 /* glCompressedTexSubImage2D() ignores pixel store state, so we
1840 * can't use the unpack row length like for glTexSubImage2D. */
1841 for (row
= 0, y
= dst_y
; row
< row_count
; ++row
)
1843 if (target
== GL_TEXTURE_2D_ARRAY
)
1845 GL_EXTCALL(glCompressedTexSubImage3D(target
, level
, dst_x
, y
,
1846 layer
, update_w
, format
->block_height
, 1, internal
, dst_row_pitch
, addr
));
1850 GL_EXTCALL(glCompressedTexSubImage2D(target
, level
, dst_x
, y
,
1851 update_w
, format
->block_height
, internal
, dst_row_pitch
, addr
));
1854 y
+= format
->block_height
;
1855 addr
+= src_row_pitch
;
1858 checkGLcall("Upload compressed texture data");
1862 const BYTE
*addr
= data
->addr
;
1864 addr
+= src_box
->top
* src_row_pitch
;
1865 addr
+= src_box
->left
* format
->byte_count
;
1867 TRACE("Uploading data, target %#x, level %u, layer %u, x %d, y %d, w %u, h %u, "
1868 "format %#x, type %#x, addr %p.\n",
1869 target
, level
, layer
, dst_x
, dst_y
,
1870 update_w
, update_h
, format
->glFormat
, format
->glType
, addr
);
1872 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, src_row_pitch
/ format
->byte_count
);
1873 if (target
== GL_TEXTURE_2D_ARRAY
)
1875 GL_EXTCALL(glTexSubImage3D(target
, level
, dst_x
, dst_y
,
1876 layer
, update_w
, update_h
, 1, format
->glFormat
, format
->glType
, addr
));
1880 gl_info
->gl_ops
.gl
.p_glTexSubImage2D(target
, level
, dst_x
, dst_y
,
1881 update_w
, update_h
, format
->glFormat
, format
->glType
, addr
);
1883 gl_info
->gl_ops
.gl
.p_glPixelStorei(GL_UNPACK_ROW_LENGTH
, 0);
1884 checkGLcall("Upload texture data");
1887 if (data
->buffer_object
)
1889 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
1890 checkGLcall("glBindBuffer");
1893 if (wined3d_settings
.strict_draw_ordering
)
1894 gl_info
->gl_ops
.gl
.p_glFlush();
1896 if (gl_info
->quirks
& WINED3D_QUIRK_FBO_TEX_UPDATE
)
1898 struct wined3d_device
*device
= texture
->resource
.device
;
1901 for (i
= 0; i
< device
->context_count
; ++i
)
1903 context_texture_update(device
->contexts
[i
], texture
);
1908 /* Context activation is done by the caller. Context may be NULL in ddraw-only mode. */
1909 static BOOL
texture2d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
1910 struct wined3d_context
*context
, DWORD location
)
1912 TRACE("texture %p, sub_resource_idx %u, context %p, location %s.\n",
1913 texture
, sub_resource_idx
, context
, wined3d_debug_location(location
));
1917 case WINED3D_LOCATION_USER_MEMORY
:
1918 case WINED3D_LOCATION_SYSMEM
:
1919 case WINED3D_LOCATION_BUFFER
:
1920 return texture2d_load_sysmem(texture
, sub_resource_idx
, context
, location
);
1922 case WINED3D_LOCATION_DRAWABLE
:
1923 return texture2d_load_drawable(texture
, sub_resource_idx
, context
);
1925 case WINED3D_LOCATION_RB_RESOLVED
:
1926 case WINED3D_LOCATION_RB_MULTISAMPLE
:
1927 return texture2d_load_renderbuffer(texture
, sub_resource_idx
, context
, location
);
1929 case WINED3D_LOCATION_TEXTURE_RGB
:
1930 case WINED3D_LOCATION_TEXTURE_SRGB
:
1931 return texture2d_load_texture(texture
, sub_resource_idx
, context
,
1932 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
1935 ERR("Don't know how to handle location %#x.\n", location
);
1940 static const struct wined3d_texture_ops texture2d_ops
=
1942 texture2d_upload_data
,
1943 texture2d_load_location
,
1946 struct wined3d_texture
* __cdecl
wined3d_texture_from_resource(struct wined3d_resource
*resource
)
1948 return texture_from_resource(resource
);
1951 static ULONG
texture_resource_incref(struct wined3d_resource
*resource
)
1953 return wined3d_texture_incref(texture_from_resource(resource
));
1956 static ULONG
texture_resource_decref(struct wined3d_resource
*resource
)
1958 return wined3d_texture_decref(texture_from_resource(resource
));
1961 static void texture_resource_preload(struct wined3d_resource
*resource
)
1963 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1964 struct wined3d_context
*context
;
1966 context
= context_acquire(resource
->device
, NULL
, 0);
1967 wined3d_texture_load(texture
, context
, texture
->flags
& WINED3D_TEXTURE_IS_SRGB
);
1968 context_release(context
);
1971 static void wined3d_texture_unload(struct wined3d_resource
*resource
)
1973 struct wined3d_texture
*texture
= texture_from_resource(resource
);
1974 UINT sub_count
= texture
->level_count
* texture
->layer_count
;
1975 struct wined3d_renderbuffer_entry
*entry
, *entry2
;
1976 struct wined3d_device
*device
= resource
->device
;
1977 const struct wined3d_gl_info
*gl_info
;
1978 struct wined3d_context
*context
;
1981 TRACE("texture %p.\n", texture
);
1983 context
= context_acquire(device
, NULL
, 0);
1984 gl_info
= context
->gl_info
;
1986 for (i
= 0; i
< sub_count
; ++i
)
1988 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[i
];
1990 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
1991 && wined3d_texture_load_location(texture
, i
, context
, resource
->map_binding
))
1993 wined3d_texture_invalidate_location(texture
, i
, ~resource
->map_binding
);
1997 /* We should only get here on device reset/teardown for implicit
1999 if (resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
2000 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2001 ERR("Discarding %s %p sub-resource %u with resource access %s.\n",
2002 debug_d3dresourcetype(resource
->type
), resource
, i
,
2003 wined3d_debug_resource_access(resource
->access
));
2004 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_DISCARDED
);
2005 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_DISCARDED
);
2008 if (sub_resource
->buffer_object
)
2009 wined3d_texture_remove_buffer_object(texture
, i
, context
->gl_info
);
2012 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, &texture
->renderbuffers
, struct wined3d_renderbuffer_entry
, entry
)
2014 context_gl_resource_released(device
, entry
->id
, TRUE
);
2015 gl_info
->fbo_ops
.glDeleteRenderbuffers(1, &entry
->id
);
2016 list_remove(&entry
->entry
);
2019 list_init(&texture
->renderbuffers
);
2020 texture
->current_renderbuffer
= NULL
;
2022 context_release(context
);
2024 wined3d_texture_force_reload(texture
);
2025 wined3d_texture_unload_gl_texture(texture
);
2028 static HRESULT
texture_resource_sub_resource_map(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
,
2029 struct wined3d_map_desc
*map_desc
, const struct wined3d_box
*box
, DWORD flags
)
2031 const struct wined3d_format
*format
= resource
->format
;
2032 struct wined3d_texture_sub_resource
*sub_resource
;
2033 struct wined3d_device
*device
= resource
->device
;
2034 unsigned int fmt_flags
= resource
->format_flags
;
2035 struct wined3d_context
*context
= NULL
;
2036 struct wined3d_texture
*texture
;
2037 struct wined3d_bo_address data
;
2038 unsigned int texture_level
;
2042 TRACE("resource %p, sub_resource_idx %u, map_desc %p, box %s, flags %#x.\n",
2043 resource
, sub_resource_idx
, map_desc
, debug_box(box
), flags
);
2045 texture
= texture_from_resource(resource
);
2046 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2047 return E_INVALIDARG
;
2049 texture_level
= sub_resource_idx
% texture
->level_count
;
2050 if (box
&& FAILED(wined3d_texture_check_box_dimensions(texture
, texture_level
, box
)))
2052 WARN("Map box is invalid.\n");
2053 if (((fmt_flags
& WINED3DFMT_FLAG_BLOCKS
) && !(resource
->access
& WINED3D_RESOURCE_ACCESS_CPU
))
2054 || resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
2055 return WINED3DERR_INVALIDCALL
;
2058 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2060 WARN("DC is in use.\n");
2061 return WINED3DERR_INVALIDCALL
;
2064 if (sub_resource
->map_count
)
2066 WARN("Sub-resource is already mapped.\n");
2067 return WINED3DERR_INVALIDCALL
;
2070 if (device
->d3d_initialized
)
2071 context
= context_acquire(device
, NULL
, 0);
2073 if (flags
& WINED3D_MAP_DISCARD
)
2075 TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
2076 wined3d_debug_location(resource
->map_binding
));
2077 if ((ret
= wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, resource
->map_binding
)))
2078 wined3d_texture_validate_location(texture
, sub_resource_idx
, resource
->map_binding
);
2082 if (resource
->usage
& WINED3DUSAGE_DYNAMIC
)
2083 WARN_(d3d_perf
)("Mapping a dynamic texture without WINED3D_MAP_DISCARD.\n");
2084 ret
= wined3d_texture_load_location(texture
, sub_resource_idx
, context
, resource
->map_binding
);
2089 ERR("Failed to prepare location.\n");
2090 context_release(context
);
2091 return E_OUTOFMEMORY
;
2094 if (flags
& WINED3D_MAP_WRITE
2095 && (!(flags
& WINED3D_MAP_NO_DIRTY_UPDATE
) || (resource
->usage
& WINED3DUSAGE_DYNAMIC
)))
2096 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, ~resource
->map_binding
);
2098 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, resource
->map_binding
);
2099 base_memory
= context_map_bo_address(context
, &data
, sub_resource
->size
, GL_PIXEL_UNPACK_BUFFER
, flags
);
2100 TRACE("Base memory pointer %p.\n", base_memory
);
2103 context_release(context
);
2105 if (fmt_flags
& WINED3DFMT_FLAG_BROKEN_PITCH
)
2107 map_desc
->row_pitch
= wined3d_texture_get_level_width(texture
, texture_level
) * format
->byte_count
;
2108 map_desc
->slice_pitch
= wined3d_texture_get_level_height(texture
, texture_level
) * map_desc
->row_pitch
;
2112 wined3d_texture_get_pitch(texture
, texture_level
, &map_desc
->row_pitch
, &map_desc
->slice_pitch
);
2117 map_desc
->data
= base_memory
;
2121 if ((fmt_flags
& (WINED3DFMT_FLAG_BLOCKS
| WINED3DFMT_FLAG_BROKEN_PITCH
)) == WINED3DFMT_FLAG_BLOCKS
)
2123 /* Compressed textures are block based, so calculate the offset of
2124 * the block that contains the top-left pixel of the mapped box. */
2125 map_desc
->data
= base_memory
2126 + (box
->front
* map_desc
->slice_pitch
)
2127 + ((box
->top
/ format
->block_height
) * map_desc
->row_pitch
)
2128 + ((box
->left
/ format
->block_width
) * format
->block_byte_count
);
2132 map_desc
->data
= base_memory
2133 + (box
->front
* map_desc
->slice_pitch
)
2134 + (box
->top
* map_desc
->row_pitch
)
2135 + (box
->left
* format
->byte_count
);
2139 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2141 RECT
*r
= &texture
->swapchain
->front_buffer_update
;
2144 SetRect(r
, 0, 0, resource
->width
, resource
->height
);
2146 SetRect(r
, box
->left
, box
->top
, box
->right
, box
->bottom
);
2147 TRACE("Mapped front buffer %s.\n", wine_dbgstr_rect(r
));
2150 ++resource
->map_count
;
2151 ++sub_resource
->map_count
;
2153 TRACE("Returning memory %p, row pitch %u, slice pitch %u.\n",
2154 map_desc
->data
, map_desc
->row_pitch
, map_desc
->slice_pitch
);
2159 static HRESULT
texture_resource_sub_resource_unmap(struct wined3d_resource
*resource
, unsigned int sub_resource_idx
)
2161 struct wined3d_texture_sub_resource
*sub_resource
;
2162 struct wined3d_device
*device
= resource
->device
;
2163 struct wined3d_context
*context
= NULL
;
2164 struct wined3d_texture
*texture
;
2165 struct wined3d_bo_address data
;
2167 TRACE("resource %p, sub_resource_idx %u.\n", resource
, sub_resource_idx
);
2169 texture
= texture_from_resource(resource
);
2170 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
2171 return E_INVALIDARG
;
2173 if (!sub_resource
->map_count
)
2175 WARN("Trying to unmap unmapped sub-resource.\n");
2176 if (texture
->flags
& WINED3D_TEXTURE_DC_IN_USE
)
2178 return WINEDDERR_NOTLOCKED
;
2181 if (device
->d3d_initialized
)
2182 context
= context_acquire(device
, NULL
, 0);
2184 wined3d_texture_get_memory(texture
, sub_resource_idx
, &data
, texture
->resource
.map_binding
);
2185 context_unmap_bo_address(context
, &data
, GL_PIXEL_UNPACK_BUFFER
);
2188 context_release(context
);
2190 if (texture
->swapchain
&& texture
->swapchain
->front_buffer
== texture
)
2192 if (!(sub_resource
->locations
& (WINED3D_LOCATION_DRAWABLE
| WINED3D_LOCATION_TEXTURE_RGB
)))
2193 texture
->swapchain
->swapchain_ops
->swapchain_frontbuffer_updated(texture
->swapchain
);
2196 --sub_resource
->map_count
;
2197 if (!--resource
->map_count
&& texture
->update_map_binding
)
2198 wined3d_texture_update_map_binding(texture
);
2203 static const struct wined3d_resource_ops texture_resource_ops
=
2205 texture_resource_incref
,
2206 texture_resource_decref
,
2207 texture_resource_preload
,
2208 wined3d_texture_unload
,
2209 texture_resource_sub_resource_map
,
2210 texture_resource_sub_resource_unmap
,
2213 static HRESULT
wined3d_texture_init(struct wined3d_texture
*texture
, const struct wined3d_resource_desc
*desc
,
2214 unsigned int layer_count
, unsigned int level_count
, DWORD flags
, struct wined3d_device
*device
,
2215 void *parent
, const struct wined3d_parent_ops
*parent_ops
, const struct wined3d_texture_ops
*texture_ops
)
2217 struct wined3d_device_parent
*device_parent
= device
->device_parent
;
2218 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
2219 unsigned int sub_count
, i
, j
, size
, offset
= 0;
2220 unsigned int pow2_width
, pow2_height
;
2221 const struct wined3d_format
*format
;
2224 TRACE("texture %p, resource_type %s, format %s, multisample_type %#x, multisample_quality %#x, "
2225 "usage %s, access %s, width %u, height %u, depth %u, layer_count %u, level_count %u, "
2226 "flags %#x, device %p, parent %p, parent_ops %p, texture_ops %p.\n",
2227 texture
, debug_d3dresourcetype(desc
->resource_type
), debug_d3dformat(desc
->format
),
2228 desc
->multisample_type
, desc
->multisample_quality
, debug_d3dusage(desc
->usage
),
2229 wined3d_debug_resource_access(desc
->access
), desc
->width
, desc
->height
, desc
->depth
,
2230 layer_count
, level_count
, flags
, device
, parent
, parent_ops
, texture_ops
);
2232 if (!desc
->width
|| !desc
->height
|| !desc
->depth
)
2233 return WINED3DERR_INVALIDCALL
;
2235 if (desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
)
2237 if (layer_count
!= 1)
2239 ERR("Invalid layer count for volume texture.\n");
2240 return E_INVALIDARG
;
2243 if (!gl_info
->supported
[EXT_TEXTURE3D
])
2245 WARN("OpenGL implementation does not support 3D textures.\n");
2246 return WINED3DERR_INVALIDCALL
;
2250 if (!(desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
> 1
2251 && !gl_info
->supported
[EXT_TEXTURE_ARRAY
])
2253 WARN("OpenGL implementation does not support array textures.\n");
2254 return WINED3DERR_INVALIDCALL
;
2257 /* TODO: It should only be possible to create textures for formats
2258 * that are reported as supported. */
2259 if (WINED3DFMT_UNKNOWN
>= desc
->format
)
2261 WARN("Texture cannot be created with a format of WINED3DFMT_UNKNOWN.\n");
2262 return WINED3DERR_INVALIDCALL
;
2265 if (desc
->usage
& WINED3DUSAGE_DYNAMIC
&& (wined3d_resource_access_is_managed(desc
->access
)
2266 || desc
->usage
& WINED3DUSAGE_SCRATCH
))
2268 WARN("Attempted to create a dynamic texture with access %s and usage %s.\n",
2269 wined3d_debug_resource_access(desc
->access
), debug_d3dusage(desc
->usage
));
2270 return WINED3DERR_INVALIDCALL
;
2273 if (!(desc
->usage
& (WINED3DUSAGE_DYNAMIC
| WINED3DUSAGE_RENDERTARGET
| WINED3DUSAGE_DEPTHSTENCIL
))
2274 && (flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
))
2275 WARN("Creating a mappable texture that doesn't specify dynamic usage.\n");
2276 if (desc
->usage
& WINED3DUSAGE_RENDERTARGET
&& desc
->access
& WINED3D_RESOURCE_ACCESS_CPU
)
2277 FIXME("Trying to create a CPU accessible render target.\n");
2279 pow2_width
= desc
->width
;
2280 pow2_height
= desc
->height
;
2281 if (((desc
->width
& (desc
->width
- 1)) || (desc
->height
& (desc
->height
- 1)) || (desc
->depth
& (desc
->depth
- 1)))
2282 && !gl_info
->supported
[ARB_TEXTURE_NON_POWER_OF_TWO
])
2284 /* level_count == 0 returns an error as well. */
2285 if (level_count
!= 1 || layer_count
!= 1 || desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
)
2287 if (!(desc
->usage
& WINED3DUSAGE_SCRATCH
))
2289 WARN("Attempted to create a mipmapped/cube/array/volume NPOT "
2290 "texture without unconditional NPOT support.\n");
2291 return WINED3DERR_INVALIDCALL
;
2294 WARN("Creating a scratch mipmapped/cube/array NPOT texture despite lack of HW support.\n");
2296 texture
->flags
|= WINED3D_TEXTURE_COND_NP2
;
2298 if (desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !gl_info
->supported
[ARB_TEXTURE_RECTANGLE
]
2299 && !gl_info
->supported
[WINED3D_GL_NORMALIZED_TEXRECT
])
2301 const struct wined3d_format
*format
= wined3d_get_format(gl_info
, desc
->format
, desc
->usage
);
2303 /* TODO: Add support for non-power-of-two compressed textures. */
2304 if (format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
]
2305 & (WINED3DFMT_FLAG_COMPRESSED
| WINED3DFMT_FLAG_HEIGHT_SCALE
))
2307 FIXME("Compressed or height scaled non-power-of-two (%ux%u) textures are not supported.\n",
2308 desc
->width
, desc
->height
);
2309 return WINED3DERR_NOTAVAILABLE
;
2312 /* Find the nearest pow2 match. */
2313 pow2_width
= pow2_height
= 1;
2314 while (pow2_width
< desc
->width
)
2316 while (pow2_height
< desc
->height
)
2318 texture
->flags
|= WINED3D_TEXTURE_COND_NP2_EMULATED
;
2321 texture
->pow2_width
= pow2_width
;
2322 texture
->pow2_height
= pow2_height
;
2324 if ((pow2_width
> gl_info
->limits
.texture_size
|| pow2_height
> gl_info
->limits
.texture_size
)
2325 && (desc
->usage
& WINED3DUSAGE_TEXTURE
))
2327 /* One of four options:
2328 * 1: Do the same as we do with NPOT and scale the texture. (Any
2329 * texture ops would require the texture to be scaled which is
2330 * potentially slow.)
2331 * 2: Set the texture to the maximum size (bad idea).
2332 * 3: WARN and return WINED3DERR_NOTAVAILABLE.
2333 * 4: Create the surface, but allow it to be used only for DirectDraw
2334 * Blts. Some apps (e.g. Swat 3) create textures with a height of
2335 * 16 and a width > 3000 and blt 16x16 letter areas from them to
2336 * the render target. */
2337 if (desc
->access
& WINED3D_RESOURCE_ACCESS_GPU
)
2339 WARN("Dimensions (%ux%u) exceed the maximum texture size.\n", pow2_width
, pow2_height
);
2340 return WINED3DERR_NOTAVAILABLE
;
2343 /* We should never use this surface in combination with OpenGL. */
2344 TRACE("Creating an oversized (%ux%u) surface.\n", pow2_width
, pow2_height
);
2347 format
= wined3d_get_format(&device
->adapter
->gl_info
, desc
->format
, desc
->usage
);
2348 for (i
= 0; i
< layer_count
; ++i
)
2350 for (j
= 0; j
< level_count
; ++j
)
2352 unsigned int idx
= i
* level_count
+ j
;
2354 size
= wined3d_format_calculate_size(format
, device
->surface_alignment
,
2355 max(1, desc
->width
>> j
), max(1, desc
->height
>> j
), max(1, desc
->depth
>> j
));
2356 texture
->sub_resources
[idx
].offset
= offset
;
2357 texture
->sub_resources
[idx
].size
= size
;
2360 offset
= (offset
+ (RESOURCE_ALIGNMENT
- 1)) & ~(RESOURCE_ALIGNMENT
- 1);
2364 return WINED3DERR_INVALIDCALL
;
2366 if (FAILED(hr
= resource_init(&texture
->resource
, device
, desc
->resource_type
, format
,
2367 desc
->multisample_type
, desc
->multisample_quality
, desc
->usage
, desc
->access
,
2368 desc
->width
, desc
->height
, desc
->depth
, offset
, parent
, parent_ops
, &texture_resource_ops
)))
2370 static unsigned int once
;
2372 /* DXTn 3D textures are not supported. Do not write the ERR for them. */
2373 if ((desc
->format
== WINED3DFMT_DXT1
|| desc
->format
== WINED3DFMT_DXT2
|| desc
->format
== WINED3DFMT_DXT3
2374 || desc
->format
== WINED3DFMT_DXT4
|| desc
->format
== WINED3DFMT_DXT5
)
2375 && !(format
->flags
[WINED3D_GL_RES_TYPE_TEX_2D
] & WINED3DFMT_FLAG_TEXTURE
)
2376 && desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
&& !once
++)
2377 ERR_(winediag
)("The application tried to create a DXTn texture, but the driver does not support them.\n");
2379 WARN("Failed to initialize resource, returning %#x\n", hr
);
2382 wined3d_resource_update_draw_binding(&texture
->resource
);
2383 if ((flags
& WINED3D_TEXTURE_CREATE_MAPPABLE
) || desc
->format
== WINED3DFMT_D16_LOCKABLE
)
2384 texture
->resource
.access
|= WINED3D_RESOURCE_ACCESS_MAP_R
| WINED3D_RESOURCE_ACCESS_MAP_W
;
2386 texture
->texture_ops
= texture_ops
;
2388 texture
->layer_count
= layer_count
;
2389 texture
->level_count
= level_count
;
2391 texture
->flags
|= WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
;
2392 if (flags
& WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
)
2393 texture
->flags
|= WINED3D_TEXTURE_PIN_SYSMEM
| WINED3D_TEXTURE_GET_DC_LENIENT
;
2394 if (flags
& (WINED3D_TEXTURE_CREATE_GET_DC
| WINED3D_TEXTURE_CREATE_GET_DC_LENIENT
))
2395 texture
->flags
|= WINED3D_TEXTURE_GET_DC
;
2396 if (flags
& WINED3D_TEXTURE_CREATE_DISCARD
)
2397 texture
->flags
|= WINED3D_TEXTURE_DISCARD
;
2398 if (flags
& WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS
)
2400 if (!(texture
->resource
.format_flags
& WINED3DFMT_FLAG_GEN_MIPMAP
))
2401 WARN("Format doesn't support mipmaps generation, "
2402 "ignoring WINED3D_TEXTURE_CREATE_GENERATE_MIPMAPS flag.\n");
2404 texture
->flags
|= WINED3D_TEXTURE_GENERATE_MIPMAPS
;
2407 list_init(&texture
->renderbuffers
);
2409 /* Precalculated scaling for 'faked' non power of two texture coords. */
2410 if (texture
->resource
.gl_type
== WINED3D_GL_RES_TYPE_TEX_RECT
)
2412 texture
->pow2_matrix
[0] = (float)desc
->width
;
2413 texture
->pow2_matrix
[5] = (float)desc
->height
;
2414 texture
->flags
&= ~(WINED3D_TEXTURE_POW2_MAT_IDENT
| WINED3D_TEXTURE_NORMALIZED_COORDS
);
2415 texture
->target
= GL_TEXTURE_RECTANGLE_ARB
;
2419 if (texture
->flags
& WINED3D_TEXTURE_COND_NP2_EMULATED
)
2421 texture
->pow2_matrix
[0] = (((float)desc
->width
) / ((float)pow2_width
));
2422 texture
->pow2_matrix
[5] = (((float)desc
->height
) / ((float)pow2_height
));
2423 texture
->flags
&= ~WINED3D_TEXTURE_POW2_MAT_IDENT
;
2427 texture
->pow2_matrix
[0] = 1.0f
;
2428 texture
->pow2_matrix
[5] = 1.0f
;
2430 if (desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
)
2432 texture
->target
= GL_TEXTURE_3D
;
2434 else if (desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
)
2436 texture
->target
= GL_TEXTURE_CUBE_MAP_ARB
;
2438 else if (desc
->multisample_type
&& gl_info
->supported
[ARB_TEXTURE_MULTISAMPLE
])
2440 if (layer_count
> 1)
2441 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE_ARRAY
;
2443 texture
->target
= GL_TEXTURE_2D_MULTISAMPLE
;
2447 if (layer_count
> 1)
2448 texture
->target
= GL_TEXTURE_2D_ARRAY
;
2450 texture
->target
= GL_TEXTURE_2D
;
2453 texture
->pow2_matrix
[10] = 1.0f
;
2454 texture
->pow2_matrix
[15] = 1.0f
;
2455 TRACE("x scale %.8e, y scale %.8e.\n", texture
->pow2_matrix
[0], texture
->pow2_matrix
[5]);
2457 if (wined3d_texture_use_pbo(texture
, gl_info
))
2459 if (desc
->resource_type
== WINED3D_RTYPE_TEXTURE_3D
2460 || (texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2461 wined3d_resource_free_sysmem(&texture
->resource
);
2462 texture
->resource
.map_binding
= WINED3D_LOCATION_BUFFER
;
2465 sub_count
= level_count
* layer_count
;
2466 if (sub_count
/ layer_count
!= level_count
)
2468 wined3d_texture_cleanup_sync(texture
);
2469 return E_OUTOFMEMORY
;
2472 if (desc
->usage
& WINED3DUSAGE_OVERLAY
)
2474 if (!(texture
->overlay_info
= heap_calloc(sub_count
, sizeof(*texture
->overlay_info
))))
2476 wined3d_texture_cleanup_sync(texture
);
2477 return E_OUTOFMEMORY
;
2480 for (i
= 0; i
< sub_count
; ++i
)
2482 list_init(&texture
->overlay_info
[i
].entry
);
2483 list_init(&texture
->overlay_info
[i
].overlays
);
2487 /* Generate all sub-resources. */
2488 for (i
= 0; i
< sub_count
; ++i
)
2490 struct wined3d_texture_sub_resource
*sub_resource
;
2492 sub_resource
= &texture
->sub_resources
[i
];
2493 sub_resource
->locations
= WINED3D_LOCATION_DISCARDED
;
2494 if (desc
->resource_type
!= WINED3D_RTYPE_TEXTURE_3D
2495 && !(texture
->resource
.usage
& WINED3DUSAGE_DEPTHSTENCIL
))
2497 wined3d_texture_validate_location(texture
, i
, WINED3D_LOCATION_SYSMEM
);
2498 wined3d_texture_invalidate_location(texture
, i
, ~WINED3D_LOCATION_SYSMEM
);
2501 if (FAILED(hr
= device_parent
->ops
->texture_sub_resource_created(device_parent
,
2502 desc
->resource_type
, texture
, i
, &sub_resource
->parent
, &sub_resource
->parent_ops
)))
2504 WARN("Failed to create sub-resource parent, hr %#x.\n", hr
);
2505 sub_resource
->parent
= NULL
;
2506 wined3d_texture_cleanup_sync(texture
);
2510 TRACE("parent %p, parent_ops %p.\n", sub_resource
->parent
, sub_resource
->parent_ops
);
2512 TRACE("Created sub-resource %u (level %u, layer %u).\n",
2513 i
, i
% texture
->level_count
, i
/ texture
->level_count
);
2515 if ((desc
->usage
& WINED3DUSAGE_OWNDC
) || (device
->wined3d
->flags
& WINED3D_NO3D
))
2517 struct wined3d_texture_idx texture_idx
= {texture
, i
};
2519 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
2520 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
2521 if (!texture
->dc_info
|| !texture
->dc_info
[i
].dc
)
2523 wined3d_texture_cleanup_sync(texture
);
2524 return WINED3DERR_INVALIDCALL
;
2532 /* This call just uploads data, the caller is responsible for binding the
2533 * correct texture. */
2534 /* Context activation is done by the caller. */
2535 static void texture3d_upload_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2536 const struct wined3d_context
*context
, const struct wined3d_format
*format
, const struct wined3d_box
*src_box
,
2537 const struct wined3d_const_bo_address
*data
, unsigned int row_pitch
, unsigned int slice_pitch
,
2538 unsigned int dst_x
, unsigned int dst_y
, unsigned int dst_z
, BOOL srgb
)
2540 unsigned int level
= sub_resource_idx
% texture
->level_count
;
2541 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2542 unsigned int dst_row_pitch
, dst_slice_pitch
;
2543 unsigned int update_w
, update_h
, update_d
;
2544 const BYTE
*addr
= data
->addr
;
2545 void *converted_mem
= NULL
;
2547 TRACE("texture %p, sub_resource_idx %u, context %p, format %s, src_box %s, data {%#x:%p}, "
2548 "row_pitch %#x, slice_pitch %#x, dst_x %u, dst_y %u, dst_z %u, srgb %#x.\n",
2549 texture
, sub_resource_idx
, context
, debug_d3dformat(format
->id
), debug_box(src_box
),
2550 data
->buffer_object
, data
->addr
, row_pitch
, slice_pitch
, dst_x
, dst_y
, dst_z
, srgb
);
2554 addr
+= src_box
->front
* slice_pitch
;
2555 addr
+= src_box
->top
* row_pitch
;
2556 addr
+= src_box
->left
* format
->byte_count
;
2558 update_w
= src_box
->right
- src_box
->left
;
2559 update_h
= src_box
->bottom
- src_box
->top
;
2560 update_d
= src_box
->back
- src_box
->front
;
2564 update_w
= wined3d_texture_get_level_width(texture
, level
);
2565 update_h
= wined3d_texture_get_level_height(texture
, level
);
2566 update_d
= wined3d_texture_get_level_depth(texture
, level
);
2569 if (format
->conv_byte_count
)
2571 if (data
->buffer_object
)
2572 ERR("Loading a converted texture from a PBO.\n");
2573 if (texture
->resource
.format_flags
& WINED3DFMT_FLAG_BLOCKS
)
2574 ERR("Converting a block-based format.\n");
2576 dst_row_pitch
= update_w
* format
->conv_byte_count
;
2577 dst_slice_pitch
= dst_row_pitch
* update_h
;
2579 converted_mem
= heap_calloc(update_d
, dst_slice_pitch
);
2580 format
->upload(addr
, converted_mem
, row_pitch
, slice_pitch
,
2581 dst_row_pitch
, dst_slice_pitch
, update_w
, update_h
, update_d
);
2582 addr
= converted_mem
;
2586 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &dst_row_pitch
, &dst_slice_pitch
);
2587 if (row_pitch
!= dst_row_pitch
|| slice_pitch
!= dst_slice_pitch
)
2588 FIXME("Ignoring row/slice pitch (%u/%u).\n", row_pitch
, slice_pitch
);
2591 if (data
->buffer_object
)
2593 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, data
->buffer_object
));
2594 checkGLcall("glBindBuffer");
2597 GL_EXTCALL(glTexSubImage3D(GL_TEXTURE_3D
, level
, dst_x
, dst_y
, dst_z
,
2598 update_w
, update_h
, update_d
, format
->glFormat
, format
->glType
, addr
));
2599 checkGLcall("glTexSubImage3D");
2601 if (data
->buffer_object
)
2603 GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER
, 0));
2604 checkGLcall("glBindBuffer");
2607 heap_free(converted_mem
);
2610 /* Context activation is done by the caller. */
2611 static void texture3d_download_data(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2612 const struct wined3d_context
*context
, const struct wined3d_bo_address
*data
)
2614 const struct wined3d_format
*format
= texture
->resource
.format
;
2615 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
2617 if (format
->conv_byte_count
)
2619 FIXME("Attempting to download a converted volume, format %s.\n",
2620 debug_d3dformat(format
->id
));
2624 if (data
->buffer_object
)
2626 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, data
->buffer_object
));
2627 checkGLcall("glBindBuffer");
2630 gl_info
->gl_ops
.gl
.p_glGetTexImage(GL_TEXTURE_3D
, sub_resource_idx
,
2631 format
->glFormat
, format
->glType
, data
->addr
);
2632 checkGLcall("glGetTexImage");
2634 if (data
->buffer_object
)
2636 GL_EXTCALL(glBindBuffer(GL_PIXEL_PACK_BUFFER
, 0));
2637 checkGLcall("glBindBuffer");
2642 /* Context activation is done by the caller. */
2643 static void texture3d_srgb_transfer(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2644 struct wined3d_context
*context
, BOOL dest_is_srgb
)
2646 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2647 unsigned int row_pitch
, slice_pitch
;
2648 struct wined3d_bo_address data
;
2650 /* Optimisations are possible, but the effort should be put into either
2651 * implementing EXT_SRGB_DECODE in the driver or finding out why we
2652 * picked the wrong copy for the original upload and fixing that.
2654 * Also keep in mind that we want to avoid using resource.heap_memory
2655 * for DEFAULT pool surfaces. */
2656 WARN_(d3d_perf
)("Performing slow rgb/srgb volume transfer.\n");
2657 data
.buffer_object
= 0;
2658 if (!(data
.addr
= heap_alloc(sub_resource
->size
)))
2661 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2662 wined3d_texture_bind_and_dirtify(texture
, context
, !dest_is_srgb
);
2663 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2664 wined3d_texture_bind_and_dirtify(texture
, context
, dest_is_srgb
);
2665 texture3d_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
2666 NULL
, wined3d_const_bo_address(&data
), row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
2668 heap_free(data
.addr
);
2671 /* Context activation is done by the caller. */
2672 static BOOL
texture3d_load_location(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2673 struct wined3d_context
*context
, DWORD location
)
2675 struct wined3d_texture_sub_resource
*sub_resource
= &texture
->sub_resources
[sub_resource_idx
];
2676 unsigned int row_pitch
, slice_pitch
;
2678 if (!wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
))
2683 case WINED3D_LOCATION_TEXTURE_RGB
:
2684 case WINED3D_LOCATION_TEXTURE_SRGB
:
2685 if (sub_resource
->locations
& WINED3D_LOCATION_SYSMEM
)
2687 struct wined3d_const_bo_address data
= {0, texture
->resource
.heap_memory
};
2688 data
.addr
+= sub_resource
->offset
;
2689 wined3d_texture_bind_and_dirtify(texture
, context
,
2690 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2691 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2692 texture3d_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
2693 NULL
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
2695 else if (sub_resource
->locations
& WINED3D_LOCATION_BUFFER
)
2697 struct wined3d_const_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2698 wined3d_texture_bind_and_dirtify(texture
, context
,
2699 location
== WINED3D_LOCATION_TEXTURE_SRGB
);
2700 wined3d_texture_get_pitch(texture
, sub_resource_idx
, &row_pitch
, &slice_pitch
);
2701 texture3d_upload_data(texture
, sub_resource_idx
, context
, texture
->resource
.format
,
2702 NULL
, &data
, row_pitch
, slice_pitch
, 0, 0, 0, FALSE
);
2704 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2706 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, TRUE
);
2708 else if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_SRGB
)
2710 texture3d_srgb_transfer(texture
, sub_resource_idx
, context
, FALSE
);
2714 FIXME("Implement texture loading from %s.\n", wined3d_debug_location(sub_resource
->locations
));
2719 case WINED3D_LOCATION_SYSMEM
:
2720 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2722 struct wined3d_bo_address data
= {0, texture
->resource
.heap_memory
};
2724 data
.addr
+= sub_resource
->offset
;
2725 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2726 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2728 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2730 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2731 ++texture
->download_count
;
2735 FIXME("Implement WINED3D_LOCATION_SYSMEM loading from %s.\n",
2736 wined3d_debug_location(sub_resource
->locations
));
2741 case WINED3D_LOCATION_BUFFER
:
2742 if (sub_resource
->locations
& (WINED3D_LOCATION_TEXTURE_RGB
| WINED3D_LOCATION_TEXTURE_SRGB
))
2744 struct wined3d_bo_address data
= {sub_resource
->buffer_object
, NULL
};
2746 if (sub_resource
->locations
& WINED3D_LOCATION_TEXTURE_RGB
)
2747 wined3d_texture_bind_and_dirtify(texture
, context
, FALSE
);
2749 wined3d_texture_bind_and_dirtify(texture
, context
, TRUE
);
2751 texture3d_download_data(texture
, sub_resource_idx
, context
, &data
);
2755 FIXME("Implement WINED3D_LOCATION_BUFFER loading from %s.\n",
2756 wined3d_debug_location(sub_resource
->locations
));
2762 FIXME("Implement %s loading from %s.\n", wined3d_debug_location(location
),
2763 wined3d_debug_location(sub_resource
->locations
));
2770 static const struct wined3d_texture_ops texture3d_ops
=
2772 texture3d_upload_data
,
2773 texture3d_load_location
,
2776 HRESULT CDECL
wined3d_texture_blt(struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2777 const RECT
*dst_rect
, struct wined3d_texture
*src_texture
, unsigned int src_sub_resource_idx
,
2778 const RECT
*src_rect
, DWORD flags
, const struct wined3d_blt_fx
*fx
, enum wined3d_texture_filter_type filter
)
2780 struct wined3d_box src_box
= {src_rect
->left
, src_rect
->top
, src_rect
->right
, src_rect
->bottom
, 0, 1};
2781 struct wined3d_box dst_box
= {dst_rect
->left
, dst_rect
->top
, dst_rect
->right
, dst_rect
->bottom
, 0, 1};
2782 unsigned int dst_format_flags
, src_format_flags
= 0;
2785 TRACE("dst_texture %p, dst_sub_resource_idx %u, dst_rect %s, src_texture %p, "
2786 "src_sub_resource_idx %u, src_rect %s, flags %#x, fx %p, filter %s.\n",
2787 dst_texture
, dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), src_texture
,
2788 src_sub_resource_idx
, wine_dbgstr_rect(src_rect
), flags
, fx
, debug_d3dtexturefiltertype(filter
));
2790 if (dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
2791 || dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2792 return WINED3DERR_INVALIDCALL
;
2794 if (src_sub_resource_idx
>= src_texture
->level_count
* src_texture
->layer_count
2795 || src_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
2796 return WINED3DERR_INVALIDCALL
;
2798 dst_format_flags
= dst_texture
->resource
.format_flags
;
2799 if (FAILED(hr
= wined3d_texture_check_box_dimensions(dst_texture
,
2800 dst_sub_resource_idx
% dst_texture
->level_count
, &dst_box
)))
2803 src_format_flags
= src_texture
->resource
.format_flags
;
2804 if (FAILED(hr
= wined3d_texture_check_box_dimensions(src_texture
,
2805 src_sub_resource_idx
% src_texture
->level_count
, &src_box
)))
2808 if (dst_texture
->sub_resources
[dst_sub_resource_idx
].map_count
2809 || src_texture
->sub_resources
[src_sub_resource_idx
].map_count
)
2811 WARN("Sub-resource is busy, returning WINEDDERR_SURFACEBUSY.\n");
2812 return WINEDDERR_SURFACEBUSY
;
2815 if ((src_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
))
2816 != (dst_format_flags
& (WINED3DFMT_FLAG_DEPTH
| WINED3DFMT_FLAG_STENCIL
)))
2818 WARN("Rejecting depth/stencil blit between incompatible formats.\n");
2819 return WINED3DERR_INVALIDCALL
;
2822 wined3d_cs_emit_blt_sub_resource(dst_texture
->resource
.device
->cs
, &dst_texture
->resource
, dst_sub_resource_idx
,
2823 &dst_box
, &src_texture
->resource
, src_sub_resource_idx
, &src_box
, flags
, fx
, filter
);
2828 HRESULT CDECL
wined3d_texture_get_overlay_position(const struct wined3d_texture
*texture
,
2829 unsigned int sub_resource_idx
, LONG
*x
, LONG
*y
)
2831 struct wined3d_overlay_info
*overlay
;
2833 TRACE("texture %p, sub_resource_idx %u, x %p, y %p.\n", texture
, sub_resource_idx
, x
, y
);
2835 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
2836 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2838 WARN("Invalid sub-resource specified.\n");
2839 return WINEDDERR_NOTAOVERLAYSURFACE
;
2842 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2843 if (!overlay
->dst_texture
)
2845 TRACE("Overlay not visible.\n");
2848 return WINEDDERR_OVERLAYNOTVISIBLE
;
2851 *x
= overlay
->dst_rect
.left
;
2852 *y
= overlay
->dst_rect
.top
;
2854 TRACE("Returning position %d, %d.\n", *x
, *y
);
2859 HRESULT CDECL
wined3d_texture_set_overlay_position(struct wined3d_texture
*texture
,
2860 unsigned int sub_resource_idx
, LONG x
, LONG y
)
2862 struct wined3d_overlay_info
*overlay
;
2865 TRACE("texture %p, sub_resource_idx %u, x %d, y %d.\n", texture
, sub_resource_idx
, x
, y
);
2867 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
)
2868 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2870 WARN("Invalid sub-resource specified.\n");
2871 return WINEDDERR_NOTAOVERLAYSURFACE
;
2874 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2875 w
= overlay
->dst_rect
.right
- overlay
->dst_rect
.left
;
2876 h
= overlay
->dst_rect
.bottom
- overlay
->dst_rect
.top
;
2877 SetRect(&overlay
->dst_rect
, x
, y
, x
+ w
, y
+ h
);
2882 HRESULT CDECL
wined3d_texture_update_overlay(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
,
2883 const RECT
*src_rect
, struct wined3d_texture
*dst_texture
, unsigned int dst_sub_resource_idx
,
2884 const RECT
*dst_rect
, DWORD flags
)
2886 struct wined3d_overlay_info
*overlay
;
2887 unsigned int level
, dst_level
;
2889 TRACE("texture %p, sub_resource_idx %u, src_rect %s, dst_texture %p, "
2890 "dst_sub_resource_idx %u, dst_rect %s, flags %#x.\n",
2891 texture
, sub_resource_idx
, wine_dbgstr_rect(src_rect
), dst_texture
,
2892 dst_sub_resource_idx
, wine_dbgstr_rect(dst_rect
), flags
);
2894 if (!(texture
->resource
.usage
& WINED3DUSAGE_OVERLAY
) || texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2895 || sub_resource_idx
>= texture
->level_count
* texture
->layer_count
)
2897 WARN("Invalid sub-resource specified.\n");
2898 return WINEDDERR_NOTAOVERLAYSURFACE
;
2901 if (!dst_texture
|| dst_texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
2902 || dst_sub_resource_idx
>= dst_texture
->level_count
* dst_texture
->layer_count
)
2904 WARN("Invalid destination sub-resource specified.\n");
2905 return WINED3DERR_INVALIDCALL
;
2908 overlay
= &texture
->overlay_info
[sub_resource_idx
];
2910 level
= sub_resource_idx
% texture
->level_count
;
2912 overlay
->src_rect
= *src_rect
;
2914 SetRect(&overlay
->src_rect
, 0, 0,
2915 wined3d_texture_get_level_width(texture
, level
),
2916 wined3d_texture_get_level_height(texture
, level
));
2918 dst_level
= dst_sub_resource_idx
% dst_texture
->level_count
;
2920 overlay
->dst_rect
= *dst_rect
;
2922 SetRect(&overlay
->dst_rect
, 0, 0,
2923 wined3d_texture_get_level_width(dst_texture
, dst_level
),
2924 wined3d_texture_get_level_height(dst_texture
, dst_level
));
2926 if (overlay
->dst_texture
&& (overlay
->dst_texture
!= dst_texture
2927 || overlay
->dst_sub_resource_idx
!= dst_sub_resource_idx
|| flags
& WINEDDOVER_HIDE
))
2929 overlay
->dst_texture
= NULL
;
2930 list_remove(&overlay
->entry
);
2933 if (flags
& WINEDDOVER_SHOW
)
2935 if (overlay
->dst_texture
!= dst_texture
|| overlay
->dst_sub_resource_idx
!= dst_sub_resource_idx
)
2937 overlay
->dst_texture
= dst_texture
;
2938 overlay
->dst_sub_resource_idx
= dst_sub_resource_idx
;
2939 list_add_tail(&texture
->overlay_info
[dst_sub_resource_idx
].overlays
, &overlay
->entry
);
2942 else if (flags
& WINEDDOVER_HIDE
)
2944 /* Tests show that the rectangles are erased on hide. */
2945 SetRectEmpty(&overlay
->src_rect
);
2946 SetRectEmpty(&overlay
->dst_rect
);
2947 overlay
->dst_texture
= NULL
;
2953 void * CDECL
wined3d_texture_get_sub_resource_parent(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
)
2955 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2957 TRACE("texture %p, sub_resource_idx %u.\n", texture
, sub_resource_idx
);
2959 if (sub_resource_idx
>= sub_count
)
2961 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2965 return texture
->sub_resources
[sub_resource_idx
].parent
;
2968 void CDECL
wined3d_texture_set_sub_resource_parent(struct wined3d_texture
*texture
,
2969 unsigned int sub_resource_idx
, void *parent
)
2971 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2973 TRACE("texture %p, sub_resource_idx %u, parent %p.\n", texture
, sub_resource_idx
, parent
);
2975 if (sub_resource_idx
>= sub_count
)
2977 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2981 texture
->sub_resources
[sub_resource_idx
].parent
= parent
;
2984 HRESULT CDECL
wined3d_texture_get_sub_resource_desc(const struct wined3d_texture
*texture
,
2985 unsigned int sub_resource_idx
, struct wined3d_sub_resource_desc
*desc
)
2987 unsigned int sub_count
= texture
->level_count
* texture
->layer_count
;
2988 const struct wined3d_resource
*resource
;
2989 unsigned int level_idx
;
2991 TRACE("texture %p, sub_resource_idx %u, desc %p.\n", texture
, sub_resource_idx
, desc
);
2993 if (sub_resource_idx
>= sub_count
)
2995 WARN("sub_resource_idx %u >= sub_count %u.\n", sub_resource_idx
, sub_count
);
2996 return WINED3DERR_INVALIDCALL
;
2999 resource
= &texture
->resource
;
3000 desc
->format
= resource
->format
->id
;
3001 desc
->multisample_type
= resource
->multisample_type
;
3002 desc
->multisample_quality
= resource
->multisample_quality
;
3003 desc
->usage
= resource
->usage
;
3004 desc
->access
= resource
->access
;
3006 level_idx
= sub_resource_idx
% texture
->level_count
;
3007 desc
->width
= wined3d_texture_get_level_width(texture
, level_idx
);
3008 desc
->height
= wined3d_texture_get_level_height(texture
, level_idx
);
3009 desc
->depth
= wined3d_texture_get_level_depth(texture
, level_idx
);
3010 desc
->size
= texture
->sub_resources
[sub_resource_idx
].size
;
3015 HRESULT CDECL
wined3d_texture_create(struct wined3d_device
*device
, const struct wined3d_resource_desc
*desc
,
3016 UINT layer_count
, UINT level_count
, DWORD flags
, const struct wined3d_sub_resource_data
*data
,
3017 void *parent
, const struct wined3d_parent_ops
*parent_ops
, struct wined3d_texture
**texture
)
3019 struct wined3d_texture
*object
;
3022 TRACE("device %p, desc %p, layer_count %u, level_count %u, flags %#x, data %p, "
3023 "parent %p, parent_ops %p, texture %p.\n",
3024 device
, desc
, layer_count
, level_count
, flags
, data
, parent
, parent_ops
, texture
);
3028 WARN("Invalid layer count.\n");
3029 return E_INVALIDARG
;
3031 if ((desc
->usage
& WINED3DUSAGE_LEGACY_CUBEMAP
) && layer_count
!= 6)
3033 ERR("Invalid layer count %u for legacy cubemap.\n", layer_count
);
3039 WARN("Invalid level count.\n");
3040 return WINED3DERR_INVALIDCALL
;
3043 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NONE
)
3045 const struct wined3d_format
*format
= wined3d_get_format(&device
->adapter
->gl_info
,
3046 desc
->format
, desc
->usage
);
3048 if (desc
->multisample_type
== WINED3D_MULTISAMPLE_NON_MASKABLE
3049 && desc
->multisample_quality
>= wined3d_popcount(format
->multisample_types
))
3051 WARN("Unsupported quality level %u requested for WINED3D_MULTISAMPLE_NON_MASKABLE.\n",
3052 desc
->multisample_quality
);
3053 return WINED3DERR_NOTAVAILABLE
;
3055 if (desc
->multisample_type
!= WINED3D_MULTISAMPLE_NON_MASKABLE
3056 && (!(format
->multisample_types
& 1u << (desc
->multisample_type
- 1))
3057 || desc
->multisample_quality
))
3059 WARN("Unsupported multisample type %u quality %u requested.\n", desc
->multisample_type
,
3060 desc
->multisample_quality
);
3061 return WINED3DERR_NOTAVAILABLE
;
3065 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d_texture
,
3066 sub_resources
[level_count
* layer_count
]))))
3067 return E_OUTOFMEMORY
;
3069 switch (desc
->resource_type
)
3071 case WINED3D_RTYPE_TEXTURE_2D
:
3072 hr
= wined3d_texture_init(object
, desc
, layer_count
, level_count
,
3073 flags
, device
, parent
, parent_ops
, &texture2d_ops
);
3076 case WINED3D_RTYPE_TEXTURE_3D
:
3077 hr
= wined3d_texture_init(object
, desc
, layer_count
, level_count
,
3078 flags
, device
, parent
, parent_ops
, &texture3d_ops
);
3082 ERR("Invalid resource type %s.\n", debug_d3dresourcetype(desc
->resource_type
));
3083 hr
= WINED3DERR_INVALIDCALL
;
3089 WARN("Failed to initialize texture, returning %#x.\n", hr
);
3094 /* FIXME: We'd like to avoid ever allocating system memory for the texture
3098 unsigned int sub_count
= level_count
* layer_count
;
3101 for (i
= 0; i
< sub_count
; ++i
)
3105 WARN("Invalid sub-resource data specified for sub-resource %u.\n", i
);
3106 wined3d_texture_cleanup_sync(object
);
3108 return E_INVALIDARG
;
3112 for (i
= 0; i
< sub_count
; ++i
)
3114 wined3d_device_update_sub_resource(device
, &object
->resource
,
3115 i
, NULL
, data
[i
].data
, data
[i
].row_pitch
, data
[i
].slice_pitch
);
3119 TRACE("Created texture %p.\n", object
);
3125 HRESULT CDECL
wined3d_texture_get_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC
*dc
)
3127 struct wined3d_device
*device
= texture
->resource
.device
;
3128 struct wined3d_texture_sub_resource
*sub_resource
;
3129 struct wined3d_dc_info
*dc_info
;
3131 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3133 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC
))
3135 WARN("Texture does not support GetDC\n");
3136 /* Don't touch the DC */
3137 return WINED3DERR_INVALIDCALL
;
3140 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3141 return WINED3DERR_INVALIDCALL
;
3143 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3145 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3146 return WINED3DERR_INVALIDCALL
;
3149 if (texture
->resource
.map_count
&& !(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3150 return WINED3DERR_INVALIDCALL
;
3152 if (!(dc_info
= texture
->dc_info
) || !dc_info
[sub_resource_idx
].dc
)
3154 struct wined3d_texture_idx texture_idx
= {texture
, sub_resource_idx
};
3156 wined3d_cs_init_object(device
->cs
, wined3d_texture_create_dc
, &texture_idx
);
3157 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3158 if (!(dc_info
= texture
->dc_info
) || !dc_info
[sub_resource_idx
].dc
)
3159 return WINED3DERR_INVALIDCALL
;
3162 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3163 texture
->flags
|= WINED3D_TEXTURE_DC_IN_USE
;
3164 ++texture
->resource
.map_count
;
3165 ++sub_resource
->map_count
;
3167 *dc
= dc_info
[sub_resource_idx
].dc
;
3168 TRACE("Returning dc %p.\n", *dc
);
3173 HRESULT CDECL
wined3d_texture_release_dc(struct wined3d_texture
*texture
, unsigned int sub_resource_idx
, HDC dc
)
3175 struct wined3d_device
*device
= texture
->resource
.device
;
3176 struct wined3d_texture_sub_resource
*sub_resource
;
3177 struct wined3d_dc_info
*dc_info
;
3179 TRACE("texture %p, sub_resource_idx %u, dc %p.\n", texture
, sub_resource_idx
, dc
);
3181 if (!(sub_resource
= wined3d_texture_get_sub_resource(texture
, sub_resource_idx
)))
3182 return WINED3DERR_INVALIDCALL
;
3184 if (texture
->resource
.type
!= WINED3D_RTYPE_TEXTURE_2D
)
3186 WARN("Not supported on %s resources.\n", debug_d3dresourcetype(texture
->resource
.type
));
3187 return WINED3DERR_INVALIDCALL
;
3190 if (!(texture
->flags
& (WINED3D_TEXTURE_GET_DC_LENIENT
| WINED3D_TEXTURE_DC_IN_USE
)))
3191 return WINED3DERR_INVALIDCALL
;
3193 if (!(dc_info
= texture
->dc_info
) || dc_info
[sub_resource_idx
].dc
!= dc
)
3195 WARN("Application tries to release invalid DC %p, sub-resource DC is %p.\n",
3196 dc
, dc_info
? dc_info
[sub_resource_idx
].dc
: NULL
);
3197 return WINED3DERR_INVALIDCALL
;
3200 if (!(texture
->resource
.usage
& WINED3DUSAGE_OWNDC
) && !(device
->wined3d
->flags
& WINED3D_NO3D
))
3202 struct wined3d_texture_idx texture_idx
= {texture
, sub_resource_idx
};
3204 wined3d_cs_destroy_object(device
->cs
, wined3d_texture_destroy_dc
, &texture_idx
);
3205 device
->cs
->ops
->finish(device
->cs
, WINED3D_CS_QUEUE_DEFAULT
);
3208 --sub_resource
->map_count
;
3209 if (!--texture
->resource
.map_count
&& texture
->update_map_binding
)
3210 wined3d_texture_update_map_binding(texture
);
3211 if (!(texture
->flags
& WINED3D_TEXTURE_GET_DC_LENIENT
))
3212 texture
->flags
&= ~WINED3D_TEXTURE_DC_IN_USE
;