wined3d: Spawn a separate thread to adjust the window state of windows belonging...
[wine.git] / dlls / wined3d / view.c
blob051da2891f3edc9f42cfc1b061cac23771ada699
1 /*
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
20 #include "wined3d_private.h"
21 #include "wined3d_shaders.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
25 #define WINED3D_VIEW_CUBE_ARRAY (WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY)
27 static BOOL is_stencil_view_format(const struct wined3d_format *format)
29 return format->id == WINED3DFMT_X24_TYPELESS_G8_UINT
30 || format->id == WINED3DFMT_X32_TYPELESS_G8X24_UINT;
33 static GLenum get_texture_view_target(const struct wined3d_gl_info *gl_info,
34 const struct wined3d_view_desc *desc, const struct wined3d_texture_gl *texture_gl)
36 static const struct
38 GLenum texture_target;
39 unsigned int view_flags;
40 GLenum view_target;
41 enum wined3d_gl_extension extension;
43 view_types[] =
45 {GL_TEXTURE_CUBE_MAP, 0, GL_TEXTURE_CUBE_MAP},
46 {GL_TEXTURE_RECTANGLE, 0, GL_TEXTURE_RECTANGLE},
47 {GL_TEXTURE_3D, 0, GL_TEXTURE_3D},
49 {GL_TEXTURE_2D, 0, GL_TEXTURE_2D},
50 {GL_TEXTURE_2D, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
51 {GL_TEXTURE_2D_ARRAY, 0, GL_TEXTURE_2D},
52 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_ARRAY},
53 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_TEXTURE_CUBE, GL_TEXTURE_CUBE_MAP},
54 {GL_TEXTURE_2D_ARRAY, WINED3D_VIEW_CUBE_ARRAY, GL_TEXTURE_CUBE_MAP_ARRAY, ARB_TEXTURE_CUBE_MAP_ARRAY},
56 {GL_TEXTURE_2D_MULTISAMPLE, 0, GL_TEXTURE_2D_MULTISAMPLE},
57 {GL_TEXTURE_2D_MULTISAMPLE, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
58 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, 0, GL_TEXTURE_2D_MULTISAMPLE},
59 {GL_TEXTURE_2D_MULTISAMPLE_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_2D_MULTISAMPLE_ARRAY},
61 {GL_TEXTURE_1D, 0, GL_TEXTURE_1D},
62 {GL_TEXTURE_1D, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_1D_ARRAY},
63 {GL_TEXTURE_1D_ARRAY, 0, GL_TEXTURE_1D},
64 {GL_TEXTURE_1D_ARRAY, WINED3D_VIEW_TEXTURE_ARRAY, GL_TEXTURE_1D_ARRAY},
66 unsigned int flags = desc->flags & (WINED3D_VIEW_BUFFER_RAW | WINED3D_VIEW_BUFFER_APPEND
67 | WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_TEXTURE_CUBE | WINED3D_VIEW_TEXTURE_ARRAY);
68 unsigned int i;
70 for (i = 0; i < ARRAY_SIZE(view_types); ++i)
72 if (view_types[i].texture_target != texture_gl->target || view_types[i].view_flags != flags)
73 continue;
74 if (gl_info->supported[view_types[i].extension])
75 return view_types[i].view_target;
77 FIXME("Extension %#x not supported.\n", view_types[i].extension);
80 FIXME("Unhandled view flags %#x for texture target %#x.\n", flags, texture_gl->target);
81 return texture_gl->target;
84 static const struct wined3d_format *validate_resource_view(const struct wined3d_view_desc *desc,
85 struct wined3d_resource *resource, BOOL mip_slice, BOOL allow_srgb_toggle)
87 const struct wined3d_adapter *adapter = resource->device->adapter;
88 const struct wined3d_format *format;
90 format = wined3d_get_format(adapter, desc->format_id, resource->bind_flags);
91 if (resource->type == WINED3D_RTYPE_BUFFER && (desc->flags & WINED3D_VIEW_BUFFER_RAW))
93 if (format->id != WINED3DFMT_R32_TYPELESS)
95 WARN("Invalid format %s for raw buffer view.\n", debug_d3dformat(format->id));
96 return NULL;
99 format = wined3d_get_format(adapter, WINED3DFMT_R32_UINT, resource->bind_flags);
102 if (wined3d_format_is_typeless(format))
104 WARN("Trying to create view for typeless format %s.\n", debug_d3dformat(format->id));
105 return NULL;
108 if (resource->type == WINED3D_RTYPE_BUFFER)
110 struct wined3d_buffer *buffer = buffer_from_resource(resource);
111 unsigned int buffer_size, element_size;
113 if (buffer->structure_byte_stride)
115 if (desc->format_id != WINED3DFMT_UNKNOWN)
117 WARN("Invalid format %s for structured buffer view.\n", debug_d3dformat(desc->format_id));
118 return NULL;
121 format = wined3d_get_format(adapter, WINED3DFMT_R32_UINT, resource->bind_flags);
122 element_size = buffer->structure_byte_stride;
124 else
126 element_size = format->byte_count;
129 if (!element_size)
130 return NULL;
132 buffer_size = buffer->resource.size / element_size;
133 if (!wined3d_bound_range(desc->u.buffer.start_idx, desc->u.buffer.count, buffer_size))
134 return NULL;
136 else
138 struct wined3d_texture *texture = texture_from_resource(resource);
139 unsigned int depth_or_layer_count;
141 if (resource->format->id != format->id && !wined3d_format_is_typeless(resource->format)
142 && (!allow_srgb_toggle || !wined3d_formats_are_srgb_variants(resource->format->id, format->id)))
144 WARN("Trying to create incompatible view for non typeless format %s.\n",
145 debug_d3dformat(format->id));
146 return NULL;
149 if (mip_slice && resource->type == WINED3D_RTYPE_TEXTURE_3D)
150 depth_or_layer_count = wined3d_texture_get_level_depth(texture, desc->u.texture.level_idx);
151 else
152 depth_or_layer_count = texture->layer_count;
154 if (!desc->u.texture.level_count
155 || (mip_slice && desc->u.texture.level_count != 1)
156 || !wined3d_bound_range(desc->u.texture.level_idx, desc->u.texture.level_count, texture->level_count)
157 || !desc->u.texture.layer_count
158 || !wined3d_bound_range(desc->u.texture.layer_idx, desc->u.texture.layer_count, depth_or_layer_count))
159 return NULL;
162 return format;
165 static void create_texture_view(struct wined3d_gl_view *view, GLenum view_target,
166 const struct wined3d_view_desc *desc, struct wined3d_texture_gl *texture_gl,
167 const struct wined3d_format *view_format)
169 const struct wined3d_format_gl *view_format_gl;
170 unsigned int level_idx, layer_idx, layer_count;
171 const struct wined3d_gl_info *gl_info;
172 struct wined3d_context_gl *context_gl;
173 struct wined3d_context *context;
174 GLuint texture_name;
176 view_format_gl = wined3d_format_gl(view_format);
177 view->target = view_target;
179 context = context_acquire(texture_gl->t.resource.device, NULL, 0);
180 context_gl = wined3d_context_gl(context);
181 gl_info = context_gl->gl_info;
183 if (!gl_info->supported[ARB_TEXTURE_VIEW])
185 context_release(context);
186 FIXME("OpenGL implementation does not support texture views.\n");
187 return;
190 wined3d_texture_gl_prepare_texture(texture_gl, context_gl, FALSE);
191 texture_name = wined3d_texture_gl_get_texture_name(texture_gl, context, FALSE);
193 level_idx = desc->u.texture.level_idx;
194 layer_idx = desc->u.texture.layer_idx;
195 layer_count = desc->u.texture.layer_count;
196 if (view_target == GL_TEXTURE_3D)
198 if (layer_idx || layer_count != wined3d_texture_get_level_depth(&texture_gl->t, level_idx))
199 FIXME("Depth slice (%u-%u) not supported.\n", layer_idx, layer_count);
200 layer_idx = 0;
201 layer_count = 1;
204 gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);
205 GL_EXTCALL(glTextureView(view->name, view->target, texture_name, view_format_gl->internal,
206 level_idx, desc->u.texture.level_count, layer_idx, layer_count));
207 checkGLcall("create texture view");
209 if (is_stencil_view_format(view_format))
211 static const GLint swizzle[] = {GL_ZERO, GL_RED, GL_ZERO, GL_ZERO};
213 if (!gl_info->supported[ARB_STENCIL_TEXTURING])
215 context_release(context);
216 FIXME("OpenGL implementation does not support stencil texturing.\n");
217 return;
220 wined3d_context_gl_bind_texture(context_gl, view->target, view->name);
221 gl_info->gl_ops.gl.p_glTexParameteriv(view->target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
222 gl_info->gl_ops.gl.p_glTexParameteri(view->target, GL_DEPTH_STENCIL_TEXTURE_MODE, GL_STENCIL_INDEX);
223 checkGLcall("initialize stencil view");
225 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
226 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
228 else if (!is_identity_fixup(view_format->color_fixup) && can_use_texture_swizzle(context->d3d_info, view_format))
230 GLint swizzle[4];
232 wined3d_context_gl_bind_texture(context_gl, view->target, view->name);
233 wined3d_gl_texture_swizzle_from_color_fixup(swizzle, view_format->color_fixup);
234 gl_info->gl_ops.gl.p_glTexParameteriv(view->target, GL_TEXTURE_SWIZZLE_RGBA, swizzle);
235 checkGLcall("set format swizzle");
237 context_invalidate_compute_state(context, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
238 context_invalidate_state(context, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
241 context_release(context);
244 static void create_buffer_texture(struct wined3d_gl_view *view, struct wined3d_context_gl *context_gl,
245 struct wined3d_buffer *buffer, const struct wined3d_format_gl *view_format_gl,
246 unsigned int offset, unsigned int size)
248 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
249 const struct wined3d_bo_gl *bo_gl;
251 if (!gl_info->supported[ARB_TEXTURE_BUFFER_OBJECT])
253 FIXME("OpenGL implementation does not support buffer textures.\n");
254 return;
257 wined3d_buffer_load_location(buffer, &context_gl->c, WINED3D_LOCATION_BUFFER);
258 bo_gl = wined3d_bo_gl(buffer->buffer_object);
259 offset += bo_gl->b.buffer_offset;
261 if ((offset & (gl_info->limits.texture_buffer_offset_alignment - 1)))
263 FIXME("Buffer offset %u is not %u byte aligned.\n",
264 offset, gl_info->limits.texture_buffer_offset_alignment);
265 return;
268 view->target = GL_TEXTURE_BUFFER;
269 if (!view->name)
270 gl_info->gl_ops.gl.p_glGenTextures(1, &view->name);
272 wined3d_context_gl_bind_texture(context_gl, GL_TEXTURE_BUFFER, view->name);
273 if (gl_info->supported[ARB_TEXTURE_BUFFER_RANGE])
275 GL_EXTCALL(glTexBufferRange(GL_TEXTURE_BUFFER, view_format_gl->internal, bo_gl->id, offset, size));
277 else
279 if (offset || size != buffer->resource.size)
280 FIXME("OpenGL implementation does not support ARB_texture_buffer_range.\n");
281 GL_EXTCALL(glTexBuffer(GL_TEXTURE_BUFFER, view_format_gl->internal, bo_gl->id));
283 checkGLcall("Create buffer texture");
285 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
286 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
289 static void get_buffer_view_range(const struct wined3d_buffer *buffer,
290 const struct wined3d_view_desc *desc, const struct wined3d_format *view_format,
291 unsigned int *offset, unsigned int *size)
293 if (desc->format_id == WINED3DFMT_UNKNOWN)
295 *offset = desc->u.buffer.start_idx * buffer->structure_byte_stride;
296 *size = desc->u.buffer.count * buffer->structure_byte_stride;
298 else
300 *offset = desc->u.buffer.start_idx * view_format->byte_count;
301 *size = desc->u.buffer.count * view_format->byte_count;
305 static void create_buffer_view(struct wined3d_gl_view *view, struct wined3d_context *context,
306 const struct wined3d_view_desc *desc, struct wined3d_buffer *buffer,
307 const struct wined3d_format *view_format)
309 unsigned int offset, size;
311 get_buffer_view_range(buffer, desc, view_format, &offset, &size);
312 create_buffer_texture(view, wined3d_context_gl(context), buffer, wined3d_format_gl(view_format), offset, size);
315 static void wined3d_view_invalidate_location(struct wined3d_resource *resource,
316 const struct wined3d_view_desc *desc, DWORD location)
318 unsigned int i, sub_resource_idx, layer_count;
319 struct wined3d_texture *texture;
321 if (resource->type == WINED3D_RTYPE_BUFFER)
323 wined3d_buffer_invalidate_location(buffer_from_resource(resource), location);
324 return;
327 texture = texture_from_resource(resource);
329 sub_resource_idx = desc->u.texture.layer_idx * texture->level_count + desc->u.texture.level_idx;
330 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? desc->u.texture.layer_count : 1;
331 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
332 wined3d_texture_invalidate_location(texture, sub_resource_idx, location);
335 static void wined3d_view_load_location(struct wined3d_resource *resource,
336 const struct wined3d_view_desc *desc, struct wined3d_context *context, DWORD location)
338 unsigned int i, sub_resource_idx, layer_count;
339 struct wined3d_texture *texture;
341 if (resource->type == WINED3D_RTYPE_BUFFER)
343 wined3d_buffer_load_location(buffer_from_resource(resource), context, location);
344 return;
347 texture = texture_from_resource(resource);
348 sub_resource_idx = desc->u.texture.layer_idx * texture->level_count + desc->u.texture.level_idx;
349 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? desc->u.texture.layer_count : 1;
350 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
351 wined3d_texture_load_location(texture, sub_resource_idx, context, location);
354 ULONG CDECL wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view *view)
356 ULONG refcount = InterlockedIncrement(&view->refcount);
358 TRACE("%p increasing refcount to %u.\n", view, refcount);
360 return refcount;
363 void wined3d_rendertarget_view_cleanup(struct wined3d_rendertarget_view *view)
365 /* Call wined3d_object_destroyed() before releasing the resource,
366 * since releasing the resource may end up destroying the parent. */
367 view->parent_ops->wined3d_object_destroyed(view->parent);
368 wined3d_resource_decref(view->resource);
371 ULONG CDECL wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view *view)
373 ULONG refcount = InterlockedDecrement(&view->refcount);
375 TRACE("%p decreasing refcount to %u.\n", view, refcount);
377 if (!refcount)
379 wined3d_mutex_lock();
380 view->resource->device->adapter->adapter_ops->adapter_destroy_rendertarget_view(view);
381 wined3d_mutex_unlock();
384 return refcount;
387 void * CDECL wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view *view)
389 TRACE("view %p.\n", view);
391 return view->parent;
394 void * CDECL wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view *view)
396 struct wined3d_texture *texture;
398 TRACE("view %p.\n", view);
400 if (view->resource->type == WINED3D_RTYPE_BUFFER)
401 return wined3d_buffer_get_parent(buffer_from_resource(view->resource));
403 texture = texture_from_resource(view->resource);
405 return texture->sub_resources[view->sub_resource_idx].parent;
408 void CDECL wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view *view, void *parent)
410 TRACE("view %p, parent %p.\n", view, parent);
412 view->parent = parent;
415 struct wined3d_resource * CDECL wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view *view)
417 TRACE("view %p.\n", view);
419 return view->resource;
422 void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view *view,
423 const struct wined3d_context *context, unsigned int *width, unsigned int *height)
425 const struct wined3d_texture *texture;
427 if (view->resource->type != WINED3D_RTYPE_TEXTURE_2D)
429 *width = view->width;
430 *height = view->height;
431 return;
434 texture = texture_from_resource(view->resource);
435 if (texture->swapchain)
437 /* The drawable size of an onscreen drawable is the surface size.
438 * (Actually: The window size, but the surface is created in window
439 * size.) */
440 *width = texture->resource.width;
441 *height = texture->resource.height;
443 else if (wined3d_settings.offscreen_rendering_mode == ORM_BACKBUFFER)
445 const struct wined3d_swapchain_desc *desc = &context->swapchain->state.desc;
447 /* The drawable size of a backbuffer / aux buffer offscreen target is
448 * the size of the current context's drawable, which is the size of
449 * the back buffer of the swapchain the active context belongs to. */
450 *width = desc->backbuffer_width;
451 *height = desc->backbuffer_height;
453 else
455 unsigned int level_idx = view->sub_resource_idx % texture->level_count;
457 /* The drawable size of an FBO target is the OpenGL texture size,
458 * which is the power of two size. */
459 *width = wined3d_texture_get_level_pow2_width(texture, level_idx);
460 *height = wined3d_texture_get_level_pow2_height(texture, level_idx);
464 void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view *view,
465 struct wined3d_context *context, DWORD location)
467 struct wined3d_resource *resource = view->resource;
468 unsigned int i, sub_resource_idx, layer_count;
469 struct wined3d_texture *texture;
471 if (resource->type == WINED3D_RTYPE_BUFFER)
473 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
474 return;
477 texture = texture_from_resource(resource);
478 sub_resource_idx = view->sub_resource_idx;
479 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
480 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
481 wined3d_texture_prepare_location(texture, sub_resource_idx, context, location);
484 void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view *view,
485 struct wined3d_context *context, DWORD location)
487 wined3d_view_load_location(view->resource, &view->desc, context, location);
490 void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view *view, DWORD location)
492 struct wined3d_resource *resource = view->resource;
493 unsigned int i, sub_resource_idx, layer_count;
494 struct wined3d_texture *texture;
496 if (resource->type == WINED3D_RTYPE_BUFFER)
498 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
499 return;
502 texture = texture_from_resource(resource);
503 sub_resource_idx = view->sub_resource_idx;
504 layer_count = resource->type != WINED3D_RTYPE_TEXTURE_3D ? view->layer_count : 1;
505 for (i = 0; i < layer_count; ++i, sub_resource_idx += texture->level_count)
506 wined3d_texture_validate_location(texture, sub_resource_idx, location);
509 void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view *view, DWORD location)
511 wined3d_view_invalidate_location(view->resource, &view->desc, location);
514 static void wined3d_render_target_view_gl_cs_init(void *object)
516 struct wined3d_rendertarget_view_gl *view_gl = object;
517 struct wined3d_resource *resource = view_gl->v.resource;
518 const struct wined3d_view_desc *desc = &view_gl->v.desc;
520 TRACE("view_gl %p.\n", view_gl);
522 if (resource->type == WINED3D_RTYPE_BUFFER)
524 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource->type));
526 else
528 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
529 unsigned int depth_or_layer_count;
531 if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
532 depth_or_layer_count = wined3d_texture_get_level_depth(&texture_gl->t, desc->u.texture.level_idx);
533 else
534 depth_or_layer_count = texture_gl->t.layer_count;
536 if (resource->format->id != view_gl->v.format->id
537 || (view_gl->v.layer_count != 1 && view_gl->v.layer_count != depth_or_layer_count))
539 GLenum resource_class, view_class;
541 resource_class = wined3d_format_gl(resource->format)->view_class;
542 view_class = wined3d_format_gl(view_gl->v.format)->view_class;
543 if (resource_class != view_class)
545 FIXME("Render target view not supported, resource format %s, view format %s.\n",
546 debug_d3dformat(resource->format->id), debug_d3dformat(view_gl->v.format->id));
547 return;
549 if (texture_gl->t.swapchain && texture_gl->t.swapchain->state.desc.backbuffer_count > 1)
551 FIXME("Swapchain views not supported.\n");
552 return;
555 create_texture_view(&view_gl->gl_view, texture_gl->target, desc, texture_gl, view_gl->v.format);
560 static HRESULT wined3d_rendertarget_view_init(struct wined3d_rendertarget_view *view,
561 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
562 void *parent, const struct wined3d_parent_ops *parent_ops)
564 BOOL allow_srgb_toggle = FALSE;
566 view->refcount = 1;
567 view->parent = parent;
568 view->parent_ops = parent_ops;
570 if (resource->type != WINED3D_RTYPE_BUFFER)
572 struct wined3d_texture *texture = texture_from_resource(resource);
574 if (texture->swapchain)
575 allow_srgb_toggle = TRUE;
577 if (!(view->format = validate_resource_view(desc, resource, TRUE, allow_srgb_toggle)))
578 return E_INVALIDARG;
579 view->format_flags = view->format->flags[resource->gl_type];
580 view->desc = *desc;
582 if (resource->type == WINED3D_RTYPE_BUFFER)
584 view->sub_resource_idx = 0;
585 view->layer_count = 1;
586 view->width = desc->u.buffer.count;
587 view->height = 1;
589 else
591 struct wined3d_texture *texture = texture_from_resource(resource);
593 view->sub_resource_idx = desc->u.texture.level_idx;
594 if (resource->type != WINED3D_RTYPE_TEXTURE_3D)
595 view->sub_resource_idx += desc->u.texture.layer_idx * texture->level_count;
596 view->layer_count = desc->u.texture.layer_count;
597 view->width = wined3d_texture_get_level_width(texture, desc->u.texture.level_idx);
598 view->height = wined3d_texture_get_level_height(texture, desc->u.texture.level_idx);
601 wined3d_resource_incref(view->resource = resource);
603 return WINED3D_OK;
606 HRESULT wined3d_rendertarget_view_no3d_init(struct wined3d_rendertarget_view *view_no3d,
607 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
608 void *parent, const struct wined3d_parent_ops *parent_ops)
610 TRACE("view_no3d %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
611 view_no3d, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
613 return wined3d_rendertarget_view_init(view_no3d, desc, resource, parent, parent_ops);
616 HRESULT wined3d_rendertarget_view_gl_init(struct wined3d_rendertarget_view_gl *view_gl,
617 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
618 void *parent, const struct wined3d_parent_ops *parent_ops)
620 HRESULT hr;
622 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
623 view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
625 if (FAILED(hr = wined3d_rendertarget_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
626 return hr;
628 wined3d_cs_init_object(resource->device->cs, wined3d_render_target_view_gl_cs_init, view_gl);
630 return hr;
633 VkImageViewType vk_image_view_type_from_wined3d(enum wined3d_resource_type type, uint32_t flags)
635 switch (type)
637 case WINED3D_RTYPE_TEXTURE_1D:
638 if (flags & WINED3D_VIEW_TEXTURE_ARRAY)
639 return VK_IMAGE_VIEW_TYPE_1D_ARRAY;
640 else
641 return VK_IMAGE_VIEW_TYPE_1D;
643 case WINED3D_RTYPE_TEXTURE_2D:
644 if (flags & WINED3D_VIEW_TEXTURE_CUBE)
646 if (flags & WINED3D_VIEW_TEXTURE_ARRAY)
647 return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
648 else
649 return VK_IMAGE_VIEW_TYPE_CUBE;
651 if (flags & WINED3D_VIEW_TEXTURE_ARRAY)
652 return VK_IMAGE_VIEW_TYPE_2D_ARRAY;
653 else
654 return VK_IMAGE_VIEW_TYPE_2D;
656 case WINED3D_RTYPE_TEXTURE_3D:
657 return VK_IMAGE_VIEW_TYPE_3D;
659 default:
660 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(type));
661 return ~0u;
665 static VkBufferView wined3d_view_vk_create_vk_buffer_view(struct wined3d_context_vk *context_vk,
666 const struct wined3d_view_desc *desc, struct wined3d_buffer_vk *buffer_vk,
667 const struct wined3d_format_vk *view_format_vk)
669 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
670 VkBufferViewCreateInfo create_info;
671 struct wined3d_device_vk *device_vk;
672 VkBufferView vk_buffer_view;
673 unsigned int offset, size;
674 struct wined3d_bo_vk *bo;
675 VkResult vr;
677 get_buffer_view_range(&buffer_vk->b, desc, &view_format_vk->f, &offset, &size);
678 wined3d_buffer_prepare_location(&buffer_vk->b, &context_vk->c, WINED3D_LOCATION_BUFFER);
679 bo = wined3d_bo_vk(buffer_vk->b.buffer_object);
681 create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
682 create_info.pNext = NULL;
683 create_info.flags = 0;
684 create_info.buffer = bo->vk_buffer;
685 create_info.format = view_format_vk->vk_format;
686 create_info.offset = bo->b.buffer_offset + offset;
687 create_info.range = size;
689 device_vk = wined3d_device_vk(buffer_vk->b.resource.device);
690 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device, &create_info, NULL, &vk_buffer_view))) < 0)
692 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
693 return VK_NULL_HANDLE;
696 return vk_buffer_view;
699 static VkImageView wined3d_view_vk_create_vk_image_view(struct wined3d_context_vk *context_vk,
700 const struct wined3d_view_desc *desc, struct wined3d_texture_vk *texture_vk,
701 const struct wined3d_format_vk *view_format_vk, struct color_fixup_desc fixup, bool rtv)
703 const struct wined3d_resource *resource = &texture_vk->t.resource;
704 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
705 const struct wined3d_format_vk *format_vk;
706 struct wined3d_device_vk *device_vk;
707 VkImageViewCreateInfo create_info;
708 VkImageView vk_image_view;
709 VkResult vr;
711 device_vk = wined3d_device_vk(resource->device);
713 if (!wined3d_texture_vk_prepare_texture(texture_vk, context_vk))
715 ERR("Failed to prepare texture.\n");
716 return VK_NULL_HANDLE;
719 /* Depth formats are a little complicated. For example, the typeless
720 * format corresponding to depth/stencil view format WINED3DFMT_D32_FLOAT
721 * is WINED3DFMT_R32_TYPELESS, and the corresponding shader resource view
722 * format would be WINED3DFMT_R32_FLOAT. Vulkan depth/stencil formats are
723 * only compatible with themselves, so it's not possible to create e.g. a
724 * VK_FORMAT_R32_SFLOAT view on a VK_FORMAT_D32_SFLOAT image. In order to
725 * make it work, we create Vulkan images for WINED3DFMT_R32_TYPELESS
726 * resources with either a depth format (VK_FORMAT_D32_SFLOAT) or a colour
727 * format, depending on whether the bind flags include
728 * WINED3D_BIND_DEPTH_STENCIL or not. In order to then create a Vulkan
729 * view on the image, we then replace the view format here with the
730 * underlying resource format. However, that means it's still not possible
731 * to create e.g. a WINED3DFMT_R32_UINT view on a WINED3DFMT_R32_TYPELESS
732 * depth/stencil resource. */
733 if (resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
734 format_vk = wined3d_format_vk(resource->format);
735 else
736 format_vk = view_format_vk;
738 create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
739 create_info.pNext = NULL;
740 create_info.flags = 0;
741 create_info.image = texture_vk->image.vk_image;
742 create_info.viewType = vk_image_view_type_from_wined3d(resource->type, desc->flags);
743 if (rtv && create_info.viewType == VK_IMAGE_VIEW_TYPE_3D)
745 if (desc->u.texture.layer_count > 1)
746 create_info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
747 else
748 create_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
750 create_info.format = format_vk->vk_format;
751 if (is_stencil_view_format(&view_format_vk->f))
753 create_info.components.r = VK_COMPONENT_SWIZZLE_ZERO;
754 create_info.components.g = VK_COMPONENT_SWIZZLE_R;
755 create_info.components.b = VK_COMPONENT_SWIZZLE_ZERO;
756 create_info.components.a = VK_COMPONENT_SWIZZLE_ZERO;
758 else if (is_identity_fixup(fixup) || !can_use_texture_swizzle(context_vk->c.d3d_info, &format_vk->f))
760 create_info.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
761 create_info.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
762 create_info.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
763 create_info.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
765 else
767 wined3d_vk_swizzle_from_color_fixup(&create_info.components, fixup);
769 if ((resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL)
770 && (view_format_vk->f.red_size || view_format_vk->f.green_size))
772 create_info.subresourceRange.aspectMask = 0;
773 if (view_format_vk->f.red_size)
774 create_info.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT;
775 if (view_format_vk->f.green_size)
776 create_info.subresourceRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
778 else
780 create_info.subresourceRange.aspectMask = vk_aspect_mask_from_format(&format_vk->f);
782 create_info.subresourceRange.baseMipLevel = desc->u.texture.level_idx;
783 create_info.subresourceRange.levelCount = desc->u.texture.level_count;
784 if (create_info.viewType == VK_IMAGE_VIEW_TYPE_3D)
786 if (desc->u.texture.layer_idx || (desc->u.texture.layer_count != texture_vk->t.resource.depth
787 && desc->u.texture.layer_count != ~0u))
788 WARN("Partial 3D texture views are not supported.\n");
789 create_info.subresourceRange.baseArrayLayer = 0;
790 create_info.subresourceRange.layerCount = 1;
792 else
794 create_info.subresourceRange.baseArrayLayer = desc->u.texture.layer_idx;
795 create_info.subresourceRange.layerCount = desc->u.texture.layer_count;
797 if ((vr = VK_CALL(vkCreateImageView(device_vk->vk_device, &create_info, NULL, &vk_image_view))) < 0)
799 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr));
800 return VK_NULL_HANDLE;
803 return vk_image_view;
806 static void wined3d_render_target_view_vk_cs_init(void *object)
808 struct wined3d_rendertarget_view_vk *view_vk = object;
809 struct wined3d_view_desc *desc = &view_vk->v.desc;
810 const struct wined3d_format_vk *format_vk;
811 struct wined3d_texture_vk *texture_vk;
812 struct wined3d_resource *resource;
813 struct wined3d_context *context;
814 uint32_t default_flags = 0;
816 TRACE("view_vk %p.\n", view_vk);
818 resource = view_vk->v.resource;
819 if (resource->type == WINED3D_RTYPE_BUFFER)
821 FIXME("Buffer views not implemented.\n");
822 return;
825 texture_vk = wined3d_texture_vk(texture_from_resource(resource));
826 format_vk = wined3d_format_vk(view_vk->v.format);
828 if (texture_vk->t.layer_count > 1)
829 default_flags |= WINED3D_VIEW_TEXTURE_ARRAY;
831 if (resource->format->id == format_vk->f.id && desc->flags == default_flags
832 && !desc->u.texture.level_idx && desc->u.texture.level_count == texture_vk->t.level_count
833 && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture_vk->t.layer_count
834 && !is_stencil_view_format(&format_vk->f) && resource->type != WINED3D_RTYPE_TEXTURE_3D
835 && is_identity_fixup(format_vk->f.color_fixup))
837 TRACE("Creating identity render target view.\n");
838 return;
841 if (texture_vk->t.swapchain && texture_vk->t.swapchain->state.desc.backbuffer_count > 1)
843 FIXME("Swapchain views not supported.\n");
844 return;
847 context = context_acquire(resource->device, NULL, 0);
848 view_vk->vk_image_view = wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context),
849 desc, texture_vk, format_vk, COLOR_FIXUP_IDENTITY, true);
850 context_release(context);
852 if (!view_vk->vk_image_view)
853 return;
855 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(view_vk->vk_image_view));
858 HRESULT wined3d_rendertarget_view_vk_init(struct wined3d_rendertarget_view_vk *view_vk,
859 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
860 void *parent, const struct wined3d_parent_ops *parent_ops)
862 HRESULT hr;
864 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
865 view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
867 if (FAILED(hr = wined3d_rendertarget_view_init(&view_vk->v, desc, resource, parent, parent_ops)))
868 return hr;
870 wined3d_cs_init_object(resource->device->cs, wined3d_render_target_view_vk_cs_init, view_vk);
872 return hr;
875 HRESULT CDECL wined3d_rendertarget_view_create(const struct wined3d_view_desc *desc,
876 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
877 struct wined3d_rendertarget_view **view)
879 const struct wined3d_adapter_ops *adapter_ops;
881 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
882 wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);
884 adapter_ops = resource->device->adapter->adapter_ops;
885 return adapter_ops->adapter_create_rendertarget_view(desc, resource, parent, parent_ops, view);
888 HRESULT CDECL wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture *texture,
889 unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops,
890 struct wined3d_rendertarget_view **view)
892 struct wined3d_view_desc desc;
894 TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
895 texture, sub_resource_idx, parent, parent_ops, view);
897 desc.format_id = texture->resource.format->id;
898 desc.flags = 0;
899 desc.u.texture.level_idx = sub_resource_idx % texture->level_count;
900 desc.u.texture.level_count = 1;
901 desc.u.texture.layer_idx = sub_resource_idx / texture->level_count;
902 desc.u.texture.layer_count = 1;
904 return wined3d_rendertarget_view_create(&desc, &texture->resource, parent, parent_ops, view);
907 ULONG CDECL wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view *view)
909 ULONG refcount = InterlockedIncrement(&view->refcount);
911 TRACE("%p increasing refcount to %u.\n", view, refcount);
913 return refcount;
916 void wined3d_shader_resource_view_cleanup(struct wined3d_shader_resource_view *view)
918 /* Call wined3d_object_destroyed() before releasing the resource,
919 * since releasing the resource may end up destroying the parent. */
920 view->parent_ops->wined3d_object_destroyed(view->parent);
921 wined3d_resource_decref(view->resource);
924 ULONG CDECL wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view *view)
926 ULONG refcount = InterlockedDecrement(&view->refcount);
928 TRACE("%p decreasing refcount to %u.\n", view, refcount);
930 if (!refcount)
932 wined3d_mutex_lock();
933 view->resource->device->adapter->adapter_ops->adapter_destroy_shader_resource_view(view);
934 wined3d_mutex_unlock();
937 return refcount;
940 void * CDECL wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view *view)
942 TRACE("view %p.\n", view);
944 return view->parent;
947 void wined3d_shader_resource_view_gl_update(struct wined3d_shader_resource_view_gl *srv_gl,
948 struct wined3d_context_gl *context_gl)
950 create_buffer_view(&srv_gl->gl_view, &context_gl->c, &srv_gl->v.desc,
951 buffer_from_resource(srv_gl->v.resource), srv_gl->v.format);
952 srv_gl->bo_user.valid = true;
955 static void wined3d_shader_resource_view_gl_cs_init(void *object)
957 struct wined3d_shader_resource_view_gl *view_gl = object;
958 struct wined3d_resource *resource = view_gl->v.resource;
959 const struct wined3d_format *view_format;
960 const struct wined3d_gl_info *gl_info;
961 const struct wined3d_view_desc *desc;
962 GLenum view_target;
964 TRACE("view_gl %p.\n", view_gl);
966 view_format = view_gl->v.format;
967 gl_info = &resource->device->adapter->gl_info;
968 desc = &view_gl->v.desc;
970 if (resource->type == WINED3D_RTYPE_BUFFER)
972 struct wined3d_buffer *buffer = buffer_from_resource(resource);
973 struct wined3d_context *context;
975 context = context_acquire(resource->device, NULL, 0);
976 create_buffer_view(&view_gl->gl_view, context, desc, buffer, view_format);
977 view_gl->bo_user.valid = true;
978 list_add_head(&buffer->buffer_object->users, &view_gl->bo_user.entry);
979 context_release(context);
981 else
983 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
984 GLenum resource_class, view_class;
986 resource_class = wined3d_format_gl(resource->format)->view_class;
987 view_class = wined3d_format_gl(view_format)->view_class;
988 view_target = get_texture_view_target(gl_info, desc, texture_gl);
990 if (resource->format->id == view_format->id && texture_gl->target == view_target
991 && !desc->u.texture.level_idx && desc->u.texture.level_count == texture_gl->t.level_count
992 && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture_gl->t.layer_count
993 && !is_stencil_view_format(view_format))
995 TRACE("Creating identity shader resource view.\n");
997 else if (texture_gl->t.swapchain && texture_gl->t.swapchain->state.desc.backbuffer_count > 1)
999 FIXME("Swapchain shader resource views not supported.\n");
1001 else if (resource->format->typeless_id == view_format->typeless_id
1002 && resource_class == view_class)
1004 create_texture_view(&view_gl->gl_view, view_target, desc, texture_gl, view_format);
1006 else if (wined3d_format_is_depth_view(resource->format->id, view_format->id))
1008 create_texture_view(&view_gl->gl_view, view_target, desc, texture_gl, resource->format);
1010 else
1012 FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
1013 debug_d3dformat(resource->format->id), debug_d3dformat(view_format->id));
1018 static HRESULT wined3d_shader_resource_view_init(struct wined3d_shader_resource_view *view,
1019 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1020 void *parent, const struct wined3d_parent_ops *parent_ops)
1022 view->refcount = 1;
1023 view->parent = parent;
1024 view->parent_ops = parent_ops;
1026 if (!(resource->bind_flags & WINED3D_BIND_SHADER_RESOURCE))
1027 return E_INVALIDARG;
1028 if (!(view->format = validate_resource_view(desc, resource, FALSE, FALSE)))
1029 return E_INVALIDARG;
1030 view->desc = *desc;
1032 wined3d_resource_incref(view->resource = resource);
1034 return WINED3D_OK;
1037 HRESULT wined3d_shader_resource_view_gl_init(struct wined3d_shader_resource_view_gl *view_gl,
1038 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1039 void *parent, const struct wined3d_parent_ops *parent_ops)
1041 HRESULT hr;
1043 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1044 view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
1046 if (FAILED(hr = wined3d_shader_resource_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
1047 return hr;
1049 list_init(&view_gl->bo_user.entry);
1050 wined3d_cs_init_object(resource->device->cs, wined3d_shader_resource_view_gl_cs_init, view_gl);
1052 return hr;
1055 void wined3d_shader_resource_view_vk_update(struct wined3d_shader_resource_view_vk *srv_vk,
1056 struct wined3d_context_vk *context_vk)
1058 const struct wined3d_format_vk *view_format_vk = wined3d_format_vk(srv_vk->v.format);
1059 const struct wined3d_view_desc *desc = &srv_vk->v.desc;
1060 struct wined3d_resource *resource = srv_vk->v.resource;
1061 struct wined3d_view_vk *view_vk = &srv_vk->view_vk;
1062 struct wined3d_buffer_vk *buffer_vk;
1063 VkBufferView vk_buffer_view;
1065 buffer_vk = wined3d_buffer_vk(buffer_from_resource(resource));
1066 wined3d_context_vk_destroy_vk_buffer_view(context_vk, view_vk->u.vk_buffer_view, view_vk->command_buffer_id);
1067 if ((vk_buffer_view = wined3d_view_vk_create_vk_buffer_view(context_vk, desc, buffer_vk, view_format_vk)))
1069 view_vk->u.vk_buffer_view = vk_buffer_view;
1070 view_vk->bo_user.valid = true;
1074 static void wined3d_shader_resource_view_vk_cs_init(void *object)
1076 struct wined3d_shader_resource_view_vk *srv_vk = object;
1077 struct wined3d_view_desc *desc = &srv_vk->v.desc;
1078 struct wined3d_texture_vk *texture_vk;
1079 const struct wined3d_format *format;
1080 struct wined3d_buffer_vk *buffer_vk;
1081 struct wined3d_resource *resource;
1082 struct wined3d_context *context;
1083 VkBufferView vk_buffer_view;
1084 uint32_t default_flags = 0;
1085 VkImageView vk_image_view;
1086 struct wined3d_bo_vk *bo;
1088 TRACE("srv_vk %p.\n", srv_vk);
1090 resource = srv_vk->v.resource;
1091 format = srv_vk->v.format;
1093 if (resource->type == WINED3D_RTYPE_BUFFER)
1095 buffer_vk = wined3d_buffer_vk(buffer_from_resource(resource));
1097 context = context_acquire(resource->device, NULL, 0);
1098 vk_buffer_view = wined3d_view_vk_create_vk_buffer_view(wined3d_context_vk(context),
1099 desc, buffer_vk, wined3d_format_vk(format));
1100 context_release(context);
1102 if (!vk_buffer_view)
1103 return;
1104 bo = wined3d_bo_vk(buffer_vk->b.buffer_object);
1106 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view));
1108 srv_vk->view_vk.u.vk_buffer_view = vk_buffer_view;
1109 srv_vk->view_vk.bo_user.valid = true;
1110 list_add_head(&bo->b.users, &srv_vk->view_vk.bo_user.entry);
1112 return;
1115 texture_vk = wined3d_texture_vk(texture_from_resource(resource));
1117 if (texture_vk->t.layer_count > 1)
1118 default_flags |= WINED3D_VIEW_TEXTURE_ARRAY;
1120 if (resource->format->id == format->id && desc->flags == default_flags
1121 && !desc->u.texture.level_idx && desc->u.texture.level_count == texture_vk->t.level_count
1122 && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture_vk->t.layer_count
1123 && !(resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL))
1125 TRACE("Creating identity shader resource view.\n");
1126 return;
1129 if (texture_vk->t.swapchain && texture_vk->t.swapchain->state.desc.backbuffer_count > 1)
1130 FIXME("Swapchain shader resource views not supported.\n");
1132 context = context_acquire(resource->device, NULL, 0);
1133 vk_image_view = wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context),
1134 desc, texture_vk, wined3d_format_vk(format), format->color_fixup, false);
1135 context_release(context);
1137 if (!vk_image_view)
1138 return;
1140 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view));
1142 srv_vk->view_vk.u.vk_image_info.imageView = vk_image_view;
1143 srv_vk->view_vk.u.vk_image_info.sampler = VK_NULL_HANDLE;
1144 srv_vk->view_vk.u.vk_image_info.imageLayout = texture_vk->layout;
1147 HRESULT wined3d_shader_resource_view_vk_init(struct wined3d_shader_resource_view_vk *view_vk,
1148 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1149 void *parent, const struct wined3d_parent_ops *parent_ops)
1151 HRESULT hr;
1153 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1154 view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
1156 if (FAILED(hr = wined3d_shader_resource_view_init(&view_vk->v, desc, resource, parent, parent_ops)))
1157 return hr;
1159 list_init(&view_vk->view_vk.bo_user.entry);
1160 wined3d_cs_init_object(resource->device->cs, wined3d_shader_resource_view_vk_cs_init, view_vk);
1162 return hr;
1165 HRESULT CDECL wined3d_shader_resource_view_create(const struct wined3d_view_desc *desc,
1166 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
1167 struct wined3d_shader_resource_view **view)
1169 const struct wined3d_adapter_ops *adapter_ops;
1171 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
1172 wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);
1174 adapter_ops = resource->device->adapter->adapter_ops;
1175 return adapter_ops->adapter_create_shader_resource_view(desc, resource, parent, parent_ops, view);
1178 void wined3d_shader_resource_view_gl_bind(struct wined3d_shader_resource_view_gl *view_gl,
1179 unsigned int unit, struct wined3d_sampler_gl *sampler_gl, struct wined3d_context_gl *context_gl)
1181 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1182 struct wined3d_texture_gl *texture_gl;
1184 wined3d_context_gl_active_texture(context_gl, gl_info, unit);
1186 if (view_gl->gl_view.name)
1188 wined3d_context_gl_bind_texture(context_gl, view_gl->gl_view.target, view_gl->gl_view.name);
1189 wined3d_sampler_gl_bind(sampler_gl, unit, NULL, context_gl);
1190 return;
1193 if (view_gl->v.resource->type == WINED3D_RTYPE_BUFFER)
1195 FIXME("Buffer shader resources not supported.\n");
1196 return;
1199 texture_gl = wined3d_texture_gl(wined3d_texture_from_resource(view_gl->v.resource));
1200 wined3d_texture_gl_bind(texture_gl, context_gl, FALSE);
1201 wined3d_sampler_gl_bind(sampler_gl, unit, texture_gl, context_gl);
1204 /* Context activation is done by the caller. */
1205 static void shader_resource_view_gl_bind_and_dirtify(struct wined3d_shader_resource_view_gl *view_gl,
1206 struct wined3d_context_gl *context_gl)
1208 if (context_gl->active_texture < ARRAY_SIZE(context_gl->rev_tex_unit_map))
1210 unsigned int active_sampler = context_gl->rev_tex_unit_map[context_gl->active_texture];
1211 if (active_sampler != WINED3D_UNMAPPED_STAGE)
1212 context_invalidate_state(&context_gl->c, STATE_SAMPLER(active_sampler));
1214 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1215 * a shader. */
1216 context_invalidate_compute_state(&context_gl->c, STATE_COMPUTE_SHADER_RESOURCE_BINDING);
1217 context_invalidate_state(&context_gl->c, STATE_GRAPHICS_SHADER_RESOURCE_BINDING);
1219 wined3d_context_gl_bind_texture(context_gl, view_gl->gl_view.target, view_gl->gl_view.name);
1222 void wined3d_shader_resource_view_gl_generate_mipmap(struct wined3d_shader_resource_view_gl *view_gl,
1223 struct wined3d_context_gl *context_gl)
1225 unsigned int i, j, layer_count, level_count, base_level, base_layer, sub_resource_idx;
1226 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1227 struct wined3d_texture_gl *texture_gl;
1228 struct gl_texture *gl_tex;
1229 DWORD location;
1230 BOOL srgb;
1232 TRACE("view_gl %p.\n", view_gl);
1234 layer_count = view_gl->v.desc.u.texture.layer_count;
1235 level_count = view_gl->v.desc.u.texture.level_count;
1236 base_level = view_gl->v.desc.u.texture.level_idx;
1237 base_layer = view_gl->v.desc.u.texture.layer_idx;
1239 texture_gl = wined3d_texture_gl(texture_from_resource(view_gl->v.resource));
1240 srgb = !!(texture_gl->t.flags & WINED3D_TEXTURE_IS_SRGB);
1241 location = srgb ? WINED3D_LOCATION_TEXTURE_SRGB : WINED3D_LOCATION_TEXTURE_RGB;
1242 for (i = 0; i < layer_count; ++i)
1244 sub_resource_idx = (base_layer + i) * texture_gl->t.level_count + base_level;
1245 if (!wined3d_texture_load_location(&texture_gl->t, sub_resource_idx, &context_gl->c, location))
1246 ERR("Failed to load source layer %u.\n", base_layer + i);
1249 if (view_gl->gl_view.name)
1251 shader_resource_view_gl_bind_and_dirtify(view_gl, context_gl);
1253 else
1255 wined3d_texture_gl_bind_and_dirtify(texture_gl, context_gl, srgb);
1256 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_BASE_LEVEL, base_level);
1257 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target, GL_TEXTURE_MAX_LEVEL, base_level + level_count - 1);
1260 if (gl_info->supported[ARB_SAMPLER_OBJECTS])
1261 GL_EXTCALL(glBindSampler(context_gl->active_texture, 0));
1262 gl_tex = wined3d_texture_gl_get_gl_texture(texture_gl, srgb);
1263 if (context_gl->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1265 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target,
1266 GL_TEXTURE_SRGB_DECODE_EXT, GL_SKIP_DECODE_EXT);
1267 gl_tex->sampler_desc.srgb_decode = FALSE;
1270 gl_info->fbo_ops.glGenerateMipmap(texture_gl->target);
1271 checkGLcall("glGenerateMipMap()");
1273 for (i = 0; i < layer_count; ++i)
1275 for (j = 1; j < level_count; ++j)
1277 sub_resource_idx = (base_layer + i) * texture_gl->t.level_count + base_level + j;
1278 wined3d_texture_validate_location(&texture_gl->t, sub_resource_idx, location);
1279 wined3d_texture_invalidate_location(&texture_gl->t, sub_resource_idx, ~location);
1283 if (!view_gl->gl_view.name)
1285 gl_tex->base_level = base_level;
1286 gl_info->gl_ops.gl.p_glTexParameteri(texture_gl->target,
1287 GL_TEXTURE_MAX_LEVEL, texture_gl->t.level_count - 1);
1291 void wined3d_shader_resource_view_vk_generate_mipmap(struct wined3d_shader_resource_view_vk *srv_vk,
1292 struct wined3d_context_vk *context_vk)
1294 unsigned int i, j, layer_count, level_count, base_level, base_layer, sub_resource_idx;
1295 const struct wined3d_vk_info *vk_info = context_vk->vk_info;
1296 VkImageSubresourceRange vk_src_range, vk_dst_range;
1297 struct wined3d_texture_vk *texture_vk;
1298 VkCommandBuffer vk_command_buffer;
1299 VkImageBlit region;
1301 TRACE("srv_vk %p.\n", srv_vk);
1303 layer_count = srv_vk->v.desc.u.texture.layer_count;
1304 level_count = srv_vk->v.desc.u.texture.level_count;
1305 base_level = srv_vk->v.desc.u.texture.level_idx;
1306 base_layer = srv_vk->v.desc.u.texture.layer_idx;
1308 texture_vk = wined3d_texture_vk(texture_from_resource(srv_vk->v.resource));
1309 for (i = 0; i < layer_count; ++i)
1311 sub_resource_idx = (base_layer + i) * texture_vk->t.level_count + base_level;
1312 if (!wined3d_texture_load_location(&texture_vk->t, sub_resource_idx,
1313 &context_vk->c, WINED3D_LOCATION_TEXTURE_RGB))
1314 ERR("Failed to load source layer %u.\n", base_layer + i);
1317 if (context_vk->c.d3d_info->wined3d_creation_flags & WINED3D_SRGB_READ_WRITE_CONTROL)
1318 FIXME("Unhandled sRGB read/write control.\n");
1320 if (wined3d_format_vk(srv_vk->v.format)->vk_format != wined3d_format_vk(texture_vk->t.resource.format)->vk_format)
1321 FIXME("Ignoring view format %s.\n", debug_d3dformat(srv_vk->v.format->id));
1323 if (wined3d_resource_get_sample_count(&texture_vk->t.resource) > 1)
1324 FIXME("Unhandled multi-sampled resource.\n");
1326 if (!(vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk)))
1328 ERR("Failed to get command buffer.\n");
1329 return;
1332 vk_src_range.aspectMask = vk_aspect_mask_from_format(texture_vk->t.resource.format);
1333 vk_src_range.baseMipLevel = base_level;
1334 vk_src_range.levelCount = 1;
1335 vk_src_range.baseArrayLayer = base_layer;
1336 vk_src_range.layerCount = layer_count;
1338 vk_dst_range = vk_src_range;
1339 ++vk_dst_range.baseMipLevel;
1341 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1342 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1343 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
1344 VK_ACCESS_TRANSFER_READ_BIT,
1345 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1346 texture_vk->image.vk_image, &vk_src_range);
1347 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1348 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1349 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
1350 VK_ACCESS_TRANSFER_WRITE_BIT,
1351 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1352 texture_vk->image.vk_image, &vk_dst_range);
1354 region.srcSubresource.aspectMask = vk_src_range.aspectMask;
1355 region.srcSubresource.mipLevel = vk_src_range.baseMipLevel;
1356 region.srcSubresource.baseArrayLayer = vk_src_range.baseArrayLayer;
1357 region.srcSubresource.layerCount = vk_src_range.layerCount;
1358 region.srcOffsets[0].x = 0;
1359 region.srcOffsets[0].y = 0;
1360 region.srcOffsets[0].z = 0;
1362 region.dstSubresource.aspectMask = vk_dst_range.aspectMask;
1363 region.dstSubresource.mipLevel = vk_dst_range.baseMipLevel;
1364 region.dstSubresource.baseArrayLayer = vk_dst_range.baseArrayLayer;
1365 region.dstSubresource.layerCount = vk_dst_range.layerCount;
1366 region.dstOffsets[0].x = 0;
1367 region.dstOffsets[0].y = 0;
1368 region.dstOffsets[0].z = 0;
1370 for (i = 1; i < level_count; ++i)
1372 region.srcOffsets[1].x = wined3d_texture_get_level_width(&texture_vk->t, vk_src_range.baseMipLevel);
1373 region.srcOffsets[1].y = wined3d_texture_get_level_height(&texture_vk->t, vk_src_range.baseMipLevel);
1374 region.srcOffsets[1].z = wined3d_texture_get_level_depth(&texture_vk->t, vk_src_range.baseMipLevel);
1376 region.dstOffsets[1].x = wined3d_texture_get_level_width(&texture_vk->t, vk_dst_range.baseMipLevel);
1377 region.dstOffsets[1].y = wined3d_texture_get_level_height(&texture_vk->t, vk_dst_range.baseMipLevel);
1378 region.dstOffsets[1].z = wined3d_texture_get_level_depth(&texture_vk->t, vk_dst_range.baseMipLevel);
1380 VK_CALL(vkCmdBlitImage(vk_command_buffer, texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1381 texture_vk->image.vk_image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region, VK_FILTER_LINEAR));
1383 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1384 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
1385 VK_ACCESS_TRANSFER_READ_BIT,
1386 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
1387 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, texture_vk->layout,
1388 texture_vk->image.vk_image, &vk_src_range);
1390 if (i == level_count - 1)
1392 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1393 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
1394 VK_ACCESS_TRANSFER_WRITE_BIT,
1395 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
1396 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, texture_vk->layout,
1397 texture_vk->image.vk_image, &vk_dst_range);
1399 else
1401 region.srcSubresource.mipLevel = ++vk_src_range.baseMipLevel;
1402 region.dstSubresource.mipLevel = ++vk_dst_range.baseMipLevel;
1404 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1405 VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1406 VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_TRANSFER_READ_BIT,
1407 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1408 texture_vk->image.vk_image, &vk_src_range);
1409 wined3d_context_vk_image_barrier(context_vk, vk_command_buffer,
1410 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
1411 vk_access_mask_from_bind_flags(texture_vk->t.resource.bind_flags),
1412 VK_ACCESS_TRANSFER_WRITE_BIT,
1413 texture_vk->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1414 texture_vk->image.vk_image, &vk_dst_range);
1418 for (i = 0; i < layer_count; ++i)
1420 for (j = 1; j < level_count; ++j)
1422 sub_resource_idx = (base_layer + i) * texture_vk->t.level_count + base_level + j;
1423 wined3d_texture_validate_location(&texture_vk->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
1424 wined3d_texture_invalidate_location(&texture_vk->t, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
1428 wined3d_context_vk_reference_texture(context_vk, texture_vk);
1431 ULONG CDECL wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view *view)
1433 ULONG refcount = InterlockedIncrement(&view->refcount);
1435 TRACE("%p increasing refcount to %u.\n", view, refcount);
1437 return refcount;
1440 void wined3d_unordered_access_view_cleanup(struct wined3d_unordered_access_view *view)
1442 /* Call wined3d_object_destroyed() before releasing the resource,
1443 * since releasing the resource may end up destroying the parent. */
1444 view->parent_ops->wined3d_object_destroyed(view->parent);
1445 wined3d_resource_decref(view->resource);
1448 ULONG CDECL wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view *view)
1450 ULONG refcount = InterlockedDecrement(&view->refcount);
1452 TRACE("%p decreasing refcount to %u.\n", view, refcount);
1454 if (!refcount)
1456 wined3d_mutex_lock();
1457 view->resource->device->adapter->adapter_ops->adapter_destroy_unordered_access_view(view);
1458 wined3d_mutex_unlock();
1461 return refcount;
1464 void * CDECL wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view *view)
1466 TRACE("view %p.\n", view);
1468 return view->parent;
1471 void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view *view,
1472 DWORD location)
1474 wined3d_view_invalidate_location(view->resource, &view->desc, location);
1477 void wined3d_unordered_access_view_gl_clear(struct wined3d_unordered_access_view_gl *view_gl,
1478 const struct wined3d_uvec4 *clear_value, struct wined3d_context_gl *context_gl, bool fp)
1480 const struct wined3d_gl_info *gl_info = context_gl->gl_info;
1481 const struct wined3d_format_gl *format_gl;
1482 struct wined3d_resource *resource;
1483 struct wined3d_buffer *buffer;
1484 struct wined3d_bo_gl *bo_gl;
1485 unsigned int offset, size;
1487 resource = view_gl->v.resource;
1488 if (resource->type != WINED3D_RTYPE_BUFFER)
1490 unsigned int layer_count, level_count, base_level, base_layer;
1491 unsigned int sub_resource_idx, width, height, depth, i, j;
1492 struct wined3d_texture_gl *texture_gl;
1493 const void *data = clear_value;
1494 GLenum gl_format, gl_type;
1495 uint32_t packed;
1497 if (!gl_info->supported[ARB_CLEAR_TEXTURE])
1499 FIXME("OpenGL implementation does not support ARB_clear_texture.\n");
1500 return;
1503 format_gl = wined3d_format_gl(resource->format);
1504 texture_gl = wined3d_texture_gl(texture_from_resource(resource));
1505 level_count = view_gl->v.desc.u.texture.level_count;
1506 base_level = view_gl->v.desc.u.texture.level_idx;
1508 if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
1510 layer_count = 1;
1511 base_layer = 0;
1513 else
1515 layer_count = view_gl->v.desc.u.texture.layer_count;
1516 base_layer = view_gl->v.desc.u.texture.layer_idx;
1519 if (format_gl->f.byte_count <= 4 && !fp)
1521 gl_format = format_gl->format;
1522 gl_type = format_gl->type;
1523 packed = wined3d_format_pack(&format_gl->f, clear_value);
1524 data = &packed;
1526 else if (resource->format_flags & WINED3DFMT_FLAG_INTEGER)
1528 gl_format = GL_RGBA_INTEGER;
1529 gl_type = GL_UNSIGNED_INT;
1531 else
1533 gl_format = GL_RGBA;
1534 gl_type = GL_FLOAT;
1537 for (i = 0; i < layer_count; ++i)
1539 for (j = 0; j < level_count; ++j)
1541 sub_resource_idx = (base_layer + i) * texture_gl->t.level_count + base_level + j;
1542 wined3d_texture_prepare_location(&texture_gl->t, sub_resource_idx,
1543 &context_gl->c, WINED3D_LOCATION_TEXTURE_RGB);
1545 width = wined3d_texture_get_level_width(&texture_gl->t, base_level + j);
1546 height = wined3d_texture_get_level_height(&texture_gl->t, base_level + j);
1547 depth = wined3d_texture_get_level_depth(&texture_gl->t, base_level + j);
1549 switch (texture_gl->target)
1551 case GL_TEXTURE_1D_ARRAY:
1552 GL_EXTCALL(glClearTexSubImage(texture_gl->texture_rgb.name, base_level + j,
1553 0, base_layer + i, 0, width, 1, 1, gl_format, gl_type, data));
1554 break;
1556 case GL_TEXTURE_2D_ARRAY:
1557 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1558 case GL_TEXTURE_CUBE_MAP:
1559 case GL_TEXTURE_CUBE_MAP_ARRAY:
1560 GL_EXTCALL(glClearTexSubImage(texture_gl->texture_rgb.name, base_level + j,
1561 0, 0, base_layer + i, width, height, 1, gl_format, gl_type, data));
1562 break;
1564 default:
1565 GL_EXTCALL(glClearTexSubImage(texture_gl->texture_rgb.name, base_level + j,
1566 0, 0, 0, width, height, depth, gl_format, gl_type, data));
1567 break;
1570 wined3d_texture_validate_location(&texture_gl->t, sub_resource_idx, WINED3D_LOCATION_TEXTURE_RGB);
1571 wined3d_texture_invalidate_location(&texture_gl->t, sub_resource_idx, ~WINED3D_LOCATION_TEXTURE_RGB);
1575 return;
1578 if (!gl_info->supported[ARB_CLEAR_BUFFER_OBJECT])
1580 FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
1581 return;
1584 format_gl = wined3d_format_gl(view_gl->v.format);
1585 if (format_gl->f.id != WINED3DFMT_R32_UINT && format_gl->f.id != WINED3DFMT_R32_SINT
1586 && format_gl->f.id != WINED3DFMT_R32G32B32A32_UINT
1587 && format_gl->f.id != WINED3DFMT_R32G32B32A32_SINT)
1589 FIXME("Not implemented for format %s.\n", debug_d3dformat(format_gl->f.id));
1590 return;
1593 if (fp)
1595 FIXME("Floating-point buffer clears not implemented.\n");
1596 return;
1599 buffer = buffer_from_resource(resource);
1600 wined3d_buffer_load_location(buffer, &context_gl->c, WINED3D_LOCATION_BUFFER);
1601 wined3d_unordered_access_view_invalidate_location(&view_gl->v, ~WINED3D_LOCATION_BUFFER);
1603 bo_gl = wined3d_bo_gl(buffer->buffer_object);
1604 get_buffer_view_range(buffer, &view_gl->v.desc, &format_gl->f, &offset, &size);
1605 wined3d_context_gl_bind_bo(context_gl, bo_gl->binding, bo_gl->id);
1606 GL_EXTCALL(glClearBufferSubData(bo_gl->binding, format_gl->internal,
1607 bo_gl->b.buffer_offset + offset, size, format_gl->format, format_gl->type, clear_value));
1608 wined3d_context_gl_reference_bo(context_gl, bo_gl);
1609 checkGLcall("clear unordered access view");
1612 void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view *view,
1613 unsigned int value)
1615 struct wined3d_bo_address dst, src;
1616 struct wined3d_context *context;
1617 struct wined3d_range range;
1619 if (!view->counter_bo)
1620 return;
1622 context = context_acquire(view->resource->device, NULL, 0);
1624 src.buffer_object = 0;
1625 src.addr = (void *)&value;
1627 dst.buffer_object = view->counter_bo;
1628 dst.addr = NULL;
1630 range.offset = 0;
1631 range.size = sizeof(value);
1632 wined3d_context_copy_bo_address(context, &dst, &src, 1, &range);
1634 context_release(context);
1637 void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view *view,
1638 struct wined3d_buffer *buffer, unsigned int offset, struct wined3d_context *context)
1640 struct wined3d_const_bo_address src;
1642 if (!view->counter_bo)
1643 return;
1645 src.buffer_object = view->counter_bo;
1646 src.addr = NULL;
1648 wined3d_buffer_copy_bo_address(buffer, context, offset, &src, sizeof(uint32_t));
1651 void wined3d_unordered_access_view_gl_update(struct wined3d_unordered_access_view_gl *uav_gl,
1652 struct wined3d_context_gl *context_gl)
1654 create_buffer_view(&uav_gl->gl_view, &context_gl->c, &uav_gl->v.desc,
1655 buffer_from_resource(uav_gl->v.resource), uav_gl->v.format);
1656 uav_gl->bo_user.valid = true;
1659 static void wined3d_unordered_access_view_gl_cs_init(void *object)
1661 struct wined3d_unordered_access_view_gl *view_gl = object;
1662 struct wined3d_resource *resource = view_gl->v.resource;
1663 struct wined3d_view_desc *desc = &view_gl->v.desc;
1664 const struct wined3d_gl_info *gl_info;
1666 TRACE("view_gl %p.\n", view_gl);
1668 gl_info = &resource->device->adapter->gl_info;
1670 if (resource->type == WINED3D_RTYPE_BUFFER)
1672 struct wined3d_device_gl *device_gl = wined3d_device_gl(resource->device);
1673 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1674 struct wined3d_context_gl *context_gl;
1676 context_gl = wined3d_context_gl(context_acquire(&device_gl->d, NULL, 0));
1677 create_buffer_view(&view_gl->gl_view, &context_gl->c, desc, buffer, view_gl->v.format);
1678 view_gl->bo_user.valid = true;
1679 list_add_head(&buffer->buffer_object->users, &view_gl->bo_user.entry);
1680 if (desc->flags & (WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_BUFFER_APPEND))
1682 struct wined3d_bo_gl *bo = &view_gl->counter_bo;
1684 view_gl->v.counter_bo = &bo->b;
1685 wined3d_device_gl_create_bo(device_gl, context_gl, sizeof(uint32_t), GL_ATOMIC_COUNTER_BUFFER,
1686 GL_STATIC_DRAW, true, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_CLIENT_STORAGE_BIT, bo);
1687 wined3d_unordered_access_view_set_counter(&view_gl->v, 0);
1689 context_release(&context_gl->c);
1691 else
1693 struct wined3d_texture_gl *texture_gl = wined3d_texture_gl(texture_from_resource(resource));
1694 unsigned int depth_or_layer_count;
1696 if (resource->type == WINED3D_RTYPE_TEXTURE_3D)
1697 depth_or_layer_count = wined3d_texture_get_level_depth(&texture_gl->t, desc->u.texture.level_idx);
1698 else
1699 depth_or_layer_count = texture_gl->t.layer_count;
1701 if (desc->u.texture.layer_idx || desc->u.texture.layer_count != depth_or_layer_count)
1703 create_texture_view(&view_gl->gl_view, get_texture_view_target(gl_info, desc, texture_gl),
1704 desc, texture_gl, view_gl->v.format);
1709 static HRESULT wined3d_unordered_access_view_init(struct wined3d_unordered_access_view *view,
1710 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1711 void *parent, const struct wined3d_parent_ops *parent_ops)
1713 view->refcount = 1;
1714 view->parent = parent;
1715 view->parent_ops = parent_ops;
1717 if (!(resource->bind_flags & WINED3D_BIND_UNORDERED_ACCESS))
1718 return E_INVALIDARG;
1719 if (!(view->format = validate_resource_view(desc, resource, TRUE, FALSE)))
1720 return E_INVALIDARG;
1721 view->desc = *desc;
1723 wined3d_resource_incref(view->resource = resource);
1725 return WINED3D_OK;
1728 HRESULT wined3d_unordered_access_view_gl_init(struct wined3d_unordered_access_view_gl *view_gl,
1729 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
1730 void *parent, const struct wined3d_parent_ops *parent_ops)
1732 HRESULT hr;
1734 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1735 view_gl, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
1737 if (FAILED(hr = wined3d_unordered_access_view_init(&view_gl->v, desc, resource, parent, parent_ops)))
1738 return hr;
1740 list_init(&view_gl->bo_user.entry);
1741 wined3d_cs_init_object(resource->device->cs, wined3d_unordered_access_view_gl_cs_init, view_gl);
1743 return hr;
1746 struct wined3d_uav_clear_constants_vk
1748 VkClearColorValue color;
1749 VkOffset2D offset;
1750 VkExtent2D extent;
1753 static VkPipeline create_uav_pipeline(struct wined3d_context_vk *context_vk,
1754 struct wined3d_pipeline_layout_vk *layout, const DWORD *byte_code, size_t byte_code_size,
1755 enum wined3d_shader_resource_type resource_type)
1757 VkComputePipelineCreateInfo pipeline_info;
1758 struct wined3d_shader_desc shader_desc;
1759 const struct wined3d_vk_info *vk_info;
1760 struct wined3d_context *context;
1761 VkShaderModule shader_module;
1762 VkDevice vk_device;
1763 VkPipeline result;
1764 VkResult vr;
1766 vk_info = context_vk->vk_info;
1767 context = &context_vk->c;
1769 shader_desc.byte_code = byte_code;
1770 shader_desc.byte_code_size = byte_code_size;
1772 shader_module = (VkShaderModule)context->device->adapter->shader_backend->shader_compile(context, &shader_desc,
1773 WINED3D_SHADER_TYPE_COMPUTE);
1774 if (shader_module == VK_NULL_HANDLE)
1776 ERR("Failed to create shader.\n");
1777 return VK_NULL_HANDLE;
1780 pipeline_info.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
1781 pipeline_info.pNext = NULL;
1782 pipeline_info.flags = 0;
1783 pipeline_info.stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
1784 pipeline_info.stage.pNext = NULL;
1785 pipeline_info.stage.flags = 0;
1786 pipeline_info.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
1787 pipeline_info.stage.pName = "main";
1788 pipeline_info.stage.pSpecializationInfo = NULL;
1789 pipeline_info.stage.module = shader_module;
1790 pipeline_info.layout = layout->vk_pipeline_layout;
1791 pipeline_info.basePipelineHandle = VK_NULL_HANDLE;
1792 pipeline_info.basePipelineIndex = -1;
1794 vk_device = wined3d_device_vk(context->device)->vk_device;
1796 if ((vr = VK_CALL(vkCreateComputePipelines(vk_device, VK_NULL_HANDLE, 1, &pipeline_info, NULL, &result))) < 0)
1798 ERR("Failed to create Vulkan compute pipeline, vr %s.\n", wined3d_debug_vkresult(vr));
1799 return VK_NULL_HANDLE;
1802 VK_CALL(vkDestroyShaderModule(vk_device, shader_module, NULL));
1803 return result;
1806 void wined3d_device_vk_uav_clear_state_init(struct wined3d_device_vk *device_vk)
1808 struct wined3d_uav_clear_state_vk *state = &device_vk->uav_clear_state;
1809 struct wined3d_context_vk *context_vk = &device_vk->context_vk;
1810 VkDescriptorSetLayoutBinding vk_set_bindings[2];
1812 vk_set_bindings[0].binding = 0;
1813 vk_set_bindings[0].descriptorCount = 1;
1814 vk_set_bindings[0].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
1815 vk_set_bindings[0].pImmutableSamplers = NULL;
1816 vk_set_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
1818 vk_set_bindings[1].binding = 1;
1819 vk_set_bindings[1].descriptorCount = 1;
1820 vk_set_bindings[1].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
1821 vk_set_bindings[1].pImmutableSamplers = NULL;
1823 vk_set_bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
1824 state->image_layout = wined3d_context_vk_get_pipeline_layout(context_vk, vk_set_bindings, 2);
1826 vk_set_bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
1827 state->buffer_layout = wined3d_context_vk_get_pipeline_layout(context_vk, vk_set_bindings, 2);
1829 #define SHADER_DESC(name) name, sizeof(name)
1830 state->float_pipelines.buffer = create_uav_pipeline(context_vk, state->buffer_layout,
1831 SHADER_DESC(cs_uav_clear_buffer_float_code), WINED3D_SHADER_RESOURCE_BUFFER);
1832 state->uint_pipelines.buffer = create_uav_pipeline(context_vk, state->buffer_layout,
1833 SHADER_DESC(cs_uav_clear_buffer_uint_code), WINED3D_SHADER_RESOURCE_BUFFER);
1834 state->float_pipelines.image_1d = create_uav_pipeline(context_vk, state->image_layout,
1835 SHADER_DESC(cs_uav_clear_1d_float_code), WINED3D_SHADER_RESOURCE_TEXTURE_1D);
1836 state->uint_pipelines.image_1d = create_uav_pipeline(context_vk, state->image_layout,
1837 SHADER_DESC(cs_uav_clear_1d_uint_code), WINED3D_SHADER_RESOURCE_TEXTURE_1D);
1838 state->float_pipelines.image_1d_array = create_uav_pipeline(context_vk, state->image_layout,
1839 SHADER_DESC(cs_uav_clear_1d_array_float_code), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY);
1840 state->uint_pipelines.image_1d_array = create_uav_pipeline(context_vk, state->image_layout,
1841 SHADER_DESC(cs_uav_clear_1d_array_uint_code), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY);
1842 state->float_pipelines.image_2d = create_uav_pipeline(context_vk, state->image_layout,
1843 SHADER_DESC(cs_uav_clear_2d_float_code), WINED3D_SHADER_RESOURCE_TEXTURE_2D);
1844 state->uint_pipelines.image_2d = create_uav_pipeline(context_vk, state->image_layout,
1845 SHADER_DESC(cs_uav_clear_2d_uint_code), WINED3D_SHADER_RESOURCE_TEXTURE_2D);
1846 state->float_pipelines.image_2d_array = create_uav_pipeline(context_vk, state->image_layout,
1847 SHADER_DESC(cs_uav_clear_2d_array_float_code), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY);
1848 state->uint_pipelines.image_2d_array = create_uav_pipeline(context_vk, state->image_layout,
1849 SHADER_DESC(cs_uav_clear_2d_array_uint_code), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY);
1850 state->float_pipelines.image_3d = create_uav_pipeline(context_vk, state->image_layout,
1851 SHADER_DESC(cs_uav_clear_3d_float_code), WINED3D_SHADER_RESOURCE_TEXTURE_3D);
1852 state->uint_pipelines.image_3d = create_uav_pipeline(context_vk, state->image_layout,
1853 SHADER_DESC(cs_uav_clear_3d_uint_code), WINED3D_SHADER_RESOURCE_TEXTURE_3D);
1854 #undef SHADER_DESC
1856 state->buffer_group_size.x = 128;
1857 state->buffer_group_size.y = 1;
1858 state->buffer_group_size.z = 1;
1859 state->image_1d_group_size.x = 64;
1860 state->image_1d_group_size.y = 1;
1861 state->image_1d_group_size.z = 1;
1862 state->image_1d_array_group_size.x = 64;
1863 state->image_1d_array_group_size.y = 1;
1864 state->image_1d_array_group_size.z = 1;
1865 state->image_2d_group_size.x = 8;
1866 state->image_2d_group_size.y = 8;
1867 state->image_2d_group_size.z = 1;
1868 state->image_2d_array_group_size.x = 8;
1869 state->image_2d_array_group_size.y = 8;
1870 state->image_2d_array_group_size.z = 1;
1871 state->image_3d_group_size.x = 8;
1872 state->image_3d_group_size.y = 8;
1873 state->image_3d_group_size.z = 1;
1876 void wined3d_device_vk_uav_clear_state_cleanup(struct wined3d_device_vk *device_vk)
1878 struct wined3d_uav_clear_state_vk *state = &device_vk->uav_clear_state;
1879 const struct wined3d_vk_info *vk_info = &device_vk->vk_info;
1881 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.buffer, NULL));
1882 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.image_1d, NULL));
1883 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.image_1d_array, NULL));
1884 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.image_2d, NULL));
1885 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.image_2d_array, NULL));
1886 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->float_pipelines.image_3d, NULL));
1888 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.buffer, NULL));
1889 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.image_1d, NULL));
1890 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.image_1d_array, NULL));
1891 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.image_2d, NULL));
1892 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.image_2d_array, NULL));
1893 VK_CALL(vkDestroyPipeline(device_vk->vk_device, state->uint_pipelines.image_3d, NULL));
1896 void wined3d_unordered_access_view_vk_clear(struct wined3d_unordered_access_view_vk *view_vk,
1897 const struct wined3d_uvec4 *clear_value, struct wined3d_context_vk *context_vk, bool fp)
1899 struct wined3d_bo_address cb_source_address, cb_destination_address;
1900 const struct wined3d_format *view_format = view_vk->v.format;
1901 struct wined3d_view_desc *view_desc = &view_vk->v.desc;
1902 struct wined3d_uav_clear_constants_vk constants = {0};
1903 struct wined3d_device *device = context_vk->c.device;
1904 struct wined3d_shader_thread_group_size group_count;
1905 enum wined3d_format_id format_id = view_format->id;
1906 struct wined3d_uav_clear_pipelines_vk *pipelines;
1907 struct wined3d_texture_vk *texture_vk = NULL;
1908 struct wined3d_pipeline_layout_vk *layout;
1909 struct wined3d_uav_clear_state_vk *state;
1910 const struct wined3d_vk_info *vk_info;
1911 VkDescriptorImageInfo vk_image_info;
1912 struct wined3d_device_vk *device_vk;
1913 VkDescriptorBufferInfo buffer_info;
1914 struct wined3d_resource *resource;
1915 VkCommandBuffer vk_command_buffer;
1916 struct wined3d_bo_vk constants_bo;
1917 VkWriteDescriptorSet vk_writes[2];
1918 VkBufferView vk_buffer_view;
1919 struct wined3d_range range;
1920 VkMemoryBarrier vk_barrier;
1921 VkPipeline vk_pipeline;
1922 DWORD uav_location;
1923 unsigned int level;
1924 bool is_array;
1926 device_vk = wined3d_device_vk(device);
1927 state = &device_vk->uav_clear_state;
1928 pipelines = fp ? &state->float_pipelines : &state->uint_pipelines;
1930 resource = view_vk->v.resource;
1931 is_array = view_desc->flags & WINED3D_VIEW_TEXTURE_ARRAY;
1933 switch (resource->type)
1935 case WINED3D_RTYPE_BUFFER:
1936 vk_pipeline = pipelines->buffer;
1937 group_count = state->buffer_group_size;
1938 break;
1939 case WINED3D_RTYPE_TEXTURE_1D:
1940 if (is_array)
1942 vk_pipeline = pipelines->image_1d_array;
1943 group_count = state->image_1d_array_group_size;
1945 else
1947 vk_pipeline = pipelines->image_1d;
1948 group_count = state->image_1d_group_size;
1950 break;
1951 case WINED3D_RTYPE_TEXTURE_2D:
1952 if (is_array)
1954 vk_pipeline = pipelines->image_2d_array;
1955 group_count = state->image_2d_array_group_size;
1957 else
1959 vk_pipeline = pipelines->image_2d;
1960 group_count = state->image_2d_group_size;
1962 break;
1963 case WINED3D_RTYPE_TEXTURE_3D:
1964 vk_pipeline = pipelines->image_3d;
1965 group_count = state->image_3d_group_size;
1966 break;
1968 default:
1969 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(resource->type));
1970 return;
1973 if (vk_pipeline == VK_NULL_HANDLE)
1975 ERR("Pipeline was not correctly initialized.\n");
1976 return;
1979 vk_writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1980 vk_writes[0].pNext = NULL;
1981 vk_writes[0].dstBinding = 1;
1982 vk_writes[0].dstArrayElement = 0;
1983 vk_writes[0].descriptorCount = 1;
1984 vk_writes[0].pImageInfo = NULL;
1985 vk_writes[0].pTexelBufferView = &vk_buffer_view;
1986 vk_writes[0].pImageInfo = &vk_image_info;
1988 if (resource->type == WINED3D_RTYPE_BUFFER)
1990 struct wined3d_buffer *buffer = buffer_from_resource(resource);
1992 uav_location = WINED3D_LOCATION_BUFFER;
1993 layout = state->buffer_layout;
1994 vk_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER;
1996 if (buffer->structure_byte_stride)
1997 constants.extent.width = view_desc->u.buffer.count * buffer->structure_byte_stride / 4;
1998 else
1999 constants.extent.width = view_desc->u.buffer.count;
2000 constants.extent.height = 1;
2002 else
2004 texture_vk = wined3d_texture_vk(wined3d_texture_from_resource(resource));
2006 uav_location = WINED3D_LOCATION_TEXTURE_RGB;
2007 layout = state->image_layout;
2008 vk_writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
2010 level = view_desc->u.texture.level_idx;
2011 constants.extent.width = wined3d_texture_get_level_width(&texture_vk->t, level);
2012 constants.extent.height = wined3d_texture_get_level_height(&texture_vk->t, level);
2013 group_count.z = (view_desc->u.texture.layer_count + group_count.z - 1) / group_count.z;
2016 group_count.x = (constants.extent.width + group_count.x - 1) / group_count.x;
2017 group_count.y = (constants.extent.height + group_count.y - 1) / group_count.y;
2019 constants.color.uint32[0] = clear_value->x;
2020 constants.color.uint32[1] = clear_value->y;
2021 constants.color.uint32[2] = clear_value->z;
2022 constants.color.uint32[3] = clear_value->w;
2024 if (!fp)
2026 /* Make sure values are truncated, not saturated to some maximum value. */
2027 constants.color.uint32[0] &= wined3d_mask_from_size(view_format->red_size);
2028 constants.color.uint32[1] &= wined3d_mask_from_size(view_format->green_size);
2029 constants.color.uint32[2] &= wined3d_mask_from_size(view_format->blue_size);
2030 constants.color.uint32[3] &= wined3d_mask_from_size(view_format->alpha_size);
2032 if (view_format->id == WINED3DFMT_R11G11B10_FLOAT)
2034 constants.color.uint32[0] |= constants.color.uint32[1] << 11;
2035 constants.color.uint32[0] |= constants.color.uint32[2] << 22;
2036 format_id = WINED3DFMT_R32_UINT;
2038 else
2040 format_id = wined3d_get_typed_format_id(context_vk->c.device->adapter, view_format,
2041 WINED3D_CHANNEL_TYPE_UINT);
2045 if (format_id == WINED3DFMT_UNKNOWN)
2047 ERR("Unsupported format %s.\n", debug_d3dformat(view_format->id));
2048 return;
2051 wined3d_view_load_location(resource, view_desc, &context_vk->c, uav_location);
2052 wined3d_unordered_access_view_invalidate_location(&view_vk->v, ~uav_location);
2054 if (resource->type == WINED3D_RTYPE_BUFFER)
2056 if (format_id == view_format->id)
2057 vk_buffer_view = view_vk->view_vk.u.vk_buffer_view;
2058 else
2060 vk_buffer_view = wined3d_view_vk_create_vk_buffer_view(context_vk, view_desc,
2061 wined3d_buffer_vk(buffer_from_resource(resource)), wined3d_format_vk(
2062 wined3d_get_format(context_vk->c.device->adapter, format_id, WINED3D_BIND_UNORDERED_ACCESS)));
2063 if (vk_buffer_view == VK_NULL_HANDLE)
2064 return;
2067 else
2069 if (format_id == view_format->id)
2071 vk_image_info = view_vk->view_vk.u.vk_image_info;
2072 if (!vk_image_info.imageView)
2074 const VkDescriptorImageInfo *default_info;
2075 if (!(default_info = wined3d_texture_vk_get_default_image_info(texture_vk, context_vk)))
2076 return;
2077 vk_image_info = *default_info;
2080 else
2082 vk_image_info.sampler = VK_NULL_HANDLE;
2083 vk_image_info.imageLayout = texture_vk->layout;
2084 vk_image_info.imageView = wined3d_view_vk_create_vk_image_view(context_vk, view_desc, texture_vk,
2085 wined3d_format_vk(wined3d_get_format(context_vk->c.device->adapter, format_id,
2086 WINED3D_BIND_UNORDERED_ACCESS)),
2087 COLOR_FIXUP_IDENTITY, false);
2089 if (vk_image_info.imageView == VK_NULL_HANDLE)
2090 return;
2094 vk_writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
2095 vk_writes[1].pNext = NULL;
2096 vk_writes[1].dstBinding = 0;
2097 vk_writes[1].dstArrayElement = 0;
2098 vk_writes[1].descriptorCount = 1;
2099 vk_writes[1].pImageInfo = NULL;
2100 vk_writes[1].pTexelBufferView = NULL;
2101 vk_writes[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
2102 vk_writes[1].pBufferInfo = &buffer_info;
2104 if (!wined3d_context_vk_create_bo(context_vk, sizeof(constants), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
2105 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, &constants_bo))
2107 ERR("Failed to create constants BO.\n");
2108 goto out;
2111 cb_source_address.buffer_object = NULL;
2112 cb_source_address.addr = (BYTE *)&constants;
2113 cb_destination_address.buffer_object = &constants_bo.b;
2114 cb_destination_address.addr = 0;
2116 range.offset = 0;
2117 range.size = sizeof(constants);
2118 adapter_vk_copy_bo_address(&context_vk->c, &cb_destination_address, &cb_source_address, 1, &range);
2120 buffer_info.buffer = constants_bo.vk_buffer;
2121 buffer_info.range = constants_bo.size;
2122 buffer_info.offset = constants_bo.b.buffer_offset;
2124 vk_info = context_vk->vk_info;
2126 if (!(vk_writes[0].dstSet = wined3d_context_vk_create_vk_descriptor_set(context_vk, layout->vk_set_layout)))
2128 ERR("Failed to create descriptor set.\n");
2129 wined3d_context_vk_destroy_bo(context_vk, &constants_bo);
2130 goto out;
2133 vk_writes[1].dstSet = vk_writes[0].dstSet;
2135 VK_CALL(vkUpdateDescriptorSets(device_vk->vk_device, 2, vk_writes, 0, NULL));
2137 vk_command_buffer = wined3d_context_vk_get_command_buffer(context_vk);
2138 wined3d_context_vk_end_current_render_pass(context_vk);
2140 wined3d_context_vk_reference_unordered_access_view(context_vk, view_vk);
2141 wined3d_context_vk_reference_bo(context_vk, &constants_bo);
2142 wined3d_context_vk_destroy_bo(context_vk, &constants_bo);
2144 vk_barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
2145 vk_barrier.pNext = NULL;
2146 vk_barrier.srcAccessMask = vk_access_mask_from_bind_flags(resource->bind_flags);
2147 vk_barrier.dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
2149 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer,
2150 vk_pipeline_stage_mask_from_bind_flags(resource->bind_flags),
2151 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
2152 0, 1, &vk_barrier, 0, NULL, 0, NULL));
2153 VK_CALL(vkCmdBindPipeline(vk_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, vk_pipeline));
2154 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
2155 layout->vk_pipeline_layout, 0, 1, &vk_writes[0].dstSet, 0, NULL));
2156 VK_CALL(vkCmdDispatch(vk_command_buffer, group_count.x, group_count.y, group_count.z));
2158 vk_barrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
2159 vk_barrier.dstAccessMask = vk_access_mask_from_bind_flags(resource->bind_flags);
2161 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer,
2162 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
2163 vk_pipeline_stage_mask_from_bind_flags(resource->bind_flags),
2164 0, 1, &vk_barrier, 0, NULL, 0, NULL));
2166 context_invalidate_compute_state(&context_vk->c, STATE_COMPUTE_SHADER);
2168 out:
2169 if (format_id != view_format->id)
2171 if (resource->type == WINED3D_RTYPE_BUFFER)
2172 wined3d_context_vk_destroy_vk_buffer_view(context_vk, vk_buffer_view, view_vk->view_vk.command_buffer_id);
2173 else
2174 wined3d_context_vk_destroy_vk_image_view(context_vk, vk_image_info.imageView,
2175 view_vk->view_vk.command_buffer_id);
2179 void wined3d_unordered_access_view_vk_update(struct wined3d_unordered_access_view_vk *uav_vk,
2180 struct wined3d_context_vk *context_vk)
2182 const struct wined3d_format_vk *view_format_vk = wined3d_format_vk(uav_vk->v.format);
2183 const struct wined3d_view_desc *desc = &uav_vk->v.desc;
2184 struct wined3d_resource *resource = uav_vk->v.resource;
2185 struct wined3d_view_vk *view_vk = &uav_vk->view_vk;
2186 struct wined3d_buffer_vk *buffer_vk;
2187 VkBufferView vk_buffer_view;
2189 buffer_vk = wined3d_buffer_vk(buffer_from_resource(resource));
2190 wined3d_context_vk_destroy_vk_buffer_view(context_vk, view_vk->u.vk_buffer_view, view_vk->command_buffer_id);
2191 if ((vk_buffer_view = wined3d_view_vk_create_vk_buffer_view(context_vk, desc, buffer_vk, view_format_vk)))
2193 view_vk->u.vk_buffer_view = vk_buffer_view;
2194 view_vk->bo_user.valid = true;
2198 static void wined3d_unordered_access_view_vk_cs_init(void *object)
2200 struct wined3d_unordered_access_view_vk *uav_vk = object;
2201 struct wined3d_view_vk *view_vk = &uav_vk->view_vk;
2202 struct wined3d_view_desc *desc = &uav_vk->v.desc;
2203 const struct wined3d_format_vk *format_vk;
2204 const struct wined3d_vk_info *vk_info;
2205 struct wined3d_texture_vk *texture_vk;
2206 struct wined3d_context_vk *context_vk;
2207 struct wined3d_device_vk *device_vk;
2208 struct wined3d_buffer_vk *buffer_vk;
2209 VkBufferViewCreateInfo create_info;
2210 struct wined3d_resource *resource;
2211 VkBufferView vk_buffer_view;
2212 uint32_t default_flags = 0;
2213 VkImageView vk_image_view;
2214 VkResult vr;
2216 TRACE("uav_vk %p.\n", uav_vk);
2218 resource = uav_vk->v.resource;
2219 device_vk = wined3d_device_vk(resource->device);
2220 format_vk = wined3d_format_vk(uav_vk->v.format);
2222 if (resource->type == WINED3D_RTYPE_BUFFER)
2224 buffer_vk = wined3d_buffer_vk(buffer_from_resource(resource));
2226 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
2227 vk_info = context_vk->vk_info;
2229 if ((vk_buffer_view = wined3d_view_vk_create_vk_buffer_view(context_vk, desc, buffer_vk, format_vk)))
2231 struct wined3d_bo_vk *bo = wined3d_bo_vk(buffer_vk->b.buffer_object);
2233 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view));
2235 uav_vk->view_vk.u.vk_buffer_view = vk_buffer_view;
2236 uav_vk->view_vk.bo_user.valid = true;
2237 list_add_head(&bo->b.users, &view_vk->bo_user.entry);
2240 if (desc->flags & (WINED3D_VIEW_BUFFER_COUNTER | WINED3D_VIEW_BUFFER_APPEND))
2242 if (!wined3d_context_vk_create_bo(context_vk, sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT
2243 | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT,
2244 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &uav_vk->counter_bo))
2246 ERR("Failed to create counter bo.\n");
2247 context_release(&context_vk->c);
2249 return;
2252 wined3d_context_vk_end_current_render_pass(context_vk);
2253 VK_CALL(vkCmdFillBuffer(wined3d_context_vk_get_command_buffer(context_vk),
2254 uav_vk->counter_bo.vk_buffer, uav_vk->counter_bo.b.buffer_offset, sizeof(uint32_t), 0));
2255 wined3d_context_vk_reference_bo(context_vk, &uav_vk->counter_bo);
2257 create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO;
2258 create_info.pNext = NULL;
2259 create_info.flags = 0;
2260 create_info.buffer = uav_vk->counter_bo.vk_buffer;
2261 create_info.format = VK_FORMAT_R32_UINT;
2262 create_info.offset = uav_vk->counter_bo.b.buffer_offset;
2263 create_info.range = sizeof(uint32_t);
2264 if ((vr = VK_CALL(vkCreateBufferView(device_vk->vk_device,
2265 &create_info, NULL, &uav_vk->vk_counter_view))) < 0)
2267 ERR("Failed to create counter buffer view, vr %s.\n", wined3d_debug_vkresult(vr));
2269 else
2271 TRACE("Created counter buffer view 0x%s.\n", wine_dbgstr_longlong(uav_vk->vk_counter_view));
2273 uav_vk->v.counter_bo = &uav_vk->counter_bo.b;
2277 context_release(&context_vk->c);
2279 return;
2282 texture_vk = wined3d_texture_vk(texture_from_resource(resource));
2284 if (texture_vk->t.layer_count > 1)
2285 default_flags |= WINED3D_VIEW_TEXTURE_ARRAY;
2287 if (resource->format->id == format_vk->f.id && desc->flags == default_flags
2288 && !desc->u.texture.level_idx && desc->u.texture.level_count == texture_vk->t.level_count
2289 && !desc->u.texture.layer_idx && desc->u.texture.layer_count == texture_vk->t.layer_count
2290 && !(resource->bind_flags & WINED3D_BIND_DEPTH_STENCIL) && resource->type != WINED3D_RTYPE_TEXTURE_3D)
2292 TRACE("Creating identity unordered access view.\n");
2293 return;
2296 if (texture_vk->t.swapchain && texture_vk->t.swapchain->state.desc.backbuffer_count > 1)
2297 FIXME("Swapchain unordered access views not supported.\n");
2299 context_vk = wined3d_context_vk(context_acquire(&device_vk->d, NULL, 0));
2300 vk_image_view = wined3d_view_vk_create_vk_image_view(context_vk, desc,
2301 texture_vk, format_vk, format_vk->f.color_fixup, false);
2302 context_release(&context_vk->c);
2304 if (!vk_image_view)
2305 return;
2307 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view));
2309 view_vk->u.vk_image_info.imageView = vk_image_view;
2310 view_vk->u.vk_image_info.sampler = VK_NULL_HANDLE;
2311 view_vk->u.vk_image_info.imageLayout = texture_vk->layout;
2314 HRESULT wined3d_unordered_access_view_vk_init(struct wined3d_unordered_access_view_vk *view_vk,
2315 const struct wined3d_view_desc *desc, struct wined3d_resource *resource,
2316 void *parent, const struct wined3d_parent_ops *parent_ops)
2318 HRESULT hr;
2320 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
2321 view_vk, wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops);
2323 if (FAILED(hr = wined3d_unordered_access_view_init(&view_vk->v, desc, resource, parent, parent_ops)))
2324 return hr;
2326 list_init(&view_vk->view_vk.bo_user.entry);
2327 wined3d_cs_init_object(resource->device->cs, wined3d_unordered_access_view_vk_cs_init, view_vk);
2329 return hr;
2332 HRESULT CDECL wined3d_unordered_access_view_create(const struct wined3d_view_desc *desc,
2333 struct wined3d_resource *resource, void *parent, const struct wined3d_parent_ops *parent_ops,
2334 struct wined3d_unordered_access_view **view)
2336 const struct wined3d_adapter_ops *adapter_ops;
2338 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
2339 wined3d_debug_view_desc(desc, resource), resource, parent, parent_ops, view);
2341 adapter_ops = resource->device->adapter->adapter_ops;
2342 return adapter_ops->adapter_create_unordered_access_view(desc, resource, parent, parent_ops, view);