2 * Copyright 2009, 2011 Henri Verbeet for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wined3d_private.h"
23 #include "wined3d_shaders.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
27 #define WINED3D_VIEW_CUBE_ARRAY (WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY)
29 static BOOL
is_stencil_view_format(const struct wined3d_format
*format
)
31 return format
->id
== WINED3DFMT_X24_TYPELESS_G8_UINT
32 || format
->id
== WINED3DFMT_X32_TYPELESS_G8X24_UINT
;
35 static GLenum
get_texture_view_target(const struct wined3d_gl_info
*gl_info
,
36 const struct wined3d_view_desc
*desc
, const struct wined3d_texture_gl
*texture_gl
)
40 GLenum texture_target
;
41 unsigned int view_flags
;
43 enum wined3d_gl_extension extension
;
47 {GL_TEXTURE_CUBE_MAP
, 0, GL_TEXTURE_CUBE_MAP
},
48 {GL_TEXTURE_RECTANGLE
, 0, GL_TEXTURE_RECTANGLE
},
49 {GL_TEXTURE_3D
, 0, GL_TEXTURE_3D
},
51 {GL_TEXTURE_2D
, 0, GL_TEXTURE_2D
},
52 {GL_TEXTURE_2D
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_ARRAY
},
53 {GL_TEXTURE_2D_ARRAY
, 0, GL_TEXTURE_2D
},
54 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_ARRAY
},
55 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_TEXTURE_CUBE
, GL_TEXTURE_CUBE_MAP
},
56 {GL_TEXTURE_2D_ARRAY
, WINED3D_VIEW_CUBE_ARRAY
, GL_TEXTURE_CUBE_MAP_ARRAY
, ARB_TEXTURE_CUBE_MAP_ARRAY
},
58 {GL_TEXTURE_2D_MULTISAMPLE
, 0, GL_TEXTURE_2D_MULTISAMPLE
},
59 {GL_TEXTURE_2D_MULTISAMPLE
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_MULTISAMPLE_ARRAY
},
60 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, 0, GL_TEXTURE_2D_MULTISAMPLE
},
61 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_2D_MULTISAMPLE_ARRAY
},
63 {GL_TEXTURE_1D
, 0, GL_TEXTURE_1D
},
64 {GL_TEXTURE_1D
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_1D_ARRAY
},
65 {GL_TEXTURE_1D_ARRAY
, 0, GL_TEXTURE_1D
},
66 {GL_TEXTURE_1D_ARRAY
, WINED3D_VIEW_TEXTURE_ARRAY
, GL_TEXTURE_1D_ARRAY
},
68 unsigned int flags
= desc
->flags
& (WINED3D_VIEW_BUFFER_RAW
| WINED3D_VIEW_BUFFER_APPEND
69 | WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_TEXTURE_CUBE
| WINED3D_VIEW_TEXTURE_ARRAY
);
72 for (i
= 0; i
< ARRAY_SIZE(view_types
); ++i
)
74 if (view_types
[i
].texture_target
!= texture_gl
->target
|| view_types
[i
].view_flags
!= flags
)
76 if (gl_info
->supported
[view_types
[i
].extension
])
77 return view_types
[i
].view_target
;
79 FIXME("Extension %#x not supported.\n", view_types
[i
].extension
);
82 FIXME("Unhandled view flags %#x for texture target %#x.\n", flags
, texture_gl
->target
);
83 return texture_gl
->target
;
86 static const struct wined3d_format
*validate_resource_view(const struct wined3d_view_desc
*desc
,
87 struct wined3d_resource
*resource
, BOOL mip_slice
, BOOL allow_srgb_toggle
)
89 const struct wined3d_adapter
*adapter
= resource
->device
->adapter
;
90 const struct wined3d_format
*format
;
92 format
= wined3d_get_format(adapter
, desc
->format_id
, resource
->bind_flags
);
93 if (resource
->type
== WINED3D_RTYPE_BUFFER
&& (desc
->flags
& WINED3D_VIEW_BUFFER_RAW
))
95 if (format
->id
!= WINED3DFMT_R32_TYPELESS
)
97 WARN("Invalid format %s for raw buffer view.\n", debug_d3dformat(format
->id
));
101 format
= wined3d_get_format(adapter
, WINED3DFMT_R32_UINT
, resource
->bind_flags
);
104 if (wined3d_format_is_typeless(format
))
106 WARN("Trying to create view for typeless format %s.\n", debug_d3dformat(format
->id
));
110 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
112 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
113 unsigned int buffer_size
, element_size
;
115 if (buffer
->structure_byte_stride
)
117 if (desc
->format_id
!= WINED3DFMT_UNKNOWN
)
119 WARN("Invalid format %s for structured buffer view.\n", debug_d3dformat(desc
->format_id
));
123 format
= wined3d_get_format(adapter
, WINED3DFMT_R32_UINT
, resource
->bind_flags
);
124 element_size
= buffer
->structure_byte_stride
;
128 element_size
= format
->byte_count
;
134 buffer_size
= buffer
->resource
.size
/ element_size
;
135 if (!wined3d_bound_range(desc
->u
.buffer
.start_idx
, desc
->u
.buffer
.count
, buffer_size
))
140 struct wined3d_texture
*texture
= texture_from_resource(resource
);
141 unsigned int depth_or_layer_count
;
143 if (resource
->format
->id
!= format
->id
&& !wined3d_format_is_typeless(resource
->format
)
144 && (!allow_srgb_toggle
|| !wined3d_formats_are_srgb_variants(resource
->format
->id
, format
->id
)))
146 WARN("Trying to create incompatible view for non typeless format %s.\n",
147 debug_d3dformat(format
->id
));
151 if (mip_slice
&& resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
152 depth_or_layer_count
= wined3d_texture_get_level_depth(texture
, desc
->u
.texture
.level_idx
);
154 depth_or_layer_count
= texture
->layer_count
;
156 if (!desc
->u
.texture
.level_count
157 || (mip_slice
&& desc
->u
.texture
.level_count
!= 1)
158 || !wined3d_bound_range(desc
->u
.texture
.level_idx
, desc
->u
.texture
.level_count
, texture
->level_count
)
159 || !desc
->u
.texture
.layer_count
160 || !wined3d_bound_range(desc
->u
.texture
.layer_idx
, desc
->u
.texture
.layer_count
, depth_or_layer_count
))
167 static void create_texture_view(struct wined3d_gl_view
*view
, GLenum view_target
,
168 const struct wined3d_view_desc
*desc
, struct wined3d_texture_gl
*texture_gl
,
169 const struct wined3d_format
*view_format
)
171 const struct wined3d_format_gl
*view_format_gl
;
172 unsigned int level_idx
, layer_idx
, layer_count
;
173 const struct wined3d_gl_info
*gl_info
;
174 struct wined3d_context_gl
*context_gl
;
175 struct wined3d_context
*context
;
178 view_format_gl
= wined3d_format_gl(view_format
);
179 view
->target
= view_target
;
181 context
= context_acquire(texture_gl
->t
.resource
.device
, NULL
, 0);
182 context_gl
= wined3d_context_gl(context
);
183 gl_info
= context_gl
->gl_info
;
185 if (!gl_info
->supported
[ARB_TEXTURE_VIEW
])
187 context_release(context
);
188 FIXME("OpenGL implementation does not support texture views.\n");
192 wined3d_texture_gl_prepare_texture(texture_gl
, context_gl
, FALSE
);
193 texture_name
= wined3d_texture_gl_get_texture_name(texture_gl
, context
, FALSE
);
195 level_idx
= desc
->u
.texture
.level_idx
;
196 layer_idx
= desc
->u
.texture
.layer_idx
;
197 layer_count
= desc
->u
.texture
.layer_count
;
198 if (view_target
== GL_TEXTURE_3D
)
200 if (layer_idx
|| layer_count
!= wined3d_texture_get_level_depth(&texture_gl
->t
, level_idx
))
201 FIXME("Depth slice (%u-%u) not supported.\n", layer_idx
, layer_count
);
206 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &view
->name
);
207 GL_EXTCALL(glTextureView(view
->name
, view
->target
, texture_name
, view_format_gl
->internal
,
208 level_idx
, desc
->u
.texture
.level_count
, layer_idx
, layer_count
));
209 checkGLcall("create texture view");
211 if (is_stencil_view_format(view_format
))
213 static const GLint swizzle
[] = {GL_ZERO
, GL_RED
, GL_ZERO
, GL_ZERO
};
215 if (!gl_info
->supported
[ARB_STENCIL_TEXTURING
])
217 context_release(context
);
218 FIXME("OpenGL implementation does not support stencil texturing.\n");
222 wined3d_context_gl_bind_texture(context_gl
, view
->target
, view
->name
);
223 gl_info
->gl_ops
.gl
.p_glTexParameteriv(view
->target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
);
224 gl_info
->gl_ops
.gl
.p_glTexParameteri(view
->target
, GL_DEPTH_STENCIL_TEXTURE_MODE
, GL_STENCIL_INDEX
);
225 checkGLcall("initialize stencil view");
227 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
228 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
230 else if (!is_identity_fixup(view_format
->color_fixup
) && can_use_texture_swizzle(context
->d3d_info
, view_format
))
234 wined3d_context_gl_bind_texture(context_gl
, view
->target
, view
->name
);
235 wined3d_gl_texture_swizzle_from_color_fixup(swizzle
, view_format
->color_fixup
);
236 gl_info
->gl_ops
.gl
.p_glTexParameteriv(view
->target
, GL_TEXTURE_SWIZZLE_RGBA
, swizzle
);
237 checkGLcall("set format swizzle");
239 context_invalidate_compute_state(context
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
240 context_invalidate_state(context
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
243 context_release(context
);
246 static void create_buffer_texture(struct wined3d_gl_view
*view
, struct wined3d_context_gl
*context_gl
,
247 struct wined3d_buffer
*buffer
, const struct wined3d_format_gl
*view_format_gl
,
248 unsigned int offset
, unsigned int size
)
250 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
251 const struct wined3d_bo_gl
*bo_gl
;
253 if (!gl_info
->supported
[ARB_TEXTURE_BUFFER_OBJECT
])
255 FIXME("OpenGL implementation does not support buffer textures.\n");
259 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
260 bo_gl
= wined3d_bo_gl(buffer
->buffer_object
);
261 offset
+= bo_gl
->b
.buffer_offset
;
263 if ((offset
& (gl_info
->limits
.texture_buffer_offset_alignment
- 1)))
265 FIXME("Buffer offset %u is not %u byte aligned.\n",
266 offset
, gl_info
->limits
.texture_buffer_offset_alignment
);
270 view
->target
= GL_TEXTURE_BUFFER
;
272 gl_info
->gl_ops
.gl
.p_glGenTextures(1, &view
->name
);
274 wined3d_context_gl_bind_texture(context_gl
, GL_TEXTURE_BUFFER
, view
->name
);
275 if (gl_info
->supported
[ARB_TEXTURE_BUFFER_RANGE
])
277 GL_EXTCALL(glTexBufferRange(GL_TEXTURE_BUFFER
, view_format_gl
->internal
, bo_gl
->id
, offset
, size
));
281 if (offset
|| size
!= buffer
->resource
.size
)
282 FIXME("OpenGL implementation does not support ARB_texture_buffer_range.\n");
283 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER
, view_format_gl
->internal
, bo_gl
->id
));
285 checkGLcall("Create buffer texture");
287 context_invalidate_compute_state(&context_gl
->c
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
288 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
291 static void get_buffer_view_range(const struct wined3d_buffer
*buffer
,
292 const struct wined3d_view_desc
*desc
, const struct wined3d_format
*view_format
,
293 unsigned int *offset
, unsigned int *size
)
295 if (desc
->format_id
== WINED3DFMT_UNKNOWN
)
297 *offset
= desc
->u
.buffer
.start_idx
* buffer
->structure_byte_stride
;
298 *size
= desc
->u
.buffer
.count
* buffer
->structure_byte_stride
;
302 *offset
= desc
->u
.buffer
.start_idx
* view_format
->byte_count
;
303 *size
= desc
->u
.buffer
.count
* view_format
->byte_count
;
307 static void create_buffer_view(struct wined3d_gl_view
*view
, struct wined3d_context
*context
,
308 const struct wined3d_view_desc
*desc
, struct wined3d_buffer
*buffer
,
309 const struct wined3d_format
*view_format
)
311 unsigned int offset
, size
;
313 get_buffer_view_range(buffer
, desc
, view_format
, &offset
, &size
);
314 create_buffer_texture(view
, wined3d_context_gl(context
), buffer
, wined3d_format_gl(view_format
), offset
, size
);
317 static void wined3d_view_invalidate_location(struct wined3d_resource
*resource
,
318 const struct wined3d_view_desc
*desc
, DWORD location
)
320 unsigned int i
, sub_resource_idx
, layer_count
;
321 struct wined3d_texture
*texture
;
323 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
325 wined3d_buffer_invalidate_location(buffer_from_resource(resource
), location
);
329 texture
= texture_from_resource(resource
);
331 sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
332 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? desc
->u
.texture
.layer_count
: 1;
333 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
334 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, location
);
337 static void wined3d_view_load_location(struct wined3d_resource
*resource
,
338 const struct wined3d_view_desc
*desc
, struct wined3d_context
*context
, DWORD location
)
340 unsigned int i
, sub_resource_idx
, layer_count
;
341 struct wined3d_texture
*texture
;
343 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
345 wined3d_buffer_load_location(buffer_from_resource(resource
), context
, location
);
349 texture
= texture_from_resource(resource
);
350 sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
351 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? desc
->u
.texture
.layer_count
: 1;
352 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
353 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
356 ULONG CDECL
wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view
*view
)
358 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
360 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
365 void wined3d_rendertarget_view_cleanup(struct wined3d_rendertarget_view
*view
)
367 /* Call wined3d_object_destroyed() before releasing the resource,
368 * since releasing the resource may end up destroying the parent. */
369 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
370 wined3d_resource_decref(view
->resource
);
373 ULONG CDECL
wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view
*view
)
375 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
377 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
381 wined3d_mutex_lock();
382 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_rendertarget_view(view
);
383 wined3d_mutex_unlock();
389 void * CDECL
wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view
*view
)
391 TRACE("view %p.\n", view
);
396 void * CDECL
wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view
*view
)
398 struct wined3d_texture
*texture
;
400 TRACE("view %p.\n", view
);
402 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
403 return wined3d_buffer_get_parent(buffer_from_resource(view
->resource
));
405 texture
= texture_from_resource(view
->resource
);
407 return texture
->sub_resources
[view
->sub_resource_idx
].parent
;
410 void CDECL
wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view
*view
, void *parent
)
412 TRACE("view %p, parent %p.\n", view
, parent
);
414 view
->parent
= parent
;
417 struct wined3d_resource
* CDECL
wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view
*view
)
419 TRACE("view %p.\n", view
);
421 return view
->resource
;
424 void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view
*view
,
425 const struct wined3d_context
*context
, unsigned int *width
, unsigned int *height
)
427 const struct wined3d_texture
*texture
;
429 if (view
->resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
431 *width
= view
->width
;
432 *height
= view
->height
;
436 texture
= texture_from_resource(view
->resource
);
437 if (texture
->swapchain
)
439 /* The drawable size of an onscreen drawable is the surface size.
440 * (Actually: The window size, but the surface is created in window
442 *width
= texture
->resource
.width
;
443 *height
= texture
->resource
.height
;
445 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
447 const struct wined3d_swapchain_desc
*desc
= &context
->swapchain
->state
.desc
;
449 /* The drawable size of a backbuffer / aux buffer offscreen target is
450 * the size of the current context's drawable, which is the size of
451 * the back buffer of the swapchain the active context belongs to. */
452 *width
= desc
->backbuffer_width
;
453 *height
= desc
->backbuffer_height
;
457 unsigned int level_idx
= view
->sub_resource_idx
% texture
->level_count
;
459 /* The drawable size of an FBO target is the OpenGL texture size,
460 * which is the power of two size. */
461 *width
= wined3d_texture_get_level_pow2_width(texture
, level_idx
);
462 *height
= wined3d_texture_get_level_pow2_height(texture
, level_idx
);
466 void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view
*view
,
467 struct wined3d_context
*context
, DWORD location
)
469 struct wined3d_resource
*resource
= view
->resource
;
470 unsigned int i
, sub_resource_idx
, layer_count
;
471 struct wined3d_texture
*texture
;
473 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
475 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
479 texture
= texture_from_resource(resource
);
480 sub_resource_idx
= view
->sub_resource_idx
;
481 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
482 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
483 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
);
486 void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view
*view
,
487 struct wined3d_context
*context
, DWORD location
)
489 wined3d_view_load_location(view
->resource
, &view
->desc
, context
, location
);
492 void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view
*view
, DWORD location
)
494 struct wined3d_resource
*resource
= view
->resource
;
495 unsigned int i
, sub_resource_idx
, layer_count
;
496 struct wined3d_texture
*texture
;
498 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
500 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
504 texture
= texture_from_resource(resource
);
505 sub_resource_idx
= view
->sub_resource_idx
;
506 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
507 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
508 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
511 void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view
*view
, DWORD location
)
513 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
516 static void wined3d_render_target_view_gl_cs_init(void *object
)
518 struct wined3d_rendertarget_view_gl
*view_gl
= object
;
519 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
520 const struct wined3d_view_desc
*desc
= &view_gl
->v
.desc
;
522 TRACE("view_gl %p.\n", view_gl
);
524 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
526 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
530 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
531 unsigned int depth_or_layer_count
;
533 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
534 depth_or_layer_count
= wined3d_texture_get_level_depth(&texture_gl
->t
, desc
->u
.texture
.level_idx
);
536 depth_or_layer_count
= texture_gl
->t
.layer_count
;
538 if (resource
->format
->id
!= view_gl
->v
.format
->id
539 || (view_gl
->v
.layer_count
!= 1 && view_gl
->v
.layer_count
!= depth_or_layer_count
))
541 GLenum resource_class
, view_class
;
543 resource_class
= wined3d_format_gl(resource
->format
)->view_class
;
544 view_class
= wined3d_format_gl(view_gl
->v
.format
)->view_class
;
545 if (resource_class
!= view_class
)
547 FIXME("Render target view not supported, resource format %s, view format %s.\n",
548 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view_gl
->v
.format
->id
));
551 if (texture_gl
->t
.swapchain
&& texture_gl
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
553 FIXME("Swapchain views not supported.\n");
557 create_texture_view(&view_gl
->gl_view
, texture_gl
->target
, desc
, texture_gl
, view_gl
->v
.format
);
562 static HRESULT
wined3d_rendertarget_view_init(struct wined3d_rendertarget_view
*view
,
563 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
564 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
566 BOOL allow_srgb_toggle
= FALSE
;
569 view
->parent
= parent
;
570 view
->parent_ops
= parent_ops
;
572 if (resource
->type
!= WINED3D_RTYPE_BUFFER
)
574 struct wined3d_texture
*texture
= texture_from_resource(resource
);
576 if (texture
->swapchain
)
577 allow_srgb_toggle
= TRUE
;
579 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
, allow_srgb_toggle
)))
581 view
->format_flags
= view
->format
->flags
[resource
->gl_type
];
584 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
586 view
->sub_resource_idx
= 0;
587 view
->layer_count
= 1;
588 view
->width
= desc
->u
.buffer
.count
;
593 struct wined3d_texture
*texture
= texture_from_resource(resource
);
595 view
->sub_resource_idx
= desc
->u
.texture
.level_idx
;
596 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
597 view
->sub_resource_idx
+= desc
->u
.texture
.layer_idx
* texture
->level_count
;
598 view
->layer_count
= desc
->u
.texture
.layer_count
;
599 view
->width
= wined3d_texture_get_level_width(texture
, desc
->u
.texture
.level_idx
);
600 view
->height
= wined3d_texture_get_level_height(texture
, desc
->u
.texture
.level_idx
);
603 wined3d_resource_incref(view
->resource
= resource
);
608 HRESULT
wined3d_rendertarget_view_no3d_init(struct wined3d_rendertarget_view
*view_no3d
,
609 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
610 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
612 TRACE("view_no3d %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
613 view_no3d
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
615 return wined3d_rendertarget_view_init(view_no3d
, desc
, resource
, parent
, parent_ops
);
618 HRESULT
wined3d_rendertarget_view_gl_init(struct wined3d_rendertarget_view_gl
*view_gl
,
619 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
620 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
624 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
625 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
627 if (FAILED(hr
= wined3d_rendertarget_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
630 wined3d_cs_init_object(resource
->device
->cs
, wined3d_render_target_view_gl_cs_init
, view_gl
);
635 VkImageViewType
vk_image_view_type_from_wined3d(enum wined3d_resource_type type
, uint32_t flags
)
639 case WINED3D_RTYPE_TEXTURE_1D
:
640 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
641 return VK_IMAGE_VIEW_TYPE_1D_ARRAY
;
643 return VK_IMAGE_VIEW_TYPE_1D
;
645 case WINED3D_RTYPE_TEXTURE_2D
:
646 if (flags
& WINED3D_VIEW_TEXTURE_CUBE
)
648 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
649 return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
;
651 return VK_IMAGE_VIEW_TYPE_CUBE
;
653 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
654 return VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
656 return VK_IMAGE_VIEW_TYPE_2D
;
658 case WINED3D_RTYPE_TEXTURE_3D
:
659 return VK_IMAGE_VIEW_TYPE_3D
;
662 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(type
));
667 static VkBufferView
wined3d_view_vk_create_vk_buffer_view(struct wined3d_context_vk
*context_vk
,
668 const struct wined3d_view_desc
*desc
, struct wined3d_buffer_vk
*buffer_vk
,
669 const struct wined3d_format_vk
*view_format_vk
)
671 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
672 VkBufferViewCreateInfo create_info
;
673 struct wined3d_device_vk
*device_vk
;
674 VkBufferView vk_buffer_view
;
675 unsigned int offset
, size
;
676 struct wined3d_bo_vk
*bo
;
679 get_buffer_view_range(&buffer_vk
->b
, desc
, &view_format_vk
->f
, &offset
, &size
);
680 wined3d_buffer_prepare_location(&buffer_vk
->b
, &context_vk
->c
, WINED3D_LOCATION_BUFFER
);
681 bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
683 create_info
.sType
= VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
;
684 create_info
.pNext
= NULL
;
685 create_info
.flags
= 0;
686 create_info
.buffer
= bo
->vk_buffer
;
687 create_info
.format
= view_format_vk
->vk_format
;
688 create_info
.offset
= bo
->b
.buffer_offset
+ offset
;
689 create_info
.range
= size
;
691 device_vk
= wined3d_device_vk(buffer_vk
->b
.resource
.device
);
692 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
, &create_info
, NULL
, &vk_buffer_view
))) < 0)
694 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
695 return VK_NULL_HANDLE
;
698 return vk_buffer_view
;
701 static VkImageView
wined3d_view_vk_create_vk_image_view(struct wined3d_context_vk
*context_vk
,
702 const struct wined3d_view_desc
*desc
, struct wined3d_texture_vk
*texture_vk
,
703 const struct wined3d_format_vk
*view_format_vk
, struct color_fixup_desc fixup
, bool rtv
)
705 const struct wined3d_resource
*resource
= &texture_vk
->t
.resource
;
706 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
707 const struct wined3d_format_vk
*format_vk
;
708 struct wined3d_device_vk
*device_vk
;
709 VkImageViewCreateInfo create_info
;
710 VkImageView vk_image_view
;
713 device_vk
= wined3d_device_vk(resource
->device
);
715 if (!wined3d_texture_vk_prepare_texture(texture_vk
, context_vk
))
717 ERR("Failed to prepare texture.\n");
718 return VK_NULL_HANDLE
;
721 /* Depth formats are a little complicated. For example, the typeless
722 * format corresponding to depth/stencil view format WINED3DFMT_D32_FLOAT
723 * is WINED3DFMT_R32_TYPELESS, and the corresponding shader resource view
724 * format would be WINED3DFMT_R32_FLOAT. Vulkan depth/stencil formats are
725 * only compatible with themselves, so it's not possible to create e.g. a
726 * VK_FORMAT_R32_SFLOAT view on a VK_FORMAT_D32_SFLOAT image. In order to
727 * make it work, we create Vulkan images for WINED3DFMT_R32_TYPELESS
728 * resources with either a depth format (VK_FORMAT_D32_SFLOAT) or a colour
729 * format, depending on whether the bind flags include
730 * WINED3D_BIND_DEPTH_STENCIL or not. In order to then create a Vulkan
731 * view on the image, we then replace the view format here with the
732 * underlying resource format. However, that means it's still not possible
733 * to create e.g. a WINED3DFMT_R32_UINT view on a WINED3DFMT_R32_TYPELESS
734 * depth/stencil resource. */
735 if (resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
736 format_vk
= wined3d_format_vk(resource
->format
);
738 format_vk
= view_format_vk
;
740 create_info
.sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
;
741 create_info
.pNext
= NULL
;
742 create_info
.flags
= 0;
743 create_info
.image
= texture_vk
->image
.vk_image
;
744 create_info
.viewType
= vk_image_view_type_from_wined3d(resource
->type
, desc
->flags
);
745 if (rtv
&& create_info
.viewType
== VK_IMAGE_VIEW_TYPE_3D
)
747 if (desc
->u
.texture
.layer_count
> 1)
748 create_info
.viewType
= VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
750 create_info
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
752 create_info
.format
= format_vk
->vk_format
;
753 if (is_stencil_view_format(&view_format_vk
->f
))
755 create_info
.components
.r
= VK_COMPONENT_SWIZZLE_ZERO
;
756 create_info
.components
.g
= VK_COMPONENT_SWIZZLE_R
;
757 create_info
.components
.b
= VK_COMPONENT_SWIZZLE_ZERO
;
758 create_info
.components
.a
= VK_COMPONENT_SWIZZLE_ZERO
;
760 else if (is_identity_fixup(fixup
) || !can_use_texture_swizzle(context_vk
->c
.d3d_info
, &format_vk
->f
))
762 create_info
.components
.r
= VK_COMPONENT_SWIZZLE_IDENTITY
;
763 create_info
.components
.g
= VK_COMPONENT_SWIZZLE_IDENTITY
;
764 create_info
.components
.b
= VK_COMPONENT_SWIZZLE_IDENTITY
;
765 create_info
.components
.a
= VK_COMPONENT_SWIZZLE_IDENTITY
;
769 wined3d_vk_swizzle_from_color_fixup(&create_info
.components
, fixup
);
771 if ((resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
772 && (view_format_vk
->f
.red_size
|| view_format_vk
->f
.green_size
))
774 create_info
.subresourceRange
.aspectMask
= 0;
775 if (view_format_vk
->f
.red_size
)
776 create_info
.subresourceRange
.aspectMask
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
777 if (view_format_vk
->f
.green_size
)
778 create_info
.subresourceRange
.aspectMask
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
782 create_info
.subresourceRange
.aspectMask
= vk_aspect_mask_from_format(&format_vk
->f
);
784 create_info
.subresourceRange
.baseMipLevel
= desc
->u
.texture
.level_idx
;
785 create_info
.subresourceRange
.levelCount
= desc
->u
.texture
.level_count
;
786 if (create_info
.viewType
== VK_IMAGE_VIEW_TYPE_3D
)
788 if (desc
->u
.texture
.layer_idx
|| (desc
->u
.texture
.layer_count
!= texture_vk
->t
.resource
.depth
789 && desc
->u
.texture
.layer_count
!= ~0u))
790 WARN("Partial 3D texture views are not supported.\n");
791 create_info
.subresourceRange
.baseArrayLayer
= 0;
792 create_info
.subresourceRange
.layerCount
= 1;
796 create_info
.subresourceRange
.baseArrayLayer
= desc
->u
.texture
.layer_idx
;
797 create_info
.subresourceRange
.layerCount
= desc
->u
.texture
.layer_count
;
799 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &create_info
, NULL
, &vk_image_view
))) < 0)
801 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr
));
802 return VK_NULL_HANDLE
;
805 return vk_image_view
;
808 static void wined3d_render_target_view_vk_cs_init(void *object
)
810 struct wined3d_rendertarget_view_vk
*view_vk
= object
;
811 struct wined3d_view_desc
*desc
= &view_vk
->v
.desc
;
812 const struct wined3d_format_vk
*format_vk
;
813 struct wined3d_texture_vk
*texture_vk
;
814 struct wined3d_resource
*resource
;
815 struct wined3d_context
*context
;
816 uint32_t default_flags
= 0;
818 TRACE("view_vk %p.\n", view_vk
);
820 resource
= view_vk
->v
.resource
;
821 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
823 FIXME("Buffer views not implemented.\n");
827 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
828 format_vk
= wined3d_format_vk(view_vk
->v
.format
);
830 if (texture_vk
->t
.layer_count
> 1)
831 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
833 if (resource
->format
->id
== format_vk
->f
.id
&& desc
->flags
== default_flags
834 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
835 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
836 && !is_stencil_view_format(&format_vk
->f
) && resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
837 && is_identity_fixup(format_vk
->f
.color_fixup
))
839 TRACE("Creating identity render target view.\n");
843 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
845 FIXME("Swapchain views not supported.\n");
849 context
= context_acquire(resource
->device
, NULL
, 0);
850 view_vk
->vk_image_view
= wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context
),
851 desc
, texture_vk
, format_vk
, COLOR_FIXUP_IDENTITY
, true);
852 context_release(context
);
854 if (!view_vk
->vk_image_view
)
857 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(view_vk
->vk_image_view
));
860 HRESULT
wined3d_rendertarget_view_vk_init(struct wined3d_rendertarget_view_vk
*view_vk
,
861 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
862 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
866 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
867 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
869 if (FAILED(hr
= wined3d_rendertarget_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
872 wined3d_cs_init_object(resource
->device
->cs
, wined3d_render_target_view_vk_cs_init
, view_vk
);
877 HRESULT CDECL
wined3d_rendertarget_view_create(const struct wined3d_view_desc
*desc
,
878 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
879 struct wined3d_rendertarget_view
**view
)
881 const struct wined3d_adapter_ops
*adapter_ops
;
883 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
884 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
886 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
887 return adapter_ops
->adapter_create_rendertarget_view(desc
, resource
, parent
, parent_ops
, view
);
890 HRESULT CDECL
wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture
*texture
,
891 unsigned int sub_resource_idx
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
892 struct wined3d_rendertarget_view
**view
)
894 struct wined3d_view_desc desc
;
896 TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
897 texture
, sub_resource_idx
, parent
, parent_ops
, view
);
899 desc
.format_id
= texture
->resource
.format
->id
;
901 desc
.u
.texture
.level_idx
= sub_resource_idx
% texture
->level_count
;
902 desc
.u
.texture
.level_count
= 1;
903 desc
.u
.texture
.layer_idx
= sub_resource_idx
/ texture
->level_count
;
904 desc
.u
.texture
.layer_count
= 1;
906 return wined3d_rendertarget_view_create(&desc
, &texture
->resource
, parent
, parent_ops
, view
);
909 ULONG CDECL
wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view
*view
)
911 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
913 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
918 void wined3d_shader_resource_view_cleanup(struct wined3d_shader_resource_view
*view
)
920 /* Call wined3d_object_destroyed() before releasing the resource,
921 * since releasing the resource may end up destroying the parent. */
922 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
923 wined3d_resource_decref(view
->resource
);
926 ULONG CDECL
wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view
*view
)
928 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
930 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
934 wined3d_mutex_lock();
935 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_shader_resource_view(view
);
936 wined3d_mutex_unlock();
942 void * CDECL
wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view
*view
)
944 TRACE("view %p.\n", view
);
949 void wined3d_shader_resource_view_gl_update(struct wined3d_shader_resource_view_gl
*srv_gl
,
950 struct wined3d_context_gl
*context_gl
)
952 create_buffer_view(&srv_gl
->gl_view
, &context_gl
->c
, &srv_gl
->v
.desc
,
953 buffer_from_resource(srv_gl
->v
.resource
), srv_gl
->v
.format
);
954 srv_gl
->bo_user
.valid
= true;
957 static void wined3d_shader_resource_view_gl_cs_init(void *object
)
959 struct wined3d_shader_resource_view_gl
*view_gl
= object
;
960 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
961 const struct wined3d_format
*view_format
;
962 const struct wined3d_gl_info
*gl_info
;
963 const struct wined3d_view_desc
*desc
;
966 TRACE("view_gl %p.\n", view_gl
);
968 view_format
= view_gl
->v
.format
;
969 gl_info
= &resource
->device
->adapter
->gl_info
;
970 desc
= &view_gl
->v
.desc
;
972 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
974 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
975 struct wined3d_context
*context
;
977 context
= context_acquire(resource
->device
, NULL
, 0);
978 create_buffer_view(&view_gl
->gl_view
, context
, desc
, buffer
, view_format
);
979 view_gl
->bo_user
.valid
= true;
980 list_add_head(&buffer
->buffer_object
->users
, &view_gl
->bo_user
.entry
);
981 context_release(context
);
985 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
986 GLenum resource_class
, view_class
;
988 resource_class
= wined3d_format_gl(resource
->format
)->view_class
;
989 view_class
= wined3d_format_gl(view_format
)->view_class
;
990 view_target
= get_texture_view_target(gl_info
, desc
, texture_gl
);
992 if (resource
->format
->id
== view_format
->id
&& texture_gl
->target
== view_target
993 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_gl
->t
.level_count
994 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_gl
->t
.layer_count
995 && !is_stencil_view_format(view_format
))
997 TRACE("Creating identity shader resource view.\n");
999 else if (texture_gl
->t
.swapchain
&& texture_gl
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
1001 FIXME("Swapchain shader resource views not supported.\n");
1003 else if (resource
->format
->typeless_id
== view_format
->typeless_id
1004 && resource_class
== view_class
)
1006 create_texture_view(&view_gl
->gl_view
, view_target
, desc
, texture_gl
, view_format
);
1008 else if (wined3d_format_is_depth_view(resource
->format
->id
, view_format
->id
))
1010 create_texture_view(&view_gl
->gl_view
, view_target
, desc
, texture_gl
, resource
->format
);
1014 FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
1015 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view_format
->id
));
1020 static HRESULT
wined3d_shader_resource_view_init(struct wined3d_shader_resource_view
*view
,
1021 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1022 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1025 view
->parent
= parent
;
1026 view
->parent_ops
= parent_ops
;
1028 if (!(resource
->bind_flags
& WINED3D_BIND_SHADER_RESOURCE
))
1029 return E_INVALIDARG
;
1030 if (!(view
->format
= validate_resource_view(desc
, resource
, FALSE
, FALSE
)))
1031 return E_INVALIDARG
;
1034 wined3d_resource_incref(view
->resource
= resource
);
1039 HRESULT
wined3d_shader_resource_view_gl_init(struct wined3d_shader_resource_view_gl
*view_gl
,
1040 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1041 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1045 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1046 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1048 if (FAILED(hr
= wined3d_shader_resource_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
1051 list_init(&view_gl
->bo_user
.entry
);
1052 wined3d_cs_init_object(resource
->device
->cs
, wined3d_shader_resource_view_gl_cs_init
, view_gl
);
1057 void wined3d_shader_resource_view_vk_update(struct wined3d_shader_resource_view_vk
*srv_vk
,
1058 struct wined3d_context_vk
*context_vk
)
1060 const struct wined3d_format_vk
*view_format_vk
= wined3d_format_vk(srv_vk
->v
.format
);
1061 const struct wined3d_view_desc
*desc
= &srv_vk
->v
.desc
;
1062 struct wined3d_resource
*resource
= srv_vk
->v
.resource
;
1063 struct wined3d_view_vk
*view_vk
= &srv_vk
->view_vk
;
1064 struct wined3d_buffer_vk
*buffer_vk
;
1065 VkBufferView vk_buffer_view
;
1067 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
1068 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, view_vk
->u
.vk_buffer_view
, view_vk
->command_buffer_id
);
1069 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, view_format_vk
)))
1071 view_vk
->u
.vk_buffer_view
= vk_buffer_view
;
1072 view_vk
->bo_user
.valid
= true;
1076 static void wined3d_shader_resource_view_vk_cs_init(void *object
)
1078 struct wined3d_shader_resource_view_vk
*srv_vk
= object
;
1079 struct wined3d_view_desc
*desc
= &srv_vk
->v
.desc
;
1080 struct wined3d_texture_vk
*texture_vk
;
1081 const struct wined3d_format
*format
;
1082 struct wined3d_buffer_vk
*buffer_vk
;
1083 struct wined3d_resource
*resource
;
1084 struct wined3d_context
*context
;
1085 VkBufferView vk_buffer_view
;
1086 uint32_t default_flags
= 0;
1087 VkImageView vk_image_view
;
1088 struct wined3d_bo_vk
*bo
;
1090 TRACE("srv_vk %p.\n", srv_vk
);
1092 resource
= srv_vk
->v
.resource
;
1093 format
= srv_vk
->v
.format
;
1095 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1097 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
1099 context
= context_acquire(resource
->device
, NULL
, 0);
1100 vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(wined3d_context_vk(context
),
1101 desc
, buffer_vk
, wined3d_format_vk(format
));
1102 context_release(context
);
1104 if (!vk_buffer_view
)
1106 bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1108 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view
));
1110 srv_vk
->view_vk
.u
.vk_buffer_view
= vk_buffer_view
;
1111 srv_vk
->view_vk
.bo_user
.valid
= true;
1112 list_add_head(&bo
->b
.users
, &srv_vk
->view_vk
.bo_user
.entry
);
1117 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
1119 if (texture_vk
->t
.layer_count
> 1)
1120 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
1122 if (resource
->format
->id
== format
->id
&& desc
->flags
== default_flags
1123 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
1124 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
1125 && !(resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
))
1127 TRACE("Creating identity shader resource view.\n");
1131 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
1132 FIXME("Swapchain shader resource views not supported.\n");
1134 context
= context_acquire(resource
->device
, NULL
, 0);
1135 vk_image_view
= wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context
),
1136 desc
, texture_vk
, wined3d_format_vk(format
), format
->color_fixup
, false);
1137 context_release(context
);
1142 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view
));
1144 srv_vk
->view_vk
.u
.vk_image_info
.imageView
= vk_image_view
;
1145 srv_vk
->view_vk
.u
.vk_image_info
.sampler
= VK_NULL_HANDLE
;
1146 srv_vk
->view_vk
.u
.vk_image_info
.imageLayout
= texture_vk
->layout
;
1149 HRESULT
wined3d_shader_resource_view_vk_init(struct wined3d_shader_resource_view_vk
*view_vk
,
1150 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1151 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1155 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1156 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1158 if (FAILED(hr
= wined3d_shader_resource_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
1161 list_init(&view_vk
->view_vk
.bo_user
.entry
);
1162 wined3d_cs_init_object(resource
->device
->cs
, wined3d_shader_resource_view_vk_cs_init
, view_vk
);
1167 HRESULT CDECL
wined3d_shader_resource_view_create(const struct wined3d_view_desc
*desc
,
1168 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1169 struct wined3d_shader_resource_view
**view
)
1171 const struct wined3d_adapter_ops
*adapter_ops
;
1173 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
1174 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
1176 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
1177 return adapter_ops
->adapter_create_shader_resource_view(desc
, resource
, parent
, parent_ops
, view
);
1180 void wined3d_shader_resource_view_gl_bind(struct wined3d_shader_resource_view_gl
*view_gl
,
1181 unsigned int unit
, struct wined3d_sampler_gl
*sampler_gl
, struct wined3d_context_gl
*context_gl
)
1183 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1184 struct wined3d_texture_gl
*texture_gl
;
1186 wined3d_context_gl_active_texture(context_gl
, gl_info
, unit
);
1188 if (view_gl
->gl_view
.name
)
1190 wined3d_context_gl_bind_texture(context_gl
, view_gl
->gl_view
.target
, view_gl
->gl_view
.name
);
1191 wined3d_sampler_gl_bind(sampler_gl
, unit
, NULL
, context_gl
);
1195 if (view_gl
->v
.resource
->type
== WINED3D_RTYPE_BUFFER
)
1197 FIXME("Buffer shader resources not supported.\n");
1201 texture_gl
= wined3d_texture_gl(wined3d_texture_from_resource(view_gl
->v
.resource
));
1202 wined3d_texture_gl_bind(texture_gl
, context_gl
, FALSE
);
1203 wined3d_sampler_gl_bind(sampler_gl
, unit
, texture_gl
, context_gl
);
1206 /* Context activation is done by the caller. */
1207 static void shader_resource_view_gl_bind_and_dirtify(struct wined3d_shader_resource_view_gl
*view_gl
,
1208 struct wined3d_context_gl
*context_gl
)
1210 if (context_gl
->active_texture
< ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
1212 unsigned int active_sampler
= context_gl
->rev_tex_unit_map
[context_gl
->active_texture
];
1213 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
1214 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(active_sampler
));
1216 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1218 context_invalidate_compute_state(&context_gl
->c
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
1219 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
1221 wined3d_context_gl_bind_texture(context_gl
, view_gl
->gl_view
.target
, view_gl
->gl_view
.name
);
1224 void wined3d_shader_resource_view_gl_generate_mipmap(struct wined3d_shader_resource_view_gl
*view_gl
,
1225 struct wined3d_context_gl
*context_gl
)
1227 unsigned int i
, j
, layer_count
, level_count
, base_level
, base_layer
, sub_resource_idx
;
1228 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1229 struct wined3d_texture_gl
*texture_gl
;
1230 struct gl_texture
*gl_tex
;
1234 TRACE("view_gl %p.\n", view_gl
);
1236 layer_count
= view_gl
->v
.desc
.u
.texture
.layer_count
;
1237 level_count
= view_gl
->v
.desc
.u
.texture
.level_count
;
1238 base_level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
1239 base_layer
= view_gl
->v
.desc
.u
.texture
.layer_idx
;
1241 texture_gl
= wined3d_texture_gl(texture_from_resource(view_gl
->v
.resource
));
1242 srgb
= !!(texture_gl
->t
.flags
& WINED3D_TEXTURE_IS_SRGB
);
1243 location
= srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
;
1244 for (i
= 0; i
< layer_count
; ++i
)
1246 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
;
1247 if (!wined3d_texture_load_location(&texture_gl
->t
, sub_resource_idx
, &context_gl
->c
, location
))
1248 ERR("Failed to load source layer %u.\n", base_layer
+ i
);
1251 if (view_gl
->gl_view
.name
)
1253 shader_resource_view_gl_bind_and_dirtify(view_gl
, context_gl
);
1257 wined3d_texture_gl_bind_and_dirtify(texture_gl
, context_gl
, srgb
);
1258 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_BASE_LEVEL
, base_level
);
1259 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_MAX_LEVEL
, base_level
+ level_count
- 1);
1262 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1263 GL_EXTCALL(glBindSampler(context_gl
->active_texture
, 0));
1264 gl_tex
= wined3d_texture_gl_get_gl_texture(texture_gl
, srgb
);
1265 if (context_gl
->c
.d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
1267 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
,
1268 GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
1269 gl_tex
->sampler_desc
.srgb_decode
= FALSE
;
1272 gl_info
->fbo_ops
.glGenerateMipmap(texture_gl
->target
);
1273 checkGLcall("glGenerateMipMap()");
1275 for (i
= 0; i
< layer_count
; ++i
)
1277 for (j
= 1; j
< level_count
; ++j
)
1279 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
+ j
;
1280 wined3d_texture_validate_location(&texture_gl
->t
, sub_resource_idx
, location
);
1281 wined3d_texture_invalidate_location(&texture_gl
->t
, sub_resource_idx
, ~location
);
1285 if (!view_gl
->gl_view
.name
)
1287 gl_tex
->base_level
= base_level
;
1288 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
,
1289 GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
1293 void wined3d_shader_resource_view_vk_generate_mipmap(struct wined3d_shader_resource_view_vk
*srv_vk
,
1294 struct wined3d_context_vk
*context_vk
)
1296 unsigned int i
, j
, layer_count
, level_count
, base_level
, base_layer
, sub_resource_idx
;
1297 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1298 VkImageSubresourceRange vk_src_range
, vk_dst_range
;
1299 struct wined3d_texture_vk
*texture_vk
;
1300 VkCommandBuffer vk_command_buffer
;
1303 TRACE("srv_vk %p.\n", srv_vk
);
1305 layer_count
= srv_vk
->v
.desc
.u
.texture
.layer_count
;
1306 level_count
= srv_vk
->v
.desc
.u
.texture
.level_count
;
1307 base_level
= srv_vk
->v
.desc
.u
.texture
.level_idx
;
1308 base_layer
= srv_vk
->v
.desc
.u
.texture
.layer_idx
;
1310 texture_vk
= wined3d_texture_vk(texture_from_resource(srv_vk
->v
.resource
));
1311 for (i
= 0; i
< layer_count
; ++i
)
1313 sub_resource_idx
= (base_layer
+ i
) * texture_vk
->t
.level_count
+ base_level
;
1314 if (!wined3d_texture_load_location(&texture_vk
->t
, sub_resource_idx
,
1315 &context_vk
->c
, WINED3D_LOCATION_TEXTURE_RGB
))
1316 ERR("Failed to load source layer %u.\n", base_layer
+ i
);
1319 if (context_vk
->c
.d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
1320 FIXME("Unhandled sRGB read/write control.\n");
1322 if (wined3d_format_vk(srv_vk
->v
.format
)->vk_format
!= wined3d_format_vk(texture_vk
->t
.resource
.format
)->vk_format
)
1323 FIXME("Ignoring view format %s.\n", debug_d3dformat(srv_vk
->v
.format
->id
));
1325 if (wined3d_resource_get_sample_count(&texture_vk
->t
.resource
) > 1)
1326 FIXME("Unhandled multi-sampled resource.\n");
1328 if (!(vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
)))
1330 ERR("Failed to get command buffer.\n");
1334 vk_src_range
.aspectMask
= vk_aspect_mask_from_format(texture_vk
->t
.resource
.format
);
1335 vk_src_range
.baseMipLevel
= base_level
;
1336 vk_src_range
.levelCount
= 1;
1337 vk_src_range
.baseArrayLayer
= base_layer
;
1338 vk_src_range
.layerCount
= layer_count
;
1340 vk_dst_range
= vk_src_range
;
1341 ++vk_dst_range
.baseMipLevel
;
1343 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1344 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1345 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1346 VK_ACCESS_TRANSFER_READ_BIT
,
1347 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1348 texture_vk
->image
.vk_image
, &vk_src_range
);
1349 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1350 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1351 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1352 VK_ACCESS_TRANSFER_WRITE_BIT
,
1353 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1354 texture_vk
->image
.vk_image
, &vk_dst_range
);
1356 region
.srcSubresource
.aspectMask
= vk_src_range
.aspectMask
;
1357 region
.srcSubresource
.mipLevel
= vk_src_range
.baseMipLevel
;
1358 region
.srcSubresource
.baseArrayLayer
= vk_src_range
.baseArrayLayer
;
1359 region
.srcSubresource
.layerCount
= vk_src_range
.layerCount
;
1360 region
.srcOffsets
[0].x
= 0;
1361 region
.srcOffsets
[0].y
= 0;
1362 region
.srcOffsets
[0].z
= 0;
1364 region
.dstSubresource
.aspectMask
= vk_dst_range
.aspectMask
;
1365 region
.dstSubresource
.mipLevel
= vk_dst_range
.baseMipLevel
;
1366 region
.dstSubresource
.baseArrayLayer
= vk_dst_range
.baseArrayLayer
;
1367 region
.dstSubresource
.layerCount
= vk_dst_range
.layerCount
;
1368 region
.dstOffsets
[0].x
= 0;
1369 region
.dstOffsets
[0].y
= 0;
1370 region
.dstOffsets
[0].z
= 0;
1372 for (i
= 1; i
< level_count
; ++i
)
1374 region
.srcOffsets
[1].x
= wined3d_texture_get_level_width(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1375 region
.srcOffsets
[1].y
= wined3d_texture_get_level_height(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1376 region
.srcOffsets
[1].z
= wined3d_texture_get_level_depth(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1378 region
.dstOffsets
[1].x
= wined3d_texture_get_level_width(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1379 region
.dstOffsets
[1].y
= wined3d_texture_get_level_height(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1380 region
.dstOffsets
[1].z
= wined3d_texture_get_level_depth(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1382 VK_CALL(vkCmdBlitImage(vk_command_buffer
, texture_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1383 texture_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, 1, ®ion
, VK_FILTER_LINEAR
));
1385 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1386 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1387 VK_ACCESS_TRANSFER_READ_BIT
,
1388 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1389 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
, texture_vk
->layout
,
1390 texture_vk
->image
.vk_image
, &vk_src_range
);
1392 if (i
== level_count
- 1)
1394 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1395 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1396 VK_ACCESS_TRANSFER_WRITE_BIT
,
1397 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1398 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, texture_vk
->layout
,
1399 texture_vk
->image
.vk_image
, &vk_dst_range
);
1403 region
.srcSubresource
.mipLevel
= ++vk_src_range
.baseMipLevel
;
1404 region
.dstSubresource
.mipLevel
= ++vk_dst_range
.baseMipLevel
;
1406 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1407 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1408 VK_ACCESS_TRANSFER_WRITE_BIT
, VK_ACCESS_TRANSFER_READ_BIT
,
1409 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1410 texture_vk
->image
.vk_image
, &vk_src_range
);
1411 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1412 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1413 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1414 VK_ACCESS_TRANSFER_WRITE_BIT
,
1415 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1416 texture_vk
->image
.vk_image
, &vk_dst_range
);
1420 for (i
= 0; i
< layer_count
; ++i
)
1422 for (j
= 1; j
< level_count
; ++j
)
1424 sub_resource_idx
= (base_layer
+ i
) * texture_vk
->t
.level_count
+ base_level
+ j
;
1425 wined3d_texture_validate_location(&texture_vk
->t
, sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1426 wined3d_texture_invalidate_location(&texture_vk
->t
, sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1430 wined3d_context_vk_reference_texture(context_vk
, texture_vk
);
1433 ULONG CDECL
wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view
*view
)
1435 ULONG refcount
= InterlockedIncrement(&view
->refcount
);
1437 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
1442 void wined3d_unordered_access_view_cleanup(struct wined3d_unordered_access_view
*view
)
1444 /* Call wined3d_object_destroyed() before releasing the resource,
1445 * since releasing the resource may end up destroying the parent. */
1446 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
1447 wined3d_resource_decref(view
->resource
);
1450 ULONG CDECL
wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view
*view
)
1452 ULONG refcount
= InterlockedDecrement(&view
->refcount
);
1454 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
1458 wined3d_mutex_lock();
1459 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_unordered_access_view(view
);
1460 wined3d_mutex_unlock();
1466 void * CDECL
wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view
*view
)
1468 TRACE("view %p.\n", view
);
1470 return view
->parent
;
1473 void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view
*view
,
1476 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
1479 void wined3d_unordered_access_view_gl_clear(struct wined3d_unordered_access_view_gl
*view_gl
,
1480 const struct wined3d_uvec4
*clear_value
, struct wined3d_context_gl
*context_gl
, bool fp
)
1482 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1483 const struct wined3d_format_gl
*format_gl
;
1484 struct wined3d_resource
*resource
;
1485 struct wined3d_buffer
*buffer
;
1486 struct wined3d_bo_gl
*bo_gl
;
1487 unsigned int offset
, size
;
1489 resource
= view_gl
->v
.resource
;
1490 if (resource
->type
!= WINED3D_RTYPE_BUFFER
)
1492 unsigned int layer_count
, level_count
, base_level
, base_layer
;
1493 unsigned int sub_resource_idx
, width
, height
, depth
, i
, j
;
1494 struct wined3d_texture_gl
*texture_gl
;
1495 const void *data
= clear_value
;
1496 GLenum gl_format
, gl_type
;
1499 if (!gl_info
->supported
[ARB_CLEAR_TEXTURE
])
1501 FIXME("OpenGL implementation does not support ARB_clear_texture.\n");
1505 format_gl
= wined3d_format_gl(resource
->format
);
1506 texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
1507 level_count
= view_gl
->v
.desc
.u
.texture
.level_count
;
1508 base_level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
1510 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
1517 layer_count
= view_gl
->v
.desc
.u
.texture
.layer_count
;
1518 base_layer
= view_gl
->v
.desc
.u
.texture
.layer_idx
;
1521 if (format_gl
->f
.byte_count
<= 4 && !fp
)
1523 gl_format
= format_gl
->format
;
1524 gl_type
= format_gl
->type
;
1525 packed
= wined3d_format_pack(&format_gl
->f
, clear_value
);
1528 else if (resource
->format_flags
& WINED3DFMT_FLAG_INTEGER
)
1530 gl_format
= GL_RGBA_INTEGER
;
1531 gl_type
= GL_UNSIGNED_INT
;
1535 gl_format
= GL_RGBA
;
1539 for (i
= 0; i
< layer_count
; ++i
)
1541 for (j
= 0; j
< level_count
; ++j
)
1543 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
+ j
;
1544 wined3d_texture_prepare_location(&texture_gl
->t
, sub_resource_idx
,
1545 &context_gl
->c
, WINED3D_LOCATION_TEXTURE_RGB
);
1547 width
= wined3d_texture_get_level_width(&texture_gl
->t
, base_level
+ j
);
1548 height
= wined3d_texture_get_level_height(&texture_gl
->t
, base_level
+ j
);
1549 depth
= wined3d_texture_get_level_depth(&texture_gl
->t
, base_level
+ j
);
1551 switch (texture_gl
->target
)
1553 case GL_TEXTURE_1D_ARRAY
:
1554 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1555 0, base_layer
+ i
, 0, width
, 1, 1, gl_format
, gl_type
, data
));
1558 case GL_TEXTURE_2D_ARRAY
:
1559 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1560 case GL_TEXTURE_CUBE_MAP
:
1561 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1562 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1563 0, 0, base_layer
+ i
, width
, height
, 1, gl_format
, gl_type
, data
));
1567 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1568 0, 0, 0, width
, height
, depth
, gl_format
, gl_type
, data
));
1572 wined3d_texture_validate_location(&texture_gl
->t
, sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1573 wined3d_texture_invalidate_location(&texture_gl
->t
, sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1580 if (!gl_info
->supported
[ARB_CLEAR_BUFFER_OBJECT
])
1582 FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
1586 format_gl
= wined3d_format_gl(view_gl
->v
.format
);
1587 if (format_gl
->f
.id
!= WINED3DFMT_R32_UINT
&& format_gl
->f
.id
!= WINED3DFMT_R32_SINT
1588 && format_gl
->f
.id
!= WINED3DFMT_R32G32B32A32_UINT
1589 && format_gl
->f
.id
!= WINED3DFMT_R32G32B32A32_SINT
)
1591 FIXME("Not implemented for format %s.\n", debug_d3dformat(format_gl
->f
.id
));
1597 FIXME("Floating-point buffer clears not implemented.\n");
1601 buffer
= buffer_from_resource(resource
);
1602 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
1603 wined3d_unordered_access_view_invalidate_location(&view_gl
->v
, ~WINED3D_LOCATION_BUFFER
);
1605 bo_gl
= wined3d_bo_gl(buffer
->buffer_object
);
1606 get_buffer_view_range(buffer
, &view_gl
->v
.desc
, &format_gl
->f
, &offset
, &size
);
1607 wined3d_context_gl_bind_bo(context_gl
, bo_gl
->binding
, bo_gl
->id
);
1608 GL_EXTCALL(glClearBufferSubData(bo_gl
->binding
, format_gl
->internal
,
1609 bo_gl
->b
.buffer_offset
+ offset
, size
, format_gl
->format
, format_gl
->type
, clear_value
));
1610 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
1611 checkGLcall("clear unordered access view");
1614 void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view
*view
,
1617 struct wined3d_bo_address dst
, src
;
1618 struct wined3d_context
*context
;
1620 if (!view
->counter_bo
)
1623 context
= context_acquire(view
->resource
->device
, NULL
, 0);
1625 src
.buffer_object
= 0;
1626 src
.addr
= (void *)&value
;
1628 dst
.buffer_object
= view
->counter_bo
;
1631 wined3d_context_copy_bo_address(context
, &dst
, &src
, sizeof(uint32_t));
1633 context_release(context
);
1636 void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view
*view
,
1637 struct wined3d_buffer
*buffer
, unsigned int offset
, struct wined3d_context
*context
)
1639 struct wined3d_const_bo_address src
;
1641 if (!view
->counter_bo
)
1644 src
.buffer_object
= view
->counter_bo
;
1647 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &src
, sizeof(uint32_t));
1650 void wined3d_unordered_access_view_gl_update(struct wined3d_unordered_access_view_gl
*uav_gl
,
1651 struct wined3d_context_gl
*context_gl
)
1653 create_buffer_view(&uav_gl
->gl_view
, &context_gl
->c
, &uav_gl
->v
.desc
,
1654 buffer_from_resource(uav_gl
->v
.resource
), uav_gl
->v
.format
);
1655 uav_gl
->bo_user
.valid
= true;
1658 static void wined3d_unordered_access_view_gl_cs_init(void *object
)
1660 struct wined3d_unordered_access_view_gl
*view_gl
= object
;
1661 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
1662 struct wined3d_view_desc
*desc
= &view_gl
->v
.desc
;
1663 const struct wined3d_gl_info
*gl_info
;
1665 TRACE("view_gl %p.\n", view_gl
);
1667 gl_info
= &resource
->device
->adapter
->gl_info
;
1669 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1671 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1672 struct wined3d_context_gl
*context_gl
;
1674 context_gl
= wined3d_context_gl(context_acquire(resource
->device
, NULL
, 0));
1675 create_buffer_view(&view_gl
->gl_view
, &context_gl
->c
, desc
, buffer
, view_gl
->v
.format
);
1676 view_gl
->bo_user
.valid
= true;
1677 list_add_head(&buffer
->buffer_object
->users
, &view_gl
->bo_user
.entry
);
1678 if (desc
->flags
& (WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_BUFFER_APPEND
))
1680 struct wined3d_bo_gl
*bo
= &view_gl
->counter_bo
;
1682 view_gl
->v
.counter_bo
= &bo
->b
;
1683 wined3d_context_gl_create_bo(context_gl
, sizeof(uint32_t), GL_ATOMIC_COUNTER_BUFFER
,
1684 GL_STATIC_DRAW
, true, GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
| GL_CLIENT_STORAGE_BIT
, bo
);
1685 wined3d_unordered_access_view_set_counter(&view_gl
->v
, 0);
1687 context_release(&context_gl
->c
);
1691 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
1692 unsigned int depth_or_layer_count
;
1694 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
1695 depth_or_layer_count
= wined3d_texture_get_level_depth(&texture_gl
->t
, desc
->u
.texture
.level_idx
);
1697 depth_or_layer_count
= texture_gl
->t
.layer_count
;
1699 if (desc
->u
.texture
.layer_idx
|| desc
->u
.texture
.layer_count
!= depth_or_layer_count
)
1701 create_texture_view(&view_gl
->gl_view
, get_texture_view_target(gl_info
, desc
, texture_gl
),
1702 desc
, texture_gl
, view_gl
->v
.format
);
1707 static HRESULT
wined3d_unordered_access_view_init(struct wined3d_unordered_access_view
*view
,
1708 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1709 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1712 view
->parent
= parent
;
1713 view
->parent_ops
= parent_ops
;
1715 if (!(resource
->bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
))
1716 return E_INVALIDARG
;
1717 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
, FALSE
)))
1718 return E_INVALIDARG
;
1721 wined3d_resource_incref(view
->resource
= resource
);
1726 HRESULT
wined3d_unordered_access_view_gl_init(struct wined3d_unordered_access_view_gl
*view_gl
,
1727 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1728 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1732 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1733 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1735 if (FAILED(hr
= wined3d_unordered_access_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
1738 list_init(&view_gl
->bo_user
.entry
);
1739 wined3d_cs_init_object(resource
->device
->cs
, wined3d_unordered_access_view_gl_cs_init
, view_gl
);
1744 struct wined3d_uav_clear_constants_vk
1746 VkClearColorValue color
;
1751 static VkPipeline
create_uav_pipeline(struct wined3d_context_vk
*context_vk
,
1752 struct wined3d_pipeline_layout_vk
*layout
, const DWORD
*byte_code
, size_t byte_code_size
,
1753 enum wined3d_shader_resource_type resource_type
)
1755 VkComputePipelineCreateInfo pipeline_info
;
1756 struct wined3d_shader_desc shader_desc
;
1757 const struct wined3d_vk_info
*vk_info
;
1758 struct wined3d_context
*context
;
1759 VkShaderModule shader_module
;
1764 vk_info
= context_vk
->vk_info
;
1765 context
= &context_vk
->c
;
1767 shader_desc
.byte_code
= byte_code
;
1768 shader_desc
.byte_code_size
= byte_code_size
;
1770 shader_module
= (VkShaderModule
)context
->device
->adapter
->shader_backend
->shader_compile(context
, &shader_desc
,
1771 WINED3D_SHADER_TYPE_COMPUTE
);
1772 if (shader_module
== VK_NULL_HANDLE
)
1774 ERR("Failed to create shader.\n");
1775 return VK_NULL_HANDLE
;
1778 pipeline_info
.sType
= VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
;
1779 pipeline_info
.pNext
= NULL
;
1780 pipeline_info
.flags
= 0;
1781 pipeline_info
.stage
.sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
;
1782 pipeline_info
.stage
.pNext
= NULL
;
1783 pipeline_info
.stage
.flags
= 0;
1784 pipeline_info
.stage
.stage
= VK_SHADER_STAGE_COMPUTE_BIT
;
1785 pipeline_info
.stage
.pName
= "main";
1786 pipeline_info
.stage
.pSpecializationInfo
= NULL
;
1787 pipeline_info
.stage
.module
= shader_module
;
1788 pipeline_info
.layout
= layout
->vk_pipeline_layout
;
1789 pipeline_info
.basePipelineHandle
= VK_NULL_HANDLE
;
1790 pipeline_info
.basePipelineIndex
= -1;
1792 vk_device
= wined3d_device_vk(context
->device
)->vk_device
;
1794 if ((vr
= VK_CALL(vkCreateComputePipelines(vk_device
, VK_NULL_HANDLE
, 1, &pipeline_info
, NULL
, &result
))) < 0)
1796 ERR("Failed to create Vulkan compute pipeline, vr %s.\n", wined3d_debug_vkresult(vr
));
1797 return VK_NULL_HANDLE
;
1800 VK_CALL(vkDestroyShaderModule(vk_device
, shader_module
, NULL
));
1804 void wined3d_device_vk_uav_clear_state_init(struct wined3d_device_vk
*device_vk
)
1806 struct wined3d_uav_clear_state_vk
*state
= &device_vk
->uav_clear_state
;
1807 struct wined3d_context_vk
*context_vk
= &device_vk
->context_vk
;
1808 VkDescriptorSetLayoutBinding vk_set_bindings
[2];
1810 vk_set_bindings
[0].binding
= 0;
1811 vk_set_bindings
[0].descriptorCount
= 1;
1812 vk_set_bindings
[0].stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
;
1813 vk_set_bindings
[0].pImmutableSamplers
= NULL
;
1814 vk_set_bindings
[0].descriptorType
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
1816 vk_set_bindings
[1].binding
= 1;
1817 vk_set_bindings
[1].descriptorCount
= 1;
1818 vk_set_bindings
[1].stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
;
1819 vk_set_bindings
[1].pImmutableSamplers
= NULL
;
1821 vk_set_bindings
[1].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
;
1822 state
->image_layout
= wined3d_context_vk_get_pipeline_layout(context_vk
, vk_set_bindings
, 2);
1824 vk_set_bindings
[1].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
;
1825 state
->buffer_layout
= wined3d_context_vk_get_pipeline_layout(context_vk
, vk_set_bindings
, 2);
1827 #define SHADER_DESC(name) name, sizeof(name)
1828 state
->float_pipelines
.buffer
= create_uav_pipeline(context_vk
, state
->buffer_layout
,
1829 SHADER_DESC(cs_uav_clear_buffer_float_code
), WINED3D_SHADER_RESOURCE_BUFFER
);
1830 state
->uint_pipelines
.buffer
= create_uav_pipeline(context_vk
, state
->buffer_layout
,
1831 SHADER_DESC(cs_uav_clear_buffer_uint_code
), WINED3D_SHADER_RESOURCE_BUFFER
);
1832 state
->float_pipelines
.image_1d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1833 SHADER_DESC(cs_uav_clear_1d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1D
);
1834 state
->uint_pipelines
.image_1d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1835 SHADER_DESC(cs_uav_clear_1d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1D
);
1836 state
->float_pipelines
.image_1d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1837 SHADER_DESC(cs_uav_clear_1d_array_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
);
1838 state
->uint_pipelines
.image_1d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1839 SHADER_DESC(cs_uav_clear_1d_array_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
);
1840 state
->float_pipelines
.image_2d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1841 SHADER_DESC(cs_uav_clear_2d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2D
);
1842 state
->uint_pipelines
.image_2d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1843 SHADER_DESC(cs_uav_clear_2d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2D
);
1844 state
->float_pipelines
.image_2d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1845 SHADER_DESC(cs_uav_clear_2d_array_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
);
1846 state
->uint_pipelines
.image_2d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1847 SHADER_DESC(cs_uav_clear_2d_array_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
);
1848 state
->float_pipelines
.image_3d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1849 SHADER_DESC(cs_uav_clear_3d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_3D
);
1850 state
->uint_pipelines
.image_3d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1851 SHADER_DESC(cs_uav_clear_3d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_3D
);
1854 state
->buffer_group_size
.x
= 128;
1855 state
->buffer_group_size
.y
= 1;
1856 state
->buffer_group_size
.z
= 1;
1857 state
->image_1d_group_size
.x
= 64;
1858 state
->image_1d_group_size
.y
= 1;
1859 state
->image_1d_group_size
.z
= 1;
1860 state
->image_1d_array_group_size
.x
= 64;
1861 state
->image_1d_array_group_size
.y
= 1;
1862 state
->image_1d_array_group_size
.z
= 1;
1863 state
->image_2d_group_size
.x
= 8;
1864 state
->image_2d_group_size
.y
= 8;
1865 state
->image_2d_group_size
.z
= 1;
1866 state
->image_2d_array_group_size
.x
= 8;
1867 state
->image_2d_array_group_size
.y
= 8;
1868 state
->image_2d_array_group_size
.z
= 1;
1869 state
->image_3d_group_size
.x
= 8;
1870 state
->image_3d_group_size
.y
= 8;
1871 state
->image_3d_group_size
.z
= 1;
1874 void wined3d_device_vk_uav_clear_state_cleanup(struct wined3d_device_vk
*device_vk
)
1876 struct wined3d_uav_clear_state_vk
*state
= &device_vk
->uav_clear_state
;
1877 const struct wined3d_vk_info
*vk_info
= &device_vk
->vk_info
;
1879 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.buffer
, NULL
));
1880 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_1d
, NULL
));
1881 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_1d_array
, NULL
));
1882 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_2d
, NULL
));
1883 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_2d_array
, NULL
));
1884 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_3d
, NULL
));
1886 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.buffer
, NULL
));
1887 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_1d
, NULL
));
1888 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_1d_array
, NULL
));
1889 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_2d
, NULL
));
1890 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_2d_array
, NULL
));
1891 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_3d
, NULL
));
1894 void wined3d_unordered_access_view_vk_clear(struct wined3d_unordered_access_view_vk
*view_vk
,
1895 const struct wined3d_uvec4
*clear_value
, struct wined3d_context_vk
*context_vk
, bool fp
)
1897 struct wined3d_bo_address cb_source_address
, cb_destination_address
;
1898 const struct wined3d_format
*view_format
= view_vk
->v
.format
;
1899 struct wined3d_view_desc
*view_desc
= &view_vk
->v
.desc
;
1900 struct wined3d_uav_clear_constants_vk constants
= {0};
1901 struct wined3d_device
*device
= context_vk
->c
.device
;
1902 struct wined3d_shader_thread_group_size group_count
;
1903 enum wined3d_format_id format_id
= view_format
->id
;
1904 struct wined3d_uav_clear_pipelines_vk
*pipelines
;
1905 struct wined3d_texture_vk
*texture_vk
= NULL
;
1906 struct wined3d_pipeline_layout_vk
*layout
;
1907 struct wined3d_uav_clear_state_vk
*state
;
1908 const struct wined3d_vk_info
*vk_info
;
1909 VkDescriptorImageInfo vk_image_info
;
1910 struct wined3d_device_vk
*device_vk
;
1911 VkDescriptorBufferInfo buffer_info
;
1912 struct wined3d_resource
*resource
;
1913 VkCommandBuffer vk_command_buffer
;
1914 struct wined3d_bo_vk constants_bo
;
1915 VkWriteDescriptorSet vk_writes
[2];
1916 VkBufferView vk_buffer_view
;
1917 VkMemoryBarrier vk_barrier
;
1918 VkPipeline vk_pipeline
;
1924 device_vk
= wined3d_device_vk(device
);
1925 state
= &device_vk
->uav_clear_state
;
1926 pipelines
= fp
? &state
->float_pipelines
: &state
->uint_pipelines
;
1928 resource
= view_vk
->v
.resource
;
1929 is_array
= view_desc
->flags
& WINED3D_VIEW_TEXTURE_ARRAY
;
1931 switch (resource
->type
)
1933 case WINED3D_RTYPE_BUFFER
:
1934 vk_pipeline
= pipelines
->buffer
;
1935 group_count
= state
->buffer_group_size
;
1937 case WINED3D_RTYPE_TEXTURE_1D
:
1940 vk_pipeline
= pipelines
->image_1d_array
;
1941 group_count
= state
->image_1d_array_group_size
;
1945 vk_pipeline
= pipelines
->image_1d
;
1946 group_count
= state
->image_1d_group_size
;
1949 case WINED3D_RTYPE_TEXTURE_2D
:
1952 vk_pipeline
= pipelines
->image_2d_array
;
1953 group_count
= state
->image_2d_array_group_size
;
1957 vk_pipeline
= pipelines
->image_2d
;
1958 group_count
= state
->image_2d_group_size
;
1961 case WINED3D_RTYPE_TEXTURE_3D
:
1962 vk_pipeline
= pipelines
->image_3d
;
1963 group_count
= state
->image_3d_group_size
;
1967 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(resource
->type
));
1971 if (vk_pipeline
== VK_NULL_HANDLE
)
1973 ERR("Pipeline was not correctly initialized.\n");
1977 vk_writes
[0].sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
;
1978 vk_writes
[0].pNext
= NULL
;
1979 vk_writes
[0].dstBinding
= 1;
1980 vk_writes
[0].dstArrayElement
= 0;
1981 vk_writes
[0].descriptorCount
= 1;
1982 vk_writes
[0].pImageInfo
= NULL
;
1983 vk_writes
[0].pTexelBufferView
= &vk_buffer_view
;
1984 vk_writes
[0].pImageInfo
= &vk_image_info
;
1986 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1988 uav_location
= WINED3D_LOCATION_BUFFER
;
1989 layout
= state
->buffer_layout
;
1990 vk_writes
[0].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
;
1992 constants
.extent
.width
= view_desc
->u
.buffer
.count
;
1993 constants
.extent
.height
= 1;
1997 texture_vk
= wined3d_texture_vk(wined3d_texture_from_resource(resource
));
1999 uav_location
= WINED3D_LOCATION_TEXTURE_RGB
;
2000 layout
= state
->image_layout
;
2001 vk_writes
[0].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
;
2003 level
= view_desc
->u
.texture
.level_idx
;
2004 constants
.extent
.width
= wined3d_texture_get_level_width(&texture_vk
->t
, level
);
2005 constants
.extent
.height
= wined3d_texture_get_level_height(&texture_vk
->t
, level
);
2006 group_count
.z
= (view_desc
->u
.texture
.layer_count
+ group_count
.z
- 1) / group_count
.z
;
2009 group_count
.x
= (constants
.extent
.width
+ group_count
.x
- 1) / group_count
.x
;
2010 group_count
.y
= (constants
.extent
.height
+ group_count
.y
- 1) / group_count
.y
;
2012 constants
.color
.uint32
[0] = clear_value
->x
;
2013 constants
.color
.uint32
[1] = clear_value
->y
;
2014 constants
.color
.uint32
[2] = clear_value
->z
;
2015 constants
.color
.uint32
[3] = clear_value
->w
;
2019 /* Make sure values are truncated, not saturated to some maximum value. */
2020 constants
.color
.uint32
[0] &= wined3d_mask_from_size(view_format
->red_size
);
2021 constants
.color
.uint32
[1] &= wined3d_mask_from_size(view_format
->green_size
);
2022 constants
.color
.uint32
[2] &= wined3d_mask_from_size(view_format
->blue_size
);
2023 constants
.color
.uint32
[3] &= wined3d_mask_from_size(view_format
->alpha_size
);
2025 if (view_format
->id
== WINED3DFMT_R11G11B10_FLOAT
)
2027 constants
.color
.uint32
[0] |= constants
.color
.uint32
[1] << 11;
2028 constants
.color
.uint32
[0] |= constants
.color
.uint32
[2] << 22;
2029 format_id
= WINED3DFMT_R32_UINT
;
2033 format_id
= wined3d_get_typed_format_id(context_vk
->c
.device
->adapter
, view_format
,
2034 WINED3D_CHANNEL_TYPE_UINT
);
2038 if (format_id
== WINED3DFMT_UNKNOWN
)
2040 ERR("Unsupported format %s.\n", debug_d3dformat(view_format
->id
));
2044 wined3d_view_load_location(resource
, view_desc
, &context_vk
->c
, uav_location
);
2045 wined3d_unordered_access_view_invalidate_location(&view_vk
->v
, ~uav_location
);
2047 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2049 if (format_id
== view_format
->id
)
2050 vk_buffer_view
= view_vk
->view_vk
.u
.vk_buffer_view
;
2053 vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, view_desc
,
2054 wined3d_buffer_vk(buffer_from_resource(resource
)), wined3d_format_vk(
2055 wined3d_get_format(context_vk
->c
.device
->adapter
, format_id
, WINED3D_BIND_UNORDERED_ACCESS
)));
2056 if (vk_buffer_view
== VK_NULL_HANDLE
)
2062 if (format_id
== view_format
->id
)
2064 vk_image_info
= view_vk
->view_vk
.u
.vk_image_info
;
2065 if (!vk_image_info
.imageView
)
2067 const VkDescriptorImageInfo
*default_info
;
2068 if (!(default_info
= wined3d_texture_vk_get_default_image_info(texture_vk
, context_vk
)))
2070 vk_image_info
= *default_info
;
2075 vk_image_info
.sampler
= VK_NULL_HANDLE
;
2076 vk_image_info
.imageLayout
= texture_vk
->layout
;
2077 vk_image_info
.imageView
= wined3d_view_vk_create_vk_image_view(context_vk
, view_desc
, texture_vk
,
2078 wined3d_format_vk(wined3d_get_format(context_vk
->c
.device
->adapter
, format_id
,
2079 WINED3D_BIND_UNORDERED_ACCESS
)),
2080 COLOR_FIXUP_IDENTITY
, false);
2082 if (vk_image_info
.imageView
== VK_NULL_HANDLE
)
2087 vk_writes
[1].sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
;
2088 vk_writes
[1].pNext
= NULL
;
2089 vk_writes
[1].dstBinding
= 0;
2090 vk_writes
[1].dstArrayElement
= 0;
2091 vk_writes
[1].descriptorCount
= 1;
2092 vk_writes
[1].pImageInfo
= NULL
;
2093 vk_writes
[1].pTexelBufferView
= NULL
;
2094 vk_writes
[1].descriptorType
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
2095 vk_writes
[1].pBufferInfo
= &buffer_info
;
2097 if (!wined3d_context_vk_create_bo(context_vk
, sizeof(constants
), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
,
2098 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
, &constants_bo
))
2100 ERR("Failed to create constants BO.\n");
2104 cb_source_address
.buffer_object
= NULL
;
2105 cb_source_address
.addr
= (BYTE
*)&constants
;
2106 cb_destination_address
.buffer_object
= &constants_bo
.b
;
2107 cb_destination_address
.addr
= 0;
2109 adapter_vk_copy_bo_address(&context_vk
->c
, &cb_destination_address
, &cb_source_address
, sizeof(constants
));
2111 buffer_info
.buffer
= constants_bo
.vk_buffer
;
2112 buffer_info
.range
= constants_bo
.size
;
2113 buffer_info
.offset
= constants_bo
.b
.buffer_offset
;
2115 vk_info
= context_vk
->vk_info
;
2117 vr
= wined3d_context_vk_create_vk_descriptor_set(context_vk
, layout
->vk_set_layout
, &vk_writes
[0].dstSet
);
2118 if (vr
!= VK_SUCCESS
)
2120 ERR("Failed to create descriptor set.\n");
2121 wined3d_context_vk_destroy_bo(context_vk
, &constants_bo
);
2125 vk_writes
[1].dstSet
= vk_writes
[0].dstSet
;
2127 VK_CALL(vkUpdateDescriptorSets(device_vk
->vk_device
, 2, vk_writes
, 0, NULL
));
2129 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
2130 wined3d_context_vk_end_current_render_pass(context_vk
);
2132 wined3d_context_vk_reference_unordered_access_view(context_vk
, view_vk
);
2133 wined3d_context_vk_reference_bo(context_vk
, &constants_bo
);
2134 wined3d_context_vk_destroy_bo(context_vk
, &constants_bo
);
2136 vk_barrier
.sType
= VK_STRUCTURE_TYPE_MEMORY_BARRIER
;
2137 vk_barrier
.pNext
= NULL
;
2138 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(resource
->bind_flags
);
2139 vk_barrier
.dstAccessMask
= VK_ACCESS_SHADER_WRITE_BIT
;
2141 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer
,
2142 vk_pipeline_stage_mask_from_bind_flags(resource
->bind_flags
),
2143 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
,
2144 0, 1, &vk_barrier
, 0, NULL
, 0, NULL
));
2145 VK_CALL(vkCmdBindPipeline(vk_command_buffer
, VK_PIPELINE_BIND_POINT_COMPUTE
, vk_pipeline
));
2146 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer
, VK_PIPELINE_BIND_POINT_COMPUTE
,
2147 layout
->vk_pipeline_layout
, 0, 1, &vk_writes
[0].dstSet
, 0, NULL
));
2148 VK_CALL(vkCmdDispatch(vk_command_buffer
, group_count
.x
, group_count
.y
, group_count
.z
));
2150 vk_barrier
.srcAccessMask
= VK_ACCESS_SHADER_WRITE_BIT
;
2151 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(resource
->bind_flags
);
2153 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer
,
2154 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
,
2155 vk_pipeline_stage_mask_from_bind_flags(resource
->bind_flags
),
2156 0, 1, &vk_barrier
, 0, NULL
, 0, NULL
));
2158 context_invalidate_compute_state(&context_vk
->c
, STATE_COMPUTE_SHADER
);
2161 if (format_id
!= view_format
->id
)
2163 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2164 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, vk_buffer_view
, view_vk
->view_vk
.command_buffer_id
);
2166 wined3d_context_vk_destroy_vk_image_view(context_vk
, vk_image_info
.imageView
,
2167 view_vk
->view_vk
.command_buffer_id
);
2171 void wined3d_unordered_access_view_vk_update(struct wined3d_unordered_access_view_vk
*uav_vk
,
2172 struct wined3d_context_vk
*context_vk
)
2174 const struct wined3d_format_vk
*view_format_vk
= wined3d_format_vk(uav_vk
->v
.format
);
2175 const struct wined3d_view_desc
*desc
= &uav_vk
->v
.desc
;
2176 struct wined3d_resource
*resource
= uav_vk
->v
.resource
;
2177 struct wined3d_view_vk
*view_vk
= &uav_vk
->view_vk
;
2178 struct wined3d_buffer_vk
*buffer_vk
;
2179 VkBufferView vk_buffer_view
;
2181 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
2182 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, view_vk
->u
.vk_buffer_view
, view_vk
->command_buffer_id
);
2183 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, view_format_vk
)))
2185 view_vk
->u
.vk_buffer_view
= vk_buffer_view
;
2186 view_vk
->bo_user
.valid
= true;
2190 static void wined3d_unordered_access_view_vk_cs_init(void *object
)
2192 struct wined3d_unordered_access_view_vk
*uav_vk
= object
;
2193 struct wined3d_view_vk
*view_vk
= &uav_vk
->view_vk
;
2194 struct wined3d_view_desc
*desc
= &uav_vk
->v
.desc
;
2195 const struct wined3d_format_vk
*format_vk
;
2196 const struct wined3d_vk_info
*vk_info
;
2197 struct wined3d_texture_vk
*texture_vk
;
2198 struct wined3d_context_vk
*context_vk
;
2199 struct wined3d_device_vk
*device_vk
;
2200 struct wined3d_buffer_vk
*buffer_vk
;
2201 VkBufferViewCreateInfo create_info
;
2202 struct wined3d_resource
*resource
;
2203 VkBufferView vk_buffer_view
;
2204 uint32_t default_flags
= 0;
2205 VkImageView vk_image_view
;
2208 TRACE("uav_vk %p.\n", uav_vk
);
2210 resource
= uav_vk
->v
.resource
;
2211 device_vk
= wined3d_device_vk(resource
->device
);
2212 format_vk
= wined3d_format_vk(uav_vk
->v
.format
);
2214 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2216 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
2218 context_vk
= wined3d_context_vk(context_acquire(&device_vk
->d
, NULL
, 0));
2219 vk_info
= context_vk
->vk_info
;
2221 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, format_vk
)))
2223 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
2225 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view
));
2227 uav_vk
->view_vk
.u
.vk_buffer_view
= vk_buffer_view
;
2228 uav_vk
->view_vk
.bo_user
.valid
= true;
2229 list_add_head(&bo
->b
.users
, &view_vk
->bo_user
.entry
);
2232 if (desc
->flags
& (WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_BUFFER_APPEND
))
2234 if (!wined3d_context_vk_create_bo(context_vk
, sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT
2235 | VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
,
2236 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
, &uav_vk
->counter_bo
))
2238 ERR("Failed to create counter bo.\n");
2239 context_release(&context_vk
->c
);
2244 wined3d_context_vk_end_current_render_pass(context_vk
);
2245 VK_CALL(vkCmdFillBuffer(wined3d_context_vk_get_command_buffer(context_vk
),
2246 uav_vk
->counter_bo
.vk_buffer
, uav_vk
->counter_bo
.b
.buffer_offset
, sizeof(uint32_t), 0));
2247 wined3d_context_vk_reference_bo(context_vk
, &uav_vk
->counter_bo
);
2249 create_info
.sType
= VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
;
2250 create_info
.pNext
= NULL
;
2251 create_info
.flags
= 0;
2252 create_info
.buffer
= uav_vk
->counter_bo
.vk_buffer
;
2253 create_info
.format
= VK_FORMAT_R32_UINT
;
2254 create_info
.offset
= uav_vk
->counter_bo
.b
.buffer_offset
;
2255 create_info
.range
= sizeof(uint32_t);
2256 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
,
2257 &create_info
, NULL
, &uav_vk
->vk_counter_view
))) < 0)
2259 ERR("Failed to create counter buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
2263 TRACE("Created counter buffer view 0x%s.\n", wine_dbgstr_longlong(uav_vk
->vk_counter_view
));
2265 uav_vk
->v
.counter_bo
= &uav_vk
->counter_bo
.b
;
2269 context_release(&context_vk
->c
);
2274 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
2276 if (texture_vk
->t
.layer_count
> 1)
2277 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
2279 if (resource
->format
->id
== format_vk
->f
.id
&& desc
->flags
== default_flags
2280 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
2281 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
2282 && !(resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
) && resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
2284 TRACE("Creating identity unordered access view.\n");
2288 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
2289 FIXME("Swapchain unordered access views not supported.\n");
2291 context_vk
= wined3d_context_vk(context_acquire(&device_vk
->d
, NULL
, 0));
2292 vk_image_view
= wined3d_view_vk_create_vk_image_view(context_vk
, desc
,
2293 texture_vk
, format_vk
, format_vk
->f
.color_fixup
, false);
2294 context_release(&context_vk
->c
);
2299 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view
));
2301 view_vk
->u
.vk_image_info
.imageView
= vk_image_view
;
2302 view_vk
->u
.vk_image_info
.sampler
= VK_NULL_HANDLE
;
2303 view_vk
->u
.vk_image_info
.imageLayout
= texture_vk
->layout
;
2306 HRESULT
wined3d_unordered_access_view_vk_init(struct wined3d_unordered_access_view_vk
*view_vk
,
2307 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
2308 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2312 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
2313 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
2315 if (FAILED(hr
= wined3d_unordered_access_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
2318 list_init(&view_vk
->view_vk
.bo_user
.entry
);
2319 wined3d_cs_init_object(resource
->device
->cs
, wined3d_unordered_access_view_vk_cs_init
, view_vk
);
2324 HRESULT CDECL
wined3d_unordered_access_view_create(const struct wined3d_view_desc
*desc
,
2325 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
2326 struct wined3d_unordered_access_view
**view
)
2328 const struct wined3d_adapter_ops
*adapter_ops
;
2330 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
2331 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
2333 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
2334 return adapter_ops
->adapter_create_unordered_access_view(desc
, resource
, parent
, parent_ops
, view
);